blob: 5a78137c420d108551aec8ba5f8b0b72220075d2 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * Assorted bcache debug code
3 *
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
6 */
7
8#include "bcache.h"
9#include "btree.h"
10#include "debug.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070011
12#include <linux/console.h>
13#include <linux/debugfs.h>
14#include <linux/module.h>
15#include <linux/random.h>
16#include <linux/seq_file.h>
17
18static struct dentry *debug;
19
20const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
21{
22 unsigned i;
23
24 for (i = 0; i < KEY_PTRS(k); i++)
25 if (ptr_available(c, k, i)) {
26 struct cache *ca = PTR_CACHE(c, k, i);
27 size_t bucket = PTR_BUCKET_NR(c, k, i);
28 size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
29
30 if (KEY_SIZE(k) + r > c->sb.bucket_size)
31 return "bad, length too big";
32 if (bucket < ca->sb.first_bucket)
33 return "bad, short offset";
34 if (bucket >= ca->sb.nbuckets)
35 return "bad, offset past end of device";
36 if (ptr_stale(c, k, i))
37 return "stale";
38 }
39
40 if (!bkey_cmp(k, &ZERO_KEY))
41 return "bad, null key";
42 if (!KEY_PTRS(k))
43 return "bad, no pointers";
44 if (!KEY_SIZE(k))
45 return "zeroed key";
46 return "";
47}
48
Kent Overstreet85b14922013-05-14 20:33:16 -070049int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -070050{
51 unsigned i = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -070052 char *out = buf, *end = buf + size;
Kent Overstreetcafe5632013-03-23 16:11:31 -070053
54#define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
55
Kent Overstreet78b77bf2013-12-17 22:49:08 -080056 p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -070057
Kent Overstreet78b77bf2013-12-17 22:49:08 -080058 for (i = 0; i < KEY_PTRS(k); i++) {
59 if (i)
Kent Overstreetcafe5632013-03-23 16:11:31 -070060 p(", ");
Kent Overstreet78b77bf2013-12-17 22:49:08 -080061
62 if (PTR_DEV(k, i) == PTR_CHECK_DEV)
63 p("check dev");
64 else
65 p("%llu:%llu gen %llu", PTR_DEV(k, i),
66 PTR_OFFSET(k, i), PTR_GEN(k, i));
67 }
Kent Overstreetcafe5632013-03-23 16:11:31 -070068
69 p("]");
70
71 if (KEY_DIRTY(k))
72 p(" dirty");
73 if (KEY_CSUM(k))
74 p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
75#undef p
Kent Overstreet85b14922013-05-14 20:33:16 -070076 return out - buf;
Kent Overstreetcafe5632013-03-23 16:11:31 -070077}
78
Kent Overstreet280481d2013-10-24 16:36:03 -070079#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetcafe5632013-03-23 16:11:31 -070080
Kent Overstreet78b77bf2013-12-17 22:49:08 -080081static void dump_bset(struct btree *b, struct bset *i, unsigned set)
Kent Overstreetcafe5632013-03-23 16:11:31 -070082{
Kent Overstreet280481d2013-10-24 16:36:03 -070083 struct bkey *k, *next;
Kent Overstreetcafe5632013-03-23 16:11:31 -070084 unsigned j;
Kent Overstreet85b14922013-05-14 20:33:16 -070085 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -070086
Kent Overstreetfafff812013-12-17 21:56:21 -080087 for (k = i->start; k < bset_bkey_last(i); k = next) {
Kent Overstreet280481d2013-10-24 16:36:03 -070088 next = bkey_next(k);
89
Kent Overstreet85b14922013-05-14 20:33:16 -070090 bch_bkey_to_text(buf, sizeof(buf), k);
Kent Overstreet78b77bf2013-12-17 22:49:08 -080091 printk(KERN_ERR "b %u k %zi/%u: %s", set,
Kent Overstreet85b14922013-05-14 20:33:16 -070092 (uint64_t *) k - i->d, i->keys, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -070093
94 for (j = 0; j < KEY_PTRS(k); j++) {
95 size_t n = PTR_BUCKET_NR(b->c, k, j);
96 printk(" bucket %zu", n);
97
98 if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
99 printk(" prio %i",
100 PTR_BUCKET(b->c, k, j)->prio);
101 }
102
103 printk(" %s\n", bch_ptr_status(b->c, k));
104
Kent Overstreetfafff812013-12-17 21:56:21 -0800105 if (next < bset_bkey_last(i) &&
Kent Overstreet280481d2013-10-24 16:36:03 -0700106 bkey_cmp(k, !b->level ? &START_KEY(next) : next) > 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107 printk(KERN_ERR "Key skipped backwards\n");
108 }
109}
110
Kent Overstreet280481d2013-10-24 16:36:03 -0700111static void bch_dump_bucket(struct btree *b)
112{
113 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114
Kent Overstreet280481d2013-10-24 16:36:03 -0700115 console_lock();
116 for (i = 0; i <= b->nsets; i++)
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800117 dump_bset(b, b->sets[i].data,
118 bset_block_offset(b, b->sets[i].data));
Kent Overstreet280481d2013-10-24 16:36:03 -0700119 console_unlock();
120}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800122#define for_each_written_bset(b, start, i) \
123 for (i = (start); \
124 (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\
125 i->seq == (start)->seq; \
Kent Overstreetee811282013-12-17 23:49:49 -0800126 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
127 block_bytes(b->c))
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800128
129void bch_btree_verify(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700130{
131 struct btree *v = b->c->verify_data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800132 struct bset *ondisk, *sorted, *inmemory;
133 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700134
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800135 if (!b->c->verify || !b->c->verify_ondisk)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136 return;
137
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800138 down(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139 mutex_lock(&b->c->verify_lock);
140
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800141 ondisk = b->c->verify_ondisk;
142 sorted = b->c->verify_data->sets->data;
143 inmemory = b->sets->data;
144
Kent Overstreetcafe5632013-03-23 16:11:31 -0700145 bkey_copy(&v->key, &b->key);
146 v->written = 0;
147 v->level = b->level;
148
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800149 bio = bch_bbio_alloc(b->c);
150 bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev;
151 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
152 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
153 bch_bio_map(bio, sorted);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700154
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800155 submit_bio_wait(REQ_META|READ_SYNC, bio);
156 bch_bbio_free(bio, b->c);
157
158 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
159
160 bch_btree_node_read_done(v);
161 sorted = v->sets->data;
162
163 if (inmemory->keys != sorted->keys ||
164 memcmp(inmemory->start,
165 sorted->start,
Kent Overstreetfafff812013-12-17 21:56:21 -0800166 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800167 struct bset *i;
168 unsigned j;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700169
170 console_lock();
171
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800172 printk(KERN_ERR "*** in memory:\n");
173 dump_bset(b, inmemory, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800175 printk(KERN_ERR "*** read back in:\n");
176 dump_bset(v, sorted, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700177
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800178 for_each_written_bset(b, ondisk, i) {
179 unsigned block = ((void *) i - (void *) ondisk) /
180 block_bytes(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800182 printk(KERN_ERR "*** on disk block %u:\n", block);
183 dump_bset(b, i, block);
184 }
185
186 printk(KERN_ERR "*** block %zu not written\n",
187 ((void *) i - (void *) ondisk) / block_bytes(b->c));
188
189 for (j = 0; j < inmemory->keys; j++)
190 if (inmemory->d[j] != sorted->d[j])
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191 break;
192
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800193 printk(KERN_ERR "b->written %u\n", b->written);
194
Kent Overstreetcafe5632013-03-23 16:11:31 -0700195 console_unlock();
196 panic("verify failed at %u\n", j);
197 }
198
199 mutex_unlock(&b->c->verify_lock);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800200 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201}
202
Kent Overstreet220bb382013-09-10 19:02:45 -0700203void bch_data_verify(struct cached_dev *dc, struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204{
205 char name[BDEVNAME_SIZE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700206 struct bio *check;
Kent Overstreet79886132013-11-23 17:19:00 -0800207 struct bio_vec bv, *bv2;
208 struct bvec_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209 int i;
210
Kent Overstreet220bb382013-09-10 19:02:45 -0700211 check = bio_clone(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700212 if (!check)
213 return;
214
Kent Overstreet8e51e412013-06-06 18:15:57 -0700215 if (bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 goto out_put;
217
Kent Overstreet220bb382013-09-10 19:02:45 -0700218 submit_bio_wait(READ_SYNC, check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219
Kent Overstreet79886132013-11-23 17:19:00 -0800220 bio_for_each_segment(bv, bio, iter) {
221 void *p1 = kmap_atomic(bv.bv_page);
222 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700223
Kent Overstreet79886132013-11-23 17:19:00 -0800224 cache_set_err_on(memcmp(p1 + bv.bv_offset,
225 p2 + bv.bv_offset,
226 bv.bv_len),
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700227 dc->disk.c,
228 "verify failed at dev %s sector %llu",
229 bdevname(dc->bdev, name),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700230 (uint64_t) bio->bi_iter.bi_sector);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700231
Kent Overstreet220bb382013-09-10 19:02:45 -0700232 kunmap_atomic(p1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700233 }
234
Kent Overstreet79886132013-11-23 17:19:00 -0800235 bio_for_each_segment_all(bv2, check, i)
236 __free_page(bv2->bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700237out_put:
238 bio_put(check);
239}
240
Kent Overstreet280481d2013-10-24 16:36:03 -0700241int __bch_count_data(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700242{
243 unsigned ret = 0;
244 struct btree_iter iter;
245 struct bkey *k;
246
247 if (!b->level)
248 for_each_key(b, k, &iter)
249 ret += KEY_SIZE(k);
250 return ret;
251}
252
Kent Overstreet280481d2013-10-24 16:36:03 -0700253void __bch_check_keys(struct btree *b, const char *fmt, ...)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700254{
255 va_list args;
256 struct bkey *k, *p = NULL;
257 struct btree_iter iter;
Kent Overstreet280481d2013-10-24 16:36:03 -0700258 const char *err;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700259
260 for_each_key(b, k, &iter) {
Kent Overstreet280481d2013-10-24 16:36:03 -0700261 if (!b->level) {
262 err = "Keys out of order";
263 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
264 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700265
Kent Overstreet280481d2013-10-24 16:36:03 -0700266 if (bch_ptr_invalid(b, k))
267 continue;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700268
Kent Overstreet280481d2013-10-24 16:36:03 -0700269 err = "Overlapping keys";
270 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
271 goto bug;
272 } else {
273 if (bch_ptr_bad(b, k))
274 continue;
275
276 err = "Duplicate keys";
277 if (p && !bkey_cmp(p, k))
278 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279 }
280 p = k;
281 }
Kent Overstreet280481d2013-10-24 16:36:03 -0700282
283 err = "Key larger than btree node key";
284 if (p && bkey_cmp(p, &b->key) > 0)
285 goto bug;
286
Kent Overstreetcafe5632013-03-23 16:11:31 -0700287 return;
288bug:
Kent Overstreet280481d2013-10-24 16:36:03 -0700289 bch_dump_bucket(b);
290
Kent Overstreetcafe5632013-03-23 16:11:31 -0700291 va_start(args, fmt);
Kent Overstreet280481d2013-10-24 16:36:03 -0700292 vprintk(fmt, args);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700293 va_end(args);
Kent Overstreet280481d2013-10-24 16:36:03 -0700294
295 panic("bcache error: %s:\n", err);
296}
297
298void bch_btree_iter_next_check(struct btree_iter *iter)
299{
300 struct bkey *k = iter->data->k, *next = bkey_next(k);
301
302 if (next < iter->data->end &&
303 bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
304 bch_dump_bucket(iter->b);
305 panic("Key skipped backwards\n");
306 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700307}
308
309#endif
310
311#ifdef CONFIG_DEBUG_FS
312
313/* XXX: cache set refcounting */
314
315struct dump_iterator {
316 char buf[PAGE_SIZE];
317 size_t bytes;
318 struct cache_set *c;
319 struct keybuf keys;
320};
321
322static bool dump_pred(struct keybuf *buf, struct bkey *k)
323{
324 return true;
325}
326
327static ssize_t bch_dump_read(struct file *file, char __user *buf,
328 size_t size, loff_t *ppos)
329{
330 struct dump_iterator *i = file->private_data;
331 ssize_t ret = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -0700332 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700333
334 while (size) {
335 struct keybuf_key *w;
336 unsigned bytes = min(i->bytes, size);
337
338 int err = copy_to_user(buf, i->buf, bytes);
339 if (err)
340 return err;
341
342 ret += bytes;
343 buf += bytes;
344 size -= bytes;
345 i->bytes -= bytes;
346 memmove(i->buf, i->buf + bytes, i->bytes);
347
348 if (i->bytes)
349 break;
350
Kent Overstreet72c27062013-06-05 06:24:39 -0700351 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700352 if (!w)
353 break;
354
Kent Overstreet85b14922013-05-14 20:33:16 -0700355 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
356 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700357 bch_keybuf_del(&i->keys, w);
358 }
359
360 return ret;
361}
362
363static int bch_dump_open(struct inode *inode, struct file *file)
364{
365 struct cache_set *c = inode->i_private;
366 struct dump_iterator *i;
367
368 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
369 if (!i)
370 return -ENOMEM;
371
372 file->private_data = i;
373 i->c = c;
Kent Overstreet72c27062013-06-05 06:24:39 -0700374 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700375 i->keys.last_scanned = KEY(0, 0, 0);
376
377 return 0;
378}
379
380static int bch_dump_release(struct inode *inode, struct file *file)
381{
382 kfree(file->private_data);
383 return 0;
384}
385
386static const struct file_operations cache_set_debug_ops = {
387 .owner = THIS_MODULE,
388 .open = bch_dump_open,
389 .read = bch_dump_read,
390 .release = bch_dump_release
391};
392
393void bch_debug_init_cache_set(struct cache_set *c)
394{
395 if (!IS_ERR_OR_NULL(debug)) {
396 char name[50];
397 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
398
399 c->debug = debugfs_create_file(name, 0400, debug, c,
400 &cache_set_debug_ops);
401 }
402}
403
404#endif
405
Kent Overstreetcafe5632013-03-23 16:11:31 -0700406void bch_debug_exit(void)
407{
408 if (!IS_ERR_OR_NULL(debug))
409 debugfs_remove_recursive(debug);
410}
411
412int __init bch_debug_init(struct kobject *kobj)
413{
414 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700415
416 debug = debugfs_create_dir("bcache", NULL);
417 return ret;
418}