abd_t *pabd =
abd_get_from_buf_struct(&abd, buf, bufsz);
size_t csize = zio_compress_data(ctype, &dabd,
- &pabd, drrw->drr_logical_size, level);
+ &pabd, drrw->drr_logical_size,
+ drrw->drr_logical_size, level);
size_t rounded =
P2ROUNDUP(csize, SPA_MINBLOCKSIZE);
if (rounded >= drrw->drr_logical_size) {
* Copyright (c) 2019, Allan Jude
* Copyright (c) 2019, 2023, 2024, Klara Inc.
* Copyright (c) 2019-2020, Michael Niewöhner
+ * Copyright (c) 2024 by George Melikov. All rights reserved.
*/
#ifndef _ZIO_H
extern void zio_flush(zio_t *zio, vdev_t *vd);
extern void zio_shrink(zio_t *zio, uint64_t size);
+extern size_t zio_get_compression_max_size(uint64_t gcd_alloc,
+ uint64_t min_alloc, size_t s_len);
extern int zio_wait(zio_t *zio);
extern void zio_nowait(zio_t *zio);
extern void zio_execute(void *zio);
* Copyright (c) 2019, 2024, Klara, Inc.
* Use is subject to license terms.
* Copyright (c) 2015, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
*/
#ifndef _SYS_ZIO_COMPRESS_H
* Compress and decompress data if necessary.
*/
extern size_t zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst,
- size_t s_len, uint8_t level);
+ size_t s_len, size_t d_len, uint8_t level);
extern int zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *abd,
size_t s_len, size_t d_len, uint8_t *level);
extern int zio_compress_to_feature(enum zio_compress comp);
When a zero-filled block is detected, it is stored as
a hole and not compressed using the indicated compression algorithm.
.Pp
-Any block being compressed must be no larger than 7/8 of its original size
-after compression, otherwise the compression will not be considered worthwhile
-and the block saved uncompressed.
-Note that when the logical block is less than
-8 times the disk sector size this effectively reduces the necessary compression
-ratio; for example, 8 KiB blocks on disks with 4 KiB disk sectors must compress
-to 1/2
-or less of their original size.
+All blocks are allocated as a whole number of sectors
+.Pq chunks of 2^ Ns Sy ashift No bytes , e.g . Sy 512B No or Sy 4KB .
+Compression may result in a non-sector-aligned size, which will be rounded up
+to a whole number of sectors.
+If compression saves less than one whole sector,
+the block will be stored uncompressed.
+Therefore, blocks whose logical size is a small number of sectors will
+experience less compression
+(e.g. for
+.Sy recordsize Ns = Ns Sy 16K
+with
+.Sy 4K
+sectors, which have 4 sectors per block,
+compression needs to save at least 25% to actually save space on disk).
+.Pp
+There is
+.Sy 12.5%
+default compression threshold in addition to sector rounding.
.It Xo
.Sy context Ns = Ns Sy none Ns | Ns
.Ar SELinux-User : Ns Ar SELinux-Role : Ns Ar SELinux-Type : Ns Ar Sensitivity-Level
* Copyright (c) 2019, 2023, Klara Inc.
* Copyright (c) 2019, Allan Jude
* Copyright (c) 2020, The FreeBSD Foundation [1]
+ * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
*
* [1] Portions of this software were developed by Allan Jude
* under sponsorship from the FreeBSD Foundation.
!HDR_COMPRESSION_ENABLED(hdr)) {
abd = NULL;
csize = zio_compress_data(HDR_GET_COMPRESS(hdr),
- hdr->b_l1hdr.b_pabd, &abd, lsize, hdr->b_complevel);
+ hdr->b_l1hdr.b_pabd, &abd, lsize, lsize, hdr->b_complevel);
ASSERT3P(abd, !=, NULL);
ASSERT3U(csize, <=, psize);
abd_zero_off(abd, csize, psize - csize);
if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) {
cabd = abd_alloc_for_io(MAX(size, asize), ismd);
uint64_t csize = zio_compress_data(compress, to_write, &cabd,
- size, hdr->b_complevel);
- if (csize > psize) {
+ size, MIN(size, psize), hdr->b_complevel);
+ if (csize >= size || csize > psize) {
/*
* We can't re-compress the block into the original
* psize. Even if it fits into asize, it does not
*/
list_insert_tail(&cb->l2wcb_abd_list, abd_buf);
- /* try to compress the buffer */
+ /* try to compress the buffer, at least one sector to save */
psize = zio_compress_data(ZIO_COMPRESS_LZ4,
- abd_buf->abd, &abd, sizeof (*lb), 0);
+ abd_buf->abd, &abd, sizeof (*lb),
+ zio_get_compression_max_size(dev->l2ad_vdev->vdev_ashift,
+ dev->l2ad_vdev->vdev_ashift, sizeof (*lb)), 0);
/* a log block is never entirely zero */
ASSERT(psize != 0);
abd_t *cabd = abd_alloc_linear(BP_GET_PSIZE(bp),
B_FALSE);
uint64_t csize = zio_compress_data(BP_GET_COMPRESS(bp),
- abd, &cabd, abd_get_size(abd),
+ abd, &cabd, abd_get_size(abd), BP_GET_PSIZE(bp),
rwa->os->os_complevel);
abd_zero_off(cabd, csize, BP_GET_PSIZE(bp) - csize);
/* Swap in newly compressed data into the abd */
* Copyright (c) 2019, 2023, 2024, Klara Inc.
* Copyright (c) 2019, Allan Jude
* Copyright (c) 2021, Datto, Inc.
+ * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
*/
#include <sys/sysmacros.h>
return (spa->spa_min_alloc);
}
+size_t
+zio_get_compression_max_size(uint64_t gcd_alloc, uint64_t min_alloc,
+ size_t s_len)
+{
+ size_t d_len;
+
+ /* minimum 12.5% must be saved (legacy value, may be changed later) */
+ d_len = s_len - (s_len >> 3);
+
+ d_len = d_len - d_len % gcd_alloc;
+ if (d_len < min_alloc)
+ return (BPE_PAYLOAD_SIZE);
+ return (d_len);
+}
+
/*
* ==========================================================================
* Prepare to read and write logical blocks
psize = lsize;
else
psize = zio_compress_data(compress, zio->io_abd, &cabd,
- lsize, zp->zp_complevel);
+ lsize,
+ zio_get_compression_max_size(spa->spa_gcd_alloc,
+ spa->spa_min_alloc, lsize),
+ zp->zp_complevel);
if (psize == 0) {
compress = ZIO_COMPRESS_OFF;
} else if (psize >= lsize) {
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
- */
-/*
* Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
- */
-
-/*
* Copyright (c) 2013, 2018 by Delphix. All rights reserved.
* Copyright (c) 2019, 2024, Klara, Inc.
* Copyright (c) 2019, Allan Jude
+ * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
*/
#include <sys/zfs_context.h>
size_t
zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
- uint8_t level)
+ size_t d_len, uint8_t level)
{
- size_t c_len, d_len;
+ size_t c_len;
uint8_t complevel;
zio_compress_info_t *ci = &zio_compress_table[c];
if (*dst == NULL)
*dst = abd_alloc_sametype(src, s_len);
- /* Compress at least 12.5%, but limit to the size of the dest abd. */
- d_len = MIN(s_len - (s_len >> 3), abd_get_size(*dst));
-
c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
if (c_len > d_len)
return (s_len);
- ASSERT3U(c_len, <=, d_len);
return (c_len);
}