1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * 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 */
21ad135b5dSChristopher Siden
22fa9e4066Sahrens /*
23069f55e2SEric Schrock * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24fa9e4066Sahrens * Use is subject to license terms.
25fa9e4066Sahrens */
26fa9e4066Sahrens
27fa9e4066Sahrens /*
28ad135b5dSChristopher Siden * Copyright (c) 2012 by Delphix. All rights reserved.
29b289d045SDan Vatca * Copyright (c) 2015 by Syneto S.R.L. All rights reserved.
30*8808ac5dSYuri Pankov * Copyright 2016 Nexenta Systems, Inc.
31ad135b5dSChristopher Siden */
32ad135b5dSChristopher Siden
33ad135b5dSChristopher Siden /*
34fa9e4066Sahrens * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
35fa9e4066Sahrens * single packed nvlist. While it would be nice to just read in this
36fa9e4066Sahrens * file from userland, this wouldn't work from a local zone. So we have to have
37fa9e4066Sahrens * a zpool ioctl to return the complete configuration for all pools. In the
38fa9e4066Sahrens * global zone, this will be identical to reading the file and unpacking it in
39fa9e4066Sahrens * userland.
40fa9e4066Sahrens */
41fa9e4066Sahrens
42fa9e4066Sahrens #include <errno.h>
43fa9e4066Sahrens #include <sys/stat.h>
44fa9e4066Sahrens #include <fcntl.h>
45fa9e4066Sahrens #include <stddef.h>
46fa9e4066Sahrens #include <string.h>
47fa9e4066Sahrens #include <unistd.h>
48fa9e4066Sahrens #include <libintl.h>
49fa9e4066Sahrens #include <libuutil.h>
50fa9e4066Sahrens
51fa9e4066Sahrens #include "libzfs_impl.h"
52fa9e4066Sahrens
53fa9e4066Sahrens typedef struct config_node {
54fa9e4066Sahrens char *cn_name;
55fa9e4066Sahrens nvlist_t *cn_config;
56fa9e4066Sahrens uu_avl_node_t cn_avl;
57fa9e4066Sahrens } config_node_t;
58fa9e4066Sahrens
59fa9e4066Sahrens /* ARGSUSED */
60fa9e4066Sahrens static int
config_node_compare(const void * a,const void * b,void * unused)61fa9e4066Sahrens config_node_compare(const void *a, const void *b, void *unused)
62fa9e4066Sahrens {
63fa9e4066Sahrens int ret;
64fa9e4066Sahrens
65fa9e4066Sahrens const config_node_t *ca = (config_node_t *)a;
66fa9e4066Sahrens const config_node_t *cb = (config_node_t *)b;
67fa9e4066Sahrens
68fa9e4066Sahrens ret = strcmp(ca->cn_name, cb->cn_name);
69fa9e4066Sahrens
70fa9e4066Sahrens if (ret < 0)
71fa9e4066Sahrens return (-1);
72fa9e4066Sahrens else if (ret > 0)
73fa9e4066Sahrens return (1);
74fa9e4066Sahrens else
75fa9e4066Sahrens return (0);
76fa9e4066Sahrens }
77fa9e4066Sahrens
7899653d4eSeschrock void
namespace_clear(libzfs_handle_t * hdl)7999653d4eSeschrock namespace_clear(libzfs_handle_t *hdl)
8099653d4eSeschrock {
8199653d4eSeschrock if (hdl->libzfs_ns_avl) {
8299653d4eSeschrock config_node_t *cn;
8368f944b7Seschrock void *cookie = NULL;
8499653d4eSeschrock
8568f944b7Seschrock while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl,
8668f944b7Seschrock &cookie)) != NULL) {
8799653d4eSeschrock nvlist_free(cn->cn_config);
8899653d4eSeschrock free(cn->cn_name);
8999653d4eSeschrock free(cn);
9099653d4eSeschrock }
9199653d4eSeschrock
9299653d4eSeschrock uu_avl_destroy(hdl->libzfs_ns_avl);
9399653d4eSeschrock hdl->libzfs_ns_avl = NULL;
9499653d4eSeschrock }
9599653d4eSeschrock
9699653d4eSeschrock if (hdl->libzfs_ns_avlpool) {
9799653d4eSeschrock uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
9899653d4eSeschrock hdl->libzfs_ns_avlpool = NULL;
9999653d4eSeschrock }
10099653d4eSeschrock }
10199653d4eSeschrock
102fa9e4066Sahrens /*
103fa9e4066Sahrens * Loads the pool namespace, or re-loads it if the cache has changed.
104fa9e4066Sahrens */
10599653d4eSeschrock static int
namespace_reload(libzfs_handle_t * hdl)10699653d4eSeschrock namespace_reload(libzfs_handle_t *hdl)
107fa9e4066Sahrens {
108fa9e4066Sahrens nvlist_t *config;
109fa9e4066Sahrens config_node_t *cn;
110fa9e4066Sahrens nvpair_t *elem;
111fa9e4066Sahrens zfs_cmd_t zc = { 0 };
11268f944b7Seschrock void *cookie;
113fa9e4066Sahrens
11499653d4eSeschrock if (hdl->libzfs_ns_gen == 0) {
115fa9e4066Sahrens /*
116fa9e4066Sahrens * This is the first time we've accessed the configuration
117fa9e4066Sahrens * cache. Initialize the AVL tree and then fall through to the
118fa9e4066Sahrens * common code.
119fa9e4066Sahrens */
12099653d4eSeschrock if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
121fa9e4066Sahrens sizeof (config_node_t),
122fa9e4066Sahrens offsetof(config_node_t, cn_avl),
123fa9e4066Sahrens config_node_compare, UU_DEFAULT)) == NULL)
12499653d4eSeschrock return (no_memory(hdl));
125fa9e4066Sahrens
12699653d4eSeschrock if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
12799653d4eSeschrock NULL, UU_DEFAULT)) == NULL)
12899653d4eSeschrock return (no_memory(hdl));
129fa9e4066Sahrens }
130fa9e4066Sahrens
131e9dbad6fSeschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
13299653d4eSeschrock return (-1);
133e9dbad6fSeschrock
134fa9e4066Sahrens for (;;) {
13599653d4eSeschrock zc.zc_cookie = hdl->libzfs_ns_gen;
13699653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
137fa9e4066Sahrens switch (errno) {
138fa9e4066Sahrens case EEXIST:
139fa9e4066Sahrens /*
140fa9e4066Sahrens * The namespace hasn't changed.
141fa9e4066Sahrens */
142e9dbad6fSeschrock zcmd_free_nvlists(&zc);
14399653d4eSeschrock return (0);
144fa9e4066Sahrens
145fa9e4066Sahrens case ENOMEM:
146e9dbad6fSeschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
147e9dbad6fSeschrock zcmd_free_nvlists(&zc);
14899653d4eSeschrock return (-1);
149e9dbad6fSeschrock }
150fa9e4066Sahrens break;
151fa9e4066Sahrens
152fa9e4066Sahrens default:
153e9dbad6fSeschrock zcmd_free_nvlists(&zc);
15499653d4eSeschrock return (zfs_standard_error(hdl, errno,
15599653d4eSeschrock dgettext(TEXT_DOMAIN, "failed to read "
15699653d4eSeschrock "pool configuration")));
157fa9e4066Sahrens }
158fa9e4066Sahrens } else {
15999653d4eSeschrock hdl->libzfs_ns_gen = zc.zc_cookie;
160fa9e4066Sahrens break;
161fa9e4066Sahrens }
162fa9e4066Sahrens }
163fa9e4066Sahrens
164e9dbad6fSeschrock if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
165e9dbad6fSeschrock zcmd_free_nvlists(&zc);
166e9dbad6fSeschrock return (-1);
16794de1d4cSeschrock }
168fa9e4066Sahrens
169e9dbad6fSeschrock zcmd_free_nvlists(&zc);
170fa9e4066Sahrens
171fa9e4066Sahrens /*
172fa9e4066Sahrens * Clear out any existing configuration information.
173fa9e4066Sahrens */
17468f944b7Seschrock cookie = NULL;
17568f944b7Seschrock while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, &cookie)) != NULL) {
176fa9e4066Sahrens nvlist_free(cn->cn_config);
177fa9e4066Sahrens free(cn->cn_name);
178fa9e4066Sahrens free(cn);
179fa9e4066Sahrens }
180fa9e4066Sahrens
181fa9e4066Sahrens elem = NULL;
182fa9e4066Sahrens while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
183fa9e4066Sahrens nvlist_t *child;
184fa9e4066Sahrens uu_avl_index_t where;
185fa9e4066Sahrens
18699653d4eSeschrock if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) {
18799653d4eSeschrock nvlist_free(config);
18899653d4eSeschrock return (-1);
18999653d4eSeschrock }
19099653d4eSeschrock
19199653d4eSeschrock if ((cn->cn_name = zfs_strdup(hdl,
19299653d4eSeschrock nvpair_name(elem))) == NULL) {
19399653d4eSeschrock free(cn);
1943bb79becSeschrock nvlist_free(config);
19599653d4eSeschrock return (-1);
19699653d4eSeschrock }
197fa9e4066Sahrens
198fa9e4066Sahrens verify(nvpair_value_nvlist(elem, &child) == 0);
19999653d4eSeschrock if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
2003bb79becSeschrock free(cn->cn_name);
2013bb79becSeschrock free(cn);
20299653d4eSeschrock nvlist_free(config);
20399653d4eSeschrock return (no_memory(hdl));
20499653d4eSeschrock }
20599653d4eSeschrock verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
20699653d4eSeschrock == NULL);
207fa9e4066Sahrens
20899653d4eSeschrock uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
209fa9e4066Sahrens }
210fa9e4066Sahrens
211fa9e4066Sahrens nvlist_free(config);
21299653d4eSeschrock return (0);
213fa9e4066Sahrens }
214fa9e4066Sahrens
215fa9e4066Sahrens /*
21668f944b7Seschrock * Retrieve the configuration for the given pool. The configuration is a nvlist
217fa9e4066Sahrens * describing the vdevs, as well as the statistics associated with each one.
218fa9e4066Sahrens */
219fa9e4066Sahrens nvlist_t *
zpool_get_config(zpool_handle_t * zhp,nvlist_t ** oldconfig)220088e9d47Seschrock zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
221fa9e4066Sahrens {
222088e9d47Seschrock if (oldconfig)
223088e9d47Seschrock *oldconfig = zhp->zpool_old_config;
224fa9e4066Sahrens return (zhp->zpool_config);
225fa9e4066Sahrens }
226fa9e4066Sahrens
227fa9e4066Sahrens /*
228ad135b5dSChristopher Siden * Retrieves a list of enabled features and their refcounts and caches it in
229ad135b5dSChristopher Siden * the pool handle.
230ad135b5dSChristopher Siden */
231ad135b5dSChristopher Siden nvlist_t *
zpool_get_features(zpool_handle_t * zhp)232ad135b5dSChristopher Siden zpool_get_features(zpool_handle_t *zhp)
233ad135b5dSChristopher Siden {
234ad135b5dSChristopher Siden nvlist_t *config, *features;
235ad135b5dSChristopher Siden
236ad135b5dSChristopher Siden config = zpool_get_config(zhp, NULL);
237ad135b5dSChristopher Siden
238ad135b5dSChristopher Siden if (config == NULL || !nvlist_exists(config,
239ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURE_STATS)) {
240ad135b5dSChristopher Siden int error;
241ad135b5dSChristopher Siden boolean_t missing = B_FALSE;
242ad135b5dSChristopher Siden
243ad135b5dSChristopher Siden error = zpool_refresh_stats(zhp, &missing);
244ad135b5dSChristopher Siden
245ad135b5dSChristopher Siden if (error != 0 || missing)
246ad135b5dSChristopher Siden return (NULL);
247ad135b5dSChristopher Siden
248ad135b5dSChristopher Siden config = zpool_get_config(zhp, NULL);
249ad135b5dSChristopher Siden }
250ad135b5dSChristopher Siden
251b289d045SDan Vatca if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
252b289d045SDan Vatca &features) != 0)
253b289d045SDan Vatca return (NULL);
254ad135b5dSChristopher Siden
255ad135b5dSChristopher Siden return (features);
256ad135b5dSChristopher Siden }
257ad135b5dSChristopher Siden
258ad135b5dSChristopher Siden /*
259fa9e4066Sahrens * Refresh the vdev statistics associated with the given pool. This is used in
260fa9e4066Sahrens * iostat to show configuration changes and determine the delta from the last
261fa9e4066Sahrens * time the function was called. This function can fail, in case the pool has
262fa9e4066Sahrens * been destroyed.
263fa9e4066Sahrens */
264fa9e4066Sahrens int
zpool_refresh_stats(zpool_handle_t * zhp,boolean_t * missing)26594de1d4cSeschrock zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
266fa9e4066Sahrens {
267fa9e4066Sahrens zfs_cmd_t zc = { 0 };
268fa9e4066Sahrens int error;
269088e9d47Seschrock nvlist_t *config;
270e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
271fa9e4066Sahrens
27294de1d4cSeschrock *missing = B_FALSE;
273fa9e4066Sahrens (void) strcpy(zc.zc_name, zhp->zpool_name);
274fa9e4066Sahrens
275fa9e4066Sahrens if (zhp->zpool_config_size == 0)
276fa9e4066Sahrens zhp->zpool_config_size = 1 << 16;
277fa9e4066Sahrens
278e9dbad6fSeschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0)
27999653d4eSeschrock return (-1);
280fa9e4066Sahrens
281ea8dc4b6Seschrock for (;;) {
28299653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_STATS,
28399653d4eSeschrock &zc) == 0) {
284fa9e4066Sahrens /*
285ea8dc4b6Seschrock * The real error is returned in the zc_cookie field.
286fa9e4066Sahrens */
28794de1d4cSeschrock error = zc.zc_cookie;
288fa9e4066Sahrens break;
289fa9e4066Sahrens }
290fa9e4066Sahrens
291ea8dc4b6Seschrock if (errno == ENOMEM) {
292e9dbad6fSeschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
293e9dbad6fSeschrock zcmd_free_nvlists(&zc);
29499653d4eSeschrock return (-1);
295e9dbad6fSeschrock }
296ea8dc4b6Seschrock } else {
297e9dbad6fSeschrock zcmd_free_nvlists(&zc);
29894de1d4cSeschrock if (errno == ENOENT || errno == EINVAL)
29994de1d4cSeschrock *missing = B_TRUE;
30094de1d4cSeschrock zhp->zpool_state = POOL_STATE_UNAVAIL;
30194de1d4cSeschrock return (0);
302fa9e4066Sahrens }
303fa9e4066Sahrens }
304fa9e4066Sahrens
305e9dbad6fSeschrock if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
306e9dbad6fSeschrock zcmd_free_nvlists(&zc);
307e9dbad6fSeschrock return (-1);
30899653d4eSeschrock }
309fa9e4066Sahrens
310e9dbad6fSeschrock zcmd_free_nvlists(&zc);
311e9dbad6fSeschrock
312e9dbad6fSeschrock zhp->zpool_config_size = zc.zc_nvlist_dst_size;
313fa9e4066Sahrens
314088e9d47Seschrock if (zhp->zpool_config != NULL) {
315088e9d47Seschrock nvlist_free(zhp->zpool_old_config);
316088e9d47Seschrock
317088e9d47Seschrock zhp->zpool_old_config = zhp->zpool_config;
318088e9d47Seschrock }
319fa9e4066Sahrens
320088e9d47Seschrock zhp->zpool_config = config;
32194de1d4cSeschrock if (error)
32294de1d4cSeschrock zhp->zpool_state = POOL_STATE_UNAVAIL;
32394de1d4cSeschrock else
32494de1d4cSeschrock zhp->zpool_state = POOL_STATE_ACTIVE;
325fa9e4066Sahrens
32694de1d4cSeschrock return (0);
327fa9e4066Sahrens }
328fa9e4066Sahrens
329fa9e4066Sahrens /*
330*8808ac5dSYuri Pankov * The following environment variables are undocumented
331*8808ac5dSYuri Pankov * and should be used for testing purposes only:
33265fec9f6SChristopher Siden *
333*8808ac5dSYuri Pankov * __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
334*8808ac5dSYuri Pankov * __ZFS_POOL_RESTRICT - iterate only over the pools it lists
33565fec9f6SChristopher Siden *
33665fec9f6SChristopher Siden * This function returns B_TRUE if the pool should be skipped
33765fec9f6SChristopher Siden * during iteration.
33865fec9f6SChristopher Siden */
339*8808ac5dSYuri Pankov boolean_t
zpool_skip_pool(const char * poolname)340*8808ac5dSYuri Pankov zpool_skip_pool(const char *poolname)
34165fec9f6SChristopher Siden {
34265fec9f6SChristopher Siden static boolean_t initialized = B_FALSE;
343*8808ac5dSYuri Pankov static const char *exclude = NULL;
344*8808ac5dSYuri Pankov static const char *restricted = NULL;
34565fec9f6SChristopher Siden
34665fec9f6SChristopher Siden const char *cur, *end;
347*8808ac5dSYuri Pankov int len;
348*8808ac5dSYuri Pankov int namelen = strlen(poolname);
34965fec9f6SChristopher Siden
35065fec9f6SChristopher Siden if (!initialized) {
35165fec9f6SChristopher Siden initialized = B_TRUE;
352*8808ac5dSYuri Pankov exclude = getenv("__ZFS_POOL_EXCLUDE");
35365fec9f6SChristopher Siden restricted = getenv("__ZFS_POOL_RESTRICT");
35465fec9f6SChristopher Siden }
35565fec9f6SChristopher Siden
356*8808ac5dSYuri Pankov if (exclude != NULL) {
357*8808ac5dSYuri Pankov cur = exclude;
358*8808ac5dSYuri Pankov do {
359*8808ac5dSYuri Pankov end = strchr(cur, ' ');
360*8808ac5dSYuri Pankov len = (NULL == end) ? strlen(cur) : (end - cur);
361*8808ac5dSYuri Pankov if (len == namelen && 0 == strncmp(cur, poolname, len))
362*8808ac5dSYuri Pankov return (B_TRUE);
363*8808ac5dSYuri Pankov cur += (len + 1);
364*8808ac5dSYuri Pankov } while (NULL != end);
365*8808ac5dSYuri Pankov }
366*8808ac5dSYuri Pankov
36765fec9f6SChristopher Siden if (NULL == restricted)
36865fec9f6SChristopher Siden return (B_FALSE);
36965fec9f6SChristopher Siden
37065fec9f6SChristopher Siden cur = restricted;
37165fec9f6SChristopher Siden do {
37265fec9f6SChristopher Siden end = strchr(cur, ' ');
37365fec9f6SChristopher Siden len = (NULL == end) ? strlen(cur) : (end - cur);
37465fec9f6SChristopher Siden
37565fec9f6SChristopher Siden if (len == namelen && 0 == strncmp(cur, poolname, len)) {
37665fec9f6SChristopher Siden return (B_FALSE);
37765fec9f6SChristopher Siden }
37865fec9f6SChristopher Siden
37965fec9f6SChristopher Siden cur += (len + 1);
38065fec9f6SChristopher Siden } while (NULL != end);
38165fec9f6SChristopher Siden
38265fec9f6SChristopher Siden return (B_TRUE);
38365fec9f6SChristopher Siden }
38465fec9f6SChristopher Siden
38565fec9f6SChristopher Siden /*
386fa9e4066Sahrens * Iterate over all pools in the system.
387fa9e4066Sahrens */
388fa9e4066Sahrens int
zpool_iter(libzfs_handle_t * hdl,zpool_iter_f func,void * data)38999653d4eSeschrock zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
390fa9e4066Sahrens {
391fa9e4066Sahrens config_node_t *cn;
392fa9e4066Sahrens zpool_handle_t *zhp;
393fa9e4066Sahrens int ret;
394fa9e4066Sahrens
395069f55e2SEric Schrock /*
396069f55e2SEric Schrock * If someone makes a recursive call to zpool_iter(), we want to avoid
397069f55e2SEric Schrock * refreshing the namespace because that will invalidate the parent
398069f55e2SEric Schrock * context. We allow recursive calls, but simply re-use the same
399069f55e2SEric Schrock * namespace AVL tree.
400069f55e2SEric Schrock */
401069f55e2SEric Schrock if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
40299653d4eSeschrock return (-1);
403fa9e4066Sahrens
404069f55e2SEric Schrock hdl->libzfs_pool_iter++;
40599653d4eSeschrock for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
40699653d4eSeschrock cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
407fa9e4066Sahrens
408*8808ac5dSYuri Pankov if (zpool_skip_pool(cn->cn_name))
40965fec9f6SChristopher Siden continue;
41065fec9f6SChristopher Siden
411069f55e2SEric Schrock if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
412069f55e2SEric Schrock hdl->libzfs_pool_iter--;
41394de1d4cSeschrock return (-1);
414069f55e2SEric Schrock }
41594de1d4cSeschrock
41694de1d4cSeschrock if (zhp == NULL)
417fa9e4066Sahrens continue;
418fa9e4066Sahrens
419069f55e2SEric Schrock if ((ret = func(zhp, data)) != 0) {
420069f55e2SEric Schrock hdl->libzfs_pool_iter--;
421fa9e4066Sahrens return (ret);
422fa9e4066Sahrens }
423069f55e2SEric Schrock }
424069f55e2SEric Schrock hdl->libzfs_pool_iter--;
425fa9e4066Sahrens
426fa9e4066Sahrens return (0);
427fa9e4066Sahrens }
428fa9e4066Sahrens
429fa9e4066Sahrens /*
430fa9e4066Sahrens * Iterate over root datasets, calling the given function for each. The zfs
431fa9e4066Sahrens * handle passed each time must be explicitly closed by the callback.
432fa9e4066Sahrens */
433fa9e4066Sahrens int
zfs_iter_root(libzfs_handle_t * hdl,zfs_iter_f func,void * data)43499653d4eSeschrock zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
435fa9e4066Sahrens {
436fa9e4066Sahrens config_node_t *cn;
437fa9e4066Sahrens zfs_handle_t *zhp;
438fa9e4066Sahrens int ret;
439fa9e4066Sahrens
44099653d4eSeschrock if (namespace_reload(hdl) != 0)
44199653d4eSeschrock return (-1);
442fa9e4066Sahrens
44399653d4eSeschrock for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
44499653d4eSeschrock cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
445fa9e4066Sahrens
446*8808ac5dSYuri Pankov if (zpool_skip_pool(cn->cn_name))
44765fec9f6SChristopher Siden continue;
44865fec9f6SChristopher Siden
44999653d4eSeschrock if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
450fa9e4066Sahrens continue;
451fa9e4066Sahrens
452fa9e4066Sahrens if ((ret = func(zhp, data)) != 0)
453fa9e4066Sahrens return (ret);
454fa9e4066Sahrens }
455fa9e4066Sahrens
456fa9e4066Sahrens return (0);
457fa9e4066Sahrens }
458