Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/zfs/blkptr.c
48383 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* This file and its contents are supplied under the terms of the
6
* Common Development and Distribution License ("CDDL"), version 1.0.
7
* You may only use this file in accordance with the terms of version
8
* 1.0 of the CDDL.
9
*
10
* A full copy of the text of the CDDL should have accompanied this
11
* source. A copy of the CDDL is also available via the Internet at
12
* http://www.illumos.org/license/CDDL.
13
*
14
* CDDL HEADER END
15
*/
16
17
/*
18
* Copyright (c) 2013, 2016 by Delphix. All rights reserved.
19
*/
20
21
#include <sys/blkptr.h>
22
#include <sys/zfs_context.h>
23
#include <sys/zio.h>
24
#include <sys/zio_compress.h>
25
26
/*
27
* Embedded-data Block Pointers
28
*
29
* Normally, block pointers point (via their DVAs) to a block which holds data.
30
* If the data that we need to store is very small, this is an inefficient
31
* use of space, because a block must be at minimum 1 sector (typically 512
32
* bytes or 4KB). Additionally, reading these small blocks tends to generate
33
* more random reads.
34
*
35
* Embedded-data Block Pointers allow small pieces of data (the "payload",
36
* up to 112 bytes) to be stored in the block pointer itself, instead of
37
* being pointed to. The "Pointer" part of this name is a bit of a
38
* misnomer, as nothing is pointed to.
39
*
40
* BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to
41
* be embedded in the block pointer. The logic for this is handled in
42
* the SPA, by the zio pipeline. Therefore most code outside the zio
43
* pipeline doesn't need special-cases to handle these block pointers.
44
*
45
* See spa.h for details on the exact layout of embedded block pointers.
46
*/
47
48
void
49
encode_embedded_bp_compressed(blkptr_t *bp, void *data,
50
enum zio_compress comp, int uncompressed_size, int compressed_size)
51
{
52
uint64_t *bp64 = (uint64_t *)bp;
53
uint64_t w = 0;
54
uint8_t *data8 = data;
55
56
ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);
57
ASSERT(uncompressed_size == compressed_size ||
58
comp != ZIO_COMPRESS_OFF);
59
ASSERT3U(comp, >=, ZIO_COMPRESS_OFF);
60
ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
61
62
memset(bp, 0, sizeof (*bp));
63
BP_SET_EMBEDDED(bp, B_TRUE);
64
BP_SET_COMPRESS(bp, comp);
65
BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
66
BPE_SET_LSIZE(bp, uncompressed_size);
67
BPE_SET_PSIZE(bp, compressed_size);
68
69
/*
70
* Encode the byte array into the words of the block pointer.
71
* First byte goes into low bits of first word (little endian).
72
*/
73
for (int i = 0; i < compressed_size; i++) {
74
BF64_SET(w, (i % sizeof (w)) * NBBY, NBBY, data8[i]);
75
if (i % sizeof (w) == sizeof (w) - 1) {
76
/* we've reached the end of a word */
77
ASSERT3P(bp64, <, bp + 1);
78
*bp64 = w;
79
bp64++;
80
if (!BPE_IS_PAYLOADWORD(bp, bp64))
81
bp64++;
82
w = 0;
83
}
84
}
85
/* write last partial word */
86
if (bp64 < (uint64_t *)(bp + 1))
87
*bp64 = w;
88
}
89
90
/*
91
* buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
92
* more than BPE_PAYLOAD_SIZE bytes).
93
*/
94
void
95
decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
96
{
97
int psize;
98
uint8_t *buf8 = buf;
99
uint64_t w = 0;
100
const uint64_t *bp64 = (const uint64_t *)bp;
101
102
ASSERT(BP_IS_EMBEDDED(bp));
103
104
psize = BPE_GET_PSIZE(bp);
105
106
/*
107
* Decode the words of the block pointer into the byte array.
108
* Low bits of first word are the first byte (little endian).
109
*/
110
for (int i = 0; i < psize; i++) {
111
if (i % sizeof (w) == 0) {
112
/* beginning of a word */
113
ASSERT3P(bp64, <, bp + 1);
114
w = *bp64;
115
bp64++;
116
if (!BPE_IS_PAYLOADWORD(bp, bp64))
117
bp64++;
118
}
119
buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
120
}
121
}
122
123
/*
124
* Fill in the buffer with the (decompressed) payload of the embedded
125
* blkptr_t. Takes into account compression and byteorder (the payload is
126
* treated as a stream of bytes).
127
* Return 0 on success, or ENOSPC if it won't fit in the buffer.
128
*/
129
int
130
decode_embedded_bp(const blkptr_t *bp, void *buf, int buflen)
131
{
132
int lsize, psize;
133
134
ASSERT(BP_IS_EMBEDDED(bp));
135
136
lsize = BPE_GET_LSIZE(bp);
137
psize = BPE_GET_PSIZE(bp);
138
139
if (lsize > buflen)
140
return (SET_ERROR(ENOSPC));
141
ASSERT3U(lsize, ==, buflen);
142
143
if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
144
uint8_t dstbuf[BPE_PAYLOAD_SIZE];
145
decode_embedded_bp_compressed(bp, dstbuf);
146
abd_t cabd, dabd;
147
abd_get_from_buf_struct(&cabd, dstbuf, psize);
148
abd_get_from_buf_struct(&dabd, buf, buflen);
149
VERIFY0(zio_decompress_data(BP_GET_COMPRESS(bp), &cabd,
150
&dabd, psize, buflen, NULL));
151
abd_free(&dabd);
152
abd_free(&cabd);
153
} else {
154
ASSERT3U(lsize, ==, psize);
155
decode_embedded_bp_compressed(bp, buf);
156
}
157
158
return (0);
159
}
160
161