1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5789Sahrens  * Common Development and Distribution License, Version 1.0 only
6789Sahrens  * (the "License").  You may not use this file except in compliance
7789Sahrens  * with the License.
8789Sahrens  *
9789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10789Sahrens  * or http://www.opensolaris.org/os/licensing.
11789Sahrens  * See the License for the specific language governing permissions
12789Sahrens  * and limitations under the License.
13789Sahrens  *
14789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19789Sahrens  *
20789Sahrens  * CDDL HEADER END
21789Sahrens  */
22789Sahrens /*
23789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens #include "availdevs.h"
30789Sahrens #include <libzfs_jni_diskmgt.h>
31894Stalley #include <libzfs_jni_ipool.h>
32789Sahrens #include <libxml/parser.h>
33789Sahrens 
34789Sahrens /*
35789Sahrens  * Function prototypes
36789Sahrens  */
37789Sahrens 
38789Sahrens static void handle_error(const char *, va_list);
39789Sahrens static int add_disk_to_xml(dmgt_disk_t *, void *);
40894Stalley static int add_pool_to_xml(char *, uint64_t, uint64_t, char *, void *);
41789Sahrens static xmlDocPtr create_doc();
42789Sahrens int main();
43789Sahrens 
44789Sahrens /*
45789Sahrens  * Static functions
46789Sahrens  */
47789Sahrens 
48789Sahrens static void
49789Sahrens handle_error(const char *fmt, va_list ap)
50789Sahrens {
51789Sahrens 	(void) vfprintf(stderr, fmt, ap);
52789Sahrens 	(void) fprintf(stderr, "\n");
53789Sahrens }
54789Sahrens 
55789Sahrens static int
56789Sahrens add_disk_to_xml(dmgt_disk_t *dp, void *data)
57789Sahrens {
58894Stalley 	int i;
59789Sahrens 	char tmp[64];
60789Sahrens 	xmlNodePtr available = *((xmlNodePtr *)data);
61789Sahrens 
62789Sahrens 	xmlNodePtr disk = xmlNewChild(
63789Sahrens 	    available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
64789Sahrens 	xmlSetProp(disk,
65789Sahrens 	    (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
66952Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", dp->size);
67789Sahrens 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_SIZE, (xmlChar *)tmp);
68789Sahrens 
69*1066Stalley 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *)
70*1066Stalley 	    (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE));
71*1066Stalley 
72789Sahrens 	if (dp->aliases != NULL) {
73789Sahrens 		for (i = 0; dp->aliases[i] != NULL; i++) {
74789Sahrens 			xmlNodePtr alias = xmlNewChild(
75789Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
76789Sahrens 			xmlSetProp(alias,
77789Sahrens 			    (xmlChar *)ATTR_ALIAS_NAME,
78789Sahrens 			    (xmlChar *)dp->aliases[i]);
79789Sahrens 		}
80789Sahrens 	}
81789Sahrens 
82789Sahrens 	if (dp->slices != NULL) {
83789Sahrens 		for (i = 0; dp->slices[i] != NULL; i++) {
84789Sahrens 			dmgt_slice_t *sp = dp->slices[i];
85789Sahrens 			xmlNodePtr slice = xmlNewChild(
86789Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
87789Sahrens 			xmlSetProp(slice,
88789Sahrens 			    (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
89789Sahrens 
90952Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->size);
91789Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_SIZE,
92789Sahrens 			    (xmlChar *)tmp);
93789Sahrens 
94952Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->start);
95789Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_START,
96789Sahrens 			    (xmlChar *)tmp);
97789Sahrens 
98789Sahrens 			if (sp->used_name != NULL) {
99789Sahrens 				xmlSetProp(slice,
100789Sahrens 				    (xmlChar *)ATTR_SLICE_USED_NAME,
101789Sahrens 				    (xmlChar *)sp->used_name);
102789Sahrens 			}
103789Sahrens 
104789Sahrens 			if (sp->used_by != NULL) {
105789Sahrens 				xmlSetProp(slice, (xmlChar *)ATTR_SLICE_USED_BY,
106789Sahrens 				    (xmlChar *)sp->used_by);
107789Sahrens 			}
108789Sahrens 		}
109789Sahrens 	}
110789Sahrens 
111789Sahrens 	return (0);
112789Sahrens }
113789Sahrens 
114894Stalley static int
115894Stalley add_pool_to_xml(char *name, uint64_t guid,
116894Stalley     uint64_t pool_state, char *health, void *data)
117894Stalley {
118894Stalley 	char *state;
119894Stalley 	char tmp[64];
120894Stalley 	xmlNodePtr importable = *((xmlNodePtr *)data);
121894Stalley 
122894Stalley 	xmlNodePtr pool = xmlNewChild(
123894Stalley 	    importable, NULL, (xmlChar *)ELEMENT_POOL, NULL);
124894Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name);
125894Stalley 
126894Stalley 	state = zjni_get_state_str(pool_state);
127894Stalley 	if (state == NULL) {
128894Stalley 		state = "";
129894Stalley 	}
130894Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE, (xmlChar *)state);
131894Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_HEALTH, (xmlChar *)health);
132894Stalley 
133952Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", guid);
134894Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_ID, (xmlChar *)tmp);
135894Stalley 
136894Stalley 	return (0);
137894Stalley }
138894Stalley 
139789Sahrens static xmlDocPtr
140789Sahrens create_doc(void)
141789Sahrens {
142789Sahrens 	/* Create the XML document */
143789Sahrens 	xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
144789Sahrens 
145789Sahrens 	/* Create the root node */
146789Sahrens 	xmlNodePtr root = xmlNewDocNode(
147789Sahrens 	    doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
148789Sahrens 	xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
149789Sahrens 
150789Sahrens 	return (doc);
151789Sahrens }
152789Sahrens 
153789Sahrens /*
154789Sahrens  * Main entry to availdisks.
155789Sahrens  *
156789Sahrens  * @return      0 on successful exit, non-zero otherwise
157789Sahrens  */
158789Sahrens int
159894Stalley main(int argc, char **argv)
160789Sahrens {
161894Stalley 	int error = 0;
162894Stalley 	int get_pools = 0;
163894Stalley 	int get_devices = 0;
164789Sahrens 
165894Stalley 	/* Examine first arg */
166894Stalley 	int c = getopt(argc, argv, CLI_OPTSTRING);
167894Stalley 	switch (c) {
168894Stalley 		case CLI_ARG_ALL:
169894Stalley 			get_devices = 1;
170894Stalley 			get_pools = 1;
171894Stalley 			break;
172789Sahrens 
173894Stalley 		case CLI_ARG_DEVICES:
174894Stalley 			get_devices = 1;
175894Stalley 			break;
176789Sahrens 
177894Stalley 		case CLI_ARG_POOLS:
178894Stalley 			get_pools = 1;
179894Stalley 			break;
180894Stalley 
181894Stalley 		default:
182894Stalley 			return (1);
183894Stalley 			break;
184789Sahrens 	}
185789Sahrens 
186894Stalley 	argc -= optind;
187894Stalley 	argv += optind;
188894Stalley 
189894Stalley 	if (get_pools || get_devices) {
190894Stalley 		xmlDocPtr doc = create_doc();
191894Stalley 		xmlNodePtr root = xmlDocGetRootElement(doc);
192894Stalley 
193894Stalley 		if (get_devices) {
194894Stalley 			/* Create the available node */
195894Stalley 			xmlNodePtr available = xmlNewChild(root, NULL,
196894Stalley 			    (xmlChar *)ELEMENT_AVAILABLE, NULL);
197894Stalley 
198894Stalley 			/* libzfs_jni_diskmgt.o error handler */
199894Stalley 			dmgt_set_error_handler(handle_error);
200894Stalley 
201894Stalley 			error = dmgt_avail_disk_iter(
202894Stalley 			    add_disk_to_xml, &available);
203894Stalley 		}
204894Stalley 
205894Stalley 		if (get_pools && !error) {
206894Stalley 			/* Create the importable node */
207894Stalley 			xmlNodePtr importable = xmlNewChild(root, NULL,
208894Stalley 			    (xmlChar *)ELEMENT_IMPORTABLE, NULL);
209894Stalley 
210894Stalley 			error = zjni_ipool_iter(
211894Stalley 			    argc, argv, add_pool_to_xml, &importable);
212894Stalley 		}
213894Stalley 
214894Stalley 		if (!error) {
215894Stalley 			/* Print out XML */
216894Stalley 			xmlDocFormatDump(stdout, doc, 1);
217894Stalley 		}
218894Stalley 
219894Stalley 		xmlFreeDoc(doc);
220894Stalley 	}
221789Sahrens 
222789Sahrens 	return (error != 0);
223789Sahrens }
224