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

package btrfsvol

import (
	"fmt"
	"sort"

	"git.lukeshu.com/btrfs-progs-ng/lib/containers"
	"git.lukeshu.com/btrfs-progs-ng/lib/maps"
	"git.lukeshu.com/btrfs-progs-ng/lib/slices"
)

// logical => []physical
type chunkMapping struct {
	LAddr      LogicalAddr
	PAddrs     []QualifiedPhysicalAddr
	Size       AddrDelta
	SizeLocked bool
	Flags      containers.Optional[BlockGroupFlags]
}

type ChunkMapping = chunkMapping

// return -1 if 'a' is wholly to the left of 'b'
// return 0 if there is some overlap between 'a' and 'b'
// return 1 if 'a is wholly to the right of 'b'
func (a chunkMapping) cmpRange(b chunkMapping) int {
	switch {
	case a.LAddr.Add(a.Size) <= b.LAddr:
		// 'a' is wholly to the left of 'b'.
		return -1
	case b.LAddr.Add(b.Size) <= a.LAddr:
		// 'a' is wholly to the right of 'b'.
		return 1
	default:
		// There is some overlap.
		return 0
	}
}

func (a chunkMapping) union(rest ...chunkMapping) (chunkMapping, error) {
	// sanity check
	for _, chunk := range rest {
		if a.cmpRange(chunk) != 0 {
			return chunkMapping{}, fmt.Errorf("chunks don't overlap")
		}
	}
	chunks := append([]chunkMapping{a}, rest...)
	// figure out the logical range (.LAddr and .Size)
	beg := chunks[0].LAddr
	end := chunks[0].LAddr.Add(chunks[0].Size)
	for _, chunk := range chunks {
		beg = slices.Min(beg, chunk.LAddr)
		end = slices.Max(end, chunk.LAddr.Add(chunk.Size))
	}
	ret := chunkMapping{
		LAddr: beg,
		Size:  end.Sub(beg),
	}
	for _, chunk := range chunks {
		if chunk.SizeLocked {
			ret.SizeLocked = true
			if ret.Size != chunk.Size {
				return chunkMapping{}, fmt.Errorf("member chunk has locked size=%v, but union would have size=%v",
					chunk.Size, ret.Size)
			}
		}
	}
	// figure out the physical stripes (.PAddrs)
	paddrs := make(containers.Set[QualifiedPhysicalAddr])
	for _, chunk := range chunks {
		offsetWithinRet := chunk.LAddr.Sub(ret.LAddr)
		for _, stripe := range chunk.PAddrs {
			paddrs.Insert(stripe.Add(-offsetWithinRet))
		}
	}
	ret.PAddrs = maps.Keys(paddrs)
	sort.Slice(ret.PAddrs, func(i, j int) bool {
		return ret.PAddrs[i].Cmp(ret.PAddrs[j]) < 0
	})
	// figure out the flags (.Flags)
	for _, chunk := range chunks {
		if !chunk.Flags.OK {
			continue
		}
		if !ret.Flags.OK {
			ret.Flags = chunk.Flags
		}
		if ret.Flags != chunk.Flags {
			return ret, fmt.Errorf("mismatch flags: %v != %v", ret.Flags.Val, chunk.Flags.Val)
		}
	}
	// done
	return ret, nil
}