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 9271171e0SMartin 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 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24eda14cbcSMatt Macy * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25eda14cbcSMatt Macy * Copyright 2015 RackTop Systems. 26eda14cbcSMatt Macy * Copyright (c) 2016, Intel Corporation. 27ee36e25aSMartin Matuska * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> 28eda14cbcSMatt Macy */ 29eda14cbcSMatt Macy 30eda14cbcSMatt Macy /* 31eda14cbcSMatt Macy * Pool import support functions. 32eda14cbcSMatt Macy * 33eda14cbcSMatt Macy * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since 34eda14cbcSMatt Macy * these commands are expected to run in the global zone, we can assume 35eda14cbcSMatt Macy * that the devices are all readable when called. 36eda14cbcSMatt Macy * 37eda14cbcSMatt Macy * To import a pool, we rely on reading the configuration information from the 38eda14cbcSMatt Macy * ZFS label of each device. If we successfully read the label, then we 39eda14cbcSMatt Macy * organize the configuration information in the following hierarchy: 40eda14cbcSMatt Macy * 41eda14cbcSMatt Macy * pool guid -> toplevel vdev guid -> label txg 42eda14cbcSMatt Macy * 43eda14cbcSMatt Macy * Duplicate entries matching this same tuple will be discarded. Once we have 44eda14cbcSMatt Macy * examined every device, we pick the best label txg config for each toplevel 45eda14cbcSMatt Macy * vdev. We then arrange these toplevel vdevs into a complete pool config, and 46eda14cbcSMatt Macy * update any paths that have changed. Finally, we attempt to import the pool 47eda14cbcSMatt Macy * using our derived config, and record the results. 48eda14cbcSMatt Macy */ 49eda14cbcSMatt Macy 50da5137abSMartin Matuska #ifdef HAVE_AIO_H 51184c1b94SMartin Matuska #include <aio.h> 52da5137abSMartin Matuska #endif 53eda14cbcSMatt Macy #include <ctype.h> 54eda14cbcSMatt Macy #include <dirent.h> 55eda14cbcSMatt Macy #include <errno.h> 56eda14cbcSMatt Macy #include <libintl.h> 57eda14cbcSMatt Macy #include <libgen.h> 58eda14cbcSMatt Macy #include <stddef.h> 59eda14cbcSMatt Macy #include <stdlib.h> 60eda14cbcSMatt Macy #include <string.h> 61eda14cbcSMatt Macy #include <sys/stat.h> 62eda14cbcSMatt Macy #include <unistd.h> 63eda14cbcSMatt Macy #include <fcntl.h> 64eda14cbcSMatt Macy #include <sys/dktp/fdisk.h> 65eda14cbcSMatt Macy #include <sys/vdev_impl.h> 66eda14cbcSMatt Macy #include <sys/fs/zfs.h> 67eda14cbcSMatt Macy 68eda14cbcSMatt Macy #include <thread_pool.h> 69eda14cbcSMatt Macy #include <libzutil.h> 70eda14cbcSMatt Macy #include <libnvpair.h> 71eda14cbcSMatt Macy 72eda14cbcSMatt Macy #include "zutil_import.h" 73eda14cbcSMatt Macy 74*dbd5678dSMartin Matuska const char * 75*dbd5678dSMartin Matuska libpc_error_description(libpc_handle_t *hdl) 76*dbd5678dSMartin Matuska { 77*dbd5678dSMartin Matuska if (hdl->lpc_desc[0] != '\0') 78*dbd5678dSMartin Matuska return (hdl->lpc_desc); 79*dbd5678dSMartin Matuska 80*dbd5678dSMartin Matuska switch (hdl->lpc_error) { 81*dbd5678dSMartin Matuska case LPC_BADCACHE: 82*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 83*dbd5678dSMartin Matuska case LPC_BADPATH: 84*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 85*dbd5678dSMartin Matuska case LPC_NOMEM: 86*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "out of memory")); 87*dbd5678dSMartin Matuska case LPC_EACCESS: 88*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "some devices require root " 89*dbd5678dSMartin Matuska "privileges")); 90*dbd5678dSMartin Matuska case LPC_UNKNOWN: 91*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "unknown error")); 92*dbd5678dSMartin Matuska default: 93*dbd5678dSMartin Matuska assert(hdl->lpc_error == 0); 94*dbd5678dSMartin Matuska return (dgettext(TEXT_DOMAIN, "no error")); 95*dbd5678dSMartin Matuska } 96*dbd5678dSMartin Matuska } 97*dbd5678dSMartin Matuska 981f88aa09SMartin Matuska static __attribute__((format(printf, 2, 3))) void 99eda14cbcSMatt Macy zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...) 100eda14cbcSMatt Macy { 101eda14cbcSMatt Macy va_list ap; 102eda14cbcSMatt Macy 103eda14cbcSMatt Macy va_start(ap, fmt); 104eda14cbcSMatt Macy 105eda14cbcSMatt Macy (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap); 106eda14cbcSMatt Macy hdl->lpc_desc_active = B_TRUE; 107eda14cbcSMatt Macy 108eda14cbcSMatt Macy va_end(ap); 109eda14cbcSMatt Macy } 110eda14cbcSMatt Macy 111eda14cbcSMatt Macy static void 112*dbd5678dSMartin Matuska zutil_verror(libpc_handle_t *hdl, lpc_error_t error, const char *fmt, 113eda14cbcSMatt Macy va_list ap) 114eda14cbcSMatt Macy { 115eda14cbcSMatt Macy char action[1024]; 116eda14cbcSMatt Macy 117eda14cbcSMatt Macy (void) vsnprintf(action, sizeof (action), fmt, ap); 118*dbd5678dSMartin Matuska hdl->lpc_error = error; 119eda14cbcSMatt Macy 120eda14cbcSMatt Macy if (hdl->lpc_desc_active) 121eda14cbcSMatt Macy hdl->lpc_desc_active = B_FALSE; 122eda14cbcSMatt Macy else 123eda14cbcSMatt Macy hdl->lpc_desc[0] = '\0'; 124eda14cbcSMatt Macy 125*dbd5678dSMartin Matuska if (hdl->lpc_printerr) 126*dbd5678dSMartin Matuska (void) fprintf(stderr, "%s: %s\n", action, 127*dbd5678dSMartin Matuska libpc_error_description(hdl)); 128eda14cbcSMatt Macy } 129eda14cbcSMatt Macy 1301f88aa09SMartin Matuska static __attribute__((format(printf, 3, 4))) int 131*dbd5678dSMartin Matuska zutil_error_fmt(libpc_handle_t *hdl, lpc_error_t error, 132*dbd5678dSMartin Matuska const char *fmt, ...) 133eda14cbcSMatt Macy { 134eda14cbcSMatt Macy va_list ap; 135eda14cbcSMatt Macy 136eda14cbcSMatt Macy va_start(ap, fmt); 137eda14cbcSMatt Macy 138eda14cbcSMatt Macy zutil_verror(hdl, error, fmt, ap); 139eda14cbcSMatt Macy 140eda14cbcSMatt Macy va_end(ap); 141eda14cbcSMatt Macy 142eda14cbcSMatt Macy return (-1); 143eda14cbcSMatt Macy } 144eda14cbcSMatt Macy 145eda14cbcSMatt Macy static int 146*dbd5678dSMartin Matuska zutil_error(libpc_handle_t *hdl, lpc_error_t error, const char *msg) 147eda14cbcSMatt Macy { 148eda14cbcSMatt Macy return (zutil_error_fmt(hdl, error, "%s", msg)); 149eda14cbcSMatt Macy } 150eda14cbcSMatt Macy 151eda14cbcSMatt Macy static int 152eda14cbcSMatt Macy zutil_no_memory(libpc_handle_t *hdl) 153eda14cbcSMatt Macy { 154*dbd5678dSMartin Matuska zutil_error(hdl, LPC_NOMEM, "internal error"); 155eda14cbcSMatt Macy exit(1); 156eda14cbcSMatt Macy } 157eda14cbcSMatt Macy 158eda14cbcSMatt Macy void * 159eda14cbcSMatt Macy zutil_alloc(libpc_handle_t *hdl, size_t size) 160eda14cbcSMatt Macy { 161eda14cbcSMatt Macy void *data; 162eda14cbcSMatt Macy 163eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL) 164eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 165eda14cbcSMatt Macy 166eda14cbcSMatt Macy return (data); 167eda14cbcSMatt Macy } 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy char * 170eda14cbcSMatt Macy zutil_strdup(libpc_handle_t *hdl, const char *str) 171eda14cbcSMatt Macy { 172eda14cbcSMatt Macy char *ret; 173eda14cbcSMatt Macy 174eda14cbcSMatt Macy if ((ret = strdup(str)) == NULL) 175eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 176eda14cbcSMatt Macy 177eda14cbcSMatt Macy return (ret); 178eda14cbcSMatt Macy } 179eda14cbcSMatt Macy 1803ff01b23SMartin Matuska static char * 1813ff01b23SMartin Matuska zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n) 1823ff01b23SMartin Matuska { 1833ff01b23SMartin Matuska char *ret; 1843ff01b23SMartin Matuska 1853ff01b23SMartin Matuska if ((ret = strndup(str, n)) == NULL) 1863ff01b23SMartin Matuska (void) zutil_no_memory(hdl); 1873ff01b23SMartin Matuska 1883ff01b23SMartin Matuska return (ret); 1893ff01b23SMartin Matuska } 1903ff01b23SMartin Matuska 191eda14cbcSMatt Macy /* 192eda14cbcSMatt Macy * Intermediate structures used to gather configuration information. 193eda14cbcSMatt Macy */ 194eda14cbcSMatt Macy typedef struct config_entry { 195eda14cbcSMatt Macy uint64_t ce_txg; 196eda14cbcSMatt Macy nvlist_t *ce_config; 197eda14cbcSMatt Macy struct config_entry *ce_next; 198eda14cbcSMatt Macy } config_entry_t; 199eda14cbcSMatt Macy 200eda14cbcSMatt Macy typedef struct vdev_entry { 201eda14cbcSMatt Macy uint64_t ve_guid; 202eda14cbcSMatt Macy config_entry_t *ve_configs; 203eda14cbcSMatt Macy struct vdev_entry *ve_next; 204eda14cbcSMatt Macy } vdev_entry_t; 205eda14cbcSMatt Macy 206eda14cbcSMatt Macy typedef struct pool_entry { 207eda14cbcSMatt Macy uint64_t pe_guid; 208eda14cbcSMatt Macy vdev_entry_t *pe_vdevs; 209eda14cbcSMatt Macy struct pool_entry *pe_next; 210eda14cbcSMatt Macy } pool_entry_t; 211eda14cbcSMatt Macy 212eda14cbcSMatt Macy typedef struct name_entry { 213eda14cbcSMatt Macy char *ne_name; 214eda14cbcSMatt Macy uint64_t ne_guid; 215eda14cbcSMatt Macy uint64_t ne_order; 216eda14cbcSMatt Macy uint64_t ne_num_labels; 217eda14cbcSMatt Macy struct name_entry *ne_next; 218eda14cbcSMatt Macy } name_entry_t; 219eda14cbcSMatt Macy 220eda14cbcSMatt Macy typedef struct pool_list { 221eda14cbcSMatt Macy pool_entry_t *pools; 222eda14cbcSMatt Macy name_entry_t *names; 223eda14cbcSMatt Macy } pool_list_t; 224eda14cbcSMatt Macy 225eda14cbcSMatt Macy /* 226eda14cbcSMatt Macy * Go through and fix up any path and/or devid information for the given vdev 227eda14cbcSMatt Macy * configuration. 228eda14cbcSMatt Macy */ 229eda14cbcSMatt Macy static int 230eda14cbcSMatt Macy fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names) 231eda14cbcSMatt Macy { 232eda14cbcSMatt Macy nvlist_t **child; 233eda14cbcSMatt Macy uint_t c, children; 234eda14cbcSMatt Macy uint64_t guid; 235eda14cbcSMatt Macy name_entry_t *ne, *best; 236eda14cbcSMatt Macy char *path; 237eda14cbcSMatt Macy 238eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 239eda14cbcSMatt Macy &child, &children) == 0) { 240eda14cbcSMatt Macy for (c = 0; c < children; c++) 241eda14cbcSMatt Macy if (fix_paths(hdl, child[c], names) != 0) 242eda14cbcSMatt Macy return (-1); 243eda14cbcSMatt Macy return (0); 244eda14cbcSMatt Macy } 245eda14cbcSMatt Macy 246eda14cbcSMatt Macy /* 247eda14cbcSMatt Macy * This is a leaf (file or disk) vdev. In either case, go through 248eda14cbcSMatt Macy * the name list and see if we find a matching guid. If so, replace 249eda14cbcSMatt Macy * the path and see if we can calculate a new devid. 250eda14cbcSMatt Macy * 251eda14cbcSMatt Macy * There may be multiple names associated with a particular guid, in 252eda14cbcSMatt Macy * which case we have overlapping partitions or multiple paths to the 253eda14cbcSMatt Macy * same disk. In this case we prefer to use the path name which 254eda14cbcSMatt Macy * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we 255eda14cbcSMatt Macy * use the lowest order device which corresponds to the first match 256eda14cbcSMatt Macy * while traversing the ZPOOL_IMPORT_PATH search path. 257eda14cbcSMatt Macy */ 258eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 259eda14cbcSMatt Macy if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 260eda14cbcSMatt Macy path = NULL; 261eda14cbcSMatt Macy 262eda14cbcSMatt Macy best = NULL; 263eda14cbcSMatt Macy for (ne = names; ne != NULL; ne = ne->ne_next) { 264eda14cbcSMatt Macy if (ne->ne_guid == guid) { 265eda14cbcSMatt Macy if (path == NULL) { 266eda14cbcSMatt Macy best = ne; 267eda14cbcSMatt Macy break; 268eda14cbcSMatt Macy } 269eda14cbcSMatt Macy 270eda14cbcSMatt Macy if ((strlen(path) == strlen(ne->ne_name)) && 271eda14cbcSMatt Macy strncmp(path, ne->ne_name, strlen(path)) == 0) { 272eda14cbcSMatt Macy best = ne; 273eda14cbcSMatt Macy break; 274eda14cbcSMatt Macy } 275eda14cbcSMatt Macy 276eda14cbcSMatt Macy if (best == NULL) { 277eda14cbcSMatt Macy best = ne; 278eda14cbcSMatt Macy continue; 279eda14cbcSMatt Macy } 280eda14cbcSMatt Macy 281eda14cbcSMatt Macy /* Prefer paths with move vdev labels. */ 282eda14cbcSMatt Macy if (ne->ne_num_labels > best->ne_num_labels) { 283eda14cbcSMatt Macy best = ne; 284eda14cbcSMatt Macy continue; 285eda14cbcSMatt Macy } 286eda14cbcSMatt Macy 287eda14cbcSMatt Macy /* Prefer paths earlier in the search order. */ 288eda14cbcSMatt Macy if (ne->ne_num_labels == best->ne_num_labels && 289eda14cbcSMatt Macy ne->ne_order < best->ne_order) { 290eda14cbcSMatt Macy best = ne; 291eda14cbcSMatt Macy continue; 292eda14cbcSMatt Macy } 293eda14cbcSMatt Macy } 294eda14cbcSMatt Macy } 295eda14cbcSMatt Macy 296eda14cbcSMatt Macy if (best == NULL) 297eda14cbcSMatt Macy return (0); 298eda14cbcSMatt Macy 299eda14cbcSMatt Macy if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 300eda14cbcSMatt Macy return (-1); 301eda14cbcSMatt Macy 302eda14cbcSMatt Macy update_vdev_config_dev_strs(nv); 303eda14cbcSMatt Macy 304eda14cbcSMatt Macy return (0); 305eda14cbcSMatt Macy } 306eda14cbcSMatt Macy 307eda14cbcSMatt Macy /* 308eda14cbcSMatt Macy * Add the given configuration to the list of known devices. 309eda14cbcSMatt Macy */ 310eda14cbcSMatt Macy static int 311eda14cbcSMatt Macy add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path, 312eda14cbcSMatt Macy int order, int num_labels, nvlist_t *config) 313eda14cbcSMatt Macy { 314eda14cbcSMatt Macy uint64_t pool_guid, vdev_guid, top_guid, txg, state; 315eda14cbcSMatt Macy pool_entry_t *pe; 316eda14cbcSMatt Macy vdev_entry_t *ve; 317eda14cbcSMatt Macy config_entry_t *ce; 318eda14cbcSMatt Macy name_entry_t *ne; 319eda14cbcSMatt Macy 320eda14cbcSMatt Macy /* 321eda14cbcSMatt Macy * If this is a hot spare not currently in use or level 2 cache 322eda14cbcSMatt Macy * device, add it to the list of names to translate, but don't do 323eda14cbcSMatt Macy * anything else. 324eda14cbcSMatt Macy */ 325eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 326eda14cbcSMatt Macy &state) == 0 && 327eda14cbcSMatt Macy (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 328eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 329eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 330eda14cbcSMatt Macy return (-1); 331eda14cbcSMatt Macy 332eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 333eda14cbcSMatt Macy free(ne); 334eda14cbcSMatt Macy return (-1); 335eda14cbcSMatt Macy } 336eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 337eda14cbcSMatt Macy ne->ne_order = order; 338eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 339eda14cbcSMatt Macy ne->ne_next = pl->names; 340eda14cbcSMatt Macy pl->names = ne; 341eda14cbcSMatt Macy 342eda14cbcSMatt Macy return (0); 343eda14cbcSMatt Macy } 344eda14cbcSMatt Macy 345eda14cbcSMatt Macy /* 346eda14cbcSMatt Macy * If we have a valid config but cannot read any of these fields, then 347eda14cbcSMatt Macy * it means we have a half-initialized label. In vdev_label_init() 348eda14cbcSMatt Macy * we write a label with txg == 0 so that we can identify the device 349eda14cbcSMatt Macy * in case the user refers to the same disk later on. If we fail to 350eda14cbcSMatt Macy * create the pool, we'll be left with a label in this state 351eda14cbcSMatt Macy * which should not be considered part of a valid pool. 352eda14cbcSMatt Macy */ 353eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 354eda14cbcSMatt Macy &pool_guid) != 0 || 355eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 356eda14cbcSMatt Macy &vdev_guid) != 0 || 357eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 358eda14cbcSMatt Macy &top_guid) != 0 || 359eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 360eda14cbcSMatt Macy &txg) != 0 || txg == 0) { 361eda14cbcSMatt Macy return (0); 362eda14cbcSMatt Macy } 363eda14cbcSMatt Macy 364eda14cbcSMatt Macy /* 365eda14cbcSMatt Macy * First, see if we know about this pool. If not, then add it to the 366eda14cbcSMatt Macy * list of known pools. 367eda14cbcSMatt Macy */ 368eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 369eda14cbcSMatt Macy if (pe->pe_guid == pool_guid) 370eda14cbcSMatt Macy break; 371eda14cbcSMatt Macy } 372eda14cbcSMatt Macy 373eda14cbcSMatt Macy if (pe == NULL) { 374eda14cbcSMatt Macy if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 375eda14cbcSMatt Macy return (-1); 376eda14cbcSMatt Macy } 377eda14cbcSMatt Macy pe->pe_guid = pool_guid; 378eda14cbcSMatt Macy pe->pe_next = pl->pools; 379eda14cbcSMatt Macy pl->pools = pe; 380eda14cbcSMatt Macy } 381eda14cbcSMatt Macy 382eda14cbcSMatt Macy /* 383eda14cbcSMatt Macy * Second, see if we know about this toplevel vdev. Add it if its 384eda14cbcSMatt Macy * missing. 385eda14cbcSMatt Macy */ 386eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 387eda14cbcSMatt Macy if (ve->ve_guid == top_guid) 388eda14cbcSMatt Macy break; 389eda14cbcSMatt Macy } 390eda14cbcSMatt Macy 391eda14cbcSMatt Macy if (ve == NULL) { 392eda14cbcSMatt Macy if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 393eda14cbcSMatt Macy return (-1); 394eda14cbcSMatt Macy } 395eda14cbcSMatt Macy ve->ve_guid = top_guid; 396eda14cbcSMatt Macy ve->ve_next = pe->pe_vdevs; 397eda14cbcSMatt Macy pe->pe_vdevs = ve; 398eda14cbcSMatt Macy } 399eda14cbcSMatt Macy 400eda14cbcSMatt Macy /* 401eda14cbcSMatt Macy * Third, see if we have a config with a matching transaction group. If 402eda14cbcSMatt Macy * so, then we do nothing. Otherwise, add it to the list of known 403eda14cbcSMatt Macy * configs. 404eda14cbcSMatt Macy */ 405eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 406eda14cbcSMatt Macy if (ce->ce_txg == txg) 407eda14cbcSMatt Macy break; 408eda14cbcSMatt Macy } 409eda14cbcSMatt Macy 410eda14cbcSMatt Macy if (ce == NULL) { 411eda14cbcSMatt Macy if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) { 412eda14cbcSMatt Macy return (-1); 413eda14cbcSMatt Macy } 414eda14cbcSMatt Macy ce->ce_txg = txg; 415eda14cbcSMatt Macy ce->ce_config = fnvlist_dup(config); 416eda14cbcSMatt Macy ce->ce_next = ve->ve_configs; 417eda14cbcSMatt Macy ve->ve_configs = ce; 418eda14cbcSMatt Macy } 419eda14cbcSMatt Macy 420eda14cbcSMatt Macy /* 421eda14cbcSMatt Macy * At this point we've successfully added our config to the list of 422eda14cbcSMatt Macy * known configs. The last thing to do is add the vdev guid -> path 423eda14cbcSMatt Macy * mappings so that we can fix up the configuration as necessary before 424eda14cbcSMatt Macy * doing the import. 425eda14cbcSMatt Macy */ 426eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 427eda14cbcSMatt Macy return (-1); 428eda14cbcSMatt Macy 429eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 430eda14cbcSMatt Macy free(ne); 431eda14cbcSMatt Macy return (-1); 432eda14cbcSMatt Macy } 433eda14cbcSMatt Macy 434eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 435eda14cbcSMatt Macy ne->ne_order = order; 436eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 437eda14cbcSMatt Macy ne->ne_next = pl->names; 438eda14cbcSMatt Macy pl->names = ne; 439eda14cbcSMatt Macy 440eda14cbcSMatt Macy return (0); 441eda14cbcSMatt Macy } 442eda14cbcSMatt Macy 443eda14cbcSMatt Macy static int 444eda14cbcSMatt Macy zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid, 445eda14cbcSMatt Macy boolean_t *isactive) 446eda14cbcSMatt Macy { 447eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_pool_active != NULL); 448eda14cbcSMatt Macy 449eda14cbcSMatt Macy int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name, 450eda14cbcSMatt Macy guid, isactive); 451eda14cbcSMatt Macy 452eda14cbcSMatt Macy return (error); 453eda14cbcSMatt Macy } 454eda14cbcSMatt Macy 455eda14cbcSMatt Macy static nvlist_t * 456eda14cbcSMatt Macy zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig) 457eda14cbcSMatt Macy { 458eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_refresh_config != NULL); 459eda14cbcSMatt Macy 460eda14cbcSMatt Macy return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle, 461eda14cbcSMatt Macy tryconfig)); 462eda14cbcSMatt Macy } 463eda14cbcSMatt Macy 464eda14cbcSMatt Macy /* 465eda14cbcSMatt Macy * Determine if the vdev id is a hole in the namespace. 466eda14cbcSMatt Macy */ 467eda14cbcSMatt Macy static boolean_t 468eda14cbcSMatt Macy vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 469eda14cbcSMatt Macy { 470eda14cbcSMatt Macy int c; 471eda14cbcSMatt Macy 472eda14cbcSMatt Macy for (c = 0; c < holes; c++) { 473eda14cbcSMatt Macy 474eda14cbcSMatt Macy /* Top-level is a hole */ 475eda14cbcSMatt Macy if (hole_array[c] == id) 476eda14cbcSMatt Macy return (B_TRUE); 477eda14cbcSMatt Macy } 478eda14cbcSMatt Macy return (B_FALSE); 479eda14cbcSMatt Macy } 480eda14cbcSMatt Macy 481eda14cbcSMatt Macy /* 482eda14cbcSMatt Macy * Convert our list of pools into the definitive set of configurations. We 483eda14cbcSMatt Macy * start by picking the best config for each toplevel vdev. Once that's done, 484eda14cbcSMatt Macy * we assemble the toplevel vdevs into a full config for the pool. We make a 485eda14cbcSMatt Macy * pass to fix up any incorrect paths, and then add it to the main list to 486eda14cbcSMatt Macy * return to the user. 487eda14cbcSMatt Macy */ 488eda14cbcSMatt Macy static nvlist_t * 489eda14cbcSMatt Macy get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok, 490eda14cbcSMatt Macy nvlist_t *policy) 491eda14cbcSMatt Macy { 492eda14cbcSMatt Macy pool_entry_t *pe; 493eda14cbcSMatt Macy vdev_entry_t *ve; 494eda14cbcSMatt Macy config_entry_t *ce; 495eda14cbcSMatt Macy nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; 496eda14cbcSMatt Macy nvlist_t **spares, **l2cache; 497eda14cbcSMatt Macy uint_t i, nspares, nl2cache; 498eda14cbcSMatt Macy boolean_t config_seen; 499eda14cbcSMatt Macy uint64_t best_txg; 500eda14cbcSMatt Macy char *name, *hostname = NULL; 501eda14cbcSMatt Macy uint64_t guid; 502eda14cbcSMatt Macy uint_t children = 0; 503eda14cbcSMatt Macy nvlist_t **child = NULL; 504eda14cbcSMatt Macy uint64_t *hole_array, max_id; 505eda14cbcSMatt Macy uint_t c; 506eda14cbcSMatt Macy boolean_t isactive; 507eda14cbcSMatt Macy nvlist_t *nvl; 508eda14cbcSMatt Macy boolean_t valid_top_config = B_FALSE; 509eda14cbcSMatt Macy 510eda14cbcSMatt Macy if (nvlist_alloc(&ret, 0, 0) != 0) 511eda14cbcSMatt Macy goto nomem; 512eda14cbcSMatt Macy 513eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 514*dbd5678dSMartin Matuska uint64_t id, max_txg = 0, hostid = 0; 515*dbd5678dSMartin Matuska uint_t holes = 0; 516eda14cbcSMatt Macy 517eda14cbcSMatt Macy if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 518eda14cbcSMatt Macy goto nomem; 519eda14cbcSMatt Macy config_seen = B_FALSE; 520eda14cbcSMatt Macy 521eda14cbcSMatt Macy /* 522eda14cbcSMatt Macy * Iterate over all toplevel vdevs. Grab the pool configuration 523eda14cbcSMatt Macy * from the first one we find, and then go through the rest and 524eda14cbcSMatt Macy * add them as necessary to the 'vdevs' member of the config. 525eda14cbcSMatt Macy */ 526eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 527eda14cbcSMatt Macy 528eda14cbcSMatt Macy /* 529eda14cbcSMatt Macy * Determine the best configuration for this vdev by 530eda14cbcSMatt Macy * selecting the config with the latest transaction 531eda14cbcSMatt Macy * group. 532eda14cbcSMatt Macy */ 533eda14cbcSMatt Macy best_txg = 0; 534eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; 535eda14cbcSMatt Macy ce = ce->ce_next) { 536eda14cbcSMatt Macy 537eda14cbcSMatt Macy if (ce->ce_txg > best_txg) { 538eda14cbcSMatt Macy tmp = ce->ce_config; 539eda14cbcSMatt Macy best_txg = ce->ce_txg; 540eda14cbcSMatt Macy } 541eda14cbcSMatt Macy } 542eda14cbcSMatt Macy 543eda14cbcSMatt Macy /* 544eda14cbcSMatt Macy * We rely on the fact that the max txg for the 545eda14cbcSMatt Macy * pool will contain the most up-to-date information 546eda14cbcSMatt Macy * about the valid top-levels in the vdev namespace. 547eda14cbcSMatt Macy */ 548eda14cbcSMatt Macy if (best_txg > max_txg) { 549eda14cbcSMatt Macy (void) nvlist_remove(config, 550eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 551eda14cbcSMatt Macy DATA_TYPE_UINT64); 552eda14cbcSMatt Macy (void) nvlist_remove(config, 553eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 554eda14cbcSMatt Macy DATA_TYPE_UINT64_ARRAY); 555eda14cbcSMatt Macy 556eda14cbcSMatt Macy max_txg = best_txg; 557eda14cbcSMatt Macy hole_array = NULL; 558eda14cbcSMatt Macy holes = 0; 559eda14cbcSMatt Macy max_id = 0; 560eda14cbcSMatt Macy valid_top_config = B_FALSE; 561eda14cbcSMatt Macy 562eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 563eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 564eda14cbcSMatt Macy verify(nvlist_add_uint64(config, 565eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 566eda14cbcSMatt Macy max_id) == 0); 567eda14cbcSMatt Macy valid_top_config = B_TRUE; 568eda14cbcSMatt Macy } 569eda14cbcSMatt Macy 570eda14cbcSMatt Macy if (nvlist_lookup_uint64_array(tmp, 571eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 572eda14cbcSMatt Macy &holes) == 0) { 573eda14cbcSMatt Macy verify(nvlist_add_uint64_array(config, 574eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 575eda14cbcSMatt Macy hole_array, holes) == 0); 576eda14cbcSMatt Macy } 577eda14cbcSMatt Macy } 578eda14cbcSMatt Macy 579eda14cbcSMatt Macy if (!config_seen) { 580eda14cbcSMatt Macy /* 581eda14cbcSMatt Macy * Copy the relevant pieces of data to the pool 582eda14cbcSMatt Macy * configuration: 583eda14cbcSMatt Macy * 584eda14cbcSMatt Macy * version 585eda14cbcSMatt Macy * pool guid 586eda14cbcSMatt Macy * name 587eda14cbcSMatt Macy * comment (if available) 588ee36e25aSMartin Matuska * compatibility features (if available) 589eda14cbcSMatt Macy * pool state 590eda14cbcSMatt Macy * hostid (if available) 591eda14cbcSMatt Macy * hostname (if available) 592eda14cbcSMatt Macy */ 593eda14cbcSMatt Macy uint64_t state, version; 594eda14cbcSMatt Macy char *comment = NULL; 595ee36e25aSMartin Matuska char *compatibility = NULL; 596eda14cbcSMatt Macy 597eda14cbcSMatt Macy version = fnvlist_lookup_uint64(tmp, 598eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION); 599eda14cbcSMatt Macy fnvlist_add_uint64(config, 600eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION, version); 601eda14cbcSMatt Macy guid = fnvlist_lookup_uint64(tmp, 602eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID); 603eda14cbcSMatt Macy fnvlist_add_uint64(config, 604eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, guid); 605eda14cbcSMatt Macy name = fnvlist_lookup_string(tmp, 606eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME); 607eda14cbcSMatt Macy fnvlist_add_string(config, 608eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, name); 609eda14cbcSMatt Macy 610eda14cbcSMatt Macy if (nvlist_lookup_string(tmp, 611eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, &comment) == 0) 612eda14cbcSMatt Macy fnvlist_add_string(config, 613eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, comment); 614eda14cbcSMatt Macy 615ee36e25aSMartin Matuska if (nvlist_lookup_string(tmp, 616ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 617ee36e25aSMartin Matuska &compatibility) == 0) 618ee36e25aSMartin Matuska fnvlist_add_string(config, 619ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 620ee36e25aSMartin Matuska compatibility); 621ee36e25aSMartin Matuska 622eda14cbcSMatt Macy state = fnvlist_lookup_uint64(tmp, 623eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE); 624eda14cbcSMatt Macy fnvlist_add_uint64(config, 625eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, state); 626eda14cbcSMatt Macy 627eda14cbcSMatt Macy hostid = 0; 628eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 629eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 630eda14cbcSMatt Macy fnvlist_add_uint64(config, 631eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, hostid); 632eda14cbcSMatt Macy hostname = fnvlist_lookup_string(tmp, 633eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME); 634eda14cbcSMatt Macy fnvlist_add_string(config, 635eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME, hostname); 636eda14cbcSMatt Macy } 637eda14cbcSMatt Macy 638eda14cbcSMatt Macy config_seen = B_TRUE; 639eda14cbcSMatt Macy } 640eda14cbcSMatt Macy 641eda14cbcSMatt Macy /* 642eda14cbcSMatt Macy * Add this top-level vdev to the child array. 643eda14cbcSMatt Macy */ 644eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(tmp, 645eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 646eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 647eda14cbcSMatt Macy &id) == 0); 648eda14cbcSMatt Macy 649eda14cbcSMatt Macy if (id >= children) { 650eda14cbcSMatt Macy nvlist_t **newchild; 651eda14cbcSMatt Macy 652eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (id + 1) * 653eda14cbcSMatt Macy sizeof (nvlist_t *)); 654eda14cbcSMatt Macy if (newchild == NULL) 655eda14cbcSMatt Macy goto nomem; 656eda14cbcSMatt Macy 657eda14cbcSMatt Macy for (c = 0; c < children; c++) 658eda14cbcSMatt Macy newchild[c] = child[c]; 659eda14cbcSMatt Macy 660eda14cbcSMatt Macy free(child); 661eda14cbcSMatt Macy child = newchild; 662eda14cbcSMatt Macy children = id + 1; 663eda14cbcSMatt Macy } 664eda14cbcSMatt Macy if (nvlist_dup(nvtop, &child[id], 0) != 0) 665eda14cbcSMatt Macy goto nomem; 666eda14cbcSMatt Macy 667eda14cbcSMatt Macy } 668eda14cbcSMatt Macy 669eda14cbcSMatt Macy /* 670eda14cbcSMatt Macy * If we have information about all the top-levels then 671eda14cbcSMatt Macy * clean up the nvlist which we've constructed. This 672eda14cbcSMatt Macy * means removing any extraneous devices that are 673eda14cbcSMatt Macy * beyond the valid range or adding devices to the end 674eda14cbcSMatt Macy * of our array which appear to be missing. 675eda14cbcSMatt Macy */ 676eda14cbcSMatt Macy if (valid_top_config) { 677eda14cbcSMatt Macy if (max_id < children) { 678eda14cbcSMatt Macy for (c = max_id; c < children; c++) 679eda14cbcSMatt Macy nvlist_free(child[c]); 680eda14cbcSMatt Macy children = max_id; 681eda14cbcSMatt Macy } else if (max_id > children) { 682eda14cbcSMatt Macy nvlist_t **newchild; 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (max_id) * 685eda14cbcSMatt Macy sizeof (nvlist_t *)); 686eda14cbcSMatt Macy if (newchild == NULL) 687eda14cbcSMatt Macy goto nomem; 688eda14cbcSMatt Macy 689eda14cbcSMatt Macy for (c = 0; c < children; c++) 690eda14cbcSMatt Macy newchild[c] = child[c]; 691eda14cbcSMatt Macy 692eda14cbcSMatt Macy free(child); 693eda14cbcSMatt Macy child = newchild; 694eda14cbcSMatt Macy children = max_id; 695eda14cbcSMatt Macy } 696eda14cbcSMatt Macy } 697eda14cbcSMatt Macy 698eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 699eda14cbcSMatt Macy &guid) == 0); 700eda14cbcSMatt Macy 701eda14cbcSMatt Macy /* 702eda14cbcSMatt Macy * The vdev namespace may contain holes as a result of 703eda14cbcSMatt Macy * device removal. We must add them back into the vdev 704eda14cbcSMatt Macy * tree before we process any missing devices. 705eda14cbcSMatt Macy */ 706eda14cbcSMatt Macy if (holes > 0) { 707eda14cbcSMatt Macy ASSERT(valid_top_config); 708eda14cbcSMatt Macy 709eda14cbcSMatt Macy for (c = 0; c < children; c++) { 710eda14cbcSMatt Macy nvlist_t *holey; 711eda14cbcSMatt Macy 712eda14cbcSMatt Macy if (child[c] != NULL || 713eda14cbcSMatt Macy !vdev_is_hole(hole_array, holes, c)) 714eda14cbcSMatt Macy continue; 715eda14cbcSMatt Macy 716eda14cbcSMatt Macy if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 717eda14cbcSMatt Macy 0) != 0) 718eda14cbcSMatt Macy goto nomem; 719eda14cbcSMatt Macy 720eda14cbcSMatt Macy /* 721eda14cbcSMatt Macy * Holes in the namespace are treated as 722eda14cbcSMatt Macy * "hole" top-level vdevs and have a 723eda14cbcSMatt Macy * special flag set on them. 724eda14cbcSMatt Macy */ 725eda14cbcSMatt Macy if (nvlist_add_string(holey, 726eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 727eda14cbcSMatt Macy VDEV_TYPE_HOLE) != 0 || 728eda14cbcSMatt Macy nvlist_add_uint64(holey, 729eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 730eda14cbcSMatt Macy nvlist_add_uint64(holey, 731eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 732eda14cbcSMatt Macy nvlist_free(holey); 733eda14cbcSMatt Macy goto nomem; 734eda14cbcSMatt Macy } 735eda14cbcSMatt Macy child[c] = holey; 736eda14cbcSMatt Macy } 737eda14cbcSMatt Macy } 738eda14cbcSMatt Macy 739eda14cbcSMatt Macy /* 740eda14cbcSMatt Macy * Look for any missing top-level vdevs. If this is the case, 741eda14cbcSMatt Macy * create a faked up 'missing' vdev as a placeholder. We cannot 742eda14cbcSMatt Macy * simply compress the child array, because the kernel performs 743eda14cbcSMatt Macy * certain checks to make sure the vdev IDs match their location 744eda14cbcSMatt Macy * in the configuration. 745eda14cbcSMatt Macy */ 746eda14cbcSMatt Macy for (c = 0; c < children; c++) { 747eda14cbcSMatt Macy if (child[c] == NULL) { 748eda14cbcSMatt Macy nvlist_t *missing; 749eda14cbcSMatt Macy if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 750eda14cbcSMatt Macy 0) != 0) 751eda14cbcSMatt Macy goto nomem; 752eda14cbcSMatt Macy if (nvlist_add_string(missing, 753eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 754eda14cbcSMatt Macy VDEV_TYPE_MISSING) != 0 || 755eda14cbcSMatt Macy nvlist_add_uint64(missing, 756eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 757eda14cbcSMatt Macy nvlist_add_uint64(missing, 758eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 759eda14cbcSMatt Macy nvlist_free(missing); 760eda14cbcSMatt Macy goto nomem; 761eda14cbcSMatt Macy } 762eda14cbcSMatt Macy child[c] = missing; 763eda14cbcSMatt Macy } 764eda14cbcSMatt Macy } 765eda14cbcSMatt Macy 766eda14cbcSMatt Macy /* 767eda14cbcSMatt Macy * Put all of this pool's top-level vdevs into a root vdev. 768eda14cbcSMatt Macy */ 769eda14cbcSMatt Macy if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 770eda14cbcSMatt Macy goto nomem; 771eda14cbcSMatt Macy if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 772eda14cbcSMatt Macy VDEV_TYPE_ROOT) != 0 || 773eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 774eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 775eda14cbcSMatt Macy nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 776681ce946SMartin Matuska (const nvlist_t **)child, children) != 0) { 777eda14cbcSMatt Macy nvlist_free(nvroot); 778eda14cbcSMatt Macy goto nomem; 779eda14cbcSMatt Macy } 780eda14cbcSMatt Macy 781eda14cbcSMatt Macy for (c = 0; c < children; c++) 782eda14cbcSMatt Macy nvlist_free(child[c]); 783eda14cbcSMatt Macy free(child); 784eda14cbcSMatt Macy children = 0; 785eda14cbcSMatt Macy child = NULL; 786eda14cbcSMatt Macy 787eda14cbcSMatt Macy /* 788eda14cbcSMatt Macy * Go through and fix up any paths and/or devids based on our 789eda14cbcSMatt Macy * known list of vdev GUID -> path mappings. 790eda14cbcSMatt Macy */ 791eda14cbcSMatt Macy if (fix_paths(hdl, nvroot, pl->names) != 0) { 792eda14cbcSMatt Macy nvlist_free(nvroot); 793eda14cbcSMatt Macy goto nomem; 794eda14cbcSMatt Macy } 795eda14cbcSMatt Macy 796eda14cbcSMatt Macy /* 797eda14cbcSMatt Macy * Add the root vdev to this pool's configuration. 798eda14cbcSMatt Macy */ 799eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 800eda14cbcSMatt Macy nvroot) != 0) { 801eda14cbcSMatt Macy nvlist_free(nvroot); 802eda14cbcSMatt Macy goto nomem; 803eda14cbcSMatt Macy } 804eda14cbcSMatt Macy nvlist_free(nvroot); 805eda14cbcSMatt Macy 806eda14cbcSMatt Macy /* 807eda14cbcSMatt Macy * zdb uses this path to report on active pools that were 808eda14cbcSMatt Macy * imported or created using -R. 809eda14cbcSMatt Macy */ 810eda14cbcSMatt Macy if (active_ok) 811eda14cbcSMatt Macy goto add_pool; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy /* 814eda14cbcSMatt Macy * Determine if this pool is currently active, in which case we 815eda14cbcSMatt Macy * can't actually import it. 816eda14cbcSMatt Macy */ 817eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 818eda14cbcSMatt Macy &name) == 0); 819eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 820eda14cbcSMatt Macy &guid) == 0); 821eda14cbcSMatt Macy 822eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, guid, &isactive) != 0) 823eda14cbcSMatt Macy goto error; 824eda14cbcSMatt Macy 825eda14cbcSMatt Macy if (isactive) { 826eda14cbcSMatt Macy nvlist_free(config); 827eda14cbcSMatt Macy config = NULL; 828eda14cbcSMatt Macy continue; 829eda14cbcSMatt Macy } 830eda14cbcSMatt Macy 831eda14cbcSMatt Macy if (policy != NULL) { 832eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 833eda14cbcSMatt Macy policy) != 0) 834eda14cbcSMatt Macy goto nomem; 835eda14cbcSMatt Macy } 836eda14cbcSMatt Macy 837eda14cbcSMatt Macy if ((nvl = zutil_refresh_config(hdl, config)) == NULL) { 838eda14cbcSMatt Macy nvlist_free(config); 839eda14cbcSMatt Macy config = NULL; 840eda14cbcSMatt Macy continue; 841eda14cbcSMatt Macy } 842eda14cbcSMatt Macy 843eda14cbcSMatt Macy nvlist_free(config); 844eda14cbcSMatt Macy config = nvl; 845eda14cbcSMatt Macy 846eda14cbcSMatt Macy /* 847eda14cbcSMatt Macy * Go through and update the paths for spares, now that we have 848eda14cbcSMatt Macy * them. 849eda14cbcSMatt Macy */ 850eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 851eda14cbcSMatt Macy &nvroot) == 0); 852eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 853eda14cbcSMatt Macy &spares, &nspares) == 0) { 854eda14cbcSMatt Macy for (i = 0; i < nspares; i++) { 855eda14cbcSMatt Macy if (fix_paths(hdl, spares[i], pl->names) != 0) 856eda14cbcSMatt Macy goto nomem; 857eda14cbcSMatt Macy } 858eda14cbcSMatt Macy } 859eda14cbcSMatt Macy 860eda14cbcSMatt Macy /* 861eda14cbcSMatt Macy * Update the paths for l2cache devices. 862eda14cbcSMatt Macy */ 863eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 864eda14cbcSMatt Macy &l2cache, &nl2cache) == 0) { 865eda14cbcSMatt Macy for (i = 0; i < nl2cache; i++) { 866eda14cbcSMatt Macy if (fix_paths(hdl, l2cache[i], pl->names) != 0) 867eda14cbcSMatt Macy goto nomem; 868eda14cbcSMatt Macy } 869eda14cbcSMatt Macy } 870eda14cbcSMatt Macy 871eda14cbcSMatt Macy /* 872eda14cbcSMatt Macy * Restore the original information read from the actual label. 873eda14cbcSMatt Macy */ 874eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 875eda14cbcSMatt Macy DATA_TYPE_UINT64); 876eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 877eda14cbcSMatt Macy DATA_TYPE_STRING); 878eda14cbcSMatt Macy if (hostid != 0) { 879eda14cbcSMatt Macy verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 880eda14cbcSMatt Macy hostid) == 0); 881eda14cbcSMatt Macy verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 882eda14cbcSMatt Macy hostname) == 0); 883eda14cbcSMatt Macy } 884eda14cbcSMatt Macy 885eda14cbcSMatt Macy add_pool: 886eda14cbcSMatt Macy /* 887eda14cbcSMatt Macy * Add this pool to the list of configs. 888eda14cbcSMatt Macy */ 889eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 890eda14cbcSMatt Macy &name) == 0); 891eda14cbcSMatt Macy 892eda14cbcSMatt Macy if (nvlist_add_nvlist(ret, name, config) != 0) 893eda14cbcSMatt Macy goto nomem; 894eda14cbcSMatt Macy 895eda14cbcSMatt Macy nvlist_free(config); 896eda14cbcSMatt Macy config = NULL; 897eda14cbcSMatt Macy } 898eda14cbcSMatt Macy 899eda14cbcSMatt Macy return (ret); 900eda14cbcSMatt Macy 901eda14cbcSMatt Macy nomem: 902eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 903eda14cbcSMatt Macy error: 904eda14cbcSMatt Macy nvlist_free(config); 905eda14cbcSMatt Macy nvlist_free(ret); 906eda14cbcSMatt Macy for (c = 0; c < children; c++) 907eda14cbcSMatt Macy nvlist_free(child[c]); 908eda14cbcSMatt Macy free(child); 909eda14cbcSMatt Macy 910eda14cbcSMatt Macy return (NULL); 911eda14cbcSMatt Macy } 912eda14cbcSMatt Macy 913eda14cbcSMatt Macy /* 914eda14cbcSMatt Macy * Return the offset of the given label. 915eda14cbcSMatt Macy */ 916eda14cbcSMatt Macy static uint64_t 917eda14cbcSMatt Macy label_offset(uint64_t size, int l) 918eda14cbcSMatt Macy { 919eda14cbcSMatt Macy ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 920eda14cbcSMatt Macy return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 921eda14cbcSMatt Macy 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 922eda14cbcSMatt Macy } 923eda14cbcSMatt Macy 924eda14cbcSMatt Macy /* 92516038816SMartin Matuska * The same description applies as to zpool_read_label below, 92616038816SMartin Matuska * except here we do it without aio, presumably because an aio call 92716038816SMartin Matuska * errored out in a way we think not using it could circumvent. 92816038816SMartin Matuska */ 92916038816SMartin Matuska static int 93016038816SMartin Matuska zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels) 93116038816SMartin Matuska { 93216038816SMartin Matuska struct stat64 statbuf; 93316038816SMartin Matuska int l, count = 0; 93416038816SMartin Matuska vdev_phys_t *label; 93516038816SMartin Matuska nvlist_t *expected_config = NULL; 93616038816SMartin Matuska uint64_t expected_guid = 0, size; 93716038816SMartin Matuska 93816038816SMartin Matuska *config = NULL; 93916038816SMartin Matuska 94016038816SMartin Matuska if (fstat64_blk(fd, &statbuf) == -1) 94116038816SMartin Matuska return (0); 94216038816SMartin Matuska size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 94316038816SMartin Matuska 944*dbd5678dSMartin Matuska label = (vdev_phys_t *)umem_alloc_aligned(sizeof (*label), PAGESIZE, 945*dbd5678dSMartin Matuska UMEM_DEFAULT); 946*dbd5678dSMartin Matuska if (label == NULL) 94716038816SMartin Matuska return (-1); 94816038816SMartin Matuska 94916038816SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 95016038816SMartin Matuska uint64_t state, guid, txg; 95116038816SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 95216038816SMartin Matuska 95316038816SMartin Matuska if (pread64(fd, label, sizeof (vdev_phys_t), 95416038816SMartin Matuska offset) != sizeof (vdev_phys_t)) 95516038816SMartin Matuska continue; 95616038816SMartin Matuska 95716038816SMartin Matuska if (nvlist_unpack(label->vp_nvlist, 95816038816SMartin Matuska sizeof (label->vp_nvlist), config, 0) != 0) 95916038816SMartin Matuska continue; 96016038816SMartin Matuska 96116038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 96216038816SMartin Matuska &guid) != 0 || guid == 0) { 96316038816SMartin Matuska nvlist_free(*config); 96416038816SMartin Matuska continue; 96516038816SMartin Matuska } 96616038816SMartin Matuska 96716038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 96816038816SMartin Matuska &state) != 0 || state > POOL_STATE_L2CACHE) { 96916038816SMartin Matuska nvlist_free(*config); 97016038816SMartin Matuska continue; 97116038816SMartin Matuska } 97216038816SMartin Matuska 97316038816SMartin Matuska if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 97416038816SMartin Matuska (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 97516038816SMartin Matuska &txg) != 0 || txg == 0)) { 97616038816SMartin Matuska nvlist_free(*config); 97716038816SMartin Matuska continue; 97816038816SMartin Matuska } 97916038816SMartin Matuska 98016038816SMartin Matuska if (expected_guid) { 98116038816SMartin Matuska if (expected_guid == guid) 98216038816SMartin Matuska count++; 98316038816SMartin Matuska 98416038816SMartin Matuska nvlist_free(*config); 98516038816SMartin Matuska } else { 98616038816SMartin Matuska expected_config = *config; 98716038816SMartin Matuska expected_guid = guid; 98816038816SMartin Matuska count++; 98916038816SMartin Matuska } 99016038816SMartin Matuska } 99116038816SMartin Matuska 99216038816SMartin Matuska if (num_labels != NULL) 99316038816SMartin Matuska *num_labels = count; 99416038816SMartin Matuska 995*dbd5678dSMartin Matuska umem_free_aligned(label, sizeof (*label)); 99616038816SMartin Matuska *config = expected_config; 99716038816SMartin Matuska 99816038816SMartin Matuska return (0); 99916038816SMartin Matuska } 100016038816SMartin Matuska 100116038816SMartin Matuska /* 1002eda14cbcSMatt Macy * Given a file descriptor, read the label information and return an nvlist 1003eda14cbcSMatt Macy * describing the configuration, if there is one. The number of valid 1004eda14cbcSMatt Macy * labels found will be returned in num_labels when non-NULL. 1005eda14cbcSMatt Macy */ 1006eda14cbcSMatt Macy int 1007eda14cbcSMatt Macy zpool_read_label(int fd, nvlist_t **config, int *num_labels) 1008eda14cbcSMatt Macy { 1009da5137abSMartin Matuska #ifndef HAVE_AIO_H 1010da5137abSMartin Matuska return (zpool_read_label_slow(fd, config, num_labels)); 1011da5137abSMartin Matuska #else 1012eda14cbcSMatt Macy struct stat64 statbuf; 1013184c1b94SMartin Matuska struct aiocb aiocbs[VDEV_LABELS]; 1014184c1b94SMartin Matuska struct aiocb *aiocbps[VDEV_LABELS]; 1015184c1b94SMartin Matuska vdev_phys_t *labels; 1016eda14cbcSMatt Macy nvlist_t *expected_config = NULL; 1017eda14cbcSMatt Macy uint64_t expected_guid = 0, size; 1018184c1b94SMartin Matuska int error, l, count = 0; 1019eda14cbcSMatt Macy 1020eda14cbcSMatt Macy *config = NULL; 1021eda14cbcSMatt Macy 1022eda14cbcSMatt Macy if (fstat64_blk(fd, &statbuf) == -1) 1023eda14cbcSMatt Macy return (0); 1024eda14cbcSMatt Macy size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 1025eda14cbcSMatt Macy 1026*dbd5678dSMartin Matuska labels = (vdev_phys_t *)umem_alloc_aligned( 1027*dbd5678dSMartin Matuska VDEV_LABELS * sizeof (*labels), PAGESIZE, UMEM_DEFAULT); 1028*dbd5678dSMartin Matuska if (labels == NULL) 1029eda14cbcSMatt Macy return (-1); 1030eda14cbcSMatt Macy 1031184c1b94SMartin Matuska memset(aiocbs, 0, sizeof (aiocbs)); 1032184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 1033184c1b94SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 1034184c1b94SMartin Matuska 1035184c1b94SMartin Matuska aiocbs[l].aio_fildes = fd; 1036184c1b94SMartin Matuska aiocbs[l].aio_offset = offset; 1037184c1b94SMartin Matuska aiocbs[l].aio_buf = &labels[l]; 1038184c1b94SMartin Matuska aiocbs[l].aio_nbytes = sizeof (vdev_phys_t); 1039184c1b94SMartin Matuska aiocbs[l].aio_lio_opcode = LIO_READ; 1040184c1b94SMartin Matuska aiocbps[l] = &aiocbs[l]; 1041184c1b94SMartin Matuska } 1042184c1b94SMartin Matuska 1043184c1b94SMartin Matuska if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) { 1044184c1b94SMartin Matuska int saved_errno = errno; 104516038816SMartin Matuska boolean_t do_slow = B_FALSE; 104616038816SMartin Matuska error = -1; 1047184c1b94SMartin Matuska 1048184c1b94SMartin Matuska if (errno == EAGAIN || errno == EINTR || errno == EIO) { 1049184c1b94SMartin Matuska /* 1050184c1b94SMartin Matuska * A portion of the requests may have been submitted. 1051184c1b94SMartin Matuska * Clean them up. 1052184c1b94SMartin Matuska */ 1053184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 1054184c1b94SMartin Matuska errno = 0; 105516038816SMartin Matuska switch (aio_error(&aiocbs[l])) { 105616038816SMartin Matuska case EINVAL: 105716038816SMartin Matuska break; 105816038816SMartin Matuska case EINPROGRESS: 105916038816SMartin Matuska // This shouldn't be possible to 106016038816SMartin Matuska // encounter, die if we do. 106116038816SMartin Matuska ASSERT(B_FALSE); 1062c03c5b1cSMartin Matuska zfs_fallthrough; 106316038816SMartin Matuska case EOPNOTSUPP: 106416038816SMartin Matuska case ENOSYS: 106516038816SMartin Matuska do_slow = B_TRUE; 1066c03c5b1cSMartin Matuska zfs_fallthrough; 106716038816SMartin Matuska case 0: 106816038816SMartin Matuska default: 1069184c1b94SMartin Matuska (void) aio_return(&aiocbs[l]); 1070184c1b94SMartin Matuska } 1071184c1b94SMartin Matuska } 107216038816SMartin Matuska } 107316038816SMartin Matuska if (do_slow) { 107416038816SMartin Matuska /* 107516038816SMartin Matuska * At least some IO involved access unsafe-for-AIO 107616038816SMartin Matuska * files. Let's try again, without AIO this time. 107716038816SMartin Matuska */ 107816038816SMartin Matuska error = zpool_read_label_slow(fd, config, num_labels); 107916038816SMartin Matuska saved_errno = errno; 108016038816SMartin Matuska } 1081*dbd5678dSMartin Matuska umem_free_aligned(labels, VDEV_LABELS * sizeof (*labels)); 1082184c1b94SMartin Matuska errno = saved_errno; 108316038816SMartin Matuska return (error); 1084184c1b94SMartin Matuska } 1085184c1b94SMartin Matuska 1086eda14cbcSMatt Macy for (l = 0; l < VDEV_LABELS; l++) { 1087eda14cbcSMatt Macy uint64_t state, guid, txg; 1088eda14cbcSMatt Macy 1089184c1b94SMartin Matuska if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t)) 1090eda14cbcSMatt Macy continue; 1091eda14cbcSMatt Macy 1092184c1b94SMartin Matuska if (nvlist_unpack(labels[l].vp_nvlist, 1093184c1b94SMartin Matuska sizeof (labels[l].vp_nvlist), config, 0) != 0) 1094eda14cbcSMatt Macy continue; 1095eda14cbcSMatt Macy 1096eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 1097eda14cbcSMatt Macy &guid) != 0 || guid == 0) { 1098eda14cbcSMatt Macy nvlist_free(*config); 1099eda14cbcSMatt Macy continue; 1100eda14cbcSMatt Macy } 1101eda14cbcSMatt Macy 1102eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 1103eda14cbcSMatt Macy &state) != 0 || state > POOL_STATE_L2CACHE) { 1104eda14cbcSMatt Macy nvlist_free(*config); 1105eda14cbcSMatt Macy continue; 1106eda14cbcSMatt Macy } 1107eda14cbcSMatt Macy 1108eda14cbcSMatt Macy if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 1109eda14cbcSMatt Macy (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 1110eda14cbcSMatt Macy &txg) != 0 || txg == 0)) { 1111eda14cbcSMatt Macy nvlist_free(*config); 1112eda14cbcSMatt Macy continue; 1113eda14cbcSMatt Macy } 1114eda14cbcSMatt Macy 1115eda14cbcSMatt Macy if (expected_guid) { 1116eda14cbcSMatt Macy if (expected_guid == guid) 1117eda14cbcSMatt Macy count++; 1118eda14cbcSMatt Macy 1119eda14cbcSMatt Macy nvlist_free(*config); 1120eda14cbcSMatt Macy } else { 1121eda14cbcSMatt Macy expected_config = *config; 1122eda14cbcSMatt Macy expected_guid = guid; 1123eda14cbcSMatt Macy count++; 1124eda14cbcSMatt Macy } 1125eda14cbcSMatt Macy } 1126eda14cbcSMatt Macy 1127eda14cbcSMatt Macy if (num_labels != NULL) 1128eda14cbcSMatt Macy *num_labels = count; 1129eda14cbcSMatt Macy 1130*dbd5678dSMartin Matuska umem_free_aligned(labels, VDEV_LABELS * sizeof (*labels)); 1131eda14cbcSMatt Macy *config = expected_config; 1132eda14cbcSMatt Macy 1133eda14cbcSMatt Macy return (0); 1134da5137abSMartin Matuska #endif 1135eda14cbcSMatt Macy } 1136eda14cbcSMatt Macy 1137eda14cbcSMatt Macy /* 1138eda14cbcSMatt Macy * Sorted by full path and then vdev guid to allow for multiple entries with 1139eda14cbcSMatt Macy * the same full path name. This is required because it's possible to 1140eda14cbcSMatt Macy * have multiple block devices with labels that refer to the same 1141eda14cbcSMatt Macy * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both 1142eda14cbcSMatt Macy * entries need to be added to the cache. Scenarios where this can occur 1143eda14cbcSMatt Macy * include overwritten pool labels, devices which are visible from multiple 1144eda14cbcSMatt Macy * hosts and multipath devices. 1145eda14cbcSMatt Macy */ 1146eda14cbcSMatt Macy int 1147eda14cbcSMatt Macy slice_cache_compare(const void *arg1, const void *arg2) 1148eda14cbcSMatt Macy { 1149eda14cbcSMatt Macy const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; 1150eda14cbcSMatt Macy const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; 1151eda14cbcSMatt Macy uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid; 1152eda14cbcSMatt Macy uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid; 1153eda14cbcSMatt Macy int rv; 1154eda14cbcSMatt Macy 1155eda14cbcSMatt Macy rv = TREE_ISIGN(strcmp(nm1, nm2)); 1156eda14cbcSMatt Macy if (rv) 1157eda14cbcSMatt Macy return (rv); 1158eda14cbcSMatt Macy 1159eda14cbcSMatt Macy return (TREE_CMP(guid1, guid2)); 1160eda14cbcSMatt Macy } 1161eda14cbcSMatt Macy 1162eda14cbcSMatt Macy static int 1163eda14cbcSMatt Macy label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid, 1164eda14cbcSMatt Macy uint64_t vdev_guid, char **path, char **devid) 1165eda14cbcSMatt Macy { 1166eda14cbcSMatt Macy nvlist_t **child; 1167eda14cbcSMatt Macy uint_t c, children; 1168eda14cbcSMatt Macy uint64_t guid; 1169eda14cbcSMatt Macy char *val; 1170eda14cbcSMatt Macy int error; 1171eda14cbcSMatt Macy 1172eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1173eda14cbcSMatt Macy &child, &children) == 0) { 1174eda14cbcSMatt Macy for (c = 0; c < children; c++) { 1175eda14cbcSMatt Macy error = label_paths_impl(hdl, child[c], 1176eda14cbcSMatt Macy pool_guid, vdev_guid, path, devid); 1177eda14cbcSMatt Macy if (error) 1178eda14cbcSMatt Macy return (error); 1179eda14cbcSMatt Macy } 1180eda14cbcSMatt Macy return (0); 1181eda14cbcSMatt Macy } 1182eda14cbcSMatt Macy 1183eda14cbcSMatt Macy if (nvroot == NULL) 1184eda14cbcSMatt Macy return (0); 1185eda14cbcSMatt Macy 1186eda14cbcSMatt Macy error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid); 1187eda14cbcSMatt Macy if ((error != 0) || (guid != vdev_guid)) 1188eda14cbcSMatt Macy return (0); 1189eda14cbcSMatt Macy 1190eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val); 1191eda14cbcSMatt Macy if (error == 0) 1192eda14cbcSMatt Macy *path = val; 1193eda14cbcSMatt Macy 1194eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val); 1195eda14cbcSMatt Macy if (error == 0) 1196eda14cbcSMatt Macy *devid = val; 1197eda14cbcSMatt Macy 1198eda14cbcSMatt Macy return (0); 1199eda14cbcSMatt Macy } 1200eda14cbcSMatt Macy 1201eda14cbcSMatt Macy /* 1202eda14cbcSMatt Macy * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID 1203eda14cbcSMatt Macy * and store these strings as config_path and devid_path respectively. 1204eda14cbcSMatt Macy * The returned pointers are only valid as long as label remains valid. 1205eda14cbcSMatt Macy */ 1206eda14cbcSMatt Macy int 1207eda14cbcSMatt Macy label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid) 1208eda14cbcSMatt Macy { 1209eda14cbcSMatt Macy nvlist_t *nvroot; 1210eda14cbcSMatt Macy uint64_t pool_guid; 1211eda14cbcSMatt Macy uint64_t vdev_guid; 1212eda14cbcSMatt Macy 1213eda14cbcSMatt Macy *path = NULL; 1214eda14cbcSMatt Macy *devid = NULL; 1215eda14cbcSMatt Macy 1216eda14cbcSMatt Macy if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1217eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) || 1218eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid)) 1219eda14cbcSMatt Macy return (ENOENT); 1220eda14cbcSMatt Macy 1221eda14cbcSMatt Macy return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path, 1222eda14cbcSMatt Macy devid)); 1223eda14cbcSMatt Macy } 1224eda14cbcSMatt Macy 1225eda14cbcSMatt Macy static void 1226eda14cbcSMatt Macy zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock, 1227eda14cbcSMatt Macy avl_tree_t *cache, const char *path, const char *name, int order) 1228eda14cbcSMatt Macy { 1229eda14cbcSMatt Macy avl_index_t where; 1230eda14cbcSMatt Macy rdsk_node_t *slice; 1231eda14cbcSMatt Macy 1232eda14cbcSMatt Macy slice = zutil_alloc(hdl, sizeof (rdsk_node_t)); 1233eda14cbcSMatt Macy if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) { 1234eda14cbcSMatt Macy free(slice); 1235eda14cbcSMatt Macy return; 1236eda14cbcSMatt Macy } 1237eda14cbcSMatt Macy slice->rn_vdev_guid = 0; 1238eda14cbcSMatt Macy slice->rn_lock = lock; 1239eda14cbcSMatt Macy slice->rn_avl = cache; 1240eda14cbcSMatt Macy slice->rn_hdl = hdl; 1241eda14cbcSMatt Macy slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET; 1242eda14cbcSMatt Macy slice->rn_labelpaths = B_FALSE; 1243eda14cbcSMatt Macy 1244eda14cbcSMatt Macy pthread_mutex_lock(lock); 1245eda14cbcSMatt Macy if (avl_find(cache, slice, &where)) { 1246eda14cbcSMatt Macy free(slice->rn_name); 1247eda14cbcSMatt Macy free(slice); 1248eda14cbcSMatt Macy } else { 1249eda14cbcSMatt Macy avl_insert(cache, slice, where); 1250eda14cbcSMatt Macy } 1251eda14cbcSMatt Macy pthread_mutex_unlock(lock); 1252eda14cbcSMatt Macy } 1253eda14cbcSMatt Macy 1254eda14cbcSMatt Macy static int 1255eda14cbcSMatt Macy zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock, 1256eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1257eda14cbcSMatt Macy { 1258eda14cbcSMatt Macy int error; 1259eda14cbcSMatt Macy char path[MAXPATHLEN]; 1260eda14cbcSMatt Macy struct dirent64 *dp; 1261eda14cbcSMatt Macy DIR *dirp; 1262eda14cbcSMatt Macy 1263eda14cbcSMatt Macy if (realpath(dir, path) == NULL) { 1264eda14cbcSMatt Macy error = errno; 1265eda14cbcSMatt Macy if (error == ENOENT) 1266eda14cbcSMatt Macy return (0); 1267eda14cbcSMatt Macy 12681f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1269*dbd5678dSMartin Matuska (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN, 1270*dbd5678dSMartin Matuska "cannot resolve path '%s'"), dir); 1271eda14cbcSMatt Macy return (error); 1272eda14cbcSMatt Macy } 1273eda14cbcSMatt Macy 1274eda14cbcSMatt Macy dirp = opendir(path); 1275eda14cbcSMatt Macy if (dirp == NULL) { 1276eda14cbcSMatt Macy error = errno; 12771f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1278*dbd5678dSMartin Matuska (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN, 1279*dbd5678dSMartin Matuska "cannot open '%s'"), path); 1280eda14cbcSMatt Macy return (error); 1281eda14cbcSMatt Macy } 1282eda14cbcSMatt Macy 1283eda14cbcSMatt Macy while ((dp = readdir64(dirp)) != NULL) { 1284eda14cbcSMatt Macy const char *name = dp->d_name; 12853ff01b23SMartin Matuska if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) 1286eda14cbcSMatt Macy continue; 1287eda14cbcSMatt Macy 12883ff01b23SMartin Matuska switch (dp->d_type) { 12893ff01b23SMartin Matuska case DT_UNKNOWN: 12903ff01b23SMartin Matuska case DT_BLK: 12910d8fe237SMartin Matuska case DT_LNK: 12923ff01b23SMartin Matuska #ifdef __FreeBSD__ 12933ff01b23SMartin Matuska case DT_CHR: 12943ff01b23SMartin Matuska #endif 12953ff01b23SMartin Matuska case DT_REG: 12963ff01b23SMartin Matuska break; 12973ff01b23SMartin Matuska default: 12983ff01b23SMartin Matuska continue; 12993ff01b23SMartin Matuska } 13003ff01b23SMartin Matuska 1301eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, 1302eda14cbcSMatt Macy order); 1303eda14cbcSMatt Macy } 1304eda14cbcSMatt Macy 1305eda14cbcSMatt Macy (void) closedir(dirp); 1306eda14cbcSMatt Macy return (0); 1307eda14cbcSMatt Macy } 1308eda14cbcSMatt Macy 1309eda14cbcSMatt Macy static int 1310eda14cbcSMatt Macy zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock, 1311eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1312eda14cbcSMatt Macy { 1313eda14cbcSMatt Macy int error = 0; 1314eda14cbcSMatt Macy char path[MAXPATHLEN]; 13153ff01b23SMartin Matuska char *d = NULL; 13163ff01b23SMartin Matuska ssize_t dl; 13173ff01b23SMartin Matuska const char *dpath, *name; 1318eda14cbcSMatt Macy 1319eda14cbcSMatt Macy /* 13203ff01b23SMartin Matuska * Separate the directory and the basename. 13213ff01b23SMartin Matuska * We do this so that we can get the realpath of 1322eda14cbcSMatt Macy * the directory. We don't get the realpath on the 1323eda14cbcSMatt Macy * whole path because if it's a symlink, we want the 1324eda14cbcSMatt Macy * path of the symlink not where it points to. 1325eda14cbcSMatt Macy */ 13263ff01b23SMartin Matuska name = zfs_basename(dir); 13273ff01b23SMartin Matuska if ((dl = zfs_dirnamelen(dir)) == -1) 13283ff01b23SMartin Matuska dpath = "."; 13293ff01b23SMartin Matuska else 13303ff01b23SMartin Matuska dpath = d = zutil_strndup(hdl, dir, dl); 1331eda14cbcSMatt Macy 1332eda14cbcSMatt Macy if (realpath(dpath, path) == NULL) { 1333eda14cbcSMatt Macy error = errno; 1334eda14cbcSMatt Macy if (error == ENOENT) { 1335eda14cbcSMatt Macy error = 0; 1336eda14cbcSMatt Macy goto out; 1337eda14cbcSMatt Macy } 1338eda14cbcSMatt Macy 13391f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1340*dbd5678dSMartin Matuska (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN, 1341*dbd5678dSMartin Matuska "cannot resolve path '%s'"), dir); 1342eda14cbcSMatt Macy goto out; 1343eda14cbcSMatt Macy } 1344eda14cbcSMatt Macy 1345eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order); 1346eda14cbcSMatt Macy 1347eda14cbcSMatt Macy out: 1348eda14cbcSMatt Macy free(d); 1349eda14cbcSMatt Macy return (error); 1350eda14cbcSMatt Macy } 1351eda14cbcSMatt Macy 1352eda14cbcSMatt Macy /* 1353eda14cbcSMatt Macy * Scan a list of directories for zfs devices. 1354eda14cbcSMatt Macy */ 1355eda14cbcSMatt Macy static int 1356eda14cbcSMatt Macy zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock, 1357eda14cbcSMatt Macy avl_tree_t **slice_cache, const char * const *dir, size_t dirs) 1358eda14cbcSMatt Macy { 1359eda14cbcSMatt Macy avl_tree_t *cache; 1360eda14cbcSMatt Macy rdsk_node_t *slice; 1361eda14cbcSMatt Macy void *cookie; 1362eda14cbcSMatt Macy int i, error; 1363eda14cbcSMatt Macy 1364eda14cbcSMatt Macy *slice_cache = NULL; 1365eda14cbcSMatt Macy cache = zutil_alloc(hdl, sizeof (avl_tree_t)); 1366eda14cbcSMatt Macy avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t), 1367eda14cbcSMatt Macy offsetof(rdsk_node_t, rn_node)); 1368eda14cbcSMatt Macy 1369eda14cbcSMatt Macy for (i = 0; i < dirs; i++) { 1370eda14cbcSMatt Macy struct stat sbuf; 1371eda14cbcSMatt Macy 1372eda14cbcSMatt Macy if (stat(dir[i], &sbuf) != 0) { 1373eda14cbcSMatt Macy error = errno; 1374eda14cbcSMatt Macy if (error == ENOENT) 1375eda14cbcSMatt Macy continue; 1376eda14cbcSMatt Macy 13771f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1378*dbd5678dSMartin Matuska (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext( 1379eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]); 1380eda14cbcSMatt Macy goto error; 1381eda14cbcSMatt Macy } 1382eda14cbcSMatt Macy 1383eda14cbcSMatt Macy /* 1384eda14cbcSMatt Macy * If dir[i] is a directory, we walk through it and add all 1385eda14cbcSMatt Macy * the entries to the cache. If it's not a directory, we just 1386eda14cbcSMatt Macy * add it to the cache. 1387eda14cbcSMatt Macy */ 1388eda14cbcSMatt Macy if (S_ISDIR(sbuf.st_mode)) { 1389eda14cbcSMatt Macy if ((error = zpool_find_import_scan_dir(hdl, lock, 1390eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1391eda14cbcSMatt Macy goto error; 1392eda14cbcSMatt Macy } else { 1393eda14cbcSMatt Macy if ((error = zpool_find_import_scan_path(hdl, lock, 1394eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1395eda14cbcSMatt Macy goto error; 1396eda14cbcSMatt Macy } 1397eda14cbcSMatt Macy } 1398eda14cbcSMatt Macy 1399eda14cbcSMatt Macy *slice_cache = cache; 1400eda14cbcSMatt Macy return (0); 1401eda14cbcSMatt Macy 1402eda14cbcSMatt Macy error: 1403eda14cbcSMatt Macy cookie = NULL; 1404eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1405eda14cbcSMatt Macy free(slice->rn_name); 1406eda14cbcSMatt Macy free(slice); 1407eda14cbcSMatt Macy } 1408eda14cbcSMatt Macy free(cache); 1409eda14cbcSMatt Macy 1410eda14cbcSMatt Macy return (error); 1411eda14cbcSMatt Macy } 1412eda14cbcSMatt Macy 1413eda14cbcSMatt Macy /* 1414eda14cbcSMatt Macy * Given a list of directories to search, find all pools stored on disk. This 1415eda14cbcSMatt Macy * includes partial pools which are not available to import. If no args are 1416eda14cbcSMatt Macy * given (argc is 0), then the default directory (/dev/dsk) is searched. 1417eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1418eda14cbcSMatt Macy * to import a specific pool. 1419eda14cbcSMatt Macy */ 1420eda14cbcSMatt Macy static nvlist_t * 14219db44a8eSMartin Matuska zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg, 14229db44a8eSMartin Matuska pthread_mutex_t *lock, avl_tree_t *cache) 1423eda14cbcSMatt Macy { 1424e92ffd9bSMartin Matuska (void) lock; 1425eda14cbcSMatt Macy nvlist_t *ret = NULL; 1426eda14cbcSMatt Macy pool_list_t pools = { 0 }; 1427eda14cbcSMatt Macy pool_entry_t *pe, *penext; 1428eda14cbcSMatt Macy vdev_entry_t *ve, *venext; 1429eda14cbcSMatt Macy config_entry_t *ce, *cenext; 1430eda14cbcSMatt Macy name_entry_t *ne, *nenext; 1431eda14cbcSMatt Macy rdsk_node_t *slice; 1432eda14cbcSMatt Macy void *cookie; 1433eda14cbcSMatt Macy tpool_t *t; 1434eda14cbcSMatt Macy 1435eda14cbcSMatt Macy verify(iarg->poolname == NULL || iarg->guid == 0); 1436eda14cbcSMatt Macy 1437eda14cbcSMatt Macy /* 1438eda14cbcSMatt Macy * Create a thread pool to parallelize the process of reading and 1439eda14cbcSMatt Macy * validating labels, a large number of threads can be used due to 1440eda14cbcSMatt Macy * minimal contention. 1441eda14cbcSMatt Macy */ 1442eda14cbcSMatt Macy t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL); 1443eda14cbcSMatt Macy for (slice = avl_first(cache); slice; 1444eda14cbcSMatt Macy (slice = avl_walk(cache, slice, AVL_AFTER))) 1445eda14cbcSMatt Macy (void) tpool_dispatch(t, zpool_open_func, slice); 1446eda14cbcSMatt Macy 1447eda14cbcSMatt Macy tpool_wait(t); 1448eda14cbcSMatt Macy tpool_destroy(t); 1449eda14cbcSMatt Macy 1450eda14cbcSMatt Macy /* 1451eda14cbcSMatt Macy * Process the cache, filtering out any entries which are not 1452eda14cbcSMatt Macy * for the specified pool then adding matching label configs. 1453eda14cbcSMatt Macy */ 1454eda14cbcSMatt Macy cookie = NULL; 1455eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1456eda14cbcSMatt Macy if (slice->rn_config != NULL) { 1457eda14cbcSMatt Macy nvlist_t *config = slice->rn_config; 1458eda14cbcSMatt Macy boolean_t matched = B_TRUE; 1459eda14cbcSMatt Macy boolean_t aux = B_FALSE; 1460eda14cbcSMatt Macy int fd; 1461eda14cbcSMatt Macy 1462eda14cbcSMatt Macy /* 1463eda14cbcSMatt Macy * Check if it's a spare or l2cache device. If it is, 1464eda14cbcSMatt Macy * we need to skip the name and guid check since they 1465eda14cbcSMatt Macy * don't exist on aux device label. 1466eda14cbcSMatt Macy */ 1467eda14cbcSMatt Macy if (iarg->poolname != NULL || iarg->guid != 0) { 1468eda14cbcSMatt Macy uint64_t state; 1469eda14cbcSMatt Macy aux = nvlist_lookup_uint64(config, 1470eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, &state) == 0 && 1471eda14cbcSMatt Macy (state == POOL_STATE_SPARE || 1472eda14cbcSMatt Macy state == POOL_STATE_L2CACHE); 1473eda14cbcSMatt Macy } 1474eda14cbcSMatt Macy 1475eda14cbcSMatt Macy if (iarg->poolname != NULL && !aux) { 1476eda14cbcSMatt Macy char *pname; 1477eda14cbcSMatt Macy 1478eda14cbcSMatt Macy matched = nvlist_lookup_string(config, 1479eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, &pname) == 0 && 1480eda14cbcSMatt Macy strcmp(iarg->poolname, pname) == 0; 1481eda14cbcSMatt Macy } else if (iarg->guid != 0 && !aux) { 1482eda14cbcSMatt Macy uint64_t this_guid; 1483eda14cbcSMatt Macy 1484eda14cbcSMatt Macy matched = nvlist_lookup_uint64(config, 1485eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 && 1486eda14cbcSMatt Macy iarg->guid == this_guid; 1487eda14cbcSMatt Macy } 1488eda14cbcSMatt Macy if (matched) { 1489eda14cbcSMatt Macy /* 1490eda14cbcSMatt Macy * Verify all remaining entries can be opened 1491eda14cbcSMatt Macy * exclusively. This will prune all underlying 1492eda14cbcSMatt Macy * multipath devices which otherwise could 1493eda14cbcSMatt Macy * result in the vdev appearing as UNAVAIL. 1494eda14cbcSMatt Macy * 1495eda14cbcSMatt Macy * Under zdb, this step isn't required and 1496eda14cbcSMatt Macy * would prevent a zdb -e of active pools with 1497eda14cbcSMatt Macy * no cachefile. 1498eda14cbcSMatt Macy */ 149916038816SMartin Matuska fd = open(slice->rn_name, 150016038816SMartin Matuska O_RDONLY | O_EXCL | O_CLOEXEC); 1501eda14cbcSMatt Macy if (fd >= 0 || iarg->can_be_active) { 1502eda14cbcSMatt Macy if (fd >= 0) 1503eda14cbcSMatt Macy close(fd); 1504eda14cbcSMatt Macy add_config(hdl, &pools, 1505eda14cbcSMatt Macy slice->rn_name, slice->rn_order, 1506eda14cbcSMatt Macy slice->rn_num_labels, config); 1507eda14cbcSMatt Macy } 1508eda14cbcSMatt Macy } 1509eda14cbcSMatt Macy nvlist_free(config); 1510eda14cbcSMatt Macy } 1511eda14cbcSMatt Macy free(slice->rn_name); 1512eda14cbcSMatt Macy free(slice); 1513eda14cbcSMatt Macy } 1514eda14cbcSMatt Macy avl_destroy(cache); 1515eda14cbcSMatt Macy free(cache); 1516eda14cbcSMatt Macy 1517eda14cbcSMatt Macy ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy); 1518eda14cbcSMatt Macy 1519eda14cbcSMatt Macy for (pe = pools.pools; pe != NULL; pe = penext) { 1520eda14cbcSMatt Macy penext = pe->pe_next; 1521eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 1522eda14cbcSMatt Macy venext = ve->ve_next; 1523eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 1524eda14cbcSMatt Macy cenext = ce->ce_next; 1525eda14cbcSMatt Macy nvlist_free(ce->ce_config); 1526eda14cbcSMatt Macy free(ce); 1527eda14cbcSMatt Macy } 1528eda14cbcSMatt Macy free(ve); 1529eda14cbcSMatt Macy } 1530eda14cbcSMatt Macy free(pe); 1531eda14cbcSMatt Macy } 1532eda14cbcSMatt Macy 1533eda14cbcSMatt Macy for (ne = pools.names; ne != NULL; ne = nenext) { 1534eda14cbcSMatt Macy nenext = ne->ne_next; 1535eda14cbcSMatt Macy free(ne->ne_name); 1536eda14cbcSMatt Macy free(ne); 1537eda14cbcSMatt Macy } 1538eda14cbcSMatt Macy 1539eda14cbcSMatt Macy return (ret); 1540eda14cbcSMatt Macy } 1541eda14cbcSMatt Macy 1542eda14cbcSMatt Macy /* 15439db44a8eSMartin Matuska * Given a config, discover the paths for the devices which 15449db44a8eSMartin Matuska * exist in the config. 15459db44a8eSMartin Matuska */ 15469db44a8eSMartin Matuska static int 15479db44a8eSMartin Matuska discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv, 15489db44a8eSMartin Matuska avl_tree_t *cache, pthread_mutex_t *lock) 15499db44a8eSMartin Matuska { 15509db44a8eSMartin Matuska char *path = NULL; 15513ff01b23SMartin Matuska ssize_t dl; 15529db44a8eSMartin Matuska uint_t children; 15539db44a8eSMartin Matuska nvlist_t **child; 15549db44a8eSMartin Matuska 15559db44a8eSMartin Matuska if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 15569db44a8eSMartin Matuska &child, &children) == 0) { 15579db44a8eSMartin Matuska for (int c = 0; c < children; c++) { 15589db44a8eSMartin Matuska discover_cached_paths(hdl, child[c], cache, lock); 15599db44a8eSMartin Matuska } 15609db44a8eSMartin Matuska } 15619db44a8eSMartin Matuska 15629db44a8eSMartin Matuska /* 15639db44a8eSMartin Matuska * Once we have the path, we need to add the directory to 156416038816SMartin Matuska * our directory cache. 15659db44a8eSMartin Matuska */ 15669db44a8eSMartin Matuska if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 15673ff01b23SMartin Matuska if ((dl = zfs_dirnamelen(path)) == -1) 1568a0b956f5SMartin Matuska path = (char *)"."; 15693ff01b23SMartin Matuska else 15703ff01b23SMartin Matuska path[dl] = '\0'; 15719db44a8eSMartin Matuska return (zpool_find_import_scan_dir(hdl, lock, cache, 15723ff01b23SMartin Matuska path, 0)); 15739db44a8eSMartin Matuska } 15749db44a8eSMartin Matuska return (0); 15759db44a8eSMartin Matuska } 15769db44a8eSMartin Matuska 15779db44a8eSMartin Matuska /* 1578eda14cbcSMatt Macy * Given a cache file, return the contents as a list of importable pools. 1579eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1580eda14cbcSMatt Macy * to import a specific pool. 1581eda14cbcSMatt Macy */ 1582eda14cbcSMatt Macy static nvlist_t * 15839db44a8eSMartin Matuska zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg) 1584eda14cbcSMatt Macy { 1585eda14cbcSMatt Macy char *buf; 1586eda14cbcSMatt Macy int fd; 1587eda14cbcSMatt Macy struct stat64 statbuf; 1588eda14cbcSMatt Macy nvlist_t *raw, *src, *dst; 1589eda14cbcSMatt Macy nvlist_t *pools; 1590eda14cbcSMatt Macy nvpair_t *elem; 1591eda14cbcSMatt Macy char *name; 1592eda14cbcSMatt Macy uint64_t this_guid; 1593eda14cbcSMatt Macy boolean_t active; 1594eda14cbcSMatt Macy 15959db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 1596eda14cbcSMatt Macy 159716038816SMartin Matuska if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) { 1598eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1599*dbd5678dSMartin Matuska (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN, 1600*dbd5678dSMartin Matuska "failed to open cache file")); 1601eda14cbcSMatt Macy return (NULL); 1602eda14cbcSMatt Macy } 1603eda14cbcSMatt Macy 1604eda14cbcSMatt Macy if (fstat64(fd, &statbuf) != 0) { 1605eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1606eda14cbcSMatt Macy (void) close(fd); 1607*dbd5678dSMartin Matuska (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN, 1608*dbd5678dSMartin Matuska "failed to get size of cache file")); 1609eda14cbcSMatt Macy return (NULL); 1610eda14cbcSMatt Macy } 1611eda14cbcSMatt Macy 1612eda14cbcSMatt Macy if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) { 1613eda14cbcSMatt Macy (void) close(fd); 1614eda14cbcSMatt Macy return (NULL); 1615eda14cbcSMatt Macy } 1616eda14cbcSMatt Macy 1617eda14cbcSMatt Macy if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 1618eda14cbcSMatt Macy (void) close(fd); 1619eda14cbcSMatt Macy free(buf); 1620*dbd5678dSMartin Matuska (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN, 1621eda14cbcSMatt Macy "failed to read cache file contents")); 1622eda14cbcSMatt Macy return (NULL); 1623eda14cbcSMatt Macy } 1624eda14cbcSMatt Macy 1625eda14cbcSMatt Macy (void) close(fd); 1626eda14cbcSMatt Macy 1627eda14cbcSMatt Macy if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 1628eda14cbcSMatt Macy free(buf); 1629*dbd5678dSMartin Matuska (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN, 1630eda14cbcSMatt Macy "invalid or corrupt cache file contents")); 1631eda14cbcSMatt Macy return (NULL); 1632eda14cbcSMatt Macy } 1633eda14cbcSMatt Macy 1634eda14cbcSMatt Macy free(buf); 1635eda14cbcSMatt Macy 1636eda14cbcSMatt Macy /* 1637eda14cbcSMatt Macy * Go through and get the current state of the pools and refresh their 1638eda14cbcSMatt Macy * state. 1639eda14cbcSMatt Macy */ 1640eda14cbcSMatt Macy if (nvlist_alloc(&pools, 0, 0) != 0) { 1641eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1642eda14cbcSMatt Macy nvlist_free(raw); 1643eda14cbcSMatt Macy return (NULL); 1644eda14cbcSMatt Macy } 1645eda14cbcSMatt Macy 1646eda14cbcSMatt Macy elem = NULL; 1647eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 1648eda14cbcSMatt Macy src = fnvpair_value_nvlist(elem); 1649eda14cbcSMatt Macy 1650eda14cbcSMatt Macy name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); 16519db44a8eSMartin Matuska if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0) 1652eda14cbcSMatt Macy continue; 1653eda14cbcSMatt Macy 1654eda14cbcSMatt Macy this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); 16559db44a8eSMartin Matuska if (iarg->guid != 0 && iarg->guid != this_guid) 1656eda14cbcSMatt Macy continue; 1657eda14cbcSMatt Macy 1658eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, this_guid, &active) != 0) { 1659eda14cbcSMatt Macy nvlist_free(raw); 1660eda14cbcSMatt Macy nvlist_free(pools); 1661eda14cbcSMatt Macy return (NULL); 1662eda14cbcSMatt Macy } 1663eda14cbcSMatt Macy 1664eda14cbcSMatt Macy if (active) 1665eda14cbcSMatt Macy continue; 1666eda14cbcSMatt Macy 16679db44a8eSMartin Matuska if (iarg->scan) { 16689db44a8eSMartin Matuska uint64_t saved_guid = iarg->guid; 16699db44a8eSMartin Matuska const char *saved_poolname = iarg->poolname; 16709db44a8eSMartin Matuska pthread_mutex_t lock; 16719db44a8eSMartin Matuska 16729db44a8eSMartin Matuska /* 16739db44a8eSMartin Matuska * Create the device cache that will hold the 16749db44a8eSMartin Matuska * devices we will scan based on the cachefile. 16759db44a8eSMartin Matuska * This will get destroyed and freed by 16769db44a8eSMartin Matuska * zpool_find_import_impl. 16779db44a8eSMartin Matuska */ 16789db44a8eSMartin Matuska avl_tree_t *cache = zutil_alloc(hdl, 16799db44a8eSMartin Matuska sizeof (avl_tree_t)); 16809db44a8eSMartin Matuska avl_create(cache, slice_cache_compare, 16819db44a8eSMartin Matuska sizeof (rdsk_node_t), 16829db44a8eSMartin Matuska offsetof(rdsk_node_t, rn_node)); 16839db44a8eSMartin Matuska nvlist_t *nvroot = fnvlist_lookup_nvlist(src, 16849db44a8eSMartin Matuska ZPOOL_CONFIG_VDEV_TREE); 16859db44a8eSMartin Matuska 16869db44a8eSMartin Matuska /* 16879db44a8eSMartin Matuska * We only want to find the pool with this_guid. 16889db44a8eSMartin Matuska * We will reset these values back later. 16899db44a8eSMartin Matuska */ 16909db44a8eSMartin Matuska iarg->guid = this_guid; 16919db44a8eSMartin Matuska iarg->poolname = NULL; 16929db44a8eSMartin Matuska 16939db44a8eSMartin Matuska /* 16949db44a8eSMartin Matuska * We need to build up a cache of devices that exists 16959db44a8eSMartin Matuska * in the paths pointed to by the cachefile. This allows 16969db44a8eSMartin Matuska * us to preserve the device namespace that was 16979db44a8eSMartin Matuska * originally specified by the user but also lets us 16989db44a8eSMartin Matuska * scan devices in those directories in case they had 16999db44a8eSMartin Matuska * been renamed. 17009db44a8eSMartin Matuska */ 17019db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 17029db44a8eSMartin Matuska discover_cached_paths(hdl, nvroot, cache, &lock); 17039db44a8eSMartin Matuska nvlist_t *nv = zpool_find_import_impl(hdl, iarg, 17049db44a8eSMartin Matuska &lock, cache); 17059db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17069db44a8eSMartin Matuska 17079db44a8eSMartin Matuska /* 17089db44a8eSMartin Matuska * zpool_find_import_impl will return back 17099db44a8eSMartin Matuska * a list of pools that it found based on the 17109db44a8eSMartin Matuska * device cache. There should only be one pool 17119db44a8eSMartin Matuska * since we're looking for a specific guid. 17129db44a8eSMartin Matuska * We will use that pool to build up the final 17139db44a8eSMartin Matuska * pool nvlist which is returned back to the 17149db44a8eSMartin Matuska * caller. 17159db44a8eSMartin Matuska */ 17169db44a8eSMartin Matuska nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 1717c7046f76SMartin Matuska if (pair == NULL) 1718c7046f76SMartin Matuska continue; 17199db44a8eSMartin Matuska fnvlist_add_nvlist(pools, nvpair_name(pair), 17209db44a8eSMartin Matuska fnvpair_value_nvlist(pair)); 17219db44a8eSMartin Matuska 17229db44a8eSMartin Matuska VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL); 17239db44a8eSMartin Matuska 17249db44a8eSMartin Matuska iarg->guid = saved_guid; 17259db44a8eSMartin Matuska iarg->poolname = saved_poolname; 17269db44a8eSMartin Matuska continue; 17279db44a8eSMartin Matuska } 17289db44a8eSMartin Matuska 1729eda14cbcSMatt Macy if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, 17309db44a8eSMartin Matuska iarg->cachefile) != 0) { 1731eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1732eda14cbcSMatt Macy nvlist_free(raw); 1733eda14cbcSMatt Macy nvlist_free(pools); 1734eda14cbcSMatt Macy return (NULL); 1735eda14cbcSMatt Macy } 1736eda14cbcSMatt Macy 17376ba2210eSMartin Matuska update_vdevs_config_dev_sysfs_path(src); 17386ba2210eSMartin Matuska 1739eda14cbcSMatt Macy if ((dst = zutil_refresh_config(hdl, src)) == NULL) { 1740eda14cbcSMatt Macy nvlist_free(raw); 1741eda14cbcSMatt Macy nvlist_free(pools); 1742eda14cbcSMatt Macy return (NULL); 1743eda14cbcSMatt Macy } 1744eda14cbcSMatt Macy 1745eda14cbcSMatt Macy if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1746eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1747eda14cbcSMatt Macy nvlist_free(dst); 1748eda14cbcSMatt Macy nvlist_free(raw); 1749eda14cbcSMatt Macy nvlist_free(pools); 1750eda14cbcSMatt Macy return (NULL); 1751eda14cbcSMatt Macy } 1752eda14cbcSMatt Macy nvlist_free(dst); 1753eda14cbcSMatt Macy } 1754eda14cbcSMatt Macy nvlist_free(raw); 1755eda14cbcSMatt Macy return (pools); 1756eda14cbcSMatt Macy } 1757eda14cbcSMatt Macy 17589db44a8eSMartin Matuska static nvlist_t * 17599db44a8eSMartin Matuska zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg) 17609db44a8eSMartin Matuska { 17619db44a8eSMartin Matuska pthread_mutex_t lock; 17629db44a8eSMartin Matuska avl_tree_t *cache; 17639db44a8eSMartin Matuska nvlist_t *pools = NULL; 17649db44a8eSMartin Matuska 17659db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 17669db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 17679db44a8eSMartin Matuska 17689db44a8eSMartin Matuska /* 17699db44a8eSMartin Matuska * Locate pool member vdevs by blkid or by directory scanning. 17709db44a8eSMartin Matuska * On success a newly allocated AVL tree which is populated with an 17719db44a8eSMartin Matuska * entry for each discovered vdev will be returned in the cache. 17729db44a8eSMartin Matuska * It's the caller's responsibility to consume and destroy this tree. 17739db44a8eSMartin Matuska */ 17749db44a8eSMartin Matuska if (iarg->scan || iarg->paths != 0) { 17759db44a8eSMartin Matuska size_t dirs = iarg->paths; 17769db44a8eSMartin Matuska const char * const *dir = (const char * const *)iarg->path; 17779db44a8eSMartin Matuska 17789db44a8eSMartin Matuska if (dirs == 0) 17799db44a8eSMartin Matuska dir = zpool_default_search_paths(&dirs); 17809db44a8eSMartin Matuska 17819db44a8eSMartin Matuska if (zpool_find_import_scan(hdl, &lock, &cache, 17829db44a8eSMartin Matuska dir, dirs) != 0) { 17839db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17849db44a8eSMartin Matuska return (NULL); 17859db44a8eSMartin Matuska } 17869db44a8eSMartin Matuska } else { 17879db44a8eSMartin Matuska if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) { 17889db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17899db44a8eSMartin Matuska return (NULL); 17909db44a8eSMartin Matuska } 17919db44a8eSMartin Matuska } 17929db44a8eSMartin Matuska 17939db44a8eSMartin Matuska pools = zpool_find_import_impl(hdl, iarg, &lock, cache); 17949db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17959db44a8eSMartin Matuska return (pools); 17969db44a8eSMartin Matuska } 17979db44a8eSMartin Matuska 17989db44a8eSMartin Matuska 1799eda14cbcSMatt Macy nvlist_t * 1800*dbd5678dSMartin Matuska zpool_search_import(libpc_handle_t *hdl, importargs_t *import) 1801eda14cbcSMatt Macy { 1802eda14cbcSMatt Macy nvlist_t *pools = NULL; 1803eda14cbcSMatt Macy 1804eda14cbcSMatt Macy verify(import->poolname == NULL || import->guid == 0); 1805eda14cbcSMatt Macy 1806eda14cbcSMatt Macy if (import->cachefile != NULL) 1807*dbd5678dSMartin Matuska pools = zpool_find_import_cached(hdl, import); 1808eda14cbcSMatt Macy else 1809*dbd5678dSMartin Matuska pools = zpool_find_import(hdl, import); 1810eda14cbcSMatt Macy 1811eda14cbcSMatt Macy if ((pools == NULL || nvlist_empty(pools)) && 1812*dbd5678dSMartin Matuska hdl->lpc_open_access_error && geteuid() != 0) { 1813*dbd5678dSMartin Matuska (void) zutil_error(hdl, LPC_EACCESS, dgettext(TEXT_DOMAIN, 1814eda14cbcSMatt Macy "no pools found")); 1815eda14cbcSMatt Macy } 1816eda14cbcSMatt Macy 1817eda14cbcSMatt Macy return (pools); 1818eda14cbcSMatt Macy } 1819eda14cbcSMatt Macy 1820eda14cbcSMatt Macy static boolean_t 1821eda14cbcSMatt Macy pool_match(nvlist_t *cfg, char *tgt) 1822eda14cbcSMatt Macy { 1823eda14cbcSMatt Macy uint64_t v, guid = strtoull(tgt, NULL, 0); 1824eda14cbcSMatt Macy char *s; 1825eda14cbcSMatt Macy 1826eda14cbcSMatt Macy if (guid != 0) { 1827eda14cbcSMatt Macy if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 1828eda14cbcSMatt Macy return (v == guid); 1829eda14cbcSMatt Macy } else { 1830eda14cbcSMatt Macy if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 1831eda14cbcSMatt Macy return (strcmp(s, tgt) == 0); 1832eda14cbcSMatt Macy } 1833eda14cbcSMatt Macy return (B_FALSE); 1834eda14cbcSMatt Macy } 1835eda14cbcSMatt Macy 1836eda14cbcSMatt Macy int 1837*dbd5678dSMartin Matuska zpool_find_config(libpc_handle_t *hdl, const char *target, nvlist_t **configp, 1838*dbd5678dSMartin Matuska importargs_t *args) 1839eda14cbcSMatt Macy { 1840eda14cbcSMatt Macy nvlist_t *pools; 1841eda14cbcSMatt Macy nvlist_t *match = NULL; 1842eda14cbcSMatt Macy nvlist_t *config = NULL; 18437877fdebSMatt Macy char *sepp = NULL; 1844eda14cbcSMatt Macy int count = 0; 1845eda14cbcSMatt Macy char *targetdup = strdup(target); 1846eda14cbcSMatt Macy 1847*dbd5678dSMartin Matuska if (targetdup == NULL) 1848*dbd5678dSMartin Matuska return (ENOMEM); 1849*dbd5678dSMartin Matuska 1850eda14cbcSMatt Macy *configp = NULL; 1851eda14cbcSMatt Macy 185216038816SMartin Matuska if ((sepp = strpbrk(targetdup, "/@")) != NULL) 1853eda14cbcSMatt Macy *sepp = '\0'; 1854eda14cbcSMatt Macy 1855*dbd5678dSMartin Matuska pools = zpool_search_import(hdl, args); 1856eda14cbcSMatt Macy 1857eda14cbcSMatt Macy if (pools != NULL) { 1858eda14cbcSMatt Macy nvpair_t *elem = NULL; 1859eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 1860eda14cbcSMatt Macy VERIFY0(nvpair_value_nvlist(elem, &config)); 1861eda14cbcSMatt Macy if (pool_match(config, targetdup)) { 1862eda14cbcSMatt Macy count++; 1863eda14cbcSMatt Macy if (match != NULL) { 1864eda14cbcSMatt Macy /* multiple matches found */ 1865eda14cbcSMatt Macy continue; 1866eda14cbcSMatt Macy } else { 18677877fdebSMatt Macy match = fnvlist_dup(config); 1868eda14cbcSMatt Macy } 1869eda14cbcSMatt Macy } 1870eda14cbcSMatt Macy } 18717877fdebSMatt Macy fnvlist_free(pools); 1872eda14cbcSMatt Macy } 1873eda14cbcSMatt Macy 1874eda14cbcSMatt Macy if (count == 0) { 1875eda14cbcSMatt Macy free(targetdup); 1876eda14cbcSMatt Macy return (ENOENT); 1877eda14cbcSMatt Macy } 1878eda14cbcSMatt Macy 1879eda14cbcSMatt Macy if (count > 1) { 1880eda14cbcSMatt Macy free(targetdup); 18817877fdebSMatt Macy fnvlist_free(match); 1882eda14cbcSMatt Macy return (EINVAL); 1883eda14cbcSMatt Macy } 1884eda14cbcSMatt Macy 1885eda14cbcSMatt Macy *configp = match; 1886eda14cbcSMatt Macy free(targetdup); 1887eda14cbcSMatt Macy 1888eda14cbcSMatt Macy return (0); 1889eda14cbcSMatt Macy } 18906ba2210eSMartin Matuska 18916ba2210eSMartin Matuska /* 18926ba2210eSMartin Matuska * Internal function for iterating over the vdevs. 18936ba2210eSMartin Matuska * 18946ba2210eSMartin Matuska * For each vdev, func() will be called and will be passed 'zhp' (which is 18956ba2210eSMartin Matuska * typically the zpool_handle_t cast as a void pointer), the vdev's nvlist, and 18966ba2210eSMartin Matuska * a user-defined data pointer). 18976ba2210eSMartin Matuska * 18986ba2210eSMartin Matuska * The return values from all the func() calls will be OR'd together and 18996ba2210eSMartin Matuska * returned. 19006ba2210eSMartin Matuska */ 19016ba2210eSMartin Matuska int 19026ba2210eSMartin Matuska for_each_vdev_cb(void *zhp, nvlist_t *nv, pool_vdev_iter_f func, 19036ba2210eSMartin Matuska void *data) 19046ba2210eSMartin Matuska { 19056ba2210eSMartin Matuska nvlist_t **child; 19066ba2210eSMartin Matuska uint_t c, children; 19076ba2210eSMartin Matuska int ret = 0; 19086ba2210eSMartin Matuska int i; 19096ba2210eSMartin Matuska char *type; 19106ba2210eSMartin Matuska 19116ba2210eSMartin Matuska const char *list[] = { 19126ba2210eSMartin Matuska ZPOOL_CONFIG_SPARES, 19136ba2210eSMartin Matuska ZPOOL_CONFIG_L2CACHE, 19146ba2210eSMartin Matuska ZPOOL_CONFIG_CHILDREN 19156ba2210eSMartin Matuska }; 19166ba2210eSMartin Matuska 1917681ce946SMartin Matuska if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 1918681ce946SMartin Matuska return (ret); 1919681ce946SMartin Matuska 1920681ce946SMartin Matuska /* Don't run our function on root or indirect vdevs */ 1921681ce946SMartin Matuska if ((strcmp(type, VDEV_TYPE_ROOT) != 0) && 1922681ce946SMartin Matuska (strcmp(type, VDEV_TYPE_INDIRECT) != 0)) { 1923681ce946SMartin Matuska ret |= func(zhp, nv, data); 1924681ce946SMartin Matuska } 1925681ce946SMartin Matuska 19266ba2210eSMartin Matuska for (i = 0; i < ARRAY_SIZE(list); i++) { 19276ba2210eSMartin Matuska if (nvlist_lookup_nvlist_array(nv, list[i], &child, 19286ba2210eSMartin Matuska &children) == 0) { 19296ba2210eSMartin Matuska for (c = 0; c < children; c++) { 19306ba2210eSMartin Matuska uint64_t ishole = 0; 19316ba2210eSMartin Matuska 19326ba2210eSMartin Matuska (void) nvlist_lookup_uint64(child[c], 19336ba2210eSMartin Matuska ZPOOL_CONFIG_IS_HOLE, &ishole); 19346ba2210eSMartin Matuska 19356ba2210eSMartin Matuska if (ishole) 19366ba2210eSMartin Matuska continue; 19376ba2210eSMartin Matuska 19386ba2210eSMartin Matuska ret |= for_each_vdev_cb(zhp, child[c], 19396ba2210eSMartin Matuska func, data); 19406ba2210eSMartin Matuska } 19416ba2210eSMartin Matuska } 19426ba2210eSMartin Matuska } 19436ba2210eSMartin Matuska 19446ba2210eSMartin Matuska return (ret); 19456ba2210eSMartin Matuska } 19466ba2210eSMartin Matuska 19476ba2210eSMartin Matuska /* 19486ba2210eSMartin Matuska * Given an ZPOOL_CONFIG_VDEV_TREE nvpair, iterate over all the vdevs, calling 19496ba2210eSMartin Matuska * func() for each one. func() is passed the vdev's nvlist and an optional 19506ba2210eSMartin Matuska * user-defined 'data' pointer. 19516ba2210eSMartin Matuska */ 19526ba2210eSMartin Matuska int 19536ba2210eSMartin Matuska for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func, void *data) 19546ba2210eSMartin Matuska { 19556ba2210eSMartin Matuska return (for_each_vdev_cb(NULL, nvroot, func, data)); 19566ba2210eSMartin Matuska } 1957