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