blob: 028f7b386e014b6bae1bf1dc5d8b9f687faa0746 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
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 Overstreetdc9d98d2013-12-17 23:47:33 -080012#include "extents.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070013
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 Xudf2b9432018-03-18 17:36:23 -070020struct dentry *bcache_debug;
Kent Overstreetcafe5632013-03-23 16:11:31 -070021
Kent Overstreet280481d2013-10-24 16:36:03 -070022#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetcafe5632013-03-23 16:11:31 -070023
Kent Overstreet78b77bf2013-12-17 22:49:08 -080024#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; \
Kent Overstreetee811282013-12-17 23:49:49 -080028 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
29 block_bytes(b->c))
Kent Overstreet78b77bf2013-12-17 22:49:08 -080030
31void bch_btree_verify(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -070032{
33 struct btree *v = b->c->verify_data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080034 struct bset *ondisk, *sorted, *inmemory;
35 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -070036
Kent Overstreet78b77bf2013-12-17 22:49:08 -080037 if (!b->c->verify || !b->c->verify_ondisk)
Kent Overstreetcafe5632013-03-23 16:11:31 -070038 return;
39
Kent Overstreetcb7a5832013-12-16 15:27:25 -080040 down(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -070041 mutex_lock(&b->c->verify_lock);
42
Kent Overstreet78b77bf2013-12-17 22:49:08 -080043 ondisk = b->c->verify_ondisk;
Kent Overstreeta85e9682013-12-20 17:28:16 -080044 sorted = b->c->verify_data->keys.set->data;
45 inmemory = b->keys.set->data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080046
Kent Overstreetcafe5632013-03-23 16:11:31 -070047 bkey_copy(&v->key, &b->key);
48 v->written = 0;
49 v->level = b->level;
Kent Overstreeta85e9682013-12-20 17:28:16 -080050 v->keys.ops = b->keys.ops;
Kent Overstreetcafe5632013-03-23 16:11:31 -070051
Kent Overstreet78b77bf2013-12-17 22:49:08 -080052 bio = bch_bbio_alloc(b->c);
Christoph Hellwig74d46992017-08-23 19:10:32 +020053 bio_set_dev(bio, PTR_CACHE(b->c, &b->key, 0)->bdev);
Kent Overstreet78b77bf2013-12-17 22:49:08 -080054 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
55 bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
Christoph Hellwig70fd7612016-11-01 07:40:10 -060056 bio->bi_opf = REQ_OP_READ | REQ_META;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080057 bch_bio_map(bio, sorted);
Kent Overstreetcafe5632013-03-23 16:11:31 -070058
Mike Christie4e49ea42016-06-05 14:31:41 -050059 submit_bio_wait(bio);
Kent Overstreet78b77bf2013-12-17 22:49:08 -080060 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 Overstreeta85e9682013-12-20 17:28:16 -080065 sorted = v->keys.set->data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080066
67 if (inmemory->keys != sorted->keys ||
68 memcmp(inmemory->start,
69 sorted->start,
Kent Overstreetfafff812013-12-17 21:56:21 -080070 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
Kent Overstreet78b77bf2013-12-17 22:49:08 -080071 struct bset *i;
72 unsigned j;
Kent Overstreetcafe5632013-03-23 16:11:31 -070073
74 console_lock();
75
Kent Overstreet78b77bf2013-12-17 22:49:08 -080076 printk(KERN_ERR "*** in memory:\n");
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080077 bch_dump_bset(&b->keys, inmemory, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -070078
Kent Overstreet78b77bf2013-12-17 22:49:08 -080079 printk(KERN_ERR "*** read back in:\n");
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080080 bch_dump_bset(&v->keys, sorted, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -070081
Kent Overstreet78b77bf2013-12-17 22:49:08 -080082 for_each_written_bset(b, ondisk, i) {
83 unsigned block = ((void *) i - (void *) ondisk) /
84 block_bytes(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -070085
Kent Overstreet78b77bf2013-12-17 22:49:08 -080086 printk(KERN_ERR "*** on disk block %u:\n", block);
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080087 bch_dump_bset(&b->keys, i, block);
Kent Overstreet78b77bf2013-12-17 22:49:08 -080088 }
89
90 printk(KERN_ERR "*** block %zu not written\n",
91 ((void *) i - (void *) ondisk) / block_bytes(b->c));
92
93 for (j = 0; j < inmemory->keys; j++)
94 if (inmemory->d[j] != sorted->d[j])
Kent Overstreetcafe5632013-03-23 16:11:31 -070095 break;
96
Kent Overstreet78b77bf2013-12-17 22:49:08 -080097 printk(KERN_ERR "b->written %u\n", b->written);
98
Kent Overstreetcafe5632013-03-23 16:11:31 -070099 console_unlock();
100 panic("verify failed at %u\n", j);
101 }
102
103 mutex_unlock(&b->c->verify_lock);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800104 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700105}
106
Kent Overstreet220bb382013-09-10 19:02:45 -0700107void bch_data_verify(struct cached_dev *dc, struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108{
109 char name[BDEVNAME_SIZE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110 struct bio *check;
Ming Lei4113b882016-11-11 20:05:33 +0800111 struct bio_vec bv, cbv;
112 struct bvec_iter iter, citer = { 0 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700113
NeilBrown5a136fd2017-06-18 14:38:59 +1000114 check = bio_clone_kmalloc(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115 if (!check)
116 return;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600117 check->bi_opf = REQ_OP_READ;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700118
Ming Lei25d8be72017-12-18 20:22:10 +0800119 if (bch_bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700120 goto out_put;
121
Mike Christie4e49ea42016-06-05 14:31:41 -0500122 submit_bio_wait(check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700123
Ming Lei4113b882016-11-11 20:05:33 +0800124 citer.bi_size = UINT_MAX;
Kent Overstreet79886132013-11-23 17:19:00 -0800125 bio_for_each_segment(bv, bio, iter) {
126 void *p1 = kmap_atomic(bv.bv_page);
Ming Lei4113b882016-11-11 20:05:33 +0800127 void *p2;
128
129 cbv = bio_iter_iovec(check, citer);
130 p2 = page_address(cbv.bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700131
Kent Overstreet79886132013-11-23 17:19:00 -0800132 cache_set_err_on(memcmp(p1 + bv.bv_offset,
133 p2 + bv.bv_offset,
134 bv.bv_len),
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700135 dc->disk.c,
136 "verify failed at dev %s sector %llu",
137 bdevname(dc->bdev, name),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700138 (uint64_t) bio->bi_iter.bi_sector);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700139
Kent Overstreet220bb382013-09-10 19:02:45 -0700140 kunmap_atomic(p1);
Ming Lei4113b882016-11-11 20:05:33 +0800141 bio_advance_iter(check, &citer, bv.bv_len);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142 }
143
Guoqing Jiang491221f2016-09-22 03:10:01 -0400144 bio_free_pages(check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700145out_put:
146 bio_put(check);
147}
148
Kent Overstreetcafe5632013-03-23 16:11:31 -0700149#endif
150
151#ifdef CONFIG_DEBUG_FS
152
153/* XXX: cache set refcounting */
154
155struct dump_iterator {
156 char buf[PAGE_SIZE];
157 size_t bytes;
158 struct cache_set *c;
159 struct keybuf keys;
160};
161
162static bool dump_pred(struct keybuf *buf, struct bkey *k)
163{
164 return true;
165}
166
167static ssize_t bch_dump_read(struct file *file, char __user *buf,
168 size_t size, loff_t *ppos)
169{
170 struct dump_iterator *i = file->private_data;
171 ssize_t ret = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -0700172 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700173
174 while (size) {
175 struct keybuf_key *w;
176 unsigned bytes = min(i->bytes, size);
177
178 int err = copy_to_user(buf, i->buf, bytes);
179 if (err)
180 return err;
181
182 ret += bytes;
183 buf += bytes;
184 size -= bytes;
185 i->bytes -= bytes;
186 memmove(i->buf, i->buf + bytes, i->bytes);
187
188 if (i->bytes)
189 break;
190
Kent Overstreet72c27062013-06-05 06:24:39 -0700191 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700192 if (!w)
193 break;
194
Kent Overstreetdc9d98d2013-12-17 23:47:33 -0800195 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
Kent Overstreet85b14922013-05-14 20:33:16 -0700196 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197 bch_keybuf_del(&i->keys, w);
198 }
199
200 return ret;
201}
202
203static int bch_dump_open(struct inode *inode, struct file *file)
204{
205 struct cache_set *c = inode->i_private;
206 struct dump_iterator *i;
207
208 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
209 if (!i)
210 return -ENOMEM;
211
212 file->private_data = i;
213 i->c = c;
Kent Overstreet72c27062013-06-05 06:24:39 -0700214 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215 i->keys.last_scanned = KEY(0, 0, 0);
216
217 return 0;
218}
219
220static int bch_dump_release(struct inode *inode, struct file *file)
221{
222 kfree(file->private_data);
223 return 0;
224}
225
226static const struct file_operations cache_set_debug_ops = {
227 .owner = THIS_MODULE,
228 .open = bch_dump_open,
229 .read = bch_dump_read,
230 .release = bch_dump_release
231};
232
233void bch_debug_init_cache_set(struct cache_set *c)
234{
Chengguang Xudf2b9432018-03-18 17:36:23 -0700235 if (!IS_ERR_OR_NULL(bcache_debug)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700236 char name[50];
237 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
238
Chengguang Xudf2b9432018-03-18 17:36:23 -0700239 c->debug = debugfs_create_file(name, 0400, bcache_debug, c,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700240 &cache_set_debug_ops);
241 }
242}
243
244#endif
245
Kent Overstreetcafe5632013-03-23 16:11:31 -0700246void bch_debug_exit(void)
247{
Chengguang Xudf2b9432018-03-18 17:36:23 -0700248 if (!IS_ERR_OR_NULL(bcache_debug))
249 debugfs_remove_recursive(bcache_debug);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700250}
251
252int __init bch_debug_init(struct kobject *kobj)
253{
Chengguang Xudf2b9432018-03-18 17:36:23 -0700254 bcache_debug = debugfs_create_dir("bcache", NULL);
Tang Junhui539d39e2018-01-08 12:21:22 -0800255
Chengguang Xudf2b9432018-03-18 17:36:23 -0700256 return IS_ERR_OR_NULL(bcache_debug);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700257}