btrfsutil

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2023 License: Apache-2.0, GPL-2.0, GPL-3.0-or-later, + 1 more Imports: 23 Imported by: 0

Documentation

Overview

Package btrfsutil implements userspace utilities for working with btrfs filesystems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListNodes

func ListNodes(ctx context.Context, fs *btrfs.FS) ([]btrfsvol.LogicalAddr, error)

func Open

func Open(ctx context.Context, flag int, filenames ...string) (*btrfs.FS, error)

func ScanDevices

func ScanDevices[Stats comparable, Result any](ctx context.Context, fs *btrfs.FS, newScanner DeviceScannerFactory[Stats, Result]) (map[btrfsvol.DeviceID]Result, error)

func ScanOneDevice

func ScanOneDevice[Stats comparable, Result any](ctx context.Context, dev *btrfs.Device, newScanner DeviceScannerFactory[Stats, Result]) (Result, error)

func WalkAllTrees

func WalkAllTrees(ctx context.Context, fs btrfs.ReadableFS, cbs WalkAllTreesHandler)

WalkAllTrees walks all trees in a btrfs.ReadableFS. Rather than returning an error, it calls the appropriate "BadXXX" callback (BadTree, BadNode, BadItem) each time an error is encountered.

Types

type DeviceScanner

type DeviceScanner[Stats comparable, Result any] interface {
	ScanStats() Stats
	ScanSector(ctx context.Context, dev *btrfs.Device, paddr btrfsvol.PhysicalAddr) error
	ScanNode(ctx context.Context, addr btrfsvol.PhysicalAddr, node *btrfstree.Node) error
	ScanDone(ctx context.Context) (Result, error)
}

type DeviceScannerFactory

type DeviceScannerFactory[Stats comparable, Result any] func(ctx context.Context, sb btrfstree.Superblock, numBytes btrfsvol.PhysicalAddr, numSectors int) DeviceScanner[Stats, Result]

type Graph

type Graph struct {
	Nodes     map[btrfsvol.LogicalAddr]GraphNode
	BadNodes  map[btrfsvol.LogicalAddr]error
	EdgesFrom map[btrfsvol.LogicalAddr][]*GraphEdge
	EdgesTo   map[btrfsvol.LogicalAddr][]*GraphEdge
}

func NewGraph

func NewGraph(ctx context.Context, sb btrfstree.Superblock) Graph

func ReadGraph

func ReadGraph(_ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr) (Graph, error)

func (Graph) FinalCheck

func (g Graph) FinalCheck(ctx context.Context, fs btrfstree.NodeSource) error

func (Graph) InsertNode

func (g Graph) InsertNode(node *btrfstree.Node)

type GraphEdge

type GraphEdge struct {
	// It is invalid for both 'FromRoot' and 'FromNode' to be
	// non-zero.  If both are zero, then the GraphEdge is from the
	// superblock.
	FromRoot btrfsvol.LogicalAddr
	FromNode btrfsvol.LogicalAddr
	FromSlot int // only valid if one of FromRoot or FromNode is non-zero

	FromTree btrfsprim.ObjID

	ToNode       btrfsvol.LogicalAddr
	ToLevel      uint8
	ToKey        btrfsprim.Key
	ToGeneration btrfsprim.Generation
}

func (GraphEdge) String

func (kp GraphEdge) String() string

type GraphNode

type GraphNode struct {
	Addr       btrfsvol.LogicalAddr
	Level      uint8
	Generation btrfsprim.Generation
	Owner      btrfsprim.ObjID
	Items      []KeyAndSize
}

func (GraphNode) CheckExpectations

func (n GraphNode) CheckExpectations(g Graph, exp btrfstree.NodeExpectations) error

func (GraphNode) MaxItem

func (n GraphNode) MaxItem(g Graph) btrfsprim.Key

func (GraphNode) MinItem

func (n GraphNode) MinItem(g Graph) btrfsprim.Key

func (GraphNode) NumItems

func (n GraphNode) NumItems(g Graph) int

func (GraphNode) String

func (n GraphNode) String() string

type ItemPtr

type ItemPtr struct {
	Node btrfsvol.LogicalAddr
	Slot int
}

func (ItemPtr) String

func (ptr ItemPtr) String() string

type KeyAndSize

type KeyAndSize struct {
	Key  btrfsprim.Key
	Size uint32
}

type RebuiltForrest

type RebuiltForrest struct {
	// contains filtered or unexported fields
}

RebuiltForrest is an abstraction for rebuilding and accessing potentially broken btrees.

