1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
// SPDX-License-Identifier: CDDL-1.0
/*
* 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 https://opensource.org/licenses/CDDL-1.0.
* 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) 2003, 2010 Oracle and/or its affiliates.
* Copyright (c) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
*/
/*
* This file gets included by c files for implementing the full set
* of zfs_impl.h defines.
*
* It's ment for easier maintaining multiple implementations of
* algorithms. Look into blake3_impl.c, sha256_impl.c or sha512_impl.c
* for reference.
*/
#include <sys/zfs_context.h>
#include <sys/zio_checksum.h>
#include <sys/zfs_impl.h>
/* Two default implementations */
#define IMPL_FASTEST (UINT32_MAX)
#define IMPL_CYCLE (UINT32_MAX - 1)
#define IMPL_READ(i) (*(volatile uint32_t *) &(i))
/* Implementation that contains the fastest method */
static IMPL_OPS_T generic_fastest_impl = {
.name = "fastest"
};
/* Hold all supported implementations */
static const IMPL_OPS_T *generic_supp_impls[ARRAY_SIZE(IMPL_ARRAY)];
static uint32_t generic_supp_impls_cnt = 0;
/* Currently selected implementation */
static uint32_t generic_impl_chosen = IMPL_FASTEST;
static struct generic_impl_selector {
const char *name;
uint32_t sel;
} generic_impl_selectors[] = {
{ "cycle", IMPL_CYCLE },
{ "fastest", IMPL_FASTEST }
};
/* check the supported implementations */
static void
generic_impl_init(void)
{
int i, c;
/* init only once */
if (likely(generic_supp_impls_cnt != 0))
return;
/* Move supported implementations into generic_supp_impls */
for (i = 0, c = 0; i < ARRAY_SIZE(IMPL_ARRAY); i++) {
const IMPL_OPS_T *impl = IMPL_ARRAY[i];
if (impl->is_supported && impl->is_supported())
generic_supp_impls[c++] = impl;
}
generic_supp_impls_cnt = c;
/* first init generic impl, may be changed via set_fastest() */
memcpy(&generic_fastest_impl, generic_supp_impls[0],
sizeof (generic_fastest_impl));
}
/* get number of supported implementations */
static uint32_t
generic_impl_getcnt(void)
{
generic_impl_init();
return (generic_supp_impls_cnt);
}
/* get id of selected implementation */
static uint32_t
generic_impl_getid(void)
{
generic_impl_init();
return (IMPL_READ(generic_impl_chosen));
}
/* get name of selected implementation */
static const char *
generic_impl_getname(void)
{
uint32_t impl = IMPL_READ(generic_impl_chosen);
generic_impl_init();
switch (impl) {
case IMPL_FASTEST:
return ("fastest");
case IMPL_CYCLE:
return ("cycle");
default:
return (generic_supp_impls[impl]->name);
}
}
/* set implementation by id */
static void
generic_impl_setid(uint32_t id)
{
generic_impl_init();
switch (id) {
case IMPL_FASTEST:
atomic_swap_32(&generic_impl_chosen, IMPL_FASTEST);
break;
case IMPL_CYCLE:
atomic_swap_32(&generic_impl_chosen, IMPL_CYCLE);
break;
default:
ASSERT3U(id, <, generic_supp_impls_cnt);
atomic_swap_32(&generic_impl_chosen, id);
break;
}
}
/* set implementation by name */
static int
generic_impl_setname(const char *val)
{
uint32_t impl = IMPL_READ(generic_impl_chosen);
size_t val_len;
int i, err = -EINVAL;
generic_impl_init();
val_len = strlen(val);
while ((val_len > 0) && !!isspace(val[val_len-1])) /* trim '\n' */
val_len--;
/* check mandatory implementations */
for (i = 0; i < ARRAY_SIZE(generic_impl_selectors); i++) {
const char *name = generic_impl_selectors[i].name;
if (val_len == strlen(name) &&
strncmp(val, name, val_len) == 0) {
impl = generic_impl_selectors[i].sel;
err = 0;
break;
}
}
/* check all supported implementations */
if (err != 0) {
for (i = 0; i < generic_supp_impls_cnt; i++) {
const char *name = generic_supp_impls[i]->name;
if (val_len == strlen(name) &&
strncmp(val, name, val_len) == 0) {
impl = i;
err = 0;
break;
}
}
}
if (err == 0) {
atomic_swap_32(&generic_impl_chosen, impl);
}
return (err);
}
/* setup id as fastest implementation */
static void
generic_impl_set_fastest(uint32_t id)
{
generic_impl_init();
memcpy(&generic_fastest_impl, generic_supp_impls[id],
sizeof (generic_fastest_impl));
}
/* return impl iterating functions */
const zfs_impl_t ZFS_IMPL_OPS = {
.name = IMPL_NAME,
.getcnt = generic_impl_getcnt,
.getid = generic_impl_getid,
.getname = generic_impl_getname,
.set_fastest = generic_impl_set_fastest,
.setid = generic_impl_setid,
.setname = generic_impl_setname
};
/* get impl ops_t of selected implementation */
const IMPL_OPS_T *
IMPL_GET_OPS(void)
{
const IMPL_OPS_T *ops = NULL;
uint32_t idx, impl = IMPL_READ(generic_impl_chosen);
static uint32_t cycle_count = 0;
generic_impl_init();
switch (impl) {
case IMPL_FASTEST:
ops = &generic_fastest_impl;
break;
case IMPL_CYCLE:
idx = (++cycle_count) % generic_supp_impls_cnt;
ops = generic_supp_impls[idx];
break;
default:
ASSERT3U(impl, <, generic_supp_impls_cnt);
ops = generic_supp_impls[impl];
break;
}
ASSERT3P(ops, !=, NULL);
return (ops);
}
|