summaryrefslogtreecommitdiff
path: root/lib/btrfsprogs
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-06 02:03:50 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:20:05 -0700
commit8530a8b7b5f9f27bb5b5c319f285bb5a26470285 (patch)
tree3070f8e76149bc98aeef0572f5147c4d83084acf /lib/btrfsprogs
parent2643592b0a802f3816a47f8d3425a8f3282eae3b (diff)
rebuildnodes: Rework to be clear about what went wrong when reading
Diffstat (limited to 'lib/btrfsprogs')
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go4
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go14
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go24
3 files changed, 14 insertions, 28 deletions
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
index c9d0fa4..f870121 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
@@ -329,10 +329,10 @@ func (tree *RebuiltTree) COWDistance(parentID btrfsprim.ObjID) (dist int, ok boo
}
// ReadItem reads an item from a tree.
-func (tree *RebuiltTree) ReadItem(ctx context.Context, key btrfsprim.Key) (item btrfsitem.Item, ok bool) {
+func (tree *RebuiltTree) ReadItem(ctx context.Context, key btrfsprim.Key) btrfsitem.Item {
ptr, ok := tree.Items(ctx).Load(key)
if !ok {
- return nil, false
+ panic(fmt.Errorf("should not happen: btrees.RebuiltTree.ReadItem called for not-included key: %v", key))
}
return tree.forrest.keyIO.ReadItem(ctx, ptr)
}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
index b4ab645..9e3b144 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go
@@ -152,13 +152,17 @@ func (o *Handle) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr) *disk
return ref
}
-func (o *Handle) ReadItem(ctx context.Context, ptr ItemPtr) (item btrfsitem.Item, ok bool) {
- if o.graph.Nodes[ptr.Node].Level != 0 || ptr.Idx < 0 {
- return nil, false
+func (o *Handle) ReadItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item {
+ 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))
+ }
+ if ptr.Idx < 0 {
+ panic(fmt.Errorf("should not happen: keyio.Handle.ReadItem called for negative item index: %v", ptr.Idx))
}
items := o.readNode(ctx, ptr.Node).Data.BodyLeaf
if ptr.Idx >= len(items) {
- return nil, false
+ 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, true
+ return items[ptr.Idx].Body
}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
index 624441f..d11ca58 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go
@@ -91,12 +91,6 @@ func NewRebuilder(ctx context.Context, fs *btrfs.FS, nodeScanResults btrfsinspec
return o, nil
}
-func (o *rebuilder) ioErr(ctx context.Context, err error) {
- err = fmt.Errorf("should not happen: i/o error: %w", err)
- dlog.Error(ctx, err)
- panic(err)
-}
-
func (o *rebuilder) ListRoots() map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr] {
return o.rebuilt.ListRoots()
}
@@ -174,13 +168,9 @@ func (o *rebuilder) Rebuild(_ctx context.Context) error {
return err
}
itemCtx := dlog.WithField(stepCtx, "btrfsinspect.rebuild-nodes.rebuild.process.item", key)
- itemBody, ok := o.rebuilt.Tree(itemCtx, key.TreeID).ReadItem(itemCtx, key.Key)
- if !ok {
- o.ioErr(itemCtx, fmt.Errorf("could not read previously read item: %v", key))
- }
itemChan <- keyAndBody{
keyAndTree: key,
- Body: itemBody,
+ Body: o.rebuilt.Tree(itemCtx, key.TreeID).ReadItem(itemCtx, key.Key),
}
}
return nil
@@ -285,11 +275,7 @@ func (o *rebuilder) cbLookupRoot(ctx context.Context, tree btrfsprim.ObjID) (off
o.enqueueRetry()
return 0, btrfsitem.Root{}, false
}
- itemBody, ok := o.rebuilt.Tree(ctx, key.TreeID).ReadItem(ctx, key.Key)
- if !ok {
- o.ioErr(ctx, fmt.Errorf("could not read previously read item: %v", key))
- }
- switch itemBody := itemBody.(type) {
+ switch itemBody := o.rebuilt.Tree(ctx, key.TreeID).ReadItem(ctx, key.Key).(type) {
case *btrfsitem.Root:
return btrfsprim.Generation(key.Offset), *itemBody, true
case *btrfsitem.Error:
@@ -310,11 +296,7 @@ func (o *rebuilder) cbLookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id b
o.enqueueRetry()
return 0, false
}
- itemBody, ok := o.rebuilt.Tree(ctx, key.TreeID).ReadItem(ctx, key.Key)
- if !ok {
- o.ioErr(ctx, fmt.Errorf("could not read previously read item: %v", key))
- }
- switch itemBody := itemBody.(type) {
+ switch itemBody := o.rebuilt.Tree(ctx, key.TreeID).ReadItem(ctx, key.Key).(type) {
case *btrfsitem.UUIDMap:
return itemBody.ObjID, true
case *btrfsitem.Error: