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