1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2024, Klara Inc.
24 */
25
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"
32
33 /*
34 * Each bit in a bitfield has three possible string representations:
35 * - single char
36 * - two-char pair
37 * - full name
38 */
39 typedef struct {
40 const char vb_bit;
41 const char vb_pair[2];
42 const char *vb_name;
43 } valstr_bit_t;
44
45 /*
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
49 * visual comparison.
50 */
51 static size_t
valstr_bitfield_bits(const valstr_bit_t * table,const size_t nelems,uint64_t bits,char * out,size_t outlen)52 valstr_bitfield_bits(const valstr_bit_t *table, const size_t nelems,
53 uint64_t bits, char *out, size_t outlen)
54 {
55 ASSERT(out);
56 size_t n = 0;
57 for (int b = 0; b < nelems; b++) {
58 if (n == outlen)
59 break;
60 uint64_t mask = (1ULL << b);
61 out[n++] = (bits & mask) ? table[b].vb_bit : ' ';
62 }
63 if (n < outlen)
64 out[n++] = '\0';
65 return (n);
66 }
67
68 /*
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
71 * whole value.
72 */
73 static size_t
valstr_bitfield_pairs(const valstr_bit_t * table,const size_t nelems,uint64_t bits,char * out,size_t outlen)74 valstr_bitfield_pairs(const valstr_bit_t *table, const size_t nelems,
75 uint64_t bits, char *out, size_t outlen)
76 {
77 ASSERT(out);
78 size_t n = 0;
79 for (int b = 0; b < nelems; b++) {
80 ASSERT3U(n, <=, outlen);
81 if (n == outlen)
82 break;
83 uint64_t mask = (1ULL << b);
84 if (bits & mask) {
85 size_t len = (n > 0) ? 3 : 2;
86 if (n > outlen-len)
87 break;
88 if (n > 0)
89 out[n++] = '|';
90 out[n++] = table[b].vb_pair[0];
91 out[n++] = table[b].vb_pair[1];
92 }
93 }
94 if (n < outlen)
95 out[n++] = '\0';
96 return (n);
97 }
98
99 /*
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
102 * can get very long.
103 */
104 static size_t
valstr_bitfield_str(const valstr_bit_t * table,const size_t nelems,uint64_t bits,char * out,size_t outlen)105 valstr_bitfield_str(const valstr_bit_t *table, const size_t nelems,
106 uint64_t bits, char *out, size_t outlen)
107 {
108 ASSERT(out);
109 size_t n = 0;
110 for (int b = 0; b < nelems; b++) {
111 ASSERT3U(n, <=, outlen);
112 if (n == outlen)
113 break;
114 uint64_t mask = (1ULL << b);
115 if (bits & mask) {
116 size_t len = strlen(table[b].vb_name);
117 if (n > 0)
118 len++;
119 if (n > outlen-len)
120 break;
121 if (n > 0) {
122 out[n++] = ' ';
123 len--;
124 }
125 memcpy(&out[n], table[b].vb_name, len);
126 n += len;
127 }
128 }
129 if (n < outlen)
130 out[n++] = '\0';
131 return (n);
132 }
133
134 /*
135 * Emits the name of the given enum value in the table.
136 */
137 static size_t
valstr_enum_str(const char ** table,const size_t nelems,int v,char * out,size_t outlen)138 valstr_enum_str(const char **table, const size_t nelems,
139 int v, char *out, size_t outlen)
140 {
141 ASSERT(out);
142 ASSERT3U(v, <, nelems);
143 if (v >= nelems)
144 return (0);
145 return (MIN(strlcpy(out, table[v], outlen), outlen));
146 }
147
148 /*
149 * These macros create the string tables for the given name, and implement
150 * the public functions described in zfs_valstr.h.
151 */
152 #define _VALSTR_BITFIELD_IMPL(name, ...) \
153 static const valstr_bit_t valstr_ ## name ## _table[] = { __VA_ARGS__ };\
154 size_t \
155 zfs_valstr_ ## name ## _bits(uint64_t bits, char *out, size_t outlen) \
156 { \
157 return (valstr_bitfield_bits(valstr_ ## name ## _table, \
158 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
159 } \
160 \
161 size_t \
162 zfs_valstr_ ## name ## _pairs(uint64_t bits, char *out, size_t outlen) \
163 { \
164 return (valstr_bitfield_pairs(valstr_ ## name ## _table, \
165 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
166 } \
167 \
168 size_t \
169 zfs_valstr_ ## name(uint64_t bits, char *out, size_t outlen) \
170 { \
171 return (valstr_bitfield_str(valstr_ ## name ## _table, \
172 ARRAY_SIZE(valstr_ ## name ## _table), bits, out, outlen)); \
173 } \
174
175 #define _VALSTR_ENUM_IMPL(name, ...) \
176 static const char *valstr_ ## name ## _table[] = { __VA_ARGS__ }; \
177 size_t \
178 zfs_valstr_ ## name(int v, char *out, size_t outlen) \
179 { \
180 return (valstr_enum_str(valstr_ ## name ## _table, \
181 ARRAY_SIZE(valstr_ ## name ## _table), v, out, outlen)); \
182 } \
183
184
185 /* String tables */
186
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" },
222 )
223
224 /*
225 * ZIO pipeline stage(s): enum zio_stage, typically zio->io_stage or
226 * zio->io_pipeline.
227 */
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" },
256 )
257
258 /* ZIO priority: zio_priority_t, typically zio->io_priority */
259 _VALSTR_ENUM_IMPL(zio_priority,
260 "SYNC_READ",
261 "SYNC_WRITE",
262 "ASYNC_READ",
263 "ASYNC_WRITE",
264 "SCRUB",
265 "REMOVAL",
266 "INITIALIZING",
267 "TRIM",
268 "REBUILD",
269 "[NUM_QUEUEABLE]",
270 "NOW",
271 )
272
273 #undef _VALSTR_BITFIELD_IMPL
274 #undef _VALSTR_ENUM_IMPL
275