mirror_zfs/module/zfs/blkptr.c

154 lines
4.3 KiB
C
Raw Normal View History

/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2013, 2016 by Delphix. All rights reserved.
*/
#include <sys/blkptr.h>
#include <sys/zfs_context.h>
#include <sys/zio.h>
#include <sys/zio_compress.h>
/*
* Embedded-data Block Pointers
*
* Normally, block pointers point (via their DVAs) to a block which holds data.
* If the data that we need to store is very small, this is an inefficient
* use of space, because a block must be at minimum 1 sector (typically 512
* bytes or 4KB). Additionally, reading these small blocks tends to generate
* more random reads.
*
* Embedded-data Block Pointers allow small pieces of data (the "payload",
* up to 112 bytes) to be stored in the block pointer itself, instead of
* being pointed to. The "Pointer" part of this name is a bit of a
* misnomer, as nothing is pointed to.
*
* BP_EMBEDDED_TYPE_DATA block pointers allow highly-compressible data to
* be embedded in the block pointer. The logic for this is handled in
* the SPA, by the zio pipeline. Therefore most code outside the zio
* pipeline doesn't need special-cases to handle these block pointers.
*
* See spa.h for details on the exact layout of embedded block pointers.
*/
void
encode_embedded_bp_compressed(blkptr_t *bp, void *data,
enum zio_compress comp, int uncompressed_size, int compressed_size)
{
uint64_t *bp64 = (uint64_t *)bp;
uint64_t w = 0;
uint8_t *data8 = data;
ASSERT3U(compressed_size, <=, BPE_PAYLOAD_SIZE);
ASSERT(uncompressed_size == compressed_size ||
comp != ZIO_COMPRESS_OFF);
ASSERT3U(comp, >=, ZIO_COMPRESS_OFF);
ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
memset(bp, 0, sizeof (*bp));
BP_SET_EMBEDDED(bp, B_TRUE);
BP_SET_COMPRESS(bp, comp);
BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
BPE_SET_LSIZE(bp, uncompressed_size);
BPE_SET_PSIZE(bp, compressed_size);
/*
* Encode the byte array into the words of the block pointer.
* First byte goes into low bits of first word (little endian).
*/
for (int i = 0; i < compressed_size; i++) {
BF64_SET(w, (i % sizeof (w)) * NBBY, NBBY, data8[i]);
if (i % sizeof (w) == sizeof (w) - 1) {
/* we've reached the end of a word */
ASSERT3P(bp64, <, bp + 1);
*bp64 = w;
bp64++;
if (!BPE_IS_PAYLOADWORD(bp, bp64))
bp64++;
w = 0;
}
}
/* write last partial word */
if (bp64 < (uint64_t *)(bp + 1))
*bp64 = w;
}
/*
* buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
* more than BPE_PAYLOAD_SIZE bytes).
*/
void
decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
{
int psize;
uint8_t *buf8 = buf;
uint64_t w = 0;
const uint64_t *bp64 = (const uint64_t *)bp;
ASSERT(BP_IS_EMBEDDED(bp));
psize = BPE_GET_PSIZE(bp);
/*
* Decode the words of the block pointer into the byte array.
* Low bits of first word are the first byte (little endian).
*/
for (int i = 0; i < psize; i++) {
if (i % sizeof (w) == 0) {
/* beginning of a word */
ASSERT3P(bp64, <, bp + 1);
w = *bp64;
bp64++;
if (!BPE_IS_PAYLOADWORD(bp, bp64))
bp64++;
}
buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
}
}
/*
* Fill in the buffer with the (decompressed) payload of the embedded
* blkptr_t. Takes into account compression and byteorder (the payload is
* treated as a stream of bytes).
* Return 0 on success, or ENOSPC if it won't fit in the buffer.
*/
int
decode_embedded_bp(const blkptr_t *bp, void *buf, int buflen)
{
int lsize, psize;
ASSERT(BP_IS_EMBEDDED(bp));
lsize = BPE_GET_LSIZE(bp);
psize = BPE_GET_PSIZE(bp);
if (lsize > buflen)
return (SET_ERROR(ENOSPC));
ASSERT3U(lsize, ==, buflen);
if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
uint8_t dstbuf[BPE_PAYLOAD_SIZE];
decode_embedded_bp_compressed(bp, dstbuf);
VERIFY0(zio_decompress_data_buf(BP_GET_COMPRESS(bp),
Add zstd support to zfs This PR adds two new compression types, based on ZStandard: - zstd: A basic ZStandard compression algorithm Available compression. Levels for zstd are zstd-1 through zstd-19, where the compression increases with every level, but speed decreases. - zstd-fast: A faster version of the ZStandard compression algorithm zstd-fast is basically a "negative" level of zstd. The compression decreases with every level, but speed increases. Available compression levels for zstd-fast: - zstd-fast-1 through zstd-fast-10 - zstd-fast-20 through zstd-fast-100 (in increments of 10) - zstd-fast-500 and zstd-fast-1000 For more information check the man page. Implementation details: Rather than treat each level of zstd as a different algorithm (as was done historically with gzip), the block pointer `enum zio_compress` value is simply zstd for all levels, including zstd-fast, since they all use the same decompression function. The compress= property (a 64bit unsigned integer) uses the lower 7 bits to store the compression algorithm (matching the number of bits used in a block pointer, as the 8th bit was borrowed for embedded block pointers). The upper bits are used to store the compression level. It is necessary to be able to determine what compression level was used when later reading a block back, so the concept used in LZ4, where the first 32bits of the on-disk value are the size of the compressed data (since the allocation is rounded up to the nearest ashift), was extended, and we store the version of ZSTD and the level as well as the compressed size. This value is returned when decompressing a block, so that if the block needs to be recompressed (L2ARC, nop-write, etc), that the same parameters will be used to result in the matching checksum. All of the internal ZFS code ( `arc_buf_hdr_t`, `objset_t`, `zio_prop_t`, etc.) uses the separated _compress and _complevel variables. Only the properties ZAP contains the combined/bit-shifted value. The combined value is split when the compression_changed_cb() callback is called, and sets both objset members (os_compress and os_complevel). The userspace tools all use the combined/bit-shifted value. Additional notes: zdb can now also decode the ZSTD compression header (flag -Z) and inspect the size, version and compression level saved in that header. For each record, if it is ZSTD compressed, the parameters of the decoded compression header get printed. ZSTD is included with all current tests and new tests are added as-needed. Per-dataset feature flags now get activated when the property is set. If a compression algorithm requires a feature flag, zfs activates the feature when the property is set, rather than waiting for the first block to be born. This is currently only used by zstd but can be extended as needed. Portions-Sponsored-By: The FreeBSD Foundation Co-authored-by: Allan Jude <allanjude@freebsd.org> Co-authored-by: Brian Behlendorf <behlendorf1@llnl.gov> Co-authored-by: Sebastian Gottschall <s.gottschall@dd-wrt.com> Co-authored-by: Kjeld Schouten-Lebbing <kjeld@schouten-lebbing.nl> Co-authored-by: Michael Niewöhner <foss@mniewoehner.de> Signed-off-by: Allan Jude <allan@klarasystems.com> Signed-off-by: Allan Jude <allanjude@freebsd.org> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Sebastian Gottschall <s.gottschall@dd-wrt.com> Signed-off-by: Kjeld Schouten-Lebbing <kjeld@schouten-lebbing.nl> Signed-off-by: Michael Niewöhner <foss@mniewoehner.de> Closes #6247 Closes #9024 Closes #10277 Closes #10278
2020-08-18 20:10:17 +03:00
dstbuf, buf, psize, buflen, NULL));
} else {
ASSERT3U(lsize, ==, psize);
decode_embedded_bp_compressed(bp, buf);
}
return (0);
}