From beddf8b9e3a45c864bb4b00b7c4fef1e560d59eb Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 30 Mar 2023 15:13:57 -0600 Subject: btrfsutil: OldRebuiltForrest: Move ReadableFS methods to the end ...and touch up comments. --- lib/btrfsutil/old_rebuilt_forrest.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/btrfsutil/old_rebuilt_forrest.go b/lib/btrfsutil/old_rebuilt_forrest.go index 58333df..91fa275 100644 --- a/lib/btrfsutil/old_rebuilt_forrest.go +++ b/lib/btrfsutil/old_rebuilt_forrest.go @@ -393,21 +393,6 @@ func (tree oldRebuiltTree) TreeWalk(ctx context.Context, cbs btrfstree.TreeWalkH tree.forrest.inner.ReleaseNode(node) } -// Superblock implements btrfs.ReadableFS. -func (bt *OldRebuiltForrest) Superblock() (*btrfstree.Superblock, error) { - return bt.inner.Superblock() -} - -// ReadAt implements diskio.ReaderAt (and btrfs.ReadableFS). -func (bt *OldRebuiltForrest) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { - return bt.inner.ReadAt(p, off) -} - -// Name implements btrfs.ReadableFS. -func (bt *OldRebuiltForrest) Name() string { - return bt.inner.Name() -} - // TreeCheckOwner implements btrfstree.Tree. func (tree oldRebuiltTree) TreeCheckOwner(ctx context.Context, failOpen bool, owner btrfsprim.ObjID, gen btrfsprim.Generation) error { var uuidTree oldRebuiltTree @@ -463,3 +448,20 @@ func (tree oldRebuiltTree) TreeCheckOwner(ctx context.Context, failOpen bool, ow } } } + +// btrfs.ReadableFS (other than btrfstree.Forrest) ///////////////////////////////////////////////////////////////////// + +// Name implements btrfs.ReadableFS. +func (bt *OldRebuiltForrest) Name() string { + return bt.inner.Name() +} + +// Superblock implements btrfstree.NodeSource (and btrfs.ReadableFS). +func (bt *OldRebuiltForrest) Superblock() (*btrfstree.Superblock, error) { + return bt.inner.Superblock() +} + +// ReadAt implements diskio.ReaderAt[btrfsvol.LogicalAddr] (and btrfs.ReadableFS). +func (bt *OldRebuiltForrest) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { + return bt.inner.ReadAt(p, off) +} -- cgit v1.2.3-2-g168b From e3f0e61282d0190b9744c69ce69dcaeea22e6a3e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 30 Mar 2023 15:11:31 -0600 Subject: btrfs: ReadableFS: Also embed btrfstree.NodeSource --- lib/btrfs/io3_btree.go | 8 +++++--- lib/btrfsutil/old_rebuilt_forrest.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/btrfs/io3_btree.go b/lib/btrfs/io3_btree.go index 01797df..50736cf 100644 --- a/lib/btrfs/io3_btree.go +++ b/lib/btrfs/io3_btree.go @@ -92,11 +92,13 @@ var _ btrfstree.Forrest = (*FS)(nil) // ReadableFS ////////////////////////////////////////////////////////////////// type ReadableFS interface { - btrfstree.Forrest + Name() string - Superblock() (*btrfstree.Superblock, error) + // For reading btrees. + btrfstree.Forrest - Name() string + // For reading the superblock and raw nodes. + btrfstree.NodeSource // For reading file contents. diskio.ReaderAt[btrfsvol.LogicalAddr] diff --git a/lib/btrfsutil/old_rebuilt_forrest.go b/lib/btrfsutil/old_rebuilt_forrest.go index 91fa275..535aa94 100644 --- a/lib/btrfsutil/old_rebuilt_forrest.go +++ b/lib/btrfsutil/old_rebuilt_forrest.go @@ -461,6 +461,16 @@ func (bt *OldRebuiltForrest) Superblock() (*btrfstree.Superblock, error) { return bt.inner.Superblock() } +// AcquireNode implements btrfstree.NodeSource (and btrfs.ReadableFS). +func (bt *OldRebuiltForrest) AcquireNode(ctx context.Context, addr btrfsvol.LogicalAddr, exp btrfstree.NodeExpectations) (*btrfstree.Node, error) { + return bt.inner.AcquireNode(ctx, addr, exp) +} + +// ReleaseNode implements btrfstree.NodeSource (and btrfs.ReadableFS). +func (bt *OldRebuiltForrest) ReleaseNode(node *btrfstree.Node) { + bt.inner.ReleaseNode(node) +} + // ReadAt implements diskio.ReaderAt[btrfsvol.LogicalAddr] (and btrfs.ReadableFS). func (bt *OldRebuiltForrest) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { return bt.inner.ReadAt(p, off) -- cgit v1.2.3-2-g168b From b983da08439f041e9b93fa61aa2db5704be60022 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 30 Mar 2023 15:15:50 -0600 Subject: btrfsutil: OldRebuiltForrest: Simplify --- lib/btrfsutil/old_rebuilt_forrest.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/btrfsutil/old_rebuilt_forrest.go b/lib/btrfsutil/old_rebuilt_forrest.go index 535aa94..e6a0399 100644 --- a/lib/btrfsutil/old_rebuilt_forrest.go +++ b/lib/btrfsutil/old_rebuilt_forrest.go @@ -246,7 +246,7 @@ func (tree oldRebuiltTree) addErrs(fn func(btrfsprim.Key, uint32) int, err error } func (bt *OldRebuiltForrest) readNode(ctx context.Context, nodeInfo nodeInfo) *btrfstree.Node { - node, err := bt.inner.AcquireNode(ctx, nodeInfo.LAddr, btrfstree.NodeExpectations{ + node, err := bt.AcquireNode(ctx, nodeInfo.LAddr, btrfstree.NodeExpectations{ LAddr: containers.OptionalValue(nodeInfo.LAddr), Level: containers.OptionalValue(nodeInfo.Level), Generation: containers.OptionalValue(nodeInfo.Generation), @@ -287,7 +287,7 @@ func (tree oldRebuiltTree) TreeSearch(ctx context.Context, searcher btrfstree.Tr } node := tree.forrest.readNode(ctx, indexItem.Value.Node) - defer tree.forrest.inner.ReleaseNode(node) + defer tree.forrest.ReleaseNode(node) item := node.BodyLeaf[indexItem.Value.Slot] item.Body = item.Body.CloneItem() @@ -307,12 +307,12 @@ func (tree oldRebuiltTree) TreeRange(ctx context.Context, handleFn func(btrfstre tree.Items.Range( func(rbnode *containers.RBNode[oldRebuiltTreeValue]) bool { if node == nil || node.Head.Addr != rbnode.Value.Node.LAddr { - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) node = tree.forrest.readNode(ctx, rbnode.Value.Node) } return handleFn(node.BodyLeaf[rbnode.Value.Slot]) }) - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) return tree.addErrs(func(btrfsprim.Key, uint32) int { return 0 }, nil) } @@ -328,12 +328,12 @@ func (tree oldRebuiltTree) TreeSubrange(ctx context.Context, min int, searcher b func(rbNode *containers.RBNode[oldRebuiltTreeValue]) bool { cnt++ if node == nil || node.Head.Addr != rbNode.Value.Node.LAddr { - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) node = tree.forrest.readNode(ctx, rbNode.Value.Node) } return handleFn(node.BodyLeaf[rbNode.Value.Slot]) }) - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) var err error if cnt < min { @@ -359,7 +359,7 @@ func (tree oldRebuiltTree) TreeWalk(ctx context.Context, cbs btrfstree.TreeWalkH } if node == nil || node.Head.Addr != indexItem.Value.Node.LAddr { - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) node = tree.forrest.readNode(ctx, indexItem.Value.Node) } item := node.BodyLeaf[indexItem.Value.Slot] @@ -390,7 +390,7 @@ func (tree oldRebuiltTree) TreeWalk(ctx context.Context, cbs btrfstree.TreeWalkH } return ctx.Err() == nil }) - tree.forrest.inner.ReleaseNode(node) + tree.forrest.ReleaseNode(node) } // TreeCheckOwner implements btrfstree.Tree. -- cgit v1.2.3-2-g168b From 8c2de0e63ac106b910173ed17f394b725103596f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 10 Mar 2023 19:31:50 -0700 Subject: cmd/btrfs-rec: Add a runWithReadableFS helper for rebuilt forrests --- cmd/btrfs-rec/inspect/mount/mount.go | 17 +++-------------- cmd/btrfs-rec/inspect_dumptrees.go | 2 +- cmd/btrfs-rec/inspect_lsfiles.go | 5 ++--- cmd/btrfs-rec/inspect_mount.go | 2 +- cmd/btrfs-rec/inspect_spewitems.go | 2 +- cmd/btrfs-rec/main.go | 6 ++++++ 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/cmd/btrfs-rec/inspect/mount/mount.go b/cmd/btrfs-rec/inspect/mount/mount.go index 28363ed..214cc8d 100644 --- a/cmd/btrfs-rec/inspect/mount/mount.go +++ b/cmd/btrfs-rec/inspect/mount/mount.go @@ -29,23 +29,12 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree" - "git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil" "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/maps" "git.lukeshu.com/btrfs-progs-ng/lib/slices" ) -func MountRO(ctx context.Context, fs *btrfs.FS, mountpoint string, noChecksums bool) error { - pvs := fs.LV.PhysicalVolumes() - if len(pvs) < 1 { - return errors.New("no devices") - } - - deviceName := pvs[maps.SortedKeys(pvs)[0]].Name() - if abs, err := filepath.Abs(deviceName); err == nil { - deviceName = abs - } - +func MountRO(ctx context.Context, fs btrfs.ReadableFS, mountpoint string, noChecksums bool) error { sb, err := fs.Superblock() if err != nil { return err @@ -54,11 +43,11 @@ func MountRO(ctx context.Context, fs *btrfs.FS, mountpoint string, noChecksums b rootSubvol := &subvolume{ Subvolume: btrfs.NewSubvolume( ctx, - btrfsutil.NewOldRebuiltForrest(fs), + fs, btrfsprim.FS_TREE_OBJECTID, noChecksums, ), - DeviceName: deviceName, + DeviceName: fs.Name(), Mountpoint: mountpoint, sb: sb, diff --git a/cmd/btrfs-rec/inspect_dumptrees.go b/cmd/btrfs-rec/inspect_dumptrees.go index fd152d3..431a302 100644 --- a/cmd/btrfs-rec/inspect_dumptrees.go +++ b/cmd/btrfs-rec/inspect_dumptrees.go @@ -20,7 +20,7 @@ func init() { Use: "dump-trees", Short: "A clone of `btrfs inspect-internal dump-tree`", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { + RunE: runWithReadableFS(func(fs btrfs.ReadableFS, cmd *cobra.Command, _ []string) error { const version = "6.1.3" out := os.Stdout textui.Fprintf(out, "btrfs-progs v%v\n", version) diff --git a/cmd/btrfs-rec/inspect_lsfiles.go b/cmd/btrfs-rec/inspect_lsfiles.go index abc73bc..e66f09f 100644 --- a/cmd/btrfs-rec/inspect_lsfiles.go +++ b/cmd/btrfs-rec/inspect_lsfiles.go @@ -13,7 +13,6 @@ import ( "git.lukeshu.com/btrfs-progs-ng/cmd/btrfs-rec/inspect/lsfiles" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" - "git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil" ) func init() { @@ -21,7 +20,7 @@ func init() { Use: "ls-files", Short: "A listing of all files in the filesystem", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { + RunE: runWithReadableFS(func(fs btrfs.ReadableFS, cmd *cobra.Command, _ []string) (err error) { out := bufio.NewWriter(os.Stdout) defer func() { if _err := out.Flush(); _err != nil && err == nil { @@ -32,7 +31,7 @@ func init() { return lsfiles.LsFiles( cmd.Context(), out, - btrfsutil.NewOldRebuiltForrest(fs)) + fs) }), }) } diff --git a/cmd/btrfs-rec/inspect_mount.go b/cmd/btrfs-rec/inspect_mount.go index 4582f9f..54cfebf 100644 --- a/cmd/btrfs-rec/inspect_mount.go +++ b/cmd/btrfs-rec/inspect_mount.go @@ -18,7 +18,7 @@ func init() { Use: "mount MOUNTPOINT", Short: "Mount the filesystem read-only", Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), - RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + RunE: runWithReadableFS(func(fs btrfs.ReadableFS, cmd *cobra.Command, args []string) error { return mount.MountRO(cmd.Context(), fs, args[0], skipFileSums) }), } diff --git a/cmd/btrfs-rec/inspect_spewitems.go b/cmd/btrfs-rec/inspect_spewitems.go index 94d34d9..40a929f 100644 --- a/cmd/btrfs-rec/inspect_spewitems.go +++ b/cmd/btrfs-rec/inspect_spewitems.go @@ -24,7 +24,7 @@ func init() { Use: "spew-items", Short: "Spew all items as parsed", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { + RunE: runWithReadableFS(func(fs btrfs.ReadableFS, cmd *cobra.Command, _ []string) error { ctx := cmd.Context() spew := spew.NewDefaultConfig() diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index e433654..d221c3d 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -175,3 +175,9 @@ func runWithRawFS(runE func(*btrfs.FS, *cobra.Command, []string) error) func(*co return runE(fs, cmd, args) }) } + +func runWithReadableFS(runE func(btrfs.ReadableFS, *cobra.Command, []string) error) func(*cobra.Command, []string) error { + return runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + return runE(btrfsutil.NewOldRebuiltForrest(fs), cmd, args) + }) +} -- cgit v1.2.3-2-g168b