xref: /illumos-gate/usr/src/uts/common/sys/nvpair.h (revision 602ca9ea8f9ce0933f0944601cc5d230e91a950d)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_NVPAIR_H
27 #define	_SYS_NVPAIR_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <sys/errno.h>
33 #include <sys/va_list.h>
34 
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/kmem.h>
37 #endif
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 typedef enum {
44 	DATA_TYPE_UNKNOWN = 0,
45 	DATA_TYPE_BOOLEAN,
46 	DATA_TYPE_BYTE,
47 	DATA_TYPE_INT16,
48 	DATA_TYPE_UINT16,
49 	DATA_TYPE_INT32,
50 	DATA_TYPE_UINT32,
51 	DATA_TYPE_INT64,
52 	DATA_TYPE_UINT64,
53 	DATA_TYPE_STRING,
54 	DATA_TYPE_BYTE_ARRAY,
55 	DATA_TYPE_INT16_ARRAY,
56 	DATA_TYPE_UINT16_ARRAY,
57 	DATA_TYPE_INT32_ARRAY,
58 	DATA_TYPE_UINT32_ARRAY,
59 	DATA_TYPE_INT64_ARRAY,
60 	DATA_TYPE_UINT64_ARRAY,
61 	DATA_TYPE_STRING_ARRAY,
62 	DATA_TYPE_HRTIME,
63 	DATA_TYPE_NVLIST,
64 	DATA_TYPE_NVLIST_ARRAY,
65 	DATA_TYPE_BOOLEAN_VALUE,
66 	DATA_TYPE_INT8,
67 	DATA_TYPE_UINT8,
68 	DATA_TYPE_BOOLEAN_ARRAY,
69 	DATA_TYPE_INT8_ARRAY,
70 	DATA_TYPE_UINT8_ARRAY
71 } data_type_t;
72 
73 typedef 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 */
85 typedef 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 */
123 typedef struct nv_alloc_ops nv_alloc_ops_t;
124 
125 typedef struct nv_alloc {
126 	const nv_alloc_ops_t *nva_ops;
127 	void *nva_arg;
128 } nv_alloc_t;
129 
130 struct 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 
138 extern const nv_alloc_ops_t *nv_fixed_ops;
139 extern nv_alloc_t *nv_alloc_nosleep;
140 
141 #if defined(_KERNEL) && !defined(_BOOT)
142 extern nv_alloc_t *nv_alloc_sleep;
143 #endif
144 
145 int nv_alloc_init(nv_alloc_t *, const nv_alloc_ops_t *, /* args */ ...);
146 void nv_alloc_reset(nv_alloc_t *);
147 void nv_alloc_fini(nv_alloc_t *);
148 
149 /* list management */
150 int nvlist_alloc(nvlist_t **, uint_t, int);
151 void nvlist_free(nvlist_t *);
152 int nvlist_size(nvlist_t *, size_t *, int);
153 int nvlist_pack(nvlist_t *, char **, size_t *, int, int);
154 int nvlist_unpack(char *, size_t, nvlist_t **, int);
155 int nvlist_dup(nvlist_t *, nvlist_t **, int);
156 int nvlist_merge(nvlist_t *, nvlist_t *, int);
157 
158 int nvlist_xalloc(nvlist_t **, uint_t, nv_alloc_t *);
159 int nvlist_xpack(nvlist_t *, char **, size_t *, int, nv_alloc_t *);
160 int nvlist_xunpack(char *, size_t, nvlist_t **, nv_alloc_t *);
161 int nvlist_xdup(nvlist_t *, nvlist_t **, nv_alloc_t *);
162 nv_alloc_t *nvlist_lookup_nv_alloc(nvlist_t *);
163 
164 int nvlist_add_nvpair(nvlist_t *, nvpair_t *);
165 int nvlist_add_boolean(nvlist_t *, const char *);
166 int nvlist_add_boolean_value(nvlist_t *, const char *, boolean_t);
167 int nvlist_add_byte(nvlist_t *, const char *, uchar_t);
168 int nvlist_add_int8(nvlist_t *, const char *, int8_t);
169 int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
170 int nvlist_add_int16(nvlist_t *, const char *, int16_t);
171 int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
172 int nvlist_add_int32(nvlist_t *, const char *, int32_t);
173 int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
174 int nvlist_add_int64(nvlist_t *, const char *, int64_t);
175 int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
176 int nvlist_add_string(nvlist_t *, const char *, const char *);
177 int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
178 int nvlist_add_boolean_array(nvlist_t *, const char *, boolean_t *, uint_t);
179 int nvlist_add_byte_array(nvlist_t *, const char *, uchar_t *, uint_t);
180 int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint_t);
181 int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint_t);
182 int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint_t);
183 int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint_t);
184 int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint_t);
185 int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint_t);
186 int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint_t);
187 int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint_t);
188 int nvlist_add_string_array(nvlist_t *, const char *, char *const *, uint_t);
189 int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint_t);
190 int nvlist_add_hrtime(nvlist_t *, const char *, hrtime_t);
191 
192 int nvlist_remove(nvlist_t *, const char *, data_type_t);
193 int nvlist_remove_all(nvlist_t *, const char *);
194 
195 int nvlist_lookup_boolean(nvlist_t *, const char *);
196 int nvlist_lookup_boolean_value(nvlist_t *, const char *, boolean_t *);
197 int nvlist_lookup_byte(nvlist_t *, const char *, uchar_t *);
198 int nvlist_lookup_int8(nvlist_t *, const char *, int8_t *);
199 int nvlist_lookup_uint8(nvlist_t *, const char *, uint8_t *);
200 int nvlist_lookup_int16(nvlist_t *, const char *, int16_t *);
201 int nvlist_lookup_uint16(nvlist_t *, const char *, uint16_t *);
202 int nvlist_lookup_int32(nvlist_t *, const char *, int32_t *);
203 int nvlist_lookup_uint32(nvlist_t *, const char *, uint32_t *);
204 int nvlist_lookup_int64(nvlist_t *, const char *, int64_t *);
205 int nvlist_lookup_uint64(nvlist_t *, const char *, uint64_t *);
206 int nvlist_lookup_string(nvlist_t *, const char *, char **);
207 int nvlist_lookup_nvlist(nvlist_t *, const char *, nvlist_t **);
208 int nvlist_lookup_boolean_array(nvlist_t *, const char *,
209     boolean_t **, uint_t *);
210 int nvlist_lookup_byte_array(nvlist_t *, const char *, uchar_t **, uint_t *);
211 int nvlist_lookup_int8_array(nvlist_t *, const char *, int8_t **, uint_t *);
212 int nvlist_lookup_uint8_array(nvlist_t *, const char *, uint8_t **, uint_t *);
213 int nvlist_lookup_int16_array(nvlist_t *, const char *, int16_t **, uint_t *);
214 int nvlist_lookup_uint16_array(nvlist_t *, const char *, uint16_t **, uint_t *);
215 int nvlist_lookup_int32_array(nvlist_t *, const char *, int32_t **, uint_t *);
216 int nvlist_lookup_uint32_array(nvlist_t *, const char *, uint32_t **, uint_t *);
217 int nvlist_lookup_int64_array(nvlist_t *, const char *, int64_t **, uint_t *);
218 int nvlist_lookup_uint64_array(nvlist_t *, const char *, uint64_t **, uint_t *);
219 int nvlist_lookup_string_array(nvlist_t *, const char *, char ***, uint_t *);
220 int nvlist_lookup_nvlist_array(nvlist_t *, const char *,
221     nvlist_t ***, uint_t *);
222 int nvlist_lookup_hrtime(nvlist_t *, const char *, hrtime_t *);
223 int nvlist_lookup_pairs(nvlist_t *, int, ...);
224 
225 int nvlist_lookup_nvpair(nvlist_t *, const char *, nvpair_t **);
226 int nvlist_lookup_nvpair_embedded_index(nvlist_t *, const char *, nvpair_t **,
227     int *, char **);
228 boolean_t nvlist_exists(nvlist_t *, const char *);
229 
230 /* processing nvpair */
231 nvpair_t *nvlist_next_nvpair(nvlist_t *, nvpair_t *);
232 char *nvpair_name(nvpair_t *);
233 data_type_t nvpair_type(nvpair_t *);
234 int nvpair_type_is_array(nvpair_t *);
235 int nvpair_value_boolean_value(nvpair_t *, boolean_t *);
236 int nvpair_value_byte(nvpair_t *, uchar_t *);
237 int nvpair_value_int8(nvpair_t *, int8_t *);
238 int nvpair_value_uint8(nvpair_t *, uint8_t *);
239 int nvpair_value_int16(nvpair_t *, int16_t *);
240 int nvpair_value_uint16(nvpair_t *, uint16_t *);
241 int nvpair_value_int32(nvpair_t *, int32_t *);
242 int nvpair_value_uint32(nvpair_t *, uint32_t *);
243 int nvpair_value_int64(nvpair_t *, int64_t *);
244 int nvpair_value_uint64(nvpair_t *, uint64_t *);
245 int nvpair_value_string(nvpair_t *, char **);
246 int nvpair_value_nvlist(nvpair_t *, nvlist_t **);
247 int nvpair_value_boolean_array(nvpair_t *, boolean_t **, uint_t *);
248 int nvpair_value_byte_array(nvpair_t *, uchar_t **, uint_t *);
249 int nvpair_value_int8_array(nvpair_t *, int8_t **, uint_t *);
250 int nvpair_value_uint8_array(nvpair_t *, uint8_t **, uint_t *);
251 int nvpair_value_int16_array(nvpair_t *, int16_t **, uint_t *);
252 int nvpair_value_uint16_array(nvpair_t *, uint16_t **, uint_t *);
253 int nvpair_value_int32_array(nvpair_t *, int32_t **, uint_t *);
254 int nvpair_value_uint32_array(nvpair_t *, uint32_t **, uint_t *);
255 int nvpair_value_int64_array(nvpair_t *, int64_t **, uint_t *);
256 int nvpair_value_uint64_array(nvpair_t *, uint64_t **, uint_t *);
257 int nvpair_value_string_array(nvpair_t *, char ***, uint_t *);
258 int nvpair_value_nvlist_array(nvpair_t *, nvlist_t ***, uint_t *);
259 int nvpair_value_hrtime(nvpair_t *, hrtime_t *);
260 
261 #ifdef	__cplusplus
262 }
263 #endif
264 
265 #endif	/* _SYS_NVPAIR_H */
266