5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens/*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * CDDL HEADER START
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *
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 *
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 * http://www.illumos.org/license/CDDL.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * CDDL HEADER END
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens/*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/zfs_context.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/zio.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/zio_compress.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens/*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Embedded-data Block Pointers
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *
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 *
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 *
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 *
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * See spa.h for details on the exact layout of embedded block pointers.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensvoid
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensencode_embedded_bp_compressed(blkptr_t *bp, void *data,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens enum zio_compress comp, int uncompressed_size, int compressed_size)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t *bp64 = (uint64_t *)bp;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t w = 0;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint8_t *data8 = data;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(uncompressed_size == compressed_size ||
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens comp != ZIO_COMPRESS_OFF);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT3U(comp, >=, ZIO_COMPRESS_OFF);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bzero(bp, sizeof (*bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_SET_EMBEDDED(bp, B_TRUE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_SET_COMPRESS(bp, comp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BPE_SET_LSIZE(bp, uncompressed_size);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BPE_SET_PSIZE(bp, compressed_size);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
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 */
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 ASSERT3P(bp64, <, bp + 1);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *bp64 = w;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bp64++;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (!BPE_IS_PAYLOADWORD(bp, bp64))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bp64++;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens w = 0;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* write last partial word */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (bp64 < (uint64_t *)(bp + 1))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *bp64 = w;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens/*
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 Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensvoid
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensdecode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens int psize;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint8_t *buf8 = buf;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t w = 0;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens const uint64_t *bp64 = (const uint64_t *)bp;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens psize = BPE_GET_PSIZE(bp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
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 */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens for (int i = 0; i < psize; i++) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (i % sizeof (w) == 0) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* beginning of a word */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT3P(bp64, <, bp + 1);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens w = *bp64;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bp64++;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (!BPE_IS_PAYLOADWORD(bp, bp64))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bp64++;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}