From 996137cdfdff9ec6fbb5c93e21d75b21052e01ae Mon Sep 17 00:00:00 2001
From: Luke Shumaker <lukeshu@lukeshu.com>
Date: Tue, 28 Feb 2023 08:05:47 -0700
Subject: rebuildnodes: Fix a data race

Calling FreeNodeRef while an item from that node is still in use is a BAD
TIME.
---
 lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go    | 6 +++++-
 lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go        | 1 +
 lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_treecb.go | 8 ++++++--
 3 files changed, 12 insertions(+), 3 deletions(-)

(limited to 'lib/btrfsprogs/btrfsinspect')

diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
index 9e3b144..04df2b6 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
@@ -7,6 +7,7 @@ package keyio
 import (
 	"context"
 	"fmt"
+	"sync"
 
 	"github.com/datawire/dlib/dlog"
 
@@ -48,6 +49,7 @@ type Handle struct {
 	Names map[ItemPtr][]byte      // DIR_INDEX
 	Sizes map[ItemPtr]SizeAndErr  // EXTENT_CSUM and EXTENT_DATA
 
+	mu    sync.Mutex
 	cache containers.ARCache[btrfsvol.LogicalAddr, *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]]
 }
 
@@ -153,6 +155,8 @@ func (o *Handle) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr) *disk
 }
 
 func (o *Handle) ReadItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item {
+	o.mu.Lock()
+	defer o.mu.Unlock()
 	if o.graph.Nodes[ptr.Node].Level != 0 {
 		panic(fmt.Errorf("should not happen: keyio.Handle.ReadItem called for non-leaf node@%v", ptr.Node))
 	}
@@ -164,5 +168,5 @@ func (o *Handle) ReadItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item {
 		panic(fmt.Errorf("should not happen: keyio.Handle.ReadItem called for out-of-bounds item index: index=%v len=%v",
 			ptr.Idx, len(items)))
 	}
-	return items[ptr.Idx].Body
+	return items[ptr.Idx].Body.CloneItem()
 }
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
index 643fbe7..bc36485 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
@@ -288,6 +288,7 @@ func (o *rebuilder) processSettledItemQueue(ctx context.Context) error {
 				Key:  item.Key,
 				Body: item.Body,
 			})
+			item.Body.Free()
 			if item.ItemType == btrfsitem.ROOT_ITEM_KEY {
 				o.treeQueue.Insert(item.ObjectID)
 			}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_treecb.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_treecb.go
index d1d354a..492436b 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_treecb.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_treecb.go
@@ -44,7 +44,9 @@ func (o *rebuilder) LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offse
 		o.enqueueRetry(btrfsprim.ROOT_TREE_OBJECTID)
 		return 0, btrfsitem.Root{}, false
 	}
-	switch itemBody := o.rebuilt.Tree(ctx, wantKey.TreeID).ReadItem(ctx, foundKey).(type) {
+	itemBody := o.rebuilt.Tree(ctx, wantKey.TreeID).ReadItem(ctx, foundKey)
+	defer itemBody.Free()
+	switch itemBody := itemBody.(type) {
 	case *btrfsitem.Root:
 		return btrfsprim.Generation(foundKey.Offset), *itemBody, true
 	case *btrfsitem.Error:
@@ -68,7 +70,9 @@ func (o *rebuilder) LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btr
 		o.enqueueRetry(btrfsprim.UUID_TREE_OBJECTID)
 		return 0, false
 	}
-	switch itemBody := o.rebuilt.Tree(ctx, wantKey.TreeID).ReadItem(ctx, wantKey.Key.Key()).(type) {
+	itemBody := o.rebuilt.Tree(ctx, wantKey.TreeID).ReadItem(ctx, wantKey.Key.Key())
+	defer itemBody.Free()
+	switch itemBody := itemBody.(type) {
 	case *btrfsitem.UUIDMap:
 		return itemBody.ObjID, true
 	case *btrfsitem.Error:
-- 
cgit v1.2.3-2-g168b