]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/debug.c
Update bcachefs sources to 4837f82ee1 bcachefs: Use cached iterators for alloc btree
[bcachefs-tools-debian] / libbcachefs / debug.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Assorted bcachefs debug code
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #include "bcachefs.h"
10 #include "bkey_methods.h"
11 #include "btree_cache.h"
12 #include "btree_io.h"
13 #include "btree_iter.h"
14 #include "btree_update.h"
15 #include "buckets.h"
16 #include "debug.h"
17 #include "error.h"
18 #include "extents.h"
19 #include "fsck.h"
20 #include "inode.h"
21 #include "io.h"
22 #include "super.h"
23
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>
29
30 static struct dentry *bch_debug;
31
32 #ifdef CONFIG_BCACHEFS_DEBUG
33
34 void __bch2_btree_verify(struct bch_fs *c, struct btree *b)
35 {
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;
40         struct bch_dev *ca;
41         struct bio *bio;
42
43         if (c->opts.nochanges)
44                 return;
45
46         btree_node_io_lock(b);
47         mutex_lock(&c->verify_lock);
48
49         n_ondisk = c->verify_ondisk;
50         n_sorted = c->verify_data->data;
51         n_inmemory = b->data;
52
53         bkey_copy(&v->key, &b->key);
54         v->written      = 0;
55         v->c.level      = b->c.level;
56         v->c.btree_id   = b->c.btree_id;
57         bch2_btree_keys_init(v, &c->expensive_debug_checks);
58
59         if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key),
60                                        NULL, &pick) <= 0)
61                 return;
62
63         ca = bch_dev_bkey_exists(c, pick.ptr.dev);
64         if (!bch2_dev_get_ioref(ca, READ))
65                 return;
66
67         bio = bio_alloc_bioset(GFP_NOIO,
68                         buf_pages(n_sorted, btree_bytes(c)),
69                         &c->btree_bio);
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));
74
75         submit_bio_wait(bio);
76
77         bio_put(bio);
78         percpu_ref_put(&ca->io_ref);
79
80         memcpy(n_ondisk, n_sorted, btree_bytes(c));
81
82         if (bch2_btree_node_read_done(c, v, false))
83                 goto out;
84
85         n_sorted = c->verify_data->data;
86         sorted = &n_sorted->keys;
87         inmemory = &n_inmemory->keys;
88
89         if (inmemory->u64s != sorted->u64s ||
90             memcmp(inmemory->start,
91                    sorted->start,
92                    vstruct_end(inmemory) - (void *) inmemory->start)) {
93                 unsigned offset = 0, sectors;
94                 struct bset *i;
95                 unsigned j;
96
97                 console_lock();
98
99                 printk(KERN_ERR "*** in memory:\n");
100                 bch2_dump_bset(b, inmemory, 0);
101
102                 printk(KERN_ERR "*** read back in:\n");
103                 bch2_dump_bset(v, sorted, 0);
104
105                 while (offset < b->written) {
106                         if (!offset ) {
107                                 i = &n_ondisk->keys;
108                                 sectors = vstruct_blocks(n_ondisk, c->block_bits) <<
109                                         c->block_bits;
110                         } else {
111                                 struct btree_node_entry *bne =
112                                         (void *) n_ondisk + (offset << 9);
113                                 i = &bne->keys;
114
115                                 sectors = vstruct_blocks(bne, c->block_bits) <<
116                                         c->block_bits;
117                         }
118
119                         printk(KERN_ERR "*** on disk block %u:\n", offset);
120                         bch2_dump_bset(b, i, offset);
121
122                         offset += sectors;
123                 }
124
125                 printk(KERN_ERR "*** block %u/%u not written\n",
126                        offset >> c->block_bits, btree_blocks(c));
127
128                 for (j = 0; j < le16_to_cpu(inmemory->u64s); j++)
129                         if (inmemory->_data[j] != sorted->_data[j])
130                                 break;
131
132                 printk(KERN_ERR "b->written %u\n", b->written);
133
134                 console_unlock();
135                 panic("verify failed at %u\n", j);
136         }
137 out:
138         mutex_unlock(&c->verify_lock);
139         btree_node_io_unlock(b);
140 }
141
142 #endif
143
144 #ifdef CONFIG_DEBUG_FS
145
146 /* XXX: bch_fs refcounting */
147
148 struct dump_iter {
149         struct bpos             from;
150         struct bch_fs   *c;
151         enum btree_id           id;
152
153         char                    buf[PAGE_SIZE];
154         size_t                  bytes;  /* what's currently in buf */
155
156         char __user             *ubuf;  /* destination user buffer */
157         size_t                  size;   /* size of requested read */
158         ssize_t                 ret;    /* bytes read so far */
159 };
160
161 static int flush_buf(struct dump_iter *i)
162 {
163         if (i->bytes) {
164                 size_t bytes = min(i->bytes, i->size);
165                 int err = copy_to_user(i->ubuf, i->buf, bytes);
166
167                 if (err)
168                         return err;
169
170                 i->ret   += bytes;
171                 i->ubuf  += bytes;
172                 i->size  -= bytes;
173                 i->bytes -= bytes;
174                 memmove(i->buf, i->buf + bytes, i->bytes);
175         }
176
177         return 0;
178 }
179
180 static int bch2_dump_open(struct inode *inode, struct file *file)
181 {
182         struct btree_debug *bd = inode->i_private;
183         struct dump_iter *i;
184
185         i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
186         if (!i)
187                 return -ENOMEM;
188
189         file->private_data = i;
190         i->from = POS_MIN;
191         i->c    = container_of(bd, struct bch_fs, btree_debug[bd->id]);
192         i->id   = bd->id;
193
194         return 0;
195 }
196
197 static int bch2_dump_release(struct inode *inode, struct file *file)
198 {
199         kfree(file->private_data);
200         return 0;
201 }
202
203 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
204                                size_t size, loff_t *ppos)
205 {
206         struct dump_iter *i = file->private_data;
207         struct btree_trans trans;
208         struct btree_iter *iter;
209         struct bkey_s_c k;
210         int err;
211
212         i->ubuf = buf;
213         i->size = size;
214         i->ret  = 0;
215
216         err = flush_buf(i);
217         if (err)
218                 return err;
219
220         if (!i->size)
221                 return i->ret;
222
223         bch2_trans_init(&trans, i->c, 0, 0);
224
225         iter = bch2_trans_get_iter(&trans, i->id, i->from, BTREE_ITER_PREFETCH);
226         k = bch2_btree_iter_peek(iter);
227
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';
233                 i->bytes++;
234
235                 k = bch2_btree_iter_next(iter);
236                 i->from = iter->pos;
237
238                 err = flush_buf(i);
239                 if (err)
240                         break;
241
242                 if (!i->size)
243                         break;
244         }
245         bch2_trans_exit(&trans);
246
247         return err < 0 ? err : i->ret;
248 }
249
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,
255 };
256
257 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
258                                        size_t size, loff_t *ppos)
259 {
260         struct dump_iter *i = file->private_data;
261         struct btree_trans trans;
262         struct btree_iter *iter;
263         struct btree *b;
264         int err;
265
266         i->ubuf = buf;
267         i->size = size;
268         i->ret  = 0;
269
270         err = flush_buf(i);
271         if (err)
272                 return err;
273
274         if (!i->size || !bkey_cmp(POS_MAX, i->from))
275                 return i->ret;
276
277         bch2_trans_init(&trans, i->c, 0, 0);
278
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);
282                 err = flush_buf(i);
283                 if (err)
284                         break;
285
286                 /*
287                  * can't easily correctly restart a btree node traversal across
288                  * all nodes, meh
289                  */
290                 i->from = bkey_cmp(POS_MAX, b->key.k.p)
291                         ? bkey_successor(b->key.k.p)
292                         : b->key.k.p;
293
294                 if (!i->size)
295                         break;
296         }
297         bch2_trans_exit(&trans);
298
299         return err < 0 ? err : i->ret;
300 }
301
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,
307 };
308
309 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
310                                        size_t size, loff_t *ppos)
311 {
312         struct dump_iter *i = file->private_data;
313         struct btree_trans trans;
314         struct btree_iter *iter;
315         struct bkey_s_c k;
316         struct btree *prev_node = NULL;
317         int err;
318
319         i->ubuf = buf;
320         i->size = size;
321         i->ret  = 0;
322
323         err = flush_buf(i);
324         if (err)
325                 return err;
326
327         if (!i->size)
328                 return i->ret;
329
330         bch2_trans_init(&trans, i->c, 0, 0);
331
332         iter = bch2_trans_get_iter(&trans, i->id, i->from, BTREE_ITER_PREFETCH);
333
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);
339
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);
343                         err = flush_buf(i);
344                         if (err)
345                                 break;
346                 }
347                 prev_node = l->b;
348
349                 bch2_bfloat_to_text(&PBUF(i->buf), l->b, _k);
350                 i->bytes = strlen(i->buf);
351                 err = flush_buf(i);
352                 if (err)
353                         break;
354
355                 bch2_btree_iter_next(iter);
356                 i->from = iter->pos;
357
358                 err = flush_buf(i);
359                 if (err)
360                         break;
361
362                 if (!i->size)
363                         break;
364         }
365         bch2_trans_exit(&trans);
366
367         return err < 0 ? err : i->ret;
368 }
369
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,
375 };
376
377 void bch2_fs_debug_exit(struct bch_fs *c)
378 {
379         if (!IS_ERR_OR_NULL(c->debug))
380                 debugfs_remove_recursive(c->debug);
381 }
382
383 void bch2_fs_debug_init(struct bch_fs *c)
384 {
385         struct btree_debug *bd;
386         char name[100];
387
388         if (IS_ERR_OR_NULL(bch_debug))
389                 return;
390
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))
394                 return;
395
396         for (bd = c->btree_debug;
397              bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
398              bd++) {
399                 bd->id = bd - c->btree_debug;
400                 bd->btree = debugfs_create_file(bch2_btree_ids[bd->id],
401                                                 0400, c->debug, bd,
402                                                 &btree_debug_ops);
403
404                 snprintf(name, sizeof(name), "%s-formats",
405                          bch2_btree_ids[bd->id]);
406
407                 bd->btree_format = debugfs_create_file(name, 0400, c->debug, bd,
408                                                        &btree_format_debug_ops);
409
410                 snprintf(name, sizeof(name), "%s-bfloat-failed",
411                          bch2_btree_ids[bd->id]);
412
413                 bd->failed = debugfs_create_file(name, 0400, c->debug, bd,
414                                                  &bfloat_failed_debug_ops);
415         }
416 }
417
418 #endif
419
420 void bch2_debug_exit(void)
421 {
422         if (!IS_ERR_OR_NULL(bch_debug))
423                 debugfs_remove_recursive(bch_debug);
424 }
425
426 int __init bch2_debug_init(void)
427 {
428         int ret = 0;
429
430         bch_debug = debugfs_create_dir("bcachefs", NULL);
431         return ret;
432 }