summaryrefslogtreecommitdiff
path: root/lib/btrfsprogs/btrfsutil/broken_btree.go
blob: 15641ab829aa1e261c83d85c81f7a036aa3de70a (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsutil

import (
	"context"
	"fmt"
	iofs "io/fs"
	"sync"

	"github.com/datawire/dlib/derror"
	"github.com/datawire/dlib/dlog"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
	"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"
)

type treeIndex struct {
	TreeRootErr error
	Items       *containers.RBTree[treeIndexValue]
	Errors      *containers.IntervalTree[btrfsprim.Key, treeIndexError]
}

type treeIndexError struct {
	Path SkinnyPath
	Err  error
}

type treeIndexValue struct {
	Path     SkinnyPath
	Key      btrfsprim.Key
	ItemSize uint32
}

// Compare implements containers.Ordered.
func (a treeIndexValue) Compare(b treeIndexValue) int {
	return a.Key.Compare(b.Key)
}

func newTreeIndex(arena *SkinnyPathArena) treeIndex {
	return treeIndex{
		Items: new(containers.RBTree[treeIndexValue]),
		Errors: &containers.IntervalTree[btrfsprim.Key, treeIndexError]{
			MinFn: func(err treeIndexError) btrfsprim.Key {
				return arena.Inflate(err.Path).Node(-1).ToKey
			},
			MaxFn: func(err treeIndexError) btrfsprim.Key {
				return arena.Inflate(err.Path).Node(-1).ToMaxKey
			},
		},
	}
}

type brokenTrees struct {
	ctx   context.Context //nolint:containedctx // don't have an option while keeping the same API
	inner *btrfs.FS

	arena *SkinnyPathArena

	// btrfsprim.ROOT_TREE_OBJECTID
	rootTreeMu    sync.Mutex
	rootTreeIndex *treeIndex
	// for all other trees
	treeMu      sync.Mutex
	treeIndexes map[btrfsprim.ObjID]treeIndex
}

var _ btrfstree.TreeOperator = (*brokenTrees)(nil)

// NewBrokenTrees wraps a *btrfs.FS to support looking up information
// from broken trees.
//
// Of the btrfstree.TreeOperator methods:
//
//   - TreeWalk works on broken trees
//   - TreeLookup relies on the tree being properly ordered (which a
//     broken tree might not be).
//   - TreeSearch relies on the tree being properly ordered (which a
//     broken tree might not be).
//   - TreeSearchAll relies on the tree being properly ordered (which a
//     broken tree might not be), and a bad node may cause it to not
//     return a truncated list of results.
//
// NewBrokenTrees attempts to remedy these deficiencies by using
// .TreeWalk to build an out-of-FS index of all of the items in the
// tree, and re-implements TreeLookup, TreeSearch, and TreeSearchAll
// using that index.
func NewBrokenTrees(ctx context.Context, inner *btrfs.FS) interface {
	btrfstree.TreeOperator
	Superblock() (*btrfstree.Superblock, error)
	ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error)
	Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.LogicalAddr) ([]btrfsprim.Key, error)
} {
	return &brokenTrees{
		ctx:   ctx,
		inner: inner,
	}
}

