blob: d030ce3025a6a6f365cb55c740e436ba43fd52bf [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{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700109 struct bio *check;
Ming Lei4113b882016-11-11 20:05:33 +0800110 struct bio_vec bv, cbv;
111 struct bvec_iter iter, citer = { 0 };
Kent Overstreetcafe5632013-03-23 16:11:31 -0700112
NeilBrown5a136fd2017-06-18 14:38:59 +1000113 check = bio_clone_kmalloc(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114 if (!check)
115 return;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600116 check->bi_opf = REQ_OP_READ;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700117
Ming Lei25d8be72017-12-18 20:22:10 +0800118 if (bch_bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700119 goto out_put;
120
Mike Christie4e49ea42016-06-05 14:31:41 -0500121 submit_bio_wait(check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700122
Ming Lei4113b882016-11-11 20:05:33 +0800123 citer.bi_size = UINT_MAX;
Kent Overstreet79886132013-11-23 17:19:00 -0800124 bio_for_each_segment(bv, bio, iter) {
125 void *p1 = kmap_atomic(bv.bv_page);
Ming Lei4113b882016-11-11 20:05:33 +0800126 void *p2;
127
128 cbv = bio_iter_iovec(check, citer);
129 p2 = page_address(cbv.bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700130
Kent Overstreet79886132013-11-23 17:19:00 -0800131 cache_set_err_on(memcmp(p1 + bv.bv_offset,
132 p2 + bv.bv_offset,
133 bv.bv_len),
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700134 dc->disk.c,
135 "verify failed at dev %s sector %llu",
Coly Li6e916a72018-05-03 18:51:32 +0800136 dc->backing_dev_name,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700137 (uint64_t) bio->bi_iter.bi_sector);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700138
Kent Overstreet220bb382013-09-10 19:02:45 -0700139 kunmap_atomic(p1);
Ming Lei4113b882016-11-11 20:05:33 +0800140 bio_advance_iter(check, &citer, bv.bv_len);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141 }
142
Guoqing Jiang491221f2016-09-22 03:10:01 -0400143 bio_free_pages(check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700144out_put:
145 bio_put(check);
146}
147
Kent Overstreetcafe5632013-03-23 16:11:31 -0700148#endif
149
150#ifdef CONFIG_DEBUG_FS
151
152/* XXX: cache set refcounting */
153
154struct dump_iterator {
155 char buf[PAGE_SIZE];
156 size_t bytes;
157 struct cache_set *c;
158 struct keybuf keys;
159};
160
161static bool dump_pred(struct keybuf *buf, struct bkey *k)
162{
163 return true;
164}
165
166static 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 Overstreet85b14922013-05-14 20:33:16 -0700171 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172
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 Overstreet72c27062013-06-05 06:24:39 -0700190 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191 if (!w)
192 break;
193
Kent Overstreetdc9d98d2013-12-17 23:47:33 -0800194 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
Kent Overstreet85b14922013-05-14 20:33:16 -0700195 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196 bch_keybuf_del(&i->keys, w);
197 }
198
199 return ret;
200}
201
202static 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 Overstreet72c27062013-06-05 06:24:39 -0700213 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214 i->keys.last_scanned = KEY(0, 0, 0);
215
216 return 0;
217}
218
219static int bch_dump_release(struct inode *inode, struct file *file)
220{
221 kfree(file->private_data);
222 return 0;
223}
224
225static 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
232void bch_debug_init_cache_set(struct cache_set *c)
233{
Chengguang Xudf2b9432018-03-18 17:36:23 -0700234 if (!IS_ERR_OR_NULL(bcache_debug)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700235 char name[50];
236 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
237
Chengguang Xudf2b9432018-03-18 17:36:23 -0700238 c->debug = debugfs_create_file(name, 0400, bcache_debug, c,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239 &cache_set_debug_ops);
240 }
241}
242
243#endif
244
Kent Overstreetcafe5632013-03-23 16:11:31 -0700245void bch_debug_exit(void)
246{
Chengguang Xudf2b9432018-03-18 17:36:23 -0700247 if (!IS_ERR_OR_NULL(bcache_debug))
248 debugfs_remove_recursive(bcache_debug);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700249}
250
251int __init bch_debug_init(struct kobject *kobj)
252{
Coly Li1c1a2ee2018-05-17 23:33:26 +0800253 if (!IS_ENABLED(CONFIG_DEBUG_FS))
254 return 0;
Tang Junhui539d39e2018-01-08 12:21:22 -0800255
Coly Li1c1a2ee2018-05-17 23:33:26 +0800256 bcache_debug = debugfs_create_dir("bcache", NULL);
Chengguang Xudf2b9432018-03-18 17:36:23 -0700257 return IS_ERR_OR_NULL(bcache_debug);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700258}