bcache: Rename/shuffle various code around
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / md / bcache / debug.c
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"
11
12 #include <linux/console.h>
13 #include <linux/debugfs.h>
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/seq_file.h>
17
18 static struct dentry *debug;
19
20 const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
21 {
22         unsigned i;
23
24         for (i = 0; i < KEY_PTRS(k); i++)
25                 if (ptr_available(c, k, i)) {
26                         struct cache *ca = PTR_CACHE(c, k, i);
27                         size_t bucket = PTR_BUCKET_NR(c, k, i);
28                         size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
29
30                         if (KEY_SIZE(k) + r > c->sb.bucket_size)
31                                 return "bad, length too big";
32                         if (bucket <  ca->sb.first_bucket)
33                                 return "bad, short offset";
34                         if (bucket >= ca->sb.nbuckets)
35                                 return "bad, offset past end of device";
36                         if (ptr_stale(c, k, i))
37                                 return "stale";
38                 }
39
40         if (!bkey_cmp(k, &ZERO_KEY))
41                 return "bad, null key";
42         if (!KEY_PTRS(k))
43                 return "bad, no pointers";
44         if (!KEY_SIZE(k))
45                 return "zeroed key";
46         return "";
47 }
48
49 int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
50 {
51         unsigned i = 0;
52         char *out = buf, *end = buf + size;
53
54 #define p(...)  (out += scnprintf(out, end - out, __VA_ARGS__))
55
56         p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
57
58         for (i = 0; i < KEY_PTRS(k); i++) {
59                 if (i)
60                         p(", ");
61
62                 if (PTR_DEV(k, i) == PTR_CHECK_DEV)
63                         p("check dev");
64                 else
65                         p("%llu:%llu gen %llu", PTR_DEV(k, i),
66                           PTR_OFFSET(k, i), PTR_GEN(k, i));
67         }
68
69         p("]");
70
71         if (KEY_DIRTY(k))
72                 p(" dirty");
73         if (KEY_CSUM(k))
74                 p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
75 #undef p
76         return out - buf;
77 }
78
79 #ifdef CONFIG_BCACHE_DEBUG
80
81 static void dump_bset(struct btree *b, struct bset *i, unsigned set)
82 {
83         struct bkey *k, *next;
84         unsigned j;
85         char buf[80];
86
87         for (k = i->start; k < bset_bkey_last(i); k = next) {
88                 next = bkey_next(k);
89
90                 bch_bkey_to_text(buf, sizeof(buf), k);
91                 printk(KERN_ERR "b %u k %zi/%u: %s", set,
92                        (uint64_t *) k - i->d, i->keys, buf);
93
94                 for (j = 0; j < KEY_PTRS(k); j++) {
95                         size_t n = PTR_BUCKET_NR(b->c, k, j);
96                         printk(" bucket %zu", n);
97
98                         if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
99                                 printk(" prio %i",
100                                        PTR_BUCKET(b->c, k, j)->prio);
101                 }
102
103                 printk(" %s\n", bch_ptr_status(b->c, k));
104
105                 if (next < bset_bkey_last(i) &&
106                     bkey_cmp(k, !b->level ? &START_KEY(next) : next) > 0)
107                         printk(KERN_ERR "Key skipped backwards\n");
108         }
109 }
110
111 static void bch_dump_bucket(struct btree *b)
112 {
113         unsigned i;
114
115         console_lock();
116         for (i = 0; i <= b->nsets; i++)
117                 dump_bset(b, b->sets[i].data,
118                           bset_block_offset(b, b->sets[i].data));
119         console_unlock();
120 }
121
122 #define for_each_written_bset(b, start, i)                              \
123         for (i = (start);                                               \
124              (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\
125              i->seq == (start)->seq;                                    \
126              i = (void *) i + set_blocks(i, block_bytes(b->c)) *        \
127                  block_bytes(b->c))
128
129 void bch_btree_verify(struct btree *b)
130 {
131         struct btree *v = b->c->verify_data;
132         struct bset *ondisk, *sorted, *inmemory;
133         struct bio *bio;
134
135         if (!b->c->verify || !b->c->verify_ondisk)
136                 return;
137
138         down(&b->io_mutex);
139         mutex_lock(&b->c->verify_lock);
140
141         ondisk = b->c->verify_ondisk;
142         sorted = b->c->verify_data->sets->data;
143         inmemory = b->sets->data;
144
145         bkey_copy(&v->key, &b->key);
146         v->written = 0;
147         v->level = b->level;
148
149         bio = bch_bbio_alloc(b->c);
150         bio->bi_bdev            = PTR_CACHE(b->c, &b->key, 0)->bdev;
151         bio->bi_iter.bi_sector  = PTR_OFFSET(&b->key, 0);
152         bio->bi_iter.bi_size    = KEY_SIZE(&v->key) << 9;
153         bch_bio_map(bio, sorted);
154
155         submit_bio_wait(REQ_META|READ_SYNC, bio);
156         bch_bbio_free(bio, b->c);
157
158         memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
159
160         bch_btree_node_read_done(v);
161         sorted = v->sets->data;
162
163         if (inmemory->keys != sorted->keys ||
164             memcmp(inmemory->start,
165                    sorted->start,
166                    (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
167                 struct bset *i;
168                 unsigned j;
169
170                 console_lock();
171
172                 printk(KERN_ERR "*** in memory:\n");
173                 dump_bset(b, inmemory, 0);
174
175                 printk(KERN_ERR "*** read back in:\n");
176                 dump_bset(v, sorted, 0);
177
178                 for_each_written_bset(b, ondisk, i) {
179                         unsigned block = ((void *) i - (void *) ondisk) /
180                                 block_bytes(b->c);
181
182                         printk(KERN_ERR "*** on disk block %u:\n", block);
183                         dump_bset(b, i, block);
184                 }
185
186                 printk(KERN_ERR "*** block %zu not written\n",
187                        ((void *) i - (void *) ondisk) / block_bytes(b->c));
188
189                 for (j = 0; j < inmemory->keys; j++)
190                         if (inmemory->d[j] != sorted->d[j])
191                                 break;
192
193                 printk(KERN_ERR "b->written %u\n", b->written);
194
195                 console_unlock();
196                 panic("verify failed at %u\n", j);
197         }
198
199         mutex_unlock(&b->c->verify_lock);
200         up(&b->io_mutex);
201 }
202
203 void bch_data_verify(struct cached_dev *dc, struct bio *bio)
204 {
205         char name[BDEVNAME_SIZE];
206         struct bio *check;
207         struct bio_vec bv, *bv2;
208         struct bvec_iter iter;
209         int i;
210
211         check = bio_clone(bio, GFP_NOIO);
212         if (!check)
213                 return;
214
215         if (bio_alloc_pages(check, GFP_NOIO))
216                 goto out_put;
217
218         submit_bio_wait(READ_SYNC, check);
219
220         bio_for_each_segment(bv, bio, iter) {
221                 void *p1 = kmap_atomic(bv.bv_page);
222                 void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
223
224                 cache_set_err_on(memcmp(p1 + bv.bv_offset,
225                                         p2 + bv.bv_offset,
226                                         bv.bv_len),
227                                  dc->disk.c,
228                                  "verify failed at dev %s sector %llu",
229                                  bdevname(dc->bdev, name),
230                                  (uint64_t) bio->bi_iter.bi_sector);
231
232                 kunmap_atomic(p1);
233         }
234
235         bio_for_each_segment_all(bv2, check, i)
236                 __free_page(bv2->bv_page);
237 out_put:
238         bio_put(check);
239 }
240
241 int __bch_count_data(struct btree *b)
242 {
243         unsigned ret = 0;
244         struct btree_iter iter;
245         struct bkey *k;
246
247         if (!b->level)
248                 for_each_key(b, k, &iter)
249                         ret += KEY_SIZE(k);
250         return ret;
251 }
252
253 void __bch_check_keys(struct btree *b, const char *fmt, ...)
254 {
255         va_list args;
256         struct bkey *k, *p = NULL;
257         struct btree_iter iter;
258         const char *err;
259
260         for_each_key(b, k, &iter) {
261                 if (!b->level) {
262                         err = "Keys out of order";
263                         if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
264                                 goto bug;
265
266                         if (bch_ptr_invalid(b, k))
267                                 continue;
268
269                         err =  "Overlapping keys";
270                         if (p && bkey_cmp(p, &START_KEY(k)) > 0)
271                                 goto bug;
272                 } else {
273                         if (bch_ptr_bad(b, k))
274                                 continue;
275
276                         err = "Duplicate keys";
277                         if (p && !bkey_cmp(p, k))
278                                 goto bug;
279                 }
280                 p = k;
281         }
282
283         err = "Key larger than btree node key";
284         if (p && bkey_cmp(p, &b->key) > 0)
285                 goto bug;
286
287         return;
288 bug:
289         bch_dump_bucket(b);
290
291         va_start(args, fmt);
292         vprintk(fmt, args);
293         va_end(args);
294
295         panic("bcache error: %s:\n", err);
296 }
297
298 void bch_btree_iter_next_check(struct btree_iter *iter)
299 {
300         struct bkey *k = iter->data->k, *next = bkey_next(k);
301
302         if (next < iter->data->end &&
303             bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) {
304                 bch_dump_bucket(iter->b);
305                 panic("Key skipped backwards\n");
306         }
307 }
308
309 #endif
310
311 #ifdef CONFIG_DEBUG_FS
312
313 /* XXX: cache set refcounting */
314
315 struct dump_iterator {
316         char                    buf[PAGE_SIZE];
317         size_t                  bytes;
318         struct cache_set        *c;
319         struct keybuf           keys;
320 };
321
322 static bool dump_pred(struct keybuf *buf, struct bkey *k)
323 {
324         return true;
325 }
326
327 static ssize_t bch_dump_read(struct file *file, char __user *buf,
328                              size_t size, loff_t *ppos)
329 {
330         struct dump_iterator *i = file->private_data;
331         ssize_t ret = 0;
332         char kbuf[80];
333
334         while (size) {
335                 struct keybuf_key *w;
336                 unsigned bytes = min(i->bytes, size);
337
338                 int err = copy_to_user(buf, i->buf, bytes);
339                 if (err)
340                         return err;
341
342                 ret      += bytes;
343                 buf      += bytes;
344                 size     -= bytes;
345                 i->bytes -= bytes;
346                 memmove(i->buf, i->buf + bytes, i->bytes);
347
348                 if (i->bytes)
349                         break;
350
351                 w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
352                 if (!w)
353                         break;
354
355                 bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
356                 i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
357                 bch_keybuf_del(&i->keys, w);
358         }
359
360         return ret;
361 }
362
363 static int bch_dump_open(struct inode *inode, struct file *file)
364 {
365         struct cache_set *c = inode->i_private;
366         struct dump_iterator *i;
367
368         i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
369         if (!i)
370                 return -ENOMEM;
371
372         file->private_data = i;
373         i->c = c;
374         bch_keybuf_init(&i->keys);
375         i->keys.last_scanned = KEY(0, 0, 0);
376
377         return 0;
378 }
379
380 static int bch_dump_release(struct inode *inode, struct file *file)
381 {
382         kfree(file->private_data);
383         return 0;
384 }
385
386 static const struct file_operations cache_set_debug_ops = {
387         .owner          = THIS_MODULE,
388         .open           = bch_dump_open,
389         .read           = bch_dump_read,
390         .release        = bch_dump_release
391 };
392
393 void bch_debug_init_cache_set(struct cache_set *c)
394 {
395         if (!IS_ERR_OR_NULL(debug)) {
396                 char name[50];
397                 snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
398
399                 c->debug = debugfs_create_file(name, 0400, debug, c,
400                                                &cache_set_debug_ops);
401         }
402 }
403
404 #endif
405
406 void bch_debug_exit(void)
407 {
408         if (!IS_ERR_OR_NULL(debug))
409                 debugfs_remove_recursive(debug);
410 }
411
412 int __init bch_debug_init(struct kobject *kobj)
413 {
414         int ret = 0;
415
416         debug = debugfs_create_dir("bcache", NULL);
417         return ret;
418 }