blob: 5a5c1f1bd8a5330c388177415d0dc1eb7f87f44c [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>
16
17static const char * const cache_replacement_policies[] = {
18 "lru",
19 "fifo",
20 "random",
21 NULL
22};
23
Kent Overstreet77c320e2013-07-11 19:42:51 -070024static const char * const error_actions[] = {
25 "unregister",
26 "panic",
27 NULL
28};
29
Kent Overstreetcafe5632013-03-23 16:11:31 -070030write_attribute(attach);
31write_attribute(detach);
32write_attribute(unregister);
33write_attribute(stop);
34write_attribute(clear_stats);
35write_attribute(trigger_gc);
36write_attribute(prune_cache);
37write_attribute(flash_vol_create);
38
39read_attribute(bucket_size);
40read_attribute(block_size);
41read_attribute(nbuckets);
42read_attribute(tree_depth);
43read_attribute(root_usage_percent);
44read_attribute(priority_stats);
45read_attribute(btree_cache_size);
46read_attribute(btree_cache_max_chain);
47read_attribute(cache_available_percent);
48read_attribute(written);
49read_attribute(btree_written);
50read_attribute(metadata_written);
51read_attribute(active_journal_entries);
52
53sysfs_time_stats_attribute(btree_gc, sec, ms);
54sysfs_time_stats_attribute(btree_split, sec, us);
55sysfs_time_stats_attribute(btree_sort, ms, us);
56sysfs_time_stats_attribute(btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -070057
58read_attribute(btree_nodes);
59read_attribute(btree_used_percent);
60read_attribute(average_key_size);
61read_attribute(dirty_data);
62read_attribute(bset_tree_stats);
63
64read_attribute(state);
65read_attribute(cache_read_races);
66read_attribute(writeback_keys_done);
67read_attribute(writeback_keys_failed);
68read_attribute(io_errors);
69read_attribute(congested);
70rw_attribute(congested_read_threshold_us);
71rw_attribute(congested_write_threshold_us);
72
73rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -070074rw_attribute(data_csum);
75rw_attribute(cache_mode);
76rw_attribute(writeback_metadata);
77rw_attribute(writeback_running);
78rw_attribute(writeback_percent);
79rw_attribute(writeback_delay);
80rw_attribute(writeback_rate);
81
82rw_attribute(writeback_rate_update_seconds);
83rw_attribute(writeback_rate_d_term);
84rw_attribute(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070085read_attribute(writeback_rate_debug);
86
Kent Overstreet72c27062013-06-05 06:24:39 -070087read_attribute(stripe_size);
88read_attribute(partial_stripes_expensive);
89
Kent Overstreetcafe5632013-03-23 16:11:31 -070090rw_attribute(synchronous);
91rw_attribute(journal_delay_ms);
92rw_attribute(discard);
93rw_attribute(running);
94rw_attribute(label);
95rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -070096rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -070097rw_attribute(io_error_limit);
98rw_attribute(io_error_halflife);
99rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700100rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700101rw_attribute(key_merging_disabled);
102rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700103rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700104rw_attribute(cache_replacement_policy);
105rw_attribute(btree_shrinker_disabled);
106rw_attribute(copy_gc_enabled);
107rw_attribute(size);
108
109SHOW(__bch_cached_dev)
110{
111 struct cached_dev *dc = container_of(kobj, struct cached_dev,
112 disk.kobj);
113 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
114
115#define var(stat) (dc->stat)
116
117 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600118 return bch_snprint_string_list(buf, PAGE_SIZE,
119 bch_cache_modes + 1,
120 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121
122 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
123 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700124 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125 var_printf(writeback_metadata, "%i");
126 var_printf(writeback_running, "%i");
127 var_print(writeback_delay);
128 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800129 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700130
131 var_print(writeback_rate_update_seconds);
132 var_print(writeback_rate_d_term);
133 var_print(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700134
135 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800136 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700137 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700138 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800139 char proportional[20];
140 char derivative[20];
141 char change[20];
142 s64 next_io;
143
144 bch_hprint(rate, dc->writeback_rate.rate << 9);
145 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600146 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800147 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
148 bch_hprint(derivative, dc->writeback_rate_derivative << 9);
149 bch_hprint(change, dc->writeback_rate_change << 9);
150
151 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
152 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700153
154 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800155 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800157 "target:\t\t%s\n"
158 "proportional:\t%s\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159 "derivative:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800160 "change:\t\t%s/sec\n"
161 "next io:\t%llims\n",
162 rate, dirty, target, proportional,
163 derivative, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700164 }
165
166 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700167 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700168
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700169 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700170 var_printf(partial_stripes_expensive, "%u");
171
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172 var_hprint(sequential_cutoff);
173 var_hprint(readahead);
174
175 sysfs_print(running, atomic_read(&dc->running));
176 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
177
178 if (attr == &sysfs_label) {
179 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
180 buf[SB_LABEL_SIZE + 1] = '\0';
181 strcat(buf, "\n");
182 return strlen(buf);
183 }
184
185#undef var
186 return 0;
187}
188SHOW_LOCKED(bch_cached_dev)
189
190STORE(__cached_dev)
191{
192 struct cached_dev *dc = container_of(kobj, struct cached_dev,
193 disk.kobj);
Tang Junhuid4008f82018-02-07 11:41:45 -0800194 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700195 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200196 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197
198#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800199#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
201
202 sysfs_strtoul(data_csum, dc->disk.data_csum);
203 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700204 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205 d_strtoul(writeback_metadata);
206 d_strtoul(writeback_running);
207 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800208
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
210
Kent Overstreet16749c22013-11-11 13:58:34 -0800211 sysfs_strtoul_clamp(writeback_rate,
212 dc->writeback_rate.rate, 1, INT_MAX);
213
214 d_strtoul_nonzero(writeback_rate_update_seconds);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215 d_strtoul(writeback_rate_d_term);
Kent Overstreet16749c22013-11-11 13:58:34 -0800216 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700217
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218 d_strtoi_h(sequential_cutoff);
219 d_strtoi_h(readahead);
220
221 if (attr == &sysfs_clear_stats)
222 bch_cache_accounting_clear(&dc->accounting);
223
224 if (attr == &sysfs_running &&
225 strtoul_or_return(buf))
226 bch_cached_dev_run(dc);
227
228 if (attr == &sysfs_cache_mode) {
Tony Aslesonfa92ff6b2017-09-06 14:25:57 +0800229 v = bch_read_string_list(buf, bch_cache_modes + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700230
231 if (v < 0)
232 return v;
233
234 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
235 SET_BDEV_CACHE_MODE(&dc->sb, v);
236 bch_write_bdev_super(dc, NULL);
237 }
238 }
239
240 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700241 if (size > SB_LABEL_SIZE)
242 return -EINVAL;
243 memcpy(dc->sb.label, buf, size);
244 if (size < SB_LABEL_SIZE)
245 dc->sb.label[size] = '\0';
246 if (size && dc->sb.label[size - 1] == '\n')
247 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700248 bch_write_bdev_super(dc, NULL);
249 if (dc->disk.c) {
250 memcpy(dc->disk.c->uuids[dc->disk.id].label,
251 buf, SB_LABEL_SIZE);
252 bch_uuid_write(dc->disk.c);
253 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200254 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300255 if (!env)
256 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200257 add_uevent_var(env, "DRIVER=bcache");
258 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
259 add_uevent_var(env, "CACHED_LABEL=%s", buf);
260 kobject_uevent_env(
261 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
262 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700263 }
264
265 if (attr == &sysfs_attach) {
Tang Junhui0d5da312018-02-07 11:41:46 -0800266 uint8_t set_uuid[16];
267
268 if (bch_parse_uuid(buf, set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700269 return -EINVAL;
270
Tang Junhuid4008f82018-02-07 11:41:45 -0800271 v = -ENOENT;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700272 list_for_each_entry(c, &bch_cache_sets, list) {
Tang Junhui0d5da312018-02-07 11:41:46 -0800273 v = bch_cached_dev_attach(dc, c, set_uuid);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700274 if (!v)
275 return size;
276 }
277
278 pr_err("Can't attach %s: cache set not found", buf);
Tang Junhuid4008f82018-02-07 11:41:45 -0800279 return v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700280 }
281
282 if (attr == &sysfs_detach && dc->disk.c)
283 bch_cached_dev_detach(dc);
284
285 if (attr == &sysfs_stop)
286 bcache_device_stop(&dc->disk);
287
288 return size;
289}
290
291STORE(bch_cached_dev)
292{
293 struct cached_dev *dc = container_of(kobj, struct cached_dev,
294 disk.kobj);
295
296 mutex_lock(&bch_register_lock);
297 size = __cached_dev_store(kobj, attr, buf, size);
298
299 if (attr == &sysfs_writeback_running)
300 bch_writeback_queue(dc);
301
302 if (attr == &sysfs_writeback_percent)
303 schedule_delayed_work(&dc->writeback_rate_update,
304 dc->writeback_rate_update_seconds * HZ);
305
306 mutex_unlock(&bch_register_lock);
307 return size;
308}
309
310static struct attribute *bch_cached_dev_files[] = {
311 &sysfs_attach,
312 &sysfs_detach,
313 &sysfs_stop,
314#if 0
315 &sysfs_data_csum,
316#endif
317 &sysfs_cache_mode,
318 &sysfs_writeback_metadata,
319 &sysfs_writeback_running,
320 &sysfs_writeback_delay,
321 &sysfs_writeback_percent,
322 &sysfs_writeback_rate,
323 &sysfs_writeback_rate_update_seconds,
324 &sysfs_writeback_rate_d_term,
325 &sysfs_writeback_rate_p_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700326 &sysfs_writeback_rate_debug,
327 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700328 &sysfs_stripe_size,
329 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700330 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700331 &sysfs_clear_stats,
332 &sysfs_running,
333 &sysfs_state,
334 &sysfs_label,
335 &sysfs_readahead,
336#ifdef CONFIG_BCACHE_DEBUG
337 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700338 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700339#endif
340 NULL
341};
342KTYPE(bch_cached_dev);
343
344SHOW(bch_flash_dev)
345{
346 struct bcache_device *d = container_of(kobj, struct bcache_device,
347 kobj);
348 struct uuid_entry *u = &d->c->uuids[d->id];
349
350 sysfs_printf(data_csum, "%i", d->data_csum);
351 sysfs_hprint(size, u->sectors << 9);
352
353 if (attr == &sysfs_label) {
354 memcpy(buf, u->label, SB_LABEL_SIZE);
355 buf[SB_LABEL_SIZE + 1] = '\0';
356 strcat(buf, "\n");
357 return strlen(buf);
358 }
359
360 return 0;
361}
362
363STORE(__bch_flash_dev)
364{
365 struct bcache_device *d = container_of(kobj, struct bcache_device,
366 kobj);
367 struct uuid_entry *u = &d->c->uuids[d->id];
368
369 sysfs_strtoul(data_csum, d->data_csum);
370
371 if (attr == &sysfs_size) {
372 uint64_t v;
373 strtoi_h_or_return(buf, v);
374
375 u->sectors = v >> 9;
376 bch_uuid_write(d->c);
377 set_capacity(d->disk, u->sectors);
378 }
379
380 if (attr == &sysfs_label) {
381 memcpy(u->label, buf, SB_LABEL_SIZE);
382 bch_uuid_write(d->c);
383 }
384
385 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700386 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700387 bcache_device_stop(d);
388 }
389
390 return size;
391}
392STORE_LOCKED(bch_flash_dev)
393
394static struct attribute *bch_flash_dev_files[] = {
395 &sysfs_unregister,
396#if 0
397 &sysfs_data_csum,
398#endif
399 &sysfs_label,
400 &sysfs_size,
401 NULL
402};
403KTYPE(bch_flash_dev);
404
Kent Overstreetf67342d2013-11-11 19:25:55 -0800405struct bset_stats_op {
406 struct btree_op op;
407 size_t nodes;
408 struct bset_stats stats;
409};
410
John Sheucb851142014-03-17 23:13:56 -0700411static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800412{
413 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
414
415 op->nodes++;
416 bch_btree_keys_stats(&b->keys, &op->stats);
417
418 return MAP_CONTINUE;
419}
420
Kent Overstreet35723242014-01-10 18:53:02 -0800421static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800422{
423 struct bset_stats_op op;
424 int ret;
425
426 memset(&op, 0, sizeof(op));
427 bch_btree_op_init(&op.op, -1);
428
John Sheucb851142014-03-17 23:13:56 -0700429 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800430 if (ret < 0)
431 return ret;
432
433 return snprintf(buf, PAGE_SIZE,
434 "btree nodes: %zu\n"
435 "written sets: %zu\n"
436 "unwritten sets: %zu\n"
437 "written key bytes: %zu\n"
438 "unwritten key bytes: %zu\n"
439 "floats: %zu\n"
440 "failed: %zu\n",
441 op.nodes,
442 op.stats.sets_written, op.stats.sets_unwritten,
443 op.stats.bytes_written, op.stats.bytes_unwritten,
444 op.stats.floats, op.stats.failed);
445}
446
John Sheucb851142014-03-17 23:13:56 -0700447static unsigned bch_root_usage(struct cache_set *c)
448{
449 unsigned bytes = 0;
450 struct bkey *k;
451 struct btree *b;
452 struct btree_iter iter;
453
454 goto lock_root;
455
456 do {
457 rw_unlock(false, b);
458lock_root:
459 b = c->root;
460 rw_lock(false, b, b->level);
461 } while (b != c->root);
462
463 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
464 bytes += bkey_bytes(k);
465
466 rw_unlock(false, b);
467
468 return (bytes * 100) / btree_bytes(c);
469}
470
471static size_t bch_cache_size(struct cache_set *c)
472{
473 size_t ret = 0;
474 struct btree *b;
475
476 mutex_lock(&c->bucket_lock);
477 list_for_each_entry(b, &c->btree_cache, list)
478 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
479
480 mutex_unlock(&c->bucket_lock);
481 return ret;
482}
483
484static unsigned bch_cache_max_chain(struct cache_set *c)
485{
486 unsigned ret = 0;
487 struct hlist_head *h;
488
489 mutex_lock(&c->bucket_lock);
490
491 for (h = c->bucket_hash;
492 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
493 h++) {
494 unsigned i = 0;
495 struct hlist_node *p;
496
497 hlist_for_each(p, h)
498 i++;
499
500 ret = max(ret, i);
501 }
502
503 mutex_unlock(&c->bucket_lock);
504 return ret;
505}
506
507static unsigned bch_btree_used(struct cache_set *c)
508{
509 return div64_u64(c->gc_stats.key_bytes * 100,
510 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
511}
512
513static unsigned bch_average_key_size(struct cache_set *c)
514{
515 return c->gc_stats.nkeys
516 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
517 : 0;
518}
519
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520SHOW(__bch_cache_set)
521{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700522 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
523
524 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
525 sysfs_print(journal_delay_ms, c->journal_delay_ms);
526 sysfs_hprint(bucket_size, bucket_bytes(c));
527 sysfs_hprint(block_size, block_bytes(c));
528 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700529 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700530
John Sheucb851142014-03-17 23:13:56 -0700531 sysfs_hprint(btree_cache_size, bch_cache_size(c));
532 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700533 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
534
535 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
536 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700537 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700538 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700539
John Sheucb851142014-03-17 23:13:56 -0700540 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700541 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700542 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700543
544 sysfs_print(cache_read_races,
545 atomic_long_read(&c->cache_read_races));
546
547 sysfs_print(writeback_keys_done,
548 atomic_long_read(&c->writeback_keys_done));
549 sysfs_print(writeback_keys_failed,
550 atomic_long_read(&c->writeback_keys_failed));
551
Kent Overstreet77c320e2013-07-11 19:42:51 -0700552 if (attr == &sysfs_errors)
553 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
554 c->on_error);
555
Kent Overstreetcafe5632013-03-23 16:11:31 -0700556 /* See count_io_errors for why 88 */
557 sysfs_print(io_error_halflife, c->error_decay * 88);
558 sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
559
560 sysfs_hprint(congested,
561 ((uint64_t) bch_get_congested(c)) << 9);
562 sysfs_print(congested_read_threshold_us,
563 c->congested_read_threshold_us);
564 sysfs_print(congested_write_threshold_us,
565 c->congested_write_threshold_us);
566
567 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
568 sysfs_printf(verify, "%i", c->verify);
569 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700570 sysfs_printf(expensive_debug_checks,
571 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700572 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
573 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
574 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
575
576 if (attr == &sysfs_bset_tree_stats)
577 return bch_bset_print_stats(c, buf);
578
579 return 0;
580}
581SHOW_LOCKED(bch_cache_set)
582
583STORE(__bch_cache_set)
584{
585 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
586
587 if (attr == &sysfs_unregister)
588 bch_cache_set_unregister(c);
589
590 if (attr == &sysfs_stop)
591 bch_cache_set_stop(c);
592
593 if (attr == &sysfs_synchronous) {
594 bool sync = strtoul_or_return(buf);
595
596 if (sync != CACHE_SYNC(&c->sb)) {
597 SET_CACHE_SYNC(&c->sb, sync);
598 bcache_write_super(c);
599 }
600 }
601
602 if (attr == &sysfs_flash_vol_create) {
603 int r;
604 uint64_t v;
605 strtoi_h_or_return(buf, v);
606
607 r = bch_flash_dev_create(c, v);
608 if (r)
609 return r;
610 }
611
612 if (attr == &sysfs_clear_stats) {
613 atomic_long_set(&c->writeback_keys_done, 0);
614 atomic_long_set(&c->writeback_keys_failed, 0);
615
616 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
617 bch_cache_accounting_clear(&c->accounting);
618 }
619
620 if (attr == &sysfs_trigger_gc)
Kent Overstreet72a44512013-10-24 17:19:26 -0700621 wake_up_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700622
623 if (attr == &sysfs_prune_cache) {
624 struct shrink_control sc;
625 sc.gfp_mask = GFP_KERNEL;
626 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000627 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700628 }
629
630 sysfs_strtoul(congested_read_threshold_us,
631 c->congested_read_threshold_us);
632 sysfs_strtoul(congested_write_threshold_us,
633 c->congested_write_threshold_us);
634
Kent Overstreet77c320e2013-07-11 19:42:51 -0700635 if (attr == &sysfs_errors) {
636 ssize_t v = bch_read_string_list(buf, error_actions);
637
638 if (v < 0)
639 return v;
640
641 c->on_error = v;
642 }
643
Kent Overstreetcafe5632013-03-23 16:11:31 -0700644 if (attr == &sysfs_io_error_limit)
645 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
646
647 /* See count_io_errors() for why 88 */
648 if (attr == &sysfs_io_error_halflife)
649 c->error_decay = strtoul_or_return(buf) / 88;
650
651 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
652 sysfs_strtoul(verify, c->verify);
653 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700654 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700655 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
656 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
657 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
658
659 return size;
660}
661STORE_LOCKED(bch_cache_set)
662
663SHOW(bch_cache_set_internal)
664{
665 struct cache_set *c = container_of(kobj, struct cache_set, internal);
666 return bch_cache_set_show(&c->kobj, attr, buf);
667}
668
669STORE(bch_cache_set_internal)
670{
671 struct cache_set *c = container_of(kobj, struct cache_set, internal);
672 return bch_cache_set_store(&c->kobj, attr, buf, size);
673}
674
675static void bch_cache_set_internal_release(struct kobject *k)
676{
677}
678
679static struct attribute *bch_cache_set_files[] = {
680 &sysfs_unregister,
681 &sysfs_stop,
682 &sysfs_synchronous,
683 &sysfs_journal_delay_ms,
684 &sysfs_flash_vol_create,
685
686 &sysfs_bucket_size,
687 &sysfs_block_size,
688 &sysfs_tree_depth,
689 &sysfs_root_usage_percent,
690 &sysfs_btree_cache_size,
691 &sysfs_cache_available_percent,
692
693 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694
Kent Overstreet77c320e2013-07-11 19:42:51 -0700695 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700696 &sysfs_io_error_limit,
697 &sysfs_io_error_halflife,
698 &sysfs_congested,
699 &sysfs_congested_read_threshold_us,
700 &sysfs_congested_write_threshold_us,
701 &sysfs_clear_stats,
702 NULL
703};
704KTYPE(bch_cache_set);
705
706static struct attribute *bch_cache_set_internal_files[] = {
707 &sysfs_active_journal_entries,
708
709 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
710 sysfs_time_stats_attribute_list(btree_split, sec, us)
711 sysfs_time_stats_attribute_list(btree_sort, ms, us)
712 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700713
714 &sysfs_btree_nodes,
715 &sysfs_btree_used_percent,
716 &sysfs_btree_cache_max_chain,
717
718 &sysfs_bset_tree_stats,
719 &sysfs_cache_read_races,
720 &sysfs_writeback_keys_done,
721 &sysfs_writeback_keys_failed,
722
723 &sysfs_trigger_gc,
724 &sysfs_prune_cache,
725#ifdef CONFIG_BCACHE_DEBUG
726 &sysfs_verify,
727 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700728 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700729#endif
730 &sysfs_gc_always_rewrite,
731 &sysfs_btree_shrinker_disabled,
732 &sysfs_copy_gc_enabled,
733 NULL
734};
735KTYPE(bch_cache_set_internal);
736
737SHOW(__bch_cache)
738{
739 struct cache *ca = container_of(kobj, struct cache, kobj);
740
741 sysfs_hprint(bucket_size, bucket_bytes(ca));
742 sysfs_hprint(block_size, block_bytes(ca));
743 sysfs_print(nbuckets, ca->sb.nbuckets);
744 sysfs_print(discard, ca->discard);
745 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
746 sysfs_hprint(btree_written,
747 atomic_long_read(&ca->btree_sectors_written) << 9);
748 sysfs_hprint(metadata_written,
749 (atomic_long_read(&ca->meta_sectors_written) +
750 atomic_long_read(&ca->btree_sectors_written)) << 9);
751
752 sysfs_print(io_errors,
753 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
754
Kent Overstreetcafe5632013-03-23 16:11:31 -0700755 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600756 return bch_snprint_string_list(buf, PAGE_SIZE,
757 cache_replacement_policies,
758 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700759
760 if (attr == &sysfs_priority_stats) {
761 int cmp(const void *l, const void *r)
762 { return *((uint16_t *) r) - *((uint16_t *) l); }
763
Kent Overstreet15754022014-02-25 17:34:21 -0800764 struct bucket *b;
765 size_t n = ca->sb.nbuckets, i;
766 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700767 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700768 /* Compute 31 quantiles */
769 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700770 ssize_t ret;
771
772 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
773 if (!p)
774 return -ENOMEM;
775
776 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800777 for_each_bucket(b, ca) {
778 if (!GC_SECTORS_USED(b))
779 unused++;
780 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
781 available++;
782 if (GC_MARK(b) == GC_MARK_DIRTY)
783 dirty++;
784 if (GC_MARK(b) == GC_MARK_METADATA)
785 meta++;
786 }
787
Kent Overstreetcafe5632013-03-23 16:11:31 -0700788 for (i = ca->sb.first_bucket; i < n; i++)
789 p[i] = ca->buckets[i].prio;
790 mutex_unlock(&ca->set->bucket_lock);
791
792 sort(p, n, sizeof(uint16_t), cmp, NULL);
793
794 while (n &&
795 !cached[n - 1])
796 --n;
797
798 unused = ca->sb.nbuckets - n;
799
800 while (cached < p + n &&
801 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800802 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700803
804 for (i = 0; i < n; i++)
805 sum += INITIAL_PRIO - cached[i];
806
807 if (n)
808 do_div(sum, n);
809
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700810 for (i = 0; i < ARRAY_SIZE(q); i++)
811 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
812 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700813
814 vfree(p);
815
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700816 ret = scnprintf(buf, PAGE_SIZE,
817 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800818 "Clean: %zu%%\n"
819 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700820 "Metadata: %zu%%\n"
821 "Average: %llu\n"
822 "Sectors per Q: %zu\n"
823 "Quantiles: [",
824 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800825 available * 100 / (size_t) ca->sb.nbuckets,
826 dirty * 100 / (size_t) ca->sb.nbuckets,
827 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700828 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700829
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700830 for (i = 0; i < ARRAY_SIZE(q); i++)
831 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
832 "%u ", q[i]);
833 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700834
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700835 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
836
Kent Overstreetcafe5632013-03-23 16:11:31 -0700837 return ret;
838 }
839
840 return 0;
841}
842SHOW_LOCKED(bch_cache)
843
844STORE(__bch_cache)
845{
846 struct cache *ca = container_of(kobj, struct cache, kobj);
847
848 if (attr == &sysfs_discard) {
849 bool v = strtoul_or_return(buf);
850
851 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
852 ca->discard = v;
853
854 if (v != CACHE_DISCARD(&ca->sb)) {
855 SET_CACHE_DISCARD(&ca->sb, v);
856 bcache_write_super(ca->set);
857 }
858 }
859
860 if (attr == &sysfs_cache_replacement_policy) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600861 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700862
863 if (v < 0)
864 return v;
865
866 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
867 mutex_lock(&ca->set->bucket_lock);
868 SET_CACHE_REPLACEMENT(&ca->sb, v);
869 mutex_unlock(&ca->set->bucket_lock);
870
871 bcache_write_super(ca->set);
872 }
873 }
874
Kent Overstreetcafe5632013-03-23 16:11:31 -0700875 if (attr == &sysfs_clear_stats) {
876 atomic_long_set(&ca->sectors_written, 0);
877 atomic_long_set(&ca->btree_sectors_written, 0);
878 atomic_long_set(&ca->meta_sectors_written, 0);
879 atomic_set(&ca->io_count, 0);
880 atomic_set(&ca->io_errors, 0);
881 }
882
883 return size;
884}
885STORE_LOCKED(bch_cache)
886
887static struct attribute *bch_cache_files[] = {
888 &sysfs_bucket_size,
889 &sysfs_block_size,
890 &sysfs_nbuckets,
891 &sysfs_priority_stats,
892 &sysfs_discard,
893 &sysfs_written,
894 &sysfs_btree_written,
895 &sysfs_metadata_written,
896 &sysfs_io_errors,
897 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700898 &sysfs_cache_replacement_policy,
899 NULL
900};
901KTYPE(bch_cache);