debug.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. bch_bio_map(bio, sorted);
  45. submit_bio_wait(REQ_META|READ_SYNC, bio);
  46. bch_bbio_free(bio, b->c);
  47. memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9);
  48. bch_btree_node_read_done(v);
  49. sorted = v->keys.set->data;
  50. if (inmemory->keys != sorted->keys ||
  51. memcmp(inmemory->start,
  52. sorted->start,
  53. (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) {
  54. struct bset *i;
  55. unsigned j;
  56. console_lock();
  57. printk(KERN_ERR "*** in memory:\n");
  58. bch_dump_bset(&b->keys, inmemory, 0);
  59. printk(KERN_ERR "*** read back in:\n");
  60. bch_dump_bset(&v->keys, sorted, 0);
  61. for_each_written_bset(b, ondisk, i) {
  62. unsigned block = ((void *) i - (void *) ondisk) /
  63. block_bytes(b->c);
  64. printk(KERN_ERR "*** on disk block %u:\n", block);
  65. bch_dump_bset(&b->keys, i, block);
  66. }
  67. printk(KERN_ERR "*** block %zu not written\n",
  68. ((void *) i - (void *) ondisk) / block_bytes(b->c));
  69. for (j = 0; j < inmemory->keys; j++)
  70. if (inmemory->d[j] != sorted->d[j])
  71. break;
  72. printk(KERN_ERR "b->written %u\n", b->written);
  73. console_unlock();
  74. panic("verify failed at %u\n", j);
  75. }
  76. mutex_unlock(&b->c->verify_lock);
  77. up(&b->io_mutex);
  78. }
  79. void bch_data_verify(struct cached_dev *dc, struct bio *bio)
  80. {
  81. char name[BDEVNAME_SIZE];
  82. struct bio *check;
  83. struct bio_vec bv, *bv2;
  84. struct bvec_iter iter;
  85. int i;
  86. check = bio_clone(bio, GFP_NOIO);
  87. if (!check)
  88. return;
  89. if (bio_alloc_pages(check, GFP_NOIO))
  90. goto out_put;
  91. submit_bio_wait(READ_SYNC, check);
  92. bio_for_each_segment(bv, bio, iter) {
  93. void *p1 = kmap_atomic(bv.bv_page);
  94. void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page);
  95. cache_set_err_on(memcmp(p1 + bv.bv_offset,
  96. p2 + bv.bv_offset,
  97. bv.bv_len),
  98. dc->disk.c,
  99. "verify failed at dev %s sector %llu",
  100. bdevname(dc->bdev, name),
  101. (uint64_t) bio->bi_iter.bi_sector);
  102. kunmap_atomic(p1);
  103. }
  104. bio_for_each_segment_all(bv2, check, i)
  105. __free_page(bv2->bv_page);
  106. out_put:
  107. bio_put(check);
  108. }
  109. #endif
  110. #ifdef CONFIG_DEBUG_FS
  111. /* XXX: cache set refcounting */
  112. struct dump_iterator {
  113. char buf[PAGE_SIZE];
  114. size_t bytes;
  115. struct cache_set *c;
  116. struct keybuf keys;
  117. };
  118. static bool dump_pred(struct keybuf *buf, struct bkey *k)
  119. {
  120. return true;
  121. }
  122. static ssize_t bch_dump_read(struct file *file, char __user *buf,
  123. size_t size, loff_t *ppos)
  124. {
  125. struct dump_iterator *i = file->private_data;
  126. ssize_t ret = 0;
  127. char kbuf[80];
  128. while (size) {
  129. struct keybuf_key *w;
  130. unsigned bytes = min(i->bytes, size);
  131. int err = copy_to_user(buf, i->buf, bytes);
  132. if (err)
  133. return err;
  134. ret += bytes;
  135. buf += bytes;
  136. size -= bytes;
  137. i->bytes -= bytes;
  138. memmove(i->buf, i->buf + bytes, i->bytes);
  139. if (i->bytes)
  140. break;
  141. w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
  142. if (!w)
  143. break;
  144. bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
  145. i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
  146. bch_keybuf_del(&i->keys, w);
  147. }
  148. return ret;
  149. }
  150. static int bch_dump_open(struct inode *inode, struct file *file)
  151. {
  152. struct cache_set *c = inode->i_private;
  153. struct dump_iterator *i;
  154. i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
  155. if (!i)
  156. return -ENOMEM;
  157. file->private_data = i;
  158. i->c = c;
  159. bch_keybuf_init(&i->keys);
  160. i->keys.last_scanned = KEY(0, 0, 0);
  161. return 0;
  162. }
  163. static int bch_dump_release(struct inode *inode, struct file *file)
  164. {
  165. kfree(file->private_data);
  166. return 0;
  167. }
  168. static const struct file_operations cache_set_debug_ops = {
  169. .owner = THIS_MODULE,
  170. .open = bch_dump_open,
  171. .read = bch_dump_read,
  172. .release = bch_dump_release
  173. };
  174. void bch_debug_init_cache_set(struct cache_set *c)
  175. {
  176. if (!IS_ERR_OR_NULL(debug)) {
  177. char name[50];
  178. snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
  179. c->debug = debugfs_create_file(name, 0400, debug, c,
  180. &cache_set_debug_ops);
  181. }
  182. }
  183. #endif
  184. void bch_debug_exit(void)
  185. {
  186. if (!IS_ERR_OR_NULL(debug))
  187. debugfs_remove_recursive(debug);
  188. }
  189. int __init bch_debug_init(struct kobject *kobj)
  190. {
  191. int ret = 0;
  192. debug = debugfs_create_dir("bcache", NULL);
  193. return ret;
  194. }