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