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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * Copyright (c) 2015 by Syneto S.R.L. All rights reserved.
30 * Copyright 2016 Nexenta Systems, Inc.
34 * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
35 * single packed nvlist. While it would be nice to just read in this
36 * file from userland, this wouldn't work from a local zone. So we have to have
37 * a zpool ioctl to return the complete configuration for all pools. In the
38 * global zone, this will be identical to reading the file and unpacking it in
51 #include "libzfs_impl.h"
53 typedef struct config_node {
61 config_node_compare(const void *a, const void *b, void *unused)
65 const config_node_t *ca = (config_node_t *)a;
66 const config_node_t *cb = (config_node_t *)b;
68 ret = strcmp(ca->cn_name, cb->cn_name);
79 namespace_clear(libzfs_handle_t *hdl)
81 if (hdl->libzfs_ns_avl) {
85 while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl,
87 nvlist_free(cn->cn_config);
92 uu_avl_destroy(hdl->libzfs_ns_avl);
93 hdl->libzfs_ns_avl = NULL;
96 if (hdl->libzfs_ns_avlpool) {
97 uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
98 hdl->libzfs_ns_avlpool = NULL;
103 * Loads the pool namespace, or re-loads it if the cache has changed.
106 namespace_reload(libzfs_handle_t *hdl)
111 zfs_cmd_t zc = {"\0"};
114 if (hdl->libzfs_ns_gen == 0) {
116 * This is the first time we've accessed the configuration
117 * cache. Initialize the AVL tree and then fall through to the
120 if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
121 sizeof (config_node_t),
122 offsetof(config_node_t, cn_avl),
123 config_node_compare, UU_DEFAULT)) == NULL)
124 return (no_memory(hdl));
126 if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
127 NULL, UU_DEFAULT)) == NULL)
128 return (no_memory(hdl));
131 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
135 zc.zc_cookie = hdl->libzfs_ns_gen;
136 if (zfs_ioctl(hdl, ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
140 * The namespace hasn't changed.
142 zcmd_free_nvlists(&zc);
146 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
147 zcmd_free_nvlists(&zc);
153 zcmd_free_nvlists(&zc);
154 return (zfs_standard_error(hdl, errno,
155 dgettext(TEXT_DOMAIN, "failed to read "
156 "pool configuration")));
159 hdl->libzfs_ns_gen = zc.zc_cookie;
164 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
165 zcmd_free_nvlists(&zc);
169 zcmd_free_nvlists(&zc);
172 * Clear out any existing configuration information.
175 while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, &cookie)) != NULL) {
176 nvlist_free(cn->cn_config);
182 while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
184 uu_avl_index_t where;
186 if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) {
191 if ((cn->cn_name = zfs_strdup(hdl,
192 nvpair_name(elem))) == NULL) {
198 verify(nvpair_value_nvlist(elem, &child) == 0);
199 if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
203 return (no_memory(hdl));
205 verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
208 uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
216 * Retrieve the configuration for the given pool. The configuration is an nvlist
217 * describing the vdevs, as well as the statistics associated with each one.
220 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
223 *oldconfig = zhp->zpool_old_config;
224 return (zhp->zpool_config);
228 * Retrieves a list of enabled features and their refcounts and caches it in
232 zpool_get_features(zpool_handle_t *zhp)
234 nvlist_t *config, *features;
236 config = zpool_get_config(zhp, NULL);
238 if (config == NULL || !nvlist_exists(config,
239 ZPOOL_CONFIG_FEATURE_STATS)) {
241 boolean_t missing = B_FALSE;
243 error = zpool_refresh_stats(zhp, &missing);
245 if (error != 0 || missing)
248 config = zpool_get_config(zhp, NULL);
251 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
259 * Refresh the vdev statistics associated with the given pool. This is used in
260 * iostat to show configuration changes and determine the delta from the last
261 * time the function was called. This function can fail, in case the pool has
265 zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
267 zfs_cmd_t zc = {"\0"};
270 libzfs_handle_t *hdl = zhp->zpool_hdl;
273 (void) strcpy(zc.zc_name, zhp->zpool_name);
275 if (zhp->zpool_config_size == 0)
276 zhp->zpool_config_size = 1 << 16;
278 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0)
282 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_STATS,
285 * The real error is returned in the zc_cookie field.
287 error = zc.zc_cookie;
291 if (errno == ENOMEM) {
292 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
293 zcmd_free_nvlists(&zc);
297 zcmd_free_nvlists(&zc);
298 if (errno == ENOENT || errno == EINVAL)
300 zhp->zpool_state = POOL_STATE_UNAVAIL;
305 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
306 zcmd_free_nvlists(&zc);
310 zcmd_free_nvlists(&zc);
312 zhp->zpool_config_size = zc.zc_nvlist_dst_size;
314 if (zhp->zpool_config != NULL) {
315 nvlist_free(zhp->zpool_old_config);
317 zhp->zpool_old_config = zhp->zpool_config;
320 zhp->zpool_config = config;
322 zhp->zpool_state = POOL_STATE_UNAVAIL;
324 zhp->zpool_state = POOL_STATE_ACTIVE;
330 * The following environment variables are undocumented
331 * and should be used for testing purposes only:
333 * __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
334 * __ZFS_POOL_RESTRICT - iterate only over the pools it lists
336 * This function returns B_TRUE if the pool should be skipped
340 zpool_skip_pool(const char *poolname)
342 static boolean_t initialized = B_FALSE;
343 static const char *exclude = NULL;
344 static const char *restricted = NULL;
346 const char *cur, *end;
348 int namelen = strlen(poolname);
351 initialized = B_TRUE;
352 exclude = getenv("__ZFS_POOL_EXCLUDE");
353 restricted = getenv("__ZFS_POOL_RESTRICT");
356 if (exclude != NULL) {
359 end = strchr(cur, ' ');
360 len = (NULL == end) ? strlen(cur) : (end - cur);
361 if (len == namelen && 0 == strncmp(cur, poolname, len))
364 } while (NULL != end);
367 if (NULL == restricted)
372 end = strchr(cur, ' ');
373 len = (NULL == end) ? strlen(cur) : (end - cur);
375 if (len == namelen && 0 == strncmp(cur, poolname, len)) {
380 } while (NULL != end);
386 * Iterate over all pools in the system.
389 zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
396 * If someone makes a recursive call to zpool_iter(), we want to avoid
397 * refreshing the namespace because that will invalidate the parent
398 * context. We allow recursive calls, but simply re-use the same
399 * namespace AVL tree.
401 if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
404 hdl->libzfs_pool_iter++;
405 for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
406 cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
408 if (zpool_skip_pool(cn->cn_name))
411 if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
412 hdl->libzfs_pool_iter--;
419 if ((ret = func(zhp, data)) != 0) {
420 hdl->libzfs_pool_iter--;
424 hdl->libzfs_pool_iter--;
430 * Iterate over root datasets, calling the given function for each. The zfs
431 * handle passed each time must be explicitly closed by the callback.
434 zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
440 if (namespace_reload(hdl) != 0)
443 for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
444 cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
446 if (zpool_skip_pool(cn->cn_name))
449 if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
452 if ((ret = func(zhp, data)) != 0)