debug.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Assorted bcache debug code
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <[email protected]>
  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. struct dentry *bcache_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->cache)) * \
  24. block_bytes(b->c->cache))
  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, b->c->cache->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) -
  56. (void *) inmemory->start)) {
  57. struct bset *i;
  58. unsigned int j;
  59. console_lock();
  60. pr_err("*** in memory:\n");
  61. bch_dump_bset(&b->keys, inmemory, 0);
  62. pr_err("*** read back in:\n");
  63. bch_dump_bset(&v->keys, sorted, 0);
  64. for_each_written_bset(b, ondisk, i) {
  65. unsigned int block = ((void *) i - (void *) ondisk) /
  66. block_bytes(b->c->cache);
  67. pr_err("*** on disk block %u:\n", block);
  68. bch_dump_bset(&b->keys, i, block);
  69. }
  70. pr_err("*** block %zu not written\n",
  71. ((void *) i - (void *) ondisk) / block_bytes(b->c->cache));
  72. for (j = 0; j < inmemory->keys; j++)
  73. if (inmemory->d[j] != sorted->d[j])
  74. break;
  75. pr_err("b->written %u\n", b->written);
  76. console_unlock();
  77. panic("verify failed at %u\n", j);
  78. }
  79. mutex_unlock(&b->c->verify_lock);
  80. up(&b->io_mutex);
  81. }
  82. void bch_data_verify(struct cached_dev *dc, struct bio *bio)
  83. {
  84. unsigned int nr_segs = bio_segments(bio);
  85. struct bio *check;
  86. struct bio_vec bv, cbv;
  87. struct bvec_iter iter, citer = { 0 };
  88. check = bio_kmalloc(nr_segs, GFP_NOIO);
  89. if (!check)
  90. return;
  91. bio_init(check, bio->bi_bdev, check->bi_inline_vecs, nr_segs,
  92. REQ_OP_READ);
  93. check->bi_iter.bi_sector = bio->bi_iter.bi_sector;
  94. check->bi_iter.bi_size = bio->bi_iter.bi_size;
  95. bch_bio_map(check, NULL);
  96. if (bch_bio_alloc_pages(check, GFP_NOIO))
  97. goto out_put;
  98. submit_bio_wait(check);
  99. citer.bi_size = UINT_MAX;
  100. bio_for_each_segment(bv, bio, iter) {
  101. void *p1 = bvec_kmap_local(&bv);
  102. void *p2;
  103. cbv = bio_iter_iovec(check, citer);
  104. p2 = bvec_kmap_local(&cbv);
  105. cache_set_err_on(memcmp(p1, p2, bv.bv_len),
  106. dc->disk.c,
  107. "verify failed at dev %pg sector %llu",
  108. dc->bdev,
  109. (uint64_t) bio->bi_iter.bi_sector);
  110. kunmap_local(p2);
  111. kunmap_local(p1);
  112. bio_advance_iter(check, &citer, bv.bv_len);
  113. }
  114. bio_free_pages(check);
  115. out_put:
  116. bio_uninit(check);
  117. kfree(check);
  118. }
  119. #endif
  120. #ifdef CONFIG_DEBUG_FS
  121. /* XXX: cache set refcounting */
  122. struct dump_iterator {
  123. char buf[PAGE_SIZE];
  124. size_t bytes;
  125. struct cache_set *c;
  126. struct keybuf keys;
  127. };
  128. static bool dump_pred(struct keybuf *buf, struct bkey *k)
  129. {
  130. return true;
  131. }
  132. static ssize_t bch_dump_read(struct file *file, char __user *buf,
  133. size_t size, loff_t *ppos)
  134. {
  135. struct dump_iterator *i = file->private_data;
  136. ssize_t ret = 0;
  137. char kbuf[80];
  138. while (size) {
  139. struct keybuf_key *w;
  140. unsigned int bytes = min(i->bytes, size);
  141. if (copy_to_user(buf, i->buf, bytes))
  142. return -EFAULT;
  143. ret += bytes;
  144. buf += bytes;
  145. size -= bytes;
  146. i->bytes -= bytes;
  147. memmove(i->buf, i->buf + bytes, i->bytes);
  148. if (i->bytes)
  149. break;
  150. w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
  151. if (!w)
  152. break;
  153. bch_extent_to_text(kbuf, sizeof(kbuf), &w->key);
  154. i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
  155. bch_keybuf_del(&i->keys, w);
  156. }
  157. return ret;
  158. }
  159. static int bch_dump_open(struct inode *inode, struct file *file)
  160. {
  161. struct cache_set *c = inode->i_private;
  162. struct dump_iterator *i;
  163. i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
  164. if (!i)
  165. return -ENOMEM;
  166. file->private_data = i;
  167. i->c = c;
  168. bch_keybuf_init(&i->keys);
  169. i->keys.last_scanned = KEY(0, 0, 0);
  170. return 0;
  171. }
  172. static int bch_dump_release(struct inode *inode, struct file *file)
  173. {
  174. kfree(file->private_data);
  175. return 0;
  176. }
  177. static const struct file_operations cache_set_debug_ops = {
  178. .owner = THIS_MODULE,
  179. .open = bch_dump_open,
  180. .read = bch_dump_read,
  181. .release = bch_dump_release
  182. };
  183. void bch_debug_init_cache_set(struct cache_set *c)
  184. {
  185. if (!IS_ERR_OR_NULL(bcache_debug)) {
  186. char name[50];
  187. snprintf(name, 50, "bcache-%pU", c->set_uuid);
  188. c->debug = debugfs_create_file(name, 0400, bcache_debug, c,
  189. &cache_set_debug_ops);
  190. }
  191. }
  192. #endif
  193. void bch_debug_exit(void)
  194. {
  195. debugfs_remove_recursive(bcache_debug);
  196. }
  197. void __init bch_debug_init(void)
  198. {
  199. /*
  200. * it is unnecessary to check return value of
  201. * debugfs_create_file(), we should not care
  202. * about this.
  203. */
  204. bcache_debug = debugfs_create_dir("bcache", NULL);
  205. }