debug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_set_op_attrs(bio, REQ_OP_READ, REQ_META|READ_SYNC);
  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, *bv2;
  85. struct bvec_iter iter;
  86. int i;
  87. check = bio_clone(bio, GFP_NOIO);
  88. if (!check)
  89. return;
  90. bio_set_op_attrs(check, REQ_OP_READ, READ_SYNC);
  91. if (bio_alloc_pages(check, GFP_NOIO))
  92. goto out_put;
  93. submit_bio_wait(check);
  94. bio_for_each_segment(bv, bio, iter) {
  95. void *p1 = kmap_atomic(bv.bv_page);
  96. void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
  97. cache_set_err_on(memcmp(p1 + bv.bv_offset,
  98. p2 + bv.bv_offset,
  99. bv.bv_len),
  100. dc->disk.c,
  101. "verify failed at dev %s sector %llu",
  102. bdevname(dc->bdev, name),
  103. (uint64_t) bio->bi_iter.bi_sector);
  104. kunmap_atomic(p1);
  105. }
  106. bio_for_each_segment_all(bv2, check, i)
  107. __free_page(bv2->bv_page);
  108. out_put:
  109. bio_put(check);
  110. }
  111. #endif
  112. #ifdef CONFIG_DEBUG_FS
  113. /* XXX: cache set refcounting */
  114. struct dump_iterator {
  115. char buf[PAGE_SIZE];
  116. size_t bytes;
  117. struct cache_set *c;
  118. struct keybuf keys;
  119. };
  120. static bool dump_pred(struct keybuf *buf, struct bkey *k)
  121. {
  122. return true;
  123. }
  124. static ssize_t bch_dump_read(struct file *file, char __user *buf,
  125. size_t size, loff_t *ppos)
  126. {
  127. struct dump_iterator *i = file->private_data;
  128. ssize_t ret = 0;
  129. char kbuf[80];
  130. while (size) {
  131. struct keybuf_key *w;
  132. unsigned bytes = min(i->bytes, size);
  133. int err = copy_to_user(buf, i->buf, bytes);
  134. if (err)
  135. return err;
  136. ret += bytes;
  137. buf += bytes;
  138. size -= bytes;
  139. i->bytes -= bytes;
  140. memmove(i->buf, i->buf + bytes, i->bytes);
  141. if (i->bytes)
  142. break;
  143. w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
  144. if (!w)
  145. break;
  146. bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
  147. i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
  148. bch_keybuf_del(&i->keys, w);
  149. }
  150. return ret;
  151. }
  152. static int bch_dump_open(struct inode *inode, struct file *file)
  153. {
  154. struct cache_set *c = inode->i_private;
  155. struct dump_iterator *i;
  156. i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
  157. if (!i)
  158. return -ENOMEM;
  159. file->private_data = i;
  160. i->c = c;
  161. bch_keybuf_init(&i->keys);
  162. i->keys.last_scanned = KEY(0, 0, 0);
  163. return 0;
  164. }
  165. static int bch_dump_release(struct inode *inode, struct file *file)
  166. {
  167. kfree(file->private_data);
  168. return 0;
  169. }
  170. static const struct file_operations cache_set_debug_ops = {
  171. .owner = THIS_MODULE,
  172. .open = bch_dump_open,
  173. .read = bch_dump_read,
  174. .release = bch_dump_release
  175. };
  176. void bch_debug_init_cache_set(struct cache_set *c)
  177. {
  178. if (!IS_ERR_OR_NULL(debug)) {
  179. char name[50];
  180. snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
  181. c->debug = debugfs_create_file(name, 0400, debug, c,
  182. &cache_set_debug_ops);
  183. }
  184. }
  185. #endif
  186. void bch_debug_exit(void)
  187. {
  188. if (!IS_ERR_OR_NULL(debug))
  189. debugfs_remove_recursive(debug);
  190. }
  191. int __init bch_debug_init(struct kobject *kobj)
  192. {
  193. int ret = 0;
  194. debug = debugfs_create_dir("bcache", NULL);
  195. return ret;
  196. }