]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/debug.c
Update bcachefs sources to 0a9f0fc68a bcachefs: Don't unconditially version_upgrade...
[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);
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, ca, 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(c, b, inmemory, 0);
101
102                 printk(KERN_ERR "*** read back in:\n");
103                 bch2_dump_bset(c, 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(c, 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_iter_put(&trans, iter);
246
247         bch2_trans_exit(&trans);
248
249         return err < 0 ? err : i->ret;
250 }
251
252 static const struct file_operations btree_debug_ops = {
253         .owner          = THIS_MODULE,
254         .open           = bch2_dump_open,
255         .release        = bch2_dump_release,
256         .read           = bch2_read_btree,
257 };
258
259 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
260                                        size_t size, loff_t *ppos)
261 {
262         struct dump_iter *i = file->private_data;
263         struct btree_trans trans;
264         struct btree_iter *iter;
265         struct btree *b;
266         int err;
267
268         i->ubuf = buf;
269         i->size = size;
270         i->ret  = 0;
271
272         err = flush_buf(i);
273         if (err)
274                 return err;
275
276         if (!i->size || !bkey_cmp(POS_MAX, i->from))
277                 return i->ret;
278
279         bch2_trans_init(&trans, i->c, 0, 0);
280
281         for_each_btree_node(&trans, iter, i->id, i->from, 0, b) {
282                 bch2_btree_node_to_text(&PBUF(i->buf), i->c, b);
283                 i->bytes = strlen(i->buf);
284                 err = flush_buf(i);
285                 if (err)
286                         break;
287
288                 /*
289                  * can't easily correctly restart a btree node traversal across
290                  * all nodes, meh
291                  */
292                 i->from = bkey_cmp(POS_MAX, b->key.k.p)
293                         ? bkey_successor(b->key.k.p)
294                         : b->key.k.p;
295
296                 if (!i->size)
297                         break;
298         }
299         bch2_trans_iter_put(&trans, iter);
300
301         bch2_trans_exit(&trans);
302
303         return err < 0 ? err : i->ret;
304 }
305
306 static const struct file_operations btree_format_debug_ops = {
307         .owner          = THIS_MODULE,
308         .open           = bch2_dump_open,
309         .release        = bch2_dump_release,
310         .read           = bch2_read_btree_formats,
311 };
312
313 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
314                                        size_t size, loff_t *ppos)
315 {
316         struct dump_iter *i = file->private_data;
317         struct btree_trans trans;
318         struct btree_iter *iter;
319         struct bkey_s_c k;
320         struct btree *prev_node = NULL;
321         int err;
322
323         i->ubuf = buf;
324         i->size = size;
325         i->ret  = 0;
326
327         err = flush_buf(i);
328         if (err)
329                 return err;
330
331         if (!i->size)
332                 return i->ret;
333
334         bch2_trans_init(&trans, i->c, 0, 0);
335
336         iter = bch2_trans_get_iter(&trans, i->id, i->from, BTREE_ITER_PREFETCH);
337
338         while ((k = bch2_btree_iter_peek(iter)).k &&
339                !(err = bkey_err(k))) {
340                 struct btree_iter_level *l = &iter->l[0];
341                 struct bkey_packed *_k =
342                         bch2_btree_node_iter_peek(&l->iter, l->b);
343
344                 if (l->b != prev_node) {
345                         bch2_btree_node_to_text(&PBUF(i->buf), i->c, l->b);
346                         i->bytes = strlen(i->buf);
347                         err = flush_buf(i);
348                         if (err)
349                                 break;
350                 }
351                 prev_node = l->b;
352
353                 bch2_bfloat_to_text(&PBUF(i->buf), l->b, _k);
354                 i->bytes = strlen(i->buf);
355                 err = flush_buf(i);
356                 if (err)
357                         break;
358
359                 bch2_btree_iter_next(iter);
360                 i->from = iter->pos;
361
362                 err = flush_buf(i);
363                 if (err)
364                         break;
365
366                 if (!i->size)
367                         break;
368         }
369         bch2_trans_exit(&trans);
370
371         return err < 0 ? err : i->ret;
372 }
373
374 static const struct file_operations bfloat_failed_debug_ops = {
375         .owner          = THIS_MODULE,
376         .open           = bch2_dump_open,
377         .release        = bch2_dump_release,
378         .read           = bch2_read_bfloat_failed,
379 };
380
381 void bch2_fs_debug_exit(struct bch_fs *c)
382 {
383         if (!IS_ERR_OR_NULL(c->debug))
384                 debugfs_remove_recursive(c->debug);
385 }
386
387 void bch2_fs_debug_init(struct bch_fs *c)
388 {
389         struct btree_debug *bd;
390         char name[100];
391
392         if (IS_ERR_OR_NULL(bch_debug))
393                 return;
394
395         snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
396         c->debug = debugfs_create_dir(name, bch_debug);
397         if (IS_ERR_OR_NULL(c->debug))
398                 return;
399
400         for (bd = c->btree_debug;
401              bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
402              bd++) {
403                 bd->id = bd - c->btree_debug;
404                 bd->btree = debugfs_create_file(bch2_btree_ids[bd->id],
405                                                 0400, c->debug, bd,
406                                                 &btree_debug_ops);
407
408                 snprintf(name, sizeof(name), "%s-formats",
409                          bch2_btree_ids[bd->id]);
410
411                 bd->btree_format = debugfs_create_file(name, 0400, c->debug, bd,
412                                                        &btree_format_debug_ops);
413
414                 snprintf(name, sizeof(name), "%s-bfloat-failed",
415                          bch2_btree_ids[bd->id]);
416
417                 bd->failed = debugfs_create_file(name, 0400, c->debug, bd,
418                                                  &bfloat_failed_debug_ops);
419         }
420 }
421
422 #endif
423
424 void bch2_debug_exit(void)
425 {
426         if (!IS_ERR_OR_NULL(bch_debug))
427                 debugfs_remove_recursive(bch_debug);
428 }
429
430 int __init bch2_debug_init(void)
431 {
432         int ret = 0;
433
434         bch_debug = debugfs_create_dir("bcachefs", NULL);
435         return ret;
436 }