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 */ 21*11583SSurya.Prakki@Sun.COM 22789Sahrens /* 23*11583SSurya.Prakki@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include "availdevs.h" 281206Stalley #include <libzfs.h> 29789Sahrens #include <libzfs_jni_diskmgt.h> 30894Stalley #include <libzfs_jni_ipool.h> 31789Sahrens #include <libxml/parser.h> 32789Sahrens 33789Sahrens /* 34789Sahrens * Function prototypes 35789Sahrens */ 36789Sahrens 37789Sahrens static void handle_error(const char *, va_list); 381378Stalley static void set_uint64_prop(xmlNodePtr, const char *, uint64_t); 39789Sahrens static int add_disk_to_xml(dmgt_disk_t *, void *); 401206Stalley static int add_pool_to_xml(nvlist_t *, 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 551378Stalley static void 561378Stalley set_uint64_prop(xmlNodePtr node, const char *attr, uint64_t value) 571378Stalley { 581378Stalley static char tmp[64]; 591378Stalley (void) snprintf(tmp, sizeof (tmp), "%llu", value); 60*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(node, (xmlChar *)attr, (xmlChar *)tmp); 611378Stalley } 621378Stalley 63789Sahrens static int 64789Sahrens add_disk_to_xml(dmgt_disk_t *dp, void *data) 65789Sahrens { 66894Stalley int i; 67789Sahrens xmlNodePtr available = *((xmlNodePtr *)data); 68789Sahrens 69789Sahrens xmlNodePtr disk = xmlNewChild( 70789Sahrens available, NULL, (xmlChar *)ELEMENT_DISK, NULL); 71*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(disk, 72789Sahrens (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name); 731378Stalley 741378Stalley set_uint64_prop(disk, ATTR_DISK_SIZE, dp->size); 75789Sahrens 76*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *) 771066Stalley (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE)); 781066Stalley 79789Sahrens if (dp->aliases != NULL) { 80789Sahrens for (i = 0; dp->aliases[i] != NULL; i++) { 81789Sahrens xmlNodePtr alias = xmlNewChild( 82789Sahrens disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL); 83*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(alias, 84789Sahrens (xmlChar *)ATTR_ALIAS_NAME, 85789Sahrens (xmlChar *)dp->aliases[i]); 86789Sahrens } 87789Sahrens } 88789Sahrens 89789Sahrens if (dp->slices != NULL) { 90789Sahrens for (i = 0; dp->slices[i] != NULL; i++) { 91789Sahrens dmgt_slice_t *sp = dp->slices[i]; 92789Sahrens xmlNodePtr slice = xmlNewChild( 93789Sahrens disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL); 94*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice, 95789Sahrens (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name); 96789Sahrens 971378Stalley set_uint64_prop(slice, ATTR_SLICE_SIZE, sp->size); 981378Stalley set_uint64_prop(slice, ATTR_SLICE_START, sp->start); 99789Sahrens 100789Sahrens if (sp->used_name != NULL) { 101*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice, 102789Sahrens (xmlChar *)ATTR_SLICE_USED_NAME, 103789Sahrens (xmlChar *)sp->used_name); 104789Sahrens } 105789Sahrens 106789Sahrens if (sp->used_by != NULL) { 107*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(slice, 108*11583SSurya.Prakki@Sun.COM (xmlChar *)ATTR_SLICE_USED_BY, 109789Sahrens (xmlChar *)sp->used_by); 110789Sahrens } 111789Sahrens } 112789Sahrens } 113789Sahrens 114789Sahrens return (0); 115789Sahrens } 116789Sahrens 117894Stalley static int 1181206Stalley add_pool_to_xml(nvlist_t *config, void *data) 119894Stalley { 1201206Stalley char *c; 1211206Stalley char *name; 1221206Stalley uint64_t guid; 1235582Scristian uint64_t version; 1241206Stalley uint64_t state; 1251206Stalley nvlist_t *devices; 1261206Stalley uint_t n; 1271206Stalley vdev_stat_t *vs; 1281206Stalley xmlNodePtr pool; 129894Stalley xmlNodePtr importable = *((xmlNodePtr *)data); 130894Stalley 1311206Stalley if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) || 1321206Stalley nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) || 1335582Scristian nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) || 1341206Stalley nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) || 1351206Stalley nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) || 1361206Stalley nvlist_lookup_uint64_array( 1375582Scristian devices, ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &n)) { 1381206Stalley return (-1); 1391206Stalley } 140894Stalley 1411206Stalley pool = xmlNewChild(importable, NULL, (xmlChar *)ELEMENT_POOL, NULL); 142*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name); 143894Stalley 1441378Stalley set_uint64_prop(pool, ATTR_POOL_ID, guid); 1455582Scristian set_uint64_prop(pool, ATTR_POOL_VERSION, version); 1461378Stalley set_uint64_prop(pool, ATTR_POOL_USED, vs->vs_alloc); 1471378Stalley set_uint64_prop(pool, ATTR_POOL_SIZE, vs->vs_space); 1481378Stalley set_uint64_prop(pool, ATTR_POOL_REPLACEMENT_SIZE, vs->vs_rsize); 1491378Stalley set_uint64_prop(pool, ATTR_POOL_READ_BYTES, 1501378Stalley vs->vs_bytes[ZIO_TYPE_READ]); 1511378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_BYTES, 1521206Stalley vs->vs_bytes[ZIO_TYPE_WRITE]); 1531378Stalley set_uint64_prop(pool, ATTR_POOL_READ_OPERATIONS, 1541378Stalley vs->vs_ops[ZIO_TYPE_READ]); 1551378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_OPERATIONS, 1561378Stalley vs->vs_ops[ZIO_TYPE_WRITE]); 1571378Stalley set_uint64_prop(pool, ATTR_POOL_READ_ERRORS, vs->vs_read_errors); 1581378Stalley set_uint64_prop(pool, ATTR_POOL_WRITE_ERRORS, vs->vs_write_errors); 1591378Stalley set_uint64_prop(pool, ATTR_POOL_CHECKSUM_ERRORS, 1601378Stalley vs->vs_checksum_errors); 1611206Stalley 162*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATE, 1631206Stalley (xmlChar *)zjni_vdev_state_to_str(vs->vs_state)); 1641206Stalley 165*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATUS, 1661206Stalley (xmlChar *)zjni_vdev_aux_to_str(vs->vs_aux)); 1671206Stalley 168*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE, 1691206Stalley (xmlChar *)zjni_pool_state_to_str(state)); 1701206Stalley 171*11583SSurya.Prakki@Sun.COM (void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATUS, (xmlChar *) 1721206Stalley zjni_pool_status_to_str(zpool_import_status(config, &c))); 1731206Stalley 174894Stalley return (0); 175894Stalley } 176894Stalley 177789Sahrens static xmlDocPtr 178789Sahrens create_doc(void) 179789Sahrens { 180789Sahrens /* Create the XML document */ 181789Sahrens xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0"); 182789Sahrens 183789Sahrens /* Create the root node */ 184789Sahrens xmlNodePtr root = xmlNewDocNode( 185789Sahrens doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL); 186*11583SSurya.Prakki@Sun.COM (void) xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root); 187789Sahrens 188789Sahrens return (doc); 189789Sahrens } 190789Sahrens 191789Sahrens /* 192789Sahrens * Main entry to availdisks. 193789Sahrens * 194789Sahrens * @return 0 on successful exit, non-zero otherwise 195789Sahrens */ 196789Sahrens int 197894Stalley main(int argc, char **argv) 198789Sahrens { 199894Stalley int error = 0; 200894Stalley int get_pools = 0; 201894Stalley int get_devices = 0; 202789Sahrens 203894Stalley /* Examine first arg */ 204894Stalley int c = getopt(argc, argv, CLI_OPTSTRING); 205894Stalley switch (c) { 206894Stalley case CLI_ARG_ALL: 207894Stalley get_devices = 1; 208894Stalley get_pools = 1; 209894Stalley break; 210789Sahrens 211894Stalley case CLI_ARG_DEVICES: 212894Stalley get_devices = 1; 213894Stalley break; 214789Sahrens 215894Stalley case CLI_ARG_POOLS: 216894Stalley get_pools = 1; 217894Stalley break; 218894Stalley 219894Stalley default: 220894Stalley return (1); 221894Stalley break; 222789Sahrens } 223789Sahrens 224894Stalley argc -= optind; 225894Stalley argv += optind; 226894Stalley 227894Stalley if (get_pools || get_devices) { 228894Stalley xmlDocPtr doc = create_doc(); 229894Stalley xmlNodePtr root = xmlDocGetRootElement(doc); 230894Stalley 231894Stalley if (get_devices) { 232894Stalley /* Create the available node */ 233894Stalley xmlNodePtr available = xmlNewChild(root, NULL, 234894Stalley (xmlChar *)ELEMENT_AVAILABLE, NULL); 235894Stalley 236894Stalley /* libzfs_jni_diskmgt.o error handler */ 237894Stalley dmgt_set_error_handler(handle_error); 238894Stalley 239894Stalley error = dmgt_avail_disk_iter( 240894Stalley add_disk_to_xml, &available); 241894Stalley } 242894Stalley 243894Stalley if (get_pools && !error) { 244894Stalley /* Create the importable node */ 245894Stalley xmlNodePtr importable = xmlNewChild(root, NULL, 246894Stalley (xmlChar *)ELEMENT_IMPORTABLE, NULL); 247894Stalley 248894Stalley error = zjni_ipool_iter( 249894Stalley argc, argv, add_pool_to_xml, &importable); 250894Stalley } 251894Stalley 252894Stalley if (!error) { 253894Stalley /* Print out XML */ 254*11583SSurya.Prakki@Sun.COM (void) xmlDocFormatDump(stdout, doc, 1); 255894Stalley } 256894Stalley 257894Stalley xmlFreeDoc(doc); 258894Stalley } 259789Sahrens 260789Sahrens return (error != 0); 261789Sahrens } 262