blob: a7552f509f50ed1cebf12fd8a763558f47da67cc [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);
Tang Junhuia728eac2018-02-07 11:41:39 -080068read_attribute(reclaim);
69read_attribute(flush_write);
70read_attribute(retry_flush_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -070071read_attribute(writeback_keys_done);
72read_attribute(writeback_keys_failed);
73read_attribute(io_errors);
74read_attribute(congested);
75rw_attribute(congested_read_threshold_us);
76rw_attribute(congested_write_threshold_us);
77
78rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -070079rw_attribute(data_csum);
80rw_attribute(cache_mode);
81rw_attribute(writeback_metadata);
82rw_attribute(writeback_running);
83rw_attribute(writeback_percent);
84rw_attribute(writeback_delay);
85rw_attribute(writeback_rate);
86
87rw_attribute(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -070088rw_attribute(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070089rw_attribute(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -070090rw_attribute(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -070091read_attribute(writeback_rate_debug);
92
Kent Overstreet72c27062013-06-05 06:24:39 -070093read_attribute(stripe_size);
94read_attribute(partial_stripes_expensive);
95
Kent Overstreetcafe5632013-03-23 16:11:31 -070096rw_attribute(synchronous);
97rw_attribute(journal_delay_ms);
98rw_attribute(discard);
99rw_attribute(running);
100rw_attribute(label);
101rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -0700102rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700103rw_attribute(io_error_limit);
104rw_attribute(io_error_halflife);
105rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700106rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107rw_attribute(key_merging_disabled);
108rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700109rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110rw_attribute(cache_replacement_policy);
111rw_attribute(btree_shrinker_disabled);
112rw_attribute(copy_gc_enabled);
113rw_attribute(size);
114
115SHOW(__bch_cached_dev)
116{
117 struct cached_dev *dc = container_of(kobj, struct cached_dev,
118 disk.kobj);
119 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
120
121#define var(stat) (dc->stat)
122
123 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600124 return bch_snprint_string_list(buf, PAGE_SIZE,
125 bch_cache_modes + 1,
126 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700127
128 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
129 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700130 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700131 var_printf(writeback_metadata, "%i");
132 var_printf(writeback_running, "%i");
133 var_print(writeback_delay);
134 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800135 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136
137 var_print(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700138 var_print(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139 var_print(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700140 var_print(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141
142 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800143 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700144 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700145 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800146 char proportional[20];
Michael Lyle1d316e62017-10-13 16:35:36 -0700147 char integral[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800148 char change[20];
149 s64 next_io;
150
151 bch_hprint(rate, dc->writeback_rate.rate << 9);
152 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600153 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800154 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
Michael Lyle1d316e62017-10-13 16:35:36 -0700155 bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800156 bch_hprint(change, dc->writeback_rate_change << 9);
157
158 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
159 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700160
161 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800162 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700163 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800164 "target:\t\t%s\n"
165 "proportional:\t%s\n"
Michael Lyle1d316e62017-10-13 16:35:36 -0700166 "integral:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800167 "change:\t\t%s/sec\n"
168 "next io:\t%llims\n",
169 rate, dirty, target, proportional,
Michael Lyle1d316e62017-10-13 16:35:36 -0700170 integral, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700171 }
172
173 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700174 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700175
Kent Overstreet2d679fc2013-08-17 02:13:15 -0700176 sysfs_hprint(stripe_size, dc->disk.stripe_size << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700177 var_printf(partial_stripes_expensive, "%u");
178
Kent Overstreetcafe5632013-03-23 16:11:31 -0700179 var_hprint(sequential_cutoff);
180 var_hprint(readahead);
181
182 sysfs_print(running, atomic_read(&dc->running));
183 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
184
185 if (attr == &sysfs_label) {
186 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
187 buf[SB_LABEL_SIZE + 1] = '\0';
188 strcat(buf, "\n");
189 return strlen(buf);
190 }
191
192#undef var
193 return 0;
194}
195SHOW_LOCKED(bch_cached_dev)
196
197STORE(__cached_dev)
198{
199 struct cached_dev *dc = container_of(kobj, struct cached_dev,
200 disk.kobj);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800201 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700202 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200203 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204
205#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800206#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
208
209 sysfs_strtoul(data_csum, dc->disk.data_csum);
210 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700211 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700212 d_strtoul(writeback_metadata);
213 d_strtoul(writeback_running);
214 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800215
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
217
Kent Overstreet16749c22013-11-11 13:58:34 -0800218 sysfs_strtoul_clamp(writeback_rate,
219 dc->writeback_rate.rate, 1, INT_MAX);
220
Coly Li7a5e3ec2018-02-07 11:41:44 -0800221 sysfs_strtoul_clamp(writeback_rate_update_seconds,
222 dc->writeback_rate_update_seconds,
223 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
Michael Lyle1d316e62017-10-13 16:35:36 -0700224 d_strtoul(writeback_rate_i_term_inverse);
Kent Overstreet16749c22013-11-11 13:58:34 -0800225 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700226
Kent Overstreetcafe5632013-03-23 16:11:31 -0700227 d_strtoi_h(sequential_cutoff);
228 d_strtoi_h(readahead);
229
230 if (attr == &sysfs_clear_stats)
231 bch_cache_accounting_clear(&dc->accounting);
232
233 if (attr == &sysfs_running &&
234 strtoul_or_return(buf))
235 bch_cached_dev_run(dc);
236
237 if (attr == &sysfs_cache_mode) {
Tony Asleson77fa1002017-09-06 14:25:57 +0800238 v = bch_read_string_list(buf, bch_cache_modes + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239
240 if (v < 0)
241 return v;
242
243 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
244 SET_BDEV_CACHE_MODE(&dc->sb, v);
245 bch_write_bdev_super(dc, NULL);
246 }
247 }
248
249 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700250 if (size > SB_LABEL_SIZE)
251 return -EINVAL;
252 memcpy(dc->sb.label, buf, size);
253 if (size < SB_LABEL_SIZE)
254 dc->sb.label[size] = '\0';
255 if (size && dc->sb.label[size - 1] == '\n')
256 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700257 bch_write_bdev_super(dc, NULL);
258 if (dc->disk.c) {
259 memcpy(dc->disk.c->uuids[dc->disk.id].label,
260 buf, SB_LABEL_SIZE);
261 bch_uuid_write(dc->disk.c);
262 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200263 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300264 if (!env)
265 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200266 add_uevent_var(env, "DRIVER=bcache");
267 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
268 add_uevent_var(env, "CACHED_LABEL=%s", buf);
269 kobject_uevent_env(
270 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
271 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700272 }
273
274 if (attr == &sysfs_attach) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600275 if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276 return -EINVAL;
277
Tang Junhui7f4fc932018-02-07 11:41:45 -0800278 v = -ENOENT;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279 list_for_each_entry(c, &bch_cache_sets, list) {
280 v = bch_cached_dev_attach(dc, c);
281 if (!v)
282 return size;
283 }
284
285 pr_err("Can't attach %s: cache set not found", buf);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800286 return v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700287 }
288
289 if (attr == &sysfs_detach && dc->disk.c)
290 bch_cached_dev_detach(dc);
291
292 if (attr == &sysfs_stop)
293 bcache_device_stop(&dc->disk);
294
295 return size;
296}
297
298STORE(bch_cached_dev)
299{
300 struct cached_dev *dc = container_of(kobj, struct cached_dev,
301 disk.kobj);
302
303 mutex_lock(&bch_register_lock);
304 size = __cached_dev_store(kobj, attr, buf, size);
305
306 if (attr == &sysfs_writeback_running)
307 bch_writeback_queue(dc);
308
309 if (attr == &sysfs_writeback_percent)
310 schedule_delayed_work(&dc->writeback_rate_update,
311 dc->writeback_rate_update_seconds * HZ);
312
313 mutex_unlock(&bch_register_lock);
314 return size;
315}
316
317static struct attribute *bch_cached_dev_files[] = {
318 &sysfs_attach,
319 &sysfs_detach,
320 &sysfs_stop,
321#if 0
322 &sysfs_data_csum,
323#endif
324 &sysfs_cache_mode,
325 &sysfs_writeback_metadata,
326 &sysfs_writeback_running,
327 &sysfs_writeback_delay,
328 &sysfs_writeback_percent,
329 &sysfs_writeback_rate,
330 &sysfs_writeback_rate_update_seconds,
Michael Lyle1d316e62017-10-13 16:35:36 -0700331 &sysfs_writeback_rate_i_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700332 &sysfs_writeback_rate_p_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700333 &sysfs_writeback_rate_debug,
334 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700335 &sysfs_stripe_size,
336 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700338 &sysfs_clear_stats,
339 &sysfs_running,
340 &sysfs_state,
341 &sysfs_label,
342 &sysfs_readahead,
343#ifdef CONFIG_BCACHE_DEBUG
344 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700345 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700346#endif
347 NULL
348};
349KTYPE(bch_cached_dev);
350
351SHOW(bch_flash_dev)
352{
353 struct bcache_device *d = container_of(kobj, struct bcache_device,
354 kobj);
355 struct uuid_entry *u = &d->c->uuids[d->id];
356
357 sysfs_printf(data_csum, "%i", d->data_csum);
358 sysfs_hprint(size, u->sectors << 9);
359
360 if (attr == &sysfs_label) {
361 memcpy(buf, u->label, SB_LABEL_SIZE);
362 buf[SB_LABEL_SIZE + 1] = '\0';
363 strcat(buf, "\n");
364 return strlen(buf);
365 }
366
367 return 0;
368}
369
370STORE(__bch_flash_dev)
371{
372 struct bcache_device *d = container_of(kobj, struct bcache_device,
373 kobj);
374 struct uuid_entry *u = &d->c->uuids[d->id];
375
376 sysfs_strtoul(data_csum, d->data_csum);
377
378 if (attr == &sysfs_size) {
379 uint64_t v;
380 strtoi_h_or_return(buf, v);
381
382 u->sectors = v >> 9;
383 bch_uuid_write(d->c);
384 set_capacity(d->disk, u->sectors);
385 }
386
387 if (attr == &sysfs_label) {
388 memcpy(u->label, buf, SB_LABEL_SIZE);
389 bch_uuid_write(d->c);
390 }
391
392 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700393 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700394 bcache_device_stop(d);
395 }
396
397 return size;
398}
399STORE_LOCKED(bch_flash_dev)
400
401static struct attribute *bch_flash_dev_files[] = {
402 &sysfs_unregister,
403#if 0
404 &sysfs_data_csum,
405#endif
406 &sysfs_label,
407 &sysfs_size,
408 NULL
409};
410KTYPE(bch_flash_dev);
411
Kent Overstreetf67342d2013-11-11 19:25:55 -0800412struct bset_stats_op {
413 struct btree_op op;
414 size_t nodes;
415 struct bset_stats stats;
416};
417
John Sheucb851142014-03-17 23:13:56 -0700418static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800419{
420 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
421
422 op->nodes++;
423 bch_btree_keys_stats(&b->keys, &op->stats);
424
425 return MAP_CONTINUE;
426}
427
Kent Overstreet35723242014-01-10 18:53:02 -0800428static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800429{
430 struct bset_stats_op op;
431 int ret;
432
433 memset(&op, 0, sizeof(op));
434 bch_btree_op_init(&op.op, -1);
435
John Sheucb851142014-03-17 23:13:56 -0700436 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800437 if (ret < 0)
438 return ret;
439
440 return snprintf(buf, PAGE_SIZE,
441 "btree nodes: %zu\n"
442 "written sets: %zu\n"
443 "unwritten sets: %zu\n"
444 "written key bytes: %zu\n"
445 "unwritten key bytes: %zu\n"
446 "floats: %zu\n"
447 "failed: %zu\n",
448 op.nodes,
449 op.stats.sets_written, op.stats.sets_unwritten,
450 op.stats.bytes_written, op.stats.bytes_unwritten,
451 op.stats.floats, op.stats.failed);
452}
453
John Sheucb851142014-03-17 23:13:56 -0700454static unsigned bch_root_usage(struct cache_set *c)
455{
456 unsigned bytes = 0;
457 struct bkey *k;
458 struct btree *b;
459 struct btree_iter iter;
460
461 goto lock_root;
462
463 do {
464 rw_unlock(false, b);
465lock_root:
466 b = c->root;
467 rw_lock(false, b, b->level);
468 } while (b != c->root);
469
470 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
471 bytes += bkey_bytes(k);
472
473 rw_unlock(false, b);
474
475 return (bytes * 100) / btree_bytes(c);
476}
477
478static size_t bch_cache_size(struct cache_set *c)
479{
480 size_t ret = 0;
481 struct btree *b;
482
483 mutex_lock(&c->bucket_lock);
484 list_for_each_entry(b, &c->btree_cache, list)
485 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
486
487 mutex_unlock(&c->bucket_lock);
488 return ret;
489}
490
491static unsigned bch_cache_max_chain(struct cache_set *c)
492{
493 unsigned ret = 0;
494 struct hlist_head *h;
495
496 mutex_lock(&c->bucket_lock);
497
498 for (h = c->bucket_hash;
499 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
500 h++) {
501 unsigned i = 0;
502 struct hlist_node *p;
503
504 hlist_for_each(p, h)
505 i++;
506
507 ret = max(ret, i);
508 }
509
510 mutex_unlock(&c->bucket_lock);
511 return ret;
512}
513
514static unsigned bch_btree_used(struct cache_set *c)
515{
516 return div64_u64(c->gc_stats.key_bytes * 100,
517 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
518}
519
520static unsigned bch_average_key_size(struct cache_set *c)
521{
522 return c->gc_stats.nkeys
523 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
524 : 0;
525}
526
Kent Overstreetcafe5632013-03-23 16:11:31 -0700527SHOW(__bch_cache_set)
528{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700529 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
530
531 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
532 sysfs_print(journal_delay_ms, c->journal_delay_ms);
533 sysfs_hprint(bucket_size, bucket_bytes(c));
534 sysfs_hprint(block_size, block_bytes(c));
535 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700536 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700537
John Sheucb851142014-03-17 23:13:56 -0700538 sysfs_hprint(btree_cache_size, bch_cache_size(c));
539 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700540 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
541
542 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
543 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700544 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700545 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700546
John Sheucb851142014-03-17 23:13:56 -0700547 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700548 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700549 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700550
551 sysfs_print(cache_read_races,
552 atomic_long_read(&c->cache_read_races));
553
Tang Junhuia728eac2018-02-07 11:41:39 -0800554 sysfs_print(reclaim,
555 atomic_long_read(&c->reclaim));
556
557 sysfs_print(flush_write,
558 atomic_long_read(&c->flush_write));
559
560 sysfs_print(retry_flush_write,
561 atomic_long_read(&c->retry_flush_write));
562
Kent Overstreetcafe5632013-03-23 16:11:31 -0700563 sysfs_print(writeback_keys_done,
564 atomic_long_read(&c->writeback_keys_done));
565 sysfs_print(writeback_keys_failed,
566 atomic_long_read(&c->writeback_keys_failed));
567
Kent Overstreet77c320e2013-07-11 19:42:51 -0700568 if (attr == &sysfs_errors)
569 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
570 c->on_error);
571
Kent Overstreetcafe5632013-03-23 16:11:31 -0700572 /* See count_io_errors for why 88 */
573 sysfs_print(io_error_halflife, c->error_decay * 88);
Coly Li7ba0d832018-02-07 11:41:42 -0800574 sysfs_print(io_error_limit, c->error_limit);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700575
576 sysfs_hprint(congested,
577 ((uint64_t) bch_get_congested(c)) << 9);
578 sysfs_print(congested_read_threshold_us,
579 c->congested_read_threshold_us);
580 sysfs_print(congested_write_threshold_us,
581 c->congested_write_threshold_us);
582
583 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
584 sysfs_printf(verify, "%i", c->verify);
585 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700586 sysfs_printf(expensive_debug_checks,
587 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700588 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
589 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
590 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
591
592 if (attr == &sysfs_bset_tree_stats)
593 return bch_bset_print_stats(c, buf);
594
595 return 0;
596}
597SHOW_LOCKED(bch_cache_set)
598
599STORE(__bch_cache_set)
600{
601 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
602
603 if (attr == &sysfs_unregister)
604 bch_cache_set_unregister(c);
605
606 if (attr == &sysfs_stop)
607 bch_cache_set_stop(c);
608
609 if (attr == &sysfs_synchronous) {
610 bool sync = strtoul_or_return(buf);
611
612 if (sync != CACHE_SYNC(&c->sb)) {
613 SET_CACHE_SYNC(&c->sb, sync);
614 bcache_write_super(c);
615 }
616 }
617
618 if (attr == &sysfs_flash_vol_create) {
619 int r;
620 uint64_t v;
621 strtoi_h_or_return(buf, v);
622
623 r = bch_flash_dev_create(c, v);
624 if (r)
625 return r;
626 }
627
628 if (attr == &sysfs_clear_stats) {
629 atomic_long_set(&c->writeback_keys_done, 0);
630 atomic_long_set(&c->writeback_keys_failed, 0);
631
632 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
633 bch_cache_accounting_clear(&c->accounting);
634 }
635
Tang Junhui0b43f492017-09-06 14:25:55 +0800636 if (attr == &sysfs_trigger_gc) {
637 /*
638 * Garbage collection thread only works when sectors_to_gc < 0,
639 * when users write to sysfs entry trigger_gc, most of time
640 * they want to forcibly triger gargage collection. Here -1 is
641 * set to c->sectors_to_gc, to make gc_should_run() give a
642 * chance to permit gc thread to run. "give a chance" means
643 * before going into gc_should_run(), there is still chance
644 * that c->sectors_to_gc being set to other positive value. So
645 * writing sysfs entry trigger_gc won't always make sure gc
646 * thread takes effect.
647 */
648 atomic_set(&c->sectors_to_gc, -1);
Kent Overstreet72a44512013-10-24 17:19:26 -0700649 wake_up_gc(c);
Tang Junhui0b43f492017-09-06 14:25:55 +0800650 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700651
652 if (attr == &sysfs_prune_cache) {
653 struct shrink_control sc;
654 sc.gfp_mask = GFP_KERNEL;
655 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000656 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700657 }
658
659 sysfs_strtoul(congested_read_threshold_us,
660 c->congested_read_threshold_us);
661 sysfs_strtoul(congested_write_threshold_us,
662 c->congested_write_threshold_us);
663
Kent Overstreet77c320e2013-07-11 19:42:51 -0700664 if (attr == &sysfs_errors) {
665 ssize_t v = bch_read_string_list(buf, error_actions);
666
667 if (v < 0)
668 return v;
669
670 c->on_error = v;
671 }
672
Kent Overstreetcafe5632013-03-23 16:11:31 -0700673 if (attr == &sysfs_io_error_limit)
Coly Li7ba0d832018-02-07 11:41:42 -0800674 c->error_limit = strtoul_or_return(buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700675
676 /* See count_io_errors() for why 88 */
677 if (attr == &sysfs_io_error_halflife)
678 c->error_decay = strtoul_or_return(buf) / 88;
679
680 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
681 sysfs_strtoul(verify, c->verify);
682 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700683 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700684 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
685 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
686 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
687
688 return size;
689}
690STORE_LOCKED(bch_cache_set)
691
692SHOW(bch_cache_set_internal)
693{
694 struct cache_set *c = container_of(kobj, struct cache_set, internal);
695 return bch_cache_set_show(&c->kobj, attr, buf);
696}
697
698STORE(bch_cache_set_internal)
699{
700 struct cache_set *c = container_of(kobj, struct cache_set, internal);
701 return bch_cache_set_store(&c->kobj, attr, buf, size);
702}
703
704static void bch_cache_set_internal_release(struct kobject *k)
705{
706}
707
708static struct attribute *bch_cache_set_files[] = {
709 &sysfs_unregister,
710 &sysfs_stop,
711 &sysfs_synchronous,
712 &sysfs_journal_delay_ms,
713 &sysfs_flash_vol_create,
714
715 &sysfs_bucket_size,
716 &sysfs_block_size,
717 &sysfs_tree_depth,
718 &sysfs_root_usage_percent,
719 &sysfs_btree_cache_size,
720 &sysfs_cache_available_percent,
721
722 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700723
Kent Overstreet77c320e2013-07-11 19:42:51 -0700724 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700725 &sysfs_io_error_limit,
726 &sysfs_io_error_halflife,
727 &sysfs_congested,
728 &sysfs_congested_read_threshold_us,
729 &sysfs_congested_write_threshold_us,
730 &sysfs_clear_stats,
731 NULL
732};
733KTYPE(bch_cache_set);
734
735static struct attribute *bch_cache_set_internal_files[] = {
736 &sysfs_active_journal_entries,
737
738 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
739 sysfs_time_stats_attribute_list(btree_split, sec, us)
740 sysfs_time_stats_attribute_list(btree_sort, ms, us)
741 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700742
743 &sysfs_btree_nodes,
744 &sysfs_btree_used_percent,
745 &sysfs_btree_cache_max_chain,
746
747 &sysfs_bset_tree_stats,
748 &sysfs_cache_read_races,
Tang Junhuia728eac2018-02-07 11:41:39 -0800749 &sysfs_reclaim,
750 &sysfs_flush_write,
751 &sysfs_retry_flush_write,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700752 &sysfs_writeback_keys_done,
753 &sysfs_writeback_keys_failed,
754
755 &sysfs_trigger_gc,
756 &sysfs_prune_cache,
757#ifdef CONFIG_BCACHE_DEBUG
758 &sysfs_verify,
759 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700760 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700761#endif
762 &sysfs_gc_always_rewrite,
763 &sysfs_btree_shrinker_disabled,
764 &sysfs_copy_gc_enabled,
765 NULL
766};
767KTYPE(bch_cache_set_internal);
768
Peter Foley58f913d2017-10-13 16:35:28 -0700769static int __bch_cache_cmp(const void *l, const void *r)
770{
771 return *((uint16_t *)r) - *((uint16_t *)l);
772}
773
Kent Overstreetcafe5632013-03-23 16:11:31 -0700774SHOW(__bch_cache)
775{
776 struct cache *ca = container_of(kobj, struct cache, kobj);
777
778 sysfs_hprint(bucket_size, bucket_bytes(ca));
779 sysfs_hprint(block_size, block_bytes(ca));
780 sysfs_print(nbuckets, ca->sb.nbuckets);
781 sysfs_print(discard, ca->discard);
782 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
783 sysfs_hprint(btree_written,
784 atomic_long_read(&ca->btree_sectors_written) << 9);
785 sysfs_hprint(metadata_written,
786 (atomic_long_read(&ca->meta_sectors_written) +
787 atomic_long_read(&ca->btree_sectors_written)) << 9);
788
789 sysfs_print(io_errors,
790 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
791
Kent Overstreetcafe5632013-03-23 16:11:31 -0700792 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600793 return bch_snprint_string_list(buf, PAGE_SIZE,
794 cache_replacement_policies,
795 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700796
797 if (attr == &sysfs_priority_stats) {
Kent Overstreet15754022014-02-25 17:34:21 -0800798 struct bucket *b;
799 size_t n = ca->sb.nbuckets, i;
800 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700802 /* Compute 31 quantiles */
803 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700804 ssize_t ret;
805
806 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
807 if (!p)
808 return -ENOMEM;
809
810 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800811 for_each_bucket(b, ca) {
812 if (!GC_SECTORS_USED(b))
813 unused++;
814 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
815 available++;
816 if (GC_MARK(b) == GC_MARK_DIRTY)
817 dirty++;
818 if (GC_MARK(b) == GC_MARK_METADATA)
819 meta++;
820 }
821
Kent Overstreetcafe5632013-03-23 16:11:31 -0700822 for (i = ca->sb.first_bucket; i < n; i++)
823 p[i] = ca->buckets[i].prio;
824 mutex_unlock(&ca->set->bucket_lock);
825
Peter Foley58f913d2017-10-13 16:35:28 -0700826 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827
828 while (n &&
829 !cached[n - 1])
830 --n;
831
832 unused = ca->sb.nbuckets - n;
833
834 while (cached < p + n &&
835 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800836 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700837
838 for (i = 0; i < n; i++)
839 sum += INITIAL_PRIO - cached[i];
840
841 if (n)
842 do_div(sum, n);
843
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700844 for (i = 0; i < ARRAY_SIZE(q); i++)
845 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
846 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700847
848 vfree(p);
849
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700850 ret = scnprintf(buf, PAGE_SIZE,
851 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800852 "Clean: %zu%%\n"
853 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700854 "Metadata: %zu%%\n"
855 "Average: %llu\n"
856 "Sectors per Q: %zu\n"
857 "Quantiles: [",
858 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800859 available * 100 / (size_t) ca->sb.nbuckets,
860 dirty * 100 / (size_t) ca->sb.nbuckets,
861 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700862 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700863
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700864 for (i = 0; i < ARRAY_SIZE(q); i++)
865 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
866 "%u ", q[i]);
867 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700868
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700869 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
870
Kent Overstreetcafe5632013-03-23 16:11:31 -0700871 return ret;
872 }
873
874 return 0;
875}
876SHOW_LOCKED(bch_cache)
877
878STORE(__bch_cache)
879{
880 struct cache *ca = container_of(kobj, struct cache, kobj);
881
882 if (attr == &sysfs_discard) {
883 bool v = strtoul_or_return(buf);
884
885 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
886 ca->discard = v;
887
888 if (v != CACHE_DISCARD(&ca->sb)) {
889 SET_CACHE_DISCARD(&ca->sb, v);
890 bcache_write_super(ca->set);
891 }
892 }
893
894 if (attr == &sysfs_cache_replacement_policy) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600895 ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700896
897 if (v < 0)
898 return v;
899
900 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
901 mutex_lock(&ca->set->bucket_lock);
902 SET_CACHE_REPLACEMENT(&ca->sb, v);
903 mutex_unlock(&ca->set->bucket_lock);
904
905 bcache_write_super(ca->set);
906 }
907 }
908
Kent Overstreetcafe5632013-03-23 16:11:31 -0700909 if (attr == &sysfs_clear_stats) {
910 atomic_long_set(&ca->sectors_written, 0);
911 atomic_long_set(&ca->btree_sectors_written, 0);
912 atomic_long_set(&ca->meta_sectors_written, 0);
913 atomic_set(&ca->io_count, 0);
914 atomic_set(&ca->io_errors, 0);
915 }
916
917 return size;
918}
919STORE_LOCKED(bch_cache)
920
921static struct attribute *bch_cache_files[] = {
922 &sysfs_bucket_size,
923 &sysfs_block_size,
924 &sysfs_nbuckets,
925 &sysfs_priority_stats,
926 &sysfs_discard,
927 &sysfs_written,
928 &sysfs_btree_written,
929 &sysfs_metadata_written,
930 &sysfs_io_errors,
931 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932 &sysfs_cache_replacement_policy,
933 NULL
934};
935KTYPE(bch_cache);