blob: 8b1f1d5c18198f078dea917f40303c98fce1ee94 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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 Overstreetdc9d98d2013-12-17 23:47:33 -080011#include "extents.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070012
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
19static struct dentry *debug;
20
Kent Overstreet280481d2013-10-24 16:36:03 -070021#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetcafe5632013-03-23 16:11:31 -070022
Kent Overstreet78b77bf2013-12-17 22:49:08 -080023#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 Overstreetee811282013-12-17 23:49:49 -080027 i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
28 block_bytes(b->c))
Kent Overstreet78b77bf2013-12-17 22:49:08 -080029
30void bch_btree_verify(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -070031{
32 struct btree *v = b->c->verify_data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080033 struct bset *ondisk, *sorted, *inmemory;
34 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -070035
Kent Overstreet78b77bf2013-12-17 22:49:08 -080036 if (!b->c->verify || !b->c->verify_ondisk)
Kent Overstreetcafe5632013-03-23 16:11:31 -070037 return;
38
Kent Overstreetcb7a5832013-12-16 15:27:25 -080039 down(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -070040 mutex_lock(&b->c->verify_lock);
41
Kent Overstreet78b77bf2013-12-17 22:49:08 -080042 ondisk = b->c->verify_ondisk;
Kent Overstreeta85e9682013-12-20 17:28:16 -080043 sorted = b->c->verify_data->keys.set->data;
44 inmemory = b->keys.set->data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080045
Kent Overstreetcafe5632013-03-23 16:11:31 -070046 bkey_copy(&v->key, &b->key);
47 v->written = 0;
48 v->level = b->level;
Kent Overstreeta85e9682013-12-20 17:28:16 -080049 v->keys.ops = b->keys.ops;
Kent Overstreetcafe5632013-03-23 16:11:31 -070050
Kent Overstreet78b77bf2013-12-17 22:49:08 -080051 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;
55 bch_bio_map(bio, sorted);
Kent Overstreetcafe5632013-03-23 16:11:31 -070056
Kent Overstreet78b77bf2013-12-17 22:49:08 -080057 submit_bio_wait(REQ_META|READ_SYNC, bio);
58 bch_bbio_free(bio, b->c);
59
60 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
61
62 bch_btree_node_read_done(v);
Kent Overstreeta85e9682013-12-20 17:28:16 -080063 sorted = v->keys.set->data;
Kent Overstreet78b77bf2013-12-17 22:49:08 -080064
65 if (inmemory->keys != sorted->keys ||
66 memcmp(inmemory->start,
67 sorted->start,
Kent Overstreetfafff812013-12-17 21:56:21 -080068 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
Kent Overstreet78b77bf2013-12-17 22:49:08 -080069 struct bset *i;
70 unsigned j;
Kent Overstreetcafe5632013-03-23 16:11:31 -070071
72 console_lock();
73
Kent Overstreet78b77bf2013-12-17 22:49:08 -080074 printk(KERN_ERR "*** in memory:\n");
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080075 bch_dump_bset(&b->keys, inmemory, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -070076
Kent Overstreet78b77bf2013-12-17 22:49:08 -080077 printk(KERN_ERR "*** read back in:\n");
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080078 bch_dump_bset(&v->keys, sorted, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -070079
Kent Overstreet78b77bf2013-12-17 22:49:08 -080080 for_each_written_bset(b, ondisk, i) {
81 unsigned block = ((void *) i - (void *) ondisk) /
82 block_bytes(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -070083
Kent Overstreet78b77bf2013-12-17 22:49:08 -080084 printk(KERN_ERR "*** on disk block %u:\n", block);
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080085 bch_dump_bset(&b->keys, i, block);
Kent Overstreet78b77bf2013-12-17 22:49:08 -080086 }
87
88 printk(KERN_ERR "*** block %zu not written\n",
89 ((void *) i - (void *) ondisk) / block_bytes(b->c));
90
91 for (j = 0; j < inmemory->keys; j++)
92 if (inmemory->d[j] != sorted->d[j])
Kent Overstreetcafe5632013-03-23 16:11:31 -070093 break;
94
Kent Overstreet78b77bf2013-12-17 22:49:08 -080095 printk(KERN_ERR "b->written %u\n", b->written);
96
Kent Overstreetcafe5632013-03-23 16:11:31 -070097 console_unlock();
98 panic("verify failed at %u\n", j);
99 }
100
101 mutex_unlock(&b->c->verify_lock);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800102 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700103}
104
Kent Overstreet220bb382013-09-10 19:02:45 -0700105void bch_data_verify(struct cached_dev *dc, struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700106{
107 char name[BDEVNAME_SIZE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108 struct bio *check;
Kent Overstreet79886132013-11-23 17:19:00 -0800109 struct bio_vec bv, *bv2;
110 struct bvec_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700111 int i;
112
Kent Overstreet220bb382013-09-10 19:02:45 -0700113 check = bio_clone(bio, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114 if (!check)
115 return;
116
Kent Overstreet8e51e412013-06-06 18:15:57 -0700117 if (bio_alloc_pages(check, GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700118 goto out_put;
119
Kent Overstreet220bb382013-09-10 19:02:45 -0700120 submit_bio_wait(READ_SYNC, check);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121
Kent Overstreet79886132013-11-23 17:19:00 -0800122 bio_for_each_segment(bv, bio, iter) {
123 void *p1 = kmap_atomic(bv.bv_page);
124 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125
Kent Overstreet79886132013-11-23 17:19:00 -0800126 cache_set_err_on(memcmp(p1 + bv.bv_offset,
127 p2 + bv.bv_offset,
128 bv.bv_len),
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700129 dc->disk.c,
130 "verify failed at dev %s sector %llu",
131 bdevname(dc->bdev, name),
Kent Overstreet4f024f32013-10-11 15:44:27 -0700132 (uint64_t) bio->bi_iter.bi_sector);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700133
Kent Overstreet220bb382013-09-10 19:02:45 -0700134 kunmap_atomic(p1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135 }
136
Kent Overstreet79886132013-11-23 17:19:00 -0800137 bio_for_each_segment_all(bv2, check, i)
138 __free_page(bv2->bv_page);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139out_put:
140 bio_put(check);
141}
142
Kent Overstreetcafe5632013-03-23 16:11:31 -0700143#endif
144
145#ifdef CONFIG_DEBUG_FS
146
147/* XXX: cache set refcounting */
148
149struct dump_iterator {
150 char buf[PAGE_SIZE];
151 size_t bytes;
152 struct cache_set *c;
153 struct keybuf keys;
154};
155
156static bool dump_pred(struct keybuf *buf, struct bkey *k)
157{
158 return true;
159}
160
161static ssize_t bch_dump_read(struct file *file, char __user *buf,
162 size_t size, loff_t *ppos)
163{
164 struct dump_iterator *i = file->private_data;
165 ssize_t ret = 0;
Kent Overstreet85b14922013-05-14 20:33:16 -0700166 char kbuf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167
168 while (size) {
169 struct keybuf_key *w;
170 unsigned bytes = min(i->bytes, size);
171
172 int err = copy_to_user(buf, i->buf, bytes);
173 if (err)
174 return err;
175
176 ret += bytes;
177 buf += bytes;
178 size -= bytes;
179 i->bytes -= bytes;
180 memmove(i->buf, i->buf + bytes, i->bytes);
181
182 if (i->bytes)
183 break;
184
Kent Overstreet72c27062013-06-05 06:24:39 -0700185 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700186 if (!w)
187 break;
188
Kent Overstreetdc9d98d2013-12-17 23:47:33 -0800189 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
Kent Overstreet85b14922013-05-14 20:33:16 -0700190 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191 bch_keybuf_del(&i->keys, w);
192 }
193
194 return ret;
195}
196
197static int bch_dump_open(struct inode *inode, struct file *file)
198{
199 struct cache_set *c = inode->i_private;
200 struct dump_iterator *i;
201
202 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
203 if (!i)
204 return -ENOMEM;
205
206 file->private_data = i;
207 i->c = c;
Kent Overstreet72c27062013-06-05 06:24:39 -0700208 bch_keybuf_init(&i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209 i->keys.last_scanned = KEY(0, 0, 0);
210
211 return 0;
212}
213
214static int bch_dump_release(struct inode *inode, struct file *file)
215{
216 kfree(file->private_data);
217 return 0;
218}
219
220static const struct file_operations cache_set_debug_ops = {
221 .owner = THIS_MODULE,
222 .open = bch_dump_open,
223 .read = bch_dump_read,
224 .release = bch_dump_release
225};
226
227void bch_debug_init_cache_set(struct cache_set *c)
228{
229 if (!IS_ERR_OR_NULL(debug)) {
230 char name[50];
231 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
232
233 c->debug = debugfs_create_file(name, 0400, debug, c,
234 &cache_set_debug_ops);
235 }
236}
237
238#endif
239
Kent Overstreetcafe5632013-03-23 16:11:31 -0700240void bch_debug_exit(void)
241{
242 if (!IS_ERR_OR_NULL(debug))
243 debugfs_remove_recursive(debug);
244}
245
246int __init bch_debug_init(struct kobject *kobj)
247{
248 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700249
250 debug = debugfs_create_dir("bcache", NULL);
251 return ret;
252}