debug.c (c052dd9a26f60bcf70c0c3fcc08e07abb60295cd) debug.c (dc9d98d621bdce0552997200ce855659875a5c9f)
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"
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"
11#include "extents.h"
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
12
13#include <linux/console.h>
14#include <linux/debugfs.h>
15#include <linux/module.h>
16#include <linux/random.h>
17#include <linux/seq_file.h>
18
19static struct dentry *debug;
20
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
49int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
50{
51 unsigned i = 0;
52 char *out = buf, *end = buf + size;
53
54#define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
55
56 p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
57
58 for (i = 0; i < KEY_PTRS(k); i++) {
59 if (i)
60 p(", ");
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 }
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
76 return out - buf;
77}
78
79#ifdef CONFIG_BCACHE_DEBUG
80
21#ifdef CONFIG_BCACHE_DEBUG
22
81static void dump_bset(struct btree *b, struct bset *i, unsigned set)
82{
83 struct bkey *k, *next;
84 unsigned j;
85 char buf[80];
86
87 for (k = i->start; k < bset_bkey_last(i); k = next) {
88 next = bkey_next(k);
89
90 bch_bkey_to_text(buf, sizeof(buf), k);
91 printk(KERN_ERR "b %u k %zi/%u: %s", set,
92 (uint64_t *) k - i->d, i->keys, buf);
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
105 if (next < bset_bkey_last(i) &&
106 bkey_cmp(k, !b->level ? &START_KEY(next) : next) > 0)
107 printk(KERN_ERR "Key skipped backwards\n");
108 }
109}
110
111static void bch_dump_bucket(struct btree *b)
112{
113 unsigned i;
114
115 console_lock();
116 for (i = 0; i <= b->keys.nsets; i++)
117 dump_bset(b, b->keys.set[i].data,
118 bset_block_offset(b, b->keys.set[i].data));
119 console_unlock();
120}
121
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; \
126 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
127 block_bytes(b->c))
128
129void bch_btree_verify(struct btree *b)

--- 36 unchanged lines hidden (view full) ---

166 sorted->start,
167 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
168 struct bset *i;
169 unsigned j;
170
171 console_lock();
172
173 printk(KERN_ERR "*** in memory:\n");
23#define for_each_written_bset(b, start, i) \
24 for (i = (start); \
25 (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\
26 i->seq == (start)->seq; \
27 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
28 block_bytes(b->c))
29
30void bch_btree_verify(struct btree *b)

--- 36 unchanged lines hidden (view full) ---

67 sorted->start,
68 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
69 struct bset *i;
70 unsigned j;
71
72 console_lock();
73
74 printk(KERN_ERR "*** in memory:\n");
174 dump_bset(b, inmemory, 0);
75 bch_dump_bset(&b->keys, inmemory, 0);
175
176 printk(KERN_ERR "*** read back in:\n");
76
77 printk(KERN_ERR "*** read back in:\n");
177 dump_bset(v, sorted, 0);
78 bch_dump_bset(&v->keys, sorted, 0);
178
179 for_each_written_bset(b, ondisk, i) {
180 unsigned block = ((void *) i - (void *) ondisk) /
181 block_bytes(b->c);
182
183 printk(KERN_ERR "*** on disk block %u:\n", block);
79
80 for_each_written_bset(b, ondisk, i) {
81 unsigned block = ((void *) i - (void *) ondisk) /
82 block_bytes(b->c);
83
84 printk(KERN_ERR "*** on disk block %u:\n", block);
184 dump_bset(b, i, block);
85 bch_dump_bset(&b->keys, 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])
192 break;

--- 41 unchanged lines hidden (view full) ---

234 }
235
236 bio_for_each_segment_all(bv2, check, i)
237 __free_page(bv2->bv_page);
238out_put:
239 bio_put(check);
240}
241
86 }
87
88 printk(KERN_ERR "*** block %zu not written\n",
89 ((void *) i - (void *) ondisk) / block_bytes(b->c));
90
91 for (j = 0; j < inmemory->keys; j++)
92 if (inmemory->d[j] != sorted->d[j])
93 break;

--- 41 unchanged lines hidden (view full) ---

135 }
136
137 bio_for_each_segment_all(bv2, check, i)
138 __free_page(bv2->bv_page);
139out_put:
140 bio_put(check);
141}
142
242int __bch_count_data(struct btree *b)
243{
244 unsigned ret = 0;
245 struct btree_iter iter;
246 struct bkey *k;
247
248 if (!b->level)
249 for_each_key(&b->keys, k, &iter)
250 ret += KEY_SIZE(k);
251 return ret;
252}
253
254void __bch_check_keys(struct btree *b, const char *fmt, ...)
255{
256 va_list args;
257 struct bkey *k, *p = NULL;
258 struct btree_iter iter;
259 const char *err;
260
261 for_each_key(&b->keys, k, &iter) {
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->keys, 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->keys, k))
275 continue;
276
277 err = "Duplicate keys";
278 if (p && !bkey_cmp(p, k))
279 goto bug;
280 }
281 p = k;
282 }
283
284 err = "Key larger than btree node key";
285 if (p && bkey_cmp(p, &b->key) > 0)
286 goto bug;
287
288 return;
289bug:
290 bch_dump_bucket(b);
291
292 va_start(args, fmt);
293 vprintk(fmt, args);
294 va_end(args);
295
296 panic("bcache error: %s:\n", err);
297}
298
299void bch_btree_iter_next_check(struct btree_iter *iter)
300{
301#if 0
302 struct bkey *k = iter->data->k, *next = bkey_next(k);
303
304 if (next < iter->data->end &&
305 bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
306 bch_dump_bucket(iter->b);
307 panic("Key skipped backwards\n");
308 }
309#endif
143#endif
310}
311
144
312#endif
313
314#ifdef CONFIG_DEBUG_FS
315
316/* XXX: cache set refcounting */
317
318struct dump_iterator {
319 char buf[PAGE_SIZE];
320 size_t bytes;
321 struct cache_set *c;

--- 28 unchanged lines hidden (view full) ---

350
351 if (i->bytes)
352 break;
353
354 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
355 if (!w)
356 break;
357
145#ifdef CONFIG_DEBUG_FS
146
147/* XXX: cache set refcounting */
148
149struct dump_iterator {
150 char buf[PAGE_SIZE];
151 size_t bytes;
152 struct cache_set *c;

--- 28 unchanged lines hidden (view full) ---

181
182 if (i->bytes)
183 break;
184
185 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
186 if (!w)
187 break;
188
358 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
189 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
359 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
360 bch_keybuf_del(&i->keys, w);
361 }
362
363 return ret;
364}
365
366static int bch_dump_open(struct inode *inode, struct file *file)

--- 55 unchanged lines hidden ---
190 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
191 bch_keybuf_del(&i->keys, w);
192 }
193
194 return ret;
195}
196
197static int bch_dump_open(struct inode *inode, struct file *file)

--- 55 unchanged lines hidden ---