4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2024, Klara Inc.
26 #include <sys/fs/zfs.h>
27 #include <sys/types.h>
28 #include <sys/sysmacros.h>
29 #include <sys/string.h>
30 #include <sys/debug.h>
31 #include "zfs_valstr.h"
34 * Each bit in a bitfield has three possible string representations:
41 const char vb_pair
[2];
46 * Emits a character for each bit in `bits`, up to the number of elements
47 * in the table. Set bits get the character in vb_bit, clear bits get a
48 * space. This results in all strings having the same width, for easier
52 valstr_bitfield_bits(const valstr_bit_t
*table
, const size_t nelems
,
53 uint64_t bits
, char *out
, size_t outlen
)
57 for (int b
= 0; b
< nelems
; b
++) {
60 uint64_t mask
= (1ULL << b
);
61 out
[n
++] = (bits
& mask
) ? table
[b
].vb_bit
: ' ';
69 * Emits a two-char pair for each bit set in `bits`, taken from vb_pair, and
70 * separated by a `|` character. This gives a concise representation of the
74 valstr_bitfield_pairs(const valstr_bit_t
*table
, const size_t nelems
,
75 uint64_t bits
, char *out
, size_t outlen
)
79 for (int b
= 0; b
< nelems
; b
++) {
80 ASSERT3U(n
, <=, outlen
);
83 uint64_t mask
= (1ULL << b
);
85 size_t len
= (n
> 0) ? 3 : 2;
90 out
[n
++] = table
[b
].vb_pair
[0];
91 out
[n
++] = table
[b
].vb_pair
[1];
100 * Emits the full name for each bit set in `bits`, taken from vb_name, and
101 * separated by a space. This unambiguously shows the entire set of bits, but
105 valstr_bitfield_str(const valstr_bit_t
*table
, const size_t nelems
,
106 uint64_t bits
, char *out
, size_t outlen
)
110 for (int b
= 0; b
< nelems
; b
++) {
111 ASSERT3U(n
, <=, outlen
);
114 uint64_t mask
= (1ULL << b
);
116 size_t len
= strlen(table
[b
].vb_name
);
125 memcpy(&out
[n
], table
[b
].vb_name
, len
);
135 * Emits the name of the given enum value in the table.
138 valstr_enum_str(const char **table
, const size_t nelems
,
139 int v
, char *out
, size_t outlen
)
142 ASSERT3U(v
, <, nelems
);
145 return (MIN(strlcpy(out
, table
[v
], outlen
), outlen
));
149 * These macros create the string tables for the given name, and implement
150 * the public functions described in zfs_valstr.h.
152 #define _VALSTR_BITFIELD_IMPL(name, ...) \
153 static const valstr_bit_t valstr_ ## name ## _table[] = { __VA_ARGS__ };\
155 zfs_valstr_ ## name ## _bits(uint64_t bits, char *out, size_t outlen) \
157 return (valstr_bitfield_bits(valstr_ ## name ## _table, \
158 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
162 zfs_valstr_ ## name ## _pairs(uint64_t bits, char *out, size_t outlen) \
164 return (valstr_bitfield_pairs(valstr_ ## name ## _table, \
165 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
169 zfs_valstr_ ## name(uint64_t bits, char *out, size_t outlen) \
171 return (valstr_bitfield_str(valstr_ ## name ## _table, \
172 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
175 #define _VALSTR_ENUM_IMPL(name, ...) \
176 static const char *valstr_ ## name ## _table[] = { __VA_ARGS__ }; \
178 zfs_valstr_ ## name(int v, char *out, size_t outlen) \
180 return (valstr_enum_str(valstr_ ## name ## _table, \
181 ARRAY_SIZE(valstr_ ## name ## _table), v, out, outlen)); \
187 /* ZIO flags: zio_flag_t, typically zio->io_flags */
188 _VALSTR_BITFIELD_IMPL(zio_flag
,
189 { '.', "DA", "DONT_AGGREGATE" },
190 { '.', "RP", "IO_REPAIR" },
191 { '.', "SH", "SELF_HEAL" },
192 { '.', "RS", "RESILVER" },
193 { '.', "SC", "SCRUB" },
194 { '.', "ST", "SCAN_THREAD" },
195 { '.', "PH", "PHYSICAL" },
196 { '.', "CF", "CANFAIL" },
197 { '.', "SP", "SPECULATIVE" },
198 { '.', "CW", "CONFIG_WRITER" },
199 { '.', "DR", "DONT_RETRY" },
200 { '?', "??", "[UNUSED 11]" },
201 { '.', "ND", "NODATA" },
202 { '.', "ID", "INDUCE_DAMAGE" },
203 { '.', "AL", "IO_ALLOCATING" },
204 { '.', "RE", "IO_RETRY" },
205 { '.', "PR", "PROBE" },
206 { '.', "TH", "TRYHARD" },
207 { '.', "OP", "OPTIONAL" },
208 { '.', "RD", "DIO_READ" },
209 { '.', "DQ", "DONT_QUEUE" },
210 { '.', "DP", "DONT_PROPAGATE" },
211 { '.', "BY", "IO_BYPASS" },
212 { '.', "RW", "IO_REWRITE" },
213 { '.', "CM", "RAW_COMPRESS" },
214 { '.', "EN", "RAW_ENCRYPT" },
215 { '.', "GG", "GANG_CHILD" },
216 { '.', "DD", "DDT_CHILD" },
217 { '.', "GF", "GODFATHER" },
218 { '.', "NP", "NOPWRITE" },
219 { '.', "EX", "REEXECUTED" },
220 { '.', "DG", "DELEGATED" },
221 { '.', "DC", "DIO_CHKSUM_ERR" },
225 * ZIO pipeline stage(s): enum zio_stage, typically zio->io_stage or
228 _VALSTR_BITFIELD_IMPL(zio_stage
,
229 { 'O', "O ", "OPEN" },
230 { 'I', "RI", "READ_BP_INIT" },
231 { 'I', "WI", "WRITE_BP_INIT" },
232 { 'I', "FI", "FREE_BP_INIT" },
233 { 'A', "IA", "ISSUE_ASYNC" },
234 { 'W', "WC", "WRITE_COMPRESS" },
235 { 'E', "EN", "ENCRYPT" },
236 { 'C', "CG", "CHECKSUM_GENERATE" },
237 { 'N', "NW", "NOP_WRITE" },
238 { 'B', "BF", "BRT_FREE" },
239 { 'd', "dS", "DDT_READ_START" },
240 { 'd', "dD", "DDT_READ_DONE" },
241 { 'd', "dW", "DDT_WRITE" },
242 { 'd', "dF", "DDT_FREE" },
243 { 'G', "GA", "GANG_ASSEMBLE" },
244 { 'G', "GI", "GANG_ISSUE" },
245 { 'D', "DT", "DVA_THROTTLE" },
246 { 'D', "DA", "DVA_ALLOCATE" },
247 { 'D', "DF", "DVA_FREE" },
248 { 'D', "DC", "DVA_CLAIM" },
249 { 'R', "R ", "READY" },
250 { 'V', "VS", "VDEV_IO_START" },
251 { 'V', "VD", "VDEV_IO_DONE" },
252 { 'V', "VA", "VDEV_IO_ASSESS" },
253 { 'C', "CV", "CHECKSUM_VERIFY" },
254 { 'C', "DC", "DIO_CHECKSUM_VERIFY" },
255 { 'X', "X ", "DONE" },
258 /* ZIO priority: zio_priority_t, typically zio->io_priority */
259 _VALSTR_ENUM_IMPL(zio_priority
,
273 #undef _VALSTR_BITFIELD_IMPL
274 #undef _VALSTR_ENUM_IMPL