Path: blob/main/sys/contrib/openzfs/module/zfs/blake3_zfs.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2022 Tino Reichardt <[email protected]>24*/2526#include <sys/zfs_context.h>27#include <sys/zio_checksum.h>28#include <sys/blake3.h>29#include <sys/abd.h>3031static int32blake3_incremental(void *buf, size_t size, void *arg)33{34BLAKE3_CTX *ctx = arg;3536Blake3_Update(ctx, buf, size);3738return (0);39}4041/*42* Computes a native 256-bit BLAKE3 MAC checksum. Please note that this43* function requires the presence of a ctx_template that should be allocated44* using abd_checksum_blake3_tmpl_init.45*/46void47abd_checksum_blake3_native(abd_t *abd, uint64_t size, const void *ctx_template,48zio_cksum_t *zcp)49{50ASSERT(ctx_template != NULL);5152#if defined(_KERNEL)53kpreempt_disable();54BLAKE3_CTX *ctx = blake3_per_cpu_ctx[CPU_SEQID];55#else56BLAKE3_CTX *ctx = kmem_alloc(sizeof (*ctx), KM_SLEEP);57#endif5859memcpy(ctx, ctx_template, sizeof (*ctx));60(void) abd_iterate_func(abd, 0, size, blake3_incremental, ctx);61Blake3_Final(ctx, (uint8_t *)zcp);6263#if defined(_KERNEL)64kpreempt_enable();65#else66memset(ctx, 0, sizeof (*ctx));67kmem_free(ctx, sizeof (*ctx));68#endif69}7071/*72* Byteswapped version of abd_checksum_blake3_native. This just invokes73* the native checksum function and byteswaps the resulting checksum (since74* BLAKE3 is internally endian-insensitive).75*/76void77abd_checksum_blake3_byteswap(abd_t *abd, uint64_t size,78const void *ctx_template, zio_cksum_t *zcp)79{80zio_cksum_t tmp;8182ASSERT(ctx_template != NULL);8384abd_checksum_blake3_native(abd, size, ctx_template, &tmp);85zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);86zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);87zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);88zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);89}9091/*92* Allocates a BLAKE3 MAC template suitable for using in BLAKE3 MAC checksum93* computations and returns a pointer to it.94*/95void *96abd_checksum_blake3_tmpl_init(const zio_cksum_salt_t *salt)97{98BLAKE3_CTX *ctx;99100ASSERT(sizeof (salt->zcs_bytes) == 32);101102/* init reference object */103ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);104Blake3_InitKeyed(ctx, salt->zcs_bytes);105106return (ctx);107}108109/*110* Frees a BLAKE3 context template previously allocated using111* zio_checksum_blake3_tmpl_init.112*/113void114abd_checksum_blake3_tmpl_free(void *ctx_template)115{116BLAKE3_CTX *ctx = ctx_template;117118memset(ctx, 0, sizeof (*ctx));119kmem_free(ctx, sizeof (*ctx));120}121122123