debug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Assorted bcache debug code
  3. *
  4. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  5. * Copyright 2012 Google, Inc.
  6. */
  7. #include "bcache.h"
  8. #include "btree.h"
  9. #include "debug.h"
  10. #include "extents.h"
  11. #include <linux/console.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/module.h>
  14. #include <linux/random.h>
  15. #include <linux/seq_file.h>
  16. static struct dentry *debug;
  17. #ifdef CONFIG_BCACHE_DEBUG
  18. #define for_each_written_bset(b, start, i) \
  19. for (i = (start); \
  20. (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\
  21. i->seq == (start)->seq; \
  22. i = (void *) i + set_blocks(i, block_bytes(b->c)) * \
  23. block_bytes(b->c))
  24. void bch_btree_verify(struct btree *b)
  25. {
  26. struct btree *v = b->c->verify_data;
  27. struct bset *ondisk, *sorted, *inmemory;
  28. struct bio *bio;
  29. if (!b->c->verify || !b->c->verify_ondisk)
  30. return;
  31. down(&b->io_mutex);
  32. mutex_lock(&b->c->verify_lock);
  33. ondisk = b->c->verify_ondisk;
  34. sorted = b->c->verify_data->keys.set->data;
  35. inmemory = b->keys.set->data;
  36. bkey_copy(&v->key, &b->key);
  37. v->written = 0;
  38. v->level = b->level;
  39. v->keys.ops = b->keys.ops;
  40. bio = bch_bbio_alloc(b->c);
  41. bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev;
  42. bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
  43. bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9;
  44. bio->bi_opf = REQ_OP_READ | REQ_META;
  45. bch_bio_map(bio, sorted);
  46. submit_bio_wait(bio);
  47. bch_bbio_free(bio, b->c);
  48. memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
  49. bch_btree_node_read_done(v);
  50. sorted = v->keys.set->data;
  51. if (inmemory->keys != sorted->keys ||
  52. memcmp(inmemory->start,
  53. sorted->start,
  54. (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
  55. struct bset *i;
  56. unsigned j;
  57. console_lock();
  58. printk(KERN_ERR "*** in memory:\n");
  59. bch_dump_bset(&b->keys, inmemory, 0);
  60. printk(KERN_ERR "*** read back in:\n");
  61. bch_dump_bset(&v->keys, sorted, 0);
  62. for_each_written_bset(b, ondisk, i) {
  63. unsigned block = ((void *) i - (void *) ondisk) /
  64. block_bytes(b->c);
  65. printk(KERN_ERR "*** on disk block %u:\n", block);
  66. bch_dump_bset(&b->keys, i, block);
  67. }
  68. printk(KERN_ERR "*** block %zu not written\n",
  69. ((void *) i - (void *) ondisk) / block_bytes(b->c));
  70. for (j = 0; j < inmemory->keys; j++)
  71. if (inmemory->d[j] != sorted->d[j])
  72. break;
  73. printk(KERN_ERR "b->written %u\n", b->written);
  74. console_unlock();
  75. panic("verify failed at %u\n", j);
  76. }
  77. mutex_unlock(&b->c->verify_lock);
  78. up(&b->io_mutex);
  79. }
  80. void bch_data_verify(struct cached_dev *dc, struct bio *bio)
  81. {
  82. char name[BDEVNAME_SIZE];
  83. struct bio *check;
  84. struct bio_vec bv, cbv;
  85. struct bvec_iter iter, citer = { 0 };
  86. check = bio_clone(bio, GFP_NOIO);
  87. if (!check)
  88. return;
  89. check->bi_opf = REQ_OP_READ;
  90. if (bio_alloc_pages(check, GFP_NOIO))
  91. goto out_put;
  92. submit_bio_wait(check);
  93. citer.bi_size = UINT_MAX;
  94. bio_for_each_segment(bv, bio, iter) {
  95. void *p1 = kmap_atomic(bv.bv_page);
  96. void *p2;
  97. cbv = bio_iter_iovec(check, citer);
  98. p2 = page_address(cbv.bv_page);
  99. cache_set_err_on(memcmp(p1 + bv.bv_offset,
  100. p2 + bv.bv_offset,
  101. bv.bv_len),
  102. dc->disk.c,
  103. "verify failed at dev %s sector %llu",
  104. bdevname(dc->bdev, name),
  105. (uint64_t) bio->bi_iter.bi_sector);
  106. kunmap_atomic(p1);
  107. bio_advance_iter(check, &citer, bv.bv_len);
  108. }
  109. bio_free_pages(check);
  110. out_put:
  111. bio_put(check);
  112. }
  113. #endif
  114. #ifdef CONFIG_DEBUG_FS
  115. /* XXX: cache set refcounting */
  116. struct dump_iterator {
  117. char buf[PAGE_SIZE];
  118. size_t bytes;
  119. struct cache_set *c;
  120. struct keybuf keys;
  121. };
  122. static bool dump_pred(struct keybuf *buf, struct bkey *k)
  123. {
  124. return true;
  125. }
  126. static ssize_t bch_dump_read(struct file *file, char __user *buf,
  127. size_t size, loff_t *ppos)
  128. {
  129. struct dump_iterator *i = file->private_data;
  130. ssize_t ret = 0;
  131. char kbuf[80];
  132. while (size) {
  133. struct keybuf_key *w;
  134. unsigned bytes = min(i->bytes, size);
  135. int err = copy_to_user(buf, i->buf, bytes);
  136. if (err)
  137. return err;
  138. ret += bytes;
  139. buf += bytes;
  140. size -= bytes;
  141. i->bytes -= bytes;
  142. memmove(i->buf, i->buf + bytes, i->bytes);
  143. if (i->bytes)
  144. break;
  145. w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
  146. if (!w)
  147. break;
  148. bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
  149. i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
  150. bch_keybuf_del(&i->keys, w);
  151. }
  152. return ret;
  153. }
  154. static int bch_dump_open(struct inode *inode, struct file *file)
  155. {
  156. struct cache_set *c = inode->i_private;
  157. struct dump_iterator *i;
  158. i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
  159. if (!i)
  160. return -ENOMEM;
  161. file->private_data = i;
  162. i->c = c;
  163. bch_keybuf_init(&i->keys);
  164. i->keys.last_scanned = KEY(0, 0, 0);
  165. return 0;
  166. }
  167. static int bch_dump_release(struct inode *inode, struct file *file)
  168. {
  169. kfree(file->private_data);
  170. return 0;
  171. }
  172. static const struct file_operations cache_set_debug_ops = {
  173. .owner = THIS_MODULE,
  174. .open = bch_dump_open,
  175. .read = bch_dump_read,
  176. .release = bch_dump_release
  177. };
  178. void bch_debug_init_cache_set(struct cache_set *c)
  179. {
  180. if (!IS_ERR_OR_NULL(debug)) {
  181. char name[50];
  182. snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
  183. c->debug = debugfs_create_file(name, 0400, debug, c,
  184. &cache_set_debug_ops);
  185. }
  186. }
  187. #endif
  188. void bch_debug_exit(void)
  189. {
  190. if (!IS_ERR_OR_NULL(debug))
  191. debugfs_remove_recursive(debug);
  192. }
  193. int __init bch_debug_init(struct kobject *kobj)
  194. {
  195. int ret = 0;
  196. debug = debugfs_create_dir("bcache", NULL);
  197. return ret;
  198. }