summaryrefslogtreecommitdiff
path: root/lib/btrfsutil/rebuilt_callbacks.go
blob: 8d96c413f080e57b7517454ec71eb1762063aff7 (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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsutil

import (
	"context"
	"fmt"

	"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/btrfs/btrfsvol"
)

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 interface {
	RebuiltForrestCallbacks
	AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key)
}

type noopRebuiltForrestCallbacks struct {
	forrest btrfstree.Forrest
}

func (noopRebuiltForrestCallbacks) AddedRoot(context.Context, btrfsprim.ObjID, btrfsvol.LogicalAddr) {
}

func (cb noopRebuiltForrestCallbacks) LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, _item btrfsitem.Root, err error) {
	rootTree, err := cb.forrest.ForrestLookup(ctx, btrfsprim.ROOT_TREE_OBJECTID)
	if err != nil {
		return 0, btrfsitem.Root{}, err
	}
	item, err := rootTree.TreeSearch(ctx, btrfstree.SearchRootItem(tree))
	if err != nil {
		return 0, btrfsitem.Root{}, err
	}
	defer item.Body.Free()
	switch itemBody := item.Body.(type) {
	case *btrfsitem.Root:
		return btrfsprim.Generation(item.Key.Offset), *itemBody, nil
	case *btrfsitem.Error:
		return 0, btrfsitem.Root{}, itemBody.Err
	default:
		// This is a panic because the item decoder should not emit ROOT_ITEM items as anything but
		// btrfsitem.Root or btrfsitem.Error without this code also being updated.
		panic(fmt.Errorf("should not happen: ROOT_ITEM item has unexpected type: %T", itemBody))
	}
}

func (cb noopRebuiltForrestCallbacks) LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, err error) {
	uuidTree, err := cb.forrest.ForrestLookup(ctx, btrfsprim.UUID_TREE_OBJECTID)
	if err != nil {
		if id, ok := cb.slowLookupUUID(ctx, uuid); ok {
			return id, nil
		}
		return 0, err
	}
	tgt := btrfsitem.UUIDToKey(uuid)
	item, err := uuidTree.TreeLookup(ctx, tgt)
	if err != nil {
		if id, ok := cb.slowLookupUUID(ctx, uuid); ok {
			return id, nil
		}
		return 0, err
	}
	defer item.Body.Free()
	switch itemBody := item.Body.(type) {
	case *btrfsitem.UUIDMap:
		return itemBody.ObjID, nil
	case *btrfsitem.Error:
		if id, ok := cb.slowLookupUUID(ctx, uuid); ok {
			return id, nil
		}
		return 0, err
	default:
		// This is a panic because the item decoder should not emit UUID_SUBVOL items as anything but
		// btrfsitem.UUIDMap or btrfsitem.Error without this code also being updated.
		panic(fmt.Errorf("should not happen: UUID_SUBVOL item has unexpected type: %T", itemBody))
	}
}

func (cb noopRebuiltForrestCallbacks) slowLookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool) {
	rootTree, err := cb.forrest.ForrestLookup(ctx, btrfsprim.ROOT_TREE_OBJECTID)
	if err != nil {
		return 0, false
	}
	var ret btrfsprim.ObjID
	_ = rootTree.TreeRange(ctx, func(item btrfstree.Item) bool {
		if item.Key.ItemType != btrfsprim.ROOT_ITEM_KEY {
			return true
		}
		switch itemBody := item.Body.(type) {
		case *btrfsitem.Root:
			if itemBody.UUID == uuid {
				ret = item.Key.ObjectID
				return false
			}
		case *btrfsitem.Error:
			// do nothing
		default:
			// This is a panic because the item decoder should not emit ROOT_ITEM items as anything but
			// btrfsitem.Root or btrfsitem.Error without this code also being updated.
			panic(fmt.Errorf("should not happen: ROOT_ITEM item has unexpected type: %T", itemBody))
		}
		return true
	})
	if ret == 0 {
		return 0, false
	}
	return ret, true
}