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