summaryrefslogtreecommitdiff
path: root/pkg/btrfs/types_bitfields.go
blob: 391ac15440ab5c85cd45f81f6186253c7914c9ac (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
package btrfs

import (
	"encoding/binary"
	"fmt"
	"strings"

	"lukeshu.com/btrfs-tools/pkg/binstruct"
)

func bitfieldString[T ~uint8 | ~uint16 | ~uint32 | ~uint64](bitfield T, bitnames []string) string {
	var out strings.Builder
	fmt.Fprintf(&out, "0x%0X", uint64(bitfield))
	if bitfield == 0 {
		out.WriteString("(none)")
	} else {
		rest := bitfield
		sep := '('
		for i := 0; rest != 0; i++ {
			if rest&(1<<i) != 0 {
				out.WriteRune(sep)
				if i < len(bitnames) {
					out.WriteString(bitnames[i])
				} else {
					fmt.Fprintf(&out, "(1<<%d)", i)
				}
				sep = '|'
			}
			rest &^= 1 << i
		}
		out.WriteRune(')')
	}
	return out.String()
}

type IncompatFlags uint64

const (
	FeatureIncompatMixedBackref = IncompatFlags(1 << iota)
	FeatureIncompatDefaultSubvol
	FeatureIncompatMixedGroups
	FeatureIncompatCompressLZO
	FeatureIncompatCompressZSTD
	FeatureIncompatBigMetadata // buggy
	FeatureIncompatExtendedIRef
	FeatureIncompatRAID56
	FeatureIncompatSkinnyMetadata
	FeatureIncompatNoHoles
	FeatureIncompatMetadataUUID
	FeatureIncompatRAID1C34
	FeatureIncompatZoned
	FeatureIncompatExtentTreeV2
)

var incompatFlagNames = []string{
	"FeatureIncompatMixedBackref",
	"FeatureIncompatDefaultSubvol",
	"FeatureIncompatMixedGroups",
	"FeatureIncompatCompressLZO",
	"FeatureIncompatCompressZSTD",
	"FeatureIncompatBigMetadata ",
	"FeatureIncompatExtendedIRef",
	"FeatureIncompatRAID56",
	"FeatureIncompatSkinnyMetadata",
	"FeatureIncompatNoHoles",
	"FeatureIncompatMetadataUUID",
	"FeatureIncompatRAID1C34",
	"FeatureIncompatZoned",
	"FeatureIncompatExtentTreeV2",
}

func (f IncompatFlags) Has(req IncompatFlags) bool { return f&req == req }
func (f IncompatFlags) String() string             { return bitfieldString(f, incompatFlagNames) }

type NodeFlags uint64

func (NodeFlags) BinarySize() int64 {
	return 7
}
func (f NodeFlags) MarshalBinary() []byte {
	var bs [8]byte
	binary.LittleEndian.PutUint64(bs[:], uint64(f))
	return bs[:7]
}
func (f *NodeFlags) UnmarshalBinary(dat []byte) {
	var bs [8]byte
	copy(bs[:7], dat[:7])
	*f = NodeFlags(binary.LittleEndian.Uint64(bs[:]))
}

var (
	_ binstruct.Marshaler   = NodeFlags(0)
	_ binstruct.Unmarshaler = (*NodeFlags)(nil)
)

const (
	NodeWritten = NodeFlags(1 << iota)
	NodeReloc
)

var nodeFlagNames = []string{
	"WRITTEN",
	"RELOC",
}

func (f NodeFlags) Has(req NodeFlags) bool { return f&req == req }
func (f NodeFlags) String() string         { return bitfieldString(f, nodeFlagNames) }

type RootItemFlags uint64

const (
	BTRFS_ROOT_SUBVOL_RDONLY = RootItemFlags(1 << iota)
)

var rootItemFlagNames = []string{
	"SUBVOL_RDONLY",
}

func (f RootItemFlags) Has(req RootItemFlags) bool { return f&req == req }
func (f RootItemFlags) String() string             { return bitfieldString(f, rootItemFlagNames) }