xref: /titanic_44/usr/src/cmd/zpool/zpool_iter.c (revision 990b4856d0eaada6f8140335733a1b1771ed2746)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
599653d4eSeschrock  * Common Development and Distribution License (the "License").
699653d4eSeschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22b1b8ab34Slling  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <libintl.h>
29fa9e4066Sahrens #include <libuutil.h>
30fa9e4066Sahrens #include <stddef.h>
31fa9e4066Sahrens #include <stdio.h>
32fa9e4066Sahrens #include <stdlib.h>
33fa9e4066Sahrens #include <strings.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens #include <libzfs.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens #include "zpool_util.h"
38fa9e4066Sahrens 
39fa9e4066Sahrens /*
40fa9e4066Sahrens  * Private interface for iterating over pools specified on the command line.
41fa9e4066Sahrens  * Most consumers will call for_each_pool, but in order to support iostat, we
42fa9e4066Sahrens  * allow fined grained control through the zpool_list_t interface.
43fa9e4066Sahrens  */
44fa9e4066Sahrens 
45fa9e4066Sahrens typedef struct zpool_node {
46fa9e4066Sahrens 	zpool_handle_t	*zn_handle;
47fa9e4066Sahrens 	uu_avl_node_t	zn_avlnode;
48fa9e4066Sahrens 	int		zn_mark;
49fa9e4066Sahrens } zpool_node_t;
50fa9e4066Sahrens 
51fa9e4066Sahrens struct zpool_list {
5299653d4eSeschrock 	boolean_t	zl_findall;
53fa9e4066Sahrens 	uu_avl_t	*zl_avl;
54fa9e4066Sahrens 	uu_avl_pool_t	*zl_pool;
55*990b4856Slling 	zprop_list_t	**zl_proplist;
56fa9e4066Sahrens };
57fa9e4066Sahrens 
58fa9e4066Sahrens /* ARGSUSED */
59fa9e4066Sahrens static int
zpool_compare(const void * larg,const void * rarg,void * unused)60fa9e4066Sahrens zpool_compare(const void *larg, const void *rarg, void *unused)
61fa9e4066Sahrens {
62fa9e4066Sahrens 	zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
63fa9e4066Sahrens 	zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
64fa9e4066Sahrens 	const char *lname = zpool_get_name(l);
65fa9e4066Sahrens 	const char *rname = zpool_get_name(r);
66fa9e4066Sahrens 
67fa9e4066Sahrens 	return (strcmp(lname, rname));
68fa9e4066Sahrens }
69fa9e4066Sahrens 
70fa9e4066Sahrens /*
71fa9e4066Sahrens  * Callback function for pool_list_get().  Adds the given pool to the AVL tree
72fa9e4066Sahrens  * of known pools.
73fa9e4066Sahrens  */
74fa9e4066Sahrens static int
add_pool(zpool_handle_t * zhp,void * data)75fa9e4066Sahrens add_pool(zpool_handle_t *zhp, void *data)
76fa9e4066Sahrens {
77fa9e4066Sahrens 	zpool_list_t *zlp = data;
78fa9e4066Sahrens 	zpool_node_t *node = safe_malloc(sizeof (zpool_node_t));
79fa9e4066Sahrens 	uu_avl_index_t idx;
80fa9e4066Sahrens 
81fa9e4066Sahrens 	node->zn_handle = zhp;
82fa9e4066Sahrens 	uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool);
83fa9e4066Sahrens 	if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) {
84*990b4856Slling 		if (zlp->zl_proplist &&
85*990b4856Slling 		    zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) {
86*990b4856Slling 			zpool_close(zhp);
87*990b4856Slling 			free(node);
88*990b4856Slling 			return (-1);
89*990b4856Slling 		}
90fa9e4066Sahrens 		uu_avl_insert(zlp->zl_avl, node, idx);
91fa9e4066Sahrens 	} else {
92fa9e4066Sahrens 		zpool_close(zhp);
93fa9e4066Sahrens 		free(node);
94b1b8ab34Slling 		return (-1);
95fa9e4066Sahrens 	}
96fa9e4066Sahrens 
97fa9e4066Sahrens 	return (0);
98fa9e4066Sahrens }
99fa9e4066Sahrens 
100fa9e4066Sahrens /*
101fa9e4066Sahrens  * Create a list of pools based on the given arguments.  If we're given no
102fa9e4066Sahrens  * arguments, then iterate over all pools in the system and add them to the AVL
103fa9e4066Sahrens  * tree.  Otherwise, add only those pool explicitly specified on the command
104fa9e4066Sahrens  * line.
105fa9e4066Sahrens  */
106fa9e4066Sahrens zpool_list_t *
pool_list_get(int argc,char ** argv,zprop_list_t ** proplist,int * err)107*990b4856Slling pool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	zpool_list_t *zlp;
110fa9e4066Sahrens 
111fa9e4066Sahrens 	zlp = safe_malloc(sizeof (zpool_list_t));
112fa9e4066Sahrens 
113fa9e4066Sahrens 	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
114fa9e4066Sahrens 	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
115fa9e4066Sahrens 
116fa9e4066Sahrens 	if (zlp->zl_pool == NULL)
1175ad82045Snd150628 		zpool_no_memory();
118fa9e4066Sahrens 
119fa9e4066Sahrens 	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
120fa9e4066Sahrens 	    UU_DEFAULT)) == NULL)
1215ad82045Snd150628 		zpool_no_memory();
122fa9e4066Sahrens 
123*990b4856Slling 	zlp->zl_proplist = proplist;
124*990b4856Slling 
125fa9e4066Sahrens 	if (argc == 0) {
12699653d4eSeschrock 		(void) zpool_iter(g_zfs, add_pool, zlp);
12799653d4eSeschrock 		zlp->zl_findall = B_TRUE;
128fa9e4066Sahrens 	} else {
129fa9e4066Sahrens 		int i;
130fa9e4066Sahrens 
131fa9e4066Sahrens 		for (i = 0; i < argc; i++) {
132fa9e4066Sahrens 			zpool_handle_t *zhp;
133fa9e4066Sahrens 
134*990b4856Slling 			if (zhp = zpool_open_canfail(g_zfs, argv[i])) {
135*990b4856Slling 				if (add_pool(zhp, zlp) != 0)
136b1b8ab34Slling 					*err = B_TRUE;
137*990b4856Slling 			} else {
13899653d4eSeschrock 				*err = B_TRUE;
139fa9e4066Sahrens 			}
140fa9e4066Sahrens 		}
141*990b4856Slling 	}
142fa9e4066Sahrens 
143fa9e4066Sahrens 	return (zlp);
144fa9e4066Sahrens }
145fa9e4066Sahrens 
146fa9e4066Sahrens /*
147fa9e4066Sahrens  * Search for any new pools, adding them to the list.  We only add pools when no
148fa9e4066Sahrens  * options were given on the command line.  Otherwise, we keep the list fixed as
149fa9e4066Sahrens  * those that were explicitly specified.
150fa9e4066Sahrens  */
151fa9e4066Sahrens void
pool_list_update(zpool_list_t * zlp)152fa9e4066Sahrens pool_list_update(zpool_list_t *zlp)
153fa9e4066Sahrens {
154fa9e4066Sahrens 	if (zlp->zl_findall)
15599653d4eSeschrock 		(void) zpool_iter(g_zfs, add_pool, zlp);
156fa9e4066Sahrens }
157fa9e4066Sahrens 
158fa9e4066Sahrens /*
159fa9e4066Sahrens  * Iterate over all pools in the list, executing the callback for each
160fa9e4066Sahrens  */
161fa9e4066Sahrens int
pool_list_iter(zpool_list_t * zlp,int unavail,zpool_iter_f func,void * data)162fa9e4066Sahrens pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
163fa9e4066Sahrens     void *data)
164fa9e4066Sahrens {
165fa9e4066Sahrens 	zpool_node_t *node, *next_node;
166fa9e4066Sahrens 	int ret = 0;
167fa9e4066Sahrens 
168fa9e4066Sahrens 	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
169fa9e4066Sahrens 		next_node = uu_avl_next(zlp->zl_avl, node);
170fa9e4066Sahrens 		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
171fa9e4066Sahrens 		    unavail)
172fa9e4066Sahrens 			ret |= func(node->zn_handle, data);
173fa9e4066Sahrens 	}
174fa9e4066Sahrens 
175fa9e4066Sahrens 	return (ret);
176fa9e4066Sahrens }
177fa9e4066Sahrens 
178fa9e4066Sahrens /*
179fa9e4066Sahrens  * Remove the given pool from the list.  When running iostat, we want to remove
180fa9e4066Sahrens  * those pools that no longer exist.
181fa9e4066Sahrens  */
182fa9e4066Sahrens void
pool_list_remove(zpool_list_t * zlp,zpool_handle_t * zhp)183fa9e4066Sahrens pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
184fa9e4066Sahrens {
185fa9e4066Sahrens 	zpool_node_t search, *node;
186fa9e4066Sahrens 
187fa9e4066Sahrens 	search.zn_handle = zhp;
188fa9e4066Sahrens 	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
189fa9e4066Sahrens 		uu_avl_remove(zlp->zl_avl, node);
190fa9e4066Sahrens 		zpool_close(node->zn_handle);
191fa9e4066Sahrens 		free(node);
192fa9e4066Sahrens 	}
193fa9e4066Sahrens }
194fa9e4066Sahrens 
195fa9e4066Sahrens /*
196fa9e4066Sahrens  * Free all the handles associated with this list.
197fa9e4066Sahrens  */
198fa9e4066Sahrens void
pool_list_free(zpool_list_t * zlp)199fa9e4066Sahrens pool_list_free(zpool_list_t *zlp)
200fa9e4066Sahrens {
201fa9e4066Sahrens 	uu_avl_walk_t *walk;
202fa9e4066Sahrens 	zpool_node_t *node;
203fa9e4066Sahrens 
204fa9e4066Sahrens 	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
205fa9e4066Sahrens 		(void) fprintf(stderr,
206fa9e4066Sahrens 		    gettext("internal error: out of memory"));
207fa9e4066Sahrens 		exit(1);
208fa9e4066Sahrens 	}
209fa9e4066Sahrens 
210fa9e4066Sahrens 	while ((node = uu_avl_walk_next(walk)) != NULL) {
211fa9e4066Sahrens 		uu_avl_remove(zlp->zl_avl, node);
212fa9e4066Sahrens 		zpool_close(node->zn_handle);
213fa9e4066Sahrens 		free(node);
214fa9e4066Sahrens 	}
215fa9e4066Sahrens 
216fa9e4066Sahrens 	uu_avl_walk_end(walk);
217fa9e4066Sahrens 	uu_avl_destroy(zlp->zl_avl);
218fa9e4066Sahrens 	uu_avl_pool_destroy(zlp->zl_pool);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	free(zlp);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
223fa9e4066Sahrens /*
224fa9e4066Sahrens  * Returns the number of elements in the pool list.
225fa9e4066Sahrens  */
226fa9e4066Sahrens int
pool_list_count(zpool_list_t * zlp)227fa9e4066Sahrens pool_list_count(zpool_list_t *zlp)
228fa9e4066Sahrens {
229fa9e4066Sahrens 	return (uu_avl_numnodes(zlp->zl_avl));
230fa9e4066Sahrens }
231fa9e4066Sahrens 
232fa9e4066Sahrens /*
233fa9e4066Sahrens  * High level function which iterates over all pools given on the command line,
234fa9e4066Sahrens  * using the pool_list_* interfaces.
235fa9e4066Sahrens  */
236fa9e4066Sahrens int
for_each_pool(int argc,char ** argv,boolean_t unavail,zprop_list_t ** proplist,zpool_iter_f func,void * data)237b1b8ab34Slling for_each_pool(int argc, char **argv, boolean_t unavail,
238*990b4856Slling     zprop_list_t **proplist, zpool_iter_f func, void *data)
239fa9e4066Sahrens {
240fa9e4066Sahrens 	zpool_list_t *list;
241fa9e4066Sahrens 	int ret = 0;
242fa9e4066Sahrens 
243b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
244fa9e4066Sahrens 		return (1);
245fa9e4066Sahrens 
246fa9e4066Sahrens 	if (pool_list_iter(list, unavail, func, data) != 0)
247fa9e4066Sahrens 		ret = 1;
248fa9e4066Sahrens 
249fa9e4066Sahrens 	pool_list_free(list);
250fa9e4066Sahrens 
251fa9e4066Sahrens 	return (ret);
252fa9e4066Sahrens }
253