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
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsutil
import (
"fmt"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
)
func ChecksumLogical(fs btrfs.Trees, alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (btrfssum.CSum, error) {
var dat [btrfssum.BlockSize]byte
if _, err := fs.ReadAt(dat[:], laddr); err != nil {
return btrfssum.CSum{}, err
}
return alg.Sum(dat[:])
}
func ChecksumPhysical(dev *btrfs.Device, alg btrfssum.CSumType, paddr btrfsvol.PhysicalAddr) (btrfssum.CSum, error) {
var dat [btrfssum.BlockSize]byte
if _, err := dev.ReadAt(dat[:], paddr); err != nil {
return btrfssum.CSum{}, err
}
return alg.Sum(dat[:])
}
func ChecksumQualifiedPhysical(fs *btrfs.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 btrfs.Trees, alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (map[btrfsvol.LogicalAddr]btrfssum.CSum, error) {
item, err := fs.TreeSearch(btrfs.CSUM_TREE_OBJECTID, func(key btrfs.Key, size uint32) int {
itemBeg := btrfsvol.LogicalAddr(key.ObjectID)
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 nil, err
}
body, ok := item.Body.(btrfsitem.ExtentCSum)
if !ok {
return nil, fmt.Errorf("item body is %T not ExtentCSum", item.Body)
}
ret := make(map[btrfsvol.LogicalAddr]btrfssum.CSum, len(body.Sums))
for i, sum := range body.Sums {
ret[btrfsvol.LogicalAddr(item.Key.ObjectID)+(btrfsvol.LogicalAddr(i)*btrfssum.BlockSize)] = sum
}
return ret, nil
}
|