1 // SPDX-License-Identifier: GPL-2.0
3 * Assorted bcachefs debug code
6 * Copyright 2012 Google, Inc.
10 #include "bkey_methods.h"
11 #include "btree_cache.h"
13 #include "btree_iter.h"
14 #include "btree_locking.h"
15 #include "btree_update.h"
16 #include "btree_update_interior.h"
25 #include <linux/console.h>
26 #include <linux/debugfs.h>
27 #include <linux/module.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;
41 bool failed = false, saw_error = false;
43 struct bch_dev *ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ);
47 bio = bio_alloc_bioset(ca->disk_sb.bdev,
48 buf_pages(n_sorted, btree_buf_bytes(b)),
52 bio->bi_iter.bi_sector = pick.ptr.offset;
53 bch2_bio_map(bio, n_sorted, btree_buf_bytes(b));
58 percpu_ref_put(&ca->io_ref);
60 memcpy(n_ondisk, n_sorted, btree_buf_bytes(b));
63 if (bch2_btree_node_read_done(c, ca, v, false, &saw_error) || saw_error)
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 = kvmalloc(btree_buf_bytes(b), 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_p_next(k))
157 if (k->type == KEY_TYPE_btree_ptr_v2)
158 ((struct bch_btree_ptr_v2 *) bkeyp_val(&b->format, k))->mem_ptr = 0;
161 bkey_copy(&v->key, &b->key);
162 v->c.level = b->c.level;
163 v->c.btree_id = b->c.btree_id;
164 bch2_btree_keys_init(v);
166 ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
167 bkey_for_each_ptr_decode(&b->key.k, ptrs, p, entry)
168 failed |= bch2_btree_verify_replica(c, b, p);
171 struct printbuf buf = PRINTBUF;
173 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
174 bch2_fs_fatal_error(c, ": btree node verify failed for: %s\n", buf.buf);
178 mutex_unlock(&c->verify_lock);
179 bch2_btree_node_io_unlock(b);
182 void bch2_btree_node_ondisk_to_text(struct printbuf *out, struct bch_fs *c,
183 const struct btree *b)
185 struct btree_node *n_ondisk = NULL;
186 struct extent_ptr_decoded pick;
188 struct bio *bio = NULL;
192 if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), NULL, &pick) <= 0) {
193 prt_printf(out, "error getting device to read from: invalid device\n");
197 ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ);
199 prt_printf(out, "error getting device to read from: not online\n");
203 n_ondisk = kvmalloc(btree_buf_bytes(b), GFP_KERNEL);
205 prt_printf(out, "memory allocation failure\n");
209 bio = bio_alloc_bioset(ca->disk_sb.bdev,
210 buf_pages(n_ondisk, btree_buf_bytes(b)),
211 REQ_OP_READ|REQ_META,
214 bio->bi_iter.bi_sector = pick.ptr.offset;
215 bch2_bio_map(bio, n_ondisk, btree_buf_bytes(b));
217 ret = submit_bio_wait(bio);
219 prt_printf(out, "IO error reading btree node: %s\n", bch2_err_str(ret));
223 while (offset < btree_sectors(c)) {
226 struct bch_csum csum;
227 struct bkey_packed *k;
233 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i))) {
234 prt_printf(out, "unknown checksum type at offset %u: %llu\n",
235 offset, BSET_CSUM_TYPE(i));
239 nonce = btree_nonce(i, offset << 9);
240 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, n_ondisk);
242 if (bch2_crc_cmp(csum, n_ondisk->csum)) {
243 prt_printf(out, "invalid checksum\n");
247 bset_encrypt(c, i, offset << 9);
249 sectors = vstruct_sectors(n_ondisk, c->block_bits);
251 struct btree_node_entry *bne = (void *) n_ondisk + (offset << 9);
255 if (i->seq != n_ondisk->keys.seq)
258 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i))) {
259 prt_printf(out, "unknown checksum type at offset %u: %llu\n",
260 offset, BSET_CSUM_TYPE(i));
264 nonce = btree_nonce(i, offset << 9);
265 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);
267 if (bch2_crc_cmp(csum, bne->csum)) {
268 prt_printf(out, "invalid checksum");
272 bset_encrypt(c, i, offset << 9);
274 sectors = vstruct_sectors(bne, c->block_bits);
277 prt_printf(out, " offset %u version %u, journal seq %llu\n",
279 le16_to_cpu(i->version),
280 le64_to_cpu(i->journal_seq));
283 printbuf_indent_add(out, 4);
285 for (k = i->start; k != vstruct_last(i); k = bkey_p_next(k)) {
288 bch2_bkey_val_to_text(out, c, bkey_disassemble(b, k, &u));
292 printbuf_indent_sub(out, 4);
298 percpu_ref_put(&ca->io_ref);
301 #ifdef CONFIG_DEBUG_FS
303 /* XXX: bch_fs refcounting */
309 struct bpos prev_node;
314 char __user *ubuf; /* destination user buffer */
315 size_t size; /* size of requested read */
316 ssize_t ret; /* bytes read so far */
319 static ssize_t flush_buf(struct dump_iter *i)
322 size_t bytes = min_t(size_t, i->buf.pos, i->size);
323 int copied = bytes - copy_to_user(i->ubuf, i->buf.buf, bytes);
328 i->buf.pos -= copied;
329 memmove(i->buf.buf, i->buf.buf + copied, i->buf.pos);
335 return i->size ? 0 : i->ret;
338 static int bch2_dump_open(struct inode *inode, struct file *file)
340 struct btree_debug *bd = inode->i_private;
343 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
347 file->private_data = i;
350 i->c = container_of(bd, struct bch_fs, btree_debug[bd->id]);
357 static int bch2_dump_release(struct inode *inode, struct file *file)
359 struct dump_iter *i = file->private_data;
361 printbuf_exit(&i->buf);
366 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
367 size_t size, loff_t *ppos)
369 struct dump_iter *i = file->private_data;
375 return flush_buf(i) ?:
377 for_each_btree_key(trans, iter, i->id, i->from,
379 BTREE_ITER_all_snapshots, k, ({
380 bch2_bkey_val_to_text(&i->buf, i->c, k);
381 prt_newline(&i->buf);
382 bch2_trans_unlock(trans);
383 i->from = bpos_successor(iter.pos);
389 static const struct file_operations btree_debug_ops = {
390 .owner = THIS_MODULE,
391 .open = bch2_dump_open,
392 .release = bch2_dump_release,
393 .read = bch2_read_btree,
396 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
397 size_t size, loff_t *ppos)
399 struct dump_iter *i = file->private_data;
400 struct btree_trans *trans;
401 struct btree_iter iter;
413 if (bpos_eq(SPOS_MAX, i->from))
416 trans = bch2_trans_get(i->c);
418 bch2_trans_begin(trans);
420 for_each_btree_node(trans, iter, i->id, i->from, 0, b, ret) {
421 bch2_btree_node_to_text(&i->buf, i->c, b);
422 i->from = !bpos_eq(SPOS_MAX, b->key.k.p)
423 ? bpos_successor(b->key.k.p)
426 ret = drop_locks_do(trans, flush_buf(i));
430 bch2_trans_iter_exit(trans, &iter);
432 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
435 bch2_trans_put(trans);
440 return ret ?: i->ret;
443 static const struct file_operations btree_format_debug_ops = {
444 .owner = THIS_MODULE,
445 .open = bch2_dump_open,
446 .release = bch2_dump_release,
447 .read = bch2_read_btree_formats,
450 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
451 size_t size, loff_t *ppos)
453 struct dump_iter *i = file->private_data;
459 return flush_buf(i) ?:
461 for_each_btree_key(trans, iter, i->id, i->from,
463 BTREE_ITER_all_snapshots, k, ({
464 struct btree_path_level *l =
465 &btree_iter_path(trans, &iter)->l[0];
466 struct bkey_packed *_k =
467 bch2_btree_node_iter_peek(&l->iter, l->b);
469 if (bpos_gt(l->b->key.k.p, i->prev_node)) {
470 bch2_btree_node_to_text(&i->buf, i->c, l->b);
471 i->prev_node = l->b->key.k.p;
474 bch2_bfloat_to_text(&i->buf, l->b, _k);
475 bch2_trans_unlock(trans);
476 i->from = bpos_successor(iter.pos);
482 static const struct file_operations bfloat_failed_debug_ops = {
483 .owner = THIS_MODULE,
484 .open = bch2_dump_open,
485 .release = bch2_dump_release,
486 .read = bch2_read_bfloat_failed,
489 static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
492 if (!out->nr_tabstops)
493 printbuf_tabstop_push(out, 32);
495 prt_printf(out, "%px btree=%s l=%u\n", b, bch2_btree_id_str(b->c.btree_id), b->c.level);
497 printbuf_indent_add(out, 2);
499 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&b->key));
502 prt_printf(out, "flags:\t");
503 prt_bitflags(out, bch2_btree_node_flags, b->flags);
506 prt_printf(out, "pcpu read locks:\t%u\n", b->c.lock.readers != NULL);
507 prt_printf(out, "written:\t%u\n", b->written);
508 prt_printf(out, "writes blocked:\t%u\n", !list_empty_careful(&b->write_blocked));
509 prt_printf(out, "will make reachable:\t%lx\n", b->will_make_reachable);
511 prt_printf(out, "journal pin %px:\t%llu\n",
512 &b->writes[0].journal, b->writes[0].journal.seq);
513 prt_printf(out, "journal pin %px:\t%llu\n",
514 &b->writes[1].journal, b->writes[1].journal.seq);
516 printbuf_indent_sub(out, 2);
519 static ssize_t bch2_cached_btree_nodes_read(struct file *file, char __user *buf,
520 size_t size, loff_t *ppos)
522 struct dump_iter *i = file->private_data;
523 struct bch_fs *c = i->c;
532 struct bucket_table *tbl;
533 struct rhash_head *pos;
542 tbl = rht_dereference_rcu(c->btree_cache.table.tbl,
543 &c->btree_cache.table);
544 if (i->iter < tbl->size) {
545 rht_for_each_entry_rcu(b, pos, tbl, i->iter, hash)
546 bch2_cached_btree_node_to_text(&i->buf, c, b);
555 if (i->buf.allocation_failure)
561 return ret ?: i->ret;
564 static const struct file_operations cached_btree_nodes_ops = {
565 .owner = THIS_MODULE,
566 .open = bch2_dump_open,
567 .release = bch2_dump_release,
568 .read = bch2_cached_btree_nodes_read,
571 typedef int (*list_cmp_fn)(const struct list_head *l, const struct list_head *r);
573 static void list_sort(struct list_head *head, list_cmp_fn cmp)
575 struct list_head *pos;
577 list_for_each(pos, head)
578 while (!list_is_last(pos, head) &&
579 cmp(pos, pos->next) > 0) {
580 struct list_head *pos2, *next = pos->next;
583 list_for_each(pos2, head)
584 if (cmp(next, pos2) < 0)
588 list_add_tail(next, pos2);
592 static int list_ptr_order_cmp(const struct list_head *l, const struct list_head *r)
594 return cmp_int(l, r);
597 static ssize_t bch2_btree_transactions_read(struct file *file, char __user *buf,
598 size_t size, loff_t *ppos)
600 struct dump_iter *i = file->private_data;
601 struct bch_fs *c = i->c;
602 struct btree_trans *trans;
609 seqmutex_lock(&c->btree_trans_lock);
610 list_sort(&c->btree_trans_list, list_ptr_order_cmp);
612 list_for_each_entry(trans, &c->btree_trans_list, list) {
613 if ((ulong) trans <= i->iter)
616 i->iter = (ulong) trans;
618 if (!closure_get_not_zero(&trans->ref))
621 u32 seq = seqmutex_unlock(&c->btree_trans_lock);
623 bch2_btree_trans_to_text(&i->buf, trans);
625 prt_printf(&i->buf, "backtrace:\n");
626 printbuf_indent_add(&i->buf, 2);
627 bch2_prt_task_backtrace(&i->buf, trans->locking_wait.task, 0, GFP_KERNEL);
628 printbuf_indent_sub(&i->buf, 2);
629 prt_newline(&i->buf);
631 closure_put(&trans->ref);
637 if (!seqmutex_relock(&c->btree_trans_lock, seq))
640 seqmutex_unlock(&c->btree_trans_lock);
642 if (i->buf.allocation_failure)
648 return ret ?: i->ret;
651 static const struct file_operations btree_transactions_ops = {
652 .owner = THIS_MODULE,
653 .open = bch2_dump_open,
654 .release = bch2_dump_release,
655 .read = bch2_btree_transactions_read,
658 static ssize_t bch2_journal_pins_read(struct file *file, char __user *buf,
659 size_t size, loff_t *ppos)
661 struct dump_iter *i = file->private_data;
662 struct bch_fs *c = i->c;
681 done = bch2_journal_seq_pins_to_text(&i->buf, &c->journal, &i->iter);
685 if (i->buf.allocation_failure)
691 static const struct file_operations journal_pins_ops = {
692 .owner = THIS_MODULE,
693 .open = bch2_dump_open,
694 .release = bch2_dump_release,
695 .read = bch2_journal_pins_read,
698 static ssize_t bch2_btree_updates_read(struct file *file, char __user *buf,
699 size_t size, loff_t *ppos)
701 struct dump_iter *i = file->private_data;
702 struct bch_fs *c = i->c;
710 bch2_btree_updates_to_text(&i->buf, c);
718 if (i->buf.allocation_failure)
724 static const struct file_operations btree_updates_ops = {
725 .owner = THIS_MODULE,
726 .open = bch2_dump_open,
727 .release = bch2_dump_release,
728 .read = bch2_btree_updates_read,
731 static int btree_transaction_stats_open(struct inode *inode, struct file *file)
733 struct bch_fs *c = inode->i_private;
736 i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
743 file->private_data = i;
748 static int btree_transaction_stats_release(struct inode *inode, struct file *file)
750 struct dump_iter *i = file->private_data;
752 printbuf_exit(&i->buf);
758 static ssize_t btree_transaction_stats_read(struct file *file, char __user *buf,
759 size_t size, loff_t *ppos)
761 struct dump_iter *i = file->private_data;
762 struct bch_fs *c = i->c;
770 struct btree_transaction_stats *s = &c->btree_transaction_stats[i->iter];
779 if (i->iter == ARRAY_SIZE(bch2_btree_transaction_fns) ||
780 !bch2_btree_transaction_fns[i->iter])
783 prt_printf(&i->buf, "%s:\n", bch2_btree_transaction_fns[i->iter]);
784 printbuf_indent_add(&i->buf, 2);
786 mutex_lock(&s->lock);
788 prt_printf(&i->buf, "Max mem used: %u\n", s->max_mem);
789 prt_printf(&i->buf, "Transaction duration:\n");
791 printbuf_indent_add(&i->buf, 2);
792 bch2_time_stats_to_text(&i->buf, &s->duration);
793 printbuf_indent_sub(&i->buf, 2);
795 if (IS_ENABLED(CONFIG_BCACHEFS_LOCK_TIME_STATS)) {
796 prt_printf(&i->buf, "Lock hold times:\n");
798 printbuf_indent_add(&i->buf, 2);
799 bch2_time_stats_to_text(&i->buf, &s->lock_hold_times);
800 printbuf_indent_sub(&i->buf, 2);
803 if (s->max_paths_text) {
804 prt_printf(&i->buf, "Maximum allocated btree paths (%u):\n", s->nr_max_paths);
806 printbuf_indent_add(&i->buf, 2);
807 prt_str_indented(&i->buf, s->max_paths_text);
808 printbuf_indent_sub(&i->buf, 2);
811 mutex_unlock(&s->lock);
813 printbuf_indent_sub(&i->buf, 2);
814 prt_newline(&i->buf);
818 if (i->buf.allocation_failure)
824 static const struct file_operations btree_transaction_stats_op = {
825 .owner = THIS_MODULE,
826 .open = btree_transaction_stats_open,
827 .release = btree_transaction_stats_release,
828 .read = btree_transaction_stats_read,
831 /* walk btree transactions until we find a deadlock and print it */
832 static void btree_deadlock_to_text(struct printbuf *out, struct bch_fs *c)
834 struct btree_trans *trans;
837 seqmutex_lock(&c->btree_trans_lock);
838 list_sort(&c->btree_trans_list, list_ptr_order_cmp);
840 list_for_each_entry(trans, &c->btree_trans_list, list) {
841 if ((ulong) trans <= iter)
844 iter = (ulong) trans;
846 if (!closure_get_not_zero(&trans->ref))
849 u32 seq = seqmutex_unlock(&c->btree_trans_lock);
851 bool found = bch2_check_for_deadlock(trans, out) != 0;
853 closure_put(&trans->ref);
858 if (!seqmutex_relock(&c->btree_trans_lock, seq))
861 seqmutex_unlock(&c->btree_trans_lock);
864 static ssize_t bch2_btree_deadlock_read(struct file *file, char __user *buf,
865 size_t size, loff_t *ppos)
867 struct dump_iter *i = file->private_data;
868 struct bch_fs *c = i->c;
876 btree_deadlock_to_text(&i->buf, c);
880 if (i->buf.allocation_failure)
886 return ret ?: i->ret;
889 static const struct file_operations btree_deadlock_ops = {
890 .owner = THIS_MODULE,
891 .open = bch2_dump_open,
892 .release = bch2_dump_release,
893 .read = bch2_btree_deadlock_read,
896 void bch2_fs_debug_exit(struct bch_fs *c)
898 if (!IS_ERR_OR_NULL(c->fs_debug_dir))
899 debugfs_remove_recursive(c->fs_debug_dir);
902 static void bch2_fs_debug_btree_init(struct bch_fs *c, struct btree_debug *bd)
906 d = debugfs_create_dir(bch2_btree_id_str(bd->id), c->btree_debug_dir);
908 debugfs_create_file("keys", 0400, d, bd, &btree_debug_ops);
910 debugfs_create_file("formats", 0400, d, bd, &btree_format_debug_ops);
912 debugfs_create_file("bfloat-failed", 0400, d, bd,
913 &bfloat_failed_debug_ops);
916 void bch2_fs_debug_init(struct bch_fs *c)
918 struct btree_debug *bd;
921 if (IS_ERR_OR_NULL(bch_debug))
924 snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
925 c->fs_debug_dir = debugfs_create_dir(name, bch_debug);
926 if (IS_ERR_OR_NULL(c->fs_debug_dir))
929 debugfs_create_file("cached_btree_nodes", 0400, c->fs_debug_dir,
930 c->btree_debug, &cached_btree_nodes_ops);
932 debugfs_create_file("btree_transactions", 0400, c->fs_debug_dir,
933 c->btree_debug, &btree_transactions_ops);
935 debugfs_create_file("journal_pins", 0400, c->fs_debug_dir,
936 c->btree_debug, &journal_pins_ops);
938 debugfs_create_file("btree_updates", 0400, c->fs_debug_dir,
939 c->btree_debug, &btree_updates_ops);
941 debugfs_create_file("btree_transaction_stats", 0400, c->fs_debug_dir,
942 c, &btree_transaction_stats_op);
944 debugfs_create_file("btree_deadlock", 0400, c->fs_debug_dir,
945 c->btree_debug, &btree_deadlock_ops);
947 c->btree_debug_dir = debugfs_create_dir("btrees", c->fs_debug_dir);
948 if (IS_ERR_OR_NULL(c->btree_debug_dir))
951 for (bd = c->btree_debug;
952 bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
954 bd->id = bd - c->btree_debug;
955 bch2_fs_debug_btree_init(c, bd);
961 void bch2_debug_exit(void)
963 if (!IS_ERR_OR_NULL(bch_debug))
964 debugfs_remove_recursive(bch_debug);
967 int __init bch2_debug_init(void)
969 bch_debug = debugfs_create_dir("bcachefs", NULL);