func (bt *brokenTrees) treeIndex(treeID btrfsprim.ObjID) treeIndex {
	var treeRoot *btrfstree.TreeRoot
	var sb *btrfstree.Superblock
	var err error
	if treeID == btrfsprim.ROOT_TREE_OBJECTID {
		bt.rootTreeMu.Lock()
		defer bt.rootTreeMu.Unlock()
		if bt.rootTreeIndex != nil {
			return *bt.rootTreeIndex
		}
		sb, err = bt.inner.Superblock()
		if err == nil {
			treeRoot, err = btrfstree.LookupTreeRoot(bt.inner, *sb, treeID)
		}
	} else {
		bt.treeMu.Lock()
		defer bt.treeMu.Unlock()
		if bt.treeIndexes == nil {
			bt.treeIndexes = make(map[btrfsprim.ObjID]treeIndex)
		}
		if cacheEntry, exists := bt.treeIndexes[treeID]; exists {
			return cacheEntry
		}
		sb, err = bt.inner.Superblock()
		if err == nil {
			treeRoot, err = btrfstree.LookupTreeRoot(bt, *sb, treeID)
		}
	}
	if bt.arena == nil {
		var _sb btrfstree.Superblock
		if sb != nil {
			_sb = *sb
		}
		bt.arena = &SkinnyPathArena{
			FS: bt.inner,
			SB: _sb,
		}
	}
	cacheEntry := newTreeIndex(bt.arena)
	if err != nil {
		cacheEntry.TreeRootErr = err
	} else {
		dlog.Infof(bt.ctx, "indexing tree %v...", treeID)
		bt.rawTreeWalk(*treeRoot, cacheEntry, nil)
		dlog.Infof(bt.ctx, "... done indexing tree %v", treeID)
	}
	if treeID == btrfsprim.ROOT_TREE_OBJECTID {
		bt.rootTreeIndex = &cacheEntry
	} else {
		bt.treeIndexes[treeID] = cacheEntry
	}
	return cacheEntry
}

func (bt *brokenTrees) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry treeIndex, walked *[]btrfsprim.Key) {
	btrfstree.TreeOperatorImpl{NodeSource: bt.inner}.RawTreeWalk(
		bt.ctx,
		root,
		func(err *btrfstree.TreeError) {
			if len(err.Path) > 0 && err.Path.Node(-1).ToNodeAddr == 0 {
				// This is a panic because on the filesystems I'm working with it more likely
				// indicates a bug in my item parser than a problem with the filesystem.
				panic(fmt.Errorf("TODO: error parsing item: %w", err))
			}
			cacheEntry.Errors.Insert(treeIndexError{
				Path: bt.arena.Deflate(err.Path),
				Err:  err.Err,
			})
		},
		btrfstree.TreeWalkHandler{
			Item: func(path btrfstree.TreePath, item btrfstree.Item) error {
				if cacheEntry.Items.Search(func(v treeIndexValue) int { return item.Key.Compare(v.Key) }) != nil {
					// This is a panic because I'm not really sure what the best way to
					// handle this is, and so if this happens I want the program to crash
					// and force me to figure out how to handle it.
					panic(fmt.Errorf("dup key=%v in tree=%v", item.Key, root.TreeID))
				}
				cacheEntry.Items.Insert(treeIndexValue{
					Path:     bt.arena.Deflate(path),
					Key:      item.Key,
					ItemSize: item.BodySize,
				})
				if walked != nil {
					*walked = append(*walked, item.Key)
				}
				return nil
			},
		},
	)
}

func (bt *brokenTrees) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (btrfstree.Item, error) {
	item, err := bt.TreeSearch(treeID, btrfstree.KeySearch(key.Compare))
	if err != nil {
		err = fmt.Errorf("item with key=%v: %w", key, err)
	}
	return item, err
}

func (bt *brokenTrees) addErrs(index treeIndex, fn func(btrfsprim.Key, uint32) int, err error) error {
	var errs derror.MultiError
	index.Errors.Subrange(
		func(k btrfsprim.Key) int { return fn(k, 0) },
		func(v treeIndexError) bool {
			errs = append(errs, &btrfstree.TreeError{
				Path: bt.arena.Inflate(v.Path),
				Err:  v.Err,
			})
			return true
		})
	if len(errs) == 0 {
		return err
	}
	if err != nil {
		errs = append(errs, err)
	}
	return errs
}

func (bt *brokenTrees) TreeSearch(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) (btrfstree.Item, error) {
	index := bt.treeIndex(treeID)
	if index.TreeRootErr != nil {
		return btrfstree.Item{}, index.TreeRootErr
	}

	indexItem := index.Items.Search(func(indexItem treeIndexValue) int {
		return fn(indexItem.Key, indexItem.ItemSize)
	})
	if indexItem == nil {
		return btrfstree.Item{}, bt.addErrs(index, fn, iofs.ErrNotExist)
	}

	itemPath := bt.arena.Inflate(indexItem.Value.Path)
	node, err := bt.inner.ReadNode(itemPath.Parent())
	defer btrfstree.FreeNodeRef(node)
	if err != nil {
		return btrfstree.Item{}, bt.addErrs(index, fn, err)
	}

	item := node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx]
	item.Body = item.Body.CloneItem()

	// Since we were only asked to return 1 item, it isn't
	// necessary to augment this `nil` with bt.addErrs().
	return item, nil
}

