diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-08-29 19:25:47 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-08-30 21:29:00 -0600 |
commit | b6a7ab5f6c1fe993271242021c3ed4050733fdf2 (patch) | |
tree | 2a0a099e0adc5bf58c586a76d9dd49c725d4dba7 /lib/btrfsprogs/btrfsinspect/rebuildnodes | |
parent | 8aea2c35c9475293c89293a148134c0e6d5c4e7b (diff) |
wip
Diffstat (limited to 'lib/btrfsprogs/btrfsinspect/rebuildnodes')
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuilttrees.go | 17 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/rebuildnodes/uuidmap.go | 40 |
2 files changed, 38 insertions, 19 deletions
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{}{} |