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> 31*c8e9ed14Stalley #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 *); 40*c8e9ed14Stalley 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 { 58*c8e9ed14Stalley 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); 66*c8e9ed14Stalley snprintf(tmp, sizeof (tmp), "%llu", dp->size); 67fa9e4066Sahrens xmlSetProp(disk, (xmlChar *)ATTR_DISK_SIZE, (xmlChar *)tmp); 68fa9e4066Sahrens 69fa9e4066Sahrens if (dp->aliases != NULL) { 70fa9e4066Sahrens for (i = 0; dp->aliases[i] != NULL; i++) { 71fa9e4066Sahrens xmlNodePtr alias = xmlNewChild( 72fa9e4066Sahrens disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL); 73fa9e4066Sahrens xmlSetProp(alias, 74fa9e4066Sahrens (xmlChar *)ATTR_ALIAS_NAME, 75fa9e4066Sahrens (xmlChar *)dp->aliases[i]); 76fa9e4066Sahrens } 77fa9e4066Sahrens } 78fa9e4066Sahrens 79fa9e4066Sahrens if (dp->slices != NULL) { 80fa9e4066Sahrens for (i = 0; dp->slices[i] != NULL; i++) { 81fa9e4066Sahrens dmgt_slice_t *sp = dp->slices[i]; 82fa9e4066Sahrens xmlNodePtr slice = xmlNewChild( 83fa9e4066Sahrens disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL); 84fa9e4066Sahrens xmlSetProp(slice, 85fa9e4066Sahrens (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name); 86fa9e4066Sahrens 87*c8e9ed14Stalley snprintf(tmp, sizeof (tmp), "%llu", sp->size); 88fa9e4066Sahrens xmlSetProp(slice, (xmlChar *)ATTR_SLICE_SIZE, 89fa9e4066Sahrens (xmlChar *)tmp); 90fa9e4066Sahrens 91*c8e9ed14Stalley snprintf(tmp, sizeof (tmp), "%llu", sp->start); 92fa9e4066Sahrens xmlSetProp(slice, (xmlChar *)ATTR_SLICE_START, 93fa9e4066Sahrens (xmlChar *)tmp); 94fa9e4066Sahrens 95fa9e4066Sahrens if (sp->used_name != NULL) { 96fa9e4066Sahrens xmlSetProp(slice, 97fa9e4066Sahrens (xmlChar *)ATTR_SLICE_USED_NAME, 98fa9e4066Sahrens (xmlChar *)sp->used_name); 99fa9e4066Sahrens } 100fa9e4066Sahrens 101fa9e4066Sahrens if (sp->used_by != NULL) { 102fa9e4066Sahrens xmlSetProp(slice, (xmlChar *)ATTR_SLICE_USED_BY, 103fa9e4066Sahrens (xmlChar *)sp->used_by); 104fa9e4066Sahrens } 105fa9e4066Sahrens } 106fa9e4066Sahrens } 107fa9e4066Sahrens 108fa9e4066Sahrens return (0); 109fa9e4066Sahrens } 110fa9e4066Sahrens 111*c8e9ed14Stalley static int 112*c8e9ed14Stalley add_pool_to_xml(char *name, uint64_t guid, 113*c8e9ed14Stalley uint64_t pool_state, char *health, void *data) 114*c8e9ed14Stalley { 115*c8e9ed14Stalley char *state; 116*c8e9ed14Stalley char tmp[64]; 117*c8e9ed14Stalley xmlNodePtr importable = *((xmlNodePtr *)data); 118*c8e9ed14Stalley 119*c8e9ed14Stalley xmlNodePtr pool = xmlNewChild( 120*c8e9ed14Stalley importable, NULL, (xmlChar *)ELEMENT_POOL, NULL); 121*c8e9ed14Stalley xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name); 122*c8e9ed14Stalley 123*c8e9ed14Stalley state = zjni_get_state_str(pool_state); 124*c8e9ed14Stalley if (state == NULL) { 125*c8e9ed14Stalley state = ""; 126*c8e9ed14Stalley } 127*c8e9ed14Stalley xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE, (xmlChar *)state); 128*c8e9ed14Stalley xmlSetProp(pool, (xmlChar *)ATTR_POOL_HEALTH, (xmlChar *)health); 129*c8e9ed14Stalley 130*c8e9ed14Stalley snprintf(tmp, sizeof (tmp), "%llu", guid); 131*c8e9ed14Stalley xmlSetProp(pool, (xmlChar *)ATTR_POOL_ID, (xmlChar *)tmp); 132*c8e9ed14Stalley 133*c8e9ed14Stalley return (0); 134*c8e9ed14Stalley } 135*c8e9ed14Stalley 136fa9e4066Sahrens static xmlDocPtr 137fa9e4066Sahrens create_doc(void) 138fa9e4066Sahrens { 139fa9e4066Sahrens /* Create the XML document */ 140fa9e4066Sahrens xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0"); 141fa9e4066Sahrens 142fa9e4066Sahrens /* Create the root node */ 143fa9e4066Sahrens xmlNodePtr root = xmlNewDocNode( 144fa9e4066Sahrens doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL); 145fa9e4066Sahrens xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root); 146fa9e4066Sahrens 147fa9e4066Sahrens return (doc); 148fa9e4066Sahrens } 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * Main entry to availdisks. 152fa9e4066Sahrens * 153fa9e4066Sahrens * @return 0 on successful exit, non-zero otherwise 154fa9e4066Sahrens */ 155fa9e4066Sahrens int 156*c8e9ed14Stalley main(int argc, char **argv) 157fa9e4066Sahrens { 158*c8e9ed14Stalley int error = 0; 159*c8e9ed14Stalley int get_pools = 0; 160*c8e9ed14Stalley int get_devices = 0; 161fa9e4066Sahrens 162*c8e9ed14Stalley /* Examine first arg */ 163*c8e9ed14Stalley int c = getopt(argc, argv, CLI_OPTSTRING); 164*c8e9ed14Stalley switch (c) { 165*c8e9ed14Stalley case CLI_ARG_ALL: 166*c8e9ed14Stalley get_devices = 1; 167*c8e9ed14Stalley get_pools = 1; 168*c8e9ed14Stalley break; 169*c8e9ed14Stalley 170*c8e9ed14Stalley case CLI_ARG_DEVICES: 171*c8e9ed14Stalley get_devices = 1; 172*c8e9ed14Stalley break; 173*c8e9ed14Stalley 174*c8e9ed14Stalley case CLI_ARG_POOLS: 175*c8e9ed14Stalley get_pools = 1; 176*c8e9ed14Stalley break; 177*c8e9ed14Stalley 178*c8e9ed14Stalley default: 179*c8e9ed14Stalley return (1); 180*c8e9ed14Stalley break; 181*c8e9ed14Stalley } 182*c8e9ed14Stalley 183*c8e9ed14Stalley argc -= optind; 184*c8e9ed14Stalley argv += optind; 185*c8e9ed14Stalley 186*c8e9ed14Stalley if (get_pools || get_devices) { 187*c8e9ed14Stalley xmlDocPtr doc = create_doc(); 188*c8e9ed14Stalley xmlNodePtr root = xmlDocGetRootElement(doc); 189*c8e9ed14Stalley 190*c8e9ed14Stalley if (get_devices) { 191*c8e9ed14Stalley /* Create the available node */ 192*c8e9ed14Stalley xmlNodePtr available = xmlNewChild(root, NULL, 193*c8e9ed14Stalley (xmlChar *)ELEMENT_AVAILABLE, NULL); 194*c8e9ed14Stalley 195*c8e9ed14Stalley /* libzfs_jni_diskmgt.o error handler */ 196fa9e4066Sahrens dmgt_set_error_handler(handle_error); 197fa9e4066Sahrens 198*c8e9ed14Stalley error = dmgt_avail_disk_iter( 199*c8e9ed14Stalley add_disk_to_xml, &available); 200*c8e9ed14Stalley } 201fa9e4066Sahrens 202*c8e9ed14Stalley if (get_pools && !error) { 203*c8e9ed14Stalley /* Create the importable node */ 204*c8e9ed14Stalley xmlNodePtr importable = xmlNewChild(root, NULL, 205*c8e9ed14Stalley (xmlChar *)ELEMENT_IMPORTABLE, NULL); 206*c8e9ed14Stalley 207*c8e9ed14Stalley error = zjni_ipool_iter( 208*c8e9ed14Stalley argc, argv, add_pool_to_xml, &importable); 209*c8e9ed14Stalley } 210*c8e9ed14Stalley 211fa9e4066Sahrens if (!error) { 212fa9e4066Sahrens /* Print out XML */ 213fa9e4066Sahrens xmlDocFormatDump(stdout, doc, 1); 214fa9e4066Sahrens } 215fa9e4066Sahrens 216fa9e4066Sahrens xmlFreeDoc(doc); 217*c8e9ed14Stalley } 218fa9e4066Sahrens 219fa9e4066Sahrens return (error != 0); 220fa9e4066Sahrens } 221