debug.c 5.5 KB

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