1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Facebook. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/stacktrace.h>
11 #include "delayed-ref.h"
12 #include "ref-verify.h"
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
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.
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
46 * stack traces because that's how I roll.
52 struct list_head list;
53 unsigned long trace[MAX_TRACE];
54 unsigned int trace_len;
58 * One of these for every block we reference, it holds the roots and references
59 * to it as well as all of the ref actions that have occurred to it. We never
60 * free it until we unmount the file system in order to make sure re-allocations
61 * are happening properly.
72 struct list_head actions;
75 static struct block_entry *insert_block_entry(struct rb_root *root,
76 struct block_entry *be)
78 struct rb_node **p = &root->rb_node;
79 struct rb_node *parent_node = NULL;
80 struct block_entry *entry;
84 entry = rb_entry(parent_node, struct block_entry, node);
85 if (entry->bytenr > be->bytenr)
87 else if (entry->bytenr < be->bytenr)
93 rb_link_node(&be->node, parent_node, p);
94 rb_insert_color(&be->node, root);
98 static struct block_entry *lookup_block_entry(struct rb_root *root, u64 bytenr)
101 struct block_entry *entry = NULL;
105 entry = rb_entry(n, struct block_entry, node);
106 if (entry->bytenr < bytenr)
108 else if (entry->bytenr > bytenr)
116 static struct root_entry *insert_root_entry(struct rb_root *root,
117 struct root_entry *re)
119 struct rb_node **p = &root->rb_node;
120 struct rb_node *parent_node = NULL;
121 struct root_entry *entry;
125 entry = rb_entry(parent_node, struct root_entry, node);
126 if (entry->root_objectid > re->root_objectid)
128 else if (entry->root_objectid < re->root_objectid)
134 rb_link_node(&re->node, parent_node, p);
135 rb_insert_color(&re->node, root);
140 static int comp_refs(struct ref_entry *ref1, struct ref_entry *ref2)
142 if (ref1->root_objectid < ref2->root_objectid)
144 if (ref1->root_objectid > ref2->root_objectid)
146 if (ref1->parent < ref2->parent)
148 if (ref1->parent > ref2->parent)
150 if (ref1->owner < ref2->owner)
152 if (ref1->owner > ref2->owner)
154 if (ref1->offset < ref2->offset)
156 if (ref1->offset > ref2->offset)
161 static struct ref_entry *insert_ref_entry(struct rb_root *root,
162 struct ref_entry *ref)
164 struct rb_node **p = &root->rb_node;
165 struct rb_node *parent_node = NULL;
166 struct ref_entry *entry;
171 entry = rb_entry(parent_node, struct ref_entry, node);
172 cmp = comp_refs(entry, ref);
181 rb_link_node(&ref->node, parent_node, p);
182 rb_insert_color(&ref->node, root);
187 static struct root_entry *lookup_root_entry(struct rb_root *root, u64 objectid)
190 struct root_entry *entry = NULL;
194 entry = rb_entry(n, struct root_entry, node);
195 if (entry->root_objectid < objectid)
197 else if (entry->root_objectid > objectid)
205 #ifdef CONFIG_STACKTRACE
206 static void __save_stack_trace(struct ref_action *ra)
208 ra->trace_len = stack_trace_save(ra->trace, MAX_TRACE, 2);
211 static void __print_stack_trace(struct btrfs_fs_info *fs_info,
212 struct ref_action *ra)
214 if (ra->trace_len == 0) {
215 btrfs_err(fs_info, " ref-verify: no stacktrace");
218 stack_trace_print(ra->trace, ra->trace_len, 2);
221 static inline void __save_stack_trace(struct ref_action *ra)
225 static inline void __print_stack_trace(struct btrfs_fs_info *fs_info,
226 struct ref_action *ra)
228 btrfs_err(fs_info, " ref-verify: no stacktrace support");
232 static void free_block_entry(struct block_entry *be)
234 struct root_entry *re;
235 struct ref_entry *ref;
236 struct ref_action *ra;
239 while ((n = rb_first(&be->roots))) {
240 re = rb_entry(n, struct root_entry, node);
241 rb_erase(&re->node, &be->roots);
245 while((n = rb_first(&be->refs))) {
246 ref = rb_entry(n, struct ref_entry, node);
247 rb_erase(&ref->node, &be->refs);
251 while (!list_empty(&be->actions)) {
252 ra = list_first_entry(&be->actions, struct ref_action,
260 static struct block_entry *add_block_entry(struct btrfs_fs_info *fs_info,
264 struct block_entry *be = NULL, *exist;
265 struct root_entry *re = NULL;
267 re = kzalloc(sizeof(struct root_entry), GFP_NOFS);
268 be = kzalloc(sizeof(struct block_entry), GFP_NOFS);
272 return ERR_PTR(-ENOMEM);
277 re->root_objectid = root_objectid;
280 spin_lock(&fs_info->ref_verify_lock);
281 exist = insert_block_entry(&fs_info->block_tree, be);
284 struct root_entry *exist_re;
286 exist_re = insert_root_entry(&exist->roots, re);
301 INIT_LIST_HEAD(&be->actions);
303 insert_root_entry(&be->roots, re);
309 static int add_tree_block(struct btrfs_fs_info *fs_info, u64 ref_root,
310 u64 parent, u64 bytenr, int level)
312 struct block_entry *be;
313 struct root_entry *re;
314 struct ref_entry *ref = NULL, *exist;
316 ref = kmalloc(sizeof(struct ref_entry), GFP_NOFS);
321 ref->root_objectid = 0;
323 ref->root_objectid = ref_root;
324 ref->parent = parent;
329 be = add_block_entry(fs_info, bytenr, fs_info->nodesize, ref_root);
340 re = lookup_root_entry(&be->roots, ref_root);
344 exist = insert_ref_entry(&be->refs, ref);
349 spin_unlock(&fs_info->ref_verify_lock);
354 static int add_shared_data_ref(struct btrfs_fs_info *fs_info,
355 u64 parent, u32 num_refs, u64 bytenr,
358 struct block_entry *be;
359 struct ref_entry *ref;
361 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
364 be = add_block_entry(fs_info, bytenr, num_bytes, 0);
369 be->num_refs += num_refs;
371 ref->parent = parent;
372 ref->num_refs = num_refs;
373 if (insert_ref_entry(&be->refs, ref)) {
374 spin_unlock(&fs_info->ref_verify_lock);
375 btrfs_err(fs_info, "existing shared ref when reading from disk?");
379 spin_unlock(&fs_info->ref_verify_lock);
383 static int add_extent_data_ref(struct btrfs_fs_info *fs_info,
384 struct extent_buffer *leaf,
385 struct btrfs_extent_data_ref *dref,
386 u64 bytenr, u64 num_bytes)
388 struct block_entry *be;
389 struct ref_entry *ref;
390 struct root_entry *re;
391 u64 ref_root = btrfs_extent_data_ref_root(leaf, dref);
392 u64 owner = btrfs_extent_data_ref_objectid(leaf, dref);
393 u64 offset = btrfs_extent_data_ref_offset(leaf, dref);
394 u32 num_refs = btrfs_extent_data_ref_count(leaf, dref);
396 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
399 be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
404 be->num_refs += num_refs;
408 ref->root_objectid = ref_root;
409 ref->offset = offset;
410 ref->num_refs = num_refs;
411 if (insert_ref_entry(&be->refs, ref)) {
412 spin_unlock(&fs_info->ref_verify_lock);
413 btrfs_err(fs_info, "existing ref when reading from disk?");
418 re = lookup_root_entry(&be->roots, ref_root);
420 spin_unlock(&fs_info->ref_verify_lock);
421 btrfs_err(fs_info, "missing root in new block entry?");
424 re->num_refs += num_refs;
425 spin_unlock(&fs_info->ref_verify_lock);
429 static int process_extent_item(struct btrfs_fs_info *fs_info,
430 struct btrfs_path *path, struct btrfs_key *key,
431 int slot, int *tree_block_level)
433 struct btrfs_extent_item *ei;
434 struct btrfs_extent_inline_ref *iref;
435 struct btrfs_extent_data_ref *dref;
436 struct btrfs_shared_data_ref *sref;
437 struct extent_buffer *leaf = path->nodes[0];
438 u32 item_size = btrfs_item_size(leaf, slot);
439 unsigned long end, ptr;
440 u64 offset, flags, count;
443 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
444 flags = btrfs_extent_flags(leaf, ei);
446 if ((key->type == BTRFS_EXTENT_ITEM_KEY) &&
447 flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
448 struct btrfs_tree_block_info *info;
450 info = (struct btrfs_tree_block_info *)(ei + 1);
451 *tree_block_level = btrfs_tree_block_level(leaf, info);
452 iref = (struct btrfs_extent_inline_ref *)(info + 1);
454 if (key->type == BTRFS_METADATA_ITEM_KEY)
455 *tree_block_level = key->offset;
456 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
459 ptr = (unsigned long)iref;
460 end = (unsigned long)ei + item_size;
462 iref = (struct btrfs_extent_inline_ref *)ptr;
463 type = btrfs_extent_inline_ref_type(leaf, iref);
464 offset = btrfs_extent_inline_ref_offset(leaf, iref);
466 case BTRFS_TREE_BLOCK_REF_KEY:
467 ret = add_tree_block(fs_info, offset, 0, key->objectid,
470 case BTRFS_SHARED_BLOCK_REF_KEY:
471 ret = add_tree_block(fs_info, 0, offset, key->objectid,
474 case BTRFS_EXTENT_DATA_REF_KEY:
475 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
476 ret = add_extent_data_ref(fs_info, leaf, dref,
477 key->objectid, key->offset);
479 case BTRFS_SHARED_DATA_REF_KEY:
480 sref = (struct btrfs_shared_data_ref *)(iref + 1);
481 count = btrfs_shared_data_ref_count(leaf, sref);
482 ret = add_shared_data_ref(fs_info, offset, count,
483 key->objectid, key->offset);
486 btrfs_err(fs_info, "invalid key type in iref");
492 ptr += btrfs_extent_inline_ref_size(type);
497 static int process_leaf(struct btrfs_root *root,
498 struct btrfs_path *path, u64 *bytenr, u64 *num_bytes,
499 int *tree_block_level)
501 struct btrfs_fs_info *fs_info = root->fs_info;
502 struct extent_buffer *leaf = path->nodes[0];
503 struct btrfs_extent_data_ref *dref;
504 struct btrfs_shared_data_ref *sref;
507 struct btrfs_key key;
508 int nritems = btrfs_header_nritems(leaf);
510 for (i = 0; i < nritems; i++) {
511 btrfs_item_key_to_cpu(leaf, &key, i);
513 case BTRFS_EXTENT_ITEM_KEY:
514 *num_bytes = key.offset;
516 case BTRFS_METADATA_ITEM_KEY:
517 *bytenr = key.objectid;
518 ret = process_extent_item(fs_info, path, &key, i,
521 case BTRFS_TREE_BLOCK_REF_KEY:
522 ret = add_tree_block(fs_info, key.offset, 0,
523 key.objectid, *tree_block_level);
525 case BTRFS_SHARED_BLOCK_REF_KEY:
526 ret = add_tree_block(fs_info, 0, key.offset,
527 key.objectid, *tree_block_level);
529 case BTRFS_EXTENT_DATA_REF_KEY:
530 dref = btrfs_item_ptr(leaf, i,
531 struct btrfs_extent_data_ref);
532 ret = add_extent_data_ref(fs_info, leaf, dref, *bytenr,
535 case BTRFS_SHARED_DATA_REF_KEY:
536 sref = btrfs_item_ptr(leaf, i,
537 struct btrfs_shared_data_ref);
538 count = btrfs_shared_data_ref_count(leaf, sref);
539 ret = add_shared_data_ref(fs_info, key.offset, count,
540 *bytenr, *num_bytes);
551 /* Walk down to the leaf from the given level */
552 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
553 int level, u64 *bytenr, u64 *num_bytes,
554 int *tree_block_level)
556 struct extent_buffer *eb;
561 eb = btrfs_read_node_slot(path->nodes[level],
565 btrfs_tree_read_lock(eb);
566 path->nodes[level-1] = eb;
567 path->slots[level-1] = 0;
568 path->locks[level-1] = BTRFS_READ_LOCK;
570 ret = process_leaf(root, path, bytenr, num_bytes,
580 /* Walk up to the next node that needs to be processed */
581 static int walk_up_tree(struct btrfs_path *path, int *level)
585 for (l = 0; l < BTRFS_MAX_LEVEL; l++) {
591 btrfs_header_nritems(path->nodes[l])) {
596 btrfs_tree_unlock_rw(path->nodes[l], path->locks[l]);
597 free_extent_buffer(path->nodes[l]);
598 path->nodes[l] = NULL;
606 static void dump_ref_action(struct btrfs_fs_info *fs_info,
607 struct ref_action *ra)
610 " Ref action %d, root %llu, ref_root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
611 ra->action, ra->root, ra->ref.root_objectid, ra->ref.parent,
612 ra->ref.owner, ra->ref.offset, ra->ref.num_refs);
613 __print_stack_trace(fs_info, ra);
617 * Dumps all the information from the block entry to printk, it's going to be
620 static void dump_block_entry(struct btrfs_fs_info *fs_info,
621 struct block_entry *be)
623 struct ref_entry *ref;
624 struct root_entry *re;
625 struct ref_action *ra;
629 "dumping block entry [%llu %llu], num_refs %llu, metadata %d, from disk %d",
630 be->bytenr, be->len, be->num_refs, be->metadata,
633 for (n = rb_first(&be->refs); n; n = rb_next(n)) {
634 ref = rb_entry(n, struct ref_entry, node);
636 " ref root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
637 ref->root_objectid, ref->parent, ref->owner,
638 ref->offset, ref->num_refs);
641 for (n = rb_first(&be->roots); n; n = rb_next(n)) {
642 re = rb_entry(n, struct root_entry, node);
643 btrfs_err(fs_info, " root entry %llu, num_refs %llu",
644 re->root_objectid, re->num_refs);
647 list_for_each_entry(ra, &be->actions, list)
648 dump_ref_action(fs_info, ra);
652 * btrfs_ref_tree_mod: called when we modify a ref for a bytenr
654 * This will add an action item to the given bytenr and do sanity checks to make
655 * sure we haven't messed something up. If we are making a new allocation and
656 * this block entry has history we will delete all previous actions as long as
657 * our sanity checks pass as they are no longer needed.
659 int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
660 struct btrfs_ref *generic_ref)
662 struct ref_entry *ref = NULL, *exist;
663 struct ref_action *ra = NULL;
664 struct block_entry *be = NULL;
665 struct root_entry *re = NULL;
666 int action = generic_ref->action;
669 u64 bytenr = generic_ref->bytenr;
670 u64 num_bytes = generic_ref->len;
671 u64 parent = generic_ref->parent;
676 if (!btrfs_test_opt(fs_info, REF_VERIFY))
679 if (generic_ref->type == BTRFS_REF_METADATA) {
681 ref_root = generic_ref->tree_ref.owning_root;
682 owner = generic_ref->tree_ref.level;
683 } else if (!parent) {
684 ref_root = generic_ref->data_ref.owning_root;
685 owner = generic_ref->data_ref.ino;
686 offset = generic_ref->data_ref.offset;
688 metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
690 ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
691 ra = kmalloc(sizeof(struct ref_action), GFP_NOFS);
699 ref->parent = parent;
701 ref->root_objectid = ref_root;
702 ref->offset = offset;
703 ref->num_refs = (action == BTRFS_DROP_DELAYED_REF) ? -1 : 1;
705 memcpy(&ra->ref, ref, sizeof(struct ref_entry));
707 * Save the extra info from the delayed ref in the ref action to make it
708 * easier to figure out what is happening. The real ref's we add to the
709 * ref tree need to reflect what we save on disk so it matches any
710 * on-disk refs we pre-loaded.
712 ra->ref.owner = owner;
713 ra->ref.offset = offset;
714 ra->ref.root_objectid = ref_root;
715 __save_stack_trace(ra);
717 INIT_LIST_HEAD(&ra->list);
719 ra->root = generic_ref->real_root;
722 * This is an allocation, preallocate the block_entry in case we haven't
726 if (action == BTRFS_ADD_DELAYED_EXTENT) {
728 * For subvol_create we'll just pass in whatever the parent root
729 * is and the new root objectid, so let's not treat the passed
730 * in root as if it really has a ref for this bytenr.
732 be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
743 if (be->num_refs != 1) {
745 "re-allocated a block that still has references to it!");
746 dump_block_entry(fs_info, be);
747 dump_ref_action(fs_info, ra);
753 while (!list_empty(&be->actions)) {
754 struct ref_action *tmp;
756 tmp = list_first_entry(&be->actions, struct ref_action,
758 list_del(&tmp->list);
762 struct root_entry *tmp;
765 re = kmalloc(sizeof(struct root_entry), GFP_NOFS);
773 * This is the root that is modifying us, so it's the
774 * one we want to lookup below when we modify the
777 ref_root = generic_ref->real_root;
778 re->root_objectid = generic_ref->real_root;
782 spin_lock(&fs_info->ref_verify_lock);
783 be = lookup_block_entry(&fs_info->block_tree, bytenr);
786 "trying to do action %d to bytenr %llu num_bytes %llu but there is no existing entry!",
787 action, bytenr, num_bytes);
788 dump_ref_action(fs_info, ra);
792 } else if (be->num_refs == 0) {
794 "trying to do action %d for a bytenr that has 0 total references",
796 dump_block_entry(fs_info, be);
797 dump_ref_action(fs_info, ra);
804 tmp = insert_root_entry(&be->roots, re);
812 exist = insert_ref_entry(&be->refs, ref);
814 if (action == BTRFS_DROP_DELAYED_REF) {
815 if (exist->num_refs == 0) {
817 "dropping a ref for a existing root that doesn't have a ref on the block");
818 dump_block_entry(fs_info, be);
819 dump_ref_action(fs_info, ra);
825 if (exist->num_refs == 0) {
826 rb_erase(&exist->node, &be->refs);
829 } else if (!be->metadata) {
833 "attempting to add another ref for an existing ref on a tree block");
834 dump_block_entry(fs_info, be);
835 dump_ref_action(fs_info, ra);
842 if (action == BTRFS_DROP_DELAYED_REF) {
844 "dropping a ref for a root that doesn't have a ref on the block");
845 dump_block_entry(fs_info, be);
846 dump_ref_action(fs_info, ra);
853 if (!parent && !re) {
854 re = lookup_root_entry(&be->roots, ref_root);
857 * This shouldn't happen because we will add our re
858 * above when we lookup the be with !parent, but just in
859 * case catch this case so we don't panic because I
860 * didn't think of some other corner case.
862 btrfs_err(fs_info, "failed to find root %llu for %llu",
863 generic_ref->real_root, be->bytenr);
864 dump_block_entry(fs_info, be);
865 dump_ref_action(fs_info, ra);
870 if (action == BTRFS_DROP_DELAYED_REF) {
874 } else if (action == BTRFS_ADD_DELAYED_REF) {
879 list_add_tail(&ra->list, &be->actions);
882 spin_unlock(&fs_info->ref_verify_lock);
885 btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
889 /* Free up the ref cache */
890 void btrfs_free_ref_cache(struct btrfs_fs_info *fs_info)
892 struct block_entry *be;
895 if (!btrfs_test_opt(fs_info, REF_VERIFY))
898 spin_lock(&fs_info->ref_verify_lock);
899 while ((n = rb_first(&fs_info->block_tree))) {
900 be = rb_entry(n, struct block_entry, node);
901 rb_erase(&be->node, &fs_info->block_tree);
902 free_block_entry(be);
903 cond_resched_lock(&fs_info->ref_verify_lock);
905 spin_unlock(&fs_info->ref_verify_lock);
908 void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
911 struct block_entry *be = NULL, *entry;
914 if (!btrfs_test_opt(fs_info, REF_VERIFY))
917 spin_lock(&fs_info->ref_verify_lock);
918 n = fs_info->block_tree.rb_node;
920 entry = rb_entry(n, struct block_entry, node);
921 if (entry->bytenr < start) {
923 } else if (entry->bytenr > start) {
929 /* We want to get as close to start as possible */
931 (entry->bytenr < start && be->bytenr > start) ||
932 (entry->bytenr < start && entry->bytenr > be->bytenr))
937 * Could have an empty block group, maybe have something to check for
938 * this case to verify we were actually empty?
941 spin_unlock(&fs_info->ref_verify_lock);
947 be = rb_entry(n, struct block_entry, node);
949 if (be->bytenr < start && be->bytenr + be->len > start) {
951 "block entry overlaps a block group [%llu,%llu]!",
953 dump_block_entry(fs_info, be);
956 if (be->bytenr < start)
958 if (be->bytenr >= start + len)
960 if (be->bytenr + be->len > start + len) {
962 "block entry overlaps a block group [%llu,%llu]!",
964 dump_block_entry(fs_info, be);
966 rb_erase(&be->node, &fs_info->block_tree);
967 free_block_entry(be);
969 spin_unlock(&fs_info->ref_verify_lock);
972 /* Walk down all roots and build the ref tree, meant to be called at mount */
973 int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
975 struct btrfs_root *extent_root;
976 struct btrfs_path *path;
977 struct extent_buffer *eb;
978 int tree_block_level = 0;
979 u64 bytenr = 0, num_bytes = 0;
982 if (!btrfs_test_opt(fs_info, REF_VERIFY))
985 path = btrfs_alloc_path();
989 extent_root = btrfs_extent_root(fs_info, 0);
990 eb = btrfs_read_lock_root_node(extent_root);
991 level = btrfs_header_level(eb);
992 path->nodes[level] = eb;
993 path->slots[level] = 0;
994 path->locks[level] = BTRFS_READ_LOCK;
998 * We have to keep track of the bytenr/num_bytes we last hit
999 * because we could have run out of space for an inline ref, and
1000 * would have had to added a ref key item which may appear on a
1001 * different leaf from the original extent item.
1003 ret = walk_down_tree(extent_root, path, level,
1004 &bytenr, &num_bytes, &tree_block_level);
1007 ret = walk_up_tree(path, &level);
1016 btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
1017 btrfs_free_ref_cache(fs_info);
1019 btrfs_free_path(path);