Skip to content

Commit

Permalink
compress: add "slack" compression option
Browse files Browse the repository at this point in the history
The "slack" option simply searches from the end of the block backwards
to the last non-zero byte, and sets that position as the "compressed"
size.

This patch is highly experimental; please see the associated PR for
discussion.

Signed-off-by: Rob Norris <[email protected]>
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
  • Loading branch information
robn committed Aug 27, 2023
1 parent bee9cfb commit d58de83
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/sys/zio_compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum zio_compress {
ZIO_COMPRESS_ZLE,
ZIO_COMPRESS_LZ4,
ZIO_COMPRESS_ZSTD,
ZIO_COMPRESS_SLACK,
ZIO_COMPRESS_FUNCTIONS
};

Expand Down Expand Up @@ -179,6 +180,8 @@ extern size_t lz4_compress_zfs(void *src, void *dst, size_t s_len, size_t d_len,
int level);
extern int lz4_decompress_zfs(void *src, void *dst, size_t s_len, size_t d_len,
int level);
extern size_t slack_compress(void *src, void *dst, size_t s_len, size_t d_len,
int level);

/*
* Compress and decompress data if necessary.
Expand Down
1 change: 1 addition & 0 deletions include/zfeature_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef enum spa_feature {
SPA_FEATURE_BLOCK_CLONING,
SPA_FEATURE_AVZ_V2,
SPA_FEATURE_REDACTION_LIST_SPILL,
SPA_FEATURE_SLACK_COMPRESS,
SPA_FEATURES
} spa_feature_t;

Expand Down
1 change: 1 addition & 0 deletions lib/libzpool/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ nodist_libzpool_la_SOURCES = \
module/zfs/rrwlock.c \
module/zfs/sa.c \
module/zfs/sha2_zfs.c \
module/zfs/slack.c \
module/zfs/skein_zfs.c \
module/zfs/spa.c \
module/zfs/spa_checkpoint.c \
Expand Down
1 change: 1 addition & 0 deletions module/Kbuild.in
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ ZFS_OBJS := \
sa.o \
sha2_zfs.o \
skein_zfs.o \
slack.o \
spa.o \
spa_checkpoint.o \
spa_config.o \
Expand Down
1 change: 1 addition & 0 deletions module/Makefile.bsd
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ SRCS+= abd.c \
sa.c \
sha2_zfs.c \
skein_zfs.c \
slack.c \
spa.c \
spa_checkpoint.c \
spa_config.c \
Expand Down
14 changes: 14 additions & 0 deletions module/zcommon/zfeature_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,20 @@ zpool_feature_init(void)
redact_list_spill_deps, sfeatures);
}

{
{
static const spa_feature_t slack_deps[] = {
SPA_FEATURE_EXTENSIBLE_DATASET,
SPA_FEATURE_NONE
};
zfeature_register(SPA_FEATURE_SLACK_COMPRESS,
"com.klarasystems:slack_compress", "slack_compress",
"slack compression support",
ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
slack_deps, sfeatures);
}
}

zfs_mod_list_supported_free(sfeatures);
}

Expand Down
1 change: 1 addition & 0 deletions module/zcommon/zfs_prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ zfs_prop_init(void)
{ "gzip-9", ZIO_COMPRESS_GZIP_9 },
{ "zle", ZIO_COMPRESS_ZLE },
{ "lz4", ZIO_COMPRESS_LZ4 },
{ "slack", ZIO_COMPRESS_SLACK },
{ "zstd", ZIO_COMPRESS_ZSTD },
{ "zstd-fast",
ZIO_COMPLEVEL_ZSTD(ZIO_ZSTD_LEVEL_FAST_DEFAULT) },
Expand Down
58 changes: 58 additions & 0 deletions module/zfs/slack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2023, Klara Inc.
*/

#include <sys/zio_compress.h>
#include <sys/types.h>

/*
* Slack compression simply searches for the last non-zero byte in the buffer,
* and sets the position as the size of the "compressed" data.
*/

size_t
slack_compress(void *src, void *dst, size_t s_len, size_t d_len, int level)
{
(void) level;

ASSERT3U(s_len, >, 0);
ASSERT0(P2PHASE(s_len, sizeof (uint64_t)));

uint64_t *buf = (uint64_t *)src;

int p = (s_len / sizeof (uint64_t)) - 1;
for (; p >= 0; p--)
if (buf[p] != 0)
break;

if (p < 0)
return (s_len);

size_t c_len = (p + 1) * sizeof (uint64_t);
if (c_len > d_len)
return (s_len);

memcpy(dst, src, c_len);
return (c_len);
}
14 changes: 14 additions & 0 deletions module/zfs/zfs_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4653,6 +4653,20 @@ zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
}
spa_close(spa, FTAG);
}

if (compval == ZIO_COMPRESS_SLACK) {
spa_t *spa;

if ((err = spa_open(dsname, &spa, FTAG)) != 0)
return (err);

if (!spa_feature_is_enabled(spa,
SPA_FEATURE_SLACK_COMPRESS)) {
spa_close(spa, FTAG);
return (SET_ERROR(ENOTSUP));
}
spa_close(spa, FTAG);
}
}
break;

Expand Down
3 changes: 3 additions & 0 deletions module/zfs/zio_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
{"lz4", 0, lz4_compress_zfs, lz4_decompress_zfs, NULL},
{"zstd", ZIO_ZSTD_LEVEL_DEFAULT, zfs_zstd_compress_wrap,
zfs_zstd_decompress, zfs_zstd_decompress_level},
{"slack", 0, slack_compress, NULL, NULL },
};

uint8_t
Expand Down Expand Up @@ -218,6 +219,8 @@ zio_compress_to_feature(enum zio_compress comp)
switch (comp) {
case ZIO_COMPRESS_ZSTD:
return (SPA_FEATURE_ZSTD_COMPRESS);
case ZIO_COMPRESS_SLACK:
return (SPA_FEATURE_SLACK_COMPRESS);
default:
break;
}
Expand Down

0 comments on commit d58de83

Please sign in to comment.