1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <ctype.h> 27 #include <errno.h> 28 #include <devid.h> 29 #include <fcntl.h> 30 #include <libintl.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <strings.h> 34 #include <unistd.h> 35 #include <sys/efi_partition.h> 36 #include <sys/vtoc.h> 37 #include <sys/zfs_ioctl.h> 38 #include <dlfcn.h> 39 40 #include "zfs_namecheck.h" 41 #include "zfs_prop.h" 42 #include "libzfs_impl.h" 43 #include "zfs_comutil.h" 44 45 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 46 47 #define DISK_ROOT "/dev/dsk" 48 #define RDISK_ROOT "/dev/rdsk" 49 #define BACKUP_SLICE "s2" 50 51 /* 52 * ==================================================================== 53 * zpool property functions 54 * ==================================================================== 55 */ 56 57 static int 58 zpool_get_all_props(zpool_handle_t *zhp) 59 { 60 zfs_cmd_t zc = { 0 }; 61 libzfs_handle_t *hdl = zhp->zpool_hdl; 62 63 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 64 65 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 66 return (-1); 67 68 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 69 if (errno == ENOMEM) { 70 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 71 zcmd_free_nvlists(&zc); 72 return (-1); 73 } 74 } else { 75 zcmd_free_nvlists(&zc); 76 return (-1); 77 } 78 } 79 80 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 81 zcmd_free_nvlists(&zc); 82 return (-1); 83 } 84 85 zcmd_free_nvlists(&zc); 86 87 return (0); 88 } 89 90 static int 91 zpool_props_refresh(zpool_handle_t *zhp) 92 { 93 nvlist_t *old_props; 94 95 old_props = zhp->zpool_props; 96 97 if (zpool_get_all_props(zhp) != 0) 98 return (-1); 99 100 nvlist_free(old_props); 101 return (0); 102 } 103 104 static char * 105 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 106 zprop_source_t *src) 107 { 108 nvlist_t *nv, *nvl; 109 uint64_t ival; 110 char *value; 111 zprop_source_t source; 112 113 nvl = zhp->zpool_props; 114 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 115 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 116 source = ival; 117 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 118 } else { 119 source = ZPROP_SRC_DEFAULT; 120 if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 121 value = "-"; 122 } 123 124 if (src) 125 *src = source; 126 127 return (value); 128 } 129 130 uint64_t 131 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 132 { 133 nvlist_t *nv, *nvl; 134 uint64_t value; 135 zprop_source_t source; 136 137 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 138 /* 139 * zpool_get_all_props() has most likely failed because 140 * the pool is faulted, but if all we need is the top level 141 * vdev's guid then get it from the zhp config nvlist. 142 */ 143 if ((prop == ZPOOL_PROP_GUID) && 144 (nvlist_lookup_nvlist(zhp->zpool_config, 145 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 146 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 147 == 0)) { 148 return (value); 149 } 150 return (zpool_prop_default_numeric(prop)); 151 } 152 153 nvl = zhp->zpool_props; 154 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 155 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 156 source = value; 157 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 158 } else { 159 source = ZPROP_SRC_DEFAULT; 160 value = zpool_prop_default_numeric(prop); 161 } 162 163 if (src) 164 *src = source; 165 166 return (value); 167 } 168 169 /* 170 * Map VDEV STATE to printed strings. 171 */ 172 char * 173 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 174 { 175 switch (state) { 176 case VDEV_STATE_CLOSED: 177 case VDEV_STATE_OFFLINE: 178 return (gettext("OFFLINE")); 179 case VDEV_STATE_REMOVED: 180 return (gettext("REMOVED")); 181 case VDEV_STATE_CANT_OPEN: 182 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 183 return (gettext("FAULTED")); 184 else if (aux == VDEV_AUX_SPLIT_POOL) 185 return (gettext("SPLIT")); 186 else 187 return (gettext("UNAVAIL")); 188 case VDEV_STATE_FAULTED: 189 return (gettext("FAULTED")); 190 case VDEV_STATE_DEGRADED: 191 return (gettext("DEGRADED")); 192 case VDEV_STATE_HEALTHY: 193 return (gettext("ONLINE")); 194 } 195 196 return (gettext("UNKNOWN")); 197 } 198 199 /* 200 * Get a zpool property value for 'prop' and return the value in 201 * a pre-allocated buffer. 202 */ 203 int 204 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 205 zprop_source_t *srctype) 206 { 207 uint64_t intval; 208 const char *strval; 209 zprop_source_t src = ZPROP_SRC_NONE; 210 nvlist_t *nvroot; 211 vdev_stat_t *vs; 212 uint_t vsc; 213 214 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 215 switch (prop) { 216 case ZPOOL_PROP_NAME: 217 (void) strlcpy(buf, zpool_get_name(zhp), len); 218 break; 219 220 case ZPOOL_PROP_HEALTH: 221 (void) strlcpy(buf, "FAULTED", len); 222 break; 223 224 case ZPOOL_PROP_GUID: 225 intval = zpool_get_prop_int(zhp, prop, &src); 226 (void) snprintf(buf, len, "%llu", intval); 227 break; 228 229 case ZPOOL_PROP_ALTROOT: 230 case ZPOOL_PROP_CACHEFILE: 231 if (zhp->zpool_props != NULL || 232 zpool_get_all_props(zhp) == 0) { 233 (void) strlcpy(buf, 234 zpool_get_prop_string(zhp, prop, &src), 235 len); 236 if (srctype != NULL) 237 *srctype = src; 238 return (0); 239 } 240 /* FALLTHROUGH */ 241 default: 242 (void) strlcpy(buf, "-", len); 243 break; 244 } 245 246 if (srctype != NULL) 247 *srctype = src; 248 return (0); 249 } 250 251 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 252 prop != ZPOOL_PROP_NAME) 253 return (-1); 254 255 switch (zpool_prop_get_type(prop)) { 256 case PROP_TYPE_STRING: 257 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 258 len); 259 break; 260 261 case PROP_TYPE_NUMBER: 262 intval = zpool_get_prop_int(zhp, prop, &src); 263 264 switch (prop) { 265 case ZPOOL_PROP_SIZE: 266 case ZPOOL_PROP_ALLOCATED: 267 case ZPOOL_PROP_FREE: 268 (void) zfs_nicenum(intval, buf, len); 269 break; 270 271 case ZPOOL_PROP_CAPACITY: 272 (void) snprintf(buf, len, "%llu%%", 273 (u_longlong_t)intval); 274 break; 275 276 case ZPOOL_PROP_DEDUPRATIO: 277 (void) snprintf(buf, len, "%llu.%02llux", 278 (u_longlong_t)(intval / 100), 279 (u_longlong_t)(intval % 100)); 280 break; 281 282 case ZPOOL_PROP_HEALTH: 283 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 284 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 285 verify(nvlist_lookup_uint64_array(nvroot, 286 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 287 == 0); 288 289 (void) strlcpy(buf, zpool_state_to_name(intval, 290 vs->vs_aux), len); 291 break; 292 default: 293 (void) snprintf(buf, len, "%llu", intval); 294 } 295 break; 296 297 case PROP_TYPE_INDEX: 298 intval = zpool_get_prop_int(zhp, prop, &src); 299 if (zpool_prop_index_to_string(prop, intval, &strval) 300 != 0) 301 return (-1); 302 (void) strlcpy(buf, strval, len); 303 break; 304 305 default: 306 abort(); 307 } 308 309 if (srctype) 310 *srctype = src; 311 312 return (0); 313 } 314 315 /* 316 * Check if the bootfs name has the same pool name as it is set to. 317 * Assuming bootfs is a valid dataset name. 318 */ 319 static boolean_t 320 bootfs_name_valid(const char *pool, char *bootfs) 321 { 322 int len = strlen(pool); 323 324 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 325 return (B_FALSE); 326 327 if (strncmp(pool, bootfs, len) == 0 && 328 (bootfs[len] == '/' || bootfs[len] == '\0')) 329 return (B_TRUE); 330 331 return (B_FALSE); 332 } 333 334 /* 335 * Inspect the configuration to determine if any of the devices contain 336 * an EFI label. 337 */ 338 static boolean_t 339 pool_uses_efi(nvlist_t *config) 340 { 341 nvlist_t **child; 342 uint_t c, children; 343 344 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 345 &child, &children) != 0) 346 return (read_efi_label(config, NULL) >= 0); 347 348 for (c = 0; c < children; c++) { 349 if (pool_uses_efi(child[c])) 350 return (B_TRUE); 351 } 352 return (B_FALSE); 353 } 354 355 static boolean_t 356 pool_is_bootable(zpool_handle_t *zhp) 357 { 358 char bootfs[ZPOOL_MAXNAMELEN]; 359 360 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 361 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 362 sizeof (bootfs)) != 0); 363 } 364 365 366 /* 367 * Given an nvlist of zpool properties to be set, validate that they are 368 * correct, and parse any numeric properties (index, boolean, etc) if they are 369 * specified as strings. 370 */ 371 static nvlist_t * 372 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 373 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 374 { 375 nvpair_t *elem; 376 nvlist_t *retprops; 377 zpool_prop_t prop; 378 char *strval; 379 uint64_t intval; 380 char *slash; 381 struct stat64 statbuf; 382 zpool_handle_t *zhp; 383 nvlist_t *nvroot; 384 385 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 386 (void) no_memory(hdl); 387 return (NULL); 388 } 389 390 elem = NULL; 391 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 392 const char *propname = nvpair_name(elem); 393 394 /* 395 * Make sure this property is valid and applies to this type. 396 */ 397 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 399 "invalid property '%s'"), propname); 400 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 401 goto error; 402 } 403 404 if (zpool_prop_readonly(prop)) { 405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 406 "is readonly"), propname); 407 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 408 goto error; 409 } 410 411 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 412 &strval, &intval, errbuf) != 0) 413 goto error; 414 415 /* 416 * Perform additional checking for specific properties. 417 */ 418 switch (prop) { 419 case ZPOOL_PROP_VERSION: 420 if (intval < version || intval > SPA_VERSION) { 421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 422 "property '%s' number %d is invalid."), 423 propname, intval); 424 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 425 goto error; 426 } 427 break; 428 429 case ZPOOL_PROP_BOOTFS: 430 if (create_or_import) { 431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 432 "property '%s' cannot be set at creation " 433 "or import time"), propname); 434 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 435 goto error; 436 } 437 438 if (version < SPA_VERSION_BOOTFS) { 439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 440 "pool must be upgraded to support " 441 "'%s' property"), propname); 442 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 443 goto error; 444 } 445 446 /* 447 * bootfs property value has to be a dataset name and 448 * the dataset has to be in the same pool as it sets to. 449 */ 450 if (strval[0] != '\0' && !bootfs_name_valid(poolname, 451 strval)) { 452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 453 "is an invalid name"), strval); 454 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 455 goto error; 456 } 457 458 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 460 "could not open pool '%s'"), poolname); 461 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 462 goto error; 463 } 464 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 465 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 466 467 /* 468 * bootfs property cannot be set on a disk which has 469 * been EFI labeled. 470 */ 471 if (pool_uses_efi(nvroot)) { 472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 473 "property '%s' not supported on " 474 "EFI labeled devices"), propname); 475 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 476 zpool_close(zhp); 477 goto error; 478 } 479 zpool_close(zhp); 480 break; 481 482 case ZPOOL_PROP_ALTROOT: 483 if (!create_or_import) { 484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 485 "property '%s' can only be set during pool " 486 "creation or import"), propname); 487 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 488 goto error; 489 } 490 491 if (strval[0] != '/') { 492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 493 "bad alternate root '%s'"), strval); 494 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 495 goto error; 496 } 497 break; 498 499 case ZPOOL_PROP_CACHEFILE: 500 if (strval[0] == '\0') 501 break; 502 503 if (strcmp(strval, "none") == 0) 504 break; 505 506 if (strval[0] != '/') { 507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 508 "property '%s' must be empty, an " 509 "absolute path, or 'none'"), propname); 510 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 511 goto error; 512 } 513 514 slash = strrchr(strval, '/'); 515 516 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 517 strcmp(slash, "/..") == 0) { 518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 519 "'%s' is not a valid file"), strval); 520 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 521 goto error; 522 } 523 524 *slash = '\0'; 525 526 if (strval[0] != '\0' && 527 (stat64(strval, &statbuf) != 0 || 528 !S_ISDIR(statbuf.st_mode))) { 529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 530 "'%s' is not a valid directory"), 531 strval); 532 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 533 goto error; 534 } 535 536 *slash = '/'; 537 break; 538 } 539 } 540 541 return (retprops); 542 error: 543 nvlist_free(retprops); 544 return (NULL); 545 } 546 547 /* 548 * Set zpool property : propname=propval. 549 */ 550 int 551 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 552 { 553 zfs_cmd_t zc = { 0 }; 554 int ret = -1; 555 char errbuf[1024]; 556 nvlist_t *nvl = NULL; 557 nvlist_t *realprops; 558 uint64_t version; 559 560 (void) snprintf(errbuf, sizeof (errbuf), 561 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 562 zhp->zpool_name); 563 564 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 565 return (no_memory(zhp->zpool_hdl)); 566 567 if (nvlist_add_string(nvl, propname, propval) != 0) { 568 nvlist_free(nvl); 569 return (no_memory(zhp->zpool_hdl)); 570 } 571 572 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 573 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 574 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 575 nvlist_free(nvl); 576 return (-1); 577 } 578 579 nvlist_free(nvl); 580 nvl = realprops; 581 582 /* 583 * Execute the corresponding ioctl() to set this property. 584 */ 585 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 586 587 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 588 nvlist_free(nvl); 589 return (-1); 590 } 591 592 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 593 594 zcmd_free_nvlists(&zc); 595 nvlist_free(nvl); 596 597 if (ret) 598 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 599 else 600 (void) zpool_props_refresh(zhp); 601 602 return (ret); 603 } 604 605 int 606 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 607 { 608 libzfs_handle_t *hdl = zhp->zpool_hdl; 609 zprop_list_t *entry; 610 char buf[ZFS_MAXPROPLEN]; 611 612 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 613 return (-1); 614 615 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 616 617 if (entry->pl_fixed) 618 continue; 619 620 if (entry->pl_prop != ZPROP_INVAL && 621 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 622 NULL) == 0) { 623 if (strlen(buf) > entry->pl_width) 624 entry->pl_width = strlen(buf); 625 } 626 } 627 628 return (0); 629 } 630 631 632 /* 633 * Don't start the slice at the default block of 34; many storage 634 * devices will use a stripe width of 128k, so start there instead. 635 */ 636 #define NEW_START_BLOCK 256 637 638 /* 639 * Validate the given pool name, optionally putting an extended error message in 640 * 'buf'. 641 */ 642 boolean_t 643 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 644 { 645 namecheck_err_t why; 646 char what; 647 int ret; 648 649 ret = pool_namecheck(pool, &why, &what); 650 651 /* 652 * The rules for reserved pool names were extended at a later point. 653 * But we need to support users with existing pools that may now be 654 * invalid. So we only check for this expanded set of names during a 655 * create (or import), and only in userland. 656 */ 657 if (ret == 0 && !isopen && 658 (strncmp(pool, "mirror", 6) == 0 || 659 strncmp(pool, "raidz", 5) == 0 || 660 strncmp(pool, "spare", 5) == 0 || 661 strcmp(pool, "log") == 0)) { 662 if (hdl != NULL) 663 zfs_error_aux(hdl, 664 dgettext(TEXT_DOMAIN, "name is reserved")); 665 return (B_FALSE); 666 } 667 668 669 if (ret != 0) { 670 if (hdl != NULL) { 671 switch (why) { 672 case NAME_ERR_TOOLONG: 673 zfs_error_aux(hdl, 674 dgettext(TEXT_DOMAIN, "name is too long")); 675 break; 676 677 case NAME_ERR_INVALCHAR: 678 zfs_error_aux(hdl, 679 dgettext(TEXT_DOMAIN, "invalid character " 680 "'%c' in pool name"), what); 681 break; 682 683 case NAME_ERR_NOLETTER: 684 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 685 "name must begin with a letter")); 686 break; 687 688 case NAME_ERR_RESERVED: 689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 690 "name is reserved")); 691 break; 692 693 case NAME_ERR_DISKLIKE: 694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 695 "pool name is reserved")); 696 break; 697 698 case NAME_ERR_LEADING_SLASH: 699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 700 "leading slash in name")); 701 break; 702 703 case NAME_ERR_EMPTY_COMPONENT: 704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 705 "empty component in name")); 706 break; 707 708 case NAME_ERR_TRAILING_SLASH: 709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 710 "trailing slash in name")); 711 break; 712 713 case NAME_ERR_MULTIPLE_AT: 714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 715 "multiple '@' delimiters in name")); 716 break; 717 718 } 719 } 720 return (B_FALSE); 721 } 722 723 return (B_TRUE); 724 } 725 726 /* 727 * Open a handle to the given pool, even if the pool is currently in the FAULTED 728 * state. 729 */ 730 zpool_handle_t * 731 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 732 { 733 zpool_handle_t *zhp; 734 boolean_t missing; 735 736 /* 737 * Make sure the pool name is valid. 738 */ 739 if (!zpool_name_valid(hdl, B_TRUE, pool)) { 740 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 741 dgettext(TEXT_DOMAIN, "cannot open '%s'"), 742 pool); 743 return (NULL); 744 } 745 746 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 747 return (NULL); 748 749 zhp->zpool_hdl = hdl; 750 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 751 752 if (zpool_refresh_stats(zhp, &missing) != 0) { 753 zpool_close(zhp); 754 return (NULL); 755 } 756 757 if (missing) { 758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 759 (void) zfs_error_fmt(hdl, EZFS_NOENT, 760 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 761 zpool_close(zhp); 762 return (NULL); 763 } 764 765 return (zhp); 766 } 767 768 /* 769 * Like the above, but silent on error. Used when iterating over pools (because 770 * the configuration cache may be out of date). 771 */ 772 int 773 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 774 { 775 zpool_handle_t *zhp; 776 boolean_t missing; 777 778 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 779 return (-1); 780 781 zhp->zpool_hdl = hdl; 782 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 783 784 if (zpool_refresh_stats(zhp, &missing) != 0) { 785 zpool_close(zhp); 786 return (-1); 787 } 788 789 if (missing) { 790 zpool_close(zhp); 791 *ret = NULL; 792 return (0); 793 } 794 795 *ret = zhp; 796 return (0); 797 } 798 799 /* 800 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 801 * state. 802 */ 803 zpool_handle_t * 804 zpool_open(libzfs_handle_t *hdl, const char *pool) 805 { 806 zpool_handle_t *zhp; 807 808 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 809 return (NULL); 810 811 if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 812 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 813 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 814 zpool_close(zhp); 815 return (NULL); 816 } 817 818 return (zhp); 819 } 820 821 /* 822 * Close the handle. Simply frees the memory associated with the handle. 823 */ 824 void 825 zpool_close(zpool_handle_t *zhp) 826 { 827 if (zhp->zpool_config) 828 nvlist_free(zhp->zpool_config); 829 if (zhp->zpool_old_config) 830 nvlist_free(zhp->zpool_old_config); 831 if (zhp->zpool_props) 832 nvlist_free(zhp->zpool_props); 833 free(zhp); 834 } 835 836 /* 837 * Return the name of the pool. 838 */ 839 const char * 840 zpool_get_name(zpool_handle_t *zhp) 841 { 842 return (zhp->zpool_name); 843 } 844 845 846 /* 847 * Return the state of the pool (ACTIVE or UNAVAILABLE) 848 */ 849 int 850 zpool_get_state(zpool_handle_t *zhp) 851 { 852 return (zhp->zpool_state); 853 } 854 855 /* 856 * Create the named pool, using the provided vdev list. It is assumed 857 * that the consumer has already validated the contents of the nvlist, so we 858 * don't have to worry about error semantics. 859 */ 860 int 861 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 862 nvlist_t *props, nvlist_t *fsprops) 863 { 864 zfs_cmd_t zc = { 0 }; 865 nvlist_t *zc_fsprops = NULL; 866 nvlist_t *zc_props = NULL; 867 char msg[1024]; 868 char *altroot; 869 int ret = -1; 870 871 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 872 "cannot create '%s'"), pool); 873 874 if (!zpool_name_valid(hdl, B_FALSE, pool)) 875 return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 876 877 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 878 return (-1); 879 880 if (props) { 881 if ((zc_props = zpool_valid_proplist(hdl, pool, props, 882 SPA_VERSION_1, B_TRUE, msg)) == NULL) { 883 goto create_failed; 884 } 885 } 886 887 if (fsprops) { 888 uint64_t zoned; 889 char *zonestr; 890 891 zoned = ((nvlist_lookup_string(fsprops, 892 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 893 strcmp(zonestr, "on") == 0); 894 895 if ((zc_fsprops = zfs_valid_proplist(hdl, 896 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 897 goto create_failed; 898 } 899 if (!zc_props && 900 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 901 goto create_failed; 902 } 903 if (nvlist_add_nvlist(zc_props, 904 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 905 goto create_failed; 906 } 907 } 908 909 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 910 goto create_failed; 911 912 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 913 914 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 915 916 zcmd_free_nvlists(&zc); 917 nvlist_free(zc_props); 918 nvlist_free(zc_fsprops); 919 920 switch (errno) { 921 case EBUSY: 922 /* 923 * This can happen if the user has specified the same 924 * device multiple times. We can't reliably detect this 925 * until we try to add it and see we already have a 926 * label. 927 */ 928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 929 "one or more vdevs refer to the same device")); 930 return (zfs_error(hdl, EZFS_BADDEV, msg)); 931 932 case EOVERFLOW: 933 /* 934 * This occurs when one of the devices is below 935 * SPA_MINDEVSIZE. Unfortunately, we can't detect which 936 * device was the problem device since there's no 937 * reliable way to determine device size from userland. 938 */ 939 { 940 char buf[64]; 941 942 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 943 944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 945 "one or more devices is less than the " 946 "minimum size (%s)"), buf); 947 } 948 return (zfs_error(hdl, EZFS_BADDEV, msg)); 949 950 case ENOSPC: 951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 952 "one or more devices is out of space")); 953 return (zfs_error(hdl, EZFS_BADDEV, msg)); 954 955 case ENOTBLK: 956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 957 "cache device must be a disk or disk slice")); 958 return (zfs_error(hdl, EZFS_BADDEV, msg)); 959 960 default: 961 return (zpool_standard_error(hdl, errno, msg)); 962 } 963 } 964 965 /* 966 * If this is an alternate root pool, then we automatically set the 967 * mountpoint of the root dataset to be '/'. 968 */ 969 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 970 &altroot) == 0) { 971 zfs_handle_t *zhp; 972 973 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 974 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 975 "/") == 0); 976 977 zfs_close(zhp); 978 } 979 980 create_failed: 981 zcmd_free_nvlists(&zc); 982 nvlist_free(zc_props); 983 nvlist_free(zc_fsprops); 984 return (ret); 985 } 986 987 /* 988 * Destroy the given pool. It is up to the caller to ensure that there are no 989 * datasets left in the pool. 990 */ 991 int 992 zpool_destroy(zpool_handle_t *zhp) 993 { 994 zfs_cmd_t zc = { 0 }; 995 zfs_handle_t *zfp = NULL; 996 libzfs_handle_t *hdl = zhp->zpool_hdl; 997 char msg[1024]; 998 999 if (zhp->zpool_state == POOL_STATE_ACTIVE && 1000 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 1001 ZFS_TYPE_FILESYSTEM)) == NULL) 1002 return (-1); 1003 1004 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1005 1006 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 1007 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1008 "cannot destroy '%s'"), zhp->zpool_name); 1009 1010 if (errno == EROFS) { 1011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1012 "one or more devices is read only")); 1013 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1014 } else { 1015 (void) zpool_standard_error(hdl, errno, msg); 1016 } 1017 1018 if (zfp) 1019 zfs_close(zfp); 1020 return (-1); 1021 } 1022 1023 if (zfp) { 1024 remove_mountpoint(zfp); 1025 zfs_close(zfp); 1026 } 1027 1028 return (0); 1029 } 1030 1031 /* 1032 * Add the given vdevs to the pool. The caller must have already performed the 1033 * necessary verification to ensure that the vdev specification is well-formed. 1034 */ 1035 int 1036 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1037 { 1038 zfs_cmd_t zc = { 0 }; 1039 int ret; 1040 libzfs_handle_t *hdl = zhp->zpool_hdl; 1041 char msg[1024]; 1042 nvlist_t **spares, **l2cache; 1043 uint_t nspares, nl2cache; 1044 1045 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1046 "cannot add to '%s'"), zhp->zpool_name); 1047 1048 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1049 SPA_VERSION_SPARES && 1050 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1051 &spares, &nspares) == 0) { 1052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1053 "upgraded to add hot spares")); 1054 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1055 } 1056 1057 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 1058 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 1059 uint64_t s; 1060 1061 for (s = 0; s < nspares; s++) { 1062 char *path; 1063 1064 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 1065 &path) == 0 && pool_uses_efi(spares[s])) { 1066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1067 "device '%s' contains an EFI label and " 1068 "cannot be used on root pools."), 1069 zpool_vdev_name(hdl, NULL, spares[s], 1070 B_FALSE)); 1071 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1072 } 1073 } 1074 } 1075 1076 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1077 SPA_VERSION_L2CACHE && 1078 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1079 &l2cache, &nl2cache) == 0) { 1080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1081 "upgraded to add cache devices")); 1082 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1083 } 1084 1085 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1086 return (-1); 1087 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1088 1089 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1090 switch (errno) { 1091 case EBUSY: 1092 /* 1093 * This can happen if the user has specified the same 1094 * device multiple times. We can't reliably detect this 1095 * until we try to add it and see we already have a 1096 * label. 1097 */ 1098 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1099 "one or more vdevs refer to the same device")); 1100 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1101 break; 1102 1103 case EOVERFLOW: 1104 /* 1105 * This occurrs when one of the devices is below 1106 * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1107 * device was the problem device since there's no 1108 * reliable way to determine device size from userland. 1109 */ 1110 { 1111 char buf[64]; 1112 1113 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1114 1115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1116 "device is less than the minimum " 1117 "size (%s)"), buf); 1118 } 1119 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1120 break; 1121 1122 case ENOTSUP: 1123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1124 "pool must be upgraded to add these vdevs")); 1125 (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1126 break; 1127 1128 case EDOM: 1129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1130 "root pool can not have multiple vdevs" 1131 " or separate logs")); 1132 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1133 break; 1134 1135 case ENOTBLK: 1136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1137 "cache device must be a disk or disk slice")); 1138 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1139 break; 1140 1141 default: 1142 (void) zpool_standard_error(hdl, errno, msg); 1143 } 1144 1145 ret = -1; 1146 } else { 1147 ret = 0; 1148 } 1149 1150 zcmd_free_nvlists(&zc); 1151 1152 return (ret); 1153 } 1154 1155 /* 1156 * Exports the pool from the system. The caller must ensure that there are no 1157 * mounted datasets in the pool. 1158 */ 1159 int 1160 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1161 { 1162 zfs_cmd_t zc = { 0 }; 1163 char msg[1024]; 1164 1165 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1166 "cannot export '%s'"), zhp->zpool_name); 1167 1168 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1169 zc.zc_cookie = force; 1170 zc.zc_guid = hardforce; 1171 1172 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 1173 switch (errno) { 1174 case EXDEV: 1175 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 1176 "use '-f' to override the following errors:\n" 1177 "'%s' has an active shared spare which could be" 1178 " used by other pools once '%s' is exported."), 1179 zhp->zpool_name, zhp->zpool_name); 1180 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 1181 msg)); 1182 default: 1183 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 1184 msg)); 1185 } 1186 } 1187 1188 return (0); 1189 } 1190 1191 int 1192 zpool_export(zpool_handle_t *zhp, boolean_t force) 1193 { 1194 return (zpool_export_common(zhp, force, B_FALSE)); 1195 } 1196 1197 int 1198 zpool_export_force(zpool_handle_t *zhp) 1199 { 1200 return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1201 } 1202 1203 static void 1204 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 1205 nvlist_t *config) 1206 { 1207 nvlist_t *nv = NULL; 1208 uint64_t rewindto; 1209 int64_t loss = -1; 1210 struct tm t; 1211 char timestr[128]; 1212 1213 if (!hdl->libzfs_printerr || config == NULL) 1214 return; 1215 1216 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0) 1217 return; 1218 1219 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1220 return; 1221 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1222 1223 if (localtime_r((time_t *)&rewindto, &t) != NULL && 1224 strftime(timestr, 128, 0, &t) != 0) { 1225 if (dryrun) { 1226 (void) printf(dgettext(TEXT_DOMAIN, 1227 "Would be able to return %s " 1228 "to its state as of %s.\n"), 1229 name, timestr); 1230 } else { 1231 (void) printf(dgettext(TEXT_DOMAIN, 1232 "Pool %s returned to its state as of %s.\n"), 1233 name, timestr); 1234 } 1235 if (loss > 120) { 1236 (void) printf(dgettext(TEXT_DOMAIN, 1237 "%s approximately %lld "), 1238 dryrun ? "Would discard" : "Discarded", 1239 (loss + 30) / 60); 1240 (void) printf(dgettext(TEXT_DOMAIN, 1241 "minutes of transactions.\n")); 1242 } else if (loss > 0) { 1243 (void) printf(dgettext(TEXT_DOMAIN, 1244 "%s approximately %lld "), 1245 dryrun ? "Would discard" : "Discarded", loss); 1246 (void) printf(dgettext(TEXT_DOMAIN, 1247 "seconds of transactions.\n")); 1248 } 1249 } 1250 } 1251 1252 void 1253 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1254 nvlist_t *config) 1255 { 1256 nvlist_t *nv = NULL; 1257 int64_t loss = -1; 1258 uint64_t edata = UINT64_MAX; 1259 uint64_t rewindto; 1260 struct tm t; 1261 char timestr[128]; 1262 1263 if (!hdl->libzfs_printerr) 1264 return; 1265 1266 if (reason >= 0) 1267 (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1268 else 1269 (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1270 1271 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1272 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1273 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1274 goto no_info; 1275 1276 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1277 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1278 &edata); 1279 1280 (void) printf(dgettext(TEXT_DOMAIN, 1281 "Recovery is possible, but will result in some data loss.\n")); 1282 1283 if (localtime_r((time_t *)&rewindto, &t) != NULL && 1284 strftime(timestr, 128, 0, &t) != 0) { 1285 (void) printf(dgettext(TEXT_DOMAIN, 1286 "\tReturning the pool to its state as of %s\n" 1287 "\tshould correct the problem. "), 1288 timestr); 1289 } else { 1290 (void) printf(dgettext(TEXT_DOMAIN, 1291 "\tReverting the pool to an earlier state " 1292 "should correct the problem.\n\t")); 1293 } 1294 1295 if (loss > 120) { 1296 (void) printf(dgettext(TEXT_DOMAIN, 1297 "Approximately %lld minutes of data\n" 1298 "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1299 } else if (loss > 0) { 1300 (void) printf(dgettext(TEXT_DOMAIN, 1301 "Approximately %lld seconds of data\n" 1302 "\tmust be discarded, irreversibly. "), loss); 1303 } 1304 if (edata != 0 && edata != UINT64_MAX) { 1305 if (edata == 1) { 1306 (void) printf(dgettext(TEXT_DOMAIN, 1307 "After rewind, at least\n" 1308 "\tone persistent user-data error will remain. ")); 1309 } else { 1310 (void) printf(dgettext(TEXT_DOMAIN, 1311 "After rewind, several\n" 1312 "\tpersistent user-data errors will remain. ")); 1313 } 1314 } 1315 (void) printf(dgettext(TEXT_DOMAIN, 1316 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1317 reason >= 0 ? "clear" : "import", name); 1318 1319 (void) printf(dgettext(TEXT_DOMAIN, 1320 "A scrub of the pool\n" 1321 "\tis strongly recommended after recovery.\n")); 1322 return; 1323 1324 no_info: 1325 (void) printf(dgettext(TEXT_DOMAIN, 1326 "Destroy and re-create the pool from\n\ta backup source.\n")); 1327 } 1328 1329 /* 1330 * zpool_import() is a contracted interface. Should be kept the same 1331 * if possible. 1332 * 1333 * Applications should use zpool_import_props() to import a pool with 1334 * new properties value to be set. 1335 */ 1336 int 1337 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1338 char *altroot) 1339 { 1340 nvlist_t *props = NULL; 1341 int ret; 1342 1343 if (altroot != NULL) { 1344 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1345 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1346 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1347 newname)); 1348 } 1349 1350 if (nvlist_add_string(props, 1351 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1352 nvlist_add_string(props, 1353 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1354 nvlist_free(props); 1355 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1356 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1357 newname)); 1358 } 1359 } 1360 1361 ret = zpool_import_props(hdl, config, newname, props, 1362 ZFS_IMPORT_NORMAL); 1363 if (props) 1364 nvlist_free(props); 1365 return (ret); 1366 } 1367 1368 static void 1369 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 1370 int indent) 1371 { 1372 nvlist_t **child; 1373 uint_t c, children; 1374 char *vname; 1375 uint64_t is_log = 0; 1376 1377 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 1378 &is_log); 1379 1380 if (name != NULL) 1381 (void) printf("\t%*s%s%s\n", indent, "", name, 1382 is_log ? " [log]" : ""); 1383 1384 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1385 &child, &children) != 0) 1386 return; 1387 1388 for (c = 0; c < children; c++) { 1389 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 1390 print_vdev_tree(hdl, vname, child[c], indent + 2); 1391 free(vname); 1392 } 1393 } 1394 1395 /* 1396 * Import the given pool using the known configuration and a list of 1397 * properties to be set. The configuration should have come from 1398 * zpool_find_import(). The 'newname' parameters control whether the pool 1399 * is imported with a different name. 1400 */ 1401 int 1402 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1403 nvlist_t *props, int flags) 1404 { 1405 zfs_cmd_t zc = { 0 }; 1406 zpool_rewind_policy_t policy; 1407 nvlist_t *nv = NULL; 1408 nvlist_t *nvinfo = NULL; 1409 nvlist_t *missing = NULL; 1410 char *thename; 1411 char *origname; 1412 int ret; 1413 int error = 0; 1414 char errbuf[1024]; 1415 1416 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1417 &origname) == 0); 1418 1419 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1420 "cannot import pool '%s'"), origname); 1421 1422 if (newname != NULL) { 1423 if (!zpool_name_valid(hdl, B_FALSE, newname)) 1424 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 1425 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1426 newname)); 1427 thename = (char *)newname; 1428 } else { 1429 thename = origname; 1430 } 1431 1432 if (props) { 1433 uint64_t version; 1434 1435 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1436 &version) == 0); 1437 1438 if ((props = zpool_valid_proplist(hdl, origname, 1439 props, version, B_TRUE, errbuf)) == NULL) { 1440 return (-1); 1441 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1442 nvlist_free(props); 1443 return (-1); 1444 } 1445 } 1446 1447 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1448 1449 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1450 &zc.zc_guid) == 0); 1451 1452 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1453 nvlist_free(props); 1454 return (-1); 1455 } 1456 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1457 nvlist_free(props); 1458 return (-1); 1459 } 1460 1461 zc.zc_cookie = flags; 1462 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 1463 errno == ENOMEM) { 1464 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 1465 zcmd_free_nvlists(&zc); 1466 return (-1); 1467 } 1468 } 1469 if (ret != 0) 1470 error = errno; 1471 1472 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1473 zpool_get_rewind_policy(config, &policy); 1474 1475 if (error) { 1476 char desc[1024]; 1477 1478 /* 1479 * Dry-run failed, but we print out what success 1480 * looks like if we found a best txg 1481 */ 1482 if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1483 zpool_rewind_exclaim(hdl, newname ? origname : thename, 1484 B_TRUE, nv); 1485 nvlist_free(nv); 1486 return (-1); 1487 } 1488 1489 if (newname == NULL) 1490 (void) snprintf(desc, sizeof (desc), 1491 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1492 thename); 1493 else 1494 (void) snprintf(desc, sizeof (desc), 1495 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1496 origname, thename); 1497 1498 switch (error) { 1499 case ENOTSUP: 1500 /* 1501 * Unsupported version. 1502 */ 1503 (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1504 break; 1505 1506 case EINVAL: 1507 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1508 break; 1509 1510 case EROFS: 1511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1512 "one or more devices is read only")); 1513 (void) zfs_error(hdl, EZFS_BADDEV, desc); 1514 break; 1515 1516 case ENXIO: 1517 if (nv && nvlist_lookup_nvlist(nv, 1518 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1519 nvlist_lookup_nvlist(nvinfo, 1520 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 1521 (void) printf(dgettext(TEXT_DOMAIN, 1522 "The devices below are missing, use " 1523 "'-m' to import the pool anyway:\n")); 1524 print_vdev_tree(hdl, NULL, missing, 2); 1525 (void) printf("\n"); 1526 } 1527 (void) zpool_standard_error(hdl, error, desc); 1528 break; 1529 1530 case EEXIST: 1531 (void) zpool_standard_error(hdl, error, desc); 1532 break; 1533 1534 default: 1535 (void) zpool_standard_error(hdl, error, desc); 1536 zpool_explain_recover(hdl, 1537 newname ? origname : thename, -error, nv); 1538 break; 1539 } 1540 1541 nvlist_free(nv); 1542 ret = -1; 1543 } else { 1544 zpool_handle_t *zhp; 1545 1546 /* 1547 * This should never fail, but play it safe anyway. 1548 */ 1549 if (zpool_open_silent(hdl, thename, &zhp) != 0) 1550 ret = -1; 1551 else if (zhp != NULL) 1552 zpool_close(zhp); 1553 if (policy.zrp_request & 1554 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1555 zpool_rewind_exclaim(hdl, newname ? origname : thename, 1556 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1557 } 1558 nvlist_free(nv); 1559 return (0); 1560 } 1561 1562 zcmd_free_nvlists(&zc); 1563 nvlist_free(props); 1564 1565 return (ret); 1566 } 1567 1568 /* 1569 * Scan the pool. 1570 */ 1571 int 1572 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1573 { 1574 zfs_cmd_t zc = { 0 }; 1575 char msg[1024]; 1576 libzfs_handle_t *hdl = zhp->zpool_hdl; 1577 1578 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1579 zc.zc_cookie = func; 1580 1581 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 1582 (errno == ENOENT && func != POOL_SCAN_NONE)) 1583 return (0); 1584 1585 if (func == POOL_SCAN_SCRUB) { 1586 (void) snprintf(msg, sizeof (msg), 1587 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1588 } else if (func == POOL_SCAN_NONE) { 1589 (void) snprintf(msg, sizeof (msg), 1590 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 1591 zc.zc_name); 1592 } else { 1593 assert(!"unexpected result"); 1594 } 1595 1596 if (errno == EBUSY) { 1597 nvlist_t *nvroot; 1598 pool_scan_stat_t *ps = NULL; 1599 uint_t psc; 1600 1601 verify(nvlist_lookup_nvlist(zhp->zpool_config, 1602 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1603 (void) nvlist_lookup_uint64_array(nvroot, 1604 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 1605 if (ps && ps->pss_func == POOL_SCAN_SCRUB) 1606 return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 1607 else 1608 return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1609 } else if (errno == ENOENT) { 1610 return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 1611 } else { 1612 return (zpool_standard_error(hdl, errno, msg)); 1613 } 1614 } 1615 1616 /* 1617 * This provides a very minimal check whether a given string is likely a 1618 * c#t#d# style string. Users of this are expected to do their own 1619 * verification of the s# part. 1620 */ 1621 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 1622 1623 /* 1624 * More elaborate version for ones which may start with "/dev/dsk/" 1625 * and the like. 1626 */ 1627 static int 1628 ctd_check_path(char *str) { 1629 /* 1630 * If it starts with a slash, check the last component. 1631 */ 1632 if (str && str[0] == '/') { 1633 char *tmp = strrchr(str, '/'); 1634 1635 /* 1636 * If it ends in "/old", check the second-to-last 1637 * component of the string instead. 1638 */ 1639 if (tmp != str && strcmp(tmp, "/old") == 0) { 1640 for (tmp--; *tmp != '/'; tmp--) 1641 ; 1642 } 1643 str = tmp + 1; 1644 } 1645 return (CTD_CHECK(str)); 1646 } 1647 1648 /* 1649 * Find a vdev that matches the search criteria specified. We use the 1650 * the nvpair name to determine how we should look for the device. 1651 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1652 * spare; but FALSE if its an INUSE spare. 1653 */ 1654 static nvlist_t * 1655 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1656 boolean_t *l2cache, boolean_t *log) 1657 { 1658 uint_t c, children; 1659 nvlist_t **child; 1660 nvlist_t *ret; 1661 uint64_t is_log; 1662 char *srchkey; 1663 nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1664 1665 /* Nothing to look for */ 1666 if (search == NULL || pair == NULL) 1667 return (NULL); 1668 1669 /* Obtain the key we will use to search */ 1670 srchkey = nvpair_name(pair); 1671 1672 switch (nvpair_type(pair)) { 1673 case DATA_TYPE_UINT64: { 1674 uint64_t srchval, theguid, present; 1675 1676 verify(nvpair_value_uint64(pair, &srchval) == 0); 1677 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1678 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1679 &present) == 0) { 1680 /* 1681 * If the device has never been present since 1682 * import, the only reliable way to match the 1683 * vdev is by GUID. 1684 */ 1685 verify(nvlist_lookup_uint64(nv, 1686 ZPOOL_CONFIG_GUID, &theguid) == 0); 1687 if (theguid == srchval) 1688 return (nv); 1689 } 1690 } 1691 break; 1692 } 1693 1694 case DATA_TYPE_STRING: { 1695 char *srchval, *val; 1696 1697 verify(nvpair_value_string(pair, &srchval) == 0); 1698 if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1699 break; 1700 1701 /* 1702 * Search for the requested value. Special cases: 1703 * 1704 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 1705 * "s0" or "s0/old". The "s0" part is hidden from the user, 1706 * but included in the string, so this matches around it. 1707 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 1708 * 1709 * Otherwise, all other searches are simple string compares. 1710 */ 1711 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 1712 ctd_check_path(val)) { 1713 uint64_t wholedisk = 0; 1714 1715 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1716 &wholedisk); 1717 if (wholedisk) { 1718 int slen = strlen(srchval); 1719 int vlen = strlen(val); 1720 1721 if (slen != vlen - 2) 1722 break; 1723 1724 /* 1725 * make_leaf_vdev() should only set 1726 * wholedisk for ZPOOL_CONFIG_PATHs which 1727 * will include "/dev/dsk/", giving plenty of 1728 * room for the indices used next. 1729 */ 1730 ASSERT(vlen >= 6); 1731 1732 /* 1733 * strings identical except trailing "s0" 1734 */ 1735 if (strcmp(&val[vlen - 2], "s0") == 0 && 1736 strncmp(srchval, val, slen) == 0) 1737 return (nv); 1738 1739 /* 1740 * strings identical except trailing "s0/old" 1741 */ 1742 if (strcmp(&val[vlen - 6], "s0/old") == 0 && 1743 strcmp(&srchval[slen - 4], "/old") == 0 && 1744 strncmp(srchval, val, slen - 4) == 0) 1745 return (nv); 1746 1747 break; 1748 } 1749 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 1750 char *type, *idx, *end, *p; 1751 uint64_t id, vdev_id; 1752 1753 /* 1754 * Determine our vdev type, keeping in mind 1755 * that the srchval is composed of a type and 1756 * vdev id pair (i.e. mirror-4). 1757 */ 1758 if ((type = strdup(srchval)) == NULL) 1759 return (NULL); 1760 1761 if ((p = strrchr(type, '-')) == NULL) { 1762 free(type); 1763 break; 1764 } 1765 idx = p + 1; 1766 *p = '\0'; 1767 1768 /* 1769 * If the types don't match then keep looking. 1770 */ 1771 if (strncmp(val, type, strlen(val)) != 0) { 1772 free(type); 1773 break; 1774 } 1775 1776 verify(strncmp(type, VDEV_TYPE_RAIDZ, 1777 strlen(VDEV_TYPE_RAIDZ)) == 0 || 1778 strncmp(type, VDEV_TYPE_MIRROR, 1779 strlen(VDEV_TYPE_MIRROR)) == 0); 1780 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 1781 &id) == 0); 1782 1783 errno = 0; 1784 vdev_id = strtoull(idx, &end, 10); 1785 1786 free(type); 1787 if (errno != 0) 1788 return (NULL); 1789 1790 /* 1791 * Now verify that we have the correct vdev id. 1792 */ 1793 if (vdev_id == id) 1794 return (nv); 1795 } 1796 1797 /* 1798 * Common case 1799 */ 1800 if (strcmp(srchval, val) == 0) 1801 return (nv); 1802 break; 1803 } 1804 1805 default: 1806 break; 1807 } 1808 1809 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1810 &child, &children) != 0) 1811 return (NULL); 1812 1813 for (c = 0; c < children; c++) { 1814 if ((ret = vdev_to_nvlist_iter(child[c], search, 1815 avail_spare, l2cache, NULL)) != NULL) { 1816 /* 1817 * The 'is_log' value is only set for the toplevel 1818 * vdev, not the leaf vdevs. So we always lookup the 1819 * log device from the root of the vdev tree (where 1820 * 'log' is non-NULL). 1821 */ 1822 if (log != NULL && 1823 nvlist_lookup_uint64(child[c], 1824 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 1825 is_log) { 1826 *log = B_TRUE; 1827 } 1828 return (ret); 1829 } 1830 } 1831 1832 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1833 &child, &children) == 0) { 1834 for (c = 0; c < children; c++) { 1835 if ((ret = vdev_to_nvlist_iter(child[c], search, 1836 avail_spare, l2cache, NULL)) != NULL) { 1837 *avail_spare = B_TRUE; 1838 return (ret); 1839 } 1840 } 1841 } 1842 1843 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1844 &child, &children) == 0) { 1845 for (c = 0; c < children; c++) { 1846 if ((ret = vdev_to_nvlist_iter(child[c], search, 1847 avail_spare, l2cache, NULL)) != NULL) { 1848 *l2cache = B_TRUE; 1849 return (ret); 1850 } 1851 } 1852 } 1853 1854 return (NULL); 1855 } 1856 1857 /* 1858 * Given a physical path (minus the "/devices" prefix), find the 1859 * associated vdev. 1860 */ 1861 nvlist_t * 1862 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 1863 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 1864 { 1865 nvlist_t *search, *nvroot, *ret; 1866 1867 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1868 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 1869 1870 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1871 &nvroot) == 0); 1872 1873 *avail_spare = B_FALSE; 1874 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1875 nvlist_free(search); 1876 1877 return (ret); 1878 } 1879 1880 /* 1881 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 1882 */ 1883 boolean_t 1884 zpool_vdev_is_interior(const char *name) 1885 { 1886 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 1887 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 1888 return (B_TRUE); 1889 return (B_FALSE); 1890 } 1891 1892 nvlist_t * 1893 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1894 boolean_t *l2cache, boolean_t *log) 1895 { 1896 char buf[MAXPATHLEN]; 1897 char *end; 1898 nvlist_t *nvroot, *search, *ret; 1899 uint64_t guid; 1900 1901 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1902 1903 guid = strtoull(path, &end, 10); 1904 if (guid != 0 && *end == '\0') { 1905 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 1906 } else if (zpool_vdev_is_interior(path)) { 1907 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 1908 } else if (path[0] != '/') { 1909 (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1910 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 1911 } else { 1912 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 1913 } 1914 1915 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1916 &nvroot) == 0); 1917 1918 *avail_spare = B_FALSE; 1919 *l2cache = B_FALSE; 1920 if (log != NULL) 1921 *log = B_FALSE; 1922 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1923 nvlist_free(search); 1924 1925 return (ret); 1926 } 1927 1928 static int 1929 vdev_online(nvlist_t *nv) 1930 { 1931 uint64_t ival; 1932 1933 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 1934 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 1935 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 1936 return (0); 1937 1938 return (1); 1939 } 1940 1941 /* 1942 * Helper function for zpool_get_physpaths(). 1943 */ 1944 static int 1945 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 1946 size_t *bytes_written) 1947 { 1948 size_t bytes_left, pos, rsz; 1949 char *tmppath; 1950 const char *format; 1951 1952 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 1953 &tmppath) != 0) 1954 return (EZFS_NODEVICE); 1955 1956 pos = *bytes_written; 1957 bytes_left = physpath_size - pos; 1958 format = (pos == 0) ? "%s" : " %s"; 1959 1960 rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 1961 *bytes_written += rsz; 1962 1963 if (rsz >= bytes_left) { 1964 /* if physpath was not copied properly, clear it */ 1965 if (bytes_left != 0) { 1966 physpath[pos] = 0; 1967 } 1968 return (EZFS_NOSPC); 1969 } 1970 return (0); 1971 } 1972 1973 static int 1974 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 1975 size_t *rsz, boolean_t is_spare) 1976 { 1977 char *type; 1978 int ret; 1979 1980 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 1981 return (EZFS_INVALCONFIG); 1982 1983 if (strcmp(type, VDEV_TYPE_DISK) == 0) { 1984 /* 1985 * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 1986 * For a spare vdev, we only want to boot from the active 1987 * spare device. 1988 */ 1989 if (is_spare) { 1990 uint64_t spare = 0; 1991 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1992 &spare); 1993 if (!spare) 1994 return (EZFS_INVALCONFIG); 1995 } 1996 1997 if (vdev_online(nv)) { 1998 if ((ret = vdev_get_one_physpath(nv, physpath, 1999 phypath_size, rsz)) != 0) 2000 return (ret); 2001 } 2002 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2003 strcmp(type, VDEV_TYPE_REPLACING) == 0 || 2004 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 2005 nvlist_t **child; 2006 uint_t count; 2007 int i, ret; 2008 2009 if (nvlist_lookup_nvlist_array(nv, 2010 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 2011 return (EZFS_INVALCONFIG); 2012 2013 for (i = 0; i < count; i++) { 2014 ret = vdev_get_physpaths(child[i], physpath, 2015 phypath_size, rsz, is_spare); 2016 if (ret == EZFS_NOSPC) 2017 return (ret); 2018 } 2019 } 2020 2021 return (EZFS_POOL_INVALARG); 2022 } 2023 2024 /* 2025 * Get phys_path for a root pool config. 2026 * Return 0 on success; non-zero on failure. 2027 */ 2028 static int 2029 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2030 { 2031 size_t rsz; 2032 nvlist_t *vdev_root; 2033 nvlist_t **child; 2034 uint_t count; 2035 char *type; 2036 2037 rsz = 0; 2038 2039 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2040 &vdev_root) != 0) 2041 return (EZFS_INVALCONFIG); 2042 2043 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2044 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2045 &child, &count) != 0) 2046 return (EZFS_INVALCONFIG); 2047 2048 /* 2049 * root pool can not have EFI labeled disks and can only have 2050 * a single top-level vdev. 2051 */ 2052 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 2053 pool_uses_efi(vdev_root)) 2054 return (EZFS_POOL_INVALARG); 2055 2056 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 2057 B_FALSE); 2058 2059 /* No online devices */ 2060 if (rsz == 0) 2061 return (EZFS_NODEVICE); 2062 2063 return (0); 2064 } 2065 2066 /* 2067 * Get phys_path for a root pool 2068 * Return 0 on success; non-zero on failure. 2069 */ 2070 int 2071 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2072 { 2073 return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2074 phypath_size)); 2075 } 2076 2077 /* 2078 * If the device has being dynamically expanded then we need to relabel 2079 * the disk to use the new unallocated space. 2080 */ 2081 static int 2082 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2083 { 2084 char path[MAXPATHLEN]; 2085 char errbuf[1024]; 2086 int fd, error; 2087 int (*_efi_use_whole_disk)(int); 2088 2089 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2090 "efi_use_whole_disk")) == NULL) 2091 return (-1); 2092 2093 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 2094 2095 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2097 "relabel '%s': unable to open device"), name); 2098 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2099 } 2100 2101 /* 2102 * It's possible that we might encounter an error if the device 2103 * does not have any unallocated space left. If so, we simply 2104 * ignore that error and continue on. 2105 */ 2106 error = _efi_use_whole_disk(fd); 2107 (void) close(fd); 2108 if (error && error != VT_ENOSPC) { 2109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2110 "relabel '%s': unable to read disk capacity"), name); 2111 return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2112 } 2113 return (0); 2114 } 2115 2116 /* 2117 * Bring the specified vdev online. The 'flags' parameter is a set of the 2118 * ZFS_ONLINE_* flags. 2119 */ 2120 int 2121 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 2122 vdev_state_t *newstate) 2123 { 2124 zfs_cmd_t zc = { 0 }; 2125 char msg[1024]; 2126 nvlist_t *tgt; 2127 boolean_t avail_spare, l2cache, islog; 2128 libzfs_handle_t *hdl = zhp->zpool_hdl; 2129 2130 if (flags & ZFS_ONLINE_EXPAND) { 2131 (void) snprintf(msg, sizeof (msg), 2132 dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2133 } else { 2134 (void) snprintf(msg, sizeof (msg), 2135 dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2136 } 2137 2138 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2139 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2140 &islog)) == NULL) 2141 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2142 2143 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2144 2145 if (avail_spare) 2146 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2147 2148 if (flags & ZFS_ONLINE_EXPAND || 2149 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2150 char *pathname = NULL; 2151 uint64_t wholedisk = 0; 2152 2153 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2154 &wholedisk); 2155 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2156 &pathname) == 0); 2157 2158 /* 2159 * XXX - L2ARC 1.0 devices can't support expansion. 2160 */ 2161 if (l2cache) { 2162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2163 "cannot expand cache devices")); 2164 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2165 } 2166 2167 if (wholedisk) { 2168 pathname += strlen(DISK_ROOT) + 1; 2169 (void) zpool_relabel_disk(zhp->zpool_hdl, pathname); 2170 } 2171 } 2172 2173 zc.zc_cookie = VDEV_STATE_ONLINE; 2174 zc.zc_obj = flags; 2175 2176 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 2177 if (errno == EINVAL) { 2178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 2179 "from this pool into a new one. Use '%s' " 2180 "instead"), "zpool detach"); 2181 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 2182 } 2183 return (zpool_standard_error(hdl, errno, msg)); 2184 } 2185 2186 *newstate = zc.zc_cookie; 2187 return (0); 2188 } 2189 2190 /* 2191 * Take the specified vdev offline 2192 */ 2193 int 2194 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2195 { 2196 zfs_cmd_t zc = { 0 }; 2197 char msg[1024]; 2198 nvlist_t *tgt; 2199 boolean_t avail_spare, l2cache; 2200 libzfs_handle_t *hdl = zhp->zpool_hdl; 2201 2202 (void) snprintf(msg, sizeof (msg), 2203 dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2204 2205 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2206 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2207 NULL)) == NULL) 2208 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2209 2210 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2211 2212 if (avail_spare) 2213 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2214 2215 zc.zc_cookie = VDEV_STATE_OFFLINE; 2216 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 2217 2218 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2219 return (0); 2220 2221 switch (errno) { 2222 case EBUSY: 2223 2224 /* 2225 * There are no other replicas of this device. 2226 */ 2227 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2228 2229 case EEXIST: 2230 /* 2231 * The log device has unplayed logs 2232 */ 2233 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2234 2235 default: 2236 return (zpool_standard_error(hdl, errno, msg)); 2237 } 2238 } 2239 2240 /* 2241 * Mark the given vdev faulted. 2242 */ 2243 int 2244 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 2245 { 2246 zfs_cmd_t zc = { 0 }; 2247 char msg[1024]; 2248 libzfs_handle_t *hdl = zhp->zpool_hdl; 2249 2250 (void) snprintf(msg, sizeof (msg), 2251 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2252 2253 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2254 zc.zc_guid = guid; 2255 zc.zc_cookie = VDEV_STATE_FAULTED; 2256 zc.zc_obj = aux; 2257 2258 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2259 return (0); 2260 2261 switch (errno) { 2262 case EBUSY: 2263 2264 /* 2265 * There are no other replicas of this device. 2266 */ 2267 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2268 2269 default: 2270 return (zpool_standard_error(hdl, errno, msg)); 2271 } 2272 2273 } 2274 2275 /* 2276 * Mark the given vdev degraded. 2277 */ 2278 int 2279 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 2280 { 2281 zfs_cmd_t zc = { 0 }; 2282 char msg[1024]; 2283 libzfs_handle_t *hdl = zhp->zpool_hdl; 2284 2285 (void) snprintf(msg, sizeof (msg), 2286 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 2287 2288 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2289 zc.zc_guid = guid; 2290 zc.zc_cookie = VDEV_STATE_DEGRADED; 2291 zc.zc_obj = aux; 2292 2293 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2294 return (0); 2295 2296 return (zpool_standard_error(hdl, errno, msg)); 2297 } 2298 2299 /* 2300 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 2301 * a hot spare. 2302 */ 2303 static boolean_t 2304 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 2305 { 2306 nvlist_t **child; 2307 uint_t c, children; 2308 char *type; 2309 2310 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 2311 &children) == 0) { 2312 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 2313 &type) == 0); 2314 2315 if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 2316 children == 2 && child[which] == tgt) 2317 return (B_TRUE); 2318 2319 for (c = 0; c < children; c++) 2320 if (is_replacing_spare(child[c], tgt, which)) 2321 return (B_TRUE); 2322 } 2323 2324 return (B_FALSE); 2325 } 2326 2327 /* 2328 * Attach new_disk (fully described by nvroot) to old_disk. 2329 * If 'replacing' is specified, the new disk will replace the old one. 2330 */ 2331 int 2332 zpool_vdev_attach(zpool_handle_t *zhp, 2333 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2334 { 2335 zfs_cmd_t zc = { 0 }; 2336 char msg[1024]; 2337 int ret; 2338 nvlist_t *tgt; 2339 boolean_t avail_spare, l2cache, islog; 2340 uint64_t val; 2341 char *path, *newname; 2342 nvlist_t **child; 2343 uint_t children; 2344 nvlist_t *config_root; 2345 libzfs_handle_t *hdl = zhp->zpool_hdl; 2346 boolean_t rootpool = pool_is_bootable(zhp); 2347 2348 if (replacing) 2349 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2350 "cannot replace %s with %s"), old_disk, new_disk); 2351 else 2352 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2353 "cannot attach %s to %s"), new_disk, old_disk); 2354 2355 /* 2356 * If this is a root pool, make sure that we're not attaching an 2357 * EFI labeled device. 2358 */ 2359 if (rootpool && pool_uses_efi(nvroot)) { 2360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2361 "EFI labeled devices are not supported on root pools.")); 2362 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 2363 } 2364 2365 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2366 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2367 &islog)) == 0) 2368 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2369 2370 if (avail_spare) 2371 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2372 2373 if (l2cache) 2374 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2375 2376 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2377 zc.zc_cookie = replacing; 2378 2379 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2380 &child, &children) != 0 || children != 1) { 2381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2382 "new device must be a single disk")); 2383 return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 2384 } 2385 2386 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2387 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 2388 2389 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 2390 return (-1); 2391 2392 /* 2393 * If the target is a hot spare that has been swapped in, we can only 2394 * replace it with another hot spare. 2395 */ 2396 if (replacing && 2397 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2398 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2399 NULL) == NULL || !avail_spare) && 2400 is_replacing_spare(config_root, tgt, 1)) { 2401 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2402 "can only be replaced by another hot spare")); 2403 free(newname); 2404 return (zfs_error(hdl, EZFS_BADTARGET, msg)); 2405 } 2406 2407 /* 2408 * If we are attempting to replace a spare, it canot be applied to an 2409 * already spared device. 2410 */ 2411 if (replacing && 2412 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 2413 zpool_find_vdev(zhp, newname, &avail_spare, 2414 &l2cache, NULL) != NULL && avail_spare && 2415 is_replacing_spare(config_root, tgt, 0)) { 2416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2417 "device has already been replaced with a spare")); 2418 free(newname); 2419 return (zfs_error(hdl, EZFS_BADTARGET, msg)); 2420 } 2421 2422 free(newname); 2423 2424 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 2425 return (-1); 2426 2427 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2428 2429 zcmd_free_nvlists(&zc); 2430 2431 if (ret == 0) { 2432 if (rootpool) { 2433 /* 2434 * XXX need a better way to prevent user from 2435 * booting up a half-baked vdev. 2436 */ 2437 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 2438 "sure to wait until resilver is done " 2439 "before rebooting.\n")); 2440 } 2441 return (0); 2442 } 2443 2444 switch (errno) { 2445 case ENOTSUP: 2446 /* 2447 * Can't attach to or replace this type of vdev. 2448 */ 2449 if (replacing) { 2450 if (islog) 2451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2452 "cannot replace a log with a spare")); 2453 else 2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2455 "cannot replace a replacing device")); 2456 } else { 2457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2458 "can only attach to mirrors and top-level " 2459 "disks")); 2460 } 2461 (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2462 break; 2463 2464 case EINVAL: 2465 /* 2466 * The new device must be a single disk. 2467 */ 2468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2469 "new device must be a single disk")); 2470 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2471 break; 2472 2473 case EBUSY: 2474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 2475 new_disk); 2476 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2477 break; 2478 2479 case EOVERFLOW: 2480 /* 2481 * The new device is too small. 2482 */ 2483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2484 "device is too small")); 2485 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2486 break; 2487 2488 case EDOM: 2489 /* 2490 * The new device has a different alignment requirement. 2491 */ 2492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2493 "devices have different sector alignment")); 2494 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2495 break; 2496 2497 case ENAMETOOLONG: 2498 /* 2499 * The resulting top-level vdev spec won't fit in the label. 2500 */ 2501 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2502 break; 2503 2504 default: 2505 (void) zpool_standard_error(hdl, errno, msg); 2506 } 2507 2508 return (-1); 2509 } 2510 2511 /* 2512 * Detach the specified device. 2513 */ 2514 int 2515 zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2516 { 2517 zfs_cmd_t zc = { 0 }; 2518 char msg[1024]; 2519 nvlist_t *tgt; 2520 boolean_t avail_spare, l2cache; 2521 libzfs_handle_t *hdl = zhp->zpool_hdl; 2522 2523 (void) snprintf(msg, sizeof (msg), 2524 dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2525 2526 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2527 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2528 NULL)) == 0) 2529 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2530 2531 if (avail_spare) 2532 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2533 2534 if (l2cache) 2535 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2536 2537 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2538 2539 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2540 return (0); 2541 2542 switch (errno) { 2543 2544 case ENOTSUP: 2545 /* 2546 * Can't detach from this type of vdev. 2547 */ 2548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 2549 "applicable to mirror and replacing vdevs")); 2550 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2551 break; 2552 2553 case EBUSY: 2554 /* 2555 * There are no other replicas of this device. 2556 */ 2557 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2558 break; 2559 2560 default: 2561 (void) zpool_standard_error(hdl, errno, msg); 2562 } 2563 2564 return (-1); 2565 } 2566 2567 /* 2568 * Find a mirror vdev in the source nvlist. 2569 * 2570 * The mchild array contains a list of disks in one of the top-level mirrors 2571 * of the source pool. The schild array contains a list of disks that the 2572 * user specified on the command line. We loop over the mchild array to 2573 * see if any entry in the schild array matches. 2574 * 2575 * If a disk in the mchild array is found in the schild array, we return 2576 * the index of that entry. Otherwise we return -1. 2577 */ 2578 static int 2579 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 2580 nvlist_t **schild, uint_t schildren) 2581 { 2582 uint_t mc; 2583 2584 for (mc = 0; mc < mchildren; mc++) { 2585 uint_t sc; 2586 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 2587 mchild[mc], B_FALSE); 2588 2589 for (sc = 0; sc < schildren; sc++) { 2590 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 2591 schild[sc], B_FALSE); 2592 boolean_t result = (strcmp(mpath, spath) == 0); 2593 2594 free(spath); 2595 if (result) { 2596 free(mpath); 2597 return (mc); 2598 } 2599 } 2600 2601 free(mpath); 2602 } 2603 2604 return (-1); 2605 } 2606 2607 /* 2608 * Split a mirror pool. If newroot points to null, then a new nvlist 2609 * is generated and it is the responsibility of the caller to free it. 2610 */ 2611 int 2612 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 2613 nvlist_t *props, splitflags_t flags) 2614 { 2615 zfs_cmd_t zc = { 0 }; 2616 char msg[1024]; 2617 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 2618 nvlist_t **varray = NULL, *zc_props = NULL; 2619 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 2620 libzfs_handle_t *hdl = zhp->zpool_hdl; 2621 uint64_t vers; 2622 boolean_t freelist = B_FALSE, memory_err = B_TRUE; 2623 int retval = 0; 2624 2625 (void) snprintf(msg, sizeof (msg), 2626 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 2627 2628 if (!zpool_name_valid(hdl, B_FALSE, newname)) 2629 return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 2630 2631 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 2632 (void) fprintf(stderr, gettext("Internal error: unable to " 2633 "retrieve pool configuration\n")); 2634 return (-1); 2635 } 2636 2637 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 2638 == 0); 2639 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 2640 2641 if (props) { 2642 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2643 props, vers, B_TRUE, msg)) == NULL) 2644 return (-1); 2645 } 2646 2647 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 2648 &children) != 0) { 2649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2650 "Source pool is missing vdev tree")); 2651 if (zc_props) 2652 nvlist_free(zc_props); 2653 return (-1); 2654 } 2655 2656 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 2657 vcount = 0; 2658 2659 if (*newroot == NULL || 2660 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 2661 &newchild, &newchildren) != 0) 2662 newchildren = 0; 2663 2664 for (c = 0; c < children; c++) { 2665 uint64_t is_log = B_FALSE, is_hole = B_FALSE; 2666 char *type; 2667 nvlist_t **mchild, *vdev; 2668 uint_t mchildren; 2669 int entry; 2670 2671 /* 2672 * Unlike cache & spares, slogs are stored in the 2673 * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 2674 */ 2675 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 2676 &is_log); 2677 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 2678 &is_hole); 2679 if (is_log || is_hole) { 2680 /* 2681 * Create a hole vdev and put it in the config. 2682 */ 2683 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 2684 goto out; 2685 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 2686 VDEV_TYPE_HOLE) != 0) 2687 goto out; 2688 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 2689 1) != 0) 2690 goto out; 2691 if (lastlog == 0) 2692 lastlog = vcount; 2693 varray[vcount++] = vdev; 2694 continue; 2695 } 2696 lastlog = 0; 2697 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 2698 == 0); 2699 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 2700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2701 "Source pool must be composed only of mirrors\n")); 2702 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 2703 goto out; 2704 } 2705 2706 verify(nvlist_lookup_nvlist_array(child[c], 2707 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 2708 2709 /* find or add an entry for this top-level vdev */ 2710 if (newchildren > 0 && 2711 (entry = find_vdev_entry(zhp, mchild, mchildren, 2712 newchild, newchildren)) >= 0) { 2713 /* We found a disk that the user specified. */ 2714 vdev = mchild[entry]; 2715 ++found; 2716 } else { 2717 /* User didn't specify a disk for this vdev. */ 2718 vdev = mchild[mchildren - 1]; 2719 } 2720 2721 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 2722 goto out; 2723 } 2724 2725 /* did we find every disk the user specified? */ 2726 if (found != newchildren) { 2727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 2728 "include at most one disk from each mirror")); 2729 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 2730 goto out; 2731 } 2732 2733 /* Prepare the nvlist for populating. */ 2734 if (*newroot == NULL) { 2735 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 2736 goto out; 2737 freelist = B_TRUE; 2738 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 2739 VDEV_TYPE_ROOT) != 0) 2740 goto out; 2741 } else { 2742 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 2743 } 2744 2745 /* Add all the children we found */ 2746 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 2747 lastlog == 0 ? vcount : lastlog) != 0) 2748 goto out; 2749 2750 /* 2751 * If we're just doing a dry run, exit now with success. 2752 */ 2753 if (flags.dryrun) { 2754 memory_err = B_FALSE; 2755 freelist = B_FALSE; 2756 goto out; 2757 } 2758 2759 /* now build up the config list & call the ioctl */ 2760 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 2761 goto out; 2762 2763 if (nvlist_add_nvlist(newconfig, 2764 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 2765 nvlist_add_string(newconfig, 2766 ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 2767 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 2768 goto out; 2769 2770 /* 2771 * The new pool is automatically part of the namespace unless we 2772 * explicitly export it. 2773 */ 2774 if (!flags.import) 2775 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 2776 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2777 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 2778 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 2779 goto out; 2780 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 2781 goto out; 2782 2783 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 2784 retval = zpool_standard_error(hdl, errno, msg); 2785 goto out; 2786 } 2787 2788 freelist = B_FALSE; 2789 memory_err = B_FALSE; 2790 2791 out: 2792 if (varray != NULL) { 2793 int v; 2794 2795 for (v = 0; v < vcount; v++) 2796 nvlist_free(varray[v]); 2797 free(varray); 2798 } 2799 zcmd_free_nvlists(&zc); 2800 if (zc_props) 2801 nvlist_free(zc_props); 2802 if (newconfig) 2803 nvlist_free(newconfig); 2804 if (freelist) { 2805 nvlist_free(*newroot); 2806 *newroot = NULL; 2807 } 2808 2809 if (retval != 0) 2810 return (retval); 2811 2812 if (memory_err) 2813 return (no_memory(hdl)); 2814 2815 return (0); 2816 } 2817 2818 /* 2819 * Remove the given device. Currently, this is supported only for hot spares 2820 * and level 2 cache devices. 2821 */ 2822 int 2823 zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 2824 { 2825 zfs_cmd_t zc = { 0 }; 2826 char msg[1024]; 2827 nvlist_t *tgt; 2828 boolean_t avail_spare, l2cache, islog; 2829 libzfs_handle_t *hdl = zhp->zpool_hdl; 2830 uint64_t version; 2831 2832 (void) snprintf(msg, sizeof (msg), 2833 dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 2834 2835 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2836 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2837 &islog)) == 0) 2838 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2839 /* 2840 * XXX - this should just go away. 2841 */ 2842 if (!avail_spare && !l2cache && !islog) { 2843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2844 "only inactive hot spares, cache, top-level, " 2845 "or log devices can be removed")); 2846 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2847 } 2848 2849 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 2850 if (islog && version < SPA_VERSION_HOLES) { 2851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2852 "pool must be upgrade to support log removal")); 2853 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 2854 } 2855 2856 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2857 2858 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 2859 return (0); 2860 2861 return (zpool_standard_error(hdl, errno, msg)); 2862 } 2863 2864 /* 2865 * Clear the errors for the pool, or the particular device if specified. 2866 */ 2867 int 2868 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 2869 { 2870 zfs_cmd_t zc = { 0 }; 2871 char msg[1024]; 2872 nvlist_t *tgt; 2873 zpool_rewind_policy_t policy; 2874 boolean_t avail_spare, l2cache; 2875 libzfs_handle_t *hdl = zhp->zpool_hdl; 2876 nvlist_t *nvi = NULL; 2877 int error; 2878 2879 if (path) 2880 (void) snprintf(msg, sizeof (msg), 2881 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2882 path); 2883 else 2884 (void) snprintf(msg, sizeof (msg), 2885 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2886 zhp->zpool_name); 2887 2888 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2889 if (path) { 2890 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 2891 &l2cache, NULL)) == 0) 2892 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2893 2894 /* 2895 * Don't allow error clearing for hot spares. Do allow 2896 * error clearing for l2cache devices. 2897 */ 2898 if (avail_spare) 2899 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2900 2901 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 2902 &zc.zc_guid) == 0); 2903 } 2904 2905 zpool_get_rewind_policy(rewindnvl, &policy); 2906 zc.zc_cookie = policy.zrp_request; 2907 2908 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 2909 return (-1); 2910 2911 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0) 2912 return (-1); 2913 2914 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 2915 errno == ENOMEM) { 2916 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2917 zcmd_free_nvlists(&zc); 2918 return (-1); 2919 } 2920 } 2921 2922 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 2923 errno != EPERM && errno != EACCES)) { 2924 if (policy.zrp_request & 2925 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2926 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 2927 zpool_rewind_exclaim(hdl, zc.zc_name, 2928 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 2929 nvi); 2930 nvlist_free(nvi); 2931 } 2932 zcmd_free_nvlists(&zc); 2933 return (0); 2934 } 2935 2936 zcmd_free_nvlists(&zc); 2937 return (zpool_standard_error(hdl, errno, msg)); 2938 } 2939 2940 /* 2941 * Similar to zpool_clear(), but takes a GUID (used by fmd). 2942 */ 2943 int 2944 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 2945 { 2946 zfs_cmd_t zc = { 0 }; 2947 char msg[1024]; 2948 libzfs_handle_t *hdl = zhp->zpool_hdl; 2949 2950 (void) snprintf(msg, sizeof (msg), 2951 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 2952 guid); 2953 2954 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2955 zc.zc_guid = guid; 2956 zc.zc_cookie = ZPOOL_NO_REWIND; 2957 2958 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 2959 return (0); 2960 2961 return (zpool_standard_error(hdl, errno, msg)); 2962 } 2963 2964 /* 2965 * Convert from a devid string to a path. 2966 */ 2967 static char * 2968 devid_to_path(char *devid_str) 2969 { 2970 ddi_devid_t devid; 2971 char *minor; 2972 char *path; 2973 devid_nmlist_t *list = NULL; 2974 int ret; 2975 2976 if (devid_str_decode(devid_str, &devid, &minor) != 0) 2977 return (NULL); 2978 2979 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 2980 2981 devid_str_free(minor); 2982 devid_free(devid); 2983 2984 if (ret != 0) 2985 return (NULL); 2986 2987 if ((path = strdup(list[0].devname)) == NULL) 2988 return (NULL); 2989 2990 devid_free_nmlist(list); 2991 2992 return (path); 2993 } 2994 2995 /* 2996 * Convert from a path to a devid string. 2997 */ 2998 static char * 2999 path_to_devid(const char *path) 3000 { 3001 int fd; 3002 ddi_devid_t devid; 3003 char *minor, *ret; 3004 3005 if ((fd = open(path, O_RDONLY)) < 0) 3006 return (NULL); 3007 3008 minor = NULL; 3009 ret = NULL; 3010 if (devid_get(fd, &devid) == 0) { 3011 if (devid_get_minor_name(fd, &minor) == 0) 3012 ret = devid_str_encode(devid, minor); 3013 if (minor != NULL) 3014 devid_str_free(minor); 3015 devid_free(devid); 3016 } 3017 (void) close(fd); 3018 3019 return (ret); 3020 } 3021 3022 /* 3023 * Issue the necessary ioctl() to update the stored path value for the vdev. We 3024 * ignore any failure here, since a common case is for an unprivileged user to 3025 * type 'zpool status', and we'll display the correct information anyway. 3026 */ 3027 static void 3028 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3029 { 3030 zfs_cmd_t zc = { 0 }; 3031 3032 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3033 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3034 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3035 &zc.zc_guid) == 0); 3036 3037 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3038 } 3039 3040 /* 3041 * Given a vdev, return the name to display in iostat. If the vdev has a path, 3042 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3043 * We also check if this is a whole disk, in which case we strip off the 3044 * trailing 's0' slice name. 3045 * 3046 * This routine is also responsible for identifying when disks have been 3047 * reconfigured in a new location. The kernel will have opened the device by 3048 * devid, but the path will still refer to the old location. To catch this, we 3049 * first do a path -> devid translation (which is fast for the common case). If 3050 * the devid matches, we're done. If not, we do a reverse devid -> path 3051 * translation and issue the appropriate ioctl() to update the path of the vdev. 3052 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3053 * of these checks. 3054 */ 3055 char * 3056 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 3057 boolean_t verbose) 3058 { 3059 char *path, *devid; 3060 uint64_t value; 3061 char buf[64]; 3062 vdev_stat_t *vs; 3063 uint_t vsc; 3064 3065 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3066 &value) == 0) { 3067 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3068 &value) == 0); 3069 (void) snprintf(buf, sizeof (buf), "%llu", 3070 (u_longlong_t)value); 3071 path = buf; 3072 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3073 3074 /* 3075 * If the device is dead (faulted, offline, etc) then don't 3076 * bother opening it. Otherwise we may be forcing the user to 3077 * open a misbehaving device, which can have undesirable 3078 * effects. 3079 */ 3080 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 3081 (uint64_t **)&vs, &vsc) != 0 || 3082 vs->vs_state >= VDEV_STATE_DEGRADED) && 3083 zhp != NULL && 3084 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3085 /* 3086 * Determine if the current path is correct. 3087 */ 3088 char *newdevid = path_to_devid(path); 3089 3090 if (newdevid == NULL || 3091 strcmp(devid, newdevid) != 0) { 3092 char *newpath; 3093 3094 if ((newpath = devid_to_path(devid)) != NULL) { 3095 /* 3096 * Update the path appropriately. 3097 */ 3098 set_path(zhp, nv, newpath); 3099 if (nvlist_add_string(nv, 3100 ZPOOL_CONFIG_PATH, newpath) == 0) 3101 verify(nvlist_lookup_string(nv, 3102 ZPOOL_CONFIG_PATH, 3103 &path) == 0); 3104 free(newpath); 3105 } 3106 } 3107 3108 if (newdevid) 3109 devid_str_free(newdevid); 3110 } 3111 3112 if (strncmp(path, "/dev/dsk/", 9) == 0) 3113 path += 9; 3114 3115 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3116 &value) == 0 && value) { 3117 int pathlen = strlen(path); 3118 char *tmp = zfs_strdup(hdl, path); 3119 3120 /* 3121 * If it starts with c#, and ends with "s0", chop 3122 * the "s0" off, or if it ends with "s0/old", remove 3123 * the "s0" from the middle. 3124 */ 3125 if (CTD_CHECK(tmp)) { 3126 if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 3127 tmp[pathlen - 2] = '\0'; 3128 } else if (pathlen > 6 && 3129 strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 3130 (void) strcpy(&tmp[pathlen - 6], 3131 "/old"); 3132 } 3133 } 3134 return (tmp); 3135 } 3136 } else { 3137 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 3138 3139 /* 3140 * If it's a raidz device, we need to stick in the parity level. 3141 */ 3142 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 3143 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 3144 &value) == 0); 3145 (void) snprintf(buf, sizeof (buf), "%s%llu", path, 3146 (u_longlong_t)value); 3147 path = buf; 3148 } 3149 3150 /* 3151 * We identify each top-level vdev by using a <type-id> 3152 * naming convention. 3153 */ 3154 if (verbose) { 3155 uint64_t id; 3156 3157 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 3158 &id) == 0); 3159 (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 3160 (u_longlong_t)id); 3161 path = buf; 3162 } 3163 } 3164 3165 return (zfs_strdup(hdl, path)); 3166 } 3167 3168 static int 3169 zbookmark_compare(const void *a, const void *b) 3170 { 3171 return (memcmp(a, b, sizeof (zbookmark_t))); 3172 } 3173 3174 /* 3175 * Retrieve the persistent error log, uniquify the members, and return to the 3176 * caller. 3177 */ 3178 int 3179 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3180 { 3181 zfs_cmd_t zc = { 0 }; 3182 uint64_t count; 3183 zbookmark_t *zb = NULL; 3184 int i; 3185 3186 /* 3187 * Retrieve the raw error list from the kernel. If the number of errors 3188 * has increased, allocate more space and continue until we get the 3189 * entire list. 3190 */ 3191 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3192 &count) == 0); 3193 if (count == 0) 3194 return (0); 3195 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 3196 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 3197 return (-1); 3198 zc.zc_nvlist_dst_size = count; 3199 (void) strcpy(zc.zc_name, zhp->zpool_name); 3200 for (;;) { 3201 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 3202 &zc) != 0) { 3203 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3204 if (errno == ENOMEM) { 3205 count = zc.zc_nvlist_dst_size; 3206 if ((zc.zc_nvlist_dst = (uintptr_t) 3207 zfs_alloc(zhp->zpool_hdl, count * 3208 sizeof (zbookmark_t))) == (uintptr_t)NULL) 3209 return (-1); 3210 } else { 3211 return (-1); 3212 } 3213 } else { 3214 break; 3215 } 3216 } 3217 3218 /* 3219 * Sort the resulting bookmarks. This is a little confusing due to the 3220 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3221 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3222 * _not_ copied as part of the process. So we point the start of our 3223 * array appropriate and decrement the total number of elements. 3224 */ 3225 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 3226 zc.zc_nvlist_dst_size; 3227 count -= zc.zc_nvlist_dst_size; 3228 3229 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 3230 3231 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3232 3233 /* 3234 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3235 */ 3236 for (i = 0; i < count; i++) { 3237 nvlist_t *nv; 3238 3239 /* ignoring zb_blkid and zb_level for now */ 3240 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3241 zb[i-1].zb_object == zb[i].zb_object) 3242 continue; 3243 3244 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 3245 goto nomem; 3246 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 3247 zb[i].zb_objset) != 0) { 3248 nvlist_free(nv); 3249 goto nomem; 3250 } 3251 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 3252 zb[i].zb_object) != 0) { 3253 nvlist_free(nv); 3254 goto nomem; 3255 } 3256 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 3257 nvlist_free(nv); 3258 goto nomem; 3259 } 3260 nvlist_free(nv); 3261 } 3262 3263 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3264 return (0); 3265 3266 nomem: 3267 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3268 return (no_memory(zhp->zpool_hdl)); 3269 } 3270 3271 /* 3272 * Upgrade a ZFS pool to the latest on-disk version. 3273 */ 3274 int 3275 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3276 { 3277 zfs_cmd_t zc = { 0 }; 3278 libzfs_handle_t *hdl = zhp->zpool_hdl; 3279 3280 (void) strcpy(zc.zc_name, zhp->zpool_name); 3281 zc.zc_cookie = new_version; 3282 3283 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3284 return (zpool_standard_error_fmt(hdl, errno, 3285 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 3286 zhp->zpool_name)); 3287 return (0); 3288 } 3289 3290 void 3291 zpool_set_history_str(const char *subcommand, int argc, char **argv, 3292 char *history_str) 3293 { 3294 int i; 3295 3296 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 3297 for (i = 1; i < argc; i++) { 3298 if (strlen(history_str) + 1 + strlen(argv[i]) > 3299 HIS_MAX_RECORD_LEN) 3300 break; 3301 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 3302 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 3303 } 3304 } 3305 3306 /* 3307 * Stage command history for logging. 3308 */ 3309 int 3310 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 3311 { 3312 if (history_str == NULL) 3313 return (EINVAL); 3314 3315 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 3316 return (EINVAL); 3317 3318 if (hdl->libzfs_log_str != NULL) 3319 free(hdl->libzfs_log_str); 3320 3321 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 3322 return (no_memory(hdl)); 3323 3324 return (0); 3325 } 3326 3327 /* 3328 * Perform ioctl to get some command history of a pool. 3329 * 3330 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 3331 * logical offset of the history buffer to start reading from. 3332 * 3333 * Upon return, 'off' is the next logical offset to read from and 3334 * 'len' is the actual amount of bytes read into 'buf'. 3335 */ 3336 static int 3337 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 3338 { 3339 zfs_cmd_t zc = { 0 }; 3340 libzfs_handle_t *hdl = zhp->zpool_hdl; 3341 3342 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3343 3344 zc.zc_history = (uint64_t)(uintptr_t)buf; 3345 zc.zc_history_len = *len; 3346 zc.zc_history_offset = *off; 3347 3348 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 3349 switch (errno) { 3350 case EPERM: 3351 return (zfs_error_fmt(hdl, EZFS_PERM, 3352 dgettext(TEXT_DOMAIN, 3353 "cannot show history for pool '%s'"), 3354 zhp->zpool_name)); 3355 case ENOENT: 3356 return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 3357 dgettext(TEXT_DOMAIN, "cannot get history for pool " 3358 "'%s'"), zhp->zpool_name)); 3359 case ENOTSUP: 3360 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3361 dgettext(TEXT_DOMAIN, "cannot get history for pool " 3362 "'%s', pool must be upgraded"), zhp->zpool_name)); 3363 default: 3364 return (zpool_standard_error_fmt(hdl, errno, 3365 dgettext(TEXT_DOMAIN, 3366 "cannot get history for '%s'"), zhp->zpool_name)); 3367 } 3368 } 3369 3370 *len = zc.zc_history_len; 3371 *off = zc.zc_history_offset; 3372 3373 return (0); 3374 } 3375 3376 /* 3377 * Process the buffer of nvlists, unpacking and storing each nvlist record 3378 * into 'records'. 'leftover' is set to the number of bytes that weren't 3379 * processed as there wasn't a complete record. 3380 */ 3381 int 3382 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 3383 nvlist_t ***records, uint_t *numrecords) 3384 { 3385 uint64_t reclen; 3386 nvlist_t *nv; 3387 int i; 3388 3389 while (bytes_read > sizeof (reclen)) { 3390 3391 /* get length of packed record (stored as little endian) */ 3392 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 3393 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 3394 3395 if (bytes_read < sizeof (reclen) + reclen) 3396 break; 3397 3398 /* unpack record */ 3399 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 3400 return (ENOMEM); 3401 bytes_read -= sizeof (reclen) + reclen; 3402 buf += sizeof (reclen) + reclen; 3403 3404 /* add record to nvlist array */ 3405 (*numrecords)++; 3406 if (ISP2(*numrecords + 1)) { 3407 *records = realloc(*records, 3408 *numrecords * 2 * sizeof (nvlist_t *)); 3409 } 3410 (*records)[*numrecords - 1] = nv; 3411 } 3412 3413 *leftover = bytes_read; 3414 return (0); 3415 } 3416 3417 #define HIS_BUF_LEN (128*1024) 3418 3419 /* 3420 * Retrieve the command history of a pool. 3421 */ 3422 int 3423 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 3424 { 3425 char buf[HIS_BUF_LEN]; 3426 uint64_t off = 0; 3427 nvlist_t **records = NULL; 3428 uint_t numrecords = 0; 3429 int err, i; 3430 3431 do { 3432 uint64_t bytes_read = sizeof (buf); 3433 uint64_t leftover; 3434 3435 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 3436 break; 3437 3438 /* if nothing else was read in, we're at EOF, just return */ 3439 if (!bytes_read) 3440 break; 3441 3442 if ((err = zpool_history_unpack(buf, bytes_read, 3443 &leftover, &records, &numrecords)) != 0) 3444 break; 3445 off -= leftover; 3446 3447 /* CONSTCOND */ 3448 } while (1); 3449 3450 if (!err) { 3451 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 3452 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 3453 records, numrecords) == 0); 3454 } 3455 for (i = 0; i < numrecords; i++) 3456 nvlist_free(records[i]); 3457 free(records); 3458 3459 return (err); 3460 } 3461 3462 void 3463 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 3464 char *pathname, size_t len) 3465 { 3466 zfs_cmd_t zc = { 0 }; 3467 boolean_t mounted = B_FALSE; 3468 char *mntpnt = NULL; 3469 char dsname[MAXNAMELEN]; 3470 3471 if (dsobj == 0) { 3472 /* special case for the MOS */ 3473 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 3474 return; 3475 } 3476 3477 /* get the dataset's name */ 3478 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3479 zc.zc_obj = dsobj; 3480 if (ioctl(zhp->zpool_hdl->libzfs_fd, 3481 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 3482 /* just write out a path of two object numbers */ 3483 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 3484 dsobj, obj); 3485 return; 3486 } 3487 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 3488 3489 /* find out if the dataset is mounted */ 3490 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 3491 3492 /* get the corrupted object's path */ 3493 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 3494 zc.zc_obj = obj; 3495 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 3496 &zc) == 0) { 3497 if (mounted) { 3498 (void) snprintf(pathname, len, "%s%s", mntpnt, 3499 zc.zc_value); 3500 } else { 3501 (void) snprintf(pathname, len, "%s:%s", 3502 dsname, zc.zc_value); 3503 } 3504 } else { 3505 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 3506 } 3507 free(mntpnt); 3508 } 3509 3510 /* 3511 * Read the EFI label from the config, if a label does not exist then 3512 * pass back the error to the caller. If the caller has passed a non-NULL 3513 * diskaddr argument then we set it to the starting address of the EFI 3514 * partition. 3515 */ 3516 static int 3517 read_efi_label(nvlist_t *config, diskaddr_t *sb) 3518 { 3519 char *path; 3520 int fd; 3521 char diskname[MAXPATHLEN]; 3522 int err = -1; 3523 3524 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 3525 return (err); 3526 3527 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 3528 strrchr(path, '/')); 3529 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 3530 struct dk_gpt *vtoc; 3531 3532 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 3533 if (sb != NULL) 3534 *sb = vtoc->efi_parts[0].p_start; 3535 efi_free(vtoc); 3536 } 3537 (void) close(fd); 3538 } 3539 return (err); 3540 } 3541 3542 /* 3543 * determine where a partition starts on a disk in the current 3544 * configuration 3545 */ 3546 static diskaddr_t 3547 find_start_block(nvlist_t *config) 3548 { 3549 nvlist_t **child; 3550 uint_t c, children; 3551 diskaddr_t sb = MAXOFFSET_T; 3552 uint64_t wholedisk; 3553 3554 if (nvlist_lookup_nvlist_array(config, 3555 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 3556 if (nvlist_lookup_uint64(config, 3557 ZPOOL_CONFIG_WHOLE_DISK, 3558 &wholedisk) != 0 || !wholedisk) { 3559 return (MAXOFFSET_T); 3560 } 3561 if (read_efi_label(config, &sb) < 0) 3562 sb = MAXOFFSET_T; 3563 return (sb); 3564 } 3565 3566 for (c = 0; c < children; c++) { 3567 sb = find_start_block(child[c]); 3568 if (sb != MAXOFFSET_T) { 3569 return (sb); 3570 } 3571 } 3572 return (MAXOFFSET_T); 3573 } 3574 3575 /* 3576 * Label an individual disk. The name provided is the short name, 3577 * stripped of any leading /dev path. 3578 */ 3579 int 3580 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 3581 { 3582 char path[MAXPATHLEN]; 3583 struct dk_gpt *vtoc; 3584 int fd; 3585 size_t resv = EFI_MIN_RESV_SIZE; 3586 uint64_t slice_size; 3587 diskaddr_t start_block; 3588 char errbuf[1024]; 3589 3590 /* prepare an error message just in case */ 3591 (void) snprintf(errbuf, sizeof (errbuf), 3592 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3593 3594 if (zhp) { 3595 nvlist_t *nvroot; 3596 3597 if (pool_is_bootable(zhp)) { 3598 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3599 "EFI labeled devices are not supported on root " 3600 "pools.")); 3601 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3602 } 3603 3604 verify(nvlist_lookup_nvlist(zhp->zpool_config, 3605 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3606 3607 if (zhp->zpool_start_block == 0) 3608 start_block = find_start_block(nvroot); 3609 else 3610 start_block = zhp->zpool_start_block; 3611 zhp->zpool_start_block = start_block; 3612 } else { 3613 /* new pool */ 3614 start_block = NEW_START_BLOCK; 3615 } 3616 3617 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 3618 BACKUP_SLICE); 3619 3620 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 3621 /* 3622 * This shouldn't happen. We've long since verified that this 3623 * is a valid device. 3624 */ 3625 zfs_error_aux(hdl, 3626 dgettext(TEXT_DOMAIN, "unable to open device")); 3627 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 3628 } 3629 3630 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 3631 /* 3632 * The only way this can fail is if we run out of memory, or we 3633 * were unable to read the disk's capacity 3634 */ 3635 if (errno == ENOMEM) 3636 (void) no_memory(hdl); 3637 3638 (void) close(fd); 3639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3640 "unable to read disk capacity"), name); 3641 3642 return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 3643 } 3644 3645 slice_size = vtoc->efi_last_u_lba + 1; 3646 slice_size -= EFI_MIN_RESV_SIZE; 3647 if (start_block == MAXOFFSET_T) 3648 start_block = NEW_START_BLOCK; 3649 slice_size -= start_block; 3650 3651 vtoc->efi_parts[0].p_start = start_block; 3652 vtoc->efi_parts[0].p_size = slice_size; 3653 3654 /* 3655 * Why we use V_USR: V_BACKUP confuses users, and is considered 3656 * disposable by some EFI utilities (since EFI doesn't have a backup 3657 * slice). V_UNASSIGNED is supposed to be used only for zero size 3658 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 3659 * etc. were all pretty specific. V_USR is as close to reality as we 3660 * can get, in the absence of V_OTHER. 3661 */ 3662 vtoc->efi_parts[0].p_tag = V_USR; 3663 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 3664 3665 vtoc->efi_parts[8].p_start = slice_size + start_block; 3666 vtoc->efi_parts[8].p_size = resv; 3667 vtoc->efi_parts[8].p_tag = V_RESERVED; 3668 3669 if (efi_write(fd, vtoc) != 0) { 3670 /* 3671 * Some block drivers (like pcata) may not support EFI 3672 * GPT labels. Print out a helpful error message dir- 3673 * ecting the user to manually label the disk and give 3674 * a specific slice. 3675 */ 3676 (void) close(fd); 3677 efi_free(vtoc); 3678 3679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3680 "try using fdisk(1M) and then provide a specific slice")); 3681 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 3682 } 3683 3684 (void) close(fd); 3685 efi_free(vtoc); 3686 return (0); 3687 } 3688 3689 static boolean_t 3690 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3691 { 3692 char *type; 3693 nvlist_t **child; 3694 uint_t children, c; 3695 3696 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3697 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 3698 strcmp(type, VDEV_TYPE_FILE) == 0 || 3699 strcmp(type, VDEV_TYPE_LOG) == 0 || 3700 strcmp(type, VDEV_TYPE_HOLE) == 0 || 3701 strcmp(type, VDEV_TYPE_MISSING) == 0) { 3702 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3703 "vdev type '%s' is not supported"), type); 3704 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3705 return (B_FALSE); 3706 } 3707 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3708 &child, &children) == 0) { 3709 for (c = 0; c < children; c++) { 3710 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3711 return (B_FALSE); 3712 } 3713 } 3714 return (B_TRUE); 3715 } 3716 3717 /* 3718 * check if this zvol is allowable for use as a dump device; zero if 3719 * it is, > 0 if it isn't, < 0 if it isn't a zvol 3720 */ 3721 int 3722 zvol_check_dump_config(char *arg) 3723 { 3724 zpool_handle_t *zhp = NULL; 3725 nvlist_t *config, *nvroot; 3726 char *p, *volname; 3727 nvlist_t **top; 3728 uint_t toplevels; 3729 libzfs_handle_t *hdl; 3730 char errbuf[1024]; 3731 char poolname[ZPOOL_MAXNAMELEN]; 3732 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3733 int ret = 1; 3734 3735 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3736 return (-1); 3737 } 3738 3739 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3740 "dump is not supported on device '%s'"), arg); 3741 3742 if ((hdl = libzfs_init()) == NULL) 3743 return (1); 3744 libzfs_print_on_error(hdl, B_TRUE); 3745 3746 volname = arg + pathlen; 3747 3748 /* check the configuration of the pool */ 3749 if ((p = strchr(volname, '/')) == NULL) { 3750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3751 "malformed dataset name")); 3752 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3753 return (1); 3754 } else if (p - volname >= ZFS_MAXNAMELEN) { 3755 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3756 "dataset name is too long")); 3757 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 3758 return (1); 3759 } else { 3760 (void) strncpy(poolname, volname, p - volname); 3761 poolname[p - volname] = '\0'; 3762 } 3763 3764 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 3765 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3766 "could not open pool '%s'"), poolname); 3767 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 3768 goto out; 3769 } 3770 config = zpool_get_config(zhp, NULL); 3771 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3772 &nvroot) != 0) { 3773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3774 "could not obtain vdev configuration for '%s'"), poolname); 3775 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3776 goto out; 3777 } 3778 3779 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3780 &top, &toplevels) == 0); 3781 if (toplevels != 1) { 3782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3783 "'%s' has multiple top level vdevs"), poolname); 3784 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3785 goto out; 3786 } 3787 3788 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 3789 goto out; 3790 } 3791 ret = 0; 3792 3793 out: 3794 if (zhp) 3795 zpool_close(zhp); 3796 libzfs_fini(hdl); 3797 return (ret); 3798 } 3799