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