Skip to content

Commit 87bb538

Browse files
committed
Replace the way we store the compression level
Rather than using a 2nd hidden property, compress_level, we store the algorithm + bit shifted level in the regular compression property in the dataset properties ZAP. Introduce new macros: ZIO_COMPRESS_ALGO(raw) extracts the algorithm from the property ZIO_COMPRESS_LEVEL(raw) extracts the level from the property ZIO_COMPRESS_RAW(algo, level) returns the combined value This resolves issues around inheritence. All of the internal ZFS code 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. Signed-off-by: Allan Jude <allan@klarasystems.com>
1 parent 0b86a75 commit 87bb538

File tree

9 files changed

+27
-126
lines changed

9 files changed

+27
-126
lines changed

include/sys/fs/zfs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ typedef enum {
186186
ZFS_PROP_IVSET_GUID, /* not exposed to the user */
187187
ZFS_PROP_REDACTED,
188188
ZFS_PROP_REDACT_SNAPS,
189-
ZFS_PROP_COMPRESS_LEVEL, /* not exposed to the user */
190189
ZFS_NUM_PROPS
191190
} zfs_prop_t;
192191

include/sys/zio.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,13 @@ enum zio_encrypt {
163163
(compress) == ZIO_COMPRESS_ON || \
164164
(compress) == ZIO_COMPRESS_OFF)
165165

166-
#define ZIO_COMPLEVEL_ZSTD(level) \
167-
(ZIO_COMPRESS_ZSTD | ((level) << SPA_COMPRESSBITS))
166+
167+
#define ZIO_COMPRESS_ALGO(x) (x & SPA_COMPRESSMASK)
168+
#define ZIO_COMPRESS_LEVEL(x) ((x & ~SPA_COMPRESSMASK) >> SPA_COMPRESSBITS)
169+
#define ZIO_COMPRESS_RAW(type, level) (type | ((level) << SPA_COMPRESSBITS))
170+
171+
#define ZIO_COMPLEVEL_ZSTD(level) \
172+
ZIO_COMPRESS_RAW(ZIO_COMPRESS_ZSTD, level)
168173

169174
#define ZIO_FAILURE_MODE_WAIT 0
170175
#define ZIO_FAILURE_MODE_CONTINUE 1

include/sys/zio_compress.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ enum zio_compress {
5757
ZIO_COMPRESS_FUNCTIONS
5858
};
5959

60+
/* Compression algorithms that have levels */
61+
#define ZIO_COMPRESS_HASLEVEL(compress) ((compress == ZIO_COMPRESS_ZSTD || \
62+
(compress >= ZIO_COMPRESS_GZIP_1 && \
63+
compress <= ZIO_COMPRESS_GZIP_9)))
64+
6065
#define ZIO_COMPLEVEL_INHERIT 0
6166
#define ZIO_COMPLEVEL_DEFAULT 255
6267

module/zcommon/zfs_prop.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,6 @@ zfs_prop_init(void)
710710
zprop_register_hidden(ZFS_PROP_REMAPTXG, "remaptxg", PROP_TYPE_NUMBER,
711711
PROP_READONLY, ZFS_TYPE_DATASET, "REMAPTXG");
712712

713-
zprop_register_impl(ZFS_PROP_COMPRESS_LEVEL, "compress_level",
714-
PROP_TYPE_NUMBER, ZIO_COMPLEVEL_INHERIT, NULL, PROP_INHERIT,
715-
ZFS_TYPE_DATASET, "<level>", "COMPLEVEL", B_TRUE,
716-
B_FALSE, NULL);
717713
/* oddball properties */
718714
zprop_register_impl(ZFS_PROP_CREATION, "creation", PROP_TYPE_NUMBER, 0,
719715
NULL, PROP_READONLY, ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK,

module/zfs/dmu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,8 @@ dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
21262126
} else {
21272127
compress = zio_compress_select(os->os_spa, dn->dn_compress,
21282128
compress);
2129+
complevel = zio_complevel_select(os->os_spa, compress,
2130+
complevel, complevel);
21292131

21302132
checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
21312133
zio_checksum_select(dn->dn_checksum, checksum) :
@@ -2201,7 +2203,6 @@ dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
22012203
os->os_zpl_special_smallblock : 0;
22022204

22032205
ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);
2204-
ASSERT3U(zp->zp_complevel, !=, ZIO_COMPLEVEL_INHERIT);
22052206
}
22062207

