debug.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "request.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. const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
  18. {
  19. unsigned i;
  20. for (i = 0; i < KEY_PTRS(k); i++)
  21. if (ptr_available(c, k, i)) {
  22. struct cache *ca = PTR_CACHE(c, k, i);
  23. size_t bucket = PTR_BUCKET_NR(c, k, i);
  24. size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
  25. if (KEY_SIZE(k) + r > c->sb.bucket_size)
  26. return "bad, length too big";
  27. if (bucket < ca->sb.first_bucket)
  28. return "bad, short offset";
  29. if (bucket >= ca->sb.nbuckets)
  30. return "bad, offset past end of device";
  31. if (ptr_stale(c, k, i))
  32. return "stale";
  33. }
  34. if (!bkey_cmp(k, &ZERO_KEY))
  35. return "bad, null key";
  36. if (!KEY_PTRS(k))
  37. return "bad, no pointers";
  38. if (!KEY_SIZE(k))
  39. return "zeroed key";
  40. return "";
  41. }
  42. int bch_bkey_to_text(char *buf, size_t size, const struct bkey *k)
  43. {
  44. unsigned i = 0;
  45. char *out = buf, *end = buf + size;
  46. #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
  47. p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_OFFSET(k), KEY_SIZE(k));
  48. if (KEY_PTRS(k))
  49. while (1) {
  50. p("%llu:%llu gen %llu",
  51. PTR_DEV(k, i), PTR_OFFSET(k, i), PTR_GEN(k, i));
  52. if (++i == KEY_PTRS(k))
  53. break;
  54. p(", ");
  55. }
  56. p("]");
  57. if (KEY_DIRTY(k))
  58. p(" dirty");
  59. if (KEY_CSUM(k))
  60. p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
  61. #undef p
  62. return out - buf;
  63. }
  64. int bch_btree_to_text(char *buf, size_t size, const struct btree *b)
  65. {
  66. return scnprintf(buf, size, "%zu level %i/%i",
  67. PTR_BUCKET_NR(b->c, &b->key, 0),
  68. b->level, b->c->root ? b->c->root->level : -1);
  69. }
  70. #if defined(CONFIG_BCACHE_DEBUG) || defined(CONFIG_BCACHE_EDEBUG)
  71. static bool skipped_backwards(struct btree *b, struct bkey *k)
  72. {
  73. return bkey_cmp(k, (!b->level)
  74. ? &START_KEY(bkey_next(k))
  75. : bkey_next(k)) > 0;
  76. }
  77. static void dump_bset(struct btree *b, struct bset *i)
  78. {
  79. struct bkey *k;
  80. unsigned j;
  81. char buf[80];
  82. for (k = i->start; k < end(i); k = bkey_next(k)) {
  83. bch_bkey_to_text(buf, sizeof(buf), k);
  84. printk(KERN_ERR "block %zu key %zi/%u: %s", index(i, b),
  85. (uint64_t *) k - i->d, i->keys, buf);
  86. for (j = 0; j < KEY_PTRS(k); j++) {
  87. size_t n = PTR_BUCKET_NR(b->c, k, j);
  88. printk(" bucket %zu", n);
  89. if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
  90. printk(" prio %i",
  91. PTR_BUCKET(b->c, k, j)->prio);
  92. }
  93. printk(" %s\n", bch_ptr_status(b->c, k));
  94. if (bkey_next(k) < end(i) &&
  95. skipped_backwards(b, k))
  96. printk(KERN_ERR "Key skipped backwards\n");
  97. }
  98. }
  99. #endif
  100. #ifdef CONFIG_BCACHE_DEBUG
  101. void bch_btree_verify(struct btree *b, struct bset *new)
  102. {
  103. struct btree *v = b->c->verify_data;
  104. struct closure cl;
  105. closure_init_stack(&cl);
  106. if (!b->c->verify)
  107. return;
  108. closure_wait_event(&b->io.wait, &cl,
  109. atomic_read(&b->io.cl.remaining) == -1);
  110. mutex_lock(&b->c->verify_lock);
  111. bkey_copy(&v->key, &b->key);
  112. v->written = 0;
  113. v->level = b->level;
  114. bch_btree_node_read(v);
  115. closure_wait_event(&v->io.wait, &cl,
  116. atomic_read(&b->io.cl.remaining) == -1);
  117. if (new->keys != v->sets[0].data->keys ||
  118. memcmp(new->start,
  119. v->sets[0].data->start,
  120. (void *) end(new) - (void *) new->start)) {
  121. unsigned i, j;
  122. console_lock();
  123. printk(KERN_ERR "*** original memory node:\n");
  124. for (i = 0; i <= b->nsets; i++)
  125. dump_bset(b, b->sets[i].data);
  126. printk(KERN_ERR "*** sorted memory node:\n");
  127. dump_bset(b, new);
  128. printk(KERN_ERR "*** on disk node:\n");
  129. dump_bset(v, v->sets[0].data);
  130. for (j = 0; j < new->keys; j++)
  131. if (new->d[j] != v->sets[0].data->d[j])
  132. break;
  133. console_unlock();
  134. panic("verify failed at %u\n", j);
  135. }
  136. mutex_unlock(&b->c->verify_lock);
  137. }
  138. static void data_verify_endio(struct bio *bio, int error)
  139. {
  140. struct closure *cl = bio->bi_private;
  141. closure_put(cl);
  142. }
  143. void bch_data_verify(struct search *s)
  144. {
  145. char name[BDEVNAME_SIZE];
  146. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  147. struct closure *cl = &s->cl;
  148. struct bio *check;
  149. struct bio_vec *bv;
  150. int i;
  151. if (!s->unaligned_bvec)
  152. bio_for_each_segment(bv, s->orig_bio, i)
  153. bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
  154. check = bio_clone(s->orig_bio, GFP_NOIO);
  155. if (!check)
  156. return;
  157. if (bio_alloc_pages(check, GFP_NOIO))
  158. goto out_put;
  159. check->bi_rw = READ_SYNC;
  160. check->bi_private = cl;
  161. check->bi_end_io = data_verify_endio;
  162. closure_bio_submit(check, cl, &dc->disk);
  163. closure_sync(cl);
  164. bio_for_each_segment(bv, s->orig_bio, i) {
  165. void *p1 = kmap(bv->bv_page);
  166. void *p2 = kmap(check->bi_io_vec[i].bv_page);
  167. if (memcmp(p1 + bv->bv_offset,
  168. p2 + bv->bv_offset,
  169. bv->bv_len))
  170. printk(KERN_ERR
  171. "bcache (%s): verify failed at sector %llu\n",
  172. bdevname(dc->bdev, name),
  173. (uint64_t) s->orig_bio->bi_sector);
  174. kunmap(bv->bv_page);
  175. kunmap(check->bi_io_vec[i].bv_page);
  176. }
  177. __bio_for_each_segment(bv, check, i, 0)
  178. __free_page(bv->bv_page);
  179. out_put:
  180. bio_put(check);
  181. }
  182. #endif
  183. #ifdef CONFIG_BCACHE_EDEBUG
  184. unsigned bch_count_data(struct btree *b)
  185. {
  186. unsigned ret = 0;
  187. struct btree_iter iter;
  188. struct bkey *k;
  189. if (!b->level)
  190. for_each_key(b, k, &iter)
  191. ret += KEY_SIZE(k);
  192. return ret;
  193. }
  194. static void vdump_bucket_and_panic(struct btree *b, const char *fmt,
  195. va_list args)
  196. {
  197. unsigned i;
  198. char buf[80];
  199. console_lock();
  200. for (i = 0; i <= b->nsets; i++)
  201. dump_bset(b, b->sets[i].data);
  202. vprintk(fmt, args);
  203. console_unlock();
  204. bch_btree_to_text(buf, sizeof(buf), b);
  205. panic("at %s\n", buf);
  206. }
  207. void bch_check_key_order_msg(struct btree *b, struct bset *i,
  208. const char *fmt, ...)
  209. {
  210. struct bkey *k;
  211. if (!i->keys)
  212. return;
  213. for (k = i->start; bkey_next(k) < end(i); k = bkey_next(k))
  214. if (skipped_backwards(b, k)) {
  215. va_list args;
  216. va_start(args, fmt);
  217. vdump_bucket_and_panic(b, fmt, args);
  218. va_end(args);
  219. }
  220. }
  221. void bch_check_keys(struct btree *b, const char *fmt, ...)
  222. {
  223. va_list args;
  224. struct bkey *k, *p = NULL;
  225. struct btree_iter iter;
  226. if (b->level)
  227. return;
  228. for_each_key(b, k, &iter) {
  229. if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) {
  230. printk(KERN_ERR "Keys out of order:\n");
  231. goto bug;
  232. }
  233. if (bch_ptr_invalid(b, k))
  234. continue;
  235. if (p && bkey_cmp(p, &START_KEY(k)) > 0) {
  236. printk(KERN_ERR "Overlapping keys:\n");
  237. goto bug;
  238. }
  239. p = k;
  240. }
  241. return;
  242. bug:
  243. va_start(args, fmt);
  244. vdump_bucket_and_panic(b, fmt, args);
  245. va_end(args);
  246. }
  247. #endif
  248. #ifdef CONFIG_DEBUG_FS
  249. /* XXX: cache set refcounting */
  250. struct dump_iterator {
  251. char buf[PAGE_SIZE];
  252. size_t bytes;
  253. struct cache_set *c;
  254. struct keybuf keys;
  255. };
  256. static bool dump_pred(struct keybuf *buf, struct bkey *k)
  257. {
  258. return true;
  259. }
  260. static ssize_t bch_dump_read(struct file *file, char __user *buf,
  261. size_t size, loff_t *ppos)
  262. {
  263. struct dump_iterator *i = file->private_data;
  264. ssize_t ret = 0;
  265. char kbuf[80];
  266. while (size) {
  267. struct keybuf_key *w;
  268. unsigned bytes = min(i->bytes, size);
  269. int err = copy_to_user(buf, i->buf, bytes);
  270. if (err)
  271. return err;
  272. ret += bytes;
  273. buf += bytes;
  274. size -= bytes;
  275. i->bytes -= bytes;
  276. memmove(i->buf, i->buf + bytes, i->bytes);
  277. if (i->bytes)
  278. break;
  279. w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred);
  280. if (!w)
  281. break;
  282. bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key);
  283. i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf);
  284. bch_keybuf_del(&i->keys, w);
  285. }
  286. return ret;
  287. }
  288. static int bch_dump_open(struct inode *inode, struct file *file)
  289. {
  290. struct cache_set *c = inode->i_private;
  291. struct dump_iterator *i;
  292. i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
  293. if (!i)
  294. return -ENOMEM;
  295. file->private_data = i;
  296. i->c = c;
  297. bch_keybuf_init(&i->keys);
  298. i->keys.last_scanned = KEY(0, 0, 0);
  299. return 0;
  300. }
  301. static int bch_dump_release(struct inode *inode, struct file *file)
  302. {
  303. kfree(file->private_data);
  304. return 0;
  305. }
  306. static const struct file_operations cache_set_debug_ops = {
  307. .owner = THIS_MODULE,
  308. .open = bch_dump_open,
  309. .read = bch_dump_read,
  310. .release = bch_dump_release
  311. };
  312. void bch_debug_init_cache_set(struct cache_set *c)
  313. {
  314. if (!IS_ERR_OR_NULL(debug)) {
  315. char name[50];
  316. snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
  317. c->debug = debugfs_create_file(name, 0400, debug, c,
  318. &cache_set_debug_ops);
  319. }
  320. }
  321. #endif
  322. void bch_debug_exit(void)
  323. {
  324. if (!IS_ERR_OR_NULL(debug))
  325. debugfs_remove_recursive(debug);
  326. }
  327. int __init bch_debug_init(struct kobject *kobj)
  328. {
  329. int ret = 0;
  330. debug = debugfs_create_dir("bcache", NULL);
  331. return ret;
  332. }