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