1cafe5635SKent Overstreet /* 2cafe5635SKent Overstreet * Assorted bcache debug code 3cafe5635SKent Overstreet * 4cafe5635SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 5cafe5635SKent Overstreet * Copyright 2012 Google, Inc. 6cafe5635SKent Overstreet */ 7cafe5635SKent Overstreet 8cafe5635SKent Overstreet #include "bcache.h" 9cafe5635SKent Overstreet #include "btree.h" 10cafe5635SKent Overstreet #include "debug.h" 11dc9d98d6SKent Overstreet #include "extents.h" 12cafe5635SKent Overstreet 13cafe5635SKent Overstreet #include <linux/console.h> 14cafe5635SKent Overstreet #include <linux/debugfs.h> 15cafe5635SKent Overstreet #include <linux/module.h> 16cafe5635SKent Overstreet #include <linux/random.h> 17cafe5635SKent Overstreet #include <linux/seq_file.h> 18cafe5635SKent Overstreet 19cafe5635SKent Overstreet static struct dentry *debug; 20cafe5635SKent Overstreet 21280481d0SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG 22cafe5635SKent Overstreet 2378b77bf8SKent Overstreet #define for_each_written_bset(b, start, i) \ 2478b77bf8SKent Overstreet for (i = (start); \ 2578b77bf8SKent Overstreet (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\ 2678b77bf8SKent Overstreet i->seq == (start)->seq; \ 27ee811287SKent Overstreet i = (void *) i + set_blocks(i, block_bytes(b->c)) * \ 28ee811287SKent Overstreet block_bytes(b->c)) 2978b77bf8SKent Overstreet 3078b77bf8SKent Overstreet void bch_btree_verify(struct btree *b) 31cafe5635SKent Overstreet { 32cafe5635SKent Overstreet struct btree *v = b->c->verify_data; 3378b77bf8SKent Overstreet struct bset *ondisk, *sorted, *inmemory; 3478b77bf8SKent Overstreet struct bio *bio; 35cafe5635SKent Overstreet 3678b77bf8SKent Overstreet if (!b->c->verify || !b->c->verify_ondisk) 37cafe5635SKent Overstreet return; 38cafe5635SKent Overstreet 39cb7a583eSKent Overstreet down(&b->io_mutex); 40cafe5635SKent Overstreet mutex_lock(&b->c->verify_lock); 41cafe5635SKent Overstreet 4278b77bf8SKent Overstreet ondisk = b->c->verify_ondisk; 43a85e968eSKent Overstreet sorted = b->c->verify_data->keys.set->data; 44a85e968eSKent Overstreet inmemory = b->keys.set->data; 4578b77bf8SKent Overstreet 46cafe5635SKent Overstreet bkey_copy(&v->key, &b->key); 47cafe5635SKent Overstreet v->written = 0; 48cafe5635SKent Overstreet v->level = b->level; 49a85e968eSKent Overstreet v->keys.ops = b->keys.ops; 50cafe5635SKent Overstreet 5178b77bf8SKent Overstreet bio = bch_bbio_alloc(b->c); 52*74d46992SChristoph Hellwig bio_set_dev(bio, PTR_CACHE(b->c, &b->key, 0)->bdev); 5378b77bf8SKent Overstreet bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0); 5478b77bf8SKent Overstreet bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9; 5570fd7614SChristoph Hellwig bio->bi_opf = REQ_OP_READ | REQ_META; 5678b77bf8SKent Overstreet bch_bio_map(bio, sorted); 57cafe5635SKent Overstreet 584e49ea4aSMike Christie submit_bio_wait(bio); 5978b77bf8SKent Overstreet bch_bbio_free(bio, b->c); 6078b77bf8SKent Overstreet 6178b77bf8SKent Overstreet memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9); 6278b77bf8SKent Overstreet 6378b77bf8SKent Overstreet bch_btree_node_read_done(v); 64a85e968eSKent Overstreet sorted = v->keys.set->data; 6578b77bf8SKent Overstreet 6678b77bf8SKent Overstreet if (inmemory->keys != sorted->keys || 6778b77bf8SKent Overstreet memcmp(inmemory->start, 6878b77bf8SKent Overstreet sorted->start, 69fafff81cSKent Overstreet (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) { 7078b77bf8SKent Overstreet struct bset *i; 7178b77bf8SKent Overstreet unsigned j; 72cafe5635SKent Overstreet 73cafe5635SKent Overstreet console_lock(); 74cafe5635SKent Overstreet 7578b77bf8SKent Overstreet printk(KERN_ERR "*** in memory:\n"); 76dc9d98d6SKent Overstreet bch_dump_bset(&b->keys, inmemory, 0); 77cafe5635SKent Overstreet 7878b77bf8SKent Overstreet printk(KERN_ERR "*** read back in:\n"); 79dc9d98d6SKent Overstreet bch_dump_bset(&v->keys, sorted, 0); 80cafe5635SKent Overstreet 8178b77bf8SKent Overstreet for_each_written_bset(b, ondisk, i) { 8278b77bf8SKent Overstreet unsigned block = ((void *) i - (void *) ondisk) / 8378b77bf8SKent Overstreet block_bytes(b->c); 84cafe5635SKent Overstreet 8578b77bf8SKent Overstreet printk(KERN_ERR "*** on disk block %u:\n", block); 86dc9d98d6SKent Overstreet bch_dump_bset(&b->keys, i, block); 8778b77bf8SKent Overstreet } 8878b77bf8SKent Overstreet 8978b77bf8SKent Overstreet printk(KERN_ERR "*** block %zu not written\n", 9078b77bf8SKent Overstreet ((void *) i - (void *) ondisk) / block_bytes(b->c)); 9178b77bf8SKent Overstreet 9278b77bf8SKent Overstreet for (j = 0; j < inmemory->keys; j++) 9378b77bf8SKent Overstreet if (inmemory->d[j] != sorted->d[j]) 94cafe5635SKent Overstreet break; 95cafe5635SKent Overstreet 9678b77bf8SKent Overstreet printk(KERN_ERR "b->written %u\n", b->written); 9778b77bf8SKent Overstreet 98cafe5635SKent Overstreet console_unlock(); 99cafe5635SKent Overstreet panic("verify failed at %u\n", j); 100cafe5635SKent Overstreet } 101cafe5635SKent Overstreet 102cafe5635SKent Overstreet mutex_unlock(&b->c->verify_lock); 103cb7a583eSKent Overstreet up(&b->io_mutex); 104cafe5635SKent Overstreet } 105cafe5635SKent Overstreet 106220bb38cSKent Overstreet void bch_data_verify(struct cached_dev *dc, struct bio *bio) 107cafe5635SKent Overstreet { 108cafe5635SKent Overstreet char name[BDEVNAME_SIZE]; 109cafe5635SKent Overstreet struct bio *check; 1104113b88aSMing Lei struct bio_vec bv, cbv; 1114113b88aSMing Lei struct bvec_iter iter, citer = { 0 }; 112cafe5635SKent Overstreet 1135a136fdfSNeilBrown check = bio_clone_kmalloc(bio, GFP_NOIO); 114cafe5635SKent Overstreet if (!check) 115cafe5635SKent Overstreet return; 11670fd7614SChristoph Hellwig check->bi_opf = REQ_OP_READ; 117cafe5635SKent Overstreet 1188e51e414SKent Overstreet if (bio_alloc_pages(check, GFP_NOIO)) 119cafe5635SKent Overstreet goto out_put; 120cafe5635SKent Overstreet 1214e49ea4aSMike Christie submit_bio_wait(check); 122cafe5635SKent Overstreet 1234113b88aSMing Lei citer.bi_size = UINT_MAX; 1247988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 1257988613bSKent Overstreet void *p1 = kmap_atomic(bv.bv_page); 1264113b88aSMing Lei void *p2; 1274113b88aSMing Lei 1284113b88aSMing Lei cbv = bio_iter_iovec(check, citer); 1294113b88aSMing Lei p2 = page_address(cbv.bv_page); 130cafe5635SKent Overstreet 1317988613bSKent Overstreet cache_set_err_on(memcmp(p1 + bv.bv_offset, 1327988613bSKent Overstreet p2 + bv.bv_offset, 1337988613bSKent Overstreet bv.bv_len), 1345ceaaad7SKent Overstreet dc->disk.c, 1355ceaaad7SKent Overstreet "verify failed at dev %s sector %llu", 136cafe5635SKent Overstreet bdevname(dc->bdev, name), 1374f024f37SKent Overstreet (uint64_t) bio->bi_iter.bi_sector); 1385ceaaad7SKent Overstreet 139220bb38cSKent Overstreet kunmap_atomic(p1); 1404113b88aSMing Lei bio_advance_iter(check, &citer, bv.bv_len); 141cafe5635SKent Overstreet } 142cafe5635SKent Overstreet 143491221f8SGuoqing Jiang bio_free_pages(check); 144cafe5635SKent Overstreet out_put: 145cafe5635SKent Overstreet bio_put(check); 146cafe5635SKent Overstreet } 147cafe5635SKent Overstreet 148cafe5635SKent Overstreet #endif 149cafe5635SKent Overstreet 150cafe5635SKent Overstreet #ifdef CONFIG_DEBUG_FS 151cafe5635SKent Overstreet 152cafe5635SKent Overstreet /* XXX: cache set refcounting */ 153cafe5635SKent Overstreet 154cafe5635SKent Overstreet struct dump_iterator { 155cafe5635SKent Overstreet char buf[PAGE_SIZE]; 156cafe5635SKent Overstreet size_t bytes; 157cafe5635SKent Overstreet struct cache_set *c; 158cafe5635SKent Overstreet struct keybuf keys; 159cafe5635SKent Overstreet }; 160cafe5635SKent Overstreet 161cafe5635SKent Overstreet static bool dump_pred(struct keybuf *buf, struct bkey *k) 162cafe5635SKent Overstreet { 163cafe5635SKent Overstreet return true; 164cafe5635SKent Overstreet } 165cafe5635SKent Overstreet 166cafe5635SKent Overstreet static ssize_t bch_dump_read(struct file *file, char __user *buf, 167cafe5635SKent Overstreet size_t size, loff_t *ppos) 168cafe5635SKent Overstreet { 169cafe5635SKent Overstreet struct dump_iterator *i = file->private_data; 170cafe5635SKent Overstreet ssize_t ret = 0; 17185b1492eSKent Overstreet char kbuf[80]; 172cafe5635SKent Overstreet 173cafe5635SKent Overstreet while (size) { 174cafe5635SKent Overstreet struct keybuf_key *w; 175cafe5635SKent Overstreet unsigned bytes = min(i->bytes, size); 176cafe5635SKent Overstreet 177cafe5635SKent Overstreet int err = copy_to_user(buf, i->buf, bytes); 178cafe5635SKent Overstreet if (err) 179cafe5635SKent Overstreet return err; 180cafe5635SKent Overstreet 181cafe5635SKent Overstreet ret += bytes; 182cafe5635SKent Overstreet buf += bytes; 183cafe5635SKent Overstreet size -= bytes; 184cafe5635SKent Overstreet i->bytes -= bytes; 185cafe5635SKent Overstreet memmove(i->buf, i->buf + bytes, i->bytes); 186cafe5635SKent Overstreet 187cafe5635SKent Overstreet if (i->bytes) 188cafe5635SKent Overstreet break; 189cafe5635SKent Overstreet 19072c27061SKent Overstreet w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY, dump_pred); 191cafe5635SKent Overstreet if (!w) 192cafe5635SKent Overstreet break; 193cafe5635SKent Overstreet 194dc9d98d6SKent Overstreet bch_extent_to_text(kbuf, sizeof(kbuf), &w->key); 19585b1492eSKent Overstreet i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf); 196cafe5635SKent Overstreet bch_keybuf_del(&i->keys, w); 197cafe5635SKent Overstreet } 198cafe5635SKent Overstreet 199cafe5635SKent Overstreet return ret; 200cafe5635SKent Overstreet } 201cafe5635SKent Overstreet 202cafe5635SKent Overstreet static int bch_dump_open(struct inode *inode, struct file *file) 203cafe5635SKent Overstreet { 204cafe5635SKent Overstreet struct cache_set *c = inode->i_private; 205cafe5635SKent Overstreet struct dump_iterator *i; 206cafe5635SKent Overstreet 207cafe5635SKent Overstreet i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL); 208cafe5635SKent Overstreet if (!i) 209cafe5635SKent Overstreet return -ENOMEM; 210cafe5635SKent Overstreet 211cafe5635SKent Overstreet file->private_data = i; 212cafe5635SKent Overstreet i->c = c; 21372c27061SKent Overstreet bch_keybuf_init(&i->keys); 214cafe5635SKent Overstreet i->keys.last_scanned = KEY(0, 0, 0); 215cafe5635SKent Overstreet 216cafe5635SKent Overstreet return 0; 217cafe5635SKent Overstreet } 218cafe5635SKent Overstreet 219cafe5635SKent Overstreet static int bch_dump_release(struct inode *inode, struct file *file) 220cafe5635SKent Overstreet { 221cafe5635SKent Overstreet kfree(file->private_data); 222cafe5635SKent Overstreet return 0; 223cafe5635SKent Overstreet } 224cafe5635SKent Overstreet 225cafe5635SKent Overstreet static const struct file_operations cache_set_debug_ops = { 226cafe5635SKent Overstreet .owner = THIS_MODULE, 227cafe5635SKent Overstreet .open = bch_dump_open, 228cafe5635SKent Overstreet .read = bch_dump_read, 229cafe5635SKent Overstreet .release = bch_dump_release 230cafe5635SKent Overstreet }; 231cafe5635SKent Overstreet 232cafe5635SKent Overstreet void bch_debug_init_cache_set(struct cache_set *c) 233cafe5635SKent Overstreet { 234cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(debug)) { 235cafe5635SKent Overstreet char name[50]; 236cafe5635SKent Overstreet snprintf(name, 50, "bcache-%pU", c->sb.set_uuid); 237cafe5635SKent Overstreet 238cafe5635SKent Overstreet c->debug = debugfs_create_file(name, 0400, debug, c, 239cafe5635SKent Overstreet &cache_set_debug_ops); 240cafe5635SKent Overstreet } 241cafe5635SKent Overstreet } 242cafe5635SKent Overstreet 243cafe5635SKent Overstreet #endif 244cafe5635SKent Overstreet 245cafe5635SKent Overstreet void bch_debug_exit(void) 246cafe5635SKent Overstreet { 247cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(debug)) 248cafe5635SKent Overstreet debugfs_remove_recursive(debug); 249cafe5635SKent Overstreet } 250cafe5635SKent Overstreet 251cafe5635SKent Overstreet int __init bch_debug_init(struct kobject *kobj) 252cafe5635SKent Overstreet { 253cafe5635SKent Overstreet int ret = 0; 254cafe5635SKent Overstreet 255cafe5635SKent Overstreet debug = debugfs_create_dir("bcache", NULL); 256cafe5635SKent Overstreet return ret; 257cafe5635SKent Overstreet } 258