]> git.ipfire.org Git - people/arne_f/kernel.git/blob - drivers/md/bcache/sysfs.c
d9481640b3e116b2ac4077ece7e94bca1c36c42e
[people/arne_f/kernel.git] / drivers / md / bcache / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * bcache sysfs interfaces
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9 #include "bcache.h"
10 #include "sysfs.h"
11 #include "btree.h"
12 #include "request.h"
13 #include "writeback.h"
14
15 #include <linux/blkdev.h>
16 #include <linux/sort.h>
17 #include <linux/sched/clock.h>
18
19 /* Default is -1; we skip past it for struct cached_dev's cache mode */
20 static const char * const bch_cache_modes[] = {
21 "writethrough",
22 "writeback",
23 "writearound",
24 "none",
25 NULL
26 };
27
28 /* Default is -1; we skip past it for stop_when_cache_set_failed */
29 static const char * const bch_stop_on_failure_modes[] = {
30 "auto",
31 "always",
32 NULL
33 };
34
35 static const char * const cache_replacement_policies[] = {
36 "lru",
37 "fifo",
38 "random",
39 NULL
40 };
41
42 static const char * const error_actions[] = {
43 "unregister",
44 "panic",
45 NULL
46 };
47
48 write_attribute(attach);
49 write_attribute(detach);
50 write_attribute(unregister);
51 write_attribute(stop);
52 write_attribute(clear_stats);
53 write_attribute(trigger_gc);
54 write_attribute(prune_cache);
55 write_attribute(flash_vol_create);
56
57 read_attribute(bucket_size);
58 read_attribute(block_size);
59 read_attribute(nbuckets);
60 read_attribute(tree_depth);
61 read_attribute(root_usage_percent);
62 read_attribute(priority_stats);
63 read_attribute(btree_cache_size);
64 read_attribute(btree_cache_max_chain);
65 read_attribute(cache_available_percent);
66 read_attribute(written);
67 read_attribute(btree_written);
68 read_attribute(metadata_written);
69 read_attribute(active_journal_entries);
70
71 sysfs_time_stats_attribute(btree_gc, sec, ms);
72 sysfs_time_stats_attribute(btree_split, sec, us);
73 sysfs_time_stats_attribute(btree_sort, ms, us);
74 sysfs_time_stats_attribute(btree_read, ms, us);
75
76 read_attribute(btree_nodes);
77 read_attribute(btree_used_percent);
78 read_attribute(average_key_size);
79 read_attribute(dirty_data);
80 read_attribute(bset_tree_stats);
81
82 read_attribute(state);
83 read_attribute(cache_read_races);
84 read_attribute(reclaim);
85 read_attribute(flush_write);
86 read_attribute(retry_flush_write);
87 read_attribute(writeback_keys_done);
88 read_attribute(writeback_keys_failed);
89 read_attribute(io_errors);
90 read_attribute(congested);
91 rw_attribute(congested_read_threshold_us);
92 rw_attribute(congested_write_threshold_us);
93
94 rw_attribute(sequential_cutoff);
95 rw_attribute(data_csum);
96 rw_attribute(cache_mode);
97 rw_attribute(stop_when_cache_set_failed);
98 rw_attribute(writeback_metadata);
99 rw_attribute(writeback_running);
100 rw_attribute(writeback_percent);
101 rw_attribute(writeback_delay);
102 rw_attribute(writeback_rate);
103
104 rw_attribute(writeback_rate_update_seconds);
105 rw_attribute(writeback_rate_i_term_inverse);
106 rw_attribute(writeback_rate_p_term_inverse);
107 rw_attribute(writeback_rate_minimum);
108 read_attribute(writeback_rate_debug);
109
110 read_attribute(stripe_size);
111 read_attribute(partial_stripes_expensive);
112
113 rw_attribute(synchronous);
114 rw_attribute(journal_delay_ms);
115 rw_attribute(io_disable);
116 rw_attribute(discard);
117 rw_attribute(running);
118 rw_attribute(label);
119 rw_attribute(readahead);
120 rw_attribute(errors);
121 rw_attribute(io_error_limit);
122 rw_attribute(io_error_halflife);
123 rw_attribute(verify);
124 rw_attribute(bypass_torture_test);
125 rw_attribute(key_merging_disabled);
126 rw_attribute(gc_always_rewrite);
127 rw_attribute(expensive_debug_checks);
128 rw_attribute(cache_replacement_policy);
129 rw_attribute(btree_shrinker_disabled);
130 rw_attribute(copy_gc_enabled);
131 rw_attribute(size);
132
133 static ssize_t bch_snprint_string_list(char *buf,
134 size_t size,
135 const char * const list[],
136 size_t selected)
137 {
138 char *out = buf;
139 size_t i;
140
141 for (i = 0; list[i]; i++)
142 out += snprintf(out, buf + size - out,
143 i == selected ? "[%s] " : "%s ", list[i]);
144
145 out[-1] = '\n';
146 return out - buf;
147 }
148
149 SHOW(__bch_cached_dev)
150 {
151 struct cached_dev *dc = container_of(kobj, struct cached_dev,
152 disk.kobj);
153 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
154 int wb = dc->writeback_running;
155
156 #define var(stat) (dc->stat)
157
158 if (attr == &sysfs_cache_mode)
159 return bch_snprint_string_list(buf, PAGE_SIZE,
160 bch_cache_modes,
161 BDEV_CACHE_MODE(&dc->sb));
162
163 if (attr == &sysfs_stop_when_cache_set_failed)
164 return bch_snprint_string_list(buf, PAGE_SIZE,
165 bch_stop_on_failure_modes,
166 dc->stop_when_cache_set_failed);
167
168
169 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
170 var_printf(verify, "%i");
171 var_printf(bypass_torture_test, "%i");
172 var_printf(writeback_metadata, "%i");
173 var_printf(writeback_running, "%i");
174 var_print(writeback_delay);
175 var_print(writeback_percent);
176 sysfs_hprint(writeback_rate,
177 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
178 sysfs_hprint(io_errors, atomic_read(&dc->io_errors));
179 sysfs_printf(io_error_limit, "%i", dc->error_limit);
180 sysfs_printf(io_disable, "%i", dc->io_disable);
181 var_print(writeback_rate_update_seconds);
182 var_print(writeback_rate_i_term_inverse);
183 var_print(writeback_rate_p_term_inverse);
184 var_print(writeback_rate_minimum);
185
186 if (attr == &sysfs_writeback_rate_debug) {
187 char rate[20];
188 char dirty[20];
189 char target[20];
190 char proportional[20];
191 char integral[20];
192 char change[20];
193 s64 next_io;
194
195 /*
196 * Except for dirty and target, other values should
197 * be 0 if writeback is not running.
198 */
199 bch_hprint(rate,
200 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
201 : 0);
202 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
203 bch_hprint(target, dc->writeback_rate_target << 9);
204 bch_hprint(proportional,
205 wb ? dc->writeback_rate_proportional << 9 : 0);
206 bch_hprint(integral,
207 wb ? dc->writeback_rate_integral_scaled << 9 : 0);
208 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
209 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
210 NSEC_PER_MSEC) : 0;
211
212 return sprintf(buf,
213 "rate:\t\t%s/sec\n"
214 "dirty:\t\t%s\n"
215 "target:\t\t%s\n"
216 "proportional:\t%s\n"
217 "integral:\t%s\n"
218 "change:\t\t%s/sec\n"
219 "next io:\t%llims\n",
220 rate, dirty, target, proportional,
221 integral, change, next_io);
222 }
223
224 sysfs_hprint(dirty_data,
225 bcache_dev_sectors_dirty(&dc->disk) << 9);
226
227 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
228 var_printf(partial_stripes_expensive, "%u");
229
230 var_hprint(sequential_cutoff);
231 var_hprint(readahead);
232
233 sysfs_print(running, atomic_read(&dc->running));
234 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
235
236 if (attr == &sysfs_label) {
237 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
238 buf[SB_LABEL_SIZE + 1] = '\0';
239 strcat(buf, "\n");
240 return strlen(buf);
241 }
242
243 #undef var
244 return 0;
245 }
246 SHOW_LOCKED(bch_cached_dev)
247
248 STORE(__cached_dev)
249 {
250 struct cached_dev *dc = container_of(kobj, struct cached_dev,
251 disk.kobj);
252 ssize_t v;
253 struct cache_set *c;
254 struct kobj_uevent_env *env;
255
256 #define d_strtoul(var) sysfs_strtoul(var, dc->var)
257 #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
258 #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
259
260 sysfs_strtoul(data_csum, dc->disk.data_csum);
261 d_strtoul(verify);
262 d_strtoul(bypass_torture_test);
263 d_strtoul(writeback_metadata);
264 d_strtoul(writeback_running);
265 d_strtoul(writeback_delay);
266
267 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
268
269 if (attr == &sysfs_writeback_rate) {
270 ssize_t ret;
271 long int v = atomic_long_read(&dc->writeback_rate.rate);
272
273 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
274
275 if (!ret) {
276 atomic_long_set(&dc->writeback_rate.rate, v);
277 ret = size;
278 }
279
280 return ret;
281 }
282
283 sysfs_strtoul_clamp(writeback_rate_update_seconds,
284 dc->writeback_rate_update_seconds,
285 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
286 sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
287 dc->writeback_rate_i_term_inverse,
288 1, UINT_MAX);
289 sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
290 dc->writeback_rate_p_term_inverse,
291 1, UINT_MAX);
292 d_strtoul_nonzero(writeback_rate_minimum);
293
294 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
295
296 if (attr == &sysfs_io_disable) {
297 int v = strtoul_or_return(buf);
298
299 dc->io_disable = v ? 1 : 0;
300 }
301
302 sysfs_strtoul_clamp(sequential_cutoff,
303 dc->sequential_cutoff,
304 0, UINT_MAX);
305 d_strtoi_h(readahead);
306
307 if (attr == &sysfs_clear_stats)
308 bch_cache_accounting_clear(&dc->accounting);
309
310 if (attr == &sysfs_running &&
311 strtoul_or_return(buf))
312 bch_cached_dev_run(dc);
313
314 if (attr == &sysfs_cache_mode) {
315 v = __sysfs_match_string(bch_cache_modes, -1, buf);
316 if (v < 0)
317 return v;
318
319 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
320 SET_BDEV_CACHE_MODE(&dc->sb, v);
321 bch_write_bdev_super(dc, NULL);
322 }
323 }
324
325 if (attr == &sysfs_stop_when_cache_set_failed) {
326 v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
327 if (v < 0)
328 return v;
329
330 dc->stop_when_cache_set_failed = v;
331 }
332
333 if (attr == &sysfs_label) {
334 if (size > SB_LABEL_SIZE)
335 return -EINVAL;
336 memcpy(dc->sb.label, buf, size);
337 if (size < SB_LABEL_SIZE)
338 dc->sb.label[size] = '\0';
339 if (size && dc->sb.label[size - 1] == '\n')
340 dc->sb.label[size - 1] = '\0';
341 bch_write_bdev_super(dc, NULL);
342 if (dc->disk.c) {
343 memcpy(dc->disk.c->uuids[dc->disk.id].label,
344 buf, SB_LABEL_SIZE);
345 bch_uuid_write(dc->disk.c);
346 }
347 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
348 if (!env)
349 return -ENOMEM;
350 add_uevent_var(env, "DRIVER=bcache");
351 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
352 add_uevent_var(env, "CACHED_LABEL=%s", buf);
353 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
354 KOBJ_CHANGE,
355 env->envp);
356 kfree(env);
357 }
358
359 if (attr == &sysfs_attach) {
360 uint8_t set_uuid[16];
361
362 if (bch_parse_uuid(buf, set_uuid) < 16)
363 return -EINVAL;
364
365 v = -ENOENT;
366 list_for_each_entry(c, &bch_cache_sets, list) {
367 v = bch_cached_dev_attach(dc, c, set_uuid);
368 if (!v)
369 return size;
370 }
371 if (v == -ENOENT)
372 pr_err("Can't attach %s: cache set not found", buf);
373 return v;
374 }
375
376 if (attr == &sysfs_detach && dc->disk.c)
377 bch_cached_dev_detach(dc);
378
379 if (attr == &sysfs_stop)
380 bcache_device_stop(&dc->disk);
381
382 return size;
383 }
384
385 STORE(bch_cached_dev)
386 {
387 struct cached_dev *dc = container_of(kobj, struct cached_dev,
388 disk.kobj);
389
390 mutex_lock(&bch_register_lock);
391 size = __cached_dev_store(kobj, attr, buf, size);
392
393 if (attr == &sysfs_writeback_running)
394 bch_writeback_queue(dc);
395
396 if (attr == &sysfs_writeback_percent)
397 if (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
398 schedule_delayed_work(&dc->writeback_rate_update,
399 dc->writeback_rate_update_seconds * HZ);
400
401 mutex_unlock(&bch_register_lock);
402 return size;
403 }
404
405 static struct attribute *bch_cached_dev_files[] = {
406 &sysfs_attach,
407 &sysfs_detach,
408 &sysfs_stop,
409 #if 0
410 &sysfs_data_csum,
411 #endif
412 &sysfs_cache_mode,
413 &sysfs_stop_when_cache_set_failed,
414 &sysfs_writeback_metadata,
415 &sysfs_writeback_running,
416 &sysfs_writeback_delay,
417 &sysfs_writeback_percent,
418 &sysfs_writeback_rate,
419 &sysfs_writeback_rate_update_seconds,
420 &sysfs_writeback_rate_i_term_inverse,
421 &sysfs_writeback_rate_p_term_inverse,
422 &sysfs_writeback_rate_minimum,
423 &sysfs_writeback_rate_debug,
424 &sysfs_errors,
425 &sysfs_io_error_limit,
426 &sysfs_io_disable,
427 &sysfs_dirty_data,
428 &sysfs_stripe_size,
429 &sysfs_partial_stripes_expensive,
430 &sysfs_sequential_cutoff,
431 &sysfs_clear_stats,
432 &sysfs_running,
433 &sysfs_state,
434 &sysfs_label,
435 &sysfs_readahead,
436 #ifdef CONFIG_BCACHE_DEBUG
437 &sysfs_verify,
438 &sysfs_bypass_torture_test,
439 #endif
440 NULL
441 };
442 KTYPE(bch_cached_dev);
443
444 SHOW(bch_flash_dev)
445 {
446 struct bcache_device *d = container_of(kobj, struct bcache_device,
447 kobj);
448 struct uuid_entry *u = &d->c->uuids[d->id];
449
450 sysfs_printf(data_csum, "%i", d->data_csum);
451 sysfs_hprint(size, u->sectors << 9);
452
453 if (attr == &sysfs_label) {
454 memcpy(buf, u->label, SB_LABEL_SIZE);
455 buf[SB_LABEL_SIZE + 1] = '\0';
456 strcat(buf, "\n");
457 return strlen(buf);
458 }
459
460 return 0;
461 }
462
463 STORE(__bch_flash_dev)
464 {
465 struct bcache_device *d = container_of(kobj, struct bcache_device,
466 kobj);
467 struct uuid_entry *u = &d->c->uuids[d->id];
468
469 sysfs_strtoul(data_csum, d->data_csum);
470
471 if (attr == &sysfs_size) {
472 uint64_t v;
473
474 strtoi_h_or_return(buf, v);
475
476 u->sectors = v >> 9;
477 bch_uuid_write(d->c);
478 set_capacity(d->disk, u->sectors);
479 }
480
481 if (attr == &sysfs_label) {
482 memcpy(u->label, buf, SB_LABEL_SIZE);
483 bch_uuid_write(d->c);
484 }
485
486 if (attr == &sysfs_unregister) {
487 set_bit(BCACHE_DEV_DETACHING, &d->flags);
488 bcache_device_stop(d);
489 }
490
491 return size;
492 }
493 STORE_LOCKED(bch_flash_dev)
494
495 static struct attribute *bch_flash_dev_files[] = {
496 &sysfs_unregister,
497 #if 0
498 &sysfs_data_csum,
499 #endif
500 &sysfs_label,
501 &sysfs_size,
502 NULL
503 };
504 KTYPE(bch_flash_dev);
505
506 struct bset_stats_op {
507 struct btree_op op;
508 size_t nodes;
509 struct bset_stats stats;
510 };
511
512 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
513 {
514 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
515
516 op->nodes++;
517 bch_btree_keys_stats(&b->keys, &op->stats);
518
519 return MAP_CONTINUE;
520 }
521
522 static int bch_bset_print_stats(struct cache_set *c, char *buf)
523 {
524 struct bset_stats_op op;
525 int ret;
526
527 memset(&op, 0, sizeof(op));
528 bch_btree_op_init(&op.op, -1);
529
530 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
531 if (ret < 0)
532 return ret;
533
534 return snprintf(buf, PAGE_SIZE,
535 "btree nodes: %zu\n"
536 "written sets: %zu\n"
537 "unwritten sets: %zu\n"
538 "written key bytes: %zu\n"
539 "unwritten key bytes: %zu\n"
540 "floats: %zu\n"
541 "failed: %zu\n",
542 op.nodes,
543 op.stats.sets_written, op.stats.sets_unwritten,
544 op.stats.bytes_written, op.stats.bytes_unwritten,
545 op.stats.floats, op.stats.failed);
546 }
547
548 static unsigned int bch_root_usage(struct cache_set *c)
549 {
550 unsigned int bytes = 0;
551 struct bkey *k;
552 struct btree *b;
553 struct btree_iter iter;
554
555 goto lock_root;
556
557 do {
558 rw_unlock(false, b);
559 lock_root:
560 b = c->root;
561 rw_lock(false, b, b->level);
562 } while (b != c->root);
563
564 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
565 bytes += bkey_bytes(k);
566
567 rw_unlock(false, b);
568
569 return (bytes * 100) / btree_bytes(c);
570 }
571
572 static size_t bch_cache_size(struct cache_set *c)
573 {
574 size_t ret = 0;
575 struct btree *b;
576
577 mutex_lock(&c->bucket_lock);
578 list_for_each_entry(b, &c->btree_cache, list)
579 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
580
581 mutex_unlock(&c->bucket_lock);
582 return ret;
583 }
584
585 static unsigned int bch_cache_max_chain(struct cache_set *c)
586 {
587 unsigned int ret = 0;
588 struct hlist_head *h;
589
590 mutex_lock(&c->bucket_lock);
591
592 for (h = c->bucket_hash;
593 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
594 h++) {
595 unsigned int i = 0;
596 struct hlist_node *p;
597
598 hlist_for_each(p, h)
599 i++;
600
601 ret = max(ret, i);
602 }
603
604 mutex_unlock(&c->bucket_lock);
605 return ret;
606 }
607
608 static unsigned int bch_btree_used(struct cache_set *c)
609 {
610 return div64_u64(c->gc_stats.key_bytes * 100,
611 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
612 }
613
614 static unsigned int bch_average_key_size(struct cache_set *c)
615 {
616 return c->gc_stats.nkeys
617 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
618 : 0;
619 }
620
621 SHOW(__bch_cache_set)
622 {
623 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
624
625 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
626 sysfs_print(journal_delay_ms, c->journal_delay_ms);
627 sysfs_hprint(bucket_size, bucket_bytes(c));
628 sysfs_hprint(block_size, block_bytes(c));
629 sysfs_print(tree_depth, c->root->level);
630 sysfs_print(root_usage_percent, bch_root_usage(c));
631
632 sysfs_hprint(btree_cache_size, bch_cache_size(c));
633 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
634 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
635
636 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
637 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
638 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
639 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
640
641 sysfs_print(btree_used_percent, bch_btree_used(c));
642 sysfs_print(btree_nodes, c->gc_stats.nodes);
643 sysfs_hprint(average_key_size, bch_average_key_size(c));
644
645 sysfs_print(cache_read_races,
646 atomic_long_read(&c->cache_read_races));
647
648 sysfs_print(reclaim,
649 atomic_long_read(&c->reclaim));
650
651 sysfs_print(flush_write,
652 atomic_long_read(&c->flush_write));
653
654 sysfs_print(retry_flush_write,
655 atomic_long_read(&c->retry_flush_write));
656
657 sysfs_print(writeback_keys_done,
658 atomic_long_read(&c->writeback_keys_done));
659 sysfs_print(writeback_keys_failed,
660 atomic_long_read(&c->writeback_keys_failed));
661
662 if (attr == &sysfs_errors)
663 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
664 c->on_error);
665
666 /* See count_io_errors for why 88 */
667 sysfs_print(io_error_halflife, c->error_decay * 88);
668 sysfs_print(io_error_limit, c->error_limit);
669
670 sysfs_hprint(congested,
671 ((uint64_t) bch_get_congested(c)) << 9);
672 sysfs_print(congested_read_threshold_us,
673 c->congested_read_threshold_us);
674 sysfs_print(congested_write_threshold_us,
675 c->congested_write_threshold_us);
676
677 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
678 sysfs_printf(verify, "%i", c->verify);
679 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
680 sysfs_printf(expensive_debug_checks,
681 "%i", c->expensive_debug_checks);
682 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
683 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
684 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
685 sysfs_printf(io_disable, "%i",
686 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
687
688 if (attr == &sysfs_bset_tree_stats)
689 return bch_bset_print_stats(c, buf);
690
691 return 0;
692 }
693 SHOW_LOCKED(bch_cache_set)
694
695 STORE(__bch_cache_set)
696 {
697 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
698 ssize_t v;
699
700 if (attr == &sysfs_unregister)
701 bch_cache_set_unregister(c);
702
703 if (attr == &sysfs_stop)
704 bch_cache_set_stop(c);
705
706 if (attr == &sysfs_synchronous) {
707 bool sync = strtoul_or_return(buf);
708
709 if (sync != CACHE_SYNC(&c->sb)) {
710 SET_CACHE_SYNC(&c->sb, sync);
711 bcache_write_super(c);
712 }
713 }
714
715 if (attr == &sysfs_flash_vol_create) {
716 int r;
717 uint64_t v;
718
719 strtoi_h_or_return(buf, v);
720
721 r = bch_flash_dev_create(c, v);
722 if (r)
723 return r;
724 }
725
726 if (attr == &sysfs_clear_stats) {
727 atomic_long_set(&c->writeback_keys_done, 0);
728 atomic_long_set(&c->writeback_keys_failed, 0);
729
730 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
731 bch_cache_accounting_clear(&c->accounting);
732 }
733
734 if (attr == &sysfs_trigger_gc) {
735 /*
736 * Garbage collection thread only works when sectors_to_gc < 0,
737 * when users write to sysfs entry trigger_gc, most of time
738 * they want to forcibly triger gargage collection. Here -1 is
739 * set to c->sectors_to_gc, to make gc_should_run() give a
740 * chance to permit gc thread to run. "give a chance" means
741 * before going into gc_should_run(), there is still chance
742 * that c->sectors_to_gc being set to other positive value. So
743 * writing sysfs entry trigger_gc won't always make sure gc
744 * thread takes effect.
745 */
746 atomic_set(&c->sectors_to_gc, -1);
747 wake_up_gc(c);
748 }
749
750 if (attr == &sysfs_prune_cache) {
751 struct shrink_control sc;
752
753 sc.gfp_mask = GFP_KERNEL;
754 sc.nr_to_scan = strtoul_or_return(buf);
755 c->shrink.scan_objects(&c->shrink, &sc);
756 }
757
758 sysfs_strtoul(congested_read_threshold_us,
759 c->congested_read_threshold_us);
760 sysfs_strtoul(congested_write_threshold_us,
761 c->congested_write_threshold_us);
762
763 if (attr == &sysfs_errors) {
764 v = __sysfs_match_string(error_actions, -1, buf);
765 if (v < 0)
766 return v;
767
768 c->on_error = v;
769 }
770
771 if (attr == &sysfs_io_error_limit)
772 c->error_limit = strtoul_or_return(buf);
773
774 /* See count_io_errors() for why 88 */
775 if (attr == &sysfs_io_error_halflife) {
776 unsigned long v = 0;
777 ssize_t ret;
778
779 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
780 if (!ret) {
781 c->error_decay = v / 88;
782 return size;
783 }
784 return ret;
785 }
786
787 if (attr == &sysfs_io_disable) {
788 v = strtoul_or_return(buf);
789 if (v) {
790 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
791 &c->flags))
792 pr_warn("CACHE_SET_IO_DISABLE already set");
793 } else {
794 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
795 &c->flags))
796 pr_warn("CACHE_SET_IO_DISABLE already cleared");
797 }
798 }
799
800 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
801 sysfs_strtoul(verify, c->verify);
802 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
803 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
804 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
805 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
806 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
807
808 return size;
809 }
810 STORE_LOCKED(bch_cache_set)
811
812 SHOW(bch_cache_set_internal)
813 {
814 struct cache_set *c = container_of(kobj, struct cache_set, internal);
815
816 return bch_cache_set_show(&c->kobj, attr, buf);
817 }
818
819 STORE(bch_cache_set_internal)
820 {
821 struct cache_set *c = container_of(kobj, struct cache_set, internal);
822
823 return bch_cache_set_store(&c->kobj, attr, buf, size);
824 }
825
826 static void bch_cache_set_internal_release(struct kobject *k)
827 {
828 }
829
830 static struct attribute *bch_cache_set_files[] = {
831 &sysfs_unregister,
832 &sysfs_stop,
833 &sysfs_synchronous,
834 &sysfs_journal_delay_ms,
835 &sysfs_flash_vol_create,
836
837 &sysfs_bucket_size,
838 &sysfs_block_size,
839 &sysfs_tree_depth,
840 &sysfs_root_usage_percent,
841 &sysfs_btree_cache_size,
842 &sysfs_cache_available_percent,
843
844 &sysfs_average_key_size,
845
846 &sysfs_errors,
847 &sysfs_io_error_limit,
848 &sysfs_io_error_halflife,
849 &sysfs_congested,
850 &sysfs_congested_read_threshold_us,
851 &sysfs_congested_write_threshold_us,
852 &sysfs_clear_stats,
853 NULL
854 };
855 KTYPE(bch_cache_set);
856
857 static struct attribute *bch_cache_set_internal_files[] = {
858 &sysfs_active_journal_entries,
859
860 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
861 sysfs_time_stats_attribute_list(btree_split, sec, us)
862 sysfs_time_stats_attribute_list(btree_sort, ms, us)
863 sysfs_time_stats_attribute_list(btree_read, ms, us)
864
865 &sysfs_btree_nodes,
866 &sysfs_btree_used_percent,
867 &sysfs_btree_cache_max_chain,
868
869 &sysfs_bset_tree_stats,
870 &sysfs_cache_read_races,
871 &sysfs_reclaim,
872 &sysfs_flush_write,
873 &sysfs_retry_flush_write,
874 &sysfs_writeback_keys_done,
875 &sysfs_writeback_keys_failed,
876
877 &sysfs_trigger_gc,
878 &sysfs_prune_cache,
879 #ifdef CONFIG_BCACHE_DEBUG
880 &sysfs_verify,
881 &sysfs_key_merging_disabled,
882 &sysfs_expensive_debug_checks,
883 #endif
884 &sysfs_gc_always_rewrite,
885 &sysfs_btree_shrinker_disabled,
886 &sysfs_copy_gc_enabled,
887 &sysfs_io_disable,
888 NULL
889 };
890 KTYPE(bch_cache_set_internal);
891
892 static int __bch_cache_cmp(const void *l, const void *r)
893 {
894 return *((uint16_t *)r) - *((uint16_t *)l);
895 }
896
897 SHOW(__bch_cache)
898 {
899 struct cache *ca = container_of(kobj, struct cache, kobj);
900
901 sysfs_hprint(bucket_size, bucket_bytes(ca));
902 sysfs_hprint(block_size, block_bytes(ca));
903 sysfs_print(nbuckets, ca->sb.nbuckets);
904 sysfs_print(discard, ca->discard);
905 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
906 sysfs_hprint(btree_written,
907 atomic_long_read(&ca->btree_sectors_written) << 9);
908 sysfs_hprint(metadata_written,
909 (atomic_long_read(&ca->meta_sectors_written) +
910 atomic_long_read(&ca->btree_sectors_written)) << 9);
911
912 sysfs_print(io_errors,
913 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
914
915 if (attr == &sysfs_cache_replacement_policy)
916 return bch_snprint_string_list(buf, PAGE_SIZE,
917 cache_replacement_policies,
918 CACHE_REPLACEMENT(&ca->sb));
919
920 if (attr == &sysfs_priority_stats) {
921 struct bucket *b;
922 size_t n = ca->sb.nbuckets, i;
923 size_t unused = 0, available = 0, dirty = 0, meta = 0;
924 uint64_t sum = 0;
925 /* Compute 31 quantiles */
926 uint16_t q[31], *p, *cached;
927 ssize_t ret;
928
929 cached = p = vmalloc(array_size(sizeof(uint16_t),
930 ca->sb.nbuckets));
931 if (!p)
932 return -ENOMEM;
933
934 mutex_lock(&ca->set->bucket_lock);
935 for_each_bucket(b, ca) {
936 if (!GC_SECTORS_USED(b))
937 unused++;
938 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
939 available++;
940 if (GC_MARK(b) == GC_MARK_DIRTY)
941 dirty++;
942 if (GC_MARK(b) == GC_MARK_METADATA)
943 meta++;
944 }
945
946 for (i = ca->sb.first_bucket; i < n; i++)
947 p[i] = ca->buckets[i].prio;
948 mutex_unlock(&ca->set->bucket_lock);
949
950 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
951
952 while (n &&
953 !cached[n - 1])
954 --n;
955
956 unused = ca->sb.nbuckets - n;
957
958 while (cached < p + n &&
959 *cached == BTREE_PRIO)
960 cached++, n--;
961
962 for (i = 0; i < n; i++)
963 sum += INITIAL_PRIO - cached[i];
964
965 if (n)
966 do_div(sum, n);
967
968 for (i = 0; i < ARRAY_SIZE(q); i++)
969 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
970 (ARRAY_SIZE(q) + 1)];
971
972 vfree(p);
973
974 ret = scnprintf(buf, PAGE_SIZE,
975 "Unused: %zu%%\n"
976 "Clean: %zu%%\n"
977 "Dirty: %zu%%\n"
978 "Metadata: %zu%%\n"
979 "Average: %llu\n"
980 "Sectors per Q: %zu\n"
981 "Quantiles: [",
982 unused * 100 / (size_t) ca->sb.nbuckets,
983 available * 100 / (size_t) ca->sb.nbuckets,
984 dirty * 100 / (size_t) ca->sb.nbuckets,
985 meta * 100 / (size_t) ca->sb.nbuckets, sum,
986 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
987
988 for (i = 0; i < ARRAY_SIZE(q); i++)
989 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
990 "%u ", q[i]);
991 ret--;
992
993 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
994
995 return ret;
996 }
997
998 return 0;
999 }
1000 SHOW_LOCKED(bch_cache)
1001
1002 STORE(__bch_cache)
1003 {
1004 struct cache *ca = container_of(kobj, struct cache, kobj);
1005 ssize_t v;
1006
1007 if (attr == &sysfs_discard) {
1008 bool v = strtoul_or_return(buf);
1009
1010 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1011 ca->discard = v;
1012
1013 if (v != CACHE_DISCARD(&ca->sb)) {
1014 SET_CACHE_DISCARD(&ca->sb, v);
1015 bcache_write_super(ca->set);
1016 }
1017 }
1018
1019 if (attr == &sysfs_cache_replacement_policy) {
1020 v = __sysfs_match_string(cache_replacement_policies, -1, buf);
1021 if (v < 0)
1022 return v;
1023
1024 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
1025 mutex_lock(&ca->set->bucket_lock);
1026 SET_CACHE_REPLACEMENT(&ca->sb, v);
1027 mutex_unlock(&ca->set->bucket_lock);
1028
1029 bcache_write_super(ca->set);
1030 }
1031 }
1032
1033 if (attr == &sysfs_clear_stats) {
1034 atomic_long_set(&ca->sectors_written, 0);
1035 atomic_long_set(&ca->btree_sectors_written, 0);
1036 atomic_long_set(&ca->meta_sectors_written, 0);
1037 atomic_set(&ca->io_count, 0);
1038 atomic_set(&ca->io_errors, 0);
1039 }
1040
1041 return size;
1042 }
1043 STORE_LOCKED(bch_cache)
1044
1045 static struct attribute *bch_cache_files[] = {
1046 &sysfs_bucket_size,
1047 &sysfs_block_size,
1048 &sysfs_nbuckets,
1049 &sysfs_priority_stats,
1050 &sysfs_discard,
1051 &sysfs_written,
1052 &sysfs_btree_written,
1053 &sysfs_metadata_written,
1054 &sysfs_io_errors,
1055 &sysfs_clear_stats,
1056 &sysfs_cache_replacement_policy,
1057 NULL
1058 };
1059 KTYPE(bch_cache);