1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
55582Scristian * Common Development and Distribution License (the "License").
65582Scristian * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
2111583SSurya.Prakki@Sun.COM
22789Sahrens /*
23*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens */
25789Sahrens
26789Sahrens #include "availdevs.h"
271206Stalley #include <libzfs.h>
28789Sahrens #include <libzfs_jni_diskmgt.h>
29894Stalley #include <libzfs_jni_ipool.h>
30789Sahrens #include <libxml/parser.h>
31789Sahrens
32789Sahrens /*
33789Sahrens * Function prototypes
34789Sahrens */
35789Sahrens
36789Sahrens static void handle_error(const char *, va_list);
371378Stalley static void set_uint64_prop(xmlNodePtr, const char *, uint64_t);
38789Sahrens static int add_disk_to_xml(dmgt_disk_t *, void *);
391206Stalley static int add_pool_to_xml(nvlist_t *, void *);
40789Sahrens static xmlDocPtr create_doc();
41789Sahrens int main();
42789Sahrens
43789Sahrens /*
44789Sahrens * Static functions
45789Sahrens */
46789Sahrens
47789Sahrens static void
handle_error(const char * fmt,va_list ap)48789Sahrens handle_error(const char *fmt, va_list ap)
49789Sahrens {
50789Sahrens (void) vfprintf(stderr, fmt, ap);
51789Sahrens (void) fprintf(stderr, "\n");
52789Sahrens }
53789Sahrens
541378Stalley static void
set_uint64_prop(xmlNodePtr node,const char * attr,uint64_t value)551378Stalley set_uint64_prop(xmlNodePtr node, const char *attr, uint64_t value)
561378Stalley {
571378Stalley static char tmp[64];
581378Stalley (void) snprintf(tmp, sizeof (tmp), "%llu", value);
5911583SSurya.Prakki@Sun.COM (void) xmlSetProp(node, (xmlChar *)attr, (xmlChar *)tmp);
601378Stalley }
611378Stalley
62789Sahrens static int
add_disk_to_xml(dmgt_disk_t * dp,void * data)63789Sahrens add_disk_to_xml(dmgt_disk_t *dp, void *data)
64789Sahrens {
65894Stalley int i;
66789Sahrens xmlNodePtr available = *((xmlNodePtr *)data);
67789Sahrens
68789Sahrens xmlNodePtr disk = xmlNewChild(
69789Sahrens available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
7011583SSurya.Prakki@Sun.COM (void) xmlSetProp(disk,
71789Sahrens (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
721378Stalley
731378Stalley set_uint64_prop(disk, ATTR_DISK_SIZE, dp->size);
74789Sahrens
7511583SSurya.Prakki@Sun.COM (void) xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *)
761066Stalley (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE));
771066Stalley
78789Sahrens if (dp->aliases != NULL) {
79789Sahrens for (i = 0; dp->aliases[i] != NULL; i++) {
80789Sahrens xmlNodePtr alias = xmlNewChild(
81789Sahrens disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
8211583SSurya.Prakki@Sun.COM (void) xmlSetProp(alias,
83789Sahrens (xmlChar *)ATTR_ALIAS_NAME,
84789Sahrens (xmlChar *)dp->aliases[i]);
85789Sahrens }
86789Sahrens }
87789Sahrens
88789Sahrens if (dp->slices != NULL) {
89789Sahrens for (i = 0; dp->slices[i] != NULL; i++) {
90789Sahrens dmgt_slice_t *sp = dp->slices[i];
91789Sahrens xmlNodePtr slice = xmlNewChild(
92789Sahrens disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
9311583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice,
94789Sahrens (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
95789Sahrens
961378Stalley set_uint64_prop(slice, ATTR_SLICE_SIZE, sp->size);
971378Stalley set_uint64_prop(slice, ATTR_SLICE_START, sp->start);
98789Sahrens
99789Sahrens if (sp->used_name != NULL) {
10011583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice,
101789Sahrens (xmlChar *)ATTR_SLICE_USED_NAME,
102789Sahrens (xmlChar *)sp->used_name);
103789Sahrens }
104789Sahrens
105789Sahrens if (sp->used_by != NULL) {
10611583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice,
10711583SSurya.Prakki@Sun.COM (xmlChar *)ATTR_SLICE_USED_BY,
108789Sahrens (xmlChar *)sp->used_by);
109789Sahrens }
110789Sahrens }
111789Sahrens }
112789Sahrens
113789Sahrens return (0);
114789Sahrens }
115789Sahrens
116894Stalley static int
add_pool_to_xml(nvlist_t * config,void * data)1171206Stalley add_pool_to_xml(nvlist_t *config, void *data)
118894Stalley {
1191206Stalley char *c;
1201206Stalley char *name;
1211206Stalley uint64_t guid;
1225582Scristian uint64_t version;
1231206Stalley uint64_t state;
1241206Stalley nvlist_t *devices;
1251206Stalley uint_t n;
1261206Stalley vdev_stat_t *vs;
1271206Stalley xmlNodePtr pool;
128894Stalley xmlNodePtr importable = *((xmlNodePtr *)data);
129894Stalley
1301206Stalley if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) ||
1311206Stalley nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) ||
1325582Scristian nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) ||
1331206Stalley nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) ||
1341206Stalley nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) ||
1351206Stalley nvlist_lookup_uint64_array(
136*12296SLin.Ling@Sun.COM devices, ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &n)) {
1371206Stalley return (-1);
1381206Stalley }
139894Stalley
1401206Stalley pool = xmlNewChild(importable, NULL, (xmlChar *)ELEMENT_POOL, NULL);
14111583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name);
142894Stalley
1431378Stalley set_uint64_prop(pool, ATTR_POOL_ID, guid);
1445582Scristian set_uint64_prop(pool, ATTR_POOL_VERSION, version);
1451378Stalley set_uint64_prop(pool, ATTR_POOL_USED, vs->vs_alloc);
1461378Stalley set_uint64_prop(pool, ATTR_POOL_SIZE, vs->vs_space);
1471378Stalley set_uint64_prop(pool, ATTR_POOL_REPLACEMENT_SIZE, vs->vs_rsize);
1481378Stalley set_uint64_prop(pool, ATTR_POOL_READ_BYTES,
1491378Stalley vs->vs_bytes[ZIO_TYPE_READ]);
1501378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_BYTES,
1511206Stalley vs->vs_bytes[ZIO_TYPE_WRITE]);
1521378Stalley set_uint64_prop(pool, ATTR_POOL_READ_OPERATIONS,
1531378Stalley vs->vs_ops[ZIO_TYPE_READ]);
1541378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_OPERATIONS,
1551378Stalley vs->vs_ops[ZIO_TYPE_WRITE]);
1561378Stalley set_uint64_prop(pool, ATTR_POOL_READ_ERRORS, vs->vs_read_errors);
1571378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_ERRORS, vs->vs_write_errors);
1581378Stalley set_uint64_prop(pool, ATTR_POOL_CHECKSUM_ERRORS,
1591378Stalley vs->vs_checksum_errors);
1601206Stalley
16111583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATE,
1621206Stalley (xmlChar *)zjni_vdev_state_to_str(vs->vs_state));
1631206Stalley
16411583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATUS,
1651206Stalley (xmlChar *)zjni_vdev_aux_to_str(vs->vs_aux));
1661206Stalley
16711583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE,
1681206Stalley (xmlChar *)zjni_pool_state_to_str(state));
1691206Stalley
17011583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATUS, (xmlChar *)
1711206Stalley zjni_pool_status_to_str(zpool_import_status(config, &c)));
1721206Stalley
173894Stalley return (0);
174894Stalley }
175894Stalley
176789Sahrens static xmlDocPtr
create_doc(void)177789Sahrens create_doc(void)
178789Sahrens {
179789Sahrens /* Create the XML document */
180789Sahrens xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
181789Sahrens
182789Sahrens /* Create the root node */
183789Sahrens xmlNodePtr root = xmlNewDocNode(
184789Sahrens doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
18511583SSurya.Prakki@Sun.COM (void) xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
186789Sahrens
187789Sahrens return (doc);
188789Sahrens }
189789Sahrens
190789Sahrens /*
191789Sahrens * Main entry to availdisks.
192789Sahrens *
193789Sahrens * @return 0 on successful exit, non-zero otherwise
194789Sahrens */
195789Sahrens int
main(int argc,char ** argv)196894Stalley main(int argc, char **argv)
197789Sahrens {
198894Stalley int error = 0;
199894Stalley int get_pools = 0;
200894Stalley int get_devices = 0;
201789Sahrens
202894Stalley /* Examine first arg */
203894Stalley int c = getopt(argc, argv, CLI_OPTSTRING);
204894Stalley switch (c) {
205894Stalley case CLI_ARG_ALL:
206894Stalley get_devices = 1;
207894Stalley get_pools = 1;
208894Stalley break;
209789Sahrens
210894Stalley case CLI_ARG_DEVICES:
211894Stalley get_devices = 1;
212894Stalley break;
213789Sahrens
214894Stalley case CLI_ARG_POOLS:
215894Stalley get_pools = 1;
216894Stalley break;
217894Stalley
218894Stalley default:
219894Stalley return (1);
220894Stalley break;
221789Sahrens }
222789Sahrens
223894Stalley argc -= optind;
224894Stalley argv += optind;
225894Stalley
226894Stalley if (get_pools || get_devices) {
227894Stalley xmlDocPtr doc = create_doc();
228894Stalley xmlNodePtr root = xmlDocGetRootElement(doc);
229894Stalley
230894Stalley if (get_devices) {
231894Stalley /* Create the available node */
232894Stalley xmlNodePtr available = xmlNewChild(root, NULL,
233894Stalley (xmlChar *)ELEMENT_AVAILABLE, NULL);
234894Stalley
235894Stalley /* libzfs_jni_diskmgt.o error handler */
236894Stalley dmgt_set_error_handler(handle_error);
237894Stalley
238894Stalley error = dmgt_avail_disk_iter(
239894Stalley add_disk_to_xml, &available);
240894Stalley }
241894Stalley
242894Stalley if (get_pools && !error) {
243894Stalley /* Create the importable node */
244894Stalley xmlNodePtr importable = xmlNewChild(root, NULL,
245894Stalley (xmlChar *)ELEMENT_IMPORTABLE, NULL);
246894Stalley
247894Stalley error = zjni_ipool_iter(
248894Stalley argc, argv, add_pool_to_xml, &importable);
249894Stalley }
250894Stalley
251894Stalley if (!error) {
252894Stalley /* Print out XML */
25311583SSurya.Prakki@Sun.COM (void) xmlDocFormatDump(stdout, doc, 1);
254894Stalley }
255894Stalley
256894Stalley xmlFreeDoc(doc);
257894Stalley }
258789Sahrens
259789Sahrens return (error != 0);
260789Sahrens }
261