debug.c 5.4 KB

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