Additionally, it provides some functionality on top of a vanilla btrfs.ReadableFS:

  • it provides a RebuiltTree.RebuiltAddRoot() method for repairing a tree.

  • it provides a RebuiltForrest.RebuiltListRoots() method for listing how trees have been repaired.

  • it provides a RebuiltForrest.RebuiltAddRoots() method for batch-loading the results from RebuiltForrest.RebuiltListroots().

  • it provides several RebuiltTree methods that provide advice on what roots should be added to a tree in order to repair it:

    .RebuiltAcquireItems()/.RebuiltReleaseItems() and .RebuiltAcquirePotentialItems()/.RebuiltReleasePotentialItems() to compare what's in the tree and what could be in the tree.

    .RebuiltLeafToRoots() to map potential items to things that can be passed to .RebuiltAddRoot().

    .RebuiltCOWDistance() and .RebuiltShouldReplace() to provide information on deciding on an option from .RebuiltLeafToRoots().

A zero RebuiltForrest is invalid; it must be initialized with NewRebuiltForrest().

func NewRebuiltForrest

func NewRebuiltForrest(fs btrfs.ReadableFS, graph Graph, cb RebuiltForrestCallbacks, laxAncestors bool) *RebuiltForrest

NewRebuiltForrest returns a new RebuiltForrest instance.

The `cb` RebuiltForrestCallbacks may be nil. If `cb` also implements RebuiltForrestExtendedCallbacks, then a series of .AddedItem() calls will be made before each call to .AddedRoot().

`laxAncestors` is whether or not an error instantiating an ancestor tree should prevent instantiating an descendant tree (lax=false prevents it, lax=true allows it).

  • `laxAncestors` inhibits calls to RebuiltForrestExtendedCallbacks.AddedItem().

  • `laxAncestors` causes a call to RebuiltTree.RebuiltAddRoot on the ROOT_TREE or the UUID_TREE to panic if a tree other than the ROOT_TREE or the UUID_TREE has been read from.

func (*RebuiltForrest) AcquireNode

AcquireNode implements btrfstree.NodeSource (and btrfs.ReadableFS).

func (*RebuiltForrest) ForrestLookup

func (ts *RebuiltForrest) ForrestLookup(ctx context.Context, treeID btrfsprim.ObjID) (btrfstree.Tree, error)

ForrestLookup implements btrfstree.Forrest (and btrfs.ReadableFS).

It is identical to .RebuiltTree(), but returns an interface rather than a concrete type.

func (*RebuiltForrest) Name

func (ts *RebuiltForrest) Name() string

Name implements btrfs.ReadableFS.

func (*RebuiltForrest) ReadAt

func (ts *RebuiltForrest) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error)

ReadAt implements diskio.ReaderAt[btrfsvol.LogicalAddr] (and btrfs.ReadableFS).

func (*RebuiltForrest) RebuiltAddRoots

func (ts *RebuiltForrest) RebuiltAddRoots(ctx context.Context, roots map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr])

RebuiltAddRoots takes a listing of the root nodes for trees (as returned by RebuiltListRoots), and augments the trees to include them.

func (*RebuiltForrest) RebuiltListRoots

RebuiltListRoots returns a listing of all initialized trees and their root nodes.

Do not mutate the set of roots for a tree; it is a pointer to the RebuiltForrest's internal set!

func (*RebuiltForrest) RebuiltTree

func (ts *RebuiltForrest) RebuiltTree(ctx context.Context, treeID btrfsprim.ObjID) (*RebuiltTree, error)

RebuiltTree returns a given tree, initializing it if nescessary.

The tree is initialized with the normal root node of the tree.

This is identical to .ForrestLookup(), but returns a concrete type rather than an interface.

func (*RebuiltForrest) ReleaseNode

func (ts *RebuiltForrest) ReleaseNode(node *btrfstree.Node)

ReleaseNode implements btrfstree.NodeSource (and btrfs.ReadableFS).

func (*RebuiltForrest) Superblock

func (ts *RebuiltForrest) Superblock() (*btrfstree.Superblock, error)

Superblock implements btrfstree.NodeSource (and btrfs.ReadableFS).

type RebuiltForrestCallbacks

type RebuiltForrestCallbacks interface {
	AddedRoot(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr)
	LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, err error)
	LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, err error)
}

type RebuiltForrestExtendedCallbacks

type RebuiltForrestExtendedCallbacks interface {
	RebuiltForrestCallbacks
	AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key)
}

type RebuiltTree

type RebuiltTree struct {
	ID        btrfsprim.ObjID
	UUID      btrfsprim.UUID
	Root      btrfsvol.LogicalAddr
	Parent    *RebuiltTree
	ParentGen btrfsprim.Generation // offset of this tree's root item

	Roots containers.Set[btrfsvol.LogicalAddr]
	// contains filtered or unexported fields
}

func (*RebuiltTree) RebuiltAcquireItems

func (tree *RebuiltTree) RebuiltAcquireItems(ctx context.Context) *containers.SortedMap[btrfsprim.Key, ItemPtr]

