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