blob: b4184092c7279fa2fe1246f6a3e70650c850d6a1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
3 * bcache sysfs interfaces
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "sysfs.h"
11#include "btree.h"
12#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
Kent Overstreetc37511b2013-04-26 15:39:55 -070015#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070016#include <linux/sort.h>
Ingo Molnare6017572017-02-01 16:36:40 +010017#include <linux/sched/clock.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070018
19static const char * const cache_replacement_policies[] = {
20 "lru",
21 "fifo",
22 "random",
23 NULL
24};
25
Kent Overstreet77c320e2013-07-11 19:42:51 -070026static const char * const error_actions[] = {
27 "unregister",
28 "panic",
29 NULL
30};
31
Kent Overstreetcafe5632013-03-23 16:11:31 -070032write_attribute(attach);
33write_attribute(detach);
34write_attribute(unregister);
35write_attribute(stop);
36write_attribute(clear_stats);
37write_attribute(trigger_gc);
38write_attribute(prune_cache);
39write_attribute(flash_vol_create);
40
41read_attribute(bucket_size);
42read_attribute(block_size);
43read_attribute(nbuckets);
44read_attribute(tree_depth);
45read_attribute(root_usage_percent);
46read_attribute(priority_stats);
47read_attribute(btree_cache_size);
48read_attribute(btree_cache_max_chain);
49read_attribute(cache_available_percent);
50read_attribute(written);
51read_attribute(btree_written);
52read_attribute(metadata_written);
53read_attribute(active_journal_entries);
54
55sysfs_time_stats_attribute(btree_gc, sec, ms);
56sysfs_time_stats_attribute(btree_split, sec, us);
57sysfs_time_stats_attribute(btree_sort, ms, us);
58sysfs_time_stats_attribute(btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -070059
60read_attribute(btree_nodes);
61read_attribute(btree_used_percent);
62read_attribute(average_key_size);
63read_attribute(dirty_data);
64read_attribute(bset_tree_stats);
65
66read_attribute(state);
67read_attribute(cache_read_races);
68read_attribute(writeback_keys_done);
69read_attribute(writeback_keys_failed);
70read_attribute(io_errors);
71read_attribute(congested);
72rw_attribute(congested_read_threshold_us);
73rw_attribute(congested_write_threshold_us);
74
75rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -070076rw_attribute(data_csum);
77rw_attribute(cache_mode);
78rw_attribute(writeback_metadata);
79rw_attribute(writeback_running);
80rw_attribute(writeback_percent);
81rw_attribute(writeback_delay);
82rw_attribute(writeback_rate);
83
84rw_attribute(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -070085rw_attribute(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070086rw_attribute(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -070087rw_attribute(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -070088read_attribute(writeback_rate_debug);
89
Kent Overstreet72c27062013-06-05 06:24:39 -070090read_attribute(stripe_size);
91read_attribute(partial_stripes_expensive);
92
Kent Overstreetcafe5632013-03-23 16:11:31 -070093rw_attribute(synchronous);
94rw_attribute(journal_delay_ms);
95rw_attribute(discard);
96rw_attribute(running);
97rw_attribute(label);
98rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -070099rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700100rw_attribute(io_error_limit);
101rw_attribute(io_error_halflife);
102rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700103rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700104rw_attribute(key_merging_disabled);
105rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700106rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107rw_attribute(cache_replacement_policy);
108rw_attribute(btree_shrinker_disabled);
109rw_attribute(copy_gc_enabled);
110rw_attribute(size);
111
112SHOW(__bch_cached_dev)
113{
114 struct cached_dev *dc = container_of(kobj, struct cached_dev,
115 disk.kobj);
116 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
117
118#define var(stat) (dc->stat)
119
120 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600121 return bch_snprint_string_list(buf, PAGE_SIZE,
122 bch_cache_modes + 1,
123 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700124
125 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
126 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700127 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700128 var_printf(writeback_metadata, "%i");
129 var_printf(writeback_running, "%i");
130 var_print(writeback_delay);
131 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800132 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700133
134 var_print(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700135 var_print(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136 var_print(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700137 var_print(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700138
139 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800140 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800143 char proportional[20];
Michael Lyle1d316e62017-10-13 16:35:36 -0700144 char integral[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800145 char change[20];
146 s64 next_io;
147
148 bch_hprint(rate, dc->writeback_rate.rate << 9);
149 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600150 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800151 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
Michael Lyle1d316e62017-10-13 16:35:36 -0700152 bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800153 bch_hprint(change, dc->writeback_rate_change << 9);
154
155 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
156 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700157
158 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800159 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700160 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800161 "target:\t\t%s\n"
162 "proportional:\t%s\n"
Michael Lyle1d316e62017-10-13 16:35:36 -0700163 "integral:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800164 "change:\t\t%s/sec\n"
165 "next io:\t%llims\n",
166 rate, dirty, target, proportional,
Michael Lyle1d316e62017-10-13 16:35:36 -0700167 integral, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700168 }
169
170 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700171 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700173 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700174 var_printf(partial_stripes_expensive, "%u");
175
Kent Overstreetcafe5632013-03-23 16:11:31 -0700176 var_hprint(sequential_cutoff);
177 var_hprint(readahead);
178
179 sysfs_print(running, atomic_read(&dc->running));
180 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
181
182 if (attr == &sysfs_label) {
183 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
184 buf[SB_LABEL_SIZE + 1] = '\0';
185 strcat(buf, "\n");
186 return strlen(buf);
187 }
188
189#undef var
190 return 0;
191}
192SHOW_LOCKED(bch_cached_dev)
193
194STORE(__cached_dev)
195{
196 struct cached_dev *dc = container_of(kobj, struct cached_dev,
197 disk.kobj);
Tony Asleson77fa1002017-09-06 14:25:57 +0800198 ssize_t v = size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700199 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200200 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201
202#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800203#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
205
206 sysfs_strtoul(data_csum, dc->disk.data_csum);
207 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700208 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209 d_strtoul(writeback_metadata);
210 d_strtoul(writeback_running);
211 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800212
Kent Overstreetcafe5632013-03-23 16:11:31 -0700213 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
214
Kent Overstreet16749c22013-11-11 13:58:34 -0800215 sysfs_strtoul_clamp(writeback_rate,
216 dc->writeback_rate.rate, 1, INT_MAX);
217
218 d_strtoul_nonzero(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700219 d_strtoul(writeback_rate_i_term_inverse);
Kent Overstreet16749c22013-11-11 13:58:34 -0800220 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700221
Kent Overstreetcafe5632013-03-23 16:11:31 -0700222 d_strtoi_h(sequential_cutoff);
223 d_strtoi_h(readahead);
224
225 if (attr == &sysfs_clear_stats)
226 bch_cache_accounting_clear(&dc->accounting);
227
228 if (attr == &sysfs_running &&
229 strtoul_or_return(buf))
230 bch_cached_dev_run(dc);
231
232 if (attr == &sysfs_cache_mode) {
Tony Asleson77fa1002017-09-06 14:25:57 +0800233 v = bch_read_string_list(buf, bch_cache_modes + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700234
235 if (v < 0)
236 return v;
237
238 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
239 SET_BDEV_CACHE_MODE(&dc->sb, v);
240 bch_write_bdev_super(dc, NULL);
241 }
242 }
243
244 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700245 if (size > SB_LABEL_SIZE)
246 return -EINVAL;
247 memcpy(dc->sb.label, buf, size);
248 if (size < SB_LABEL_SIZE)
249 dc->sb.label[size] = '\0';
250 if (size && dc->sb.label[size - 1] == '\n')
251 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700252 bch_write_bdev_super(dc, NULL);
253 if (dc->disk.c) {
254 memcpy(dc->disk.c->uuids[dc->disk.id].label,
255 buf, SB_LABEL_SIZE);
256 bch_uuid_write(dc->disk.c);
257 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200258 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300259 if (!env)
260 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200261 add_uevent_var(env, "DRIVER=bcache");
262 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
263 add_uevent_var(env, "CACHED_LABEL=%s", buf);
264 kobject_uevent_env(
265 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
266 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700267 }
268
269 if (attr == &sysfs_attach) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600270 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700271 return -EINVAL;
272
273 list_for_each_entry(c, &bch_cache_sets, list) {
274 v = bch_cached_dev_attach(dc, c);
275 if (!v)
276 return size;
277 }
278
279 pr_err("Can't attach %s: cache set not found", buf);
280 size = v;
281 }
282
283 if (attr == &sysfs_detach && dc->disk.c)
284 bch_cached_dev_detach(dc);
285
286 if (attr == &sysfs_stop)
287 bcache_device_stop(&dc->disk);
288
289 return size;
290}
291
292STORE(bch_cached_dev)
293{
294 struct cached_dev *dc = container_of(kobj, struct cached_dev,
295 disk.kobj);
296
297 mutex_lock(&bch_register_lock);
298 size = __cached_dev_store(kobj, attr, buf, size);
299
300 if (attr == &sysfs_writeback_running)
301 bch_writeback_queue(dc);
302
303 if (attr == &sysfs_writeback_percent)
304 schedule_delayed_work(&dc->writeback_rate_update,
305 dc->writeback_rate_update_seconds * HZ);
306
307 mutex_unlock(&bch_register_lock);
308 return size;
309}
310
311static struct attribute *bch_cached_dev_files[] = {
312 &sysfs_attach,
313 &sysfs_detach,
314 &sysfs_stop,
315#if 0
316 &sysfs_data_csum,
317#endif
318 &sysfs_cache_mode,
319 &sysfs_writeback_metadata,
320 &sysfs_writeback_running,
321 &sysfs_writeback_delay,
322 &sysfs_writeback_percent,
323 &sysfs_writeback_rate,
324 &sysfs_writeback_rate_update_seconds,
Michael Lyle1d316e62017-10-13 16:35:36 -0700325 &sysfs_writeback_rate_i_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700326 &sysfs_writeback_rate_p_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700327 &sysfs_writeback_rate_debug,
328 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700329 &sysfs_stripe_size,
330 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700331 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700332 &sysfs_clear_stats,
333 &sysfs_running,
334 &sysfs_state,
335 &sysfs_label,
336 &sysfs_readahead,
337#ifdef CONFIG_BCACHE_DEBUG
338 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700339 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700340#endif
341 NULL
342};
343KTYPE(bch_cached_dev);
344
345SHOW(bch_flash_dev)
346{
347 struct bcache_device *d = container_of(kobj, struct bcache_device,
348 kobj);
349 struct uuid_entry *u = &d->c->uuids[d->id];
350
351 sysfs_printf(data_csum, "%i", d->data_csum);
352 sysfs_hprint(size, u->sectors << 9);
353
354 if (attr == &sysfs_label) {
355 memcpy(buf, u->label, SB_LABEL_SIZE);
356 buf[SB_LABEL_SIZE + 1] = '\0';
357 strcat(buf, "\n");
358 return strlen(buf);
359 }
360
361 return 0;
362}
363
364STORE(__bch_flash_dev)
365{
366 struct bcache_device *d = container_of(kobj, struct bcache_device,
367 kobj);
368 struct uuid_entry *u = &d->c->uuids[d->id];
369
370 sysfs_strtoul(data_csum, d->data_csum);
371
372 if (attr == &sysfs_size) {
373 uint64_t v;
374 strtoi_h_or_return(buf, v);
375
376 u->sectors = v >> 9;
377 bch_uuid_write(d->c);
378 set_capacity(d->disk, u->sectors);
379 }
380
381 if (attr == &sysfs_label) {
382 memcpy(u->label, buf, SB_LABEL_SIZE);
383 bch_uuid_write(d->c);
384 }
385
386 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700387 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700388 bcache_device_stop(d);
389 }
390
391 return size;
392}
393STORE_LOCKED(bch_flash_dev)
394
395static struct attribute *bch_flash_dev_files[] = {
396 &sysfs_unregister,
397#if 0
398 &sysfs_data_csum,
399#endif
400 &sysfs_label,
401 &sysfs_size,
402 NULL
403};
404KTYPE(bch_flash_dev);
405
Kent Overstreetf67342d2013-11-11 19:25:55 -0800406struct bset_stats_op {
407 struct btree_op op;
408 size_t nodes;
409 struct bset_stats stats;
410};
411
John Sheucb851142014-03-17 23:13:56 -0700412static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800413{
414 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
415
416 op->nodes++;
417 bch_btree_keys_stats(&b->keys, &op->stats);
418
419 return MAP_CONTINUE;
420}
421
Kent Overstreet35723242014-01-10 18:53:02 -0800422static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800423{
424 struct bset_stats_op op;
425 int ret;
426
427 memset(&op, 0, sizeof(op));
428 bch_btree_op_init(&op.op, -1);
429
John Sheucb851142014-03-17 23:13:56 -0700430 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800431 if (ret < 0)
432 return ret;
433
434 return snprintf(buf, PAGE_SIZE,
435 "btree nodes: %zu\n"
436 "written sets: %zu\n"
437 "unwritten sets: %zu\n"
438 "written key bytes: %zu\n"
439 "unwritten key bytes: %zu\n"
440 "floats: %zu\n"
441 "failed: %zu\n",
442 op.nodes,
443 op.stats.sets_written, op.stats.sets_unwritten,
444 op.stats.bytes_written, op.stats.bytes_unwritten,
445 op.stats.floats, op.stats.failed);
446}
447
John Sheucb851142014-03-17 23:13:56 -0700448static unsigned bch_root_usage(struct cache_set *c)
449{
450 unsigned bytes = 0;
451 struct bkey *k;
452 struct btree *b;
453 struct btree_iter iter;
454
455 goto lock_root;
456
457 do {
458 rw_unlock(false, b);
459lock_root:
460 b = c->root;
461 rw_lock(false, b, b->level);
462 } while (b != c->root);
463
464 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
465 bytes += bkey_bytes(k);
466
467 rw_unlock(false, b);
468
469 return (bytes * 100) / btree_bytes(c);
470}
471
472static size_t bch_cache_size(struct cache_set *c)
473{
474 size_t ret = 0;
475 struct btree *b;
476
477 mutex_lock(&c->bucket_lock);
478 list_for_each_entry(b, &c->btree_cache, list)
479 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
480
481 mutex_unlock(&c->bucket_lock);
482 return ret;
483}
484
485static unsigned bch_cache_max_chain(struct cache_set *c)
486{
487 unsigned ret = 0;
488 struct hlist_head *h;
489
490 mutex_lock(&c->bucket_lock);
491
492 for (h = c->bucket_hash;
493 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
494 h++) {
495 unsigned i = 0;
496 struct hlist_node *p;
497
498 hlist_for_each(p, h)
499 i++;
500
501 ret = max(ret, i);
502 }
503
504 mutex_unlock(&c->bucket_lock);
505 return ret;
506}
507
508static unsigned bch_btree_used(struct cache_set *c)
509{
510 return div64_u64(c->gc_stats.key_bytes * 100,
511 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
512}
513
514static unsigned bch_average_key_size(struct cache_set *c)
515{
516 return c->gc_stats.nkeys
517 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
518 : 0;
519}
520
Kent Overstreetcafe5632013-03-23 16:11:31 -0700521SHOW(__bch_cache_set)
522{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700523 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
524
525 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
526 sysfs_print(journal_delay_ms, c->journal_delay_ms);
527 sysfs_hprint(bucket_size, bucket_bytes(c));
528 sysfs_hprint(block_size, block_bytes(c));
529 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700530 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700531
John Sheucb851142014-03-17 23:13:56 -0700532 sysfs_hprint(btree_cache_size, bch_cache_size(c));
533 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700534 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
535
536 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
537 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700538 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700539 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700540
John Sheucb851142014-03-17 23:13:56 -0700541 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700542 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700543 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700544
545 sysfs_print(cache_read_races,
546 atomic_long_read(&c->cache_read_races));
547
548 sysfs_print(writeback_keys_done,
549 atomic_long_read(&c->writeback_keys_done));
550 sysfs_print(writeback_keys_failed,
551 atomic_long_read(&c->writeback_keys_failed));
552
Kent Overstreet77c320e2013-07-11 19:42:51 -0700553 if (attr == &sysfs_errors)
554 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
555 c->on_error);
556
Kent Overstreetcafe5632013-03-23 16:11:31 -0700557 /* See count_io_errors for why 88 */
558 sysfs_print(io_error_halflife, c->error_decay * 88);
559 sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
560
561 sysfs_hprint(congested,
562 ((uint64_t) bch_get_congested(c)) << 9);
563 sysfs_print(congested_read_threshold_us,
564 c->congested_read_threshold_us);
565 sysfs_print(congested_write_threshold_us,
566 c->congested_write_threshold_us);
567
568 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
569 sysfs_printf(verify, "%i", c->verify);
570 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700571 sysfs_printf(expensive_debug_checks,
572 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700573 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
574 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
575 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
576
577 if (attr == &sysfs_bset_tree_stats)
578 return bch_bset_print_stats(c, buf);
579
580 return 0;
581}
582SHOW_LOCKED(bch_cache_set)
583
584STORE(__bch_cache_set)
585{
586 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
587
588 if (attr == &sysfs_unregister)
589 bch_cache_set_unregister(c);
590
591 if (attr == &sysfs_stop)
592 bch_cache_set_stop(c);
593
594 if (attr == &sysfs_synchronous) {
595 bool sync = strtoul_or_return(buf);
596
597 if (sync != CACHE_SYNC(&c->sb)) {
598 SET_CACHE_SYNC(&c->sb, sync);
599 bcache_write_super(c);
600 }
601 }
602
603 if (attr == &sysfs_flash_vol_create) {
604 int r;
605 uint64_t v;
606 strtoi_h_or_return(buf, v);
607
608 r = bch_flash_dev_create(c, v);
609 if (r)
610 return r;
611 }
612
613 if (attr == &sysfs_clear_stats) {
614 atomic_long_set(&c->writeback_keys_done, 0);
615 atomic_long_set(&c->writeback_keys_failed, 0);
616
617 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
618 bch_cache_accounting_clear(&c->accounting);
619 }
620
Tang Junhui0b43f492017-09-06 14:25:55 +0800621 if (attr == &sysfs_trigger_gc) {
622 /*
623 * Garbage collection thread only works when sectors_to_gc < 0,
624 * when users write to sysfs entry trigger_gc, most of time
625 * they want to forcibly triger gargage collection. Here -1 is
626 * set to c->sectors_to_gc, to make gc_should_run() give a
627 * chance to permit gc thread to run. "give a chance" means
628 * before going into gc_should_run(), there is still chance
629 * that c->sectors_to_gc being set to other positive value. So
630 * writing sysfs entry trigger_gc won't always make sure gc
631 * thread takes effect.
632 */
633 atomic_set(&c->sectors_to_gc, -1);
Kent Overstreet72a44512013-10-24 17:19:26 -0700634 wake_up_gc(c);
Tang Junhui0b43f492017-09-06 14:25:55 +0800635 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700636
637 if (attr == &sysfs_prune_cache) {
638 struct shrink_control sc;
639 sc.gfp_mask = GFP_KERNEL;
640 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000641 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700642 }
643
644 sysfs_strtoul(congested_read_threshold_us,
645 c->congested_read_threshold_us);
646 sysfs_strtoul(congested_write_threshold_us,
647 c->congested_write_threshold_us);
648
Kent Overstreet77c320e2013-07-11 19:42:51 -0700649 if (attr == &sysfs_errors) {
650 ssize_t v = bch_read_string_list(buf, error_actions);
651
652 if (v < 0)
653 return v;
654
655 c->on_error = v;
656 }
657
Kent Overstreetcafe5632013-03-23 16:11:31 -0700658 if (attr == &sysfs_io_error_limit)
659 c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
660
661 /* See count_io_errors() for why 88 */
662 if (attr == &sysfs_io_error_halflife)
663 c->error_decay = strtoul_or_return(buf) / 88;
664
665 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
666 sysfs_strtoul(verify, c->verify);
667 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700668 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700669 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
670 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
671 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
672
673 return size;
674}
675STORE_LOCKED(bch_cache_set)
676
677SHOW(bch_cache_set_internal)
678{
679 struct cache_set *c = container_of(kobj, struct cache_set, internal);
680 return bch_cache_set_show(&c->kobj, attr, buf);
681}
682
683STORE(bch_cache_set_internal)
684{
685 struct cache_set *c = container_of(kobj, struct cache_set, internal);
686 return bch_cache_set_store(&c->kobj, attr, buf, size);
687}
688
689static void bch_cache_set_internal_release(struct kobject *k)
690{
691}
692
693static struct attribute *bch_cache_set_files[] = {
694 &sysfs_unregister,
695 &sysfs_stop,
696 &sysfs_synchronous,
697 &sysfs_journal_delay_ms,
698 &sysfs_flash_vol_create,
699
700 &sysfs_bucket_size,
701 &sysfs_block_size,
702 &sysfs_tree_depth,
703 &sysfs_root_usage_percent,
704 &sysfs_btree_cache_size,
705 &sysfs_cache_available_percent,
706
707 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700708
Kent Overstreet77c320e2013-07-11 19:42:51 -0700709 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 &sysfs_io_error_limit,
711 &sysfs_io_error_halflife,
712 &sysfs_congested,
713 &sysfs_congested_read_threshold_us,
714 &sysfs_congested_write_threshold_us,
715 &sysfs_clear_stats,
716 NULL
717};
718KTYPE(bch_cache_set);
719
720static struct attribute *bch_cache_set_internal_files[] = {
721 &sysfs_active_journal_entries,
722
723 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
724 sysfs_time_stats_attribute_list(btree_split, sec, us)
725 sysfs_time_stats_attribute_list(btree_sort, ms, us)
726 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727
728 &sysfs_btree_nodes,
729 &sysfs_btree_used_percent,
730 &sysfs_btree_cache_max_chain,
731
732 &sysfs_bset_tree_stats,
733 &sysfs_cache_read_races,
734 &sysfs_writeback_keys_done,
735 &sysfs_writeback_keys_failed,
736
737 &sysfs_trigger_gc,
738 &sysfs_prune_cache,
739#ifdef CONFIG_BCACHE_DEBUG
740 &sysfs_verify,
741 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700742 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700743#endif
744 &sysfs_gc_always_rewrite,
745 &sysfs_btree_shrinker_disabled,
746 &sysfs_copy_gc_enabled,
747 NULL
748};
749KTYPE(bch_cache_set_internal);
750
Peter Foley58f913d2017-10-13 16:35:28 -0700751static int __bch_cache_cmp(const void *l, const void *r)
752{
753 return *((uint16_t *)r) - *((uint16_t *)l);
754}
755
Kent Overstreetcafe5632013-03-23 16:11:31 -0700756SHOW(__bch_cache)
757{
758 struct cache *ca = container_of(kobj, struct cache, kobj);
759
760 sysfs_hprint(bucket_size, bucket_bytes(ca));
761 sysfs_hprint(block_size, block_bytes(ca));
762 sysfs_print(nbuckets, ca->sb.nbuckets);
763 sysfs_print(discard, ca->discard);
764 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
765 sysfs_hprint(btree_written,
766 atomic_long_read(&ca->btree_sectors_written) << 9);
767 sysfs_hprint(metadata_written,
768 (atomic_long_read(&ca->meta_sectors_written) +
769 atomic_long_read(&ca->btree_sectors_written)) << 9);
770
771 sysfs_print(io_errors,
772 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
773
Kent Overstreetcafe5632013-03-23 16:11:31 -0700774 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600775 return bch_snprint_string_list(buf, PAGE_SIZE,
776 cache_replacement_policies,
777 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700778
779 if (attr == &sysfs_priority_stats) {
Kent Overstreet15754022014-02-25 17:34:21 -0800780 struct bucket *b;
781 size_t n = ca->sb.nbuckets, i;
782 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700783 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700784 /* Compute 31 quantiles */
785 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700786 ssize_t ret;
787
788 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
789 if (!p)
790 return -ENOMEM;
791
792 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800793 for_each_bucket(b, ca) {
794 if (!GC_SECTORS_USED(b))
795 unused++;
796 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
797 available++;
798 if (GC_MARK(b) == GC_MARK_DIRTY)
799 dirty++;
800 if (GC_MARK(b) == GC_MARK_METADATA)
801 meta++;
802 }
803
Kent Overstreetcafe5632013-03-23 16:11:31 -0700804 for (i = ca->sb.first_bucket; i < n; i++)
805 p[i] = ca->buckets[i].prio;
806 mutex_unlock(&ca->set->bucket_lock);
807
Peter Foley58f913d2017-10-13 16:35:28 -0700808 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700809
810 while (n &&
811 !cached[n - 1])
812 --n;
813
814 unused = ca->sb.nbuckets - n;
815
816 while (cached < p + n &&
817 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800818 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819
820 for (i = 0; i < n; i++)
821 sum += INITIAL_PRIO - cached[i];
822
823 if (n)
824 do_div(sum, n);
825
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700826 for (i = 0; i < ARRAY_SIZE(q); i++)
827 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
828 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700829
830 vfree(p);
831
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700832 ret = scnprintf(buf, PAGE_SIZE,
833 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800834 "Clean: %zu%%\n"
835 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700836 "Metadata: %zu%%\n"
837 "Average: %llu\n"
838 "Sectors per Q: %zu\n"
839 "Quantiles: [",
840 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800841 available * 100 / (size_t) ca->sb.nbuckets,
842 dirty * 100 / (size_t) ca->sb.nbuckets,
843 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700844 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700845
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700846 for (i = 0; i < ARRAY_SIZE(q); i++)
847 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
848 "%u ", q[i]);
849 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700850
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700851 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
852
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853 return ret;
854 }
855
856 return 0;
857}
858SHOW_LOCKED(bch_cache)
859
860STORE(__bch_cache)
861{
862 struct cache *ca = container_of(kobj, struct cache, kobj);
863
864 if (attr == &sysfs_discard) {
865 bool v = strtoul_or_return(buf);
866
867 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
868 ca->discard = v;
869
870 if (v != CACHE_DISCARD(&ca->sb)) {
871 SET_CACHE_DISCARD(&ca->sb, v);
872 bcache_write_super(ca->set);
873 }
874 }
875
876 if (attr == &sysfs_cache_replacement_policy) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600877 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700878
879 if (v < 0)
880 return v;
881
882 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
883 mutex_lock(&ca->set->bucket_lock);
884 SET_CACHE_REPLACEMENT(&ca->sb, v);
885 mutex_unlock(&ca->set->bucket_lock);
886
887 bcache_write_super(ca->set);
888 }
889 }
890
Kent Overstreetcafe5632013-03-23 16:11:31 -0700891 if (attr == &sysfs_clear_stats) {
892 atomic_long_set(&ca->sectors_written, 0);
893 atomic_long_set(&ca->btree_sectors_written, 0);
894 atomic_long_set(&ca->meta_sectors_written, 0);
895 atomic_set(&ca->io_count, 0);
896 atomic_set(&ca->io_errors, 0);
897 }
898
899 return size;
900}
901STORE_LOCKED(bch_cache)
902
903static struct attribute *bch_cache_files[] = {
904 &sysfs_bucket_size,
905 &sysfs_block_size,
906 &sysfs_nbuckets,
907 &sysfs_priority_stats,
908 &sysfs_discard,
909 &sysfs_written,
910 &sysfs_btree_written,
911 &sysfs_metadata_written,
912 &sysfs_io_errors,
913 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700914 &sysfs_cache_replacement_policy,
915 NULL
916};
917KTYPE(bch_cache);