blob: d9ccb3169aa28660270738671be7d5067063df30 [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
56 p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_OFFSET(k), KEY_SIZE(k));
57
58 if (KEY_PTRS(k))
59 while (1) {
60 p("%llu:%llu gen %llu",
61 PTR_DEV(k, i), PTR_OFFSET(k, i), PTR_GEN(k, i));
62
63 if (++i == KEY_PTRS(k))
64 break;
65
66 p(", ");
67 }
68
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 Overstreet85b14922013-05-14 20:33:16 -070079int bch_btree_to_text(char *buf, size_t size, const struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -070080{
Kent Overstreet85b14922013-05-14 20:33:16 -070081 return scnprintf(buf, size, "%zu level %i/%i",
82 PTR_BUCKET_NR(b->c, &b->key, 0),
83 b->level, b->c->root ? b->c->root->level : -1);
Kent Overstreetcafe5632013-03-23 16:11:31 -070084}
85
86#if defined(CONFIG_BCACHE_DEBUG) || defined(CONFIG_BCACHE_EDEBUG)
87
88static bool skipped_backwards(struct btree *b, struct bkey *k)
89{
90 return bkey_cmp(k, (!b->level)
91 ? &START_KEY(bkey_next(k))
92 : bkey_next(k)) > 0;
93}
94
95static void dump_bset(struct btree *b, struct bset *i)
96{
97 struct bkey *k;
98 unsigned j;
Kent Overstreet85b14922013-05-14 20:33:16 -070099 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700100
101 for (k = i->start; k < end(i); k = bkey_next(k)) {
Kent Overstreet85b14922013-05-14 20:33:16 -0700102 bch_bkey_to_text(buf, sizeof(buf), k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700103 printk(KERN_ERR "block %zu key %zi/%u: %s", index(i, b),
Kent Overstreet85b14922013-05-14 20:33:16 -0700104 (uint64_t *) k - i->d, i->keys, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700105
106 for (j = 0; j < KEY_PTRS(k); j++) {
107 size_t n = PTR_BUCKET_NR(b->c, k, j);
108 printk(" bucket %zu", n);
109
110 if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
111 printk(" prio %i",
112 PTR_BUCKET(b->c, k, j)->prio);
113 }
114
115 printk(" %s\n", bch_ptr_status(b->c, k));
116
117 if (bkey_next(k) < end(i) &&
118 skipped_backwards(b, k))
119 printk(KERN_ERR "Key skipped backwards\n");
120 }
121}
122
123#endif
124
125#ifdef CONFIG_BCACHE_DEBUG
126
127void bch_btree_verify(struct btree *b, struct bset *new)
128{
129 struct btree *v = b->c->verify_data;
130 struct closure cl;
131 closure_init_stack(&cl);
132
133 if (!b->c->verify)
134 return;
135
136 closure_wait_event(&b->io.wait, &cl,
137 atomic_read(&b->io.cl.remaining) == -1);
138
139 mutex_lock(&b->c->verify_lock);
140
141 bkey_copy(&v->key, &b->key);
142 v->written = 0;
143 v->level = b->level;
144
Kent Overstreet57943512013-04-25 13:58:35 -0700145 bch_btree_node_read(v);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700146 closure_wait_event(&v->io.wait, &cl,
147 atomic_read(&b->io.cl.remaining) == -1);
148
149 if (new->keys != v->sets[0].data->keys ||
150 memcmp(new->start,
151 v->sets[0].data->start,
152 (void *) end(new) - (void *) new->start)) {
153 unsigned i, j;
154
155 console_lock();
156
157 printk(KERN_ERR "*** original memory node:\n");
158 for (i = 0; i <= b->nsets; i++)
159 dump_bset(b, b->sets[i].data);
160
161 printk(KERN_ERR "*** sorted memory node:\n");
162 dump_bset(b, new);
163
164 printk(KERN_ERR "*** on disk node:\n");
165 dump_bset(v, v->sets[0].data);
166
167 for (j = 0; j < new->keys; j++)
168 if (new->d[j] != v->sets[0].data->d[j])
169 break;
170
171 console_unlock();
172 panic("verify failed at %u\n", j);
173 }
174
175 mutex_unlock(&b->c->verify_lock);
176}
177
Kent Overstreet220bb382013-09-10 19:02:45 -0700178void bch_data_verify(struct cached_dev *dc, struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700179{
180 char name[BDEVNAME_SIZE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181 struct bio *check;
182 struct bio_vec *bv;
183 int i;
184
Kent Overstreet220bb382013-09-10 19:02:45 -0700185 check = bio_clone(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700186 if (!check)
187 return;
188
Kent Overstreet8e51e412013-06-06 18:15:57 -0700189 if (bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190 goto out_put;
191
Kent Overstreet220bb382013-09-10 19:02:45 -0700192 submit_bio_wait(READ_SYNC, check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700193
Kent Overstreet220bb382013-09-10 19:02:45 -0700194 bio_for_each_segment(bv, bio, i) {
195 void *p1 = kmap_atomic(bv->bv_page);
196 void *p2 = page_address(check->bi_io_vec[i].bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197
198 if (memcmp(p1 + bv->bv_offset,
199 p2 + bv->bv_offset,
200 bv->bv_len))
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700201 printk(KERN_ERR
202 "bcache (%s): verify failed at sector %llu\n",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203 bdevname(dc->bdev, name),
Kent Overstreet220bb382013-09-10 19:02:45 -0700204 (uint64_t) bio->bi_sector);
205 kunmap_atomic(p1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700206 }
207
Kent Overstreet220bb382013-09-10 19:02:45 -0700208 bio_for_each_segment_all(bv, check, i)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209 __free_page(bv->bv_page);
210out_put:
211 bio_put(check);
212}
213
214#endif
215
216#ifdef CONFIG_BCACHE_EDEBUG
217
218unsigned bch_count_data(struct btree *b)
219{
220 unsigned ret = 0;
221 struct btree_iter iter;
222 struct bkey *k;
223
224 if (!b->level)
225 for_each_key(b, k, &iter)
226 ret += KEY_SIZE(k);
227 return ret;
228}
229
230static void vdump_bucket_and_panic(struct btree *b, const char *fmt,
231 va_list args)
232{
233 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -0700234 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700235
236 console_lock();
237
238 for (i = 0; i <= b->nsets; i++)
239 dump_bset(b, b->sets[i].data);
240
241 vprintk(fmt, args);
242
243 console_unlock();
244
Kent Overstreet85b14922013-05-14 20:33:16 -0700245 bch_btree_to_text(buf, sizeof(buf), b);
246 panic("at %s\n", buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700247}
248
249void bch_check_key_order_msg(struct btree *b, struct bset *i,
250 const char *fmt, ...)
251{
252 struct bkey *k;
253
254 if (!i->keys)
255 return;
256
257 for (k = i->start; bkey_next(k) < end(i); k = bkey_next(k))
258 if (skipped_backwards(b, k)) {
259 va_list args;
260 va_start(args, fmt);
261
262 vdump_bucket_and_panic(b, fmt, args);
263 va_end(args);
264 }
265}
266
267void bch_check_keys(struct btree *b, const char *fmt, ...)
268{
269 va_list args;
270 struct bkey *k, *p = NULL;
271 struct btree_iter iter;
272
273 if (b->level)
274 return;
275
276 for_each_key(b, k, &iter) {
277 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) {
278 printk(KERN_ERR "Keys out of order:\n");
279 goto bug;
280 }
281
282 if (bch_ptr_invalid(b, k))
283 continue;
284
285 if (p && bkey_cmp(p, &START_KEY(k)) > 0) {
286 printk(KERN_ERR "Overlapping keys:\n");
287 goto bug;
288 }
289 p = k;
290 }
291 return;
292bug:
293 va_start(args, fmt);
294 vdump_bucket_and_panic(b, fmt, args);
295 va_end(args);
296}
297
298#endif
299
300#ifdef CONFIG_DEBUG_FS
301
302/* XXX: cache set refcounting */
303
304struct dump_iterator {
305 char buf[PAGE_SIZE];
306 size_t bytes;
307 struct cache_set *c;
308 struct keybuf keys;
309};
310
311static bool dump_pred(struct keybuf *buf, struct bkey *k)
312{
313 return true;
314}
315
316static ssize_t bch_dump_read(struct file *file, char __user *buf,
317 size_t size, loff_t *ppos)
318{
319 struct dump_iterator *i = file->private_data;
320 ssize_t ret = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -0700321 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700322
323 while (size) {
324 struct keybuf_key *w;
325 unsigned bytes = min(i->bytes, size);
326
327 int err = copy_to_user(buf, i->buf, bytes);
328 if (err)
329 return err;
330
331 ret += bytes;
332 buf += bytes;
333 size -= bytes;
334 i->bytes -= bytes;
335 memmove(i->buf, i->buf + bytes, i->bytes);
336
337 if (i->bytes)
338 break;
339
Kent Overstreet72c27062013-06-05 06:24:39 -0700340 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700341 if (!w)
342 break;
343
Kent Overstreet85b14922013-05-14 20:33:16 -0700344 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
345 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700346 bch_keybuf_del(&i->keys, w);
347 }
348
349 return ret;
350}
351
352static int bch_dump_open(struct inode *inode, struct file *file)
353{
354 struct cache_set *c = inode->i_private;
355 struct dump_iterator *i;
356
357 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
358 if (!i)
359 return -ENOMEM;
360
361 file->private_data = i;
362 i->c = c;
Kent Overstreet72c27062013-06-05 06:24:39 -0700363 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700364 i->keys.last_scanned = KEY(0, 0, 0);
365
366 return 0;
367}
368
369static int bch_dump_release(struct inode *inode, struct file *file)
370{
371 kfree(file->private_data);
372 return 0;
373}
374
375static const struct file_operations cache_set_debug_ops = {
376 .owner = THIS_MODULE,
377 .open = bch_dump_open,
378 .read = bch_dump_read,
379 .release = bch_dump_release
380};
381
382void bch_debug_init_cache_set(struct cache_set *c)
383{
384 if (!IS_ERR_OR_NULL(debug)) {
385 char name[50];
386 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
387
388 c->debug = debugfs_create_file(name, 0400, debug, c,
389 &cache_set_debug_ops);
390 }
391}
392
393#endif
394
Kent Overstreetcafe5632013-03-23 16:11:31 -0700395void bch_debug_exit(void)
396{
397 if (!IS_ERR_OR_NULL(debug))
398 debugfs_remove_recursive(debug);
399}
400
401int __init bch_debug_init(struct kobject *kobj)
402{
403 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700404
405 debug = debugfs_create_dir("bcache", NULL);
406 return ret;
407}