1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfs
import (
"context"
"fmt"
"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/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)
// This file is ordered from low-level to high-level.
// btrfstree.NodeSource ////////////////////////////////////////////////////////
type nodeCacheEntry struct {
node *btrfstree.Node
err error
}
// AcquireNode implements btrfstree.NodeSource.
func (fs *FS) AcquireNode(ctx context.Context, addr btrfsvol.LogicalAddr, exp btrfstree.NodeExpectations) (*btrfstree.Node, error) {
if fs.cacheNodes == nil {
fs.cacheNodes = containers.NewARCache[btrfsvol.LogicalAddr, nodeCacheEntry](
textui.Tunable(4*(btrfstree.MaxLevel+1)),
containers.SourceFunc[btrfsvol.LogicalAddr, nodeCacheEntry](fs.readNode),
)
}
nodeEntry := fs.cacheNodes.Acquire(ctx, addr)
if nodeEntry.err != nil {
err := nodeEntry.err
fs.cacheNodes.Release(addr)
return nil, err
}
if nodeEntry.node != nil {
if err := exp.Check(nodeEntry.node); err != nil {
fs.cacheNodes.Release(addr)
return nil, fmt.Errorf("btrfstree.ReadNode: node@%v: %w", addr, err) // fmt.Errorf("btrfs.FS.AcquireNode: node@%v: %w", addr, err)
}
}
return nodeEntry.node, nil
}
// ReleaseNode implements btrfstree.NodeSource.
func (fs *FS) ReleaseNode(node *btrfstree.Node) {
if node == nil {
return
}
fs.cacheNodes.Release(node.Head.Addr)
}
func (fs *FS) readNode(_ context.Context, addr btrfsvol.LogicalAddr, nodeEntry *nodeCacheEntry) {
nodeEntry.node.RawFree()
nodeEntry.node = nil
sb, err := fs.Superblock()
if err != nil {
nodeEntry.err = err
return
}
nodeEntry.node, nodeEntry.err = btrfstree.ReadNode[btrfsvol.LogicalAddr](fs, *sb, addr)
}
var _ btrfstree.NodeSource = (*FS)(nil)
// btrfstree.Forrest ///////////////////////////////////////////////////////////
// RawTree is a variant of ForrestLookup that returns a concrete type
// instead of an interface.
func (fs *FS) RawTree(ctx context.Context, treeID btrfsprim.ObjID) (*btrfstree.RawTree, error) {
return btrfstree.RawForrest{NodeSource: fs}.RawTree(ctx, treeID)
}
// ForrestLookup implements btree.Forrest.
func (fs *FS) ForrestLookup(ctx context.Context, treeID btrfsprim.ObjID) (btrfstree.Tree, error) {
return btrfstree.RawForrest{NodeSource: fs}.ForrestLookup(ctx, treeID)
}
var _ btrfstree.Forrest = (*FS)(nil)
// ReadableFS //////////////////////////////////////////////////////////////////
type ReadableFS interface {
btrfstree.Forrest
Superblock() (*btrfstree.Superblock, error)
Name() string
// For reading file contents.
diskio.ReaderAt[btrfsvol.LogicalAddr]
}
var _ ReadableFS = (*FS)(nil)
|