func (bt *brokenTrees) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) ([]btrfstree.Item, error) {
	index := bt.treeIndex(treeID)
	if index.TreeRootErr != nil {
		return nil, index.TreeRootErr
	}

	var indexItems []treeIndexValue
	index.Items.Subrange(
		func(indexItem treeIndexValue) int { return fn(indexItem.Key, indexItem.ItemSize) },
		func(node *containers.RBNode[treeIndexValue]) bool {
			indexItems = append(indexItems, node.Value)
			return true
		})
	if len(indexItems) == 0 {
		return nil, bt.addErrs(index, fn, iofs.ErrNotExist)
	}

	ret := make([]btrfstree.Item, len(indexItems))
	var node *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]
	for i := range indexItems {
		itemPath := bt.arena.Inflate(indexItems[i].Path)
		if node == nil || node.Addr != itemPath.Node(-2).ToNodeAddr {
			var err error
			btrfstree.FreeNodeRef(node)
			node, err = bt.inner.ReadNode(itemPath.Parent())
			if err != nil {
				btrfstree.FreeNodeRef(node)
				return nil, bt.addErrs(index, fn, err)
			}
		}
		ret[i] = node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx]
	}
	btrfstree.FreeNodeRef(node)

	return ret, bt.addErrs(index, fn, nil)
}

func (bt *brokenTrees) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, errHandle func(*btrfstree.TreeError), cbs btrfstree.TreeWalkHandler) {
	index := bt.treeIndex(treeID)
	if index.TreeRootErr != nil {
		errHandle(&btrfstree.TreeError{
			Path: btrfstree.TreePath{{
				FromTree: treeID,
				ToMaxKey: btrfsprim.MaxKey,
			}},
			Err: index.TreeRootErr,
		})
		return
	}
	var node *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]
	index.Items.Range(func(indexItem *containers.RBNode[treeIndexValue]) bool {
		if ctx.Err() != nil {
			return false
		}
		if bt.ctx.Err() != nil {
			return false
		}
		if cbs.Item != nil {
			itemPath := bt.arena.Inflate(indexItem.Value.Path)
			if node == nil || node.Addr != itemPath.Node(-2).ToNodeAddr {
				var err error
				btrfstree.FreeNodeRef(node)
				node, err = bt.inner.ReadNode(itemPath.Parent())
				if err != nil {
					btrfstree.FreeNodeRef(node)
					errHandle(&btrfstree.TreeError{Path: itemPath, Err: err})
					return true
				}
			}
			item := node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx]
			if err := cbs.Item(itemPath, item); err != nil {
				errHandle(&btrfstree.TreeError{Path: itemPath, Err: err})
			}
		}
		return true
	})
	btrfstree.FreeNodeRef(node)
}

func (bt *brokenTrees) Superblock() (*btrfstree.Superblock, error) {
	return bt.inner.Superblock()
}

func (bt *brokenTrees) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) {
	return bt.inner.ReadAt(p, off)
}

func (bt *brokenTrees) Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.LogicalAddr) ([]btrfsprim.Key, error) {
	sb, err := bt.Superblock()
	if err != nil {
		return nil, err
	}
	index := bt.treeIndex(treeID)
	if index.TreeRootErr != nil {
		return nil, index.TreeRootErr
	}
	nodeRef, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](bt.inner, *sb, nodeAddr, btrfstree.NodeExpectations{})
	defer btrfstree.FreeNodeRef(nodeRef)
	if err != nil {
		return nil, err
	}
	var ret []btrfsprim.Key
	bt.rawTreeWalk(btrfstree.TreeRoot{
		TreeID:     treeID,
		RootNode:   nodeAddr,
		Level:      nodeRef.Data.Head.Level,
		Generation: nodeRef.Data.Head.Generation,
	}, index, &ret)
	return ret, nil
}