xref: /onnv-gate/usr/src/cmd/lvm/metassist/xml/xml_convert.c (revision 0:68f95e015346)
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 "xml_convert.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <libxslt/xslt.h>
35*0Sstevel@tonic-gate #include <libxslt/xsltInternals.h>
36*0Sstevel@tonic-gate #include <libxslt/transform.h>
37*0Sstevel@tonic-gate #include <libxslt/xsltutils.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include "volume_error.h"
41*0Sstevel@tonic-gate #include "volume_output.h"
42*0Sstevel@tonic-gate #include "volume_string.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * IDs for localized messages in the generated command script
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	CMD_MSG_ENVIRONMENT		"Environment"
49*0Sstevel@tonic-gate #define	CMD_MSG_AMEND_PATH		"Amend PATH"
50*0Sstevel@tonic-gate #define	CMD_MSG_DISK_SET_NAME		"Disk set name"
51*0Sstevel@tonic-gate #define	CMD_MSG_FUNCTIONS		"Functions"
52*0Sstevel@tonic-gate /* CSTYLED */
53*0Sstevel@tonic-gate #define	CMD_MSG_ECHO_AND_EXEC		"Echo (verbose) and exec given command, exit on error"
54*0Sstevel@tonic-gate #define	CMD_MSG_GET_FULL_PATH		"Get full /dev/rdsk path of given slice"
55*0Sstevel@tonic-gate /* CSTYLED */
56*0Sstevel@tonic-gate #define	CMD_MSG_FMTHARD_SPECIAL		"Run fmthard, ignore partboot error, error if output"
57*0Sstevel@tonic-gate #define	CMD_MSG_MAIN			"Main"
58*0Sstevel@tonic-gate #define	CMD_MSG_VERIFY_ROOT		"Verify root"
59*0Sstevel@tonic-gate #define	CMD_MSG_RUN_AS_ROOT		"This script must be run as root."
60*0Sstevel@tonic-gate #define	CMD_MSG_CHECK_FOR_VERBOSE	"Check for verbose option"
61*0Sstevel@tonic-gate #define	CMD_MSG_DOES_DISK_SET_EXIST	"Does the disk set exist?"
62*0Sstevel@tonic-gate #define	CMD_MSG_TAKE_DISK_SET		"Take control of disk set"
63*0Sstevel@tonic-gate #define	CMD_MSG_CREATE_THE_DISK_SET	"Create the disk set"
64*0Sstevel@tonic-gate #define	CMD_MSG_ADD_DISKS_TO_SET	"Add disks to set"
65*0Sstevel@tonic-gate #define	CMD_MSG_FORMAT_SLICES		"Format slices"
66*0Sstevel@tonic-gate #define	CMD_MSG_CREATE			"Create {1} {2}"
67*0Sstevel@tonic-gate #define	CMD_MSG_DOES_EXIST		"Does {1} exist?"
68*0Sstevel@tonic-gate #define	CMD_MSG_ADD_SLICES_TO		"Add slices to {1}"
69*0Sstevel@tonic-gate /* CSTYLED */
70*0Sstevel@tonic-gate #define	CMD_MSG_ASSOCIATE_WITH_HSP	"Associate {1} {2} with hot spare pool {3}"
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * ******************************************************************
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  * Data types
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  * ******************************************************************
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * Encapsulates the parsing of an XML attribute
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate typedef struct {
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* The name of the attribute */
86*0Sstevel@tonic-gate 	char *name;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	/*
89*0Sstevel@tonic-gate 	 * A function to validate and set the XML attribute value in
90*0Sstevel@tonic-gate 	 * the given devconfig_t structure.
91*0Sstevel@tonic-gate 	 *
92*0Sstevel@tonic-gate 	 * @param	    name
93*0Sstevel@tonic-gate 	 *		    the name of the XML attribute
94*0Sstevel@tonic-gate 	 *
95*0Sstevel@tonic-gate 	 * @param	    value
96*0Sstevel@tonic-gate 	 *		    the value of the XML attribute
97*0Sstevel@tonic-gate 	 *
98*0Sstevel@tonic-gate 	 * @return	    0 if the given value was valid and set
99*0Sstevel@tonic-gate 	 *		    successfully, non-zero otherwise.
100*0Sstevel@tonic-gate 	 */
101*0Sstevel@tonic-gate 	int (*validate_set)(devconfig_t *device, char *name, char *value);
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/*
104*0Sstevel@tonic-gate 	 * A function to get the XML attribute value in the given
105*0Sstevel@tonic-gate 	 * devconfig_t structure.
106*0Sstevel@tonic-gate 	 *
107*0Sstevel@tonic-gate 	 * @param	    name
108*0Sstevel@tonic-gate 	 *		    the name of the XML attribute
109*0Sstevel@tonic-gate 	 *
110*0Sstevel@tonic-gate 	 * @param	    value
111*0Sstevel@tonic-gate 	 *		    the value of the XML attribute
112*0Sstevel@tonic-gate 	 *
113*0Sstevel@tonic-gate 	 * @return	    0 if the given value was retrieved
114*0Sstevel@tonic-gate 	 *		    successfully, non-zero otherwise.
115*0Sstevel@tonic-gate 	 */
116*0Sstevel@tonic-gate 	int (*get_as_string)(devconfig_t *device, char *name, char **value);
117*0Sstevel@tonic-gate } attr_t;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /*
120*0Sstevel@tonic-gate  * Encapsulates the parsing of an XML element
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate typedef struct {
123*0Sstevel@tonic-gate 	/* The name of the element */
124*0Sstevel@tonic-gate 	char *name;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/* The type of element to set in the devconfig_t */
127*0Sstevel@tonic-gate 	component_type_t type;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/*
130*0Sstevel@tonic-gate 	 * When converting from XML to a devconfig_t hierarchy,
131*0Sstevel@tonic-gate 	 * indicates whether to create a new devconfig_t structure in
132*0Sstevel@tonic-gate 	 * the hierarchy when this XML element is encountered.
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 	boolean_t is_hierarchical;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	/*
137*0Sstevel@tonic-gate 	 * If is_hierarchical is B_TRUE, whether to use an existing
138*0Sstevel@tonic-gate 	 * devconfig_t structure of this type when this element is
139*0Sstevel@tonic-gate 	 * encountered
140*0Sstevel@tonic-gate 	 */
141*0Sstevel@tonic-gate 	boolean_t singleton;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/* The valid XML attributes for this element */
144*0Sstevel@tonic-gate 	attr_t *attributes;
145*0Sstevel@tonic-gate } element_t;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate typedef struct {
148*0Sstevel@tonic-gate 	char *msgid;
149*0Sstevel@tonic-gate 	char *message;
150*0Sstevel@tonic-gate } l10nmessage_t;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * ******************************************************************
154*0Sstevel@tonic-gate  *
155*0Sstevel@tonic-gate  * Function prototypes
156*0Sstevel@tonic-gate  *
157*0Sstevel@tonic-gate  * ******************************************************************
158*0Sstevel@tonic-gate  */
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate static int validate_doc(xmlDocPtr doc, const char *name, const char *systemID);
161*0Sstevel@tonic-gate static int devconfig_to_xml(
162*0Sstevel@tonic-gate     xmlNodePtr parent, element_t elements[], devconfig_t *device);
163*0Sstevel@tonic-gate static int xml_to_devconfig(
164*0Sstevel@tonic-gate     xmlNodePtr cur, element_t elements[], devconfig_t *device);
165*0Sstevel@tonic-gate static int compare_is_a_diskset(void *obj1, void *obj2);
166*0Sstevel@tonic-gate static xmlNodePtr xml_find_node(
167*0Sstevel@tonic-gate     xmlNodePtr node, xmlChar *element, xmlChar *name);
168*0Sstevel@tonic-gate static xmlDocPtr create_localized_message_doc();
169*0Sstevel@tonic-gate static int create_localized_message_file(char **tmpfile);
170*0Sstevel@tonic-gate static int strtobool(char *str, boolean_t *value);
171*0Sstevel@tonic-gate static int ofprintf_terse(void *unused, char *fmt, ...);
172*0Sstevel@tonic-gate static int ofprintf_verbose(void *unused, char *fmt, ...);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate static int validate_set_size(
175*0Sstevel@tonic-gate     devconfig_t *volume, char *attr, char *value);
176*0Sstevel@tonic-gate static int validate_set_size_in_blocks(
177*0Sstevel@tonic-gate     devconfig_t *slice, char *attr, char *value);
178*0Sstevel@tonic-gate static int validate_set_diskset_name(
179*0Sstevel@tonic-gate     devconfig_t *diskset, char *attr, char *name);
180*0Sstevel@tonic-gate static int validate_add_available_name(
181*0Sstevel@tonic-gate     devconfig_t *device, char *attr, char *name);
182*0Sstevel@tonic-gate static int validate_add_unavailable_name(
183*0Sstevel@tonic-gate     devconfig_t *device, char *attr, char *name);
184*0Sstevel@tonic-gate static int validate_set_hsp_name(
185*0Sstevel@tonic-gate     devconfig_t *hsp, char *attr, char *name);
186*0Sstevel@tonic-gate static int validate_set_disk_name(
187*0Sstevel@tonic-gate     devconfig_t *disk, char *attr, char *name);
188*0Sstevel@tonic-gate static int validate_set_slice_name(
189*0Sstevel@tonic-gate     devconfig_t *slice, char *attr, char *name);
190*0Sstevel@tonic-gate static int validate_set_slice_start_block(
191*0Sstevel@tonic-gate     devconfig_t *slice, char *attr, char *value);
192*0Sstevel@tonic-gate static int validate_set_volume_name(
193*0Sstevel@tonic-gate     devconfig_t *volume, char *attr, char *name);
194*0Sstevel@tonic-gate static int validate_set_stripe_interlace(
195*0Sstevel@tonic-gate     devconfig_t *stripe, char *attr, char *value);
196*0Sstevel@tonic-gate static int validate_set_stripe_mincomp(
197*0Sstevel@tonic-gate     devconfig_t *stripe, char *attr, char *value);
198*0Sstevel@tonic-gate static int validate_set_stripe_maxcomp(
199*0Sstevel@tonic-gate     devconfig_t *stripe, char *attr, char *value);
200*0Sstevel@tonic-gate static int validate_set_volume_usehsp(
201*0Sstevel@tonic-gate     devconfig_t *volume, char *attr, char *value);
202*0Sstevel@tonic-gate static int validate_set_mirror_nsubmirrors(
203*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char *value);
204*0Sstevel@tonic-gate static int validate_set_mirror_read(
205*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char *value);
206*0Sstevel@tonic-gate static int validate_set_mirror_write(
207*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char *value);
208*0Sstevel@tonic-gate static int validate_set_mirror_passnum(
209*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char *value);
210*0Sstevel@tonic-gate static int validate_set_volume_redundancy(
211*0Sstevel@tonic-gate     devconfig_t *volume, char *attr, char *value);
212*0Sstevel@tonic-gate static int validate_set_volume_datapaths(
213*0Sstevel@tonic-gate     devconfig_t *volume, char *attr, char *value);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static int get_as_string_name(
216*0Sstevel@tonic-gate     devconfig_t *device, char *attr, char **value);
217*0Sstevel@tonic-gate static int get_as_string_mirror_passnum(
218*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char **value);
219*0Sstevel@tonic-gate static int get_as_string_mirror_read(
220*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char **value);
221*0Sstevel@tonic-gate static int get_as_string_mirror_write(
222*0Sstevel@tonic-gate     devconfig_t *mirror, char *attr, char **value);
223*0Sstevel@tonic-gate static int get_as_string_size_in_blocks(
224*0Sstevel@tonic-gate     devconfig_t *device, char *attr, char **value);
225*0Sstevel@tonic-gate static int get_as_string_slice_start_block(
226*0Sstevel@tonic-gate     devconfig_t *slice, char *attr, char **value);
227*0Sstevel@tonic-gate static int get_as_string_stripe_interlace(
228*0Sstevel@tonic-gate     devconfig_t *stripe, char *attr, char **value);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate  * ******************************************************************
232*0Sstevel@tonic-gate  *
233*0Sstevel@tonic-gate  * Data
234*0Sstevel@tonic-gate  *
235*0Sstevel@tonic-gate  * ******************************************************************
236*0Sstevel@tonic-gate  */
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /* Valid units for the size attribute */
239*0Sstevel@tonic-gate units_t size_units[] = {
240*0Sstevel@tonic-gate 	{UNIT_KILOBYTES, BYTES_PER_KILOBYTE},
241*0Sstevel@tonic-gate 	{UNIT_MEGABYTES, BYTES_PER_MEGABYTE},
242*0Sstevel@tonic-gate 	{UNIT_GIGABYTES, BYTES_PER_GIGABYTE},
243*0Sstevel@tonic-gate 	{UNIT_TERABYTES, BYTES_PER_TERABYTE},
244*0Sstevel@tonic-gate 	{NULL, 0}
245*0Sstevel@tonic-gate };
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /* Valid units for the interlace attribute */
248*0Sstevel@tonic-gate units_t interlace_units[] = {
249*0Sstevel@tonic-gate 	{UNIT_BLOCKS, BYTES_PER_BLOCK},
250*0Sstevel@tonic-gate 	{UNIT_KILOBYTES, BYTES_PER_KILOBYTE},
251*0Sstevel@tonic-gate 	{UNIT_MEGABYTES, BYTES_PER_MEGABYTE},
252*0Sstevel@tonic-gate 	{NULL, 0}
253*0Sstevel@tonic-gate };
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /* <diskset> attributes */
256*0Sstevel@tonic-gate static attr_t diskset_attrs[] = {
257*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_diskset_name, get_as_string_name },
258*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
259*0Sstevel@tonic-gate };
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate /* <available> attributes */
262*0Sstevel@tonic-gate static attr_t available_attrs[] = {
263*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_add_available_name, NULL },
264*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
265*0Sstevel@tonic-gate };
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /* <unavailable> attributes */
268*0Sstevel@tonic-gate static attr_t unavailable_attrs[] = {
269*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_add_unavailable_name, NULL },
270*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
271*0Sstevel@tonic-gate };
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate /* <hsp> attributes */
274*0Sstevel@tonic-gate static attr_t hsp_attrs[] = {
275*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_hsp_name, get_as_string_name },
276*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
277*0Sstevel@tonic-gate };
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /* <disk> attributes */
280*0Sstevel@tonic-gate static attr_t disk_attrs[] = {
281*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_disk_name, get_as_string_name },
282*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
283*0Sstevel@tonic-gate };
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate /* <slice> attributes */
286*0Sstevel@tonic-gate static attr_t slice_attrs[] = {
287*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_slice_name, get_as_string_name },
288*0Sstevel@tonic-gate 	{ ATTR_SIZEINBLOCKS, validate_set_size_in_blocks,
289*0Sstevel@tonic-gate 	    get_as_string_size_in_blocks },
290*0Sstevel@tonic-gate 	{ ATTR_SLICE_STARTSECTOR, validate_set_slice_start_block,
291*0Sstevel@tonic-gate 	    get_as_string_slice_start_block },
292*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
293*0Sstevel@tonic-gate };
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate /* <stripe> attributes */
296*0Sstevel@tonic-gate static attr_t stripe_attrs[] = {
297*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_volume_name, get_as_string_name },
298*0Sstevel@tonic-gate 	{ ATTR_SIZEINBYTES, validate_set_size, NULL },
299*0Sstevel@tonic-gate 	{ ATTR_STRIPE_MINCOMP, validate_set_stripe_mincomp, NULL },
300*0Sstevel@tonic-gate 	{ ATTR_STRIPE_MAXCOMP, validate_set_stripe_maxcomp, NULL },
301*0Sstevel@tonic-gate 	{ ATTR_STRIPE_INTERLACE, validate_set_stripe_interlace,
302*0Sstevel@tonic-gate 	    get_as_string_stripe_interlace },
303*0Sstevel@tonic-gate 	{ ATTR_VOLUME_USEHSP, validate_set_volume_usehsp, NULL },
304*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
305*0Sstevel@tonic-gate };
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /* <concat> attributes */
308*0Sstevel@tonic-gate static attr_t concat_attrs[] = {
309*0Sstevel@tonic-gate 	{ ATTR_NAME,   validate_set_volume_name, get_as_string_name },
310*0Sstevel@tonic-gate 	{ ATTR_SIZEINBYTES,   validate_set_size, NULL },
311*0Sstevel@tonic-gate 	{ ATTR_VOLUME_USEHSP, validate_set_volume_usehsp, NULL },
312*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
313*0Sstevel@tonic-gate };
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate /* <mirror> attributes */
316*0Sstevel@tonic-gate static attr_t mirror_attrs[] = {
317*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_volume_name, get_as_string_name },
318*0Sstevel@tonic-gate 	{ ATTR_MIRROR_NSUBMIRRORS, validate_set_mirror_nsubmirrors, NULL },
319*0Sstevel@tonic-gate 	{ ATTR_SIZEINBYTES, validate_set_size, NULL },
320*0Sstevel@tonic-gate 	{ ATTR_MIRROR_READ, validate_set_mirror_read,
321*0Sstevel@tonic-gate 	    get_as_string_mirror_read },
322*0Sstevel@tonic-gate 	{ ATTR_MIRROR_WRITE, validate_set_mirror_write,
323*0Sstevel@tonic-gate 	    get_as_string_mirror_write },
324*0Sstevel@tonic-gate 	{ ATTR_MIRROR_PASSNUM, validate_set_mirror_passnum,
325*0Sstevel@tonic-gate 	    get_as_string_mirror_passnum },
326*0Sstevel@tonic-gate 	{ ATTR_VOLUME_USEHSP, validate_set_volume_usehsp, NULL },
327*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
328*0Sstevel@tonic-gate };
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate /* <volume> attributes */
331*0Sstevel@tonic-gate static attr_t volume_attrs[] = {
332*0Sstevel@tonic-gate 	{ ATTR_NAME, validate_set_volume_name, get_as_string_name },
333*0Sstevel@tonic-gate 	{ ATTR_SIZEINBYTES, validate_set_size, NULL },
334*0Sstevel@tonic-gate 	{ ATTR_VOLUME_REDUNDANCY, validate_set_volume_redundancy, NULL },
335*0Sstevel@tonic-gate 	{ ATTR_VOLUME_FAULTRECOVERY, validate_set_volume_usehsp, NULL },
336*0Sstevel@tonic-gate 	{ ATTR_VOLUME_DATAPATHS, validate_set_volume_datapaths, NULL },
337*0Sstevel@tonic-gate 	{ NULL, NULL, NULL }
338*0Sstevel@tonic-gate };
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate /* volume-request elements */
341*0Sstevel@tonic-gate static element_t request_elements[] = {
342*0Sstevel@tonic-gate 	{ ELEMENT_DISKSET, TYPE_DISKSET, B_FALSE, B_FALSE, diskset_attrs },
343*0Sstevel@tonic-gate 	{ ELEMENT_AVAILABLE, TYPE_UNKNOWN, B_FALSE, B_FALSE, available_attrs },
344*0Sstevel@tonic-gate 	{ ELEMENT_UNAVAILABLE, TYPE_UNKNOWN, B_FALSE, B_FALSE,
345*0Sstevel@tonic-gate 	    unavailable_attrs },
346*0Sstevel@tonic-gate 	{ ELEMENT_HSP, TYPE_HSP, B_TRUE, B_FALSE, hsp_attrs },
347*0Sstevel@tonic-gate 	{ ELEMENT_SLICE, TYPE_SLICE, B_TRUE, B_FALSE, slice_attrs },
348*0Sstevel@tonic-gate 	{ ELEMENT_STRIPE, TYPE_STRIPE, B_TRUE, B_FALSE, stripe_attrs },
349*0Sstevel@tonic-gate 	{ ELEMENT_CONCAT, TYPE_CONCAT, B_TRUE, B_FALSE, concat_attrs },
350*0Sstevel@tonic-gate 	{ ELEMENT_MIRROR, TYPE_MIRROR, B_TRUE, B_FALSE, mirror_attrs },
351*0Sstevel@tonic-gate 	{ ELEMENT_VOLUME, TYPE_VOLUME, B_TRUE, B_FALSE, volume_attrs },
352*0Sstevel@tonic-gate 	{ NULL, NULL, B_FALSE, B_FALSE, NULL }
353*0Sstevel@tonic-gate };
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /* volume-defaults elements */
356*0Sstevel@tonic-gate static element_t default_elements[] = {
357*0Sstevel@tonic-gate 	{ ELEMENT_DISKSET, TYPE_DISKSET, B_TRUE, B_FALSE, diskset_attrs },
358*0Sstevel@tonic-gate 	{ ELEMENT_AVAILABLE, TYPE_UNKNOWN, B_FALSE, B_TRUE, available_attrs },
359*0Sstevel@tonic-gate 	{ ELEMENT_UNAVAILABLE, TYPE_UNKNOWN, B_FALSE, B_TRUE,
360*0Sstevel@tonic-gate 	    unavailable_attrs },
361*0Sstevel@tonic-gate 	{ ELEMENT_HSP, TYPE_HSP, B_TRUE, B_TRUE, hsp_attrs },
362*0Sstevel@tonic-gate 	{ ELEMENT_SLICE, TYPE_SLICE, B_TRUE, B_TRUE, slice_attrs },
363*0Sstevel@tonic-gate 	{ ELEMENT_STRIPE, TYPE_STRIPE, B_TRUE, B_TRUE, stripe_attrs },
364*0Sstevel@tonic-gate 	{ ELEMENT_CONCAT, TYPE_CONCAT, B_TRUE, B_TRUE, concat_attrs },
365*0Sstevel@tonic-gate 	{ ELEMENT_MIRROR, TYPE_MIRROR, B_TRUE, B_TRUE, mirror_attrs },
366*0Sstevel@tonic-gate 	{ ELEMENT_VOLUME, TYPE_VOLUME, B_TRUE, B_TRUE, volume_attrs },
367*0Sstevel@tonic-gate 	{ NULL, NULL, B_FALSE, B_FALSE, NULL }
368*0Sstevel@tonic-gate };
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate /* volume-config elements */
371*0Sstevel@tonic-gate static element_t config_elements[] = {
372*0Sstevel@tonic-gate 	{ ELEMENT_DISKSET, TYPE_DISKSET, B_FALSE, B_FALSE, diskset_attrs },
373*0Sstevel@tonic-gate 	{ ELEMENT_DISK, TYPE_DRIVE, B_TRUE, B_FALSE, disk_attrs },
374*0Sstevel@tonic-gate 	{ ELEMENT_SLICE, TYPE_SLICE, B_TRUE, B_FALSE, slice_attrs },
375*0Sstevel@tonic-gate 	{ ELEMENT_HSP, TYPE_HSP, B_TRUE, B_FALSE, hsp_attrs },
376*0Sstevel@tonic-gate 	{ ELEMENT_STRIPE, TYPE_STRIPE, B_TRUE, B_FALSE, stripe_attrs },
377*0Sstevel@tonic-gate 	{ ELEMENT_CONCAT, TYPE_CONCAT, B_TRUE, B_FALSE, concat_attrs },
378*0Sstevel@tonic-gate 	{ ELEMENT_MIRROR, TYPE_MIRROR, B_TRUE, B_FALSE, mirror_attrs },
379*0Sstevel@tonic-gate 	{ NULL, NULL, B_FALSE, B_FALSE, NULL }
380*0Sstevel@tonic-gate };
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate /*
383*0Sstevel@tonic-gate  * ******************************************************************
384*0Sstevel@tonic-gate  *
385*0Sstevel@tonic-gate  * External functions
386*0Sstevel@tonic-gate  *
387*0Sstevel@tonic-gate  * ******************************************************************
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate /*
391*0Sstevel@tonic-gate  * Initialize the XML parser, setting defaults across all XML
392*0Sstevel@tonic-gate  * routines.
393*0Sstevel@tonic-gate  */
394*0Sstevel@tonic-gate void
init_xml()395*0Sstevel@tonic-gate init_xml()
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	/* COMPAT: Do not generate nodes for formatting spaces */
398*0Sstevel@tonic-gate 	LIBXML_TEST_VERSION
399*0Sstevel@tonic-gate 	xmlKeepBlanksDefault(0);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	/* Turn on line numbers for debugging */
402*0Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	/* Substitute entities as files are parsed */
405*0Sstevel@tonic-gate 	xmlSubstituteEntitiesDefault(1);
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	/* Don't load external entity subsets */
408*0Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue = 0;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	/* Don't validate against DTD by default */
411*0Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 0;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	/* Set up output handlers for XML parsing */
414*0Sstevel@tonic-gate 	xmlDefaultSAXHandler.warning = (warningSAXFunc)ofprintf_verbose;
415*0Sstevel@tonic-gate 	xmlDefaultSAXHandler.error  = (errorSAXFunc)ofprintf_terse;
416*0Sstevel@tonic-gate 	xmlDefaultSAXHandler.fatalError = (fatalErrorSAXFunc)ofprintf_terse;
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate /*
420*0Sstevel@tonic-gate  * Clean up any remaining structures before exiting.
421*0Sstevel@tonic-gate  */
422*0Sstevel@tonic-gate void
cleanup_xml()423*0Sstevel@tonic-gate cleanup_xml()
424*0Sstevel@tonic-gate {
425*0Sstevel@tonic-gate 	xsltCleanupGlobals();
426*0Sstevel@tonic-gate 	xmlCleanupParser();
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate  * Converts a volume-request XML document into a request_t.
431*0Sstevel@tonic-gate  *
432*0Sstevel@tonic-gate  * @param       doc
433*0Sstevel@tonic-gate  *		an existing volume-request XML document
434*0Sstevel@tonic-gate  *
435*0Sstevel@tonic-gate  * @param       request
436*0Sstevel@tonic-gate  *		RETURN: a new request_t which must be freed via
437*0Sstevel@tonic-gate  *		free_request
438*0Sstevel@tonic-gate  *
439*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
440*0Sstevel@tonic-gate  */
441*0Sstevel@tonic-gate int
xml_to_request(xmlDocPtr doc,request_t ** request)442*0Sstevel@tonic-gate xml_to_request(
443*0Sstevel@tonic-gate 	xmlDocPtr doc,
444*0Sstevel@tonic-gate 	request_t **request)
445*0Sstevel@tonic-gate {
446*0Sstevel@tonic-gate 	int error = 0;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	*request = NULL;
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	/* Validate doc against known DTD */
451*0Sstevel@tonic-gate 	if ((error = validate_doc(
452*0Sstevel@tonic-gate 	    doc, ELEMENT_VOLUMEREQUEST, VOLUME_REQUEST_DTD_LOC)) == 0) {
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	    /* Create a request */
455*0Sstevel@tonic-gate 	    if ((error = new_request(request)) == 0) {
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 		/* Convert the XML doc into a request_t */
458*0Sstevel@tonic-gate 		error = xml_to_devconfig(xmlDocGetRootElement(doc),
459*0Sstevel@tonic-gate 		    request_elements, request_get_diskset_req(*request));
460*0Sstevel@tonic-gate 	    }
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	return (error);
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate /*
467*0Sstevel@tonic-gate  * Converts a volume-defaults XML document into a defaults_t.
468*0Sstevel@tonic-gate  *
469*0Sstevel@tonic-gate  * @param       doc
470*0Sstevel@tonic-gate  *		an existing volume-defaults XML document
471*0Sstevel@tonic-gate  *
472*0Sstevel@tonic-gate  * @param       defaults
473*0Sstevel@tonic-gate  *		RETURN: a new defaults_t which must be freed via
474*0Sstevel@tonic-gate  *		free_defaults
475*0Sstevel@tonic-gate  *
476*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
477*0Sstevel@tonic-gate  */
478*0Sstevel@tonic-gate int
xml_to_defaults(xmlDocPtr doc,defaults_t ** defaults)479*0Sstevel@tonic-gate xml_to_defaults(
480*0Sstevel@tonic-gate 	xmlDocPtr doc,
481*0Sstevel@tonic-gate 	defaults_t **defaults)
482*0Sstevel@tonic-gate {
483*0Sstevel@tonic-gate 	int error = 0;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	*defaults = NULL;
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	/* Validate doc against known DTD */
488*0Sstevel@tonic-gate 	if ((error = validate_doc(doc, ELEMENT_VOLUMEDEFAULTS,
489*0Sstevel@tonic-gate 	    VOLUME_DEFAULTS_DTD_LOC)) == 0) {
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	    /* Create request defaults */
492*0Sstevel@tonic-gate 	    if ((error = new_defaults(defaults)) == 0) {
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 		devconfig_t *global;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		/* Get defaults for all disk sets */
497*0Sstevel@tonic-gate 		if ((error = defaults_get_diskset_by_name(
498*0Sstevel@tonic-gate 		    *defaults, NULL, &global)) == 0) {
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 		    /* Populate the global devconfig_t from the XML doc */
501*0Sstevel@tonic-gate 		    if ((error = xml_to_devconfig(xmlDocGetRootElement(doc),
502*0Sstevel@tonic-gate 			default_elements, global)) == 0) {
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 			/* Get the components of the global devconfig_t */
505*0Sstevel@tonic-gate 			dlist_t *list = devconfig_get_components(global);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 			/*
508*0Sstevel@tonic-gate 			 * Move all named disk set settings out from
509*0Sstevel@tonic-gate 			 * under global settings
510*0Sstevel@tonic-gate 			 */
511*0Sstevel@tonic-gate 			/* CONSTANTCONDITION */
512*0Sstevel@tonic-gate 			while (1) {
513*0Sstevel@tonic-gate 			    dlist_t *removed = NULL;
514*0Sstevel@tonic-gate 			    devconfig_t *component;
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 			    /* Remove named disk set from under global */
517*0Sstevel@tonic-gate 			    list = dlist_remove_equivalent_item(
518*0Sstevel@tonic-gate 				list, NULL, compare_is_a_diskset, &removed);
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 			    if (removed == NULL) {
521*0Sstevel@tonic-gate 				/* No named disk set found */
522*0Sstevel@tonic-gate 				break;
523*0Sstevel@tonic-gate 			    }
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 			    component = removed->obj;
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 			    /* Append named disk set to disk set list */
528*0Sstevel@tonic-gate 			    defaults_set_disksets(*defaults,
529*0Sstevel@tonic-gate 				dlist_append(dlist_new_item(component),
530*0Sstevel@tonic-gate 				defaults_get_disksets(*defaults), AT_TAIL));
531*0Sstevel@tonic-gate 			}
532*0Sstevel@tonic-gate 		    }
533*0Sstevel@tonic-gate 		}
534*0Sstevel@tonic-gate 	    }
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	return (error);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * Converts a volume-config XML document into a devconfig_t.
542*0Sstevel@tonic-gate  *
543*0Sstevel@tonic-gate  * @param       doc
544*0Sstevel@tonic-gate  *		an existing volume-config XML document
545*0Sstevel@tonic-gate  *
546*0Sstevel@tonic-gate  * @param       config
547*0Sstevel@tonic-gate  *		RETURN: a new devconfig_t which must be freed via
548*0Sstevel@tonic-gate  *		free_devconfig
549*0Sstevel@tonic-gate  *
550*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
551*0Sstevel@tonic-gate  */
552*0Sstevel@tonic-gate int
xml_to_config(xmlDocPtr doc,devconfig_t ** config)553*0Sstevel@tonic-gate xml_to_config(
554*0Sstevel@tonic-gate 	xmlDocPtr doc,
555*0Sstevel@tonic-gate 	devconfig_t **config)
556*0Sstevel@tonic-gate {
557*0Sstevel@tonic-gate 	int error = 0;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	*config = NULL;
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	/* Validate doc against known DTD */
562*0Sstevel@tonic-gate 	if ((error = validate_doc(
563*0Sstevel@tonic-gate 	    doc, ELEMENT_VOLUMECONFIG, VOLUME_CONFIG_DTD_LOC)) == 0) {
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	    /* Create a devconfig_t */
566*0Sstevel@tonic-gate 	    if ((error = new_devconfig(config, TYPE_DISKSET)) == 0) {
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		/* Populate the devconfig_t from the XML doc */
569*0Sstevel@tonic-gate 		error = xml_to_devconfig(
570*0Sstevel@tonic-gate 		    xmlDocGetRootElement(doc), config_elements, *config);
571*0Sstevel@tonic-gate 	    }
572*0Sstevel@tonic-gate 	}
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	return (error);
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate /*
578*0Sstevel@tonic-gate  * Converts a devconfig_t into a volume-config XML document.
579*0Sstevel@tonic-gate  *
580*0Sstevel@tonic-gate  * @param       config
581*0Sstevel@tonic-gate  *		an existing devconfig_t representing a volume
582*0Sstevel@tonic-gate  *		configuration.
583*0Sstevel@tonic-gate  *
584*0Sstevel@tonic-gate  * @param       doc
585*0Sstevel@tonic-gate  *		RETURN: a new volume-config XML document which must be
586*0Sstevel@tonic-gate  *		freed via xmlFreeDoc
587*0Sstevel@tonic-gate  *
588*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
589*0Sstevel@tonic-gate  */
590*0Sstevel@tonic-gate int
config_to_xml(devconfig_t * config,xmlDocPtr * doc)591*0Sstevel@tonic-gate config_to_xml(
592*0Sstevel@tonic-gate 	devconfig_t *config,
593*0Sstevel@tonic-gate 	xmlDocPtr *doc)
594*0Sstevel@tonic-gate {
595*0Sstevel@tonic-gate 	xmlNodePtr root;
596*0Sstevel@tonic-gate 	int error = 0;
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	/* Create the XML document */
599*0Sstevel@tonic-gate 	*doc = xmlNewDoc((xmlChar *)"1.0");
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	/* Create the root node */
602*0Sstevel@tonic-gate 	root = xmlNewDocNode(
603*0Sstevel@tonic-gate 	    *doc, NULL, (xmlChar *)ELEMENT_VOLUMECONFIG, NULL);
604*0Sstevel@tonic-gate 	xmlAddChild((xmlNodePtr)*doc, (xmlNodePtr)root);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	/* Create sub-nodes from the config devconfig_t */
607*0Sstevel@tonic-gate 	if ((error = devconfig_to_xml(root, config_elements, config)) == 0) {
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	    /* Add DTD node and validate */
610*0Sstevel@tonic-gate 	    error = validate_doc(
611*0Sstevel@tonic-gate 		*doc, ELEMENT_VOLUMECONFIG, VOLUME_CONFIG_DTD_LOC);
612*0Sstevel@tonic-gate 	}
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	if (error) {
615*0Sstevel@tonic-gate 	    xmlFreeDoc(*doc);
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	return (error);
619*0Sstevel@tonic-gate }
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate /*
622*0Sstevel@tonic-gate  * Converts a volume-config XML document into a Bourne shell script.
623*0Sstevel@tonic-gate  *
624*0Sstevel@tonic-gate  * @param       doc
625*0Sstevel@tonic-gate  *		an existing volume-config XML document
626*0Sstevel@tonic-gate  *
627*0Sstevel@tonic-gate  * @param       commands
628*0Sstevel@tonic-gate  *		RETURN: a new char* which must be freed
629*0Sstevel@tonic-gate  *
630*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
631*0Sstevel@tonic-gate  */
632*0Sstevel@tonic-gate int
xml_to_commands(xmlDocPtr doc,char ** commands)633*0Sstevel@tonic-gate xml_to_commands(
634*0Sstevel@tonic-gate 	xmlDocPtr doc,
635*0Sstevel@tonic-gate 	char **commands)
636*0Sstevel@tonic-gate {
637*0Sstevel@tonic-gate 	char *tmpfile = NULL;
638*0Sstevel@tonic-gate 	int error = 0;
639*0Sstevel@tonic-gate 	xsltStylesheetPtr style = NULL;
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	/* Read in XSL stylesheet as a normal XML document */
642*0Sstevel@tonic-gate 	xmlDocPtr xsl_doc = xmlSAXParseFile((xmlSAXHandlerPtr)
643*0Sstevel@tonic-gate 	    &xmlDefaultSAXHandler, VOLUME_COMMAND_XSL_LOC, 0);
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	if (xsl_doc != NULL && xsl_doc->xmlChildrenNode != NULL) {
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 		/*
648*0Sstevel@tonic-gate 		 * Find the "msgfile" variable node.  This is where
649*0Sstevel@tonic-gate 		 * we'll set the location of the file we'll create
650*0Sstevel@tonic-gate 		 * containing the localized messages.
651*0Sstevel@tonic-gate 		 */
652*0Sstevel@tonic-gate 	    xmlNodePtr msgfile_node = xml_find_node(
653*0Sstevel@tonic-gate 		xmlDocGetRootElement(xsl_doc), (xmlChar *)ELEMENT_VARIABLE,
654*0Sstevel@tonic-gate 		(xmlChar *)NAME_L10N_MESSAGE_FILE);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 		/*
657*0Sstevel@tonic-gate 		 * Find the "lang" node.  This is where we'll set the
658*0Sstevel@tonic-gate 		 * current locale.
659*0Sstevel@tonic-gate 		 */
660*0Sstevel@tonic-gate 	    xmlNodePtr lang_node = xml_find_node(xmlDocGetRootElement(xsl_doc),
661*0Sstevel@tonic-gate 		(xmlChar *)ELEMENT_PARAM, (xmlChar *)NAME_LANG);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 		/*
664*0Sstevel@tonic-gate 		 * Ignore if the nodes are not found -- the script
665*0Sstevel@tonic-gate 		 * will default to the C locale.
666*0Sstevel@tonic-gate 		 */
667*0Sstevel@tonic-gate 	    if (msgfile_node != NULL && lang_node != NULL) {
668*0Sstevel@tonic-gate 		/* Get/set current locale in the "lang" node */
669*0Sstevel@tonic-gate 		char *locale = setlocale(LC_MESSAGES, NULL);
670*0Sstevel@tonic-gate 		xmlNodeSetContent(lang_node, (xmlChar *)locale);
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate 		/* Write localized messages to a temporary file */
673*0Sstevel@tonic-gate 		if ((error = create_localized_message_file(&tmpfile)) == 0) {
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 		    char *newsel;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 		    /* Clear current value of select attribute, if any */
678*0Sstevel@tonic-gate 		    xmlChar *cursel = xmlGetProp(
679*0Sstevel@tonic-gate 			msgfile_node, (xmlChar *)ATTR_SELECT);
680*0Sstevel@tonic-gate 		    if (cursel != NULL) {
681*0Sstevel@tonic-gate 			xmlFree(cursel);
682*0Sstevel@tonic-gate 		    }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 			/*
685*0Sstevel@tonic-gate 			 * The select attribute calls the XSLT function
686*0Sstevel@tonic-gate 			 * document() to load an external XML file
687*0Sstevel@tonic-gate 			 */
688*0Sstevel@tonic-gate 		    newsel = stralloccat(3, "document('", tmpfile, "')");
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 		    if (newsel == NULL) {
691*0Sstevel@tonic-gate 			volume_set_error(gettext("out of memory"));
692*0Sstevel@tonic-gate 			error = -1;
693*0Sstevel@tonic-gate 		    } else {
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 			/* Set the new value of the select attribute */
696*0Sstevel@tonic-gate 			xmlSetProp(msgfile_node,
697*0Sstevel@tonic-gate 			    (xmlChar *)ATTR_SELECT, (xmlChar *)newsel);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 			free(newsel);
700*0Sstevel@tonic-gate 		    }
701*0Sstevel@tonic-gate 		}
702*0Sstevel@tonic-gate 	    }
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	    if (error == 0) {
705*0Sstevel@tonic-gate 		style = xsltParseStylesheetDoc(xsl_doc);
706*0Sstevel@tonic-gate 	    }
707*0Sstevel@tonic-gate 	}
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 	if (style == NULL) {
710*0Sstevel@tonic-gate 	    volume_set_error(
711*0Sstevel@tonic-gate 		gettext("could not load stylesheet from %s"),
712*0Sstevel@tonic-gate 		VOLUME_COMMAND_XSL_LOC);
713*0Sstevel@tonic-gate 	    error = -1;
714*0Sstevel@tonic-gate 	} else {
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	    xmlDocPtr result = xsltApplyStylesheet(style, doc, NULL);
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	    if (result == NULL) {
719*0Sstevel@tonic-gate 		volume_set_error(
720*0Sstevel@tonic-gate 		    gettext("could not apply stylesheet to volume-config"));
721*0Sstevel@tonic-gate 		error = -1;
722*0Sstevel@tonic-gate 	    } else {
723*0Sstevel@tonic-gate 		int length;
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 		if (xsltSaveResultToString((xmlChar **)commands,
726*0Sstevel@tonic-gate 		    &length, result, style) == -1) {
727*0Sstevel@tonic-gate 		    error = ENOMEM;
728*0Sstevel@tonic-gate 		}
729*0Sstevel@tonic-gate 	    }
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	    xsltFreeStylesheet(style);
732*0Sstevel@tonic-gate 	}
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	if (tmpfile != NULL) {
735*0Sstevel@tonic-gate 	    /* Ignore failure */
736*0Sstevel@tonic-gate 	    unlink(tmpfile);
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	    free(tmpfile);
739*0Sstevel@tonic-gate 	}
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	return (error);
742*0Sstevel@tonic-gate }
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate /*
745*0Sstevel@tonic-gate  * ******************************************************************
746*0Sstevel@tonic-gate  *
747*0Sstevel@tonic-gate  * Static functions
748*0Sstevel@tonic-gate  *
749*0Sstevel@tonic-gate  * ******************************************************************
750*0Sstevel@tonic-gate  */
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate /*
753*0Sstevel@tonic-gate  * Sets the external DTD node in the given XML document and then
754*0Sstevel@tonic-gate  * validates it.
755*0Sstevel@tonic-gate  *
756*0Sstevel@tonic-gate  * @param       doc
757*0Sstevel@tonic-gate  *		an existing XML document
758*0Sstevel@tonic-gate  *
759*0Sstevel@tonic-gate  * @param       name
760*0Sstevel@tonic-gate  *		the expected root element name of the XML document
761*0Sstevel@tonic-gate  *
762*0Sstevel@tonic-gate  * @param       systemID
763*0Sstevel@tonic-gate  *		the location of the DTD
764*0Sstevel@tonic-gate  *
765*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
766*0Sstevel@tonic-gate  */
767*0Sstevel@tonic-gate static int
validate_doc(xmlDocPtr doc,const char * name,const char * systemID)768*0Sstevel@tonic-gate validate_doc(
769*0Sstevel@tonic-gate 	xmlDocPtr doc,
770*0Sstevel@tonic-gate 	const char *name,
771*0Sstevel@tonic-gate 	const char *systemID)
772*0Sstevel@tonic-gate {
773*0Sstevel@tonic-gate 	xmlValidCtxt context;
774*0Sstevel@tonic-gate 	xmlDtdPtr dtd;
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 	if (doc == NULL) {
777*0Sstevel@tonic-gate 	    volume_set_error(gettext("NULL %s document"), name);
778*0Sstevel@tonic-gate 	    return (-1);
779*0Sstevel@tonic-gate 	}
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	/*
782*0Sstevel@tonic-gate 	 * Assume that we can't trust any DTD but our own.
783*0Sstevel@tonic-gate 	 */
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 	/* Was a DTD (external or internal) included in the document? */
786*0Sstevel@tonic-gate 	if ((dtd = xmlGetIntSubset(doc)) != NULL) {
787*0Sstevel@tonic-gate 	    /* Remove the DTD node */
788*0Sstevel@tonic-gate 	    oprintf(OUTPUT_DEBUG, gettext("Removing DTD from %s\n"), name);
789*0Sstevel@tonic-gate 	    xmlUnlinkNode((xmlNodePtr)dtd);
790*0Sstevel@tonic-gate 	    xmlFreeDtd(dtd);
791*0Sstevel@tonic-gate 	}
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	/* Create the (external) DTD node */
794*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
795*0Sstevel@tonic-gate 	    gettext("Creating new external DTD for %s\n"), name);
796*0Sstevel@tonic-gate 	dtd = xmlCreateIntSubset(
797*0Sstevel@tonic-gate 	    doc, (xmlChar *)name, NULL, (xmlChar *)systemID);
798*0Sstevel@tonic-gate 	if (dtd == NULL) {
799*0Sstevel@tonic-gate 	    volume_set_error(
800*0Sstevel@tonic-gate 		gettext("could not create DTD node from %s"), systemID);
801*0Sstevel@tonic-gate 	    return (-1);
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	/* Validate against DTD */
805*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG, gettext("Validating %s against DTD\n"), name);
806*0Sstevel@tonic-gate 	context.userData = NULL;
807*0Sstevel@tonic-gate 	context.error = (xmlValidityErrorFunc)ofprintf_terse;
808*0Sstevel@tonic-gate 	context.warning = (xmlValidityWarningFunc)ofprintf_terse;
809*0Sstevel@tonic-gate 	if (!xmlValidateDocument(&context, doc)) {
810*0Sstevel@tonic-gate 	    volume_set_error(gettext("invalid %s"), name);
811*0Sstevel@tonic-gate 	    return (-1);
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	return (0);
815*0Sstevel@tonic-gate }
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate /*
818*0Sstevel@tonic-gate  * Converts a devconfig_t into an XML node subject to the rules in
819*0Sstevel@tonic-gate  * the given element_t array.
820*0Sstevel@tonic-gate  *
821*0Sstevel@tonic-gate  * @param       parent
822*0Sstevel@tonic-gate  *		the XML node to which to add new XML nodes resulting
823*0Sstevel@tonic-gate  *		from conversion of the given devconfig_t
824*0Sstevel@tonic-gate  *
825*0Sstevel@tonic-gate  * @param       elements
826*0Sstevel@tonic-gate  *		the element_ts that describe the structure of the XML
827*0Sstevel@tonic-gate  *		document and govern the conversion of the given
828*0Sstevel@tonic-gate  *		devconfig_t
829*0Sstevel@tonic-gate  *
830*0Sstevel@tonic-gate  * @param       device
831*0Sstevel@tonic-gate  *		the devconfig_t to convert
832*0Sstevel@tonic-gate  *
833*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
834*0Sstevel@tonic-gate  */
835*0Sstevel@tonic-gate static int
devconfig_to_xml(xmlNodePtr parent,element_t elements[],devconfig_t * device)836*0Sstevel@tonic-gate devconfig_to_xml(
837*0Sstevel@tonic-gate 	xmlNodePtr parent,
838*0Sstevel@tonic-gate 	element_t elements[],
839*0Sstevel@tonic-gate 	devconfig_t *device)
840*0Sstevel@tonic-gate {
841*0Sstevel@tonic-gate 	int i;
842*0Sstevel@tonic-gate 	int error = 0;
843*0Sstevel@tonic-gate 	xmlNodePtr node = NULL;
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	/* Get device type */
846*0Sstevel@tonic-gate 	component_type_t type;
847*0Sstevel@tonic-gate 	if ((error = devconfig_get_type(device, &type)) != 0) {
848*0Sstevel@tonic-gate 	    return (error);
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	/* Search for this element definition */
852*0Sstevel@tonic-gate 	for (i = 0; elements[i].name != NULL; i++) {
853*0Sstevel@tonic-gate 	    element_t *element = &(elements[i]);
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	    if (element->type == type) {
856*0Sstevel@tonic-gate 		int j;
857*0Sstevel@tonic-gate 		char **array;
858*0Sstevel@tonic-gate 		dlist_t *components;
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 		oprintf(OUTPUT_DEBUG, gettext("Element: %s\n"),
861*0Sstevel@tonic-gate 		    devconfig_type_to_str(type));
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 		/* Create the XML node */
864*0Sstevel@tonic-gate 		node = xmlNewChild(
865*0Sstevel@tonic-gate 		    parent, NULL, (xmlChar *)element->name, NULL);
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 		/* For each attribute defined for this element... */
868*0Sstevel@tonic-gate 		for (j = 0; element->attributes[j].name != NULL; j++) {
869*0Sstevel@tonic-gate 		    attr_t *attribute = &(element->attributes[j]);
870*0Sstevel@tonic-gate 		    char *value;
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		    /* Is there a valid accessor for this attribute? */
873*0Sstevel@tonic-gate 		    if (attribute->get_as_string != NULL) {
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 			/* Get the attribute value from the device */
876*0Sstevel@tonic-gate 			switch (error = attribute->get_as_string(
877*0Sstevel@tonic-gate 			    device, attribute->name, &value)) {
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 			    /* Attribute is set in this device */
880*0Sstevel@tonic-gate 			    case 0:
881*0Sstevel@tonic-gate 				oprintf(OUTPUT_DEBUG, "    %s: %s\n",
882*0Sstevel@tonic-gate 				    attribute->name, value);
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 				/* Set the value in the XML node */
885*0Sstevel@tonic-gate 				xmlSetProp(node, (uchar_t *)attribute->name,
886*0Sstevel@tonic-gate 				    (uchar_t *)value);
887*0Sstevel@tonic-gate 				free(value);
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 			    /* FALLTHROUGH */
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 			    /* Attribute is not set in this device */
892*0Sstevel@tonic-gate 			    case ERR_ATTR_UNSET:
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate 				error = 0;
895*0Sstevel@tonic-gate 				break;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 			    /* Error */
898*0Sstevel@tonic-gate 			    default:
899*0Sstevel@tonic-gate 				return (error);
900*0Sstevel@tonic-gate 			}
901*0Sstevel@tonic-gate 		    }
902*0Sstevel@tonic-gate 		}
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 		/* Is this node hierarchical? */
905*0Sstevel@tonic-gate 		if (element->is_hierarchical == B_FALSE) {
906*0Sstevel@tonic-gate 		    node = parent;
907*0Sstevel@tonic-gate 		}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 		/* Create <available> nodes */
910*0Sstevel@tonic-gate 		array = devconfig_get_available(device);
911*0Sstevel@tonic-gate 		if (array != NULL) {
912*0Sstevel@tonic-gate 		    for (j = 0; array[j] != NULL; j++) {
913*0Sstevel@tonic-gate 			xmlNodePtr child = xmlNewChild(
914*0Sstevel@tonic-gate 			    node, NULL, (xmlChar *)ELEMENT_AVAILABLE, NULL);
915*0Sstevel@tonic-gate 			xmlSetProp(child,
916*0Sstevel@tonic-gate 			    (xmlChar *)ATTR_NAME, (xmlChar *)array[j]);
917*0Sstevel@tonic-gate 		    }
918*0Sstevel@tonic-gate 		}
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 		/* Create <unavailable> nodes */
921*0Sstevel@tonic-gate 		array = devconfig_get_unavailable(device);
922*0Sstevel@tonic-gate 		if (array != NULL) {
923*0Sstevel@tonic-gate 		    for (j = 0; array[j] != NULL; j++) {
924*0Sstevel@tonic-gate 			xmlNodePtr child = xmlNewChild(
925*0Sstevel@tonic-gate 			    node, NULL, (xmlChar *)ELEMENT_UNAVAILABLE, NULL);
926*0Sstevel@tonic-gate 			xmlSetProp(child,
927*0Sstevel@tonic-gate 			    (xmlChar *)ATTR_NAME, (xmlChar *)array[j]);
928*0Sstevel@tonic-gate 		    }
929*0Sstevel@tonic-gate 		}
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 		/*
932*0Sstevel@tonic-gate 		 * Recursively convert subcomponents of this device to
933*0Sstevel@tonic-gate 		 * XML, taking care to encode them in the order
934*0Sstevel@tonic-gate 		 * specified in the element_t list (which should
935*0Sstevel@tonic-gate 		 * mirror what's expected by the DTD).
936*0Sstevel@tonic-gate 		 */
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 		/* For each element type... */
939*0Sstevel@tonic-gate 		for (j = 0; elements[j].name != NULL; j++) {
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 		    /* For each component of this device... */
942*0Sstevel@tonic-gate 		    for (components = devconfig_get_components(device);
943*0Sstevel@tonic-gate 			components != NULL && error == 0;
944*0Sstevel@tonic-gate 			components = components->next) {
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 			devconfig_t *component = (devconfig_t *)components->obj;
947*0Sstevel@tonic-gate 			component_type_t t;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 			/* Are the types the same? */
950*0Sstevel@tonic-gate 			if ((error = devconfig_get_type(component, &t)) != 0) {
951*0Sstevel@tonic-gate 			    return (error);
952*0Sstevel@tonic-gate 			} else {
953*0Sstevel@tonic-gate 			    if (elements[j].type == t) {
954*0Sstevel@tonic-gate 				/* Encode child */
955*0Sstevel@tonic-gate 				error = devconfig_to_xml(
956*0Sstevel@tonic-gate 				    node, elements, component);
957*0Sstevel@tonic-gate 			    }
958*0Sstevel@tonic-gate 			}
959*0Sstevel@tonic-gate 		    }
960*0Sstevel@tonic-gate 		}
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate 		/* Element found */
963*0Sstevel@tonic-gate 		break;
964*0Sstevel@tonic-gate 	    }
965*0Sstevel@tonic-gate 	}
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	/* Was this device successfully converted? */
968*0Sstevel@tonic-gate 	if (node == NULL) {
969*0Sstevel@tonic-gate 	    volume_set_error(
970*0Sstevel@tonic-gate 		gettext("can't convert device of type \"%s\" to XML element"),
971*0Sstevel@tonic-gate 		devconfig_type_to_str(type));
972*0Sstevel@tonic-gate 	    error = -1;
973*0Sstevel@tonic-gate 	}
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 	return (error);
976*0Sstevel@tonic-gate }
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate /*
979*0Sstevel@tonic-gate  * Converts an XML node into a devconfig_t subject to the rules in
980*0Sstevel@tonic-gate  * the given element_t array.
981*0Sstevel@tonic-gate  *
982*0Sstevel@tonic-gate  * @param       cure
983*0Sstevel@tonic-gate  *		the existing XML node to convert
984*0Sstevel@tonic-gate  *
985*0Sstevel@tonic-gate  * @param       elements
986*0Sstevel@tonic-gate  *		the element_ts that describe the structure of the XML
987*0Sstevel@tonic-gate  *		document and govern the conversion of the given XML
988*0Sstevel@tonic-gate  *		node
989*0Sstevel@tonic-gate  *
990*0Sstevel@tonic-gate  * @param       device
991*0Sstevel@tonic-gate  *		the devconfig_t node to which to add new devconfig_ts
992*0Sstevel@tonic-gate  *		resulting from conversion of the given XML node
993*0Sstevel@tonic-gate  *
994*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
995*0Sstevel@tonic-gate  */
996*0Sstevel@tonic-gate static int
xml_to_devconfig(xmlNodePtr cur,element_t elements[],devconfig_t * device)997*0Sstevel@tonic-gate xml_to_devconfig(
998*0Sstevel@tonic-gate 	xmlNodePtr cur,
999*0Sstevel@tonic-gate 	element_t elements[],
1000*0Sstevel@tonic-gate 	devconfig_t *device)
1001*0Sstevel@tonic-gate {
1002*0Sstevel@tonic-gate 	int error = 0;
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 	/* For each child node... */
1005*0Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
1006*0Sstevel@tonic-gate 	    int i;
1007*0Sstevel@tonic-gate 	    boolean_t parsed_elem = B_FALSE;
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	    /* Search for this element definition */
1010*0Sstevel@tonic-gate 	    for (i = 0; elements[i].name != NULL; i++) {
1011*0Sstevel@tonic-gate 		element_t *element = &(elements[i]);
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, (xmlChar *)element->name) == 0) {
1014*0Sstevel@tonic-gate 		    int j;
1015*0Sstevel@tonic-gate 		    devconfig_t *component = NULL;
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 		    /* Flag that this element has been parsed */
1018*0Sstevel@tonic-gate 		    parsed_elem = B_TRUE;
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 		    oprintf(OUTPUT_DEBUG, gettext("line %d: Element <%s>\n"),
1021*0Sstevel@tonic-gate 			    XML_GET_LINE(cur), cur->name);
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 		    /* Should a new device be created for this element? */
1024*0Sstevel@tonic-gate 		    if (element->is_hierarchical == B_TRUE) {
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 			/* Should we use an existing device of this type? */
1027*0Sstevel@tonic-gate 			if (element->singleton) {
1028*0Sstevel@tonic-gate 			    devconfig_get_component(
1029*0Sstevel@tonic-gate 				device, element->type, &component, B_FALSE);
1030*0Sstevel@tonic-gate 			}
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 			if (component == NULL) {
1033*0Sstevel@tonic-gate 			    oprintf(OUTPUT_DEBUG,
1034*0Sstevel@tonic-gate 				gettext("Creating new device\n"));
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate 			    /* Create device of this type */
1037*0Sstevel@tonic-gate 			    if ((error = new_devconfig(
1038*0Sstevel@tonic-gate 				    &component, element->type)) != 0) {
1039*0Sstevel@tonic-gate 				return (error);
1040*0Sstevel@tonic-gate 			    }
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 			    /* Add component to the toplevel device */
1043*0Sstevel@tonic-gate 			    devconfig_set_components(
1044*0Sstevel@tonic-gate 				device, dlist_append(dlist_new_item(component),
1045*0Sstevel@tonic-gate 				devconfig_get_components(device), AT_TAIL));
1046*0Sstevel@tonic-gate 			}
1047*0Sstevel@tonic-gate 		    } else {
1048*0Sstevel@tonic-gate 			component = device;
1049*0Sstevel@tonic-gate 		    }
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 		    /* For each attribute defined for this element... */
1052*0Sstevel@tonic-gate 		    for (j = 0; element->attributes[j].name != NULL; j++) {
1053*0Sstevel@tonic-gate 			attr_t *attribute = &(element->attributes[j]);
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 			/* Get the value of this attribute */
1056*0Sstevel@tonic-gate 			char *value = (char *)
1057*0Sstevel@tonic-gate 			    xmlGetProp(cur, (xmlChar *)attribute->name);
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 			/* Was this attribute specified? */
1060*0Sstevel@tonic-gate 			if (value != NULL) {
1061*0Sstevel@tonic-gate 			    oprintf(OUTPUT_DEBUG,
1062*0Sstevel@tonic-gate 				gettext("line %d:\tAttribute %s=%s\n"),
1063*0Sstevel@tonic-gate 				XML_GET_LINE(cur), attribute->name, value);
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 			    /* Set this value in the device */
1066*0Sstevel@tonic-gate 			    if ((error = attribute->validate_set(
1067*0Sstevel@tonic-gate 				component, attribute->name, value)) != 0) {
1068*0Sstevel@tonic-gate 				return (error);
1069*0Sstevel@tonic-gate 			    }
1070*0Sstevel@tonic-gate 			}
1071*0Sstevel@tonic-gate 		    }
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 		    /* Get recursive sub-elements */
1074*0Sstevel@tonic-gate 		    if ((error = xml_to_devconfig(
1075*0Sstevel@tonic-gate 			cur, elements, component)) != 0) {
1076*0Sstevel@tonic-gate 			return (error);
1077*0Sstevel@tonic-gate 		    }
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 		    /* Element found */
1080*0Sstevel@tonic-gate 		    break;
1081*0Sstevel@tonic-gate 		}
1082*0Sstevel@tonic-gate 	    }
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	    /* Make sure all non-text/comment elements were parsed */
1086*0Sstevel@tonic-gate 	    if (parsed_elem == B_FALSE &&
1087*0Sstevel@tonic-gate 		xmlStrcmp(cur->name, (xmlChar *)ELEMENT_TEXT) != 0 &&
1088*0Sstevel@tonic-gate 		xmlStrcmp(cur->name, (xmlChar *)ELEMENT_COMMENT) != 0) {
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 		oprintf(OUTPUT_DEBUG, gettext("Element <%s> NOT PARSED!!!\n"),
1091*0Sstevel@tonic-gate 		    cur->name);
1092*0Sstevel@tonic-gate 	    }
1093*0Sstevel@tonic-gate 	}
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 	return (0);
1096*0Sstevel@tonic-gate }
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate /*
1099*0Sstevel@tonic-gate  * Returns 0 if obj2 (devconfig_t *) is a disk set, 1 otherwise.
1100*0Sstevel@tonic-gate  */
1101*0Sstevel@tonic-gate static int
compare_is_a_diskset(void * obj1,void * obj2)1102*0Sstevel@tonic-gate compare_is_a_diskset(
1103*0Sstevel@tonic-gate 	void *obj1,
1104*0Sstevel@tonic-gate 	void *obj2)
1105*0Sstevel@tonic-gate {
1106*0Sstevel@tonic-gate 	return (devconfig_isA(
1107*0Sstevel@tonic-gate 	    (devconfig_t *)obj2, TYPE_DISKSET) == B_TRUE ? 0 : 1);
1108*0Sstevel@tonic-gate }
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate /*
1111*0Sstevel@tonic-gate  * Recursively searches the given xmlNodePtr for an element of the
1112*0Sstevel@tonic-gate  * specified type and name.
1113*0Sstevel@tonic-gate  *
1114*0Sstevel@tonic-gate  * @param       node
1115*0Sstevel@tonic-gate  *              the root node to search
1116*0Sstevel@tonic-gate  *
1117*0Sstevel@tonic-gate  * @param       element
1118*0Sstevel@tonic-gate  *              the name of the element type
1119*0Sstevel@tonic-gate  *
1120*0Sstevel@tonic-gate  * @param       name
1121*0Sstevel@tonic-gate  *              the value of the name attribute
1122*0Sstevel@tonic-gate  *
1123*0Sstevel@tonic-gate  * @return      a valid xmlNodePtr if an element of the specified
1124*0Sstevel@tonic-gate  *              type and name was found, NULL otherwise.
1125*0Sstevel@tonic-gate  */
1126*0Sstevel@tonic-gate static xmlNodePtr
xml_find_node(xmlNodePtr node,xmlChar * element,xmlChar * name)1127*0Sstevel@tonic-gate xml_find_node(
1128*0Sstevel@tonic-gate 	xmlNodePtr node,
1129*0Sstevel@tonic-gate 	xmlChar *element,
1130*0Sstevel@tonic-gate 	xmlChar *name)
1131*0Sstevel@tonic-gate {
1132*0Sstevel@tonic-gate 	xmlNodePtr child;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 	/* Is the element the right type? */
1135*0Sstevel@tonic-gate 	if (xmlStrcmp(element, node->name) == 0 &&
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate 	    /* Does this element's name attribute match? */
1138*0Sstevel@tonic-gate 	    xmlStrcmp(name, xmlGetProp(node, (xmlChar *)ATTR_NAME)) == 0) {
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 	    return (node);
1141*0Sstevel@tonic-gate 	}
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 	/* Check child nodes */
1144*0Sstevel@tonic-gate 	for (child = node->xmlChildrenNode; child != NULL;
1145*0Sstevel@tonic-gate 	    child = child->next) {
1146*0Sstevel@tonic-gate 	    xmlNodePtr found = xml_find_node(child, element, name);
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 	    if (found != NULL) {
1149*0Sstevel@tonic-gate 		return (found);
1150*0Sstevel@tonic-gate 	    }
1151*0Sstevel@tonic-gate 	}
1152*0Sstevel@tonic-gate 
1153*0Sstevel@tonic-gate 	return (NULL);
1154*0Sstevel@tonic-gate }
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate /*
1157*0Sstevel@tonic-gate  * Creates an XML document containing all of the localized message
1158*0Sstevel@tonic-gate  * strings for the generated command script.
1159*0Sstevel@tonic-gate  *
1160*0Sstevel@tonic-gate  * @return      a xmlDocPtr which must be freed via xmlFreeDoc
1161*0Sstevel@tonic-gate  */
1162*0Sstevel@tonic-gate static xmlDocPtr
create_localized_message_doc()1163*0Sstevel@tonic-gate create_localized_message_doc()
1164*0Sstevel@tonic-gate {
1165*0Sstevel@tonic-gate 	int i;
1166*0Sstevel@tonic-gate 	char *locale;
1167*0Sstevel@tonic-gate 	xmlDocPtr doc;
1168*0Sstevel@tonic-gate 	xmlNodePtr root;
1169*0Sstevel@tonic-gate 	l10nmessage_t _cmd_messages[21];
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 	/* Create the XML document */
1172*0Sstevel@tonic-gate 	doc = xmlNewDoc((xmlChar *)"1.0");
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	/* Create the root node */
1175*0Sstevel@tonic-gate 	root = xmlNewDocNode(
1176*0Sstevel@tonic-gate 	    doc, NULL, (xmlChar *)ELEMENT_L10N, NULL);
1177*0Sstevel@tonic-gate 	xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate 	_cmd_messages[0].msgid = CMD_MSG_ENVIRONMENT;
1180*0Sstevel@tonic-gate 	_cmd_messages[0].message = gettext(CMD_MSG_ENVIRONMENT);
1181*0Sstevel@tonic-gate 	_cmd_messages[1].msgid = CMD_MSG_AMEND_PATH;
1182*0Sstevel@tonic-gate 	_cmd_messages[1].message = gettext(CMD_MSG_AMEND_PATH);
1183*0Sstevel@tonic-gate 	_cmd_messages[2].msgid = CMD_MSG_DISK_SET_NAME;
1184*0Sstevel@tonic-gate 	_cmd_messages[2].message = gettext(CMD_MSG_DISK_SET_NAME);
1185*0Sstevel@tonic-gate 	_cmd_messages[3].msgid = CMD_MSG_FUNCTIONS;
1186*0Sstevel@tonic-gate 	_cmd_messages[3].message = gettext(CMD_MSG_FUNCTIONS);
1187*0Sstevel@tonic-gate 	_cmd_messages[4].msgid = CMD_MSG_ECHO_AND_EXEC;
1188*0Sstevel@tonic-gate 	_cmd_messages[4].message = gettext(CMD_MSG_ECHO_AND_EXEC);
1189*0Sstevel@tonic-gate 	_cmd_messages[5].msgid = CMD_MSG_FMTHARD_SPECIAL;
1190*0Sstevel@tonic-gate 	_cmd_messages[5].message = gettext(CMD_MSG_FMTHARD_SPECIAL);
1191*0Sstevel@tonic-gate 	_cmd_messages[6].msgid = CMD_MSG_GET_FULL_PATH;
1192*0Sstevel@tonic-gate 	_cmd_messages[6].message = gettext(CMD_MSG_GET_FULL_PATH);
1193*0Sstevel@tonic-gate 	_cmd_messages[7].msgid = CMD_MSG_MAIN;
1194*0Sstevel@tonic-gate 	_cmd_messages[7].message = gettext(CMD_MSG_MAIN);
1195*0Sstevel@tonic-gate 	_cmd_messages[8].msgid = CMD_MSG_VERIFY_ROOT;
1196*0Sstevel@tonic-gate 	_cmd_messages[8].message = gettext(CMD_MSG_VERIFY_ROOT);
1197*0Sstevel@tonic-gate 	_cmd_messages[9].msgid = CMD_MSG_RUN_AS_ROOT;
1198*0Sstevel@tonic-gate 	_cmd_messages[9].message = gettext(CMD_MSG_RUN_AS_ROOT);
1199*0Sstevel@tonic-gate 	_cmd_messages[10].msgid = CMD_MSG_CHECK_FOR_VERBOSE;
1200*0Sstevel@tonic-gate 	_cmd_messages[10].message = gettext(CMD_MSG_CHECK_FOR_VERBOSE);
1201*0Sstevel@tonic-gate 	_cmd_messages[11].msgid = (CMD_MSG_DOES_DISK_SET_EXIST);
1202*0Sstevel@tonic-gate 	_cmd_messages[11].message = gettext(CMD_MSG_DOES_DISK_SET_EXIST);
1203*0Sstevel@tonic-gate 	_cmd_messages[12].msgid = (CMD_MSG_TAKE_DISK_SET);
1204*0Sstevel@tonic-gate 	_cmd_messages[12].message = gettext(CMD_MSG_TAKE_DISK_SET);
1205*0Sstevel@tonic-gate 	_cmd_messages[13].msgid = (CMD_MSG_CREATE_THE_DISK_SET);
1206*0Sstevel@tonic-gate 	_cmd_messages[13].message = gettext(CMD_MSG_CREATE_THE_DISK_SET);
1207*0Sstevel@tonic-gate 	_cmd_messages[14].msgid = (CMD_MSG_ADD_DISKS_TO_SET);
1208*0Sstevel@tonic-gate 	_cmd_messages[14].message = gettext(CMD_MSG_ADD_DISKS_TO_SET);
1209*0Sstevel@tonic-gate 	_cmd_messages[15].msgid = (CMD_MSG_FORMAT_SLICES);
1210*0Sstevel@tonic-gate 	_cmd_messages[15].message = gettext(CMD_MSG_FORMAT_SLICES);
1211*0Sstevel@tonic-gate 	_cmd_messages[16].msgid = (CMD_MSG_CREATE);
1212*0Sstevel@tonic-gate 	_cmd_messages[16].message = gettext(CMD_MSG_CREATE);
1213*0Sstevel@tonic-gate 	_cmd_messages[17].msgid = (CMD_MSG_DOES_EXIST);
1214*0Sstevel@tonic-gate 	_cmd_messages[17].message = gettext(CMD_MSG_DOES_EXIST);
1215*0Sstevel@tonic-gate 	_cmd_messages[18].msgid = (CMD_MSG_ADD_SLICES_TO);
1216*0Sstevel@tonic-gate 	_cmd_messages[18].message = gettext(CMD_MSG_ADD_SLICES_TO);
1217*0Sstevel@tonic-gate 	_cmd_messages[19].msgid = (CMD_MSG_ASSOCIATE_WITH_HSP);
1218*0Sstevel@tonic-gate 	_cmd_messages[19].message = gettext(CMD_MSG_ASSOCIATE_WITH_HSP);
1219*0Sstevel@tonic-gate 	_cmd_messages[20].msgid = NULL;
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate 	/* Get/set current locale in the "lang" node */
1222*0Sstevel@tonic-gate 	locale = setlocale(LC_MESSAGES, NULL);
1223*0Sstevel@tonic-gate 
1224*0Sstevel@tonic-gate 	/* Add localized <message> elements to stylesheet */
1225*0Sstevel@tonic-gate 	for (i = 0; _cmd_messages[i].msgid != NULL; i++) {
1226*0Sstevel@tonic-gate 	    xmlNsPtr ns = xmlNewNs(NULL, NULL, NULL);
1227*0Sstevel@tonic-gate 
1228*0Sstevel@tonic-gate 	    xmlNodePtr node = xmlNewTextChild(
1229*0Sstevel@tonic-gate 		root, ns, (xmlChar *)ELEMENT_MESSAGE,
1230*0Sstevel@tonic-gate 		(xmlChar *)_cmd_messages[i].message);
1231*0Sstevel@tonic-gate 	    /* Lang attribute */
1232*0Sstevel@tonic-gate 	    xmlSetProp(node,
1233*0Sstevel@tonic-gate 		(xmlChar *)ATTR_LANG, (xmlChar *)locale);
1234*0Sstevel@tonic-gate 
1235*0Sstevel@tonic-gate 	    /* Message ID attribute */
1236*0Sstevel@tonic-gate 	    xmlSetProp(node, (xmlChar *)ATTR_MESSAGEID,
1237*0Sstevel@tonic-gate 		(xmlChar *)_cmd_messages[i].msgid);
1238*0Sstevel@tonic-gate 	}
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	if (get_max_verbosity() >= OUTPUT_DEBUG) {
1241*0Sstevel@tonic-gate 	    xmlChar *text;
1242*0Sstevel@tonic-gate 	    /* Get the text dump */
1243*0Sstevel@tonic-gate 	    xmlDocDumpFormatMemory(doc, &text, NULL, 1);
1244*0Sstevel@tonic-gate 	    oprintf(OUTPUT_DEBUG,
1245*0Sstevel@tonic-gate 		gettext("Generated message file:\n%s"), text);
1246*0Sstevel@tonic-gate 	    xmlFree(text);
1247*0Sstevel@tonic-gate 	}
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate 	return (doc);
1250*0Sstevel@tonic-gate }
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate /*
1253*0Sstevel@tonic-gate  * Creates a temporary XML file containing all of the localized
1254*0Sstevel@tonic-gate  * message strings for the generated command script.
1255*0Sstevel@tonic-gate  *
1256*0Sstevel@tonic-gate  * @param       tmpfile
1257*0Sstevel@tonic-gate  *		RETURN: the name of the temporary XML file
1258*0Sstevel@tonic-gate  *
1259*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1260*0Sstevel@tonic-gate  */
1261*0Sstevel@tonic-gate static int
create_localized_message_file(char ** tmpfile)1262*0Sstevel@tonic-gate create_localized_message_file(
1263*0Sstevel@tonic-gate 	char **tmpfile)
1264*0Sstevel@tonic-gate {
1265*0Sstevel@tonic-gate 	int error = 0;
1266*0Sstevel@tonic-gate 
1267*0Sstevel@tonic-gate 	/*
1268*0Sstevel@tonic-gate 	 * Create temporary file name -- "XXXXXX" is replaced with
1269*0Sstevel@tonic-gate 	 * unique char sequence by mkstemp()
1270*0Sstevel@tonic-gate 	 */
1271*0Sstevel@tonic-gate 	*tmpfile = stralloccat(3, "/tmp/", ELEMENT_L10N, "XXXXXX");
1272*0Sstevel@tonic-gate 
1273*0Sstevel@tonic-gate 	if (*tmpfile == NULL) {
1274*0Sstevel@tonic-gate 	    volume_set_error(gettext("out of memory"));
1275*0Sstevel@tonic-gate 	    error = -1;
1276*0Sstevel@tonic-gate 	} else {
1277*0Sstevel@tonic-gate 	    int fildes;
1278*0Sstevel@tonic-gate 	    FILE *msgfile = NULL;
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate 	    /* Open temp file */
1281*0Sstevel@tonic-gate 	    if ((fildes = mkstemp(*tmpfile)) != -1) {
1282*0Sstevel@tonic-gate 		msgfile = fdopen(fildes, "w");
1283*0Sstevel@tonic-gate 	    }
1284*0Sstevel@tonic-gate 
1285*0Sstevel@tonic-gate 	    if (msgfile == NULL) {
1286*0Sstevel@tonic-gate 		volume_set_error(gettext(
1287*0Sstevel@tonic-gate 		    "could not open file for writing: %s"), *tmpfile);
1288*0Sstevel@tonic-gate 		error = -1;
1289*0Sstevel@tonic-gate 	    } else {
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 		xmlChar *text;
1292*0Sstevel@tonic-gate 		xmlDocPtr message_doc = create_localized_message_doc();
1293*0Sstevel@tonic-gate 		xmlDocDumpFormatMemory(message_doc, &text, NULL, 1);
1294*0Sstevel@tonic-gate 
1295*0Sstevel@tonic-gate 		if (fprintf(msgfile, "%s", text) < 0) {
1296*0Sstevel@tonic-gate 		    volume_set_error(gettext(
1297*0Sstevel@tonic-gate 			"could not create localized message file: %s"),
1298*0Sstevel@tonic-gate 			*tmpfile);
1299*0Sstevel@tonic-gate 		    error = -1;
1300*0Sstevel@tonic-gate 		}
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 		xmlFree(text);
1303*0Sstevel@tonic-gate 		xmlFreeDoc(message_doc);
1304*0Sstevel@tonic-gate 	    }
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 	    fclose(msgfile);
1307*0Sstevel@tonic-gate 	}
1308*0Sstevel@tonic-gate 
1309*0Sstevel@tonic-gate 	return (error);
1310*0Sstevel@tonic-gate }
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate /*
1313*0Sstevel@tonic-gate  * Converts the given string into a boolean.  The string must be
1314*0Sstevel@tonic-gate  * either VALID_ATTR_TRUE or VALID_ATTR_FALSE.
1315*0Sstevel@tonic-gate  *
1316*0Sstevel@tonic-gate  * @param       str
1317*0Sstevel@tonic-gate  *              the string to convert
1318*0Sstevel@tonic-gate  *
1319*0Sstevel@tonic-gate  * @param       bool
1320*0Sstevel@tonic-gate  *              the addr of the boolean_t
1321*0Sstevel@tonic-gate  *
1322*0Sstevel@tonic-gate  * @return      0 if the given string could be converted to a boolean
1323*0Sstevel@tonic-gate  *              non-zero otherwise.
1324*0Sstevel@tonic-gate  */
1325*0Sstevel@tonic-gate static int
strtobool(char * str,boolean_t * value)1326*0Sstevel@tonic-gate strtobool(
1327*0Sstevel@tonic-gate 	char *str,
1328*0Sstevel@tonic-gate 	boolean_t *value)
1329*0Sstevel@tonic-gate {
1330*0Sstevel@tonic-gate 	int error = 0;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 	if (strcmp(str, VALID_ATTR_TRUE) == 0) {
1333*0Sstevel@tonic-gate 	    *value = B_TRUE;
1334*0Sstevel@tonic-gate 	} else
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate 	if (strcmp(str, VALID_ATTR_FALSE) == 0) {
1337*0Sstevel@tonic-gate 	    *value = B_FALSE;
1338*0Sstevel@tonic-gate 	} else
1339*0Sstevel@tonic-gate 
1340*0Sstevel@tonic-gate 	    error = -1;
1341*0Sstevel@tonic-gate 
1342*0Sstevel@tonic-gate 	return (error);
1343*0Sstevel@tonic-gate }
1344*0Sstevel@tonic-gate 
1345*0Sstevel@tonic-gate /*
1346*0Sstevel@tonic-gate  * Wrapper for oprintf with a OUTPUT_TERSE level of verbosity.
1347*0Sstevel@tonic-gate  * Provides an fprintf-like syntax to enable use as substitute output
1348*0Sstevel@tonic-gate  * handler for man of the XML commands.
1349*0Sstevel@tonic-gate  *
1350*0Sstevel@tonic-gate  * @param       unused
1351*0Sstevel@tonic-gate  *		unused, in favor of the FILE* passed to
1352*0Sstevel@tonic-gate  *		set_max_verbosity().
1353*0Sstevel@tonic-gate  *
1354*0Sstevel@tonic-gate  * @param       fmt
1355*0Sstevel@tonic-gate  *		a printf-style format string
1356*0Sstevel@tonic-gate  *
1357*0Sstevel@tonic-gate  * @return      the number of characters output
1358*0Sstevel@tonic-gate  */
1359*0Sstevel@tonic-gate static int
ofprintf_terse(void * unused,char * fmt,...)1360*0Sstevel@tonic-gate ofprintf_terse(
1361*0Sstevel@tonic-gate 	void *unused,
1362*0Sstevel@tonic-gate 	char *fmt,
1363*0Sstevel@tonic-gate 	...)
1364*0Sstevel@tonic-gate {
1365*0Sstevel@tonic-gate 	int ret;
1366*0Sstevel@tonic-gate 	va_list ap;
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 	va_start(ap, fmt);
1369*0Sstevel@tonic-gate 	ret = oprintf_va(OUTPUT_TERSE, fmt, ap);
1370*0Sstevel@tonic-gate 	va_end(ap);
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 	return (ret);
1373*0Sstevel@tonic-gate }
1374*0Sstevel@tonic-gate 
1375*0Sstevel@tonic-gate /*
1376*0Sstevel@tonic-gate  * Wrapper for oprintf with a OUTPUT_VERBOSE level of verbosity.
1377*0Sstevel@tonic-gate  * Provides an fprintf-like syntax to enable use as substitute output
1378*0Sstevel@tonic-gate  * handler for man of the XML commands.
1379*0Sstevel@tonic-gate  *
1380*0Sstevel@tonic-gate  * @param       unused
1381*0Sstevel@tonic-gate  *		unused, in favor of the FILE* passed to
1382*0Sstevel@tonic-gate  *		set_max_verbosity().
1383*0Sstevel@tonic-gate  *
1384*0Sstevel@tonic-gate  * @param       fmt
1385*0Sstevel@tonic-gate  *		a printf-style format string
1386*0Sstevel@tonic-gate  *
1387*0Sstevel@tonic-gate  * @return      the number of characters output
1388*0Sstevel@tonic-gate  */
1389*0Sstevel@tonic-gate static int
ofprintf_verbose(void * unused,char * fmt,...)1390*0Sstevel@tonic-gate ofprintf_verbose(
1391*0Sstevel@tonic-gate 	void *unused,
1392*0Sstevel@tonic-gate 	char *fmt,
1393*0Sstevel@tonic-gate 	...)
1394*0Sstevel@tonic-gate {
1395*0Sstevel@tonic-gate 	int ret;
1396*0Sstevel@tonic-gate 	va_list ap;
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 	va_start(ap, fmt);
1399*0Sstevel@tonic-gate 	ret = oprintf_va(OUTPUT_VERBOSE, fmt, ap);
1400*0Sstevel@tonic-gate 	va_end(ap);
1401*0Sstevel@tonic-gate 
1402*0Sstevel@tonic-gate 	return (ret);
1403*0Sstevel@tonic-gate }
1404*0Sstevel@tonic-gate 
1405*0Sstevel@tonic-gate /*
1406*0Sstevel@tonic-gate  * ******************************************************************
1407*0Sstevel@tonic-gate  *
1408*0Sstevel@tonic-gate  * XML attribute validators/mutators
1409*0Sstevel@tonic-gate  *
1410*0Sstevel@tonic-gate  * These functions convert the given XML attribute string to the
1411*0Sstevel@tonic-gate  * appropriate data type, and then pass it on to the appropriate
1412*0Sstevel@tonic-gate  * devconfig_t mutator.  A non-zero status is returned if the given
1413*0Sstevel@tonic-gate  * string could not be converted or was invalid.
1414*0Sstevel@tonic-gate  *
1415*0Sstevel@tonic-gate  * ******************************************************************
1416*0Sstevel@tonic-gate  */
1417*0Sstevel@tonic-gate 
1418*0Sstevel@tonic-gate /*
1419*0Sstevel@tonic-gate  * Validate and set the size attribute in the given volume
1420*0Sstevel@tonic-gate  * devconfig_t.
1421*0Sstevel@tonic-gate  *
1422*0Sstevel@tonic-gate  * @param       volume
1423*0Sstevel@tonic-gate  *		the devconfig_t in which to set the size
1424*0Sstevel@tonic-gate  *
1425*0Sstevel@tonic-gate  * @param       attr
1426*0Sstevel@tonic-gate  *		the name of the XML attribute
1427*0Sstevel@tonic-gate  *
1428*0Sstevel@tonic-gate  * @param       value
1429*0Sstevel@tonic-gate  *		the value of the XML attribute
1430*0Sstevel@tonic-gate  *
1431*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1432*0Sstevel@tonic-gate  */
1433*0Sstevel@tonic-gate static int
validate_set_size(devconfig_t * volume,char * attr,char * value)1434*0Sstevel@tonic-gate validate_set_size(
1435*0Sstevel@tonic-gate 	devconfig_t *volume,
1436*0Sstevel@tonic-gate 	char *attr,
1437*0Sstevel@tonic-gate 	char *value)
1438*0Sstevel@tonic-gate {
1439*0Sstevel@tonic-gate 	int error;
1440*0Sstevel@tonic-gate 	uint64_t size = 0;
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 	/* Convert size string to bytes */
1443*0Sstevel@tonic-gate 	if ((error = sizestr_to_bytes(value, &size, size_units)) != 0) {
1444*0Sstevel@tonic-gate 	    return (error);
1445*0Sstevel@tonic-gate 	}
1446*0Sstevel@tonic-gate 
1447*0Sstevel@tonic-gate 	/* Set size in volume */
1448*0Sstevel@tonic-gate 	return (devconfig_set_size(volume, size));
1449*0Sstevel@tonic-gate }
1450*0Sstevel@tonic-gate 
1451*0Sstevel@tonic-gate /*
1452*0Sstevel@tonic-gate  * Validate and set the size_in_blocks attribute in the given slice
1453*0Sstevel@tonic-gate  * devconfig_t.
1454*0Sstevel@tonic-gate  *
1455*0Sstevel@tonic-gate  * @param       volume
1456*0Sstevel@tonic-gate  *		the devconfig_t in which to set the size_in_blocks
1457*0Sstevel@tonic-gate  *
1458*0Sstevel@tonic-gate  * @param       attr
1459*0Sstevel@tonic-gate  *		the name of the XML attribute
1460*0Sstevel@tonic-gate  *
1461*0Sstevel@tonic-gate  * @param       value
1462*0Sstevel@tonic-gate  *		the value of the XML attribute
1463*0Sstevel@tonic-gate  *
1464*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1465*0Sstevel@tonic-gate  */
1466*0Sstevel@tonic-gate static int
validate_set_size_in_blocks(devconfig_t * slice,char * attr,char * value)1467*0Sstevel@tonic-gate validate_set_size_in_blocks(
1468*0Sstevel@tonic-gate 	devconfig_t *slice,
1469*0Sstevel@tonic-gate 	char *attr,
1470*0Sstevel@tonic-gate 	char *value)
1471*0Sstevel@tonic-gate {
1472*0Sstevel@tonic-gate 	long long size;
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate 	/* Convert string to long long */
1475*0Sstevel@tonic-gate 	if (sscanf(value, "%lld", &size) != 1) {
1476*0Sstevel@tonic-gate 	    volume_set_error(gettext("%s: invalid size in blocks"), value);
1477*0Sstevel@tonic-gate 	    return (-1);
1478*0Sstevel@tonic-gate 	}
1479*0Sstevel@tonic-gate 
1480*0Sstevel@tonic-gate 	/* Set the number of submirrors in the slice */
1481*0Sstevel@tonic-gate 	return (devconfig_set_size_in_blocks(slice, (uint64_t)size));
1482*0Sstevel@tonic-gate }
1483*0Sstevel@tonic-gate 
1484*0Sstevel@tonic-gate /*
1485*0Sstevel@tonic-gate  * Validate and set the name attribute in the given diskset
1486*0Sstevel@tonic-gate  * devconfig_t.
1487*0Sstevel@tonic-gate  *
1488*0Sstevel@tonic-gate  * @param       volume
1489*0Sstevel@tonic-gate  *		the devconfig_t in which to set the name
1490*0Sstevel@tonic-gate  *
1491*0Sstevel@tonic-gate  * @param       attr
1492*0Sstevel@tonic-gate  *		the name of the XML attribute
1493*0Sstevel@tonic-gate  *
1494*0Sstevel@tonic-gate  * @param       name
1495*0Sstevel@tonic-gate  *		the value of the XML attribute
1496*0Sstevel@tonic-gate  *
1497*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1498*0Sstevel@tonic-gate  */
1499*0Sstevel@tonic-gate static int
validate_set_diskset_name(devconfig_t * diskset,char * attr,char * name)1500*0Sstevel@tonic-gate validate_set_diskset_name(
1501*0Sstevel@tonic-gate 	devconfig_t *diskset,
1502*0Sstevel@tonic-gate 	char *attr,
1503*0Sstevel@tonic-gate 	char *name)
1504*0Sstevel@tonic-gate {
1505*0Sstevel@tonic-gate 	return (devconfig_set_diskset_name(diskset, name));
1506*0Sstevel@tonic-gate }
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate /*
1509*0Sstevel@tonic-gate  * Validate and add the given name to the list of available devices in
1510*0Sstevel@tonic-gate  * the given volume devconfig_t.
1511*0Sstevel@tonic-gate  *
1512*0Sstevel@tonic-gate  * @param       device
1513*0Sstevel@tonic-gate  *		the devconfig_t whose available device list to modify
1514*0Sstevel@tonic-gate  *
1515*0Sstevel@tonic-gate  * @param       attr
1516*0Sstevel@tonic-gate  *		the name of the XML attribute
1517*0Sstevel@tonic-gate  *
1518*0Sstevel@tonic-gate  * @param       name
1519*0Sstevel@tonic-gate  *		the value of the XML attribute
1520*0Sstevel@tonic-gate  *
1521*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1522*0Sstevel@tonic-gate  */
1523*0Sstevel@tonic-gate static int
validate_add_available_name(devconfig_t * device,char * attr,char * name)1524*0Sstevel@tonic-gate validate_add_available_name(
1525*0Sstevel@tonic-gate 	devconfig_t *device,
1526*0Sstevel@tonic-gate 	char *attr,
1527*0Sstevel@tonic-gate 	char *name)
1528*0Sstevel@tonic-gate {
1529*0Sstevel@tonic-gate 	char **available;
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 	/* Get available devices for this device */
1532*0Sstevel@tonic-gate 	available = devconfig_get_available(device);
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	/* Try to add name to array via realloc */
1535*0Sstevel@tonic-gate 	if ((available = append_to_string_array(available, name)) == NULL) {
1536*0Sstevel@tonic-gate 	    return (ENOMEM);
1537*0Sstevel@tonic-gate 	}
1538*0Sstevel@tonic-gate 
1539*0Sstevel@tonic-gate 	/* Set available devices in the device */
1540*0Sstevel@tonic-gate 	devconfig_set_available(device, available);
1541*0Sstevel@tonic-gate 
1542*0Sstevel@tonic-gate 	return (0);
1543*0Sstevel@tonic-gate }
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate /*
1546*0Sstevel@tonic-gate  * Validate and add the given name to the list of unavailable devices
1547*0Sstevel@tonic-gate  * in the given volume devconfig_t.
1548*0Sstevel@tonic-gate  *
1549*0Sstevel@tonic-gate  * @param       device
1550*0Sstevel@tonic-gate  *		the devconfig_t whose unavailable device list to modify
1551*0Sstevel@tonic-gate  *
1552*0Sstevel@tonic-gate  * @param       attr
1553*0Sstevel@tonic-gate  *		the name of the XML attribute
1554*0Sstevel@tonic-gate  *
1555*0Sstevel@tonic-gate  * @param       name
1556*0Sstevel@tonic-gate  *		the value of the XML attribute
1557*0Sstevel@tonic-gate  *
1558*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1559*0Sstevel@tonic-gate  */
1560*0Sstevel@tonic-gate static int
validate_add_unavailable_name(devconfig_t * device,char * attr,char * name)1561*0Sstevel@tonic-gate validate_add_unavailable_name(
1562*0Sstevel@tonic-gate 	devconfig_t *device,
1563*0Sstevel@tonic-gate 	char *attr,
1564*0Sstevel@tonic-gate 	char *name)
1565*0Sstevel@tonic-gate {
1566*0Sstevel@tonic-gate 	char **unavailable;
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 	/* Get unavailable devices for this device */
1569*0Sstevel@tonic-gate 	unavailable = devconfig_get_unavailable(device);
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate 	/* Try to add name to array via realloc */
1572*0Sstevel@tonic-gate 	if ((unavailable = append_to_string_array(unavailable, name)) == NULL) {
1573*0Sstevel@tonic-gate 	    return (ENOMEM);
1574*0Sstevel@tonic-gate 	}
1575*0Sstevel@tonic-gate 
1576*0Sstevel@tonic-gate 	/* Set unavailable devices in the device */
1577*0Sstevel@tonic-gate 	devconfig_set_unavailable(device, unavailable);
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	return (0);
1580*0Sstevel@tonic-gate }
1581*0Sstevel@tonic-gate 
1582*0Sstevel@tonic-gate /*
1583*0Sstevel@tonic-gate  * Validate and set the name attribute in the given hsp devconfig_t.
1584*0Sstevel@tonic-gate  *
1585*0Sstevel@tonic-gate  * @param       volume
1586*0Sstevel@tonic-gate  *		the devconfig_t in which to set the name
1587*0Sstevel@tonic-gate  *
1588*0Sstevel@tonic-gate  * @param       attr
1589*0Sstevel@tonic-gate  *		the name of the XML attribute
1590*0Sstevel@tonic-gate  *
1591*0Sstevel@tonic-gate  * @param       name
1592*0Sstevel@tonic-gate  *		the value of the XML attribute
1593*0Sstevel@tonic-gate  *
1594*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1595*0Sstevel@tonic-gate  */
1596*0Sstevel@tonic-gate static int
validate_set_hsp_name(devconfig_t * hsp,char * attr,char * name)1597*0Sstevel@tonic-gate validate_set_hsp_name(
1598*0Sstevel@tonic-gate 	devconfig_t *hsp,
1599*0Sstevel@tonic-gate 	char *attr,
1600*0Sstevel@tonic-gate 	char *name)
1601*0Sstevel@tonic-gate {
1602*0Sstevel@tonic-gate 	return (devconfig_set_hsp_name(hsp, name));
1603*0Sstevel@tonic-gate }
1604*0Sstevel@tonic-gate 
1605*0Sstevel@tonic-gate /*
1606*0Sstevel@tonic-gate  * Validate and set the name attribute in the given disk devconfig_t.
1607*0Sstevel@tonic-gate  *
1608*0Sstevel@tonic-gate  * @param       volume
1609*0Sstevel@tonic-gate  *		the devconfig_t in which to set the name
1610*0Sstevel@tonic-gate  *
1611*0Sstevel@tonic-gate  * @param       attr
1612*0Sstevel@tonic-gate  *		the name of the XML attribute
1613*0Sstevel@tonic-gate  *
1614*0Sstevel@tonic-gate  * @param       name
1615*0Sstevel@tonic-gate  *		the value of the XML attribute
1616*0Sstevel@tonic-gate  *
1617*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1618*0Sstevel@tonic-gate  */
1619*0Sstevel@tonic-gate static int
validate_set_disk_name(devconfig_t * disk,char * attr,char * name)1620*0Sstevel@tonic-gate validate_set_disk_name(
1621*0Sstevel@tonic-gate 	devconfig_t *disk,
1622*0Sstevel@tonic-gate 	char *attr,
1623*0Sstevel@tonic-gate 	char *name)
1624*0Sstevel@tonic-gate {
1625*0Sstevel@tonic-gate 	return (devconfig_set_name(disk, name));
1626*0Sstevel@tonic-gate }
1627*0Sstevel@tonic-gate 
1628*0Sstevel@tonic-gate /*
1629*0Sstevel@tonic-gate  * Validate and set the name attribute in the given slice devconfig_t.
1630*0Sstevel@tonic-gate  *
1631*0Sstevel@tonic-gate  * @param       volume
1632*0Sstevel@tonic-gate  *		the devconfig_t in which to set the name
1633*0Sstevel@tonic-gate  *
1634*0Sstevel@tonic-gate  * @param       attr
1635*0Sstevel@tonic-gate  *		the name of the XML attribute
1636*0Sstevel@tonic-gate  *
1637*0Sstevel@tonic-gate  * @param       name
1638*0Sstevel@tonic-gate  *		the value of the XML attribute
1639*0Sstevel@tonic-gate  *
1640*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1641*0Sstevel@tonic-gate  */
1642*0Sstevel@tonic-gate static int
validate_set_slice_name(devconfig_t * slice,char * attr,char * name)1643*0Sstevel@tonic-gate validate_set_slice_name(
1644*0Sstevel@tonic-gate 	devconfig_t *slice,
1645*0Sstevel@tonic-gate 	char *attr,
1646*0Sstevel@tonic-gate 	char *name)
1647*0Sstevel@tonic-gate {
1648*0Sstevel@tonic-gate 	return (devconfig_set_name(slice, name));
1649*0Sstevel@tonic-gate }
1650*0Sstevel@tonic-gate 
1651*0Sstevel@tonic-gate /*
1652*0Sstevel@tonic-gate  * Validate and set the start_block attribute in the given slice
1653*0Sstevel@tonic-gate  * devconfig_t.
1654*0Sstevel@tonic-gate  *
1655*0Sstevel@tonic-gate  * @param       volume
1656*0Sstevel@tonic-gate  *		the devconfig_t in which to set the start_block
1657*0Sstevel@tonic-gate  *
1658*0Sstevel@tonic-gate  * @param       attr
1659*0Sstevel@tonic-gate  *		the name of the XML attribute
1660*0Sstevel@tonic-gate  *
1661*0Sstevel@tonic-gate  * @param       value
1662*0Sstevel@tonic-gate  *		the value of the XML attribute
1663*0Sstevel@tonic-gate  *
1664*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1665*0Sstevel@tonic-gate  */
1666*0Sstevel@tonic-gate static int
validate_set_slice_start_block(devconfig_t * slice,char * attr,char * value)1667*0Sstevel@tonic-gate validate_set_slice_start_block(
1668*0Sstevel@tonic-gate 	devconfig_t *slice,
1669*0Sstevel@tonic-gate 	char *attr,
1670*0Sstevel@tonic-gate 	char *value)
1671*0Sstevel@tonic-gate {
1672*0Sstevel@tonic-gate 	long long startsector;
1673*0Sstevel@tonic-gate 
1674*0Sstevel@tonic-gate 	/* Convert string to long long */
1675*0Sstevel@tonic-gate 	if (sscanf(value, "%lld", &startsector) != 1) {
1676*0Sstevel@tonic-gate 	    volume_set_error(gettext("%s: invalid start sector"), value);
1677*0Sstevel@tonic-gate 	    return (-1);
1678*0Sstevel@tonic-gate 	}
1679*0Sstevel@tonic-gate 
1680*0Sstevel@tonic-gate 	/* Set the number of submirrors in the slice */
1681*0Sstevel@tonic-gate 	return (devconfig_set_slice_start_block(slice, (uint64_t)startsector));
1682*0Sstevel@tonic-gate }
1683*0Sstevel@tonic-gate 
1684*0Sstevel@tonic-gate /*
1685*0Sstevel@tonic-gate  * Validate and set the name attribute in the given volume
1686*0Sstevel@tonic-gate  * devconfig_t.
1687*0Sstevel@tonic-gate  *
1688*0Sstevel@tonic-gate  * @param       volume
1689*0Sstevel@tonic-gate  *		the devconfig_t in which to set the name
1690*0Sstevel@tonic-gate  *
1691*0Sstevel@tonic-gate  * @param       attr
1692*0Sstevel@tonic-gate  *		the name of the XML attribute
1693*0Sstevel@tonic-gate  *
1694*0Sstevel@tonic-gate  * @param       name
1695*0Sstevel@tonic-gate  *		the value of the XML attribute
1696*0Sstevel@tonic-gate  *
1697*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1698*0Sstevel@tonic-gate  */
1699*0Sstevel@tonic-gate static int
validate_set_volume_name(devconfig_t * volume,char * attr,char * name)1700*0Sstevel@tonic-gate validate_set_volume_name(
1701*0Sstevel@tonic-gate 	devconfig_t *volume,
1702*0Sstevel@tonic-gate 	char *attr,
1703*0Sstevel@tonic-gate 	char *name)
1704*0Sstevel@tonic-gate {
1705*0Sstevel@tonic-gate 	return (devconfig_set_volume_name(volume, name));
1706*0Sstevel@tonic-gate }
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate /*
1709*0Sstevel@tonic-gate  * Validate and set the interlace attribute in the given stripe
1710*0Sstevel@tonic-gate  * devconfig_t.
1711*0Sstevel@tonic-gate  *
1712*0Sstevel@tonic-gate  * @param       volume
1713*0Sstevel@tonic-gate  *		the devconfig_t in which to set the interlace
1714*0Sstevel@tonic-gate  *
1715*0Sstevel@tonic-gate  * @param       attr
1716*0Sstevel@tonic-gate  *		the name of the XML attribute
1717*0Sstevel@tonic-gate  *
1718*0Sstevel@tonic-gate  * @param       value
1719*0Sstevel@tonic-gate  *		the value of the XML attribute
1720*0Sstevel@tonic-gate  *
1721*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1722*0Sstevel@tonic-gate  */
1723*0Sstevel@tonic-gate static int
validate_set_stripe_interlace(devconfig_t * stripe,char * attr,char * value)1724*0Sstevel@tonic-gate validate_set_stripe_interlace(
1725*0Sstevel@tonic-gate 	devconfig_t *stripe,
1726*0Sstevel@tonic-gate 	char *attr,
1727*0Sstevel@tonic-gate 	char *value)
1728*0Sstevel@tonic-gate {
1729*0Sstevel@tonic-gate 	int error;
1730*0Sstevel@tonic-gate 	uint64_t interlace = 0;
1731*0Sstevel@tonic-gate 
1732*0Sstevel@tonic-gate 	/* Convert interlace string to bytes */
1733*0Sstevel@tonic-gate 	if ((error = sizestr_to_bytes(
1734*0Sstevel@tonic-gate 		value, &interlace, interlace_units)) != 0) {
1735*0Sstevel@tonic-gate 	    return (error);
1736*0Sstevel@tonic-gate 	}
1737*0Sstevel@tonic-gate 
1738*0Sstevel@tonic-gate 	/* Set interlace in stripe */
1739*0Sstevel@tonic-gate 	return (devconfig_set_stripe_interlace(stripe, interlace));
1740*0Sstevel@tonic-gate }
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate /*
1743*0Sstevel@tonic-gate  * Validate and set the mincomp attribute in the given stripe
1744*0Sstevel@tonic-gate  * devconfig_t.
1745*0Sstevel@tonic-gate  *
1746*0Sstevel@tonic-gate  * @param       volume
1747*0Sstevel@tonic-gate  *		the devconfig_t in which to set the mincomp
1748*0Sstevel@tonic-gate  *
1749*0Sstevel@tonic-gate  * @param       attr
1750*0Sstevel@tonic-gate  *		the name of the XML attribute
1751*0Sstevel@tonic-gate  *
1752*0Sstevel@tonic-gate  * @param       value
1753*0Sstevel@tonic-gate  *		the value of the XML attribute
1754*0Sstevel@tonic-gate  *
1755*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1756*0Sstevel@tonic-gate  */
1757*0Sstevel@tonic-gate static int
validate_set_stripe_mincomp(devconfig_t * stripe,char * attr,char * value)1758*0Sstevel@tonic-gate validate_set_stripe_mincomp(
1759*0Sstevel@tonic-gate 	devconfig_t *stripe,
1760*0Sstevel@tonic-gate 	char *attr,
1761*0Sstevel@tonic-gate 	char *value)
1762*0Sstevel@tonic-gate {
1763*0Sstevel@tonic-gate 	uint16_t mincomp;
1764*0Sstevel@tonic-gate 
1765*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
1766*0Sstevel@tonic-gate 	if (str_to_uint16(value, &mincomp) != 0) {
1767*0Sstevel@tonic-gate 	    volume_set_error(
1768*0Sstevel@tonic-gate 		gettext("invalid minimum stripe components (%s): %s"),
1769*0Sstevel@tonic-gate 		attr, value);
1770*0Sstevel@tonic-gate 	    return (-1);
1771*0Sstevel@tonic-gate 	}
1772*0Sstevel@tonic-gate 
1773*0Sstevel@tonic-gate 	/* Set in stripe */
1774*0Sstevel@tonic-gate 	return (devconfig_set_stripe_mincomp(stripe, mincomp));
1775*0Sstevel@tonic-gate }
1776*0Sstevel@tonic-gate 
1777*0Sstevel@tonic-gate /*
1778*0Sstevel@tonic-gate  * Validate and set the maxcomp attribute in the given stripe
1779*0Sstevel@tonic-gate  * devconfig_t.
1780*0Sstevel@tonic-gate  *
1781*0Sstevel@tonic-gate  * @param       volume
1782*0Sstevel@tonic-gate  *		the devconfig_t in which to set the maxcomp
1783*0Sstevel@tonic-gate  *
1784*0Sstevel@tonic-gate  * @param       attr
1785*0Sstevel@tonic-gate  *		the name of the XML attribute
1786*0Sstevel@tonic-gate  *
1787*0Sstevel@tonic-gate  * @param       value
1788*0Sstevel@tonic-gate  *		the value of the XML attribute
1789*0Sstevel@tonic-gate  *
1790*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1791*0Sstevel@tonic-gate  */
1792*0Sstevel@tonic-gate static int
validate_set_stripe_maxcomp(devconfig_t * stripe,char * attr,char * value)1793*0Sstevel@tonic-gate validate_set_stripe_maxcomp(
1794*0Sstevel@tonic-gate 	devconfig_t *stripe,
1795*0Sstevel@tonic-gate 	char *attr,
1796*0Sstevel@tonic-gate 	char *value)
1797*0Sstevel@tonic-gate {
1798*0Sstevel@tonic-gate 	uint16_t maxcomp;
1799*0Sstevel@tonic-gate 
1800*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
1801*0Sstevel@tonic-gate 	if (str_to_uint16(value, &maxcomp) != 0) {
1802*0Sstevel@tonic-gate 	    volume_set_error(
1803*0Sstevel@tonic-gate 		gettext("invalid maximum stripe components (%s): %s"),
1804*0Sstevel@tonic-gate 		attr, value);
1805*0Sstevel@tonic-gate 	    return (-1);
1806*0Sstevel@tonic-gate 	}
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 	/* Set in stripe */
1809*0Sstevel@tonic-gate 	return (devconfig_set_stripe_maxcomp(stripe, maxcomp));
1810*0Sstevel@tonic-gate }
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate /*
1813*0Sstevel@tonic-gate  * Validate and set the usehsp attribute in the given volume
1814*0Sstevel@tonic-gate  * devconfig_t.
1815*0Sstevel@tonic-gate  *
1816*0Sstevel@tonic-gate  * @param       volume
1817*0Sstevel@tonic-gate  *		the devconfig_t in which to set the usehsp
1818*0Sstevel@tonic-gate  *
1819*0Sstevel@tonic-gate  * @param       attr
1820*0Sstevel@tonic-gate  *		the name of the XML attribute
1821*0Sstevel@tonic-gate  *
1822*0Sstevel@tonic-gate  * @param       value
1823*0Sstevel@tonic-gate  *		the value of the XML attribute
1824*0Sstevel@tonic-gate  *
1825*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1826*0Sstevel@tonic-gate  */
1827*0Sstevel@tonic-gate static int
validate_set_volume_usehsp(devconfig_t * volume,char * attr,char * value)1828*0Sstevel@tonic-gate validate_set_volume_usehsp(
1829*0Sstevel@tonic-gate 	devconfig_t *volume,
1830*0Sstevel@tonic-gate 	char *attr,
1831*0Sstevel@tonic-gate 	char *value)
1832*0Sstevel@tonic-gate {
1833*0Sstevel@tonic-gate 	boolean_t usehsp;
1834*0Sstevel@tonic-gate 
1835*0Sstevel@tonic-gate 	/* Get boolean value */
1836*0Sstevel@tonic-gate 	if (strtobool(value, &usehsp) != 0) {
1837*0Sstevel@tonic-gate 	    volume_set_error(
1838*0Sstevel@tonic-gate 		gettext("%s: invalid boolean value for \"%s\" attribute"),
1839*0Sstevel@tonic-gate 		value, attr);
1840*0Sstevel@tonic-gate 	    return (-1);
1841*0Sstevel@tonic-gate 	}
1842*0Sstevel@tonic-gate 
1843*0Sstevel@tonic-gate 	/* Set in volume */
1844*0Sstevel@tonic-gate 	return (devconfig_set_volume_usehsp(volume, usehsp));
1845*0Sstevel@tonic-gate }
1846*0Sstevel@tonic-gate 
1847*0Sstevel@tonic-gate /*
1848*0Sstevel@tonic-gate  * Validate and set the nsubmirrors attribute in the given mirror
1849*0Sstevel@tonic-gate  * devconfig_t.
1850*0Sstevel@tonic-gate  *
1851*0Sstevel@tonic-gate  * @param       volume
1852*0Sstevel@tonic-gate  *		the devconfig_t in which to set the nsubmirrors
1853*0Sstevel@tonic-gate  *
1854*0Sstevel@tonic-gate  * @param       attr
1855*0Sstevel@tonic-gate  *		the name of the XML attribute
1856*0Sstevel@tonic-gate  *
1857*0Sstevel@tonic-gate  * @param       value
1858*0Sstevel@tonic-gate  *		the value of the XML attribute
1859*0Sstevel@tonic-gate  *
1860*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1861*0Sstevel@tonic-gate  */
1862*0Sstevel@tonic-gate static int
validate_set_mirror_nsubmirrors(devconfig_t * mirror,char * attr,char * value)1863*0Sstevel@tonic-gate validate_set_mirror_nsubmirrors(
1864*0Sstevel@tonic-gate 	devconfig_t *mirror,
1865*0Sstevel@tonic-gate 	char *attr,
1866*0Sstevel@tonic-gate 	char *value)
1867*0Sstevel@tonic-gate {
1868*0Sstevel@tonic-gate 	uint16_t nsubmirrors;
1869*0Sstevel@tonic-gate 
1870*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
1871*0Sstevel@tonic-gate 	if (str_to_uint16(value, &nsubmirrors) != 0) {
1872*0Sstevel@tonic-gate 	    volume_set_error(
1873*0Sstevel@tonic-gate 		gettext("invalid number of submirrors (%s): %s"),
1874*0Sstevel@tonic-gate 		attr, value);
1875*0Sstevel@tonic-gate 	    return (-1);
1876*0Sstevel@tonic-gate 	}
1877*0Sstevel@tonic-gate 
1878*0Sstevel@tonic-gate 	/* Set in stripe */
1879*0Sstevel@tonic-gate 	return (devconfig_set_mirror_nsubs(mirror, nsubmirrors));
1880*0Sstevel@tonic-gate }
1881*0Sstevel@tonic-gate 
1882*0Sstevel@tonic-gate /*
1883*0Sstevel@tonic-gate  * Validate and set the read attribute in the given mirror
1884*0Sstevel@tonic-gate  * devconfig_t.
1885*0Sstevel@tonic-gate  *
1886*0Sstevel@tonic-gate  * @param       volume
1887*0Sstevel@tonic-gate  *		the devconfig_t in which to set the read
1888*0Sstevel@tonic-gate  *
1889*0Sstevel@tonic-gate  * @param       attr
1890*0Sstevel@tonic-gate  *		the name of the XML attribute
1891*0Sstevel@tonic-gate  *
1892*0Sstevel@tonic-gate  * @param       value
1893*0Sstevel@tonic-gate  *		the value of the XML attribute
1894*0Sstevel@tonic-gate  *
1895*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1896*0Sstevel@tonic-gate  */
1897*0Sstevel@tonic-gate static int
validate_set_mirror_read(devconfig_t * mirror,char * attr,char * value)1898*0Sstevel@tonic-gate validate_set_mirror_read(
1899*0Sstevel@tonic-gate 	devconfig_t *mirror,
1900*0Sstevel@tonic-gate 	char *attr,
1901*0Sstevel@tonic-gate 	char *value)
1902*0Sstevel@tonic-gate {
1903*0Sstevel@tonic-gate 	mirror_read_strategy_t strategy;
1904*0Sstevel@tonic-gate 
1905*0Sstevel@tonic-gate 	if (strcmp(value, VALID_MIRROR_READ_ROUNDROBIN) == 0) {
1906*0Sstevel@tonic-gate 	    strategy = MIRROR_READ_ROUNDROBIN;
1907*0Sstevel@tonic-gate 	} else
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate 	if (strcmp(value, VALID_MIRROR_READ_GEOMETRIC) == 0) {
1910*0Sstevel@tonic-gate 	    strategy = MIRROR_READ_GEOMETRIC;
1911*0Sstevel@tonic-gate 	} else
1912*0Sstevel@tonic-gate 
1913*0Sstevel@tonic-gate 	if (strcmp(value, VALID_MIRROR_READ_FIRST) == 0) {
1914*0Sstevel@tonic-gate 	    strategy = MIRROR_READ_FIRST;
1915*0Sstevel@tonic-gate 	} else
1916*0Sstevel@tonic-gate 
1917*0Sstevel@tonic-gate 	{
1918*0Sstevel@tonic-gate 	    volume_set_error(gettext("%s: invalid mirror read value"), value);
1919*0Sstevel@tonic-gate 	    return (-1);
1920*0Sstevel@tonic-gate 	}
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	return (devconfig_set_mirror_read(mirror, strategy));
1923*0Sstevel@tonic-gate }
1924*0Sstevel@tonic-gate 
1925*0Sstevel@tonic-gate /*
1926*0Sstevel@tonic-gate  * Validate and set the write attribute in the given mirror
1927*0Sstevel@tonic-gate  * devconfig_t.
1928*0Sstevel@tonic-gate  *
1929*0Sstevel@tonic-gate  * @param       volume
1930*0Sstevel@tonic-gate  *		the devconfig_t in which to set the write
1931*0Sstevel@tonic-gate  *
1932*0Sstevel@tonic-gate  * @param       attr
1933*0Sstevel@tonic-gate  *		the name of the XML attribute
1934*0Sstevel@tonic-gate  *
1935*0Sstevel@tonic-gate  * @param       value
1936*0Sstevel@tonic-gate  *		the value of the XML attribute
1937*0Sstevel@tonic-gate  *
1938*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1939*0Sstevel@tonic-gate  */
1940*0Sstevel@tonic-gate static int
validate_set_mirror_write(devconfig_t * mirror,char * attr,char * value)1941*0Sstevel@tonic-gate validate_set_mirror_write(
1942*0Sstevel@tonic-gate 	devconfig_t *mirror,
1943*0Sstevel@tonic-gate 	char *attr,
1944*0Sstevel@tonic-gate 	char *value)
1945*0Sstevel@tonic-gate {
1946*0Sstevel@tonic-gate 	mirror_write_strategy_t strategy;
1947*0Sstevel@tonic-gate 
1948*0Sstevel@tonic-gate 	if (strcmp(value, VALID_MIRROR_WRITE_PARALLEL) == 0) {
1949*0Sstevel@tonic-gate 	    strategy = MIRROR_WRITE_PARALLEL;
1950*0Sstevel@tonic-gate 	} else
1951*0Sstevel@tonic-gate 
1952*0Sstevel@tonic-gate 	if (strcmp(value, VALID_MIRROR_WRITE_SERIAL) == 0) {
1953*0Sstevel@tonic-gate 	    strategy = MIRROR_WRITE_SERIAL;
1954*0Sstevel@tonic-gate 	} else
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate 	{
1957*0Sstevel@tonic-gate 	    volume_set_error(gettext("%s: invalid mirror write value"), value);
1958*0Sstevel@tonic-gate 	    return (-1);
1959*0Sstevel@tonic-gate 	}
1960*0Sstevel@tonic-gate 
1961*0Sstevel@tonic-gate 	return (devconfig_set_mirror_write(mirror, strategy));
1962*0Sstevel@tonic-gate }
1963*0Sstevel@tonic-gate 
1964*0Sstevel@tonic-gate /*
1965*0Sstevel@tonic-gate  * Validate and set the passnum attribute in the given mirror
1966*0Sstevel@tonic-gate  * devconfig_t.
1967*0Sstevel@tonic-gate  *
1968*0Sstevel@tonic-gate  * @param       volume
1969*0Sstevel@tonic-gate  *		the devconfig_t in which to set the passnum
1970*0Sstevel@tonic-gate  *
1971*0Sstevel@tonic-gate  * @param       attr
1972*0Sstevel@tonic-gate  *		the name of the XML attribute
1973*0Sstevel@tonic-gate  *
1974*0Sstevel@tonic-gate  * @param       value
1975*0Sstevel@tonic-gate  *		the value of the XML attribute
1976*0Sstevel@tonic-gate  *
1977*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
1978*0Sstevel@tonic-gate  */
1979*0Sstevel@tonic-gate static int
validate_set_mirror_passnum(devconfig_t * mirror,char * attr,char * value)1980*0Sstevel@tonic-gate validate_set_mirror_passnum(
1981*0Sstevel@tonic-gate 	devconfig_t *mirror,
1982*0Sstevel@tonic-gate 	char *attr,
1983*0Sstevel@tonic-gate 	char *value)
1984*0Sstevel@tonic-gate {
1985*0Sstevel@tonic-gate 	uint16_t passnum;
1986*0Sstevel@tonic-gate 
1987*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
1988*0Sstevel@tonic-gate 	if (str_to_uint16(value, &passnum) != 0) {
1989*0Sstevel@tonic-gate 	    volume_set_error(
1990*0Sstevel@tonic-gate 		gettext("invalid mirror pass number (%s): %s"),
1991*0Sstevel@tonic-gate 		attr, value);
1992*0Sstevel@tonic-gate 	    return (-1);
1993*0Sstevel@tonic-gate 	}
1994*0Sstevel@tonic-gate 
1995*0Sstevel@tonic-gate 	/* Set in stripe */
1996*0Sstevel@tonic-gate 	return (devconfig_set_mirror_pass(mirror, passnum));
1997*0Sstevel@tonic-gate }
1998*0Sstevel@tonic-gate 
1999*0Sstevel@tonic-gate /*
2000*0Sstevel@tonic-gate  * Validate and set the redundancy attribute in the given volume
2001*0Sstevel@tonic-gate  * devconfig_t.
2002*0Sstevel@tonic-gate  *
2003*0Sstevel@tonic-gate  * @param       volume
2004*0Sstevel@tonic-gate  *		the devconfig_t in which to set the redundancy
2005*0Sstevel@tonic-gate  *
2006*0Sstevel@tonic-gate  * @param       attr
2007*0Sstevel@tonic-gate  *		the name of the XML attribute
2008*0Sstevel@tonic-gate  *
2009*0Sstevel@tonic-gate  * @param       value
2010*0Sstevel@tonic-gate  *		the value of the XML attribute
2011*0Sstevel@tonic-gate  *
2012*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2013*0Sstevel@tonic-gate  */
2014*0Sstevel@tonic-gate static int
validate_set_volume_redundancy(devconfig_t * volume,char * attr,char * value)2015*0Sstevel@tonic-gate validate_set_volume_redundancy(
2016*0Sstevel@tonic-gate 	devconfig_t *volume,
2017*0Sstevel@tonic-gate 	char *attr,
2018*0Sstevel@tonic-gate 	char *value)
2019*0Sstevel@tonic-gate {
2020*0Sstevel@tonic-gate 	uint16_t redundancy;
2021*0Sstevel@tonic-gate 
2022*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
2023*0Sstevel@tonic-gate 	if (str_to_uint16(value, &redundancy) != 0) {
2024*0Sstevel@tonic-gate 	    volume_set_error(
2025*0Sstevel@tonic-gate 		gettext("invalid redundancy level (%s): %s"),
2026*0Sstevel@tonic-gate 		attr, value);
2027*0Sstevel@tonic-gate 	    return (-1);
2028*0Sstevel@tonic-gate 	}
2029*0Sstevel@tonic-gate 
2030*0Sstevel@tonic-gate 	/* Set in stripe */
2031*0Sstevel@tonic-gate 	return (devconfig_set_volume_redundancy_level(volume, redundancy));
2032*0Sstevel@tonic-gate }
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate /*
2035*0Sstevel@tonic-gate  * Validate and set the datapaths attribute in the given volume
2036*0Sstevel@tonic-gate  * devconfig_t.
2037*0Sstevel@tonic-gate  *
2038*0Sstevel@tonic-gate  * @param       volume
2039*0Sstevel@tonic-gate  *		the devconfig_t in which to set the datapaths
2040*0Sstevel@tonic-gate  *
2041*0Sstevel@tonic-gate  * @param       attr
2042*0Sstevel@tonic-gate  *		the name of the XML attribute
2043*0Sstevel@tonic-gate  *
2044*0Sstevel@tonic-gate  * @param       value
2045*0Sstevel@tonic-gate  *		the value of the XML attribute
2046*0Sstevel@tonic-gate  *
2047*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2048*0Sstevel@tonic-gate  */
2049*0Sstevel@tonic-gate static int
validate_set_volume_datapaths(devconfig_t * volume,char * attr,char * value)2050*0Sstevel@tonic-gate validate_set_volume_datapaths(
2051*0Sstevel@tonic-gate 	devconfig_t *volume,
2052*0Sstevel@tonic-gate 	char *attr,
2053*0Sstevel@tonic-gate 	char *value)
2054*0Sstevel@tonic-gate {
2055*0Sstevel@tonic-gate 	uint16_t redundancy;
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 	/* Convert string to a uint16_t */
2058*0Sstevel@tonic-gate 	if (str_to_uint16(value, &redundancy) != 0) {
2059*0Sstevel@tonic-gate 	    volume_set_error(
2060*0Sstevel@tonic-gate 		gettext("invalid number of data paths (%s): %s"),
2061*0Sstevel@tonic-gate 		attr, value);
2062*0Sstevel@tonic-gate 	    return (-1);
2063*0Sstevel@tonic-gate 	}
2064*0Sstevel@tonic-gate 
2065*0Sstevel@tonic-gate 	/* Set in stripe */
2066*0Sstevel@tonic-gate 	return (devconfig_set_volume_npaths(volume, redundancy));
2067*0Sstevel@tonic-gate }
2068*0Sstevel@tonic-gate 
2069*0Sstevel@tonic-gate /*
2070*0Sstevel@tonic-gate  * ******************************************************************
2071*0Sstevel@tonic-gate  *
2072*0Sstevel@tonic-gate  * XML attribute accessors/converters
2073*0Sstevel@tonic-gate  *
2074*0Sstevel@tonic-gate  * These functions get a value from the appropriate devconfig_t
2075*0Sstevel@tonic-gate  * accessor, and then convert it to a string.
2076*0Sstevel@tonic-gate  *
2077*0Sstevel@tonic-gate  * ******************************************************************
2078*0Sstevel@tonic-gate  */
2079*0Sstevel@tonic-gate 
2080*0Sstevel@tonic-gate /*
2081*0Sstevel@tonic-gate  * Get, as a string, the value of the name attribute of the given
2082*0Sstevel@tonic-gate  * devconfig_t.  This data must be freed.
2083*0Sstevel@tonic-gate  *
2084*0Sstevel@tonic-gate  * @param       device
2085*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the name
2086*0Sstevel@tonic-gate  *
2087*0Sstevel@tonic-gate  * @param       attr
2088*0Sstevel@tonic-gate  *		the name of the XML attribute
2089*0Sstevel@tonic-gate  *
2090*0Sstevel@tonic-gate  * @param       value
2091*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2092*0Sstevel@tonic-gate  *
2093*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2094*0Sstevel@tonic-gate  */
2095*0Sstevel@tonic-gate static int
get_as_string_name(devconfig_t * device,char * attr,char ** value)2096*0Sstevel@tonic-gate get_as_string_name(
2097*0Sstevel@tonic-gate 	devconfig_t *device,
2098*0Sstevel@tonic-gate 	char *attr,
2099*0Sstevel@tonic-gate 	char **value)
2100*0Sstevel@tonic-gate {
2101*0Sstevel@tonic-gate 	int error;
2102*0Sstevel@tonic-gate 	char *name;
2103*0Sstevel@tonic-gate 
2104*0Sstevel@tonic-gate 	/* Get name */
2105*0Sstevel@tonic-gate 	if ((error = devconfig_get_name(device, &name)) == 0) {
2106*0Sstevel@tonic-gate 	    if ((*value = strdup(name)) == NULL) {
2107*0Sstevel@tonic-gate 		error = ENOMEM;
2108*0Sstevel@tonic-gate 	    }
2109*0Sstevel@tonic-gate 	}
2110*0Sstevel@tonic-gate 
2111*0Sstevel@tonic-gate 	return (error);
2112*0Sstevel@tonic-gate }
2113*0Sstevel@tonic-gate 
2114*0Sstevel@tonic-gate /*
2115*0Sstevel@tonic-gate  * Get, as a string, the value of the passnum attribute of the given
2116*0Sstevel@tonic-gate  * mirror devconfig_t.  This data must be freed.
2117*0Sstevel@tonic-gate  *
2118*0Sstevel@tonic-gate  * @param       device
2119*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the passnum
2120*0Sstevel@tonic-gate  *
2121*0Sstevel@tonic-gate  * @param       attr
2122*0Sstevel@tonic-gate  *		the name of the XML attribute
2123*0Sstevel@tonic-gate  *
2124*0Sstevel@tonic-gate  * @param       value
2125*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2126*0Sstevel@tonic-gate  *
2127*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2128*0Sstevel@tonic-gate  */
2129*0Sstevel@tonic-gate static int
get_as_string_mirror_passnum(devconfig_t * mirror,char * attr,char ** value)2130*0Sstevel@tonic-gate get_as_string_mirror_passnum(
2131*0Sstevel@tonic-gate 	devconfig_t *mirror,
2132*0Sstevel@tonic-gate 	char *attr,
2133*0Sstevel@tonic-gate 	char **value)
2134*0Sstevel@tonic-gate {
2135*0Sstevel@tonic-gate 	int error;
2136*0Sstevel@tonic-gate 	uint16_t passnum;
2137*0Sstevel@tonic-gate 
2138*0Sstevel@tonic-gate 	/* Get mirror pass number */
2139*0Sstevel@tonic-gate 	if ((error = devconfig_get_mirror_pass(mirror, &passnum)) == 0) {
2140*0Sstevel@tonic-gate 	    error = ll_to_str(passnum, value);
2141*0Sstevel@tonic-gate 	}
2142*0Sstevel@tonic-gate 
2143*0Sstevel@tonic-gate 	return (error);
2144*0Sstevel@tonic-gate }
2145*0Sstevel@tonic-gate 
2146*0Sstevel@tonic-gate /*
2147*0Sstevel@tonic-gate  * Get, as a string, the value of the read attribute of the given
2148*0Sstevel@tonic-gate  * mirror devconfig_t.  This data must be freed.
2149*0Sstevel@tonic-gate  *
2150*0Sstevel@tonic-gate  * @param       device
2151*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the read
2152*0Sstevel@tonic-gate  *
2153*0Sstevel@tonic-gate  * @param       attr
2154*0Sstevel@tonic-gate  *		the name of the XML attribute
2155*0Sstevel@tonic-gate  *
2156*0Sstevel@tonic-gate  * @param       value
2157*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2158*0Sstevel@tonic-gate  *
2159*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2160*0Sstevel@tonic-gate  */
2161*0Sstevel@tonic-gate static int
get_as_string_mirror_read(devconfig_t * mirror,char * attr,char ** value)2162*0Sstevel@tonic-gate get_as_string_mirror_read(
2163*0Sstevel@tonic-gate 	devconfig_t *mirror,
2164*0Sstevel@tonic-gate 	char *attr,
2165*0Sstevel@tonic-gate 	char **value)
2166*0Sstevel@tonic-gate {
2167*0Sstevel@tonic-gate 	int error;
2168*0Sstevel@tonic-gate 	mirror_read_strategy_t read;
2169*0Sstevel@tonic-gate 
2170*0Sstevel@tonic-gate 	/* Get mirror read strategy */
2171*0Sstevel@tonic-gate 	if ((error = devconfig_get_mirror_read(mirror, &read)) == 0) {
2172*0Sstevel@tonic-gate 	    if ((*value = strdup(
2173*0Sstevel@tonic-gate 		devconfig_read_strategy_to_str(read))) == NULL) {
2174*0Sstevel@tonic-gate 		error = ENOMEM;
2175*0Sstevel@tonic-gate 	    }
2176*0Sstevel@tonic-gate 	}
2177*0Sstevel@tonic-gate 
2178*0Sstevel@tonic-gate 	return (error);
2179*0Sstevel@tonic-gate }
2180*0Sstevel@tonic-gate 
2181*0Sstevel@tonic-gate /*
2182*0Sstevel@tonic-gate  * Get, as a string, the value of the write attribute of the given
2183*0Sstevel@tonic-gate  * mirror devconfig_t.  This data must be freed.
2184*0Sstevel@tonic-gate  *
2185*0Sstevel@tonic-gate  * @param       device
2186*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the write
2187*0Sstevel@tonic-gate  *
2188*0Sstevel@tonic-gate  * @param       attr
2189*0Sstevel@tonic-gate  *		the name of the XML attribute
2190*0Sstevel@tonic-gate  *
2191*0Sstevel@tonic-gate  * @param       value
2192*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2193*0Sstevel@tonic-gate  *
2194*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2195*0Sstevel@tonic-gate  */
2196*0Sstevel@tonic-gate static int
get_as_string_mirror_write(devconfig_t * mirror,char * attr,char ** value)2197*0Sstevel@tonic-gate get_as_string_mirror_write(
2198*0Sstevel@tonic-gate 	devconfig_t *mirror,
2199*0Sstevel@tonic-gate 	char *attr,
2200*0Sstevel@tonic-gate 	char **value)
2201*0Sstevel@tonic-gate {
2202*0Sstevel@tonic-gate 	int error;
2203*0Sstevel@tonic-gate 	mirror_write_strategy_t write;
2204*0Sstevel@tonic-gate 
2205*0Sstevel@tonic-gate 	/* Get mirror write strategy */
2206*0Sstevel@tonic-gate 	if ((error = devconfig_get_mirror_write(mirror, &write)) == 0) {
2207*0Sstevel@tonic-gate 	    if ((*value = strdup(
2208*0Sstevel@tonic-gate 		devconfig_write_strategy_to_str(write))) == NULL) {
2209*0Sstevel@tonic-gate 		error = ENOMEM;
2210*0Sstevel@tonic-gate 	    }
2211*0Sstevel@tonic-gate 	}
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate 	return (error);
2214*0Sstevel@tonic-gate }
2215*0Sstevel@tonic-gate 
2216*0Sstevel@tonic-gate /*
2217*0Sstevel@tonic-gate  * Get, as a string, the value of the in_blocks attribute of the given
2218*0Sstevel@tonic-gate  * device devconfig_t.  This data must be freed.
2219*0Sstevel@tonic-gate  *
2220*0Sstevel@tonic-gate  * @param       device
2221*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the in_blocks
2222*0Sstevel@tonic-gate  *
2223*0Sstevel@tonic-gate  * @param       attr
2224*0Sstevel@tonic-gate  *		the name of the XML attribute
2225*0Sstevel@tonic-gate  *
2226*0Sstevel@tonic-gate  * @param       value
2227*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2228*0Sstevel@tonic-gate  *
2229*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2230*0Sstevel@tonic-gate  */
2231*0Sstevel@tonic-gate static int
get_as_string_size_in_blocks(devconfig_t * device,char * attr,char ** value)2232*0Sstevel@tonic-gate get_as_string_size_in_blocks(
2233*0Sstevel@tonic-gate 	devconfig_t *device,
2234*0Sstevel@tonic-gate 	char *attr,
2235*0Sstevel@tonic-gate 	char **value)
2236*0Sstevel@tonic-gate {
2237*0Sstevel@tonic-gate 	int error;
2238*0Sstevel@tonic-gate 	uint64_t size;
2239*0Sstevel@tonic-gate 
2240*0Sstevel@tonic-gate 	/* Get size in blocks */
2241*0Sstevel@tonic-gate 	if ((error = devconfig_get_size_in_blocks(device, &size)) == 0) {
2242*0Sstevel@tonic-gate 	    error = ll_to_str(size, value);
2243*0Sstevel@tonic-gate 	}
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate 	return (error);
2246*0Sstevel@tonic-gate }
2247*0Sstevel@tonic-gate 
2248*0Sstevel@tonic-gate /*
2249*0Sstevel@tonic-gate  * Get, as a string, the value of the start_block attribute of the
2250*0Sstevel@tonic-gate  * given slice devconfig_t.  This data must be freed.
2251*0Sstevel@tonic-gate  *
2252*0Sstevel@tonic-gate  * @param       device
2253*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the start_block
2254*0Sstevel@tonic-gate  *
2255*0Sstevel@tonic-gate  * @param       attr
2256*0Sstevel@tonic-gate  *		the name of the XML attribute
2257*0Sstevel@tonic-gate  *
2258*0Sstevel@tonic-gate  * @param       value
2259*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2260*0Sstevel@tonic-gate  *
2261*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2262*0Sstevel@tonic-gate  */
2263*0Sstevel@tonic-gate static int
get_as_string_slice_start_block(devconfig_t * slice,char * attr,char ** value)2264*0Sstevel@tonic-gate get_as_string_slice_start_block(
2265*0Sstevel@tonic-gate 	devconfig_t *slice,
2266*0Sstevel@tonic-gate 	char *attr,
2267*0Sstevel@tonic-gate 	char **value)
2268*0Sstevel@tonic-gate {
2269*0Sstevel@tonic-gate 	int error;
2270*0Sstevel@tonic-gate 	uint64_t start;
2271*0Sstevel@tonic-gate 
2272*0Sstevel@tonic-gate 	/* Get slice start block */
2273*0Sstevel@tonic-gate 	if ((error = devconfig_get_slice_start_block(slice, &start)) == 0) {
2274*0Sstevel@tonic-gate 	    error = ll_to_str(start, value);
2275*0Sstevel@tonic-gate 	}
2276*0Sstevel@tonic-gate 
2277*0Sstevel@tonic-gate 	return (error);
2278*0Sstevel@tonic-gate }
2279*0Sstevel@tonic-gate 
2280*0Sstevel@tonic-gate /*
2281*0Sstevel@tonic-gate  * Get, as a string, the value of the interlace attribute of the given
2282*0Sstevel@tonic-gate  * stripe devconfig_t.  This data must be freed.
2283*0Sstevel@tonic-gate  *
2284*0Sstevel@tonic-gate  * @param       device
2285*0Sstevel@tonic-gate  *		the devconfig_t from which to retrieve the interlace
2286*0Sstevel@tonic-gate  *
2287*0Sstevel@tonic-gate  * @param       attr
2288*0Sstevel@tonic-gate  *		the name of the XML attribute
2289*0Sstevel@tonic-gate  *
2290*0Sstevel@tonic-gate  * @param       value
2291*0Sstevel@tonic-gate  *		RETURN: the value of the XML attribute
2292*0Sstevel@tonic-gate  *
2293*0Sstevel@tonic-gate  * @return      0 on success, non-zero otherwise.
2294*0Sstevel@tonic-gate  */
2295*0Sstevel@tonic-gate static int
get_as_string_stripe_interlace(devconfig_t * stripe,char * attr,char ** value)2296*0Sstevel@tonic-gate get_as_string_stripe_interlace(
2297*0Sstevel@tonic-gate 	devconfig_t *stripe,
2298*0Sstevel@tonic-gate 	char *attr,
2299*0Sstevel@tonic-gate 	char **value)
2300*0Sstevel@tonic-gate {
2301*0Sstevel@tonic-gate 	int error;
2302*0Sstevel@tonic-gate 	uint64_t interlace;
2303*0Sstevel@tonic-gate 
2304*0Sstevel@tonic-gate 	/* Get interlace */
2305*0Sstevel@tonic-gate 	if ((error = devconfig_get_stripe_interlace(
2306*0Sstevel@tonic-gate 		stripe, &interlace)) == 0) {
2307*0Sstevel@tonic-gate 	    error = bytes_to_sizestr(interlace, value, interlace_units, B_TRUE);
2308*0Sstevel@tonic-gate 	}
2309*0Sstevel@tonic-gate 
2310*0Sstevel@tonic-gate 	return (error);
2311*0Sstevel@tonic-gate }
2312