From de9874d67f8a5b4bd934137c82d7010b58dfe004 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 18:44:26 -0700 Subject: tree-wide: Audit for simplistic type assertions --- lib/btrfs/btrfstree/root.go | 23 ++++--- lib/btrfs/csums.go | 16 +++-- lib/btrfs/io4_fs.go | 24 +++++--- lib/btrfsprogs/btrfsinspect/scandevices.go | 98 ++++++++++++++++-------------- 4 files changed, 91 insertions(+), 70 deletions(-) (limited to 'lib') diff --git a/lib/btrfs/btrfstree/root.go b/lib/btrfs/btrfstree/root.go index 12df0b2..6ec45b5 100644 --- a/lib/btrfs/btrfstree/root.go +++ b/lib/btrfs/btrfstree/root.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -71,15 +71,18 @@ func LookupTreeRoot(fs TreeOperator, sb Superblock, treeID btrfsprim.ObjID) (*Tr if err != nil { return nil, err } - rootItemBody, ok := rootItem.Body.(btrfsitem.Root) - if !ok { - return nil, fmt.Errorf("malformed ROOT_ITEM for tree %v", treeID) + switch rootItemBody := rootItem.Body.(type) { + case btrfsitem.Root: + return &TreeRoot{ + TreeID: treeID, + RootNode: rootItemBody.ByteNr, + Level: rootItemBody.Level, + Generation: rootItemBody.Generation, + }, nil + case btrfsitem.Error: + return nil, fmt.Errorf("malformed ROOT_ITEM for tree %v: %w", treeID, rootItemBody.Err) + default: + panic(fmt.Errorf("should not happen: ROOT_ITEM has unexpected item type: %T", rootItemBody)) } - return &TreeRoot{ - TreeID: treeID, - RootNode: rootItemBody.ByteNr, - Level: rootItemBody.Level, - Generation: rootItemBody.Generation, - }, nil } } diff --git a/lib/btrfs/csums.go b/lib/btrfs/csums.go index bbd19bd..a32f090 100644 --- a/lib/btrfs/csums.go +++ b/lib/btrfs/csums.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -56,9 +56,15 @@ func LookupCSum(fs btrfstree.TreeOperator, alg btrfssum.CSumType, laddr btrfsvol if err != nil { return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, err } - body, ok := item.Body.(btrfsitem.ExtentCSum) - if !ok { - return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, fmt.Errorf("item body is %T not ExtentCSum", item.Body) + if item.Key.ItemType != btrfsitem.EXTENT_CSUM_KEY { + return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, fmt.Errorf("item type is %v, not EXTENT_CSUM", item.Key.ItemType) + } + switch body := item.Body.(type) { + case btrfsitem.ExtentCSum: + return body.SumRun, nil + case btrfsitem.Error: + return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, body.Err + default: + panic(fmt.Errorf("should not happen: EXTENT_CSUM has unexpected item type: %T", body)) } - return body.SumRun, nil } diff --git a/lib/btrfs/io4_fs.go b/lib/btrfs/io4_fs.go index d7c2770..d9c7cdb 100644 --- a/lib/btrfs/io4_fs.go +++ b/lib/btrfs/io4_fs.go @@ -87,13 +87,15 @@ func (sv *Subvolume) init() { return } - rootBody, ok := root.Body.(btrfsitem.Root) - if !ok { - sv.rootErr = fmt.Errorf("FS_TREE ROOT_ITEM has malformed body") - return + switch rootBody := root.Body.(type) { + case btrfsitem.Root: + sv.rootVal = rootBody + case btrfsitem.Error: + sv.rootErr = fmt.Errorf("FS_TREE ROOT_ITEM has malformed body: %w", rootBody.Err) + default: + panic(fmt.Errorf("should not happen: ROOT_ITEM has unexpected item type: %T", rootBody)) } - sv.rootVal = rootBody }) } @@ -117,12 +119,14 @@ func (sv *Subvolume) LoadBareInode(inode btrfsprim.ObjID) (*BareInode, error) { return } - itemBody, ok := item.Body.(btrfsitem.Inode) - if !ok { - val.Errs = append(val.Errs, fmt.Errorf("malformed inode")) - return + switch itemBody := item.Body.(type) { + case btrfsitem.Inode: + val.InodeItem = &itemBody + case btrfsitem.Error: + val.Errs = append(val.Errs, fmt.Errorf("malformed inode: %w", itemBody.Err)) + default: + panic(fmt.Errorf("should not happen: INODE_ITEM has unexpected item type: %T", itemBody)) } - val.InodeItem = &itemBody return }) diff --git a/lib/btrfsprogs/btrfsinspect/scandevices.go b/lib/btrfsprogs/btrfsinspect/scandevices.go index 628995a..4058663 100644 --- a/lib/btrfsprogs/btrfsinspect/scandevices.go +++ b/lib/btrfsprogs/btrfsinspect/scandevices.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -176,57 +176,65 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfstree.Superblo for i, item := range nodeRef.Data.BodyLeaf { switch item.Key.ItemType { case btrfsitem.CHUNK_ITEM_KEY: - chunk, ok := item.Body.(btrfsitem.Chunk) - if !ok { - dlog.Errorf(ctx, "node@%v: item %v: error: type is CHUNK_ITEM_KEY, but struct is %T", - nodeRef.Addr, i, item.Body) - continue + switch itemBody := item.Body.(type) { + case btrfsitem.Chunk: + dlog.Tracef(ctx, "node@%v: item %v: found chunk", + nodeRef.Addr, i) + result.FoundChunks = append(result.FoundChunks, btrfstree.SysChunk{ + Key: item.Key, + Chunk: itemBody, + }) + case btrfsitem.Error: + dlog.Errorf(ctx, "node@%v: item %v: error: malformed CHUNK_ITEM: %v", + nodeRef.Addr, i, itemBody.Err) + default: + panic(fmt.Errorf("should not happen: CHUNK_ITEM has unexpected item type: %T", itemBody)) } - dlog.Tracef(ctx, "node@%v: item %v: found chunk", - nodeRef.Addr, i) - result.FoundChunks = append(result.FoundChunks, btrfstree.SysChunk{ - Key: item.Key, - Chunk: chunk, - }) case btrfsitem.BLOCK_GROUP_ITEM_KEY: - bg, ok := item.Body.(btrfsitem.BlockGroup) - if !ok { - dlog.Errorf(ctx, "node@%v: item %v: error: type is BLOCK_GROUP_ITEM_KEY, but struct is %T", - nodeRef.Addr, i, item.Body) - continue + switch itemBody := item.Body.(type) { + case btrfsitem.BlockGroup: + dlog.Tracef(ctx, "node@%v: item %v: found block group", + nodeRef.Addr, i) + result.FoundBlockGroups = append(result.FoundBlockGroups, SysBlockGroup{ + Key: item.Key, + BG: itemBody, + }) + case btrfsitem.Error: + dlog.Errorf(ctx, "node@%v: item %v: error: malformed BLOCK_GROUP_ITEM: %v", + nodeRef.Addr, i, itemBody.Err) + default: + panic(fmt.Errorf("should not happen: BLOCK_GROUP_ITEM has unexpected item type: %T", itemBody)) } - dlog.Tracef(ctx, "node@%v: item %v: found block group", - nodeRef.Addr, i) - result.FoundBlockGroups = append(result.FoundBlockGroups, SysBlockGroup{ - Key: item.Key, - BG: bg, - }) case btrfsitem.DEV_EXTENT_KEY: - devext, ok := item.Body.(btrfsitem.DevExtent) - if !ok { - dlog.Errorf(ctx, "node@%v: item %v: error: type is DEV_EXTENT_KEY, but struct is %T", - nodeRef.Addr, i, item.Body) - continue + switch itemBody := item.Body.(type) { + case btrfsitem.DevExtent: + dlog.Tracef(ctx, "node@%v: item %v: found dev extent", + nodeRef.Addr, i) + result.FoundDevExtents = append(result.FoundDevExtents, SysDevExtent{ + Key: item.Key, + DevExt: itemBody, + }) + case btrfsitem.Error: + dlog.Errorf(ctx, "node@%v: item %v: error: malformed DEV_EXTENT: %v", + nodeRef.Addr, i, itemBody.Err) + default: + panic(fmt.Errorf("should not happen: DEV_EXTENT has unexpected item type: %T", itemBody)) } - dlog.Tracef(ctx, "node@%v: item %v: found dev extent", - nodeRef.Addr, i) - result.FoundDevExtents = append(result.FoundDevExtents, SysDevExtent{ - Key: item.Key, - DevExt: devext, - }) case btrfsitem.EXTENT_CSUM_KEY: - sums, ok := item.Body.(btrfsitem.ExtentCSum) - if !ok { - dlog.Errorf(ctx, "node@%v: item %v: error: type is EXTENT_CSUM_OBJECTID, but struct is %T", - nodeRef.Addr, i, item.Body) - continue + switch itemBody := item.Body.(type) { + case btrfsitem.ExtentCSum: + dlog.Tracef(ctx, "node@%v: item %v: found csums", + nodeRef.Addr, i) + result.FoundExtentCSums = append(result.FoundExtentCSums, SysExtentCSum{ + Generation: nodeRef.Data.Head.Generation, + Sums: itemBody, + }) + case btrfsitem.Error: + dlog.Errorf(ctx, "node@%v: item %v: error: malformed is EXTENT_CSUM: %v", + nodeRef.Addr, i, itemBody.Err) + default: + panic(fmt.Errorf("should not happen: EXTENT_CSUM has unexpected item type: %T", itemBody)) } - dlog.Tracef(ctx, "node@%v: item %v: found csums", - nodeRef.Addr, i) - result.FoundExtentCSums = append(result.FoundExtentCSums, SysExtentCSum{ - Generation: nodeRef.Data.Head.Generation, - Sums: sums, - }) } } minNextNode = pos + btrfsvol.PhysicalAddr(sb.NodeSize) -- cgit v1.2.3-2-g168b