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