2 * Assorted bcache debug code
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
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>
18 static struct dentry *debug;
20 const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
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));
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))
40 if (!bkey_cmp(k, &ZERO_KEY))
41 return "bad, null key";
43 return "bad, no pointers";
49 int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
52 char *out = buf, *end = buf + size;
54 #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
56 p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
58 for (i = 0; i < KEY_PTRS(k); i++) {
62 if (PTR_DEV(k, i) == PTR_CHECK_DEV)
65 p("%llu:%llu gen %llu", PTR_DEV(k, i),
66 PTR_OFFSET(k, i), PTR_GEN(k, i));
74 p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
79 #ifdef CONFIG_BCACHE_DEBUG
81 static void dump_bset(struct btree *b, struct bset *i, unsigned set)
83 struct bkey *k, *next;
87 for (k = i->start; k < bset_bkey_last(i); k = next) {
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);
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);
98 if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
100 PTR_BUCKET(b->c, k, j)->prio);
103 printk(" %s\n", bch_ptr_status(b->c, k));
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");
111 static void bch_dump_bucket(struct btree *b)
116 for (i = 0; i <= b->nsets; i++)
117 dump_bset(b, b->sets[i].data,
118 bset_block_offset(b, b->sets[i].data));
122 #define for_each_written_bset(b, start, i) \
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))
128 void bch_btree_verify(struct btree *b)
130 struct btree *v = b->c->verify_data;
131 struct bset *ondisk, *sorted, *inmemory;
134 if (!b->c->verify || !b->c->verify_ondisk)
138 mutex_lock(&b->c->verify_lock);
140 ondisk = b->c->verify_ondisk;
141 sorted = b->c->verify_data->sets->data;
142 inmemory = b->sets->data;
144 bkey_copy(&v->key, &b->key);
148 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);
154 submit_bio_wait(REQ_META|READ_SYNC, bio);
155 bch_bbio_free(bio, b->c);
157 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
159 bch_btree_node_read_done(v);
160 sorted = v->sets->data;
162 if (inmemory->keys != sorted->keys ||
163 memcmp(inmemory->start,
165 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
171 printk(KERN_ERR "*** in memory:\n");
172 dump_bset(b, inmemory, 0);
174 printk(KERN_ERR "*** read back in:\n");
175 dump_bset(v, sorted, 0);
177 for_each_written_bset(b, ondisk, i) {
178 unsigned block = ((void *) i - (void *) ondisk) /
181 printk(KERN_ERR "*** on disk block %u:\n", block);
182 dump_bset(b, i, block);
185 printk(KERN_ERR "*** block %zu not written\n",
186 ((void *) i - (void *) ondisk) / block_bytes(b->c));
188 for (j = 0; j < inmemory->keys; j++)
189 if (inmemory->d[j] != sorted->d[j])
192 printk(KERN_ERR "b->written %u\n", b->written);
195 panic("verify failed at %u\n", j);
198 mutex_unlock(&b->c->verify_lock);
202 void bch_data_verify(struct cached_dev *dc, struct bio *bio)
204 char name[BDEVNAME_SIZE];
206 struct bio_vec bv, *bv2;
207 struct bvec_iter iter;
210 check = bio_clone(bio, GFP_NOIO);
214 if (bio_alloc_pages(check, GFP_NOIO))
217 submit_bio_wait(READ_SYNC, check);
219 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);
223 cache_set_err_on(memcmp(p1 + bv.bv_offset,
227 "verify failed at dev %s sector %llu",
228 bdevname(dc->bdev, name),
229 (uint64_t) bio->bi_iter.bi_sector);
234 bio_for_each_segment_all(bv2, check, i)
235 __free_page(bv2->bv_page);
240 int __bch_count_data(struct btree *b)
243 struct btree_iter iter;
247 for_each_key(b, k, &iter)
252 void __bch_check_keys(struct btree *b, const char *fmt, ...)
255 struct bkey *k, *p = NULL;
256 struct btree_iter iter;
259 for_each_key(b, k, &iter) {
261 err = "Keys out of order";
262 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
265 if (bch_ptr_invalid(b, k))
268 err = "Overlapping keys";
269 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
272 if (bch_ptr_bad(b, k))
275 err = "Duplicate keys";
276 if (p && !bkey_cmp(p, k))
282 err = "Key larger than btree node key";
283 if (p && bkey_cmp(p, &b->key) > 0)
294 panic("bcache error: %s:\n", err);
297 void bch_btree_iter_next_check(struct btree_iter *iter)
299 struct bkey *k = iter->data->k, *next = bkey_next(k);
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");
310 #ifdef CONFIG_DEBUG_FS
312 /* XXX: cache set refcounting */
314 struct dump_iterator {
321 static bool dump_pred(struct keybuf *buf, struct bkey *k)
326 static ssize_t bch_dump_read(struct file *file, char __user *buf,
327 size_t size, loff_t *ppos)
329 struct dump_iterator *i = file->private_data;
334 struct keybuf_key *w;
335 unsigned bytes = min(i->bytes, size);
337 int err = copy_to_user(buf, i->buf, bytes);
345 memmove(i->buf, i->buf + bytes, i->bytes);
350 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
354 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
355 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
356 bch_keybuf_del(&i->keys, w);
362 static int bch_dump_open(struct inode *inode, struct file *file)
364 struct cache_set *c = inode->i_private;
365 struct dump_iterator *i;
367 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
371 file->private_data = i;
373 bch_keybuf_init(&i->keys);
374 i->keys.last_scanned = KEY(0, 0, 0);
379 static int bch_dump_release(struct inode *inode, struct file *file)
381 kfree(file->private_data);
385 static 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
392 void bch_debug_init_cache_set(struct cache_set *c)
394 if (!IS_ERR_OR_NULL(debug)) {
396 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
398 c->debug = debugfs_create_file(name, 0400, debug, c,
399 &cache_set_debug_ops);
405 void bch_debug_exit(void)
407 if (!IS_ERR_OR_NULL(debug))
408 debugfs_remove_recursive(debug);
411 int __init bch_debug_init(struct kobject *kobj)
415 debug = debugfs_create_dir("bcache", NULL);