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