xref: /titanic_41/usr/src/uts/common/fs/zfs/blkptr.c (revision e54e0be9db8d75da4531ae3618fb713c9cf0604b)
1*e54e0be9SMatthew Ahrens /*
2*e54e0be9SMatthew Ahrens  * CDDL HEADER START
3*e54e0be9SMatthew Ahrens  *
4*e54e0be9SMatthew Ahrens  * This file and its contents are supplied under the terms of the
5*e54e0be9SMatthew Ahrens  * Common Development and Distribution License ("CDDL"), version 1.0.
6*e54e0be9SMatthew Ahrens  * You may only use this file in accordance with the terms of version
7*e54e0be9SMatthew Ahrens  * 1.0 of the CDDL.
8*e54e0be9SMatthew Ahrens  *
9*e54e0be9SMatthew Ahrens  * A full copy of the text of the CDDL should have accompanied this
10*e54e0be9SMatthew Ahrens  * source.  A copy of the CDDL is also available via the Internet at
11*e54e0be9SMatthew Ahrens  * http://www.illumos.org/license/CDDL.
12*e54e0be9SMatthew Ahrens  *
13*e54e0be9SMatthew Ahrens  * CDDL HEADER END
14*e54e0be9SMatthew Ahrens  */
15*e54e0be9SMatthew Ahrens 
16*e54e0be9SMatthew Ahrens /*
17*e54e0be9SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
18*e54e0be9SMatthew Ahrens  */
19*e54e0be9SMatthew Ahrens 
20*e54e0be9SMatthew Ahrens #include <sys/zfs_context.h>
21*e54e0be9SMatthew Ahrens #include <sys/zio.h>
22*e54e0be9SMatthew Ahrens #include <sys/zio_compress.h>
23*e54e0be9SMatthew Ahrens 
24*e54e0be9SMatthew Ahrens /*
25*e54e0be9SMatthew Ahrens  * Embedded-data Block Pointers
26*e54e0be9SMatthew Ahrens  *
27*e54e0be9SMatthew Ahrens  * Normally, block pointers point (via their DVAs) to a block which holds data.
28*e54e0be9SMatthew Ahrens  * If the data that we need to store is very small, this is an inefficient
29*e54e0be9SMatthew Ahrens  * use of space, because a block must be at minimum 1 sector (typically 512
30*e54e0be9SMatthew Ahrens  * bytes or 4KB).  Additionally, reading these small blocks tends to generate
31*e54e0be9SMatthew Ahrens  * more random reads.
32*e54e0be9SMatthew Ahrens  *
33*e54e0be9SMatthew Ahrens  * Embedded-data Block Pointers allow small pieces of data (the "payload",
34*e54e0be9SMatthew Ahrens  * up to 112 bytes) to be stored in the block pointer itself, instead of
35*e54e0be9SMatthew Ahrens  * being pointed to.  The "Pointer" part of this name is a bit of a
36*e54e0be9SMatthew Ahrens  * misnomer, as nothing is pointed to.
37*e54e0be9SMatthew Ahrens  *
38*e54e0be9SMatthew Ahrens  * BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to
39*e54e0be9SMatthew Ahrens  * be embedded in the block pointer.  The logic for this is handled in
40*e54e0be9SMatthew Ahrens  * the SPA, by the zio pipeline.  Therefore most code outside the zio
41*e54e0be9SMatthew Ahrens  * pipeline doesn't need special-cases to handle these block pointers.
42*e54e0be9SMatthew Ahrens  *
43*e54e0be9SMatthew Ahrens  * See spa.h for details on the exact layout of embedded block pointers.
44*e54e0be9SMatthew Ahrens  */
45*e54e0be9SMatthew Ahrens 
46*e54e0be9SMatthew Ahrens void
encode_embedded_bp_compressed(blkptr_t * bp,void * data,enum zio_compress comp,int uncompressed_size,int compressed_size)47*e54e0be9SMatthew Ahrens encode_embedded_bp_compressed(blkptr_t *bp, void *data,
48*e54e0be9SMatthew Ahrens     enum zio_compress comp, int uncompressed_size, int compressed_size)
49*e54e0be9SMatthew Ahrens {
50*e54e0be9SMatthew Ahrens 	uint64_t *bp64 = (uint64_t *)bp;
51*e54e0be9SMatthew Ahrens 	uint64_t w = 0;
52*e54e0be9SMatthew Ahrens 	uint8_t *data8 = data;
53*e54e0be9SMatthew Ahrens 
54*e54e0be9SMatthew Ahrens 	ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);
55*e54e0be9SMatthew Ahrens 	ASSERT(uncompressed_size == compressed_size ||
56*e54e0be9SMatthew Ahrens 	    comp != ZIO_COMPRESS_OFF);
57*e54e0be9SMatthew Ahrens 	ASSERT3U(comp, >=, ZIO_COMPRESS_OFF);
58*e54e0be9SMatthew Ahrens 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
59*e54e0be9SMatthew Ahrens 
60*e54e0be9SMatthew Ahrens 	bzero(bp, sizeof (*bp));
61*e54e0be9SMatthew Ahrens 	BP_SET_EMBEDDED(bp, B_TRUE);
62*e54e0be9SMatthew Ahrens 	BP_SET_COMPRESS(bp, comp);
63*e54e0be9SMatthew Ahrens 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
64*e54e0be9SMatthew Ahrens 	BPE_SET_LSIZE(bp, uncompressed_size);
65*e54e0be9SMatthew Ahrens 	BPE_SET_PSIZE(bp, compressed_size);
66*e54e0be9SMatthew Ahrens 
67*e54e0be9SMatthew Ahrens 	/*
68*e54e0be9SMatthew Ahrens 	 * Encode the byte array into the words of the block pointer.
69*e54e0be9SMatthew Ahrens 	 * First byte goes into low bits of first word (little endian).
70*e54e0be9SMatthew Ahrens 	 */
71*e54e0be9SMatthew Ahrens 	for (int i = 0; i < compressed_size; i++) {
72*e54e0be9SMatthew Ahrens 		BF64_SET(w, (i % sizeof (w)) * NBBY, NBBY, data8[i]);
73*e54e0be9SMatthew Ahrens 		if (i % sizeof (w) == sizeof (w) - 1) {
74*e54e0be9SMatthew Ahrens 			/* we've reached the end of a word */
75*e54e0be9SMatthew Ahrens 			ASSERT3P(bp64, <, bp + 1);
76*e54e0be9SMatthew Ahrens 			*bp64 = w;
77*e54e0be9SMatthew Ahrens 			bp64++;
78*e54e0be9SMatthew Ahrens 			if (!BPE_IS_PAYLOADWORD(bp, bp64))
79*e54e0be9SMatthew Ahrens 				bp64++;
80*e54e0be9SMatthew Ahrens 			w = 0;
81*e54e0be9SMatthew Ahrens 		}
82*e54e0be9SMatthew Ahrens 	}
83*e54e0be9SMatthew Ahrens 	/* write last partial word */
84*e54e0be9SMatthew Ahrens 	if (bp64 < (uint64_t *)(bp + 1))
85*e54e0be9SMatthew Ahrens 		*bp64 = w;
86*e54e0be9SMatthew Ahrens }
87*e54e0be9SMatthew Ahrens 
88*e54e0be9SMatthew Ahrens /*
89*e54e0be9SMatthew Ahrens  * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
90*e54e0be9SMatthew Ahrens  * more than BPE_PAYLOAD_SIZE bytes).
91*e54e0be9SMatthew Ahrens  */
92*e54e0be9SMatthew Ahrens void
decode_embedded_bp_compressed(const blkptr_t * bp,void * buf)93*e54e0be9SMatthew Ahrens decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
94*e54e0be9SMatthew Ahrens {
95*e54e0be9SMatthew Ahrens 	int psize;
96*e54e0be9SMatthew Ahrens 	uint8_t *buf8 = buf;
97*e54e0be9SMatthew Ahrens 	uint64_t w = 0;
98*e54e0be9SMatthew Ahrens 	const uint64_t *bp64 = (const uint64_t *)bp;
99*e54e0be9SMatthew Ahrens 
100*e54e0be9SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
101*e54e0be9SMatthew Ahrens 
102*e54e0be9SMatthew Ahrens 	psize = BPE_GET_PSIZE(bp);
103*e54e0be9SMatthew Ahrens 
104*e54e0be9SMatthew Ahrens 	/*
105*e54e0be9SMatthew Ahrens 	 * Decode the words of the block pointer into the byte array.
106*e54e0be9SMatthew Ahrens 	 * Low bits of first word are the first byte (little endian).
107*e54e0be9SMatthew Ahrens 	 */
108*e54e0be9SMatthew Ahrens 	for (int i = 0; i < psize; i++) {
109*e54e0be9SMatthew Ahrens 		if (i % sizeof (w) == 0) {
110*e54e0be9SMatthew Ahrens 			/* beginning of a word */
111*e54e0be9SMatthew Ahrens 			ASSERT3P(bp64, <, bp + 1);
112*e54e0be9SMatthew Ahrens 			w = *bp64;
113*e54e0be9SMatthew Ahrens 			bp64++;
114*e54e0be9SMatthew Ahrens 			if (!BPE_IS_PAYLOADWORD(bp, bp64))
115*e54e0be9SMatthew Ahrens 				bp64++;
116*e54e0be9SMatthew Ahrens 		}
117*e54e0be9SMatthew Ahrens 		buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
118*e54e0be9SMatthew Ahrens 	}
119*e54e0be9SMatthew Ahrens }
120