2 * Assorted bcachefs debug code
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
9 #include "bkey_methods.h"
10 #include "btree_cache.h"
12 #include "btree_iter.h"
13 #include "btree_update.h"
23 #include <linux/console.h>
24 #include <linux/debugfs.h>
25 #include <linux/module.h>
26 #include <linux/random.h>
27 #include <linux/seq_file.h>
29 static struct dentry *bch_debug;
31 #ifdef CONFIG_BCACHEFS_DEBUG
33 void __bch2_btree_verify(struct bch_fs *c, struct btree *b)
35 struct btree *v = c->verify_data;
36 struct btree_node *n_ondisk, *n_sorted, *n_inmemory;
37 struct bset *sorted, *inmemory;
38 struct extent_pick_ptr pick;
41 if (c->opts.nochanges)
44 btree_node_io_lock(b);
45 mutex_lock(&c->verify_lock);
47 n_ondisk = c->verify_ondisk;
48 n_sorted = c->verify_data->data;
51 bkey_copy(&v->key, &b->key);
54 v->btree_id = b->btree_id;
55 bch2_btree_keys_init(v, &c->expensive_debug_checks);
57 pick = bch2_btree_pick_ptr(c, b, NULL);
58 if (IS_ERR_OR_NULL(pick.ca))
61 bio = bio_alloc_bioset(GFP_NOIO, btree_pages(c), &c->btree_bio);
62 bio_set_dev(bio, pick.ca->disk_sb.bdev);
63 bio->bi_opf = REQ_OP_READ|REQ_META;
64 bio->bi_iter.bi_sector = pick.ptr.offset;
65 bio->bi_iter.bi_size = btree_bytes(c);
66 bch2_bio_map(bio, n_sorted);
71 percpu_ref_put(&pick.ca->io_ref);
73 memcpy(n_ondisk, n_sorted, btree_bytes(c));
75 if (bch2_btree_node_read_done(c, v, false))
78 n_sorted = c->verify_data->data;
79 sorted = &n_sorted->keys;
80 inmemory = &n_inmemory->keys;
82 if (inmemory->u64s != sorted->u64s ||
83 memcmp(inmemory->start,
85 vstruct_end(inmemory) - (void *) inmemory->start)) {
86 unsigned offset = 0, sectors;
92 printk(KERN_ERR "*** in memory:\n");
93 bch2_dump_bset(b, inmemory, 0);
95 printk(KERN_ERR "*** read back in:\n");
96 bch2_dump_bset(v, sorted, 0);
98 while (offset < b->written) {
101 sectors = vstruct_blocks(n_ondisk, c->block_bits) <<
104 struct btree_node_entry *bne =
105 (void *) n_ondisk + (offset << 9);
108 sectors = vstruct_blocks(bne, c->block_bits) <<
112 printk(KERN_ERR "*** on disk block %u:\n", offset);
113 bch2_dump_bset(b, i, offset);
118 printk(KERN_ERR "*** block %u/%u not written\n",
119 offset >> c->block_bits, btree_blocks(c));
121 for (j = 0; j < le16_to_cpu(inmemory->u64s); j++)
122 if (inmemory->_data[j] != sorted->_data[j])
125 printk(KERN_ERR "b->written %u\n", b->written);
128 panic("verify failed at %u\n", j);
131 mutex_unlock(&c->verify_lock);
132 btree_node_io_unlock(b);
137 #ifdef CONFIG_DEBUG_FS
139 /* XXX: bch_fs refcounting */
147 size_t bytes; /* what's currently in buf */
149 char __user *ubuf; /* destination user buffer */
150 size_t size; /* size of requested read */
151 ssize_t ret; /* bytes read so far */
154 static int flush_buf(struct dump_iter *i)
157 size_t bytes = min(i->bytes, i->size);
158 int err = copy_to_user(i->ubuf, i->buf, bytes);
167 memmove(i->buf, i->buf + bytes, i->bytes);
173 static int bch2_dump_open(struct inode *inode, struct file *file)
175 struct btree_debug *bd = inode->i_private;
178 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
182 file->private_data = i;
184 i->c = container_of(bd, struct bch_fs, btree_debug[bd->id]);
190 static int bch2_dump_release(struct inode *inode, struct file *file)
192 kfree(file->private_data);
196 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
197 size_t size, loff_t *ppos)
199 struct dump_iter *i = file->private_data;
200 struct btree_iter iter;
215 for_each_btree_key(&iter, i->c, i->id, i->from,
216 BTREE_ITER_PREFETCH, k) {
219 bch2_bkey_val_to_text(i->c, bkey_type(0, i->id),
220 i->buf, sizeof(i->buf), k);
221 i->bytes = strlen(i->buf);
222 BUG_ON(i->bytes >= PAGE_SIZE);
223 i->buf[i->bytes] = '\n';
233 err = bch2_btree_iter_unlock(&iter) ?: err;
235 return err < 0 ? err : i->ret;
238 static const struct file_operations btree_debug_ops = {
239 .owner = THIS_MODULE,
240 .open = bch2_dump_open,
241 .release = bch2_dump_release,
242 .read = bch2_read_btree,
245 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
246 size_t size, loff_t *ppos)
248 struct dump_iter *i = file->private_data;
249 struct btree_iter iter;
261 if (!i->size || !bkey_cmp(POS_MAX, i->from))
264 for_each_btree_node(&iter, i->c, i->id, i->from, 0, b) {
265 i->bytes = bch2_print_btree_node(i->c, b, i->buf,
272 * can't easily correctly restart a btree node traversal across
275 i->from = bkey_cmp(POS_MAX, b->key.k.p)
276 ? bkey_successor(b->key.k.p)
282 bch2_btree_iter_unlock(&iter);
284 return err < 0 ? err : i->ret;
287 static const struct file_operations btree_format_debug_ops = {
288 .owner = THIS_MODULE,
289 .open = bch2_dump_open,
290 .release = bch2_dump_release,
291 .read = bch2_read_btree_formats,
294 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
295 size_t size, loff_t *ppos)
297 struct dump_iter *i = file->private_data;
298 struct btree_iter iter;
300 struct btree *prev_node = NULL;
314 bch2_btree_iter_init(&iter, i->c, i->id, i->from, BTREE_ITER_PREFETCH);
316 while ((k = bch2_btree_iter_peek(&iter)).k &&
317 !(err = btree_iter_err(k))) {
318 struct btree_iter_level *l = &iter.l[0];
319 struct bkey_packed *_k =
320 bch2_btree_node_iter_peek(&l->iter, l->b);
322 if (l->b != prev_node) {
323 i->bytes = bch2_print_btree_node(i->c, l->b, i->buf,
331 i->bytes = bch2_bkey_print_bfloat(l->b, _k, i->buf,
338 bch2_btree_iter_next(&iter);
348 bch2_btree_iter_unlock(&iter);
350 return err < 0 ? err : i->ret;
353 static const struct file_operations bfloat_failed_debug_ops = {
354 .owner = THIS_MODULE,
355 .open = bch2_dump_open,
356 .release = bch2_dump_release,
357 .read = bch2_read_bfloat_failed,
360 void bch2_fs_debug_exit(struct bch_fs *c)
362 if (!IS_ERR_OR_NULL(c->debug))
363 debugfs_remove_recursive(c->debug);
366 void bch2_fs_debug_init(struct bch_fs *c)
368 struct btree_debug *bd;
371 if (IS_ERR_OR_NULL(bch_debug))
374 snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
375 c->debug = debugfs_create_dir(name, bch_debug);
376 if (IS_ERR_OR_NULL(c->debug))
379 for (bd = c->btree_debug;
380 bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
382 bd->id = bd - c->btree_debug;
383 bd->btree = debugfs_create_file(bch2_btree_ids[bd->id],
387 snprintf(name, sizeof(name), "%s-formats",
388 bch2_btree_ids[bd->id]);
390 bd->btree_format = debugfs_create_file(name, 0400, c->debug, bd,
391 &btree_format_debug_ops);
393 snprintf(name, sizeof(name), "%s-bfloat-failed",
394 bch2_btree_ids[bd->id]);
396 bd->failed = debugfs_create_file(name, 0400, c->debug, bd,
397 &bfloat_failed_debug_ops);
403 void bch2_debug_exit(void)
405 if (!IS_ERR_OR_NULL(bch_debug))
406 debugfs_remove_recursive(bch_debug);
409 int __init bch2_debug_init(void)
413 bch_debug = debugfs_create_dir("bcachefs", NULL);