xref: /titanic_44/usr/src/cmd/zpool/zpool_iter.c (revision e3320f40ba20e6851e73a3237eedf089700bf001)
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 #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 		return (-1);
88 	}
89 
90 	return (0);
91 }
92 
93 /*
94  * Create a list of pools based on the given arguments.  If we're given no
95  * arguments, then iterate over all pools in the system and add them to the AVL
96  * tree.  Otherwise, add only those pool explicitly specified on the command
97  * line.
98  */
99 zpool_list_t *
100 pool_list_get(int argc, char **argv, zpool_proplist_t **proplist, int *err)
101 {
102 	zpool_list_t *zlp;
103 
104 	zlp = safe_malloc(sizeof (zpool_list_t));
105 
106 	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
107 	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
108 
109 	if (zlp->zl_pool == NULL)
110 		zpool_no_memory();
111 
112 	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
113 	    UU_DEFAULT)) == NULL)
114 		zpool_no_memory();
115 
116 	if (argc == 0) {
117 		(void) zpool_iter(g_zfs, add_pool, zlp);
118 		zlp->zl_findall = B_TRUE;
119 	} else {
120 		int i;
121 
122 		for (i = 0; i < argc; i++) {
123 			zpool_handle_t *zhp;
124 
125 			if ((zhp = zpool_open_canfail(g_zfs,
126 			    argv[i])) != NULL && add_pool(zhp, zlp) == 0) {
127 				if (proplist &&
128 				    zpool_expand_proplist(zhp, proplist) != 0)
129 					*err = B_TRUE;
130 			} else
131 				*err = B_TRUE;
132 		}
133 	}
134 
135 	return (zlp);
136 }
137 
138 /*
139  * Search for any new pools, adding them to the list.  We only add pools when no
140  * options were given on the command line.  Otherwise, we keep the list fixed as
141  * those that were explicitly specified.
142  */
143 void
144 pool_list_update(zpool_list_t *zlp)
145 {
146 	if (zlp->zl_findall)
147 		(void) zpool_iter(g_zfs, add_pool, zlp);
148 }
149 
150 /*
151  * Iterate over all pools in the list, executing the callback for each
152  */
153 int
154 pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
155     void *data)
156 {
157 	zpool_node_t *node, *next_node;
158 	int ret = 0;
159 
160 	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
161 		next_node = uu_avl_next(zlp->zl_avl, node);
162 		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
163 		    unavail)
164 			ret |= func(node->zn_handle, data);
165 	}
166 
167 	return (ret);
168 }
169 
170 /*
171  * Remove the given pool from the list.  When running iostat, we want to remove
172  * those pools that no longer exist.
173  */
174 void
175 pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
176 {
177 	zpool_node_t search, *node;
178 
179 	search.zn_handle = zhp;
180 	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
181 		uu_avl_remove(zlp->zl_avl, node);
182 		zpool_close(node->zn_handle);
183 		free(node);
184 	}
185 }
186 
187 /*
188  * Free all the handles associated with this list.
189  */
190 void
191 pool_list_free(zpool_list_t *zlp)
192 {
193 	uu_avl_walk_t *walk;
194 	zpool_node_t *node;
195 
196 	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
197 		(void) fprintf(stderr,
198 		    gettext("internal error: out of memory"));
199 		exit(1);
200 	}
201 
202 	while ((node = uu_avl_walk_next(walk)) != NULL) {
203 		uu_avl_remove(zlp->zl_avl, node);
204 		zpool_close(node->zn_handle);
205 		free(node);
206 	}
207 
208 	uu_avl_walk_end(walk);
209 	uu_avl_destroy(zlp->zl_avl);
210 	uu_avl_pool_destroy(zlp->zl_pool);
211 
212 	free(zlp);
213 }
214 
215 /*
216  * Returns the number of elements in the pool list.
217  */
218 int
219 pool_list_count(zpool_list_t *zlp)
220 {
221 	return (uu_avl_numnodes(zlp->zl_avl));
222 }
223 
224 /*
225  * High level function which iterates over all pools given on the command line,
226  * using the pool_list_* interfaces.
227  */
228 int
229 for_each_pool(int argc, char **argv, boolean_t unavail,
230     zpool_proplist_t **proplist, zpool_iter_f func, void *data)
231 {
232 	zpool_list_t *list;
233 	int ret = 0;
234 
235 	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
236 		return (1);
237 
238 	if (pool_list_iter(list, unavail, func, data) != 0)
239 		ret = 1;
240 
241 	pool_list_free(list);
242 
243 	return (ret);
244 }
245