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/pretty-printers.h>
28 #include <linux/random.h>
29 #include <linux/seq_file.h>
31 static struct dentry *bch_debug;
33 static bool bch2_btree_verify_replica(struct bch_fs *c, struct btree *b,
34 struct extent_ptr_decoded pick)
36 struct btree *v = c->verify_data;
37 struct btree_node *n_ondisk = c->verify_ondisk;
38 struct btree_node *n_sorted = c->verify_data->data;
39 struct bset *sorted, *inmemory = &b->data->keys;
40 struct bch_dev *ca = bch_dev_bkey_exists(c, pick.ptr.dev);
44 if (!bch2_dev_get_ioref(ca, READ))
47 bio = bio_alloc_bioset(ca->disk_sb.bdev,
48 buf_pages(n_sorted, btree_bytes(c)),
52 bio->bi_iter.bi_sector = pick.ptr.offset;
53 bch2_bio_map(bio, n_sorted, btree_bytes(c));
58 percpu_ref_put(&ca->io_ref);
60 memcpy(n_ondisk, n_sorted, btree_bytes(c));
63 if (bch2_btree_node_read_done(c, ca, v, false))
66 n_sorted = c->verify_data->data;
67 sorted = &n_sorted->keys;
69 if (inmemory->u64s != sorted->u64s ||
70 memcmp(inmemory->start,
72 vstruct_end(inmemory) - (void *) inmemory->start)) {
73 unsigned offset = 0, sectors;
79 printk(KERN_ERR "*** in memory:\n");
80 bch2_dump_bset(c, b, inmemory, 0);
82 printk(KERN_ERR "*** read back in:\n");
83 bch2_dump_bset(c, v, sorted, 0);
85 while (offset < v->written) {
88 sectors = vstruct_blocks(n_ondisk, c->block_bits) <<
91 struct btree_node_entry *bne =
92 (void *) n_ondisk + (offset << 9);
95 sectors = vstruct_blocks(bne, c->block_bits) <<
99 printk(KERN_ERR "*** on disk block %u:\n", offset);
100 bch2_dump_bset(c, b, i, offset);
105 for (j = 0; j < le16_to_cpu(inmemory->u64s); j++)
106 if (inmemory->_data[j] != sorted->_data[j])
110 bch_err(c, "verify failed at key %u", j);
115 if (v->written != b->written) {
116 bch_err(c, "written wrong: expected %u, got %u",
117 b->written, v->written);
124 void __bch2_btree_verify(struct bch_fs *c, struct btree *b)
126 struct bkey_ptrs_c ptrs;
127 struct extent_ptr_decoded p;
128 const union bch_extent_entry *entry;
130 struct bset *inmemory = &b->data->keys;
131 struct bkey_packed *k;
134 if (c->opts.nochanges)
137 bch2_btree_node_io_lock(b);
138 mutex_lock(&c->verify_lock);
140 if (!c->verify_ondisk) {
141 c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
142 if (!c->verify_ondisk)
146 if (!c->verify_data) {
147 c->verify_data = __bch2_btree_node_mem_alloc(c);
151 list_del_init(&c->verify_data->list);
154 BUG_ON(b->nsets != 1);
156 for (k = inmemory->start; k != vstruct_last(inmemory); k = bkey_next(k))
157 if (k->type == KEY_TYPE_btree_ptr_v2) {
158 struct bch_btree_ptr_v2 *v = (void *) bkeyp_val(&b->format, k);
163 bkey_copy(&v->key, &b->key);
164 v->c.level = b->c.level;
165 v->c.btree_id = b->c.btree_id;
166 bch2_btree_keys_init(v);
168 ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
169 bkey_for_each_ptr_decode(&b->key.k, ptrs, p, entry)
170 failed |= bch2_btree_verify_replica(c, b, p);
173 struct printbuf buf = PRINTBUF;
175 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
176 bch2_fs_fatal_error(c, "btree node verify failed for : %s\n", buf.buf);
180 mutex_unlock(&c->verify_lock);
181 bch2_btree_node_io_unlock(b);
184 #ifdef CONFIG_DEBUG_FS
186 /* XXX: bch_fs refcounting */
192 struct bpos prev_node;
197 char __user *ubuf; /* destination user buffer */
198 size_t size; /* size of requested read */
199 ssize_t ret; /* bytes read so far */
202 static int flush_buf(struct dump_iter *i)
205 size_t bytes = min_t(size_t, i->buf.pos, i->size);
206 int err = copy_to_user(i->ubuf, i->buf.buf, bytes);
215 memmove(i->buf.buf, i->buf.buf + bytes, i->buf.pos);
221 static int bch2_dump_open(struct inode *inode, struct file *file)
223 struct btree_debug *bd = inode->i_private;
226 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
230 file->private_data = i;
233 i->c = container_of(bd, struct bch_fs, btree_debug[bd->id]);
240 static int bch2_dump_release(struct inode *inode, struct file *file)
242 struct dump_iter *i = file->private_data;
244 printbuf_exit(&i->buf);
249 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
250 size_t size, loff_t *ppos)
252 struct dump_iter *i = file->private_data;
253 struct btree_trans trans;
254 struct btree_iter iter;
262 bch2_trans_init(&trans, i->c, 0, 0);
264 err = for_each_btree_key2(&trans, iter, i->id, i->from,
266 BTREE_ITER_ALL_SNAPSHOTS, k, ({
274 bch2_bkey_val_to_text(&i->buf, i->c, k);
275 prt_newline(&i->buf);
283 bch2_trans_exit(&trans);
285 return err ?: i->ret;
288 static const struct file_operations btree_debug_ops = {
289 .owner = THIS_MODULE,
290 .open = bch2_dump_open,
291 .release = bch2_dump_release,
292 .read = bch2_read_btree,
295 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
296 size_t size, loff_t *ppos)
298 struct dump_iter *i = file->private_data;
299 struct btree_trans trans;
300 struct btree_iter iter;
312 if (!i->size || !bpos_cmp(SPOS_MAX, i->from))
315 bch2_trans_init(&trans, i->c, 0, 0);
317 for_each_btree_node(&trans, iter, i->id, i->from, 0, b, err) {
318 bch2_btree_node_to_text(&i->buf, i->c, b);
324 * can't easily correctly restart a btree node traversal across
327 i->from = bpos_cmp(SPOS_MAX, b->key.k.p)
328 ? bpos_successor(b->key.k.p)
334 bch2_trans_iter_exit(&trans, &iter);
336 bch2_trans_exit(&trans);
338 return err < 0 ? err : i->ret;
341 static const struct file_operations btree_format_debug_ops = {
342 .owner = THIS_MODULE,
343 .open = bch2_dump_open,
344 .release = bch2_dump_release,
345 .read = bch2_read_btree_formats,
348 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
349 size_t size, loff_t *ppos)
351 struct dump_iter *i = file->private_data;
352 struct btree_trans trans;
353 struct btree_iter iter;
368 bch2_trans_init(&trans, i->c, 0, 0);
370 err = for_each_btree_key2(&trans, iter, i->id, i->from,
372 BTREE_ITER_ALL_SNAPSHOTS, k, ({
373 struct btree_path_level *l = &iter.path->l[0];
374 struct bkey_packed *_k =
375 bch2_btree_node_iter_peek(&l->iter, l->b);
384 if (bpos_cmp(l->b->key.k.p, i->prev_node) > 0) {
385 bch2_btree_node_to_text(&i->buf, i->c, l->b);
386 i->prev_node = l->b->key.k.p;
389 bch2_bfloat_to_text(&i->buf, l->b, _k);
397 bch2_trans_exit(&trans);
399 return err ?: i->ret;
402 static const struct file_operations bfloat_failed_debug_ops = {
403 .owner = THIS_MODULE,
404 .open = bch2_dump_open,
405 .release = bch2_dump_release,
406 .read = bch2_read_bfloat_failed,
409 static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
412 out->tabstops[0] = 32;
414 prt_printf(out, "%px btree=%s l=%u ",
416 bch2_btree_ids[b->c.btree_id],
420 printbuf_indent_add(out, 2);
422 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&b->key));
425 prt_printf(out, "flags: ");
427 prt_bitflags(out, bch2_btree_node_flags, b->flags);
430 prt_printf(out, "pcpu read locks: ");
432 prt_printf(out, "%u", b->c.lock.readers != NULL);
435 prt_printf(out, "written:");
437 prt_printf(out, "%u", b->written);
440 prt_printf(out, "writes blocked:");
442 prt_printf(out, "%u", !list_empty_careful(&b->write_blocked));
445 prt_printf(out, "will make reachable:");
447 prt_printf(out, "%lx", b->will_make_reachable);
450 prt_printf(out, "journal pin %px:", &b->writes[0].journal);
452 prt_printf(out, "%llu", b->writes[0].journal.seq);
455 prt_printf(out, "journal pin %px:", &b->writes[1].journal);
457 prt_printf(out, "%llu", b->writes[1].journal.seq);
460 printbuf_indent_sub(out, 2);
463 static ssize_t bch2_cached_btree_nodes_read(struct file *file, char __user *buf,
464 size_t size, loff_t *ppos)
466 struct dump_iter *i = file->private_data;
467 struct bch_fs *c = i->c;
476 struct bucket_table *tbl;
477 struct rhash_head *pos;
489 tbl = rht_dereference_rcu(c->btree_cache.table.tbl,
490 &c->btree_cache.table);
491 if (i->iter < tbl->size) {
492 rht_for_each_entry_rcu(b, pos, tbl, i->iter, hash)
493 bch2_cached_btree_node_to_text(&i->buf, c, b);
502 if (i->buf.allocation_failure)
508 static const struct file_operations cached_btree_nodes_ops = {
509 .owner = THIS_MODULE,
510 .open = bch2_dump_open,
511 .release = bch2_dump_release,
512 .read = bch2_cached_btree_nodes_read,
515 static int prt_backtrace(struct printbuf *out, struct task_struct *task)
517 unsigned long entries[32];
518 unsigned i, nr_entries;
521 ret = down_read_killable(&task->signal->exec_update_lock);
525 nr_entries = stack_trace_save_tsk(task, entries, ARRAY_SIZE(entries), 0);
526 for (i = 0; i < nr_entries; i++) {
527 prt_printf(out, "[<0>] %pB", (void *)entries[i]);
531 up_read(&task->signal->exec_update_lock);
535 static ssize_t bch2_btree_transactions_read(struct file *file, char __user *buf,
536 size_t size, loff_t *ppos)
538 struct dump_iter *i = file->private_data;
539 struct bch_fs *c = i->c;
540 struct btree_trans *trans;
547 mutex_lock(&c->btree_trans_lock);
548 list_for_each_entry(trans, &c->btree_trans_list, list) {
549 if (trans->task->pid <= i->iter)
559 bch2_btree_trans_to_text(&i->buf, trans);
561 prt_printf(&i->buf, "backtrace:");
562 prt_newline(&i->buf);
563 printbuf_indent_add(&i->buf, 2);
564 prt_backtrace(&i->buf, trans->task);
565 printbuf_indent_sub(&i->buf, 2);
566 prt_newline(&i->buf);
568 i->iter = trans->task->pid;
570 mutex_unlock(&c->btree_trans_lock);
572 if (i->buf.allocation_failure)
578 static const struct file_operations btree_transactions_ops = {
579 .owner = THIS_MODULE,
580 .open = bch2_dump_open,
581 .release = bch2_dump_release,
582 .read = bch2_btree_transactions_read,
585 static ssize_t bch2_journal_pins_read(struct file *file, char __user *buf,
586 size_t size, loff_t *ppos)
588 struct dump_iter *i = file->private_data;
589 struct bch_fs *c = i->c;
605 done = bch2_journal_seq_pins_to_text(&i->buf, &c->journal, &i->iter);
609 if (i->buf.allocation_failure)
615 static const struct file_operations journal_pins_ops = {
616 .owner = THIS_MODULE,
617 .open = bch2_dump_open,
618 .release = bch2_dump_release,
619 .read = bch2_journal_pins_read,
622 static int lock_held_stats_open(struct inode *inode, struct file *file)
624 struct bch_fs *c = inode->i_private;
627 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
635 file->private_data = i;
640 static int lock_held_stats_release(struct inode *inode, struct file *file)
642 struct dump_iter *i = file->private_data;
644 printbuf_exit(&i->buf);
650 static ssize_t lock_held_stats_read(struct file *file, char __user *buf,
651 size_t size, loff_t *ppos)
653 struct dump_iter *i = file->private_data;
654 struct lock_held_stats *lhs = &i->c->lock_held_stats;
661 while (lhs->names[i->iter] != 0 && i->iter < BCH_LOCK_TIME_NR) {
669 prt_printf(&i->buf, "%s:", lhs->names[i->iter]);
670 prt_newline(&i->buf);
671 printbuf_indent_add(&i->buf, 8);
672 bch2_time_stats_to_text(&i->buf, &lhs->times[i->iter]);
673 printbuf_indent_sub(&i->buf, 8);
674 prt_newline(&i->buf);
678 if (i->buf.allocation_failure)
684 static const struct file_operations lock_held_stats_op = {
685 .owner = THIS_MODULE,
686 .open = lock_held_stats_open,
687 .release = lock_held_stats_release,
688 .read = lock_held_stats_read,
691 void bch2_fs_debug_exit(struct bch_fs *c)
693 if (!IS_ERR_OR_NULL(c->fs_debug_dir))
694 debugfs_remove_recursive(c->fs_debug_dir);
697 void bch2_fs_debug_init(struct bch_fs *c)
699 struct btree_debug *bd;
702 if (IS_ERR_OR_NULL(bch_debug))
705 snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
706 c->fs_debug_dir = debugfs_create_dir(name, bch_debug);
707 if (IS_ERR_OR_NULL(c->fs_debug_dir))
710 debugfs_create_file("cached_btree_nodes", 0400, c->fs_debug_dir,
711 c->btree_debug, &cached_btree_nodes_ops);
713 debugfs_create_file("btree_transactions", 0400, c->fs_debug_dir,
714 c->btree_debug, &btree_transactions_ops);
716 debugfs_create_file("journal_pins", 0400, c->fs_debug_dir,
717 c->btree_debug, &journal_pins_ops);
719 if (IS_ENABLED(CONFIG_BCACHEFS_LOCK_TIME_STATS)) {
720 debugfs_create_file("lock_held_stats", 0400, c->fs_debug_dir,
721 c, &lock_held_stats_op);
724 c->btree_debug_dir = debugfs_create_dir("btrees", c->fs_debug_dir);
725 if (IS_ERR_OR_NULL(c->btree_debug_dir))
728 for (bd = c->btree_debug;
729 bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
731 bd->id = bd - c->btree_debug;
732 debugfs_create_file(bch2_btree_ids[bd->id],
733 0400, c->btree_debug_dir, bd,
736 snprintf(name, sizeof(name), "%s-formats",
737 bch2_btree_ids[bd->id]);
739 debugfs_create_file(name, 0400, c->btree_debug_dir, bd,
740 &btree_format_debug_ops);
742 snprintf(name, sizeof(name), "%s-bfloat-failed",
743 bch2_btree_ids[bd->id]);
745 debugfs_create_file(name, 0400, c->btree_debug_dir, bd,
746 &bfloat_failed_debug_ops);
752 void bch2_debug_exit(void)
754 if (!IS_ERR_OR_NULL(bch_debug))
755 debugfs_remove_recursive(bch_debug);
758 int __init bch2_debug_init(void)
762 bch_debug = debugfs_create_dir("bcachefs", NULL);