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