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