blob: 955fa1d317740a6e87bf7cfee80ba619fdecafd1 [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; \
126 i = (void *) i + set_blocks(i, b->c) * block_bytes(b->c))
127
128void bch_btree_verify(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700129{
130 struct btree *v = b->c->verify_data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800131 struct bset *ondisk, *sorted, *inmemory;
132 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700133
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800134 if (!b->c->verify || !b->c->verify_ondisk)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135 return;
136
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800137 down(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700138 mutex_lock(&b->c->verify_lock);
139
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800140 ondisk = b->c->verify_ondisk;
141 sorted = b->c->verify_data->sets->data;
142 inmemory = b->sets->data;
143
Kent Overstreetcafe5632013-03-23 16:11:31 -0700144 bkey_copy(&v->key, &b->key);
145 v->written = 0;
146 v->level = b->level;
147
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800148 bio = bch_bbio_alloc(b->c);
149 bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev;
150 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
151 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
152 bch_bio_map(bio, sorted);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700153
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800154 submit_bio_wait(REQ_META|READ_SYNC, bio);
155 bch_bbio_free(bio, b->c);
156
157 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
158
159 bch_btree_node_read_done(v);
160 sorted = v->sets->data;
161
162 if (inmemory->keys != sorted->keys ||
163 memcmp(inmemory->start,
164 sorted->start,
Kent Overstreetfafff812013-12-17 21:56:21 -0800165 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800166 struct bset *i;
167 unsigned j;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700168
169 console_lock();
170
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800171 printk(KERN_ERR "*** in memory:\n");
172 dump_bset(b, inmemory, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700173
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800174 printk(KERN_ERR "*** read back in:\n");
175 dump_bset(v, sorted, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700176
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800177 for_each_written_bset(b, ondisk, i) {
178 unsigned block = ((void *) i - (void *) ondisk) /
179 block_bytes(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700180
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800181 printk(KERN_ERR "*** on disk block %u:\n", block);
182 dump_bset(b, i, block);
183 }
184
185 printk(KERN_ERR "*** block %zu not written\n",
186 ((void *) i - (void *) ondisk) / block_bytes(b->c));
187
188 for (j = 0; j < inmemory->keys; j++)
189 if (inmemory->d[j] != sorted->d[j])
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190 break;
191
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800192 printk(KERN_ERR "b->written %u\n", b->written);
193
Kent Overstreetcafe5632013-03-23 16:11:31 -0700194 console_unlock();
195 panic("verify failed at %u\n", j);
196 }
197
198 mutex_unlock(&b->c->verify_lock);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800199 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200}
201
Kent Overstreet220bb382013-09-10 19:02:45 -0700202void bch_data_verify(struct cached_dev *dc, struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203{
204 char name[BDEVNAME_SIZE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205 struct bio *check;
Kent Overstreet79886132013-11-23 17:19:00 -0800206 struct bio_vec bv, *bv2;
207 struct bvec_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700208 int i;
209
Kent Overstreet220bb382013-09-10 19:02:45 -0700210 check = bio_clone(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 if (!check)
212 return;
213
Kent Overstreet8e51e412013-06-06 18:15:57 -0700214 if (bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215 goto out_put;
216
Kent Overstreet220bb382013-09-10 19:02:45 -0700217 submit_bio_wait(READ_SYNC, check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218
Kent Overstreet79886132013-11-23 17:19:00 -0800219 bio_for_each_segment(bv, bio, iter) {
220 void *p1 = kmap_atomic(bv.bv_page);
221 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700222
Kent Overstreet79886132013-11-23 17:19:00 -0800223 cache_set_err_on(memcmp(p1 + bv.bv_offset,
224 p2 + bv.bv_offset,
225 bv.bv_len),
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700226 dc->disk.c,
227 "verify failed at dev %s sector %llu",
228 bdevname(dc->bdev, name),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700229 (uint64_t) bio->bi_iter.bi_sector);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700230
Kent Overstreet220bb382013-09-10 19:02:45 -0700231 kunmap_atomic(p1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700232 }
233
Kent Overstreet79886132013-11-23 17:19:00 -0800234 bio_for_each_segment_all(bv2, check, i)
235 __free_page(bv2->bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700236out_put:
237 bio_put(check);
238}
239
Kent Overstreet280481d2013-10-24 16:36:03 -0700240int __bch_count_data(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700241{
242 unsigned ret = 0;
243 struct btree_iter iter;
244 struct bkey *k;
245
246 if (!b->level)
247 for_each_key(b, k, &iter)
248 ret += KEY_SIZE(k);
249 return ret;
250}
251
Kent Overstreet280481d2013-10-24 16:36:03 -0700252void __bch_check_keys(struct btree *b, const char *fmt, ...)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700253{
254 va_list args;
255 struct bkey *k, *p = NULL;
256 struct btree_iter iter;
Kent Overstreet280481d2013-10-24 16:36:03 -0700257 const char *err;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700258
259 for_each_key(b, k, &iter) {
Kent Overstreet280481d2013-10-24 16:36:03 -0700260 if (!b->level) {
261 err = "Keys out of order";
262 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
263 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700264
Kent Overstreet280481d2013-10-24 16:36:03 -0700265 if (bch_ptr_invalid(b, k))
266 continue;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700267
Kent Overstreet280481d2013-10-24 16:36:03 -0700268 err = "Overlapping keys";
269 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
270 goto bug;
271 } else {
272 if (bch_ptr_bad(b, k))
273 continue;
274
275 err = "Duplicate keys";
276 if (p && !bkey_cmp(p, k))
277 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700278 }
279 p = k;
280 }
Kent Overstreet280481d2013-10-24 16:36:03 -0700281
282 err = "Key larger than btree node key";
283 if (p && bkey_cmp(p, &b->key) > 0)
284 goto bug;
285
Kent Overstreetcafe5632013-03-23 16:11:31 -0700286 return;
287bug:
Kent Overstreet280481d2013-10-24 16:36:03 -0700288 bch_dump_bucket(b);
289
Kent Overstreetcafe5632013-03-23 16:11:31 -0700290 va_start(args, fmt);
Kent Overstreet280481d2013-10-24 16:36:03 -0700291 vprintk(fmt, args);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292 va_end(args);
Kent Overstreet280481d2013-10-24 16:36:03 -0700293
294 panic("bcache error: %s:\n", err);
295}
296
297void bch_btree_iter_next_check(struct btree_iter *iter)
298{
299 struct bkey *k = iter->data->k, *next = bkey_next(k);
300
301 if (next < iter->data->end &&
302 bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
303 bch_dump_bucket(iter->b);
304 panic("Key skipped backwards\n");
305 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700306}
307
308#endif
309
310#ifdef CONFIG_DEBUG_FS
311
312/* XXX: cache set refcounting */
313
314struct dump_iterator {
315 char buf[PAGE_SIZE];
316 size_t bytes;
317 struct cache_set *c;
318 struct keybuf keys;
319};
320
321static bool dump_pred(struct keybuf *buf, struct bkey *k)
322{
323 return true;
324}
325
326static ssize_t bch_dump_read(struct file *file, char __user *buf,
327 size_t size, loff_t *ppos)
328{
329 struct dump_iterator *i = file->private_data;
330 ssize_t ret = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -0700331 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700332
333 while (size) {
334 struct keybuf_key *w;
335 unsigned bytes = min(i->bytes, size);
336
337 int err = copy_to_user(buf, i->buf, bytes);
338 if (err)
339 return err;
340
341 ret += bytes;
342 buf += bytes;
343 size -= bytes;
344 i->bytes -= bytes;
345 memmove(i->buf, i->buf + bytes, i->bytes);
346
347 if (i->bytes)
348 break;
349
Kent Overstreet72c27062013-06-05 06:24:39 -0700350 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700351 if (!w)
352 break;
353
Kent Overstreet85b14922013-05-14 20:33:16 -0700354 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
355 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700356 bch_keybuf_del(&i->keys, w);
357 }
358
359 return ret;
360}
361
362static int bch_dump_open(struct inode *inode, struct file *file)
363{
364 struct cache_set *c = inode->i_private;
365 struct dump_iterator *i;
366
367 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
368 if (!i)
369 return -ENOMEM;
370
371 file->private_data = i;
372 i->c = c;
Kent Overstreet72c27062013-06-05 06:24:39 -0700373 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700374 i->keys.last_scanned = KEY(0, 0, 0);
375
376 return 0;
377}
378
379static int bch_dump_release(struct inode *inode, struct file *file)
380{
381 kfree(file->private_data);
382 return 0;
383}
384
385static const struct file_operations cache_set_debug_ops = {
386 .owner = THIS_MODULE,
387 .open = bch_dump_open,
388 .read = bch_dump_read,
389 .release = bch_dump_release
390};
391
392void bch_debug_init_cache_set(struct cache_set *c)
393{
394 if (!IS_ERR_OR_NULL(debug)) {
395 char name[50];
396 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
397
398 c->debug = debugfs_create_file(name, 0400, debug, c,
399 &cache_set_debug_ops);
400 }
401}
402
403#endif
404
Kent Overstreetcafe5632013-03-23 16:11:31 -0700405void bch_debug_exit(void)
406{
407 if (!IS_ERR_OR_NULL(debug))
408 debugfs_remove_recursive(debug);
409}
410
411int __init bch_debug_init(struct kobject *kobj)
412{
413 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700414
415 debug = debugfs_create_dir("bcache", NULL);
416 return ret;
417}