xref: /illumos-gate/usr/src/cmd/zpool/zpool_iter.c (revision bde334a8dbd66dfa70ce4d7fc9dcad6e1ae45fe4)
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 
36 #include <libzfs.h>
37 
38 #include "zpool_util.h"
39 
40 /*
41  * Private interface for iterating over pools specified on the command line.
42  * Most consumers will call for_each_pool, but in order to support iostat, we
43  * allow fined grained control through the zpool_list_t interface.
44  */
45 
46 typedef struct zpool_node {
47 	zpool_handle_t	*zn_handle;
48 	uu_avl_node_t	zn_avlnode;
49 	int		zn_mark;
50 } zpool_node_t;
51 
52 struct zpool_list {
53 	boolean_t	zl_findall;
54 	uu_avl_t	*zl_avl;
55 	uu_avl_pool_t	*zl_pool;
56 	zprop_list_t	**zl_proplist;
57 };
58 
59 /* ARGSUSED */
60 static int
61 zpool_compare(const void *larg, const void *rarg, void *unused)
62 {
63 	zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
64 	zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
65 	const char *lname = zpool_get_name(l);
66 	const char *rname = zpool_get_name(r);
67 
68 	return (strcmp(lname, rname));
69 }
70 
71 /*
72  * Callback function for pool_list_get().  Adds the given pool to the AVL tree
73  * of known pools.
74  */
75 static int
76 add_pool(zpool_handle_t *zhp, void *data)
77 {
78 	zpool_list_t *zlp = data;
79 	zpool_node_t *node = safe_malloc(sizeof (zpool_node_t));
80 	uu_avl_index_t idx;
81 
82 	node->zn_handle = zhp;
83 	uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool);
84 	if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) {
85 		if (zlp->zl_proplist &&
86 		    zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) {
87 			zpool_close(zhp);
88 			free(node);
89 			return (-1);
90 		}
91 		uu_avl_insert(zlp->zl_avl, node, idx);
92 	} else {
93 		zpool_close(zhp);
94 		free(node);
95 		return (-1);
96 	}
97 
98 	return (0);
99 }
100 
101 /*
102  * Create a list of pools based on the given arguments.  If we're given no
103  * arguments, then iterate over all pools in the system and add them to the AVL
104  * tree.  Otherwise, add only those pool explicitly specified on the command
105  * line.
106  */
107 zpool_list_t *
108 pool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err)
109 {
110 	zpool_list_t *zlp;
111 
112 	zlp = safe_malloc(sizeof (zpool_list_t));
113 
114 	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
115 	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
116 
117 	if (zlp->zl_pool == NULL)
118 		zpool_no_memory();
119 
120 	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
121 	    UU_DEFAULT)) == NULL)
122 		zpool_no_memory();
123 
124 	zlp->zl_proplist = proplist;
125 
126 	if (argc == 0) {
127 		(void) zpool_iter(g_zfs, add_pool, zlp);
128 		zlp->zl_findall = B_TRUE;
129 	} else {
130 		int i;
131 
132 		for (i = 0; i < argc; i++) {
133 			zpool_handle_t *zhp;
134 
135 			if ((zhp = zpool_open_canfail(g_zfs, argv[i])) !=
136 			    NULL) {
137 				if (add_pool(zhp, zlp) != 0)
138 					*err = B_TRUE;
139 			} else {
140 				*err = B_TRUE;
141 			}
142 		}
143 	}
144 
145 	return (zlp);
146 }
147 
148 /*
149  * Search for any new pools, adding them to the list.  We only add pools when no
150  * options were given on the command line.  Otherwise, we keep the list fixed as
151  * those that were explicitly specified.
152  */
153 void
154 pool_list_update(zpool_list_t *zlp)
155 {
156 	if (zlp->zl_findall)
157 		(void) zpool_iter(g_zfs, add_pool, zlp);
158 }
159 
160 /*
161  * Iterate over all pools in the list, executing the callback for each
162  */
163 int
164 pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
165     void *data)
166 {
167 	zpool_node_t *node, *next_node;
168 	int ret = 0;
169 
170 	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
171 		next_node = uu_avl_next(zlp->zl_avl, node);
172 		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
173 		    unavail)
174 			ret |= func(node->zn_handle, data);
175 	}
176 
177 	return (ret);
178 }
179 
180 /*
181  * Remove the given pool from the list.  When running iostat, we want to remove
182  * those pools that no longer exist.
183  */
184 void
185 pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
186 {
187 	zpool_node_t search, *node;
188 
189 	search.zn_handle = zhp;
190 	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
191 		uu_avl_remove(zlp->zl_avl, node);
192 		zpool_close(node->zn_handle);
193 		free(node);
194 	}
195 }
196 
197 /*
198  * Free all the handles associated with this list.
199  */
200 void
201 pool_list_free(zpool_list_t *zlp)
202 {
203 	uu_avl_walk_t *walk;
204 	zpool_node_t *node;
205 
206 	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
207 		(void) fprintf(stderr,
208 		    gettext("internal error: out of memory"));
209 		exit(1);
210 	}
211 
212 	while ((node = uu_avl_walk_next(walk)) != NULL) {
213 		uu_avl_remove(zlp->zl_avl, node);
214 		zpool_close(node->zn_handle);
215 		free(node);
216 	}
217 
218 	uu_avl_walk_end(walk);
219 	uu_avl_destroy(zlp->zl_avl);
220 	uu_avl_pool_destroy(zlp->zl_pool);
221 
222 	free(zlp);
223 }
224 
225 /*
226  * Returns the number of elements in the pool list.
227  */
228 int
229 pool_list_count(zpool_list_t *zlp)
230 {
231 	return (uu_avl_numnodes(zlp->zl_avl));
232 }
233 
234 /*
235  * High level function which iterates over all pools given on the command line,
236  * using the pool_list_* interfaces.
237  */
238 int
239 for_each_pool(int argc, char **argv, boolean_t unavail,
240     zprop_list_t **proplist, zpool_iter_f func, void *data)
241 {
242 	zpool_list_t *list;
243 	int ret = 0;
244 
245 	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
246 		return (1);
247 
248 	if (pool_list_iter(list, unavail, func, data) != 0)
249 		ret = 1;
250 
251 	pool_list_free(list);
252 
253 	return (ret);
254 }
255