xref: /freebsd/sys/contrib/openzfs/module/zcommon/zfs_valstr.c (revision 6463b6b59152fb1695bbe0de78f6e2675c5a765a)
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
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
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
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
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 /* BEGIN CSTYLED */
189 _VALSTR_BITFIELD_IMPL(zio_flag,
190 	{ '.', "DA", "DONT_AGGREGATE" },
191 	{ '.', "RP", "IO_REPAIR" },
192 	{ '.', "SH", "SELF_HEAL" },
193 	{ '.', "RS", "RESILVER" },
194 	{ '.', "SC", "SCRUB" },
195 	{ '.', "ST", "SCAN_THREAD" },
196 	{ '.', "PH", "PHYSICAL" },
197 	{ '.', "CF", "CANFAIL" },
198 	{ '.', "SP", "SPECULATIVE" },
199 	{ '.', "CW", "CONFIG_WRITER" },
200 	{ '.', "DR", "DONT_RETRY" },
201 	{ '?', "??", "[UNUSED 11]" },
202 	{ '.', "ND", "NODATA" },
203 	{ '.', "ID", "INDUCE_DAMAGE" },
204 	{ '.', "AL", "IO_ALLOCATING" },
205 	{ '.', "RE", "IO_RETRY" },
206 	{ '.', "PR", "PROBE" },
207 	{ '.', "TH", "TRYHARD" },
208 	{ '.', "OP", "OPTIONAL" },
209 	{ '.', "RD", "DIO_READ" },
210 	{ '.', "DQ", "DONT_QUEUE" },
211 	{ '.', "DP", "DONT_PROPAGATE" },
212 	{ '.', "BY", "IO_BYPASS" },
213 	{ '.', "RW", "IO_REWRITE" },
214 	{ '.', "CM", "RAW_COMPRESS" },
215 	{ '.', "EN", "RAW_ENCRYPT" },
216 	{ '.', "GG", "GANG_CHILD" },
217 	{ '.', "DD", "DDT_CHILD" },
218 	{ '.', "GF", "GODFATHER" },
219 	{ '.', "NP", "NOPWRITE" },
220 	{ '.', "EX", "REEXECUTED" },
221 	{ '.', "DG", "DELEGATED" },
222 	{ '.', "DC", "DIO_CHKSUM_ERR" },
223 )
224 /* END CSTYLED */
225 
226 /*
227  * ZIO pipeline stage(s): enum zio_stage, typically zio->io_stage or
228  *                        zio->io_pipeline.
229  */
230 /* BEGIN CSTYLED */
231 _VALSTR_BITFIELD_IMPL(zio_stage,
232 	{ 'O', "O ", "OPEN" },
233 	{ 'I', "RI", "READ_BP_INIT" },
234 	{ 'I', "WI", "WRITE_BP_INIT" },
235 	{ 'I', "FI", "FREE_BP_INIT" },
236 	{ 'A', "IA", "ISSUE_ASYNC" },
237 	{ 'W', "WC", "WRITE_COMPRESS" },
238 	{ 'E', "EN", "ENCRYPT" },
239 	{ 'C', "CG", "CHECKSUM_GENERATE" },
240 	{ 'N', "NW", "NOP_WRITE" },
241 	{ 'B', "BF", "BRT_FREE" },
242 	{ 'd', "dS", "DDT_READ_START" },
243 	{ 'd', "dD", "DDT_READ_DONE" },
244 	{ 'd', "dW", "DDT_WRITE" },
245 	{ 'd', "dF", "DDT_FREE" },
246 	{ 'G', "GA", "GANG_ASSEMBLE" },
247 	{ 'G', "GI", "GANG_ISSUE" },
248 	{ 'D', "DT", "DVA_THROTTLE" },
249 	{ 'D', "DA", "DVA_ALLOCATE" },
250 	{ 'D', "DF", "DVA_FREE" },
251 	{ 'D', "DC", "DVA_CLAIM" },
252 	{ 'R', "R ", "READY" },
253 	{ 'V', "VS", "VDEV_IO_START" },
254 	{ 'V', "VD", "VDEV_IO_DONE" },
255 	{ 'V', "VA", "VDEV_IO_ASSESS" },
256 	{ 'C', "CV", "CHECKSUM_VERIFY" },
257 	{ 'C', "DC", "DIO_CHECKSUM_VERIFY" },
258 	{ 'X', "X ", "DONE" },
259 )
260 /* END CSTYLED */
261 
262 /* ZIO priority: zio_priority_t, typically zio->io_priority */
263 /* BEGIN CSTYLED */
264 _VALSTR_ENUM_IMPL(zio_priority,
265 	"SYNC_READ",
266 	"SYNC_WRITE",
267 	"ASYNC_READ",
268 	"ASYNC_WRITE",
269 	"SCRUB",
270 	"REMOVAL",
271 	"INITIALIZING",
272 	"TRIM",
273 	"REBUILD",
274 	"[NUM_QUEUEABLE]",
275 	"NOW",
276 )
277 /* END CSTYLED */
278 
279 #undef _VALSTR_BITFIELD_IMPL
280 #undef _VALSTR_ENUM_IMPL
281