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