1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <libintl.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include "volume_nvpair.h"
32*0Sstevel@tonic-gate #include "volume_error.h"
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate * ******************************************************************
36*0Sstevel@tonic-gate *
37*0Sstevel@tonic-gate * Function prototypes
38*0Sstevel@tonic-gate *
39*0Sstevel@tonic-gate * ******************************************************************
40*0Sstevel@tonic-gate */
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate static nvpair_t *nvlist_walk_nvpair(nvlist_t *nvl,
43*0Sstevel@tonic-gate const char *name, data_type_t type, nvpair_t *nvp);
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate * ******************************************************************
47*0Sstevel@tonic-gate *
48*0Sstevel@tonic-gate * External functions
49*0Sstevel@tonic-gate *
50*0Sstevel@tonic-gate * ******************************************************************
51*0Sstevel@tonic-gate */
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate * Get the named uint16 from the given nvlist_t.
55*0Sstevel@tonic-gate *
56*0Sstevel@tonic-gate * @param attrs
57*0Sstevel@tonic-gate * the nvlist_t to search
58*0Sstevel@tonic-gate *
59*0Sstevel@tonic-gate * @param which
60*0Sstevel@tonic-gate * the string key for this element in the list
61*0Sstevel@tonic-gate *
62*0Sstevel@tonic-gate * @param val
63*0Sstevel@tonic-gate * RETURN: the value of the requested uint16
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * @return 0
66*0Sstevel@tonic-gate * if successful
67*0Sstevel@tonic-gate *
68*0Sstevel@tonic-gate * @return ENOENT
69*0Sstevel@tonic-gate * if no matching name-value pair is found
70*0Sstevel@tonic-gate */
71*0Sstevel@tonic-gate int
get_uint16(nvlist_t * attrs,char * which,uint16_t * val)72*0Sstevel@tonic-gate get_uint16(
73*0Sstevel@tonic-gate nvlist_t *attrs,
74*0Sstevel@tonic-gate char *which,
75*0Sstevel@tonic-gate uint16_t *val)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate int error;
78*0Sstevel@tonic-gate nvpair_t *match =
79*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_UINT16, NULL);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate if (match == NULL) {
82*0Sstevel@tonic-gate error = ENOENT;
83*0Sstevel@tonic-gate } else {
84*0Sstevel@tonic-gate error = nvpair_value_uint16(match, val);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate return (error);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate * Set the named uint16 in the given nvlist_t.
92*0Sstevel@tonic-gate *
93*0Sstevel@tonic-gate * @param attrs
94*0Sstevel@tonic-gate * the nvlist_t to search
95*0Sstevel@tonic-gate *
96*0Sstevel@tonic-gate * @param which
97*0Sstevel@tonic-gate * the string key for this element in the list
98*0Sstevel@tonic-gate *
99*0Sstevel@tonic-gate * @param val
100*0Sstevel@tonic-gate * the value to set
101*0Sstevel@tonic-gate *
102*0Sstevel@tonic-gate * @return 0
103*0Sstevel@tonic-gate * if successful
104*0Sstevel@tonic-gate *
105*0Sstevel@tonic-gate * @return EINVAL
106*0Sstevel@tonic-gate * if there is an invalid argument
107*0Sstevel@tonic-gate *
108*0Sstevel@tonic-gate * @return ENOMEM
109*0Sstevel@tonic-gate * if there is insufficient memory
110*0Sstevel@tonic-gate */
111*0Sstevel@tonic-gate int
set_uint16(nvlist_t * attrs,char * which,uint16_t val)112*0Sstevel@tonic-gate set_uint16(
113*0Sstevel@tonic-gate nvlist_t *attrs,
114*0Sstevel@tonic-gate char *which,
115*0Sstevel@tonic-gate uint16_t val)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate int error = 0;
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate if ((error = nvlist_add_uint16(attrs, which, val)) != 0) {
120*0Sstevel@tonic-gate volume_set_error(
121*0Sstevel@tonic-gate gettext("nvlist_add_int16(%s) failed: %d\n"), which, error);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate return (error);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate * Get the named uint32 from the given nvlist_t.
129*0Sstevel@tonic-gate *
130*0Sstevel@tonic-gate * @param attrs
131*0Sstevel@tonic-gate * the nvlist_t to search
132*0Sstevel@tonic-gate *
133*0Sstevel@tonic-gate * @param which
134*0Sstevel@tonic-gate * the string key for this element in the list
135*0Sstevel@tonic-gate *
136*0Sstevel@tonic-gate * @param val
137*0Sstevel@tonic-gate * RETURN: the value of the requested uint32
138*0Sstevel@tonic-gate *
139*0Sstevel@tonic-gate * @return 0
140*0Sstevel@tonic-gate * if successful
141*0Sstevel@tonic-gate *
142*0Sstevel@tonic-gate * @return ENOENT
143*0Sstevel@tonic-gate * if no matching name-value pair is found
144*0Sstevel@tonic-gate */
145*0Sstevel@tonic-gate int
get_uint32(nvlist_t * attrs,char * which,uint32_t * val)146*0Sstevel@tonic-gate get_uint32(
147*0Sstevel@tonic-gate nvlist_t *attrs,
148*0Sstevel@tonic-gate char *which,
149*0Sstevel@tonic-gate uint32_t *val)
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate int error;
152*0Sstevel@tonic-gate nvpair_t *match =
153*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_UINT32, NULL);
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate if (match == NULL) {
156*0Sstevel@tonic-gate error = ENOENT;
157*0Sstevel@tonic-gate } else {
158*0Sstevel@tonic-gate error = nvpair_value_uint32(match, val);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate return (error);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate * Set the named uint32 in the given nvlist_t.
166*0Sstevel@tonic-gate *
167*0Sstevel@tonic-gate * @param attrs
168*0Sstevel@tonic-gate * the nvlist_t to search
169*0Sstevel@tonic-gate *
170*0Sstevel@tonic-gate * @param which
171*0Sstevel@tonic-gate * the string key for this element in the list
172*0Sstevel@tonic-gate *
173*0Sstevel@tonic-gate * @param val
174*0Sstevel@tonic-gate * the value to set
175*0Sstevel@tonic-gate *
176*0Sstevel@tonic-gate * @return 0
177*0Sstevel@tonic-gate * if successful
178*0Sstevel@tonic-gate *
179*0Sstevel@tonic-gate * @return EINVAL
180*0Sstevel@tonic-gate * if there is an invalid argument
181*0Sstevel@tonic-gate *
182*0Sstevel@tonic-gate * @return ENOMEM
183*0Sstevel@tonic-gate * if there is insufficient memory
184*0Sstevel@tonic-gate */
185*0Sstevel@tonic-gate int
set_uint32(nvlist_t * attrs,char * which,uint32_t val)186*0Sstevel@tonic-gate set_uint32(
187*0Sstevel@tonic-gate nvlist_t *attrs,
188*0Sstevel@tonic-gate char *which,
189*0Sstevel@tonic-gate uint32_t val)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate int error = 0;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate if ((error = nvlist_add_uint32(attrs, which, val)) != 0) {
194*0Sstevel@tonic-gate volume_set_error(
195*0Sstevel@tonic-gate gettext("nvlist_add_int32(%s) failed: %d\n"), which, error);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate return (error);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate * Get the named uint64 from the given nvlist_t.
203*0Sstevel@tonic-gate *
204*0Sstevel@tonic-gate * @param attrs
205*0Sstevel@tonic-gate * the nvlist_t to search
206*0Sstevel@tonic-gate *
207*0Sstevel@tonic-gate * @param which
208*0Sstevel@tonic-gate * the string key for this element in the list
209*0Sstevel@tonic-gate *
210*0Sstevel@tonic-gate * @param val
211*0Sstevel@tonic-gate * RETURN: the value of the requested uint64
212*0Sstevel@tonic-gate *
213*0Sstevel@tonic-gate * @return 0
214*0Sstevel@tonic-gate * if successful
215*0Sstevel@tonic-gate *
216*0Sstevel@tonic-gate * @return ENOENT
217*0Sstevel@tonic-gate * if no matching name-value pair is found
218*0Sstevel@tonic-gate */
219*0Sstevel@tonic-gate int
get_uint64(nvlist_t * attrs,char * which,uint64_t * val)220*0Sstevel@tonic-gate get_uint64(
221*0Sstevel@tonic-gate nvlist_t *attrs,
222*0Sstevel@tonic-gate char *which,
223*0Sstevel@tonic-gate uint64_t *val)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate int error;
226*0Sstevel@tonic-gate nvpair_t *match =
227*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_UINT64, NULL);
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate if (match == NULL) {
230*0Sstevel@tonic-gate error = ENOENT;
231*0Sstevel@tonic-gate } else {
232*0Sstevel@tonic-gate error = nvpair_value_uint64(match, val);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate return (error);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate * Set the named uint64 in the given nvlist_t.
240*0Sstevel@tonic-gate *
241*0Sstevel@tonic-gate * @param attrs
242*0Sstevel@tonic-gate * the nvlist_t to search
243*0Sstevel@tonic-gate *
244*0Sstevel@tonic-gate * @param which
245*0Sstevel@tonic-gate * the string key for this element in the list
246*0Sstevel@tonic-gate *
247*0Sstevel@tonic-gate * @param val
248*0Sstevel@tonic-gate * the value to set
249*0Sstevel@tonic-gate *
250*0Sstevel@tonic-gate * @return 0
251*0Sstevel@tonic-gate * if successful
252*0Sstevel@tonic-gate *
253*0Sstevel@tonic-gate * @return EINVAL
254*0Sstevel@tonic-gate * if there is an invalid argument
255*0Sstevel@tonic-gate *
256*0Sstevel@tonic-gate * @return ENOMEM
257*0Sstevel@tonic-gate * if there is insufficient memory
258*0Sstevel@tonic-gate */
259*0Sstevel@tonic-gate int
set_uint64(nvlist_t * attrs,char * which,uint64_t val)260*0Sstevel@tonic-gate set_uint64(
261*0Sstevel@tonic-gate nvlist_t *attrs,
262*0Sstevel@tonic-gate char *which,
263*0Sstevel@tonic-gate uint64_t val)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate int error = 0;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate if ((error = nvlist_add_uint64(attrs, which, val)) != 0) {
268*0Sstevel@tonic-gate volume_set_error(
269*0Sstevel@tonic-gate gettext("nvlist_add_int64(%s) failed: %d\n"), which, error);
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate
272*0Sstevel@tonic-gate return (error);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate * Set the named boolean in the given nvlist_t.
277*0Sstevel@tonic-gate *
278*0Sstevel@tonic-gate * @param attrs
279*0Sstevel@tonic-gate * the nvlist_t to search
280*0Sstevel@tonic-gate *
281*0Sstevel@tonic-gate * @param which
282*0Sstevel@tonic-gate * the string key for this element in the list
283*0Sstevel@tonic-gate *
284*0Sstevel@tonic-gate * @param val
285*0Sstevel@tonic-gate * the value to set
286*0Sstevel@tonic-gate *
287*0Sstevel@tonic-gate * @return 0
288*0Sstevel@tonic-gate * if successful
289*0Sstevel@tonic-gate *
290*0Sstevel@tonic-gate * @return EINVAL
291*0Sstevel@tonic-gate * if there is an invalid argument
292*0Sstevel@tonic-gate *
293*0Sstevel@tonic-gate * @return ENOMEM
294*0Sstevel@tonic-gate * if there is insufficient memory
295*0Sstevel@tonic-gate */
296*0Sstevel@tonic-gate int
set_boolean(nvlist_t * attrs,char * which,boolean_t val)297*0Sstevel@tonic-gate set_boolean(
298*0Sstevel@tonic-gate nvlist_t *attrs,
299*0Sstevel@tonic-gate char *which,
300*0Sstevel@tonic-gate boolean_t val)
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate /*
303*0Sstevel@tonic-gate * Use set_uint16 to distinguish "attr = B_FALSE" from
304*0Sstevel@tonic-gate * "attribute unset".
305*0Sstevel@tonic-gate */
306*0Sstevel@tonic-gate return (set_uint16(attrs, which, val == B_TRUE ? 1 : 0));
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate * Get the named boolean from the given nvlist_t.
311*0Sstevel@tonic-gate *
312*0Sstevel@tonic-gate * @param attrs
313*0Sstevel@tonic-gate * the nvlist_t to search
314*0Sstevel@tonic-gate *
315*0Sstevel@tonic-gate * @param which
316*0Sstevel@tonic-gate * the string key for this element in the list
317*0Sstevel@tonic-gate *
318*0Sstevel@tonic-gate * @param boolval
319*0Sstevel@tonic-gate * RETURN: the value of the requested boolean
320*0Sstevel@tonic-gate *
321*0Sstevel@tonic-gate * @return 0
322*0Sstevel@tonic-gate * if successful
323*0Sstevel@tonic-gate *
324*0Sstevel@tonic-gate * @return ENOENT
325*0Sstevel@tonic-gate * if no matching name-value pair is found
326*0Sstevel@tonic-gate */
327*0Sstevel@tonic-gate int
get_boolean(nvlist_t * attrs,char * which,boolean_t * boolval)328*0Sstevel@tonic-gate get_boolean(
329*0Sstevel@tonic-gate nvlist_t *attrs,
330*0Sstevel@tonic-gate char *which,
331*0Sstevel@tonic-gate boolean_t *boolval)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate int error;
334*0Sstevel@tonic-gate uint16_t val;
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate /*
337*0Sstevel@tonic-gate * Use get_uint16 to distinguish "attr = B_FALSE" from
338*0Sstevel@tonic-gate * "attribute unset".
339*0Sstevel@tonic-gate */
340*0Sstevel@tonic-gate if ((error = get_uint16(attrs, which, &val)) == 0) {
341*0Sstevel@tonic-gate *boolval = (val ? B_TRUE : B_FALSE);
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate
344*0Sstevel@tonic-gate return (error);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate /*
348*0Sstevel@tonic-gate * Get the named string from the given nvlist_t.
349*0Sstevel@tonic-gate *
350*0Sstevel@tonic-gate * @param attrs
351*0Sstevel@tonic-gate * the nvlist_t to search
352*0Sstevel@tonic-gate *
353*0Sstevel@tonic-gate * @param which
354*0Sstevel@tonic-gate * the string key for this element in the list
355*0Sstevel@tonic-gate *
356*0Sstevel@tonic-gate * @param str
357*0Sstevel@tonic-gate * RETURN: the requested string
358*0Sstevel@tonic-gate *
359*0Sstevel@tonic-gate * @return 0
360*0Sstevel@tonic-gate * if successful
361*0Sstevel@tonic-gate *
362*0Sstevel@tonic-gate * @return ENOENT
363*0Sstevel@tonic-gate * if no matching name-value pair is found
364*0Sstevel@tonic-gate */
365*0Sstevel@tonic-gate int
get_string(nvlist_t * attrs,char * which,char ** str)366*0Sstevel@tonic-gate get_string(
367*0Sstevel@tonic-gate nvlist_t *attrs,
368*0Sstevel@tonic-gate char *which,
369*0Sstevel@tonic-gate char **str)
370*0Sstevel@tonic-gate {
371*0Sstevel@tonic-gate int error;
372*0Sstevel@tonic-gate nvpair_t *match =
373*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_STRING, NULL);
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gate if (match == NULL) {
376*0Sstevel@tonic-gate error = ENOENT;
377*0Sstevel@tonic-gate } else {
378*0Sstevel@tonic-gate error = nvpair_value_string(match, str);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate return (error);
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate /*
385*0Sstevel@tonic-gate * Set the named string in the given nvlist_t.
386*0Sstevel@tonic-gate *
387*0Sstevel@tonic-gate * @param attrs
388*0Sstevel@tonic-gate * the nvlist_t to search
389*0Sstevel@tonic-gate *
390*0Sstevel@tonic-gate * @param which
391*0Sstevel@tonic-gate * the string key for this element in the list
392*0Sstevel@tonic-gate *
393*0Sstevel@tonic-gate * @param val
394*0Sstevel@tonic-gate * the value to set
395*0Sstevel@tonic-gate *
396*0Sstevel@tonic-gate * @return 0
397*0Sstevel@tonic-gate * if successful
398*0Sstevel@tonic-gate *
399*0Sstevel@tonic-gate * @return EINVAL
400*0Sstevel@tonic-gate * if there is an invalid argument
401*0Sstevel@tonic-gate *
402*0Sstevel@tonic-gate * @return ENOMEM
403*0Sstevel@tonic-gate * if there is insufficient memory
404*0Sstevel@tonic-gate */
405*0Sstevel@tonic-gate int
set_string(nvlist_t * attrs,char * which,char * val)406*0Sstevel@tonic-gate set_string(
407*0Sstevel@tonic-gate nvlist_t *attrs,
408*0Sstevel@tonic-gate char *which,
409*0Sstevel@tonic-gate char *val)
410*0Sstevel@tonic-gate {
411*0Sstevel@tonic-gate int error = 0;
412*0Sstevel@tonic-gate
413*0Sstevel@tonic-gate if ((error = nvlist_add_string(attrs, which, val)) != 0) {
414*0Sstevel@tonic-gate volume_set_error(
415*0Sstevel@tonic-gate gettext("nvlist_add_string(%s) failed: %d\n"), which, error);
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gate return (error);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate
421*0Sstevel@tonic-gate /*
422*0Sstevel@tonic-gate * Get the named uint16 array from the given nvlist_t.
423*0Sstevel@tonic-gate *
424*0Sstevel@tonic-gate * @param attrs
425*0Sstevel@tonic-gate * the nvlist_t to search
426*0Sstevel@tonic-gate *
427*0Sstevel@tonic-gate * @param which
428*0Sstevel@tonic-gate * the string key for this element in the list
429*0Sstevel@tonic-gate *
430*0Sstevel@tonic-gate * @param val
431*0Sstevel@tonic-gate * RETURN: the value of the requested uint16 array
432*0Sstevel@tonic-gate *
433*0Sstevel@tonic-gate * @param nelem
434*0Sstevel@tonic-gate * RETURN: the number of elements in the array
435*0Sstevel@tonic-gate *
436*0Sstevel@tonic-gate * @return 0
437*0Sstevel@tonic-gate * if successful
438*0Sstevel@tonic-gate *
439*0Sstevel@tonic-gate * @return ENOENT
440*0Sstevel@tonic-gate * if no matching name-value pair is found
441*0Sstevel@tonic-gate */
442*0Sstevel@tonic-gate int
get_uint16_array(nvlist_t * attrs,char * which,uint16_t ** val,uint_t * nelem)443*0Sstevel@tonic-gate get_uint16_array(
444*0Sstevel@tonic-gate nvlist_t *attrs,
445*0Sstevel@tonic-gate char *which,
446*0Sstevel@tonic-gate uint16_t **val,
447*0Sstevel@tonic-gate uint_t *nelem)
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate int error;
450*0Sstevel@tonic-gate nvpair_t *match =
451*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_UINT16_ARRAY, NULL);
452*0Sstevel@tonic-gate
453*0Sstevel@tonic-gate if (match == NULL) {
454*0Sstevel@tonic-gate error = ENOENT;
455*0Sstevel@tonic-gate } else {
456*0Sstevel@tonic-gate error = nvpair_value_uint16_array(match, val, nelem);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate
459*0Sstevel@tonic-gate return (error);
460*0Sstevel@tonic-gate }
461*0Sstevel@tonic-gate
462*0Sstevel@tonic-gate /*
463*0Sstevel@tonic-gate * Set the named uint16 array from the given nvlist_t.
464*0Sstevel@tonic-gate *
465*0Sstevel@tonic-gate * @param attrs
466*0Sstevel@tonic-gate * the nvlist_t to search
467*0Sstevel@tonic-gate *
468*0Sstevel@tonic-gate * @param which
469*0Sstevel@tonic-gate * the string key for this element in the list
470*0Sstevel@tonic-gate *
471*0Sstevel@tonic-gate * @param val
472*0Sstevel@tonic-gate * the value of the requested uint16 array
473*0Sstevel@tonic-gate *
474*0Sstevel@tonic-gate * @param nelem
475*0Sstevel@tonic-gate * the number of elements in the array
476*0Sstevel@tonic-gate *
477*0Sstevel@tonic-gate * @return 0
478*0Sstevel@tonic-gate * if successful
479*0Sstevel@tonic-gate *
480*0Sstevel@tonic-gate * @return EINVAL
481*0Sstevel@tonic-gate * if there is an invalid argument
482*0Sstevel@tonic-gate *
483*0Sstevel@tonic-gate * @return ENOMEM
484*0Sstevel@tonic-gate * if there is insufficient memory
485*0Sstevel@tonic-gate */
486*0Sstevel@tonic-gate int
set_uint16_array(nvlist_t * attrs,char * which,uint16_t * val,uint_t nelem)487*0Sstevel@tonic-gate set_uint16_array(
488*0Sstevel@tonic-gate nvlist_t *attrs,
489*0Sstevel@tonic-gate char *which,
490*0Sstevel@tonic-gate uint16_t *val,
491*0Sstevel@tonic-gate uint_t nelem)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate int error = 0;
494*0Sstevel@tonic-gate
495*0Sstevel@tonic-gate if ((error = nvlist_add_uint16_array(
496*0Sstevel@tonic-gate attrs, which, val, nelem)) != 0) {
497*0Sstevel@tonic-gate volume_set_error(
498*0Sstevel@tonic-gate gettext("nvlist_add_uint16_array(%s) failed: %d.\n"),
499*0Sstevel@tonic-gate which, error);
500*0Sstevel@tonic-gate }
501*0Sstevel@tonic-gate
502*0Sstevel@tonic-gate return (error);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate
505*0Sstevel@tonic-gate /*
506*0Sstevel@tonic-gate * Get the named uint64 array from the given nvlist_t.
507*0Sstevel@tonic-gate *
508*0Sstevel@tonic-gate * @param attrs
509*0Sstevel@tonic-gate * the nvlist_t to search
510*0Sstevel@tonic-gate *
511*0Sstevel@tonic-gate * @param which
512*0Sstevel@tonic-gate * the string key for this element in the list
513*0Sstevel@tonic-gate *
514*0Sstevel@tonic-gate * @param val
515*0Sstevel@tonic-gate * RETURN: the value of the requested uint64 array
516*0Sstevel@tonic-gate *
517*0Sstevel@tonic-gate * @param nelem
518*0Sstevel@tonic-gate * RETURN: the number of elements in the array
519*0Sstevel@tonic-gate *
520*0Sstevel@tonic-gate * @return 0
521*0Sstevel@tonic-gate * if successful
522*0Sstevel@tonic-gate *
523*0Sstevel@tonic-gate * @return ENOENT
524*0Sstevel@tonic-gate * if no matching name-value pair is found
525*0Sstevel@tonic-gate */
526*0Sstevel@tonic-gate int
get_uint64_array(nvlist_t * attrs,char * which,uint64_t ** val,uint_t * nelem)527*0Sstevel@tonic-gate get_uint64_array(
528*0Sstevel@tonic-gate nvlist_t *attrs,
529*0Sstevel@tonic-gate char *which,
530*0Sstevel@tonic-gate uint64_t **val,
531*0Sstevel@tonic-gate uint_t *nelem)
532*0Sstevel@tonic-gate {
533*0Sstevel@tonic-gate int error;
534*0Sstevel@tonic-gate nvpair_t *match =
535*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_UINT64_ARRAY, NULL);
536*0Sstevel@tonic-gate
537*0Sstevel@tonic-gate if (match == NULL) {
538*0Sstevel@tonic-gate error = ENOENT;
539*0Sstevel@tonic-gate } else {
540*0Sstevel@tonic-gate error = nvpair_value_uint64_array(match, val, nelem);
541*0Sstevel@tonic-gate }
542*0Sstevel@tonic-gate
543*0Sstevel@tonic-gate return (error);
544*0Sstevel@tonic-gate }
545*0Sstevel@tonic-gate
546*0Sstevel@tonic-gate /*
547*0Sstevel@tonic-gate * Set the named uint64 array from the given nvlist_t.
548*0Sstevel@tonic-gate *
549*0Sstevel@tonic-gate * @param attrs
550*0Sstevel@tonic-gate * the nvlist_t to search
551*0Sstevel@tonic-gate *
552*0Sstevel@tonic-gate * @param which
553*0Sstevel@tonic-gate * the string key for this element in the list
554*0Sstevel@tonic-gate *
555*0Sstevel@tonic-gate * @param val
556*0Sstevel@tonic-gate * the value of the requested uint64 array
557*0Sstevel@tonic-gate *
558*0Sstevel@tonic-gate * @param nelem
559*0Sstevel@tonic-gate * the number of elements in the array
560*0Sstevel@tonic-gate *
561*0Sstevel@tonic-gate * @return 0
562*0Sstevel@tonic-gate * if successful
563*0Sstevel@tonic-gate *
564*0Sstevel@tonic-gate * @return EINVAL
565*0Sstevel@tonic-gate * if there is an invalid argument
566*0Sstevel@tonic-gate *
567*0Sstevel@tonic-gate * @return ENOMEM
568*0Sstevel@tonic-gate * if there is insufficient memory
569*0Sstevel@tonic-gate */
570*0Sstevel@tonic-gate int
set_uint64_array(nvlist_t * attrs,char * which,uint64_t * val,uint_t nelem)571*0Sstevel@tonic-gate set_uint64_array(
572*0Sstevel@tonic-gate nvlist_t *attrs,
573*0Sstevel@tonic-gate char *which,
574*0Sstevel@tonic-gate uint64_t *val,
575*0Sstevel@tonic-gate uint_t nelem)
576*0Sstevel@tonic-gate {
577*0Sstevel@tonic-gate int error = 0;
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate if ((error = nvlist_add_uint64_array(
580*0Sstevel@tonic-gate attrs, which, val, nelem)) != 0) {
581*0Sstevel@tonic-gate volume_set_error(
582*0Sstevel@tonic-gate gettext("nvlist_add_uint64_array(%s) failed: %d.\n"),
583*0Sstevel@tonic-gate which, error);
584*0Sstevel@tonic-gate }
585*0Sstevel@tonic-gate
586*0Sstevel@tonic-gate return (error);
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate /*
590*0Sstevel@tonic-gate * Get the named string array from the given nvlist_t.
591*0Sstevel@tonic-gate *
592*0Sstevel@tonic-gate * @param attrs
593*0Sstevel@tonic-gate * the nvlist_t to search
594*0Sstevel@tonic-gate *
595*0Sstevel@tonic-gate * @param which
596*0Sstevel@tonic-gate * the string key for this element in the list
597*0Sstevel@tonic-gate *
598*0Sstevel@tonic-gate * @param val
599*0Sstevel@tonic-gate * RETURN: the value of the requested string array
600*0Sstevel@tonic-gate *
601*0Sstevel@tonic-gate * @param nelem
602*0Sstevel@tonic-gate * RETURN: the number of elements in the array
603*0Sstevel@tonic-gate *
604*0Sstevel@tonic-gate * @return 0
605*0Sstevel@tonic-gate * if successful
606*0Sstevel@tonic-gate *
607*0Sstevel@tonic-gate * @return ENOENT
608*0Sstevel@tonic-gate * if no matching name-value pair is found
609*0Sstevel@tonic-gate */
610*0Sstevel@tonic-gate int
get_string_array(nvlist_t * attrs,char * which,char *** val,uint_t * nelem)611*0Sstevel@tonic-gate get_string_array(
612*0Sstevel@tonic-gate nvlist_t *attrs,
613*0Sstevel@tonic-gate char *which,
614*0Sstevel@tonic-gate char ***val,
615*0Sstevel@tonic-gate uint_t *nelem)
616*0Sstevel@tonic-gate {
617*0Sstevel@tonic-gate int error;
618*0Sstevel@tonic-gate nvpair_t *match =
619*0Sstevel@tonic-gate nvlist_walk_nvpair(attrs, which, DATA_TYPE_STRING_ARRAY, NULL);
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate if (match == NULL) {
622*0Sstevel@tonic-gate error = ENOENT;
623*0Sstevel@tonic-gate } else {
624*0Sstevel@tonic-gate error = nvpair_value_string_array(match, val, nelem);
625*0Sstevel@tonic-gate }
626*0Sstevel@tonic-gate
627*0Sstevel@tonic-gate return (error);
628*0Sstevel@tonic-gate }
629*0Sstevel@tonic-gate
630*0Sstevel@tonic-gate /*
631*0Sstevel@tonic-gate * Set the named string array from the given nvlist_t.
632*0Sstevel@tonic-gate *
633*0Sstevel@tonic-gate * @param attrs
634*0Sstevel@tonic-gate * the nvlist_t to search
635*0Sstevel@tonic-gate *
636*0Sstevel@tonic-gate * @param which
637*0Sstevel@tonic-gate * the string key for this element in the list
638*0Sstevel@tonic-gate *
639*0Sstevel@tonic-gate * @param val
640*0Sstevel@tonic-gate * the value of the requested string array
641*0Sstevel@tonic-gate *
642*0Sstevel@tonic-gate * @param nelem
643*0Sstevel@tonic-gate * the number of elements in the array
644*0Sstevel@tonic-gate *
645*0Sstevel@tonic-gate * @return 0
646*0Sstevel@tonic-gate * if successful
647*0Sstevel@tonic-gate *
648*0Sstevel@tonic-gate * @return EINVAL
649*0Sstevel@tonic-gate * if there is an invalid argument
650*0Sstevel@tonic-gate *
651*0Sstevel@tonic-gate * @return ENOMEM
652*0Sstevel@tonic-gate * if there is insufficient memory
653*0Sstevel@tonic-gate */
654*0Sstevel@tonic-gate int
set_string_array(nvlist_t * attrs,char * which,char ** val,uint_t nelem)655*0Sstevel@tonic-gate set_string_array(
656*0Sstevel@tonic-gate nvlist_t *attrs,
657*0Sstevel@tonic-gate char *which,
658*0Sstevel@tonic-gate char **val,
659*0Sstevel@tonic-gate uint_t nelem)
660*0Sstevel@tonic-gate {
661*0Sstevel@tonic-gate int error = 0;
662*0Sstevel@tonic-gate
663*0Sstevel@tonic-gate if ((error = nvlist_add_string_array(
664*0Sstevel@tonic-gate attrs, which, val, nelem)) != 0) {
665*0Sstevel@tonic-gate volume_set_error(
666*0Sstevel@tonic-gate gettext("nvlist_add_string_array(%s) failed: %d.\n"),
667*0Sstevel@tonic-gate which, error);
668*0Sstevel@tonic-gate }
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gate return (error);
671*0Sstevel@tonic-gate }
672*0Sstevel@tonic-gate
673*0Sstevel@tonic-gate /*
674*0Sstevel@tonic-gate * ******************************************************************
675*0Sstevel@tonic-gate *
676*0Sstevel@tonic-gate * Static functions
677*0Sstevel@tonic-gate *
678*0Sstevel@tonic-gate * ******************************************************************
679*0Sstevel@tonic-gate */
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gate /*
682*0Sstevel@tonic-gate * Get a handle to the next nvpair with the specified name and data
683*0Sstevel@tonic-gate * type in the list following the given nvpair.
684*0Sstevel@tonic-gate *
685*0Sstevel@tonic-gate * Some variation of this function will likely appear in the libnvpair
686*0Sstevel@tonic-gate * library per 4981923.
687*0Sstevel@tonic-gate *
688*0Sstevel@tonic-gate * @param nvl
689*0Sstevel@tonic-gate * the nvlist_t to search
690*0Sstevel@tonic-gate *
691*0Sstevel@tonic-gate * @param name
692*0Sstevel@tonic-gate * the string key for the pair to find in the list, or
693*0Sstevel@tonic-gate * NULL to match any name
694*0Sstevel@tonic-gate *
695*0Sstevel@tonic-gate * @param type
696*0Sstevel@tonic-gate * the data type for the pair to find in the list, or
697*0Sstevel@tonic-gate * DATA_TYPE_UNKNOWN to match any type
698*0Sstevel@tonic-gate *
699*0Sstevel@tonic-gate * @param nvp
700*0Sstevel@tonic-gate * the pair to search from in the list, or NULL to search
701*0Sstevel@tonic-gate * from the beginning of the list
702*0Sstevel@tonic-gate *
703*0Sstevel@tonic-gate * @return the next nvpair in the list matching the given
704*0Sstevel@tonic-gate * criteria, or NULL if no matching nvpair is found
705*0Sstevel@tonic-gate */
706*0Sstevel@tonic-gate static nvpair_t *
nvlist_walk_nvpair(nvlist_t * nvl,const char * name,data_type_t type,nvpair_t * nvp)707*0Sstevel@tonic-gate nvlist_walk_nvpair(
708*0Sstevel@tonic-gate nvlist_t *nvl,
709*0Sstevel@tonic-gate const char *name,
710*0Sstevel@tonic-gate data_type_t type,
711*0Sstevel@tonic-gate nvpair_t *nvp)
712*0Sstevel@tonic-gate {
713*0Sstevel@tonic-gate /* For each nvpair in the list following nvp... */
714*0Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
715*0Sstevel@tonic-gate
716*0Sstevel@tonic-gate /* Does this pair's name match the given name? */
717*0Sstevel@tonic-gate if ((name == NULL || strcmp(nvpair_name(nvp), name) == 0) &&
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gate /* Does this pair's type match the given type? */
720*0Sstevel@tonic-gate (type == DATA_TYPE_UNKNOWN || type == nvpair_type(nvp))) {
721*0Sstevel@tonic-gate return (nvp);
722*0Sstevel@tonic-gate }
723*0Sstevel@tonic-gate }
724*0Sstevel@tonic-gate
725*0Sstevel@tonic-gate return (NULL);
726*0Sstevel@tonic-gate }
727