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