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