Commit | Line | Data |
---|---|---|
cafe5635 KO |
1 | /* |
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" | |
dc9d98d6 | 11 | #include "extents.h" |
cafe5635 KO |
12 | |
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 | ||
19 | static struct dentry *debug; | |
20 | ||
280481d0 | 21 | #ifdef CONFIG_BCACHE_DEBUG |
cafe5635 | 22 | |
78b77bf8 KO |
23 | #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; \ | |
ee811287 KO |
27 | i = (void *) i + set_blocks(i, block_bytes(b->c)) * \ |
28 | block_bytes(b->c)) | |
78b77bf8 KO |
29 | |
30 | void bch_btree_verify(struct btree *b) | |
cafe5635 KO |
31 | { |
32 | struct btree *v = b->c->verify_data; | |
78b77bf8 KO |
33 | struct bset *ondisk, *sorted, *inmemory; |
34 | struct bio *bio; | |
cafe5635 | 35 | |
78b77bf8 | 36 | if (!b->c->verify || !b->c->verify_ondisk) |
cafe5635 KO |
37 | return; |
38 | ||
cb7a583e | 39 | down(&b->io_mutex); |
cafe5635 KO |
40 | mutex_lock(&b->c->verify_lock); |
41 | ||
78b77bf8 | 42 | ondisk = b->c->verify_ondisk; |
a85e968e KO |
43 | sorted = b->c->verify_data->keys.set->data; |
44 | inmemory = b->keys.set->data; | |
78b77bf8 | 45 | |
cafe5635 KO |
46 | bkey_copy(&v->key, &b->key); |
47 | v->written = 0; | |
48 | v->level = b->level; | |
a85e968e | 49 | v->keys.ops = b->keys.ops; |
cafe5635 | 50 | |
78b77bf8 KO |
51 | 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); | |
cafe5635 | 56 | |
78b77bf8 KO |
57 | 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); | |
a85e968e | 63 | sorted = v->keys.set->data; |
78b77bf8 KO |
64 | |
65 | if (inmemory->keys != sorted->keys || | |
66 | memcmp(inmemory->start, | |
67 | sorted->start, | |
fafff81c | 68 | (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) { |
78b77bf8 KO |
69 | struct bset *i; |
70 | unsigned j; | |
cafe5635 KO |
71 | |
72 | console_lock(); | |
73 | ||
78b77bf8 | 74 | printk(KERN_ERR "*** in memory:\n"); |
dc9d98d6 | 75 | bch_dump_bset(&b->keys, inmemory, 0); |
cafe5635 | 76 | |
78b77bf8 | 77 | printk(KERN_ERR "*** read back in:\n"); |
dc9d98d6 | 78 | bch_dump_bset(&v->keys, sorted, 0); |
cafe5635 | 79 | |
78b77bf8 KO |
80 | for_each_written_bset(b, ondisk, i) { |
81 | unsigned block = ((void *) i - (void *) ondisk) / | |
82 | block_bytes(b->c); | |
cafe5635 | 83 | |
78b77bf8 | 84 | printk(KERN_ERR "*** on disk block %u:\n", block); |
dc9d98d6 | 85 | bch_dump_bset(&b->keys, i, block); |
78b77bf8 KO |
86 | } |
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]) | |
cafe5635 KO |
93 | break; |
94 | ||
78b77bf8 KO |
95 | printk(KERN_ERR "b->written %u\n", b->written); |
96 | ||
cafe5635 KO |
97 | console_unlock(); |
98 | panic("verify failed at %u\n", j); | |
99 | } | |
100 | ||
101 | mutex_unlock(&b->c->verify_lock); | |
cb7a583e | 102 | up(&b->io_mutex); |
cafe5635 KO |
103 | } |
104 | ||
220bb38c | 105 | void bch_data_verify(struct cached_dev *dc, struct bio *bio) |
cafe5635 KO |
106 | { |
107 | char name[BDEVNAME_SIZE]; | |
cafe5635 | 108 | struct bio *check; |
7988613b KO |
109 | struct bio_vec bv, *bv2; |
110 | struct bvec_iter iter; | |
cafe5635 KO |
111 | int i; |
112 | ||
220bb38c | 113 | check = bio_clone(bio, GFP_NOIO); |
cafe5635 KO |
114 | if (!check) |
115 | return; | |
116 | ||
8e51e414 | 117 | if (bio_alloc_pages(check, GFP_NOIO)) |
cafe5635 KO |
118 | goto out_put; |
119 | ||
220bb38c | 120 | submit_bio_wait(READ_SYNC, check); |
cafe5635 | 121 | |
7988613b KO |
122 | 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); | |
cafe5635 | 125 | |
7988613b KO |
126 | cache_set_err_on(memcmp(p1 + bv.bv_offset, |
127 | p2 + bv.bv_offset, | |
128 | bv.bv_len), | |
5ceaaad7 KO |
129 | dc->disk.c, |
130 | "verify failed at dev %s sector %llu", | |
131 | bdevname(dc->bdev, name), | |
4f024f37 | 132 | (uint64_t) bio->bi_iter.bi_sector); |
5ceaaad7 | 133 | |
220bb38c | 134 | kunmap_atomic(p1); |
cafe5635 KO |
135 | } |
136 | ||
7988613b KO |
137 | bio_for_each_segment_all(bv2, check, i) |
138 | __free_page(bv2->bv_page); | |
cafe5635 KO |
139 | out_put: |
140 | bio_put(check); | |
141 | } | |
142 | ||
cafe5635 KO |
143 | #endif |
144 | ||
145 | #ifdef CONFIG_DEBUG_FS | |
146 | ||
147 | /* XXX: cache set refcounting */ | |
148 | ||
149 | struct dump_iterator { | |
150 | char buf[PAGE_SIZE]; | |
151 | size_t bytes; | |
152 | struct cache_set *c; | |
153 | struct keybuf keys; | |
154 | }; | |
155 | ||
156 | static bool dump_pred(struct keybuf *buf, struct bkey *k) | |
157 | { | |
158 | return true; | |
159 | } | |
160 | ||
161 | static 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; | |
85b1492e | 166 | char kbuf[80]; |
cafe5635 KO |
167 | |
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 | ||
72c27061 | 185 | w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred); |
cafe5635 KO |
186 | if (!w) |
187 | break; | |
188 | ||
dc9d98d6 | 189 | bch_extent_to_text(kbuf, sizeof(kbuf), &w->key); |
85b1492e | 190 | i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf); |
cafe5635 KO |
191 | bch_keybuf_del(&i->keys, w); |
192 | } | |
193 | ||
194 | return ret; | |
195 | } | |
196 | ||
197 | static 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; | |
72c27061 | 208 | bch_keybuf_init(&i->keys); |
cafe5635 KO |
209 | i->keys.last_scanned = KEY(0, 0, 0); |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
214 | static int bch_dump_release(struct inode *inode, struct file *file) | |
215 | { | |
216 | kfree(file->private_data); | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static 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 | ||
227 | void 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 | ||
cafe5635 KO |
240 | void bch_debug_exit(void) |
241 | { | |
242 | if (!IS_ERR_OR_NULL(debug)) | |
243 | debugfs_remove_recursive(debug); | |
244 | } | |
245 | ||
246 | int __init bch_debug_init(struct kobject *kobj) | |
247 | { | |
248 | int ret = 0; | |
cafe5635 KO |
249 | |
250 | debug = debugfs_create_dir("bcache", NULL); | |
251 | return ret; | |
252 | } |