1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25 * Copyright 2015 RackTop Systems. 26 * Copyright (c) 2016, Intel Corporation. 27 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> 28 */ 29 30 /* 31 * Pool import support functions. 32 * 33 * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since 34 * these commands are expected to run in the global zone, we can assume 35 * that the devices are all readable when called. 36 * 37 * To import a pool, we rely on reading the configuration information from the 38 * ZFS label of each device. If we successfully read the label, then we 39 * organize the configuration information in the following hierarchy: 40 * 41 * pool guid -> toplevel vdev guid -> label txg 42 * 43 * Duplicate entries matching this same tuple will be discarded. Once we have 44 * examined every device, we pick the best label txg config for each toplevel 45 * vdev. We then arrange these toplevel vdevs into a complete pool config, and 46 * update any paths that have changed. Finally, we attempt to import the pool 47 * using our derived config, and record the results. 48 */ 49 50 #include <aio.h> 51 #include <ctype.h> 52 #include <dirent.h> 53 #include <errno.h> 54 #include <libintl.h> 55 #include <libgen.h> 56 #include <stddef.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <sys/stat.h> 60 #include <unistd.h> 61 #include <fcntl.h> 62 #include <sys/dktp/fdisk.h> 63 #include <sys/vdev_impl.h> 64 #include <sys/fs/zfs.h> 65 #include <sys/vdev_impl.h> 66 67 #include <thread_pool.h> 68 #include <libzutil.h> 69 #include <libnvpair.h> 70 71 #include "zutil_import.h" 72 73 /*PRINTFLIKE2*/ 74 static void 75 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...) 76 { 77 va_list ap; 78 79 va_start(ap, fmt); 80 81 (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap); 82 hdl->lpc_desc_active = B_TRUE; 83 84 va_end(ap); 85 } 86 87 static void 88 zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt, 89 va_list ap) 90 { 91 char action[1024]; 92 93 (void) vsnprintf(action, sizeof (action), fmt, ap); 94 95 if (hdl->lpc_desc_active) 96 hdl->lpc_desc_active = B_FALSE; 97 else 98 hdl->lpc_desc[0] = '\0'; 99 100 if (hdl->lpc_printerr) { 101 if (hdl->lpc_desc[0] != '\0') 102 error = hdl->lpc_desc; 103 104 (void) fprintf(stderr, "%s: %s\n", action, error); 105 } 106 } 107 108 /*PRINTFLIKE3*/ 109 static int 110 zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...) 111 { 112 va_list ap; 113 114 va_start(ap, fmt); 115 116 zutil_verror(hdl, error, fmt, ap); 117 118 va_end(ap); 119 120 return (-1); 121 } 122 123 static int 124 zutil_error(libpc_handle_t *hdl, const char *error, const char *msg) 125 { 126 return (zutil_error_fmt(hdl, error, "%s", msg)); 127 } 128 129 static int 130 zutil_no_memory(libpc_handle_t *hdl) 131 { 132 zutil_error(hdl, EZFS_NOMEM, "internal error"); 133 exit(1); 134 } 135 136 void * 137 zutil_alloc(libpc_handle_t *hdl, size_t size) 138 { 139 void *data; 140 141 if ((data = calloc(1, size)) == NULL) 142 (void) zutil_no_memory(hdl); 143 144 return (data); 145 } 146 147 char * 148 zutil_strdup(libpc_handle_t *hdl, const char *str) 149 { 150 char *ret; 151 152 if ((ret = strdup(str)) == NULL) 153 (void) zutil_no_memory(hdl); 154 155 return (ret); 156 } 157 158 /* 159 * Intermediate structures used to gather configuration information. 160 */ 161 typedef struct config_entry { 162 uint64_t ce_txg; 163 nvlist_t *ce_config; 164 struct config_entry *ce_next; 165 } config_entry_t; 166 167 typedef struct vdev_entry { 168 uint64_t ve_guid; 169 config_entry_t *ve_configs; 170 struct vdev_entry *ve_next; 171 } vdev_entry_t; 172 173 typedef struct pool_entry { 174 uint64_t pe_guid; 175 vdev_entry_t *pe_vdevs; 176 struct pool_entry *pe_next; 177 } pool_entry_t; 178 179 typedef struct name_entry { 180 char *ne_name; 181 uint64_t ne_guid; 182 uint64_t ne_order; 183 uint64_t ne_num_labels; 184 struct name_entry *ne_next; 185 } name_entry_t; 186 187 typedef struct pool_list { 188 pool_entry_t *pools; 189 name_entry_t *names; 190 } pool_list_t; 191 192 /* 193 * Go through and fix up any path and/or devid information for the given vdev 194 * configuration. 195 */ 196 static int 197 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names) 198 { 199 nvlist_t **child; 200 uint_t c, children; 201 uint64_t guid; 202 name_entry_t *ne, *best; 203 char *path; 204 205 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 206 &child, &children) == 0) { 207 for (c = 0; c < children; c++) 208 if (fix_paths(hdl, child[c], names) != 0) 209 return (-1); 210 return (0); 211 } 212 213 /* 214 * This is a leaf (file or disk) vdev. In either case, go through 215 * the name list and see if we find a matching guid. If so, replace 216 * the path and see if we can calculate a new devid. 217 * 218 * There may be multiple names associated with a particular guid, in 219 * which case we have overlapping partitions or multiple paths to the 220 * same disk. In this case we prefer to use the path name which 221 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we 222 * use the lowest order device which corresponds to the first match 223 * while traversing the ZPOOL_IMPORT_PATH search path. 224 */ 225 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 226 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 227 path = NULL; 228 229 best = NULL; 230 for (ne = names; ne != NULL; ne = ne->ne_next) { 231 if (ne->ne_guid == guid) { 232 if (path == NULL) { 233 best = ne; 234 break; 235 } 236 237 if ((strlen(path) == strlen(ne->ne_name)) && 238 strncmp(path, ne->ne_name, strlen(path)) == 0) { 239 best = ne; 240 break; 241 } 242 243 if (best == NULL) { 244 best = ne; 245 continue; 246 } 247 248 /* Prefer paths with move vdev labels. */ 249 if (ne->ne_num_labels > best->ne_num_labels) { 250 best = ne; 251 continue; 252 } 253 254 /* Prefer paths earlier in the search order. */ 255 if (ne->ne_num_labels == best->ne_num_labels && 256 ne->ne_order < best->ne_order) { 257 best = ne; 258 continue; 259 } 260 } 261 } 262 263 if (best == NULL) 264 return (0); 265 266 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 267 return (-1); 268 269 update_vdev_config_dev_strs(nv); 270 271 return (0); 272 } 273 274 /* 275 * Add the given configuration to the list of known devices. 276 */ 277 static int 278 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path, 279 int order, int num_labels, nvlist_t *config) 280 { 281 uint64_t pool_guid, vdev_guid, top_guid, txg, state; 282 pool_entry_t *pe; 283 vdev_entry_t *ve; 284 config_entry_t *ce; 285 name_entry_t *ne; 286 287 /* 288 * If this is a hot spare not currently in use or level 2 cache 289 * device, add it to the list of names to translate, but don't do 290 * anything else. 291 */ 292 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 293 &state) == 0 && 294 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 295 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 296 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 297 return (-1); 298 299 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 300 free(ne); 301 return (-1); 302 } 303 ne->ne_guid = vdev_guid; 304 ne->ne_order = order; 305 ne->ne_num_labels = num_labels; 306 ne->ne_next = pl->names; 307 pl->names = ne; 308 309 return (0); 310 } 311 312 /* 313 * If we have a valid config but cannot read any of these fields, then 314 * it means we have a half-initialized label. In vdev_label_init() 315 * we write a label with txg == 0 so that we can identify the device 316 * in case the user refers to the same disk later on. If we fail to 317 * create the pool, we'll be left with a label in this state 318 * which should not be considered part of a valid pool. 319 */ 320 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 321 &pool_guid) != 0 || 322 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 323 &vdev_guid) != 0 || 324 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 325 &top_guid) != 0 || 326 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 327 &txg) != 0 || txg == 0) { 328 return (0); 329 } 330 331 /* 332 * First, see if we know about this pool. If not, then add it to the 333 * list of known pools. 334 */ 335 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 336 if (pe->pe_guid == pool_guid) 337 break; 338 } 339 340 if (pe == NULL) { 341 if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 342 return (-1); 343 } 344 pe->pe_guid = pool_guid; 345 pe->pe_next = pl->pools; 346 pl->pools = pe; 347 } 348 349 /* 350 * Second, see if we know about this toplevel vdev. Add it if its 351 * missing. 352 */ 353 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 354 if (ve->ve_guid == top_guid) 355 break; 356 } 357 358 if (ve == NULL) { 359 if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 360 return (-1); 361 } 362 ve->ve_guid = top_guid; 363 ve->ve_next = pe->pe_vdevs; 364 pe->pe_vdevs = ve; 365 } 366 367 /* 368 * Third, see if we have a config with a matching transaction group. If 369 * so, then we do nothing. Otherwise, add it to the list of known 370 * configs. 371 */ 372 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 373 if (ce->ce_txg == txg) 374 break; 375 } 376 377 if (ce == NULL) { 378 if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) { 379 return (-1); 380 } 381 ce->ce_txg = txg; 382 ce->ce_config = fnvlist_dup(config); 383 ce->ce_next = ve->ve_configs; 384 ve->ve_configs = ce; 385 } 386 387 /* 388 * At this point we've successfully added our config to the list of 389 * known configs. The last thing to do is add the vdev guid -> path 390 * mappings so that we can fix up the configuration as necessary before 391 * doing the import. 392 */ 393 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL) 394 return (-1); 395 396 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) { 397 free(ne); 398 return (-1); 399 } 400 401 ne->ne_guid = vdev_guid; 402 ne->ne_order = order; 403 ne->ne_num_labels = num_labels; 404 ne->ne_next = pl->names; 405 pl->names = ne; 406 407 return (0); 408 } 409 410 static int 411 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid, 412 boolean_t *isactive) 413 { 414 ASSERT(hdl->lpc_ops->pco_pool_active != NULL); 415 416 int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name, 417 guid, isactive); 418 419 return (error); 420 } 421 422 static nvlist_t * 423 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig) 424 { 425 ASSERT(hdl->lpc_ops->pco_refresh_config != NULL); 426 427 return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle, 428 tryconfig)); 429 } 430 431 /* 432 * Determine if the vdev id is a hole in the namespace. 433 */ 434 static boolean_t 435 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 436 { 437 int c; 438 439 for (c = 0; c < holes; c++) { 440 441 /* Top-level is a hole */ 442 if (hole_array[c] == id) 443 return (B_TRUE); 444 } 445 return (B_FALSE); 446 } 447 448 /* 449 * Convert our list of pools into the definitive set of configurations. We 450 * start by picking the best config for each toplevel vdev. Once that's done, 451 * we assemble the toplevel vdevs into a full config for the pool. We make a 452 * pass to fix up any incorrect paths, and then add it to the main list to 453 * return to the user. 454 */ 455 static nvlist_t * 456 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok, 457 nvlist_t *policy) 458 { 459 pool_entry_t *pe; 460 vdev_entry_t *ve; 461 config_entry_t *ce; 462 nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; 463 nvlist_t **spares, **l2cache; 464 uint_t i, nspares, nl2cache; 465 boolean_t config_seen; 466 uint64_t best_txg; 467 char *name, *hostname = NULL; 468 uint64_t guid; 469 uint_t children = 0; 470 nvlist_t **child = NULL; 471 uint_t holes; 472 uint64_t *hole_array, max_id; 473 uint_t c; 474 boolean_t isactive; 475 uint64_t hostid; 476 nvlist_t *nvl; 477 boolean_t valid_top_config = B_FALSE; 478 479 if (nvlist_alloc(&ret, 0, 0) != 0) 480 goto nomem; 481 482 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 483 uint64_t id, max_txg = 0; 484 485 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 486 goto nomem; 487 config_seen = B_FALSE; 488 489 /* 490 * Iterate over all toplevel vdevs. Grab the pool configuration 491 * from the first one we find, and then go through the rest and 492 * add them as necessary to the 'vdevs' member of the config. 493 */ 494 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 495 496 /* 497 * Determine the best configuration for this vdev by 498 * selecting the config with the latest transaction 499 * group. 500 */ 501 best_txg = 0; 502 for (ce = ve->ve_configs; ce != NULL; 503 ce = ce->ce_next) { 504 505 if (ce->ce_txg > best_txg) { 506 tmp = ce->ce_config; 507 best_txg = ce->ce_txg; 508 } 509 } 510 511 /* 512 * We rely on the fact that the max txg for the 513 * pool will contain the most up-to-date information 514 * about the valid top-levels in the vdev namespace. 515 */ 516 if (best_txg > max_txg) { 517 (void) nvlist_remove(config, 518 ZPOOL_CONFIG_VDEV_CHILDREN, 519 DATA_TYPE_UINT64); 520 (void) nvlist_remove(config, 521 ZPOOL_CONFIG_HOLE_ARRAY, 522 DATA_TYPE_UINT64_ARRAY); 523 524 max_txg = best_txg; 525 hole_array = NULL; 526 holes = 0; 527 max_id = 0; 528 valid_top_config = B_FALSE; 529 530 if (nvlist_lookup_uint64(tmp, 531 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 532 verify(nvlist_add_uint64(config, 533 ZPOOL_CONFIG_VDEV_CHILDREN, 534 max_id) == 0); 535 valid_top_config = B_TRUE; 536 } 537 538 if (nvlist_lookup_uint64_array(tmp, 539 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 540 &holes) == 0) { 541 verify(nvlist_add_uint64_array(config, 542 ZPOOL_CONFIG_HOLE_ARRAY, 543 hole_array, holes) == 0); 544 } 545 } 546 547 if (!config_seen) { 548 /* 549 * Copy the relevant pieces of data to the pool 550 * configuration: 551 * 552 * version 553 * pool guid 554 * name 555 * comment (if available) 556 * compatibility features (if available) 557 * pool state 558 * hostid (if available) 559 * hostname (if available) 560 */ 561 uint64_t state, version; 562 char *comment = NULL; 563 char *compatibility = NULL; 564 565 version = fnvlist_lookup_uint64(tmp, 566 ZPOOL_CONFIG_VERSION); 567 fnvlist_add_uint64(config, 568 ZPOOL_CONFIG_VERSION, version); 569 guid = fnvlist_lookup_uint64(tmp, 570 ZPOOL_CONFIG_POOL_GUID); 571 fnvlist_add_uint64(config, 572 ZPOOL_CONFIG_POOL_GUID, guid); 573 name = fnvlist_lookup_string(tmp, 574 ZPOOL_CONFIG_POOL_NAME); 575 fnvlist_add_string(config, 576 ZPOOL_CONFIG_POOL_NAME, name); 577 578 if (nvlist_lookup_string(tmp, 579 ZPOOL_CONFIG_COMMENT, &comment) == 0) 580 fnvlist_add_string(config, 581 ZPOOL_CONFIG_COMMENT, comment); 582 583 if (nvlist_lookup_string(tmp, 584 ZPOOL_CONFIG_COMPATIBILITY, 585 &compatibility) == 0) 586 fnvlist_add_string(config, 587 ZPOOL_CONFIG_COMPATIBILITY, 588 compatibility); 589 590 state = fnvlist_lookup_uint64(tmp, 591 ZPOOL_CONFIG_POOL_STATE); 592 fnvlist_add_uint64(config, 593 ZPOOL_CONFIG_POOL_STATE, state); 594 595 hostid = 0; 596 if (nvlist_lookup_uint64(tmp, 597 ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 598 fnvlist_add_uint64(config, 599 ZPOOL_CONFIG_HOSTID, hostid); 600 hostname = fnvlist_lookup_string(tmp, 601 ZPOOL_CONFIG_HOSTNAME); 602 fnvlist_add_string(config, 603 ZPOOL_CONFIG_HOSTNAME, hostname); 604 } 605 606 config_seen = B_TRUE; 607 } 608 609 /* 610 * Add this top-level vdev to the child array. 611 */ 612 verify(nvlist_lookup_nvlist(tmp, 613 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 614 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 615 &id) == 0); 616 617 if (id >= children) { 618 nvlist_t **newchild; 619 620 newchild = zutil_alloc(hdl, (id + 1) * 621 sizeof (nvlist_t *)); 622 if (newchild == NULL) 623 goto nomem; 624 625 for (c = 0; c < children; c++) 626 newchild[c] = child[c]; 627 628 free(child); 629 child = newchild; 630 children = id + 1; 631 } 632 if (nvlist_dup(nvtop, &child[id], 0) != 0) 633 goto nomem; 634 635 } 636 637 /* 638 * If we have information about all the top-levels then 639 * clean up the nvlist which we've constructed. This 640 * means removing any extraneous devices that are 641 * beyond the valid range or adding devices to the end 642 * of our array which appear to be missing. 643 */ 644 if (valid_top_config) { 645 if (max_id < children) { 646 for (c = max_id; c < children; c++) 647 nvlist_free(child[c]); 648 children = max_id; 649 } else if (max_id > children) { 650 nvlist_t **newchild; 651 652 newchild = zutil_alloc(hdl, (max_id) * 653 sizeof (nvlist_t *)); 654 if (newchild == NULL) 655 goto nomem; 656 657 for (c = 0; c < children; c++) 658 newchild[c] = child[c]; 659 660 free(child); 661 child = newchild; 662 children = max_id; 663 } 664 } 665 666 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 667 &guid) == 0); 668 669 /* 670 * The vdev namespace may contain holes as a result of 671 * device removal. We must add them back into the vdev 672 * tree before we process any missing devices. 673 */ 674 if (holes > 0) { 675 ASSERT(valid_top_config); 676 677 for (c = 0; c < children; c++) { 678 nvlist_t *holey; 679 680 if (child[c] != NULL || 681 !vdev_is_hole(hole_array, holes, c)) 682 continue; 683 684 if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 685 0) != 0) 686 goto nomem; 687 688 /* 689 * Holes in the namespace are treated as 690 * "hole" top-level vdevs and have a 691 * special flag set on them. 692 */ 693 if (nvlist_add_string(holey, 694 ZPOOL_CONFIG_TYPE, 695 VDEV_TYPE_HOLE) != 0 || 696 nvlist_add_uint64(holey, 697 ZPOOL_CONFIG_ID, c) != 0 || 698 nvlist_add_uint64(holey, 699 ZPOOL_CONFIG_GUID, 0ULL) != 0) { 700 nvlist_free(holey); 701 goto nomem; 702 } 703 child[c] = holey; 704 } 705 } 706 707 /* 708 * Look for any missing top-level vdevs. If this is the case, 709 * create a faked up 'missing' vdev as a placeholder. We cannot 710 * simply compress the child array, because the kernel performs 711 * certain checks to make sure the vdev IDs match their location 712 * in the configuration. 713 */ 714 for (c = 0; c < children; c++) { 715 if (child[c] == NULL) { 716 nvlist_t *missing; 717 if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 718 0) != 0) 719 goto nomem; 720 if (nvlist_add_string(missing, 721 ZPOOL_CONFIG_TYPE, 722 VDEV_TYPE_MISSING) != 0 || 723 nvlist_add_uint64(missing, 724 ZPOOL_CONFIG_ID, c) != 0 || 725 nvlist_add_uint64(missing, 726 ZPOOL_CONFIG_GUID, 0ULL) != 0) { 727 nvlist_free(missing); 728 goto nomem; 729 } 730 child[c] = missing; 731 } 732 } 733 734 /* 735 * Put all of this pool's top-level vdevs into a root vdev. 736 */ 737 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 738 goto nomem; 739 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 740 VDEV_TYPE_ROOT) != 0 || 741 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 742 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 743 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 744 child, children) != 0) { 745 nvlist_free(nvroot); 746 goto nomem; 747 } 748 749 for (c = 0; c < children; c++) 750 nvlist_free(child[c]); 751 free(child); 752 children = 0; 753 child = NULL; 754 755 /* 756 * Go through and fix up any paths and/or devids based on our 757 * known list of vdev GUID -> path mappings. 758 */ 759 if (fix_paths(hdl, nvroot, pl->names) != 0) { 760 nvlist_free(nvroot); 761 goto nomem; 762 } 763 764 /* 765 * Add the root vdev to this pool's configuration. 766 */ 767 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 768 nvroot) != 0) { 769 nvlist_free(nvroot); 770 goto nomem; 771 } 772 nvlist_free(nvroot); 773 774 /* 775 * zdb uses this path to report on active pools that were 776 * imported or created using -R. 777 */ 778 if (active_ok) 779 goto add_pool; 780 781 /* 782 * Determine if this pool is currently active, in which case we 783 * can't actually import it. 784 */ 785 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 786 &name) == 0); 787 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 788 &guid) == 0); 789 790 if (zutil_pool_active(hdl, name, guid, &isactive) != 0) 791 goto error; 792 793 if (isactive) { 794 nvlist_free(config); 795 config = NULL; 796 continue; 797 } 798 799 if (policy != NULL) { 800 if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 801 policy) != 0) 802 goto nomem; 803 } 804 805 if ((nvl = zutil_refresh_config(hdl, config)) == NULL) { 806 nvlist_free(config); 807 config = NULL; 808 continue; 809 } 810 811 nvlist_free(config); 812 config = nvl; 813 814 /* 815 * Go through and update the paths for spares, now that we have 816 * them. 817 */ 818 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 819 &nvroot) == 0); 820 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 821 &spares, &nspares) == 0) { 822 for (i = 0; i < nspares; i++) { 823 if (fix_paths(hdl, spares[i], pl->names) != 0) 824 goto nomem; 825 } 826 } 827 828 /* 829 * Update the paths for l2cache devices. 830 */ 831 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 832 &l2cache, &nl2cache) == 0) { 833 for (i = 0; i < nl2cache; i++) { 834 if (fix_paths(hdl, l2cache[i], pl->names) != 0) 835 goto nomem; 836 } 837 } 838 839 /* 840 * Restore the original information read from the actual label. 841 */ 842 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 843 DATA_TYPE_UINT64); 844 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 845 DATA_TYPE_STRING); 846 if (hostid != 0) { 847 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 848 hostid) == 0); 849 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 850 hostname) == 0); 851 } 852 853 add_pool: 854 /* 855 * Add this pool to the list of configs. 856 */ 857 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 858 &name) == 0); 859 860 if (nvlist_add_nvlist(ret, name, config) != 0) 861 goto nomem; 862 863 nvlist_free(config); 864 config = NULL; 865 } 866 867 return (ret); 868 869 nomem: 870 (void) zutil_no_memory(hdl); 871 error: 872 nvlist_free(config); 873 nvlist_free(ret); 874 for (c = 0; c < children; c++) 875 nvlist_free(child[c]); 876 free(child); 877 878 return (NULL); 879 } 880 881 /* 882 * Return the offset of the given label. 883 */ 884 static uint64_t 885 label_offset(uint64_t size, int l) 886 { 887 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 888 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 889 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 890 } 891 892 /* 893 * Given a file descriptor, read the label information and return an nvlist 894 * describing the configuration, if there is one. The number of valid 895 * labels found will be returned in num_labels when non-NULL. 896 */ 897 int 898 zpool_read_label(int fd, nvlist_t **config, int *num_labels) 899 { 900 struct stat64 statbuf; 901 struct aiocb aiocbs[VDEV_LABELS]; 902 struct aiocb *aiocbps[VDEV_LABELS]; 903 vdev_phys_t *labels; 904 nvlist_t *expected_config = NULL; 905 uint64_t expected_guid = 0, size; 906 int error, l, count = 0; 907 908 *config = NULL; 909 910 if (fstat64_blk(fd, &statbuf) == -1) 911 return (0); 912 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 913 914 error = posix_memalign((void **)&labels, PAGESIZE, 915 VDEV_LABELS * sizeof (*labels)); 916 if (error) 917 return (-1); 918 919 memset(aiocbs, 0, sizeof (aiocbs)); 920 for (l = 0; l < VDEV_LABELS; l++) { 921 off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE; 922 923 aiocbs[l].aio_fildes = fd; 924 aiocbs[l].aio_offset = offset; 925 aiocbs[l].aio_buf = &labels[l]; 926 aiocbs[l].aio_nbytes = sizeof (vdev_phys_t); 927 aiocbs[l].aio_lio_opcode = LIO_READ; 928 aiocbps[l] = &aiocbs[l]; 929 } 930 931 if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) { 932 int saved_errno = errno; 933 934 if (errno == EAGAIN || errno == EINTR || errno == EIO) { 935 /* 936 * A portion of the requests may have been submitted. 937 * Clean them up. 938 */ 939 for (l = 0; l < VDEV_LABELS; l++) { 940 errno = 0; 941 int r = aio_error(&aiocbs[l]); 942 if (r != EINVAL) 943 (void) aio_return(&aiocbs[l]); 944 } 945 } 946 free(labels); 947 errno = saved_errno; 948 return (-1); 949 } 950 951 for (l = 0; l < VDEV_LABELS; l++) { 952 uint64_t state, guid, txg; 953 954 if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t)) 955 continue; 956 957 if (nvlist_unpack(labels[l].vp_nvlist, 958 sizeof (labels[l].vp_nvlist), config, 0) != 0) 959 continue; 960 961 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, 962 &guid) != 0 || guid == 0) { 963 nvlist_free(*config); 964 continue; 965 } 966 967 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 968 &state) != 0 || state > POOL_STATE_L2CACHE) { 969 nvlist_free(*config); 970 continue; 971 } 972 973 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 974 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 975 &txg) != 0 || txg == 0)) { 976 nvlist_free(*config); 977 continue; 978 } 979 980 if (expected_guid) { 981 if (expected_guid == guid) 982 count++; 983 984 nvlist_free(*config); 985 } else { 986 expected_config = *config; 987 expected_guid = guid; 988 count++; 989 } 990 } 991 992 if (num_labels != NULL) 993 *num_labels = count; 994 995 free(labels); 996 *config = expected_config; 997 998 return (0); 999 } 1000 1001 /* 1002 * Sorted by full path and then vdev guid to allow for multiple entries with 1003 * the same full path name. This is required because it's possible to 1004 * have multiple block devices with labels that refer to the same 1005 * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both 1006 * entries need to be added to the cache. Scenarios where this can occur 1007 * include overwritten pool labels, devices which are visible from multiple 1008 * hosts and multipath devices. 1009 */ 1010 int 1011 slice_cache_compare(const void *arg1, const void *arg2) 1012 { 1013 const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; 1014 const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; 1015 uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid; 1016 uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid; 1017 int rv; 1018 1019 rv = TREE_ISIGN(strcmp(nm1, nm2)); 1020 if (rv) 1021 return (rv); 1022 1023 return (TREE_CMP(guid1, guid2)); 1024 } 1025 1026 static int 1027 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid, 1028 uint64_t vdev_guid, char **path, char **devid) 1029 { 1030 nvlist_t **child; 1031 uint_t c, children; 1032 uint64_t guid; 1033 char *val; 1034 int error; 1035 1036 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1037 &child, &children) == 0) { 1038 for (c = 0; c < children; c++) { 1039 error = label_paths_impl(hdl, child[c], 1040 pool_guid, vdev_guid, path, devid); 1041 if (error) 1042 return (error); 1043 } 1044 return (0); 1045 } 1046 1047 if (nvroot == NULL) 1048 return (0); 1049 1050 error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid); 1051 if ((error != 0) || (guid != vdev_guid)) 1052 return (0); 1053 1054 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val); 1055 if (error == 0) 1056 *path = val; 1057 1058 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val); 1059 if (error == 0) 1060 *devid = val; 1061 1062 return (0); 1063 } 1064 1065 /* 1066 * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID 1067 * and store these strings as config_path and devid_path respectively. 1068 * The returned pointers are only valid as long as label remains valid. 1069 */ 1070 int 1071 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid) 1072 { 1073 nvlist_t *nvroot; 1074 uint64_t pool_guid; 1075 uint64_t vdev_guid; 1076 1077 *path = NULL; 1078 *devid = NULL; 1079 1080 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1081 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) || 1082 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid)) 1083 return (ENOENT); 1084 1085 return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path, 1086 devid)); 1087 } 1088 1089 static void 1090 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock, 1091 avl_tree_t *cache, const char *path, const char *name, int order) 1092 { 1093 avl_index_t where; 1094 rdsk_node_t *slice; 1095 1096 slice = zutil_alloc(hdl, sizeof (rdsk_node_t)); 1097 if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) { 1098 free(slice); 1099 return; 1100 } 1101 slice->rn_vdev_guid = 0; 1102 slice->rn_lock = lock; 1103 slice->rn_avl = cache; 1104 slice->rn_hdl = hdl; 1105 slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET; 1106 slice->rn_labelpaths = B_FALSE; 1107 1108 pthread_mutex_lock(lock); 1109 if (avl_find(cache, slice, &where)) { 1110 free(slice->rn_name); 1111 free(slice); 1112 } else { 1113 avl_insert(cache, slice, where); 1114 } 1115 pthread_mutex_unlock(lock); 1116 } 1117 1118 static int 1119 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock, 1120 avl_tree_t *cache, const char *dir, int order) 1121 { 1122 int error; 1123 char path[MAXPATHLEN]; 1124 struct dirent64 *dp; 1125 DIR *dirp; 1126 1127 if (realpath(dir, path) == NULL) { 1128 error = errno; 1129 if (error == ENOENT) 1130 return (0); 1131 1132 zutil_error_aux(hdl, strerror(error)); 1133 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1134 TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1135 return (error); 1136 } 1137 1138 dirp = opendir(path); 1139 if (dirp == NULL) { 1140 error = errno; 1141 zutil_error_aux(hdl, strerror(error)); 1142 (void) zutil_error_fmt(hdl, EZFS_BADPATH, 1143 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 1144 return (error); 1145 } 1146 1147 while ((dp = readdir64(dirp)) != NULL) { 1148 const char *name = dp->d_name; 1149 if (name[0] == '.' && 1150 (name[1] == 0 || (name[1] == '.' && name[2] == 0))) 1151 continue; 1152 1153 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, 1154 order); 1155 } 1156 1157 (void) closedir(dirp); 1158 return (0); 1159 } 1160 1161 static int 1162 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock, 1163 avl_tree_t *cache, const char *dir, int order) 1164 { 1165 int error = 0; 1166 char path[MAXPATHLEN]; 1167 char *d, *b; 1168 char *dpath, *name; 1169 1170 /* 1171 * Separate the directory part and last part of the 1172 * path. We do this so that we can get the realpath of 1173 * the directory. We don't get the realpath on the 1174 * whole path because if it's a symlink, we want the 1175 * path of the symlink not where it points to. 1176 */ 1177 d = zutil_strdup(hdl, dir); 1178 b = zutil_strdup(hdl, dir); 1179 dpath = dirname(d); 1180 name = basename(b); 1181 1182 if (realpath(dpath, path) == NULL) { 1183 error = errno; 1184 if (error == ENOENT) { 1185 error = 0; 1186 goto out; 1187 } 1188 1189 zutil_error_aux(hdl, strerror(error)); 1190 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1191 TEXT_DOMAIN, "cannot resolve path '%s'"), dir); 1192 goto out; 1193 } 1194 1195 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order); 1196 1197 out: 1198 free(b); 1199 free(d); 1200 return (error); 1201 } 1202 1203 /* 1204 * Scan a list of directories for zfs devices. 1205 */ 1206 static int 1207 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock, 1208 avl_tree_t **slice_cache, const char * const *dir, size_t dirs) 1209 { 1210 avl_tree_t *cache; 1211 rdsk_node_t *slice; 1212 void *cookie; 1213 int i, error; 1214 1215 *slice_cache = NULL; 1216 cache = zutil_alloc(hdl, sizeof (avl_tree_t)); 1217 avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t), 1218 offsetof(rdsk_node_t, rn_node)); 1219 1220 for (i = 0; i < dirs; i++) { 1221 struct stat sbuf; 1222 1223 if (stat(dir[i], &sbuf) != 0) { 1224 error = errno; 1225 if (error == ENOENT) 1226 continue; 1227 1228 zutil_error_aux(hdl, strerror(error)); 1229 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext( 1230 TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]); 1231 goto error; 1232 } 1233 1234 /* 1235 * If dir[i] is a directory, we walk through it and add all 1236 * the entries to the cache. If it's not a directory, we just 1237 * add it to the cache. 1238 */ 1239 if (S_ISDIR(sbuf.st_mode)) { 1240 if ((error = zpool_find_import_scan_dir(hdl, lock, 1241 cache, dir[i], i)) != 0) 1242 goto error; 1243 } else { 1244 if ((error = zpool_find_import_scan_path(hdl, lock, 1245 cache, dir[i], i)) != 0) 1246 goto error; 1247 } 1248 } 1249 1250 *slice_cache = cache; 1251 return (0); 1252 1253 error: 1254 cookie = NULL; 1255 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1256 free(slice->rn_name); 1257 free(slice); 1258 } 1259 free(cache); 1260 1261 return (error); 1262 } 1263 1264 /* 1265 * Given a list of directories to search, find all pools stored on disk. This 1266 * includes partial pools which are not available to import. If no args are 1267 * given (argc is 0), then the default directory (/dev/dsk) is searched. 1268 * poolname or guid (but not both) are provided by the caller when trying 1269 * to import a specific pool. 1270 */ 1271 static nvlist_t * 1272 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg) 1273 { 1274 nvlist_t *ret = NULL; 1275 pool_list_t pools = { 0 }; 1276 pool_entry_t *pe, *penext; 1277 vdev_entry_t *ve, *venext; 1278 config_entry_t *ce, *cenext; 1279 name_entry_t *ne, *nenext; 1280 pthread_mutex_t lock; 1281 avl_tree_t *cache; 1282 rdsk_node_t *slice; 1283 void *cookie; 1284 tpool_t *t; 1285 1286 verify(iarg->poolname == NULL || iarg->guid == 0); 1287 pthread_mutex_init(&lock, NULL); 1288 1289 /* 1290 * Locate pool member vdevs by blkid or by directory scanning. 1291 * On success a newly allocated AVL tree which is populated with an 1292 * entry for each discovered vdev will be returned in the cache. 1293 * It's the caller's responsibility to consume and destroy this tree. 1294 */ 1295 if (iarg->scan || iarg->paths != 0) { 1296 size_t dirs = iarg->paths; 1297 const char * const *dir = (const char * const *)iarg->path; 1298 1299 if (dirs == 0) 1300 dir = zpool_default_search_paths(&dirs); 1301 1302 if (zpool_find_import_scan(hdl, &lock, &cache, dir, dirs) != 0) 1303 return (NULL); 1304 } else { 1305 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) 1306 return (NULL); 1307 } 1308 1309 /* 1310 * Create a thread pool to parallelize the process of reading and 1311 * validating labels, a large number of threads can be used due to 1312 * minimal contention. 1313 */ 1314 t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL); 1315 for (slice = avl_first(cache); slice; 1316 (slice = avl_walk(cache, slice, AVL_AFTER))) 1317 (void) tpool_dispatch(t, zpool_open_func, slice); 1318 1319 tpool_wait(t); 1320 tpool_destroy(t); 1321 1322 /* 1323 * Process the cache, filtering out any entries which are not 1324 * for the specified pool then adding matching label configs. 1325 */ 1326 cookie = NULL; 1327 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { 1328 if (slice->rn_config != NULL) { 1329 nvlist_t *config = slice->rn_config; 1330 boolean_t matched = B_TRUE; 1331 boolean_t aux = B_FALSE; 1332 int fd; 1333 1334 /* 1335 * Check if it's a spare or l2cache device. If it is, 1336 * we need to skip the name and guid check since they 1337 * don't exist on aux device label. 1338 */ 1339 if (iarg->poolname != NULL || iarg->guid != 0) { 1340 uint64_t state; 1341 aux = nvlist_lookup_uint64(config, 1342 ZPOOL_CONFIG_POOL_STATE, &state) == 0 && 1343 (state == POOL_STATE_SPARE || 1344 state == POOL_STATE_L2CACHE); 1345 } 1346 1347 if (iarg->poolname != NULL && !aux) { 1348 char *pname; 1349 1350 matched = nvlist_lookup_string(config, 1351 ZPOOL_CONFIG_POOL_NAME, &pname) == 0 && 1352 strcmp(iarg->poolname, pname) == 0; 1353 } else if (iarg->guid != 0 && !aux) { 1354 uint64_t this_guid; 1355 1356 matched = nvlist_lookup_uint64(config, 1357 ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 && 1358 iarg->guid == this_guid; 1359 } 1360 if (matched) { 1361 /* 1362 * Verify all remaining entries can be opened 1363 * exclusively. This will prune all underlying 1364 * multipath devices which otherwise could 1365 * result in the vdev appearing as UNAVAIL. 1366 * 1367 * Under zdb, this step isn't required and 1368 * would prevent a zdb -e of active pools with 1369 * no cachefile. 1370 */ 1371 fd = open(slice->rn_name, O_RDONLY | O_EXCL); 1372 if (fd >= 0 || iarg->can_be_active) { 1373 if (fd >= 0) 1374 close(fd); 1375 add_config(hdl, &pools, 1376 slice->rn_name, slice->rn_order, 1377 slice->rn_num_labels, config); 1378 } 1379 } 1380 nvlist_free(config); 1381 } 1382 free(slice->rn_name); 1383 free(slice); 1384 } 1385 avl_destroy(cache); 1386 free(cache); 1387 pthread_mutex_destroy(&lock); 1388 1389 ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy); 1390 1391 for (pe = pools.pools; pe != NULL; pe = penext) { 1392 penext = pe->pe_next; 1393 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 1394 venext = ve->ve_next; 1395 for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 1396 cenext = ce->ce_next; 1397 nvlist_free(ce->ce_config); 1398 free(ce); 1399 } 1400 free(ve); 1401 } 1402 free(pe); 1403 } 1404 1405 for (ne = pools.names; ne != NULL; ne = nenext) { 1406 nenext = ne->ne_next; 1407 free(ne->ne_name); 1408 free(ne); 1409 } 1410 1411 return (ret); 1412 } 1413 1414 /* 1415 * Given a cache file, return the contents as a list of importable pools. 1416 * poolname or guid (but not both) are provided by the caller when trying 1417 * to import a specific pool. 1418 */ 1419 static nvlist_t * 1420 zpool_find_import_cached(libpc_handle_t *hdl, const char *cachefile, 1421 const char *poolname, uint64_t guid) 1422 { 1423 char *buf; 1424 int fd; 1425 struct stat64 statbuf; 1426 nvlist_t *raw, *src, *dst; 1427 nvlist_t *pools; 1428 nvpair_t *elem; 1429 char *name; 1430 uint64_t this_guid; 1431 boolean_t active; 1432 1433 verify(poolname == NULL || guid == 0); 1434 1435 if ((fd = open(cachefile, O_RDONLY)) < 0) { 1436 zutil_error_aux(hdl, "%s", strerror(errno)); 1437 (void) zutil_error(hdl, EZFS_BADCACHE, 1438 dgettext(TEXT_DOMAIN, "failed to open cache file")); 1439 return (NULL); 1440 } 1441 1442 if (fstat64(fd, &statbuf) != 0) { 1443 zutil_error_aux(hdl, "%s", strerror(errno)); 1444 (void) close(fd); 1445 (void) zutil_error(hdl, EZFS_BADCACHE, 1446 dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 1447 return (NULL); 1448 } 1449 1450 if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) { 1451 (void) close(fd); 1452 return (NULL); 1453 } 1454 1455 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 1456 (void) close(fd); 1457 free(buf); 1458 (void) zutil_error(hdl, EZFS_BADCACHE, 1459 dgettext(TEXT_DOMAIN, 1460 "failed to read cache file contents")); 1461 return (NULL); 1462 } 1463 1464 (void) close(fd); 1465 1466 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 1467 free(buf); 1468 (void) zutil_error(hdl, EZFS_BADCACHE, 1469 dgettext(TEXT_DOMAIN, 1470 "invalid or corrupt cache file contents")); 1471 return (NULL); 1472 } 1473 1474 free(buf); 1475 1476 /* 1477 * Go through and get the current state of the pools and refresh their 1478 * state. 1479 */ 1480 if (nvlist_alloc(&pools, 0, 0) != 0) { 1481 (void) zutil_no_memory(hdl); 1482 nvlist_free(raw); 1483 return (NULL); 1484 } 1485 1486 elem = NULL; 1487 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 1488 src = fnvpair_value_nvlist(elem); 1489 1490 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); 1491 if (poolname != NULL && strcmp(poolname, name) != 0) 1492 continue; 1493 1494 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); 1495 if (guid != 0 && guid != this_guid) 1496 continue; 1497 1498 if (zutil_pool_active(hdl, name, this_guid, &active) != 0) { 1499 nvlist_free(raw); 1500 nvlist_free(pools); 1501 return (NULL); 1502 } 1503 1504 if (active) 1505 continue; 1506 1507 if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, 1508 cachefile) != 0) { 1509 (void) zutil_no_memory(hdl); 1510 nvlist_free(raw); 1511 nvlist_free(pools); 1512 return (NULL); 1513 } 1514 1515 if ((dst = zutil_refresh_config(hdl, src)) == NULL) { 1516 nvlist_free(raw); 1517 nvlist_free(pools); 1518 return (NULL); 1519 } 1520 1521 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1522 (void) zutil_no_memory(hdl); 1523 nvlist_free(dst); 1524 nvlist_free(raw); 1525 nvlist_free(pools); 1526 return (NULL); 1527 } 1528 nvlist_free(dst); 1529 } 1530 1531 nvlist_free(raw); 1532 return (pools); 1533 } 1534 1535 nvlist_t * 1536 zpool_search_import(void *hdl, importargs_t *import, 1537 const pool_config_ops_t *pco) 1538 { 1539 libpc_handle_t handle = { 0 }; 1540 nvlist_t *pools = NULL; 1541 1542 handle.lpc_lib_handle = hdl; 1543 handle.lpc_ops = pco; 1544 handle.lpc_printerr = B_TRUE; 1545 1546 verify(import->poolname == NULL || import->guid == 0); 1547 1548 if (import->cachefile != NULL) 1549 pools = zpool_find_import_cached(&handle, import->cachefile, 1550 import->poolname, import->guid); 1551 else 1552 pools = zpool_find_import_impl(&handle, import); 1553 1554 if ((pools == NULL || nvlist_empty(pools)) && 1555 handle.lpc_open_access_error && geteuid() != 0) { 1556 (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN, 1557 "no pools found")); 1558 } 1559 1560 return (pools); 1561 } 1562 1563 static boolean_t 1564 pool_match(nvlist_t *cfg, char *tgt) 1565 { 1566 uint64_t v, guid = strtoull(tgt, NULL, 0); 1567 char *s; 1568 1569 if (guid != 0) { 1570 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 1571 return (v == guid); 1572 } else { 1573 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 1574 return (strcmp(s, tgt) == 0); 1575 } 1576 return (B_FALSE); 1577 } 1578 1579 int 1580 zpool_find_config(void *hdl, const char *target, nvlist_t **configp, 1581 importargs_t *args, const pool_config_ops_t *pco) 1582 { 1583 nvlist_t *pools; 1584 nvlist_t *match = NULL; 1585 nvlist_t *config = NULL; 1586 char *sepp = NULL; 1587 char sep = '\0'; 1588 int count = 0; 1589 char *targetdup = strdup(target); 1590 1591 *configp = NULL; 1592 1593 if ((sepp = strpbrk(targetdup, "/@")) != NULL) { 1594 sep = *sepp; 1595 *sepp = '\0'; 1596 } 1597 1598 pools = zpool_search_import(hdl, args, pco); 1599 1600 if (pools != NULL) { 1601 nvpair_t *elem = NULL; 1602 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 1603 VERIFY0(nvpair_value_nvlist(elem, &config)); 1604 if (pool_match(config, targetdup)) { 1605 count++; 1606 if (match != NULL) { 1607 /* multiple matches found */ 1608 continue; 1609 } else { 1610 match = fnvlist_dup(config); 1611 } 1612 } 1613 } 1614 fnvlist_free(pools); 1615 } 1616 1617 if (count == 0) { 1618 free(targetdup); 1619 return (ENOENT); 1620 } 1621 1622 if (count > 1) { 1623 free(targetdup); 1624 fnvlist_free(match); 1625 return (EINVAL); 1626 } 1627 1628 *configp = match; 1629 free(targetdup); 1630 1631 return (0); 1632 } 1633