xref: /freebsd/sys/cddl/boot/zfs/blkptr.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * CDDL HEADER START
3  *
4  * This file and its contents are supplied under the terms of the
5  * Common Development and Distribution License ("CDDL"), version 1.0.
6  * You may only use this file in accordance with the terms of version
7  * 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this
10  * source.  A copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15 
16 /*
17  * Copyright (c) 2013 by Delphix. All rights reserved.
18  */
19 
20 /*
21  * Embedded-data Block Pointers
22  *
23  * Normally, block pointers point (via their DVAs) to a block which holds data.
24  * If the data that we need to store is very small, this is an inefficient
25  * use of space, because a block must be at minimum 1 sector (typically 512
26  * bytes or 4KB).  Additionally, reading these small blocks tends to generate
27  * more random reads.
28  *
29  * Embedded-data Block Pointers allow small pieces of data (the "payload",
30  * up to 112 bytes) to be stored in the block pointer itself, instead of
31  * being pointed to.  The "Pointer" part of this name is a bit of a
32  * misnomer, as nothing is pointed to.
33  *
34  * BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to
35  * be embedded in the block pointer.  The logic for this is handled in
36  * the SPA, by the zio pipeline.  Therefore most code outside the zio
37  * pipeline doesn't need special-cases to handle these block pointers.
38  *
39  * See spa.h for details on the exact layout of embedded block pointers.
40  */
41 
42 /*
43  * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
44  * more than BPE_PAYLOAD_SIZE bytes).
45  */
46 void
47 decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
48 {
49 	int psize;
50 	uint8_t *buf8 = buf;
51 	uint64_t w = 0;
52 	const uint64_t *bp64 = (const uint64_t *)bp;
53 
54 	ASSERT(BP_IS_EMBEDDED(bp));
55 
56 	psize = BPE_GET_PSIZE(bp);
57 
58 	/*
59 	 * Decode the words of the block pointer into the byte array.
60 	 * Low bits of first word are the first byte (little endian).
61 	 */
62 	for (int i = 0; i < psize; i++) {
63 		if (i % sizeof (w) == 0) {
64 			/* beginning of a word */
65 			ASSERT3P(bp64, <, bp + 1);
66 			w = *bp64;
67 			bp64++;
68 			if (!BPE_IS_PAYLOADWORD(bp, bp64))
69 				bp64++;
70 		}
71 		buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
72 	}
73 }
74