xref: /titanic_44/usr/src/cmd/availdevs/availdevs.c (revision 7ff178cd8db129d385d3177eb20744d3b6efc59b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include "availdevs.h"
27 #include <libzfs.h>
28 #include <libzfs_jni_diskmgt.h>
29 #include <libzfs_jni_ipool.h>
30 #include <libxml/parser.h>
31 
32 /*
33  * Function prototypes
34  */
35 
36 static void handle_error(const char *, va_list);
37 static void set_uint64_prop(xmlNodePtr, const char *, uint64_t);
38 static int add_disk_to_xml(dmgt_disk_t *, void *);
39 static int add_pool_to_xml(nvlist_t *, void *);
40 static xmlDocPtr create_doc();
41 int main();
42 
43 /*
44  * Static functions
45  */
46 
47 static void
48 handle_error(const char *fmt, va_list ap)
49 {
50 	(void) vfprintf(stderr, fmt, ap);
51 	(void) fprintf(stderr, "\n");
52 }
53 
54 static void
55 set_uint64_prop(xmlNodePtr node, const char *attr, uint64_t value)
56 {
57 	static char tmp[64];
58 	(void) snprintf(tmp, sizeof (tmp), "%llu", value);
59 	(void) xmlSetProp(node, (xmlChar *)attr, (xmlChar *)tmp);
60 }
61 
62 static int
63 add_disk_to_xml(dmgt_disk_t *dp, void *data)
64 {
65 	int i;
66 	xmlNodePtr available = *((xmlNodePtr *)data);
67 
68 	xmlNodePtr disk = xmlNewChild(
69 	    available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
70 	(void) xmlSetProp(disk,
71 	    (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
72 
73 	set_uint64_prop(disk, ATTR_DISK_SIZE, dp->size);
74 
75 	(void) xmlSetProp(disk, (xmlChar *)ATTR_DISK_INUSE, (xmlChar *)
76 	    (dp->in_use ? VAL_ATTR_TRUE : VAL_ATTR_FALSE));
77 
78 	if (dp->aliases != NULL) {
79 		for (i = 0; dp->aliases[i] != NULL; i++) {
80 			xmlNodePtr alias = xmlNewChild(
81 			    disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
82 			(void) xmlSetProp(alias,
83 			    (xmlChar *)ATTR_ALIAS_NAME,
84 			    (xmlChar *)dp->aliases[i]);
85 		}
86 	}
87 
88 	if (dp->slices != NULL) {
89 		for (i = 0; dp->slices[i] != NULL; i++) {
90 			dmgt_slice_t *sp = dp->slices[i];
91 			xmlNodePtr slice = xmlNewChild(
92 			    disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
93 			(void) xmlSetProp(slice,
94 			    (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
95 
96 			set_uint64_prop(slice, ATTR_SLICE_SIZE, sp->size);
97 			set_uint64_prop(slice, ATTR_SLICE_START, sp->start);
98 
99 			if (sp->used_name != NULL) {
100 				(void) xmlSetProp(slice,
101 				    (xmlChar *)ATTR_SLICE_USED_NAME,
102 				    (xmlChar *)sp->used_name);
103 			}
104 
105 			if (sp->used_by != NULL) {
106 				(void) xmlSetProp(slice,
107 				    (xmlChar *)ATTR_SLICE_USED_BY,
108 				    (xmlChar *)sp->used_by);
109 			}
110 		}
111 	}
112 
113 	return (0);
114 }
115 
116 static int
117 add_pool_to_xml(nvlist_t *config, void *data)
118 {
119 	char *c;
120 	char *name;
121 	uint64_t guid;
122 	uint64_t version;
123 	uint64_t state;
124 	nvlist_t *devices;
125 	uint_t n;
126 	vdev_stat_t *vs;
127 	xmlNodePtr pool;
128 	xmlNodePtr importable = *((xmlNodePtr *)data);
129 
130 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) ||
131 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) ||
132 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) ||
133 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) ||
134 	    nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) ||
135 	    nvlist_lookup_uint64_array(
136 	    devices, ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &n)) {
137 		return (-1);
138 	}
139 
140 	pool = xmlNewChild(importable, NULL, (xmlChar *)ELEMENT_POOL, NULL);
141 	(void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_NAME, (xmlChar *)name);
142 
143 	set_uint64_prop(pool, ATTR_POOL_ID, guid);
144 	set_uint64_prop(pool, ATTR_POOL_VERSION, version);
145 	set_uint64_prop(pool, ATTR_POOL_USED, vs->vs_alloc);
146 	set_uint64_prop(pool, ATTR_POOL_SIZE, vs->vs_space);
147 	set_uint64_prop(pool, ATTR_POOL_REPLACEMENT_SIZE, vs->vs_rsize);
148 	set_uint64_prop(pool, ATTR_POOL_READ_BYTES,
149 	    vs->vs_bytes[ZIO_TYPE_READ]);
150 	set_uint64_prop(pool, ATTR_POOL_WRITE_BYTES,
151 	    vs->vs_bytes[ZIO_TYPE_WRITE]);
152 	set_uint64_prop(pool, ATTR_POOL_READ_OPERATIONS,
153 	    vs->vs_ops[ZIO_TYPE_READ]);
154 	set_uint64_prop(pool, ATTR_POOL_WRITE_OPERATIONS,
155 	    vs->vs_ops[ZIO_TYPE_WRITE]);
156 	set_uint64_prop(pool, ATTR_POOL_READ_ERRORS, vs->vs_read_errors);
157 	set_uint64_prop(pool, ATTR_POOL_WRITE_ERRORS, vs->vs_write_errors);
158 	set_uint64_prop(pool, ATTR_POOL_CHECKSUM_ERRORS,
159 	    vs->vs_checksum_errors);
160 
161 	(void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATE,
162 	    (xmlChar *)zjni_vdev_state_to_str(vs->vs_state));
163 
164 	(void) xmlSetProp(pool, (xmlChar *)ATTR_DEVICE_STATUS,
165 	    (xmlChar *)zjni_vdev_aux_to_str(vs->vs_aux));
166 
167 	(void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATE,
168 	    (xmlChar *)zjni_pool_state_to_str(state));
169 
170 	(void) xmlSetProp(pool, (xmlChar *)ATTR_POOL_STATUS, (xmlChar *)
171 	    zjni_pool_status_to_str(zpool_import_status(config, &c)));
172 
173 	return (0);
174 }
175 
176 static xmlDocPtr
177 create_doc(void)
178 {
179 	/* Create the XML document */
180 	xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
181 
182 	/* Create the root node */
183 	xmlNodePtr root = xmlNewDocNode(
184 	    doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
185 	(void) xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
186 
187 	return (doc);
188 }
189 
190 /*
191  * Main entry to availdisks.
192  *
193  * @return      0 on successful exit, non-zero otherwise
194  */
195 int
196 main(int argc, char **argv)
197 {
198 	int error = 0;
199 	int get_pools = 0;
200 	int get_devices = 0;
201 
202 	/* Examine first arg */
203 	int c = getopt(argc, argv, CLI_OPTSTRING);
204 	switch (c) {
205 		case CLI_ARG_ALL:
206 			get_devices = 1;
207 			get_pools = 1;
208 			break;
209 
210 		case CLI_ARG_DEVICES:
211 			get_devices = 1;
212 			break;
213 
214 		case CLI_ARG_POOLS:
215 			get_pools = 1;
216 			break;
217 
218 		default:
219 			return (1);
220 			break;
221 	}
222 
223 	argc -= optind;
224 	argv += optind;
225 
226 	if (get_pools || get_devices) {
227 		xmlDocPtr doc = create_doc();
228 		xmlNodePtr root = xmlDocGetRootElement(doc);
229 
230 		if (get_devices) {
231 			/* Create the available node */
232 			xmlNodePtr available = xmlNewChild(root, NULL,
233 			    (xmlChar *)ELEMENT_AVAILABLE, NULL);
234 
235 			/* libzfs_jni_diskmgt.o error handler */
236 			dmgt_set_error_handler(handle_error);
237 
238 			error = dmgt_avail_disk_iter(
239 			    add_disk_to_xml, &available);
240 		}
241 
242 		if (get_pools && !error) {
243 			/* Create the importable node */
244 			xmlNodePtr importable = xmlNewChild(root, NULL,
245 			    (xmlChar *)ELEMENT_IMPORTABLE, NULL);
246 
247 			error = zjni_ipool_iter(
248 			    argc, argv, add_pool_to_xml, &importable);
249 		}
250 
251 		if (!error) {
252 			/* Print out XML */
253 			(void) xmlDocFormatDump(stdout, doc, 1);
254 		}
255 
256 		xmlFreeDoc(doc);
257 	}
258 
259 	return (error != 0);
260 }
261