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