diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-30 19:08:25 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-30 19:08:25 -0600 |
commit | 5d0bd02aefed7e08f3afddf20066605149cb0b87 (patch) | |
tree | a1c8b09c01b761c4b294a701c091bb047a100397 | |
parent | 3d5e080385ed64ca5e0810263acc2d9970f14baa (diff) | |
parent | 8c2de0e63ac106b910173ed17f394b725103596f (diff) |
Merge branch 'lukeshu/tree-api-pt4-touchup'
-rw-r--r-- | cmd/btrfs-rec/inspect/mount/mount.go | 17 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_dumptrees.go | 2 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_lsfiles.go | 5 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_mount.go | 2 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_spewitems.go | 2 | ||||
-rw-r--r-- | cmd/btrfs-rec/main.go | 6 | ||||
-rw-r--r-- | lib/btrfs/io3_btree.go | 8 | ||||
-rw-r--r-- | lib/btrfsutil/old_rebuilt_forrest.go | 58 |
8 files changed, 54 insertions, 46 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) + }) +} 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 58333df..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,22 +390,7 @@ func (tree oldRebuiltTree) TreeWalk(ctx context.Context, cbs btrfstree.TreeWalkH } return ctx.Err() == nil }) - 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() + tree.forrest.ReleaseNode(node) } // TreeCheckOwner implements btrfstree.Tree. @@ -463,3 +448,30 @@ 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() +} + +// 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) +} |