5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * CDDL HEADER START
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * This file and its contents are supplied under the terms of the
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Common Development and Distribution License ("CDDL"), version 1.0.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * You may only use this file in accordance with the terms of version
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * 1.0 of the CDDL.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * A full copy of the text of the CDDL should have accompanied this
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * source. A copy of the CDDL is also available via the Internet at
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * CDDL HEADER END
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Embedded-data Block Pointers
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Normally, block pointers point (via their DVAs) to a block which holds data.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * If the data that we need to store is very small, this is an inefficient
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * use of space, because a block must be at minimum 1 sector (typically 512
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * bytes or 4KB). Additionally, reading these small blocks tends to generate
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * more random reads.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Embedded-data Block Pointers allow small pieces of data (the "payload",
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * up to 112 bytes) to be stored in the block pointer itself, instead of
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * being pointed to. The "Pointer" part of this name is a bit of a
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * misnomer, as nothing is pointed to.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * be embedded in the block pointer. The logic for this is handled in
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * the SPA, by the zio pipeline. Therefore most code outside the zio
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * pipeline doesn't need special-cases to handle these block pointers.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * See spa.h for details on the exact layout of embedded block pointers.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensencode_embedded_bp_compressed(blkptr_t *bp, void *data,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens enum zio_compress comp, int uncompressed_size, int compressed_size)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(uncompressed_size == compressed_size ||
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Encode the byte array into the words of the block pointer.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * First byte goes into low bits of first word (little endian).
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens for (int i = 0; i < compressed_size; i++) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BF64_SET(w, (i % sizeof (w)) * NBBY, NBBY, data8[i]);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (i % sizeof (w) == sizeof (w) - 1) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* we've reached the end of a word */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* write last partial word */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * more than BPE_PAYLOAD_SIZE bytes).
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensdecode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Decode the words of the block pointer into the byte array.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Low bits of first word are the first byte (little endian).
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens for (int i = 0; i < psize; i++) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (i % sizeof (w) == 0) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* beginning of a word */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);