22072208
/*

module/zfs/dmu_objset.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,9 @@ compression_changed_cb(void *arg, uint64_t newval)
196196
ASSERT(newval != ZIO_COMPRESS_INHERIT);
197197

198198
os->os_compress = zio_compress_select(os->os_spa,
199-
newval & SPA_COMPRESSMASK, ZIO_COMPRESS_ON);
200-
}
201-
202-
static void
203-
compress_level_changed_cb(void *arg, uint64_t newval)
204-
{
205-
objset_t *os = arg;
206-
199+
ZIO_COMPRESS_ALGO(newval), ZIO_COMPRESS_ON);
207200
os->os_complevel = zio_complevel_select(os->os_spa, os->os_compress,
208-
newval, ZIO_COMPLEVEL_DEFAULT);
201+
ZIO_COMPRESS_LEVEL(newval), ZIO_COMPLEVEL_DEFAULT);
209202
}
210203

211204
static void
@@ -540,11 +533,6 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
540533
zfs_prop_to_name(ZFS_PROP_COMPRESSION),
541534
compression_changed_cb, os);
542535
}
543-
if (err == 0) {
544-
err = dsl_prop_register(ds,
545-
zfs_prop_to_name(ZFS_PROP_COMPRESS_LEVEL),
546-
compress_level_changed_cb, os);
547-
}
548536
if (err == 0) {
549537
err = dsl_prop_register(ds,
550538
zfs_prop_to_name(ZFS_PROP_COPIES),

module/zfs/zcp_get.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
/*
1717
* Copyright (c) 2016 by Delphix. All rights reserved.
18-
* Copyright (c) 2019, Klara Inc.
19-
* Copyright (c) 2019, Allan Jude
2018
*/
2119

2220
#include <sys/lua/lua.h>
@@ -233,7 +231,6 @@ get_special_prop(lua_State *state, dsl_dataset_t *ds, const char *dsname,
233231
char setpoint[ZFS_MAX_DATASET_NAME_LEN] =
234232
"Internal error - setpoint not determined";
235233
zfs_type_t ds_type;
236-
const char *prop_name = zfs_prop_to_name(zfs_prop);
237234
zprop_type_t prop_type = zfs_prop_get_type(zfs_prop);
238235
(void) get_objset_type(ds, &ds_type);
239236

@@ -412,25 +409,7 @@ get_special_prop(lua_State *state, dsl_dataset_t *ds, const char *dsname,
412409
nvlist_free(nvl);
413410
break;
414411
}
415-
case ZFS_PROP_COMPRESSION:
416-
error = dsl_prop_get_ds(ds, prop_name, sizeof (numval), 1,
417-
&numval, setpoint);
418-
/* Special handling is only required for ZSTD */
419-
if (error || numval != ZIO_COMPRESS_ZSTD)
420-
break;
421-
422-
uint64_t levelval;
423-
const char *complevel_name =
424-
zfs_prop_to_name(ZFS_PROP_COMPRESS_LEVEL);
425412

426-
error = dsl_prop_get_ds(ds, complevel_name, sizeof (levelval),
427-
1, &levelval, setpoint);
428-
if (error == 0) {
429-
if (levelval == ZIO_COMPLEVEL_DEFAULT)
430-
break;
431-
numval |= levelval << SPA_COMPRESSBITS;
432-
}
433-
break;
434413
default:
435414
/* Did not match these props, check in the dsl_dir */
436415
error = get_dsl_dir_prop(ds, zfs_prop, &numval);

module/zfs/zfs_ioctl.c

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,34 +2043,6 @@ zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20432043
return (error);
20442044
}
20452045