RebuiltAcquireItems returns a map of the items contained in this tree.

Do not mutate the returned map; it is a pointer to the RebuiltTree's internal map!

When done with the map, call .RebuiltReleaseItems().

func (*RebuiltTree) RebuiltAcquirePotentialItems

func (tree *RebuiltTree) RebuiltAcquirePotentialItems(ctx context.Context) *containers.SortedMap[btrfsprim.Key, ItemPtr]

RebuiltAcquirePotentialItems returns a map of items that could be added to this tree with .RebuiltAddRoot().

Do not mutate the returned map; it is a pointer to the RebuiltTree's internal map!

When done with the map, call .RebuiltReleasePotentialItems().

func (*RebuiltTree) RebuiltAddRoot

func (tree *RebuiltTree) RebuiltAddRoot(ctx context.Context, rootNode btrfsvol.LogicalAddr)

RebuiltAddRoot adds an additional root node to the tree. It is useful to call .RebuiltAddRoot() to re-attach part of the tree that has been broken off.

If the RebuiltForrest has laxAncestors=false, then:

  • calls to RebuiltForrestExtendedCallbacks.AddedItem() are inhibited.

  • calling RebuiltAddRoot on the ROOT_TREE or the UUID_TREE will panic if a tree other than the ROOT_TREE or UUID_TREE has been read from.

func (*RebuiltTree) RebuiltCOWDistance

func (tree *RebuiltTree) RebuiltCOWDistance(parentID btrfsprim.ObjID) (dist int, ok bool)

RebuiltCOWDistance returns how many COW-snapshots down the 'tree' is from the 'parent'.

func (*RebuiltTree) RebuiltLeafToRoots

func (tree *RebuiltTree) RebuiltLeafToRoots(ctx context.Context, leaf btrfsvol.LogicalAddr) containers.Set[btrfsvol.LogicalAddr]

RebuiltLeafToRoots returns the list of potential roots (to pass to .RebuiltAddRoot) that include a given leaf-node.

func (*RebuiltTree) RebuiltReleaseItems

func (tree *RebuiltTree) RebuiltReleaseItems()

RebuiltReleaseItems releases resources after a call to .RebuiltAcquireItems().

func (*RebuiltTree) RebuiltReleasePotentialItems

func (tree *RebuiltTree) RebuiltReleasePotentialItems()

RebuiltReleasePotentialItems releases resources after a call to .RebuiltAcquirePotentialItems().

func (*RebuiltTree) RebuiltShouldReplace

func (tree *RebuiltTree) RebuiltShouldReplace(oldNode, newNode btrfsvol.LogicalAddr) bool

func (*RebuiltTree) TreeLookup

func (tree *RebuiltTree) TreeLookup(ctx context.Context, key btrfsprim.Key) (btrfstree.Item, error)

TreeLookup implements btrfstree.Tree.

func (*RebuiltTree) TreeParentID

func (tree *RebuiltTree) TreeParentID(_ context.Context) (btrfsprim.ObjID, btrfsprim.Generation, error)

TreeParentID implements btrfstree.Tree.

func (*RebuiltTree) TreeRange

func (tree *RebuiltTree) TreeRange(ctx context.Context, handleFn func(btrfstree.Item) bool) error

TreeRange implements btrfstree.Tree. It is a thin wrapper around tree.RebuiltItems(ctx).Range (to do the iteration) and tree.TreeLookup (to read item bodies).

func (*RebuiltTree) TreeSearch

func (tree *RebuiltTree) TreeSearch(ctx context.Context, searcher btrfstree.TreeSearcher) (btrfstree.Item, error)

TreeSearch implements btrfstree.Tree. It is a thin wrapper around tree.RebuiltItems(ctx).Search (to do the search) and tree.TreeLookup (to read item bodies).

func (*RebuiltTree) TreeSubrange

func (tree *RebuiltTree) TreeSubrange(ctx context.Context,
	min int,
	searcher btrfstree.TreeSearcher,
	handleFn func(btrfstree.Item) bool,
) error

TreeSubrange implements btrfstree.Tree. It is a thin wrapper around tree.RebuiltItems(ctx).Subrange (to do the iteration) and tree.TreeLookup (to read item bodies).

func (*RebuiltTree) TreeWalk

func (tree *RebuiltTree) TreeWalk(ctx context.Context, cbs btrfstree.TreeWalkHandler)

TreeWalk implements btrfstree.Tree.

type WalkAllTreesHandler

type WalkAllTreesHandler struct {
	PreTree  func(name string, id btrfsprim.ObjID)
	BadTree  func(name string, id btrfsprim.ObjID, err error)
	Tree     btrfstree.TreeWalkHandler
	PostTree func(name string, id btrfsprim.ObjID)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL