2 * Assorted bcache debug code
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
13 #include <linux/console.h>
14 #include <linux/debugfs.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/seq_file.h>
19 static struct dentry
*debug
;
21 const char *bch_ptr_status(struct cache_set
*c
, const struct bkey
*k
)
25 for (i
= 0; i
< KEY_PTRS(k
); i
++)
26 if (ptr_available(c
, k
, i
)) {
27 struct cache
*ca
= PTR_CACHE(c
, k
, i
);
28 size_t bucket
= PTR_BUCKET_NR(c
, k
, i
);
29 size_t r
= bucket_remainder(c
, PTR_OFFSET(k
, i
));
31 if (KEY_SIZE(k
) + r
> c
->sb
.bucket_size
)
32 return "bad, length too big";
33 if (bucket
< ca
->sb
.first_bucket
)
34 return "bad, short offset";
35 if (bucket
>= ca
->sb
.nbuckets
)
36 return "bad, offset past end of device";
37 if (ptr_stale(c
, k
, i
))
41 if (!bkey_cmp(k
, &ZERO_KEY
))
42 return "bad, null key";
44 return "bad, no pointers";
50 struct keyprint_hack
bch_pkey(const struct bkey
*k
)
53 struct keyprint_hack r
;
54 char *out
= r
.s
, *end
= r
.s
+ KEYHACK_SIZE
;
56 #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
58 p("%llu:%llu len %llu -> [", KEY_INODE(k
), KEY_OFFSET(k
), KEY_SIZE(k
));
62 p("%llu:%llu gen %llu",
63 PTR_DEV(k
, i
), PTR_OFFSET(k
, i
), PTR_GEN(k
, i
));
65 if (++i
== KEY_PTRS(k
))
76 p(" cs%llu %llx", KEY_CSUM(k
), k
->ptr
[1]);
81 struct keyprint_hack
bch_pbtree(const struct btree
*b
)
83 struct keyprint_hack r
;
85 snprintf(r
.s
, 40, "%zu level %i/%i", PTR_BUCKET_NR(b
->c
, &b
->key
, 0),
86 b
->level
, b
->c
->root
? b
->c
->root
->level
: -1);
90 #if defined(CONFIG_BCACHE_DEBUG) || defined(CONFIG_BCACHE_EDEBUG)
92 static bool skipped_backwards(struct btree
*b
, struct bkey
*k
)
94 return bkey_cmp(k
, (!b
->level
)
95 ? &START_KEY(bkey_next(k
))
99 static void dump_bset(struct btree
*b
, struct bset
*i
)
104 for (k
= i
->start
; k
< end(i
); k
= bkey_next(k
)) {
105 printk(KERN_ERR
"block %zu key %zi/%u: %s", index(i
, b
),
106 (uint64_t *) k
- i
->d
, i
->keys
, pkey(k
));
108 for (j
= 0; j
< KEY_PTRS(k
); j
++) {
109 size_t n
= PTR_BUCKET_NR(b
->c
, k
, j
);
110 printk(" bucket %zu", n
);
112 if (n
>= b
->c
->sb
.first_bucket
&& n
< b
->c
->sb
.nbuckets
)
114 PTR_BUCKET(b
->c
, k
, j
)->prio
);
117 printk(" %s\n", bch_ptr_status(b
->c
, k
));
119 if (bkey_next(k
) < end(i
) &&
120 skipped_backwards(b
, k
))
121 printk(KERN_ERR
"Key skipped backwards\n");
127 #ifdef CONFIG_BCACHE_DEBUG
129 void bch_btree_verify(struct btree
*b
, struct bset
*new)
131 struct btree
*v
= b
->c
->verify_data
;
133 closure_init_stack(&cl
);
138 closure_wait_event(&b
->io
.wait
, &cl
,
139 atomic_read(&b
->io
.cl
.remaining
) == -1);
141 mutex_lock(&b
->c
->verify_lock
);
143 bkey_copy(&v
->key
, &b
->key
);
148 closure_wait_event(&v
->io
.wait
, &cl
,
149 atomic_read(&b
->io
.cl
.remaining
) == -1);
151 if (new->keys
!= v
->sets
[0].data
->keys
||
153 v
->sets
[0].data
->start
,
154 (void *) end(new) - (void *) new->start
)) {
159 printk(KERN_ERR
"*** original memory node:\n");
160 for (i
= 0; i
<= b
->nsets
; i
++)
161 dump_bset(b
, b
->sets
[i
].data
);
163 printk(KERN_ERR
"*** sorted memory node:\n");
166 printk(KERN_ERR
"*** on disk node:\n");
167 dump_bset(v
, v
->sets
[0].data
);
169 for (j
= 0; j
< new->keys
; j
++)
170 if (new->d
[j
] != v
->sets
[0].data
->d
[j
])
174 panic("verify failed at %u\n", j
);
177 mutex_unlock(&b
->c
->verify_lock
);
180 static void data_verify_endio(struct bio
*bio
, int error
)
182 struct closure
*cl
= bio
->bi_private
;
186 void bch_data_verify(struct search
*s
)
188 char name
[BDEVNAME_SIZE
];
189 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
190 struct closure
*cl
= &s
->cl
;
195 if (!s
->unaligned_bvec
)
196 bio_for_each_segment(bv
, s
->orig_bio
, i
)
197 bv
->bv_offset
= 0, bv
->bv_len
= PAGE_SIZE
;
199 check
= bio_clone(s
->orig_bio
, GFP_NOIO
);
203 if (bch_bio_alloc_pages(check
, GFP_NOIO
))
206 check
->bi_rw
= READ_SYNC
;
207 check
->bi_private
= cl
;
208 check
->bi_end_io
= data_verify_endio
;
210 closure_bio_submit(check
, cl
, &dc
->disk
);
213 bio_for_each_segment(bv
, s
->orig_bio
, i
) {
214 void *p1
= kmap(bv
->bv_page
);
215 void *p2
= kmap(check
->bi_io_vec
[i
].bv_page
);
217 if (memcmp(p1
+ bv
->bv_offset
,
221 "bcache (%s): verify failed at sector %llu\n",
222 bdevname(dc
->bdev
, name
),
223 (uint64_t) s
->orig_bio
->bi_sector
);
226 kunmap(check
->bi_io_vec
[i
].bv_page
);
229 __bio_for_each_segment(bv
, check
, i
, 0)
230 __free_page(bv
->bv_page
);
237 #ifdef CONFIG_BCACHE_EDEBUG
239 unsigned bch_count_data(struct btree
*b
)
242 struct btree_iter iter
;
246 for_each_key(b
, k
, &iter
)
251 static void vdump_bucket_and_panic(struct btree
*b
, const char *fmt
,
258 for (i
= 0; i
<= b
->nsets
; i
++)
259 dump_bset(b
, b
->sets
[i
].data
);
265 panic("at %s\n", pbtree(b
));
268 void bch_check_key_order_msg(struct btree
*b
, struct bset
*i
,
269 const char *fmt
, ...)
276 for (k
= i
->start
; bkey_next(k
) < end(i
); k
= bkey_next(k
))
277 if (skipped_backwards(b
, k
)) {
281 vdump_bucket_and_panic(b
, fmt
, args
);
286 void bch_check_keys(struct btree
*b
, const char *fmt
, ...)
289 struct bkey
*k
, *p
= NULL
;
290 struct btree_iter iter
;
295 for_each_key(b
, k
, &iter
) {
296 if (p
&& bkey_cmp(&START_KEY(p
), &START_KEY(k
)) > 0) {
297 printk(KERN_ERR
"Keys out of order:\n");
301 if (bch_ptr_invalid(b
, k
))
304 if (p
&& bkey_cmp(p
, &START_KEY(k
)) > 0) {
305 printk(KERN_ERR
"Overlapping keys:\n");
313 vdump_bucket_and_panic(b
, fmt
, args
);
319 #ifdef CONFIG_DEBUG_FS
321 /* XXX: cache set refcounting */
323 struct dump_iterator
{
330 static bool dump_pred(struct keybuf
*buf
, struct bkey
*k
)
335 static ssize_t
bch_dump_read(struct file
*file
, char __user
*buf
,
336 size_t size
, loff_t
*ppos
)
338 struct dump_iterator
*i
= file
->private_data
;
342 struct keybuf_key
*w
;
343 unsigned bytes
= min(i
->bytes
, size
);
345 int err
= copy_to_user(buf
, i
->buf
, bytes
);
353 memmove(i
->buf
, i
->buf
+ bytes
, i
->bytes
);
358 w
= bch_keybuf_next_rescan(i
->c
, &i
->keys
, &MAX_KEY
);
362 i
->bytes
= snprintf(i
->buf
, PAGE_SIZE
, "%s\n", pkey(&w
->key
));
363 bch_keybuf_del(&i
->keys
, w
);
369 static int bch_dump_open(struct inode
*inode
, struct file
*file
)
371 struct cache_set
*c
= inode
->i_private
;
372 struct dump_iterator
*i
;
374 i
= kzalloc(sizeof(struct dump_iterator
), GFP_KERNEL
);
378 file
->private_data
= i
;
380 bch_keybuf_init(&i
->keys
, dump_pred
);
381 i
->keys
.last_scanned
= KEY(0, 0, 0);
386 static int bch_dump_release(struct inode
*inode
, struct file
*file
)
388 kfree(file
->private_data
);
392 static const struct file_operations cache_set_debug_ops
= {
393 .owner
= THIS_MODULE
,
394 .open
= bch_dump_open
,
395 .read
= bch_dump_read
,
396 .release
= bch_dump_release
399 void bch_debug_init_cache_set(struct cache_set
*c
)
401 if (!IS_ERR_OR_NULL(debug
)) {
403 snprintf(name
, 50, "bcache-%pU", c
->sb
.set_uuid
);
405 c
->debug
= debugfs_create_file(name
, 0400, debug
, c
,
406 &cache_set_debug_ops
);
412 /* Fuzz tester has rotted: */
415 static ssize_t
btree_fuzz(struct kobject
*k
, struct kobj_attribute
*a
,
416 const char *buffer
, size_t size
)
418 void dump(struct btree
*b
)
422 for (i
= b
->sets
[0].data
;
423 index(i
, b
) < btree_blocks(b
) &&
424 i
->seq
== b
->sets
[0].data
->seq
;
425 i
= ((void *) i
) + set_blocks(i
, b
->c
) * block_bytes(b
->c
))
431 struct btree
*all
[3], *b
, *fill
, *orig
;
435 bch_btree_op_init_stack(&op
);
437 sb
= kzalloc(sizeof(struct cache_sb
), GFP_KERNEL
);
441 sb
->bucket_size
= 128;
444 c
= bch_cache_set_alloc(sb
);
448 for (j
= 0; j
< 3; j
++) {
449 BUG_ON(list_empty(&c
->btree_cache
));
450 all
[j
] = list_first_entry(&c
->btree_cache
, struct btree
, list
);
451 list_del_init(&all
[j
]->list
);
453 all
[j
]->key
= KEY(0, 0, c
->sb
.bucket_size
);
454 bkey_copy_key(&all
[j
]->key
, &MAX_KEY
);
462 for (j
= 0; j
< 3; j
++)
463 all
[j
]->written
= all
[j
]->nsets
= 0;
465 bch_bset_init_next(b
);
468 struct bset
*i
= write_block(b
);
469 struct bkey
*k
= op
.keys
.top
;
473 rand
= get_random_int();
480 SET_KEY_SIZE(k
, bucket_remainder(c
, rand
));
481 rand
>>= c
->bucket_bits
;
482 rand
&= 1024 * 512 - 1;
483 rand
+= c
->sb
.bucket_size
;
484 SET_KEY_OFFSET(k
, rand
);
488 bch_keylist_push(&op
.keys
);
489 bch_btree_insert_keys(b
, &op
);
491 if (should_split(b
) ||
492 set_blocks(i
, b
->c
) !=
493 __set_blocks(i
, i
->keys
+ 15, b
->c
)) {
494 i
->csum
= csum_set(i
);
496 memcpy(write_block(fill
),
499 b
->written
+= set_blocks(i
, b
->c
);
500 fill
->written
= b
->written
;
501 if (b
->written
== btree_blocks(b
))
504 bch_btree_sort_lazy(b
);
505 bch_bset_init_next(b
);
509 memcpy(orig
->sets
[0].data
,
515 bch_btree_read_done(&fill
->io
.cl
);
517 if (b
->sets
[0].data
->keys
!= fill
->sets
[0].data
->keys
||
518 memcmp(b
->sets
[0].data
->start
,
519 fill
->sets
[0].data
->start
,
520 b
->sets
[0].data
->keys
* sizeof(uint64_t))) {
521 struct bset
*i
= b
->sets
[0].data
;
525 l
= fill
->sets
[0].data
->start
;
527 k
= bkey_next(k
), l
= bkey_next(l
))
528 if (bkey_cmp(k
, l
) ||
529 KEY_SIZE(k
) != KEY_SIZE(l
))
530 pr_err("key %zi differs: %s != %s",
531 (uint64_t *) k
- i
->d
,
534 for (j
= 0; j
< 3; j
++) {
535 pr_err("**** Set %i ****", j
);
541 pr_info("fuzz complete: %i keys", b
->sets
[0].data
->keys
);
545 kobj_attribute_write(fuzz
, btree_fuzz
);
548 void bch_debug_exit(void)
550 if (!IS_ERR_OR_NULL(debug
))
551 debugfs_remove_recursive(debug
);
554 int __init
bch_debug_init(struct kobject
*kobj
)
558 ret
= sysfs_create_file(kobj
, &ksysfs_fuzz
.attr
);
563 debug
= debugfs_create_dir("bcache", NULL
);