Home | History | Annotate | Line # | Download | only in common
      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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     24  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
     25  * Copyright 2015 RackTop Systems.
     26  * Copyright 2016 Nexenta Systems, Inc.
     27  */
     28 
     29 /*
     30  * Pool import support functions.
     31  *
     32  * To import a pool, we rely on reading the configuration information from the
     33  * ZFS label of each device.  If we successfully read the label, then we
     34  * organize the configuration information in the following hierarchy:
     35  *
     36  * 	pool guid -> toplevel vdev guid -> label txg
     37  *
     38  * Duplicate entries matching this same tuple will be discarded.  Once we have
     39  * examined every device, we pick the best label txg config for each toplevel
     40  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
     41  * update any paths that have changed.  Finally, we attempt to import the pool
     42  * using our derived config, and record the results.
     43  */
     44 
     45 #include <ctype.h>
     46 #include <devid.h>
     47 #include <dirent.h>
     48 #include <errno.h>
     49 #include <libintl.h>
     50 #include <stddef.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <sys/stat.h>
     54 #include <sys/ioctl.h>
     55 #include <unistd.h>
     56 #include <fcntl.h>
     57 #include <thread_pool.h>
     58 #ifdef __FreeBSD__
     59 #include <libgeom.h>
     60 #endif
     61 #ifdef __NetBSD__
     62 #include <util.h>
     63 static int native_ioctl(int fd, unsigned long cmd, void *arg);
     64 #endif
     65 
     66 #include <sys/vdev_impl.h>
     67 
     68 #include "libzfs.h"
     69 #include "libzfs_impl.h"
     70 
     71 /*
     72  * Intermediate structures used to gather configuration information.
     73  */
     74 typedef struct config_entry {
     75 	uint64_t		ce_txg;
     76 	nvlist_t		*ce_config;
     77 	struct config_entry	*ce_next;
     78 } config_entry_t;
     79 
     80 typedef struct vdev_entry {
     81 	uint64_t		ve_guid;
     82 	config_entry_t		*ve_configs;
     83 	struct vdev_entry	*ve_next;
     84 } vdev_entry_t;
     85 
     86 typedef struct pool_entry {
     87 	uint64_t		pe_guid;
     88 	vdev_entry_t		*pe_vdevs;
     89 	struct pool_entry	*pe_next;
     90 } pool_entry_t;
     91 
     92 typedef struct name_entry {
     93 	char			*ne_name;
     94 	uint64_t		ne_guid;
     95 	struct name_entry	*ne_next;
     96 } name_entry_t;
     97 
     98 typedef struct pool_list {
     99 	pool_entry_t		*pools;
    100 	name_entry_t		*names;
    101 } pool_list_t;
    102 
    103 static char *
    104 get_devid(const char *path)
    105 {
    106 #ifdef have_devid
    107 	int fd;
    108 	ddi_devid_t devid;
    109 	char *minor, *ret;
    110 
    111 	if ((fd = open(path, O_RDONLY)) < 0)
    112 		return (NULL);
    113 
    114 	minor = NULL;
    115 	ret = NULL;
    116 	if (devid_get(fd, &devid) == 0) {
    117 		if (devid_get_minor_name(fd, &minor) == 0)
    118 			ret = devid_str_encode(devid, minor);
    119 		if (minor != NULL)
    120 			devid_str_free(minor);
    121 		devid_free(devid);
    122 	}
    123 	(void) close(fd);
    124 
    125 	return (ret);
    126 #else
    127 	return (NULL);
    128 #endif
    129 }
    130 
    131 
    132 /*
    133  * Go through and fix up any path and/or devid information for the given vdev
    134  * configuration.
    135  */
    136 static int
    137 fix_paths(nvlist_t *nv, name_entry_t *names)
    138 {
    139 	nvlist_t **child;
    140 	uint_t c, children;
    141 	uint64_t guid;
    142 	name_entry_t *ne, *best;
    143 	char *path, *devid;
    144 	int matched;
    145 
    146 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
    147 	    &child, &children) == 0) {
    148 		for (c = 0; c < children; c++)
    149 			if (fix_paths(child[c], names) != 0)
    150 				return (-1);
    151 		return (0);
    152 	}
    153 
    154 	/*
    155 	 * This is a leaf (file or disk) vdev.  In either case, go through
    156 	 * the name list and see if we find a matching guid.  If so, replace
    157 	 * the path and see if we can calculate a new devid.
    158 	 *
    159 	 * There may be multiple names associated with a particular guid, in
    160 	 * which case we have overlapping slices or multiple paths to the same
    161 	 * disk.  If this is the case, then we want to pick the path that is
    162 	 * the most similar to the original, where "most similar" is the number
    163 	 * of matching characters starting from the end of the path.  This will
    164 	 * preserve slice numbers even if the disks have been reorganized, and
    165 	 * will also catch preferred disk names if multiple paths exist.
    166 	 */
    167 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
    168 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
    169 		path = NULL;
    170 
    171 	matched = 0;
    172 	best = NULL;
    173 	for (ne = names; ne != NULL; ne = ne->ne_next) {
    174 		if (ne->ne_guid == guid) {
    175 			const char *src, *dst;
    176 			int count;
    177 
    178 			if (path == NULL) {
    179 				best = ne;
    180 				break;
    181 			}
    182 
    183 			src = ne->ne_name + strlen(ne->ne_name) - 1;
    184 			dst = path + strlen(path) - 1;
    185 			for (count = 0; src >= ne->ne_name && dst >= path;
    186 			    src--, dst--, count++)
    187 				if (*src != *dst)
    188 					break;
    189 
    190 			/*
    191 			 * At this point, 'count' is the number of characters
    192 			 * matched from the end.
    193 			 */
    194 			if (count > matched || best == NULL) {
    195 				best = ne;
    196 				matched = count;
    197 			}
    198 		}
    199 	}
    200 
    201 	if (best == NULL)
    202 		return (0);
    203 
    204 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
    205 		return (-1);
    206 
    207 	if ((devid = get_devid(best->ne_name)) == NULL) {
    208 		(void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
    209 	} else {
    210 		if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0) {
    211 			devid_str_free(devid);
    212 			return (-1);
    213 		}
    214 		devid_str_free(devid);
    215 	}
    216 
    217 	return (0);
    218 }
    219 
    220 /*
    221  * Add the given configuration to the list of known devices.
    222  */
    223 static int
    224 add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
    225     nvlist_t *config)
    226 {
    227 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
    228 	pool_entry_t *pe;
    229 	vdev_entry_t *ve;
    230 	config_entry_t *ce;
    231 	name_entry_t *ne;
    232 
    233 	/*
    234 	 * If this is a hot spare not currently in use or level 2 cache
    235 	 * device, add it to the list of names to translate, but don't do
    236 	 * anything else.
    237 	 */
    238 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
    239 	    &state) == 0 &&
    240 	    (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
    241 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
    242 		if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
    243 			return (-1);
    244 
    245 		if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
    246 			free(ne);
    247 			return (-1);
    248 		}
    249 		ne->ne_guid = vdev_guid;
    250 		ne->ne_next = pl->names;
    251 		pl->names = ne;
    252 		return (0);
    253 	}
    254 
    255 	/*
    256 	 * If we have a valid config but cannot read any of these fields, then
    257 	 * it means we have a half-initialized label.  In vdev_label_init()
    258 	 * we write a label with txg == 0 so that we can identify the device
    259 	 * in case the user refers to the same disk later on.  If we fail to
    260 	 * create the pool, we'll be left with a label in this state
    261 	 * which should not be considered part of a valid pool.
    262 	 */
    263 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
    264 	    &pool_guid) != 0 ||
    265 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
    266 	    &vdev_guid) != 0 ||
    267 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
    268 	    &top_guid) != 0 ||
    269 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
    270 	    &txg) != 0 || txg == 0) {
    271 		nvlist_free(config);
    272 		return (0);
    273 	}
    274 
    275 	/*
    276 	 * First, see if we know about this pool.  If not, then add it to the
    277 	 * list of known pools.
    278 	 */
    279 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
    280 		if (pe->pe_guid == pool_guid)
    281 			break;
    282 	}
    283 
    284 	if (pe == NULL) {
    285 		if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
    286 			nvlist_free(config);
    287 			return (-1);
    288 		}
    289 		pe->pe_guid = pool_guid;
    290 		pe->pe_next = pl->pools;
    291 		pl->pools = pe;
    292 	}
    293 
    294 	/*
    295 	 * Second, see if we know about this toplevel vdev.  Add it if its
    296 	 * missing.
    297 	 */
    298 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
    299 		if (ve->ve_guid == top_guid)
    300 			break;
    301 	}
    302 
    303 	if (ve == NULL) {
    304 		if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
    305 			nvlist_free(config);
    306 			return (-1);
    307 		}
    308 		ve->ve_guid = top_guid;
    309 		ve->ve_next = pe->pe_vdevs;
    310 		pe->pe_vdevs = ve;
    311 	}
    312 
    313 	/*
    314 	 * Third, see if we have a config with a matching transaction group.  If
    315 	 * so, then we do nothing.  Otherwise, add it to the list of known
    316 	 * configs.
    317 	 */
    318 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
    319 		if (ce->ce_txg == txg)
    320 			break;
    321 	}
    322 
    323 	if (ce == NULL) {
    324 		if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
    325 			nvlist_free(config);
    326 			return (-1);
    327 		}
    328 		ce->ce_txg = txg;
    329 		ce->ce_config = config;
    330 		ce->ce_next = ve->ve_configs;
    331 		ve->ve_configs = ce;
    332 	} else {
    333 		nvlist_free(config);
    334 	}
    335 
    336 	/*
    337 	 * At this point we've successfully added our config to the list of
    338 	 * known configs.  The last thing to do is add the vdev guid -> path
    339 	 * mappings so that we can fix up the configuration as necessary before
    340 	 * doing the import.
    341 	 */
    342 	if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
    343 		return (-1);
    344 
    345 	if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
    346 		free(ne);
    347 		return (-1);
    348 	}
    349 
    350 	ne->ne_guid = vdev_guid;
    351 	ne->ne_next = pl->names;
    352 	pl->names = ne;
    353 
    354 	return (0);
    355 }
    356 
    357 /*
    358  * Returns true if the named pool matches the given GUID.
    359  */
    360 static int
    361 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
    362     boolean_t *isactive)
    363 {
    364 	zpool_handle_t *zhp;
    365 	uint64_t theguid;
    366 
    367 	if (zpool_open_silent(hdl, name, &zhp) != 0)
    368 		return (-1);
    369 
    370 	if (zhp == NULL) {
    371 		*isactive = B_FALSE;
    372 		return (0);
    373 	}
    374 
    375 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
    376 	    &theguid) == 0);
    377 
    378 	zpool_close(zhp);
    379 
    380 	*isactive = (theguid == guid);
    381 	return (0);
    382 }
    383 
    384 static nvlist_t *
    385 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
    386 {
    387 	nvlist_t *nvl;
    388 	zfs_cmd_t zc = { 0 };
    389 	int err;
    390 
    391 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
    392 		return (NULL);
    393 
    394 	if (zcmd_alloc_dst_nvlist(hdl, &zc,
    395 	    zc.zc_nvlist_conf_size * 2) != 0) {
    396 		zcmd_free_nvlists(&zc);
    397 		return (NULL);
    398 	}
    399 
    400 	while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
    401 	    &zc)) != 0 && errno == ENOMEM) {
    402 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
    403 			zcmd_free_nvlists(&zc);
    404 			return (NULL);
    405 		}
    406 	}
    407 
    408 	if (err) {
    409 		zcmd_free_nvlists(&zc);
    410 		return (NULL);
    411 	}
    412 
    413 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
    414 		zcmd_free_nvlists(&zc);
    415 		return (NULL);
    416 	}
    417 
    418 	zcmd_free_nvlists(&zc);
    419 	return (nvl);
    420 }
    421 
    422 /*
    423  * Determine if the vdev id is a hole in the namespace.
    424  */
    425 boolean_t
    426 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
    427 {
    428 	for (int c = 0; c < holes; c++) {
    429 
    430 		/* Top-level is a hole */
    431 		if (hole_array[c] == id)
    432 			return (B_TRUE);
    433 	}
    434 	return (B_FALSE);
    435 }
    436 
    437 /*
    438  * Convert our list of pools into the definitive set of configurations.  We
    439  * start by picking the best config for each toplevel vdev.  Once that's done,
    440  * we assemble the toplevel vdevs into a full config for the pool.  We make a
    441  * pass to fix up any incorrect paths, and then add it to the main list to
    442  * return to the user.
    443  */
    444 static nvlist_t *
    445 get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
    446 {
    447 	pool_entry_t *pe;
    448 	vdev_entry_t *ve;
    449 	config_entry_t *ce;
    450 	nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
    451 	nvlist_t **spares, **l2cache;
    452 	uint_t i, nspares, nl2cache;
    453 	boolean_t config_seen;
    454 	uint64_t best_txg;
    455 	char *name, *hostname = NULL;
    456 	uint64_t guid;
    457 	uint_t children = 0;
    458 	nvlist_t **child = NULL;
    459 	uint_t holes;
    460 	uint64_t *hole_array, max_id;
    461 	uint_t c;
    462 	boolean_t isactive;
    463 	uint64_t hostid;
    464 	nvlist_t *nvl;
    465 	boolean_t found_one = B_FALSE;
    466 	boolean_t valid_top_config = B_FALSE;
    467 
    468 	if (nvlist_alloc(&ret, 0, 0) != 0)
    469 		goto nomem;
    470 
    471 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
    472 		uint64_t id, max_txg = 0;
    473 
    474 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
    475 			goto nomem;
    476 		config_seen = B_FALSE;
    477 
    478 		/*
    479 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
    480 		 * from the first one we find, and then go through the rest and
    481 		 * add them as necessary to the 'vdevs' member of the config.
    482 		 */
    483 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
    484 
    485 			/*
    486 			 * Determine the best configuration for this vdev by
    487 			 * selecting the config with the latest transaction
    488 			 * group.
    489 			 */
    490 			best_txg = 0;
    491 			for (ce = ve->ve_configs; ce != NULL;
    492 			    ce = ce->ce_next) {
    493 
    494 				if (ce->ce_txg > best_txg) {
    495 					tmp = ce->ce_config;
    496 					best_txg = ce->ce_txg;
    497 				}
    498 			}
    499 
    500 			/*
    501 			 * We rely on the fact that the max txg for the
    502 			 * pool will contain the most up-to-date information
    503 			 * about the valid top-levels in the vdev namespace.
    504 			 */
    505 			if (best_txg > max_txg) {
    506 				(void) nvlist_remove(config,
    507 				    ZPOOL_CONFIG_VDEV_CHILDREN,
    508 				    DATA_TYPE_UINT64);
    509 				(void) nvlist_remove(config,
    510 				    ZPOOL_CONFIG_HOLE_ARRAY,
    511 				    DATA_TYPE_UINT64_ARRAY);
    512 
    513 				max_txg = best_txg;
    514 				hole_array = NULL;
    515 				holes = 0;
    516 				max_id = 0;
    517 				valid_top_config = B_FALSE;
    518 
    519 				if (nvlist_lookup_uint64(tmp,
    520 				    ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
    521 					verify(nvlist_add_uint64(config,
    522 					    ZPOOL_CONFIG_VDEV_CHILDREN,
    523 					    max_id) == 0);
    524 					valid_top_config = B_TRUE;
    525 				}
    526 
    527 				if (nvlist_lookup_uint64_array(tmp,
    528 				    ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
    529 				    &holes) == 0) {
    530 					verify(nvlist_add_uint64_array(config,
    531 					    ZPOOL_CONFIG_HOLE_ARRAY,
    532 					    hole_array, holes) == 0);
    533 				}
    534 			}
    535 
    536 			if (!config_seen) {
    537 				/*
    538 				 * Copy the relevant pieces of data to the pool
    539 				 * configuration:
    540 				 *
    541 				 *	version
    542 				 *	pool guid
    543 				 *	name
    544 				 *	comment (if available)
    545 				 *	pool state
    546 				 *	hostid (if available)
    547 				 *	hostname (if available)
    548 				 */
    549 				uint64_t state, version;
    550 				char *comment = NULL;
    551 
    552 				version = fnvlist_lookup_uint64(tmp,
    553 				    ZPOOL_CONFIG_VERSION);
    554 				fnvlist_add_uint64(config,
    555 				    ZPOOL_CONFIG_VERSION, version);
    556 				guid = fnvlist_lookup_uint64(tmp,
    557 				    ZPOOL_CONFIG_POOL_GUID);
    558 				fnvlist_add_uint64(config,
    559 				    ZPOOL_CONFIG_POOL_GUID, guid);
    560 				name = fnvlist_lookup_string(tmp,
    561 				    ZPOOL_CONFIG_POOL_NAME);
    562 				fnvlist_add_string(config,
    563 				    ZPOOL_CONFIG_POOL_NAME, name);
    564 
    565 				if (nvlist_lookup_string(tmp,
    566 				    ZPOOL_CONFIG_COMMENT, &comment) == 0)
    567 					fnvlist_add_string(config,
    568 					    ZPOOL_CONFIG_COMMENT, comment);
    569 
    570 				state = fnvlist_lookup_uint64(tmp,
    571 				    ZPOOL_CONFIG_POOL_STATE);
    572 				fnvlist_add_uint64(config,
    573 				    ZPOOL_CONFIG_POOL_STATE, state);
    574 
    575 				hostid = 0;
    576 				if (nvlist_lookup_uint64(tmp,
    577 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
    578 					fnvlist_add_uint64(config,
    579 					    ZPOOL_CONFIG_HOSTID, hostid);
    580 					hostname = fnvlist_lookup_string(tmp,
    581 					    ZPOOL_CONFIG_HOSTNAME);
    582 					fnvlist_add_string(config,
    583 					    ZPOOL_CONFIG_HOSTNAME, hostname);
    584 				}
    585 
    586 				config_seen = B_TRUE;
    587 			}
    588 
    589 			/*
    590 			 * Add this top-level vdev to the child array.
    591 			 */
    592 			verify(nvlist_lookup_nvlist(tmp,
    593 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
    594 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
    595 			    &id) == 0);
    596 
    597 			if (id >= children) {
    598 				nvlist_t **newchild;
    599 
    600 				newchild = zfs_alloc(hdl, (id + 1) *
    601 				    sizeof (nvlist_t *));
    602 				if (newchild == NULL)
    603 					goto nomem;
    604 
    605 				for (c = 0; c < children; c++)
    606 					newchild[c] = child[c];
    607 
    608 				free(child);
    609 				child = newchild;
    610 				children = id + 1;
    611 			}
    612 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
    613 				goto nomem;
    614 
    615 		}
    616 
    617 		/*
    618 		 * If we have information about all the top-levels then
    619 		 * clean up the nvlist which we've constructed. This
    620 		 * means removing any extraneous devices that are
    621 		 * beyond the valid range or adding devices to the end
    622 		 * of our array which appear to be missing.
    623 		 */
    624 		if (valid_top_config) {
    625 			if (max_id < children) {
    626 				for (c = max_id; c < children; c++)
    627 					nvlist_free(child[c]);
    628 				children = max_id;
    629 			} else if (max_id > children) {
    630 				nvlist_t **newchild;
    631 
    632 				newchild = zfs_alloc(hdl, (max_id) *
    633 				    sizeof (nvlist_t *));
    634 				if (newchild == NULL)
    635 					goto nomem;
    636 
    637 				for (c = 0; c < children; c++)
    638 					newchild[c] = child[c];
    639 
    640 				free(child);
    641 				child = newchild;
    642 				children = max_id;
    643 			}
    644 		}
    645 
    646 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
    647 		    &guid) == 0);
    648 
    649 		/*
    650 		 * The vdev namespace may contain holes as a result of
    651 		 * device removal. We must add them back into the vdev
    652 		 * tree before we process any missing devices.
    653 		 */
    654 		if (holes > 0) {
    655 			ASSERT(valid_top_config);
    656 
    657 			for (c = 0; c < children; c++) {
    658 				nvlist_t *holey;
    659 
    660 				if (child[c] != NULL ||
    661 				    !vdev_is_hole(hole_array, holes, c))
    662 					continue;
    663 
    664 				if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
    665 				    0) != 0)
    666 					goto nomem;
    667 
    668 				/*
    669 				 * Holes in the namespace are treated as
    670 				 * "hole" top-level vdevs and have a
    671 				 * special flag set on them.
    672 				 */
    673 				if (nvlist_add_string(holey,
    674 				    ZPOOL_CONFIG_TYPE,
    675 				    VDEV_TYPE_HOLE) != 0 ||
    676 				    nvlist_add_uint64(holey,
    677 				    ZPOOL_CONFIG_ID, c) != 0 ||
    678 				    nvlist_add_uint64(holey,
    679 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
    680 					nvlist_free(holey);
    681 					goto nomem;
    682 				}
    683 				child[c] = holey;
    684 			}
    685 		}
    686 
    687 		/*
    688 		 * Look for any missing top-level vdevs.  If this is the case,
    689 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
    690 		 * simply compress the child array, because the kernel performs
    691 		 * certain checks to make sure the vdev IDs match their location
    692 		 * in the configuration.
    693 		 */
    694 		for (c = 0; c < children; c++) {
    695 			if (child[c] == NULL) {
    696 				nvlist_t *missing;
    697 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
    698 				    0) != 0)
    699 					goto nomem;
    700 				if (nvlist_add_string(missing,
    701 				    ZPOOL_CONFIG_TYPE,
    702 				    VDEV_TYPE_MISSING) != 0 ||
    703 				    nvlist_add_uint64(missing,
    704 				    ZPOOL_CONFIG_ID, c) != 0 ||
    705 				    nvlist_add_uint64(missing,
    706 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
    707 					nvlist_free(missing);
    708 					goto nomem;
    709 				}
    710 				child[c] = missing;
    711 			}
    712 		}
    713 
    714 		/*
    715 		 * Put all of this pool's top-level vdevs into a root vdev.
    716 		 */
    717 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
    718 			goto nomem;
    719 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
    720 		    VDEV_TYPE_ROOT) != 0 ||
    721 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
    722 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
    723 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
    724 		    child, children) != 0) {
    725 			nvlist_free(nvroot);
    726 			goto nomem;
    727 		}
    728 
    729 		for (c = 0; c < children; c++)
    730 			nvlist_free(child[c]);
    731 		free(child);
    732 		children = 0;
    733 		child = NULL;
    734 
    735 		/*
    736 		 * Go through and fix up any paths and/or devids based on our
    737 		 * known list of vdev GUID -> path mappings.
    738 		 */
    739 		if (fix_paths(nvroot, pl->names) != 0) {
    740 			nvlist_free(nvroot);
    741 			goto nomem;
    742 		}
    743 
    744 		/*
    745 		 * Add the root vdev to this pool's configuration.
    746 		 */
    747 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
    748 		    nvroot) != 0) {
    749 			nvlist_free(nvroot);
    750 			goto nomem;
    751 		}
    752 		nvlist_free(nvroot);
    753 
    754 		/*
    755 		 * zdb uses this path to report on active pools that were
    756 		 * imported or created using -R.
    757 		 */
    758 		if (active_ok)
    759 			goto add_pool;
    760 
    761 		/*
    762 		 * Determine if this pool is currently active, in which case we
    763 		 * can't actually import it.
    764 		 */
    765 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
    766 		    &name) == 0);
    767 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
    768 		    &guid) == 0);
    769 
    770 		if (pool_active(hdl, name, guid, &isactive) != 0)
    771 			goto error;
    772 
    773 		if (isactive) {
    774 			nvlist_free(config);
    775 			config = NULL;
    776 			continue;
    777 		}
    778 
    779 		if ((nvl = refresh_config(hdl, config)) == NULL) {
    780 			nvlist_free(config);
    781 			config = NULL;
    782 			continue;
    783 		}
    784 
    785 		nvlist_free(config);
    786 		config = nvl;
    787 
    788 		/*
    789 		 * Go through and update the paths for spares, now that we have
    790 		 * them.
    791 		 */
    792 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
    793 		    &nvroot) == 0);
    794 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
    795 		    &spares, &nspares) == 0) {
    796 			for (i = 0; i < nspares; i++) {
    797 				if (fix_paths(spares[i], pl->names) != 0)
    798 					goto nomem;
    799 			}
    800 		}
    801 
    802 		/*
    803 		 * Update the paths for l2cache devices.
    804 		 */
    805 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
    806 		    &l2cache, &nl2cache) == 0) {
    807 			for (i = 0; i < nl2cache; i++) {
    808 				if (fix_paths(l2cache[i], pl->names) != 0)
    809 					goto nomem;
    810 			}
    811 		}
    812 
    813 		/*
    814 		 * Restore the original information read from the actual label.
    815 		 */
    816 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
    817 		    DATA_TYPE_UINT64);
    818 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
    819 		    DATA_TYPE_STRING);
    820 		if (hostid != 0) {
    821 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
    822 			    hostid) == 0);
    823 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
    824 			    hostname) == 0);
    825 		}
    826 
    827 add_pool:
    828 		/*
    829 		 * Add this pool to the list of configs.
    830 		 */
    831 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
    832 		    &name) == 0);
    833 		if (nvlist_add_nvlist(ret, name, config) != 0)
    834 			goto nomem;
    835 
    836 		found_one = B_TRUE;
    837 		nvlist_free(config);
    838 		config = NULL;
    839 	}
    840 
    841 	if (!found_one) {
    842 		nvlist_free(ret);
    843 		ret = NULL;
    844 	}
    845 
    846 	return (ret);
    847 
    848 nomem:
    849 	(void) no_memory(hdl);
    850 error:
    851 	nvlist_free(config);
    852 	nvlist_free(ret);
    853 	for (c = 0; c < children; c++)
    854 		nvlist_free(child[c]);
    855 	free(child);
    856 
    857 	return (NULL);
    858 }
    859 
    860 /*
    861  * Return the offset of the given label.
    862  */
    863 static uint64_t
    864 label_offset(uint64_t size, int l)
    865 {
    866 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
    867 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
    868 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
    869 }
    870 
    871 /*
    872  * Given a file descriptor, read the label information and return an nvlist
    873  * describing the configuration, if there is one.
    874  */
    875 int
    876 zpool_read_label(int fd, nvlist_t **config)
    877 {
    878 	struct stat64 statbuf;
    879 	int l;
    880 	vdev_label_t *label;
    881 	uint64_t state, txg, size;
    882 
    883 	*config = NULL;
    884 
    885 	if (fstat64(fd, &statbuf) == -1)
    886 		return (0);
    887 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
    888 
    889 	if ((label = malloc(sizeof (vdev_label_t))) == NULL)
    890 		return (-1);
    891 
    892 	for (l = 0; l < VDEV_LABELS; l++) {
    893 		if (pread64(fd, label, sizeof (vdev_label_t),
    894 		    label_offset(size, l)) != sizeof (vdev_label_t))
    895 			continue;
    896 
    897 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
    898 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
    899 			continue;
    900 
    901 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
    902 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
    903 			nvlist_free(*config);
    904 			continue;
    905 		}
    906 
    907 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
    908 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
    909 		    &txg) != 0 || txg == 0)) {
    910 			nvlist_free(*config);
    911 			continue;
    912 		}
    913 
    914 		free(label);
    915 		return (0);
    916 	}
    917 
    918 	free(label);
    919 	*config = NULL;
    920 	return (0);
    921 }
    922 
    923 typedef struct rdsk_node {
    924 	char *rn_name;
    925 	int rn_dfd;
    926 	libzfs_handle_t *rn_hdl;
    927 	nvlist_t *rn_config;
    928 	avl_tree_t *rn_avl;
    929 	avl_node_t rn_node;
    930 	boolean_t rn_nozpool;
    931 } rdsk_node_t;
    932 
    933 static int
    934 slice_cache_compare(const void *arg1, const void *arg2)
    935 {
    936 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
    937 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
    938 	char *nm1slice, *nm2slice;
    939 	int rv;
    940 
    941 	/*
    942 	 * slices zero and two are the most likely to provide results,
    943 	 * so put those first
    944 	 */
    945 	nm1slice = strstr(nm1, "s0");
    946 	nm2slice = strstr(nm2, "s0");
    947 	if (nm1slice && !nm2slice) {
    948 		return (-1);
    949 	}
    950 	if (!nm1slice && nm2slice) {
    951 		return (1);
    952 	}
    953 	nm1slice = strstr(nm1, "s2");
    954 	nm2slice = strstr(nm2, "s2");
    955 	if (nm1slice && !nm2slice) {
    956 		return (-1);
    957 	}
    958 	if (!nm1slice && nm2slice) {
    959 		return (1);
    960 	}
    961 
    962 	rv = strcmp(nm1, nm2);
    963 	if (rv == 0)
    964 		return (0);
    965 	return (rv > 0 ? 1 : -1);
    966 }
    967 
    968 #ifdef illumos
    969 static void
    970 check_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
    971     diskaddr_t size, uint_t blksz)
    972 {
    973 	rdsk_node_t tmpnode;
    974 	rdsk_node_t *node;
    975 	char sname[MAXNAMELEN];
    976 
    977 	tmpnode.rn_name = &sname[0];
    978 	(void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u",
    979 	    diskname, partno);
    980 	/*
    981 	 * protect against division by zero for disk labels that
    982 	 * contain a bogus sector size
    983 	 */
    984 	if (blksz == 0)
    985 		blksz = DEV_BSIZE;
    986 	/* too small to contain a zpool? */
    987 	if ((size < (SPA_MINDEVSIZE / blksz)) &&
    988 	    (node = avl_find(r, &tmpnode, NULL)))
    989 		node->rn_nozpool = B_TRUE;
    990 }
    991 #endif	/* illumos */
    992 
    993 static void
    994 nozpool_all_slices(avl_tree_t *r, const char *sname)
    995 {
    996 #ifdef illumos
    997 	char diskname[MAXNAMELEN];
    998 	char *ptr;
    999 	int i;
   1000 
   1001 	(void) strncpy(diskname, sname, MAXNAMELEN);
   1002 	if (((ptr = strrchr(diskname, 's')) == NULL) &&
   1003 	    ((ptr = strrchr(diskname, 'p')) == NULL))
   1004 		return;
   1005 	ptr[0] = 's';
   1006 	ptr[1] = '\0';
   1007 	for (i = 0; i < NDKMAP; i++)
   1008 		check_one_slice(r, diskname, i, 0, 1);
   1009 	ptr[0] = 'p';
   1010 	for (i = 0; i <= FD_NUMPART; i++)
   1011 		check_one_slice(r, diskname, i, 0, 1);
   1012 #endif	/* illumos */
   1013 }
   1014 
   1015 #ifdef illumos
   1016 static void
   1017 check_slices(avl_tree_t *r, int fd, const char *sname)
   1018 {
   1019 	struct extvtoc vtoc;
   1020 	struct dk_gpt *gpt;
   1021 	char diskname[MAXNAMELEN];
   1022 	char *ptr;
   1023 	int i;
   1024 
   1025 	(void) strncpy(diskname, sname, MAXNAMELEN);
   1026 	if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
   1027 		return;
   1028 	ptr[1] = '\0';
   1029 
   1030 	if (read_extvtoc(fd, &vtoc) >= 0) {
   1031 		for (i = 0; i < NDKMAP; i++)
   1032 			check_one_slice(r, diskname, i,
   1033 			    vtoc.v_part[i].p_size, vtoc.v_sectorsz);
   1034 	} else if (efi_alloc_and_read(fd, &gpt) >= 0) {
   1035 		/*
   1036 		 * on x86 we'll still have leftover links that point
   1037 		 * to slices s[9-15], so use NDKMAP instead
   1038 		 */
   1039 		for (i = 0; i < NDKMAP; i++)
   1040 			check_one_slice(r, diskname, i,
   1041 			    gpt->efi_parts[i].p_size, gpt->efi_lbasize);
   1042 		/* nodes p[1-4] are never used with EFI labels */
   1043 		ptr[0] = 'p';
   1044 		for (i = 1; i <= FD_NUMPART; i++)
   1045 			check_one_slice(r, diskname, i, 0, 1);
   1046 		efi_free(gpt);
   1047 	}
   1048 }
   1049 #endif	/* illumos */
   1050 
   1051 static void
   1052 zpool_open_func(void *arg)
   1053 {
   1054 	rdsk_node_t *rn = arg;
   1055 	struct stat64 statbuf;
   1056 	nvlist_t *config;
   1057 	int fd;
   1058 
   1059 	if (rn->rn_nozpool)
   1060 		return;
   1061 	if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) {
   1062 		/* symlink to a device that's no longer there */
   1063 		if (errno == ENOENT)
   1064 			nozpool_all_slices(rn->rn_avl, rn->rn_name);
   1065 		return;
   1066 	}
   1067 	/*
   1068 	 * Ignore failed stats.  We only want regular
   1069 	 * files, character devs and block devs.
   1070 	 */
   1071 	if (fstat64(fd, &statbuf) != 0 ||
   1072 	    (!S_ISREG(statbuf.st_mode) &&
   1073 	    !S_ISCHR(statbuf.st_mode) &&
   1074 	    !S_ISBLK(statbuf.st_mode))) {
   1075 		(void) close(fd);
   1076 		return;
   1077 	}
   1078 	/* this file is too small to hold a zpool */
   1079 #ifdef illumos
   1080 	if (S_ISREG(statbuf.st_mode) &&
   1081 	    statbuf.st_size < SPA_MINDEVSIZE) {
   1082 		(void) close(fd);
   1083 		return;
   1084 	} else if (!S_ISREG(statbuf.st_mode)) {
   1085 		/*
   1086 		 * Try to read the disk label first so we don't have to
   1087 		 * open a bunch of minor nodes that can't have a zpool.
   1088 		 */
   1089 		check_slices(rn->rn_avl, fd, rn->rn_name);
   1090 	}
   1091 #endif /* illumos */
   1092 #ifdef __FreeBSD__
   1093 	if (statbuf.st_size < SPA_MINDEVSIZE) {
   1094 		(void) close(fd);
   1095 		return;
   1096 	}
   1097 #endif /* __FreeBSD__ */
   1098 #ifdef __NetBSD__
   1099 	if (S_ISREG(statbuf.st_mode) &&
   1100 	    statbuf.st_size < SPA_MINDEVSIZE) {
   1101 		(void) close(fd);
   1102 		return;
   1103 	}
   1104 	/*
   1105 	 * skip character devices.
   1106 	 * note: for NetBSD, we abuse rn_name for block device names
   1107 	 * like dk0.
   1108 	 */
   1109 	if (S_ISCHR(statbuf.st_mode)) {
   1110 		(void) close(fd);
   1111 		return;
   1112 	}
   1113 	if (S_ISBLK(statbuf.st_mode)) {
   1114 		/*
   1115 		 * if the corresponding raw device is also available,
   1116 		 * we prefer to use it for zpool_read_label.
   1117 		 * otherwise, just use the block device.
   1118 		 */
   1119 		char raw_name[MAXPATHLEN];
   1120 
   1121 		/* eg. dk0 -> rdk0 */
   1122 		snprintf(raw_name, sizeof(raw_name), "r%s", rn->rn_name);
   1123 		int raw_fd = openat64(rn->rn_dfd, raw_name, O_RDONLY);
   1124 		if (raw_fd >= 0) {
   1125 			struct stat64 raw_statbuf;
   1126 
   1127 			if (fstat64(raw_fd, &raw_statbuf) == 0 &&
   1128 			    S_ISCHR(raw_statbuf.st_mode)) {
   1129 				(void) close(fd);
   1130 				fd = raw_fd;
   1131 			} else {
   1132 				(void) close(raw_fd);
   1133 			}
   1134 		}
   1135 
   1136 		struct dkwedge_list dkwl;
   1137 		off_t size;
   1138 
   1139 		/* skip devices with wedges */
   1140 		memset(&dkwl, 0, sizeof(dkwl));
   1141 		if (native_ioctl(fd, DIOCLWEDGES, &dkwl) == 0 &&
   1142 		    dkwl.dkwl_nwedges > 0) {
   1143 			(void) close(fd);
   1144 			return;
   1145 		}
   1146 
   1147 		if (native_ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
   1148 		    size < SPA_MINDEVSIZE) {
   1149 			(void) close(fd);
   1150 			return;
   1151 		}
   1152 	}
   1153 #endif
   1154 
   1155 	if ((zpool_read_label(fd, &config)) != 0) {
   1156 		(void) close(fd);
   1157 		(void) no_memory(rn->rn_hdl);
   1158 		return;
   1159 	}
   1160 	(void) close(fd);
   1161 
   1162 	rn->rn_config = config;
   1163 }
   1164 
   1165 /*
   1166  * Given a file descriptor, clear (zero) the label information.
   1167  */
   1168 int
   1169 zpool_clear_label(int fd)
   1170 {
   1171 	struct stat64 statbuf;
   1172 	int l;
   1173 	vdev_label_t *label;
   1174 	uint64_t size;
   1175 
   1176 	if (fstat64(fd, &statbuf) == -1)
   1177 		return (0);
   1178 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
   1179 
   1180 	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
   1181 		return (-1);
   1182 
   1183 	for (l = 0; l < VDEV_LABELS; l++) {
   1184 		if (pwrite64(fd, label, sizeof (vdev_label_t),
   1185 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
   1186 			free(label);
   1187 			return (-1);
   1188 		}
   1189 	}
   1190 
   1191 	free(label);
   1192 	return (0);
   1193 }
   1194 
   1195 /*
   1196  * Given a list of directories to search, find all pools stored on disk.  This
   1197  * includes partial pools which are not available to import.  If no args are
   1198  * given (argc is 0), then the default directory (/dev/dsk) is searched.
   1199  * poolname or guid (but not both) are provided by the caller when trying
   1200  * to import a specific pool.
   1201  */
   1202 static nvlist_t *
   1203 zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
   1204 {
   1205 	int i, dirs = iarg->paths;
   1206 	struct dirent64 *dp;
   1207 	char path[MAXPATHLEN];
   1208 	char *end, **dir = iarg->path;
   1209 	size_t pathleft;
   1210 	nvlist_t *ret = NULL;
   1211 	static char *default_dir = "/dev";
   1212 	pool_list_t pools = { 0 };
   1213 	pool_entry_t *pe, *penext;
   1214 	vdev_entry_t *ve, *venext;
   1215 	config_entry_t *ce, *cenext;
   1216 	name_entry_t *ne, *nenext;
   1217 	avl_tree_t slice_cache;
   1218 	rdsk_node_t *slice;
   1219 	void *cookie;
   1220 
   1221 	if (dirs == 0) {
   1222 		dirs = 1;
   1223 		dir = &default_dir;
   1224 	}
   1225 
   1226 	/*
   1227 	 * Go through and read the label configuration information from every
   1228 	 * possible device, organizing the information according to pool GUID
   1229 	 * and toplevel GUID.
   1230 	 */
   1231 	for (i = 0; i < dirs; i++) {
   1232 		tpool_t *t;
   1233 		char rdsk[MAXPATHLEN];
   1234 		int dfd;
   1235 		boolean_t config_failed = B_FALSE;
   1236 		DIR *dirp;
   1237 
   1238 		/* use realpath to normalize the path */
   1239 		if (realpath(dir[i], path) == 0) {
   1240 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
   1241 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]);
   1242 			goto error;
   1243 		}
   1244 		end = &path[strlen(path)];
   1245 		*end++ = '/';
   1246 		*end = 0;
   1247 		pathleft = &path[sizeof (path)] - end;
   1248 
   1249 #ifdef illumos
   1250 		/*
   1251 		 * Using raw devices instead of block devices when we're
   1252 		 * reading the labels skips a bunch of slow operations during
   1253 		 * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
   1254 		 */
   1255 		if (strcmp(path, ZFS_DISK_ROOTD) == 0)
   1256 			(void) strlcpy(rdsk, ZFS_RDISK_ROOTD, sizeof (rdsk));
   1257 		else
   1258 #endif
   1259 			(void) strlcpy(rdsk, path, sizeof (rdsk));
   1260 
   1261 		if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
   1262 		    (dirp = fdopendir(dfd)) == NULL) {
   1263 			if (dfd >= 0)
   1264 				(void) close(dfd);
   1265 			zfs_error_aux(hdl, strerror(errno));
   1266 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
   1267 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
   1268 			    rdsk);
   1269 			goto error;
   1270 		}
   1271 
   1272 		avl_create(&slice_cache, slice_cache_compare,
   1273 		    sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
   1274 
   1275 #ifdef __FreeBSD__
   1276 		if (strcmp(rdsk, "/dev/") == 0) {
   1277 			struct gmesh mesh;
   1278 			struct gclass *mp;
   1279 			struct ggeom *gp;
   1280 			struct gprovider *pp;
   1281 
   1282 			errno = geom_gettree(&mesh);
   1283 			if (errno != 0) {
   1284 				zfs_error_aux(hdl, strerror(errno));
   1285 				(void) zfs_error_fmt(hdl, EZFS_BADPATH,
   1286 				    dgettext(TEXT_DOMAIN, "cannot get GEOM tree"));
   1287 				goto error;
   1288 			}
   1289 
   1290 			LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
   1291 		        	LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
   1292 					LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
   1293 						slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
   1294 						slice->rn_name = zfs_strdup(hdl, pp->lg_name);
   1295 						slice->rn_avl = &slice_cache;
   1296 						slice->rn_dfd = dfd;
   1297 						slice->rn_hdl = hdl;
   1298 						slice->rn_nozpool = B_FALSE;
   1299 						avl_add(&slice_cache, slice);
   1300 					}
   1301 				}
   1302 			}
   1303 
   1304 			geom_deletetree(&mesh);
   1305 			goto skipdir;
   1306 		}
   1307 #endif
   1308 #ifdef __NetBSD__
   1309 		if (strcmp(rdsk, "/dev/") == 0) {
   1310 			static const char mib_name[] = "hw.disknames";
   1311 			size_t len;
   1312 			char *disknames, *last, *name;
   1313 
   1314 			/*
   1315 			 * note: hw.disknames contains block device names
   1316 			 * like "dk0". we store them to rn_name.
   1317 			 * zpool_open_func() will try to find the
   1318 			 * corresponding character device.
   1319 			 */
   1320 			if (sysctlbyname(mib_name, NULL, &len, NULL, 0) == -1) {
   1321 				zfs_error_aux(hdl, strerror(errno));
   1322 				(void) zfs_error_fmt(hdl, EZFS_BADPATH,
   1323 				    dgettext(TEXT_DOMAIN, "cannot get hw.disknames list"));
   1324 
   1325 				avl_destroy(&slice_cache);
   1326 				(void) closedir(dirp);
   1327 				goto error;
   1328 			}
   1329 			disknames = zfs_alloc(hdl, len + 2);
   1330 			(void)sysctlbyname(mib_name, disknames, &len, NULL, 0);
   1331 
   1332 			for ((name = strtok_r(disknames, " ", &last)); name;
   1333 			    (name = strtok_r(NULL, " ", &last))) {
   1334 				slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
   1335 				slice->rn_name = zfs_strdup(hdl, name);
   1336 				slice->rn_avl = &slice_cache;
   1337 				slice->rn_dfd = dfd;
   1338 				slice->rn_hdl = hdl;
   1339 				slice->rn_nozpool = B_FALSE;
   1340 				avl_add(&slice_cache, slice);
   1341 			}
   1342 			free(disknames);
   1343 
   1344 			goto skipdir;
   1345 		}
   1346 #endif
   1347 
   1348 		/*
   1349 		 * This is not MT-safe, but we have no MT consumers of libzfs
   1350 		 */
   1351 		while ((dp = readdir64(dirp)) != NULL) {
   1352 			const char *name = dp->d_name;
   1353 			if (name[0] == '.' &&
   1354 			    (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
   1355 				continue;
   1356 
   1357 			slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
   1358 			slice->rn_name = zfs_strdup(hdl, name);
   1359 			slice->rn_avl = &slice_cache;
   1360 			slice->rn_dfd = dfd;
   1361 			slice->rn_hdl = hdl;
   1362 			slice->rn_nozpool = B_FALSE;
   1363 			avl_add(&slice_cache, slice);
   1364 		}
   1365 skipdir:
   1366 		/*
   1367 		 * create a thread pool to do all of this in parallel;
   1368 		 * rn_nozpool is not protected, so this is racy in that
   1369 		 * multiple tasks could decide that the same slice can
   1370 		 * not hold a zpool, which is benign.  Also choose
   1371 		 * double the number of processors; we hold a lot of
   1372 		 * locks in the kernel, so going beyond this doesn't
   1373 		 * buy us much.
   1374 		 */
   1375 		t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
   1376 		    0, NULL);
   1377 		for (slice = avl_first(&slice_cache); slice;
   1378 		    (slice = avl_walk(&slice_cache, slice,
   1379 		    AVL_AFTER)))
   1380 			(void) tpool_dispatch(t, zpool_open_func, slice);
   1381 		tpool_wait(t);
   1382 		tpool_destroy(t);
   1383 
   1384 		cookie = NULL;
   1385 		while ((slice = avl_destroy_nodes(&slice_cache,
   1386 		    &cookie)) != NULL) {
   1387 			if (slice->rn_config != NULL && !config_failed) {
   1388 				nvlist_t *config = slice->rn_config;
   1389 				boolean_t matched = B_TRUE;
   1390 
   1391 				if (iarg->poolname != NULL) {
   1392 					char *pname;
   1393 
   1394 					matched = nvlist_lookup_string(config,
   1395 					    ZPOOL_CONFIG_POOL_NAME,
   1396 					    &pname) == 0 &&
   1397 					    strcmp(iarg->poolname, pname) == 0;
   1398 				} else if (iarg->guid != 0) {
   1399 					uint64_t this_guid;
   1400 
   1401 					matched = nvlist_lookup_uint64(config,
   1402 					    ZPOOL_CONFIG_POOL_GUID,
   1403 					    &this_guid) == 0 &&
   1404 					    iarg->guid == this_guid;
   1405 				}
   1406 				if (!matched) {
   1407 					nvlist_free(config);
   1408 				} else {
   1409 					/*
   1410 					 * use the non-raw path for the config
   1411 					 */
   1412 					(void) strlcpy(end, slice->rn_name,
   1413 					    pathleft);
   1414 					if (add_config(hdl, &pools, path,
   1415 					    config) != 0)
   1416 						config_failed = B_TRUE;
   1417 				}
   1418 			}
   1419 			free(slice->rn_name);
   1420 			free(slice);
   1421 		}
   1422 		avl_destroy(&slice_cache);
   1423 
   1424 		(void) closedir(dirp);
   1425 
   1426 		if (config_failed)
   1427 			goto error;
   1428 	}
   1429 
   1430 	ret = get_configs(hdl, &pools, iarg->can_be_active);
   1431 
   1432 error:
   1433 	for (pe = pools.pools; pe != NULL; pe = penext) {
   1434 		penext = pe->pe_next;
   1435 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
   1436 			venext = ve->ve_next;
   1437 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
   1438 				cenext = ce->ce_next;
   1439 				nvlist_free(ce->ce_config);
   1440 				free(ce);
   1441 			}
   1442 			free(ve);
   1443 		}
   1444 		free(pe);
   1445 	}
   1446 
   1447 	for (ne = pools.names; ne != NULL; ne = nenext) {
   1448 		nenext = ne->ne_next;
   1449 		free(ne->ne_name);
   1450 		free(ne);
   1451 	}
   1452 
   1453 	return (ret);
   1454 }
   1455 
   1456 nvlist_t *
   1457 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
   1458 {
   1459 	importargs_t iarg = { 0 };
   1460 
   1461 	iarg.paths = argc;
   1462 	iarg.path = argv;
   1463 
   1464 	return (zpool_find_import_impl(hdl, &iarg));
   1465 }
   1466 
   1467 /*
   1468  * Given a cache file, return the contents as a list of importable pools.
   1469  * poolname or guid (but not both) are provided by the caller when trying
   1470  * to import a specific pool.
   1471  */
   1472 nvlist_t *
   1473 zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
   1474     char *poolname, uint64_t guid)
   1475 {
   1476 	char *buf;
   1477 	int fd;
   1478 	struct stat64 statbuf;
   1479 	nvlist_t *raw, *src, *dst;
   1480 	nvlist_t *pools;
   1481 	nvpair_t *elem;
   1482 	char *name;
   1483 	uint64_t this_guid;
   1484 	boolean_t active;
   1485 
   1486 	verify(poolname == NULL || guid == 0);
   1487 
   1488 	if ((fd = open(cachefile, O_RDONLY)) < 0) {
   1489 		zfs_error_aux(hdl, "%s", strerror(errno));
   1490 		(void) zfs_error(hdl, EZFS_BADCACHE,
   1491 		    dgettext(TEXT_DOMAIN, "failed to open cache file"));
   1492 		return (NULL);
   1493 	}
   1494 
   1495 	if (fstat64(fd, &statbuf) != 0) {
   1496 		zfs_error_aux(hdl, "%s", strerror(errno));
   1497 		(void) close(fd);
   1498 		(void) zfs_error(hdl, EZFS_BADCACHE,
   1499 		    dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
   1500 		return (NULL);
   1501 	}
   1502 
   1503 	if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
   1504 		(void) close(fd);
   1505 		return (NULL);
   1506 	}
   1507 
   1508 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
   1509 		(void) close(fd);
   1510 		free(buf);
   1511 		(void) zfs_error(hdl, EZFS_BADCACHE,
   1512 		    dgettext(TEXT_DOMAIN,
   1513 		    "failed to read cache file contents"));
   1514 		return (NULL);
   1515 	}
   1516 
   1517 	(void) close(fd);
   1518 
   1519 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
   1520 		free(buf);
   1521 		(void) zfs_error(hdl, EZFS_BADCACHE,
   1522 		    dgettext(TEXT_DOMAIN,
   1523 		    "invalid or corrupt cache file contents"));
   1524 		return (NULL);
   1525 	}
   1526 
   1527 	free(buf);
   1528 
   1529 	/*
   1530 	 * Go through and get the current state of the pools and refresh their
   1531 	 * state.
   1532 	 */
   1533 	if (nvlist_alloc(&pools, 0, 0) != 0) {
   1534 		(void) no_memory(hdl);
   1535 		nvlist_free(raw);
   1536 		return (NULL);
   1537 	}
   1538 
   1539 	elem = NULL;
   1540 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
   1541 		src = fnvpair_value_nvlist(elem);
   1542 
   1543 		name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
   1544 		if (poolname != NULL && strcmp(poolname, name) != 0)
   1545 			continue;
   1546 
   1547 		this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
   1548 		if (guid != 0 && guid != this_guid)
   1549 			continue;
   1550 
   1551 		if (pool_active(hdl, name, this_guid, &active) != 0) {
   1552 			nvlist_free(raw);
   1553 			nvlist_free(pools);
   1554 			return (NULL);
   1555 		}
   1556 
   1557 		if (active)
   1558 			continue;
   1559 
   1560 		if ((dst = refresh_config(hdl, src)) == NULL) {
   1561 			nvlist_free(raw);
   1562 			nvlist_free(pools);
   1563 			return (NULL);
   1564 		}
   1565 
   1566 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
   1567 			(void) no_memory(hdl);
   1568 			nvlist_free(dst);
   1569 			nvlist_free(raw);
   1570 			nvlist_free(pools);
   1571 			return (NULL);
   1572 		}
   1573 		nvlist_free(dst);
   1574 	}
   1575 
   1576 	nvlist_free(raw);
   1577 	return (pools);
   1578 }
   1579 
   1580 static int
   1581 name_or_guid_exists(zpool_handle_t *zhp, void *data)
   1582 {
   1583 	importargs_t *import = data;
   1584 	int found = 0;
   1585 
   1586 	if (import->poolname != NULL) {
   1587 		char *pool_name;
   1588 
   1589 		verify(nvlist_lookup_string(zhp->zpool_config,
   1590 		    ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
   1591 		if (strcmp(pool_name, import->poolname) == 0)
   1592 			found = 1;
   1593 	} else {
   1594 		uint64_t pool_guid;
   1595 
   1596 		verify(nvlist_lookup_uint64(zhp->zpool_config,
   1597 		    ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
   1598 		if (pool_guid == import->guid)
   1599 			found = 1;
   1600 	}
   1601 
   1602 	zpool_close(zhp);
   1603 	return (found);
   1604 }
   1605 
   1606 nvlist_t *
   1607 zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
   1608 {
   1609 	verify(import->poolname == NULL || import->guid == 0);
   1610 
   1611 	if (import->unique)
   1612 		import->exists = zpool_iter(hdl, name_or_guid_exists, import);
   1613 
   1614 	if (import->cachefile != NULL)
   1615 		return (zpool_find_import_cached(hdl, import->cachefile,
   1616 		    import->poolname, import->guid));
   1617 
   1618 	return (zpool_find_import_impl(hdl, import));
   1619 }
   1620 
   1621 boolean_t
   1622 find_guid(nvlist_t *nv, uint64_t guid)
   1623 {
   1624 	uint64_t tmp;
   1625 	nvlist_t **child;
   1626 	uint_t c, children;
   1627 
   1628 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
   1629 	if (tmp == guid)
   1630 		return (B_TRUE);
   1631 
   1632 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
   1633 	    &child, &children) == 0) {
   1634 		for (c = 0; c < children; c++)
   1635 			if (find_guid(child[c], guid))
   1636 				return (B_TRUE);
   1637 	}
   1638 
   1639 	return (B_FALSE);
   1640 }
   1641 
   1642 typedef struct aux_cbdata {
   1643 	const char	*cb_type;
   1644 	uint64_t	cb_guid;
   1645 	zpool_handle_t	*cb_zhp;
   1646 } aux_cbdata_t;
   1647 
   1648 static int
   1649 find_aux(zpool_handle_t *zhp, void *data)
   1650 {
   1651 	aux_cbdata_t *cbp = data;
   1652 	nvlist_t **list;
   1653 	uint_t i, count;
   1654 	uint64_t guid;
   1655 	nvlist_t *nvroot;
   1656 
   1657 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
   1658 	    &nvroot) == 0);
   1659 
   1660 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
   1661 	    &list, &count) == 0) {
   1662 		for (i = 0; i < count; i++) {
   1663 			verify(nvlist_lookup_uint64(list[i],
   1664 			    ZPOOL_CONFIG_GUID, &guid) == 0);
   1665 			if (guid == cbp->cb_guid) {
   1666 				cbp->cb_zhp = zhp;
   1667 				return (1);
   1668 			}
   1669 		}
   1670 	}
   1671 
   1672 	zpool_close(zhp);
   1673 	return (0);
   1674 }
   1675 
   1676 /*
   1677  * Determines if the pool is in use.  If so, it returns true and the state of
   1678  * the pool as well as the name of the pool.  Both strings are allocated and
   1679  * must be freed by the caller.
   1680  */
   1681 int
   1682 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
   1683     boolean_t *inuse)
   1684 {
   1685 	nvlist_t *config;
   1686 	char *name;
   1687 	boolean_t ret;
   1688 	uint64_t guid, vdev_guid;
   1689 	zpool_handle_t *zhp;
   1690 	nvlist_t *pool_config;
   1691 	uint64_t stateval, isspare;
   1692 	aux_cbdata_t cb = { 0 };
   1693 	boolean_t isactive;
   1694 
   1695 	*inuse = B_FALSE;
   1696 
   1697 	if (zpool_read_label(fd, &config) != 0) {
   1698 		(void) no_memory(hdl);
   1699 		return (-1);
   1700 	}
   1701 
   1702 	if (config == NULL)
   1703 		return (0);
   1704 
   1705 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
   1706 	    &stateval) == 0);
   1707 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
   1708 	    &vdev_guid) == 0);
   1709 
   1710 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
   1711 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
   1712 		    &name) == 0);
   1713 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
   1714 		    &guid) == 0);
   1715 	}
   1716 
   1717 	switch (stateval) {
   1718 	case POOL_STATE_EXPORTED:
   1719 		/*
   1720 		 * A pool with an exported state may in fact be imported
   1721 		 * read-only, so check the in-core state to see if it's
   1722 		 * active and imported read-only.  If it is, set
   1723 		 * its state to active.
   1724 		 */
   1725 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
   1726 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
   1727 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
   1728 				stateval = POOL_STATE_ACTIVE;
   1729 
   1730 			/*
   1731 			 * All we needed the zpool handle for is the
   1732 			 * readonly prop check.
   1733 			 */
   1734 			zpool_close(zhp);
   1735 		}
   1736 
   1737 		ret = B_TRUE;
   1738 		break;
   1739 
   1740 	case POOL_STATE_ACTIVE:
   1741 		/*
   1742 		 * For an active pool, we have to determine if it's really part
   1743 		 * of a currently active pool (in which case the pool will exist
   1744 		 * and the guid will be the same), or whether it's part of an
   1745 		 * active pool that was disconnected without being explicitly
   1746 		 * exported.
   1747 		 */
   1748 		if (pool_active(hdl, name, guid, &isactive) != 0) {
   1749 			nvlist_free(config);
   1750 			return (-1);
   1751 		}
   1752 
   1753 		if (isactive) {
   1754 			/*
   1755 			 * Because the device may have been removed while
   1756 			 * offlined, we only report it as active if the vdev is
   1757 			 * still present in the config.  Otherwise, pretend like
   1758 			 * it's not in use.
   1759 			 */
   1760 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
   1761 			    (pool_config = zpool_get_config(zhp, NULL))
   1762 			    != NULL) {
   1763 				nvlist_t *nvroot;
   1764 
   1765 				verify(nvlist_lookup_nvlist(pool_config,
   1766 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
   1767 				ret = find_guid(nvroot, vdev_guid);
   1768 			} else {
   1769 				ret = B_FALSE;
   1770 			}
   1771 
   1772 			/*
   1773 			 * If this is an active spare within another pool, we
   1774 			 * treat it like an unused hot spare.  This allows the
   1775 			 * user to create a pool with a hot spare that currently
   1776 			 * in use within another pool.  Since we return B_TRUE,
   1777 			 * libdiskmgt will continue to prevent generic consumers
   1778 			 * from using the device.
   1779 			 */
   1780 			if (ret && nvlist_lookup_uint64(config,
   1781 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
   1782 				stateval = POOL_STATE_SPARE;
   1783 
   1784 			if (zhp != NULL)
   1785 				zpool_close(zhp);
   1786 		} else {
   1787 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
   1788 			ret = B_TRUE;
   1789 		}
   1790 		break;
   1791 
   1792 	case POOL_STATE_SPARE:
   1793 		/*
   1794 		 * For a hot spare, it can be either definitively in use, or
   1795 		 * potentially active.  To determine if it's in use, we iterate
   1796 		 * over all pools in the system and search for one with a spare
   1797 		 * with a matching guid.
   1798 		 *
   1799 		 * Due to the shared nature of spares, we don't actually report
   1800 		 * the potentially active case as in use.  This means the user
   1801 		 * can freely create pools on the hot spares of exported pools,
   1802 		 * but to do otherwise makes the resulting code complicated, and
   1803 		 * we end up having to deal with this case anyway.
   1804 		 */
   1805 		cb.cb_zhp = NULL;
   1806 		cb.cb_guid = vdev_guid;
   1807 		cb.cb_type = ZPOOL_CONFIG_SPARES;
   1808 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
   1809 			name = (char *)zpool_get_name(cb.cb_zhp);
   1810 			ret = B_TRUE;
   1811 		} else {
   1812 			ret = B_FALSE;
   1813 		}
   1814 		break;
   1815 
   1816 	case POOL_STATE_L2CACHE:
   1817 
   1818 		/*
   1819 		 * Check if any pool is currently using this l2cache device.
   1820 		 */
   1821 		cb.cb_zhp = NULL;
   1822 		cb.cb_guid = vdev_guid;
   1823 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
   1824 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
   1825 			name = (char *)zpool_get_name(cb.cb_zhp);
   1826 			ret = B_TRUE;
   1827 		} else {
   1828 			ret = B_FALSE;
   1829 		}
   1830 		break;
   1831 
   1832 	default:
   1833 		ret = B_FALSE;
   1834 	}
   1835 
   1836 
   1837 	if (ret) {
   1838 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
   1839 			if (cb.cb_zhp)
   1840 				zpool_close(cb.cb_zhp);
   1841 			nvlist_free(config);
   1842 			return (-1);
   1843 		}
   1844 		*state = (pool_state_t)stateval;
   1845 	}
   1846 
   1847 	if (cb.cb_zhp)
   1848 		zpool_close(cb.cb_zhp);
   1849 
   1850 	nvlist_free(config);
   1851 	*inuse = ret;
   1852 	return (0);
   1853 }
   1854 
   1855 #ifdef __NetBSD__
   1856 /*
   1857  * This needs to be at the end of the file so that we can #undef ioctl
   1858  * without affecting anything else.
   1859  */
   1860 #undef ioctl
   1861 
   1862 static int
   1863 native_ioctl(int fd, unsigned long cmd, void *arg)
   1864 {
   1865 
   1866 	return ioctl(fd, cmd, arg);
   1867 }
   1868 #endif
   1869