xref: /titanic_54/usr/src/cmd/availdevs/availdevs.c (revision dc307942eac821bc70a7a69cbb1ddec4184607c1)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5fa9e4066Sahrens  * Common Development and Distribution License, Version 1.0 only
6fa9e4066Sahrens  * (the "License").  You may not use this file except in compliance
7fa9e4066Sahrens  * with the License.
8fa9e4066Sahrens  *
9fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
11fa9e4066Sahrens  * See the License for the specific language governing permissions
12fa9e4066Sahrens  * and limitations under the License.
13fa9e4066Sahrens  *
14fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19fa9e4066Sahrens  *
20fa9e4066Sahrens  * CDDL HEADER END
21fa9e4066Sahrens  */
22fa9e4066Sahrens /*
23fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens #include "availdevs.h"
30fa9e4066Sahrens #include <libzfs_jni_diskmgt.h>
31c8e9ed14Stalley #include <libzfs_jni_ipool.h>
32fa9e4066Sahrens #include <libxml/parser.h>
33fa9e4066Sahrens 
34fa9e4066Sahrens /*
35fa9e4066Sahrens  * Function prototypes
36fa9e4066Sahrens  */
37fa9e4066Sahrens 
38fa9e4066Sahrens static void handle_error(const char *, va_list);
39fa9e4066Sahrens static int add_disk_to_xml(dmgt_disk_t *, void *);
40c8e9ed14Stalley static int add_pool_to_xml(char *, uint64_t, uint64_t, char *, void *);
41fa9e4066Sahrens static xmlDocPtr create_doc();
42fa9e4066Sahrens int main();
43fa9e4066Sahrens 
44fa9e4066Sahrens /*
45fa9e4066Sahrens  * Static functions
46fa9e4066Sahrens  */
47fa9e4066Sahrens 
48fa9e4066Sahrens static void
49fa9e4066Sahrens handle_error(const char *fmt, va_list ap)
50fa9e4066Sahrens {
51fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
52fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
53fa9e4066Sahrens }
54fa9e4066Sahrens 
55fa9e4066Sahrens static int
56fa9e4066Sahrens add_disk_to_xml(dmgt_disk_t *dp, void *data)
57fa9e4066Sahrens {
58c8e9ed14Stalley 	int i;
59fa9e4066Sahrens 	char tmp[64];
60fa9e4066Sahrens 	xmlNodePtr available = *((xmlNodePtr *)data);
61fa9e4066Sahrens 
62fa9e4066Sahrens 	xmlNodePtr disk = xmlNewChild(
63fa9e4066Sahrens 	    available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
64fa9e4066Sahrens 	xmlSetProp(disk,
65fa9e4066Sahrens 	    (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
66088e9d47Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", dp->size);
67fa9e4066Sahrens 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_SIZE, (xmlChar *)tmp);
68fa9e4066Sahrens 
69*dc307942Stalley 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *)
70*dc307942Stalley 	    (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE));
71*dc307942Stalley 
72fa9e4066Sahrens 	if (dp->aliases != NULL) {
73fa9e4066Sahrens 		for (i = 0; dp->aliases[i] != NULL; i++) {
74fa9e4066Sahrens 			xmlNodePtr alias = xmlNewChild(
75fa9e4066Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
76fa9e4066Sahrens 			xmlSetProp(alias,
77fa9e4066Sahrens 			    (xmlChar *)ATTR_ALIAS_NAME,
78fa9e4066Sahrens 			    (xmlChar *)dp->aliases[i]);
79fa9e4066Sahrens 		}
80fa9e4066Sahrens 	}
81fa9e4066Sahrens 
82fa9e4066Sahrens 	if (dp->slices != NULL) {
83fa9e4066Sahrens 		for (i = 0; dp->slices[i] != NULL; i++) {
84fa9e4066Sahrens 			dmgt_slice_t *sp = dp->slices[i];
85fa9e4066Sahrens 			xmlNodePtr slice = xmlNewChild(
86fa9e4066Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
87fa9e4066Sahrens 			xmlSetProp(slice,
88fa9e4066Sahrens 			    (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
89fa9e4066Sahrens 
90088e9d47Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->size);
91fa9e4066Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_SIZE,
92fa9e4066Sahrens 			    (xmlChar *)tmp);
93fa9e4066Sahrens 
94088e9d47Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->start);
95fa9e4066Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_START,
96fa9e4066Sahrens 			    (xmlChar *)tmp);
97fa9e4066Sahrens 
98fa9e4066Sahrens 			if (sp->used_name != NULL) {
99fa9e4066Sahrens 				xmlSetProp(slice,
100fa9e4066Sahrens 				    (xmlChar *)ATTR_SLICE_USED_NAME,
101fa9e4066Sahrens 				    (xmlChar *)sp->used_name);
102fa9e4066Sahrens 			}
103fa9e4066Sahrens 
104fa9e4066Sahrens 			if (sp->used_by != NULL) {
105fa9e4066Sahrens 				xmlSetProp(slice, (xmlChar *)ATTR_SLICE_USED_BY,
106fa9e4066Sahrens 				    (xmlChar *)sp->used_by);
107fa9e4066Sahrens 			}
108fa9e4066Sahrens 		}
109fa9e4066Sahrens 	}
110fa9e4066Sahrens 
111fa9e4066Sahrens 	return (0);
112fa9e4066Sahrens }
113fa9e4066Sahrens 
114c8e9ed14Stalley static int
115c8e9ed14Stalley add_pool_to_xml(char *name, uint64_t guid,
116c8e9ed14Stalley     uint64_t pool_state, char *health, void *data)
117c8e9ed14Stalley {
118c8e9ed14Stalley 	char *state;
119c8e9ed14Stalley 	char tmp[64];
120c8e9ed14Stalley 	xmlNodePtr importable = *((xmlNodePtr *)data);
121c8e9ed14Stalley 
122c8e9ed14Stalley 	xmlNodePtr pool = xmlNewChild(
123c8e9ed14Stalley 	    importable, NULL, (xmlChar *)ELEMENT_POOL, NULL);
124c8e9ed14Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name);
125c8e9ed14Stalley 
126c8e9ed14Stalley 	state = zjni_get_state_str(pool_state);
127c8e9ed14Stalley 	if (state == NULL) {
128c8e9ed14Stalley 		state = "";
129c8e9ed14Stalley 	}
130c8e9ed14Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE, (xmlChar *)state);
131c8e9ed14Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_HEALTH, (xmlChar *)health);
132c8e9ed14Stalley 
133088e9d47Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", guid);
134c8e9ed14Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_ID, (xmlChar *)tmp);
135c8e9ed14Stalley 
136c8e9ed14Stalley 	return (0);
137c8e9ed14Stalley }
138c8e9ed14Stalley 
139fa9e4066Sahrens static xmlDocPtr
140fa9e4066Sahrens create_doc(void)
141fa9e4066Sahrens {
142fa9e4066Sahrens 	/* Create the XML document */
143fa9e4066Sahrens 	xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
144fa9e4066Sahrens 
145fa9e4066Sahrens 	/* Create the root node */
146fa9e4066Sahrens 	xmlNodePtr root = xmlNewDocNode(
147fa9e4066Sahrens 	    doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
148fa9e4066Sahrens 	xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
149fa9e4066Sahrens 
150fa9e4066Sahrens 	return (doc);
151fa9e4066Sahrens }
152fa9e4066Sahrens 
153fa9e4066Sahrens /*
154fa9e4066Sahrens  * Main entry to availdisks.
155fa9e4066Sahrens  *
156fa9e4066Sahrens  * @return      0 on successful exit, non-zero otherwise
157fa9e4066Sahrens  */
158fa9e4066Sahrens int
159c8e9ed14Stalley main(int argc, char **argv)
160fa9e4066Sahrens {
161c8e9ed14Stalley 	int error = 0;
162c8e9ed14Stalley 	int get_pools = 0;
163c8e9ed14Stalley 	int get_devices = 0;
164fa9e4066Sahrens 
165c8e9ed14Stalley 	/* Examine first arg */
166c8e9ed14Stalley 	int c = getopt(argc, argv, CLI_OPTSTRING);
167c8e9ed14Stalley 	switch (c) {
168c8e9ed14Stalley 		case CLI_ARG_ALL:
169c8e9ed14Stalley 			get_devices = 1;
170c8e9ed14Stalley 			get_pools = 1;
171c8e9ed14Stalley 			break;
172c8e9ed14Stalley 
173c8e9ed14Stalley 		case CLI_ARG_DEVICES:
174c8e9ed14Stalley 			get_devices = 1;
175c8e9ed14Stalley 			break;
176c8e9ed14Stalley 
177c8e9ed14Stalley 		case CLI_ARG_POOLS:
178c8e9ed14Stalley 			get_pools = 1;
179c8e9ed14Stalley 			break;
180c8e9ed14Stalley 
181c8e9ed14Stalley 		default:
182c8e9ed14Stalley 			return (1);
183c8e9ed14Stalley 			break;
184c8e9ed14Stalley 	}
185c8e9ed14Stalley 
186c8e9ed14Stalley 	argc -= optind;
187c8e9ed14Stalley 	argv += optind;
188c8e9ed14Stalley 
189c8e9ed14Stalley 	if (get_pools || get_devices) {
190c8e9ed14Stalley 		xmlDocPtr doc = create_doc();
191c8e9ed14Stalley 		xmlNodePtr root = xmlDocGetRootElement(doc);
192c8e9ed14Stalley 
193c8e9ed14Stalley 		if (get_devices) {
194c8e9ed14Stalley 			/* Create the available node */
195c8e9ed14Stalley 			xmlNodePtr available = xmlNewChild(root, NULL,
196c8e9ed14Stalley 			    (xmlChar *)ELEMENT_AVAILABLE, NULL);
197c8e9ed14Stalley 
198c8e9ed14Stalley 			/* libzfs_jni_diskmgt.o error handler */
199fa9e4066Sahrens 			dmgt_set_error_handler(handle_error);
200fa9e4066Sahrens 
201c8e9ed14Stalley 			error = dmgt_avail_disk_iter(
202c8e9ed14Stalley 			    add_disk_to_xml, &available);
203c8e9ed14Stalley 		}
204fa9e4066Sahrens 
205c8e9ed14Stalley 		if (get_pools && !error) {
206c8e9ed14Stalley 			/* Create the importable node */
207c8e9ed14Stalley 			xmlNodePtr importable = xmlNewChild(root, NULL,
208c8e9ed14Stalley 			    (xmlChar *)ELEMENT_IMPORTABLE, NULL);
209c8e9ed14Stalley 
210c8e9ed14Stalley 			error = zjni_ipool_iter(
211c8e9ed14Stalley 			    argc, argv, add_pool_to_xml, &importable);
212c8e9ed14Stalley 		}
213c8e9ed14Stalley 
214fa9e4066Sahrens 		if (!error) {
215fa9e4066Sahrens 			/* Print out XML */
216fa9e4066Sahrens 			xmlDocFormatDump(stdout, doc, 1);
217fa9e4066Sahrens 		}
218fa9e4066Sahrens 
219fa9e4066Sahrens 		xmlFreeDoc(doc);
220c8e9ed14Stalley 	}
221fa9e4066Sahrens 
222fa9e4066Sahrens 	return (error != 0);
223fa9e4066Sahrens }
224