Compress block using zstd */
| 363 | |
| 364 | /* Compress block using zstd */ |
| 365 | size_t |
| 366 | zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, |
| 367 | int level) |
| 368 | { |
| 369 | size_t c_len; |
| 370 | int16_t zstd_level; |
| 371 | zfs_zstdhdr_t *hdr; |
| 372 | ZSTD_CCtx *cctx; |
| 373 | |
| 374 | hdr = (zfs_zstdhdr_t *)d_start; |
| 375 | |
| 376 | /* Skip compression if the specified level is invalid */ |
| 377 | if (zstd_enum_to_level(level, &zstd_level)) { |
| 378 | ZSTDSTAT_BUMP(zstd_stat_com_inval); |
| 379 | return (s_len); |
| 380 | } |
| 381 | |
| 382 | ASSERT3U(d_len, >=, sizeof (*hdr)); |
| 383 | ASSERT3U(d_len, <=, s_len); |
| 384 | ASSERT3U(zstd_level, !=, 0); |
| 385 | |
| 386 | cctx = ZSTD_createCCtx_advanced(zstd_malloc); |
| 387 | |
| 388 | /* |
| 389 | * Out of kernel memory, gently fall through - this will disable |
| 390 | * compression in zio_compress_data |
| 391 | */ |
| 392 | if (!cctx) { |
| 393 | ZSTDSTAT_BUMP(zstd_stat_com_alloc_fail); |
| 394 | return (s_len); |
| 395 | } |
| 396 | |
| 397 | /* Set the compression level */ |
| 398 | ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, zstd_level); |
| 399 | |
| 400 | /* Use the "magicless" zstd header which saves us 4 header bytes */ |
| 401 | ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless); |
| 402 | |
| 403 | /* |
| 404 | * Disable redundant checksum calculation and content size storage since |
| 405 | * this is already done by ZFS itself. |
| 406 | */ |
| 407 | ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0); |
| 408 | ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0); |
| 409 | |
| 410 | c_len = ZSTD_compress2(cctx, |
| 411 | hdr->data, |
| 412 | d_len - sizeof (*hdr), |
| 413 | s_start, s_len); |
| 414 | |
| 415 | ZSTD_freeCCtx(cctx); |
| 416 | |
| 417 | /* Error in the compression routine, disable compression. */ |
| 418 | if (ZSTD_isError(c_len)) { |
| 419 | /* |
| 420 | * If we are aborting the compression because the saves are |
| 421 | * too small, that is not a failure. Everything else is a |
| 422 | * failure, so increment the compression failure counter. |
nothing calls this directly
no test coverage detected