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, block_bytes(b->c)) * \
129 void bch_btree_verify(struct btree *b)
131 struct btree *v = b->c->verify_data;
132 struct bset *ondisk, *sorted, *inmemory;
135 if (!b->c->verify || !b->c->verify_ondisk)
139 mutex_lock(&b->c->verify_lock);
141 ondisk = b->c->verify_ondisk;
142 sorted = b->c->verify_data->sets->data;
143 inmemory = b->sets->data;
145 bkey_copy(&v->key, &b->key);
149 bio = bch_bbio_alloc(b->c);
150 bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev;
151 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
152 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
153 bch_bio_map(bio, sorted);
155 submit_bio_wait(REQ_META|READ_SYNC, bio);
156 bch_bbio_free(bio, b->c);
158 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
160 bch_btree_node_read_done(v);
161 sorted = v->sets->data;
163 if (inmemory->keys != sorted->keys ||
164 memcmp(inmemory->start,
166 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
172 printk(KERN_ERR "*** in memory:\n");
173 dump_bset(b, inmemory, 0);
175 printk(KERN_ERR "*** read back in:\n");
176 dump_bset(v, sorted, 0);
178 for_each_written_bset(b, ondisk, i) {
179 unsigned block = ((void *) i - (void *) ondisk) /
182 printk(KERN_ERR "*** on disk block %u:\n", block);
183 dump_bset(b, i, block);
186 printk(KERN_ERR "*** block %zu not written\n",
187 ((void *) i - (void *) ondisk) / block_bytes(b->c));
189 for (j = 0; j < inmemory->keys; j++)
190 if (inmemory->d[j] != sorted->d[j])
193 printk(KERN_ERR "b->written %u\n", b->written);
196 panic("verify failed at %u\n", j);
199 mutex_unlock(&b->c->verify_lock);
203 void bch_data_verify(struct cached_dev *dc, struct bio *bio)
205 char name[BDEVNAME_SIZE];
207 struct bio_vec bv, *bv2;
208 struct bvec_iter iter;
211 check = bio_clone(bio, GFP_NOIO);
215 if (bio_alloc_pages(check, GFP_NOIO))
218 submit_bio_wait(READ_SYNC, check);
220 bio_for_each_segment(bv, bio, iter) {
221 void *p1 = kmap_atomic(bv.bv_page);
222 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
224 cache_set_err_on(memcmp(p1 + bv.bv_offset,
228 "verify failed at dev %s sector %llu",
229 bdevname(dc->bdev, name),
230 (uint64_t) bio->bi_iter.bi_sector);
235 bio_for_each_segment_all(bv2, check, i)
236 __free_page(bv2->bv_page);
241 int __bch_count_data(struct btree *b)
244 struct btree_iter iter;
248 for_each_key(b, k, &iter)
253 void __bch_check_keys(struct btree *b, const char *fmt, ...)
256 struct bkey *k, *p = NULL;
257 struct btree_iter iter;
260 for_each_key(b, k, &iter) {
262 err = "Keys out of order";
263 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
266 if (bch_ptr_invalid(b, k))
269 err = "Overlapping keys";
270 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
273 if (bch_ptr_bad(b, k))
276 err = "Duplicate keys";
277 if (p && !bkey_cmp(p, k))
283 err = "Key larger than btree node key";
284 if (p && bkey_cmp(p, &b->key) > 0)
295 panic("bcache error: %s:\n", err);
298 void bch_btree_iter_next_check(struct btree_iter *iter)
300 struct bkey *k = iter->data->k, *next = bkey_next(k);
302 if (next < iter->data->end &&
303 bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
304 bch_dump_bucket(iter->b);
305 panic("Key skipped backwards\n");
311 #ifdef CONFIG_DEBUG_FS
313 /* XXX: cache set refcounting */
315 struct dump_iterator {
322 static bool dump_pred(struct keybuf *buf, struct bkey *k)
327 static ssize_t bch_dump_read(struct file *file, char __user *buf,
328 size_t size, loff_t *ppos)
330 struct dump_iterator *i = file->private_data;
335 struct keybuf_key *w;
336 unsigned bytes = min(i->bytes, size);
338 int err = copy_to_user(buf, i->buf, bytes);
346 memmove(i->buf, i->buf + bytes, i->bytes);
351 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
355 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
356 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
357 bch_keybuf_del(&i->keys, w);
363 static int bch_dump_open(struct inode *inode, struct file *file)
365 struct cache_set *c = inode->i_private;
366 struct dump_iterator *i;
368 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
372 file->private_data = i;
374 bch_keybuf_init(&i->keys);
375 i->keys.last_scanned = KEY(0, 0, 0);
380 static int bch_dump_release(struct inode *inode, struct file *file)
382 kfree(file->private_data);
386 static const struct file_operations cache_set_debug_ops = {
387 .owner = THIS_MODULE,
388 .open = bch_dump_open,
389 .read = bch_dump_read,
390 .release = bch_dump_release
393 void bch_debug_init_cache_set(struct cache_set *c)
395 if (!IS_ERR_OR_NULL(debug)) {
397 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
399 c->debug = debugfs_create_file(name, 0400, debug, c,
400 &cache_set_debug_ops);
406 void bch_debug_exit(void)
408 if (!IS_ERR_OR_NULL(debug))
409 debugfs_remove_recursive(debug);
412 int __init bch_debug_init(struct kobject *kobj)
416 debug = debugfs_create_dir("bcache", NULL);