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 http://www.opensolaris.org/os/licensing.
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 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 */
25
26#ifndef	_SYS_NVPAIR_H
27#define	_SYS_NVPAIR_H
28
29#include <sys/types.h>
30#include <sys/time.h>
31#include <sys/errno.h>
32
33#ifdef	__cplusplus
34extern "C" {
35#endif
36
37typedef enum {
38	DATA_TYPE_DONTCARE = -1,
39	DATA_TYPE_UNKNOWN = 0,
40	DATA_TYPE_BOOLEAN,
41	DATA_TYPE_BYTE,
42	DATA_TYPE_INT16,
43	DATA_TYPE_UINT16,
44	DATA_TYPE_INT32,
45	DATA_TYPE_UINT32,
46	DATA_TYPE_INT64,
47	DATA_TYPE_UINT64,
48	DATA_TYPE_STRING,
49	DATA_TYPE_BYTE_ARRAY,
50	DATA_TYPE_INT16_ARRAY,
51	DATA_TYPE_UINT16_ARRAY,
52	DATA_TYPE_INT32_ARRAY,
53	DATA_TYPE_UINT32_ARRAY,
54	DATA_TYPE_INT64_ARRAY,
55	DATA_TYPE_UINT64_ARRAY,
56	DATA_TYPE_STRING_ARRAY,
57	DATA_TYPE_HRTIME,
58	DATA_TYPE_NVLIST,
59	DATA_TYPE_NVLIST_ARRAY,
60	DATA_TYPE_BOOLEAN_VALUE,
61	DATA_TYPE_INT8,
62	DATA_TYPE_UINT8,
63	DATA_TYPE_BOOLEAN_ARRAY,
64	DATA_TYPE_INT8_ARRAY,
65#if !defined(_KERNEL) && !defined(_STANDALONE)
66	DATA_TYPE_UINT8_ARRAY,
67	DATA_TYPE_DOUBLE
68#else
69	DATA_TYPE_UINT8_ARRAY
70#endif
71} data_type_t;
72
73typedef struct nvpair {
74	int32_t nvp_size;	/* size of this nvpair */
75	int16_t	nvp_name_sz;	/* length of name string */
76	int16_t	nvp_reserve;	/* not used */
77	int32_t	nvp_value_elem;	/* number of elements for array types */
78	data_type_t nvp_type;	/* type of value */
79	/* name string */
80	/* aligned ptr array for string arrays */
81	/* aligned array of data for value */
82} nvpair_t;
83
84/* nvlist header */
85typedef struct nvlist {
86	int32_t		nvl_version;
87	uint32_t	nvl_nvflag;	/* persistent flags */
88	uint64_t	nvl_priv;	/* ptr to private data if not packed */
89	uint32_t	nvl_flag;
90	int32_t		nvl_pad;	/* currently not used, for alignment */
91} nvlist_t;
92
93/* nvp implementation version */
94#define	NV_VERSION	0
95
96/* nvlist pack encoding */
97#define	NV_ENCODE_NATIVE	0
98#define	NV_ENCODE_XDR		1
99
100/* nvlist persistent unique name flags, stored in nvl_nvflags */
101#define	NV_UNIQUE_NAME		0x1
102#define	NV_UNIQUE_NAME_TYPE	0x2
103
104/* nvlist lookup pairs related flags */
105#define	NV_FLAG_NOENTOK		0x1
106
107/* convenience macros */
108#define	NV_ALIGN(x)		(((ulong_t)(x) + 7ul) & ~7ul)
109#define	NV_ALIGN4(x)		(((x) + 3) & ~3)
110
111#define	NVP_SIZE(nvp)		((nvp)->nvp_size)
112#define	NVP_NAME(nvp)		((char *)(nvp) + sizeof (nvpair_t))
113#define	NVP_TYPE(nvp)		((nvp)->nvp_type)
114#define	NVP_NELEM(nvp)		((nvp)->nvp_value_elem)
115#define	NVP_VALUE(nvp)		((char *)(nvp) + NV_ALIGN(sizeof (nvpair_t) \
116				+ (nvp)->nvp_name_sz))
117
118#define	NVL_VERSION(nvl)	((nvl)->nvl_version)
119#define	NVL_SIZE(nvl)		((nvl)->nvl_size)
120#define	NVL_FLAG(nvl)		((nvl)->nvl_flag)
121
122/* NV allocator framework */
123typedef struct nv_alloc_ops nv_alloc_ops_t;
124
125typedef struct nv_alloc {
126	const nv_alloc_ops_t *nva_ops;
127	void *nva_arg;
128} nv_alloc_t;
129
130struct nv_alloc_ops {
131	int (*nv_ao_init)(nv_alloc_t *, va_list);
132	void (*nv_ao_fini)(nv_alloc_t *);
133	void *(*nv_ao_alloc)(nv_alloc_t *, size_t);
134	void (*nv_ao_free)(nv_alloc_t *, void *, size_t);
135	void (*nv_ao_reset)(nv_alloc_t *);
136};
137
138extern const nv_alloc_ops_t *nv_fixed_ops;
139extern nv_alloc_t *nv_alloc_nosleep;
140
141#if defined(_KERNEL)
142extern nv_alloc_t *nv_alloc_sleep;
143extern nv_alloc_t *nv_alloc_pushpage;
144#endif
145
146int nv_alloc_init(nv_alloc_t *, const nv_alloc_ops_t *, /* args */ ...);
147void nv_alloc_reset(nv_alloc_t *);
148void nv_alloc_fini(nv_alloc_t *);
149
150/* list management */
151int nvlist_alloc(nvlist_t **, uint_t, int);
152void nvlist_free(nvlist_t *);
153int nvlist_size(nvlist_t *, size_t *, int);
154int nvlist_pack(nvlist_t *, char **, size_t *, int, int);
155int nvlist_unpack(char *, size_t, nvlist_t **, int);
156int nvlist_dup(nvlist_t *, nvlist_t **, int);
157int nvlist_merge(nvlist_t *, nvlist_t *, int);
158
159uint_t nvlist_nvflag(nvlist_t *);
160
161int nvlist_xalloc(nvlist_t **, uint_t, nv_alloc_t *);
162int nvlist_xpack(nvlist_t *, char **, size_t *, int, nv_alloc_t *);
163int nvlist_xunpack(char *, size_t, nvlist_t **, nv_alloc_t *);
164int nvlist_xdup(nvlist_t *, nvlist_t **, nv_alloc_t *);
165nv_alloc_t *nvlist_lookup_nv_alloc(nvlist_t *);
166
167int nvlist_add_nvpair(nvlist_t *, nvpair_t *);
168int nvlist_add_boolean(nvlist_t *, const char *);
169int nvlist_add_boolean_value(nvlist_t *, const char *, boolean_t);
170int nvlist_add_byte(nvlist_t *, const char *, uchar_t);
171int nvlist_add_int8(nvlist_t *, const char *, int8_t);
172int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
173int nvlist_add_int16(nvlist_t *, const char *, int16_t);
174int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
175int nvlist_add_int32(nvlist_t *, const char *, int32_t);
176int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
177int nvlist_add_int64(nvlist_t *, const char *, int64_t);
178int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
179int nvlist_add_string(nvlist_t *, const char *, const char *);
180int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
181int nvlist_add_boolean_array(nvlist_t *, const char *, boolean_t *, uint_t);
182int nvlist_add_byte_array(nvlist_t *, const char *, uchar_t *, uint_t);
183int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint_t);
184int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint_t);
185int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint_t);
186int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint_t);
187int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint_t);
188int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint_t);
189int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint_t);
190int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint_t);
191int nvlist_add_string_array(nvlist_t *, const char *, char *const *, uint_t);
192int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint_t);
193int nvlist_add_hrtime(nvlist_t *, const char *, hrtime_t);
194#if !defined(_KERNEL) && !defined(_STANDALONE)
195int nvlist_add_double(nvlist_t *, const char *, double);
196#endif
197
198int nvlist_remove(nvlist_t *, const char *, data_type_t);
199int nvlist_remove_all(nvlist_t *, const char *);
200int nvlist_remove_nvpair(nvlist_t *, nvpair_t *);
201
202int nvlist_lookup_boolean(nvlist_t *, const char *);
203int nvlist_lookup_boolean_value(nvlist_t *, const char *, boolean_t *);
204int nvlist_lookup_byte(nvlist_t *, const char *, uchar_t *);
205int nvlist_lookup_int8(nvlist_t *, const char *, int8_t *);
206int nvlist_lookup_uint8(nvlist_t *, const char *, uint8_t *);
207int nvlist_lookup_int16(nvlist_t *, const char *, int16_t *);
208int nvlist_lookup_uint16(nvlist_t *, const char *, uint16_t *);
209int nvlist_lookup_int32(nvlist_t *, const char *, int32_t *);
210int nvlist_lookup_uint32(nvlist_t *, const char *, uint32_t *);
211int nvlist_lookup_int64(nvlist_t *, const char *, int64_t *);
212int nvlist_lookup_uint64(nvlist_t *, const char *, uint64_t *);
213int nvlist_lookup_string(nvlist_t *, const char *, char **);
214int nvlist_lookup_nvlist(nvlist_t *, const char *, nvlist_t **);
215int nvlist_lookup_boolean_array(nvlist_t *, const char *,
216    boolean_t **, uint_t *);
217int nvlist_lookup_byte_array(nvlist_t *, const char *, uchar_t **, uint_t *);
218int nvlist_lookup_int8_array(nvlist_t *, const char *, int8_t **, uint_t *);
219int nvlist_lookup_uint8_array(nvlist_t *, const char *, uint8_t **, uint_t *);
220int nvlist_lookup_int16_array(nvlist_t *, const char *, int16_t **, uint_t *);
221int nvlist_lookup_uint16_array(nvlist_t *, const char *, uint16_t **, uint_t *);
222int nvlist_lookup_int32_array(nvlist_t *, const char *, int32_t **, uint_t *);
223int nvlist_lookup_uint32_array(nvlist_t *, const char *, uint32_t **, uint_t *);
224int nvlist_lookup_int64_array(nvlist_t *, const char *, int64_t **, uint_t *);
225int nvlist_lookup_uint64_array(nvlist_t *, const char *, uint64_t **, uint_t *);
226int nvlist_lookup_string_array(nvlist_t *, const char *, char ***, uint_t *);
227int nvlist_lookup_nvlist_array(nvlist_t *, const char *,
228    nvlist_t ***, uint_t *);
229int nvlist_lookup_hrtime(nvlist_t *, const char *, hrtime_t *);
230int nvlist_lookup_pairs(nvlist_t *, int, ...);
231#if !defined(_KERNEL) && !defined(_STANDALONE)
232int nvlist_lookup_double(nvlist_t *, const char *, double *);
233#endif
234
235int nvlist_lookup_nvpair(nvlist_t *, const char *, nvpair_t **);
236int nvlist_lookup_nvpair_embedded_index(nvlist_t *, const char *, nvpair_t **,
237    int *, char **);
238boolean_t nvlist_exists(nvlist_t *, const char *);
239boolean_t nvlist_empty(nvlist_t *);
240
241/* processing nvpair */
242nvpair_t *nvlist_next_nvpair(nvlist_t *, nvpair_t *);
243nvpair_t *nvlist_prev_nvpair(nvlist_t *, nvpair_t *);
244char *nvpair_name(nvpair_t *);
245data_type_t nvpair_type(nvpair_t *);
246int nvpair_type_is_array(nvpair_t *);
247int nvpair_value_boolean_value(nvpair_t *, boolean_t *);
248int nvpair_value_byte(nvpair_t *, uchar_t *);
249int nvpair_value_int8(nvpair_t *, int8_t *);
250int nvpair_value_uint8(nvpair_t *, uint8_t *);
251int nvpair_value_int16(nvpair_t *, int16_t *);
252int nvpair_value_uint16(nvpair_t *, uint16_t *);
253int nvpair_value_int32(nvpair_t *, int32_t *);
254int nvpair_value_uint32(nvpair_t *, uint32_t *);
255int nvpair_value_int64(nvpair_t *, int64_t *);
256int nvpair_value_uint64(nvpair_t *, uint64_t *);
257int nvpair_value_string(nvpair_t *, char **);
258int nvpair_value_nvlist(nvpair_t *, nvlist_t **);
259int nvpair_value_boolean_array(nvpair_t *, boolean_t **, uint_t *);
260int nvpair_value_byte_array(nvpair_t *, uchar_t **, uint_t *);
261int nvpair_value_int8_array(nvpair_t *, int8_t **, uint_t *);
262int nvpair_value_uint8_array(nvpair_t *, uint8_t **, uint_t *);
263int nvpair_value_int16_array(nvpair_t *, int16_t **, uint_t *);
264int nvpair_value_uint16_array(nvpair_t *, uint16_t **, uint_t *);
265int nvpair_value_int32_array(nvpair_t *, int32_t **, uint_t *);
266int nvpair_value_uint32_array(nvpair_t *, uint32_t **, uint_t *);
267int nvpair_value_int64_array(nvpair_t *, int64_t **, uint_t *);
268int nvpair_value_uint64_array(nvpair_t *, uint64_t **, uint_t *);
269int nvpair_value_string_array(nvpair_t *, char ***, uint_t *);
270int nvpair_value_nvlist_array(nvpair_t *, nvlist_t ***, uint_t *);
271int nvpair_value_hrtime(nvpair_t *, hrtime_t *);
272#if !defined(_KERNEL) && !defined(_STANDALONE)
273int nvpair_value_double(nvpair_t *, double *);
274#endif
275
276nvlist_t *fnvlist_alloc(void);
277void fnvlist_free(nvlist_t *);
278size_t fnvlist_size(nvlist_t *);
279char *fnvlist_pack(nvlist_t *, size_t *);
280void fnvlist_pack_free(char *, size_t);
281nvlist_t *fnvlist_unpack(char *, size_t);
282nvlist_t *fnvlist_dup(nvlist_t *);
283void fnvlist_merge(nvlist_t *, nvlist_t *);
284size_t fnvlist_num_pairs(nvlist_t *);
285
286void fnvlist_add_boolean(nvlist_t *, const char *);
287void fnvlist_add_boolean_value(nvlist_t *, const char *, boolean_t);
288void fnvlist_add_byte(nvlist_t *, const char *, uchar_t);
289void fnvlist_add_int8(nvlist_t *, const char *, int8_t);
290void fnvlist_add_uint8(nvlist_t *, const char *, uint8_t);
291void fnvlist_add_int16(nvlist_t *, const char *, int16_t);
292void fnvlist_add_uint16(nvlist_t *, const char *, uint16_t);
293void fnvlist_add_int32(nvlist_t *, const char *, int32_t);
294void fnvlist_add_uint32(nvlist_t *, const char *, uint32_t);
295void fnvlist_add_int64(nvlist_t *, const char *, int64_t);
296void fnvlist_add_uint64(nvlist_t *, const char *, uint64_t);
297void fnvlist_add_string(nvlist_t *, const char *, const char *);
298void fnvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
299void fnvlist_add_nvpair(nvlist_t *, nvpair_t *);
300void fnvlist_add_boolean_array(nvlist_t *, const char *, boolean_t *, uint_t);
301void fnvlist_add_byte_array(nvlist_t *, const char *, uchar_t *, uint_t);
302void fnvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint_t);
303void fnvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint_t);
304void fnvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint_t);
305void fnvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint_t);
306void fnvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint_t);
307void fnvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint_t);
308void fnvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint_t);
309void fnvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint_t);
310void fnvlist_add_string_array(nvlist_t *, const char *, char * const *, uint_t);
311void fnvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint_t);
312
313void fnvlist_remove(nvlist_t *, const char *);
314void fnvlist_remove_nvpair(nvlist_t *, nvpair_t *);
315
316nvpair_t *fnvlist_lookup_nvpair(nvlist_t *, const char *);
317boolean_t fnvlist_lookup_boolean(nvlist_t *, const char *);
318boolean_t fnvlist_lookup_boolean_value(nvlist_t *, const char *);
319uchar_t fnvlist_lookup_byte(nvlist_t *, const char *);
320int8_t fnvlist_lookup_int8(nvlist_t *, const char *);
321int16_t fnvlist_lookup_int16(nvlist_t *, const char *);
322int32_t fnvlist_lookup_int32(nvlist_t *, const char *);
323int64_t fnvlist_lookup_int64(nvlist_t *, const char *);
324uint8_t fnvlist_lookup_uint8(nvlist_t *, const char *);
325uint16_t fnvlist_lookup_uint16(nvlist_t *, const char *);
326uint32_t fnvlist_lookup_uint32(nvlist_t *, const char *);
327uint64_t fnvlist_lookup_uint64(nvlist_t *, const char *);
328char *fnvlist_lookup_string(nvlist_t *, const char *);
329nvlist_t *fnvlist_lookup_nvlist(nvlist_t *, const char *);
330boolean_t *fnvlist_lookup_boolean_array(nvlist_t *, const char *, uint_t *);
331uchar_t *fnvlist_lookup_byte_array(nvlist_t *, const char *, uint_t *);
332int8_t *fnvlist_lookup_int8_array(nvlist_t *, const char *, uint_t *);
333uint8_t *fnvlist_lookup_uint8_array(nvlist_t *, const char *, uint_t *);
334int16_t *fnvlist_lookup_int16_array(nvlist_t *, const char *, uint_t *);
335uint16_t *fnvlist_lookup_uint16_array(nvlist_t *, const char *, uint_t *);
336int32_t *fnvlist_lookup_int32_array(nvlist_t *, const char *, uint_t *);
337uint32_t *fnvlist_lookup_uint32_array(nvlist_t *, const char *, uint_t *);
338int64_t *fnvlist_lookup_int64_array(nvlist_t *, const char *, uint_t *);
339uint64_t *fnvlist_lookup_uint64_array(nvlist_t *, const char *, uint_t *);
340
341boolean_t fnvpair_value_boolean_value(nvpair_t *nvp);
342uchar_t fnvpair_value_byte(nvpair_t *nvp);
343int8_t fnvpair_value_int8(nvpair_t *nvp);
344int16_t fnvpair_value_int16(nvpair_t *nvp);
345int32_t fnvpair_value_int32(nvpair_t *nvp);
346int64_t fnvpair_value_int64(nvpair_t *nvp);
347uint8_t fnvpair_value_uint8(nvpair_t *nvp);
348uint16_t fnvpair_value_uint16(nvpair_t *nvp);
349uint32_t fnvpair_value_uint32(nvpair_t *nvp);
350uint64_t fnvpair_value_uint64(nvpair_t *nvp);
351char *fnvpair_value_string(nvpair_t *nvp);
352nvlist_t *fnvpair_value_nvlist(nvpair_t *nvp);
353
354#ifdef	__cplusplus
355}
356#endif
357
358#endif	/* _SYS_NVPAIR_H */
359