Lines Matching +full:ref +full:-
1 // SPDX-License-Identifier: GPL-2.0
10 #include "disk-io.h"
12 #include "delayed-ref.h"
13 #include "ref-verify.h"
46 * back to the delayed ref action. We hold the ref we are changing in the
54 struct ref_entry ref; member
62 * to it as well as all of the ref actions that have occurred to it. We never
63 * free it until we unmount the file system in order to make sure re-allocations
81 struct rb_node **p = &root->rb_node; in insert_block_entry()
88 if (entry->bytenr > be->bytenr) in insert_block_entry()
89 p = &(*p)->rb_left; in insert_block_entry()
90 else if (entry->bytenr < be->bytenr) in insert_block_entry()
91 p = &(*p)->rb_right; in insert_block_entry()
96 rb_link_node(&be->node, parent_node, p); in insert_block_entry()
97 rb_insert_color(&be->node, root); in insert_block_entry()
106 n = root->rb_node; in lookup_block_entry()
109 if (entry->bytenr < bytenr) in lookup_block_entry()
110 n = n->rb_right; in lookup_block_entry()
111 else if (entry->bytenr > bytenr) in lookup_block_entry()
112 n = n->rb_left; in lookup_block_entry()
122 struct rb_node **p = &root->rb_node; in insert_root_entry()
129 if (entry->root_objectid > re->root_objectid) in insert_root_entry()
130 p = &(*p)->rb_left; in insert_root_entry()
131 else if (entry->root_objectid < re->root_objectid) in insert_root_entry()
132 p = &(*p)->rb_right; in insert_root_entry()
137 rb_link_node(&re->node, parent_node, p); in insert_root_entry()
138 rb_insert_color(&re->node, root); in insert_root_entry()
145 if (ref1->root_objectid < ref2->root_objectid) in comp_refs()
146 return -1; in comp_refs()
147 if (ref1->root_objectid > ref2->root_objectid) in comp_refs()
149 if (ref1->parent < ref2->parent) in comp_refs()
150 return -1; in comp_refs()
151 if (ref1->parent > ref2->parent) in comp_refs()
153 if (ref1->owner < ref2->owner) in comp_refs()
154 return -1; in comp_refs()
155 if (ref1->owner > ref2->owner) in comp_refs()
157 if (ref1->offset < ref2->offset) in comp_refs()
158 return -1; in comp_refs()
159 if (ref1->offset > ref2->offset) in comp_refs()
165 struct ref_entry *ref) in insert_ref_entry() argument
167 struct rb_node **p = &root->rb_node; in insert_ref_entry()
175 cmp = comp_refs(entry, ref); in insert_ref_entry()
177 p = &(*p)->rb_left; in insert_ref_entry()
179 p = &(*p)->rb_right; in insert_ref_entry()
184 rb_link_node(&ref->node, parent_node, p); in insert_ref_entry()
185 rb_insert_color(&ref->node, root); in insert_ref_entry()
195 n = root->rb_node; in lookup_root_entry()
198 if (entry->root_objectid < objectid) in lookup_root_entry()
199 n = n->rb_right; in lookup_root_entry()
200 else if (entry->root_objectid > objectid) in lookup_root_entry()
201 n = n->rb_left; in lookup_root_entry()
211 ra->trace_len = stack_trace_save(ra->trace, MAX_TRACE, 2); in __save_stack_trace()
217 if (ra->trace_len == 0) { in __print_stack_trace()
218 btrfs_err(fs_info, " ref-verify: no stacktrace"); in __print_stack_trace()
221 stack_trace_print(ra->trace, ra->trace_len, 2); in __print_stack_trace()
231 btrfs_err(fs_info, " ref-verify: no stacktrace support"); in __print_stack_trace()
238 struct ref_entry *ref; in free_block_entry() local
242 while ((n = rb_first(&be->roots))) { in free_block_entry()
244 rb_erase(&re->node, &be->roots); in free_block_entry()
248 while((n = rb_first(&be->refs))) { in free_block_entry()
249 ref = rb_entry(n, struct ref_entry, node); in free_block_entry()
250 rb_erase(&ref->node, &be->refs); in free_block_entry()
251 kfree(ref); in free_block_entry()
254 while (!list_empty(&be->actions)) { in free_block_entry()
255 ra = list_first_entry(&be->actions, struct ref_action, in free_block_entry()
257 list_del(&ra->list); in free_block_entry()
275 return ERR_PTR(-ENOMEM); in add_block_entry()
277 be->bytenr = bytenr; in add_block_entry()
278 be->len = len; in add_block_entry()
280 re->root_objectid = root_objectid; in add_block_entry()
281 re->num_refs = 0; in add_block_entry()
283 spin_lock(&fs_info->ref_verify_lock); in add_block_entry()
284 exist = insert_block_entry(&fs_info->block_tree, be); in add_block_entry()
289 exist_re = insert_root_entry(&exist->roots, re); in add_block_entry()
299 be->num_refs = 0; in add_block_entry()
300 be->metadata = 0; in add_block_entry()
301 be->from_disk = 0; in add_block_entry()
302 be->roots = RB_ROOT; in add_block_entry()
303 be->refs = RB_ROOT; in add_block_entry()
304 INIT_LIST_HEAD(&be->actions); in add_block_entry()
306 insert_root_entry(&be->roots, re); in add_block_entry()
317 struct ref_entry *ref = NULL, *exist; in add_tree_block() local
319 ref = kmalloc(sizeof(struct ref_entry), GFP_NOFS); in add_tree_block()
320 if (!ref) in add_tree_block()
321 return -ENOMEM; in add_tree_block()
324 ref->root_objectid = 0; in add_tree_block()
326 ref->root_objectid = ref_root; in add_tree_block()
327 ref->parent = parent; in add_tree_block()
328 ref->owner = level; in add_tree_block()
329 ref->offset = 0; in add_tree_block()
330 ref->num_refs = 1; in add_tree_block()
332 be = add_block_entry(fs_info, bytenr, fs_info->nodesize, ref_root); in add_tree_block()
334 kfree(ref); in add_tree_block()
337 be->num_refs++; in add_tree_block()
338 be->from_disk = 1; in add_tree_block()
339 be->metadata = 1; in add_tree_block()
343 re = lookup_root_entry(&be->roots, ref_root); in add_tree_block()
345 re->num_refs++; in add_tree_block()
347 exist = insert_ref_entry(&be->refs, ref); in add_tree_block()
349 exist->num_refs++; in add_tree_block()
350 kfree(ref); in add_tree_block()
352 spin_unlock(&fs_info->ref_verify_lock); in add_tree_block()
362 struct ref_entry *ref; in add_shared_data_ref() local
364 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS); in add_shared_data_ref()
365 if (!ref) in add_shared_data_ref()
366 return -ENOMEM; in add_shared_data_ref()
369 kfree(ref); in add_shared_data_ref()
372 be->num_refs += num_refs; in add_shared_data_ref()
374 ref->parent = parent; in add_shared_data_ref()
375 ref->num_refs = num_refs; in add_shared_data_ref()
376 if (insert_ref_entry(&be->refs, ref)) { in add_shared_data_ref()
377 spin_unlock(&fs_info->ref_verify_lock); in add_shared_data_ref()
378 btrfs_err(fs_info, "existing shared ref when reading from disk?"); in add_shared_data_ref()
379 kfree(ref); in add_shared_data_ref()
380 return -EINVAL; in add_shared_data_ref()
382 spin_unlock(&fs_info->ref_verify_lock); in add_shared_data_ref()
392 struct ref_entry *ref; in add_extent_data_ref() local
399 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS); in add_extent_data_ref()
400 if (!ref) in add_extent_data_ref()
401 return -ENOMEM; in add_extent_data_ref()
404 kfree(ref); in add_extent_data_ref()
407 be->num_refs += num_refs; in add_extent_data_ref()
409 ref->parent = 0; in add_extent_data_ref()
410 ref->owner = owner; in add_extent_data_ref()
411 ref->root_objectid = ref_root; in add_extent_data_ref()
412 ref->offset = offset; in add_extent_data_ref()
413 ref->num_refs = num_refs; in add_extent_data_ref()
414 if (insert_ref_entry(&be->refs, ref)) { in add_extent_data_ref()
415 spin_unlock(&fs_info->ref_verify_lock); in add_extent_data_ref()
416 btrfs_err(fs_info, "existing ref when reading from disk?"); in add_extent_data_ref()
417 kfree(ref); in add_extent_data_ref()
418 return -EINVAL; in add_extent_data_ref()
421 re = lookup_root_entry(&be->roots, ref_root); in add_extent_data_ref()
423 spin_unlock(&fs_info->ref_verify_lock); in add_extent_data_ref()
425 return -EINVAL; in add_extent_data_ref()
427 re->num_refs += num_refs; in add_extent_data_ref()
428 spin_unlock(&fs_info->ref_verify_lock); in add_extent_data_ref()
440 struct extent_buffer *leaf = path->nodes[0]; in process_extent_item()
450 if ((key->type == BTRFS_EXTENT_ITEM_KEY) && in process_extent_item()
458 if (key->type == BTRFS_METADATA_ITEM_KEY) in process_extent_item()
459 *tree_block_level = key->offset; in process_extent_item()
471 ret = add_tree_block(fs_info, offset, 0, key->objectid, in process_extent_item()
475 ret = add_tree_block(fs_info, 0, offset, key->objectid, in process_extent_item()
479 dref = (struct btrfs_extent_data_ref *)(&iref->offset); in process_extent_item()
481 key->objectid, key->offset); in process_extent_item()
487 key->objectid, key->offset); in process_extent_item()
492 "found extent owner ref without simple quotas enabled"); in process_extent_item()
493 ret = -EINVAL; in process_extent_item()
498 ret = -EINVAL; in process_extent_item()
512 struct btrfs_fs_info *fs_info = root->fs_info; in process_leaf()
513 struct extent_buffer *leaf = path->nodes[0]; in process_leaf()
572 eb = btrfs_read_node_slot(path->nodes[level], in walk_down_tree()
573 path->slots[level]); in walk_down_tree()
577 path->nodes[level-1] = eb; in walk_down_tree()
578 path->slots[level-1] = 0; in walk_down_tree()
579 path->locks[level-1] = BTRFS_READ_LOCK; in walk_down_tree()
586 level--; in walk_down_tree()
597 if (!path->nodes[l]) in walk_up_tree()
600 path->slots[l]++; in walk_up_tree()
601 if (path->slots[l] < in walk_up_tree()
602 btrfs_header_nritems(path->nodes[l])) { in walk_up_tree()
607 btrfs_tree_unlock_rw(path->nodes[l], path->locks[l]); in walk_up_tree()
608 free_extent_buffer(path->nodes[l]); in walk_up_tree()
609 path->nodes[l] = NULL; in walk_up_tree()
610 path->slots[l] = 0; in walk_up_tree()
611 path->locks[l] = 0; in walk_up_tree()
621 " Ref action %d, root %llu, ref_root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu", in dump_ref_action()
622 ra->action, ra->root, ra->ref.root_objectid, ra->ref.parent, in dump_ref_action()
623 ra->ref.owner, ra->ref.offset, ra->ref.num_refs); in dump_ref_action()
634 struct ref_entry *ref; in dump_block_entry() local
641 be->bytenr, be->len, be->num_refs, be->metadata, in dump_block_entry()
642 be->from_disk); in dump_block_entry()
644 for (n = rb_first(&be->refs); n; n = rb_next(n)) { in dump_block_entry()
645 ref = rb_entry(n, struct ref_entry, node); in dump_block_entry()
647 " ref root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu", in dump_block_entry()
648 ref->root_objectid, ref->parent, ref->owner, in dump_block_entry()
649 ref->offset, ref->num_refs); in dump_block_entry()
652 for (n = rb_first(&be->roots); n; n = rb_next(n)) { in dump_block_entry()
655 re->root_objectid, re->num_refs); in dump_block_entry()
658 list_for_each_entry(ra, &be->actions, list) in dump_block_entry()
663 * Called when we modify a ref for a bytenr.
673 struct ref_entry *ref = NULL, *exist; in btrfs_ref_tree_mod() local
677 int action = generic_ref->action; in btrfs_ref_tree_mod()
680 u64 bytenr = generic_ref->bytenr; in btrfs_ref_tree_mod()
681 u64 num_bytes = generic_ref->num_bytes; in btrfs_ref_tree_mod()
682 u64 parent = generic_ref->parent; in btrfs_ref_tree_mod()
690 if (generic_ref->type == BTRFS_REF_METADATA) { in btrfs_ref_tree_mod()
692 ref_root = generic_ref->ref_root; in btrfs_ref_tree_mod()
693 owner = generic_ref->tree_ref.level; in btrfs_ref_tree_mod()
695 ref_root = generic_ref->ref_root; in btrfs_ref_tree_mod()
696 owner = generic_ref->data_ref.objectid; in btrfs_ref_tree_mod()
697 offset = generic_ref->data_ref.offset; in btrfs_ref_tree_mod()
701 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS); in btrfs_ref_tree_mod()
703 if (!ra || !ref) { in btrfs_ref_tree_mod()
704 kfree(ref); in btrfs_ref_tree_mod()
706 ret = -ENOMEM; in btrfs_ref_tree_mod()
710 ref->parent = parent; in btrfs_ref_tree_mod()
711 ref->owner = owner; in btrfs_ref_tree_mod()
712 ref->root_objectid = ref_root; in btrfs_ref_tree_mod()
713 ref->offset = offset; in btrfs_ref_tree_mod()
714 ref->num_refs = (action == BTRFS_DROP_DELAYED_REF) ? -1 : 1; in btrfs_ref_tree_mod()
716 memcpy(&ra->ref, ref, sizeof(struct ref_entry)); in btrfs_ref_tree_mod()
718 * Save the extra info from the delayed ref in the ref action to make it in btrfs_ref_tree_mod()
719 * easier to figure out what is happening. The real ref's we add to the in btrfs_ref_tree_mod()
720 * ref tree need to reflect what we save on disk so it matches any in btrfs_ref_tree_mod()
721 * on-disk refs we pre-loaded. in btrfs_ref_tree_mod()
723 ra->ref.owner = owner; in btrfs_ref_tree_mod()
724 ra->ref.offset = offset; in btrfs_ref_tree_mod()
725 ra->ref.root_objectid = ref_root; in btrfs_ref_tree_mod()
728 INIT_LIST_HEAD(&ra->list); in btrfs_ref_tree_mod()
729 ra->action = action; in btrfs_ref_tree_mod()
730 ra->root = generic_ref->real_root; in btrfs_ref_tree_mod()
736 ret = -EINVAL; in btrfs_ref_tree_mod()
741 * in root as if it really has a ref for this bytenr. in btrfs_ref_tree_mod()
745 kfree(ref); in btrfs_ref_tree_mod()
750 be->num_refs++; in btrfs_ref_tree_mod()
752 be->metadata = 1; in btrfs_ref_tree_mod()
754 if (be->num_refs != 1) { in btrfs_ref_tree_mod()
756 "re-allocated a block that still has references to it!"); in btrfs_ref_tree_mod()
759 kfree(ref); in btrfs_ref_tree_mod()
764 while (!list_empty(&be->actions)) { in btrfs_ref_tree_mod()
767 tmp = list_first_entry(&be->actions, struct ref_action, in btrfs_ref_tree_mod()
769 list_del(&tmp->list); in btrfs_ref_tree_mod()
778 kfree(ref); in btrfs_ref_tree_mod()
780 ret = -ENOMEM; in btrfs_ref_tree_mod()
786 * re->num_refs. in btrfs_ref_tree_mod()
788 ref_root = generic_ref->real_root; in btrfs_ref_tree_mod()
789 re->root_objectid = generic_ref->real_root; in btrfs_ref_tree_mod()
790 re->num_refs = 0; in btrfs_ref_tree_mod()
793 spin_lock(&fs_info->ref_verify_lock); in btrfs_ref_tree_mod()
794 be = lookup_block_entry(&fs_info->block_tree, bytenr); in btrfs_ref_tree_mod()
800 kfree(ref); in btrfs_ref_tree_mod()
804 } else if (be->num_refs == 0) { in btrfs_ref_tree_mod()
810 kfree(ref); in btrfs_ref_tree_mod()
817 tmp = insert_root_entry(&be->roots, re); in btrfs_ref_tree_mod()
825 exist = insert_ref_entry(&be->refs, ref); in btrfs_ref_tree_mod()
828 if (exist->num_refs == 0) { in btrfs_ref_tree_mod()
830 "dropping a ref for a existing root that doesn't have a ref on the block"); in btrfs_ref_tree_mod()
833 kfree(ref); in btrfs_ref_tree_mod()
837 exist->num_refs--; in btrfs_ref_tree_mod()
838 if (exist->num_refs == 0) { in btrfs_ref_tree_mod()
839 rb_erase(&exist->node, &be->refs); in btrfs_ref_tree_mod()
842 } else if (!be->metadata) { in btrfs_ref_tree_mod()
843 exist->num_refs++; in btrfs_ref_tree_mod()
846 "attempting to add another ref for an existing ref on a tree block"); in btrfs_ref_tree_mod()
849 kfree(ref); in btrfs_ref_tree_mod()
853 kfree(ref); in btrfs_ref_tree_mod()
857 "dropping a ref for a root that doesn't have a ref on the block"); in btrfs_ref_tree_mod()
860 rb_erase(&ref->node, &be->refs); in btrfs_ref_tree_mod()
861 kfree(ref); in btrfs_ref_tree_mod()
868 re = lookup_root_entry(&be->roots, ref_root); in btrfs_ref_tree_mod()
877 generic_ref->real_root, be->bytenr); in btrfs_ref_tree_mod()
886 re->num_refs--; in btrfs_ref_tree_mod()
887 be->num_refs--; in btrfs_ref_tree_mod()
889 be->num_refs++; in btrfs_ref_tree_mod()
891 re->num_refs++; in btrfs_ref_tree_mod()
893 list_add_tail(&ra->list, &be->actions); in btrfs_ref_tree_mod()
896 spin_unlock(&fs_info->ref_verify_lock); in btrfs_ref_tree_mod()
900 btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY); in btrfs_ref_tree_mod()
905 /* Free up the ref cache */
914 spin_lock(&fs_info->ref_verify_lock); in btrfs_free_ref_cache()
915 while ((n = rb_first(&fs_info->block_tree))) { in btrfs_free_ref_cache()
917 rb_erase(&be->node, &fs_info->block_tree); in btrfs_free_ref_cache()
919 cond_resched_lock(&fs_info->ref_verify_lock); in btrfs_free_ref_cache()
921 spin_unlock(&fs_info->ref_verify_lock); in btrfs_free_ref_cache()
933 spin_lock(&fs_info->ref_verify_lock); in btrfs_free_ref_tree_range()
934 n = fs_info->block_tree.rb_node; in btrfs_free_ref_tree_range()
937 if (entry->bytenr < start) { in btrfs_free_ref_tree_range()
938 n = n->rb_right; in btrfs_free_ref_tree_range()
939 } else if (entry->bytenr > start) { in btrfs_free_ref_tree_range()
940 n = n->rb_left; in btrfs_free_ref_tree_range()
947 (entry->bytenr < start && be->bytenr > start) || in btrfs_free_ref_tree_range()
948 (entry->bytenr < start && entry->bytenr > be->bytenr)) in btrfs_free_ref_tree_range()
957 spin_unlock(&fs_info->ref_verify_lock); in btrfs_free_ref_tree_range()
961 n = &be->node; in btrfs_free_ref_tree_range()
965 if (be->bytenr < start && be->bytenr + be->len > start) { in btrfs_free_ref_tree_range()
972 if (be->bytenr < start) in btrfs_free_ref_tree_range()
974 if (be->bytenr >= start + len) in btrfs_free_ref_tree_range()
976 if (be->bytenr + be->len > start + len) { in btrfs_free_ref_tree_range()
982 rb_erase(&be->node, &fs_info->block_tree); in btrfs_free_ref_tree_range()
985 spin_unlock(&fs_info->ref_verify_lock); in btrfs_free_ref_tree_range()
988 /* Walk down all roots and build the ref tree, meant to be called at mount */
1003 return -ENOMEM; in btrfs_build_ref_tree()
1008 path->nodes[level] = eb; in btrfs_build_ref_tree()
1009 path->slots[level] = 0; in btrfs_build_ref_tree()
1010 path->locks[level] = BTRFS_READ_LOCK; in btrfs_build_ref_tree()
1015 * because we could have run out of space for an inline ref, and in btrfs_build_ref_tree()
1016 * would have had to added a ref key item which may appear on a in btrfs_build_ref_tree()
1033 btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY); in btrfs_build_ref_tree()