blob: 2290bffd49228e8ac85f7bd999caf9524e7d9036 [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);
Michael Lyle1d316e62017-10-13 16:35:36 -070084rw_attribute(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070085rw_attribute(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -070086rw_attribute(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -070087read_attribute(writeback_rate_debug);
88
Kent Overstreet72c27062013-06-05 06:24:39 -070089read_attribute(stripe_size);
90read_attribute(partial_stripes_expensive);
91
Kent Overstreetcafe5632013-03-23 16:11:31 -070092rw_attribute(synchronous);
93rw_attribute(journal_delay_ms);
94rw_attribute(discard);
95rw_attribute(running);
96rw_attribute(label);
97rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -070098rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -070099rw_attribute(io_error_limit);
100rw_attribute(io_error_halflife);
101rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700102rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700103rw_attribute(key_merging_disabled);
104rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700105rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700106rw_attribute(cache_replacement_policy);
107rw_attribute(btree_shrinker_disabled);
108rw_attribute(copy_gc_enabled);
109rw_attribute(size);
110
111SHOW(__bch_cached_dev)
112{
113 struct cached_dev *dc = container_of(kobj, struct cached_dev,
114 disk.kobj);
115 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
116
117#define var(stat) (dc->stat)
118
119 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600120 return bch_snprint_string_list(buf, PAGE_SIZE,
121 bch_cache_modes + 1,
122 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700123
124 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
125 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700126 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700127 var_printf(writeback_metadata, "%i");
128 var_printf(writeback_running, "%i");
129 var_print(writeback_delay);
130 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800131 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700132
133 var_print(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700134 var_print(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135 var_print(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700136 var_print(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700137
138 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800139 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700140 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800142 char proportional[20];
Michael Lyle1d316e62017-10-13 16:35:36 -0700143 char integral[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800144 char change[20];
145 s64 next_io;
146
147 bch_hprint(rate, dc->writeback_rate.rate << 9);
148 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600149 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800150 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
Michael Lyle1d316e62017-10-13 16:35:36 -0700151 bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800152 bch_hprint(change, dc->writeback_rate_change << 9);
153
154 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
155 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156
157 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800158 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800160 "target:\t\t%s\n"
161 "proportional:\t%s\n"
Michael Lyle1d316e62017-10-13 16:35:36 -0700162 "integral:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800163 "change:\t\t%s/sec\n"
164 "next io:\t%llims\n",
165 rate, dirty, target, proportional,
Michael Lyle1d316e62017-10-13 16:35:36 -0700166 integral, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167 }
168
169 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700170 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700171
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700172 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700173 var_printf(partial_stripes_expensive, "%u");
174
Kent Overstreetcafe5632013-03-23 16:11:31 -0700175 var_hprint(sequential_cutoff);
176 var_hprint(readahead);
177
178 sysfs_print(running, atomic_read(&dc->running));
179 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
180
181 if (attr == &sysfs_label) {
182 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
183 buf[SB_LABEL_SIZE + 1] = '\0';
184 strcat(buf, "\n");
185 return strlen(buf);
186 }
187
188#undef var
189 return 0;
190}
191SHOW_LOCKED(bch_cached_dev)
192
193STORE(__cached_dev)
194{
195 struct cached_dev *dc = container_of(kobj, struct cached_dev,
196 disk.kobj);
Tony Asleson77fa1002017-09-06 14:25:57 +0800197 ssize_t v = size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200199 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200
201#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800202#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
204
205 sysfs_strtoul(data_csum, dc->disk.data_csum);
206 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700207 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700208 d_strtoul(writeback_metadata);
209 d_strtoul(writeback_running);
210 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800211
Kent Overstreetcafe5632013-03-23 16:11:31 -0700212 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
213
Kent Overstreet16749c22013-11-11 13:58:34 -0800214 sysfs_strtoul_clamp(writeback_rate,
215 dc->writeback_rate.rate, 1, INT_MAX);
216
217 d_strtoul_nonzero(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700218 d_strtoul(writeback_rate_i_term_inverse);
Kent Overstreet16749c22013-11-11 13:58:34 -0800219 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700220
Kent Overstreetcafe5632013-03-23 16:11:31 -0700221 d_strtoi_h(sequential_cutoff);
222 d_strtoi_h(readahead);
223
224 if (attr == &sysfs_clear_stats)
225 bch_cache_accounting_clear(&dc->accounting);
226
227 if (attr == &sysfs_running &&
228 strtoul_or_return(buf))
229 bch_cached_dev_run(dc);
230
231 if (attr == &sysfs_cache_mode) {
Tony Asleson77fa1002017-09-06 14:25:57 +0800232 v = bch_read_string_list(buf, bch_cache_modes + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700233
234 if (v < 0)
235 return v;
236
237 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
238 SET_BDEV_CACHE_MODE(&dc->sb, v);
239 bch_write_bdev_super(dc, NULL);
240 }
241 }
242
243 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700244 if (size > SB_LABEL_SIZE)
245 return -EINVAL;
246 memcpy(dc->sb.label, buf, size);
247 if (size < SB_LABEL_SIZE)
248 dc->sb.label[size] = '\0';
249 if (size && dc->sb.label[size - 1] == '\n')
250 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700251 bch_write_bdev_super(dc, NULL);
252 if (dc->disk.c) {
253 memcpy(dc->disk.c->uuids[dc->disk.id].label,
254 buf, SB_LABEL_SIZE);
255 bch_uuid_write(dc->disk.c);
256 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200257 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300258 if (!env)
259 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200260 add_uevent_var(env, "DRIVER=bcache");
261 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
262 add_uevent_var(env, "CACHED_LABEL=%s", buf);
263 kobject_uevent_env(
264 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
265 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700266 }
267
268 if (attr == &sysfs_attach) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600269 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700270 return -EINVAL;
271
272 list_for_each_entry(c, &bch_cache_sets, list) {
273 v = bch_cached_dev_attach(dc, c);
274 if (!v)
275 return size;
276 }
277
278 pr_err("Can't attach %s: cache set not found", buf);
279 size = v;
280 }
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,
Michael Lyle1d316e62017-10-13 16:35:36 -0700324 &sysfs_writeback_rate_i_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700325 &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
Tang Junhui0b43f492017-09-06 14:25:55 +0800620 if (attr == &sysfs_trigger_gc) {
621 /*
622 * Garbage collection thread only works when sectors_to_gc < 0,
623 * when users write to sysfs entry trigger_gc, most of time
624 * they want to forcibly triger gargage collection. Here -1 is
625 * set to c->sectors_to_gc, to make gc_should_run() give a
626 * chance to permit gc thread to run. "give a chance" means
627 * before going into gc_should_run(), there is still chance
628 * that c->sectors_to_gc being set to other positive value. So
629 * writing sysfs entry trigger_gc won't always make sure gc
630 * thread takes effect.
631 */
632 atomic_set(&c->sectors_to_gc, -1);
Kent Overstreet72a44512013-10-24 17:19:26 -0700633 wake_up_gc(c);
Tang Junhui0b43f492017-09-06 14:25:55 +0800634 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700635
636 if (attr == &sysfs_prune_cache) {
637 struct shrink_control sc;
638 sc.gfp_mask = GFP_KERNEL;
639 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000640 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700641 }
642
643 sysfs_strtoul(congested_read_threshold_us,
644 c->congested_read_threshold_us);
645 sysfs_strtoul(congested_write_threshold_us,
646 c->congested_write_threshold_us);
647
Kent Overstreet77c320e2013-07-11 19:42:51 -0700648 if (attr == &sysfs_errors) {
649 ssize_t v = bch_read_string_list(buf, error_actions);
650
651 if (v < 0)
652 return v;
653
654 c->on_error = v;
655 }
656
Kent Overstreetcafe5632013-03-23 16:11:31 -0700657 if (attr == &sysfs_io_error_limit)
658 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
659
660 /* See count_io_errors() for why 88 */
661 if (attr == &sysfs_io_error_halflife)
662 c->error_decay = strtoul_or_return(buf) / 88;
663
664 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
665 sysfs_strtoul(verify, c->verify);
666 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700667 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700668 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
669 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
670 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
671
672 return size;
673}
674STORE_LOCKED(bch_cache_set)
675
676SHOW(bch_cache_set_internal)
677{
678 struct cache_set *c = container_of(kobj, struct cache_set, internal);
679 return bch_cache_set_show(&c->kobj, attr, buf);
680}
681
682STORE(bch_cache_set_internal)
683{
684 struct cache_set *c = container_of(kobj, struct cache_set, internal);
685 return bch_cache_set_store(&c->kobj, attr, buf, size);
686}
687
688static void bch_cache_set_internal_release(struct kobject *k)
689{
690}
691
692static struct attribute *bch_cache_set_files[] = {
693 &sysfs_unregister,
694 &sysfs_stop,
695 &sysfs_synchronous,
696 &sysfs_journal_delay_ms,
697 &sysfs_flash_vol_create,
698
699 &sysfs_bucket_size,
700 &sysfs_block_size,
701 &sysfs_tree_depth,
702 &sysfs_root_usage_percent,
703 &sysfs_btree_cache_size,
704 &sysfs_cache_available_percent,
705
706 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700707
Kent Overstreet77c320e2013-07-11 19:42:51 -0700708 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700709 &sysfs_io_error_limit,
710 &sysfs_io_error_halflife,
711 &sysfs_congested,
712 &sysfs_congested_read_threshold_us,
713 &sysfs_congested_write_threshold_us,
714 &sysfs_clear_stats,
715 NULL
716};
717KTYPE(bch_cache_set);
718
719static struct attribute *bch_cache_set_internal_files[] = {
720 &sysfs_active_journal_entries,
721
722 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
723 sysfs_time_stats_attribute_list(btree_split, sec, us)
724 sysfs_time_stats_attribute_list(btree_sort, ms, us)
725 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700726
727 &sysfs_btree_nodes,
728 &sysfs_btree_used_percent,
729 &sysfs_btree_cache_max_chain,
730
731 &sysfs_bset_tree_stats,
732 &sysfs_cache_read_races,
733 &sysfs_writeback_keys_done,
734 &sysfs_writeback_keys_failed,
735
736 &sysfs_trigger_gc,
737 &sysfs_prune_cache,
738#ifdef CONFIG_BCACHE_DEBUG
739 &sysfs_verify,
740 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700741 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700742#endif
743 &sysfs_gc_always_rewrite,
744 &sysfs_btree_shrinker_disabled,
745 &sysfs_copy_gc_enabled,
746 NULL
747};
748KTYPE(bch_cache_set_internal);
749
Peter Foley58f913d2017-10-13 16:35:28 -0700750static int __bch_cache_cmp(const void *l, const void *r)
751{
752 return *((uint16_t *)r) - *((uint16_t *)l);
753}
754
Kent Overstreetcafe5632013-03-23 16:11:31 -0700755SHOW(__bch_cache)
756{
757 struct cache *ca = container_of(kobj, struct cache, kobj);
758
759 sysfs_hprint(bucket_size, bucket_bytes(ca));
760 sysfs_hprint(block_size, block_bytes(ca));
761 sysfs_print(nbuckets, ca->sb.nbuckets);
762 sysfs_print(discard, ca->discard);
763 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
764 sysfs_hprint(btree_written,
765 atomic_long_read(&ca->btree_sectors_written) << 9);
766 sysfs_hprint(metadata_written,
767 (atomic_long_read(&ca->meta_sectors_written) +
768 atomic_long_read(&ca->btree_sectors_written)) << 9);
769
770 sysfs_print(io_errors,
771 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
772
Kent Overstreetcafe5632013-03-23 16:11:31 -0700773 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600774 return bch_snprint_string_list(buf, PAGE_SIZE,
775 cache_replacement_policies,
776 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700777
778 if (attr == &sysfs_priority_stats) {
Kent Overstreet15754022014-02-25 17:34:21 -0800779 struct bucket *b;
780 size_t n = ca->sb.nbuckets, i;
781 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700782 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700783 /* Compute 31 quantiles */
784 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700785 ssize_t ret;
786
787 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
788 if (!p)
789 return -ENOMEM;
790
791 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800792 for_each_bucket(b, ca) {
793 if (!GC_SECTORS_USED(b))
794 unused++;
795 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
796 available++;
797 if (GC_MARK(b) == GC_MARK_DIRTY)
798 dirty++;
799 if (GC_MARK(b) == GC_MARK_METADATA)
800 meta++;
801 }
802
Kent Overstreetcafe5632013-03-23 16:11:31 -0700803 for (i = ca->sb.first_bucket; i < n; i++)
804 p[i] = ca->buckets[i].prio;
805 mutex_unlock(&ca->set->bucket_lock);
806
Peter Foley58f913d2017-10-13 16:35:28 -0700807 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700808
809 while (n &&
810 !cached[n - 1])
811 --n;
812
813 unused = ca->sb.nbuckets - n;
814
815 while (cached < p + n &&
816 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800817 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700818
819 for (i = 0; i < n; i++)
820 sum += INITIAL_PRIO - cached[i];
821
822 if (n)
823 do_div(sum, n);
824
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700825 for (i = 0; i < ARRAY_SIZE(q); i++)
826 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
827 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700828
829 vfree(p);
830
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700831 ret = scnprintf(buf, PAGE_SIZE,
832 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800833 "Clean: %zu%%\n"
834 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700835 "Metadata: %zu%%\n"
836 "Average: %llu\n"
837 "Sectors per Q: %zu\n"
838 "Quantiles: [",
839 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800840 available * 100 / (size_t) ca->sb.nbuckets,
841 dirty * 100 / (size_t) ca->sb.nbuckets,
842 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700843 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700844
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700845 for (i = 0; i < ARRAY_SIZE(q); i++)
846 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
847 "%u ", q[i]);
848 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700849
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700850 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
851
Kent Overstreetcafe5632013-03-23 16:11:31 -0700852 return ret;
853 }
854
855 return 0;
856}
857SHOW_LOCKED(bch_cache)
858
859STORE(__bch_cache)
860{
861 struct cache *ca = container_of(kobj, struct cache, kobj);
862
863 if (attr == &sysfs_discard) {
864 bool v = strtoul_or_return(buf);
865
866 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
867 ca->discard = v;
868
869 if (v != CACHE_DISCARD(&ca->sb)) {
870 SET_CACHE_DISCARD(&ca->sb, v);
871 bcache_write_super(ca->set);
872 }
873 }
874
875 if (attr == &sysfs_cache_replacement_policy) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600876 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700877
878 if (v < 0)
879 return v;
880
881 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
882 mutex_lock(&ca->set->bucket_lock);
883 SET_CACHE_REPLACEMENT(&ca->sb, v);
884 mutex_unlock(&ca->set->bucket_lock);
885
886 bcache_write_super(ca->set);
887 }
888 }
889
Kent Overstreetcafe5632013-03-23 16:11:31 -0700890 if (attr == &sysfs_clear_stats) {
891 atomic_long_set(&ca->sectors_written, 0);
892 atomic_long_set(&ca->btree_sectors_written, 0);
893 atomic_long_set(&ca->meta_sectors_written, 0);
894 atomic_set(&ca->io_count, 0);
895 atomic_set(&ca->io_errors, 0);
896 }
897
898 return size;
899}
900STORE_LOCKED(bch_cache)
901
902static struct attribute *bch_cache_files[] = {
903 &sysfs_bucket_size,
904 &sysfs_block_size,
905 &sysfs_nbuckets,
906 &sysfs_priority_stats,
907 &sysfs_discard,
908 &sysfs_written,
909 &sysfs_btree_written,
910 &sysfs_metadata_written,
911 &sysfs_io_errors,
912 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700913 &sysfs_cache_replacement_policy,
914 NULL
915};
916KTYPE(bch_cache);