net: s2io: simplify logical constraint
[deliverable/linux.git] / drivers / md / bcache / debug.c
CommitLineData
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
19static 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
30void 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;
ad0d9e76 55 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META|READ_SYNC);
78b77bf8 56 bch_bio_map(bio, sorted);
cafe5635 57
4e49ea4a 58 submit_bio_wait(bio);
78b77bf8
KO
59 bch_bbio_free(bio, b->c);
60
61 memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
62
63 bch_btree_node_read_done(v);
a85e968e 64 sorted = v->keys.set->data;
78b77bf8
KO
65
66 if (inmemory->keys != sorted->keys ||
67 memcmp(inmemory->start,
68 sorted->start,
fafff81c 69 (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
78b77bf8
KO
70 struct bset *i;
71 unsigned j;
cafe5635
KO
72
73 console_lock();
74
78b77bf8 75 printk(KERN_ERR "*** in memory:\n");
dc9d98d6 76 bch_dump_bset(&b->keys, inmemory, 0);
cafe5635 77
78b77bf8 78 printk(KERN_ERR "*** read back in:\n");
dc9d98d6 79 bch_dump_bset(&v->keys, sorted, 0);
cafe5635 80
78b77bf8
KO
81 for_each_written_bset(b, ondisk, i) {
82 unsigned block = ((void *) i - (void *) ondisk) /
83 block_bytes(b->c);
cafe5635 84
78b77bf8 85 printk(KERN_ERR "*** on disk block %u:\n", block);
dc9d98d6 86 bch_dump_bset(&b->keys, i, block);
78b77bf8
KO
87 }
88
89 printk(KERN_ERR "*** block %zu not written\n",
90 ((void *) i - (void *) ondisk) / block_bytes(b->c));
91
92 for (j = 0; j < inmemory->keys; j++)
93 if (inmemory->d[j] != sorted->d[j])
cafe5635
KO
94 break;
95
78b77bf8
KO
96 printk(KERN_ERR "b->written %u\n", b->written);
97
cafe5635
KO
98 console_unlock();
99 panic("verify failed at %u\n", j);
100 }
101
102 mutex_unlock(&b->c->verify_lock);
cb7a583e 103 up(&b->io_mutex);
cafe5635
KO
104}
105
220bb38c 106void bch_data_verify(struct cached_dev *dc, struct bio *bio)
cafe5635
KO
107{
108 char name[BDEVNAME_SIZE];
cafe5635 109 struct bio *check;
7988613b
KO
110 struct bio_vec bv, *bv2;
111 struct bvec_iter iter;
cafe5635
KO
112 int i;
113
220bb38c 114 check = bio_clone(bio, GFP_NOIO);
cafe5635
KO
115 if (!check)
116 return;
ad0d9e76 117 bio_set_op_attrs(check, REQ_OP_READ, READ_SYNC);
cafe5635 118
8e51e414 119 if (bio_alloc_pages(check, GFP_NOIO))
cafe5635
KO
120 goto out_put;
121
4e49ea4a 122 submit_bio_wait(check);
cafe5635 123
7988613b
KO
124 bio_for_each_segment(bv, bio, iter) {
125 void *p1 = kmap_atomic(bv.bv_page);
126 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
cafe5635 127
7988613b
KO
128 cache_set_err_on(memcmp(p1 + bv.bv_offset,
129 p2 + bv.bv_offset,
130 bv.bv_len),
5ceaaad7
KO
131 dc->disk.c,
132 "verify failed at dev %s sector %llu",
133 bdevname(dc->bdev, name),
4f024f37 134 (uint64_t) bio->bi_iter.bi_sector);
5ceaaad7 135
220bb38c 136 kunmap_atomic(p1);
cafe5635
KO
137 }
138
7988613b
KO
139 bio_for_each_segment_all(bv2, check, i)
140 __free_page(bv2->bv_page);
cafe5635
KO
141out_put:
142 bio_put(check);
143}
144
cafe5635
KO
145#endif
146
147#ifdef CONFIG_DEBUG_FS
148
149/* XXX: cache set refcounting */
150
151struct dump_iterator {
152 char buf[PAGE_SIZE];
153 size_t bytes;
154 struct cache_set *c;
155 struct keybuf keys;
156};
157
158static bool dump_pred(struct keybuf *buf, struct bkey *k)
159{
160 return true;
161}
162
163static ssize_t bch_dump_read(struct file *file, char __user *buf,
164 size_t size, loff_t *ppos)
165{
166 struct dump_iterator *i = file->private_data;
167 ssize_t ret = 0;
85b1492e 168 char kbuf[80];
cafe5635
KO
169
170 while (size) {
171 struct keybuf_key *w;
172 unsigned bytes = min(i->bytes, size);
173
174 int err = copy_to_user(buf, i->buf, bytes);
175 if (err)
176 return err;
177
178 ret += bytes;
179 buf += bytes;
180 size -= bytes;
181 i->bytes -= bytes;
182 memmove(i->buf, i->buf + bytes, i->bytes);
183
184 if (i->bytes)
185 break;
186
72c27061 187 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
cafe5635
KO
188 if (!w)
189 break;
190
dc9d98d6 191 bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
85b1492e 192 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
cafe5635
KO
193 bch_keybuf_del(&i->keys, w);
194 }
195
196 return ret;
197}
198
199static int bch_dump_open(struct inode *inode, struct file *file)
200{
201 struct cache_set *c = inode->i_private;
202 struct dump_iterator *i;
203
204 i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
205 if (!i)
206 return -ENOMEM;
207
208 file->private_data = i;
209 i->c = c;
72c27061 210 bch_keybuf_init(&i->keys);
cafe5635
KO
211 i->keys.last_scanned = KEY(0, 0, 0);
212
213 return 0;
214}
215
216static int bch_dump_release(struct inode *inode, struct file *file)
217{
218 kfree(file->private_data);
219 return 0;
220}
221
222static const struct file_operations cache_set_debug_ops = {
223 .owner = THIS_MODULE,
224 .open = bch_dump_open,
225 .read = bch_dump_read,
226 .release = bch_dump_release
227};
228
229void bch_debug_init_cache_set(struct cache_set *c)
230{
231 if (!IS_ERR_OR_NULL(debug)) {
232 char name[50];
233 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
234
235 c->debug = debugfs_create_file(name, 0400, debug, c,
236 &cache_set_debug_ops);
237 }
238}
239
240#endif
241
cafe5635
KO
242void bch_debug_exit(void)
243{
244 if (!IS_ERR_OR_NULL(debug))
245 debugfs_remove_recursive(debug);
246}
247
248int __init bch_debug_init(struct kobject *kobj)
249{
250 int ret = 0;
cafe5635
KO
251
252 debug = debugfs_create_dir("bcache", NULL);
253 return ret;
254}
This page took 0.267804 seconds and 5 git commands to generate.