xref: /linux/fs/bcachefs/debug.c (revision 4412b8b23de24a94a0b78ac283db043c833a3975)
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 "alloc_foreground.h"
11 #include "async_objs.h"
12 #include "bkey_methods.h"
13 #include "btree_cache.h"
14 #include "btree_io.h"
15 #include "btree_iter.h"
16 #include "btree_locking.h"
17 #include "btree_update.h"
18 #include "btree_update_interior.h"
19 #include "buckets.h"
20 #include "data_update.h"
21 #include "debug.h"
22 #include "error.h"
23 #include "extents.h"
24 #include "fsck.h"
25 #include "inode.h"
26 #include "journal_reclaim.h"
27 #include "super.h"
28 
29 #include <linux/console.h>
30 #include <linux/debugfs.h>
31 #include <linux/module.h>
32 #include <linux/random.h>
33 #include <linux/seq_file.h>
34 
35 static struct dentry *bch_debug;
36 
bch2_btree_verify_replica(struct bch_fs * c,struct btree * b,struct extent_ptr_decoded pick)37 static bool bch2_btree_verify_replica(struct bch_fs *c, struct btree *b,
38 				      struct extent_ptr_decoded pick)
39 {
40 	struct btree *v = c->verify_data;
41 	struct btree_node *n_ondisk = c->verify_ondisk;
42 	struct btree_node *n_sorted = c->verify_data->data;
43 	struct bset *sorted, *inmemory = &b->data->keys;
44 	struct bio *bio;
45 	bool failed = false;
46 
47 	struct bch_dev *ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ,
48 				BCH_DEV_READ_REF_btree_verify_replicas);
49 	if (!ca)
50 		return false;
51 
52 	bio = bio_alloc_bioset(ca->disk_sb.bdev,
53 			       buf_pages(n_sorted, btree_buf_bytes(b)),
54 			       REQ_OP_READ|REQ_META,
55 			       GFP_NOFS,
56 			       &c->btree_bio);
57 	bio->bi_iter.bi_sector	= pick.ptr.offset;
58 	bch2_bio_map(bio, n_sorted, btree_buf_bytes(b));
59 
60 	submit_bio_wait(bio);
61 
62 	bio_put(bio);
63 	enumerated_ref_put(&ca->io_ref[READ],
64 			   BCH_DEV_READ_REF_btree_verify_replicas);
65 
66 	memcpy(n_ondisk, n_sorted, btree_buf_bytes(b));
67 
68 	v->written = 0;
69 	if (bch2_btree_node_read_done(c, ca, v, NULL, NULL))
70 		return false;
71 
72 	n_sorted = c->verify_data->data;
73 	sorted = &n_sorted->keys;
74 
75 	if (inmemory->u64s != sorted->u64s ||
76 	    memcmp(inmemory->start,
77 		   sorted->start,
78 		   vstruct_end(inmemory) - (void *) inmemory->start)) {
79 		unsigned offset = 0, sectors;
80 		struct bset *i;
81 		unsigned j;
82 
83 		console_lock();
84 
85 		printk(KERN_ERR "*** in memory:\n");
86 		bch2_dump_bset(c, b, inmemory, 0);
87 
88 		printk(KERN_ERR "*** read back in:\n");
89 		bch2_dump_bset(c, v, sorted, 0);
90 
91 		while (offset < v->written) {
92 			if (!offset) {
93 				i = &n_ondisk->keys;
94 				sectors = vstruct_blocks(n_ondisk, c->block_bits) <<
95 					c->block_bits;
96 			} else {
97 				struct btree_node_entry *bne =
98 					(void *) n_ondisk + (offset << 9);
99 				i = &bne->keys;
100 
101 				sectors = vstruct_blocks(bne, c->block_bits) <<
102 					c->block_bits;
103 			}
104 
105 			printk(KERN_ERR "*** on disk block %u:\n", offset);
106 			bch2_dump_bset(c, b, i, offset);
107 
108 			offset += sectors;
109 		}
110 
111 		for (j = 0; j < le16_to_cpu(inmemory->u64s); j++)
112 			if (inmemory->_data[j] != sorted->_data[j])
113 				break;
114 
115 		console_unlock();
116 		bch_err(c, "verify failed at key %u", j);
117 
118 		failed = true;
119 	}
120 
121 	if (v->written != b->written) {
122 		bch_err(c, "written wrong: expected %u, got %u",
123 			b->written, v->written);
124 		failed = true;
125 	}
126 
127 	return failed;
128 }
129 
__bch2_btree_verify(struct bch_fs * c,struct btree * b)130 void __bch2_btree_verify(struct bch_fs *c, struct btree *b)
131 {
132 	struct bkey_ptrs_c ptrs;
133 	struct extent_ptr_decoded p;
134 	const union bch_extent_entry *entry;
135 	struct btree *v;
136 	struct bset *inmemory = &b->data->keys;
137 	struct bkey_packed *k;
138 	bool failed = false;
139 
140 	if (c->opts.nochanges)
141 		return;
142 
143 	bch2_btree_node_io_lock(b);
144 	mutex_lock(&c->verify_lock);
145 
146 	if (!c->verify_ondisk) {
147 		c->verify_ondisk = kvmalloc(btree_buf_bytes(b), GFP_KERNEL);
148 		if (!c->verify_ondisk)
149 			goto out;
150 	}
151 
152 	if (!c->verify_data) {
153 		c->verify_data = __bch2_btree_node_mem_alloc(c);
154 		if (!c->verify_data)
155 			goto out;
156 	}
157 
158 	BUG_ON(b->nsets != 1);
159 
160 	for (k = inmemory->start; k != vstruct_last(inmemory); k = bkey_p_next(k))
161 		if (k->type == KEY_TYPE_btree_ptr_v2)
162 			((struct bch_btree_ptr_v2 *) bkeyp_val(&b->format, k))->mem_ptr = 0;
163 
164 	v = c->verify_data;
165 	bkey_copy(&v->key, &b->key);
166 	v->c.level	= b->c.level;
167 	v->c.btree_id	= b->c.btree_id;
168 	bch2_btree_keys_init(v);
169 
170 	ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
171 	bkey_for_each_ptr_decode(&b->key.k, ptrs, p, entry)
172 		failed |= bch2_btree_verify_replica(c, b, p);
173 
174 	if (failed) {
175 		struct printbuf buf = PRINTBUF;
176 
177 		bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
178 		bch2_fs_fatal_error(c, ": btree node verify failed for: %s\n", buf.buf);
179 		printbuf_exit(&buf);
180 	}
181 out:
182 	mutex_unlock(&c->verify_lock);
183 	bch2_btree_node_io_unlock(b);
184 }
185 
bch2_btree_node_ondisk_to_text(struct printbuf * out,struct bch_fs * c,const struct btree * b)186 void bch2_btree_node_ondisk_to_text(struct printbuf *out, struct bch_fs *c,
187 				    const struct btree *b)
188 {
189 	struct btree_node *n_ondisk = NULL;
190 	struct extent_ptr_decoded pick;
191 	struct bch_dev *ca;
192 	struct bio *bio = NULL;
193 	unsigned offset = 0;
194 	int ret;
195 
196 	if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), NULL, &pick, -1) <= 0) {
197 		prt_printf(out, "error getting device to read from: invalid device\n");
198 		return;
199 	}
200 
201 	ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ,
202 			BCH_DEV_READ_REF_btree_node_ondisk_to_text);
203 	if (!ca) {
204 		prt_printf(out, "error getting device to read from: not online\n");
205 		return;
206 	}
207 
208 	n_ondisk = kvmalloc(btree_buf_bytes(b), GFP_KERNEL);
209 	if (!n_ondisk) {
210 		prt_printf(out, "memory allocation failure\n");
211 		goto out;
212 	}
213 
214 	bio = bio_alloc_bioset(ca->disk_sb.bdev,
215 			       buf_pages(n_ondisk, btree_buf_bytes(b)),
216 			       REQ_OP_READ|REQ_META,
217 			       GFP_NOFS,
218 			       &c->btree_bio);
219 	bio->bi_iter.bi_sector	= pick.ptr.offset;
220 	bch2_bio_map(bio, n_ondisk, btree_buf_bytes(b));
221 
222 	ret = submit_bio_wait(bio);
223 	if (ret) {
224 		prt_printf(out, "IO error reading btree node: %s\n", bch2_err_str(ret));
225 		goto out;
226 	}
227 
228 	while (offset < btree_sectors(c)) {
229 		struct bset *i;
230 		struct nonce nonce;
231 		struct bch_csum csum;
232 		struct bkey_packed *k;
233 		unsigned sectors;
234 
235 		if (!offset) {
236 			i = &n_ondisk->keys;
237 
238 			if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i))) {
239 				prt_printf(out, "unknown checksum type at offset %u: %llu\n",
240 					   offset, BSET_CSUM_TYPE(i));
241 				goto out;
242 			}
243 
244 			nonce = btree_nonce(i, offset << 9);
245 			csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, n_ondisk);
246 
247 			if (bch2_crc_cmp(csum, n_ondisk->csum)) {
248 				prt_printf(out, "invalid checksum\n");
249 				goto out;
250 			}
251 
252 			bset_encrypt(c, i, offset << 9);
253 
254 			sectors = vstruct_sectors(n_ondisk, c->block_bits);
255 		} else {
256 			struct btree_node_entry *bne = (void *) n_ondisk + (offset << 9);
257 
258 			i = &bne->keys;
259 
260 			if (i->seq != n_ondisk->keys.seq)
261 				break;
262 
263 			if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i))) {
264 				prt_printf(out, "unknown checksum type at offset %u: %llu\n",
265 					   offset, BSET_CSUM_TYPE(i));
266 				goto out;
267 			}
268 
269 			nonce = btree_nonce(i, offset << 9);
270 			csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);
271 
272 			if (bch2_crc_cmp(csum, bne->csum)) {
273 				prt_printf(out, "invalid checksum");
274 				goto out;
275 			}
276 
277 			bset_encrypt(c, i, offset << 9);
278 
279 			sectors = vstruct_sectors(bne, c->block_bits);
280 		}
281 
282 		prt_printf(out, "  offset %u version %u, journal seq %llu\n",
283 			   offset,
284 			   le16_to_cpu(i->version),
285 			   le64_to_cpu(i->journal_seq));
286 		offset += sectors;
287 
288 		printbuf_indent_add(out, 4);
289 
290 		for (k = i->start; k != vstruct_last(i); k = bkey_p_next(k)) {
291 			struct bkey u;
292 
293 			bch2_bkey_val_to_text(out, c, bkey_disassemble(b, k, &u));
294 			prt_newline(out);
295 		}
296 
297 		printbuf_indent_sub(out, 4);
298 	}
299 out:
300 	if (bio)
301 		bio_put(bio);
302 	kvfree(n_ondisk);
303 	enumerated_ref_put(&ca->io_ref[READ],
304 			   BCH_DEV_READ_REF_btree_node_ondisk_to_text);
305 }
306 
307 #ifdef CONFIG_DEBUG_FS
308 
bch2_debugfs_flush_buf(struct dump_iter * i)309 ssize_t bch2_debugfs_flush_buf(struct dump_iter *i)
310 {
311 	if (i->buf.pos) {
312 		size_t bytes = min_t(size_t, i->buf.pos, i->size);
313 		int copied = bytes - copy_to_user(i->ubuf, i->buf.buf, bytes);
314 
315 		i->ret	 += copied;
316 		i->ubuf	 += copied;
317 		i->size	 -= copied;
318 		i->buf.pos -= copied;
319 		memmove(i->buf.buf, i->buf.buf + copied, i->buf.pos);
320 
321 		if (i->buf.last_newline >= copied)
322 			i->buf.last_newline -= copied;
323 		if (i->buf.last_field >= copied)
324 			i->buf.last_field -= copied;
325 
326 		if (copied != bytes)
327 			return -EFAULT;
328 	}
329 
330 	return i->size ? 0 : i->ret;
331 }
332 
bch2_dump_open(struct inode * inode,struct file * file)333 static int bch2_dump_open(struct inode *inode, struct file *file)
334 {
335 	struct btree_debug *bd = inode->i_private;
336 	struct dump_iter *i;
337 
338 	i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
339 	if (!i)
340 		return -ENOMEM;
341 
342 	file->private_data = i;
343 	i->from = POS_MIN;
344 	i->iter	= 0;
345 	i->c	= container_of(bd, struct bch_fs, btree_debug[bd->id]);
346 	i->id	= bd->id;
347 	i->buf	= PRINTBUF;
348 
349 	return 0;
350 }
351 
bch2_dump_release(struct inode * inode,struct file * file)352 int bch2_dump_release(struct inode *inode, struct file *file)
353 {
354 	struct dump_iter *i = file->private_data;
355 
356 	printbuf_exit(&i->buf);
357 	kfree(i);
358 	return 0;
359 }
360 
bch2_read_btree(struct file * file,char __user * buf,size_t size,loff_t * ppos)361 static ssize_t bch2_read_btree(struct file *file, char __user *buf,
362 			       size_t size, loff_t *ppos)
363 {
364 	struct dump_iter *i = file->private_data;
365 
366 	i->ubuf = buf;
367 	i->size	= size;
368 	i->ret	= 0;
369 
370 	return bch2_debugfs_flush_buf(i) ?:
371 		bch2_trans_run(i->c,
372 			for_each_btree_key(trans, iter, i->id, i->from,
373 					   BTREE_ITER_prefetch|
374 					   BTREE_ITER_all_snapshots, k, ({
375 				bch2_bkey_val_to_text(&i->buf, i->c, k);
376 				prt_newline(&i->buf);
377 				bch2_trans_unlock(trans);
378 				i->from = bpos_successor(iter.pos);
379 				bch2_debugfs_flush_buf(i);
380 			}))) ?:
381 		i->ret;
382 }
383 
384 static const struct file_operations btree_debug_ops = {
385 	.owner		= THIS_MODULE,
386 	.open		= bch2_dump_open,
387 	.release	= bch2_dump_release,
388 	.read		= bch2_read_btree,
389 };
390 
bch2_read_btree_formats(struct file * file,char __user * buf,size_t size,loff_t * ppos)391 static ssize_t bch2_read_btree_formats(struct file *file, char __user *buf,
392 				       size_t size, loff_t *ppos)
393 {
394 	struct dump_iter *i = file->private_data;
395 
396 	i->ubuf = buf;
397 	i->size	= size;
398 	i->ret	= 0;
399 
400 	ssize_t ret = bch2_debugfs_flush_buf(i);
401 	if (ret)
402 		return ret;
403 
404 	if (bpos_eq(SPOS_MAX, i->from))
405 		return i->ret;
406 
407 	return bch2_trans_run(i->c,
408 		for_each_btree_node(trans, iter, i->id, i->from, 0, b, ({
409 			bch2_btree_node_to_text(&i->buf, i->c, b);
410 			i->from = !bpos_eq(SPOS_MAX, b->key.k.p)
411 				? bpos_successor(b->key.k.p)
412 				: b->key.k.p;
413 
414 			drop_locks_do(trans, bch2_debugfs_flush_buf(i));
415 		}))) ?: i->ret;
416 }
417 
418 static const struct file_operations btree_format_debug_ops = {
419 	.owner		= THIS_MODULE,
420 	.open		= bch2_dump_open,
421 	.release	= bch2_dump_release,
422 	.read		= bch2_read_btree_formats,
423 };
424 
bch2_read_bfloat_failed(struct file * file,char __user * buf,size_t size,loff_t * ppos)425 static ssize_t bch2_read_bfloat_failed(struct file *file, char __user *buf,
426 				       size_t size, loff_t *ppos)
427 {
428 	struct dump_iter *i = file->private_data;
429 
430 	i->ubuf = buf;
431 	i->size	= size;
432 	i->ret	= 0;
433 
434 	return bch2_debugfs_flush_buf(i) ?:
435 		bch2_trans_run(i->c,
436 			for_each_btree_key(trans, iter, i->id, i->from,
437 					   BTREE_ITER_prefetch|
438 					   BTREE_ITER_all_snapshots, k, ({
439 				struct btree_path_level *l =
440 					&btree_iter_path(trans, &iter)->l[0];
441 				struct bkey_packed *_k =
442 					bch2_btree_node_iter_peek(&l->iter, l->b);
443 
444 				if (bpos_gt(l->b->key.k.p, i->prev_node)) {
445 					bch2_btree_node_to_text(&i->buf, i->c, l->b);
446 					i->prev_node = l->b->key.k.p;
447 				}
448 
449 				bch2_bfloat_to_text(&i->buf, l->b, _k);
450 				bch2_trans_unlock(trans);
451 				i->from = bpos_successor(iter.pos);
452 				bch2_debugfs_flush_buf(i);
453 			}))) ?:
454 		i->ret;
455 }
456 
457 static const struct file_operations bfloat_failed_debug_ops = {
458 	.owner		= THIS_MODULE,
459 	.open		= bch2_dump_open,
460 	.release	= bch2_dump_release,
461 	.read		= bch2_read_bfloat_failed,
462 };
463 
bch2_cached_btree_node_to_text(struct printbuf * out,struct bch_fs * c,struct btree * b)464 static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
465 					   struct btree *b)
466 {
467 	if (!out->nr_tabstops)
468 		printbuf_tabstop_push(out, 32);
469 
470 	prt_printf(out, "%px ", b);
471 	bch2_btree_id_level_to_text(out, b->c.btree_id, b->c.level);
472 	prt_printf(out, "\n");
473 
474 	printbuf_indent_add(out, 2);
475 
476 	bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&b->key));
477 	prt_newline(out);
478 
479 	prt_printf(out, "flags:\t");
480 	prt_bitflags(out, bch2_btree_node_flags, b->flags);
481 	prt_newline(out);
482 
483 	prt_printf(out, "pcpu read locks:\t%u\n",	b->c.lock.readers != NULL);
484 	prt_printf(out, "written:\t%u\n",		b->written);
485 	prt_printf(out, "writes blocked:\t%u\n",	!list_empty_careful(&b->write_blocked));
486 	prt_printf(out, "will make reachable:\t%lx\n",	b->will_make_reachable);
487 
488 	prt_printf(out, "journal pin %px:\t%llu\n",
489 		   &b->writes[0].journal, b->writes[0].journal.seq);
490 	prt_printf(out, "journal pin %px:\t%llu\n",
491 		   &b->writes[1].journal, b->writes[1].journal.seq);
492 
493 	prt_printf(out, "ob:\t%u\n", b->ob.nr);
494 
495 	printbuf_indent_sub(out, 2);
496 }
497 
bch2_cached_btree_nodes_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)498 static ssize_t bch2_cached_btree_nodes_read(struct file *file, char __user *buf,
499 					    size_t size, loff_t *ppos)
500 {
501 	struct dump_iter *i = file->private_data;
502 	struct bch_fs *c = i->c;
503 	bool done = false;
504 	ssize_t ret = 0;
505 
506 	i->ubuf = buf;
507 	i->size	= size;
508 	i->ret	= 0;
509 
510 	do {
511 		ret = bch2_debugfs_flush_buf(i);
512 		if (ret)
513 			return ret;
514 
515 		i->buf.atomic++;
516 		scoped_guard(rcu) {
517 			struct bucket_table *tbl =
518 				rht_dereference_rcu(c->btree_cache.table.tbl,
519 						    &c->btree_cache.table);
520 			if (i->iter < tbl->size) {
521 				struct rhash_head *pos;
522 				struct btree *b;
523 
524 				rht_for_each_entry_rcu(b, pos, tbl, i->iter, hash)
525 					bch2_cached_btree_node_to_text(&i->buf, c, b);
526 				i->iter++;
527 			} else {
528 				done = true;
529 			}
530 		}
531 		--i->buf.atomic;
532 	} while (!done);
533 
534 	if (i->buf.allocation_failure)
535 		ret = -ENOMEM;
536 
537 	if (!ret)
538 		ret = bch2_debugfs_flush_buf(i);
539 
540 	return ret ?: i->ret;
541 }
542 
543 static const struct file_operations cached_btree_nodes_ops = {
544 	.owner		= THIS_MODULE,
545 	.open		= bch2_dump_open,
546 	.release	= bch2_dump_release,
547 	.read		= bch2_cached_btree_nodes_read,
548 };
549 
550 typedef int (*list_cmp_fn)(const struct list_head *l, const struct list_head *r);
551 
list_sort(struct list_head * head,list_cmp_fn cmp)552 static void list_sort(struct list_head *head, list_cmp_fn cmp)
553 {
554 	struct list_head *pos;
555 
556 	list_for_each(pos, head)
557 		while (!list_is_last(pos, head) &&
558 		       cmp(pos, pos->next) > 0) {
559 			struct list_head *pos2, *next = pos->next;
560 
561 			list_del(next);
562 			list_for_each(pos2, head)
563 				if (cmp(next, pos2) < 0)
564 					goto pos_found;
565 			BUG();
566 pos_found:
567 			list_add_tail(next, pos2);
568 		}
569 }
570 
list_ptr_order_cmp(const struct list_head * l,const struct list_head * r)571 static int list_ptr_order_cmp(const struct list_head *l, const struct list_head *r)
572 {
573 	return cmp_int(l, r);
574 }
575 
bch2_btree_transactions_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)576 static ssize_t bch2_btree_transactions_read(struct file *file, char __user *buf,
577 					    size_t size, loff_t *ppos)
578 {
579 	struct dump_iter *i = file->private_data;
580 	struct bch_fs *c = i->c;
581 	struct btree_trans *trans;
582 	ssize_t ret = 0;
583 
584 	i->ubuf = buf;
585 	i->size	= size;
586 	i->ret	= 0;
587 
588 	int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
589 restart:
590 	seqmutex_lock(&c->btree_trans_lock);
591 	list_sort(&c->btree_trans_list, list_ptr_order_cmp);
592 
593 	list_for_each_entry(trans, &c->btree_trans_list, list) {
594 		if ((ulong) trans <= i->iter)
595 			continue;
596 
597 		i->iter = (ulong) trans;
598 
599 		if (!closure_get_not_zero(&trans->ref))
600 			continue;
601 
602 		if (!trans->srcu_held) {
603 			closure_put(&trans->ref);
604 			continue;
605 		}
606 
607 		u32 seq = seqmutex_unlock(&c->btree_trans_lock);
608 
609 		bch2_btree_trans_to_text(&i->buf, trans);
610 
611 		prt_printf(&i->buf, "backtrace:\n");
612 		printbuf_indent_add(&i->buf, 2);
613 		bch2_prt_task_backtrace(&i->buf, trans->locking_wait.task, 0, GFP_KERNEL);
614 		printbuf_indent_sub(&i->buf, 2);
615 		prt_newline(&i->buf);
616 
617 		closure_put(&trans->ref);
618 
619 		ret = bch2_debugfs_flush_buf(i);
620 		if (ret)
621 			goto unlocked;
622 
623 		if (!seqmutex_relock(&c->btree_trans_lock, seq))
624 			goto restart;
625 	}
626 	seqmutex_unlock(&c->btree_trans_lock);
627 unlocked:
628 	srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
629 
630 	if (i->buf.allocation_failure)
631 		ret = -ENOMEM;
632 
633 	if (!ret)
634 		ret = bch2_debugfs_flush_buf(i);
635 
636 	return ret ?: i->ret;
637 }
638 
639 static const struct file_operations btree_transactions_ops = {
640 	.owner		= THIS_MODULE,
641 	.open		= bch2_dump_open,
642 	.release	= bch2_dump_release,
643 	.read		= bch2_btree_transactions_read,
644 };
645 
bch2_journal_pins_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)646 static ssize_t bch2_journal_pins_read(struct file *file, char __user *buf,
647 				      size_t size, loff_t *ppos)
648 {
649 	struct dump_iter *i = file->private_data;
650 	struct bch_fs *c = i->c;
651 	bool done = false;
652 	int err;
653 
654 	i->ubuf = buf;
655 	i->size	= size;
656 	i->ret	= 0;
657 
658 	while (1) {
659 		err = bch2_debugfs_flush_buf(i);
660 		if (err)
661 			return err;
662 
663 		if (!i->size)
664 			break;
665 
666 		if (done)
667 			break;
668 
669 		done = bch2_journal_seq_pins_to_text(&i->buf, &c->journal, &i->iter);
670 		i->iter++;
671 	}
672 
673 	if (i->buf.allocation_failure)
674 		return -ENOMEM;
675 
676 	return i->ret;
677 }
678 
679 static const struct file_operations journal_pins_ops = {
680 	.owner		= THIS_MODULE,
681 	.open		= bch2_dump_open,
682 	.release	= bch2_dump_release,
683 	.read		= bch2_journal_pins_read,
684 };
685 
bch2_btree_updates_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)686 static ssize_t bch2_btree_updates_read(struct file *file, char __user *buf,
687 				       size_t size, loff_t *ppos)
688 {
689 	struct dump_iter *i = file->private_data;
690 	struct bch_fs *c = i->c;
691 	int err;
692 
693 	i->ubuf = buf;
694 	i->size	= size;
695 	i->ret	= 0;
696 
697 	if (!i->iter) {
698 		bch2_btree_updates_to_text(&i->buf, c);
699 		i->iter++;
700 	}
701 
702 	err = bch2_debugfs_flush_buf(i);
703 	if (err)
704 		return err;
705 
706 	if (i->buf.allocation_failure)
707 		return -ENOMEM;
708 
709 	return i->ret;
710 }
711 
712 static const struct file_operations btree_updates_ops = {
713 	.owner		= THIS_MODULE,
714 	.open		= bch2_dump_open,
715 	.release	= bch2_dump_release,
716 	.read		= bch2_btree_updates_read,
717 };
718 
btree_transaction_stats_open(struct inode * inode,struct file * file)719 static int btree_transaction_stats_open(struct inode *inode, struct file *file)
720 {
721 	struct bch_fs *c = inode->i_private;
722 	struct dump_iter *i;
723 
724 	i = kzalloc(sizeof(struct dump_iter), GFP_KERNEL);
725 	if (!i)
726 		return -ENOMEM;
727 
728 	i->iter = 1;
729 	i->c    = c;
730 	i->buf  = PRINTBUF;
731 	file->private_data = i;
732 
733 	return 0;
734 }
735 
btree_transaction_stats_release(struct inode * inode,struct file * file)736 static int btree_transaction_stats_release(struct inode *inode, struct file *file)
737 {
738 	struct dump_iter *i = file->private_data;
739 
740 	printbuf_exit(&i->buf);
741 	kfree(i);
742 
743 	return 0;
744 }
745 
btree_transaction_stats_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)746 static ssize_t btree_transaction_stats_read(struct file *file, char __user *buf,
747 					    size_t size, loff_t *ppos)
748 {
749 	struct dump_iter        *i = file->private_data;
750 	struct bch_fs *c = i->c;
751 	int err;
752 
753 	i->ubuf = buf;
754 	i->size = size;
755 	i->ret  = 0;
756 
757 	while (1) {
758 		struct btree_transaction_stats *s = &c->btree_transaction_stats[i->iter];
759 
760 		err = bch2_debugfs_flush_buf(i);
761 		if (err)
762 			return err;
763 
764 		if (!i->size)
765 			break;
766 
767 		if (i->iter == ARRAY_SIZE(bch2_btree_transaction_fns) ||
768 		    !bch2_btree_transaction_fns[i->iter])
769 			break;
770 
771 		prt_printf(&i->buf, "%s:\n", bch2_btree_transaction_fns[i->iter]);
772 		printbuf_indent_add(&i->buf, 2);
773 
774 		mutex_lock(&s->lock);
775 
776 		prt_printf(&i->buf, "Max mem used: %u\n", s->max_mem);
777 #ifdef CONFIG_BCACHEFS_TRANS_KMALLOC_TRACE
778 		printbuf_indent_add(&i->buf, 2);
779 		bch2_trans_kmalloc_trace_to_text(&i->buf, &s->trans_kmalloc_trace);
780 		printbuf_indent_sub(&i->buf, 2);
781 #endif
782 
783 		prt_printf(&i->buf, "Transaction duration:\n");
784 
785 		printbuf_indent_add(&i->buf, 2);
786 		bch2_time_stats_to_text(&i->buf, &s->duration);
787 		printbuf_indent_sub(&i->buf, 2);
788 
789 		if (IS_ENABLED(CONFIG_BCACHEFS_LOCK_TIME_STATS)) {
790 			prt_printf(&i->buf, "Lock hold times:\n");
791 
792 			printbuf_indent_add(&i->buf, 2);
793 			bch2_time_stats_to_text(&i->buf, &s->lock_hold_times);
794 			printbuf_indent_sub(&i->buf, 2);
795 		}
796 
797 		if (s->max_paths_text) {
798 			prt_printf(&i->buf, "Maximum allocated btree paths (%u):\n", s->nr_max_paths);
799 
800 			printbuf_indent_add(&i->buf, 2);
801 			prt_str_indented(&i->buf, s->max_paths_text);
802 			printbuf_indent_sub(&i->buf, 2);
803 		}
804 
805 		mutex_unlock(&s->lock);
806 
807 		printbuf_indent_sub(&i->buf, 2);
808 		prt_newline(&i->buf);
809 		i->iter++;
810 	}
811 
812 	if (i->buf.allocation_failure)
813 		return -ENOMEM;
814 
815 	return i->ret;
816 }
817 
818 static const struct file_operations btree_transaction_stats_op = {
819 	.owner		= THIS_MODULE,
820 	.open		= btree_transaction_stats_open,
821 	.release	= btree_transaction_stats_release,
822 	.read		= btree_transaction_stats_read,
823 };
824 
825 /* walk btree transactions until we find a deadlock and print it */
btree_deadlock_to_text(struct printbuf * out,struct bch_fs * c)826 static void btree_deadlock_to_text(struct printbuf *out, struct bch_fs *c)
827 {
828 	struct btree_trans *trans;
829 	ulong iter = 0;
830 restart:
831 	seqmutex_lock(&c->btree_trans_lock);
832 	list_sort(&c->btree_trans_list, list_ptr_order_cmp);
833 
834 	list_for_each_entry(trans, &c->btree_trans_list, list) {
835 		if ((ulong) trans <= iter)
836 			continue;
837 
838 		iter = (ulong) trans;
839 
840 		if (!closure_get_not_zero(&trans->ref))
841 			continue;
842 
843 		u32 seq = seqmutex_unlock(&c->btree_trans_lock);
844 
845 		bool found = bch2_check_for_deadlock(trans, out) != 0;
846 
847 		closure_put(&trans->ref);
848 
849 		if (found)
850 			return;
851 
852 		if (!seqmutex_relock(&c->btree_trans_lock, seq))
853 			goto restart;
854 	}
855 	seqmutex_unlock(&c->btree_trans_lock);
856 }
857 
858 typedef void (*fs_to_text_fn)(struct printbuf *, struct bch_fs *);
859 
bch2_simple_print(struct file * file,char __user * buf,size_t size,loff_t * ppos,fs_to_text_fn fn)860 static ssize_t bch2_simple_print(struct file *file, char __user *buf,
861 				 size_t size, loff_t *ppos,
862 				 fs_to_text_fn fn)
863 {
864 	struct dump_iter *i = file->private_data;
865 	struct bch_fs *c = i->c;
866 	ssize_t ret = 0;
867 
868 	i->ubuf = buf;
869 	i->size	= size;
870 	i->ret	= 0;
871 
872 	if (!i->iter) {
873 		fn(&i->buf, c);
874 		i->iter++;
875 	}
876 
877 	if (i->buf.allocation_failure)
878 		ret = -ENOMEM;
879 
880 	if (!ret)
881 		ret = bch2_debugfs_flush_buf(i);
882 
883 	return ret ?: i->ret;
884 }
885 
bch2_btree_deadlock_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)886 static ssize_t bch2_btree_deadlock_read(struct file *file, char __user *buf,
887 					size_t size, loff_t *ppos)
888 {
889 	return bch2_simple_print(file, buf, size, ppos, btree_deadlock_to_text);
890 }
891 
892 static const struct file_operations btree_deadlock_ops = {
893 	.owner		= THIS_MODULE,
894 	.open		= bch2_dump_open,
895 	.release	= bch2_dump_release,
896 	.read		= bch2_btree_deadlock_read,
897 };
898 
bch2_write_points_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)899 static ssize_t bch2_write_points_read(struct file *file, char __user *buf,
900 				     size_t size, loff_t *ppos)
901 {
902 	return bch2_simple_print(file, buf, size, ppos, bch2_write_points_to_text);
903 }
904 
905 static const struct file_operations write_points_ops = {
906 	.owner		= THIS_MODULE,
907 	.open		= bch2_dump_open,
908 	.release	= bch2_dump_release,
909 	.read		= bch2_write_points_read,
910 };
911 
bch2_fs_debug_exit(struct bch_fs * c)912 void bch2_fs_debug_exit(struct bch_fs *c)
913 {
914 	if (!IS_ERR_OR_NULL(c->fs_debug_dir))
915 		debugfs_remove_recursive(c->fs_debug_dir);
916 }
917 
bch2_fs_debug_btree_init(struct bch_fs * c,struct btree_debug * bd)918 static void bch2_fs_debug_btree_init(struct bch_fs *c, struct btree_debug *bd)
919 {
920 	struct dentry *d;
921 
922 	d = debugfs_create_dir(bch2_btree_id_str(bd->id), c->btree_debug_dir);
923 
924 	debugfs_create_file("keys", 0400, d, bd, &btree_debug_ops);
925 
926 	debugfs_create_file("formats", 0400, d, bd, &btree_format_debug_ops);
927 
928 	debugfs_create_file("bfloat-failed", 0400, d, bd,
929 			    &bfloat_failed_debug_ops);
930 }
931 
bch2_fs_debug_init(struct bch_fs * c)932 void bch2_fs_debug_init(struct bch_fs *c)
933 {
934 	struct btree_debug *bd;
935 	char name[100];
936 
937 	if (IS_ERR_OR_NULL(bch_debug))
938 		return;
939 
940 	if (c->sb.multi_device)
941 		snprintf(name, sizeof(name), "%pU", c->sb.user_uuid.b);
942 	else
943 		strscpy(name, c->name, sizeof(name));
944 
945 	c->fs_debug_dir = debugfs_create_dir(name, bch_debug);
946 	if (IS_ERR_OR_NULL(c->fs_debug_dir))
947 		return;
948 
949 	debugfs_create_file("cached_btree_nodes", 0400, c->fs_debug_dir,
950 			    c->btree_debug, &cached_btree_nodes_ops);
951 
952 	debugfs_create_file("btree_transactions", 0400, c->fs_debug_dir,
953 			    c->btree_debug, &btree_transactions_ops);
954 
955 	debugfs_create_file("journal_pins", 0400, c->fs_debug_dir,
956 			    c->btree_debug, &journal_pins_ops);
957 
958 	debugfs_create_file("btree_updates", 0400, c->fs_debug_dir,
959 			    c->btree_debug, &btree_updates_ops);
960 
961 	debugfs_create_file("btree_transaction_stats", 0400, c->fs_debug_dir,
962 			    c, &btree_transaction_stats_op);
963 
964 	debugfs_create_file("btree_deadlock", 0400, c->fs_debug_dir,
965 			    c->btree_debug, &btree_deadlock_ops);
966 
967 	debugfs_create_file("write_points", 0400, c->fs_debug_dir,
968 			    c->btree_debug, &write_points_ops);
969 
970 	bch2_fs_async_obj_debugfs_init(c);
971 
972 	c->btree_debug_dir = debugfs_create_dir("btrees", c->fs_debug_dir);
973 	if (IS_ERR_OR_NULL(c->btree_debug_dir))
974 		return;
975 
976 	for (bd = c->btree_debug;
977 	     bd < c->btree_debug + ARRAY_SIZE(c->btree_debug);
978 	     bd++) {
979 		bd->id = bd - c->btree_debug;
980 		bch2_fs_debug_btree_init(c, bd);
981 	}
982 }
983 
984 #endif
985 
bch2_debug_exit(void)986 void bch2_debug_exit(void)
987 {
988 	if (!IS_ERR_OR_NULL(bch_debug))
989 		debugfs_remove_recursive(bch_debug);
990 }
991 
bch2_debug_init(void)992 int __init bch2_debug_init(void)
993 {
994 	bch_debug = debugfs_create_dir("bcachefs", NULL);
995 	return 0;
996 }
997