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 72eda14cbcSMatt Macy /*PRINTFLIKE2*/ 73eda14cbcSMatt Macy static void 74eda14cbcSMatt Macy zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...) 75eda14cbcSMatt Macy { 76eda14cbcSMatt Macy va_list ap; 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy va_start(ap, fmt); 79eda14cbcSMatt Macy 80eda14cbcSMatt Macy (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap); 81eda14cbcSMatt Macy hdl->lpc_desc_active = B_TRUE; 82eda14cbcSMatt Macy 83eda14cbcSMatt Macy va_end(ap); 84eda14cbcSMatt Macy } 85eda14cbcSMatt Macy 86eda14cbcSMatt Macy static void 87eda14cbcSMatt Macy zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt, 88eda14cbcSMatt Macy va_list ap) 89eda14cbcSMatt Macy { 90eda14cbcSMatt Macy char action[1024]; 91eda14cbcSMatt Macy 92eda14cbcSMatt Macy (void) vsnprintf(action, sizeof (action), fmt, ap); 93eda14cbcSMatt Macy 94eda14cbcSMatt Macy if (hdl->lpc_desc_active) 95eda14cbcSMatt Macy hdl->lpc_desc_active = B_FALSE; 96eda14cbcSMatt Macy else 97eda14cbcSMatt Macy hdl->lpc_desc[0] = '\0'; 98eda14cbcSMatt Macy 99eda14cbcSMatt Macy if (hdl->lpc_printerr) { 100eda14cbcSMatt Macy if (hdl->lpc_desc[0] != '\0') 101eda14cbcSMatt Macy error = hdl->lpc_desc; 102eda14cbcSMatt Macy 103eda14cbcSMatt Macy (void) fprintf(stderr, "%s: %s\n", action, error); 104eda14cbcSMatt Macy } 105eda14cbcSMatt Macy } 106eda14cbcSMatt Macy 107eda14cbcSMatt Macy /*PRINTFLIKE3*/ 108eda14cbcSMatt Macy static int 109eda14cbcSMatt Macy zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...) 110eda14cbcSMatt Macy { 111eda14cbcSMatt Macy va_list ap; 112eda14cbcSMatt Macy 113eda14cbcSMatt Macy va_start(ap, fmt); 114eda14cbcSMatt Macy 115eda14cbcSMatt Macy zutil_verror(hdl, error, fmt, ap); 116eda14cbcSMatt Macy 117eda14cbcSMatt Macy va_end(ap); 118eda14cbcSMatt Macy 119eda14cbcSMatt Macy return (-1); 120eda14cbcSMatt Macy } 121eda14cbcSMatt Macy 122eda14cbcSMatt Macy static int 123eda14cbcSMatt Macy zutil_error(libpc_handle_t *hdl, const char *error, const char *msg) 124eda14cbcSMatt Macy { 125eda14cbcSMatt Macy return (zutil_error_fmt(hdl, error, "%s", msg)); 126eda14cbcSMatt Macy } 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy static int 129eda14cbcSMatt Macy zutil_no_memory(libpc_handle_t *hdl) 130eda14cbcSMatt Macy { 131eda14cbcSMatt Macy zutil_error(hdl, EZFS_NOMEM, "internal error"); 132eda14cbcSMatt Macy exit(1); 133eda14cbcSMatt Macy } 134eda14cbcSMatt Macy 135eda14cbcSMatt Macy void * 136eda14cbcSMatt Macy zutil_alloc(libpc_handle_t *hdl, size_t size) 137eda14cbcSMatt Macy { 138eda14cbcSMatt Macy void *data; 139eda14cbcSMatt Macy 140eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL) 141eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 142eda14cbcSMatt Macy 143eda14cbcSMatt Macy return (data); 144eda14cbcSMatt Macy } 145eda14cbcSMatt Macy 146eda14cbcSMatt Macy char * 147eda14cbcSMatt Macy zutil_strdup(libpc_handle_t *hdl, const char *str) 148eda14cbcSMatt Macy { 149eda14cbcSMatt Macy char *ret; 150eda14cbcSMatt Macy 151eda14cbcSMatt Macy if ((ret = strdup(str)) == NULL) 152eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 153eda14cbcSMatt Macy 154eda14cbcSMatt Macy return (ret); 155eda14cbcSMatt Macy } 156eda14cbcSMatt Macy 157eda14cbcSMatt Macy /* 158eda14cbcSMatt Macy * Intermediate structures used to gather configuration information. 159eda14cbcSMatt Macy */ 160eda14cbcSMatt Macy typedef struct config_entry { 161eda14cbcSMatt Macy uint64_t ce_txg; 162eda14cbcSMatt Macy nvlist_t *ce_config; 163eda14cbcSMatt Macy struct config_entry *ce_next; 164eda14cbcSMatt Macy } config_entry_t; 165eda14cbcSMatt Macy 166eda14cbcSMatt Macy typedef struct vdev_entry { 167eda14cbcSMatt Macy uint64_t ve_guid; 168eda14cbcSMatt Macy config_entry_t *ve_configs; 169eda14cbcSMatt Macy struct vdev_entry *ve_next; 170eda14cbcSMatt Macy } vdev_entry_t; 171eda14cbcSMatt Macy 172eda14cbcSMatt Macy typedef struct pool_entry { 173eda14cbcSMatt Macy uint64_t pe_guid; 174eda14cbcSMatt Macy vdev_entry_t *pe_vdevs; 175eda14cbcSMatt Macy struct pool_entry *pe_next; 176eda14cbcSMatt Macy } pool_entry_t; 177eda14cbcSMatt Macy 178eda14cbcSMatt Macy typedef struct name_entry { 179eda14cbcSMatt Macy char *ne_name; 180eda14cbcSMatt Macy uint64_t ne_guid; 181eda14cbcSMatt Macy uint64_t ne_order; 182eda14cbcSMatt Macy uint64_t ne_num_labels; 183eda14cbcSMatt Macy struct name_entry *ne_next; 184eda14cbcSMatt Macy } name_entry_t; 185eda14cbcSMatt Macy 186eda14cbcSMatt Macy typedef struct pool_list { 187eda14cbcSMatt Macy pool_entry_t *pools; 188eda14cbcSMatt Macy name_entry_t *names; 189eda14cbcSMatt Macy } pool_list_t; 190eda14cbcSMatt Macy 191eda14cbcSMatt Macy /* 192eda14cbcSMatt Macy * Go through and fix up any path and/or devid information for the given vdev 193eda14cbcSMatt Macy * configuration. 194eda14cbcSMatt Macy */ 195eda14cbcSMatt Macy static int 196eda14cbcSMatt Macy fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names) 197eda14cbcSMatt Macy { 198eda14cbcSMatt Macy nvlist_t **child; 199eda14cbcSMatt Macy uint_t c, children; 200eda14cbcSMatt Macy uint64_t guid; 201eda14cbcSMatt Macy name_entry_t *ne, *best; 202eda14cbcSMatt Macy char *path; 203eda14cbcSMatt Macy 204eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 205eda14cbcSMatt Macy &child, &children) == 0) { 206eda14cbcSMatt Macy for (c = 0; c < children; c++) 207eda14cbcSMatt Macy if (fix_paths(hdl, child[c], names) != 0) 208eda14cbcSMatt Macy return (-1); 209eda14cbcSMatt Macy return (0); 210eda14cbcSMatt Macy } 211eda14cbcSMatt Macy 212eda14cbcSMatt Macy /* 213eda14cbcSMatt Macy * This is a leaf (file or disk) vdev. In either case, go through 214eda14cbcSMatt Macy * the name list and see if we find a matching guid. If so, replace 215eda14cbcSMatt Macy * the path and see if we can calculate a new devid. 216eda14cbcSMatt Macy * 217eda14cbcSMatt Macy * There may be multiple names associated with a particular guid, in 218eda14cbcSMatt Macy * which case we have overlapping partitions or multiple paths to the 219eda14cbcSMatt Macy * same disk. In this case we prefer to use the path name which 220eda14cbcSMatt Macy * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we 221eda14cbcSMatt Macy * use the lowest order device which corresponds to the first match 222eda14cbcSMatt Macy * while traversing the ZPOOL_IMPORT_PATH search path. 223eda14cbcSMatt Macy */ 224eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 225eda14cbcSMatt Macy if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 226eda14cbcSMatt Macy path = NULL; 227eda14cbcSMatt Macy 228eda14cbcSMatt Macy best = NULL; 229eda14cbcSMatt Macy for (ne = names; ne != NULL; ne = ne->ne_next) { 230eda14cbcSMatt Macy if (ne->ne_guid == guid) { 231eda14cbcSMatt Macy if (path == NULL) { 232eda14cbcSMatt Macy best = ne; 233eda14cbcSMatt Macy break; 234eda14cbcSMatt Macy } 235eda14cbcSMatt Macy 236eda14cbcSMatt Macy if ((strlen(path) == strlen(ne->ne_name)) && 237eda14cbcSMatt Macy strncmp(path, ne->ne_name, strlen(path)) == 0) { 238eda14cbcSMatt Macy best = ne; 239eda14cbcSMatt Macy break; 240eda14cbcSMatt Macy } 241eda14cbcSMatt Macy 242eda14cbcSMatt Macy if (best == NULL) { 243eda14cbcSMatt Macy best = ne; 244eda14cbcSMatt Macy continue; 245eda14cbcSMatt Macy } 246eda14cbcSMatt Macy 247eda14cbcSMatt Macy /* Prefer paths with move vdev labels. */ 248eda14cbcSMatt Macy if (ne->ne_num_labels > best->ne_num_labels) { 249eda14cbcSMatt Macy best = ne; 250eda14cbcSMatt Macy continue; 251eda14cbcSMatt Macy } 252eda14cbcSMatt Macy 253eda14cbcSMatt Macy /* Prefer paths earlier in the search order. */ 254eda14cbcSMatt Macy if (ne->ne_num_labels == best->ne_num_labels && 255eda14cbcSMatt Macy ne->ne_order < best->ne_order) { 256eda14cbcSMatt Macy best = ne; 257eda14cbcSMatt Macy continue; 258eda14cbcSMatt Macy } 259eda14cbcSMatt Macy } 260eda14cbcSMatt Macy } 261eda14cbcSMatt Macy 262eda14cbcSMatt Macy if (best == NULL) 263eda14cbcSMatt Macy return (0); 264eda14cbcSMatt Macy 265eda14cbcSMatt Macy if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 266eda14cbcSMatt Macy return (-1); 267eda14cbcSMatt Macy 268eda14cbcSMatt Macy update_vdev_config_dev_strs(nv); 269eda14cbcSMatt Macy 270eda14cbcSMatt Macy return (0); 271eda14cbcSMatt Macy } 272eda14cbcSMatt Macy 273eda14cbcSMatt Macy /* 274eda14cbcSMatt Macy * Add the given configuration to the list of known devices. 275eda14cbcSMatt Macy */ 276eda14cbcSMatt Macy static int 277eda14cbcSMatt Macy add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path, 278eda14cbcSMatt Macy int order, int num_labels, nvlist_t *config) 279eda14cbcSMatt Macy { 280eda14cbcSMatt Macy uint64_t pool_guid, vdev_guid, top_guid, txg, state; 281eda14cbcSMatt Macy pool_entry_t *pe; 282eda14cbcSMatt Macy vdev_entry_t *ve; 283eda14cbcSMatt Macy config_entry_t *ce; 284eda14cbcSMatt Macy name_entry_t *ne; 285eda14cbcSMatt Macy 286eda14cbcSMatt Macy /* 287eda14cbcSMatt Macy * If this is a hot spare not currently in use or level 2 cache 288eda14cbcSMatt Macy * device, add it to the list of names to translate, but don't do 289eda14cbcSMatt Macy * anything else. 290eda14cbcSMatt Macy */ 291eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 292eda14cbcSMatt Macy &state) == 0 && 293eda14cbcSMatt Macy (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 294eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 295eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 296eda14cbcSMatt Macy return (-1); 297eda14cbcSMatt Macy 298eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 299eda14cbcSMatt Macy free(ne); 300eda14cbcSMatt Macy return (-1); 301eda14cbcSMatt Macy } 302eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 303eda14cbcSMatt Macy ne->ne_order = order; 304eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 305eda14cbcSMatt Macy ne->ne_next = pl->names; 306eda14cbcSMatt Macy pl->names = ne; 307eda14cbcSMatt Macy 308eda14cbcSMatt Macy return (0); 309eda14cbcSMatt Macy } 310eda14cbcSMatt Macy 311eda14cbcSMatt Macy /* 312eda14cbcSMatt Macy * If we have a valid config but cannot read any of these fields, then 313eda14cbcSMatt Macy * it means we have a half-initialized label. In vdev_label_init() 314eda14cbcSMatt Macy * we write a label with txg == 0 so that we can identify the device 315eda14cbcSMatt Macy * in case the user refers to the same disk later on. If we fail to 316eda14cbcSMatt Macy * create the pool, we'll be left with a label in this state 317eda14cbcSMatt Macy * which should not be considered part of a valid pool. 318eda14cbcSMatt Macy */ 319eda14cbcSMatt Macy if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 320eda14cbcSMatt Macy &pool_guid) != 0 || 321eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 322eda14cbcSMatt Macy &vdev_guid) != 0 || 323eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 324eda14cbcSMatt Macy &top_guid) != 0 || 325eda14cbcSMatt Macy nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 326eda14cbcSMatt Macy &txg) != 0 || txg == 0) { 327eda14cbcSMatt Macy return (0); 328eda14cbcSMatt Macy } 329eda14cbcSMatt Macy 330eda14cbcSMatt Macy /* 331eda14cbcSMatt Macy * First, see if we know about this pool. If not, then add it to the 332eda14cbcSMatt Macy * list of known pools. 333eda14cbcSMatt Macy */ 334eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 335eda14cbcSMatt Macy if (pe->pe_guid == pool_guid) 336eda14cbcSMatt Macy break; 337eda14cbcSMatt Macy } 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy if (pe == NULL) { 340eda14cbcSMatt Macy if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 341eda14cbcSMatt Macy return (-1); 342eda14cbcSMatt Macy } 343eda14cbcSMatt Macy pe->pe_guid = pool_guid; 344eda14cbcSMatt Macy pe->pe_next = pl->pools; 345eda14cbcSMatt Macy pl->pools = pe; 346eda14cbcSMatt Macy } 347eda14cbcSMatt Macy 348eda14cbcSMatt Macy /* 349eda14cbcSMatt Macy * Second, see if we know about this toplevel vdev. Add it if its 350eda14cbcSMatt Macy * missing. 351eda14cbcSMatt Macy */ 352eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 353eda14cbcSMatt Macy if (ve->ve_guid == top_guid) 354eda14cbcSMatt Macy break; 355eda14cbcSMatt Macy } 356eda14cbcSMatt Macy 357eda14cbcSMatt Macy if (ve == NULL) { 358eda14cbcSMatt Macy if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 359eda14cbcSMatt Macy return (-1); 360eda14cbcSMatt Macy } 361eda14cbcSMatt Macy ve->ve_guid = top_guid; 362eda14cbcSMatt Macy ve->ve_next = pe->pe_vdevs; 363eda14cbcSMatt Macy pe->pe_vdevs = ve; 364eda14cbcSMatt Macy } 365eda14cbcSMatt Macy 366eda14cbcSMatt Macy /* 367eda14cbcSMatt Macy * Third, see if we have a config with a matching transaction group. If 368eda14cbcSMatt Macy * so, then we do nothing. Otherwise, add it to the list of known 369eda14cbcSMatt Macy * configs. 370eda14cbcSMatt Macy */ 371eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 372eda14cbcSMatt Macy if (ce->ce_txg == txg) 373eda14cbcSMatt Macy break; 374eda14cbcSMatt Macy } 375eda14cbcSMatt Macy 376eda14cbcSMatt Macy if (ce == NULL) { 377eda14cbcSMatt Macy if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) { 378eda14cbcSMatt Macy return (-1); 379eda14cbcSMatt Macy } 380eda14cbcSMatt Macy ce->ce_txg = txg; 381eda14cbcSMatt Macy ce->ce_config = fnvlist_dup(config); 382eda14cbcSMatt Macy ce->ce_next = ve->ve_configs; 383eda14cbcSMatt Macy ve->ve_configs = ce; 384eda14cbcSMatt Macy } 385eda14cbcSMatt Macy 386eda14cbcSMatt Macy /* 387eda14cbcSMatt Macy * At this point we've successfully added our config to the list of 388eda14cbcSMatt Macy * known configs. The last thing to do is add the vdev guid -> path 389eda14cbcSMatt Macy * mappings so that we can fix up the configuration as necessary before 390eda14cbcSMatt Macy * doing the import. 391eda14cbcSMatt Macy */ 392eda14cbcSMatt Macy if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 393eda14cbcSMatt Macy return (-1); 394eda14cbcSMatt Macy 395eda14cbcSMatt Macy if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 396eda14cbcSMatt Macy free(ne); 397eda14cbcSMatt Macy return (-1); 398eda14cbcSMatt Macy } 399eda14cbcSMatt Macy 400eda14cbcSMatt Macy ne->ne_guid = vdev_guid; 401eda14cbcSMatt Macy ne->ne_order = order; 402eda14cbcSMatt Macy ne->ne_num_labels = num_labels; 403eda14cbcSMatt Macy ne->ne_next = pl->names; 404eda14cbcSMatt Macy pl->names = ne; 405eda14cbcSMatt Macy 406eda14cbcSMatt Macy return (0); 407eda14cbcSMatt Macy } 408eda14cbcSMatt Macy 409eda14cbcSMatt Macy static int 410eda14cbcSMatt Macy zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid, 411eda14cbcSMatt Macy boolean_t *isactive) 412eda14cbcSMatt Macy { 413eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_pool_active != NULL); 414eda14cbcSMatt Macy 415eda14cbcSMatt Macy int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name, 416eda14cbcSMatt Macy guid, isactive); 417eda14cbcSMatt Macy 418eda14cbcSMatt Macy return (error); 419eda14cbcSMatt Macy } 420eda14cbcSMatt Macy 421eda14cbcSMatt Macy static nvlist_t * 422eda14cbcSMatt Macy zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig) 423eda14cbcSMatt Macy { 424eda14cbcSMatt Macy ASSERT(hdl->lpc_ops->pco_refresh_config != NULL); 425eda14cbcSMatt Macy 426eda14cbcSMatt Macy return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle, 427eda14cbcSMatt Macy tryconfig)); 428eda14cbcSMatt Macy } 429eda14cbcSMatt Macy 430eda14cbcSMatt Macy /* 431eda14cbcSMatt Macy * Determine if the vdev id is a hole in the namespace. 432eda14cbcSMatt Macy */ 433eda14cbcSMatt Macy static boolean_t 434eda14cbcSMatt Macy vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 435eda14cbcSMatt Macy { 436eda14cbcSMatt Macy int c; 437eda14cbcSMatt Macy 438eda14cbcSMatt Macy for (c = 0; c < holes; c++) { 439eda14cbcSMatt Macy 440eda14cbcSMatt Macy /* Top-level is a hole */ 441eda14cbcSMatt Macy if (hole_array[c] == id) 442eda14cbcSMatt Macy return (B_TRUE); 443eda14cbcSMatt Macy } 444eda14cbcSMatt Macy return (B_FALSE); 445eda14cbcSMatt Macy } 446eda14cbcSMatt Macy 447eda14cbcSMatt Macy /* 448eda14cbcSMatt Macy * Convert our list of pools into the definitive set of configurations. We 449eda14cbcSMatt Macy * start by picking the best config for each toplevel vdev. Once that's done, 450eda14cbcSMatt Macy * we assemble the toplevel vdevs into a full config for the pool. We make a 451eda14cbcSMatt Macy * pass to fix up any incorrect paths, and then add it to the main list to 452eda14cbcSMatt Macy * return to the user. 453eda14cbcSMatt Macy */ 454eda14cbcSMatt Macy static nvlist_t * 455eda14cbcSMatt Macy get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok, 456eda14cbcSMatt Macy nvlist_t *policy) 457eda14cbcSMatt Macy { 458eda14cbcSMatt Macy pool_entry_t *pe; 459eda14cbcSMatt Macy vdev_entry_t *ve; 460eda14cbcSMatt Macy config_entry_t *ce; 461eda14cbcSMatt Macy nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; 462eda14cbcSMatt Macy nvlist_t **spares, **l2cache; 463eda14cbcSMatt Macy uint_t i, nspares, nl2cache; 464eda14cbcSMatt Macy boolean_t config_seen; 465eda14cbcSMatt Macy uint64_t best_txg; 466eda14cbcSMatt Macy char *name, *hostname = NULL; 467eda14cbcSMatt Macy uint64_t guid; 468eda14cbcSMatt Macy uint_t children = 0; 469eda14cbcSMatt Macy nvlist_t **child = NULL; 470eda14cbcSMatt Macy uint_t holes; 471eda14cbcSMatt Macy uint64_t *hole_array, max_id; 472eda14cbcSMatt Macy uint_t c; 473eda14cbcSMatt Macy boolean_t isactive; 474eda14cbcSMatt Macy uint64_t hostid; 475eda14cbcSMatt Macy nvlist_t *nvl; 476eda14cbcSMatt Macy boolean_t valid_top_config = B_FALSE; 477eda14cbcSMatt Macy 478eda14cbcSMatt Macy if (nvlist_alloc(&ret, 0, 0) != 0) 479eda14cbcSMatt Macy goto nomem; 480eda14cbcSMatt Macy 481eda14cbcSMatt Macy for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 482eda14cbcSMatt Macy uint64_t id, max_txg = 0; 483eda14cbcSMatt Macy 484eda14cbcSMatt Macy if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 485eda14cbcSMatt Macy goto nomem; 486eda14cbcSMatt Macy config_seen = B_FALSE; 487eda14cbcSMatt Macy 488eda14cbcSMatt Macy /* 489eda14cbcSMatt Macy * Iterate over all toplevel vdevs. Grab the pool configuration 490eda14cbcSMatt Macy * from the first one we find, and then go through the rest and 491eda14cbcSMatt Macy * add them as necessary to the 'vdevs' member of the config. 492eda14cbcSMatt Macy */ 493eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 494eda14cbcSMatt Macy 495eda14cbcSMatt Macy /* 496eda14cbcSMatt Macy * Determine the best configuration for this vdev by 497eda14cbcSMatt Macy * selecting the config with the latest transaction 498eda14cbcSMatt Macy * group. 499eda14cbcSMatt Macy */ 500eda14cbcSMatt Macy best_txg = 0; 501eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; 502eda14cbcSMatt Macy ce = ce->ce_next) { 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy if (ce->ce_txg > best_txg) { 505eda14cbcSMatt Macy tmp = ce->ce_config; 506eda14cbcSMatt Macy best_txg = ce->ce_txg; 507eda14cbcSMatt Macy } 508eda14cbcSMatt Macy } 509eda14cbcSMatt Macy 510eda14cbcSMatt Macy /* 511eda14cbcSMatt Macy * We rely on the fact that the max txg for the 512eda14cbcSMatt Macy * pool will contain the most up-to-date information 513eda14cbcSMatt Macy * about the valid top-levels in the vdev namespace. 514eda14cbcSMatt Macy */ 515eda14cbcSMatt Macy if (best_txg > max_txg) { 516eda14cbcSMatt Macy (void) nvlist_remove(config, 517eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 518eda14cbcSMatt Macy DATA_TYPE_UINT64); 519eda14cbcSMatt Macy (void) nvlist_remove(config, 520eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 521eda14cbcSMatt Macy DATA_TYPE_UINT64_ARRAY); 522eda14cbcSMatt Macy 523eda14cbcSMatt Macy max_txg = best_txg; 524eda14cbcSMatt Macy hole_array = NULL; 525eda14cbcSMatt Macy holes = 0; 526eda14cbcSMatt Macy max_id = 0; 527eda14cbcSMatt Macy valid_top_config = B_FALSE; 528eda14cbcSMatt Macy 529eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 530eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 531eda14cbcSMatt Macy verify(nvlist_add_uint64(config, 532eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_CHILDREN, 533eda14cbcSMatt Macy max_id) == 0); 534eda14cbcSMatt Macy valid_top_config = B_TRUE; 535eda14cbcSMatt Macy } 536eda14cbcSMatt Macy 537eda14cbcSMatt Macy if (nvlist_lookup_uint64_array(tmp, 538eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 539eda14cbcSMatt Macy &holes) == 0) { 540eda14cbcSMatt Macy verify(nvlist_add_uint64_array(config, 541eda14cbcSMatt Macy ZPOOL_CONFIG_HOLE_ARRAY, 542eda14cbcSMatt Macy hole_array, holes) == 0); 543eda14cbcSMatt Macy } 544eda14cbcSMatt Macy } 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy if (!config_seen) { 547eda14cbcSMatt Macy /* 548eda14cbcSMatt Macy * Copy the relevant pieces of data to the pool 549eda14cbcSMatt Macy * configuration: 550eda14cbcSMatt Macy * 551eda14cbcSMatt Macy * version 552eda14cbcSMatt Macy * pool guid 553eda14cbcSMatt Macy * name 554eda14cbcSMatt Macy * comment (if available) 555ee36e25aSMartin Matuska * compatibility features (if available) 556eda14cbcSMatt Macy * pool state 557eda14cbcSMatt Macy * hostid (if available) 558eda14cbcSMatt Macy * hostname (if available) 559eda14cbcSMatt Macy */ 560eda14cbcSMatt Macy uint64_t state, version; 561eda14cbcSMatt Macy char *comment = NULL; 562ee36e25aSMartin Matuska char *compatibility = NULL; 563eda14cbcSMatt Macy 564eda14cbcSMatt Macy version = fnvlist_lookup_uint64(tmp, 565eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION); 566eda14cbcSMatt Macy fnvlist_add_uint64(config, 567eda14cbcSMatt Macy ZPOOL_CONFIG_VERSION, version); 568eda14cbcSMatt Macy guid = fnvlist_lookup_uint64(tmp, 569eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID); 570eda14cbcSMatt Macy fnvlist_add_uint64(config, 571eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, guid); 572eda14cbcSMatt Macy name = fnvlist_lookup_string(tmp, 573eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME); 574eda14cbcSMatt Macy fnvlist_add_string(config, 575eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, name); 576eda14cbcSMatt Macy 577eda14cbcSMatt Macy if (nvlist_lookup_string(tmp, 578eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, &comment) == 0) 579eda14cbcSMatt Macy fnvlist_add_string(config, 580eda14cbcSMatt Macy ZPOOL_CONFIG_COMMENT, comment); 581eda14cbcSMatt Macy 582ee36e25aSMartin Matuska if (nvlist_lookup_string(tmp, 583ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 584ee36e25aSMartin Matuska &compatibility) == 0) 585ee36e25aSMartin Matuska fnvlist_add_string(config, 586ee36e25aSMartin Matuska ZPOOL_CONFIG_COMPATIBILITY, 587ee36e25aSMartin Matuska compatibility); 588ee36e25aSMartin Matuska 589eda14cbcSMatt Macy state = fnvlist_lookup_uint64(tmp, 590eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE); 591eda14cbcSMatt Macy fnvlist_add_uint64(config, 592eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, state); 593eda14cbcSMatt Macy 594eda14cbcSMatt Macy hostid = 0; 595eda14cbcSMatt Macy if (nvlist_lookup_uint64(tmp, 596eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 597eda14cbcSMatt Macy fnvlist_add_uint64(config, 598eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTID, hostid); 599eda14cbcSMatt Macy hostname = fnvlist_lookup_string(tmp, 600eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME); 601eda14cbcSMatt Macy fnvlist_add_string(config, 602eda14cbcSMatt Macy ZPOOL_CONFIG_HOSTNAME, hostname); 603eda14cbcSMatt Macy } 604eda14cbcSMatt Macy 605eda14cbcSMatt Macy config_seen = B_TRUE; 606eda14cbcSMatt Macy } 607eda14cbcSMatt Macy 608eda14cbcSMatt Macy /* 609eda14cbcSMatt Macy * Add this top-level vdev to the child array. 610eda14cbcSMatt Macy */ 611eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(tmp, 612eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 613eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 614eda14cbcSMatt Macy &id) == 0); 615eda14cbcSMatt Macy 616eda14cbcSMatt Macy if (id >= children) { 617eda14cbcSMatt Macy nvlist_t **newchild; 618eda14cbcSMatt Macy 619eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (id + 1) * 620eda14cbcSMatt Macy sizeof (nvlist_t *)); 621eda14cbcSMatt Macy if (newchild == NULL) 622eda14cbcSMatt Macy goto nomem; 623eda14cbcSMatt Macy 624eda14cbcSMatt Macy for (c = 0; c < children; c++) 625eda14cbcSMatt Macy newchild[c] = child[c]; 626eda14cbcSMatt Macy 627eda14cbcSMatt Macy free(child); 628eda14cbcSMatt Macy child = newchild; 629eda14cbcSMatt Macy children = id + 1; 630eda14cbcSMatt Macy } 631eda14cbcSMatt Macy if (nvlist_dup(nvtop, &child[id], 0) != 0) 632eda14cbcSMatt Macy goto nomem; 633eda14cbcSMatt Macy 634eda14cbcSMatt Macy } 635eda14cbcSMatt Macy 636eda14cbcSMatt Macy /* 637eda14cbcSMatt Macy * If we have information about all the top-levels then 638eda14cbcSMatt Macy * clean up the nvlist which we've constructed. This 639eda14cbcSMatt Macy * means removing any extraneous devices that are 640eda14cbcSMatt Macy * beyond the valid range or adding devices to the end 641eda14cbcSMatt Macy * of our array which appear to be missing. 642eda14cbcSMatt Macy */ 643eda14cbcSMatt Macy if (valid_top_config) { 644eda14cbcSMatt Macy if (max_id < children) { 645eda14cbcSMatt Macy for (c = max_id; c < children; c++) 646eda14cbcSMatt Macy nvlist_free(child[c]); 647eda14cbcSMatt Macy children = max_id; 648eda14cbcSMatt Macy } else if (max_id > children) { 649eda14cbcSMatt Macy nvlist_t **newchild; 650eda14cbcSMatt Macy 651eda14cbcSMatt Macy newchild = zutil_alloc(hdl, (max_id) * 652eda14cbcSMatt Macy sizeof (nvlist_t *)); 653eda14cbcSMatt Macy if (newchild == NULL) 654eda14cbcSMatt Macy goto nomem; 655eda14cbcSMatt Macy 656eda14cbcSMatt Macy for (c = 0; c < children; c++) 657eda14cbcSMatt Macy newchild[c] = child[c]; 658eda14cbcSMatt Macy 659eda14cbcSMatt Macy free(child); 660eda14cbcSMatt Macy child = newchild; 661eda14cbcSMatt Macy children = max_id; 662eda14cbcSMatt Macy } 663eda14cbcSMatt Macy } 664eda14cbcSMatt Macy 665eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 666eda14cbcSMatt Macy &guid) == 0); 667eda14cbcSMatt Macy 668eda14cbcSMatt Macy /* 669eda14cbcSMatt Macy * The vdev namespace may contain holes as a result of 670eda14cbcSMatt Macy * device removal. We must add them back into the vdev 671eda14cbcSMatt Macy * tree before we process any missing devices. 672eda14cbcSMatt Macy */ 673eda14cbcSMatt Macy if (holes > 0) { 674eda14cbcSMatt Macy ASSERT(valid_top_config); 675eda14cbcSMatt Macy 676eda14cbcSMatt Macy for (c = 0; c < children; c++) { 677eda14cbcSMatt Macy nvlist_t *holey; 678eda14cbcSMatt Macy 679eda14cbcSMatt Macy if (child[c] != NULL || 680eda14cbcSMatt Macy !vdev_is_hole(hole_array, holes, c)) 681eda14cbcSMatt Macy continue; 682eda14cbcSMatt Macy 683eda14cbcSMatt Macy if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 684eda14cbcSMatt Macy 0) != 0) 685eda14cbcSMatt Macy goto nomem; 686eda14cbcSMatt Macy 687eda14cbcSMatt Macy /* 688eda14cbcSMatt Macy * Holes in the namespace are treated as 689eda14cbcSMatt Macy * "hole" top-level vdevs and have a 690eda14cbcSMatt Macy * special flag set on them. 691eda14cbcSMatt Macy */ 692eda14cbcSMatt Macy if (nvlist_add_string(holey, 693eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 694eda14cbcSMatt Macy VDEV_TYPE_HOLE) != 0 || 695eda14cbcSMatt Macy nvlist_add_uint64(holey, 696eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 697eda14cbcSMatt Macy nvlist_add_uint64(holey, 698eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 699eda14cbcSMatt Macy nvlist_free(holey); 700eda14cbcSMatt Macy goto nomem; 701eda14cbcSMatt Macy } 702eda14cbcSMatt Macy child[c] = holey; 703eda14cbcSMatt Macy } 704eda14cbcSMatt Macy } 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy /* 707eda14cbcSMatt Macy * Look for any missing top-level vdevs. If this is the case, 708eda14cbcSMatt Macy * create a faked up 'missing' vdev as a placeholder. We cannot 709eda14cbcSMatt Macy * simply compress the child array, because the kernel performs 710eda14cbcSMatt Macy * certain checks to make sure the vdev IDs match their location 711eda14cbcSMatt Macy * in the configuration. 712eda14cbcSMatt Macy */ 713eda14cbcSMatt Macy for (c = 0; c < children; c++) { 714eda14cbcSMatt Macy if (child[c] == NULL) { 715eda14cbcSMatt Macy nvlist_t *missing; 716eda14cbcSMatt Macy if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 717eda14cbcSMatt Macy 0) != 0) 718eda14cbcSMatt Macy goto nomem; 719eda14cbcSMatt Macy if (nvlist_add_string(missing, 720eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, 721eda14cbcSMatt Macy VDEV_TYPE_MISSING) != 0 || 722eda14cbcSMatt Macy nvlist_add_uint64(missing, 723eda14cbcSMatt Macy ZPOOL_CONFIG_ID, c) != 0 || 724eda14cbcSMatt Macy nvlist_add_uint64(missing, 725eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, 0ULL) != 0) { 726eda14cbcSMatt Macy nvlist_free(missing); 727eda14cbcSMatt Macy goto nomem; 728eda14cbcSMatt Macy } 729eda14cbcSMatt Macy child[c] = missing; 730eda14cbcSMatt Macy } 731eda14cbcSMatt Macy } 732eda14cbcSMatt Macy 733eda14cbcSMatt Macy /* 734eda14cbcSMatt Macy * Put all of this pool's top-level vdevs into a root vdev. 735eda14cbcSMatt Macy */ 736eda14cbcSMatt Macy if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 737eda14cbcSMatt Macy goto nomem; 738eda14cbcSMatt Macy if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 739eda14cbcSMatt Macy VDEV_TYPE_ROOT) != 0 || 740eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 741eda14cbcSMatt Macy nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 742eda14cbcSMatt Macy nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 743eda14cbcSMatt Macy child, children) != 0) { 744eda14cbcSMatt Macy nvlist_free(nvroot); 745eda14cbcSMatt Macy goto nomem; 746eda14cbcSMatt Macy } 747eda14cbcSMatt Macy 748eda14cbcSMatt Macy for (c = 0; c < children; c++) 749eda14cbcSMatt Macy nvlist_free(child[c]); 750eda14cbcSMatt Macy free(child); 751eda14cbcSMatt Macy children = 0; 752eda14cbcSMatt Macy child = NULL; 753eda14cbcSMatt Macy 754eda14cbcSMatt Macy /* 755eda14cbcSMatt Macy * Go through and fix up any paths and/or devids based on our 756eda14cbcSMatt Macy * known list of vdev GUID -> path mappings. 757eda14cbcSMatt Macy */ 758eda14cbcSMatt Macy if (fix_paths(hdl, nvroot, pl->names) != 0) { 759eda14cbcSMatt Macy nvlist_free(nvroot); 760eda14cbcSMatt Macy goto nomem; 761eda14cbcSMatt Macy } 762eda14cbcSMatt Macy 763eda14cbcSMatt Macy /* 764eda14cbcSMatt Macy * Add the root vdev to this pool's configuration. 765eda14cbcSMatt Macy */ 766eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 767eda14cbcSMatt Macy nvroot) != 0) { 768eda14cbcSMatt Macy nvlist_free(nvroot); 769eda14cbcSMatt Macy goto nomem; 770eda14cbcSMatt Macy } 771eda14cbcSMatt Macy nvlist_free(nvroot); 772eda14cbcSMatt Macy 773eda14cbcSMatt Macy /* 774eda14cbcSMatt Macy * zdb uses this path to report on active pools that were 775eda14cbcSMatt Macy * imported or created using -R. 776eda14cbcSMatt Macy */ 777eda14cbcSMatt Macy if (active_ok) 778eda14cbcSMatt Macy goto add_pool; 779eda14cbcSMatt Macy 780eda14cbcSMatt Macy /* 781eda14cbcSMatt Macy * Determine if this pool is currently active, in which case we 782eda14cbcSMatt Macy * can't actually import it. 783eda14cbcSMatt Macy */ 784eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 785eda14cbcSMatt Macy &name) == 0); 786eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 787eda14cbcSMatt Macy &guid) == 0); 788eda14cbcSMatt Macy 789eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, guid, &isactive) != 0) 790eda14cbcSMatt Macy goto error; 791eda14cbcSMatt Macy 792eda14cbcSMatt Macy if (isactive) { 793eda14cbcSMatt Macy nvlist_free(config); 794eda14cbcSMatt Macy config = NULL; 795eda14cbcSMatt Macy continue; 796eda14cbcSMatt Macy } 797eda14cbcSMatt Macy 798eda14cbcSMatt Macy if (policy != NULL) { 799eda14cbcSMatt Macy if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 800eda14cbcSMatt Macy policy) != 0) 801eda14cbcSMatt Macy goto nomem; 802eda14cbcSMatt Macy } 803eda14cbcSMatt Macy 804eda14cbcSMatt Macy if ((nvl = zutil_refresh_config(hdl, config)) == NULL) { 805eda14cbcSMatt Macy nvlist_free(config); 806eda14cbcSMatt Macy config = NULL; 807eda14cbcSMatt Macy continue; 808eda14cbcSMatt Macy } 809eda14cbcSMatt Macy 810eda14cbcSMatt Macy nvlist_free(config); 811eda14cbcSMatt Macy config = nvl; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy /* 814eda14cbcSMatt Macy * Go through and update the paths for spares, now that we have 815eda14cbcSMatt Macy * them. 816eda14cbcSMatt Macy */ 817eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 818eda14cbcSMatt Macy &nvroot) == 0); 819eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 820eda14cbcSMatt Macy &spares, &nspares) == 0) { 821eda14cbcSMatt Macy for (i = 0; i < nspares; i++) { 822eda14cbcSMatt Macy if (fix_paths(hdl, spares[i], pl->names) != 0) 823eda14cbcSMatt Macy goto nomem; 824eda14cbcSMatt Macy } 825eda14cbcSMatt Macy } 826eda14cbcSMatt Macy 827eda14cbcSMatt Macy /* 828eda14cbcSMatt Macy * Update the paths for l2cache devices. 829eda14cbcSMatt Macy */ 830eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 831eda14cbcSMatt Macy &l2cache, &nl2cache) == 0) { 832eda14cbcSMatt Macy for (i = 0; i < nl2cache; i++) { 833eda14cbcSMatt Macy if (fix_paths(hdl, l2cache[i], pl->names) != 0) 834eda14cbcSMatt Macy goto nomem; 835eda14cbcSMatt Macy } 836eda14cbcSMatt Macy } 837eda14cbcSMatt Macy 838eda14cbcSMatt Macy /* 839eda14cbcSMatt Macy * Restore the original information read from the actual label. 840eda14cbcSMatt Macy */ 841eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 842eda14cbcSMatt Macy DATA_TYPE_UINT64); 843eda14cbcSMatt Macy (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 844eda14cbcSMatt Macy DATA_TYPE_STRING); 845eda14cbcSMatt Macy if (hostid != 0) { 846eda14cbcSMatt Macy verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 847eda14cbcSMatt Macy hostid) == 0); 848eda14cbcSMatt Macy verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 849eda14cbcSMatt Macy hostname) == 0); 850eda14cbcSMatt Macy } 851eda14cbcSMatt Macy 852eda14cbcSMatt Macy add_pool: 853eda14cbcSMatt Macy /* 854eda14cbcSMatt Macy * Add this pool to the list of configs. 855eda14cbcSMatt Macy */ 856eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 857eda14cbcSMatt Macy &name) == 0); 858eda14cbcSMatt Macy 859eda14cbcSMatt Macy if (nvlist_add_nvlist(ret, name, config) != 0) 860eda14cbcSMatt Macy goto nomem; 861eda14cbcSMatt Macy 862eda14cbcSMatt Macy nvlist_free(config); 863eda14cbcSMatt Macy config = NULL; 864eda14cbcSMatt Macy } 865eda14cbcSMatt Macy 866eda14cbcSMatt Macy return (ret); 867eda14cbcSMatt Macy 868eda14cbcSMatt Macy nomem: 869eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 870eda14cbcSMatt Macy error: 871eda14cbcSMatt Macy nvlist_free(config); 872eda14cbcSMatt Macy nvlist_free(ret); 873eda14cbcSMatt Macy for (c = 0; c < children; c++) 874eda14cbcSMatt Macy nvlist_free(child[c]); 875eda14cbcSMatt Macy free(child); 876eda14cbcSMatt Macy 877eda14cbcSMatt Macy return (NULL); 878eda14cbcSMatt Macy } 879eda14cbcSMatt Macy 880eda14cbcSMatt Macy /* 881eda14cbcSMatt Macy * Return the offset of the given label. 882eda14cbcSMatt Macy */ 883eda14cbcSMatt Macy static uint64_t 884eda14cbcSMatt Macy label_offset(uint64_t size, int l) 885eda14cbcSMatt Macy { 886eda14cbcSMatt Macy ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 887eda14cbcSMatt Macy return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 888eda14cbcSMatt Macy 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 889eda14cbcSMatt Macy } 890eda14cbcSMatt Macy 891eda14cbcSMatt Macy /* 892*16038816SMartin Matuska * The same description applies as to zpool_read_label below, 893*16038816SMartin Matuska * except here we do it without aio, presumably because an aio call 894*16038816SMartin Matuska * errored out in a way we think not using it could circumvent. 895*16038816SMartin Matuska */ 896*16038816SMartin Matuska static int 897*16038816SMartin Matuska zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels) 898*16038816SMartin Matuska { 899*16038816SMartin Matuska struct stat64 statbuf; 900*16038816SMartin Matuska int l, count = 0; 901*16038816SMartin Matuska vdev_phys_t *label; 902*16038816SMartin Matuska nvlist_t *expected_config = NULL; 903*16038816SMartin Matuska uint64_t expected_guid = 0, size; 904*16038816SMartin Matuska int error; 905*16038816SMartin Matuska 906*16038816SMartin Matuska *config = NULL; 907*16038816SMartin Matuska 908*16038816SMartin Matuska if (fstat64_blk(fd, &statbuf) == -1) 909*16038816SMartin Matuska return (0); 910*16038816SMartin Matuska size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 911*16038816SMartin Matuska 912*16038816SMartin Matuska error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label)); 913*16038816SMartin Matuska if (error) 914*16038816SMartin Matuska return (-1); 915*16038816SMartin Matuska 916*16038816SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 917*16038816SMartin Matuska uint64_t state, guid, txg; 918*16038816SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 919*16038816SMartin Matuska 920*16038816SMartin Matuska if (pread64(fd, label, sizeof (vdev_phys_t), 921*16038816SMartin Matuska offset) != sizeof (vdev_phys_t)) 922*16038816SMartin Matuska continue; 923*16038816SMartin Matuska 924*16038816SMartin Matuska if (nvlist_unpack(label->vp_nvlist, 925*16038816SMartin Matuska sizeof (label->vp_nvlist), config, 0) != 0) 926*16038816SMartin Matuska continue; 927*16038816SMartin Matuska 928*16038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 929*16038816SMartin Matuska &guid) != 0 || guid == 0) { 930*16038816SMartin Matuska nvlist_free(*config); 931*16038816SMartin Matuska continue; 932*16038816SMartin Matuska } 933*16038816SMartin Matuska 934*16038816SMartin Matuska if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 935*16038816SMartin Matuska &state) != 0 || state > POOL_STATE_L2CACHE) { 936*16038816SMartin Matuska nvlist_free(*config); 937*16038816SMartin Matuska continue; 938*16038816SMartin Matuska } 939*16038816SMartin Matuska 940*16038816SMartin Matuska if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 941*16038816SMartin Matuska (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 942*16038816SMartin Matuska &txg) != 0 || txg == 0)) { 943*16038816SMartin Matuska nvlist_free(*config); 944*16038816SMartin Matuska continue; 945*16038816SMartin Matuska } 946*16038816SMartin Matuska 947*16038816SMartin Matuska if (expected_guid) { 948*16038816SMartin Matuska if (expected_guid == guid) 949*16038816SMartin Matuska count++; 950*16038816SMartin Matuska 951*16038816SMartin Matuska nvlist_free(*config); 952*16038816SMartin Matuska } else { 953*16038816SMartin Matuska expected_config = *config; 954*16038816SMartin Matuska expected_guid = guid; 955*16038816SMartin Matuska count++; 956*16038816SMartin Matuska } 957*16038816SMartin Matuska } 958*16038816SMartin Matuska 959*16038816SMartin Matuska if (num_labels != NULL) 960*16038816SMartin Matuska *num_labels = count; 961*16038816SMartin Matuska 962*16038816SMartin Matuska free(label); 963*16038816SMartin Matuska *config = expected_config; 964*16038816SMartin Matuska 965*16038816SMartin Matuska return (0); 966*16038816SMartin Matuska } 967*16038816SMartin Matuska 968*16038816SMartin Matuska /* 969eda14cbcSMatt Macy * Given a file descriptor, read the label information and return an nvlist 970eda14cbcSMatt Macy * describing the configuration, if there is one. The number of valid 971eda14cbcSMatt Macy * labels found will be returned in num_labels when non-NULL. 972eda14cbcSMatt Macy */ 973eda14cbcSMatt Macy int 974eda14cbcSMatt Macy zpool_read_label(int fd, nvlist_t **config, int *num_labels) 975eda14cbcSMatt Macy { 976eda14cbcSMatt Macy struct stat64 statbuf; 977184c1b94SMartin Matuska struct aiocb aiocbs[VDEV_LABELS]; 978184c1b94SMartin Matuska struct aiocb *aiocbps[VDEV_LABELS]; 979184c1b94SMartin Matuska vdev_phys_t *labels; 980eda14cbcSMatt Macy nvlist_t *expected_config = NULL; 981eda14cbcSMatt Macy uint64_t expected_guid = 0, size; 982184c1b94SMartin Matuska int error, l, count = 0; 983eda14cbcSMatt Macy 984eda14cbcSMatt Macy *config = NULL; 985eda14cbcSMatt Macy 986eda14cbcSMatt Macy if (fstat64_blk(fd, &statbuf) == -1) 987eda14cbcSMatt Macy return (0); 988eda14cbcSMatt Macy size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 989eda14cbcSMatt Macy 990184c1b94SMartin Matuska error = posix_memalign((void **)&labels, PAGESIZE, 991184c1b94SMartin Matuska VDEV_LABELS * sizeof (*labels)); 992eda14cbcSMatt Macy if (error) 993eda14cbcSMatt Macy return (-1); 994eda14cbcSMatt Macy 995184c1b94SMartin Matuska memset(aiocbs, 0, sizeof (aiocbs)); 996184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 997184c1b94SMartin Matuska off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 998184c1b94SMartin Matuska 999184c1b94SMartin Matuska aiocbs[l].aio_fildes = fd; 1000184c1b94SMartin Matuska aiocbs[l].aio_offset = offset; 1001184c1b94SMartin Matuska aiocbs[l].aio_buf = &labels[l]; 1002184c1b94SMartin Matuska aiocbs[l].aio_nbytes = sizeof (vdev_phys_t); 1003184c1b94SMartin Matuska aiocbs[l].aio_lio_opcode = LIO_READ; 1004184c1b94SMartin Matuska aiocbps[l] = &aiocbs[l]; 1005184c1b94SMartin Matuska } 1006184c1b94SMartin Matuska 1007184c1b94SMartin Matuska if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) { 1008184c1b94SMartin Matuska int saved_errno = errno; 1009*16038816SMartin Matuska boolean_t do_slow = B_FALSE; 1010*16038816SMartin Matuska error = -1; 1011184c1b94SMartin Matuska 1012184c1b94SMartin Matuska if (errno == EAGAIN || errno == EINTR || errno == EIO) { 1013184c1b94SMartin Matuska /* 1014184c1b94SMartin Matuska * A portion of the requests may have been submitted. 1015184c1b94SMartin Matuska * Clean them up. 1016184c1b94SMartin Matuska */ 1017184c1b94SMartin Matuska for (l = 0; l < VDEV_LABELS; l++) { 1018184c1b94SMartin Matuska errno = 0; 1019*16038816SMartin Matuska switch (aio_error(&aiocbs[l])) { 1020*16038816SMartin Matuska case EINVAL: 1021*16038816SMartin Matuska break; 1022*16038816SMartin Matuska case EINPROGRESS: 1023*16038816SMartin Matuska // This shouldn't be possible to 1024*16038816SMartin Matuska // encounter, die if we do. 1025*16038816SMartin Matuska ASSERT(B_FALSE); 1026*16038816SMartin Matuska case EOPNOTSUPP: 1027*16038816SMartin Matuska case ENOSYS: 1028*16038816SMartin Matuska do_slow = B_TRUE; 1029*16038816SMartin Matuska case 0: 1030*16038816SMartin Matuska default: 1031184c1b94SMartin Matuska (void) aio_return(&aiocbs[l]); 1032184c1b94SMartin Matuska } 1033184c1b94SMartin Matuska } 1034*16038816SMartin Matuska } 1035*16038816SMartin Matuska if (do_slow) { 1036*16038816SMartin Matuska /* 1037*16038816SMartin Matuska * At least some IO involved access unsafe-for-AIO 1038*16038816SMartin Matuska * files. Let's try again, without AIO this time. 1039*16038816SMartin Matuska */ 1040*16038816SMartin Matuska error = zpool_read_label_slow(fd, config, num_labels); 1041*16038816SMartin Matuska saved_errno = errno; 1042*16038816SMartin Matuska } 1043184c1b94SMartin Matuska free(labels); 1044184c1b94SMartin Matuska errno = saved_errno; 1045*16038816SMartin Matuska return (error); 1046184c1b94SMartin Matuska } 1047184c1b94SMartin Matuska 1048eda14cbcSMatt Macy for (l = 0; l < VDEV_LABELS; l++) { 1049eda14cbcSMatt Macy uint64_t state, guid, txg; 1050eda14cbcSMatt Macy 1051184c1b94SMartin Matuska if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t)) 1052eda14cbcSMatt Macy continue; 1053eda14cbcSMatt Macy 1054184c1b94SMartin Matuska if (nvlist_unpack(labels[l].vp_nvlist, 1055184c1b94SMartin Matuska sizeof (labels[l].vp_nvlist), config, 0) != 0) 1056eda14cbcSMatt Macy continue; 1057eda14cbcSMatt Macy 1058eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 1059eda14cbcSMatt Macy &guid) != 0 || guid == 0) { 1060eda14cbcSMatt Macy nvlist_free(*config); 1061eda14cbcSMatt Macy continue; 1062eda14cbcSMatt Macy } 1063eda14cbcSMatt Macy 1064eda14cbcSMatt Macy if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 1065eda14cbcSMatt Macy &state) != 0 || state > POOL_STATE_L2CACHE) { 1066eda14cbcSMatt Macy nvlist_free(*config); 1067eda14cbcSMatt Macy continue; 1068eda14cbcSMatt Macy } 1069eda14cbcSMatt Macy 1070eda14cbcSMatt Macy if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 1071eda14cbcSMatt Macy (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 1072eda14cbcSMatt Macy &txg) != 0 || txg == 0)) { 1073eda14cbcSMatt Macy nvlist_free(*config); 1074eda14cbcSMatt Macy continue; 1075eda14cbcSMatt Macy } 1076eda14cbcSMatt Macy 1077eda14cbcSMatt Macy if (expected_guid) { 1078eda14cbcSMatt Macy if (expected_guid == guid) 1079eda14cbcSMatt Macy count++; 1080eda14cbcSMatt Macy 1081eda14cbcSMatt Macy nvlist_free(*config); 1082eda14cbcSMatt Macy } else { 1083eda14cbcSMatt Macy expected_config = *config; 1084eda14cbcSMatt Macy expected_guid = guid; 1085eda14cbcSMatt Macy count++; 1086eda14cbcSMatt Macy } 1087eda14cbcSMatt Macy } 1088eda14cbcSMatt Macy 1089eda14cbcSMatt Macy if (num_labels != NULL) 1090eda14cbcSMatt Macy *num_labels = count; 1091eda14cbcSMatt Macy 1092184c1b94SMartin Matuska free(labels); 1093eda14cbcSMatt Macy *config = expected_config; 1094eda14cbcSMatt Macy 1095eda14cbcSMatt Macy return (0); 1096eda14cbcSMatt Macy } 1097eda14cbcSMatt Macy 1098eda14cbcSMatt Macy /* 1099eda14cbcSMatt Macy * Sorted by full path and then vdev guid to allow for multiple entries with 1100eda14cbcSMatt Macy * the same full path name. This is required because it's possible to 1101eda14cbcSMatt Macy * have multiple block devices with labels that refer to the same 1102eda14cbcSMatt Macy * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both 1103eda14cbcSMatt Macy * entries need to be added to the cache. Scenarios where this can occur 1104eda14cbcSMatt Macy * include overwritten pool labels, devices which are visible from multiple 1105eda14cbcSMatt Macy * hosts and multipath devices. 1106eda14cbcSMatt Macy */ 1107eda14cbcSMatt Macy int 1108eda14cbcSMatt Macy slice_cache_compare(const void *arg1, const void *arg2) 1109eda14cbcSMatt Macy { 1110eda14cbcSMatt Macy const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; 1111eda14cbcSMatt Macy const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; 1112eda14cbcSMatt Macy uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid; 1113eda14cbcSMatt Macy uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid; 1114eda14cbcSMatt Macy int rv; 1115eda14cbcSMatt Macy 1116eda14cbcSMatt Macy rv = TREE_ISIGN(strcmp(nm1, nm2)); 1117eda14cbcSMatt Macy if (rv) 1118eda14cbcSMatt Macy return (rv); 1119eda14cbcSMatt Macy 1120eda14cbcSMatt Macy return (TREE_CMP(guid1, guid2)); 1121eda14cbcSMatt Macy } 1122eda14cbcSMatt Macy 1123eda14cbcSMatt Macy static int 1124eda14cbcSMatt Macy label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid, 1125eda14cbcSMatt Macy uint64_t vdev_guid, char **path, char **devid) 1126eda14cbcSMatt Macy { 1127eda14cbcSMatt Macy nvlist_t **child; 1128eda14cbcSMatt Macy uint_t c, children; 1129eda14cbcSMatt Macy uint64_t guid; 1130eda14cbcSMatt Macy char *val; 1131eda14cbcSMatt Macy int error; 1132eda14cbcSMatt Macy 1133eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1134eda14cbcSMatt Macy &child, &children) == 0) { 1135eda14cbcSMatt Macy for (c = 0; c < children; c++) { 1136eda14cbcSMatt Macy error = label_paths_impl(hdl, child[c], 1137eda14cbcSMatt Macy pool_guid, vdev_guid, path, devid); 1138eda14cbcSMatt Macy if (error) 1139eda14cbcSMatt Macy return (error); 1140eda14cbcSMatt Macy } 1141eda14cbcSMatt Macy return (0); 1142eda14cbcSMatt Macy } 1143eda14cbcSMatt Macy 1144eda14cbcSMatt Macy if (nvroot == NULL) 1145eda14cbcSMatt Macy return (0); 1146eda14cbcSMatt Macy 1147eda14cbcSMatt Macy error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid); 1148eda14cbcSMatt Macy if ((error != 0) || (guid != vdev_guid)) 1149eda14cbcSMatt Macy return (0); 1150eda14cbcSMatt Macy 1151eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val); 1152eda14cbcSMatt Macy if (error == 0) 1153eda14cbcSMatt Macy *path = val; 1154eda14cbcSMatt Macy 1155eda14cbcSMatt Macy error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val); 1156eda14cbcSMatt Macy if (error == 0) 1157eda14cbcSMatt Macy *devid = val; 1158eda14cbcSMatt Macy 1159eda14cbcSMatt Macy return (0); 1160eda14cbcSMatt Macy } 1161eda14cbcSMatt Macy 1162eda14cbcSMatt Macy /* 1163eda14cbcSMatt Macy * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID 1164eda14cbcSMatt Macy * and store these strings as config_path and devid_path respectively. 1165eda14cbcSMatt Macy * The returned pointers are only valid as long as label remains valid. 1166eda14cbcSMatt Macy */ 1167eda14cbcSMatt Macy int 1168eda14cbcSMatt Macy label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid) 1169eda14cbcSMatt Macy { 1170eda14cbcSMatt Macy nvlist_t *nvroot; 1171eda14cbcSMatt Macy uint64_t pool_guid; 1172eda14cbcSMatt Macy uint64_t vdev_guid; 1173eda14cbcSMatt Macy 1174eda14cbcSMatt Macy *path = NULL; 1175eda14cbcSMatt Macy *devid = NULL; 1176eda14cbcSMatt Macy 1177eda14cbcSMatt Macy if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1178eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) || 1179eda14cbcSMatt Macy nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid)) 1180eda14cbcSMatt Macy return (ENOENT); 1181eda14cbcSMatt Macy 1182eda14cbcSMatt Macy return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path, 1183eda14cbcSMatt Macy devid)); 1184eda14cbcSMatt Macy } 1185eda14cbcSMatt Macy 1186eda14cbcSMatt Macy static void 1187eda14cbcSMatt Macy zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock, 1188eda14cbcSMatt Macy avl_tree_t *cache, const char *path, const char *name, int order) 1189eda14cbcSMatt Macy { 1190eda14cbcSMatt Macy avl_index_t where; 1191eda14cbcSMatt Macy rdsk_node_t *slice; 1192eda14cbcSMatt Macy 1193eda14cbcSMatt Macy slice = zutil_alloc(hdl, sizeof (rdsk_node_t)); 1194eda14cbcSMatt Macy if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) { 1195eda14cbcSMatt Macy free(slice); 1196eda14cbcSMatt Macy return; 1197eda14cbcSMatt Macy } 1198eda14cbcSMatt Macy slice->rn_vdev_guid = 0; 1199eda14cbcSMatt Macy slice->rn_lock = lock; 1200eda14cbcSMatt Macy slice->rn_avl = cache; 1201eda14cbcSMatt Macy slice->rn_hdl = hdl; 1202eda14cbcSMatt Macy slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET; 1203eda14cbcSMatt Macy slice->rn_labelpaths = B_FALSE; 1204eda14cbcSMatt Macy 1205eda14cbcSMatt Macy pthread_mutex_lock(lock); 1206eda14cbcSMatt Macy if (avl_find(cache, slice, &where)) { 1207eda14cbcSMatt Macy free(slice->rn_name); 1208eda14cbcSMatt Macy free(slice); 1209eda14cbcSMatt Macy } else { 1210eda14cbcSMatt Macy avl_insert(cache, slice, where); 1211eda14cbcSMatt Macy } 1212eda14cbcSMatt Macy pthread_mutex_unlock(lock); 1213eda14cbcSMatt Macy } 1214eda14cbcSMatt Macy 1215eda14cbcSMatt Macy static int 1216eda14cbcSMatt Macy zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock, 1217eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1218eda14cbcSMatt Macy { 1219eda14cbcSMatt Macy int error; 1220eda14cbcSMatt Macy char path[MAXPATHLEN]; 1221eda14cbcSMatt Macy struct dirent64 *dp; 1222eda14cbcSMatt Macy DIR *dirp; 1223eda14cbcSMatt Macy 1224eda14cbcSMatt Macy if (realpath(dir, path) == NULL) { 1225eda14cbcSMatt Macy error = errno; 1226eda14cbcSMatt Macy if (error == ENOENT) 1227eda14cbcSMatt Macy return (0); 1228eda14cbcSMatt Macy 1229eda14cbcSMatt Macy zutil_error_aux(hdl, strerror(error)); 1230eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1231eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1232eda14cbcSMatt Macy return (error); 1233eda14cbcSMatt Macy } 1234eda14cbcSMatt Macy 1235eda14cbcSMatt Macy dirp = opendir(path); 1236eda14cbcSMatt Macy if (dirp == NULL) { 1237eda14cbcSMatt Macy error = errno; 1238eda14cbcSMatt Macy zutil_error_aux(hdl, strerror(error)); 1239eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, 1240eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 1241eda14cbcSMatt Macy return (error); 1242eda14cbcSMatt Macy } 1243eda14cbcSMatt Macy 1244eda14cbcSMatt Macy while ((dp = readdir64(dirp)) != NULL) { 1245eda14cbcSMatt Macy const char *name = dp->d_name; 1246eda14cbcSMatt Macy if (name[0] == '.' && 1247eda14cbcSMatt Macy (name[1] == 0 || (name[1] == '.' && name[2] == 0))) 1248eda14cbcSMatt Macy continue; 1249eda14cbcSMatt Macy 1250eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, 1251eda14cbcSMatt Macy order); 1252eda14cbcSMatt Macy } 1253eda14cbcSMatt Macy 1254eda14cbcSMatt Macy (void) closedir(dirp); 1255eda14cbcSMatt Macy return (0); 1256eda14cbcSMatt Macy } 1257eda14cbcSMatt Macy 1258eda14cbcSMatt Macy static int 1259eda14cbcSMatt Macy zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock, 1260eda14cbcSMatt Macy avl_tree_t *cache, const char *dir, int order) 1261eda14cbcSMatt Macy { 1262eda14cbcSMatt Macy int error = 0; 1263eda14cbcSMatt Macy char path[MAXPATHLEN]; 1264eda14cbcSMatt Macy char *d, *b; 1265eda14cbcSMatt Macy char *dpath, *name; 1266eda14cbcSMatt Macy 1267eda14cbcSMatt Macy /* 1268eda14cbcSMatt Macy * Separate the directory part and last part of the 1269eda14cbcSMatt Macy * path. We do this so that we can get the realpath of 1270eda14cbcSMatt Macy * the directory. We don't get the realpath on the 1271eda14cbcSMatt Macy * whole path because if it's a symlink, we want the 1272eda14cbcSMatt Macy * path of the symlink not where it points to. 1273eda14cbcSMatt Macy */ 1274eda14cbcSMatt Macy d = zutil_strdup(hdl, dir); 1275eda14cbcSMatt Macy b = zutil_strdup(hdl, dir); 1276eda14cbcSMatt Macy dpath = dirname(d); 1277eda14cbcSMatt Macy name = basename(b); 1278eda14cbcSMatt Macy 1279eda14cbcSMatt Macy if (realpath(dpath, path) == NULL) { 1280eda14cbcSMatt Macy error = errno; 1281eda14cbcSMatt Macy if (error == ENOENT) { 1282eda14cbcSMatt Macy error = 0; 1283eda14cbcSMatt Macy goto out; 1284eda14cbcSMatt Macy } 1285eda14cbcSMatt Macy 1286eda14cbcSMatt Macy zutil_error_aux(hdl, strerror(error)); 1287eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1288eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1289eda14cbcSMatt Macy goto out; 1290eda14cbcSMatt Macy } 1291eda14cbcSMatt Macy 1292eda14cbcSMatt Macy zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order); 1293eda14cbcSMatt Macy 1294eda14cbcSMatt Macy out: 1295eda14cbcSMatt Macy free(b); 1296eda14cbcSMatt Macy free(d); 1297eda14cbcSMatt Macy return (error); 1298eda14cbcSMatt Macy } 1299eda14cbcSMatt Macy 1300eda14cbcSMatt Macy /* 1301eda14cbcSMatt Macy * Scan a list of directories for zfs devices. 1302eda14cbcSMatt Macy */ 1303eda14cbcSMatt Macy static int 1304eda14cbcSMatt Macy zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock, 1305eda14cbcSMatt Macy avl_tree_t **slice_cache, const char * const *dir, size_t dirs) 1306eda14cbcSMatt Macy { 1307eda14cbcSMatt Macy avl_tree_t *cache; 1308eda14cbcSMatt Macy rdsk_node_t *slice; 1309eda14cbcSMatt Macy void *cookie; 1310eda14cbcSMatt Macy int i, error; 1311eda14cbcSMatt Macy 1312eda14cbcSMatt Macy *slice_cache = NULL; 1313eda14cbcSMatt Macy cache = zutil_alloc(hdl, sizeof (avl_tree_t)); 1314eda14cbcSMatt Macy avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t), 1315eda14cbcSMatt Macy offsetof(rdsk_node_t, rn_node)); 1316eda14cbcSMatt Macy 1317eda14cbcSMatt Macy for (i = 0; i < dirs; i++) { 1318eda14cbcSMatt Macy struct stat sbuf; 1319eda14cbcSMatt Macy 1320eda14cbcSMatt Macy if (stat(dir[i], &sbuf) != 0) { 1321eda14cbcSMatt Macy error = errno; 1322eda14cbcSMatt Macy if (error == ENOENT) 1323eda14cbcSMatt Macy continue; 1324eda14cbcSMatt Macy 1325eda14cbcSMatt Macy zutil_error_aux(hdl, strerror(error)); 1326eda14cbcSMatt Macy (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1327eda14cbcSMatt Macy TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]); 1328eda14cbcSMatt Macy goto error; 1329eda14cbcSMatt Macy } 1330eda14cbcSMatt Macy 1331eda14cbcSMatt Macy /* 1332eda14cbcSMatt Macy * If dir[i] is a directory, we walk through it and add all 1333eda14cbcSMatt Macy * the entries to the cache. If it's not a directory, we just 1334eda14cbcSMatt Macy * add it to the cache. 1335eda14cbcSMatt Macy */ 1336eda14cbcSMatt Macy if (S_ISDIR(sbuf.st_mode)) { 1337eda14cbcSMatt Macy if ((error = zpool_find_import_scan_dir(hdl, lock, 1338eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1339eda14cbcSMatt Macy goto error; 1340eda14cbcSMatt Macy } else { 1341eda14cbcSMatt Macy if ((error = zpool_find_import_scan_path(hdl, lock, 1342eda14cbcSMatt Macy cache, dir[i], i)) != 0) 1343eda14cbcSMatt Macy goto error; 1344eda14cbcSMatt Macy } 1345eda14cbcSMatt Macy } 1346eda14cbcSMatt Macy 1347eda14cbcSMatt Macy *slice_cache = cache; 1348eda14cbcSMatt Macy return (0); 1349eda14cbcSMatt Macy 1350eda14cbcSMatt Macy error: 1351eda14cbcSMatt Macy cookie = NULL; 1352eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1353eda14cbcSMatt Macy free(slice->rn_name); 1354eda14cbcSMatt Macy free(slice); 1355eda14cbcSMatt Macy } 1356eda14cbcSMatt Macy free(cache); 1357eda14cbcSMatt Macy 1358eda14cbcSMatt Macy return (error); 1359eda14cbcSMatt Macy } 1360eda14cbcSMatt Macy 1361eda14cbcSMatt Macy /* 1362eda14cbcSMatt Macy * Given a list of directories to search, find all pools stored on disk. This 1363eda14cbcSMatt Macy * includes partial pools which are not available to import. If no args are 1364eda14cbcSMatt Macy * given (argc is 0), then the default directory (/dev/dsk) is searched. 1365eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1366eda14cbcSMatt Macy * to import a specific pool. 1367eda14cbcSMatt Macy */ 1368eda14cbcSMatt Macy static nvlist_t * 13699db44a8eSMartin Matuska zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg, 13709db44a8eSMartin Matuska pthread_mutex_t *lock, avl_tree_t *cache) 1371eda14cbcSMatt Macy { 1372eda14cbcSMatt Macy nvlist_t *ret = NULL; 1373eda14cbcSMatt Macy pool_list_t pools = { 0 }; 1374eda14cbcSMatt Macy pool_entry_t *pe, *penext; 1375eda14cbcSMatt Macy vdev_entry_t *ve, *venext; 1376eda14cbcSMatt Macy config_entry_t *ce, *cenext; 1377eda14cbcSMatt Macy name_entry_t *ne, *nenext; 1378eda14cbcSMatt Macy rdsk_node_t *slice; 1379eda14cbcSMatt Macy void *cookie; 1380eda14cbcSMatt Macy tpool_t *t; 1381eda14cbcSMatt Macy 1382eda14cbcSMatt Macy verify(iarg->poolname == NULL || iarg->guid == 0); 1383eda14cbcSMatt Macy 1384eda14cbcSMatt Macy /* 1385eda14cbcSMatt Macy * Create a thread pool to parallelize the process of reading and 1386eda14cbcSMatt Macy * validating labels, a large number of threads can be used due to 1387eda14cbcSMatt Macy * minimal contention. 1388eda14cbcSMatt Macy */ 1389eda14cbcSMatt Macy t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL); 1390eda14cbcSMatt Macy for (slice = avl_first(cache); slice; 1391eda14cbcSMatt Macy (slice = avl_walk(cache, slice, AVL_AFTER))) 1392eda14cbcSMatt Macy (void) tpool_dispatch(t, zpool_open_func, slice); 1393eda14cbcSMatt Macy 1394eda14cbcSMatt Macy tpool_wait(t); 1395eda14cbcSMatt Macy tpool_destroy(t); 1396eda14cbcSMatt Macy 1397eda14cbcSMatt Macy /* 1398eda14cbcSMatt Macy * Process the cache, filtering out any entries which are not 1399eda14cbcSMatt Macy * for the specified pool then adding matching label configs. 1400eda14cbcSMatt Macy */ 1401eda14cbcSMatt Macy cookie = NULL; 1402eda14cbcSMatt Macy while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1403eda14cbcSMatt Macy if (slice->rn_config != NULL) { 1404eda14cbcSMatt Macy nvlist_t *config = slice->rn_config; 1405eda14cbcSMatt Macy boolean_t matched = B_TRUE; 1406eda14cbcSMatt Macy boolean_t aux = B_FALSE; 1407eda14cbcSMatt Macy int fd; 1408eda14cbcSMatt Macy 1409eda14cbcSMatt Macy /* 1410eda14cbcSMatt Macy * Check if it's a spare or l2cache device. If it is, 1411eda14cbcSMatt Macy * we need to skip the name and guid check since they 1412eda14cbcSMatt Macy * don't exist on aux device label. 1413eda14cbcSMatt Macy */ 1414eda14cbcSMatt Macy if (iarg->poolname != NULL || iarg->guid != 0) { 1415eda14cbcSMatt Macy uint64_t state; 1416eda14cbcSMatt Macy aux = nvlist_lookup_uint64(config, 1417eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_STATE, &state) == 0 && 1418eda14cbcSMatt Macy (state == POOL_STATE_SPARE || 1419eda14cbcSMatt Macy state == POOL_STATE_L2CACHE); 1420eda14cbcSMatt Macy } 1421eda14cbcSMatt Macy 1422eda14cbcSMatt Macy if (iarg->poolname != NULL && !aux) { 1423eda14cbcSMatt Macy char *pname; 1424eda14cbcSMatt Macy 1425eda14cbcSMatt Macy matched = nvlist_lookup_string(config, 1426eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, &pname) == 0 && 1427eda14cbcSMatt Macy strcmp(iarg->poolname, pname) == 0; 1428eda14cbcSMatt Macy } else if (iarg->guid != 0 && !aux) { 1429eda14cbcSMatt Macy uint64_t this_guid; 1430eda14cbcSMatt Macy 1431eda14cbcSMatt Macy matched = nvlist_lookup_uint64(config, 1432eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 && 1433eda14cbcSMatt Macy iarg->guid == this_guid; 1434eda14cbcSMatt Macy } 1435eda14cbcSMatt Macy if (matched) { 1436eda14cbcSMatt Macy /* 1437eda14cbcSMatt Macy * Verify all remaining entries can be opened 1438eda14cbcSMatt Macy * exclusively. This will prune all underlying 1439eda14cbcSMatt Macy * multipath devices which otherwise could 1440eda14cbcSMatt Macy * result in the vdev appearing as UNAVAIL. 1441eda14cbcSMatt Macy * 1442eda14cbcSMatt Macy * Under zdb, this step isn't required and 1443eda14cbcSMatt Macy * would prevent a zdb -e of active pools with 1444eda14cbcSMatt Macy * no cachefile. 1445eda14cbcSMatt Macy */ 1446*16038816SMartin Matuska fd = open(slice->rn_name, 1447*16038816SMartin Matuska O_RDONLY | O_EXCL | O_CLOEXEC); 1448eda14cbcSMatt Macy if (fd >= 0 || iarg->can_be_active) { 1449eda14cbcSMatt Macy if (fd >= 0) 1450eda14cbcSMatt Macy close(fd); 1451eda14cbcSMatt Macy add_config(hdl, &pools, 1452eda14cbcSMatt Macy slice->rn_name, slice->rn_order, 1453eda14cbcSMatt Macy slice->rn_num_labels, config); 1454eda14cbcSMatt Macy } 1455eda14cbcSMatt Macy } 1456eda14cbcSMatt Macy nvlist_free(config); 1457eda14cbcSMatt Macy } 1458eda14cbcSMatt Macy free(slice->rn_name); 1459eda14cbcSMatt Macy free(slice); 1460eda14cbcSMatt Macy } 1461eda14cbcSMatt Macy avl_destroy(cache); 1462eda14cbcSMatt Macy free(cache); 1463eda14cbcSMatt Macy 1464eda14cbcSMatt Macy ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy); 1465eda14cbcSMatt Macy 1466eda14cbcSMatt Macy for (pe = pools.pools; pe != NULL; pe = penext) { 1467eda14cbcSMatt Macy penext = pe->pe_next; 1468eda14cbcSMatt Macy for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 1469eda14cbcSMatt Macy venext = ve->ve_next; 1470eda14cbcSMatt Macy for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 1471eda14cbcSMatt Macy cenext = ce->ce_next; 1472eda14cbcSMatt Macy nvlist_free(ce->ce_config); 1473eda14cbcSMatt Macy free(ce); 1474eda14cbcSMatt Macy } 1475eda14cbcSMatt Macy free(ve); 1476eda14cbcSMatt Macy } 1477eda14cbcSMatt Macy free(pe); 1478eda14cbcSMatt Macy } 1479eda14cbcSMatt Macy 1480eda14cbcSMatt Macy for (ne = pools.names; ne != NULL; ne = nenext) { 1481eda14cbcSMatt Macy nenext = ne->ne_next; 1482eda14cbcSMatt Macy free(ne->ne_name); 1483eda14cbcSMatt Macy free(ne); 1484eda14cbcSMatt Macy } 1485eda14cbcSMatt Macy 1486eda14cbcSMatt Macy return (ret); 1487eda14cbcSMatt Macy } 1488eda14cbcSMatt Macy 1489eda14cbcSMatt Macy /* 14909db44a8eSMartin Matuska * Given a config, discover the paths for the devices which 14919db44a8eSMartin Matuska * exist in the config. 14929db44a8eSMartin Matuska */ 14939db44a8eSMartin Matuska static int 14949db44a8eSMartin Matuska discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv, 14959db44a8eSMartin Matuska avl_tree_t *cache, pthread_mutex_t *lock) 14969db44a8eSMartin Matuska { 14979db44a8eSMartin Matuska char *path = NULL; 14989db44a8eSMartin Matuska uint_t children; 14999db44a8eSMartin Matuska nvlist_t **child; 15009db44a8eSMartin Matuska 15019db44a8eSMartin Matuska if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 15029db44a8eSMartin Matuska &child, &children) == 0) { 15039db44a8eSMartin Matuska for (int c = 0; c < children; c++) { 15049db44a8eSMartin Matuska discover_cached_paths(hdl, child[c], cache, lock); 15059db44a8eSMartin Matuska } 15069db44a8eSMartin Matuska } 15079db44a8eSMartin Matuska 15089db44a8eSMartin Matuska /* 15099db44a8eSMartin Matuska * Once we have the path, we need to add the directory to 1510*16038816SMartin Matuska * our directory cache. 15119db44a8eSMartin Matuska */ 15129db44a8eSMartin Matuska if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 15139db44a8eSMartin Matuska return (zpool_find_import_scan_dir(hdl, lock, cache, 15149db44a8eSMartin Matuska dirname(path), 0)); 15159db44a8eSMartin Matuska } 15169db44a8eSMartin Matuska return (0); 15179db44a8eSMartin Matuska } 15189db44a8eSMartin Matuska 15199db44a8eSMartin Matuska /* 1520eda14cbcSMatt Macy * Given a cache file, return the contents as a list of importable pools. 1521eda14cbcSMatt Macy * poolname or guid (but not both) are provided by the caller when trying 1522eda14cbcSMatt Macy * to import a specific pool. 1523eda14cbcSMatt Macy */ 1524eda14cbcSMatt Macy static nvlist_t * 15259db44a8eSMartin Matuska zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg) 1526eda14cbcSMatt Macy { 1527eda14cbcSMatt Macy char *buf; 1528eda14cbcSMatt Macy int fd; 1529eda14cbcSMatt Macy struct stat64 statbuf; 1530eda14cbcSMatt Macy nvlist_t *raw, *src, *dst; 1531eda14cbcSMatt Macy nvlist_t *pools; 1532eda14cbcSMatt Macy nvpair_t *elem; 1533eda14cbcSMatt Macy char *name; 1534eda14cbcSMatt Macy uint64_t this_guid; 1535eda14cbcSMatt Macy boolean_t active; 1536eda14cbcSMatt Macy 15379db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 1538eda14cbcSMatt Macy 1539*16038816SMartin Matuska if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) { 1540eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1541eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1542eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "failed to open cache file")); 1543eda14cbcSMatt Macy return (NULL); 1544eda14cbcSMatt Macy } 1545eda14cbcSMatt Macy 1546eda14cbcSMatt Macy if (fstat64(fd, &statbuf) != 0) { 1547eda14cbcSMatt Macy zutil_error_aux(hdl, "%s", strerror(errno)); 1548eda14cbcSMatt Macy (void) close(fd); 1549eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1550eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 1551eda14cbcSMatt Macy return (NULL); 1552eda14cbcSMatt Macy } 1553eda14cbcSMatt Macy 1554eda14cbcSMatt Macy if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) { 1555eda14cbcSMatt Macy (void) close(fd); 1556eda14cbcSMatt Macy return (NULL); 1557eda14cbcSMatt Macy } 1558eda14cbcSMatt Macy 1559eda14cbcSMatt Macy if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 1560eda14cbcSMatt Macy (void) close(fd); 1561eda14cbcSMatt Macy free(buf); 1562eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1563eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 1564eda14cbcSMatt Macy "failed to read cache file contents")); 1565eda14cbcSMatt Macy return (NULL); 1566eda14cbcSMatt Macy } 1567eda14cbcSMatt Macy 1568eda14cbcSMatt Macy (void) close(fd); 1569eda14cbcSMatt Macy 1570eda14cbcSMatt Macy if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 1571eda14cbcSMatt Macy free(buf); 1572eda14cbcSMatt Macy (void) zutil_error(hdl, EZFS_BADCACHE, 1573eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 1574eda14cbcSMatt Macy "invalid or corrupt cache file contents")); 1575eda14cbcSMatt Macy return (NULL); 1576eda14cbcSMatt Macy } 1577eda14cbcSMatt Macy 1578eda14cbcSMatt Macy free(buf); 1579eda14cbcSMatt Macy 1580eda14cbcSMatt Macy /* 1581eda14cbcSMatt Macy * Go through and get the current state of the pools and refresh their 1582eda14cbcSMatt Macy * state. 1583eda14cbcSMatt Macy */ 1584eda14cbcSMatt Macy if (nvlist_alloc(&pools, 0, 0) != 0) { 1585eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1586eda14cbcSMatt Macy nvlist_free(raw); 1587eda14cbcSMatt Macy return (NULL); 1588eda14cbcSMatt Macy } 1589eda14cbcSMatt Macy 1590eda14cbcSMatt Macy elem = NULL; 1591eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 1592eda14cbcSMatt Macy src = fnvpair_value_nvlist(elem); 1593eda14cbcSMatt Macy 1594eda14cbcSMatt Macy name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); 15959db44a8eSMartin Matuska if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0) 1596eda14cbcSMatt Macy continue; 1597eda14cbcSMatt Macy 1598eda14cbcSMatt Macy this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); 15999db44a8eSMartin Matuska if (iarg->guid != 0 && iarg->guid != this_guid) 1600eda14cbcSMatt Macy continue; 1601eda14cbcSMatt Macy 1602eda14cbcSMatt Macy if (zutil_pool_active(hdl, name, this_guid, &active) != 0) { 1603eda14cbcSMatt Macy nvlist_free(raw); 1604eda14cbcSMatt Macy nvlist_free(pools); 1605eda14cbcSMatt Macy return (NULL); 1606eda14cbcSMatt Macy } 1607eda14cbcSMatt Macy 1608eda14cbcSMatt Macy if (active) 1609eda14cbcSMatt Macy continue; 1610eda14cbcSMatt Macy 16119db44a8eSMartin Matuska if (iarg->scan) { 16129db44a8eSMartin Matuska uint64_t saved_guid = iarg->guid; 16139db44a8eSMartin Matuska const char *saved_poolname = iarg->poolname; 16149db44a8eSMartin Matuska pthread_mutex_t lock; 16159db44a8eSMartin Matuska 16169db44a8eSMartin Matuska /* 16179db44a8eSMartin Matuska * Create the device cache that will hold the 16189db44a8eSMartin Matuska * devices we will scan based on the cachefile. 16199db44a8eSMartin Matuska * This will get destroyed and freed by 16209db44a8eSMartin Matuska * zpool_find_import_impl. 16219db44a8eSMartin Matuska */ 16229db44a8eSMartin Matuska avl_tree_t *cache = zutil_alloc(hdl, 16239db44a8eSMartin Matuska sizeof (avl_tree_t)); 16249db44a8eSMartin Matuska avl_create(cache, slice_cache_compare, 16259db44a8eSMartin Matuska sizeof (rdsk_node_t), 16269db44a8eSMartin Matuska offsetof(rdsk_node_t, rn_node)); 16279db44a8eSMartin Matuska nvlist_t *nvroot = fnvlist_lookup_nvlist(src, 16289db44a8eSMartin Matuska ZPOOL_CONFIG_VDEV_TREE); 16299db44a8eSMartin Matuska 16309db44a8eSMartin Matuska /* 16319db44a8eSMartin Matuska * We only want to find the pool with this_guid. 16329db44a8eSMartin Matuska * We will reset these values back later. 16339db44a8eSMartin Matuska */ 16349db44a8eSMartin Matuska iarg->guid = this_guid; 16359db44a8eSMartin Matuska iarg->poolname = NULL; 16369db44a8eSMartin Matuska 16379db44a8eSMartin Matuska /* 16389db44a8eSMartin Matuska * We need to build up a cache of devices that exists 16399db44a8eSMartin Matuska * in the paths pointed to by the cachefile. This allows 16409db44a8eSMartin Matuska * us to preserve the device namespace that was 16419db44a8eSMartin Matuska * originally specified by the user but also lets us 16429db44a8eSMartin Matuska * scan devices in those directories in case they had 16439db44a8eSMartin Matuska * been renamed. 16449db44a8eSMartin Matuska */ 16459db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 16469db44a8eSMartin Matuska discover_cached_paths(hdl, nvroot, cache, &lock); 16479db44a8eSMartin Matuska nvlist_t *nv = zpool_find_import_impl(hdl, iarg, 16489db44a8eSMartin Matuska &lock, cache); 16499db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 16509db44a8eSMartin Matuska 16519db44a8eSMartin Matuska /* 16529db44a8eSMartin Matuska * zpool_find_import_impl will return back 16539db44a8eSMartin Matuska * a list of pools that it found based on the 16549db44a8eSMartin Matuska * device cache. There should only be one pool 16559db44a8eSMartin Matuska * since we're looking for a specific guid. 16569db44a8eSMartin Matuska * We will use that pool to build up the final 16579db44a8eSMartin Matuska * pool nvlist which is returned back to the 16589db44a8eSMartin Matuska * caller. 16599db44a8eSMartin Matuska */ 16609db44a8eSMartin Matuska nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 16619db44a8eSMartin Matuska fnvlist_add_nvlist(pools, nvpair_name(pair), 16629db44a8eSMartin Matuska fnvpair_value_nvlist(pair)); 16639db44a8eSMartin Matuska 16649db44a8eSMartin Matuska VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL); 16659db44a8eSMartin Matuska 16669db44a8eSMartin Matuska iarg->guid = saved_guid; 16679db44a8eSMartin Matuska iarg->poolname = saved_poolname; 16689db44a8eSMartin Matuska continue; 16699db44a8eSMartin Matuska } 16709db44a8eSMartin Matuska 1671eda14cbcSMatt Macy if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, 16729db44a8eSMartin Matuska iarg->cachefile) != 0) { 1673eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1674eda14cbcSMatt Macy nvlist_free(raw); 1675eda14cbcSMatt Macy nvlist_free(pools); 1676eda14cbcSMatt Macy return (NULL); 1677eda14cbcSMatt Macy } 1678eda14cbcSMatt Macy 1679eda14cbcSMatt Macy if ((dst = zutil_refresh_config(hdl, src)) == NULL) { 1680eda14cbcSMatt Macy nvlist_free(raw); 1681eda14cbcSMatt Macy nvlist_free(pools); 1682eda14cbcSMatt Macy return (NULL); 1683eda14cbcSMatt Macy } 1684eda14cbcSMatt Macy 1685eda14cbcSMatt Macy if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1686eda14cbcSMatt Macy (void) zutil_no_memory(hdl); 1687eda14cbcSMatt Macy nvlist_free(dst); 1688eda14cbcSMatt Macy nvlist_free(raw); 1689eda14cbcSMatt Macy nvlist_free(pools); 1690eda14cbcSMatt Macy return (NULL); 1691eda14cbcSMatt Macy } 1692eda14cbcSMatt Macy nvlist_free(dst); 1693eda14cbcSMatt Macy } 1694eda14cbcSMatt Macy nvlist_free(raw); 1695eda14cbcSMatt Macy return (pools); 1696eda14cbcSMatt Macy } 1697eda14cbcSMatt Macy 16989db44a8eSMartin Matuska static nvlist_t * 16999db44a8eSMartin Matuska zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg) 17009db44a8eSMartin Matuska { 17019db44a8eSMartin Matuska pthread_mutex_t lock; 17029db44a8eSMartin Matuska avl_tree_t *cache; 17039db44a8eSMartin Matuska nvlist_t *pools = NULL; 17049db44a8eSMartin Matuska 17059db44a8eSMartin Matuska verify(iarg->poolname == NULL || iarg->guid == 0); 17069db44a8eSMartin Matuska pthread_mutex_init(&lock, NULL); 17079db44a8eSMartin Matuska 17089db44a8eSMartin Matuska /* 17099db44a8eSMartin Matuska * Locate pool member vdevs by blkid or by directory scanning. 17109db44a8eSMartin Matuska * On success a newly allocated AVL tree which is populated with an 17119db44a8eSMartin Matuska * entry for each discovered vdev will be returned in the cache. 17129db44a8eSMartin Matuska * It's the caller's responsibility to consume and destroy this tree. 17139db44a8eSMartin Matuska */ 17149db44a8eSMartin Matuska if (iarg->scan || iarg->paths != 0) { 17159db44a8eSMartin Matuska size_t dirs = iarg->paths; 17169db44a8eSMartin Matuska const char * const *dir = (const char * const *)iarg->path; 17179db44a8eSMartin Matuska 17189db44a8eSMartin Matuska if (dirs == 0) 17199db44a8eSMartin Matuska dir = zpool_default_search_paths(&dirs); 17209db44a8eSMartin Matuska 17219db44a8eSMartin Matuska if (zpool_find_import_scan(hdl, &lock, &cache, 17229db44a8eSMartin Matuska dir, dirs) != 0) { 17239db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17249db44a8eSMartin Matuska return (NULL); 17259db44a8eSMartin Matuska } 17269db44a8eSMartin Matuska } else { 17279db44a8eSMartin Matuska if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) { 17289db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17299db44a8eSMartin Matuska return (NULL); 17309db44a8eSMartin Matuska } 17319db44a8eSMartin Matuska } 17329db44a8eSMartin Matuska 17339db44a8eSMartin Matuska pools = zpool_find_import_impl(hdl, iarg, &lock, cache); 17349db44a8eSMartin Matuska pthread_mutex_destroy(&lock); 17359db44a8eSMartin Matuska return (pools); 17369db44a8eSMartin Matuska } 17379db44a8eSMartin Matuska 17389db44a8eSMartin Matuska 1739eda14cbcSMatt Macy nvlist_t * 1740eda14cbcSMatt Macy zpool_search_import(void *hdl, importargs_t *import, 1741eda14cbcSMatt Macy const pool_config_ops_t *pco) 1742eda14cbcSMatt Macy { 1743eda14cbcSMatt Macy libpc_handle_t handle = { 0 }; 1744eda14cbcSMatt Macy nvlist_t *pools = NULL; 1745eda14cbcSMatt Macy 1746eda14cbcSMatt Macy handle.lpc_lib_handle = hdl; 1747eda14cbcSMatt Macy handle.lpc_ops = pco; 1748eda14cbcSMatt Macy handle.lpc_printerr = B_TRUE; 1749eda14cbcSMatt Macy 1750eda14cbcSMatt Macy verify(import->poolname == NULL || import->guid == 0); 1751eda14cbcSMatt Macy 1752eda14cbcSMatt Macy if (import->cachefile != NULL) 17539db44a8eSMartin Matuska pools = zpool_find_import_cached(&handle, import); 1754eda14cbcSMatt Macy else 17559db44a8eSMartin Matuska pools = zpool_find_import(&handle, import); 1756eda14cbcSMatt Macy 1757eda14cbcSMatt Macy if ((pools == NULL || nvlist_empty(pools)) && 1758eda14cbcSMatt Macy handle.lpc_open_access_error && geteuid() != 0) { 1759eda14cbcSMatt Macy (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN, 1760eda14cbcSMatt Macy "no pools found")); 1761eda14cbcSMatt Macy } 1762eda14cbcSMatt Macy 1763eda14cbcSMatt Macy return (pools); 1764eda14cbcSMatt Macy } 1765eda14cbcSMatt Macy 1766eda14cbcSMatt Macy static boolean_t 1767eda14cbcSMatt Macy pool_match(nvlist_t *cfg, char *tgt) 1768eda14cbcSMatt Macy { 1769eda14cbcSMatt Macy uint64_t v, guid = strtoull(tgt, NULL, 0); 1770eda14cbcSMatt Macy char *s; 1771eda14cbcSMatt Macy 1772eda14cbcSMatt Macy if (guid != 0) { 1773eda14cbcSMatt Macy if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 1774eda14cbcSMatt Macy return (v == guid); 1775eda14cbcSMatt Macy } else { 1776eda14cbcSMatt Macy if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 1777eda14cbcSMatt Macy return (strcmp(s, tgt) == 0); 1778eda14cbcSMatt Macy } 1779eda14cbcSMatt Macy return (B_FALSE); 1780eda14cbcSMatt Macy } 1781eda14cbcSMatt Macy 1782eda14cbcSMatt Macy int 1783eda14cbcSMatt Macy zpool_find_config(void *hdl, const char *target, nvlist_t **configp, 1784eda14cbcSMatt Macy importargs_t *args, const pool_config_ops_t *pco) 1785eda14cbcSMatt Macy { 1786eda14cbcSMatt Macy nvlist_t *pools; 1787eda14cbcSMatt Macy nvlist_t *match = NULL; 1788eda14cbcSMatt Macy nvlist_t *config = NULL; 17897877fdebSMatt Macy char *sepp = NULL; 1790eda14cbcSMatt Macy int count = 0; 1791eda14cbcSMatt Macy char *targetdup = strdup(target); 1792eda14cbcSMatt Macy 1793eda14cbcSMatt Macy *configp = NULL; 1794eda14cbcSMatt Macy 1795*16038816SMartin Matuska if ((sepp = strpbrk(targetdup, "/@")) != NULL) 1796eda14cbcSMatt Macy *sepp = '\0'; 1797eda14cbcSMatt Macy 1798eda14cbcSMatt Macy pools = zpool_search_import(hdl, args, pco); 1799eda14cbcSMatt Macy 1800eda14cbcSMatt Macy if (pools != NULL) { 1801eda14cbcSMatt Macy nvpair_t *elem = NULL; 1802eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 1803eda14cbcSMatt Macy VERIFY0(nvpair_value_nvlist(elem, &config)); 1804eda14cbcSMatt Macy if (pool_match(config, targetdup)) { 1805eda14cbcSMatt Macy count++; 1806eda14cbcSMatt Macy if (match != NULL) { 1807eda14cbcSMatt Macy /* multiple matches found */ 1808eda14cbcSMatt Macy continue; 1809eda14cbcSMatt Macy } else { 18107877fdebSMatt Macy match = fnvlist_dup(config); 1811eda14cbcSMatt Macy } 1812eda14cbcSMatt Macy } 1813eda14cbcSMatt Macy } 18147877fdebSMatt Macy fnvlist_free(pools); 1815eda14cbcSMatt Macy } 1816eda14cbcSMatt Macy 1817eda14cbcSMatt Macy if (count == 0) { 1818eda14cbcSMatt Macy free(targetdup); 1819eda14cbcSMatt Macy return (ENOENT); 1820eda14cbcSMatt Macy } 1821eda14cbcSMatt Macy 1822eda14cbcSMatt Macy if (count > 1) { 1823eda14cbcSMatt Macy free(targetdup); 18247877fdebSMatt Macy fnvlist_free(match); 1825eda14cbcSMatt Macy return (EINVAL); 1826eda14cbcSMatt Macy } 1827eda14cbcSMatt Macy 1828eda14cbcSMatt Macy *configp = match; 1829eda14cbcSMatt Macy free(targetdup); 1830eda14cbcSMatt Macy 1831eda14cbcSMatt Macy return (0); 1832eda14cbcSMatt Macy } 1833