Path: blob/main/sys/contrib/openzfs/module/zfs/blkptr.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*17* Copyright (c) 2013, 2016 by Delphix. All rights reserved.18*/1920#include <sys/blkptr.h>21#include <sys/zfs_context.h>22#include <sys/zio.h>23#include <sys/zio_compress.h>2425/*26* Embedded-data Block Pointers27*28* Normally, block pointers point (via their DVAs) to a block which holds data.29* If the data that we need to store is very small, this is an inefficient30* use of space, because a block must be at minimum 1 sector (typically 51231* bytes or 4KB). Additionally, reading these small blocks tends to generate32* more random reads.33*34* Embedded-data Block Pointers allow small pieces of data (the "payload",35* up to 112 bytes) to be stored in the block pointer itself, instead of36* being pointed to. The "Pointer" part of this name is a bit of a37* misnomer, as nothing is pointed to.38*39* BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to40* be embedded in the block pointer. The logic for this is handled in41* the SPA, by the zio pipeline. Therefore most code outside the zio42* pipeline doesn't need special-cases to handle these block pointers.43*44* See spa.h for details on the exact layout of embedded block pointers.45*/4647void48encode_embedded_bp_compressed(blkptr_t *bp, void *data,49enum zio_compress comp, int uncompressed_size, int compressed_size)50{51uint64_t *bp64 = (uint64_t *)bp;52uint64_t w = 0;53uint8_t *data8 = data;5455ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);56ASSERT(uncompressed_size == compressed_size ||57comp != ZIO_COMPRESS_OFF);58ASSERT3U(comp, >=, ZIO_COMPRESS_OFF);59ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);6061memset(bp, 0, sizeof (*bp));62BP_SET_EMBEDDED(bp, B_TRUE);63BP_SET_COMPRESS(bp, comp);64BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);65BPE_SET_LSIZE(bp, uncompressed_size);66BPE_SET_PSIZE(bp, compressed_size);6768/*69* Encode the byte array into the words of the block pointer.70* First byte goes into low bits of first word (little endian).71*/72for (int i = 0; i < compressed_size; i++) {73BF64_SET(w, (i % sizeof (w)) * NBBY, NBBY, data8[i]);74if (i % sizeof (w) == sizeof (w) - 1) {75/* we've reached the end of a word */76ASSERT3P(bp64, <, bp + 1);77*bp64 = w;78bp64++;79if (!BPE_IS_PAYLOADWORD(bp, bp64))80bp64++;81w = 0;82}83}84/* write last partial word */85if (bp64 < (uint64_t *)(bp + 1))86*bp64 = w;87}8889/*90* buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be91* more than BPE_PAYLOAD_SIZE bytes).92*/93void94decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)95{96int psize;97uint8_t *buf8 = buf;98uint64_t w = 0;99const uint64_t *bp64 = (const uint64_t *)bp;100101ASSERT(BP_IS_EMBEDDED(bp));102103psize = BPE_GET_PSIZE(bp);104105/*106* Decode the words of the block pointer into the byte array.107* Low bits of first word are the first byte (little endian).108*/109for (int i = 0; i < psize; i++) {110if (i % sizeof (w) == 0) {111/* beginning of a word */112ASSERT3P(bp64, <, bp + 1);113w = *bp64;114bp64++;115if (!BPE_IS_PAYLOADWORD(bp, bp64))116bp64++;117}118buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);119}120}121122/*123* Fill in the buffer with the (decompressed) payload of the embedded124* blkptr_t. Takes into account compression and byteorder (the payload is125* treated as a stream of bytes).126* Return 0 on success, or ENOSPC if it won't fit in the buffer.127*/128int129decode_embedded_bp(const blkptr_t *bp, void *buf, int buflen)130{131int lsize, psize;132133ASSERT(BP_IS_EMBEDDED(bp));134135lsize = BPE_GET_LSIZE(bp);136psize = BPE_GET_PSIZE(bp);137138if (lsize > buflen)139return (SET_ERROR(ENOSPC));140ASSERT3U(lsize, ==, buflen);141142if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {143uint8_t dstbuf[BPE_PAYLOAD_SIZE];144decode_embedded_bp_compressed(bp, dstbuf);145abd_t cabd, dabd;146abd_get_from_buf_struct(&cabd, dstbuf, psize);147abd_get_from_buf_struct(&dabd, buf, buflen);148VERIFY0(zio_decompress_data(BP_GET_COMPRESS(bp), &cabd,149&dabd, psize, buflen, NULL));150abd_free(&dabd);151abd_free(&cabd);152} else {153ASSERT3U(lsize, ==, psize);154decode_embedded_bp_compressed(bp, buf);155}156157return (0);158}159160161