summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/item_fileextent.go
blob: df3d3710acf55edb3f8e2a25725390667cb806ea (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsitem

import (
	"fmt"

	"git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
)

// FileExtent items map from regions in a file to regions in the
// logical address space.
//
// Compare with:
//
//   - Extents, which map from regions in the logical address space to
//     regions in a file.
//   - Metadata, which are like Extents but without .Info.
//
// Key:
//
//	key.objectid = inode
//	key.offset   = offset within file
type FileExtent struct { // complex EXTENT_DATA=108
	Generation btrfsprim.Generation `bin:"off=0x0, siz=0x8"` // transaction ID that created this extent
	RAMBytes   int64                `bin:"off=0x8, siz=0x8"` // upper bound of what compressed data will decompress to

	// 32 bits describing the data encoding
	Compression   CompressionType `bin:"off=0x10, siz=0x1"`
	Encryption    uint8           `bin:"off=0x11, siz=0x1"`
	OtherEncoding uint16          `bin:"off=0x12, siz=0x2"` // reserved for later use

	Type FileExtentType `bin:"off=0x14, siz=0x1"` // inline data or real extent

	binstruct.End `bin:"off=0x15"`

	// only one of these, depending on .Type
	BodyInline []byte           `bin:"-"` // .Type == FILE_EXTENT_INLINE
	BodyExtent FileExtentExtent `bin:"-"` // .Type == FILE_EXTENT_REG or FILE_EXTENT_PREALLOC
}

type FileExtentExtent struct {
	// Position and size of extent within the device
	DiskByteNr   btrfsvol.LogicalAddr `bin:"off=0x0, siz=0x8"`
	DiskNumBytes btrfsvol.AddrDelta   `bin:"off=0x8, siz=0x8"`

	// Position of data within the extent
	Offset btrfsvol.AddrDelta `bin:"off=0x10, siz=0x8"`

	// Decompressed/unencrypted size
	NumBytes int64 `bin:"off=0x18, siz=0x8"`

	binstruct.End `bin:"off=0x20"`
}

func (o *FileExtent) Free() {
	bytePool.Put(o.BodyInline)
	*o = FileExtent{}
	fileExtentPool.Put(o)
}

func (o FileExtent) Clone() FileExtent {
	o.BodyInline = cloneBytes(o.BodyInline)
	return o
}

func (o *FileExtent) UnmarshalBinary(dat []byte) (int, error) {
	n, err := binstruct.UnmarshalWithoutInterface(dat, o)
	if err != nil {
		return n, err
	}
	switch o.Type {
	case FILE_EXTENT_INLINE:
		o.BodyInline = cloneBytes(dat[n:])
		n += len(o.BodyInline)
	case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC:
		_n, err := binstruct.Unmarshal(dat[n:], &o.BodyExtent)
		n += _n
		if err != nil {
			return n, err
		}
	default:
		return n, fmt.Errorf("unknown file extent type %v", o.Type)
	}
	return n, nil
}

func (o FileExtent) MarshalBinary() ([]byte, error) {
	dat, err := binstruct.MarshalWithoutInterface(o)
	if err != nil {
		return dat, err
	}
	switch o.Type {
	case FILE_EXTENT_INLINE:
		dat = append(dat, o.BodyInline...)
	case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC:
		bs, err := binstruct.Marshal(o.BodyExtent)
		dat = append(dat, bs...)
		if err != nil {
			return dat, err
		}
	default:
		return dat, fmt.Errorf("unknown file extent type %v", o.Type)
	}
	return dat, nil
}

type FileExtentType uint8

const (
	FILE_EXTENT_INLINE FileExtentType = iota
	FILE_EXTENT_REG
	FILE_EXTENT_PREALLOC
)

var fileExtentTypeNames = []string{
	"inline",
	"regular",
	"prealloc",
}

func (o FileExtent) Size() (int64, error) {
	switch o.Type {
	case FILE_EXTENT_INLINE:
		return int64(len(o.BodyInline)), nil
	case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC:
		return o.BodyExtent.NumBytes, nil
	default:
		return 0, fmt.Errorf("unknown file extent type %v", o.Type)
	}
}

func (fet FileExtentType) String() string {
	name := "unknown"
	if int(fet) < len(fileExtentTypeNames) {
		name = fileExtentTypeNames[fet]
	}
	return fmt.Sprintf("%d (%s)", fet, name)
}

type CompressionType uint8

const (
	COMPRESS_NONE CompressionType = iota
	COMPRESS_ZLIB
	COMPRESS_LZO
	COMPRESS_ZSTD
)

var compressionTypeNames = []string{
	"none",
	"zlib",
	"lzo",
	"zstd",
}

func (ct CompressionType) String() string {
	name := "unknown"
	if int(ct) < len(compressionTypeNames) {
		name = compressionTypeNames[ct]
	}
	return fmt.Sprintf("%d (%s)", ct, name)
}