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 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 2012 by Delphix. All rights reserved. 29 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 30 */ 31 32 /* 33 * The pool configuration repository is stored in /etc/zfs/zpool.cache as a 34 * single packed nvlist. While it would be nice to just read in this 35 * file from userland, this wouldn't work from a local zone. So we have to have 36 * a zpool ioctl to return the complete configuration for all pools. In the 37 * global zone, this will be identical to reading the file and unpacking it in 38 * userland. 39 */ 40 41 #include <errno.h> 42 #include <sys/stat.h> 43 #include <fcntl.h> 44 #include <stddef.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <libintl.h> 48 #include <libuutil.h> 49 50 #include "libzfs_impl.h" 51 52 typedef struct config_node { 53 char *cn_name; 54 nvlist_t *cn_config; 55 uu_avl_node_t cn_avl; 56 } config_node_t; 57 58 /* ARGSUSED */ 59 static int 60 config_node_compare(const void *a, const void *b, void *unused) 61 { 62 int ret; 63 64 const config_node_t *ca = (config_node_t *)a; 65 const config_node_t *cb = (config_node_t *)b; 66 67 ret = strcmp(ca->cn_name, cb->cn_name); 68 69 if (ret < 0) 70 return (-1); 71 else if (ret > 0) 72 return (1); 73 else 74 return (0); 75 } 76 77 void 78 namespace_clear(libzfs_handle_t *hdl) 79 { 80 if (hdl->libzfs_ns_avl) { 81 config_node_t *cn; 82 void *cookie = NULL; 83 84 while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, 85 &cookie)) != NULL) { 86 nvlist_free(cn->cn_config); 87 free(cn->cn_name); 88 free(cn); 89 } 90 91 uu_avl_destroy(hdl->libzfs_ns_avl); 92 hdl->libzfs_ns_avl = NULL; 93 } 94 95 if (hdl->libzfs_ns_avlpool) { 96 uu_avl_pool_destroy(hdl->libzfs_ns_avlpool); 97 hdl->libzfs_ns_avlpool = NULL; 98 } 99 } 100 101 /* 102 * Loads the pool namespace, or re-loads it if the cache has changed. 103 */ 104 static int 105 namespace_reload(libzfs_handle_t *hdl) 106 { 107 nvlist_t *config; 108 config_node_t *cn; 109 nvpair_t *elem; 110 zfs_cmd_t zc = { 0 }; 111 void *cookie; 112 113 if (hdl->libzfs_ns_gen == 0) { 114 /* 115 * This is the first time we've accessed the configuration 116 * cache. Initialize the AVL tree and then fall through to the 117 * common code. 118 */ 119 if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool", 120 sizeof (config_node_t), 121 offsetof(config_node_t, cn_avl), 122 config_node_compare, UU_DEFAULT)) == NULL) 123 return (no_memory(hdl)); 124 125 if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool, 126 NULL, UU_DEFAULT)) == NULL) 127 return (no_memory(hdl)); 128 } 129 130 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 131 return (-1); 132 133 for (;;) { 134 zc.zc_cookie = hdl->libzfs_ns_gen; 135 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CONFIGS, &zc) != 0) { 136 switch (errno) { 137 case EEXIST: 138 /* 139 * The namespace hasn't changed. 140 */ 141 zcmd_free_nvlists(&zc); 142 return (0); 143 144 case ENOMEM: 145 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 146 zcmd_free_nvlists(&zc); 147 return (-1); 148 } 149 break; 150 151 default: 152 zcmd_free_nvlists(&zc); 153 return (zfs_standard_error(hdl, errno, 154 dgettext(TEXT_DOMAIN, "failed to read " 155 "pool configuration"))); 156 } 157 } else { 158 hdl->libzfs_ns_gen = zc.zc_cookie; 159 break; 160 } 161 } 162 163 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) { 164 zcmd_free_nvlists(&zc); 165 return (-1); 166 } 167 168 zcmd_free_nvlists(&zc); 169 170 /* 171 * Clear out any existing configuration information, and recreate 172 * the AVL tree. 173 */ 174 cookie = NULL; 175 while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, &cookie)) != NULL) { 176 nvlist_free(cn->cn_config); 177 free(cn->cn_name); 178 free(cn); 179 } 180 181 uu_avl_recreate(hdl->libzfs_ns_avl); 182 183 elem = NULL; 184 while ((elem = nvlist_next_nvpair(config, elem)) != NULL) { 185 nvlist_t *child; 186 uu_avl_index_t where; 187 188 if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) { 189 nvlist_free(config); 190 return (-1); 191 } 192 193 if ((cn->cn_name = zfs_strdup(hdl, 194 nvpair_name(elem))) == NULL) { 195 free(cn); 196 nvlist_free(config); 197 return (-1); 198 } 199 200 verify(nvpair_value_nvlist(elem, &child) == 0); 201 if (nvlist_dup(child, &cn->cn_config, 0) != 0) { 202 free(cn->cn_name); 203 free(cn); 204 nvlist_free(config); 205 return (no_memory(hdl)); 206 } 207 verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where) 208 == NULL); 209 210 uu_avl_insert(hdl->libzfs_ns_avl, cn, where); 211 } 212 213 nvlist_free(config); 214 return (0); 215 } 216 217 /* 218 * Retrieve the configuration for the given pool. The configuration is a nvlist 219 * describing the vdevs, as well as the statistics associated with each one. 220 */ 221 nvlist_t * 222 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig) 223 { 224 if (oldconfig) 225 *oldconfig = zhp->zpool_old_config; 226 return (zhp->zpool_config); 227 } 228 229 /* 230 * Retrieves a list of enabled features and their refcounts and caches it in 231 * the pool handle. 232 */ 233 nvlist_t * 234 zpool_get_features(zpool_handle_t *zhp) 235 { 236 nvlist_t *config, *features; 237 238 config = zpool_get_config(zhp, NULL); 239 240 if (config == NULL || !nvlist_exists(config, 241 ZPOOL_CONFIG_FEATURE_STATS)) { 242 int error; 243 boolean_t missing = B_FALSE; 244 245 error = zpool_refresh_stats(zhp, &missing); 246 247 if (error != 0 || missing) 248 return (NULL); 249 250 config = zpool_get_config(zhp, NULL); 251 } 252 253 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 254 &features) == 0); 255 256 return (features); 257 } 258 259 /* 260 * Refresh the vdev statistics associated with the given pool. This is used in 261 * iostat to show configuration changes and determine the delta from the last 262 * time the function was called. This function can fail, in case the pool has 263 * been destroyed. 264 */ 265 int 266 zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing) 267 { 268 zfs_cmd_t zc = { 0 }; 269 int error; 270 nvlist_t *config; 271 libzfs_handle_t *hdl = zhp->zpool_hdl; 272 273 *missing = B_FALSE; 274 (void) strcpy(zc.zc_name, zhp->zpool_name); 275 276 if (zhp->zpool_config_size == 0) 277 zhp->zpool_config_size = 1 << 16; 278 279 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0) 280 return (-1); 281 282 for (;;) { 283 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_STATS, 284 &zc) == 0) { 285 /* 286 * The real error is returned in the zc_cookie field. 287 */ 288 error = zc.zc_cookie; 289 break; 290 } 291 292 if (errno == ENOMEM) { 293 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 294 zcmd_free_nvlists(&zc); 295 return (-1); 296 } 297 } else { 298 zcmd_free_nvlists(&zc); 299 if (errno == ENOENT || errno == EINVAL) 300 *missing = B_TRUE; 301 zhp->zpool_state = POOL_STATE_UNAVAIL; 302 return (0); 303 } 304 } 305 306 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) { 307 zcmd_free_nvlists(&zc); 308 return (-1); 309 } 310 311 zcmd_free_nvlists(&zc); 312 313 zhp->zpool_config_size = zc.zc_nvlist_dst_size; 314 315 if (zhp->zpool_config != NULL) { 316 uint64_t oldtxg, newtxg; 317 318 verify(nvlist_lookup_uint64(zhp->zpool_config, 319 ZPOOL_CONFIG_POOL_TXG, &oldtxg) == 0); 320 verify(nvlist_lookup_uint64(config, 321 ZPOOL_CONFIG_POOL_TXG, &newtxg) == 0); 322 323 if (zhp->zpool_old_config != NULL) 324 nvlist_free(zhp->zpool_old_config); 325 326 if (oldtxg != newtxg) { 327 nvlist_free(zhp->zpool_config); 328 zhp->zpool_old_config = NULL; 329 } else { 330 zhp->zpool_old_config = zhp->zpool_config; 331 } 332 } 333 334 zhp->zpool_config = config; 335 if (error) 336 zhp->zpool_state = POOL_STATE_UNAVAIL; 337 else 338 zhp->zpool_state = POOL_STATE_ACTIVE; 339 340 return (0); 341 } 342 343 /* 344 * If the __ZFS_POOL_RESTRICT environment variable is set we only iterate over 345 * pools it lists. 346 * 347 * This is an undocumented feature for use during testing only. 348 * 349 * This function returns B_TRUE if the pool should be skipped 350 * during iteration. 351 */ 352 static boolean_t 353 check_restricted(const char *poolname) 354 { 355 static boolean_t initialized = B_FALSE; 356 static char *restricted = NULL; 357 358 const char *cur, *end; 359 int len, namelen; 360 361 if (!initialized) { 362 initialized = B_TRUE; 363 restricted = getenv("__ZFS_POOL_RESTRICT"); 364 } 365 366 if (NULL == restricted) 367 return (B_FALSE); 368 369 cur = restricted; 370 namelen = strlen(poolname); 371 do { 372 end = strchr(cur, ' '); 373 len = (NULL == end) ? strlen(cur) : (end - cur); 374 375 if (len == namelen && 0 == strncmp(cur, poolname, len)) { 376 return (B_FALSE); 377 } 378 379 cur += (len + 1); 380 } while (NULL != end); 381 382 return (B_TRUE); 383 } 384 385 /* 386 * Iterate over all pools in the system. 387 */ 388 int 389 zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data) 390 { 391 config_node_t *cn; 392 zpool_handle_t *zhp; 393 int ret; 394 395 /* 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. 400 */ 401 if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0) 402 return (-1); 403 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)) { 407 408 if (check_restricted(cn->cn_name)) 409 continue; 410 411 if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) { 412 hdl->libzfs_pool_iter--; 413 return (-1); 414 } 415 416 if (zhp == NULL) 417 continue; 418 419 if ((ret = func(zhp, data)) != 0) { 420 hdl->libzfs_pool_iter--; 421 return (ret); 422 } 423 } 424 hdl->libzfs_pool_iter--; 425 426 return (0); 427 } 428 429 /* 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. 432 */ 433 int 434 zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data) 435 { 436 config_node_t *cn; 437 zfs_handle_t *zhp; 438 int ret; 439 440 if (namespace_reload(hdl) != 0) 441 return (-1); 442 443 for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL; 444 cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) { 445 446 if (check_restricted(cn->cn_name)) 447 continue; 448 449 if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL) 450 continue; 451 452 if ((ret = func(zhp, data)) != 0) 453 return (ret); 454 } 455 456 return (0); 457 }