xref: /titanic_50/usr/src/cmd/availdevs/availdevs.c (revision fbfd10ff571cfd0139aa5127460f1b8a53dac971)
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 /*
23*fbfd10ffStalley  * Copyright 2006 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"
30*fbfd10ffStalley #include <libzfs.h>
31fa9e4066Sahrens #include <libzfs_jni_diskmgt.h>
32c8e9ed14Stalley #include <libzfs_jni_ipool.h>
33fa9e4066Sahrens #include <libxml/parser.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens /*
36fa9e4066Sahrens  * Function prototypes
37fa9e4066Sahrens  */
38fa9e4066Sahrens 
39fa9e4066Sahrens static void handle_error(const char *, va_list);
40fa9e4066Sahrens static int add_disk_to_xml(dmgt_disk_t *, void *);
41*fbfd10ffStalley static int add_pool_to_xml(nvlist_t *, void *);
42fa9e4066Sahrens static xmlDocPtr create_doc();
43fa9e4066Sahrens int main();
44fa9e4066Sahrens 
45fa9e4066Sahrens /*
46fa9e4066Sahrens  * Static functions
47fa9e4066Sahrens  */
48fa9e4066Sahrens 
49fa9e4066Sahrens static void
50fa9e4066Sahrens handle_error(const char *fmt, va_list ap)
51fa9e4066Sahrens {
52fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
53fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
54fa9e4066Sahrens }
55fa9e4066Sahrens 
56fa9e4066Sahrens static int
57fa9e4066Sahrens add_disk_to_xml(dmgt_disk_t *dp, void *data)
58fa9e4066Sahrens {
59c8e9ed14Stalley 	int i;
60fa9e4066Sahrens 	char tmp[64];
61fa9e4066Sahrens 	xmlNodePtr available = *((xmlNodePtr *)data);
62fa9e4066Sahrens 
63fa9e4066Sahrens 	xmlNodePtr disk = xmlNewChild(
64fa9e4066Sahrens 	    available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
65fa9e4066Sahrens 	xmlSetProp(disk,
66fa9e4066Sahrens 	    (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
67088e9d47Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", dp->size);
68fa9e4066Sahrens 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_SIZE, (xmlChar *)tmp);
69fa9e4066Sahrens 
70dc307942Stalley 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *)
71dc307942Stalley 	    (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE));
72dc307942Stalley 
73fa9e4066Sahrens 	if (dp->aliases != NULL) {
74fa9e4066Sahrens 		for (i = 0; dp->aliases[i] != NULL; i++) {
75fa9e4066Sahrens 			xmlNodePtr alias = xmlNewChild(
76fa9e4066Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
77fa9e4066Sahrens 			xmlSetProp(alias,
78fa9e4066Sahrens 			    (xmlChar *)ATTR_ALIAS_NAME,
79fa9e4066Sahrens 			    (xmlChar *)dp->aliases[i]);
80fa9e4066Sahrens 		}
81fa9e4066Sahrens 	}
82fa9e4066Sahrens 
83fa9e4066Sahrens 	if (dp->slices != NULL) {
84fa9e4066Sahrens 		for (i = 0; dp->slices[i] != NULL; i++) {
85fa9e4066Sahrens 			dmgt_slice_t *sp = dp->slices[i];
86fa9e4066Sahrens 			xmlNodePtr slice = xmlNewChild(
87fa9e4066Sahrens 			    disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
88fa9e4066Sahrens 			xmlSetProp(slice,
89fa9e4066Sahrens 			    (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
90fa9e4066Sahrens 
91088e9d47Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->size);
92fa9e4066Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_SIZE,
93fa9e4066Sahrens 			    (xmlChar *)tmp);
94fa9e4066Sahrens 
95088e9d47Seschrock 			(void) snprintf(tmp, sizeof (tmp), "%llu", sp->start);
96fa9e4066Sahrens 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_START,
97fa9e4066Sahrens 			    (xmlChar *)tmp);
98fa9e4066Sahrens 
99fa9e4066Sahrens 			if (sp->used_name != NULL) {
100fa9e4066Sahrens 				xmlSetProp(slice,
101fa9e4066Sahrens 				    (xmlChar *)ATTR_SLICE_USED_NAME,
102fa9e4066Sahrens 				    (xmlChar *)sp->used_name);
103fa9e4066Sahrens 			}
104fa9e4066Sahrens 
105fa9e4066Sahrens 			if (sp->used_by != NULL) {
106fa9e4066Sahrens 				xmlSetProp(slice, (xmlChar *)ATTR_SLICE_USED_BY,
107fa9e4066Sahrens 				    (xmlChar *)sp->used_by);
108fa9e4066Sahrens 			}
109fa9e4066Sahrens 		}
110fa9e4066Sahrens 	}
111fa9e4066Sahrens 
112fa9e4066Sahrens 	return (0);
113fa9e4066Sahrens }
114fa9e4066Sahrens 
115c8e9ed14Stalley static int
116*fbfd10ffStalley add_pool_to_xml(nvlist_t *config, void *data)
117c8e9ed14Stalley {
118*fbfd10ffStalley 	char *c;
119*fbfd10ffStalley 	char *name;
120*fbfd10ffStalley 	uint64_t guid;
121*fbfd10ffStalley 	uint64_t state;
122*fbfd10ffStalley 	nvlist_t *devices;
123*fbfd10ffStalley 	uint_t n;
124*fbfd10ffStalley 	vdev_stat_t *vs;
125c8e9ed14Stalley 	char tmp[64];
126*fbfd10ffStalley 	xmlNodePtr pool;
127c8e9ed14Stalley 	xmlNodePtr importable = *((xmlNodePtr *)data);
128c8e9ed14Stalley 
129*fbfd10ffStalley 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) ||
130*fbfd10ffStalley 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) ||
131*fbfd10ffStalley 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) ||
132*fbfd10ffStalley 	    nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) ||
133*fbfd10ffStalley 	    nvlist_lookup_uint64_array(
134*fbfd10ffStalley 		devices, ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &n)) {
135*fbfd10ffStalley 		return (-1);
136c8e9ed14Stalley 	}
137*fbfd10ffStalley 
138*fbfd10ffStalley 	pool = xmlNewChild(importable, NULL, (xmlChar *)ELEMENT_POOL, NULL);
139*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name);
140c8e9ed14Stalley 
141088e9d47Seschrock 	(void) snprintf(tmp, sizeof (tmp), "%llu", guid);
142c8e9ed14Stalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_ID, (xmlChar *)tmp);
143c8e9ed14Stalley 
144*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_alloc);
145*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_USED, (xmlChar *)tmp);
146*fbfd10ffStalley 
147*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_space);
148*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_SIZE, (xmlChar *)tmp);
149*fbfd10ffStalley 
150*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_bytes[ZIO_TYPE_READ]);
151*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_READ_BYTES, (xmlChar *)tmp);
152*fbfd10ffStalley 
153*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu",
154*fbfd10ffStalley 	    vs->vs_bytes[ZIO_TYPE_WRITE]);
155*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_WRITE_BYTES, (xmlChar *)tmp);
156*fbfd10ffStalley 
157*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_ops[ZIO_TYPE_READ]);
158*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_READ_OPERATIONS, (xmlChar *)tmp);
159*fbfd10ffStalley 
160*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_ops[ZIO_TYPE_WRITE]);
161*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_WRITE_OPERATIONS, (xmlChar *)tmp);
162*fbfd10ffStalley 
163*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_read_errors);
164*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_READ_ERRORS, (xmlChar *)tmp);
165*fbfd10ffStalley 
166*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_write_errors);
167*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_WRITE_ERRORS, (xmlChar *)tmp);
168*fbfd10ffStalley 
169*fbfd10ffStalley 	(void) snprintf(tmp, sizeof (tmp), "%llu", vs->vs_checksum_errors);
170*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_CHECKSUM_ERRORS, (xmlChar *)tmp);
171*fbfd10ffStalley 
172*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATE,
173*fbfd10ffStalley 	    (xmlChar *)zjni_vdev_state_to_str(vs->vs_state));
174*fbfd10ffStalley 
175*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATUS,
176*fbfd10ffStalley 	    (xmlChar *)zjni_vdev_aux_to_str(vs->vs_aux));
177*fbfd10ffStalley 
178*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE,
179*fbfd10ffStalley 	    (xmlChar *)zjni_pool_state_to_str(state));
180*fbfd10ffStalley 
181*fbfd10ffStalley 	xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATUS, (xmlChar *)
182*fbfd10ffStalley 	    zjni_pool_status_to_str(zpool_import_status(config, &c)));
183*fbfd10ffStalley 
184c8e9ed14Stalley 	return (0);
185c8e9ed14Stalley }
186c8e9ed14Stalley 
187fa9e4066Sahrens static xmlDocPtr
188fa9e4066Sahrens create_doc(void)
189fa9e4066Sahrens {
190fa9e4066Sahrens 	/* Create the XML document */
191fa9e4066Sahrens 	xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
192fa9e4066Sahrens 
193fa9e4066Sahrens 	/* Create the root node */
194fa9e4066Sahrens 	xmlNodePtr root = xmlNewDocNode(
195fa9e4066Sahrens 	    doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
196fa9e4066Sahrens 	xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
197fa9e4066Sahrens 
198fa9e4066Sahrens 	return (doc);
199fa9e4066Sahrens }
200fa9e4066Sahrens 
201fa9e4066Sahrens /*
202fa9e4066Sahrens  * Main entry to availdisks.
203fa9e4066Sahrens  *
204fa9e4066Sahrens  * @return      0 on successful exit, non-zero otherwise
205fa9e4066Sahrens  */
206fa9e4066Sahrens int
207c8e9ed14Stalley main(int argc, char **argv)
208fa9e4066Sahrens {
209c8e9ed14Stalley 	int error = 0;
210c8e9ed14Stalley 	int get_pools = 0;
211c8e9ed14Stalley 	int get_devices = 0;
212fa9e4066Sahrens 
213c8e9ed14Stalley 	/* Examine first arg */
214c8e9ed14Stalley 	int c = getopt(argc, argv, CLI_OPTSTRING);
215c8e9ed14Stalley 	switch (c) {
216c8e9ed14Stalley 		case CLI_ARG_ALL:
217c8e9ed14Stalley 			get_devices = 1;
218c8e9ed14Stalley 			get_pools = 1;
219c8e9ed14Stalley 			break;
220c8e9ed14Stalley 
221c8e9ed14Stalley 		case CLI_ARG_DEVICES:
222c8e9ed14Stalley 			get_devices = 1;
223c8e9ed14Stalley 			break;
224c8e9ed14Stalley 
225c8e9ed14Stalley 		case CLI_ARG_POOLS:
226c8e9ed14Stalley 			get_pools = 1;
227c8e9ed14Stalley 			break;
228c8e9ed14Stalley 
229c8e9ed14Stalley 		default:
230c8e9ed14Stalley 			return (1);
231c8e9ed14Stalley 			break;
232c8e9ed14Stalley 	}
233c8e9ed14Stalley 
234c8e9ed14Stalley 	argc -= optind;
235c8e9ed14Stalley 	argv += optind;
236c8e9ed14Stalley 
237c8e9ed14Stalley 	if (get_pools || get_devices) {
238c8e9ed14Stalley 		xmlDocPtr doc = create_doc();
239c8e9ed14Stalley 		xmlNodePtr root = xmlDocGetRootElement(doc);
240c8e9ed14Stalley 
241c8e9ed14Stalley 		if (get_devices) {
242c8e9ed14Stalley 			/* Create the available node */
243c8e9ed14Stalley 			xmlNodePtr available = xmlNewChild(root, NULL,
244c8e9ed14Stalley 			    (xmlChar *)ELEMENT_AVAILABLE, NULL);
245c8e9ed14Stalley 
246c8e9ed14Stalley 			/* libzfs_jni_diskmgt.o error handler */
247fa9e4066Sahrens 			dmgt_set_error_handler(handle_error);
248fa9e4066Sahrens 
249c8e9ed14Stalley 			error = dmgt_avail_disk_iter(
250c8e9ed14Stalley 			    add_disk_to_xml, &available);
251c8e9ed14Stalley 		}
252fa9e4066Sahrens 
253c8e9ed14Stalley 		if (get_pools && !error) {
254c8e9ed14Stalley 			/* Create the importable node */
255c8e9ed14Stalley 			xmlNodePtr importable = xmlNewChild(root, NULL,
256c8e9ed14Stalley 			    (xmlChar *)ELEMENT_IMPORTABLE, NULL);
257c8e9ed14Stalley 
258c8e9ed14Stalley 			error = zjni_ipool_iter(
259c8e9ed14Stalley 			    argc, argv, add_pool_to_xml, &importable);
260c8e9ed14Stalley 		}
261c8e9ed14Stalley 
262fa9e4066Sahrens 		if (!error) {
263fa9e4066Sahrens 			/* Print out XML */
264fa9e4066Sahrens 			xmlDocFormatDump(stdout, doc, 1);
265fa9e4066Sahrens 		}
266fa9e4066Sahrens 
267fa9e4066Sahrens 		xmlFreeDoc(doc);
268c8e9ed14Stalley 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	return (error != 0);
271fa9e4066Sahrens }
272