Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/zfs/blake3_zfs.c
48383 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* The contents of this file are subject to the terms of the
6
* Common Development and Distribution License (the "License").
7
* You may not use this file except in compliance with the License.
8
*
9
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10
* or http://opensource.org/licenses/CDDL-1.0.
11
* See the License for the specific language governing permissions
12
* and limitations under the License.
13
*
14
* When distributing Covered Code, include this CDDL HEADER in each
15
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16
* If applicable, add the following below this CDDL HEADER, with the
17
* fields enclosed by brackets "[]" replaced with your own identifying
18
* information: Portions Copyright [yyyy] [name of copyright owner]
19
*
20
* CDDL HEADER END
21
*/
22
23
/*
24
* Copyright 2022 Tino Reichardt <[email protected]>
25
*/
26
27
#include <sys/zfs_context.h>
28
#include <sys/zio_checksum.h>
29
#include <sys/blake3.h>
30
#include <sys/abd.h>
31
32
static int
33
blake3_incremental(void *buf, size_t size, void *arg)
34
{
35
BLAKE3_CTX *ctx = arg;
36
37
Blake3_Update(ctx, buf, size);
38
39
return (0);
40
}
41
42
/*
43
* Computes a native 256-bit BLAKE3 MAC checksum. Please note that this
44
* function requires the presence of a ctx_template that should be allocated
45
* using abd_checksum_blake3_tmpl_init.
46
*/
47
void
48
abd_checksum_blake3_native(abd_t *abd, uint64_t size, const void *ctx_template,
49
zio_cksum_t *zcp)
50
{
51
ASSERT(ctx_template != NULL);
52
53
#if defined(_KERNEL)
54
kpreempt_disable();
55
BLAKE3_CTX *ctx = blake3_per_cpu_ctx[CPU_SEQID];
56
#else
57
BLAKE3_CTX *ctx = kmem_alloc(sizeof (*ctx), KM_SLEEP);
58
#endif
59
60
memcpy(ctx, ctx_template, sizeof (*ctx));
61
(void) abd_iterate_func(abd, 0, size, blake3_incremental, ctx);
62
Blake3_Final(ctx, (uint8_t *)zcp);
63
64
#if defined(_KERNEL)
65
kpreempt_enable();
66
#else
67
memset(ctx, 0, sizeof (*ctx));
68
kmem_free(ctx, sizeof (*ctx));
69
#endif
70
}
71
72
/*
73
* Byteswapped version of abd_checksum_blake3_native. This just invokes
74
* the native checksum function and byteswaps the resulting checksum (since
75
* BLAKE3 is internally endian-insensitive).
76
*/
77
void
78
abd_checksum_blake3_byteswap(abd_t *abd, uint64_t size,
79
const void *ctx_template, zio_cksum_t *zcp)
80
{
81
zio_cksum_t tmp;
82
83
ASSERT(ctx_template != NULL);
84
85
abd_checksum_blake3_native(abd, size, ctx_template, &tmp);
86
zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
87
zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
88
zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
89
zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
90
}
91
92
/*
93
* Allocates a BLAKE3 MAC template suitable for using in BLAKE3 MAC checksum
94
* computations and returns a pointer to it.
95
*/
96
void *
97
abd_checksum_blake3_tmpl_init(const zio_cksum_salt_t *salt)
98
{
99
BLAKE3_CTX *ctx;
100
101
ASSERT(sizeof (salt->zcs_bytes) == 32);
102
103
/* init reference object */
104
ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
105
Blake3_InitKeyed(ctx, salt->zcs_bytes);
106
107
return (ctx);
108
}
109
110
/*
111
* Frees a BLAKE3 context template previously allocated using
112
* zio_checksum_blake3_tmpl_init.
113
*/
114
void
115
abd_checksum_blake3_tmpl_free(void *ctx_template)
116
{
117
BLAKE3_CTX *ctx = ctx_template;
118
119
memset(ctx, 0, sizeof (*ctx));
120
kmem_free(ctx, sizeof (*ctx));
121
}
122
123