xref: /illumos-gate/usr/src/cmd/zpool/zpool_iter.c (revision a92282e44f968185a6bba094d1e5fece2da819cf)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
27  */
28 
29 #include <libintl.h>
30 #include <libuutil.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <sys/sysmacros.h>
36 
37 #include <libzfs.h>
38 #include <libzutil.h>
39 
40 #include "zpool_util.h"
41 
42 /*
43  * Private interface for iterating over pools specified on the command line.
44  * Most consumers will call for_each_pool, but in order to support iostat, we
45  * allow fined grained control through the zpool_list_t interface.
46  */
47 
48 typedef struct zpool_node {
49 	zpool_handle_t	*zn_handle;
50 	uu_avl_node_t	zn_avlnode;
51 	int		zn_mark;
52 } zpool_node_t;
53 
54 struct zpool_list {
55 	boolean_t	zl_findall;
56 	uu_avl_t	*zl_avl;
57 	uu_avl_pool_t	*zl_pool;
58 	zprop_list_t	**zl_proplist;
59 };
60 
61 /* ARGSUSED */
62 static int
63 zpool_compare(const void *larg, const void *rarg, void *unused)
64 {
65 	zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
66 	zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
67 	const char *lname = zpool_get_name(l);
68 	const char *rname = zpool_get_name(r);
69 
70 	return (strcmp(lname, rname));
71 }
72 
73 /*
74  * Callback function for pool_list_get().  Adds the given pool to the AVL tree
75  * of known pools.
76  */
77 static int
78 add_pool(zpool_handle_t *zhp, void *data)
79 {
80 	zpool_list_t *zlp = data;
81 	zpool_node_t *node = safe_malloc(sizeof (zpool_node_t));
82 	uu_avl_index_t idx;
83 
84 	node->zn_handle = zhp;
85 	uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool);
86 	if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) {
87 		if (zlp->zl_proplist &&
88 		    zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) {
89 			zpool_close(zhp);
90 			free(node);
91 			return (-1);
92 		}
93 		uu_avl_insert(zlp->zl_avl, node, idx);
94 	} else {
95 		zpool_close(zhp);
96 		free(node);
97 		return (-1);
98 	}
99 
100 	return (0);
101 }
102 
103 /*
104  * Create a list of pools based on the given arguments.  If we're given no
105  * arguments, then iterate over all pools in the system and add them to the AVL
106  * tree.  Otherwise, add only those pool explicitly specified on the command
107  * line.
108  */
109 zpool_list_t *
110 pool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err)
111 {
112 	zpool_list_t *zlp;
113 
114 	zlp = safe_malloc(sizeof (zpool_list_t));
115 
116 	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
117 	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
118 
119 	if (zlp->zl_pool == NULL)
120 		zpool_no_memory();
121 
122 	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
123 	    UU_DEFAULT)) == NULL)
124 		zpool_no_memory();
125 
126 	zlp->zl_proplist = proplist;
127 
128 	if (argc == 0) {
129 		(void) zpool_iter(g_zfs, add_pool, zlp);
130 		zlp->zl_findall = B_TRUE;
131 	} else {
132 		int i;
133 
134 		for (i = 0; i < argc; i++) {
135 			zpool_handle_t *zhp;
136 
137 			if ((zhp = zpool_open_canfail(g_zfs, argv[i])) !=
138 			    NULL) {
139 				if (add_pool(zhp, zlp) != 0)
140 					*err = B_TRUE;
141 			} else {
142 				*err = B_TRUE;
143 			}
144 		}
145 	}
146 
147 	return (zlp);
148 }
149 
150 /*
151  * Search for any new pools, adding them to the list.  We only add pools when no
152  * options were given on the command line.  Otherwise, we keep the list fixed as
153  * those that were explicitly specified.
154  */
155 void
156 pool_list_update(zpool_list_t *zlp)
157 {
158 	if (zlp->zl_findall)
159 		(void) zpool_iter(g_zfs, add_pool, zlp);
160 }
161 
162 /*
163  * Iterate over all pools in the list, executing the callback for each
164  */
165 int
166 pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
167     void *data)
168 {
169 	zpool_node_t *node, *next_node;
170 	int ret = 0;
171 
172 	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
173 		next_node = uu_avl_next(zlp->zl_avl, node);
174 		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
175 		    unavail)
176 			ret |= func(node->zn_handle, data);
177 	}
178 
179 	return (ret);
180 }
181 
182 /*
183  * Remove the given pool from the list.  When running iostat, we want to remove
184  * those pools that no longer exist.
185  */
186 void
187 pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
188 {
189 	zpool_node_t search, *node;
190 
191 	search.zn_handle = zhp;
192 	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
193 		uu_avl_remove(zlp->zl_avl, node);
194 		zpool_close(node->zn_handle);
195 		free(node);
196 	}
197 }
198 
199 /*
200  * Free all the handles associated with this list.
201  */
202 void
203 pool_list_free(zpool_list_t *zlp)
204 {
205 	uu_avl_walk_t *walk;
206 	zpool_node_t *node;
207 
208 	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
209 		(void) fprintf(stderr,
210 		    gettext("internal error: out of memory"));
211 		exit(1);
212 	}
213 
214 	while ((node = uu_avl_walk_next(walk)) != NULL) {
215 		uu_avl_remove(zlp->zl_avl, node);
216 		zpool_close(node->zn_handle);
217 		free(node);
218 	}
219 
220 	uu_avl_walk_end(walk);
221 	uu_avl_destroy(zlp->zl_avl);
222 	uu_avl_pool_destroy(zlp->zl_pool);
223 
224 	free(zlp);
225 }
226 
227 /*
228  * Returns the number of elements in the pool list.
229  */
230 int
231 pool_list_count(zpool_list_t *zlp)
232 {
233 	return (uu_avl_numnodes(zlp->zl_avl));
234 }
235 
236 /*
237  * High level function which iterates over all pools given on the command line,
238  * using the pool_list_* interfaces.
239  */
240 int
241 for_each_pool(int argc, char **argv, boolean_t unavail,
242     zprop_list_t **proplist, zpool_iter_f func, void *data)
243 {
244 	zpool_list_t *list;
245 	int ret = 0;
246 
247 	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
248 		return (1);
249 
250 	if (pool_list_iter(list, unavail, func, data) != 0)
251 		ret = 1;
252 
253 	pool_list_free(list);
254 
255 	return (ret);
256 }
257 
258 static int
259 for_each_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, pool_vdev_iter_f func,
260     void *data)
261 {
262 	nvlist_t **child;
263 	uint_t c, children;
264 	int ret = 0;
265 	int i;
266 	char *type;
267 
268 	const char *list[] = {
269 	    ZPOOL_CONFIG_SPARES,
270 	    ZPOOL_CONFIG_L2CACHE,
271 	    ZPOOL_CONFIG_CHILDREN
272 	};
273 
274 	for (i = 0; i < ARRAY_SIZE(list); i++) {
275 		if (nvlist_lookup_nvlist_array(nv, list[i], &child,
276 		    &children) == 0) {
277 			for (c = 0; c < children; c++) {
278 				uint64_t ishole = 0;
279 
280 				(void) nvlist_lookup_uint64(child[c],
281 				    ZPOOL_CONFIG_IS_HOLE, &ishole);
282 
283 				if (ishole)
284 					continue;
285 
286 				ret |= for_each_vdev_cb(zhp, child[c], func,
287 				    data);
288 			}
289 		}
290 	}
291 
292 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
293 		return (ret);
294 
295 	/* Don't run our function on root vdevs */
296 	if (strcmp(type, VDEV_TYPE_ROOT) != 0) {
297 		ret |= func(zhp, nv, data);
298 	}
299 
300 	return (ret);
301 }
302 
303 /*
304  * This is the equivalent of for_each_pool() for vdevs.  It iterates thorough
305  * all vdevs in the pool, ignoring root vdevs and holes, calling func() on
306  * each one.
307  *
308  * @zhp:	Zpool handle
309  * @func:	Function to call on each vdev
310  * @data:	Custom data to pass to the function
311  */
312 int
313 for_each_vdev(zpool_handle_t *zhp, pool_vdev_iter_f func, void *data)
314 {
315 	nvlist_t *config, *nvroot;
316 
317 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
318 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
319 		    &nvroot) == 0);
320 	}
321 	return (for_each_vdev_cb(zhp, nvroot, func, data));
322 }
323