summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/btrfs/btrfstree/readnode.go27
-rw-r--r--lib/btrfs/io3_btree.go17
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go17
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go40
4 files changed, 71 insertions, 30 deletions
diff --git a/lib/btrfs/btrfstree/readnode.go b/lib/btrfs/btrfstree/readnode.go
index bb80d20..b1c34e4 100644
--- a/lib/btrfs/btrfstree/readnode.go
+++ b/lib/btrfs/btrfstree/readnode.go
@@ -13,14 +13,26 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
)
+type NodeFile interface {
+ diskio.File[btrfsvol.LogicalAddr]
+ Superblock() (*Superblock, error)
+
+ // ParentTree, given a tree ID, returns that tree's parent
+ // tree, if it has one.
+ //
+ // - non-zero, true : the parent tree ID
+ //
+ // - 0, true : the tree does not have a parent
+ //
+ // - any, false : the tree's parent information could not be
+ // looked up
+ ParentTree(btrfsprim.ObjID) (btrfsprim.ObjID, bool)
+}
+
// FSReadNode is a utility function to help with implementing the
// 'NodeSource' interface.
func FSReadNode(
- fs interface {
- diskio.File[btrfsvol.LogicalAddr]
- Superblock() (*Superblock, error)
- ParentTree(btrfsprim.ObjID) (btrfsprim.ObjID, bool)
- },
+ fs NodeFile,
path TreePath,
) (*diskio.Ref[btrfsvol.LogicalAddr, Node], error) {
sb, err := fs.Superblock()
@@ -39,6 +51,11 @@ func FSReadNode(
var ok bool
exp, ok = fs.ParentTree(exp)
if !ok {
+ // Failed lookup up parent info; fail open.
+ return nil
+ }
+ if exp == 0 {
+ // End of the line.
return fmt.Errorf("expected owner in %v but claims to have owner=%v",
treeParents, owner)
}
diff --git a/lib/btrfs/io3_btree.go b/lib/btrfs/io3_btree.go
index 8455eee..26acc22 100644
--- a/lib/btrfs/io3_btree.go
+++ b/lib/btrfs/io3_btree.go
@@ -58,18 +58,23 @@ func (fs *FS) populateTreeUUIDs(ctx context.Context) {
}
func (fs *FS) ParentTree(tree btrfsprim.ObjID) (btrfsprim.ObjID, bool) {
- ctx := context.TODO()
- if tree < btrfsprim.FIRST_FREE_OBJECTID {
+ if tree < btrfsprim.FIRST_FREE_OBJECTID || tree > btrfsprim.LAST_FREE_OBJECTID {
+ // no parent
+ return 0, true
+ }
+ fs.populateTreeUUIDs(context.TODO())
+ parentUUID, ok := fs.cacheTreeParent[tree]
+ if !ok {
+ // could not look up parent info
return 0, false
}
- fs.populateTreeUUIDs(ctx)
- parentUUID := fs.cacheTreeParent[tree]
if parentUUID == (btrfsprim.UUID{}) {
- return 0, false
+ // no parent
+ return 0, true
}
parentObjID, ok := fs.cacheUUID2ObjID[parentUUID]
if !ok {
- dlog.Errorf(ctx, "dbg: could not resolve parentUUID=%v to an ObjID", parentUUID)
+ // could not look up parent info
return 0, false
}
return parentObjID, true
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go
index 17116c6..37db791 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go
@@ -54,19 +54,26 @@ func (fs *RebuiltTrees) WriteAt(p []byte, off btrfsvol.LogicalAddr) (int, error)
return fs.inner.WriteAt(p, off)
}
-// btrfstree.NodeSource backend
+// btrfstree.NodeFile
func (fs *RebuiltTrees) ParentTree(tree btrfsprim.ObjID) (btrfsprim.ObjID, bool) {
- if tree < btrfsprim.FIRST_FREE_OBJECTID {
+ if tree < btrfsprim.FIRST_FREE_OBJECTID || tree > btrfsprim.LAST_FREE_OBJECTID {
+ // no parent
+ return 0, true
+ }
+ parentUUID, ok := fs.uuidMap.TreeParent[tree]
+ if !ok {
+ // could not look up parent info
return 0, false
}
- parentUUID := fs.uuidMap.TreeParent[tree]
if parentUUID == (btrfsprim.UUID{}) {
- return 0, false
+ // no parent
+ return 0, true
}
parentObjID, ok := fs.uuidMap.UUID2ObjID[parentUUID]
if !ok {
- panic(fmt.Errorf("should not happen: could not resolve parentUUID=%v to an ObjID", parentUUID))
+ // could not look up parent info
+ return 0, false
}
return parentObjID, true
}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go
index 0179a6e..95bb3b1 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go
@@ -6,6 +6,7 @@ package rebuildnodes
import (
"context"
+ "encoding/binary"
"fmt"
"github.com/datawire/dlib/dlog"
@@ -26,9 +27,9 @@ type treeUUIDMap struct {
TreeParent map[btrfsprim.ObjID]btrfsprim.UUID
}
-func maybeSet[K, V comparable](m map[K]V, k K, v V) error {
+func maybeSet[K, V comparable](name string, m map[K]V, k K, v V) error {
if other, conflict := m[k]; conflict && other != v {
- return fmt.Errorf("conflict: %v can't have both %v and %v", k, other, v)
+ return fmt.Errorf("conflict: %s %v can't have both %v and %v", name, k, other, v)
}
m[k] = v
return nil
@@ -71,18 +72,29 @@ func buildTreeUUIDMap(ctx context.Context, fs *btrfs.FS, scanResults btrfsinspec
return treeUUIDMap{}, nil
}
for _, item := range nodeRef.Data.BodyLeaf {
- itemBody, ok := item.Body.(btrfsitem.Root)
- if !ok {
- continue
- }
- if err := maybeSet(ret.ObjID2UUID, item.Key.ObjectID, itemBody.UUID); err != nil {
- return treeUUIDMap{}, err
- }
- if err := maybeSet(ret.TreeParent, item.Key.ObjectID, itemBody.ParentUUID); err != nil {
- return treeUUIDMap{}, err
- }
- if err := maybeSet(ret.UUID2ObjID, itemBody.UUID, item.Key.ObjectID); err != nil {
- return treeUUIDMap{}, err
+ switch itemBody := item.Body.(type) {
+ case btrfsitem.Root:
+ if err := maybeSet("ObjID2UUID", ret.ObjID2UUID, item.Key.ObjectID, itemBody.UUID); err != nil {
+ return treeUUIDMap{}, err
+ }
+ if itemBody.UUID != (btrfsprim.UUID{}) {
+ if err := maybeSet("UUID2ObjID", ret.UUID2ObjID, itemBody.UUID, item.Key.ObjectID); err != nil {
+ return treeUUIDMap{}, err
+ }
+ }
+ if err := maybeSet("ParentUUID", ret.TreeParent, item.Key.ObjectID, itemBody.ParentUUID); err != nil {
+ return treeUUIDMap{}, err
+ }
+ case btrfsitem.UUIDMap:
+ var uuid btrfsprim.UUID
+ binary.BigEndian.PutUint64(uuid[:8], uint64(item.Key.ObjectID))
+ binary.BigEndian.PutUint64(uuid[8:], uint64(item.Key.Offset))
+ if err := maybeSet("ObjID2UUID", ret.ObjID2UUID, itemBody.ObjID, uuid); err != nil {
+ return treeUUIDMap{}, err
+ }
+ if err := maybeSet("UUID2ObjID", ret.UUID2ObjID, uuid, itemBody.ObjID); err != nil {
+ return treeUUIDMap{}, err
+ }
}
}
treeIDs[nodeRef.Data.Head.Owner] = struct{}{}