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

package btrfs

import (
	"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/btrfssum"
	"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"
)

// dat doesn't escape to the heap in .ReadAt(dat, …), but the compiler
// can't figure that out, so we use a Pool for our byte arrays, since
// the compiler won't let us allocate them on the stack.
var blockPool containers.SlicePool[byte]

func ChecksumLogical(fs diskio.File[btrfsvol.LogicalAddr], alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (btrfssum.CSum, error) {
	dat := blockPool.Get(btrfssum.BlockSize)
	defer blockPool.Put(dat)
	if _, err := fs.ReadAt(dat, laddr); err != nil {
		return btrfssum.CSum{}, err
	}
	return alg.Sum(dat)
}

func ChecksumPhysical(dev *Device, alg btrfssum.CSumType, paddr btrfsvol.PhysicalAddr) (btrfssum.CSum, error) {
	dat := blockPool.Get(btrfssum.BlockSize)
	defer blockPool.Put(dat)
	if _, err := dev.ReadAt(dat, paddr); err != nil {
		return btrfssum.CSum{}, err
	}
	return alg.Sum(dat)
}

func ChecksumQualifiedPhysical(fs *FS, alg btrfssum.CSumType, paddr btrfsvol.QualifiedPhysicalAddr) (btrfssum.CSum, error) {
	dev := fs.LV.PhysicalVolumes()[paddr.Dev]
	if dev == nil {
		return btrfssum.CSum{}, fmt.Errorf("no such device_id=%v", paddr.Dev)
	}
	return ChecksumPhysical(dev, alg, paddr.Addr)
}

func LookupCSum(fs btrfstree.TreeOperator, alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (btrfssum.SumRun[btrfsvol.LogicalAddr], error) {
	item, err := fs.TreeSearch(btrfsprim.CSUM_TREE_OBJECTID, func(key btrfsprim.Key, size uint32) int {
		itemBeg := btrfsvol.LogicalAddr(key.Offset)
		numSums := int64(size) / int64(alg.Size())
		itemEnd := itemBeg + btrfsvol.LogicalAddr(numSums*btrfssum.BlockSize)
		switch {
		case itemEnd <= laddr:
			return 1
		case laddr < itemBeg:
			return -1
		default:
			return 0
		}
	})
	if err != nil {
		return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, err
	}
	if item.Key.ItemType != btrfsitem.EXTENT_CSUM_KEY {
		return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, fmt.Errorf("item type is %v, not EXTENT_CSUM", item.Key.ItemType)
	}
	switch body := item.Body.(type) {
	case *btrfsitem.ExtentCSum:
		return body.SumRun, nil
	case *btrfsitem.Error:
		return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, body.Err
	default:
		panic(fmt.Errorf("should not happen: EXTENT_CSUM has unexpected item type: %T", body))
	}
}