2046-
static int
2047-
get_prop_uint64(nvlist_t *nv, const char *prop, nvlist_t **nvp,
2048-
uint64_t *val)
2049-
{
2050-
int err = 0;
2051-
nvlist_t *subnv;
2052-
nvpair_t *pair;
2053-
nvpair_t *propval;
2054-
2055-
if (nvlist_lookup_nvpair(nv, prop, &pair) != 0)
2056-
return (EINVAL);
2057-
2058-
/* decode the property value */
2059-
propval = pair;
2060-
if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2061-
subnv = fnvpair_value_nvlist(pair);
2062-
if (nvp != NULL)
2063-
*nvp = subnv;
2064-
if (nvlist_lookup_nvpair(subnv, ZPROP_VALUE, &propval) != 0)
2065-
err = EINVAL;
2066-
}
2067-
if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2068-
*val = fnvpair_value_uint64(propval);
2069-
}
2070-
2071-
return (err);
2072-
}
2073-
20742046
static int
20752047
zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
20762048
{
@@ -2098,28 +2070,6 @@ zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
20982070
}
20992071
VERIFY0(error);
21002072
}
2101-
/*
2102-
* ZSTD stores the compression level in a separate hidden
2103-
* property to avoid using up a large number of bits in the
2104-
* on-disk compression algorithm enum. We need to swap things
2105-
* back around when the property is read.
2106-
*/
2107-
nvlist_t *cnv;
2108-
uint64_t compval, levelval;
2109-
2110-
if (get_prop_uint64(nv, "compression", &cnv, &compval) != 0)
2111-
compval = ZIO_COMPRESS_INHERIT;
2112-
2113-
if (error == 0 && compval == ZIO_COMPRESS_ZSTD &&
2114-
get_prop_uint64(nv, "compress_level", NULL,
2115-
&levelval) == 0) {
2116-
if (levelval == ZIO_COMPLEVEL_DEFAULT)
2117-
levelval = 0;
2118-
fnvlist_remove(cnv, ZPROP_VALUE);
2119-
fnvlist_add_uint64(cnv, ZPROP_VALUE,
2120-
compval | (levelval << SPA_COMPRESSBITS));
2121-
}
2122-
21232073
if (error == 0)
21242074
error = put_nvlist(zc, nv);
21252075
nvlist_free(nv);
@@ -2563,32 +2513,6 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
25632513
}
25642514
break;
25652515
}
2566-
case ZFS_PROP_COMPRESSION:
2567-
/* Special handling is only required for ZSTD */
2568-
if ((intval & SPA_COMPRESSMASK) != ZIO_COMPRESS_ZSTD) {
2569-
err = -1;
2570-
break;
2571-
}
2572-
/*
2573-
* Store the ZSTD compression level separate from the compress
2574-
* property in its own hidden property.
2575-
*/
2576-
uint64_t levelval;
2577-
2578-
if (intval == ZIO_COMPRESS_ZSTD) {
2579-
levelval = ZIO_COMPLEVEL_DEFAULT;
2580-
} else {
2581-
levelval = (intval & ~SPA_COMPRESSMASK)
2582-
>> SPA_COMPRESSBITS;
2583-
}
2584-
err = dsl_prop_set_int(dsname, "compress_level", source,
2585-
levelval);
2586-
if (err == 0) {
2587-
/* Store the compression algorithm normally */
2588-
err = dsl_prop_set_int(dsname, propname, source,
2589-
intval & SPA_COMPRESSMASK);
2590-
}
2591-
break;
25922516
default:
25932517
err = -1;
25942518
}
@@ -4448,7 +4372,7 @@ zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
44484372
const char *propname = nvpair_name(pair);
44494373
boolean_t issnap = (strchr(dsname, '@') != NULL);
44504374
zfs_prop_t prop = zfs_name_to_prop(propname);
4451-
uint64_t intval;
4375+
uint64_t intval, compval;
44524376
int err;
44534377

44544378
if (prop == ZPROP_INVAL) {
@@ -4530,19 +4454,20 @@ zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
45304454
* we'll catch them later.
45314455
*/
45324456
if (nvpair_value_uint64(pair, &intval) == 0) {
4533-
if (intval >= ZIO_COMPRESS_GZIP_1 &&
4534-
intval <= ZIO_COMPRESS_GZIP_9 &&
4457+
compval = ZIO_COMPRESS_ALGO(intval);
4458+
if (compval >= ZIO_COMPRESS_GZIP_1 &&
4459+
compval <= ZIO_COMPRESS_GZIP_9 &&
45354460
zfs_earlier_version(dsname,
45364461
SPA_VERSION_GZIP_COMPRESSION)) {
45374462
return (SET_ERROR(ENOTSUP));
45384463
}
45394464

4540-
if (intval == ZIO_COMPRESS_ZLE &&
4465+
if (compval == ZIO_COMPRESS_ZLE &&
45414466
zfs_earlier_version(dsname,
45424467
SPA_VERSION_ZLE_COMPRESSION))
45434468
return (SET_ERROR(ENOTSUP));
45444469

4545-
if (intval == ZIO_COMPRESS_LZ4) {
4470+
if (compval == ZIO_COMPRESS_LZ4) {
45464471
spa_t *spa;
45474472

45484473
if ((err = spa_open(dsname, &spa, FTAG)) != 0)
@@ -4556,7 +4481,7 @@ zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
45564481
spa_close(spa, FTAG);
45574482
}
45584483

4559-
if (intval == ZIO_COMPRESS_ZSTD) {
4484+
if (compval == ZIO_COMPRESS_ZSTD) {
45604485
spa_t *spa;
45614486

45624487
if ((err = spa_open(dsname, &spa, FTAG)) != 0)
@@ -4578,7 +4503,7 @@ zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
45784503
* implies a downrev pool version.
45794504
*/
45804505
if (zfs_is_bootfs(dsname) &&
4581-
!BOOTFS_COMPRESS_VALID(intval)) {
4506+
!BOOTFS_COMPRESS_VALID(compval)) {
45824507
return (SET_ERROR(ERANGE));
45834508
}
45844509
}

module/zfs/zio_compress.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
7676
{
7777
uint8_t result;
7878

79+
if (!ZIO_COMPRESS_HASLEVEL(compress))
80+
return (0);
81+
7982
result = child;
8083
if (result == ZIO_COMPLEVEL_INHERIT)
8184
result = parent;

0 commit comments

Comments
 (0)