1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25eda14cbcSMatt Macy * Use is subject to license terms.
26eda14cbcSMatt Macy */
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy /*
29eda14cbcSMatt Macy * Copyright (c) 2012 by Delphix. All rights reserved.
30eda14cbcSMatt Macy * Copyright (c) 2015 by Syneto S.R.L. All rights reserved.
31eda14cbcSMatt Macy * Copyright 2016 Nexenta Systems, Inc.
32eda14cbcSMatt Macy */
33eda14cbcSMatt Macy
34eda14cbcSMatt Macy /*
35eda14cbcSMatt Macy * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
36eda14cbcSMatt Macy * single packed nvlist. While it would be nice to just read in this
37eda14cbcSMatt Macy * file from userland, this wouldn't work from a local zone. So we have to have
38eda14cbcSMatt Macy * a zpool ioctl to return the complete configuration for all pools. In the
39eda14cbcSMatt Macy * global zone, this will be identical to reading the file and unpacking it in
40eda14cbcSMatt Macy * userland.
41eda14cbcSMatt Macy */
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy #include <errno.h>
44eda14cbcSMatt Macy #include <sys/stat.h>
45eda14cbcSMatt Macy #include <fcntl.h>
46eda14cbcSMatt Macy #include <stddef.h>
47eda14cbcSMatt Macy #include <string.h>
48eda14cbcSMatt Macy #include <unistd.h>
49eda14cbcSMatt Macy #include <libintl.h>
50eda14cbcSMatt Macy #include <libuutil.h>
51eda14cbcSMatt Macy
52eda14cbcSMatt Macy #include "libzfs_impl.h"
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy typedef struct config_node {
55eda14cbcSMatt Macy char *cn_name;
56eda14cbcSMatt Macy nvlist_t *cn_config;
57eda14cbcSMatt Macy uu_avl_node_t cn_avl;
58eda14cbcSMatt Macy } config_node_t;
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy static int
config_node_compare(const void * a,const void * b,void * unused)61eda14cbcSMatt Macy config_node_compare(const void *a, const void *b, void *unused)
62eda14cbcSMatt Macy {
63e92ffd9bSMartin Matuska (void) unused;
64eda14cbcSMatt Macy const config_node_t *ca = (config_node_t *)a;
65eda14cbcSMatt Macy const config_node_t *cb = (config_node_t *)b;
66eda14cbcSMatt Macy
67e92ffd9bSMartin Matuska int ret = strcmp(ca->cn_name, cb->cn_name);
68eda14cbcSMatt Macy
69eda14cbcSMatt Macy if (ret < 0)
70eda14cbcSMatt Macy return (-1);
71eda14cbcSMatt Macy else if (ret > 0)
72eda14cbcSMatt Macy return (1);
73eda14cbcSMatt Macy else
74eda14cbcSMatt Macy return (0);
75eda14cbcSMatt Macy }
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy void
namespace_clear(libzfs_handle_t * hdl)78eda14cbcSMatt Macy namespace_clear(libzfs_handle_t *hdl)
79eda14cbcSMatt Macy {
80eda14cbcSMatt Macy if (hdl->libzfs_ns_avl) {
81eda14cbcSMatt Macy config_node_t *cn;
82eda14cbcSMatt Macy void *cookie = NULL;
83eda14cbcSMatt Macy
84eda14cbcSMatt Macy while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl,
85eda14cbcSMatt Macy &cookie)) != NULL) {
86eda14cbcSMatt Macy nvlist_free(cn->cn_config);
87eda14cbcSMatt Macy free(cn->cn_name);
88eda14cbcSMatt Macy free(cn);
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy
91eda14cbcSMatt Macy uu_avl_destroy(hdl->libzfs_ns_avl);
92eda14cbcSMatt Macy hdl->libzfs_ns_avl = NULL;
93eda14cbcSMatt Macy }
94eda14cbcSMatt Macy
95eda14cbcSMatt Macy if (hdl->libzfs_ns_avlpool) {
96eda14cbcSMatt Macy uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
97eda14cbcSMatt Macy hdl->libzfs_ns_avlpool = NULL;
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy }
100eda14cbcSMatt Macy
101eda14cbcSMatt Macy /*
102eda14cbcSMatt Macy * Loads the pool namespace, or re-loads it if the cache has changed.
103eda14cbcSMatt Macy */
104eda14cbcSMatt Macy static int
namespace_reload(libzfs_handle_t * hdl)105eda14cbcSMatt Macy namespace_reload(libzfs_handle_t *hdl)
106eda14cbcSMatt Macy {
107eda14cbcSMatt Macy nvlist_t *config;
108eda14cbcSMatt Macy config_node_t *cn;
109eda14cbcSMatt Macy nvpair_t *elem;
110eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"};
111eda14cbcSMatt Macy void *cookie;
112eda14cbcSMatt Macy
113eda14cbcSMatt Macy if (hdl->libzfs_ns_gen == 0) {
114eda14cbcSMatt Macy /*
115eda14cbcSMatt Macy * This is the first time we've accessed the configuration
116eda14cbcSMatt Macy * cache. Initialize the AVL tree and then fall through to the
117eda14cbcSMatt Macy * common code.
118eda14cbcSMatt Macy */
119eda14cbcSMatt Macy if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
120eda14cbcSMatt Macy sizeof (config_node_t),
121eda14cbcSMatt Macy offsetof(config_node_t, cn_avl),
122eda14cbcSMatt Macy config_node_compare, UU_DEFAULT)) == NULL)
123eda14cbcSMatt Macy return (no_memory(hdl));
124eda14cbcSMatt Macy
125eda14cbcSMatt Macy if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
126eda14cbcSMatt Macy NULL, UU_DEFAULT)) == NULL)
127eda14cbcSMatt Macy return (no_memory(hdl));
128eda14cbcSMatt Macy }
129eda14cbcSMatt Macy
130716fd348SMartin Matuska zcmd_alloc_dst_nvlist(hdl, &zc, 0);
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy for (;;) {
133eda14cbcSMatt Macy zc.zc_cookie = hdl->libzfs_ns_gen;
134eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
135eda14cbcSMatt Macy switch (errno) {
136eda14cbcSMatt Macy case EEXIST:
137eda14cbcSMatt Macy /*
138eda14cbcSMatt Macy * The namespace hasn't changed.
139eda14cbcSMatt Macy */
140eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
141eda14cbcSMatt Macy return (0);
142eda14cbcSMatt Macy
143eda14cbcSMatt Macy case ENOMEM:
144716fd348SMartin Matuska zcmd_expand_dst_nvlist(hdl, &zc);
145eda14cbcSMatt Macy break;
146eda14cbcSMatt Macy
147eda14cbcSMatt Macy default:
148eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
149eda14cbcSMatt Macy return (zfs_standard_error(hdl, errno,
150eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "failed to read "
151eda14cbcSMatt Macy "pool configuration")));
152eda14cbcSMatt Macy }
153eda14cbcSMatt Macy } else {
154eda14cbcSMatt Macy hdl->libzfs_ns_gen = zc.zc_cookie;
155eda14cbcSMatt Macy break;
156eda14cbcSMatt Macy }
157eda14cbcSMatt Macy }
158eda14cbcSMatt Macy
159eda14cbcSMatt Macy if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
160eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
161eda14cbcSMatt Macy return (-1);
162eda14cbcSMatt Macy }
163eda14cbcSMatt Macy
164eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
165eda14cbcSMatt Macy
166eda14cbcSMatt Macy /*
167eda14cbcSMatt Macy * Clear out any existing configuration information.
168eda14cbcSMatt Macy */
169eda14cbcSMatt Macy cookie = NULL;
170eda14cbcSMatt Macy while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, &cookie)) != NULL) {
171eda14cbcSMatt Macy nvlist_free(cn->cn_config);
172eda14cbcSMatt Macy free(cn->cn_name);
173eda14cbcSMatt Macy free(cn);
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy
176eda14cbcSMatt Macy elem = NULL;
177eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
178eda14cbcSMatt Macy nvlist_t *child;
179eda14cbcSMatt Macy uu_avl_index_t where;
180eda14cbcSMatt Macy
181716fd348SMartin Matuska cn = zfs_alloc(hdl, sizeof (config_node_t));
182716fd348SMartin Matuska cn->cn_name = zfs_strdup(hdl, nvpair_name(elem));
183da5137abSMartin Matuska child = fnvpair_value_nvlist(elem);
184eda14cbcSMatt Macy if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
185eda14cbcSMatt Macy free(cn->cn_name);
186eda14cbcSMatt Macy free(cn);
187eda14cbcSMatt Macy nvlist_free(config);
188eda14cbcSMatt Macy return (no_memory(hdl));
189eda14cbcSMatt Macy }
190eda14cbcSMatt Macy verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
191eda14cbcSMatt Macy == NULL);
192eda14cbcSMatt Macy
193eda14cbcSMatt Macy uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy
196eda14cbcSMatt Macy nvlist_free(config);
197eda14cbcSMatt Macy return (0);
198eda14cbcSMatt Macy }
199eda14cbcSMatt Macy
200eda14cbcSMatt Macy /*
201eda14cbcSMatt Macy * Retrieve the configuration for the given pool. The configuration is an nvlist
202eda14cbcSMatt Macy * describing the vdevs, as well as the statistics associated with each one.
203eda14cbcSMatt Macy */
204eda14cbcSMatt Macy nvlist_t *
zpool_get_config(zpool_handle_t * zhp,nvlist_t ** oldconfig)205eda14cbcSMatt Macy zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
206eda14cbcSMatt Macy {
207eda14cbcSMatt Macy if (oldconfig)
208eda14cbcSMatt Macy *oldconfig = zhp->zpool_old_config;
209eda14cbcSMatt Macy return (zhp->zpool_config);
210eda14cbcSMatt Macy }
211eda14cbcSMatt Macy
212eda14cbcSMatt Macy /*
213eda14cbcSMatt Macy * Retrieves a list of enabled features and their refcounts and caches it in
214eda14cbcSMatt Macy * the pool handle.
215eda14cbcSMatt Macy */
216eda14cbcSMatt Macy nvlist_t *
zpool_get_features(zpool_handle_t * zhp)217eda14cbcSMatt Macy zpool_get_features(zpool_handle_t *zhp)
218eda14cbcSMatt Macy {
219eda14cbcSMatt Macy nvlist_t *config, *features;
220eda14cbcSMatt Macy
221eda14cbcSMatt Macy config = zpool_get_config(zhp, NULL);
222eda14cbcSMatt Macy
223eda14cbcSMatt Macy if (config == NULL || !nvlist_exists(config,
224eda14cbcSMatt Macy ZPOOL_CONFIG_FEATURE_STATS)) {
225eda14cbcSMatt Macy int error;
226eda14cbcSMatt Macy boolean_t missing = B_FALSE;
227eda14cbcSMatt Macy
228eda14cbcSMatt Macy error = zpool_refresh_stats(zhp, &missing);
229eda14cbcSMatt Macy
230eda14cbcSMatt Macy if (error != 0 || missing)
231eda14cbcSMatt Macy return (NULL);
232eda14cbcSMatt Macy
233eda14cbcSMatt Macy config = zpool_get_config(zhp, NULL);
234eda14cbcSMatt Macy }
235eda14cbcSMatt Macy
236eda14cbcSMatt Macy if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
237eda14cbcSMatt Macy &features) != 0)
238eda14cbcSMatt Macy return (NULL);
239eda14cbcSMatt Macy
240eda14cbcSMatt Macy return (features);
241eda14cbcSMatt Macy }
242eda14cbcSMatt Macy
243eda14cbcSMatt Macy /*
244eda14cbcSMatt Macy * Refresh the vdev statistics associated with the given pool. This is used in
245eda14cbcSMatt Macy * iostat to show configuration changes and determine the delta from the last
246eda14cbcSMatt Macy * time the function was called. This function can fail, in case the pool has
247eda14cbcSMatt Macy * been destroyed.
248eda14cbcSMatt Macy */
249eda14cbcSMatt Macy int
zpool_refresh_stats(zpool_handle_t * zhp,boolean_t * missing)250eda14cbcSMatt Macy zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
251eda14cbcSMatt Macy {
252eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"};
253eda14cbcSMatt Macy int error;
254eda14cbcSMatt Macy nvlist_t *config;
255eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl;
256eda14cbcSMatt Macy
257eda14cbcSMatt Macy *missing = B_FALSE;
258eda14cbcSMatt Macy (void) strcpy(zc.zc_name, zhp->zpool_name);
259eda14cbcSMatt Macy
260eda14cbcSMatt Macy if (zhp->zpool_config_size == 0)
261eda14cbcSMatt Macy zhp->zpool_config_size = 1 << 16;
262eda14cbcSMatt Macy
263716fd348SMartin Matuska zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size);
264eda14cbcSMatt Macy
265eda14cbcSMatt Macy for (;;) {
266eda14cbcSMatt Macy if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_STATS,
267eda14cbcSMatt Macy &zc) == 0) {
268eda14cbcSMatt Macy /*
269eda14cbcSMatt Macy * The real error is returned in the zc_cookie field.
270eda14cbcSMatt Macy */
271eda14cbcSMatt Macy error = zc.zc_cookie;
272eda14cbcSMatt Macy break;
273eda14cbcSMatt Macy }
274eda14cbcSMatt Macy
275716fd348SMartin Matuska if (errno == ENOMEM)
276716fd348SMartin Matuska zcmd_expand_dst_nvlist(hdl, &zc);
277716fd348SMartin Matuska else {
278eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
279eda14cbcSMatt Macy if (errno == ENOENT || errno == EINVAL)
280eda14cbcSMatt Macy *missing = B_TRUE;
281eda14cbcSMatt Macy zhp->zpool_state = POOL_STATE_UNAVAIL;
282eda14cbcSMatt Macy return (0);
283eda14cbcSMatt Macy }
284eda14cbcSMatt Macy }
285eda14cbcSMatt Macy
286eda14cbcSMatt Macy if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
287eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
288eda14cbcSMatt Macy return (-1);
289eda14cbcSMatt Macy }
290eda14cbcSMatt Macy
291eda14cbcSMatt Macy zcmd_free_nvlists(&zc);
292eda14cbcSMatt Macy
293eda14cbcSMatt Macy zhp->zpool_config_size = zc.zc_nvlist_dst_size;
294eda14cbcSMatt Macy
295eda14cbcSMatt Macy if (zhp->zpool_config != NULL) {
296eda14cbcSMatt Macy nvlist_free(zhp->zpool_old_config);
297eda14cbcSMatt Macy
298eda14cbcSMatt Macy zhp->zpool_old_config = zhp->zpool_config;
299eda14cbcSMatt Macy }
300eda14cbcSMatt Macy
301eda14cbcSMatt Macy zhp->zpool_config = config;
302eda14cbcSMatt Macy if (error)
303eda14cbcSMatt Macy zhp->zpool_state = POOL_STATE_UNAVAIL;
304eda14cbcSMatt Macy else
305eda14cbcSMatt Macy zhp->zpool_state = POOL_STATE_ACTIVE;
306eda14cbcSMatt Macy
307eda14cbcSMatt Macy return (0);
308eda14cbcSMatt Macy }
309eda14cbcSMatt Macy
310eda14cbcSMatt Macy /*
311eda14cbcSMatt Macy * The following environment variables are undocumented
312eda14cbcSMatt Macy * and should be used for testing purposes only:
313eda14cbcSMatt Macy *
314eda14cbcSMatt Macy * __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
315eda14cbcSMatt Macy * __ZFS_POOL_RESTRICT - iterate only over the pools it lists
316eda14cbcSMatt Macy *
317eda14cbcSMatt Macy * This function returns B_TRUE if the pool should be skipped
318eda14cbcSMatt Macy * during iteration.
319eda14cbcSMatt Macy */
320eda14cbcSMatt Macy boolean_t
zpool_skip_pool(const char * poolname)321eda14cbcSMatt Macy zpool_skip_pool(const char *poolname)
322eda14cbcSMatt Macy {
323eda14cbcSMatt Macy static boolean_t initialized = B_FALSE;
324eda14cbcSMatt Macy static const char *exclude = NULL;
325eda14cbcSMatt Macy static const char *restricted = NULL;
326eda14cbcSMatt Macy
327eda14cbcSMatt Macy const char *cur, *end;
328eda14cbcSMatt Macy int len;
329eda14cbcSMatt Macy int namelen = strlen(poolname);
330eda14cbcSMatt Macy
331eda14cbcSMatt Macy if (!initialized) {
332eda14cbcSMatt Macy initialized = B_TRUE;
333eda14cbcSMatt Macy exclude = getenv("__ZFS_POOL_EXCLUDE");
334eda14cbcSMatt Macy restricted = getenv("__ZFS_POOL_RESTRICT");
335eda14cbcSMatt Macy }
336eda14cbcSMatt Macy
337eda14cbcSMatt Macy if (exclude != NULL) {
338eda14cbcSMatt Macy cur = exclude;
339eda14cbcSMatt Macy do {
340eda14cbcSMatt Macy end = strchr(cur, ' ');
341eda14cbcSMatt Macy len = (NULL == end) ? strlen(cur) : (end - cur);
342eda14cbcSMatt Macy if (len == namelen && 0 == strncmp(cur, poolname, len))
343eda14cbcSMatt Macy return (B_TRUE);
344eda14cbcSMatt Macy cur += (len + 1);
345eda14cbcSMatt Macy } while (NULL != end);
346eda14cbcSMatt Macy }
347eda14cbcSMatt Macy
348eda14cbcSMatt Macy if (NULL == restricted)
349eda14cbcSMatt Macy return (B_FALSE);
350eda14cbcSMatt Macy
351eda14cbcSMatt Macy cur = restricted;
352eda14cbcSMatt Macy do {
353eda14cbcSMatt Macy end = strchr(cur, ' ');
354eda14cbcSMatt Macy len = (NULL == end) ? strlen(cur) : (end - cur);
355eda14cbcSMatt Macy
356eda14cbcSMatt Macy if (len == namelen && 0 == strncmp(cur, poolname, len)) {
357eda14cbcSMatt Macy return (B_FALSE);
358eda14cbcSMatt Macy }
359eda14cbcSMatt Macy
360eda14cbcSMatt Macy cur += (len + 1);
361eda14cbcSMatt Macy } while (NULL != end);
362eda14cbcSMatt Macy
363eda14cbcSMatt Macy return (B_TRUE);
364eda14cbcSMatt Macy }
365eda14cbcSMatt Macy
366eda14cbcSMatt Macy /*
367eda14cbcSMatt Macy * Iterate over all pools in the system.
368eda14cbcSMatt Macy */
369eda14cbcSMatt Macy int
zpool_iter(libzfs_handle_t * hdl,zpool_iter_f func,void * data)370eda14cbcSMatt Macy zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
371eda14cbcSMatt Macy {
372eda14cbcSMatt Macy config_node_t *cn;
373eda14cbcSMatt Macy zpool_handle_t *zhp;
374eda14cbcSMatt Macy int ret;
375eda14cbcSMatt Macy
376eda14cbcSMatt Macy /*
377eda14cbcSMatt Macy * If someone makes a recursive call to zpool_iter(), we want to avoid
378eda14cbcSMatt Macy * refreshing the namespace because that will invalidate the parent
379eda14cbcSMatt Macy * context. We allow recursive calls, but simply re-use the same
380eda14cbcSMatt Macy * namespace AVL tree.
381eda14cbcSMatt Macy */
382eda14cbcSMatt Macy if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
383eda14cbcSMatt Macy return (-1);
384eda14cbcSMatt Macy
385eda14cbcSMatt Macy hdl->libzfs_pool_iter++;
386eda14cbcSMatt Macy for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
387eda14cbcSMatt Macy cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
388eda14cbcSMatt Macy
389eda14cbcSMatt Macy if (zpool_skip_pool(cn->cn_name))
390eda14cbcSMatt Macy continue;
391eda14cbcSMatt Macy
392eda14cbcSMatt Macy if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
393eda14cbcSMatt Macy hdl->libzfs_pool_iter--;
394eda14cbcSMatt Macy return (-1);
395eda14cbcSMatt Macy }
396eda14cbcSMatt Macy
397eda14cbcSMatt Macy if (zhp == NULL)
398eda14cbcSMatt Macy continue;
399eda14cbcSMatt Macy
400eda14cbcSMatt Macy if ((ret = func(zhp, data)) != 0) {
401eda14cbcSMatt Macy hdl->libzfs_pool_iter--;
402eda14cbcSMatt Macy return (ret);
403eda14cbcSMatt Macy }
404eda14cbcSMatt Macy }
405eda14cbcSMatt Macy hdl->libzfs_pool_iter--;
406eda14cbcSMatt Macy
407eda14cbcSMatt Macy return (0);
408eda14cbcSMatt Macy }
409eda14cbcSMatt Macy
410eda14cbcSMatt Macy /*
411eda14cbcSMatt Macy * Iterate over root datasets, calling the given function for each. The zfs
412eda14cbcSMatt Macy * handle passed each time must be explicitly closed by the callback.
413eda14cbcSMatt Macy */
414eda14cbcSMatt Macy int
zfs_iter_root(libzfs_handle_t * hdl,zfs_iter_f func,void * data)415eda14cbcSMatt Macy zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
416eda14cbcSMatt Macy {
417eda14cbcSMatt Macy config_node_t *cn;
418eda14cbcSMatt Macy zfs_handle_t *zhp;
419eda14cbcSMatt Macy int ret;
420eda14cbcSMatt Macy
421eda14cbcSMatt Macy if (namespace_reload(hdl) != 0)
422eda14cbcSMatt Macy return (-1);
423eda14cbcSMatt Macy
424eda14cbcSMatt Macy for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
425eda14cbcSMatt Macy cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
426eda14cbcSMatt Macy
427eda14cbcSMatt Macy if (zpool_skip_pool(cn->cn_name))
428eda14cbcSMatt Macy continue;
429eda14cbcSMatt Macy
430eda14cbcSMatt Macy if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
431eda14cbcSMatt Macy continue;
432eda14cbcSMatt Macy
433eda14cbcSMatt Macy if ((ret = func(zhp, data)) != 0)
434eda14cbcSMatt Macy return (ret);
435eda14cbcSMatt Macy }
436eda14cbcSMatt Macy
437eda14cbcSMatt Macy return (0);
438eda14cbcSMatt Macy }
439