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 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 56 | p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 57 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 58 | for (i = 0; i < KEY_PTRS(k); i++) { |
| 59 | if (i) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 60 | p(", "); |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 61 | |
| 62 | if (PTR_DEV(k, i) == PTR_CHECK_DEV) |
| 63 | p("check dev"); |
| 64 | else |
| 65 | p("%llu:%llu gen %llu", PTR_DEV(k, i), |
| 66 | PTR_OFFSET(k, i), PTR_GEN(k, i)); |
| 67 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 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 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 81 | static void dump_bset(struct btree *b, struct bset *i, unsigned set) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 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 | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 87 | for (k = i->start; k < bset_bkey_last(i); k = next) { |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 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 | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 91 | printk(KERN_ERR "b %u k %zi/%u: %s", set, |
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 | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 105 | if (next < bset_bkey_last(i) && |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 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++) |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 117 | dump_bset(b, b->sets[i].data, |
| 118 | bset_block_offset(b, b->sets[i].data)); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 119 | console_unlock(); |
| 120 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 121 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 122 | #define for_each_written_bset(b, start, i) \ |
| 123 | for (i = (start); \ |
| 124 | (void *) i < (void *) (start) + (KEY_SIZE(&b->key) << 9) &&\ |
| 125 | i->seq == (start)->seq; \ |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame^] | 126 | i = (void *) i + set_blocks(i, block_bytes(b->c)) * \ |
| 127 | block_bytes(b->c)) |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 128 | |
| 129 | void bch_btree_verify(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 130 | { |
| 131 | struct btree *v = b->c->verify_data; |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 132 | struct bset *ondisk, *sorted, *inmemory; |
| 133 | struct bio *bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 134 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 135 | if (!b->c->verify || !b->c->verify_ondisk) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 136 | return; |
| 137 | |
Kent Overstreet | cb7a583 | 2013-12-16 15:27:25 -0800 | [diff] [blame] | 138 | down(&b->io_mutex); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 139 | mutex_lock(&b->c->verify_lock); |
| 140 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 141 | ondisk = b->c->verify_ondisk; |
| 142 | sorted = b->c->verify_data->sets->data; |
| 143 | inmemory = b->sets->data; |
| 144 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 145 | bkey_copy(&v->key, &b->key); |
| 146 | v->written = 0; |
| 147 | v->level = b->level; |
| 148 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 149 | bio = bch_bbio_alloc(b->c); |
| 150 | bio->bi_bdev = PTR_CACHE(b->c, &b->key, 0)->bdev; |
| 151 | bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0); |
| 152 | bio->bi_iter.bi_size = KEY_SIZE(&v->key) << 9; |
| 153 | bch_bio_map(bio, sorted); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 154 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 155 | submit_bio_wait(REQ_META|READ_SYNC, bio); |
| 156 | bch_bbio_free(bio, b->c); |
| 157 | |
| 158 | memcpy(ondisk, sorted, KEY_SIZE(&v->key) << 9); |
| 159 | |
| 160 | bch_btree_node_read_done(v); |
| 161 | sorted = v->sets->data; |
| 162 | |
| 163 | if (inmemory->keys != sorted->keys || |
| 164 | memcmp(inmemory->start, |
| 165 | sorted->start, |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 166 | (void *) bset_bkey_last(inmemory) - (void *) inmemory->start)) { |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 167 | struct bset *i; |
| 168 | unsigned j; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 169 | |
| 170 | console_lock(); |
| 171 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 172 | printk(KERN_ERR "*** in memory:\n"); |
| 173 | dump_bset(b, inmemory, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 174 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 175 | printk(KERN_ERR "*** read back in:\n"); |
| 176 | dump_bset(v, sorted, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 177 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 178 | for_each_written_bset(b, ondisk, i) { |
| 179 | unsigned block = ((void *) i - (void *) ondisk) / |
| 180 | block_bytes(b->c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 181 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 182 | printk(KERN_ERR "*** on disk block %u:\n", block); |
| 183 | dump_bset(b, i, block); |
| 184 | } |
| 185 | |
| 186 | printk(KERN_ERR "*** block %zu not written\n", |
| 187 | ((void *) i - (void *) ondisk) / block_bytes(b->c)); |
| 188 | |
| 189 | for (j = 0; j < inmemory->keys; j++) |
| 190 | if (inmemory->d[j] != sorted->d[j]) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 191 | break; |
| 192 | |
Kent Overstreet | 78b77bf | 2013-12-17 22:49:08 -0800 | [diff] [blame] | 193 | printk(KERN_ERR "b->written %u\n", b->written); |
| 194 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 195 | console_unlock(); |
| 196 | panic("verify failed at %u\n", j); |
| 197 | } |
| 198 | |
| 199 | mutex_unlock(&b->c->verify_lock); |
Kent Overstreet | cb7a583 | 2013-12-16 15:27:25 -0800 | [diff] [blame] | 200 | up(&b->io_mutex); |
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 | void bch_data_verify(struct cached_dev *dc, struct bio *bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 204 | { |
| 205 | char name[BDEVNAME_SIZE]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 206 | struct bio *check; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 207 | struct bio_vec bv, *bv2; |
| 208 | struct bvec_iter iter; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 209 | int i; |
| 210 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 211 | check = bio_clone(bio, GFP_NOIO); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 212 | if (!check) |
| 213 | return; |
| 214 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 215 | if (bio_alloc_pages(check, GFP_NOIO)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 216 | goto out_put; |
| 217 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 218 | submit_bio_wait(READ_SYNC, check); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 219 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 220 | bio_for_each_segment(bv, bio, iter) { |
| 221 | void *p1 = kmap_atomic(bv.bv_page); |
| 222 | void *p2 = page_address(check->bi_io_vec[iter.bi_idx].bv_page); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 223 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 224 | cache_set_err_on(memcmp(p1 + bv.bv_offset, |
| 225 | p2 + bv.bv_offset, |
| 226 | bv.bv_len), |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 227 | dc->disk.c, |
| 228 | "verify failed at dev %s sector %llu", |
| 229 | bdevname(dc->bdev, name), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 230 | (uint64_t) bio->bi_iter.bi_sector); |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 231 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 232 | kunmap_atomic(p1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 235 | bio_for_each_segment_all(bv2, check, i) |
| 236 | __free_page(bv2->bv_page); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 237 | out_put: |
| 238 | bio_put(check); |
| 239 | } |
| 240 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 241 | int __bch_count_data(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 242 | { |
| 243 | unsigned ret = 0; |
| 244 | struct btree_iter iter; |
| 245 | struct bkey *k; |
| 246 | |
| 247 | if (!b->level) |
| 248 | for_each_key(b, k, &iter) |
| 249 | ret += KEY_SIZE(k); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 253 | void __bch_check_keys(struct btree *b, const char *fmt, ...) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 254 | { |
| 255 | va_list args; |
| 256 | struct bkey *k, *p = NULL; |
| 257 | struct btree_iter iter; |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 258 | const char *err; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 259 | |
| 260 | for_each_key(b, k, &iter) { |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 261 | if (!b->level) { |
| 262 | err = "Keys out of order"; |
| 263 | if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) |
| 264 | goto bug; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 265 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 266 | if (bch_ptr_invalid(b, k)) |
| 267 | continue; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 268 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 269 | err = "Overlapping keys"; |
| 270 | if (p && bkey_cmp(p, &START_KEY(k)) > 0) |
| 271 | goto bug; |
| 272 | } else { |
| 273 | if (bch_ptr_bad(b, k)) |
| 274 | continue; |
| 275 | |
| 276 | err = "Duplicate keys"; |
| 277 | if (p && !bkey_cmp(p, k)) |
| 278 | goto bug; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 279 | } |
| 280 | p = k; |
| 281 | } |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 282 | |
| 283 | err = "Key larger than btree node key"; |
| 284 | if (p && bkey_cmp(p, &b->key) > 0) |
| 285 | goto bug; |
| 286 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 287 | return; |
| 288 | bug: |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 289 | bch_dump_bucket(b); |
| 290 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 291 | va_start(args, fmt); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 292 | vprintk(fmt, args); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 293 | va_end(args); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 294 | |
| 295 | panic("bcache error: %s:\n", err); |
| 296 | } |
| 297 | |
| 298 | void bch_btree_iter_next_check(struct btree_iter *iter) |
| 299 | { |
| 300 | struct bkey *k = iter->data->k, *next = bkey_next(k); |
| 301 | |
| 302 | if (next < iter->data->end && |
| 303 | bkey_cmp(k, iter->b->level ? next : &START_KEY(next)) > 0) { |
| 304 | bch_dump_bucket(iter->b); |
| 305 | panic("Key skipped backwards\n"); |
| 306 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #endif |
| 310 | |
| 311 | #ifdef CONFIG_DEBUG_FS |
| 312 | |
| 313 | /* XXX: cache set refcounting */ |
| 314 | |
| 315 | struct dump_iterator { |
| 316 | char buf[PAGE_SIZE]; |
| 317 | size_t bytes; |
| 318 | struct cache_set *c; |
| 319 | struct keybuf keys; |
| 320 | }; |
| 321 | |
| 322 | static bool dump_pred(struct keybuf *buf, struct bkey *k) |
| 323 | { |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | static ssize_t bch_dump_read(struct file *file, char __user *buf, |
| 328 | size_t size, loff_t *ppos) |
| 329 | { |
| 330 | struct dump_iterator *i = file->private_data; |
| 331 | ssize_t ret = 0; |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 332 | char kbuf[80]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 333 | |
| 334 | while (size) { |
| 335 | struct keybuf_key *w; |
| 336 | unsigned bytes = min(i->bytes, size); |
| 337 | |
| 338 | int err = copy_to_user(buf, i->buf, bytes); |
| 339 | if (err) |
| 340 | return err; |
| 341 | |
| 342 | ret += bytes; |
| 343 | buf += bytes; |
| 344 | size -= bytes; |
| 345 | i->bytes -= bytes; |
| 346 | memmove(i->buf, i->buf + bytes, i->bytes); |
| 347 | |
| 348 | if (i->bytes) |
| 349 | break; |
| 350 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 351 | 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] | 352 | if (!w) |
| 353 | break; |
| 354 | |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 355 | bch_bkey_to_text(kbuf, sizeof(kbuf), &w->key); |
| 356 | i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", kbuf); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 357 | bch_keybuf_del(&i->keys, w); |
| 358 | } |
| 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static int bch_dump_open(struct inode *inode, struct file *file) |
| 364 | { |
| 365 | struct cache_set *c = inode->i_private; |
| 366 | struct dump_iterator *i; |
| 367 | |
| 368 | i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL); |
| 369 | if (!i) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | file->private_data = i; |
| 373 | i->c = c; |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 374 | bch_keybuf_init(&i->keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 375 | i->keys.last_scanned = KEY(0, 0, 0); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int bch_dump_release(struct inode *inode, struct file *file) |
| 381 | { |
| 382 | kfree(file->private_data); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static const struct file_operations cache_set_debug_ops = { |
| 387 | .owner = THIS_MODULE, |
| 388 | .open = bch_dump_open, |
| 389 | .read = bch_dump_read, |
| 390 | .release = bch_dump_release |
| 391 | }; |
| 392 | |
| 393 | void bch_debug_init_cache_set(struct cache_set *c) |
| 394 | { |
| 395 | if (!IS_ERR_OR_NULL(debug)) { |
| 396 | char name[50]; |
| 397 | snprintf(name, 50, "bcache-%pU", c->sb.set_uuid); |
| 398 | |
| 399 | c->debug = debugfs_create_file(name, 0400, debug, c, |
| 400 | &cache_set_debug_ops); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | #endif |
| 405 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 406 | void bch_debug_exit(void) |
| 407 | { |
| 408 | if (!IS_ERR_OR_NULL(debug)) |
| 409 | debugfs_remove_recursive(debug); |
| 410 | } |
| 411 | |
| 412 | int __init bch_debug_init(struct kobject *kobj) |
| 413 | { |
| 414 | int ret = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 415 | |
| 416 | debug = debugfs_create_dir("bcache", NULL); |
| 417 | return ret; |
| 418 | } |