Fix generation of kernel uevents for snapshot rename on linux
[zfs.git] / lib / libzfsbootenv / lzbe_pair.c
blobb9222598451781e833e5ef7f01eeef17df436e15
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
12 * Copyright 2020 Toomas Soome <tsoome@me.com>
15 #include <sys/types.h>
16 #include <string.h>
17 #include <libzfs.h>
18 #include <libzfsbootenv.h>
19 #include <sys/zfs_bootenv.h>
20 #include <sys/vdev_impl.h>
23 * Get or create nvlist. If key is not NULL, get nvlist from bootenv,
24 * otherwise return bootenv.
26 int
27 lzbe_nvlist_get(const char *pool, const char *key, void **ptr)
29 libzfs_handle_t *hdl;
30 zpool_handle_t *zphdl;
31 nvlist_t *nv;
32 int rv = -1;
34 if (pool == NULL || *pool == '\0')
35 return (rv);
37 if ((hdl = libzfs_init()) == NULL) {
38 return (rv);
41 zphdl = zpool_open(hdl, pool);
42 if (zphdl == NULL) {
43 libzfs_fini(hdl);
44 return (rv);
47 rv = zpool_get_bootenv(zphdl, &nv);
48 if (rv == 0) {
49 nvlist_t *nvl, *dup;
51 if (key != NULL) {
52 rv = nvlist_lookup_nvlist(nv, key, &nvl);
53 if (rv == 0) {
54 rv = nvlist_dup(nvl, &dup, 0);
55 nvlist_free(nv);
56 if (rv == 0)
57 nv = dup;
58 else
59 nv = NULL;
60 } else {
61 nvlist_free(nv);
62 rv = nvlist_alloc(&nv, NV_UNIQUE_NAME, 0);
65 *ptr = nv;
68 zpool_close(zphdl);
69 libzfs_fini(hdl);
70 return (rv);
73 int
74 lzbe_nvlist_set(const char *pool, const char *key, void *ptr)
76 libzfs_handle_t *hdl;
77 zpool_handle_t *zphdl;
78 nvlist_t *nv;
79 uint64_t version;
80 int rv = -1;
82 if (pool == NULL || *pool == '\0')
83 return (rv);
85 if ((hdl = libzfs_init()) == NULL) {
86 return (rv);
89 zphdl = zpool_open(hdl, pool);
90 if (zphdl == NULL) {
91 libzfs_fini(hdl);
92 return (rv);
95 if (key != NULL) {
96 rv = zpool_get_bootenv(zphdl, &nv);
97 if (rv == 0) {
99 * We got the nvlist, check for version.
100 * if version is missing or is not VB_NVLIST,
101 * create new list.
103 rv = nvlist_lookup_uint64(nv, BOOTENV_VERSION,
104 &version);
105 if (rv != 0 || version != VB_NVLIST) {
106 /* Drop this nvlist */
107 fnvlist_free(nv);
108 /* Create and prepare new nvlist */
109 nv = fnvlist_alloc();
110 fnvlist_add_uint64(nv, BOOTENV_VERSION,
111 VB_NVLIST);
113 rv = nvlist_add_nvlist(nv, key, ptr);
114 if (rv == 0)
115 rv = zpool_set_bootenv(zphdl, nv);
116 nvlist_free(nv);
118 } else {
119 rv = zpool_set_bootenv(zphdl, ptr);
122 zpool_close(zphdl);
123 libzfs_fini(hdl);
124 return (rv);
128 * free nvlist we got via lzbe_nvlist_get()
130 void
131 lzbe_nvlist_free(void *ptr)
133 nvlist_free(ptr);
136 static const char *typenames[] = {
137 "DATA_TYPE_UNKNOWN",
138 "DATA_TYPE_BOOLEAN",
139 "DATA_TYPE_BYTE",
140 "DATA_TYPE_INT16",
141 "DATA_TYPE_UINT16",
142 "DATA_TYPE_INT32",
143 "DATA_TYPE_UINT32",
144 "DATA_TYPE_INT64",
145 "DATA_TYPE_UINT64",
146 "DATA_TYPE_STRING",
147 "DATA_TYPE_BYTE_ARRAY",
148 "DATA_TYPE_INT16_ARRAY",
149 "DATA_TYPE_UINT16_ARRAY",
150 "DATA_TYPE_INT32_ARRAY",
151 "DATA_TYPE_UINT32_ARRAY",
152 "DATA_TYPE_INT64_ARRAY",
153 "DATA_TYPE_UINT64_ARRAY",
154 "DATA_TYPE_STRING_ARRAY",
155 "DATA_TYPE_HRTIME",
156 "DATA_TYPE_NVLIST",
157 "DATA_TYPE_NVLIST_ARRAY",
158 "DATA_TYPE_BOOLEAN_VALUE",
159 "DATA_TYPE_INT8",
160 "DATA_TYPE_UINT8",
161 "DATA_TYPE_BOOLEAN_ARRAY",
162 "DATA_TYPE_INT8_ARRAY",
163 "DATA_TYPE_UINT8_ARRAY"
166 static int
167 nvpair_type_from_name(const char *name)
169 unsigned i;
171 for (i = 0; i < ARRAY_SIZE(typenames); i++) {
172 if (strcmp(name, typenames[i]) == 0)
173 return (i);
175 return (0);
179 * Add pair defined by key, type and value into nvlist.
182 lzbe_add_pair(void *ptr, const char *key, const char *type, void *value,
183 size_t size)
185 nvlist_t *nv = ptr;
186 data_type_t dt;
187 int rv = 0;
189 if (ptr == NULL || key == NULL || value == NULL)
190 return (rv);
192 if (type == NULL)
193 type = "DATA_TYPE_STRING";
194 dt = nvpair_type_from_name(type);
195 if (dt == DATA_TYPE_UNKNOWN)
196 return (EINVAL);
198 switch (dt) {
199 case DATA_TYPE_BYTE:
200 if (size != sizeof (uint8_t)) {
201 rv = EINVAL;
202 break;
204 rv = nvlist_add_byte(nv, key, *(uint8_t *)value);
205 break;
207 case DATA_TYPE_INT16:
208 if (size != sizeof (int16_t)) {
209 rv = EINVAL;
210 break;
212 rv = nvlist_add_int16(nv, key, *(int16_t *)value);
213 break;
215 case DATA_TYPE_UINT16:
216 if (size != sizeof (uint16_t)) {
217 rv = EINVAL;
218 break;
220 rv = nvlist_add_uint16(nv, key, *(uint16_t *)value);
221 break;
223 case DATA_TYPE_INT32:
224 if (size != sizeof (int32_t)) {
225 rv = EINVAL;
226 break;
228 rv = nvlist_add_int32(nv, key, *(int32_t *)value);
229 break;
231 case DATA_TYPE_UINT32:
232 if (size != sizeof (uint32_t)) {
233 rv = EINVAL;
234 break;
236 rv = nvlist_add_uint32(nv, key, *(uint32_t *)value);
237 break;
239 case DATA_TYPE_INT64:
240 if (size != sizeof (int64_t)) {
241 rv = EINVAL;
242 break;
244 rv = nvlist_add_int64(nv, key, *(int64_t *)value);
245 break;
247 case DATA_TYPE_UINT64:
248 if (size != sizeof (uint64_t)) {
249 rv = EINVAL;
250 break;
252 rv = nvlist_add_uint64(nv, key, *(uint64_t *)value);
253 break;
255 case DATA_TYPE_STRING:
256 rv = nvlist_add_string(nv, key, value);
257 break;
259 case DATA_TYPE_BYTE_ARRAY:
260 rv = nvlist_add_byte_array(nv, key, value, size);
261 break;
263 case DATA_TYPE_INT16_ARRAY:
264 rv = nvlist_add_int16_array(nv, key, value, size);
265 break;
267 case DATA_TYPE_UINT16_ARRAY:
268 rv = nvlist_add_uint16_array(nv, key, value, size);
269 break;
271 case DATA_TYPE_INT32_ARRAY:
272 rv = nvlist_add_int32_array(nv, key, value, size);
273 break;
275 case DATA_TYPE_UINT32_ARRAY:
276 rv = nvlist_add_uint32_array(nv, key, value, size);
277 break;
279 case DATA_TYPE_INT64_ARRAY:
280 rv = nvlist_add_int64_array(nv, key, value, size);
281 break;
283 case DATA_TYPE_UINT64_ARRAY:
284 rv = nvlist_add_uint64_array(nv, key, value, size);
285 break;
287 case DATA_TYPE_STRING_ARRAY:
288 rv = nvlist_add_string_array(nv, key, value, size);
289 break;
291 case DATA_TYPE_NVLIST:
292 rv = nvlist_add_nvlist(nv, key, (nvlist_t *)value);
293 break;
295 case DATA_TYPE_NVLIST_ARRAY:
296 rv = nvlist_add_nvlist_array(nv, key, (const nvlist_t **)value,
297 size);
298 break;
300 case DATA_TYPE_BOOLEAN_VALUE:
301 if (size != sizeof (boolean_t)) {
302 rv = EINVAL;
303 break;
305 rv = nvlist_add_boolean_value(nv, key, *(boolean_t *)value);
306 break;
308 case DATA_TYPE_INT8:
309 if (size != sizeof (int8_t)) {
310 rv = EINVAL;
311 break;
313 rv = nvlist_add_int8(nv, key, *(int8_t *)value);
314 break;
316 case DATA_TYPE_UINT8:
317 if (size != sizeof (uint8_t)) {
318 rv = EINVAL;
319 break;
321 rv = nvlist_add_uint8(nv, key, *(uint8_t *)value);
322 break;
324 case DATA_TYPE_BOOLEAN_ARRAY:
325 rv = nvlist_add_boolean_array(nv, key, value, size);
326 break;
328 case DATA_TYPE_INT8_ARRAY:
329 rv = nvlist_add_int8_array(nv, key, value, size);
330 break;
332 case DATA_TYPE_UINT8_ARRAY:
333 rv = nvlist_add_uint8_array(nv, key, value, size);
334 break;
336 default:
337 return (ENOTSUP);
340 return (rv);
344 lzbe_remove_pair(void *ptr, const char *key)
347 return (nvlist_remove_all(ptr, key));