xref: /linux-6.15/fs/btrfs/ref-verify.c (revision fc97a410)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Facebook.  All rights reserved.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/stacktrace.h>
8 #include "messages.h"
9 #include "ctree.h"
10 #include "disk-io.h"
11 #include "locking.h"
12 #include "delayed-ref.h"
13 #include "ref-verify.h"
14 #include "fs.h"
15 
16 /*
17  * Used to keep track the roots and number of refs each root has for a given
18  * bytenr.  This just tracks the number of direct references, no shared
19  * references.
20  */
21 struct root_entry {
22 	u64 root_objectid;
23 	u64 num_refs;
24 	struct rb_node node;
25 };
26 
27 /*
28  * These are meant to represent what should exist in the extent tree, these can
29  * be used to verify the extent tree is consistent as these should all match
30  * what the extent tree says.
31  */
32 struct ref_entry {
33 	u64 root_objectid;
34 	u64 parent;
35 	u64 owner;
36 	u64 offset;
37 	u64 num_refs;
38 	struct rb_node node;
39 };
40 
41 #define MAX_TRACE	16
42 
43 /*
44  * Whenever we add/remove a reference we record the action.  The action maps
45  * back to the delayed ref action.  We hold the ref we are changing in the
46  * action so we can account for the history properly, and we record the root we
47  * were called with since it could be different from ref_root.  We also store
48  * stack traces because that's how I roll.
49  */
50 struct ref_action {
51 	int action;
52 	u64 root;
53 	struct ref_entry ref;
54 	struct list_head list;
55 	unsigned long trace[MAX_TRACE];
56 	unsigned int trace_len;
57 };
58 
59 /*
60  * One of these for every block we reference, it holds the roots and references
61  * to it as well as all of the ref actions that have occurred to it.  We never
62  * free it until we unmount the file system in order to make sure re-allocations
63  * are happening properly.
64  */
65 struct block_entry {
66 	u64 bytenr;
67 	u64 len;
68 	u64 num_refs;
69 	int metadata;
70 	int from_disk;
71 	struct rb_root roots;
72 	struct rb_root refs;
73 	struct rb_node node;
74 	struct list_head actions;
75 };
76 
77 static struct block_entry *insert_block_entry(struct rb_root *root,
78 					      struct block_entry *be)
79 {
80 	struct rb_node **p = &root->rb_node;
81 	struct rb_node *parent_node = NULL;
82 	struct block_entry *entry;
83 
84 	while (*p) {
85 		parent_node = *p;
86 		entry = rb_entry(parent_node, struct block_entry, node);
87 		if (entry->bytenr > be->bytenr)
88 			p = &(*p)->rb_left;
89 		else if (entry->bytenr < be->bytenr)
90 			p = &(*p)->rb_right;
91 		else
92 			return entry;
93 	}
94 
95 	rb_link_node(&be->node, parent_node, p);
96 	rb_insert_color(&be->node, root);
97 	return NULL;
98 }
99 
100 static struct block_entry *lookup_block_entry(struct rb_root *root, u64 bytenr)
101 {
102 	struct rb_node *n;
103 	struct block_entry *entry = NULL;
104 
105 	n = root->rb_node;
106 	while (n) {
107 		entry = rb_entry(n, struct block_entry, node);
108 		if (entry->bytenr < bytenr)
109 			n = n->rb_right;
110 		else if (entry->bytenr > bytenr)
111 			n = n->rb_left;
112 		else
113 			return entry;
114 	}
115 	return NULL;
116 }
117 
118 static struct root_entry *insert_root_entry(struct rb_root *root,
119 					    struct root_entry *re)
120 {
121 	struct rb_node **p = &root->rb_node;
122 	struct rb_node *parent_node = NULL;
123 	struct root_entry *entry;
124 
125 	while (*p) {
126 		parent_node = *p;
127 		entry = rb_entry(parent_node, struct root_entry, node);
128 		if (entry->root_objectid > re->root_objectid)
129 			p = &(*p)->rb_left;
130 		else if (entry->root_objectid < re->root_objectid)
131 			p = &(*p)->rb_right;
132 		else
133 			return entry;
134 	}
135 
136 	rb_link_node(&re->node, parent_node, p);
137 	rb_insert_color(&re->node, root);
138 	return NULL;
139 
140 }
141 
142 static int comp_refs(struct ref_entry *ref1, struct ref_entry *ref2)
143 {
144 	if (ref1->root_objectid < ref2->root_objectid)
145 		return -1;
146 	if (ref1->root_objectid > ref2->root_objectid)
147 		return 1;
148 	if (ref1->parent < ref2->parent)
149 		return -1;
150 	if (ref1->parent > ref2->parent)
151 		return 1;
152 	if (ref1->owner < ref2->owner)
153 		return -1;
154 	if (ref1->owner > ref2->owner)
155 		return 1;
156 	if (ref1->offset < ref2->offset)
157 		return -1;
158 	if (ref1->offset > ref2->offset)
159 		return 1;
160 	return 0;
161 }
162 
163 static struct ref_entry *insert_ref_entry(struct rb_root *root,
164 					  struct ref_entry *ref)
165 {
166 	struct rb_node **p = &root->rb_node;
167 	struct rb_node *parent_node = NULL;
168 	struct ref_entry *entry;
169 	int cmp;
170 
171 	while (*p) {
172 		parent_node = *p;
173 		entry = rb_entry(parent_node, struct ref_entry, node);
174 		cmp = comp_refs(entry, ref);
175 		if (cmp > 0)
176 			p = &(*p)->rb_left;
177 		else if (cmp < 0)
178 			p = &(*p)->rb_right;
179 		else
180 			return entry;
181 	}
182 
183 	rb_link_node(&ref->node, parent_node, p);
184 	rb_insert_color(&ref->node, root);
185 	return NULL;
186 
187 }
188 
189 static struct root_entry *lookup_root_entry(struct rb_root *root, u64 objectid)
190 {
191 	struct rb_node *n;
192 	struct root_entry *entry = NULL;
193 
194 	n = root->rb_node;
195 	while (n) {
196 		entry = rb_entry(n, struct root_entry, node);
197 		if (entry->root_objectid < objectid)
198 			n = n->rb_right;
199 		else if (entry->root_objectid > objectid)
200 			n = n->rb_left;
201 		else
202 			return entry;
203 	}
204 	return NULL;
205 }
206 
207 #ifdef CONFIG_STACKTRACE
208 static void __save_stack_trace(struct ref_action *ra)
209 {
210 	ra->trace_len = stack_trace_save(ra->trace, MAX_TRACE, 2);
211 }
212 
213 static void __print_stack_trace(struct btrfs_fs_info *fs_info,
214 				struct ref_action *ra)
215 {
216 	if (ra->trace_len == 0) {
217 		btrfs_err(fs_info, "  ref-verify: no stacktrace");
218 		return;
219 	}
220 	stack_trace_print(ra->trace, ra->trace_len, 2);
221 }
222 #else
223 static inline void __save_stack_trace(struct ref_action *ra)
224 {
225 }
226 
227 static inline void __print_stack_trace(struct btrfs_fs_info *fs_info,
228 				       struct ref_action *ra)
229 {
230 	btrfs_err(fs_info, "  ref-verify: no stacktrace support");
231 }
232 #endif
233 
234 static void free_block_entry(struct block_entry *be)
235 {
236 	struct root_entry *re;
237 	struct ref_entry *ref;
238 	struct ref_action *ra;
239 	struct rb_node *n;
240 
241 	while ((n = rb_first(&be->roots))) {
242 		re = rb_entry(n, struct root_entry, node);
243 		rb_erase(&re->node, &be->roots);
244 		kfree(re);
245 	}
246 
247 	while((n = rb_first(&be->refs))) {
248 		ref = rb_entry(n, struct ref_entry, node);
249 		rb_erase(&ref->node, &be->refs);
250 		kfree(ref);
251 	}
252 
253 	while (!list_empty(&be->actions)) {
254 		ra = list_first_entry(&be->actions, struct ref_action,
255 				      list);
256 		list_del(&ra->list);
257 		kfree(ra);
258 	}
259 	kfree(be);
260 }
261 
262 static struct block_entry *add_block_entry(struct btrfs_fs_info *fs_info,
263 					   u64 bytenr, u64 len,
264 					   u64 root_objectid)
265 {
266 	struct block_entry *be = NULL, *exist;
267 	struct root_entry *re = NULL;
268 
269 	re = kzalloc(sizeof(struct root_entry), GFP_NOFS);
270 	be = kzalloc(sizeof(struct block_entry), GFP_NOFS);
271 	if (!be || !re) {
272 		kfree(re);
273 		kfree(be);
274 		return ERR_PTR(-ENOMEM);
275 	}
276 	be->bytenr = bytenr;
277 	be->len = len;
278 
279 	re->root_objectid = root_objectid;
280 	re->num_refs = 0;
281 
282 	spin_lock(&fs_info->ref_verify_lock);
283 	exist = insert_block_entry(&fs_info->block_tree, be);
284 	if (exist) {
285 		if (root_objectid) {
286 			struct root_entry *exist_re;
287 
288 			exist_re = insert_root_entry(&exist->roots, re);
289 			if (exist_re)
290 				kfree(re);
291 		} else {
292 			kfree(re);
293 		}
294 		kfree(be);
295 		return exist;
296 	}
297 
298 	be->num_refs = 0;
299 	be->metadata = 0;
300 	be->from_disk = 0;
301 	be->roots = RB_ROOT;
302 	be->refs = RB_ROOT;
303 	INIT_LIST_HEAD(&be->actions);
304 	if (root_objectid)
305 		insert_root_entry(&be->roots, re);
306 	else
307 		kfree(re);
308 	return be;
309 }
310 
311 static int add_tree_block(struct btrfs_fs_info *fs_info, u64 ref_root,
312 			  u64 parent, u64 bytenr, int level)
313 {
314 	struct block_entry *be;
315 	struct root_entry *re;
316 	struct ref_entry *ref = NULL, *exist;
317 
318 	ref = kmalloc(sizeof(struct ref_entry), GFP_NOFS);
319 	if (!ref)
320 		return -ENOMEM;
321 
322 	if (parent)
323 		ref->root_objectid = 0;
324 	else
325 		ref->root_objectid = ref_root;
326 	ref->parent = parent;
327 	ref->owner = level;
328 	ref->offset = 0;
329 	ref->num_refs = 1;
330 
331 	be = add_block_entry(fs_info, bytenr, fs_info->nodesize, ref_root);
332 	if (IS_ERR(be)) {
333 		kfree(ref);
334 		return PTR_ERR(be);
335 	}
336 	be->num_refs++;
337 	be->from_disk = 1;
338 	be->metadata = 1;
339 
340 	if (!parent) {
341 		ASSERT(ref_root);
342 		re = lookup_root_entry(&be->roots, ref_root);
343 		ASSERT(re);
344 		re->num_refs++;
345 	}
346 	exist = insert_ref_entry(&be->refs, ref);
347 	if (exist) {
348 		exist->num_refs++;
349 		kfree(ref);
350 	}
351 	spin_unlock(&fs_info->ref_verify_lock);
352 
353 	return 0;
354 }
355 
356 static int add_shared_data_ref(struct btrfs_fs_info *fs_info,
357 			       u64 parent, u32 num_refs, u64 bytenr,
358 			       u64 num_bytes)
359 {
360 	struct block_entry *be;
361 	struct ref_entry *ref;
362 
363 	ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
364 	if (!ref)
365 		return -ENOMEM;
366 	be = add_block_entry(fs_info, bytenr, num_bytes, 0);
367 	if (IS_ERR(be)) {
368 		kfree(ref);
369 		return PTR_ERR(be);
370 	}
371 	be->num_refs += num_refs;
372 
373 	ref->parent = parent;
374 	ref->num_refs = num_refs;
375 	if (insert_ref_entry(&be->refs, ref)) {
376 		spin_unlock(&fs_info->ref_verify_lock);
377 		btrfs_err(fs_info, "existing shared ref when reading from disk?");
378 		kfree(ref);
379 		return -EINVAL;
380 	}
381 	spin_unlock(&fs_info->ref_verify_lock);
382 	return 0;
383 }
384 
385 static int add_extent_data_ref(struct btrfs_fs_info *fs_info,
386 			       struct extent_buffer *leaf,
387 			       struct btrfs_extent_data_ref *dref,
388 			       u64 bytenr, u64 num_bytes)
389 {
390 	struct block_entry *be;
391 	struct ref_entry *ref;
392 	struct root_entry *re;
393 	u64 ref_root = btrfs_extent_data_ref_root(leaf, dref);
394 	u64 owner = btrfs_extent_data_ref_objectid(leaf, dref);
395 	u64 offset = btrfs_extent_data_ref_offset(leaf, dref);
396 	u32 num_refs = btrfs_extent_data_ref_count(leaf, dref);
397 
398 	ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
399 	if (!ref)
400 		return -ENOMEM;
401 	be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
402 	if (IS_ERR(be)) {
403 		kfree(ref);
404 		return PTR_ERR(be);
405 	}
406 	be->num_refs += num_refs;
407 
408 	ref->parent = 0;
409 	ref->owner = owner;
410 	ref->root_objectid = ref_root;
411 	ref->offset = offset;
412 	ref->num_refs = num_refs;
413 	if (insert_ref_entry(&be->refs, ref)) {
414 		spin_unlock(&fs_info->ref_verify_lock);
415 		btrfs_err(fs_info, "existing ref when reading from disk?");
416 		kfree(ref);
417 		return -EINVAL;
418 	}
419 
420 	re = lookup_root_entry(&be->roots, ref_root);
421 	if (!re) {
422 		spin_unlock(&fs_info->ref_verify_lock);
423 		btrfs_err(fs_info, "missing root in new block entry?");
424 		return -EINVAL;
425 	}
426 	re->num_refs += num_refs;
427 	spin_unlock(&fs_info->ref_verify_lock);
428 	return 0;
429 }
430 
431 static int process_extent_item(struct btrfs_fs_info *fs_info,
432 			       struct btrfs_path *path, struct btrfs_key *key,
433 			       int slot, int *tree_block_level)
434 {
435 	struct btrfs_extent_item *ei;
436 	struct btrfs_extent_inline_ref *iref;
437 	struct btrfs_extent_data_ref *dref;
438 	struct btrfs_shared_data_ref *sref;
439 	struct extent_buffer *leaf = path->nodes[0];
440 	u32 item_size = btrfs_item_size(leaf, slot);
441 	unsigned long end, ptr;
442 	u64 offset, flags, count;
443 	int type, ret;
444 
445 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
446 	flags = btrfs_extent_flags(leaf, ei);
447 
448 	if ((key->type == BTRFS_EXTENT_ITEM_KEY) &&
449 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
450 		struct btrfs_tree_block_info *info;
451 
452 		info = (struct btrfs_tree_block_info *)(ei + 1);
453 		*tree_block_level = btrfs_tree_block_level(leaf, info);
454 		iref = (struct btrfs_extent_inline_ref *)(info + 1);
455 	} else {
456 		if (key->type == BTRFS_METADATA_ITEM_KEY)
457 			*tree_block_level = key->offset;
458 		iref = (struct btrfs_extent_inline_ref *)(ei + 1);
459 	}
460 
461 	ptr = (unsigned long)iref;
462 	end = (unsigned long)ei + item_size;
463 	while (ptr < end) {
464 		iref = (struct btrfs_extent_inline_ref *)ptr;
465 		type = btrfs_extent_inline_ref_type(leaf, iref);
466 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
467 		switch (type) {
468 		case BTRFS_TREE_BLOCK_REF_KEY:
469 			ret = add_tree_block(fs_info, offset, 0, key->objectid,
470 					     *tree_block_level);
471 			break;
472 		case BTRFS_SHARED_BLOCK_REF_KEY:
473 			ret = add_tree_block(fs_info, 0, offset, key->objectid,
474 					     *tree_block_level);
475 			break;
476 		case BTRFS_EXTENT_DATA_REF_KEY:
477 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
478 			ret = add_extent_data_ref(fs_info, leaf, dref,
479 						  key->objectid, key->offset);
480 			break;
481 		case BTRFS_SHARED_DATA_REF_KEY:
482 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
483 			count = btrfs_shared_data_ref_count(leaf, sref);
484 			ret = add_shared_data_ref(fs_info, offset, count,
485 						  key->objectid, key->offset);
486 			break;
487 		default:
488 			btrfs_err(fs_info, "invalid key type in iref");
489 			ret = -EINVAL;
490 			break;
491 		}
492 		if (ret)
493 			break;
494 		ptr += btrfs_extent_inline_ref_size(type);
495 	}
496 	return ret;
497 }
498 
499 static int process_leaf(struct btrfs_root *root,
500 			struct btrfs_path *path, u64 *bytenr, u64 *num_bytes,
501 			int *tree_block_level)
502 {
503 	struct btrfs_fs_info *fs_info = root->fs_info;
504 	struct extent_buffer *leaf = path->nodes[0];
505 	struct btrfs_extent_data_ref *dref;
506 	struct btrfs_shared_data_ref *sref;
507 	u32 count;
508 	int i = 0, ret = 0;
509 	struct btrfs_key key;
510 	int nritems = btrfs_header_nritems(leaf);
511 
512 	for (i = 0; i < nritems; i++) {
513 		btrfs_item_key_to_cpu(leaf, &key, i);
514 		switch (key.type) {
515 		case BTRFS_EXTENT_ITEM_KEY:
516 			*num_bytes = key.offset;
517 			fallthrough;
518 		case BTRFS_METADATA_ITEM_KEY:
519 			*bytenr = key.objectid;
520 			ret = process_extent_item(fs_info, path, &key, i,
521 						  tree_block_level);
522 			break;
523 		case BTRFS_TREE_BLOCK_REF_KEY:
524 			ret = add_tree_block(fs_info, key.offset, 0,
525 					     key.objectid, *tree_block_level);
526 			break;
527 		case BTRFS_SHARED_BLOCK_REF_KEY:
528 			ret = add_tree_block(fs_info, 0, key.offset,
529 					     key.objectid, *tree_block_level);
530 			break;
531 		case BTRFS_EXTENT_DATA_REF_KEY:
532 			dref = btrfs_item_ptr(leaf, i,
533 					      struct btrfs_extent_data_ref);
534 			ret = add_extent_data_ref(fs_info, leaf, dref, *bytenr,
535 						  *num_bytes);
536 			break;
537 		case BTRFS_SHARED_DATA_REF_KEY:
538 			sref = btrfs_item_ptr(leaf, i,
539 					      struct btrfs_shared_data_ref);
540 			count = btrfs_shared_data_ref_count(leaf, sref);
541 			ret = add_shared_data_ref(fs_info, key.offset, count,
542 						  *bytenr, *num_bytes);
543 			break;
544 		default:
545 			break;
546 		}
547 		if (ret)
548 			break;
549 	}
550 	return ret;
551 }
552 
553 /* Walk down to the leaf from the given level */
554 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
555 			  int level, u64 *bytenr, u64 *num_bytes,
556 			  int *tree_block_level)
557 {
558 	struct extent_buffer *eb;
559 	int ret = 0;
560 
561 	while (level >= 0) {
562 		if (level) {
563 			eb = btrfs_read_node_slot(path->nodes[level],
564 						  path->slots[level]);
565 			if (IS_ERR(eb))
566 				return PTR_ERR(eb);
567 			btrfs_tree_read_lock(eb);
568 			path->nodes[level-1] = eb;
569 			path->slots[level-1] = 0;
570 			path->locks[level-1] = BTRFS_READ_LOCK;
571 		} else {
572 			ret = process_leaf(root, path, bytenr, num_bytes,
573 					   tree_block_level);
574 			if (ret)
575 				break;
576 		}
577 		level--;
578 	}
579 	return ret;
580 }
581 
582 /* Walk up to the next node that needs to be processed */
583 static int walk_up_tree(struct btrfs_path *path, int *level)
584 {
585 	int l;
586 
587 	for (l = 0; l < BTRFS_MAX_LEVEL; l++) {
588 		if (!path->nodes[l])
589 			continue;
590 		if (l) {
591 			path->slots[l]++;
592 			if (path->slots[l] <
593 			    btrfs_header_nritems(path->nodes[l])) {
594 				*level = l;
595 				return 0;
596 			}
597 		}
598 		btrfs_tree_unlock_rw(path->nodes[l], path->locks[l]);
599 		free_extent_buffer(path->nodes[l]);
600 		path->nodes[l] = NULL;
601 		path->slots[l] = 0;
602 		path->locks[l] = 0;
603 	}
604 
605 	return 1;
606 }
607 
608 static void dump_ref_action(struct btrfs_fs_info *fs_info,
609 			    struct ref_action *ra)
610 {
611 	btrfs_err(fs_info,
612 "  Ref action %d, root %llu, ref_root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
613 		  ra->action, ra->root, ra->ref.root_objectid, ra->ref.parent,
614 		  ra->ref.owner, ra->ref.offset, ra->ref.num_refs);
615 	__print_stack_trace(fs_info, ra);
616 }
617 
618 /*
619  * Dumps all the information from the block entry to printk, it's going to be
620  * awesome.
621  */
622 static void dump_block_entry(struct btrfs_fs_info *fs_info,
623 			     struct block_entry *be)
624 {
625 	struct ref_entry *ref;
626 	struct root_entry *re;
627 	struct ref_action *ra;
628 	struct rb_node *n;
629 
630 	btrfs_err(fs_info,
631 "dumping block entry [%llu %llu], num_refs %llu, metadata %d, from disk %d",
632 		  be->bytenr, be->len, be->num_refs, be->metadata,
633 		  be->from_disk);
634 
635 	for (n = rb_first(&be->refs); n; n = rb_next(n)) {
636 		ref = rb_entry(n, struct ref_entry, node);
637 		btrfs_err(fs_info,
638 "  ref root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
639 			  ref->root_objectid, ref->parent, ref->owner,
640 			  ref->offset, ref->num_refs);
641 	}
642 
643 	for (n = rb_first(&be->roots); n; n = rb_next(n)) {
644 		re = rb_entry(n, struct root_entry, node);
645 		btrfs_err(fs_info, "  root entry %llu, num_refs %llu",
646 			  re->root_objectid, re->num_refs);
647 	}
648 
649 	list_for_each_entry(ra, &be->actions, list)
650 		dump_ref_action(fs_info, ra);
651 }
652 
653 /*
654  * btrfs_ref_tree_mod: called when we modify a ref for a bytenr
655  *
656  * This will add an action item to the given bytenr and do sanity checks to make
657  * sure we haven't messed something up.  If we are making a new allocation and
658  * this block entry has history we will delete all previous actions as long as
659  * our sanity checks pass as they are no longer needed.
660  */
661 int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
662 		       struct btrfs_ref *generic_ref)
663 {
664 	struct ref_entry *ref = NULL, *exist;
665 	struct ref_action *ra = NULL;
666 	struct block_entry *be = NULL;
667 	struct root_entry *re = NULL;
668 	int action = generic_ref->action;
669 	int ret = 0;
670 	bool metadata;
671 	u64 bytenr = generic_ref->bytenr;
672 	u64 num_bytes = generic_ref->len;
673 	u64 parent = generic_ref->parent;
674 	u64 ref_root = 0;
675 	u64 owner = 0;
676 	u64 offset = 0;
677 
678 	if (!btrfs_test_opt(fs_info, REF_VERIFY))
679 		return 0;
680 
681 	if (generic_ref->type == BTRFS_REF_METADATA) {
682 		if (!parent)
683 			ref_root = generic_ref->tree_ref.owning_root;
684 		owner = generic_ref->tree_ref.level;
685 	} else if (!parent) {
686 		ref_root = generic_ref->data_ref.owning_root;
687 		owner = generic_ref->data_ref.ino;
688 		offset = generic_ref->data_ref.offset;
689 	}
690 	metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
691 
692 	ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
693 	ra = kmalloc(sizeof(struct ref_action), GFP_NOFS);
694 	if (!ra || !ref) {
695 		kfree(ref);
696 		kfree(ra);
697 		ret = -ENOMEM;
698 		goto out;
699 	}
700 
701 	ref->parent = parent;
702 	ref->owner = owner;
703 	ref->root_objectid = ref_root;
704 	ref->offset = offset;
705 	ref->num_refs = (action == BTRFS_DROP_DELAYED_REF) ? -1 : 1;
706 
707 	memcpy(&ra->ref, ref, sizeof(struct ref_entry));
708 	/*
709 	 * Save the extra info from the delayed ref in the ref action to make it
710 	 * easier to figure out what is happening.  The real ref's we add to the
711 	 * ref tree need to reflect what we save on disk so it matches any
712 	 * on-disk refs we pre-loaded.
713 	 */
714 	ra->ref.owner = owner;
715 	ra->ref.offset = offset;
716 	ra->ref.root_objectid = ref_root;
717 	__save_stack_trace(ra);
718 
719 	INIT_LIST_HEAD(&ra->list);
720 	ra->action = action;
721 	ra->root = generic_ref->real_root;
722 
723 	/*
724 	 * This is an allocation, preallocate the block_entry in case we haven't
725 	 * used it before.
726 	 */
727 	ret = -EINVAL;
728 	if (action == BTRFS_ADD_DELAYED_EXTENT) {
729 		/*
730 		 * For subvol_create we'll just pass in whatever the parent root
731 		 * is and the new root objectid, so let's not treat the passed
732 		 * in root as if it really has a ref for this bytenr.
733 		 */
734 		be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
735 		if (IS_ERR(be)) {
736 			kfree(ref);
737 			kfree(ra);
738 			ret = PTR_ERR(be);
739 			goto out;
740 		}
741 		be->num_refs++;
742 		if (metadata)
743 			be->metadata = 1;
744 
745 		if (be->num_refs != 1) {
746 			btrfs_err(fs_info,
747 			"re-allocated a block that still has references to it!");
748 			dump_block_entry(fs_info, be);
749 			dump_ref_action(fs_info, ra);
750 			kfree(ref);
751 			kfree(ra);
752 			goto out_unlock;
753 		}
754 
755 		while (!list_empty(&be->actions)) {
756 			struct ref_action *tmp;
757 
758 			tmp = list_first_entry(&be->actions, struct ref_action,
759 					       list);
760 			list_del(&tmp->list);
761 			kfree(tmp);
762 		}
763 	} else {
764 		struct root_entry *tmp;
765 
766 		if (!parent) {
767 			re = kmalloc(sizeof(struct root_entry), GFP_NOFS);
768 			if (!re) {
769 				kfree(ref);
770 				kfree(ra);
771 				ret = -ENOMEM;
772 				goto out;
773 			}
774 			/*
775 			 * This is the root that is modifying us, so it's the
776 			 * one we want to lookup below when we modify the
777 			 * re->num_refs.
778 			 */
779 			ref_root = generic_ref->real_root;
780 			re->root_objectid = generic_ref->real_root;
781 			re->num_refs = 0;
782 		}
783 
784 		spin_lock(&fs_info->ref_verify_lock);
785 		be = lookup_block_entry(&fs_info->block_tree, bytenr);
786 		if (!be) {
787 			btrfs_err(fs_info,
788 "trying to do action %d to bytenr %llu num_bytes %llu but there is no existing entry!",
789 				  action, bytenr, num_bytes);
790 			dump_ref_action(fs_info, ra);
791 			kfree(ref);
792 			kfree(ra);
793 			goto out_unlock;
794 		} else if (be->num_refs == 0) {
795 			btrfs_err(fs_info,
796 		"trying to do action %d for a bytenr that has 0 total references",
797 				action);
798 			dump_block_entry(fs_info, be);
799 			dump_ref_action(fs_info, ra);
800 			kfree(ref);
801 			kfree(ra);
802 			goto out_unlock;
803 		}
804 
805 		if (!parent) {
806 			tmp = insert_root_entry(&be->roots, re);
807 			if (tmp) {
808 				kfree(re);
809 				re = tmp;
810 			}
811 		}
812 	}
813 
814 	exist = insert_ref_entry(&be->refs, ref);
815 	if (exist) {
816 		if (action == BTRFS_DROP_DELAYED_REF) {
817 			if (exist->num_refs == 0) {
818 				btrfs_err(fs_info,
819 "dropping a ref for a existing root that doesn't have a ref on the block");
820 				dump_block_entry(fs_info, be);
821 				dump_ref_action(fs_info, ra);
822 				kfree(ref);
823 				kfree(ra);
824 				goto out_unlock;
825 			}
826 			exist->num_refs--;
827 			if (exist->num_refs == 0) {
828 				rb_erase(&exist->node, &be->refs);
829 				kfree(exist);
830 			}
831 		} else if (!be->metadata) {
832 			exist->num_refs++;
833 		} else {
834 			btrfs_err(fs_info,
835 "attempting to add another ref for an existing ref on a tree block");
836 			dump_block_entry(fs_info, be);
837 			dump_ref_action(fs_info, ra);
838 			kfree(ref);
839 			kfree(ra);
840 			goto out_unlock;
841 		}
842 		kfree(ref);
843 	} else {
844 		if (action == BTRFS_DROP_DELAYED_REF) {
845 			btrfs_err(fs_info,
846 "dropping a ref for a root that doesn't have a ref on the block");
847 			dump_block_entry(fs_info, be);
848 			dump_ref_action(fs_info, ra);
849 			kfree(ref);
850 			kfree(ra);
851 			goto out_unlock;
852 		}
853 	}
854 
855 	if (!parent && !re) {
856 		re = lookup_root_entry(&be->roots, ref_root);
857 		if (!re) {
858 			/*
859 			 * This shouldn't happen because we will add our re
860 			 * above when we lookup the be with !parent, but just in
861 			 * case catch this case so we don't panic because I
862 			 * didn't think of some other corner case.
863 			 */
864 			btrfs_err(fs_info, "failed to find root %llu for %llu",
865 				  generic_ref->real_root, be->bytenr);
866 			dump_block_entry(fs_info, be);
867 			dump_ref_action(fs_info, ra);
868 			kfree(ra);
869 			goto out_unlock;
870 		}
871 	}
872 	if (action == BTRFS_DROP_DELAYED_REF) {
873 		if (re)
874 			re->num_refs--;
875 		be->num_refs--;
876 	} else if (action == BTRFS_ADD_DELAYED_REF) {
877 		be->num_refs++;
878 		if (re)
879 			re->num_refs++;
880 	}
881 	list_add_tail(&ra->list, &be->actions);
882 	ret = 0;
883 out_unlock:
884 	spin_unlock(&fs_info->ref_verify_lock);
885 out:
886 	if (ret)
887 		btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
888 	return ret;
889 }
890 
891 /* Free up the ref cache */
892 void btrfs_free_ref_cache(struct btrfs_fs_info *fs_info)
893 {
894 	struct block_entry *be;
895 	struct rb_node *n;
896 
897 	if (!btrfs_test_opt(fs_info, REF_VERIFY))
898 		return;
899 
900 	spin_lock(&fs_info->ref_verify_lock);
901 	while ((n = rb_first(&fs_info->block_tree))) {
902 		be = rb_entry(n, struct block_entry, node);
903 		rb_erase(&be->node, &fs_info->block_tree);
904 		free_block_entry(be);
905 		cond_resched_lock(&fs_info->ref_verify_lock);
906 	}
907 	spin_unlock(&fs_info->ref_verify_lock);
908 }
909 
910 void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
911 			       u64 len)
912 {
913 	struct block_entry *be = NULL, *entry;
914 	struct rb_node *n;
915 
916 	if (!btrfs_test_opt(fs_info, REF_VERIFY))
917 		return;
918 
919 	spin_lock(&fs_info->ref_verify_lock);
920 	n = fs_info->block_tree.rb_node;
921 	while (n) {
922 		entry = rb_entry(n, struct block_entry, node);
923 		if (entry->bytenr < start) {
924 			n = n->rb_right;
925 		} else if (entry->bytenr > start) {
926 			n = n->rb_left;
927 		} else {
928 			be = entry;
929 			break;
930 		}
931 		/* We want to get as close to start as possible */
932 		if (be == NULL ||
933 		    (entry->bytenr < start && be->bytenr > start) ||
934 		    (entry->bytenr < start && entry->bytenr > be->bytenr))
935 			be = entry;
936 	}
937 
938 	/*
939 	 * Could have an empty block group, maybe have something to check for
940 	 * this case to verify we were actually empty?
941 	 */
942 	if (!be) {
943 		spin_unlock(&fs_info->ref_verify_lock);
944 		return;
945 	}
946 
947 	n = &be->node;
948 	while (n) {
949 		be = rb_entry(n, struct block_entry, node);
950 		n = rb_next(n);
951 		if (be->bytenr < start && be->bytenr + be->len > start) {
952 			btrfs_err(fs_info,
953 				"block entry overlaps a block group [%llu,%llu]!",
954 				start, len);
955 			dump_block_entry(fs_info, be);
956 			continue;
957 		}
958 		if (be->bytenr < start)
959 			continue;
960 		if (be->bytenr >= start + len)
961 			break;
962 		if (be->bytenr + be->len > start + len) {
963 			btrfs_err(fs_info,
964 				"block entry overlaps a block group [%llu,%llu]!",
965 				start, len);
966 			dump_block_entry(fs_info, be);
967 		}
968 		rb_erase(&be->node, &fs_info->block_tree);
969 		free_block_entry(be);
970 	}
971 	spin_unlock(&fs_info->ref_verify_lock);
972 }
973 
974 /* Walk down all roots and build the ref tree, meant to be called at mount */
975 int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
976 {
977 	struct btrfs_root *extent_root;
978 	struct btrfs_path *path;
979 	struct extent_buffer *eb;
980 	int tree_block_level = 0;
981 	u64 bytenr = 0, num_bytes = 0;
982 	int ret, level;
983 
984 	if (!btrfs_test_opt(fs_info, REF_VERIFY))
985 		return 0;
986 
987 	path = btrfs_alloc_path();
988 	if (!path)
989 		return -ENOMEM;
990 
991 	extent_root = btrfs_extent_root(fs_info, 0);
992 	eb = btrfs_read_lock_root_node(extent_root);
993 	level = btrfs_header_level(eb);
994 	path->nodes[level] = eb;
995 	path->slots[level] = 0;
996 	path->locks[level] = BTRFS_READ_LOCK;
997 
998 	while (1) {
999 		/*
1000 		 * We have to keep track of the bytenr/num_bytes we last hit
1001 		 * because we could have run out of space for an inline ref, and
1002 		 * would have had to added a ref key item which may appear on a
1003 		 * different leaf from the original extent item.
1004 		 */
1005 		ret = walk_down_tree(extent_root, path, level,
1006 				     &bytenr, &num_bytes, &tree_block_level);
1007 		if (ret)
1008 			break;
1009 		ret = walk_up_tree(path, &level);
1010 		if (ret < 0)
1011 			break;
1012 		if (ret > 0) {
1013 			ret = 0;
1014 			break;
1015 		}
1016 	}
1017 	if (ret) {
1018 		btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
1019 		btrfs_free_ref_cache(fs_info);
1020 	}
1021 	btrfs_free_path(path);
1022 	return ret;
1023 }
1024