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