bcache: Abstract out stuff needed for sorting
[linux-2.6-block.git] / drivers / md / bcache / debug.c
CommitLineData
cafe5635
KO
1/*
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"
cafe5635
KO
11
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
85b1492e 49int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
cafe5635
KO
50{
51 unsigned i = 0;
85b1492e 52 char *out = buf, *end = buf + size;
cafe5635
KO
53
54#define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
55
78b77bf8 56 p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
cafe5635 57
78b77bf8
KO
58 for (i = 0; i < KEY_PTRS(k); i++) {
59 if (i)
cafe5635 60 p(", ");
78b77bf8
KO
61
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 }
cafe5635
KO
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
85b1492e 76 return out - buf;
cafe5635
KO
77}
78
280481d0 79#ifdef CONFIG_BCACHE_DEBUG
cafe5635 80
78b77bf8 81static void dump_bset(struct btree *b, struct bset *i, unsigned set)
cafe5635 82{
280481d0 83 struct bkey *k, *next;
cafe5635 84 unsigned j;
85b1492e 85 char buf[80];
cafe5635 86
fafff81c 87 for (k = i->start; k < bset_bkey_last(i); k = next) {
280481d0
KO
88 next = bkey_next(k);
89
85b1492e 90 bch_bkey_to_text(buf, sizeof(buf), k);
78b77bf8 91 printk(KERN_ERR "b %u k %zi/%u: %s", set,
85b1492e 92 (uint64_t *) k - i->d, i->keys, buf);
cafe5635
KO
93
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
fafff81c 105 if (next < bset_bkey_last(i) &&
280481d0 106 bkey_cmp(k, !b->level ? &START_KEY(next) : next) > 0)
cafe5635
KO
107 printk(KERN_ERR "Key skipped backwards\n");
108 }
109}
110
280481d0
KO
111static void bch_dump_bucket(struct btree *b)
112{
113 unsigned i;
cafe5635 114
280481d0
KO
115 console_lock();
116 for (i = 0; i <= b->nsets; i++)
78b77bf8
KO
117 dump_bset(b, b->sets[i].data,
118 bset_block_offset(b, b->sets[i].data));
280481d0
KO
119 console_unlock();
120}
cafe5635 121
78b77bf8
KO
122#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; \
ee811287
KO
126 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
127 block_bytes(b->c))
78b77bf8
KO
128
129void bch_btree_verify(struct btree *b)
cafe5635
KO
130{
131 struct btree *v = b->c->verify_data;
78b77bf8
KO
132 struct bset *ondisk, *sorted, *inmemory;
133 struct bio *bio;
cafe5635 134
78b77bf8 135 if (!b->c->verify || !b->c->verify_ondisk)
cafe5635
KO
136 return;
137
cb7a583e 138 down(&b->io_mutex);
cafe5635
KO
139 mutex_lock(&b->c->verify_lock);
140
78b77bf8
KO
141 ondisk = b->c->verify_ondisk;
142 sorted = b->c->verify_data->sets->data;
143 inmemory = b->sets->data;
144
cafe5635
KO
145 bkey_copy(&v->key, &b->key);
146 v->written = 0;
147 v->level = b->level;
65d45231 148 v->ops = b->ops;
cafe5635 149
78b77bf8
KO
150 bio = bch_bbio_alloc(b->c);
151 bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev;
152 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
153 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
154 bch_bio_map(bio, sorted);
cafe5635 155
78b77bf8
KO
156 submit_bio_wait(REQ_META|READ_SYNC, bio);
157 bch_bbio_free(bio, b->c);
158
159 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
160
161 bch_btree_node_read_done(v);
162 sorted = v->sets->data;
163
164 if (inmemory->keys != sorted->keys ||
165 memcmp(inmemory->start,
166 sorted->start,
fafff81c 167 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
78b77bf8
KO
168 struct bset *i;
169 unsigned j;
cafe5635
KO
170
171 console_lock();
172
78b77bf8
KO
173 printk(KERN_ERR "*** in memory:\n");
174 dump_bset(b, inmemory, 0);
cafe5635 175
78b77bf8
KO
176 printk(KERN_ERR "*** read back in:\n");
177 dump_bset(v, sorted, 0);
cafe5635 178
78b77bf8
KO
179 for_each_written_bset(b, ondisk, i) {
180 unsigned block = ((void *) i - (void *) ondisk) /
181 block_bytes(b->c);
cafe5635 182
78b77bf8
KO
183 printk(KERN_ERR "*** on disk block %u:\n", block);
184 dump_bset(b, i, block);
185 }
186
187 printk(KERN_ERR "*** block %zu not written\n",
188 ((void *) i - (void *) ondisk) / block_bytes(b->c));
189
190 for (j = 0; j < inmemory->keys; j++)
191 if (inmemory->d[j] != sorted->d[j])
cafe5635
KO
192 break;
193
78b77bf8
KO
194 printk(KERN_ERR "b->written %u\n", b->written);
195
cafe5635
KO
196 console_unlock();
197 panic("verify failed at %u\n", j);
198 }
199
200 mutex_unlock(&b->c->verify_lock);
cb7a583e 201 up(&b->io_mutex);
cafe5635
KO
202}
203
220bb38c 204void bch_data_verify(struct cached_dev *dc, struct bio *bio)
cafe5635
KO
205{
206 char name[BDEVNAME_SIZE];
cafe5635 207 struct bio *check;
7988613b
KO
208 struct bio_vec bv, *bv2;
209 struct bvec_iter iter;
cafe5635
KO
210 int i;
211
220bb38c 212 check = bio_clone(bio, GFP_NOIO);
cafe5635
KO
213 if (!check)
214 return;
215
8e51e414 216 if (bio_alloc_pages(check, GFP_NOIO))
cafe5635
KO
217 goto out_put;
218
220bb38c 219 submit_bio_wait(READ_SYNC, check);
cafe5635 220
7988613b
KO
221 bio_for_each_segment(bv, bio, iter) {
222 void *p1 = kmap_atomic(bv.bv_page);
223 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
cafe5635 224
7988613b
KO
225 cache_set_err_on(memcmp(p1 + bv.bv_offset,
226 p2 + bv.bv_offset,
227 bv.bv_len),
5ceaaad7
KO
228 dc->disk.c,
229 "verify failed at dev %s sector %llu",
230 bdevname(dc->bdev, name),
4f024f37 231 (uint64_t) bio->bi_iter.bi_sector);
5ceaaad7 232
220bb38c 233 kunmap_atomic(p1);
cafe5635
KO
234 }
235
7988613b
KO
236 bio_for_each_segment_all(bv2, check, i)
237 __free_page(bv2->bv_page);
cafe5635
KO
238out_put:
239 bio_put(check);
240}
241
280481d0 242int __bch_count_data(struct btree *b)
cafe5635
KO
243{
244 unsigned ret = 0;
245 struct btree_iter iter;
246 struct bkey *k;
247
248 if (!b->level)
249 for_each_key(b, k, &iter)
250 ret += KEY_SIZE(k);
251 return ret;
252}
253
280481d0 254void __bch_check_keys(struct btree *b, const char *fmt, ...)
cafe5635
KO
255{
256 va_list args;
257 struct bkey *k, *p = NULL;
258 struct btree_iter iter;
280481d0 259 const char *err;
cafe5635
KO
260
261 for_each_key(b, k, &iter) {
280481d0
KO
262 if (!b->level) {
263 err = "Keys out of order";
264 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
265 goto bug;
266
267 if (bch_ptr_invalid(b, k))
268 continue;
269
270 err = "Overlapping keys";
271 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
272 goto bug;
273 } else {
274 if (bch_ptr_bad(b, k))
275 continue;
276
277 err = "Duplicate keys";
278 if (p && !bkey_cmp(p, k))
279 goto bug;
cafe5635
KO
280 }
281 p = k;
282 }
280481d0
KO
283
284 err = "Key larger than btree node key";
285 if (p && bkey_cmp(p, &b->key) > 0)
286 goto bug;
287
cafe5635
KO
288 return;
289bug:
280481d0
KO
290 bch_dump_bucket(b);
291
cafe5635 292 va_start(args, fmt);
280481d0 293 vprintk(fmt, args);
cafe5635 294 va_end(args);
280481d0
KO
295
296 panic("bcache error: %s:\n", err);
297}
298
299void bch_btree_iter_next_check(struct btree_iter *iter)
300{
301 struct bkey *k = iter->data->k, *next = bkey_next(k);
302
303 if (next < iter->data->end &&
304 bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
305 bch_dump_bucket(iter->b);
306 panic("Key skipped backwards\n");
307 }
cafe5635
KO
308}
309
310#endif
311
312#ifdef CONFIG_DEBUG_FS
313
314/* XXX: cache set refcounting */
315
316struct dump_iterator {
317 char buf[PAGE_SIZE];
318 size_t bytes;
319 struct cache_set *c;
320 struct keybuf keys;
321};
322
323static bool dump_pred(struct keybuf *buf, struct bkey *k)
324{
325 return true;
326}
327
328static ssize_t bch_dump_read(struct file *file, char __user *buf,
329 size_t size, loff_t *ppos)
330{
331 struct dump_iterator *i = file->private_data;
332 ssize_t ret = 0;
85b1492e 333 char kbuf[80];
cafe5635
KO
334
335 while (size) {
336 struct keybuf_key *w;
337 unsigned bytes = min(i->bytes, size);
338
339 int err = copy_to_user(buf, i->buf, bytes);
340 if (err)
341 return err;
342
343 ret += bytes;
344 buf += bytes;
345 size -= bytes;
346 i->bytes -= bytes;
347 memmove(i->buf, i->buf + bytes, i->bytes);
348
349 if (i->bytes)
350 break;
351
72c27061 352 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
cafe5635
KO
353 if (!w)
354 break;
355
85b1492e
KO
356 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
357 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
cafe5635
KO
358 bch_keybuf_del(&i->keys, w);
359 }
360
361 return ret;
362}
363
364static int bch_dump_open(struct inode *inode, struct file *file)
365{
366 struct cache_set *c = inode->i_private;
367 struct dump_iterator *i;
368
369 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
370 if (!i)
371 return -ENOMEM;
372
373 file->private_data = i;
374 i->c = c;
72c27061 375 bch_keybuf_init(&i->keys);
cafe5635
KO
376 i->keys.last_scanned = KEY(0, 0, 0);
377
378 return 0;
379}
380
381static int bch_dump_release(struct inode *inode, struct file *file)
382{
383 kfree(file->private_data);
384 return 0;
385}
386
387static const struct file_operations cache_set_debug_ops = {
388 .owner = THIS_MODULE,
389 .open = bch_dump_open,
390 .read = bch_dump_read,
391 .release = bch_dump_release
392};
393
394void bch_debug_init_cache_set(struct cache_set *c)
395{
396 if (!IS_ERR_OR_NULL(debug)) {
397 char name[50];
398 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
399
400 c->debug = debugfs_create_file(name, 0400, debug, c,
401 &cache_set_debug_ops);
402 }
403}
404
405#endif
406
cafe5635
KO
407void bch_debug_exit(void)
408{
409 if (!IS_ERR_OR_NULL(debug))
410 debugfs_remove_recursive(debug);
411}
412
413int __init bch_debug_init(struct kobject *kobj)
414{
415 int ret = 0;
cafe5635
KO
416
417 debug = debugfs_create_dir("bcache", NULL);
418 return ret;
419}