1 // SPDX-License-Identifier: GPL-2.0
3 * Assorted bcachefs debug code
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
10 #include "bkey_methods.h"
11 #include "btree_cache.h"
13 #include "btree_iter.h"
14 #include "btree_update.h"
24 #include <linux/console.h>
25 #include <linux/debugfs.h>
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #include <linux/seq_file.h>
30 static struct dentry *bch_debug;
32 #ifdef CONFIG_BCACHEFS_DEBUG
34 void __bch2_btree_verify(struct bch_fs *c, struct btree *b)
36 struct btree *v = c->verify_data;
37 struct btree_node *n_ondisk, *n_sorted, *n_inmemory;
38 struct bset *sorted, *inmemory;
39 struct extent_ptr_decoded pick;
43 if (c->opts.nochanges)
46 btree_node_io_lock(b);
47 mutex_lock(&c->verify_lock);
49 n_ondisk = c->verify_ondisk;
50 n_sorted = c->verify_data->data;
53 bkey_copy(&v->key, &b->key);
56 v->btree_id = b->btree_id;
57 bch2_btree_keys_init(v, &c->expensive_debug_checks);
59 if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key),
63 ca = bch_dev_bkey_exists(c, pick.ptr.dev);
64 if (!bch2_dev_get_ioref(ca, READ))
67 bio = bio_alloc_bioset(GFP_NOIO,
68 buf_pages(n_sorted, btree_bytes(c)),
70 bio_set_dev(bio, ca->disk_sb.bdev);
71 bio->bi_opf = REQ_OP_READ|REQ_META;
72 bio->bi_iter.bi_sector = pick.ptr.offset;
73 bch2_bio_map(bio, n_sorted, btree_bytes(c));
78 percpu_ref_put(&ca->io_ref);
80 memcpy(n_ondisk, n_sorted, btree_bytes(c));
82 if (bch2_btree_node_read_done(c, v, false))
85 n_sorted = c->verify_data->data;
86 sorted = &n_sorted->keys;
87 inmemory = &n_inmemory->keys;
89 if (inmemory->u64s != sorted->u64s ||
90 memcmp(inmemory->start,
92 vstruct_end(inmemory) - (void *) inmemory->start)) {
93 unsigned offset = 0, sectors;
99 printk(KERN_ERR "*** in memory:\n");
100 bch2_dump_bset(b, inmemory, 0);
102 printk(KERN_ERR "*** read back in:\n");
103 bch2_dump_bset(v, sorted, 0);
105 while (offset < b->written) {
108 sectors = vstruct_blocks(n_ondisk, c->block_bits) <<
111 struct btree_node_entry *bne =
112 (void *) n_ondisk + (offset << 9);
115 sectors = vstruct_blocks(bne, c->block_bits) <<
119 printk(KERN_ERR "*** on disk block %u:\n", offset);
120 bch2_dump_bset(b, i, offset);
125 printk(KERN_ERR "*** block %u/%u not written\n",
126 offset >> c->block_bits, btree_blocks(c));
128 for (j = 0; j < le16_to_cpu(inmemory->u64s); j++)
129 if (inmemory->_data[j] != sorted->_data[j])
132 printk(KERN_ERR "b->written %u\n", b->written);
135 panic("verify failed at %u\n", j);
138 mutex_unlock(&c->verify_lock);
139 btree_node_io_unlock(b);
144 #ifdef CONFIG_DEBUG_FS
146 /* XXX: bch_fs refcounting */
154 size_t bytes; /* what's currently in buf */
156 char __user *ubuf; /* destination user buffer */
157 size_t size; /* size of requested read */
158 ssize_t ret; /* bytes read so far */
161 static int flush_buf(struct dump_iter *i)
164 size_t bytes = min(i->bytes, i->size);
165 int err = copy_to_user(i->ubuf, i->buf, bytes);
174 memmove(i->buf, i->buf + bytes, i->bytes);
180 static int bch2_dump_open(struct inode *inode, struct file *file)
182 struct btree_debug *bd = inode->i_private;
185 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
189 file->private_data = i;
191 i->c = container_of(bd, struct bch_fs, btree_debug[bd->id]);
197 static int bch2_dump_release(struct inode *inode, struct file *file)
199 kfree(file->private_data);
203 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
204 size_t size, loff_t *ppos)
206 struct dump_iter *i = file->private_data;
207 struct btree_trans trans;
208 struct btree_iter *iter;
223 bch2_trans_init(&trans, i->c, 0, 0);
225 iter = bch2_trans_get_iter(&trans, i->id, i->from, BTREE_ITER_PREFETCH);
226 k = bch2_btree_iter_peek(iter);
228 while (k.k && !(err = bkey_err(k))) {
229 bch2_bkey_val_to_text(&PBUF(i->buf), i->c, k);
230 i->bytes = strlen(i->buf);
231 BUG_ON(i->bytes >= PAGE_SIZE);
232 i->buf[i->bytes] = '\n';
235 k = bch2_btree_iter_next(iter);
245 bch2_trans_exit(&trans);
247 return err < 0 ? err : i->ret;
250 static const struct file_operations btree_debug_ops = {
251 .owner = THIS_MODULE,
252 .open = bch2_dump_open,
253 .release = bch2_dump_release,
254 .read = bch2_read_btree,
257 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
258 size_t size, loff_t *ppos)
260 struct dump_iter *i = file->private_data;
261 struct btree_trans trans;
262 struct btree_iter *iter;
274 if (!i->size || !bkey_cmp(POS_MAX, i->from))
277 bch2_trans_init(&trans, i->c, 0, 0);
279 for_each_btree_node(&trans, iter, i->id, i->from, 0, b) {
280 bch2_btree_node_to_text(&PBUF(i->buf), i->c, b);
281 i->bytes = strlen(i->buf);
287 * can't easily correctly restart a btree node traversal across
290 i->from = bkey_cmp(POS_MAX, b->key.k.p)
291 ? bkey_successor(b->key.k.p)
297 bch2_trans_exit(&trans);
299 return err < 0 ? err : i->ret;
302 static const struct file_operations btree_format_debug_ops = {
303 .owner = THIS_MODULE,
304 .open = bch2_dump_open,
305 .release = bch2_dump_release,
306 .read = bch2_read_btree_formats,
309 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
310 size_t size, loff_t *ppos)
312 struct dump_iter *i = file->private_data;
313 struct btree_trans trans;
314 struct btree_iter *iter;
316 struct btree *prev_node = NULL;
330 bch2_trans_init(&trans, i->c, 0, 0);
332 iter = bch2_trans_get_iter(&trans, i->id, i->from, BTREE_ITER_PREFETCH);
334 while ((k = bch2_btree_iter_peek(iter)).k &&
335 !(err = bkey_err(k))) {
336 struct btree_iter_level *l = &iter->l[0];
337 struct bkey_packed *_k =
338 bch2_btree_node_iter_peek(&l->iter, l->b);
340 if (l->b != prev_node) {
341 bch2_btree_node_to_text(&PBUF(i->buf), i->c, l->b);
342 i->bytes = strlen(i->buf);
349 bch2_bfloat_to_text(&PBUF(i->buf), l->b, _k);
350 i->bytes = strlen(i->buf);
355 bch2_btree_iter_next(iter);
365 bch2_trans_exit(&trans);
367 return err < 0 ? err : i->ret;
370 static const struct file_operations bfloat_failed_debug_ops = {
371 .owner = THIS_MODULE,
372 .open = bch2_dump_open,
373 .release = bch2_dump_release,
374 .read = bch2_read_bfloat_failed,
377 void bch2_fs_debug_exit(struct bch_fs *c)
379 if (!IS_ERR_OR_NULL(c->debug))
380 debugfs_remove_recursive(c->debug);
383 void bch2_fs_debug_init(struct bch_fs *c)
385 struct btree_debug *bd;
388 if (IS_ERR_OR_NULL(bch_debug))
391 snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
392 c->debug = debugfs_create_dir(name, bch_debug);
393 if (IS_ERR_OR_NULL(c->debug))
396 for (bd = c->btree_debug;
397 bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
399 bd->id = bd - c->btree_debug;
400 bd->btree = debugfs_create_file(bch2_btree_ids[bd->id],
404 snprintf(name, sizeof(name), "%s-formats",
405 bch2_btree_ids[bd->id]);
407 bd->btree_format = debugfs_create_file(name, 0400, c->debug, bd,
408 &btree_format_debug_ops);
410 snprintf(name, sizeof(name), "%s-bfloat-failed",
411 bch2_btree_ids[bd->id]);
413 bd->failed = debugfs_create_file(name, 0400, c->debug, bd,
414 &bfloat_failed_debug_ops);
420 void bch2_debug_exit(void)
422 if (!IS_ERR_OR_NULL(bch_debug))
423 debugfs_remove_recursive(bch_debug);
426 int __init bch2_debug_init(void)
430 bch_debug = debugfs_create_dir("bcachefs", NULL);