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 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 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 50184c1b94SMartin Matuska #include <aio.h> 51eda14cbcSMatt Macy #include <ctype.h> 52eda14cbcSMatt Macy #include <dirent.h> 53eda14cbcSMatt Macy #include <errno.h> 54eda14cbcSMatt Macy #include <libintl.h> 55eda14cbcSMatt Macy #include <libgen.h> 56eda14cbcSMatt Macy #include <stddef.h> 57eda14cbcSMatt Macy #include <stdlib.h> 58eda14cbcSMatt Macy #include <string.h> 59eda14cbcSMatt Macy #include <sys/stat.h> 60eda14cbcSMatt Macy #include <unistd.h> 61eda14cbcSMatt Macy #include <fcntl.h> 62eda14cbcSMatt Macy #include <sys/dktp/fdisk.h> 63eda14cbcSMatt Macy #include <sys/vdev_impl.h> 64eda14cbcSMatt Macy #include <sys/fs/zfs.h> 65eda14cbcSMatt Macy 66eda14cbcSMatt Macy #include <thread_pool.h> 67eda14cbcSMatt Macy #include <libzutil.h> 68eda14cbcSMatt Macy #include <libnvpair.h> 69eda14cbcSMatt Macy 70eda14cbcSMatt Macy #include "zutil_import.h" 71eda14cbcSMatt Macy 72*1f88aa09SMartin Matuska static __attribute__((format(printf, 2, 3))) void 73eda14cbcSMatt Macy zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...) 74eda14cbcSMatt Macy { 75eda14cbcSMatt Macy va_list ap; 76eda14cbcSMatt Macy 77eda14cbcSMatt Macy va_start(ap, fmt); 78eda14cbcSMatt Macy 79eda14cbcSMatt Macy (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap); 80eda14cbcSMatt Macy hdl->lpc_desc_active = B_TRUE; 81eda14cbcSMatt Macy 82eda14cbcSMatt Macy va_end(ap); 83eda14cbcSMatt Macy } 84eda14cbcSMatt Macy 85eda14cbcSMatt Macy static void 86eda14cbcSMatt Macy zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt, 87eda14cbcSMatt Macy va_list ap) 88eda14cbcSMatt Macy { 89eda14cbcSMatt Macy char action[1024]; 90eda14cbcSMatt Macy 91eda14cbcSMatt Macy (void) vsnprintf(action, sizeof (action), fmt, ap); 92eda14cbcSMatt Macy 93eda14cbcSMatt Macy if (hdl->lpc_desc_active) 94eda14cbcSMatt Macy hdl->lpc_desc_active = B_FALSE; 95eda14cbcSMatt Macy else 96eda14cbcSMatt Macy hdl->lpc_desc[0] = '\0'; 97eda14cbcSMatt Macy 98eda14cbcSMatt Macy if (hdl->lpc_printerr) { 99eda14cbcSMatt Macy if (hdl->lpc_desc[0] != '\0') 100eda14cbcSMatt Macy error = hdl->lpc_desc; 101eda14cbcSMatt Macy 102eda14cbcSMatt Macy (void) fprintf(stderr, "%s: %s\n", action, error); 103eda14cbcSMatt Macy } 104eda14cbcSMatt Macy } 105eda14cbcSMatt Macy 106*1f88aa09SMartin Matuska static __attribute__((format(printf, 3, 4))) int 107eda14cbcSMatt Macy zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...) 108eda14cbcSMatt Macy { 109eda14cbcSMatt Macy va_list ap; 110eda14cbcSMatt Macy 111eda14cbcSMatt Macy va_start(ap, fmt); 112eda14cbcSMatt Macy 113eda14cbcSMatt Macy zutil_verror(hdl, error, fmt, ap); 114eda14cbcSMatt Macy 115eda14cbcSMatt Macy va_end(ap); 116eda14cbcSMatt Macy 117eda14cbcSMatt Macy return (-1); 118eda14cbcSMatt Macy } 119eda14cbcSMatt Macy 120eda14cbcSMatt Macy static int 121eda14cbcSMatt Macy zutil_error(libpc_handle_t *hdl, const char *error, const char *msg) 122eda14cbcSMatt Macy { 123eda14cbcSMatt Macy return (zutil_error_fmt(hdl, error, "%s", msg)); 124eda14cbcSMatt Macy } 125eda14cbcSMatt Macy 126eda14cbcSMatt Macy static int 127eda14cbcSMatt Macy zutil_no_memory(libpc_handle_t *hdl) 128eda14cbcSMatt Macy { 129eda14cbcSMatt Macy zutil_error(hdl, EZFS_NOMEM, "internal error"); 130eda14cbcSMatt Macy exit(1); 131eda14cbcSMatt Macy } 132eda14cbcSMatt Macy 133eda14cbcSMatt Macy void * 134eda14cbcSMatt Macy zutil_alloc(libpc_handle_t *hdl, size_t size) 135eda14cbcSMatt Macy { 136eda14cbcSMatt Macy void *data; 137eda14cbcSMatt Macy 138eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL) 139eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 140eda14cbcSMatt Macy 141eda14cbcSMatt Macy return (data); 142eda14cbcSMatt Macy } 143eda14cbcSMatt Macy 144eda14cbcSMatt Macy char * 145eda14cbcSMatt Macy zutil_strdup(libpc_handle_t *hdl, const char *str) 146eda14cbcSMatt Macy { 147eda14cbcSMatt Macy char *ret; 148eda14cbcSMatt Macy 149eda14cbcSMatt Macy if ((ret = strdup(str)) == NULL) 150eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 151eda14cbcSMatt Macy 152eda14cbcSMatt Macy return (ret); 153eda14cbcSMatt Macy } 154eda14cbcSMatt Macy 1553ff01b23SMartin Matuska static char * 1563ff01b23SMartin Matuska zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n) 1573ff01b23SMartin Matuska { 1583ff01b23SMartin Matuska char *ret; 1593ff01b23SMartin Matuska 1603ff01b23SMartin Matuska if ((ret = strndup(str, n)) == NULL) 1613ff01b23SMartin Matuska (void) zutil_no_memory(hdl); 1623ff01b23SMartin Matuska 1633ff01b23SMartin Matuska return (ret); 1643ff01b23SMartin Matuska } 1653ff01b23SMartin Matuska 166eda14cbcSMatt Macy /* 167eda14cbcSMatt Macy * Intermediate structures used to gather configuration information. 168eda14cbcSMatt Macy */ 169eda14cbcSMatt Macy typedef struct config_entry { 170eda14cbcSMatt Macy uint64_t ce_txg; 171eda14cbcSMatt Macy nvlist_t *ce_config; 172eda14cbcSMatt Macy struct config_entry *ce_next; 173eda14cbcSMatt Macy } config_entry_t; 174eda14cbcSMatt Macy 175eda14cbcSMatt Macy typedef struct vdev_entry { 176eda14cbcSMatt Macy uint64_t ve_guid; 177eda14cbcSMatt Macy config_entry_t *ve_configs; 178eda14cbcSMatt Macy struct vdev_entry *ve_next; 179eda14cbcSMatt Macy } vdev_entry_t; 180eda14cbcSMatt Macy 181eda14cbcSMatt Macy typedef struct pool_entry { 182eda14cbcSMatt Macy uint64_t pe_guid; 183eda14cbcSMatt Macy vdev_entry_t *pe_vdevs; 184eda14cbcSMatt Macy struct pool_entry *pe_next; 185eda14cbcSMatt Macy } pool_entry_t; 186eda14cbcSMatt Macy 187eda14cbcSMatt Macy typedef struct name_entry { 188eda14cbcSMatt Macy char *ne_name; 189eda14cbcSMatt Macy uint64_t ne_guid; 190eda14cbcSMatt Macy uint64_t ne_order; 191eda14cbcSMatt Macy uint64_t ne_num_labels; 192eda14cbcSMatt Macy struct name_entry *ne_next; 193eda14cbcSMatt Macy } name_entry_t; 194eda14cbcSMatt Macy 195eda14cbcSMatt Macy typedef struct pool_list { 196eda14cbcSMatt Macy pool_entry_t *pools; 197eda14cbcSMatt Macy name_entry_t *names; 198eda14cbcSMatt Macy } pool_list_t; 199eda14cbcSMatt Macy 200eda14cbcSMatt Macy /* 201eda14cbcSMatt Macy * Go through and fix up any path and/or devid information for the given vdev 202eda14cbcSMatt Macy * configuration. 203eda14cbcSMatt Macy */ 204eda14cbcSMatt Macy static int 205eda14cbcSMatt Macy fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names) 206eda14cbcSMatt Macy { 207eda14cbcSMatt Macy nvlist_t **child; 208eda14cbcSMatt Macy uint_t c, children; 209eda14cbcSMatt Macy uint64_t guid; 210eda14cbcSMatt Macy name_entry_t *ne, *best; 211eda14cbcSMatt Macy char *path; 212eda14cbcSMatt Macy 213eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 214eda14cbcSMatt Macy &child, &children) == 0) { 215eda14cbcSMatt Macy for (c = 0; c < children; c++) 216eda14cbcSMatt Macy if (fix_paths(hdl, child[c], names) != 0) 217eda14cbcSMatt Macy return (-1); 218eda14cbcSMatt Macy return (0); 219eda14cbcSMatt Macy } 220eda14cbcSMatt Macy 221eda14cbcSMatt Macy /* 222eda14cbcSMatt Macy * This is a leaf (file or disk) vdev. In either case, go through 223eda14cbcSMatt Macy * the name list and see if we find a matching guid. If so, replace 224eda14cbcSMatt Macy * the path and see if we can calculate a new devid. 225eda14cbcSMatt Macy * 226eda14cbcSMatt Macy * There may be multiple names associated with a particular guid, in 227eda14cbcSMatt Macy * which case we have overlapping partitions or multiple paths to the 228eda14cbcSMatt Macy * same disk. In this case we prefer to use the path name which 229eda14cbcSMatt Macy * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we 230eda14cbcSMatt Macy * use the lowest order device which corresponds to the first match 231eda14cbcSMatt Macy * while traversing the ZPOOL_IMPORT_PATH search path. 232eda14cbcSMatt Macy */ 233eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 234eda14cbcSMatt Macy if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 235eda14cbcSMatt Macy path = NULL; 236eda14cbcSMatt Macy 237eda14cbcSMatt Macy best = NULL; 238eda14cbcSMatt Macy for (ne = names; ne != NULL; ne = ne->ne_next) { 239eda14cbcSMatt Macy if (ne->ne_guid == guid) { 240eda14cbcSMatt Macy if (path == NULL) { 241eda14cbcSMatt Macy best = ne; 242eda14cbcSMatt Macy break; 243eda14cbcSMatt Macy } 244eda14cbcSMatt Macy 245eda14cbcSMatt Macy if ((strlen(path) == strlen(ne->ne_name)) && 246eda14cbcSMatt Macy strncmp(path, ne->ne_name, strlen(path)) == 0) { 247eda14cbcSMatt Macy best = ne; 248eda14cbcSMatt Macy break; 249eda14cbcSMatt Macy } 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy if (best == NULL) { 252eda14cbcSMatt Macy best = ne; 253eda14cbcSMatt Macy continue; 254eda14cbcSMatt Macy } 255eda14cbcSMatt Macy 256eda14cbcSMatt Macy /* Prefer paths with move vdev labels. */ 257eda14cbcSMatt Macy if (ne->ne_num_labels > best->ne_num_labels) { 258eda14cbcSMatt Macy best = ne; 259eda14cbcSMatt Macy continue; 260eda14cbcSMatt Macy } 261eda14cbcSMatt Macy 262eda14cbcSMatt Macy /* Prefer paths earlier in the search order. */ 263eda14cbcSMatt Macy if (ne->ne_num_labels == best->ne_num_labels && 264eda14cbcSMatt Macy ne->ne_order < best->ne_order) { 265eda14cbcSMatt Macy best = ne; 266eda14cbcSMatt Macy continue; 267eda14cbcSMatt Macy } 268eda14cbcSMatt Macy } 269eda14cbcSMatt Macy } 270eda14cbcSMatt Macy 271eda14cbcSMatt Macy if (best == NULL) 272eda14cbcSMatt Macy return (0); 273eda14cbcSMatt Macy 274eda14cbcSMatt Macy if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 275eda14cbcSMatt Macy return (-1); 276eda14cbcSMatt Macy 277eda14cbcSMatt Macy update_vdev_config_dev_strs(nv); 278eda14cbcSMatt Macy 279eda14cbcSMatt Macy return (0); 280eda14cbcSMatt Macy } 281eda14cbcSMatt Macy 282eda14cbcSMatt Macy /* 283eda14cbcSMatt Macy * Add the given configuration to the list of known devices. 284eda14cbcSMatt Macy */ 285eda14cbcSMatt Macy static int 286eda14cbcSMatt Macy add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path, 287eda14cbcSMatt Macy int order, int num_labels, nvlist_t *config) 288eda14cbcSMatt Macy { 289eda14cbcSMatt Macy uint64_t pool_guid, vdev_guid, top_guid, txg, state; 290eda14cbcSMatt Macy pool_entry_t *pe; 291eda14cbcSMatt Macy vdev_entry_t *ve; 292eda14cbcSMatt Macy config_entry_t *ce; 293eda14cbcSMatt Macy name_entry_t *ne; 294eda14cbcSMatt Macy 295eda14cbcSMatt Macy /* 296eda14cbcSMatt Macy * If this is a hot spare not currently in use or level 2 cache 297eda14cbcSMatt Macy * device, add it to the list of names to translate, but don't do 298eda14cbcSMatt Macy * anything else. 299eda14cbcSMatt Macy */ 300eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 301eda14cbcSMatt Macy &state) == 0 && 302eda14cbcSMatt Macy (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 303eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 304eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 305eda14cbcSMatt Macy return (-1); 306eda14cbcSMatt Macy 307eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 308eda14cbcSMatt Macy free(ne); 309eda14cbcSMatt Macy return (-1); 310eda14cbcSMatt Macy } 311eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 312eda14cbcSMatt Macy ne->ne_order = order; 313eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 314eda14cbcSMatt Macy ne->ne_next = pl->names; 315eda14cbcSMatt Macy pl->names = ne; 316eda14cbcSMatt Macy 317eda14cbcSMatt Macy return (0); 318eda14cbcSMatt Macy } 319eda14cbcSMatt Macy 320eda14cbcSMatt Macy /* 321eda14cbcSMatt Macy * If we have a valid config but cannot read any of these fields, then 322eda14cbcSMatt Macy * it means we have a half-initialized label. In vdev_label_init() 323eda14cbcSMatt Macy * we write a label with txg == 0 so that we can identify the device 324eda14cbcSMatt Macy * in case the user refers to the same disk later on. If we fail to 325eda14cbcSMatt Macy * create the pool, we'll be left with a label in this state 326eda14cbcSMatt Macy * which should not be considered part of a valid pool. 327eda14cbcSMatt Macy */ 328eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 329eda14cbcSMatt Macy &pool_guid) != 0 || 330eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 331eda14cbcSMatt Macy &vdev_guid) != 0 || 332eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 333eda14cbcSMatt Macy &top_guid) != 0 || 334eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 335eda14cbcSMatt Macy &txg) != 0 || txg == 0) { 336eda14cbcSMatt Macy return (0); 337eda14cbcSMatt Macy } 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy /* 340eda14cbcSMatt Macy * First, see if we know about this pool. If not, then add it to the 341eda14cbcSMatt Macy * list of known pools. 342eda14cbcSMatt Macy */ 343eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 344eda14cbcSMatt Macy if (pe->pe_guid == pool_guid) 345eda14cbcSMatt Macy break; 346eda14cbcSMatt Macy } 347eda14cbcSMatt Macy 348eda14cbcSMatt Macy if (pe == NULL) { 349eda14cbcSMatt Macy if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 350eda14cbcSMatt Macy return (-1); 351eda14cbcSMatt Macy } 352eda14cbcSMatt Macy pe->pe_guid = pool_guid; 353eda14cbcSMatt Macy pe->pe_next = pl->pools; 354eda14cbcSMatt Macy pl->pools = pe; 355eda14cbcSMatt Macy } 356eda14cbcSMatt Macy 357eda14cbcSMatt Macy /* 358eda14cbcSMatt Macy * Second, see if we know about this toplevel vdev. Add it if its 359eda14cbcSMatt Macy * missing. 360eda14cbcSMatt Macy */ 361eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 362eda14cbcSMatt Macy if (ve->ve_guid == top_guid) 363eda14cbcSMatt Macy break; 364eda14cbcSMatt Macy } 365eda14cbcSMatt Macy 366eda14cbcSMatt Macy if (ve == NULL) { 367eda14cbcSMatt Macy if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 368eda14cbcSMatt Macy return (-1); 369eda14cbcSMatt Macy } 370eda14cbcSMatt Macy ve->ve_guid = top_guid; 371eda14cbcSMatt Macy ve->ve_next = pe->pe_vdevs; 372eda14cbcSMatt Macy pe->pe_vdevs = ve; 373eda14cbcSMatt Macy } 374eda14cbcSMatt Macy 375eda14cbcSMatt Macy /* 376eda14cbcSMatt Macy * Third, see if we have a config with a matching transaction group. If 377eda14cbcSMatt Macy * so, then we do nothing. Otherwise, add it to the list of known 378eda14cbcSMatt Macy * configs. 379eda14cbcSMatt Macy */ 380eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 381eda14cbcSMatt Macy if (ce->ce_txg == txg) 382eda14cbcSMatt Macy break; 383eda14cbcSMatt Macy } 384eda14cbcSMatt Macy 385eda14cbcSMatt Macy if (ce == NULL) { 386eda14cbcSMatt Macy if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) { 387eda14cbcSMatt Macy return (-1); 388eda14cbcSMatt Macy } 389eda14cbcSMatt Macy ce->ce_txg = txg; 390eda14cbcSMatt Macy ce->ce_config = fnvlist_dup(config); 391eda14cbcSMatt Macy ce->ce_next = ve->ve_configs; 392eda14cbcSMatt Macy ve->ve_configs = ce; 393eda14cbcSMatt Macy } 394eda14cbcSMatt Macy 395eda14cbcSMatt Macy /* 396eda14cbcSMatt Macy * At this point we've successfully added our config to the list of 397eda14cbcSMatt Macy * known configs. The last thing to do is add the vdev guid -> path 398eda14cbcSMatt Macy * mappings so that we can fix up the configuration as necessary before 399eda14cbcSMatt Macy * doing the import. 400eda14cbcSMatt Macy */ 401eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 402eda14cbcSMatt Macy return (-1); 403eda14cbcSMatt Macy 404eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 405eda14cbcSMatt Macy free(ne); 406eda14cbcSMatt Macy return (-1); 407eda14cbcSMatt Macy } 408eda14cbcSMatt Macy 409eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 410eda14cbcSMatt Macy ne->ne_order = order; 411eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 412eda14cbcSMatt Macy ne->ne_next = pl->names; 413eda14cbcSMatt Macy pl->names = ne; 414eda14cbcSMatt Macy 415eda14cbcSMatt Macy return (0); 416eda14cbcSMatt Macy } 417eda14cbcSMatt Macy 418eda14cbcSMatt Macy static int 419eda14cbcSMatt Macy zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid, 420eda14cbcSMatt Macy boolean_t *isactive) 421eda14cbcSMatt Macy { 422eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_pool_active != NULL); 423eda14cbcSMatt Macy 424eda14cbcSMatt Macy int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name, 425eda14cbcSMatt Macy guid, isactive); 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy return (error); 428eda14cbcSMatt Macy } 429eda14cbcSMatt Macy 430eda14cbcSMatt Macy static nvlist_t * 431eda14cbcSMatt Macy zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig) 432eda14cbcSMatt Macy { 433eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_refresh_config != NULL); 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle, 436eda14cbcSMatt Macy tryconfig)); 437eda14cbcSMatt Macy } 438eda14cbcSMatt Macy 439eda14cbcSMatt Macy /* 440eda14cbcSMatt Macy * Determine if the vdev id is a hole in the namespace. 441eda14cbcSMatt Macy */ 442eda14cbcSMatt Macy static boolean_t 443eda14cbcSMatt Macy vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 444eda14cbcSMatt Macy { 445eda14cbcSMatt Macy int c; 446eda14cbcSMatt Macy 447eda14cbcSMatt Macy for (c = 0; c < holes; c++) { 448eda14cbcSMatt Macy 449eda14cbcSMatt Macy /* Top-level is a hole */ 450eda14cbcSMatt Macy if (hole_array[c] == id) 451eda14cbcSMatt Macy return (B_TRUE); 452eda14cbcSMatt Macy } 453eda14cbcSMatt Macy return (B_FALSE); 454eda14cbcSMatt Macy } 455eda14cbcSMatt Macy 456eda14cbcSMatt Macy /* 457eda14cbcSMatt Macy * Convert our list of pools into the definitive set of configurations. We 458eda14cbcSMatt Macy * start by picking the best config for each toplevel vdev. Once that's done, 459eda14cbcSMatt Macy * we assemble the toplevel vdevs into a full config for the pool. We make a 460eda14cbcSMatt Macy * pass to fix up any incorrect paths, and then add it to the main list to 461eda14cbcSMatt Macy * return to the user. 462eda14cbcSMatt Macy */ 463eda14cbcSMatt Macy static nvlist_t * 464eda14cbcSMatt Macy get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok, 465eda14cbcSMatt Macy nvlist_t *policy) 466eda14cbcSMatt Macy { 467eda14cbcSMatt Macy pool_entry_t *pe; 468eda14cbcSMatt Macy vdev_entry_t *ve; 469eda14cbcSMatt Macy config_entry_t *ce; 470eda14cbcSMatt Macy nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; 471eda14cbcSMatt Macy nvlist_t **spares, **l2cache; 472eda14cbcSMatt Macy uint_t i, nspares, nl2cache; 473eda14cbcSMatt Macy boolean_t config_seen; 474eda14cbcSMatt Macy uint64_t best_txg; 475eda14cbcSMatt Macy char *name, *hostname = NULL; 476eda14cbcSMatt Macy uint64_t guid; 477eda14cbcSMatt Macy uint_t children = 0; 478eda14cbcSMatt Macy nvlist_t **child = NULL; 479eda14cbcSMatt Macy uint_t holes; 480eda14cbcSMatt Macy uint64_t *hole_array, max_id; 481eda14cbcSMatt Macy uint_t c; 482eda14cbcSMatt Macy boolean_t isactive; 483eda14cbcSMatt Macy uint64_t hostid; 484eda14cbcSMatt Macy nvlist_t *nvl; 485eda14cbcSMatt Macy boolean_t valid_top_config = B_FALSE; 486eda14cbcSMatt Macy 487eda14cbcSMatt Macy if (nvlist_alloc(&ret, 0, 0) != 0) 488eda14cbcSMatt Macy goto nomem; 489eda14cbcSMatt Macy 490eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 491eda14cbcSMatt Macy uint64_t id, max_txg = 0; 492eda14cbcSMatt Macy 493eda14cbcSMatt Macy if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 494eda14cbcSMatt Macy goto nomem; 495eda14cbcSMatt Macy config_seen = B_FALSE; 496eda14cbcSMatt Macy 497eda14cbcSMatt Macy /* 498eda14cbcSMatt Macy * Iterate over all toplevel vdevs. Grab the pool configuration 499eda14cbcSMatt Macy * from the first one we find, and then go through the rest and 500eda14cbcSMatt Macy * add them as necessary to the 'vdevs' member of the config. 501eda14cbcSMatt Macy */ 502eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy /* 505eda14cbcSMatt Macy * Determine the best configuration for this vdev by 506eda14cbcSMatt Macy * selecting the config with the latest transaction 507eda14cbcSMatt Macy * group. 508eda14cbcSMatt Macy */ 509eda14cbcSMatt Macy best_txg = 0; 510eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; 511eda14cbcSMatt Macy ce = ce->ce_next) { 512eda14cbcSMatt Macy 513eda14cbcSMatt Macy if (ce->ce_txg > best_txg) { 514eda14cbcSMatt Macy tmp = ce->ce_config; 515eda14cbcSMatt Macy best_txg = ce->ce_txg; 516eda14cbcSMatt Macy } 517eda14cbcSMatt Macy } 518eda14cbcSMatt Macy 519eda14cbcSMatt Macy /* 520eda14cbcSMatt Macy * We rely on the fact that the max txg for the 521eda14cbcSMatt Macy * pool will contain the most up-to-date information 522eda14cbcSMatt Macy * about the valid top-levels in the vdev namespace. 523eda14cbcSMatt Macy */ 524eda14cbcSMatt Macy if (best_txg > max_txg) { 525eda14cbcSMatt Macy (void) nvlist_remove(config, 526eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 527eda14cbcSMatt Macy DATA_TYPE_UINT64); 528eda14cbcSMatt Macy (void) nvlist_remove(config, 529eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 530eda14cbcSMatt Macy DATA_TYPE_UINT64_ARRAY); 531eda14cbcSMatt Macy 532eda14cbcSMatt Macy max_txg = best_txg; 533eda14cbcSMatt Macy hole_array = NULL; 534eda14cbcSMatt Macy holes = 0; 535eda14cbcSMatt Macy max_id = 0; 536eda14cbcSMatt Macy valid_top_config = B_FALSE; 537eda14cbcSMatt Macy 538eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 539eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 540eda14cbcSMatt Macy verify(nvlist_add_uint64(config, 541eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 542eda14cbcSMatt Macy max_id) == 0); 543eda14cbcSMatt Macy valid_top_config = B_TRUE; 544eda14cbcSMatt Macy } 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy if (nvlist_lookup_uint64_array(tmp, 547eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 548eda14cbcSMatt Macy &holes) == 0) { 549eda14cbcSMatt Macy verify(nvlist_add_uint64_array(config, 550eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 551eda14cbcSMatt Macy hole_array, holes) == 0); 552eda14cbcSMatt Macy } 553eda14cbcSMatt Macy } 554eda14cbcSMatt Macy 555eda14cbcSMatt Macy if (!config_seen) { 556eda14cbcSMatt Macy /* 557eda14cbcSMatt Macy * Copy the relevant pieces of data to the pool 558eda14cbcSMatt Macy * configuration: 559eda14cbcSMatt Macy * 560eda14cbcSMatt Macy * version 561eda14cbcSMatt Macy * pool guid 562eda14cbcSMatt Macy * name 563eda14cbcSMatt Macy * comment (if available) 564ee36e25aSMartin Matuska * compatibility features (if available) 565eda14cbcSMatt Macy * pool state 566eda14cbcSMatt Macy * hostid (if available) 567eda14cbcSMatt Macy * hostname (if available) 568eda14cbcSMatt Macy */ 569eda14cbcSMatt Macy uint64_t state, version; 570eda14cbcSMatt Macy char *comment = NULL; 571ee36e25aSMartin Matuska char *compatibility = NULL; 572eda14cbcSMatt Macy 573eda14cbcSMatt Macy version = fnvlist_lookup_uint64(tmp, 574eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION); 575eda14cbcSMatt Macy fnvlist_add_uint64(config, 576eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION, version); 577eda14cbcSMatt Macy guid = fnvlist_lookup_uint64(tmp, 578eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID); 579eda14cbcSMatt Macy fnvlist_add_uint64(config, 580eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, guid); 581eda14cbcSMatt Macy name = fnvlist_lookup_string(tmp, 582eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME); 583eda14cbcSMatt Macy fnvlist_add_string(config, 584eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, name); 585eda14cbcSMatt Macy 586eda14cbcSMatt Macy if (nvlist_lookup_string(tmp, 587eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, &comment) == 0) 588eda14cbcSMatt Macy fnvlist_add_string(config, 589eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, comment); 590eda14cbcSMatt Macy 591ee36e25aSMartin Matuska if (nvlist_lookup_string(tmp, 592ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 593ee36e25aSMartin Matuska &compatibility) == 0) 594ee36e25aSMartin Matuska fnvlist_add_string(config, 595ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 596ee36e25aSMartin Matuska compatibility); 597ee36e25aSMartin Matuska 598eda14cbcSMatt Macy state = fnvlist_lookup_uint64(tmp, 599eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE); 600eda14cbcSMatt Macy fnvlist_add_uint64(config, 601eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, state); 602eda14cbcSMatt Macy 603eda14cbcSMatt Macy hostid = 0; 604eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 605eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 606eda14cbcSMatt Macy fnvlist_add_uint64(config, 607eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, hostid); 608eda14cbcSMatt Macy hostname = fnvlist_lookup_string(tmp, 609eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME); 610eda14cbcSMatt Macy fnvlist_add_string(config, 611eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME, hostname); 612eda14cbcSMatt Macy } 613eda14cbcSMatt Macy 614eda14cbcSMatt Macy config_seen = B_TRUE; 615eda14cbcSMatt Macy } 616eda14cbcSMatt Macy 617eda14cbcSMatt Macy /* 618eda14cbcSMatt Macy * Add this top-level vdev to the child array. 619eda14cbcSMatt Macy */ 620eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(tmp, 621eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 622eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 623eda14cbcSMatt Macy &id) == 0); 624eda14cbcSMatt Macy 625eda14cbcSMatt Macy if (id >= children) { 626eda14cbcSMatt Macy nvlist_t **newchild; 627eda14cbcSMatt Macy 628eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (id + 1) * 629eda14cbcSMatt Macy sizeof (nvlist_t *)); 630eda14cbcSMatt Macy if (newchild == NULL) 631eda14cbcSMatt Macy goto nomem; 632eda14cbcSMatt Macy 633eda14cbcSMatt Macy for (c = 0; c < children; c++) 634eda14cbcSMatt Macy newchild[c] = child[c]; 635eda14cbcSMatt Macy 636eda14cbcSMatt Macy free(child); 637eda14cbcSMatt Macy child = newchild; 638eda14cbcSMatt Macy children = id + 1; 639eda14cbcSMatt Macy } 640eda14cbcSMatt Macy if (nvlist_dup(nvtop, &child[id], 0) != 0) 641eda14cbcSMatt Macy goto nomem; 642eda14cbcSMatt Macy 643eda14cbcSMatt Macy } 644eda14cbcSMatt Macy 645eda14cbcSMatt Macy /* 646eda14cbcSMatt Macy * If we have information about all the top-levels then 647eda14cbcSMatt Macy * clean up the nvlist which we've constructed. This 648eda14cbcSMatt Macy * means removing any extraneous devices that are 649eda14cbcSMatt Macy * beyond the valid range or adding devices to the end 650eda14cbcSMatt Macy * of our array which appear to be missing. 651eda14cbcSMatt Macy */ 652eda14cbcSMatt Macy if (valid_top_config) { 653eda14cbcSMatt Macy if (max_id < children) { 654eda14cbcSMatt Macy for (c = max_id; c < children; c++) 655eda14cbcSMatt Macy nvlist_free(child[c]); 656eda14cbcSMatt Macy children = max_id; 657eda14cbcSMatt Macy } else if (max_id > children) { 658eda14cbcSMatt Macy nvlist_t **newchild; 659eda14cbcSMatt Macy 660eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (max_id) * 661eda14cbcSMatt Macy sizeof (nvlist_t *)); 662eda14cbcSMatt Macy if (newchild == NULL) 663eda14cbcSMatt Macy goto nomem; 664eda14cbcSMatt Macy 665eda14cbcSMatt Macy for (c = 0; c < children; c++) 666eda14cbcSMatt Macy newchild[c] = child[c]; 667eda14cbcSMatt Macy 668eda14cbcSMatt Macy free(child); 669eda14cbcSMatt Macy child = newchild; 670eda14cbcSMatt Macy children = max_id; 671eda14cbcSMatt Macy } 672eda14cbcSMatt Macy } 673eda14cbcSMatt Macy 674eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 675eda14cbcSMatt Macy &guid) == 0); 676eda14cbcSMatt Macy 677eda14cbcSMatt Macy /* 678eda14cbcSMatt Macy * The vdev namespace may contain holes as a result of 679eda14cbcSMatt Macy * device removal. We must add them back into the vdev 680eda14cbcSMatt Macy * tree before we process any missing devices. 681eda14cbcSMatt Macy */ 682eda14cbcSMatt Macy if (holes > 0) { 683eda14cbcSMatt Macy ASSERT(valid_top_config); 684eda14cbcSMatt Macy 685eda14cbcSMatt Macy for (c = 0; c < children; c++) { 686eda14cbcSMatt Macy nvlist_t *holey; 687eda14cbcSMatt Macy 688eda14cbcSMatt Macy if (child[c] != NULL || 689eda14cbcSMatt Macy !vdev_is_hole(hole_array, holes, c)) 690eda14cbcSMatt Macy continue; 691eda14cbcSMatt Macy 692eda14cbcSMatt Macy if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 693eda14cbcSMatt Macy 0) != 0) 694eda14cbcSMatt Macy goto nomem; 695eda14cbcSMatt Macy 696eda14cbcSMatt Macy /* 697eda14cbcSMatt Macy * Holes in the namespace are treated as 698eda14cbcSMatt Macy * "hole" top-level vdevs and have a 699eda14cbcSMatt Macy * special flag set on them. 700eda14cbcSMatt Macy */ 701eda14cbcSMatt Macy if (nvlist_add_string(holey, 702eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 703eda14cbcSMatt Macy VDEV_TYPE_HOLE) != 0 || 704eda14cbcSMatt Macy nvlist_add_uint64(holey, 705eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 706eda14cbcSMatt Macy nvlist_add_uint64(holey, 707eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 708eda14cbcSMatt Macy nvlist_free(holey); 709eda14cbcSMatt Macy goto nomem; 710eda14cbcSMatt Macy } 711eda14cbcSMatt Macy child[c] = holey; 712eda14cbcSMatt Macy } 713eda14cbcSMatt Macy } 714eda14cbcSMatt Macy 715eda14cbcSMatt Macy /* 716eda14cbcSMatt Macy * Look for any missing top-level vdevs. If this is the case, 717eda14cbcSMatt Macy * create a faked up 'missing' vdev as a placeholder. We cannot 718eda14cbcSMatt Macy * simply compress the child array, because the kernel performs 719eda14cbcSMatt Macy * certain checks to make sure the vdev IDs match their location 720eda14cbcSMatt Macy * in the configuration. 721eda14cbcSMatt Macy */ 722eda14cbcSMatt Macy for (c = 0; c < children; c++) { 723eda14cbcSMatt Macy if (child[c] == NULL) { 724eda14cbcSMatt Macy nvlist_t *missing; 725eda14cbcSMatt Macy if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 726eda14cbcSMatt Macy 0) != 0) 727eda14cbcSMatt Macy goto nomem; 728eda14cbcSMatt Macy if (nvlist_add_string(missing, 729eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 730eda14cbcSMatt Macy VDEV_TYPE_MISSING) != 0 || 731eda14cbcSMatt Macy nvlist_add_uint64(missing, 732eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 733eda14cbcSMatt Macy nvlist_add_uint64(missing, 734eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 735eda14cbcSMatt Macy nvlist_free(missing); 736eda14cbcSMatt Macy goto nomem; 737eda14cbcSMatt Macy } 738eda14cbcSMatt Macy child[c] = missing; 739eda14cbcSMatt Macy } 740eda14cbcSMatt Macy } 741eda14cbcSMatt Macy 742eda14cbcSMatt Macy /* 743eda14cbcSMatt Macy * Put all of this pool's top-level vdevs into a root vdev. 744eda14cbcSMatt Macy */ 745eda14cbcSMatt Macy if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 746eda14cbcSMatt Macy goto nomem; 747eda14cbcSMatt Macy if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 748eda14cbcSMatt Macy VDEV_TYPE_ROOT) != 0 || 749eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 750eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 751eda14cbcSMatt Macy nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 752eda14cbcSMatt Macy child, children) != 0) { 753eda14cbcSMatt Macy nvlist_free(nvroot); 754eda14cbcSMatt Macy goto nomem; 755eda14cbcSMatt Macy } 756eda14cbcSMatt Macy 757eda14cbcSMatt Macy for (c = 0; c < children; c++) 758eda14cbcSMatt Macy nvlist_free(child[c]); 759eda14cbcSMatt Macy free(child); 760eda14cbcSMatt Macy children = 0; 761eda14cbcSMatt Macy child = NULL; 762eda14cbcSMatt Macy 763eda14cbcSMatt Macy /* 764eda14cbcSMatt Macy * Go through and fix up any paths and/or devids based on our 765eda14cbcSMatt Macy * known list of vdev GUID -> path mappings. 766eda14cbcSMatt Macy */ 767eda14cbcSMatt Macy if (fix_paths(hdl, nvroot, pl->names) != 0) { 768eda14cbcSMatt Macy nvlist_free(nvroot); 769eda14cbcSMatt Macy goto nomem; 770eda14cbcSMatt Macy } 771eda14cbcSMatt Macy 772eda14cbcSMatt Macy /* 773eda14cbcSMatt Macy * Add the root vdev to this pool's configuration. 774eda14cbcSMatt Macy */ 775eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 776eda14cbcSMatt Macy nvroot) != 0) { 777eda14cbcSMatt Macy nvlist_free(nvroot); 778eda14cbcSMatt Macy goto nomem; 779eda14cbcSMatt Macy } 780eda14cbcSMatt Macy nvlist_free(nvroot); 781eda14cbcSMatt Macy 782eda14cbcSMatt Macy /* 783eda14cbcSMatt Macy * zdb uses this path to report on active pools that were 784eda14cbcSMatt Macy * imported or created using -R. 785eda14cbcSMatt Macy */ 786eda14cbcSMatt Macy if (active_ok) 787eda14cbcSMatt Macy goto add_pool; 788eda14cbcSMatt Macy 789eda14cbcSMatt Macy /* 790eda14cbcSMatt Macy * Determine if this pool is currently active, in which case we 791eda14cbcSMatt Macy * can't actually import it. 792eda14cbcSMatt Macy */ 793eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 794eda14cbcSMatt Macy &name) == 0); 795eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 796eda14cbcSMatt Macy &guid) == 0); 797eda14cbcSMatt Macy 798eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, guid, &isactive) != 0) 799eda14cbcSMatt Macy goto error; 800eda14cbcSMatt Macy 801eda14cbcSMatt Macy if (isactive) { 802eda14cbcSMatt Macy nvlist_free(config); 803eda14cbcSMatt Macy config = NULL; 804eda14cbcSMatt Macy continue; 805eda14cbcSMatt Macy } 806eda14cbcSMatt Macy 807eda14cbcSMatt Macy if (policy != NULL) { 808eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 809eda14cbcSMatt Macy policy) != 0) 810eda14cbcSMatt Macy goto nomem; 811eda14cbcSMatt Macy } 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy if ((nvl = zutil_refresh_config(hdl, config)) == NULL) { 814eda14cbcSMatt Macy nvlist_free(config); 815eda14cbcSMatt Macy config = NULL; 816eda14cbcSMatt Macy continue; 817eda14cbcSMatt Macy } 818eda14cbcSMatt Macy 819eda14cbcSMatt Macy nvlist_free(config); 820eda14cbcSMatt Macy config = nvl; 821eda14cbcSMatt Macy 822eda14cbcSMatt Macy /* 823eda14cbcSMatt Macy * Go through and update the paths for spares, now that we have 824eda14cbcSMatt Macy * them. 825eda14cbcSMatt Macy */ 826eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 827eda14cbcSMatt Macy &nvroot) == 0); 828eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 829eda14cbcSMatt Macy &spares, &nspares) == 0) { 830eda14cbcSMatt Macy for (i = 0; i < nspares; i++) { 831eda14cbcSMatt Macy if (fix_paths(hdl, spares[i], pl->names) != 0) 832eda14cbcSMatt Macy goto nomem; 833eda14cbcSMatt Macy } 834eda14cbcSMatt Macy } 835eda14cbcSMatt Macy 836eda14cbcSMatt Macy /* 837eda14cbcSMatt Macy * Update the paths for l2cache devices. 838eda14cbcSMatt Macy */ 839eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 840eda14cbcSMatt Macy &l2cache, &nl2cache) == 0) { 841eda14cbcSMatt Macy for (i = 0; i < nl2cache; i++) { 842eda14cbcSMatt Macy if (fix_paths(hdl, l2cache[i], pl->names) != 0) 843eda14cbcSMatt Macy goto nomem; 844eda14cbcSMatt Macy } 845eda14cbcSMatt Macy } 846eda14cbcSMatt Macy 847eda14cbcSMatt Macy /* 848eda14cbcSMatt Macy * Restore the original information read from the actual label. 849eda14cbcSMatt Macy */ 850eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 851eda14cbcSMatt Macy DATA_TYPE_UINT64); 852eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 853eda14cbcSMatt Macy DATA_TYPE_STRING); 854eda14cbcSMatt Macy if (hostid != 0) { 855eda14cbcSMatt Macy verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 856eda14cbcSMatt Macy hostid) == 0); 857eda14cbcSMatt Macy verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 858eda14cbcSMatt Macy hostname) == 0); 859eda14cbcSMatt Macy } 860eda14cbcSMatt Macy 861eda14cbcSMatt Macy add_pool: 862eda14cbcSMatt Macy /* 863eda14cbcSMatt Macy * Add this pool to the list of configs. 864eda14cbcSMatt Macy */ 865eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 866eda14cbcSMatt Macy &name) == 0); 867eda14cbcSMatt Macy 868eda14cbcSMatt Macy if (nvlist_add_nvlist(ret, name, config) != 0) 869eda14cbcSMatt Macy goto nomem; 870eda14cbcSMatt Macy 871eda14cbcSMatt Macy nvlist_free(config); 872eda14cbcSMatt Macy config = NULL; 873eda14cbcSMatt Macy } 874eda14cbcSMatt Macy 875eda14cbcSMatt Macy return (ret); 876eda14cbcSMatt Macy 877eda14cbcSMatt Macy nomem: 878eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 879eda14cbcSMatt Macy error: 880eda14cbcSMatt Macy nvlist_free(config); 881eda14cbcSMatt Macy nvlist_free(ret); 882eda14cbcSMatt Macy for (c = 0; c < children; c++) 883eda14cbcSMatt Macy nvlist_free(child[c]); 884eda14cbcSMatt Macy free(child); 885eda14cbcSMatt Macy 886eda14cbcSMatt Macy return (NULL); 887eda14cbcSMatt Macy } 888eda14cbcSMatt Macy 889eda14cbcSMatt Macy /* 890eda14cbcSMatt Macy * Return the offset of the given label. 891eda14cbcSMatt Macy */ 892eda14cbcSMatt Macy static uint64_t 893eda14cbcSMatt Macy label_offset(uint64_t size, int l) 894eda14cbcSMatt Macy { 895eda14cbcSMatt Macy ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 896eda14cbcSMatt Macy return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 897eda14cbcSMatt Macy 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 898eda14cbcSMatt Macy } 899eda14cbcSMatt Macy 900eda14cbcSMatt Macy /* 90116038816SMartin Matuska * The same description applies as to zpool_read_label below, 90216038816SMartin Matuska * except here we do it without aio, presumably because an aio call 90316038816SMartin Matuska * errored out in a way we think not using it could circumvent. 90416038816SMartin Matuska */ 90516038816SMartin Matuska static int 90616038816SMartin Matuska zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels) 90716038816SMartin Matuska { 90816038816SMartin Matuska struct stat64 statbuf; 90916038816SMartin Matuska int l, count = 0; 91016038816SMartin Matuska vdev_phys_t *label; 91116038816SMartin Matuska nvlist_t *expected_config = NULL; 91216038816SMartin Matuska uint64_t expected_guid = 0, size; 91316038816SMartin Matuska int error; 91416038816SMartin Matuska 91516038816SMartin Matuska *config = NULL; 91616038816SMartin Matuska 91716038816SMartin Matuska if (fstat64_blk(fd, &statbuf) == -1) 91816038816SMartin Matuska return (0); 91916038816SMartin Matuska size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 92016038816SMartin Matuska 92116038816SMartin Matuska error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label)); 92216038816SMartin Matuska if (error) 92316038816SMartin Matuska return (-1); 92416038816SMartin Matuska 92516038816SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 92616038816SMartin Matuska uint64_t state, guid, txg; 92716038816SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 92816038816SMartin Matuska 92916038816SMartin Matuska if (pread64(fd, label, sizeof (vdev_phys_t), 93016038816SMartin Matuska offset) != sizeof (vdev_phys_t)) 93116038816SMartin Matuska continue; 93216038816SMartin Matuska 93316038816SMartin Matuska if (nvlist_unpack(label->vp_nvlist, 93416038816SMartin Matuska sizeof (label->vp_nvlist), config, 0) != 0) 93516038816SMartin Matuska continue; 93616038816SMartin Matuska 93716038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 93816038816SMartin Matuska &guid) != 0 || guid == 0) { 93916038816SMartin Matuska nvlist_free(*config); 94016038816SMartin Matuska continue; 94116038816SMartin Matuska } 94216038816SMartin Matuska 94316038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 94416038816SMartin Matuska &state) != 0 || state > POOL_STATE_L2CACHE) { 94516038816SMartin Matuska nvlist_free(*config); 94616038816SMartin Matuska continue; 94716038816SMartin Matuska } 94816038816SMartin Matuska 94916038816SMartin Matuska if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 95016038816SMartin Matuska (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 95116038816SMartin Matuska &txg) != 0 || txg == 0)) { 95216038816SMartin Matuska nvlist_free(*config); 95316038816SMartin Matuska continue; 95416038816SMartin Matuska } 95516038816SMartin Matuska 95616038816SMartin Matuska if (expected_guid) { 95716038816SMartin Matuska if (expected_guid == guid) 95816038816SMartin Matuska count++; 95916038816SMartin Matuska 96016038816SMartin Matuska nvlist_free(*config); 96116038816SMartin Matuska } else { 96216038816SMartin Matuska expected_config = *config; 96316038816SMartin Matuska expected_guid = guid; 96416038816SMartin Matuska count++; 96516038816SMartin Matuska } 96616038816SMartin Matuska } 96716038816SMartin Matuska 96816038816SMartin Matuska if (num_labels != NULL) 96916038816SMartin Matuska *num_labels = count; 97016038816SMartin Matuska 97116038816SMartin Matuska free(label); 97216038816SMartin Matuska *config = expected_config; 97316038816SMartin Matuska 97416038816SMartin Matuska return (0); 97516038816SMartin Matuska } 97616038816SMartin Matuska 97716038816SMartin Matuska /* 978eda14cbcSMatt Macy * Given a file descriptor, read the label information and return an nvlist 979eda14cbcSMatt Macy * describing the configuration, if there is one. The number of valid 980eda14cbcSMatt Macy * labels found will be returned in num_labels when non-NULL. 981eda14cbcSMatt Macy */ 982eda14cbcSMatt Macy int 983eda14cbcSMatt Macy zpool_read_label(int fd, nvlist_t **config, int *num_labels) 984eda14cbcSMatt Macy { 985eda14cbcSMatt Macy struct stat64 statbuf; 986184c1b94SMartin Matuska struct aiocb aiocbs[VDEV_LABELS]; 987184c1b94SMartin Matuska struct aiocb *aiocbps[VDEV_LABELS]; 988184c1b94SMartin Matuska vdev_phys_t *labels; 989eda14cbcSMatt Macy nvlist_t *expected_config = NULL; 990eda14cbcSMatt Macy uint64_t expected_guid = 0, size; 991184c1b94SMartin Matuska int error, l, count = 0; 992eda14cbcSMatt Macy 993eda14cbcSMatt Macy *config = NULL; 994eda14cbcSMatt Macy 995eda14cbcSMatt Macy if (fstat64_blk(fd, &statbuf) == -1) 996eda14cbcSMatt Macy return (0); 997eda14cbcSMatt Macy size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 998eda14cbcSMatt Macy 999184c1b94SMartin Matuska error = posix_memalign((void **)&labels, PAGESIZE, 1000184c1b94SMartin Matuska VDEV_LABELS * sizeof (*labels)); 1001eda14cbcSMatt Macy if (error) 1002eda14cbcSMatt Macy return (-1); 1003eda14cbcSMatt Macy 1004184c1b94SMartin Matuska memset(aiocbs, 0, sizeof (aiocbs)); 1005184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 1006184c1b94SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 1007184c1b94SMartin Matuska 1008184c1b94SMartin Matuska aiocbs[l].aio_fildes = fd; 1009184c1b94SMartin Matuska aiocbs[l].aio_offset = offset; 1010184c1b94SMartin Matuska aiocbs[l].aio_buf = &labels[l]; 1011184c1b94SMartin Matuska aiocbs[l].aio_nbytes = sizeof (vdev_phys_t); 1012184c1b94SMartin Matuska aiocbs[l].aio_lio_opcode = LIO_READ; 1013184c1b94SMartin Matuska aiocbps[l] = &aiocbs[l]; 1014184c1b94SMartin Matuska } 1015184c1b94SMartin Matuska 1016184c1b94SMartin Matuska if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) { 1017184c1b94SMartin Matuska int saved_errno = errno; 101816038816SMartin Matuska boolean_t do_slow = B_FALSE; 101916038816SMartin Matuska error = -1; 1020184c1b94SMartin Matuska 1021184c1b94SMartin Matuska if (errno == EAGAIN || errno == EINTR || errno == EIO) { 1022184c1b94SMartin Matuska /* 1023184c1b94SMartin Matuska * A portion of the requests may have been submitted. 1024184c1b94SMartin Matuska * Clean them up. 1025184c1b94SMartin Matuska */ 1026184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 1027184c1b94SMartin Matuska errno = 0; 102816038816SMartin Matuska switch (aio_error(&aiocbs[l])) { 102916038816SMartin Matuska case EINVAL: 103016038816SMartin Matuska break; 103116038816SMartin Matuska case EINPROGRESS: 103216038816SMartin Matuska // This shouldn't be possible to 103316038816SMartin Matuska // encounter, die if we do. 103416038816SMartin Matuska ASSERT(B_FALSE); 103516038816SMartin Matuska case EOPNOTSUPP: 103616038816SMartin Matuska case ENOSYS: 103716038816SMartin Matuska do_slow = B_TRUE; 1038*1f88aa09SMartin Matuska /* FALLTHROUGH */ 103916038816SMartin Matuska case 0: 104016038816SMartin Matuska default: 1041184c1b94SMartin Matuska (void) aio_return(&aiocbs[l]); 1042184c1b94SMartin Matuska } 1043184c1b94SMartin Matuska } 104416038816SMartin Matuska } 104516038816SMartin Matuska if (do_slow) { 104616038816SMartin Matuska /* 104716038816SMartin Matuska * At least some IO involved access unsafe-for-AIO 104816038816SMartin Matuska * files. Let's try again, without AIO this time. 104916038816SMartin Matuska */ 105016038816SMartin Matuska error = zpool_read_label_slow(fd, config, num_labels); 105116038816SMartin Matuska saved_errno = errno; 105216038816SMartin Matuska } 1053184c1b94SMartin Matuska free(labels); 1054184c1b94SMartin Matuska errno = saved_errno; 105516038816SMartin Matuska return (error); 1056184c1b94SMartin Matuska } 1057184c1b94SMartin Matuska 1058eda14cbcSMatt Macy for (l = 0; l < VDEV_LABELS; l++) { 1059eda14cbcSMatt Macy uint64_t state, guid, txg; 1060eda14cbcSMatt Macy 1061184c1b94SMartin Matuska if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t)) 1062eda14cbcSMatt Macy continue; 1063eda14cbcSMatt Macy 1064184c1b94SMartin Matuska if (nvlist_unpack(labels[l].vp_nvlist, 1065184c1b94SMartin Matuska sizeof (labels[l].vp_nvlist), config, 0) != 0) 1066eda14cbcSMatt Macy continue; 1067eda14cbcSMatt Macy 1068eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 1069eda14cbcSMatt Macy &guid) != 0 || guid == 0) { 1070eda14cbcSMatt Macy nvlist_free(*config); 1071eda14cbcSMatt Macy continue; 1072eda14cbcSMatt Macy } 1073eda14cbcSMatt Macy 1074eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 1075eda14cbcSMatt Macy &state) != 0 || state > POOL_STATE_L2CACHE) { 1076eda14cbcSMatt Macy nvlist_free(*config); 1077eda14cbcSMatt Macy continue; 1078eda14cbcSMatt Macy } 1079eda14cbcSMatt Macy 1080eda14cbcSMatt Macy if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 1081eda14cbcSMatt Macy (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 1082eda14cbcSMatt Macy &txg) != 0 || txg == 0)) { 1083eda14cbcSMatt Macy nvlist_free(*config); 1084eda14cbcSMatt Macy continue; 1085eda14cbcSMatt Macy } 1086eda14cbcSMatt Macy 1087eda14cbcSMatt Macy if (expected_guid) { 1088eda14cbcSMatt Macy if (expected_guid == guid) 1089eda14cbcSMatt Macy count++; 1090eda14cbcSMatt Macy 1091eda14cbcSMatt Macy nvlist_free(*config); 1092eda14cbcSMatt Macy } else { 1093eda14cbcSMatt Macy expected_config = *config; 1094eda14cbcSMatt Macy expected_guid = guid; 1095eda14cbcSMatt Macy count++; 1096eda14cbcSMatt Macy } 1097eda14cbcSMatt Macy } 1098eda14cbcSMatt Macy 1099eda14cbcSMatt Macy if (num_labels != NULL) 1100eda14cbcSMatt Macy *num_labels = count; 1101eda14cbcSMatt Macy 1102184c1b94SMartin Matuska free(labels); 1103eda14cbcSMatt Macy *config = expected_config; 1104eda14cbcSMatt Macy 1105eda14cbcSMatt Macy return (0); 1106eda14cbcSMatt Macy } 1107eda14cbcSMatt Macy 1108eda14cbcSMatt Macy /* 1109eda14cbcSMatt Macy * Sorted by full path and then vdev guid to allow for multiple entries with 1110eda14cbcSMatt Macy * the same full path name. This is required because it's possible to 1111eda14cbcSMatt Macy * have multiple block devices with labels that refer to the same 1112eda14cbcSMatt Macy * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both 1113eda14cbcSMatt Macy * entries need to be added to the cache. Scenarios where this can occur 1114eda14cbcSMatt Macy * include overwritten pool labels, devices which are visible from multiple 1115eda14cbcSMatt Macy * hosts and multipath devices. 1116eda14cbcSMatt Macy */ 1117eda14cbcSMatt Macy int 1118eda14cbcSMatt Macy slice_cache_compare(const void *arg1, const void *arg2) 1119eda14cbcSMatt Macy { 1120eda14cbcSMatt Macy const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; 1121eda14cbcSMatt Macy const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; 1122eda14cbcSMatt Macy uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid; 1123eda14cbcSMatt Macy uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid; 1124eda14cbcSMatt Macy int rv; 1125eda14cbcSMatt Macy 1126eda14cbcSMatt Macy rv = TREE_ISIGN(strcmp(nm1, nm2)); 1127eda14cbcSMatt Macy if (rv) 1128eda14cbcSMatt Macy return (rv); 1129eda14cbcSMatt Macy 1130eda14cbcSMatt Macy return (TREE_CMP(guid1, guid2)); 1131eda14cbcSMatt Macy } 1132eda14cbcSMatt Macy 1133eda14cbcSMatt Macy static int 1134eda14cbcSMatt Macy label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid, 1135eda14cbcSMatt Macy uint64_t vdev_guid, char **path, char **devid) 1136eda14cbcSMatt Macy { 1137eda14cbcSMatt Macy nvlist_t **child; 1138eda14cbcSMatt Macy uint_t c, children; 1139eda14cbcSMatt Macy uint64_t guid; 1140eda14cbcSMatt Macy char *val; 1141eda14cbcSMatt Macy int error; 1142eda14cbcSMatt Macy 1143eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1144eda14cbcSMatt Macy &child, &children) == 0) { 1145eda14cbcSMatt Macy for (c = 0; c < children; c++) { 1146eda14cbcSMatt Macy error = label_paths_impl(hdl, child[c], 1147eda14cbcSMatt Macy pool_guid, vdev_guid, path, devid); 1148eda14cbcSMatt Macy if (error) 1149eda14cbcSMatt Macy return (error); 1150eda14cbcSMatt Macy } 1151eda14cbcSMatt Macy return (0); 1152eda14cbcSMatt Macy } 1153eda14cbcSMatt Macy 1154eda14cbcSMatt Macy if (nvroot == NULL) 1155eda14cbcSMatt Macy return (0); 1156eda14cbcSMatt Macy 1157eda14cbcSMatt Macy error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid); 1158eda14cbcSMatt Macy if ((error != 0) || (guid != vdev_guid)) 1159eda14cbcSMatt Macy return (0); 1160eda14cbcSMatt Macy 1161eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val); 1162eda14cbcSMatt Macy if (error == 0) 1163eda14cbcSMatt Macy *path = val; 1164eda14cbcSMatt Macy 1165eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val); 1166eda14cbcSMatt Macy if (error == 0) 1167eda14cbcSMatt Macy *devid = val; 1168eda14cbcSMatt Macy 1169eda14cbcSMatt Macy return (0); 1170eda14cbcSMatt Macy } 1171eda14cbcSMatt Macy 1172eda14cbcSMatt Macy /* 1173eda14cbcSMatt Macy * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID 1174eda14cbcSMatt Macy * and store these strings as config_path and devid_path respectively. 1175eda14cbcSMatt Macy * The returned pointers are only valid as long as label remains valid. 1176eda14cbcSMatt Macy */ 1177eda14cbcSMatt Macy int 1178eda14cbcSMatt Macy label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid) 1179eda14cbcSMatt Macy { 1180eda14cbcSMatt Macy nvlist_t *nvroot; 1181eda14cbcSMatt Macy uint64_t pool_guid; 1182eda14cbcSMatt Macy uint64_t vdev_guid; 1183eda14cbcSMatt Macy 1184eda14cbcSMatt Macy *path = NULL; 1185eda14cbcSMatt Macy *devid = NULL; 1186eda14cbcSMatt Macy 1187eda14cbcSMatt Macy if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1188eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) || 1189eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid)) 1190eda14cbcSMatt Macy return (ENOENT); 1191eda14cbcSMatt Macy 1192eda14cbcSMatt Macy return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path, 1193eda14cbcSMatt Macy devid)); 1194eda14cbcSMatt Macy } 1195eda14cbcSMatt Macy 1196eda14cbcSMatt Macy static void 1197eda14cbcSMatt Macy zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock, 1198eda14cbcSMatt Macy avl_tree_t *cache, const char *path, const char *name, int order) 1199eda14cbcSMatt Macy { 1200eda14cbcSMatt Macy avl_index_t where; 1201eda14cbcSMatt Macy rdsk_node_t *slice; 1202eda14cbcSMatt Macy 1203eda14cbcSMatt Macy slice = zutil_alloc(hdl, sizeof (rdsk_node_t)); 1204eda14cbcSMatt Macy if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) { 1205eda14cbcSMatt Macy free(slice); 1206eda14cbcSMatt Macy return; 1207eda14cbcSMatt Macy } 1208eda14cbcSMatt Macy slice->rn_vdev_guid = 0; 1209eda14cbcSMatt Macy slice->rn_lock = lock; 1210eda14cbcSMatt Macy slice->rn_avl = cache; 1211eda14cbcSMatt Macy slice->rn_hdl = hdl; 1212eda14cbcSMatt Macy slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET; 1213eda14cbcSMatt Macy slice->rn_labelpaths = B_FALSE; 1214eda14cbcSMatt Macy 1215eda14cbcSMatt Macy pthread_mutex_lock(lock); 1216eda14cbcSMatt Macy if (avl_find(cache, slice, &where)) { 1217eda14cbcSMatt Macy free(slice->rn_name); 1218eda14cbcSMatt Macy free(slice); 1219eda14cbcSMatt Macy } else { 1220eda14cbcSMatt Macy avl_insert(cache, slice, where); 1221eda14cbcSMatt Macy } 1222eda14cbcSMatt Macy pthread_mutex_unlock(lock); 1223eda14cbcSMatt Macy } 1224eda14cbcSMatt Macy 1225eda14cbcSMatt Macy static int 1226eda14cbcSMatt Macy zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock, 1227eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1228eda14cbcSMatt Macy { 1229eda14cbcSMatt Macy int error; 1230eda14cbcSMatt Macy char path[MAXPATHLEN]; 1231eda14cbcSMatt Macy struct dirent64 *dp; 1232eda14cbcSMatt Macy DIR *dirp; 1233eda14cbcSMatt Macy 1234eda14cbcSMatt Macy if (realpath(dir, path) == NULL) { 1235eda14cbcSMatt Macy error = errno; 1236eda14cbcSMatt Macy if (error == ENOENT) 1237eda14cbcSMatt Macy return (0); 1238eda14cbcSMatt Macy 1239*1f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1240eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1241eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1242eda14cbcSMatt Macy return (error); 1243eda14cbcSMatt Macy } 1244eda14cbcSMatt Macy 1245eda14cbcSMatt Macy dirp = opendir(path); 1246eda14cbcSMatt Macy if (dirp == NULL) { 1247eda14cbcSMatt Macy error = errno; 1248*1f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1249eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, 1250eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 1251eda14cbcSMatt Macy return (error); 1252eda14cbcSMatt Macy } 1253eda14cbcSMatt Macy 1254eda14cbcSMatt Macy while ((dp = readdir64(dirp)) != NULL) { 1255eda14cbcSMatt Macy const char *name = dp->d_name; 12563ff01b23SMartin Matuska if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) 1257eda14cbcSMatt Macy continue; 1258eda14cbcSMatt Macy 12593ff01b23SMartin Matuska switch (dp->d_type) { 12603ff01b23SMartin Matuska case DT_UNKNOWN: 12613ff01b23SMartin Matuska case DT_BLK: 12620d8fe237SMartin Matuska case DT_LNK: 12633ff01b23SMartin Matuska #ifdef __FreeBSD__ 12643ff01b23SMartin Matuska case DT_CHR: 12653ff01b23SMartin Matuska #endif 12663ff01b23SMartin Matuska case DT_REG: 12673ff01b23SMartin Matuska break; 12683ff01b23SMartin Matuska default: 12693ff01b23SMartin Matuska continue; 12703ff01b23SMartin Matuska } 12713ff01b23SMartin Matuska 1272eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, 1273eda14cbcSMatt Macy order); 1274eda14cbcSMatt Macy } 1275eda14cbcSMatt Macy 1276eda14cbcSMatt Macy (void) closedir(dirp); 1277eda14cbcSMatt Macy return (0); 1278eda14cbcSMatt Macy } 1279eda14cbcSMatt Macy 1280eda14cbcSMatt Macy static int 1281eda14cbcSMatt Macy zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock, 1282eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1283eda14cbcSMatt Macy { 1284eda14cbcSMatt Macy int error = 0; 1285eda14cbcSMatt Macy char path[MAXPATHLEN]; 12863ff01b23SMartin Matuska char *d = NULL; 12873ff01b23SMartin Matuska ssize_t dl; 12883ff01b23SMartin Matuska const char *dpath, *name; 1289eda14cbcSMatt Macy 1290eda14cbcSMatt Macy /* 12913ff01b23SMartin Matuska * Separate the directory and the basename. 12923ff01b23SMartin Matuska * We do this so that we can get the realpath of 1293eda14cbcSMatt Macy * the directory. We don't get the realpath on the 1294eda14cbcSMatt Macy * whole path because if it's a symlink, we want the 1295eda14cbcSMatt Macy * path of the symlink not where it points to. 1296eda14cbcSMatt Macy */ 12973ff01b23SMartin Matuska name = zfs_basename(dir); 12983ff01b23SMartin Matuska if ((dl = zfs_dirnamelen(dir)) == -1) 12993ff01b23SMartin Matuska dpath = "."; 13003ff01b23SMartin Matuska else 13013ff01b23SMartin Matuska dpath = d = zutil_strndup(hdl, dir, dl); 1302eda14cbcSMatt Macy 1303eda14cbcSMatt Macy if (realpath(dpath, path) == NULL) { 1304eda14cbcSMatt Macy error = errno; 1305eda14cbcSMatt Macy if (error == ENOENT) { 1306eda14cbcSMatt Macy error = 0; 1307eda14cbcSMatt Macy goto out; 1308eda14cbcSMatt Macy } 1309eda14cbcSMatt Macy 1310*1f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1311eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1312eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1313eda14cbcSMatt Macy goto out; 1314eda14cbcSMatt Macy } 1315eda14cbcSMatt Macy 1316eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order); 1317eda14cbcSMatt Macy 1318eda14cbcSMatt Macy out: 1319eda14cbcSMatt Macy free(d); 1320eda14cbcSMatt Macy return (error); 1321eda14cbcSMatt Macy } 1322eda14cbcSMatt Macy 1323eda14cbcSMatt Macy /* 1324eda14cbcSMatt Macy * Scan a list of directories for zfs devices. 1325eda14cbcSMatt Macy */ 1326eda14cbcSMatt Macy static int 1327eda14cbcSMatt Macy zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock, 1328eda14cbcSMatt Macy avl_tree_t **slice_cache, const char * const *dir, size_t dirs) 1329eda14cbcSMatt Macy { 1330eda14cbcSMatt Macy avl_tree_t *cache; 1331eda14cbcSMatt Macy rdsk_node_t *slice; 1332eda14cbcSMatt Macy void *cookie; 1333eda14cbcSMatt Macy int i, error; 1334eda14cbcSMatt Macy 1335eda14cbcSMatt Macy *slice_cache = NULL; 1336eda14cbcSMatt Macy cache = zutil_alloc(hdl, sizeof (avl_tree_t)); 1337eda14cbcSMatt Macy avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t), 1338eda14cbcSMatt Macy offsetof(rdsk_node_t, rn_node)); 1339eda14cbcSMatt Macy 1340eda14cbcSMatt Macy for (i = 0; i < dirs; i++) { 1341eda14cbcSMatt Macy struct stat sbuf; 1342eda14cbcSMatt Macy 1343eda14cbcSMatt Macy if (stat(dir[i], &sbuf) != 0) { 1344eda14cbcSMatt Macy error = errno; 1345eda14cbcSMatt Macy if (error == ENOENT) 1346eda14cbcSMatt Macy continue; 1347eda14cbcSMatt Macy 1348*1f88aa09SMartin Matuska zutil_error_aux(hdl, "%s", strerror(error)); 1349eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1350eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]); 1351eda14cbcSMatt Macy goto error; 1352eda14cbcSMatt Macy } 1353eda14cbcSMatt Macy 1354eda14cbcSMatt Macy /* 1355eda14cbcSMatt Macy * If dir[i] is a directory, we walk through it and add all 1356eda14cbcSMatt Macy * the entries to the cache. If it's not a directory, we just 1357eda14cbcSMatt Macy * add it to the cache. 1358eda14cbcSMatt Macy */ 1359eda14cbcSMatt Macy if (S_ISDIR(sbuf.st_mode)) { 1360eda14cbcSMatt Macy if ((error = zpool_find_import_scan_dir(hdl, lock, 1361eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1362eda14cbcSMatt Macy goto error; 1363eda14cbcSMatt Macy } else { 1364eda14cbcSMatt Macy if ((error = zpool_find_import_scan_path(hdl, lock, 1365eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1366eda14cbcSMatt Macy goto error; 1367eda14cbcSMatt Macy } 1368eda14cbcSMatt Macy } 1369eda14cbcSMatt Macy 1370eda14cbcSMatt Macy *slice_cache = cache; 1371eda14cbcSMatt Macy return (0); 1372eda14cbcSMatt Macy 1373eda14cbcSMatt Macy error: 1374eda14cbcSMatt Macy cookie = NULL; 1375eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1376eda14cbcSMatt Macy free(slice->rn_name); 1377eda14cbcSMatt Macy free(slice); 1378eda14cbcSMatt Macy } 1379eda14cbcSMatt Macy free(cache); 1380eda14cbcSMatt Macy 1381eda14cbcSMatt Macy return (error); 1382eda14cbcSMatt Macy } 1383eda14cbcSMatt Macy 1384eda14cbcSMatt Macy /* 1385eda14cbcSMatt Macy * Given a list of directories to search, find all pools stored on disk. This 1386eda14cbcSMatt Macy * includes partial pools which are not available to import. If no args are 1387eda14cbcSMatt Macy * given (argc is 0), then the default directory (/dev/dsk) is searched. 1388eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1389eda14cbcSMatt Macy * to import a specific pool. 1390eda14cbcSMatt Macy */ 1391eda14cbcSMatt Macy static nvlist_t * 13929db44a8eSMartin Matuska zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg, 13939db44a8eSMartin Matuska pthread_mutex_t *lock, avl_tree_t *cache) 1394eda14cbcSMatt Macy { 1395eda14cbcSMatt Macy nvlist_t *ret = NULL; 1396eda14cbcSMatt Macy pool_list_t pools = { 0 }; 1397eda14cbcSMatt Macy pool_entry_t *pe, *penext; 1398eda14cbcSMatt Macy vdev_entry_t *ve, *venext; 1399eda14cbcSMatt Macy config_entry_t *ce, *cenext; 1400eda14cbcSMatt Macy name_entry_t *ne, *nenext; 1401eda14cbcSMatt Macy rdsk_node_t *slice; 1402eda14cbcSMatt Macy void *cookie; 1403eda14cbcSMatt Macy tpool_t *t; 1404eda14cbcSMatt Macy 1405eda14cbcSMatt Macy verify(iarg->poolname == NULL || iarg->guid == 0); 1406eda14cbcSMatt Macy 1407eda14cbcSMatt Macy /* 1408eda14cbcSMatt Macy * Create a thread pool to parallelize the process of reading and 1409eda14cbcSMatt Macy * validating labels, a large number of threads can be used due to 1410eda14cbcSMatt Macy * minimal contention. 1411eda14cbcSMatt Macy */ 1412eda14cbcSMatt Macy t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL); 1413eda14cbcSMatt Macy for (slice = avl_first(cache); slice; 1414eda14cbcSMatt Macy (slice = avl_walk(cache, slice, AVL_AFTER))) 1415eda14cbcSMatt Macy (void) tpool_dispatch(t, zpool_open_func, slice); 1416eda14cbcSMatt Macy 1417eda14cbcSMatt Macy tpool_wait(t); 1418eda14cbcSMatt Macy tpool_destroy(t); 1419eda14cbcSMatt Macy 1420eda14cbcSMatt Macy /* 1421eda14cbcSMatt Macy * Process the cache, filtering out any entries which are not 1422eda14cbcSMatt Macy * for the specified pool then adding matching label configs. 1423eda14cbcSMatt Macy */ 1424eda14cbcSMatt Macy cookie = NULL; 1425eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1426eda14cbcSMatt Macy if (slice->rn_config != NULL) { 1427eda14cbcSMatt Macy nvlist_t *config = slice->rn_config; 1428eda14cbcSMatt Macy boolean_t matched = B_TRUE; 1429eda14cbcSMatt Macy boolean_t aux = B_FALSE; 1430eda14cbcSMatt Macy int fd; 1431eda14cbcSMatt Macy 1432eda14cbcSMatt Macy /* 1433eda14cbcSMatt Macy * Check if it's a spare or l2cache device. If it is, 1434eda14cbcSMatt Macy * we need to skip the name and guid check since they 1435eda14cbcSMatt Macy * don't exist on aux device label. 1436eda14cbcSMatt Macy */ 1437eda14cbcSMatt Macy if (iarg->poolname != NULL || iarg->guid != 0) { 1438eda14cbcSMatt Macy uint64_t state; 1439eda14cbcSMatt Macy aux = nvlist_lookup_uint64(config, 1440eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, &state) == 0 && 1441eda14cbcSMatt Macy (state == POOL_STATE_SPARE || 1442eda14cbcSMatt Macy state == POOL_STATE_L2CACHE); 1443eda14cbcSMatt Macy } 1444eda14cbcSMatt Macy 1445eda14cbcSMatt Macy if (iarg->poolname != NULL && !aux) { 1446eda14cbcSMatt Macy char *pname; 1447eda14cbcSMatt Macy 1448eda14cbcSMatt Macy matched = nvlist_lookup_string(config, 1449eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, &pname) == 0 && 1450eda14cbcSMatt Macy strcmp(iarg->poolname, pname) == 0; 1451eda14cbcSMatt Macy } else if (iarg->guid != 0 && !aux) { 1452eda14cbcSMatt Macy uint64_t this_guid; 1453eda14cbcSMatt Macy 1454eda14cbcSMatt Macy matched = nvlist_lookup_uint64(config, 1455eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 && 1456eda14cbcSMatt Macy iarg->guid == this_guid; 1457eda14cbcSMatt Macy } 1458eda14cbcSMatt Macy if (matched) { 1459eda14cbcSMatt Macy /* 1460eda14cbcSMatt Macy * Verify all remaining entries can be opened 1461eda14cbcSMatt Macy * exclusively. This will prune all underlying 1462eda14cbcSMatt Macy * multipath devices which otherwise could 1463eda14cbcSMatt Macy * result in the vdev appearing as UNAVAIL. 1464eda14cbcSMatt Macy * 1465eda14cbcSMatt Macy * Under zdb, this step isn't required and 1466eda14cbcSMatt Macy * would prevent a zdb -e of active pools with 1467eda14cbcSMatt Macy * no cachefile. 1468eda14cbcSMatt Macy */ 146916038816SMartin Matuska fd = open(slice->rn_name, 147016038816SMartin Matuska O_RDONLY | O_EXCL | O_CLOEXEC); 1471eda14cbcSMatt Macy if (fd >= 0 || iarg->can_be_active) { 1472eda14cbcSMatt Macy if (fd >= 0) 1473eda14cbcSMatt Macy close(fd); 1474eda14cbcSMatt Macy add_config(hdl, &pools, 1475eda14cbcSMatt Macy slice->rn_name, slice->rn_order, 1476eda14cbcSMatt Macy slice->rn_num_labels, config); 1477eda14cbcSMatt Macy } 1478eda14cbcSMatt Macy } 1479eda14cbcSMatt Macy nvlist_free(config); 1480eda14cbcSMatt Macy } 1481eda14cbcSMatt Macy free(slice->rn_name); 1482eda14cbcSMatt Macy free(slice); 1483eda14cbcSMatt Macy } 1484eda14cbcSMatt Macy avl_destroy(cache); 1485eda14cbcSMatt Macy free(cache); 1486eda14cbcSMatt Macy 1487eda14cbcSMatt Macy ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy); 1488eda14cbcSMatt Macy 1489eda14cbcSMatt Macy for (pe = pools.pools; pe != NULL; pe = penext) { 1490eda14cbcSMatt Macy penext = pe->pe_next; 1491eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 1492eda14cbcSMatt Macy venext = ve->ve_next; 1493eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 1494eda14cbcSMatt Macy cenext = ce->ce_next; 1495eda14cbcSMatt Macy nvlist_free(ce->ce_config); 1496eda14cbcSMatt Macy free(ce); 1497eda14cbcSMatt Macy } 1498eda14cbcSMatt Macy free(ve); 1499eda14cbcSMatt Macy } 1500eda14cbcSMatt Macy free(pe); 1501eda14cbcSMatt Macy } 1502eda14cbcSMatt Macy 1503eda14cbcSMatt Macy for (ne = pools.names; ne != NULL; ne = nenext) { 1504eda14cbcSMatt Macy nenext = ne->ne_next; 1505eda14cbcSMatt Macy free(ne->ne_name); 1506eda14cbcSMatt Macy free(ne); 1507eda14cbcSMatt Macy } 1508eda14cbcSMatt Macy 1509eda14cbcSMatt Macy return (ret); 1510eda14cbcSMatt Macy } 1511eda14cbcSMatt Macy 1512eda14cbcSMatt Macy /* 15139db44a8eSMartin Matuska * Given a config, discover the paths for the devices which 15149db44a8eSMartin Matuska * exist in the config. 15159db44a8eSMartin Matuska */ 15169db44a8eSMartin Matuska static int 15179db44a8eSMartin Matuska discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv, 15189db44a8eSMartin Matuska avl_tree_t *cache, pthread_mutex_t *lock) 15199db44a8eSMartin Matuska { 15209db44a8eSMartin Matuska char *path = NULL; 15213ff01b23SMartin Matuska ssize_t dl; 15229db44a8eSMartin Matuska uint_t children; 15239db44a8eSMartin Matuska nvlist_t **child; 15249db44a8eSMartin Matuska 15259db44a8eSMartin Matuska if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 15269db44a8eSMartin Matuska &child, &children) == 0) { 15279db44a8eSMartin Matuska for (int c = 0; c < children; c++) { 15289db44a8eSMartin Matuska discover_cached_paths(hdl, child[c], cache, lock); 15299db44a8eSMartin Matuska } 15309db44a8eSMartin Matuska } 15319db44a8eSMartin Matuska 15329db44a8eSMartin Matuska /* 15339db44a8eSMartin Matuska * Once we have the path, we need to add the directory to 153416038816SMartin Matuska * our directory cache. 15359db44a8eSMartin Matuska */ 15369db44a8eSMartin Matuska if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 15373ff01b23SMartin Matuska if ((dl = zfs_dirnamelen(path)) == -1) 15383ff01b23SMartin Matuska path = "."; 15393ff01b23SMartin Matuska else 15403ff01b23SMartin Matuska path[dl] = '\0'; 15419db44a8eSMartin Matuska return (zpool_find_import_scan_dir(hdl, lock, cache, 15423ff01b23SMartin Matuska path, 0)); 15439db44a8eSMartin Matuska } 15449db44a8eSMartin Matuska return (0); 15459db44a8eSMartin Matuska } 15469db44a8eSMartin Matuska 15479db44a8eSMartin Matuska /* 1548eda14cbcSMatt Macy * Given a cache file, return the contents as a list of importable pools. 1549eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1550eda14cbcSMatt Macy * to import a specific pool. 1551eda14cbcSMatt Macy */ 1552eda14cbcSMatt Macy static nvlist_t * 15539db44a8eSMartin Matuska zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg) 1554eda14cbcSMatt Macy { 1555eda14cbcSMatt Macy char *buf; 1556eda14cbcSMatt Macy int fd; 1557eda14cbcSMatt Macy struct stat64 statbuf; 1558eda14cbcSMatt Macy nvlist_t *raw, *src, *dst; 1559eda14cbcSMatt Macy nvlist_t *pools; 1560eda14cbcSMatt Macy nvpair_t *elem; 1561eda14cbcSMatt Macy char *name; 1562eda14cbcSMatt Macy uint64_t this_guid; 1563eda14cbcSMatt Macy boolean_t active; 1564eda14cbcSMatt Macy 15659db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 1566eda14cbcSMatt Macy 156716038816SMartin Matuska if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) { 1568eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1569eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1570eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "failed to open cache file")); 1571eda14cbcSMatt Macy return (NULL); 1572eda14cbcSMatt Macy } 1573eda14cbcSMatt Macy 1574eda14cbcSMatt Macy if (fstat64(fd, &statbuf) != 0) { 1575eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1576eda14cbcSMatt Macy (void) close(fd); 1577eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1578eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 1579eda14cbcSMatt Macy return (NULL); 1580eda14cbcSMatt Macy } 1581eda14cbcSMatt Macy 1582eda14cbcSMatt Macy if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) { 1583eda14cbcSMatt Macy (void) close(fd); 1584eda14cbcSMatt Macy return (NULL); 1585eda14cbcSMatt Macy } 1586eda14cbcSMatt Macy 1587eda14cbcSMatt Macy if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 1588eda14cbcSMatt Macy (void) close(fd); 1589eda14cbcSMatt Macy free(buf); 1590eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1591eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 1592eda14cbcSMatt Macy "failed to read cache file contents")); 1593eda14cbcSMatt Macy return (NULL); 1594eda14cbcSMatt Macy } 1595eda14cbcSMatt Macy 1596eda14cbcSMatt Macy (void) close(fd); 1597eda14cbcSMatt Macy 1598eda14cbcSMatt Macy if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 1599eda14cbcSMatt Macy free(buf); 1600eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1601eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 1602eda14cbcSMatt Macy "invalid or corrupt cache file contents")); 1603eda14cbcSMatt Macy return (NULL); 1604eda14cbcSMatt Macy } 1605eda14cbcSMatt Macy 1606eda14cbcSMatt Macy free(buf); 1607eda14cbcSMatt Macy 1608eda14cbcSMatt Macy /* 1609eda14cbcSMatt Macy * Go through and get the current state of the pools and refresh their 1610eda14cbcSMatt Macy * state. 1611eda14cbcSMatt Macy */ 1612eda14cbcSMatt Macy if (nvlist_alloc(&pools, 0, 0) != 0) { 1613eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1614eda14cbcSMatt Macy nvlist_free(raw); 1615eda14cbcSMatt Macy return (NULL); 1616eda14cbcSMatt Macy } 1617eda14cbcSMatt Macy 1618eda14cbcSMatt Macy elem = NULL; 1619eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 1620eda14cbcSMatt Macy src = fnvpair_value_nvlist(elem); 1621eda14cbcSMatt Macy 1622eda14cbcSMatt Macy name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); 16239db44a8eSMartin Matuska if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0) 1624eda14cbcSMatt Macy continue; 1625eda14cbcSMatt Macy 1626eda14cbcSMatt Macy this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); 16279db44a8eSMartin Matuska if (iarg->guid != 0 && iarg->guid != this_guid) 1628eda14cbcSMatt Macy continue; 1629eda14cbcSMatt Macy 1630eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, this_guid, &active) != 0) { 1631eda14cbcSMatt Macy nvlist_free(raw); 1632eda14cbcSMatt Macy nvlist_free(pools); 1633eda14cbcSMatt Macy return (NULL); 1634eda14cbcSMatt Macy } 1635eda14cbcSMatt Macy 1636eda14cbcSMatt Macy if (active) 1637eda14cbcSMatt Macy continue; 1638eda14cbcSMatt Macy 16399db44a8eSMartin Matuska if (iarg->scan) { 16409db44a8eSMartin Matuska uint64_t saved_guid = iarg->guid; 16419db44a8eSMartin Matuska const char *saved_poolname = iarg->poolname; 16429db44a8eSMartin Matuska pthread_mutex_t lock; 16439db44a8eSMartin Matuska 16449db44a8eSMartin Matuska /* 16459db44a8eSMartin Matuska * Create the device cache that will hold the 16469db44a8eSMartin Matuska * devices we will scan based on the cachefile. 16479db44a8eSMartin Matuska * This will get destroyed and freed by 16489db44a8eSMartin Matuska * zpool_find_import_impl. 16499db44a8eSMartin Matuska */ 16509db44a8eSMartin Matuska avl_tree_t *cache = zutil_alloc(hdl, 16519db44a8eSMartin Matuska sizeof (avl_tree_t)); 16529db44a8eSMartin Matuska avl_create(cache, slice_cache_compare, 16539db44a8eSMartin Matuska sizeof (rdsk_node_t), 16549db44a8eSMartin Matuska offsetof(rdsk_node_t, rn_node)); 16559db44a8eSMartin Matuska nvlist_t *nvroot = fnvlist_lookup_nvlist(src, 16569db44a8eSMartin Matuska ZPOOL_CONFIG_VDEV_TREE); 16579db44a8eSMartin Matuska 16589db44a8eSMartin Matuska /* 16599db44a8eSMartin Matuska * We only want to find the pool with this_guid. 16609db44a8eSMartin Matuska * We will reset these values back later. 16619db44a8eSMartin Matuska */ 16629db44a8eSMartin Matuska iarg->guid = this_guid; 16639db44a8eSMartin Matuska iarg->poolname = NULL; 16649db44a8eSMartin Matuska 16659db44a8eSMartin Matuska /* 16669db44a8eSMartin Matuska * We need to build up a cache of devices that exists 16679db44a8eSMartin Matuska * in the paths pointed to by the cachefile. This allows 16689db44a8eSMartin Matuska * us to preserve the device namespace that was 16699db44a8eSMartin Matuska * originally specified by the user but also lets us 16709db44a8eSMartin Matuska * scan devices in those directories in case they had 16719db44a8eSMartin Matuska * been renamed. 16729db44a8eSMartin Matuska */ 16739db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 16749db44a8eSMartin Matuska discover_cached_paths(hdl, nvroot, cache, &lock); 16759db44a8eSMartin Matuska nvlist_t *nv = zpool_find_import_impl(hdl, iarg, 16769db44a8eSMartin Matuska &lock, cache); 16779db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 16789db44a8eSMartin Matuska 16799db44a8eSMartin Matuska /* 16809db44a8eSMartin Matuska * zpool_find_import_impl will return back 16819db44a8eSMartin Matuska * a list of pools that it found based on the 16829db44a8eSMartin Matuska * device cache. There should only be one pool 16839db44a8eSMartin Matuska * since we're looking for a specific guid. 16849db44a8eSMartin Matuska * We will use that pool to build up the final 16859db44a8eSMartin Matuska * pool nvlist which is returned back to the 16869db44a8eSMartin Matuska * caller. 16879db44a8eSMartin Matuska */ 16889db44a8eSMartin Matuska nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 16899db44a8eSMartin Matuska fnvlist_add_nvlist(pools, nvpair_name(pair), 16909db44a8eSMartin Matuska fnvpair_value_nvlist(pair)); 16919db44a8eSMartin Matuska 16929db44a8eSMartin Matuska VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL); 16939db44a8eSMartin Matuska 16949db44a8eSMartin Matuska iarg->guid = saved_guid; 16959db44a8eSMartin Matuska iarg->poolname = saved_poolname; 16969db44a8eSMartin Matuska continue; 16979db44a8eSMartin Matuska } 16989db44a8eSMartin Matuska 1699eda14cbcSMatt Macy if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, 17009db44a8eSMartin Matuska iarg->cachefile) != 0) { 1701eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1702eda14cbcSMatt Macy nvlist_free(raw); 1703eda14cbcSMatt Macy nvlist_free(pools); 1704eda14cbcSMatt Macy return (NULL); 1705eda14cbcSMatt Macy } 1706eda14cbcSMatt Macy 1707eda14cbcSMatt Macy if ((dst = zutil_refresh_config(hdl, src)) == NULL) { 1708eda14cbcSMatt Macy nvlist_free(raw); 1709eda14cbcSMatt Macy nvlist_free(pools); 1710eda14cbcSMatt Macy return (NULL); 1711eda14cbcSMatt Macy } 1712eda14cbcSMatt Macy 1713eda14cbcSMatt Macy if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1714eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1715eda14cbcSMatt Macy nvlist_free(dst); 1716eda14cbcSMatt Macy nvlist_free(raw); 1717eda14cbcSMatt Macy nvlist_free(pools); 1718eda14cbcSMatt Macy return (NULL); 1719eda14cbcSMatt Macy } 1720eda14cbcSMatt Macy nvlist_free(dst); 1721eda14cbcSMatt Macy } 1722eda14cbcSMatt Macy nvlist_free(raw); 1723eda14cbcSMatt Macy return (pools); 1724eda14cbcSMatt Macy } 1725eda14cbcSMatt Macy 17269db44a8eSMartin Matuska static nvlist_t * 17279db44a8eSMartin Matuska zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg) 17289db44a8eSMartin Matuska { 17299db44a8eSMartin Matuska pthread_mutex_t lock; 17309db44a8eSMartin Matuska avl_tree_t *cache; 17319db44a8eSMartin Matuska nvlist_t *pools = NULL; 17329db44a8eSMartin Matuska 17339db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 17349db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 17359db44a8eSMartin Matuska 17369db44a8eSMartin Matuska /* 17379db44a8eSMartin Matuska * Locate pool member vdevs by blkid or by directory scanning. 17389db44a8eSMartin Matuska * On success a newly allocated AVL tree which is populated with an 17399db44a8eSMartin Matuska * entry for each discovered vdev will be returned in the cache. 17409db44a8eSMartin Matuska * It's the caller's responsibility to consume and destroy this tree. 17419db44a8eSMartin Matuska */ 17429db44a8eSMartin Matuska if (iarg->scan || iarg->paths != 0) { 17439db44a8eSMartin Matuska size_t dirs = iarg->paths; 17449db44a8eSMartin Matuska const char * const *dir = (const char * const *)iarg->path; 17459db44a8eSMartin Matuska 17469db44a8eSMartin Matuska if (dirs == 0) 17479db44a8eSMartin Matuska dir = zpool_default_search_paths(&dirs); 17489db44a8eSMartin Matuska 17499db44a8eSMartin Matuska if (zpool_find_import_scan(hdl, &lock, &cache, 17509db44a8eSMartin Matuska dir, dirs) != 0) { 17519db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17529db44a8eSMartin Matuska return (NULL); 17539db44a8eSMartin Matuska } 17549db44a8eSMartin Matuska } else { 17559db44a8eSMartin Matuska if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) { 17569db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17579db44a8eSMartin Matuska return (NULL); 17589db44a8eSMartin Matuska } 17599db44a8eSMartin Matuska } 17609db44a8eSMartin Matuska 17619db44a8eSMartin Matuska pools = zpool_find_import_impl(hdl, iarg, &lock, cache); 17629db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17639db44a8eSMartin Matuska return (pools); 17649db44a8eSMartin Matuska } 17659db44a8eSMartin Matuska 17669db44a8eSMartin Matuska 1767eda14cbcSMatt Macy nvlist_t * 1768eda14cbcSMatt Macy zpool_search_import(void *hdl, importargs_t *import, 1769eda14cbcSMatt Macy const pool_config_ops_t *pco) 1770eda14cbcSMatt Macy { 1771eda14cbcSMatt Macy libpc_handle_t handle = { 0 }; 1772eda14cbcSMatt Macy nvlist_t *pools = NULL; 1773eda14cbcSMatt Macy 1774eda14cbcSMatt Macy handle.lpc_lib_handle = hdl; 1775eda14cbcSMatt Macy handle.lpc_ops = pco; 1776eda14cbcSMatt Macy handle.lpc_printerr = B_TRUE; 1777eda14cbcSMatt Macy 1778eda14cbcSMatt Macy verify(import->poolname == NULL || import->guid == 0); 1779eda14cbcSMatt Macy 1780eda14cbcSMatt Macy if (import->cachefile != NULL) 17819db44a8eSMartin Matuska pools = zpool_find_import_cached(&handle, import); 1782eda14cbcSMatt Macy else 17839db44a8eSMartin Matuska pools = zpool_find_import(&handle, import); 1784eda14cbcSMatt Macy 1785eda14cbcSMatt Macy if ((pools == NULL || nvlist_empty(pools)) && 1786eda14cbcSMatt Macy handle.lpc_open_access_error && geteuid() != 0) { 1787eda14cbcSMatt Macy (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN, 1788eda14cbcSMatt Macy "no pools found")); 1789eda14cbcSMatt Macy } 1790eda14cbcSMatt Macy 1791eda14cbcSMatt Macy return (pools); 1792eda14cbcSMatt Macy } 1793eda14cbcSMatt Macy 1794eda14cbcSMatt Macy static boolean_t 1795eda14cbcSMatt Macy pool_match(nvlist_t *cfg, char *tgt) 1796eda14cbcSMatt Macy { 1797eda14cbcSMatt Macy uint64_t v, guid = strtoull(tgt, NULL, 0); 1798eda14cbcSMatt Macy char *s; 1799eda14cbcSMatt Macy 1800eda14cbcSMatt Macy if (guid != 0) { 1801eda14cbcSMatt Macy if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 1802eda14cbcSMatt Macy return (v == guid); 1803eda14cbcSMatt Macy } else { 1804eda14cbcSMatt Macy if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 1805eda14cbcSMatt Macy return (strcmp(s, tgt) == 0); 1806eda14cbcSMatt Macy } 1807eda14cbcSMatt Macy return (B_FALSE); 1808eda14cbcSMatt Macy } 1809eda14cbcSMatt Macy 1810eda14cbcSMatt Macy int 1811eda14cbcSMatt Macy zpool_find_config(void *hdl, const char *target, nvlist_t **configp, 1812eda14cbcSMatt Macy importargs_t *args, const pool_config_ops_t *pco) 1813eda14cbcSMatt Macy { 1814eda14cbcSMatt Macy nvlist_t *pools; 1815eda14cbcSMatt Macy nvlist_t *match = NULL; 1816eda14cbcSMatt Macy nvlist_t *config = NULL; 18177877fdebSMatt Macy char *sepp = NULL; 1818eda14cbcSMatt Macy int count = 0; 1819eda14cbcSMatt Macy char *targetdup = strdup(target); 1820eda14cbcSMatt Macy 1821eda14cbcSMatt Macy *configp = NULL; 1822eda14cbcSMatt Macy 182316038816SMartin Matuska if ((sepp = strpbrk(targetdup, "/@")) != NULL) 1824eda14cbcSMatt Macy *sepp = '\0'; 1825eda14cbcSMatt Macy 1826eda14cbcSMatt Macy pools = zpool_search_import(hdl, args, pco); 1827eda14cbcSMatt Macy 1828eda14cbcSMatt Macy if (pools != NULL) { 1829eda14cbcSMatt Macy nvpair_t *elem = NULL; 1830eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 1831eda14cbcSMatt Macy VERIFY0(nvpair_value_nvlist(elem, &config)); 1832eda14cbcSMatt Macy if (pool_match(config, targetdup)) { 1833eda14cbcSMatt Macy count++; 1834eda14cbcSMatt Macy if (match != NULL) { 1835eda14cbcSMatt Macy /* multiple matches found */ 1836eda14cbcSMatt Macy continue; 1837eda14cbcSMatt Macy } else { 18387877fdebSMatt Macy match = fnvlist_dup(config); 1839eda14cbcSMatt Macy } 1840eda14cbcSMatt Macy } 1841eda14cbcSMatt Macy } 18427877fdebSMatt Macy fnvlist_free(pools); 1843eda14cbcSMatt Macy } 1844eda14cbcSMatt Macy 1845eda14cbcSMatt Macy if (count == 0) { 1846eda14cbcSMatt Macy free(targetdup); 1847eda14cbcSMatt Macy return (ENOENT); 1848eda14cbcSMatt Macy } 1849eda14cbcSMatt Macy 1850eda14cbcSMatt Macy if (count > 1) { 1851eda14cbcSMatt Macy free(targetdup); 18527877fdebSMatt Macy fnvlist_free(match); 1853eda14cbcSMatt Macy return (EINVAL); 1854eda14cbcSMatt Macy } 1855eda14cbcSMatt Macy 1856eda14cbcSMatt Macy *configp = match; 1857eda14cbcSMatt Macy free(targetdup); 1858eda14cbcSMatt Macy 1859eda14cbcSMatt Macy return (0); 1860eda14cbcSMatt Macy } 1861