1 // SPDX-License-Identifier: GPL-2.0
3 * Assorted bcache debug code
6 * Copyright 2012 Google, Inc.
14 #include <linux/console.h>
15 #include <linux/debugfs.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/seq_file.h>
20 struct dentry *bcache_debug;
22 #ifdef CONFIG_BCACHE_DEBUG
24 #define for_each_written_bset(b, start, i) \
26 (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\
27 i->seq == (start)->seq; \
28 i = (void *) i + set_blocks(i, block_bytes(b->c->cache)) * \
29 block_bytes(b->c->cache))
31 void bch_btree_verify(struct btree *b)
33 struct btree *v = b->c->verify_data;
34 struct bset *ondisk, *sorted, *inmemory;
37 if (!b->c->verify || !b->c->verify_ondisk)
41 mutex_lock(&b->c->verify_lock);
43 ondisk = b->c->verify_ondisk;
44 sorted = b->c->verify_data->keys.set->data;
45 inmemory = b->keys.set->data;
47 bkey_copy(&v->key, &b->key);
50 v->keys.ops = b->keys.ops;
52 bio = bch_bbio_alloc(b->c);
53 bio_set_dev(bio, b->c->cache->bdev);
54 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
55 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
56 bio->bi_opf = REQ_OP_READ | REQ_META;
57 bch_bio_map(bio, sorted);
60 bch_bbio_free(bio, b->c);
62 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
64 bch_btree_node_read_done(v);
65 sorted = v->keys.set->data;
67 if (inmemory->keys != sorted->keys ||
68 memcmp(inmemory->start,
70 (void *) bset_bkey_last(inmemory) -
71 (void *) inmemory->start)) {
77 pr_err("*** in memory:\n");
78 bch_dump_bset(&b->keys, inmemory, 0);
80 pr_err("*** read back in:\n");
81 bch_dump_bset(&v->keys, sorted, 0);
83 for_each_written_bset(b, ondisk, i) {
84 unsigned int block = ((void *) i - (void *) ondisk) /
85 block_bytes(b->c->cache);
87 pr_err("*** on disk block %u:\n", block);
88 bch_dump_bset(&b->keys, i, block);
91 pr_err("*** block %zu not written\n",
92 ((void *) i - (void *) ondisk) / block_bytes(b->c->cache));
94 for (j = 0; j < inmemory->keys; j++)
95 if (inmemory->d[j] != sorted->d[j])
98 pr_err("b->written %u\n", b->written);
101 panic("verify failed at %u\n", j);
104 mutex_unlock(&b->c->verify_lock);
108 void bch_data_verify(struct cached_dev *dc, struct bio *bio)
110 unsigned int nr_segs = bio_segments(bio);
112 struct bio_vec bv, cbv;
113 struct bvec_iter iter, citer = { 0 };
115 check = bio_kmalloc(nr_segs, GFP_NOIO);
118 bio_init(check, bio->bi_bdev, check->bi_inline_vecs, nr_segs,
120 check->bi_iter.bi_sector = bio->bi_iter.bi_sector;
121 check->bi_iter.bi_size = bio->bi_iter.bi_size;
123 bch_bio_map(check, NULL);
124 if (bch_bio_alloc_pages(check, GFP_NOIO))
127 submit_bio_wait(check);
129 citer.bi_size = UINT_MAX;
130 bio_for_each_segment(bv, bio, iter) {
131 void *p1 = bvec_kmap_local(&bv);
134 cbv = bio_iter_iovec(check, citer);
135 p2 = bvec_kmap_local(&cbv);
137 cache_set_err_on(memcmp(p1, p2, bv.bv_len),
139 "verify failed at dev %pg sector %llu",
141 (uint64_t) bio->bi_iter.bi_sector);
145 bio_advance_iter(check, &citer, bv.bv_len);
148 bio_free_pages(check);
156 #ifdef CONFIG_DEBUG_FS
158 /* XXX: cache set refcounting */
160 struct dump_iterator {
167 static bool dump_pred(struct keybuf *buf, struct bkey *k)
172 static ssize_t bch_dump_read(struct file *file, char __user *buf,
173 size_t size, loff_t *ppos)
175 struct dump_iterator *i = file->private_data;
180 struct keybuf_key *w;
181 unsigned int bytes = min(i->bytes, size);
183 if (copy_to_user(buf, i->buf, bytes))
190 memmove(i->buf, i->buf + bytes, i->bytes);
195 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
199 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
200 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
201 bch_keybuf_del(&i->keys, w);
207 static int bch_dump_open(struct inode *inode, struct file *file)
209 struct cache_set *c = inode->i_private;
210 struct dump_iterator *i;
212 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
216 file->private_data = i;
218 bch_keybuf_init(&i->keys);
219 i->keys.last_scanned = KEY(0, 0, 0);
224 static int bch_dump_release(struct inode *inode, struct file *file)
226 kfree(file->private_data);
230 static const struct file_operations cache_set_debug_ops = {
231 .owner = THIS_MODULE,
232 .open = bch_dump_open,
233 .read = bch_dump_read,
234 .release = bch_dump_release
237 void bch_debug_init_cache_set(struct cache_set *c)
239 if (!IS_ERR_OR_NULL(bcache_debug)) {
242 snprintf(name, 50, "bcache-%pU", c->set_uuid);
243 c->debug = debugfs_create_file(name, 0400, bcache_debug, c,
244 &cache_set_debug_ops);
250 void bch_debug_exit(void)
252 debugfs_remove_recursive(bcache_debug);
255 void __init bch_debug_init(void)
258 * it is unnecessary to check return value of
259 * debugfs_create_file(), we should not care
262 bcache_debug = debugfs_create_dir("bcache", NULL);