Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Assorted bcache debug code |
| 4 | * |
| 5 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> |
| 6 | * Copyright 2012 Google, Inc. |
| 7 | */ |
| 8 | |
| 9 | #include "bcache.h" |
| 10 | #include "btree.h" |
| 11 | #include "debug.h" |
Kent Overstreet | dc9d98d6 | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 12 | #include "extents.h" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 13 | |
| 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> |
| 19 | |
Chengguang Xu | df2b943 | 2018-03-18 17:36:23 -0700 | [diff] [blame] | 20 | struct dentry *bcache_debug; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 21 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 22 | #ifdef CONFIG_BCACHE_DEBUG |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 23 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 24 | #define for_each_written_bset(b, start, i) \ |
| 25 | for (i = (start); \ |
| 26 | (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\ |
| 27 | i->seq == (start)->seq; \ |
Coly Li | 4e1ebae | 2020-10-01 14:50:49 +0800 | [diff] [blame] | 28 | i = (void *) i + set_blocks(i, block_bytes(b->c->cache)) * \ |
| 29 | block_bytes(b->c->cache)) |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 30 | |
| 31 | void bch_btree_verify(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 32 | { |
| 33 | struct btree *v = b->c->verify_data; |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 34 | struct bset *ondisk, *sorted, *inmemory; |
| 35 | struct bio *bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 36 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 37 | if (!b->c->verify || !b->c->verify_ondisk) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 38 | return; |
| 39 | |
Kent Overstreet | cb7a583 | 2013-12-16 15:27:25 -0800 | [diff] [blame] | 40 | down(&b->io_mutex); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 41 | mutex_lock(&b->c->verify_lock); |
| 42 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 43 | ondisk = b->c->verify_ondisk; |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 44 | sorted = b->c->verify_data->keys.set->data; |
| 45 | inmemory = b->keys.set->data; |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 46 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 47 | bkey_copy(&v->key, &b->key); |
| 48 | v->written = 0; |
| 49 | v->level = b->level; |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 50 | v->keys.ops = b->keys.ops; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 51 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 52 | bio = bch_bbio_alloc(b->c); |
Coly Li | 33ec5df | 2021-04-11 21:43:16 +0800 | [diff] [blame] | 53 | bio_set_dev(bio, b->c->cache->bdev); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 54 | bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0); |
| 55 | bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 56 | bio->bi_opf = REQ_OP_READ | REQ_META; |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 57 | bch_bio_map(bio, sorted); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 58 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 59 | submit_bio_wait(bio); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 60 | bch_bbio_free(bio, b->c); |
| 61 | |
| 62 | memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9); |
| 63 | |
| 64 | bch_btree_node_read_done(v); |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 65 | sorted = v->keys.set->data; |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 66 | |
| 67 | if (inmemory->keys != sorted->keys || |
| 68 | memcmp(inmemory->start, |
| 69 | sorted->start, |
Coly Li | b0d3098 | 2018-08-11 13:19:47 +0800 | [diff] [blame] | 70 | (void *) bset_bkey_last(inmemory) - |
| 71 | (void *) inmemory->start)) { |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 72 | struct bset *i; |
Coly Li | 6f10f7d | 2018-08-11 13:19:44 +0800 | [diff] [blame] | 73 | unsigned int j; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 74 | |
| 75 | console_lock(); |
| 76 | |
Coly Li | 6ae63e3 | 2018-08-11 13:19:49 +0800 | [diff] [blame] | 77 | pr_err("*** in memory:\n"); |
Kent Overstreet | dc9d98d6 | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 78 | bch_dump_bset(&b->keys, inmemory, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 79 | |
Coly Li | 6ae63e3 | 2018-08-11 13:19:49 +0800 | [diff] [blame] | 80 | pr_err("*** read back in:\n"); |
Kent Overstreet | dc9d98d6 | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 81 | bch_dump_bset(&v->keys, sorted, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 82 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 83 | for_each_written_bset(b, ondisk, i) { |
Coly Li | 6f10f7d | 2018-08-11 13:19:44 +0800 | [diff] [blame] | 84 | unsigned int block = ((void *) i - (void *) ondisk) / |
Coly Li | 4e1ebae | 2020-10-01 14:50:49 +0800 | [diff] [blame] | 85 | block_bytes(b->c->cache); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 86 | |
Coly Li | 6ae63e3 | 2018-08-11 13:19:49 +0800 | [diff] [blame] | 87 | pr_err("*** on disk block %u:\n", block); |
Kent Overstreet | dc9d98d6 | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 88 | bch_dump_bset(&b->keys, i, block); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Coly Li | 6ae63e3 | 2018-08-11 13:19:49 +0800 | [diff] [blame] | 91 | pr_err("*** block %zu not written\n", |
Coly Li | 4e1ebae | 2020-10-01 14:50:49 +0800 | [diff] [blame] | 92 | ((void *) i - (void *) ondisk) / block_bytes(b->c->cache)); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 93 | |
| 94 | for (j = 0; j < inmemory->keys; j++) |
| 95 | if (inmemory->d[j] != sorted->d[j]) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 96 | break; |
| 97 | |
Coly Li | 6ae63e3 | 2018-08-11 13:19:49 +0800 | [diff] [blame] | 98 | pr_err("b->written %u\n", b->written); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 99 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 100 | console_unlock(); |
| 101 | panic("verify failed at %u\n", j); |
| 102 | } |
| 103 | |
| 104 | mutex_unlock(&b->c->verify_lock); |
Kent Overstreet | cb7a583 | 2013-12-16 15:27:25 -0800 | [diff] [blame] | 105 | up(&b->io_mutex); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 108 | void bch_data_verify(struct cached_dev *dc, struct bio *bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 109 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 110 | struct bio *check; |
Ming Lei | 4113b88 | 2016-11-11 20:05:33 +0800 | [diff] [blame] | 111 | struct bio_vec bv, cbv; |
| 112 | struct bvec_iter iter, citer = { 0 }; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 113 | |
Christoph Hellwig | c8b27ac | 2018-07-24 09:52:30 +0200 | [diff] [blame] | 114 | check = bio_kmalloc(GFP_NOIO, bio_segments(bio)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 115 | if (!check) |
| 116 | return; |
Christoph Hellwig | f65b95f | 2021-01-26 15:33:07 +0100 | [diff] [blame] | 117 | bio_set_dev(check, bio->bi_bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 118 | check->bi_opf = REQ_OP_READ; |
Christoph Hellwig | c8b27ac | 2018-07-24 09:52:30 +0200 | [diff] [blame] | 119 | check->bi_iter.bi_sector = bio->bi_iter.bi_sector; |
| 120 | check->bi_iter.bi_size = bio->bi_iter.bi_size; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 121 | |
Christoph Hellwig | c8b27ac | 2018-07-24 09:52:30 +0200 | [diff] [blame] | 122 | bch_bio_map(check, NULL); |
Ming Lei | 25d8be7 | 2017-12-18 20:22:10 +0800 | [diff] [blame] | 123 | if (bch_bio_alloc_pages(check, GFP_NOIO)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 124 | goto out_put; |
| 125 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 126 | submit_bio_wait(check); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 127 | |
Ming Lei | 4113b88 | 2016-11-11 20:05:33 +0800 | [diff] [blame] | 128 | citer.bi_size = UINT_MAX; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 129 | bio_for_each_segment(bv, bio, iter) { |
Christoph Hellwig | 00387bd | 2021-10-20 22:38:11 +0800 | [diff] [blame] | 130 | void *p1 = bvec_kmap_local(&bv); |
Ming Lei | 4113b88 | 2016-11-11 20:05:33 +0800 | [diff] [blame] | 131 | void *p2; |
| 132 | |
| 133 | cbv = bio_iter_iovec(check, citer); |
Christoph Hellwig | 00387bd | 2021-10-20 22:38:11 +0800 | [diff] [blame] | 134 | p2 = bvec_kmap_local(&cbv); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 135 | |
Christoph Hellwig | 00387bd | 2021-10-20 22:38:11 +0800 | [diff] [blame] | 136 | cache_set_err_on(memcmp(p1, p2, bv.bv_len), |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 137 | dc->disk.c, |
Christoph Hellwig | 0f5cd78 | 2021-10-20 22:38:10 +0800 | [diff] [blame] | 138 | "verify failed at dev %pg sector %llu", |
| 139 | dc->bdev, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 140 | (uint64_t) bio->bi_iter.bi_sector); |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 141 | |
Christoph Hellwig | 00387bd | 2021-10-20 22:38:11 +0800 | [diff] [blame] | 142 | kunmap_local(p2); |
| 143 | kunmap_local(p1); |
Ming Lei | 4113b88 | 2016-11-11 20:05:33 +0800 | [diff] [blame] | 144 | bio_advance_iter(check, &citer, bv.bv_len); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Guoqing Jiang | 491221f | 2016-09-22 03:10:01 -0400 | [diff] [blame] | 147 | bio_free_pages(check); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 148 | out_put: |
| 149 | bio_put(check); |
| 150 | } |
| 151 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | #ifdef CONFIG_DEBUG_FS |
| 155 | |
| 156 | /* XXX: cache set refcounting */ |
| 157 | |
| 158 | struct dump_iterator { |
| 159 | char buf[PAGE_SIZE]; |
| 160 | size_t bytes; |
| 161 | struct cache_set *c; |
| 162 | struct keybuf keys; |
| 163 | }; |
| 164 | |
| 165 | static bool dump_pred(struct keybuf *buf, struct bkey *k) |
| 166 | { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | static ssize_t bch_dump_read(struct file *file, char __user *buf, |
| 171 | size_t size, loff_t *ppos) |
| 172 | { |
| 173 | struct dump_iterator *i = file->private_data; |
| 174 | ssize_t ret = 0; |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 175 | char kbuf[80]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 176 | |
| 177 | while (size) { |
| 178 | struct keybuf_key *w; |
Coly Li | 6f10f7d | 2018-08-11 13:19:44 +0800 | [diff] [blame] | 179 | unsigned int bytes = min(i->bytes, size); |
Coly Li | 1fae7cf | 2018-08-11 13:19:45 +0800 | [diff] [blame] | 180 | |
Dan Carpenter | d66c992 | 2019-09-03 21:25:44 +0800 | [diff] [blame] | 181 | if (copy_to_user(buf, i->buf, bytes)) |
| 182 | return -EFAULT; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 183 | |
| 184 | ret += bytes; |
| 185 | buf += bytes; |
| 186 | size -= bytes; |
| 187 | i->bytes -= bytes; |
| 188 | memmove(i->buf, i->buf + bytes, i->bytes); |
| 189 | |
| 190 | if (i->bytes) |
| 191 | break; |
| 192 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 193 | w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 194 | if (!w) |
| 195 | break; |
| 196 | |
Kent Overstreet | dc9d98d6 | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 197 | bch_extent_to_text(kbuf, sizeof(kbuf), &w->key); |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 198 | i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 199 | bch_keybuf_del(&i->keys, w); |
| 200 | } |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static int bch_dump_open(struct inode *inode, struct file *file) |
| 206 | { |
| 207 | struct cache_set *c = inode->i_private; |
| 208 | struct dump_iterator *i; |
| 209 | |
| 210 | i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL); |
| 211 | if (!i) |
| 212 | return -ENOMEM; |
| 213 | |
| 214 | file->private_data = i; |
| 215 | i->c = c; |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 216 | bch_keybuf_init(&i->keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 217 | i->keys.last_scanned = KEY(0, 0, 0); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int bch_dump_release(struct inode *inode, struct file *file) |
| 223 | { |
| 224 | kfree(file->private_data); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static const struct file_operations cache_set_debug_ops = { |
| 229 | .owner = THIS_MODULE, |
| 230 | .open = bch_dump_open, |
| 231 | .read = bch_dump_read, |
| 232 | .release = bch_dump_release |
| 233 | }; |
| 234 | |
| 235 | void bch_debug_init_cache_set(struct cache_set *c) |
| 236 | { |
Chengguang Xu | df2b943 | 2018-03-18 17:36:23 -0700 | [diff] [blame] | 237 | if (!IS_ERR_OR_NULL(bcache_debug)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 238 | char name[50]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 239 | |
Coly Li | 1132e56 | 2020-10-01 14:50:48 +0800 | [diff] [blame] | 240 | snprintf(name, 50, "bcache-%pU", c->set_uuid); |
Chengguang Xu | df2b943 | 2018-03-18 17:36:23 -0700 | [diff] [blame] | 241 | c->debug = debugfs_create_file(name, 0400, bcache_debug, c, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 242 | &cache_set_debug_ops); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #endif |
| 247 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 248 | void bch_debug_exit(void) |
| 249 | { |
Shenghui Wang | ae17102 | 2018-12-13 22:53:47 +0800 | [diff] [blame] | 250 | debugfs_remove_recursive(bcache_debug); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Dongbo Cao | 91bafdf | 2018-10-08 20:41:17 +0800 | [diff] [blame] | 253 | void __init bch_debug_init(void) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 254 | { |
Coly Li | 78ac210 | 2018-08-09 15:48:42 +0800 | [diff] [blame] | 255 | /* |
| 256 | * it is unnecessary to check return value of |
| 257 | * debugfs_create_file(), we should not care |
| 258 | * about this. |
| 259 | */ |
Coly Li | 1c1a2ee | 2018-05-17 23:33:26 +0800 | [diff] [blame] | 260 | bcache_debug = debugfs_create_dir("bcache", NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 261 | } |