summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-12 11:30:13 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-12 11:43:25 -0600
commit68555944f694e941b9cac3f8349364ec965db2fb (patch)
tree3b966ca9c633a1cf9740c8ca17904cf31121eda3
parenta0daaacdd61f196fbc0ca90ed996e7eeb4d4fcdd (diff)
Don't let TreeWalk bail early, add TreeID in to TreePath
-rw-r--r--cmd/btrfs-fsck/pass1.go14
-rw-r--r--cmd/btrfs-fsck/pass2.go4
-rw-r--r--cmd/btrfs-rec/inspect_dumptrees.go5
-rw-r--r--cmd/btrfs-rec/inspect_lstrees.go2
-rw-r--r--cmd/btrfs-rec/repair_clearbadnodes.go6
-rw-r--r--lib/btrfs/io2_lv.go37
-rw-r--r--lib/btrfs/io3_btree.go273
-rw-r--r--lib/btrfsprogs/btrfsinspect/print_tree.go579
-rw-r--r--lib/btrfsprogs/btrfsrepair/clearnodes.go53
-rw-r--r--lib/btrfsprogs/btrfsutil/open.go4
-rw-r--r--lib/btrfsprogs/btrfsutil/walk.go49
11 files changed, 501 insertions, 525 deletions
diff --git a/cmd/btrfs-fsck/pass1.go b/cmd/btrfs-fsck/pass1.go
index 0899cfc..c75e066 100644
--- a/cmd/btrfs-fsck/pass1.go
+++ b/cmd/btrfs-fsck/pass1.go
@@ -24,18 +24,12 @@ func pass1(ctx context.Context, fs *btrfs.FS, superblock *util.Ref[btrfsvol.Phys
visitedNodes := make(map[btrfsvol.LogicalAddr]struct{})
btrfsutil.WalkAllTrees(fs, btrfsutil.WalkAllTreesHandler{
TreeWalkHandler: btrfs.TreeWalkHandler{
- Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
- if err != nil {
- err = fmt.Errorf("%v: %w", path, err)
- fmt.Printf("Pass 1: ... walk fs: error: %v\n", err)
- }
- if node != nil {
- visitedNodes[node.Addr] = struct{}{}
- }
- return err
+ Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node]) error {
+ visitedNodes[node.Addr] = struct{}{}
+ return nil
},
},
- Err: func(err error) {
+ Err: func(err *btrfsutil.WalkError) {
fmt.Printf("Pass 1: ... walk fs: error: %v\n", err)
},
})
diff --git a/cmd/btrfs-fsck/pass2.go b/cmd/btrfs-fsck/pass2.go
index 9256220..78bd2e5 100644
--- a/cmd/btrfs-fsck/pass2.go
+++ b/cmd/btrfs-fsck/pass2.go
@@ -19,12 +19,12 @@ func pass2(fs *btrfs.FS, foundNodes map[btrfsvol.LogicalAddr]struct{}) {
visitedNodes := make(map[btrfsvol.LogicalAddr]struct{})
btrfsutil.WalkAllTrees(fs, btrfsutil.WalkAllTreesHandler{
TreeWalkHandler: btrfs.TreeWalkHandler{
- Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
+ Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node]) error {
visitedNodes[node.Addr] = struct{}{}
return nil
},
},
- Err: func(err error) {
+ Err: func(err *btrfsutil.WalkError) {
fmt.Printf("Pass 2: walk FS: error: %v\n", err)
},
})
diff --git a/cmd/btrfs-rec/inspect_dumptrees.go b/cmd/btrfs-rec/inspect_dumptrees.go
index 5ddff60..4abeaba 100644
--- a/cmd/btrfs-rec/inspect_dumptrees.go
+++ b/cmd/btrfs-rec/inspect_dumptrees.go
@@ -22,10 +22,11 @@ func init() {
Short: "A clone of `btrfs inspect-internal dump-tree`",
Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
},
- RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
+ RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error {
const version = "5.18.1"
fmt.Printf("btrfs-progs v%v\n", version)
- return btrfsinspect.DumpTrees(os.Stdout, os.Stderr, fs)
+ btrfsinspect.DumpTrees(cmd.Context(), os.Stdout, fs)
+ return nil
},
})
}
diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go
index 976514f..e308b20 100644
--- a/cmd/btrfs-rec/inspect_lstrees.go
+++ b/cmd/btrfs-rec/inspect_lstrees.go
@@ -35,7 +35,7 @@ func init() {
treeItemCnt = make(map[btrfsitem.Type]int)
fmt.Printf("tree id=%v name=%q\n", treeID, name)
},
- Err: func(_ error) {
+ Err: func(_ *btrfsutil.WalkError) {
treeErrCnt++
},
TreeWalkHandler: btrfs.TreeWalkHandler{
diff --git a/cmd/btrfs-rec/repair_clearbadnodes.go b/cmd/btrfs-rec/repair_clearbadnodes.go
index 58b8bd6..3ae0810 100644
--- a/cmd/btrfs-rec/repair_clearbadnodes.go
+++ b/cmd/btrfs-rec/repair_clearbadnodes.go
@@ -5,8 +5,6 @@
package main
import (
- "os"
-
"github.com/datawire/ocibuild/pkg/cliutil"
"github.com/spf13/cobra"
@@ -21,8 +19,8 @@ func init() {
Short: "Overwrite corrupt nodes with empty nodes",
Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
},
- RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
- return btrfsrepair.ClearBadNodes(os.Stdout, os.Stderr, fs)
+ RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error {
+ return btrfsrepair.ClearBadNodes(cmd.Context(), fs)
},
})
}
diff --git a/lib/btrfs/io2_lv.go b/lib/btrfs/io2_lv.go
index dce6e27..aac902b 100644
--- a/lib/btrfs/io2_lv.go
+++ b/lib/btrfs/io2_lv.go
@@ -5,10 +5,12 @@
package btrfs
import (
+ "context"
"fmt"
"io"
"github.com/datawire/dlib/derror"
+ "github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
@@ -26,7 +28,7 @@ type FS struct {
var _ util.File[btrfsvol.LogicalAddr] = (*FS)(nil)
-func (fs *FS) AddDevice(dev *Device) error {
+func (fs *FS) AddDevice(ctx context.Context, dev *Device) error {
sb, err := dev.Superblock()
if err != nil {
return err
@@ -37,7 +39,7 @@ func (fs *FS) AddDevice(dev *Device) error {
fs.cacheSuperblocks = nil
fs.cacheSuperblock = nil
if err := fs.initDev(sb); err != nil {
- return err
+ dlog.Errorf(ctx, "error: AddDevice: %q: %v", dev.Name(), err)
}
return nil
}
@@ -157,20 +159,27 @@ func (fs *FS) initDev(sb *util.Ref[btrfsvol.PhysicalAddr, Superblock]) error {
}
}
}
- if err := fs.TreeWalk(CHUNK_TREE_OBJECTID, TreeWalkHandler{
- Item: func(_ TreePath, item Item) error {
- if item.Head.Key.ItemType != btrfsitem.CHUNK_ITEM_KEY {
- return nil
- }
- for _, mapping := range item.Body.(btrfsitem.Chunk).Mappings(item.Head.Key) {
- if err := fs.LV.AddMapping(mapping); err != nil {
- return err
+ var errs derror.MultiError
+ fs.TreeWalk(CHUNK_TREE_OBJECTID,
+ func(err *TreeError) {
+ errs = append(errs, err)
+ },
+ TreeWalkHandler{
+ Item: func(_ TreePath, item Item) error {
+ if item.Head.Key.ItemType != btrfsitem.CHUNK_ITEM_KEY {
+ return nil
}
- }
- return nil
+ for _, mapping := range item.Body.(btrfsitem.Chunk).Mappings(item.Head.Key) {
+ if err := fs.LV.AddMapping(mapping); err != nil {
+ return err
+ }
+ }
+ return nil
+ },
},
- }); err != nil {
- return err
+ )
+ if len(errs) > 0 {
+ return errs
}
return nil
}
diff --git a/lib/btrfs/io3_btree.go b/lib/btrfs/io3_btree.go
index 7e0f8af..14419fe 100644
--- a/lib/btrfs/io3_btree.go
+++ b/lib/btrfs/io3_btree.go
@@ -5,7 +5,6 @@
package btrfs
import (
- "errors"
"fmt"
"io"
iofs "io/fs"
@@ -66,7 +65,10 @@ import (
// the path would be
//
// {-1, 0x01, 3}→{8, 0x02, 2}→{7, 0x03, 1}→{4, 0x04, 0}→{2, 0, 0}
-type TreePath []TreePathElem
+type TreePath struct {
+ TreeID ObjID
+ Nodes []TreePathElem
+}
// A TreePathElem essentially represents a KeyPointer.
type TreePathElem struct {
@@ -87,21 +89,46 @@ func (elem TreePathElem) writeNodeTo(w io.Writer) {
}
func (path TreePath) String() string {
- if len(path) == 0 {
- return "(empty-path)"
- }
var ret strings.Builder
- path[0].writeNodeTo(&ret)
- for _, elem := range path[1:] {
- fmt.Fprintf(&ret, "[%v]", elem.ItemIdx)
- if elem.NodeAddr != 0 {
- ret.WriteString("->")
- elem.writeNodeTo(&ret)
+ fmt.Fprintf(&ret, "%s->", path.TreeID.Format(btrfsitem.ROOT_ITEM_KEY))
+ if len(path.Nodes) == 0 {
+ ret.WriteString("(empty-path)")
+ } else {
+ path.Nodes[0].writeNodeTo(&ret)
+ for _, elem := range path.Nodes[1:] {
+ fmt.Fprintf(&ret, "[%v]", elem.ItemIdx)
+ if elem.NodeAddr != 0 {
+ ret.WriteString("->")
+ elem.writeNodeTo(&ret)
+ }
}
}
return ret.String()
}
+func (path TreePath) DeepCopy() TreePath {
+ return TreePath{
+ TreeID: path.TreeID,
+ Nodes: append([]TreePathElem(nil), path.Nodes...),
+ }
+}
+
+func (path TreePath) Append(elem TreePathElem) TreePath {
+ path.Nodes = append(path.Nodes, elem)
+ return path
+}
+
+type TreeError struct {
+ Path TreePath
+ Err error
+}
+
+func (e *TreeError) Unwrap() error { return e.Err }
+
+func (e *TreeError) Error() string {
+ return fmt.Sprintf("%v: %v", e.Path, e.Err)
+}
+
// A treeRoot is more-or-less a btrfsitem.Root, but simpler and generalized for
type treeRoot struct {
TreeID ObjID
@@ -174,7 +201,8 @@ func (fs *FS) lookupTree(treeID ObjID) (*treeRoot, error) {
type TreeWalkHandler struct {
// Callbacks for entire nodes
PreNode func(TreePath) error
- Node func(TreePath, *util.Ref[btrfsvol.LogicalAddr, Node], error) error
+ Node func(TreePath, *util.Ref[btrfsvol.LogicalAddr, Node]) error
+ BadNode func(TreePath, *util.Ref[btrfsvol.LogicalAddr, Node], error) error
PostNode func(TreePath, *util.Ref[btrfsvol.LogicalAddr, Node]) error
// Callbacks for items on internal nodes
PreKeyPointer func(TreePath, KeyPointer) error
@@ -187,7 +215,7 @@ type TreeWalkHandler struct {
//
// 001 .PreNode()
// 002 (read node)
-// 003 .Node()
+// 003 .Node() (or .BadNode())
// for item in node.items:
// if internal:
// 004 .PreKeyPointer()
@@ -196,116 +224,101 @@ type TreeWalkHandler struct {
// else:
// 004 .Item()
// 007 .PostNode()
-func (fs *FS) TreeWalk(treeID ObjID, cbs TreeWalkHandler) error {
- rootInfo, err := fs.lookupTree(treeID)
- if err != nil {
- return err
- }
+func (fs *FS) TreeWalk(treeID ObjID, errHandle func(*TreeError), cbs TreeWalkHandler) {
path := TreePath{
- TreePathElem{
- ItemIdx: -1,
- NodeAddr: rootInfo.RootNode,
- NodeLevel: rootInfo.Level,
- },
+ TreeID: treeID,
}
- return fs.treeWalk(path, cbs)
+ rootInfo, err := fs.lookupTree(treeID)
+ if err != nil {
+ errHandle(&TreeError{Path: path, Err: err})
+ return
+ }
+ path = path.Append(TreePathElem{
+ ItemIdx: -1,
+ NodeAddr: rootInfo.RootNode,
+ NodeLevel: rootInfo.Level,
+ })
+ fs.treeWalk(path, errHandle, cbs)
}
-func (fs *FS) treeWalk(path TreePath, cbs TreeWalkHandler) error {
- if path[len(path)-1].NodeAddr == 0 {
- return nil
+func (fs *FS) treeWalk(path TreePath, errHandle func(*TreeError), cbs TreeWalkHandler) {
+ if path.Nodes[len(path.Nodes)-1].NodeAddr == 0 {
+ return
}
if cbs.PreNode != nil {
if err := cbs.PreNode(path); err != nil {
- if errors.Is(err, iofs.SkipDir) {
- return nil
- }
- return err
+ errHandle(&TreeError{Path: path, Err: err})
}
}
- node, err := fs.readNodeAtLevel(path[len(path)-1].NodeAddr, path[len(path)-1].NodeLevel)
- if node != nil && err == nil {
- path[len(path)-1].NodeLevel = node.Data.Head.Level
- }
- if cbs.Node != nil {
- err = cbs.Node(path, node, err)
+ node, err := fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-1].NodeAddr, path.Nodes[len(path.Nodes)-1].NodeLevel)
+ if err != nil && node != nil && cbs.BadNode != nil {
+ // opportunity to fix the node
+ err = cbs.BadNode(path, node, err)
}
if err != nil {
- if errors.Is(err, iofs.SkipDir) {
- return nil
+ errHandle(&TreeError{Path: path, Err: err})
+ } else {
+ if err := cbs.Node(path, node); err != nil {
+ errHandle(&TreeError{Path: path, Err: err})
}
- return fmt.Errorf("btrfs.FS.TreeWalk: %w", err)
}
if node != nil {
for i, item := range node.Data.BodyInternal {
- itemPath := append(path, TreePathElem{
+ itemPath := path.Append(TreePathElem{
ItemIdx: i,
NodeAddr: item.BlockPtr,
NodeLevel: node.Data.Head.Level - 1,
})
if cbs.PreKeyPointer != nil {
if err := cbs.PreKeyPointer(itemPath, item); err != nil {
- if errors.Is(err, iofs.SkipDir) {
- continue
- }
- return err
+ errHandle(&TreeError{Path: path, Err: err})
}
}
- if err := fs.treeWalk(itemPath, cbs); err != nil {
- return err
- }
+ fs.treeWalk(itemPath, errHandle, cbs)
if cbs.PostKeyPointer != nil {
if err := cbs.PostKeyPointer(itemPath, item); err != nil {
- if errors.Is(err, iofs.SkipDir) {
- continue
- }
- return err
+ errHandle(&TreeError{Path: path, Err: err})
}
}
}
for i, item := range node.Data.BodyLeaf {
if cbs.Item != nil {
- itemPath := append(path, TreePathElem{
+ itemPath := path.Append(TreePathElem{
ItemIdx: i,
})
if err := cbs.Item(itemPath, item); err != nil {
- if errors.Is(err, iofs.SkipDir) {
- continue
- }
- return fmt.Errorf("btrfs.FS.TreeWalk: callback: %w", err)
+ errHandle(&TreeError{Path: path, Err: err})
}
}
}
}
if cbs.PostNode != nil {
if err := cbs.PostNode(path, node); err != nil {
- if errors.Is(err, iofs.SkipDir) {
- return nil
- }
- return err
+ errHandle(&TreeError{Path: path, Err: err})
}
}
- return nil
}
func (fs *FS) treeSearch(treeRoot treeRoot, fn func(Key) int) (TreePath, *util.Ref[btrfsvol.LogicalAddr, Node], error) {
path := TreePath{
- TreePathElem{
- ItemIdx: -1,
- NodeAddr: treeRoot.RootNode,
- NodeLevel: treeRoot.Level,
+ TreeID: treeRoot.TreeID,
+ Nodes: []TreePathElem{
+ {
+ ItemIdx: -1,
+ NodeAddr: treeRoot.RootNode,
+ NodeLevel: treeRoot.Level,
+ },
},
}
for {
- if path[len(path)-1].NodeAddr == 0 {
- return nil, nil, iofs.ErrNotExist
+ if path.Nodes[len(path.Nodes)-1].NodeAddr == 0 {
+ return TreePath{}, nil, iofs.ErrNotExist
}
- node, err := fs.readNodeAtLevel(path[len(path)-1].NodeAddr, path[len(path)-1].NodeLevel)
+ node, err := fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-1].NodeAddr, path.Nodes[len(path.Nodes)-1].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-1].NodeLevel = node.Data.Head.Level
if node.Data.Head.Level > 0 {
// internal node
@@ -330,9 +343,9 @@ func (fs *FS) treeSearch(treeRoot treeRoot, fn func(Key) int) (TreePath, *util.R
}
}
if lastGood < 0 {
- return nil, nil, iofs.ErrNotExist
+ return TreePath{}, nil, iofs.ErrNotExist
}
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: lastGood,
NodeAddr: node.Data.BodyInternal[lastGood].BlockPtr,
NodeLevel: node.Data.Head.Level - 1,
@@ -361,64 +374,64 @@ func (fs *FS) treeSearch(treeRoot treeRoot, fn func(Key) int) (TreePath, *util.R
case direction > 0:
beg = midpoint + 1
case direction == 0:
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: midpoint,
})
return path, node, nil
}
}
- return nil, nil, iofs.ErrNotExist
+ return TreePath{}, nil, iofs.ErrNotExist
}
}
}
func (fs *FS) prev(path TreePath, node *util.Ref[btrfsvol.LogicalAddr, Node]) (TreePath, *util.Ref[btrfsvol.LogicalAddr, Node], error) {
var err error
- path = append(TreePath(nil), path...)
+ path = path.DeepCopy()
// go up
- for path[len(path)-1].ItemIdx < 1 {
- path = path[:len(path)-1]
- if len(path) == 0 {
- return nil, nil, nil
+ for path.Nodes[len(path.Nodes)-1].ItemIdx < 1 {
+ path.Nodes = path.Nodes[:len(path.Nodes)-1]
+ if len(path.Nodes) == 0 {
+ return TreePath{}, nil, nil
}
}
// go left
- path[len(path)-1].ItemIdx--
- if path[len(path)-1].NodeAddr != 0 {
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ path.Nodes[len(path.Nodes)-1].ItemIdx--
+ if path.Nodes[len(path.Nodes)-1].NodeAddr != 0 {
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-1].NodeAddr = node.Data.BodyInternal[path[len(path)-1].ItemIdx].BlockPtr
+ path.Nodes[len(path.Nodes)-1].NodeAddr = node.Data.BodyInternal[path.Nodes[len(path.Nodes)-1].ItemIdx].BlockPtr
}
}
// go down
- for path[len(path)-1].NodeAddr != 0 {
- if node.Addr != path[len(path)-1].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-1].NodeAddr, path[len(path)-1].NodeLevel)
+ for path.Nodes[len(path.Nodes)-1].NodeAddr != 0 {
+ if node.Addr != path.Nodes[len(path.Nodes)-1].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-1].NodeAddr, path.Nodes[len(path.Nodes)-1].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
}
if node.Data.Head.Level > 0 {
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: len(node.Data.BodyInternal) - 1,
NodeAddr: node.Data.BodyInternal[len(node.Data.BodyInternal)-1].BlockPtr,
NodeLevel: node.Data.Head.Level - 1,
})
} else {
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: len(node.Data.BodyLeaf) - 1,
})
}
}
// return
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
}
return path, node, nil
@@ -426,66 +439,66 @@ func (fs *FS) prev(path TreePath, node *util.Ref[btrfsvol.LogicalAddr, Node]) (T
func (fs *FS) next(path TreePath, node *util.Ref[btrfsvol.LogicalAddr, Node]) (TreePath, *util.Ref[btrfsvol.LogicalAddr, Node], error) {
var err error
- path = append(TreePath(nil), path...)
+ path = path.DeepCopy()
// go up
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-2].NodeLevel = node.Data.Head.Level
+ path.Nodes[len(path.Nodes)-2].NodeLevel = node.Data.Head.Level
}
- for path[len(path)-1].ItemIdx+1 >= int(node.Data.Head.NumItems) {
- path = path[:len(path)-1]
- if len(path) == 1 {
- return nil, nil, nil
+ for path.Nodes[len(path.Nodes)-1].ItemIdx+1 >= int(node.Data.Head.NumItems) {
+ path.Nodes = path.Nodes[:len(path.Nodes)-1]
+ if len(path.Nodes) == 1 {
+ return TreePath{}, nil, nil
}
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-2].NodeLevel = node.Data.Head.Level
+ path.Nodes[len(path.Nodes)-2].NodeLevel = node.Data.Head.Level
}
}
// go left
- path[len(path)-1].ItemIdx++
- if path[len(path)-1].NodeAddr != 0 {
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ path.Nodes[len(path.Nodes)-1].ItemIdx++
+ if path.Nodes[len(path.Nodes)-1].NodeAddr != 0 {
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-1].NodeAddr = node.Data.BodyInternal[path[len(path)-1].ItemIdx].BlockPtr
+ path.Nodes[len(path.Nodes)-1].NodeAddr = node.Data.BodyInternal[path.Nodes[len(path.Nodes)-1].ItemIdx].BlockPtr
}
}
// go down
- for path[len(path)-1].NodeAddr != 0 {
- if node.Addr != path[len(path)-1].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-1].NodeAddr, path[len(path)-1].NodeLevel)
+ for path.Nodes[len(path.Nodes)-1].NodeAddr != 0 {
+ if node.Addr != path.Nodes[len(path.Nodes)-1].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-1].NodeAddr, path.Nodes[len(path.Nodes)-1].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
- path[len(path)-1].NodeLevel = node.Data.Head.Level
+ path.Nodes[len(path.Nodes)-1].NodeLevel = node.Data.Head.Level
}
if node.Data.Head.Level > 0 {
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: 0,
NodeAddr: node.Data.BodyInternal[len(node.Data.BodyInternal)-1].BlockPtr,
NodeLevel: node.Data.Head.Level - 1,
})
} else {
- path = append(path, TreePathElem{
+ path = path.Append(TreePathElem{
ItemIdx: 0,
})
}
}
// return
- if node.Addr != path[len(path)-2].NodeAddr {
- node, err = fs.readNodeAtLevel(path[len(path)-2].NodeAddr, path[len(path)-2].NodeLevel)
+ if node.Addr != path.Nodes[len(path.Nodes)-2].NodeAddr {
+ node, err = fs.readNodeAtLevel(path.Nodes[len(path.Nodes)-2].NodeAddr, path.Nodes[len(path.Nodes)-2].NodeLevel)
if err != nil {
- return nil, nil, err
+ return TreePath{}, nil, err
}
}
return path, node, nil
@@ -500,7 +513,7 @@ func (fs *FS) TreeSearch(treeID ObjID, fn func(Key) int) (Item, error) {
if err != nil {
return Item{}, err
}
- return node.Data.BodyLeaf[path[len(path)-1].ItemIdx], nil
+ return node.Data.BodyLeaf[path.Nodes[len(path.Nodes)-1].ItemIdx], nil
}
func (fs *FS) TreeLookup(treeID ObjID, key Key) (Item, error) {
@@ -524,7 +537,7 @@ func (fs *FS) TreeSearchAll(treeID ObjID, fn func(Key) int) ([]Item, error) {
if err != nil {
return nil, err
}
- middleItem := middleNode.Data.BodyLeaf[middlePath[len(middlePath)-1].ItemIdx]
+ middleItem := middleNode.Data.BodyLeaf[middlePath.Nodes[len(middlePath.Nodes)-1].ItemIdx]
var ret = []Item{middleItem}
var errs derror.MultiError
@@ -534,10 +547,10 @@ func (fs *FS) TreeSearchAll(treeID ObjID, fn func(Key) int) ([]Item, error) {
errs = append(errs, err)
break
}
- if prevPath == nil {
+ if len(prevPath.Nodes) == 0 {
break
}
- prevItem := prevNode.Data.BodyLeaf[prevPath[len(prevPath)-1].ItemIdx]
+ prevItem := prevNode.Data.BodyLeaf[prevPath.Nodes[len(prevPath.Nodes)-1].ItemIdx]
if fn(prevItem.Head.Key) != 0 {
break
}
@@ -550,10 +563,10 @@ func (fs *FS) TreeSearchAll(treeID ObjID, fn func(Key) int) ([]Item, error) {
errs = append(errs, err)
break
}
- if nextPath == nil {
+ if len(nextPath.Nodes) == 0 {
break
}
- nextItem := nextNode.Data.BodyLeaf[nextPath[len(nextPath)-1].ItemIdx]
+ nextItem := nextNode.Data.BodyLeaf[nextPath.Nodes[len(nextPath.Nodes)-1].ItemIdx]
if fn(nextItem.Head.Key) != 0 {
break
}
diff --git a/lib/btrfsprogs/btrfsinspect/print_tree.go b/lib/btrfsprogs/btrfsinspect/print_tree.go
index cc57ae6..2bb6762 100644
--- a/lib/btrfsprogs/btrfsinspect/print_tree.go
+++ b/lib/btrfsprogs/btrfsinspect/print_tree.go
@@ -5,10 +5,13 @@
package btrfsinspect
import (
+ "context"
"fmt"
"io"
"strings"
+ "github.com/datawire/dlib/dlog"
+
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
@@ -16,330 +19,328 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/util"
)
-func DumpTrees(out, errout io.Writer, fs *btrfs.FS) error {
+func DumpTrees(ctx context.Context, out io.Writer, fs *btrfs.FS) {
superblock, err := fs.Superblock()
if err != nil {
- return err
+ dlog.Error(ctx, err)
+ return
}
if superblock.Data.RootTree != 0 {
fmt.Fprintf(out, "root tree\n")
- if err := printTree(out, errout, fs, btrfs.ROOT_TREE_OBJECTID); err != nil {
- return err
- }
+ printTree(ctx, out, fs, btrfs.ROOT_TREE_OBJECTID)
}
if superblock.Data.ChunkTree != 0 {
fmt.Fprintf(out, "chunk tree\n")
- if err := printTree(out, errout, fs, btrfs.CHUNK_TREE_OBJECTID); err != nil {
- return err
- }
+ printTree(ctx, out, fs, btrfs.CHUNK_TREE_OBJECTID)
}
if superblock.Data.LogTree != 0 {
fmt.Fprintf(out, "log root tree\n")
- if err := printTree(out, errout, fs, btrfs.TREE_LOG_OBJECTID); err != nil {
- return err
- }
+ printTree(ctx, out, fs, btrfs.TREE_LOG_OBJECTID)
}
if superblock.Data.BlockGroupRoot != 0 {
fmt.Fprintf(out, "block group tree\n")
- if err := printTree(out, errout, fs, btrfs.BLOCK_GROUP_TREE_OBJECTID); err != nil {
- return err
- }
+ printTree(ctx, out, fs, btrfs.BLOCK_GROUP_TREE_OBJECTID)
}
- if err := fs.TreeWalk(btrfs.ROOT_TREE_OBJECTID, btrfs.TreeWalkHandler{
- Item: func(_ btrfs.TreePath, item btrfs.Item) error {
- if item.Head.Key.ItemType != btrfsitem.ROOT_ITEM_KEY {
+ fs.TreeWalk(
+ btrfs.ROOT_TREE_OBJECTID,
+ func(err *btrfs.TreeError) {
+ dlog.Error(ctx, err)
+ },
+ btrfs.TreeWalkHandler{
+ Item: func(_ btrfs.TreePath, item btrfs.Item) error {
+ if item.Head.Key.ItemType != btrfsitem.ROOT_ITEM_KEY {
+ return nil
+ }
+ treeName, ok := map[btrfs.ObjID]string{
+ btrfs.ROOT_TREE_OBJECTID: "root",
+ btrfs.EXTENT_TREE_OBJECTID: "extent",
+ btrfs.CHUNK_TREE_OBJECTID: "chunk",
+ btrfs.DEV_TREE_OBJECTID: "device",
+ btrfs.FS_TREE_OBJECTID: "fs",
+ btrfs.ROOT_TREE_DIR_OBJECTID: "directory",
+ btrfs.CSUM_TREE_OBJECTID: "checksum",
+ btrfs.ORPHAN_OBJECTID: "orphan",
+ btrfs.TREE_LOG_OBJECTID: "log",
+ btrfs.TREE_LOG_FIXUP_OBJECTID: "log fixup",
+ btrfs.TREE_RELOC_OBJECTID: "reloc",
+ btrfs.DATA_RELOC_TREE_OBJECTID: "data reloc",
+ btrfs.EXTENT_CSUM_OBJECTID: "extent checksum",
+ btrfs.QUOTA_TREE_OBJECTID: "quota",
+ btrfs.UUID_TREE_OBJECTID: "uuid",
+ btrfs.FREE_SPACE_TREE_OBJECTID: "free space",
+ btrfs.MULTIPLE_OBJECTIDS: "multiple",
+ btrfs.BLOCK_GROUP_TREE_OBJECTID: "block group",
+ }[item.Head.Key.ObjectID]
+ if !ok {
+ treeName = "file"
+ }
+ fmt.Fprintf(out, "%v tree %v \n", treeName, fmtKey(item.Head.Key))
+ printTree(ctx, out, fs, item.Head.Key.ObjectID)
return nil
- }
- treeName, ok := map[btrfs.ObjID]string{
- btrfs.ROOT_TREE_OBJECTID: "root",
- btrfs.EXTENT_TREE_OBJECTID: "extent",
- btrfs.CHUNK_TREE_OBJECTID: "chunk",
- btrfs.DEV_TREE_OBJECTID: "device",
- btrfs.FS_TREE_OBJECTID: "fs",
- btrfs.ROOT_TREE_DIR_OBJECTID: "directory",
- btrfs.CSUM_TREE_OBJECTID: "checksum",
- btrfs.ORPHAN_OBJECTID: "orphan",
- btrfs.TREE_LOG_OBJECTID: "log",
- btrfs.TREE_LOG_FIXUP_OBJECTID: "log fixup",
- btrfs.TREE_RELOC_OBJECTID: "reloc",
- btrfs.DATA_RELOC_TREE_OBJECTID: "data reloc",
- btrfs.EXTENT_CSUM_OBJECTID: "extent checksum",
- btrfs.QUOTA_TREE_OBJECTID: "quota",
- btrfs.UUID_TREE_OBJECTID: "uuid",
- btrfs.FREE_SPACE_TREE_OBJECTID: "free space",
- btrfs.MULTIPLE_OBJECTIDS: "multiple",
- btrfs.BLOCK_GROUP_TREE_OBJECTID: "block group",
- }[item.Head.Key.ObjectID]
- if !ok {
- treeName = "file"
- }
- fmt.Fprintf(out, "%v tree %v \n", treeName, fmtKey(item.Head.Key))
- return printTree(out, errout, fs, item.Head.Key.ObjectID)
+ },
},
- }); err != nil {
- return err
- }
+ )
fmt.Fprintf(out, "total bytes %v\n", superblock.Data.TotalBytes)
fmt.Fprintf(out, "bytes used %v\n", superblock.Data.BytesUsed)
fmt.Fprintf(out, "uuid %v\n", superblock.Data.FSUUID)
- return nil
}
// printTree mimics btrfs-progs
// kernel-shared/print-tree.c:btrfs_print_tree() and
// kernel-shared/print-tree.c:btrfs_print_leaf()
-func printTree(out, errout io.Writer, fs *btrfs.FS, treeID btrfs.ObjID) error {
- return fs.TreeWalk(treeID, btrfs.TreeWalkHandler{
- Node: func(path btrfs.TreePath, nodeRef *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
- if err != nil {
- fmt.Fprintf(errout, "error: %v: %v\n", path, err)
- }
- if nodeRef != nil {
- printHeaderInfo(out, nodeRef.Data)
- }
- return nil
- },
- PreKeyPointer: func(_ btrfs.TreePath, item btrfs.KeyPointer) error {
- fmt.Fprintf(out, "\t%v block %v gen %v\n",
- fmtKey(item.Key),
- item.BlockPtr,
- item.Generation)
- return nil
+func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfs.ObjID) {
+ fs.TreeWalk(
+ treeID,
+ func(err *btrfs.TreeError) {
+ dlog.Error(ctx, err)
},
- Item: func(path btrfs.TreePath, item btrfs.Item) error {
- i := path[len(path)-1].ItemIdx
- fmt.Fprintf(out, "\titem %v %v itemoff %v itemsize %v\n",
- i,
- fmtKey(item.Head.Key),
- item.Head.DataOffset,
- item.Head.DataSize)
- switch body := item.Body.(type) {
- case btrfsitem.FreeSpaceHeader:
- fmt.Fprintf(out, "\t\tlocation %v\n", fmtKey(body.Location))
- fmt.Fprintf(out, "\t\tcache generation %v entries %v bitmaps %v\n",
- body.Generation, body.NumEntries, body.NumBitmaps)
- case btrfsitem.Inode:
- fmt.Fprintf(out, ""+
- "\t\tgeneration %v transid %v size %v nbytes %v\n"+
- "\t\tblock group %v mode %o links %v uid %v gid %v rdev %v\n"+
- "\t\tsequence %v flags %v\n",
- body.Generation, body.TransID, body.Size, body.NumBytes,
- body.BlockGroup, body.Mode, body.NLink, body.UID, body.GID, body.RDev,
- body.Sequence, body.Flags)
- fmt.Fprintf(out, "\t\tatime %v\n", fmtTime(body.ATime))
- fmt.Fprintf(out, "\t\tctime %v\n", fmtTime(body.CTime))
- fmt.Fprintf(out, "\t\tmtime %v\n", fmtTime(body.MTime))
- fmt.Fprintf(out, "\t\totime %v\n", fmtTime(body.OTime))
- case btrfsitem.InodeRef:
- fmt.Fprintf(out, "\t\tindex %v namelen %v name: %s\n",
- body.Index, body.NameLen, body.Name)
- //case btrfsitem.INODE_EXTREF_KEY:
- // // TODO
- case btrfsitem.DirEntry:
- fmt.Fprintf(out, "\t\tlocation %v type %v\n",
- fmtKey(body.Location), body.Type)
- fmt.Fprintf(out, "\t\ttransid %v data_len %v name_len %v\n",
- body.TransID, body.DataLen, body.NameLen)
- fmt.Fprintf(out, "\t\tname: %s\n", body.Name)
- if len(body.Data) > 0 {
- fmt.Fprintf(out, "\t\tdata %v\n", body.Data)
- }
- //case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY:
- // // TODO
- case btrfsitem.Root:
- fmt.Fprintf(out, "\t\tgeneration %v root_dirid %v bytenr %d byte_limit %v bytes_used %v\n",
- body.Generation, body.RootDirID, body.ByteNr, body.ByteLimit, body.BytesUsed)
- fmt.Fprintf(out, "\t\tlast_snapshot %v flags %v refs %v\n",
- body.LastSnapshot, body.Flags, body.Refs)
- fmt.Fprintf(out, "\t\tdrop_progress %v drop_level %v\n",
- fmtKey(body.DropProgress), body.DropLevel)
- fmt.Fprintf(out, "\t\tlevel %v generation_v2 %v\n",
- body.Level, body.GenerationV2)
- if body.Generation == body.GenerationV2 {
- fmt.Fprintf(out, "\t\tuuid %v\n", body.UUID)
- fmt.Fprintf(out, "\t\tparent_uuid %v\n", body.ParentUUID)
- fmt.Fprintf(out, "\t\treceived_uuid %v\n", body.ReceivedUUID)
- fmt.Fprintf(out, "\t\tctransid %v otransid %v stransid %v rtransid %v\n",
- body.CTransID, body.OTransID, body.STransID, body.RTransID)
+ btrfs.TreeWalkHandler{
+ Node: func(path btrfs.TreePath, nodeRef *util.Ref[btrfsvol.LogicalAddr, btrfs.Node]) error {
+ printHeaderInfo(out, nodeRef.Data)
+ return nil
+ },
+ PreKeyPointer: func(_ btrfs.TreePath, item btrfs.KeyPointer) error {
+ fmt.Fprintf(out, "\t%v block %v gen %v\n",
+ fmtKey(item.Key),
+ item.BlockPtr,
+ item.Generation)
+ return nil
+ },
+ Item: func(path btrfs.TreePath, item btrfs.Item) error {
+ i := path.Nodes[len(path.Nodes)-1].ItemIdx
+ fmt.Fprintf(out, "\titem %v %v itemoff %v itemsize %v\n",
+ i,
+ fmtKey(item.Head.Key),
+ item.Head.DataOffset,
+ item.Head.DataSize)
+ switch body := item.Body.(type) {
+ case btrfsitem.FreeSpaceHeader:
+ fmt.Fprintf(out, "\t\tlocation %v\n", fmtKey(body.Location))
+ fmt.Fprintf(out, "\t\tcache generation %v entries %v bitmaps %v\n",
+ body.Generation, body.NumEntries, body.NumBitmaps)
+ case btrfsitem.Inode:
+ fmt.Fprintf(out, ""+
+ "\t\tgeneration %v transid %v size %v nbytes %v\n"+
+ "\t\tblock group %v mode %o links %v uid %v gid %v rdev %v\n"+
+ "\t\tsequence %v flags %v\n",
+ body.Generation, body.TransID, body.Size, body.NumBytes,
+ body.BlockGroup, body.Mode, body.NLink, body.UID, body.GID, body.RDev,
+ body.Sequence, body.Flags)
+ fmt.Fprintf(out, "\t\tatime %v\n", fmtTime(body.ATime))
fmt.Fprintf(out, "\t\tctime %v\n", fmtTime(body.CTime))
+ fmt.Fprintf(out, "\t\tmtime %v\n", fmtTime(body.MTime))
fmt.Fprintf(out, "\t\totime %v\n", fmtTime(body.OTime))
- fmt.Fprintf(out, "\t\tstime %v\n", fmtTime(body.STime))
- fmt.Fprintf(out, "\t\trtime %v\n", fmtTime(body.RTime))
- }
- case btrfsitem.RootRef:
- var tag string
- switch item.Head.Key.ItemType {
- case btrfsitem.ROOT_REF_KEY:
- tag = "ref"
- case btrfsitem.ROOT_BACKREF_KEY:
- tag = "backref"
- default:
- tag = fmt.Sprintf("(error: unhandled RootRef item type: %v)", item.Head.Key.ItemType)
- }
- fmt.Fprintf(out, "\t\troot %v key dirid %v sequence %v name %s\n",
- tag, body.DirID, body.Sequence, body.Name)
- case btrfsitem.Extent:
- fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n",
- body.Head.Refs, body.Head.Generation, body.Head.Flags)
- if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) {
- fmt.Fprintf(out, "\t\ttree block %v level %v\n",
- fmtKey(body.Info.Key), body.Info.Level)
- }
- printExtentInlineRefs(out, body.Refs)
- case btrfsitem.Metadata:
- fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n",
- body.Head.Refs, body.Head.Generation, body.Head.Flags)
- fmt.Fprintf(out, "\t\ttree block skinny level %v\n", item.Head.Key.Offset)
- printExtentInlineRefs(out, body.Refs)
- //case btrfsitem.EXTENT_DATA_REF_KEY:
- // // TODO
- //case btrfsitem.SHARED_DATA_REF_KEY:
- // // TODO
- case btrfsitem.ExtentCSum:
- sb, _ := fs.Superblock()
- sectorSize := btrfsvol.AddrDelta(sb.Data.SectorSize)
+ case btrfsitem.InodeRef:
+ fmt.Fprintf(out, "\t\tindex %v namelen %v name: %s\n",
+ body.Index, body.NameLen, body.Name)
+ //case btrfsitem.INODE_EXTREF_KEY:
+ // // TODO
+ case btrfsitem.DirEntry:
+ fmt.Fprintf(out, "\t\tlocation %v type %v\n",
+ fmtKey(body.Location), body.Type)
+ fmt.Fprintf(out, "\t\ttransid %v data_len %v name_len %v\n",
+ body.TransID, body.DataLen, body.NameLen)
+ fmt.Fprintf(out, "\t\tname: %s\n", body.Name)
+ if len(body.Data) > 0 {
+ fmt.Fprintf(out, "\t\tdata %v\n", body.Data)
+ }
+ //case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY:
+ // // TODO
+ case btrfsitem.Root:
+ fmt.Fprintf(out, "\t\tgeneration %v root_dirid %v bytenr %d byte_limit %v bytes_used %v\n",
+ body.Generation, body.RootDirID, body.ByteNr, body.ByteLimit, body.BytesUsed)
+ fmt.Fprintf(out, "\t\tlast_snapshot %v flags %v refs %v\n",
+ body.LastSnapshot, body.Flags, body.Refs)
+ fmt.Fprintf(out, "\t\tdrop_progress %v drop_level %v\n",
+ fmtKey(body.DropProgress), body.DropLevel)
+ fmt.Fprintf(out, "\t\tlevel %v generation_v2 %v\n",
+ body.Level, body.GenerationV2)
+ if body.Generation == body.GenerationV2 {
+ fmt.Fprintf(out, "\t\tuuid %v\n", body.UUID)
+ fmt.Fprintf(out, "\t\tparent_uuid %v\n", body.ParentUUID)
+ fmt.Fprintf(out, "\t\treceived_uuid %v\n", body.ReceivedUUID)
+ fmt.Fprintf(out, "\t\tctransid %v otransid %v stransid %v rtransid %v\n",
+ body.CTransID, body.OTransID, body.STransID, body.RTransID)
+ fmt.Fprintf(out, "\t\tctime %v\n", fmtTime(body.CTime))
+ fmt.Fprintf(out, "\t\totime %v\n", fmtTime(body.OTime))
+ fmt.Fprintf(out, "\t\tstime %v\n", fmtTime(body.STime))
+ fmt.Fprintf(out, "\t\trtime %v\n", fmtTime(body.RTime))
+ }
+ case btrfsitem.RootRef:
+ var tag string
+ switch item.Head.Key.ItemType {
+ case btrfsitem.ROOT_REF_KEY:
+ tag = "ref"
+ case btrfsitem.ROOT_BACKREF_KEY:
+ tag = "backref"
+ default:
+ tag = fmt.Sprintf("(error: unhandled RootRef item type: %v)", item.Head.Key.ItemType)
+ }
+ fmt.Fprintf(out, "\t\troot %v key dirid %v sequence %v name %s\n",
+ tag, body.DirID, body.Sequence, body.Name)
+ case btrfsitem.Extent:
+ fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n",
+ body.Head.Refs, body.Head.Generation, body.Head.Flags)
+ if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) {
+ fmt.Fprintf(out, "\t\ttree block %v level %v\n",
+ fmtKey(body.Info.Key), body.Info.Level)
+ }
+ printExtentInlineRefs(out, body.Refs)
+ case btrfsitem.Metadata:
+ fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n",
+ body.Head.Refs, body.Head.Generation, body.Head.Flags)
+ fmt.Fprintf(out, "\t\ttree block skinny level %v\n", item.Head.Key.Offset)
+ printExtentInlineRefs(out, body.Refs)
+ //case btrfsitem.EXTENT_DATA_REF_KEY:
+ // // TODO
+ //case btrfsitem.SHARED_DATA_REF_KEY:
+ // // TODO
+ case btrfsitem.ExtentCSum:
+ sb, _ := fs.Superblock()
+ sectorSize := btrfsvol.AddrDelta(sb.Data.SectorSize)
- start := btrfsvol.LogicalAddr(item.Head.Key.Offset)
- itemSize := btrfsvol.AddrDelta(len(body.Sums)) * sectorSize
- fmt.Fprintf(out, "\t\trange start %d end %d length %d",
- start, start.Add(itemSize), itemSize)
- sumsPerLine := util.Max(1, len(btrfssum.CSum{})/body.ChecksumSize/2)
+ start := btrfsvol.LogicalAddr(item.Head.Key.Offset)
+ itemSize := btrfsvol.AddrDelta(len(body.Sums)) * sectorSize
+ fmt.Fprintf(out, "\t\trange start %d end %d length %d",
+ start, start.Add(itemSize), itemSize)
+ sumsPerLine := util.Max(1, len(btrfssum.CSum{})/body.ChecksumSize/2)
- pos := start
- for i, sum := range body.Sums {
- if i%sumsPerLine == 0 {
- fmt.Fprintf(out, "\n\t\t")
- } else {
- fmt.Fprintf(out, " ")
+ pos := start
+ for i, sum := range body.Sums {
+ if i%sumsPerLine == 0 {
+ fmt.Fprintf(out, "\n\t\t")
+ } else {
+ fmt.Fprintf(out, " ")
+ }
+ fmt.Fprintf(out, "[%d] 0x%s", pos, sum.Fmt(sb.Data.ChecksumType))
+ pos = pos.Add(sectorSize)
}
- fmt.Fprintf(out, "[%d] 0x%s", pos, sum.Fmt(sb.Data.ChecksumType))
- pos = pos.Add(sectorSize)
- }
- fmt.Fprintf(out, "\n")
- case btrfsitem.FileExtent:
- fmt.Fprintf(out, "\t\tgeneration %v type %v\n",
- body.Generation, body.Type)
- switch body.Type {
- case btrfsitem.FILE_EXTENT_INLINE:
- fmt.Fprintf(out, "\t\tinline extent data size %v ram_bytes %v compression %v\n",
- len(body.BodyInline), body.RAMBytes, body.Compression)
- case btrfsitem.FILE_EXTENT_PREALLOC:
- fmt.Fprintf(out, "\t\tprealloc data disk byte %v nr %v\n",
- body.BodyExtent.DiskByteNr,
- body.BodyExtent.DiskNumBytes)
- fmt.Fprintf(out, "\t\tprealloc data offset %v nr %v\n",
- body.BodyExtent.Offset,
- body.BodyExtent.NumBytes)
- case btrfsitem.FILE_EXTENT_REG:
- fmt.Fprintf(out, "\t\textent data disk byte %d nr %d\n",
- body.BodyExtent.DiskByteNr,
- body.BodyExtent.DiskNumBytes)
- fmt.Fprintf(out, "\t\textent data offset %d nr %d ram %v\n",
- body.BodyExtent.Offset,
- body.BodyExtent.NumBytes,
- body.RAMBytes)
- fmt.Fprintf(out, "\t\textent compression %v\n",
- body.Compression)
- default:
- fmt.Fprintf(out, "\t\t(error) unknown file extent type %v", body.Type)
- }
- case btrfsitem.BlockGroup:
- fmt.Fprintf(out, "\t\tblock group used %v chunk_objectid %v flags %v\n",
- body.Used, body.ChunkObjectID, body.Flags)
- case btrfsitem.FreeSpaceInfo:
- fmt.Fprintf(out, "\t\tfree space info extent count %v flags %v\n",
- body.ExtentCount, body.Flags)
- case btrfsitem.FreeSpaceBitmap:
- fmt.Fprintf(out, "\t\tfree space bitmap\n")
- case btrfsitem.Chunk:
- fmt.Fprintf(out, "\t\tlength %d owner %d stripe_len %v type %v\n",
- body.Head.Size, body.Head.Owner, body.Head.StripeLen, body.Head.Type)
- fmt.Fprintf(out, "\t\tio_align %v io_width %v sector_size %v\n",
- body.Head.IOOptimalAlign, body.Head.IOOptimalWidth, body.Head.IOMinSize)
- fmt.Fprintf(out, "\t\tnum_stripes %v sub_stripes %v\n",
- body.Head.NumStripes, body.Head.SubStripes)
- for i, stripe := range body.Stripes {
- fmt.Fprintf(out, "\t\t\tstripe %v devid %d offset %d\n",
- i, stripe.DeviceID, stripe.Offset)
- fmt.Fprintf(out, "\t\t\tdev_uuid %v\n",
- stripe.DeviceUUID)
- }
- case btrfsitem.Dev:
- fmt.Fprintf(out, ""+
- "\t\tdevid %d total_bytes %v bytes_used %v\n"+
- "\t\tio_align %v io_width %v sector_size %v type %v\n"+
- "\t\tgeneration %v start_offset %v dev_group %v\n"+
- "\t\tseek_speed %v bandwidth %v\n"+
- "\t\tuuid %v\n"+
- "\t\tfsid %v\n",
- body.DevID, body.NumBytes, body.NumBytesUsed,
- body.IOOptimalAlign, body.IOOptimalWidth, body.IOMinSize, body.Type,
- body.Generation, body.StartOffset, body.DevGroup,
- body.SeekSpeed, body.Bandwidth,
- body.DevUUID,
- body.FSUUID)
- case btrfsitem.DevExtent:
- fmt.Fprintf(out, ""+
- "\t\tdev extent chunk_tree %v\n"+
- "\t\tchunk_objectid %v chunk_offset %d length %d\n"+
- "\t\tchunk_tree_uuid %v\n",
- body.ChunkTree, body.ChunkObjectID, body.ChunkOffset, body.Length,
- body.ChunkTreeUUID)
- //case btrfsitem.QGROUP_STATUS_KEY:
- // // TODO
- //case btrfsitem.QGROUP_INFO_KEY:
- // // TODO
- //case btrfsitem.QGROUP_LIMIT_KEY:
- // // TODO
- case btrfsitem.UUIDMap:
- fmt.Fprintf(out, "\t\tsubvol_id %d\n", body.ObjID)
- //case btrfsitem.STRING_ITEM_KEY:
- // // TODO
- case btrfsitem.DevStats:
- fmt.Fprintf(out, "\t\tpersistent item objectid %v offset %v\n",
- item.Head.Key.ObjectID.Format(item.Head.Key.ItemType), item.Head.Key.Offset)
- switch item.Head.Key.ObjectID {
- case btrfs.DEV_STATS_OBJECTID:
- fmt.Fprintf(out, "\t\tdevice stats\n")
- fmt.Fprintf(out, "\t\twrite_errs %v read_errs %v flush_errs %v corruption_errs %v generation %v\n",
- body.Values[btrfsitem.DEV_STAT_WRITE_ERRS],
- body.Values[btrfsitem.DEV_STAT_READ_ERRS],
- body.Values[btrfsitem.DEV_STAT_FLUSH_ERRS],
- body.Values[btrfsitem.DEV_STAT_CORRUPTION_ERRS],
- body.Values[btrfsitem.DEV_STAT_GENERATION_ERRS])
- default:
- fmt.Fprintf(out, "\t\tunknown persistent item objectid %v\n", item.Head.Key.ObjectID)
- }
- //case btrfsitem.TEMPORARY_ITEM_KEY:
- // // TODO
- case btrfsitem.Empty:
- switch item.Head.Key.ItemType {
- case btrfsitem.ORPHAN_ITEM_KEY: // 48
- fmt.Fprintf(out, "\t\torphan item\n")
- case btrfsitem.TREE_BLOCK_REF_KEY: // 176
- fmt.Fprintf(out, "\t\ttree block backref\n")
- case btrfsitem.SHARED_BLOCK_REF_KEY: // 182
- fmt.Fprintf(out, "\t\tshared block backref\n")
- case btrfsitem.FREE_SPACE_EXTENT_KEY: // 199
- fmt.Fprintf(out, "\t\tfree space extent\n")
- case btrfsitem.QGROUP_RELATION_KEY: // 246
- // do nothing
- //case btrfsitem.EXTENT_REF_V0_KEY:
- // fmt.Fprintf(out, "\t\textent ref v0 (deprecated)\n")
- //case btrfsitem.CSUM_ITEM_KEY:
- // fmt.Fprintf(out, "\t\tcsum item\n")
+ fmt.Fprintf(out, "\n")
+ case btrfsitem.FileExtent:
+ fmt.Fprintf(out, "\t\tgeneration %v type %v\n",
+ body.Generation, body.Type)
+ switch body.Type {
+ case btrfsitem.FILE_EXTENT_INLINE:
+ fmt.Fprintf(out, "\t\tinline extent data size %v ram_bytes %v compression %v\n",
+ len(body.BodyInline), body.RAMBytes, body.Compression)
+ case btrfsitem.FILE_EXTENT_PREALLOC:
+ fmt.Fprintf(out, "\t\tprealloc data disk byte %v nr %v\n",
+ body.BodyExtent.DiskByteNr,
+ body.BodyExtent.DiskNumBytes)
+ fmt.Fprintf(out, "\t\tprealloc data offset %v nr %v\n",
+ body.BodyExtent.Offset,
+ body.BodyExtent.NumBytes)
+ case btrfsitem.FILE_EXTENT_REG:
+ fmt.Fprintf(out, "\t\textent data disk byte %d nr %d\n",
+ body.BodyExtent.DiskByteNr,
+ body.BodyExtent.DiskNumBytes)
+ fmt.Fprintf(out, "\t\textent data offset %d nr %d ram %v\n",
+ body.BodyExtent.Offset,
+ body.BodyExtent.NumBytes,
+ body.RAMBytes)
+ fmt.Fprintf(out, "\t\textent compression %v\n",
+ body.Compression)
+ default:
+ fmt.Fprintf(out, "\t\t(error) unknown file extent type %v", body.Type)
+ }
+ case btrfsitem.BlockGroup:
+ fmt.Fprintf(out, "\t\tblock group used %v chunk_objectid %v flags %v\n",
+ body.Used, body.ChunkObjectID, body.Flags)
+ case btrfsitem.FreeSpaceInfo:
+ fmt.Fprintf(out, "\t\tfree space info extent count %v flags %v\n",
+ body.ExtentCount, body.Flags)
+ case btrfsitem.FreeSpaceBitmap:
+ fmt.Fprintf(out, "\t\tfree space bitmap\n")
+ case btrfsitem.Chunk:
+ fmt.Fprintf(out, "\t\tlength %d owner %d stripe_len %v type %v\n",
+ body.Head.Size, body.Head.Owner, body.Head.StripeLen, body.Head.Type)
+ fmt.Fprintf(out, "\t\tio_align %v io_width %v sector_size %v\n",
+ body.Head.IOOptimalAlign, body.Head.IOOptimalWidth, body.Head.IOMinSize)
+ fmt.Fprintf(out, "\t\tnum_stripes %v sub_stripes %v\n",
+ body.Head.NumStripes, body.Head.SubStripes)
+ for i, stripe := range body.Stripes {
+ fmt.Fprintf(out, "\t\t\tstripe %v devid %d offset %d\n",
+ i, stripe.DeviceID, stripe.Offset)
+ fmt.Fprintf(out, "\t\t\tdev_uuid %v\n",
+ stripe.DeviceUUID)
+ }
+ case btrfsitem.Dev:
+ fmt.Fprintf(out, ""+
+ "\t\tdevid %d total_bytes %v bytes_used %v\n"+
+ "\t\tio_align %v io_width %v sector_size %v type %v\n"+
+ "\t\tgeneration %v start_offset %v dev_group %v\n"+
+ "\t\tseek_speed %v bandwidth %v\n"+
+ "\t\tuuid %v\n"+
+ "\t\tfsid %v\n",
+ body.DevID, body.NumBytes, body.NumBytesUsed,
+ body.IOOptimalAlign, body.IOOptimalWidth, body.IOMinSize, body.Type,
+ body.Generation, body.StartOffset, body.DevGroup,
+ body.SeekSpeed, body.Bandwidth,
+ body.DevUUID,
+ body.FSUUID)
+ case btrfsitem.DevExtent:
+ fmt.Fprintf(out, ""+
+ "\t\tdev extent chunk_tree %v\n"+
+ "\t\tchunk_objectid %v chunk_offset %d length %d\n"+
+ "\t\tchunk_tree_uuid %v\n",
+ body.ChunkTree, body.ChunkObjectID, body.ChunkOffset, body.Length,
+ body.ChunkTreeUUID)
+ //case btrfsitem.QGROUP_STATUS_KEY:
+ // // TODO
+ //case btrfsitem.QGROUP_INFO_KEY:
+ // // TODO
+ //case btrfsitem.QGROUP_LIMIT_KEY:
+ // // TODO
+ case btrfsitem.UUIDMap:
+ fmt.Fprintf(out, "\t\tsubvol_id %d\n", body.ObjID)
+ //case btrfsitem.STRING_ITEM_KEY:
+ // // TODO
+ case btrfsitem.DevStats:
+ fmt.Fprintf(out, "\t\tpersistent item objectid %v offset %v\n",
+ item.Head.Key.ObjectID.Format(item.Head.Key.ItemType), item.Head.Key.Offset)
+ switch item.Head.Key.ObjectID {
+ case btrfs.DEV_STATS_OBJECTID:
+ fmt.Fprintf(out, "\t\tdevice stats\n")
+ fmt.Fprintf(out, "\t\twrite_errs %v read_errs %v flush_errs %v corruption_errs %v generation %v\n",
+ body.Values[btrfsitem.DEV_STAT_WRITE_ERRS],
+ body.Values[btrfsitem.DEV_STAT_READ_ERRS],
+ body.Values[btrfsitem.DEV_STAT_FLUSH_ERRS],
+ body.Values[btrfsitem.DEV_STAT_CORRUPTION_ERRS],
+ body.Values[btrfsitem.DEV_STAT_GENERATION_ERRS])
+ default:
+ fmt.Fprintf(out, "\t\tunknown persistent item objectid %v\n", item.Head.Key.ObjectID)
+ }
+ //case btrfsitem.TEMPORARY_ITEM_KEY:
+ // // TODO
+ case btrfsitem.Empty:
+ switch item.Head.Key.ItemType {
+ case btrfsitem.ORPHAN_ITEM_KEY: // 48
+ fmt.Fprintf(out, "\t\torphan item\n")
+ case btrfsitem.TREE_BLOCK_REF_KEY: // 176
+ fmt.Fprintf(out, "\t\ttree block backref\n")
+ case btrfsitem.SHARED_BLOCK_REF_KEY: // 182
+ fmt.Fprintf(out, "\t\tshared block backref\n")
+ case btrfsitem.FREE_SPACE_EXTENT_KEY: // 199
+ fmt.Fprintf(out, "\t\tfree space extent\n")
+ case btrfsitem.QGROUP_RELATION_KEY: // 246
+ // do nothing
+ //case btrfsitem.EXTENT_REF_V0_KEY:
+ // fmt.Fprintf(out, "\t\textent ref v0 (deprecated)\n")
+ //case btrfsitem.CSUM_ITEM_KEY:
+ // fmt.Fprintf(out, "\t\tcsum item\n")
+ default:
+ fmt.Fprintf(out, "\t\t(error) unhandled empty item type: %v\n", item.Head.Key.ItemType)
+ }
+ case btrfsitem.Error:
+ fmt.Fprintf(out, "\t\t(error) error item: %v\n", body.Err)
default:
- fmt.Fprintf(out, "\t\t(error) unhandled empty item type: %v\n", item.Head.Key.ItemType)
+ fmt.Fprintf(out, "\t\t(error) unhandled item type: %T\n", body)
}
- case btrfsitem.Error:
- fmt.Fprintf(out, "\t\t(error) error item: %v\n", body.Err)
- default:
- fmt.Fprintf(out, "\t\t(error) unhandled item type: %T\n", body)
- }
- return nil
+ return nil
+ },
},
- })
+ )
}
// printHeaderInfo mimics btrfs-progs kernel-shared/print-tree.c:print_header_info()
diff --git a/lib/btrfsprogs/btrfsrepair/clearnodes.go b/lib/btrfsprogs/btrfsrepair/clearnodes.go
index e9cd879..dbe1c3a 100644
--- a/lib/btrfsprogs/btrfsrepair/clearnodes.go
+++ b/lib/btrfsprogs/btrfsrepair/clearnodes.go
@@ -5,9 +5,11 @@
package btrfsrepair
import (
+ "context"
"errors"
"fmt"
- "io"
+
+ "github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
@@ -15,39 +17,26 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/util"
)
-func ClearBadNodes(out, errout io.Writer, fs *btrfs.FS) error {
+func ClearBadNodes(ctx context.Context, fs *btrfs.FS) error {
var uuidsInited bool
var metadataUUID, chunkTreeUUID btrfs.UUID
- var treeName string
- var treeID btrfs.ObjID
btrfsutil.WalkAllTrees(fs, btrfsutil.WalkAllTreesHandler{
- PreTree: func(name string, id btrfs.ObjID) {
- treeName = name
- treeID = id
- },
- Err: func(err error) {
- fmt.Fprintf(errout, "error: %v\n", err)
+ Err: func(err *btrfsutil.WalkError) {
+ dlog.Error(ctx, err)
},
- UnsafeNodes: true,
TreeWalkHandler: btrfs.TreeWalkHandler{
- Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
- if err == nil {
- if !uuidsInited {
- metadataUUID = node.Data.Head.MetadataUUID
- chunkTreeUUID = node.Data.Head.ChunkTreeUUID
- uuidsInited = true
- }
- return nil
+ Node: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node]) error {
+ if !uuidsInited {
+ metadataUUID = node.Data.Head.MetadataUUID
+ chunkTreeUUID = node.Data.Head.ChunkTreeUUID
+ uuidsInited = true
}
+ return nil
+ },
+ BadNode: func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
if !errors.Is(err, btrfs.ErrNotANode) {
- err = btrfsutil.WalkErr{
- TreeName: treeName,
- Path: path,
- Err: err,
- }
- fmt.Fprintf(errout, "error: %v\n", err)
- return nil
+ return err
}
origErr := err
if !uuidsInited {
@@ -66,24 +55,20 @@ func ClearBadNodes(out, errout io.Writer, fs *btrfs.FS) error {
BackrefRev: btrfs.MixedBackrefRev,
ChunkTreeUUID: chunkTreeUUID,
Generation: 0,
- Owner: treeID,
+ Owner: path.TreeID,
NumItems: 0,
- Level: path[len(path)-1].NodeLevel,
+ Level: path.Nodes[len(path.Nodes)-1].NodeLevel,
},
}
node.Data.Head.Checksum, err = node.Data.CalculateChecksum()
if err != nil {
- return btrfsutil.WalkErr{
- TreeName: treeName,
- Path: path,
- Err: err,
- }
+ return err
}
if err := node.Write(); err != nil {
return err
}
- fmt.Fprintf(out, "fixed node@%v (err was %v)\n", node.Addr, origErr)
+ dlog.Infof(ctx, "fixed node@%v (err was %v)\n", node.Addr, origErr)
return nil
},
},
diff --git a/lib/btrfsprogs/btrfsutil/open.go b/lib/btrfsprogs/btrfsutil/open.go
index 80bfe7a..c237b54 100644
--- a/lib/btrfsprogs/btrfsutil/open.go
+++ b/lib/btrfsprogs/btrfsutil/open.go
@@ -23,8 +23,8 @@ func Open(ctx context.Context, flag int, filenames ...string) (*btrfs.FS, error)
_ = fs.Close()
return nil, fmt.Errorf("device file %q: %w", filename, err)
}
- if err := fs.AddDevice(&btrfs.Device{File: fh}); err != nil {
- dlog.Errorf(ctx, "device file %q: %v", filename, err)
+ if err := fs.AddDevice(ctx, &btrfs.Device{File: fh}); err != nil {
+ return nil, fmt.Errorf("device file %q: %w", filename, err)
}
}
return fs, nil
diff --git a/lib/btrfsprogs/btrfsutil/walk.go b/lib/btrfsprogs/btrfsutil/walk.go
index 0c54384..d2322b2 100644
--- a/lib/btrfsprogs/btrfsutil/walk.go
+++ b/lib/btrfsprogs/btrfsutil/walk.go
@@ -9,47 +9,33 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
- "git.lukeshu.com/btrfs-progs-ng/lib/util"
)
-type WalkErr struct {
+type WalkError struct {
TreeName string
- Path btrfs.TreePath
- Err error
+ Err *btrfs.TreeError
}
-func (e WalkErr) Unwrap() error { return e.Err }
+func (e *WalkError) Unwrap() error { return e.Err }
-func (e WalkErr) Error() string {
- if len(e.Path) == 0 {
- return fmt.Sprintf("%v: %v", e.TreeName, e.Err)
- }
- return fmt.Sprintf("%v: %v: %v", e.TreeName, e.Path, e.Err)
+func (e *WalkError) Error() string {
+ return fmt.Sprintf("%v: %v", e.TreeName, e.Err)
}
type WalkAllTreesHandler struct {
- Err func(error)
+ Err func(*WalkError)
// Callbacks for entire trees
PreTree func(name string, id btrfs.ObjID)
PostTree func(name string, id btrfs.ObjID)
// Callbacks for nodes or smaller
- UnsafeNodes bool
btrfs.TreeWalkHandler
}
// WalkAllTrees walks all trees in a *btrfs.FS. Rather than returning
// an error, it calls errCb each time an error is encountered. The
-// error will always be of type WalkErr.
+// error will always be of type WalkError.
func WalkAllTrees(fs *btrfs.FS, cbs WalkAllTreesHandler) {
var treeName string
- handleErr := func(path btrfs.TreePath, err error) {
- cbs.Err(WalkErr{
- TreeName: treeName,
- Path: path,
- Err: err,
- })
- }
trees := []struct {
Name string
@@ -90,28 +76,17 @@ func WalkAllTrees(fs *btrfs.FS, cbs WalkAllTreesHandler) {
return nil
}
- if !cbs.UnsafeNodes {
- origNode := cbs.Node
- cbs.Node = func(path btrfs.TreePath, node *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error {
- if err != nil {
- handleErr(path, err)
- }
- if node != nil && origNode != nil {
- return origNode(path, node, nil)
- }
- return nil
- }
- }
-
for i := 0; i < len(trees); i++ {
tree := trees[i]
treeName = tree.Name
if cbs.PreTree != nil {
cbs.PreTree(treeName, tree.ID)
}
- if err := fs.TreeWalk(tree.ID, cbs.TreeWalkHandler); err != nil {
- handleErr(nil, err)
- }
+ fs.TreeWalk(
+ tree.ID,
+ func(err *btrfs.TreeError) { cbs.Err(&WalkError{TreeName: treeName, Err: err}) },
+ cbs.TreeWalkHandler,
+ )
if cbs.PostTree != nil {
cbs.PostTree(treeName, tree.ID)
}