blob: f90f1361698080654ffe41778a4c637cbd40898b [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache sysfs interfaces
3 *
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
6 */
7
8#include "bcache.h"
9#include "sysfs.h"
10#include "btree.h"
11#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070012#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070013
Kent Overstreetc37511b2013-04-26 15:39:55 -070014#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070015#include <linux/sort.h>
Ingo Molnare6017572017-02-01 16:36:40 +010016#include <linux/sched/clock.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070017
18static const char * const cache_replacement_policies[] = {
19 "lru",
20 "fifo",
21 "random",
22 NULL
23};
24
Kent Overstreet77c320e2013-07-11 19:42:51 -070025static const char * const error_actions[] = {
26 "unregister",
27 "panic",
28 NULL
29};
30
Kent Overstreetcafe5632013-03-23 16:11:31 -070031write_attribute(attach);
32write_attribute(detach);
33write_attribute(unregister);
34write_attribute(stop);
35write_attribute(clear_stats);
36write_attribute(trigger_gc);
37write_attribute(prune_cache);
38write_attribute(flash_vol_create);
39
40read_attribute(bucket_size);
41read_attribute(block_size);
42read_attribute(nbuckets);
43read_attribute(tree_depth);
44read_attribute(root_usage_percent);
45read_attribute(priority_stats);
46read_attribute(btree_cache_size);
47read_attribute(btree_cache_max_chain);
48read_attribute(cache_available_percent);
49read_attribute(written);
50read_attribute(btree_written);
51read_attribute(metadata_written);
52read_attribute(active_journal_entries);
53
54sysfs_time_stats_attribute(btree_gc, sec, ms);
55sysfs_time_stats_attribute(btree_split, sec, us);
56sysfs_time_stats_attribute(btree_sort, ms, us);
57sysfs_time_stats_attribute(btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -070058
59read_attribute(btree_nodes);
60read_attribute(btree_used_percent);
61read_attribute(average_key_size);
62read_attribute(dirty_data);
63read_attribute(bset_tree_stats);
64
65read_attribute(state);
66read_attribute(cache_read_races);
67read_attribute(writeback_keys_done);
68read_attribute(writeback_keys_failed);
69read_attribute(io_errors);
70read_attribute(congested);
71rw_attribute(congested_read_threshold_us);
72rw_attribute(congested_write_threshold_us);
73
74rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -070075rw_attribute(data_csum);
76rw_attribute(cache_mode);
77rw_attribute(writeback_metadata);
78rw_attribute(writeback_running);
79rw_attribute(writeback_percent);
80rw_attribute(writeback_delay);
81rw_attribute(writeback_rate);
82
83rw_attribute(writeback_rate_update_seconds);
84rw_attribute(writeback_rate_d_term);
85rw_attribute(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070086read_attribute(writeback_rate_debug);
87
Kent Overstreet72c27062013-06-05 06:24:39 -070088read_attribute(stripe_size);
89read_attribute(partial_stripes_expensive);
90
Kent Overstreetcafe5632013-03-23 16:11:31 -070091rw_attribute(synchronous);
92rw_attribute(journal_delay_ms);
93rw_attribute(discard);
94rw_attribute(running);
95rw_attribute(label);
96rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -070097rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -070098rw_attribute(io_error_limit);
99rw_attribute(io_error_halflife);
100rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700101rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700102rw_attribute(key_merging_disabled);
103rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700104rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700105rw_attribute(cache_replacement_policy);
106rw_attribute(btree_shrinker_disabled);
107rw_attribute(copy_gc_enabled);
108rw_attribute(size);
109
110SHOW(__bch_cached_dev)
111{
112 struct cached_dev *dc = container_of(kobj, struct cached_dev,
113 disk.kobj);
114 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
115
116#define var(stat) (dc->stat)
117
118 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600119 return bch_snprint_string_list(buf, PAGE_SIZE,
120 bch_cache_modes + 1,
121 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700122
123 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
124 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700125 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700126 var_printf(writeback_metadata, "%i");
127 var_printf(writeback_running, "%i");
128 var_print(writeback_delay);
129 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800130 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700131
132 var_print(writeback_rate_update_seconds);
133 var_print(writeback_rate_d_term);
134 var_print(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135
136 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800137 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700138 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800140 char proportional[20];
141 char derivative[20];
142 char change[20];
143 s64 next_io;
144
145 bch_hprint(rate, dc->writeback_rate.rate << 9);
146 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600147 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800148 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
149 bch_hprint(derivative, dc->writeback_rate_derivative << 9);
150 bch_hprint(change, dc->writeback_rate_change << 9);
151
152 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
153 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700154
155 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800156 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700157 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800158 "target:\t\t%s\n"
159 "proportional:\t%s\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700160 "derivative:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800161 "change:\t\t%s/sec\n"
162 "next io:\t%llims\n",
163 rate, dirty, target, proportional,
164 derivative, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700165 }
166
167 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700168 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700169
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700170 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700171 var_printf(partial_stripes_expensive, "%u");
172
Kent Overstreetcafe5632013-03-23 16:11:31 -0700173 var_hprint(sequential_cutoff);
174 var_hprint(readahead);
175
176 sysfs_print(running, atomic_read(&dc->running));
177 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
178
179 if (attr == &sysfs_label) {
180 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
181 buf[SB_LABEL_SIZE + 1] = '\0';
182 strcat(buf, "\n");
183 return strlen(buf);
184 }
185
186#undef var
187 return 0;
188}
189SHOW_LOCKED(bch_cached_dev)
190
191STORE(__cached_dev)
192{
193 struct cached_dev *dc = container_of(kobj, struct cached_dev,
194 disk.kobj);
195 unsigned v = size;
196 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200197 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198
199#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800200#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
202
203 sysfs_strtoul(data_csum, dc->disk.data_csum);
204 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700205 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700206 d_strtoul(writeback_metadata);
207 d_strtoul(writeback_running);
208 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800209
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
211
Kent Overstreet16749c22013-11-11 13:58:34 -0800212 sysfs_strtoul_clamp(writeback_rate,
213 dc->writeback_rate.rate, 1, INT_MAX);
214
215 d_strtoul_nonzero(writeback_rate_update_seconds);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 d_strtoul(writeback_rate_d_term);
Kent Overstreet16749c22013-11-11 13:58:34 -0800217 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 d_strtoi_h(sequential_cutoff);
220 d_strtoi_h(readahead);
221
222 if (attr == &sysfs_clear_stats)
223 bch_cache_accounting_clear(&dc->accounting);
224
225 if (attr == &sysfs_running &&
226 strtoul_or_return(buf))
227 bch_cached_dev_run(dc);
228
229 if (attr == &sysfs_cache_mode) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600230 ssize_t v = bch_read_string_list(buf, bch_cache_modes + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700231
232 if (v < 0)
233 return v;
234
235 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
236 SET_BDEV_CACHE_MODE(&dc->sb, v);
237 bch_write_bdev_super(dc, NULL);
238 }
239 }
240
241 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700242 if (size > SB_LABEL_SIZE)
243 return -EINVAL;
244 memcpy(dc->sb.label, buf, size);
245 if (size < SB_LABEL_SIZE)
246 dc->sb.label[size] = '\0';
247 if (size && dc->sb.label[size - 1] == '\n')
248 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700249 bch_write_bdev_super(dc, NULL);
250 if (dc->disk.c) {
251 memcpy(dc->disk.c->uuids[dc->disk.id].label,
252 buf, SB_LABEL_SIZE);
253 bch_uuid_write(dc->disk.c);
254 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200255 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300256 if (!env)
257 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200258 add_uevent_var(env, "DRIVER=bcache");
259 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
260 add_uevent_var(env, "CACHED_LABEL=%s", buf);
261 kobject_uevent_env(
262 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
263 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700264 }
265
266 if (attr == &sysfs_attach) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600267 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700268 return -EINVAL;
269
270 list_for_each_entry(c, &bch_cache_sets, list) {
271 v = bch_cached_dev_attach(dc, c);
272 if (!v)
273 return size;
274 }
275
276 pr_err("Can't attach %s: cache set not found", buf);
277 size = v;
278 }
279
280 if (attr == &sysfs_detach && dc->disk.c)
281 bch_cached_dev_detach(dc);
282
283 if (attr == &sysfs_stop)
284 bcache_device_stop(&dc->disk);
285
286 return size;
287}
288
289STORE(bch_cached_dev)
290{
291 struct cached_dev *dc = container_of(kobj, struct cached_dev,
292 disk.kobj);
293
294 mutex_lock(&bch_register_lock);
295 size = __cached_dev_store(kobj, attr, buf, size);
296
297 if (attr == &sysfs_writeback_running)
298 bch_writeback_queue(dc);
299
300 if (attr == &sysfs_writeback_percent)
301 schedule_delayed_work(&dc->writeback_rate_update,
302 dc->writeback_rate_update_seconds * HZ);
303
304 mutex_unlock(&bch_register_lock);
305 return size;
306}
307
308static struct attribute *bch_cached_dev_files[] = {
309 &sysfs_attach,
310 &sysfs_detach,
311 &sysfs_stop,
312#if 0
313 &sysfs_data_csum,
314#endif
315 &sysfs_cache_mode,
316 &sysfs_writeback_metadata,
317 &sysfs_writeback_running,
318 &sysfs_writeback_delay,
319 &sysfs_writeback_percent,
320 &sysfs_writeback_rate,
321 &sysfs_writeback_rate_update_seconds,
322 &sysfs_writeback_rate_d_term,
323 &sysfs_writeback_rate_p_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700324 &sysfs_writeback_rate_debug,
325 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700326 &sysfs_stripe_size,
327 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700328 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700329 &sysfs_clear_stats,
330 &sysfs_running,
331 &sysfs_state,
332 &sysfs_label,
333 &sysfs_readahead,
334#ifdef CONFIG_BCACHE_DEBUG
335 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700336 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337#endif
338 NULL
339};
340KTYPE(bch_cached_dev);
341
342SHOW(bch_flash_dev)
343{
344 struct bcache_device *d = container_of(kobj, struct bcache_device,
345 kobj);
346 struct uuid_entry *u = &d->c->uuids[d->id];
347
348 sysfs_printf(data_csum, "%i", d->data_csum);
349 sysfs_hprint(size, u->sectors << 9);
350
351 if (attr == &sysfs_label) {
352 memcpy(buf, u->label, SB_LABEL_SIZE);
353 buf[SB_LABEL_SIZE + 1] = '\0';
354 strcat(buf, "\n");
355 return strlen(buf);
356 }
357
358 return 0;
359}
360
361STORE(__bch_flash_dev)
362{
363 struct bcache_device *d = container_of(kobj, struct bcache_device,
364 kobj);
365 struct uuid_entry *u = &d->c->uuids[d->id];
366
367 sysfs_strtoul(data_csum, d->data_csum);
368
369 if (attr == &sysfs_size) {
370 uint64_t v;
371 strtoi_h_or_return(buf, v);
372
373 u->sectors = v >> 9;
374 bch_uuid_write(d->c);
375 set_capacity(d->disk, u->sectors);
376 }
377
378 if (attr == &sysfs_label) {
379 memcpy(u->label, buf, SB_LABEL_SIZE);
380 bch_uuid_write(d->c);
381 }
382
383 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700384 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700385 bcache_device_stop(d);
386 }
387
388 return size;
389}
390STORE_LOCKED(bch_flash_dev)
391
392static struct attribute *bch_flash_dev_files[] = {
393 &sysfs_unregister,
394#if 0
395 &sysfs_data_csum,
396#endif
397 &sysfs_label,
398 &sysfs_size,
399 NULL
400};
401KTYPE(bch_flash_dev);
402
Kent Overstreetf67342d2013-11-11 19:25:55 -0800403struct bset_stats_op {
404 struct btree_op op;
405 size_t nodes;
406 struct bset_stats stats;
407};
408
John Sheucb851142014-03-17 23:13:56 -0700409static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800410{
411 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
412
413 op->nodes++;
414 bch_btree_keys_stats(&b->keys, &op->stats);
415
416 return MAP_CONTINUE;
417}
418
Kent Overstreet35723242014-01-10 18:53:02 -0800419static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800420{
421 struct bset_stats_op op;
422 int ret;
423
424 memset(&op, 0, sizeof(op));
425 bch_btree_op_init(&op.op, -1);
426
John Sheucb851142014-03-17 23:13:56 -0700427 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800428 if (ret < 0)
429 return ret;
430
431 return snprintf(buf, PAGE_SIZE,
432 "btree nodes: %zu\n"
433 "written sets: %zu\n"
434 "unwritten sets: %zu\n"
435 "written key bytes: %zu\n"
436 "unwritten key bytes: %zu\n"
437 "floats: %zu\n"
438 "failed: %zu\n",
439 op.nodes,
440 op.stats.sets_written, op.stats.sets_unwritten,
441 op.stats.bytes_written, op.stats.bytes_unwritten,
442 op.stats.floats, op.stats.failed);
443}
444
John Sheucb851142014-03-17 23:13:56 -0700445static unsigned bch_root_usage(struct cache_set *c)
446{
447 unsigned bytes = 0;
448 struct bkey *k;
449 struct btree *b;
450 struct btree_iter iter;
451
452 goto lock_root;
453
454 do {
455 rw_unlock(false, b);
456lock_root:
457 b = c->root;
458 rw_lock(false, b, b->level);
459 } while (b != c->root);
460
461 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
462 bytes += bkey_bytes(k);
463
464 rw_unlock(false, b);
465
466 return (bytes * 100) / btree_bytes(c);
467}
468
469static size_t bch_cache_size(struct cache_set *c)
470{
471 size_t ret = 0;
472 struct btree *b;
473
474 mutex_lock(&c->bucket_lock);
475 list_for_each_entry(b, &c->btree_cache, list)
476 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
477
478 mutex_unlock(&c->bucket_lock);
479 return ret;
480}
481
482static unsigned bch_cache_max_chain(struct cache_set *c)
483{
484 unsigned ret = 0;
485 struct hlist_head *h;
486
487 mutex_lock(&c->bucket_lock);
488
489 for (h = c->bucket_hash;
490 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
491 h++) {
492 unsigned i = 0;
493 struct hlist_node *p;
494
495 hlist_for_each(p, h)
496 i++;
497
498 ret = max(ret, i);
499 }
500
501 mutex_unlock(&c->bucket_lock);
502 return ret;
503}
504
505static unsigned bch_btree_used(struct cache_set *c)
506{
507 return div64_u64(c->gc_stats.key_bytes * 100,
508 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
509}
510
511static unsigned bch_average_key_size(struct cache_set *c)
512{
513 return c->gc_stats.nkeys
514 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
515 : 0;
516}
517
Kent Overstreetcafe5632013-03-23 16:11:31 -0700518SHOW(__bch_cache_set)
519{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
521
522 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
523 sysfs_print(journal_delay_ms, c->journal_delay_ms);
524 sysfs_hprint(bucket_size, bucket_bytes(c));
525 sysfs_hprint(block_size, block_bytes(c));
526 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700527 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700528
John Sheucb851142014-03-17 23:13:56 -0700529 sysfs_hprint(btree_cache_size, bch_cache_size(c));
530 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700531 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
532
533 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
534 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700535 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700536 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700537
John Sheucb851142014-03-17 23:13:56 -0700538 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700539 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700540 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700541
542 sysfs_print(cache_read_races,
543 atomic_long_read(&c->cache_read_races));
544
545 sysfs_print(writeback_keys_done,
546 atomic_long_read(&c->writeback_keys_done));
547 sysfs_print(writeback_keys_failed,
548 atomic_long_read(&c->writeback_keys_failed));
549
Kent Overstreet77c320e2013-07-11 19:42:51 -0700550 if (attr == &sysfs_errors)
551 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
552 c->on_error);
553
Kent Overstreetcafe5632013-03-23 16:11:31 -0700554 /* See count_io_errors for why 88 */
555 sysfs_print(io_error_halflife, c->error_decay * 88);
556 sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
557
558 sysfs_hprint(congested,
559 ((uint64_t) bch_get_congested(c)) << 9);
560 sysfs_print(congested_read_threshold_us,
561 c->congested_read_threshold_us);
562 sysfs_print(congested_write_threshold_us,
563 c->congested_write_threshold_us);
564
565 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
566 sysfs_printf(verify, "%i", c->verify);
567 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700568 sysfs_printf(expensive_debug_checks,
569 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700570 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
571 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
572 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
573
574 if (attr == &sysfs_bset_tree_stats)
575 return bch_bset_print_stats(c, buf);
576
577 return 0;
578}
579SHOW_LOCKED(bch_cache_set)
580
581STORE(__bch_cache_set)
582{
583 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
584
585 if (attr == &sysfs_unregister)
586 bch_cache_set_unregister(c);
587
588 if (attr == &sysfs_stop)
589 bch_cache_set_stop(c);
590
591 if (attr == &sysfs_synchronous) {
592 bool sync = strtoul_or_return(buf);
593
594 if (sync != CACHE_SYNC(&c->sb)) {
595 SET_CACHE_SYNC(&c->sb, sync);
596 bcache_write_super(c);
597 }
598 }
599
600 if (attr == &sysfs_flash_vol_create) {
601 int r;
602 uint64_t v;
603 strtoi_h_or_return(buf, v);
604
605 r = bch_flash_dev_create(c, v);
606 if (r)
607 return r;
608 }
609
610 if (attr == &sysfs_clear_stats) {
611 atomic_long_set(&c->writeback_keys_done, 0);
612 atomic_long_set(&c->writeback_keys_failed, 0);
613
614 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
615 bch_cache_accounting_clear(&c->accounting);
616 }
617
618 if (attr == &sysfs_trigger_gc)
Kent Overstreet72a44512013-10-24 17:19:26 -0700619 wake_up_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700620
621 if (attr == &sysfs_prune_cache) {
622 struct shrink_control sc;
623 sc.gfp_mask = GFP_KERNEL;
624 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000625 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700626 }
627
628 sysfs_strtoul(congested_read_threshold_us,
629 c->congested_read_threshold_us);
630 sysfs_strtoul(congested_write_threshold_us,
631 c->congested_write_threshold_us);
632
Kent Overstreet77c320e2013-07-11 19:42:51 -0700633 if (attr == &sysfs_errors) {
634 ssize_t v = bch_read_string_list(buf, error_actions);
635
636 if (v < 0)
637 return v;
638
639 c->on_error = v;
640 }
641
Kent Overstreetcafe5632013-03-23 16:11:31 -0700642 if (attr == &sysfs_io_error_limit)
643 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
644
645 /* See count_io_errors() for why 88 */
646 if (attr == &sysfs_io_error_halflife)
647 c->error_decay = strtoul_or_return(buf) / 88;
648
649 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
650 sysfs_strtoul(verify, c->verify);
651 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700652 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700653 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
654 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
655 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
656
657 return size;
658}
659STORE_LOCKED(bch_cache_set)
660
661SHOW(bch_cache_set_internal)
662{
663 struct cache_set *c = container_of(kobj, struct cache_set, internal);
664 return bch_cache_set_show(&c->kobj, attr, buf);
665}
666
667STORE(bch_cache_set_internal)
668{
669 struct cache_set *c = container_of(kobj, struct cache_set, internal);
670 return bch_cache_set_store(&c->kobj, attr, buf, size);
671}
672
673static void bch_cache_set_internal_release(struct kobject *k)
674{
675}
676
677static struct attribute *bch_cache_set_files[] = {
678 &sysfs_unregister,
679 &sysfs_stop,
680 &sysfs_synchronous,
681 &sysfs_journal_delay_ms,
682 &sysfs_flash_vol_create,
683
684 &sysfs_bucket_size,
685 &sysfs_block_size,
686 &sysfs_tree_depth,
687 &sysfs_root_usage_percent,
688 &sysfs_btree_cache_size,
689 &sysfs_cache_available_percent,
690
691 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700692
Kent Overstreet77c320e2013-07-11 19:42:51 -0700693 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694 &sysfs_io_error_limit,
695 &sysfs_io_error_halflife,
696 &sysfs_congested,
697 &sysfs_congested_read_threshold_us,
698 &sysfs_congested_write_threshold_us,
699 &sysfs_clear_stats,
700 NULL
701};
702KTYPE(bch_cache_set);
703
704static struct attribute *bch_cache_set_internal_files[] = {
705 &sysfs_active_journal_entries,
706
707 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
708 sysfs_time_stats_attribute_list(btree_split, sec, us)
709 sysfs_time_stats_attribute_list(btree_sort, ms, us)
710 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700711
712 &sysfs_btree_nodes,
713 &sysfs_btree_used_percent,
714 &sysfs_btree_cache_max_chain,
715
716 &sysfs_bset_tree_stats,
717 &sysfs_cache_read_races,
718 &sysfs_writeback_keys_done,
719 &sysfs_writeback_keys_failed,
720
721 &sysfs_trigger_gc,
722 &sysfs_prune_cache,
723#ifdef CONFIG_BCACHE_DEBUG
724 &sysfs_verify,
725 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700726 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727#endif
728 &sysfs_gc_always_rewrite,
729 &sysfs_btree_shrinker_disabled,
730 &sysfs_copy_gc_enabled,
731 NULL
732};
733KTYPE(bch_cache_set_internal);
734
735SHOW(__bch_cache)
736{
737 struct cache *ca = container_of(kobj, struct cache, kobj);
738
739 sysfs_hprint(bucket_size, bucket_bytes(ca));
740 sysfs_hprint(block_size, block_bytes(ca));
741 sysfs_print(nbuckets, ca->sb.nbuckets);
742 sysfs_print(discard, ca->discard);
743 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
744 sysfs_hprint(btree_written,
745 atomic_long_read(&ca->btree_sectors_written) << 9);
746 sysfs_hprint(metadata_written,
747 (atomic_long_read(&ca->meta_sectors_written) +
748 atomic_long_read(&ca->btree_sectors_written)) << 9);
749
750 sysfs_print(io_errors,
751 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
752
Kent Overstreetcafe5632013-03-23 16:11:31 -0700753 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600754 return bch_snprint_string_list(buf, PAGE_SIZE,
755 cache_replacement_policies,
756 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700757
758 if (attr == &sysfs_priority_stats) {
759 int cmp(const void *l, const void *r)
760 { return *((uint16_t *) r) - *((uint16_t *) l); }
761
Kent Overstreet15754022014-02-25 17:34:21 -0800762 struct bucket *b;
763 size_t n = ca->sb.nbuckets, i;
764 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700765 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700766 /* Compute 31 quantiles */
767 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700768 ssize_t ret;
769
770 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
771 if (!p)
772 return -ENOMEM;
773
774 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800775 for_each_bucket(b, ca) {
776 if (!GC_SECTORS_USED(b))
777 unused++;
778 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
779 available++;
780 if (GC_MARK(b) == GC_MARK_DIRTY)
781 dirty++;
782 if (GC_MARK(b) == GC_MARK_METADATA)
783 meta++;
784 }
785
Kent Overstreetcafe5632013-03-23 16:11:31 -0700786 for (i = ca->sb.first_bucket; i < n; i++)
787 p[i] = ca->buckets[i].prio;
788 mutex_unlock(&ca->set->bucket_lock);
789
790 sort(p, n, sizeof(uint16_t), cmp, NULL);
791
792 while (n &&
793 !cached[n - 1])
794 --n;
795
796 unused = ca->sb.nbuckets - n;
797
798 while (cached < p + n &&
799 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800800 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801
802 for (i = 0; i < n; i++)
803 sum += INITIAL_PRIO - cached[i];
804
805 if (n)
806 do_div(sum, n);
807
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700808 for (i = 0; i < ARRAY_SIZE(q); i++)
809 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
810 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700811
812 vfree(p);
813
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700814 ret = scnprintf(buf, PAGE_SIZE,
815 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800816 "Clean: %zu%%\n"
817 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700818 "Metadata: %zu%%\n"
819 "Average: %llu\n"
820 "Sectors per Q: %zu\n"
821 "Quantiles: [",
822 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800823 available * 100 / (size_t) ca->sb.nbuckets,
824 dirty * 100 / (size_t) ca->sb.nbuckets,
825 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700826 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700828 for (i = 0; i < ARRAY_SIZE(q); i++)
829 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
830 "%u ", q[i]);
831 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700832
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700833 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
834
Kent Overstreetcafe5632013-03-23 16:11:31 -0700835 return ret;
836 }
837
838 return 0;
839}
840SHOW_LOCKED(bch_cache)
841
842STORE(__bch_cache)
843{
844 struct cache *ca = container_of(kobj, struct cache, kobj);
845
846 if (attr == &sysfs_discard) {
847 bool v = strtoul_or_return(buf);
848
849 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
850 ca->discard = v;
851
852 if (v != CACHE_DISCARD(&ca->sb)) {
853 SET_CACHE_DISCARD(&ca->sb, v);
854 bcache_write_super(ca->set);
855 }
856 }
857
858 if (attr == &sysfs_cache_replacement_policy) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600859 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700860
861 if (v < 0)
862 return v;
863
864 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
865 mutex_lock(&ca->set->bucket_lock);
866 SET_CACHE_REPLACEMENT(&ca->sb, v);
867 mutex_unlock(&ca->set->bucket_lock);
868
869 bcache_write_super(ca->set);
870 }
871 }
872
Kent Overstreetcafe5632013-03-23 16:11:31 -0700873 if (attr == &sysfs_clear_stats) {
874 atomic_long_set(&ca->sectors_written, 0);
875 atomic_long_set(&ca->btree_sectors_written, 0);
876 atomic_long_set(&ca->meta_sectors_written, 0);
877 atomic_set(&ca->io_count, 0);
878 atomic_set(&ca->io_errors, 0);
879 }
880
881 return size;
882}
883STORE_LOCKED(bch_cache)
884
885static struct attribute *bch_cache_files[] = {
886 &sysfs_bucket_size,
887 &sysfs_block_size,
888 &sysfs_nbuckets,
889 &sysfs_priority_stats,
890 &sysfs_discard,
891 &sysfs_written,
892 &sysfs_btree_written,
893 &sysfs_metadata_written,
894 &sysfs_io_errors,
895 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700896 &sysfs_cache_replacement_policy,
897 NULL
898};
899KTYPE(bch_cache);