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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include "zpool_util.h"
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.
45 typedef struct zpool_node
{
46 zpool_handle_t
*zn_handle
;
47 uu_avl_node_t zn_avlnode
;
54 uu_avl_pool_t
*zl_pool
;
55 zprop_list_t
**zl_proplist
;
60 zpool_compare(const void *larg
, const void *rarg
, void *unused
)
62 zpool_handle_t
*l
= ((zpool_node_t
*)larg
)->zn_handle
;
63 zpool_handle_t
*r
= ((zpool_node_t
*)rarg
)->zn_handle
;
64 const char *lname
= zpool_get_name(l
);
65 const char *rname
= zpool_get_name(r
);
67 return (strcmp(lname
, rname
));
71 * Callback function for pool_list_get(). Adds the given pool to the AVL tree
75 add_pool(zpool_handle_t
*zhp
, void *data
)
77 zpool_list_t
*zlp
= data
;
78 zpool_node_t
*node
= safe_malloc(sizeof (zpool_node_t
));
81 node
->zn_handle
= zhp
;
82 uu_avl_node_init(node
, &node
->zn_avlnode
, zlp
->zl_pool
);
83 if (uu_avl_find(zlp
->zl_avl
, node
, NULL
, &idx
) == NULL
) {
84 if (zlp
->zl_proplist
&&
85 zpool_expand_proplist(zhp
, zlp
->zl_proplist
) != 0) {
90 uu_avl_insert(zlp
->zl_avl
, node
, idx
);
101 * Create a list of pools based on the given arguments. If we're given no
102 * arguments, then iterate over all pools in the system and add them to the AVL
103 * tree. Otherwise, add only those pool explicitly specified on the command
107 pool_list_get(int argc
, char **argv
, zprop_list_t
**proplist
, int *err
)
111 zlp
= safe_malloc(sizeof (zpool_list_t
));
113 zlp
->zl_pool
= uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t
),
114 offsetof(zpool_node_t
, zn_avlnode
), zpool_compare
, UU_DEFAULT
);
116 if (zlp
->zl_pool
== NULL
)
119 if ((zlp
->zl_avl
= uu_avl_create(zlp
->zl_pool
, NULL
,
120 UU_DEFAULT
)) == NULL
)
123 zlp
->zl_proplist
= proplist
;
126 (void) zpool_iter(g_zfs
, add_pool
, zlp
);
127 zlp
->zl_findall
= B_TRUE
;
131 for (i
= 0; i
< argc
; i
++) {
134 if (zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) {
135 if (add_pool(zhp
, zlp
) != 0)
147 * Search for any new pools, adding them to the list. We only add pools when no
148 * options were given on the command line. Otherwise, we keep the list fixed as
149 * those that were explicitly specified.
152 pool_list_update(zpool_list_t
*zlp
)
155 (void) zpool_iter(g_zfs
, add_pool
, zlp
);
159 * Iterate over all pools in the list, executing the callback for each
162 pool_list_iter(zpool_list_t
*zlp
, int unavail
, zpool_iter_f func
,
165 zpool_node_t
*node
, *next_node
;
168 for (node
= uu_avl_first(zlp
->zl_avl
); node
!= NULL
; node
= next_node
) {
169 next_node
= uu_avl_next(zlp
->zl_avl
, node
);
170 if (zpool_get_state(node
->zn_handle
) != POOL_STATE_UNAVAIL
||
172 ret
|= func(node
->zn_handle
, data
);
179 * Remove the given pool from the list. When running iostat, we want to remove
180 * those pools that no longer exist.
183 pool_list_remove(zpool_list_t
*zlp
, zpool_handle_t
*zhp
)
185 zpool_node_t search
, *node
;
187 search
.zn_handle
= zhp
;
188 if ((node
= uu_avl_find(zlp
->zl_avl
, &search
, NULL
, NULL
)) != NULL
) {
189 uu_avl_remove(zlp
->zl_avl
, node
);
190 zpool_close(node
->zn_handle
);
196 * Free all the handles associated with this list.
199 pool_list_free(zpool_list_t
*zlp
)
204 if ((walk
= uu_avl_walk_start(zlp
->zl_avl
, UU_WALK_ROBUST
)) == NULL
) {
205 (void) fprintf(stderr
,
206 gettext("internal error: out of memory"));
210 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
211 uu_avl_remove(zlp
->zl_avl
, node
);
212 zpool_close(node
->zn_handle
);
216 uu_avl_walk_end(walk
);
217 uu_avl_destroy(zlp
->zl_avl
);
218 uu_avl_pool_destroy(zlp
->zl_pool
);
224 * Returns the number of elements in the pool list.
227 pool_list_count(zpool_list_t
*zlp
)
229 return (uu_avl_numnodes(zlp
->zl_avl
));
233 * High level function which iterates over all pools given on the command line,
234 * using the pool_list_* interfaces.
237 for_each_pool(int argc
, char **argv
, boolean_t unavail
,
238 zprop_list_t
**proplist
, zpool_iter_f func
, void *data
)
243 if ((list
= pool_list_get(argc
, argv
, proplist
, &ret
)) == NULL
)
246 if (pool_list_iter(list
, unavail
, func
, data
) != 0)
249 pool_list_free(list
);