From b6a7ab5f6c1fe993271242021c3ed4050733fdf2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 29 Aug 2022 19:25:47 -0600 Subject: wip --- lib/btrfs/btrfstree/readnode.go | 27 ++++++++++++++++++++++----- lib/btrfs/io3_btree.go | 17 +++++++++++------ 2 files changed, 33 insertions(+), 11 deletions(-) (limited to 'lib/btrfs') 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 -- cgit v1.2.3-54-g00ecf