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(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1001 return (-1); 1002 1003 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1004 1005 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 1006 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1007 "cannot destroy '%s'"), zhp->zpool_name); 1008 1009 if (errno == EROFS) { 1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1011 "one or more devices is read only")); 1012 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1013 } else { 1014 (void) zpool_standard_error(hdl, errno, msg); 1015 } 1016 1017 if (zfp) 1018 zfs_close(zfp); 1019 return (-1); 1020 } 1021 1022 if (zfp) { 1023 remove_mountpoint(zfp); 1024 zfs_close(zfp); 1025 } 1026 1027 return (0); 1028 } 1029 1030 /* 1031 * Add the given vdevs to the pool. The caller must have already performed the 1032 * necessary verification to ensure that the vdev specification is well-formed. 1033 */ 1034 int 1035 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1036 { 1037 zfs_cmd_t zc = { 0 }; 1038 int ret; 1039 libzfs_handle_t *hdl = zhp->zpool_hdl; 1040 char msg[1024]; 1041 nvlist_t **spares, **l2cache; 1042 uint_t nspares, nl2cache; 1043 1044 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1045 "cannot add to '%s'"), zhp->zpool_name); 1046 1047 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1048 SPA_VERSION_SPARES && 1049 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1050 &spares, &nspares) == 0) { 1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1052 "upgraded to add hot spares")); 1053 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1054 } 1055 1056 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 1057 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 1058 uint64_t s; 1059 1060 for (s = 0; s < nspares; s++) { 1061 char *path; 1062 1063 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 1064 &path) == 0 && pool_uses_efi(spares[s])) { 1065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1066 "device '%s' contains an EFI label and " 1067 "cannot be used on root pools."), 1068 zpool_vdev_name(hdl, NULL, spares[s], 1069 B_FALSE)); 1070 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1071 } 1072 } 1073 } 1074 1075 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1076 SPA_VERSION_L2CACHE && 1077 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1078 &l2cache, &nl2cache) == 0) { 1079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1080 "upgraded to add cache devices")); 1081 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1082 } 1083 1084 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1085 return (-1); 1086 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1087 1088 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1089 switch (errno) { 1090 case EBUSY: 1091 /* 1092 * This can happen if the user has specified the same 1093 * device multiple times. We can't reliably detect this 1094 * until we try to add it and see we already have a 1095 * label. 1096 */ 1097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1098 "one or more vdevs refer to the same device")); 1099 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1100 break; 1101 1102 case EOVERFLOW: 1103 /* 1104 * This occurrs when one of the devices is below 1105 * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1106 * device was the problem device since there's no 1107 * reliable way to determine device size from userland. 1108 */ 1109 { 1110 char buf[64]; 1111 1112 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1113 1114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1115 "device is less than the minimum " 1116 "size (%s)"), buf); 1117 } 1118 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1119 break; 1120 1121 case ENOTSUP: 1122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1123 "pool must be upgraded to add these vdevs")); 1124 (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1125 break; 1126 1127 case EDOM: 1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1129 "root pool can not have multiple vdevs" 1130 " or separate logs")); 1131 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1132 break; 1133 1134 case ENOTBLK: 1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1136 "cache device must be a disk or disk slice")); 1137 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1138 break; 1139 1140 default: 1141 (void) zpool_standard_error(hdl, errno, msg); 1142 } 1143 1144 ret = -1; 1145 } else { 1146 ret = 0; 1147 } 1148 1149 zcmd_free_nvlists(&zc); 1150 1151 return (ret); 1152 } 1153 1154 /* 1155 * Exports the pool from the system. The caller must ensure that there are no 1156 * mounted datasets in the pool. 1157 */ 1158 int 1159 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1160 { 1161 zfs_cmd_t zc = { 0 }; 1162 char msg[1024]; 1163 1164 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1165 "cannot export '%s'"), zhp->zpool_name); 1166 1167 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1168 zc.zc_cookie = force; 1169 zc.zc_guid = hardforce; 1170 1171 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 1172 switch (errno) { 1173 case EXDEV: 1174 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 1175 "use '-f' to override the following errors:\n" 1176 "'%s' has an active shared spare which could be" 1177 " used by other pools once '%s' is exported."), 1178 zhp->zpool_name, zhp->zpool_name); 1179 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 1180 msg)); 1181 default: 1182 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 1183 msg)); 1184 } 1185 } 1186 1187 return (0); 1188 } 1189 1190 int 1191 zpool_export(zpool_handle_t *zhp, boolean_t force) 1192 { 1193 return (zpool_export_common(zhp, force, B_FALSE)); 1194 } 1195 1196 int 1197 zpool_export_force(zpool_handle_t *zhp) 1198 { 1199 return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1200 } 1201 1202 static void 1203 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 1204 nvlist_t *config) 1205 { 1206 nvlist_t *nv = NULL; 1207 uint64_t rewindto; 1208 int64_t loss = -1; 1209 struct tm t; 1210 char timestr[128]; 1211 1212 if (!hdl->libzfs_printerr || config == NULL) 1213 return; 1214 1215 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0) 1216 return; 1217 1218 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1219 return; 1220 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1221 1222 if (localtime_r((time_t *)&rewindto, &t) != NULL && 1223 strftime(timestr, 128, 0, &t) != 0) { 1224 if (dryrun) { 1225 (void) printf(dgettext(TEXT_DOMAIN, 1226 "Would be able to return %s " 1227 "to its state as of %s.\n"), 1228 name, timestr); 1229 } else { 1230 (void) printf(dgettext(TEXT_DOMAIN, 1231 "Pool %s returned to its state as of %s.\n"), 1232 name, timestr); 1233 } 1234 if (loss > 120) { 1235 (void) printf(dgettext(TEXT_DOMAIN, 1236 "%s approximately %lld "), 1237 dryrun ? "Would discard" : "Discarded", 1238 (loss + 30) / 60); 1239 (void) printf(dgettext(TEXT_DOMAIN, 1240 "minutes of transactions.\n")); 1241 } else if (loss > 0) { 1242 (void) printf(dgettext(TEXT_DOMAIN, 1243 "%s approximately %lld "), 1244 dryrun ? "Would discard" : "Discarded", loss); 1245 (void) printf(dgettext(TEXT_DOMAIN, 1246 "seconds of transactions.\n")); 1247 } 1248 } 1249 } 1250 1251 void 1252 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1253 nvlist_t *config) 1254 { 1255 nvlist_t *nv = NULL; 1256 int64_t loss = -1; 1257 uint64_t edata = UINT64_MAX; 1258 uint64_t rewindto; 1259 struct tm t; 1260 char timestr[128]; 1261 1262 if (!hdl->libzfs_printerr) 1263 return; 1264 1265 if (reason >= 0) 1266 (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1267 else 1268 (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1269 1270 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1271 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1272 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1273 goto no_info; 1274 1275 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1276 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1277 &edata); 1278 1279 (void) printf(dgettext(TEXT_DOMAIN, 1280 "Recovery is possible, but will result in some data loss.\n")); 1281 1282 if (localtime_r((time_t *)&rewindto, &t) != NULL && 1283 strftime(timestr, 128, 0, &t) != 0) { 1284 (void) printf(dgettext(TEXT_DOMAIN, 1285 "\tReturning the pool to its state as of %s\n" 1286 "\tshould correct the problem. "), 1287 timestr); 1288 } else { 1289 (void) printf(dgettext(TEXT_DOMAIN, 1290 "\tReverting the pool to an earlier state " 1291 "should correct the problem.\n\t")); 1292 } 1293 1294 if (loss > 120) { 1295 (void) printf(dgettext(TEXT_DOMAIN, 1296 "Approximately %lld minutes of data\n" 1297 "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1298 } else if (loss > 0) { 1299 (void) printf(dgettext(TEXT_DOMAIN, 1300 "Approximately %lld seconds of data\n" 1301 "\tmust be discarded, irreversibly. "), loss); 1302 } 1303 if (edata != 0 && edata != UINT64_MAX) { 1304 if (edata == 1) { 1305 (void) printf(dgettext(TEXT_DOMAIN, 1306 "After rewind, at least\n" 1307 "\tone persistent user-data error will remain. ")); 1308 } else { 1309 (void) printf(dgettext(TEXT_DOMAIN, 1310 "After rewind, several\n" 1311 "\tpersistent user-data errors will remain. ")); 1312 } 1313 } 1314 (void) printf(dgettext(TEXT_DOMAIN, 1315 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1316 reason >= 0 ? "clear" : "import", name); 1317 1318 (void) printf(dgettext(TEXT_DOMAIN, 1319 "A scrub of the pool\n" 1320 "\tis strongly recommended after recovery.\n")); 1321 return; 1322 1323 no_info: 1324 (void) printf(dgettext(TEXT_DOMAIN, 1325 "Destroy and re-create the pool from\n\ta backup source.\n")); 1326 } 1327 1328 /* 1329 * zpool_import() is a contracted interface. Should be kept the same 1330 * if possible. 1331 * 1332 * Applications should use zpool_import_props() to import a pool with 1333 * new properties value to be set. 1334 */ 1335 int 1336 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1337 char *altroot) 1338 { 1339 nvlist_t *props = NULL; 1340 int ret; 1341 1342 if (altroot != NULL) { 1343 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1344 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1345 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1346 newname)); 1347 } 1348 1349 if (nvlist_add_string(props, 1350 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1351 nvlist_add_string(props, 1352 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1353 nvlist_free(props); 1354 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1355 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1356 newname)); 1357 } 1358 } 1359 1360 ret = zpool_import_props(hdl, config, newname, props, 1361 ZFS_IMPORT_NORMAL); 1362 if (props) 1363 nvlist_free(props); 1364 return (ret); 1365 } 1366 1367 static void 1368 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 1369 int indent) 1370 { 1371 nvlist_t **child; 1372 uint_t c, children; 1373 char *vname; 1374 uint64_t is_log = 0; 1375 1376 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 1377 &is_log); 1378 1379 if (name != NULL) 1380 (void) printf("\t%*s%s%s\n", indent, "", name, 1381 is_log ? " [log]" : ""); 1382 1383 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1384 &child, &children) != 0) 1385 return; 1386 1387 for (c = 0; c < children; c++) { 1388 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 1389 print_vdev_tree(hdl, vname, child[c], indent + 2); 1390 free(vname); 1391 } 1392 } 1393 1394 /* 1395 * Import the given pool using the known configuration and a list of 1396 * properties to be set. The configuration should have come from 1397 * zpool_find_import(). The 'newname' parameters control whether the pool 1398 * is imported with a different name. 1399 */ 1400 int 1401 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1402 nvlist_t *props, int flags) 1403 { 1404 zfs_cmd_t zc = { 0 }; 1405 zpool_rewind_policy_t policy; 1406 nvlist_t *nv = NULL; 1407 nvlist_t *nvinfo = NULL; 1408 nvlist_t *missing = NULL; 1409 char *thename; 1410 char *origname; 1411 int ret; 1412 int error = 0; 1413 char errbuf[1024]; 1414 1415 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1416 &origname) == 0); 1417 1418 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1419 "cannot import pool '%s'"), origname); 1420 1421 if (newname != NULL) { 1422 if (!zpool_name_valid(hdl, B_FALSE, newname)) 1423 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 1424 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1425 newname)); 1426 thename = (char *)newname; 1427 } else { 1428 thename = origname; 1429 } 1430 1431 if (props) { 1432 uint64_t version; 1433 1434 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1435 &version) == 0); 1436 1437 if ((props = zpool_valid_proplist(hdl, origname, 1438 props, version, B_TRUE, errbuf)) == NULL) { 1439 return (-1); 1440 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1441 nvlist_free(props); 1442 return (-1); 1443 } 1444 } 1445 1446 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1447 1448 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1449 &zc.zc_guid) == 0); 1450 1451 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1452 nvlist_free(props); 1453 return (-1); 1454 } 1455 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1456 nvlist_free(props); 1457 return (-1); 1458 } 1459 1460 zc.zc_cookie = flags; 1461 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 1462 errno == ENOMEM) { 1463 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 1464 zcmd_free_nvlists(&zc); 1465 return (-1); 1466 } 1467 } 1468 if (ret != 0) 1469 error = errno; 1470 1471 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1472 zpool_get_rewind_policy(config, &policy); 1473 1474 if (error) { 1475 char desc[1024]; 1476 1477 /* 1478 * Dry-run failed, but we print out what success 1479 * looks like if we found a best txg 1480 */ 1481 if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1482 zpool_rewind_exclaim(hdl, newname ? origname : thename, 1483 B_TRUE, nv); 1484 nvlist_free(nv); 1485 return (-1); 1486 } 1487 1488 if (newname == NULL) 1489 (void) snprintf(desc, sizeof (desc), 1490 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1491 thename); 1492 else 1493 (void) snprintf(desc, sizeof (desc), 1494 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1495 origname, thename); 1496 1497 switch (error) { 1498 case ENOTSUP: 1499 /* 1500 * Unsupported version. 1501 */ 1502 (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1503 break; 1504 1505 case EINVAL: 1506 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1507 break; 1508 1509 case EROFS: 1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1511 "one or more devices is read only")); 1512 (void) zfs_error(hdl, EZFS_BADDEV, desc); 1513 break; 1514 1515 case ENXIO: 1516 if (nv && nvlist_lookup_nvlist(nv, 1517 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1518 nvlist_lookup_nvlist(nvinfo, 1519 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 1520 (void) printf(dgettext(TEXT_DOMAIN, 1521 "The devices below are missing, use " 1522 "'-m' to import the pool anyway:\n")); 1523 print_vdev_tree(hdl, NULL, missing, 2); 1524 (void) printf("\n"); 1525 } 1526 (void) zpool_standard_error(hdl, error, desc); 1527 break; 1528 1529 case EEXIST: 1530 (void) zpool_standard_error(hdl, error, desc); 1531 break; 1532 1533 default: 1534 (void) zpool_standard_error(hdl, error, desc); 1535 zpool_explain_recover(hdl, 1536 newname ? origname : thename, -error, nv); 1537 break; 1538 } 1539 1540 nvlist_free(nv); 1541 ret = -1; 1542 } else { 1543 zpool_handle_t *zhp; 1544 1545 /* 1546 * This should never fail, but play it safe anyway. 1547 */ 1548 if (zpool_open_silent(hdl, thename, &zhp) != 0) 1549 ret = -1; 1550 else if (zhp != NULL) 1551 zpool_close(zhp); 1552 if (policy.zrp_request & 1553 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1554 zpool_rewind_exclaim(hdl, newname ? origname : thename, 1555 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1556 } 1557 nvlist_free(nv); 1558 return (0); 1559 } 1560 1561 zcmd_free_nvlists(&zc); 1562 nvlist_free(props); 1563 1564 return (ret); 1565 } 1566 1567 /* 1568 * Scan the pool. 1569 */ 1570 int 1571 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1572 { 1573 zfs_cmd_t zc = { 0 }; 1574 char msg[1024]; 1575 libzfs_handle_t *hdl = zhp->zpool_hdl; 1576 1577 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1578 zc.zc_cookie = func; 1579 1580 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 1581 (errno == ENOENT && func != POOL_SCAN_NONE)) 1582 return (0); 1583 1584 if (func == POOL_SCAN_SCRUB) { 1585 (void) snprintf(msg, sizeof (msg), 1586 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1587 } else if (func == POOL_SCAN_NONE) { 1588 (void) snprintf(msg, sizeof (msg), 1589 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 1590 zc.zc_name); 1591 } else { 1592 assert(!"unexpected result"); 1593 } 1594 1595 if (errno == EBUSY) { 1596 nvlist_t *nvroot; 1597 pool_scan_stat_t *ps = NULL; 1598 uint_t psc; 1599 1600 verify(nvlist_lookup_nvlist(zhp->zpool_config, 1601 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1602 (void) nvlist_lookup_uint64_array(nvroot, 1603 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 1604 if (ps && ps->pss_func == POOL_SCAN_SCRUB) 1605 return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 1606 else 1607 return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1608 } else if (errno == ENOENT) { 1609 return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 1610 } else { 1611 return (zpool_standard_error(hdl, errno, msg)); 1612 } 1613 } 1614 1615 /* 1616 * This provides a very minimal check whether a given string is likely a 1617 * c#t#d# style string. Users of this are expected to do their own 1618 * verification of the s# part. 1619 */ 1620 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 1621 1622 /* 1623 * More elaborate version for ones which may start with "/dev/dsk/" 1624 * and the like. 1625 */ 1626 static int 1627 ctd_check_path(char *str) { 1628 /* 1629 * If it starts with a slash, check the last component. 1630 */ 1631 if (str && str[0] == '/') { 1632 char *tmp = strrchr(str, '/'); 1633 1634 /* 1635 * If it ends in "/old", check the second-to-last 1636 * component of the string instead. 1637 */ 1638 if (tmp != str && strcmp(tmp, "/old") == 0) { 1639 for (tmp--; *tmp != '/'; tmp--) 1640 ; 1641 } 1642 str = tmp + 1; 1643 } 1644 return (CTD_CHECK(str)); 1645 } 1646 1647 /* 1648 * Find a vdev that matches the search criteria specified. We use the 1649 * the nvpair name to determine how we should look for the device. 1650 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1651 * spare; but FALSE if its an INUSE spare. 1652 */ 1653 static nvlist_t * 1654 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1655 boolean_t *l2cache, boolean_t *log) 1656 { 1657 uint_t c, children; 1658 nvlist_t **child; 1659 nvlist_t *ret; 1660 uint64_t is_log; 1661 char *srchkey; 1662 nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1663 1664 /* Nothing to look for */ 1665 if (search == NULL || pair == NULL) 1666 return (NULL); 1667 1668 /* Obtain the key we will use to search */ 1669 srchkey = nvpair_name(pair); 1670 1671 switch (nvpair_type(pair)) { 1672 case DATA_TYPE_UINT64: 1673 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1674 uint64_t srchval, theguid; 1675 1676 verify(nvpair_value_uint64(pair, &srchval) == 0); 1677 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1678 &theguid) == 0); 1679 if (theguid == srchval) 1680 return (nv); 1681 } 1682 break; 1683 1684 case DATA_TYPE_STRING: { 1685 char *srchval, *val; 1686 1687 verify(nvpair_value_string(pair, &srchval) == 0); 1688 if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1689 break; 1690 1691 /* 1692 * Search for the requested value. Special cases: 1693 * 1694 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 1695 * "s0" or "s0/old". The "s0" part is hidden from the user, 1696 * but included in the string, so this matches around it. 1697 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 1698 * 1699 * Otherwise, all other searches are simple string compares. 1700 */ 1701 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 1702 ctd_check_path(val)) { 1703 uint64_t wholedisk = 0; 1704 1705 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1706 &wholedisk); 1707 if (wholedisk) { 1708 int slen = strlen(srchval); 1709 int vlen = strlen(val); 1710 1711 if (slen != vlen - 2) 1712 break; 1713 1714 /* 1715 * make_leaf_vdev() should only set 1716 * wholedisk for ZPOOL_CONFIG_PATHs which 1717 * will include "/dev/dsk/", giving plenty of 1718 * room for the indices used next. 1719 */ 1720 ASSERT(vlen >= 6); 1721 1722 /* 1723 * strings identical except trailing "s0" 1724 */ 1725 if (strcmp(&val[vlen - 2], "s0") == 0 && 1726 strncmp(srchval, val, slen) == 0) 1727 return (nv); 1728 1729 /* 1730 * strings identical except trailing "s0/old" 1731 */ 1732 if (strcmp(&val[vlen - 6], "s0/old") == 0 && 1733 strcmp(&srchval[slen - 4], "/old") == 0 && 1734 strncmp(srchval, val, slen - 4) == 0) 1735 return (nv); 1736 1737 break; 1738 } 1739 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 1740 char *type, *idx, *end, *p; 1741 uint64_t id, vdev_id; 1742 1743 /* 1744 * Determine our vdev type, keeping in mind 1745 * that the srchval is composed of a type and 1746 * vdev id pair (i.e. mirror-4). 1747 */ 1748 if ((type = strdup(srchval)) == NULL) 1749 return (NULL); 1750 1751 if ((p = strrchr(type, '-')) == NULL) { 1752 free(type); 1753 break; 1754 } 1755 idx = p + 1; 1756 *p = '\0'; 1757 1758 /* 1759 * If the types don't match then keep looking. 1760 */ 1761 if (strncmp(val, type, strlen(val)) != 0) { 1762 free(type); 1763 break; 1764 } 1765 1766 verify(strncmp(type, VDEV_TYPE_RAIDZ, 1767 strlen(VDEV_TYPE_RAIDZ)) == 0 || 1768 strncmp(type, VDEV_TYPE_MIRROR, 1769 strlen(VDEV_TYPE_MIRROR)) == 0); 1770 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 1771 &id) == 0); 1772 1773 errno = 0; 1774 vdev_id = strtoull(idx, &end, 10); 1775 1776 free(type); 1777 if (errno != 0) 1778 return (NULL); 1779 1780 /* 1781 * Now verify that we have the correct vdev id. 1782 */ 1783 if (vdev_id == id) 1784 return (nv); 1785 } 1786 1787 /* 1788 * Common case 1789 */ 1790 if (strcmp(srchval, val) == 0) 1791 return (nv); 1792 break; 1793 } 1794 1795 default: 1796 break; 1797 } 1798 1799 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1800 &child, &children) != 0) 1801 return (NULL); 1802 1803 for (c = 0; c < children; c++) { 1804 if ((ret = vdev_to_nvlist_iter(child[c], search, 1805 avail_spare, l2cache, NULL)) != NULL) { 1806 /* 1807 * The 'is_log' value is only set for the toplevel 1808 * vdev, not the leaf vdevs. So we always lookup the 1809 * log device from the root of the vdev tree (where 1810 * 'log' is non-NULL). 1811 */ 1812 if (log != NULL && 1813 nvlist_lookup_uint64(child[c], 1814 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 1815 is_log) { 1816 *log = B_TRUE; 1817 } 1818 return (ret); 1819 } 1820 } 1821 1822 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1823 &child, &children) == 0) { 1824 for (c = 0; c < children; c++) { 1825 if ((ret = vdev_to_nvlist_iter(child[c], search, 1826 avail_spare, l2cache, NULL)) != NULL) { 1827 *avail_spare = B_TRUE; 1828 return (ret); 1829 } 1830 } 1831 } 1832 1833 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1834 &child, &children) == 0) { 1835 for (c = 0; c < children; c++) { 1836 if ((ret = vdev_to_nvlist_iter(child[c], search, 1837 avail_spare, l2cache, NULL)) != NULL) { 1838 *l2cache = B_TRUE; 1839 return (ret); 1840 } 1841 } 1842 } 1843 1844 return (NULL); 1845 } 1846 1847 /* 1848 * Given a physical path (minus the "/devices" prefix), find the 1849 * associated vdev. 1850 */ 1851 nvlist_t * 1852 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 1853 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 1854 { 1855 nvlist_t *search, *nvroot, *ret; 1856 1857 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1858 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 1859 1860 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1861 &nvroot) == 0); 1862 1863 *avail_spare = B_FALSE; 1864 *l2cache = B_FALSE; 1865 if (log != NULL) 1866 *log = B_FALSE; 1867 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1868 nvlist_free(search); 1869 1870 return (ret); 1871 } 1872 1873 /* 1874 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 1875 */ 1876 boolean_t 1877 zpool_vdev_is_interior(const char *name) 1878 { 1879 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 1880 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 1881 return (B_TRUE); 1882 return (B_FALSE); 1883 } 1884 1885 nvlist_t * 1886 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1887 boolean_t *l2cache, boolean_t *log) 1888 { 1889 char buf[MAXPATHLEN]; 1890 char *end; 1891 nvlist_t *nvroot, *search, *ret; 1892 uint64_t guid; 1893 1894 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1895 1896 guid = strtoull(path, &end, 10); 1897 if (guid != 0 && *end == '\0') { 1898 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 1899 } else if (zpool_vdev_is_interior(path)) { 1900 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 1901 } else if (path[0] != '/') { 1902 (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1903 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 1904 } else { 1905 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 1906 } 1907 1908 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1909 &nvroot) == 0); 1910 1911 *avail_spare = B_FALSE; 1912 *l2cache = B_FALSE; 1913 if (log != NULL) 1914 *log = B_FALSE; 1915 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1916 nvlist_free(search); 1917 1918 return (ret); 1919 } 1920 1921 static int 1922 vdev_online(nvlist_t *nv) 1923 { 1924 uint64_t ival; 1925 1926 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 1927 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 1928 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 1929 return (0); 1930 1931 return (1); 1932 } 1933 1934 /* 1935 * Helper function for zpool_get_physpaths(). 1936 */ 1937 static int 1938 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 1939 size_t *bytes_written) 1940 { 1941 size_t bytes_left, pos, rsz; 1942 char *tmppath; 1943 const char *format; 1944 1945 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 1946 &tmppath) != 0) 1947 return (EZFS_NODEVICE); 1948 1949 pos = *bytes_written; 1950 bytes_left = physpath_size - pos; 1951 format = (pos == 0) ? "%s" : " %s"; 1952 1953 rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 1954 *bytes_written += rsz; 1955 1956 if (rsz >= bytes_left) { 1957 /* if physpath was not copied properly, clear it */ 1958 if (bytes_left != 0) { 1959 physpath[pos] = 0; 1960 } 1961 return (EZFS_NOSPC); 1962 } 1963 return (0); 1964 } 1965 1966 static int 1967 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 1968 size_t *rsz, boolean_t is_spare) 1969 { 1970 char *type; 1971 int ret; 1972 1973 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 1974 return (EZFS_INVALCONFIG); 1975 1976 if (strcmp(type, VDEV_TYPE_DISK) == 0) { 1977 /* 1978 * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 1979 * For a spare vdev, we only want to boot from the active 1980 * spare device. 1981 */ 1982 if (is_spare) { 1983 uint64_t spare = 0; 1984 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1985 &spare); 1986 if (!spare) 1987 return (EZFS_INVALCONFIG); 1988 } 1989 1990 if (vdev_online(nv)) { 1991 if ((ret = vdev_get_one_physpath(nv, physpath, 1992 phypath_size, rsz)) != 0) 1993 return (ret); 1994 } 1995 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 1996 strcmp(type, VDEV_TYPE_REPLACING) == 0 || 1997 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 1998 nvlist_t **child; 1999 uint_t count; 2000 int i, ret; 2001 2002 if (nvlist_lookup_nvlist_array(nv, 2003 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 2004 return (EZFS_INVALCONFIG); 2005 2006 for (i = 0; i < count; i++) { 2007 ret = vdev_get_physpaths(child[i], physpath, 2008 phypath_size, rsz, is_spare); 2009 if (ret == EZFS_NOSPC) 2010 return (ret); 2011 } 2012 } 2013 2014 return (EZFS_POOL_INVALARG); 2015 } 2016 2017 /* 2018 * Get phys_path for a root pool config. 2019 * Return 0 on success; non-zero on failure. 2020 */ 2021 static int 2022 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2023 { 2024 size_t rsz; 2025 nvlist_t *vdev_root; 2026 nvlist_t **child; 2027 uint_t count; 2028 char *type; 2029 2030 rsz = 0; 2031 2032 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2033 &vdev_root) != 0) 2034 return (EZFS_INVALCONFIG); 2035 2036 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2037 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2038 &child, &count) != 0) 2039 return (EZFS_INVALCONFIG); 2040 2041 /* 2042 * root pool can not have EFI labeled disks and can only have 2043 * a single top-level vdev. 2044 */ 2045 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 2046 pool_uses_efi(vdev_root)) 2047 return (EZFS_POOL_INVALARG); 2048 2049 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 2050 B_FALSE); 2051 2052 /* No online devices */ 2053 if (rsz == 0) 2054 return (EZFS_NODEVICE); 2055 2056 return (0); 2057 } 2058 2059 /* 2060 * Get phys_path for a root pool 2061 * Return 0 on success; non-zero on failure. 2062 */ 2063 int 2064 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2065 { 2066 return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2067 phypath_size)); 2068 } 2069 2070 /* 2071 * If the device has being dynamically expanded then we need to relabel 2072 * the disk to use the new unallocated space. 2073 */ 2074 static int 2075 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2076 { 2077 char path[MAXPATHLEN]; 2078 char errbuf[1024]; 2079 int fd, error; 2080 int (*_efi_use_whole_disk)(int); 2081 2082 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2083 "efi_use_whole_disk")) == NULL) 2084 return (-1); 2085 2086 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 2087 2088 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2090 "relabel '%s': unable to open device"), name); 2091 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2092 } 2093 2094 /* 2095 * It's possible that we might encounter an error if the device 2096 * does not have any unallocated space left. If so, we simply 2097 * ignore that error and continue on. 2098 */ 2099 error = _efi_use_whole_disk(fd); 2100 (void) close(fd); 2101 if (error && error != VT_ENOSPC) { 2102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2103 "relabel '%s': unable to read disk capacity"), name); 2104 return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2105 } 2106 return (0); 2107 } 2108 2109 /* 2110 * Bring the specified vdev online. The 'flags' parameter is a set of the 2111 * ZFS_ONLINE_* flags. 2112 */ 2113 int 2114 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 2115 vdev_state_t *newstate) 2116 { 2117 zfs_cmd_t zc = { 0 }; 2118 char msg[1024]; 2119 nvlist_t *tgt; 2120 boolean_t avail_spare, l2cache, islog; 2121 libzfs_handle_t *hdl = zhp->zpool_hdl; 2122 2123 if (flags & ZFS_ONLINE_EXPAND) { 2124 (void) snprintf(msg, sizeof (msg), 2125 dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2126 } else { 2127 (void) snprintf(msg, sizeof (msg), 2128 dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2129 } 2130 2131 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2132 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2133 &islog)) == NULL) 2134 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2135 2136 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2137 2138 if (avail_spare) 2139 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2140 2141 if (flags & ZFS_ONLINE_EXPAND || 2142 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2143 char *pathname = NULL; 2144 uint64_t wholedisk = 0; 2145 2146 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2147 &wholedisk); 2148 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2149 &pathname) == 0); 2150 2151 /* 2152 * XXX - L2ARC 1.0 devices can't support expansion. 2153 */ 2154 if (l2cache) { 2155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2156 "cannot expand cache devices")); 2157 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2158 } 2159 2160 if (wholedisk) { 2161 pathname += strlen(DISK_ROOT) + 1; 2162 (void) zpool_relabel_disk(hdl, pathname); 2163 } 2164 } 2165 2166 zc.zc_cookie = VDEV_STATE_ONLINE; 2167 zc.zc_obj = flags; 2168 2169 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 2170 if (errno == EINVAL) { 2171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 2172 "from this pool into a new one. Use '%s' " 2173 "instead"), "zpool detach"); 2174 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 2175 } 2176 return (zpool_standard_error(hdl, errno, msg)); 2177 } 2178 2179 *newstate = zc.zc_cookie; 2180 return (0); 2181 } 2182 2183 /* 2184 * Take the specified vdev offline 2185 */ 2186 int 2187 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2188 { 2189 zfs_cmd_t zc = { 0 }; 2190 char msg[1024]; 2191 nvlist_t *tgt; 2192 boolean_t avail_spare, l2cache; 2193 libzfs_handle_t *hdl = zhp->zpool_hdl; 2194 2195 (void) snprintf(msg, sizeof (msg), 2196 dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2197 2198 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2199 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2200 NULL)) == NULL) 2201 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2202 2203 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2204 2205 if (avail_spare) 2206 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2207 2208 zc.zc_cookie = VDEV_STATE_OFFLINE; 2209 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 2210 2211 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2212 return (0); 2213 2214 switch (errno) { 2215 case EBUSY: 2216 2217 /* 2218 * There are no other replicas of this device. 2219 */ 2220 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2221 2222 case EEXIST: 2223 /* 2224 * The log device has unplayed logs 2225 */ 2226 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2227 2228 default: 2229 return (zpool_standard_error(hdl, errno, msg)); 2230 } 2231 } 2232 2233 /* 2234 * Mark the given vdev faulted. 2235 */ 2236 int 2237 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 2238 { 2239 zfs_cmd_t zc = { 0 }; 2240 char msg[1024]; 2241 libzfs_handle_t *hdl = zhp->zpool_hdl; 2242 2243 (void) snprintf(msg, sizeof (msg), 2244 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2245 2246 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2247 zc.zc_guid = guid; 2248 zc.zc_cookie = VDEV_STATE_FAULTED; 2249 zc.zc_obj = aux; 2250 2251 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2252 return (0); 2253 2254 switch (errno) { 2255 case EBUSY: 2256 2257 /* 2258 * There are no other replicas of this device. 2259 */ 2260 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2261 2262 default: 2263 return (zpool_standard_error(hdl, errno, msg)); 2264 } 2265 2266 } 2267 2268 /* 2269 * Mark the given vdev degraded. 2270 */ 2271 int 2272 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 2273 { 2274 zfs_cmd_t zc = { 0 }; 2275 char msg[1024]; 2276 libzfs_handle_t *hdl = zhp->zpool_hdl; 2277 2278 (void) snprintf(msg, sizeof (msg), 2279 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 2280 2281 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2282 zc.zc_guid = guid; 2283 zc.zc_cookie = VDEV_STATE_DEGRADED; 2284 zc.zc_obj = aux; 2285 2286 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2287 return (0); 2288 2289 return (zpool_standard_error(hdl, errno, msg)); 2290 } 2291 2292 /* 2293 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 2294 * a hot spare. 2295 */ 2296 static boolean_t 2297 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 2298 { 2299 nvlist_t **child; 2300 uint_t c, children; 2301 char *type; 2302 2303 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 2304 &children) == 0) { 2305 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 2306 &type) == 0); 2307 2308 if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 2309 children == 2 && child[which] == tgt) 2310 return (B_TRUE); 2311 2312 for (c = 0; c < children; c++) 2313 if (is_replacing_spare(child[c], tgt, which)) 2314 return (B_TRUE); 2315 } 2316 2317 return (B_FALSE); 2318 } 2319 2320 /* 2321 * Attach new_disk (fully described by nvroot) to old_disk. 2322 * If 'replacing' is specified, the new disk will replace the old one. 2323 */ 2324 int 2325 zpool_vdev_attach(zpool_handle_t *zhp, 2326 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2327 { 2328 zfs_cmd_t zc = { 0 }; 2329 char msg[1024]; 2330 int ret; 2331 nvlist_t *tgt; 2332 boolean_t avail_spare, l2cache, islog; 2333 uint64_t val; 2334 char *newname; 2335 nvlist_t **child; 2336 uint_t children; 2337 nvlist_t *config_root; 2338 libzfs_handle_t *hdl = zhp->zpool_hdl; 2339 boolean_t rootpool = pool_is_bootable(zhp); 2340 2341 if (replacing) 2342 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2343 "cannot replace %s with %s"), old_disk, new_disk); 2344 else 2345 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2346 "cannot attach %s to %s"), new_disk, old_disk); 2347 2348 /* 2349 * If this is a root pool, make sure that we're not attaching an 2350 * EFI labeled device. 2351 */ 2352 if (rootpool && pool_uses_efi(nvroot)) { 2353 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2354 "EFI labeled devices are not supported on root pools.")); 2355 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 2356 } 2357 2358 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2359 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2360 &islog)) == 0) 2361 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2362 2363 if (avail_spare) 2364 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2365 2366 if (l2cache) 2367 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2368 2369 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2370 zc.zc_cookie = replacing; 2371 2372 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2373 &child, &children) != 0 || children != 1) { 2374 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2375 "new device must be a single disk")); 2376 return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 2377 } 2378 2379 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2380 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 2381 2382 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 2383 return (-1); 2384 2385 /* 2386 * If the target is a hot spare that has been swapped in, we can only 2387 * replace it with another hot spare. 2388 */ 2389 if (replacing && 2390 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2391 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2392 NULL) == NULL || !avail_spare) && 2393 is_replacing_spare(config_root, tgt, 1)) { 2394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2395 "can only be replaced by another hot spare")); 2396 free(newname); 2397 return (zfs_error(hdl, EZFS_BADTARGET, msg)); 2398 } 2399 2400 free(newname); 2401 2402 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 2403 return (-1); 2404 2405 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2406 2407 zcmd_free_nvlists(&zc); 2408 2409 if (ret == 0) { 2410 if (rootpool) { 2411 /* 2412 * XXX need a better way to prevent user from 2413 * booting up a half-baked vdev. 2414 */ 2415 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 2416 "sure to wait until resilver is done " 2417 "before rebooting.\n")); 2418 } 2419 return (0); 2420 } 2421 2422 switch (errno) { 2423 case ENOTSUP: 2424 /* 2425 * Can't attach to or replace this type of vdev. 2426 */ 2427 if (replacing) { 2428 uint64_t version = zpool_get_prop_int(zhp, 2429 ZPOOL_PROP_VERSION, NULL); 2430 2431 if (islog) 2432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2433 "cannot replace a log with a spare")); 2434 else if (version >= SPA_VERSION_MULTI_REPLACE) 2435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2436 "already in replacing/spare config; wait " 2437 "for completion or use 'zpool detach'")); 2438 else 2439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2440 "cannot replace a replacing device")); 2441 } else { 2442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2443 "can only attach to mirrors and top-level " 2444 "disks")); 2445 } 2446 (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2447 break; 2448 2449 case EINVAL: 2450 /* 2451 * The new device must be a single disk. 2452 */ 2453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2454 "new device must be a single disk")); 2455 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2456 break; 2457 2458 case EBUSY: 2459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 2460 new_disk); 2461 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2462 break; 2463 2464 case EOVERFLOW: 2465 /* 2466 * The new device is too small. 2467 */ 2468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2469 "device is too small")); 2470 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2471 break; 2472 2473 case EDOM: 2474 /* 2475 * The new device has a different alignment requirement. 2476 */ 2477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2478 "devices have different sector alignment")); 2479 (void) zfs_error(hdl, EZFS_BADDEV, msg); 2480 break; 2481 2482 case ENAMETOOLONG: 2483 /* 2484 * The resulting top-level vdev spec won't fit in the label. 2485 */ 2486 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2487 break; 2488 2489 default: 2490 (void) zpool_standard_error(hdl, errno, msg); 2491 } 2492 2493 return (-1); 2494 } 2495 2496 /* 2497 * Detach the specified device. 2498 */ 2499 int 2500 zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2501 { 2502 zfs_cmd_t zc = { 0 }; 2503 char msg[1024]; 2504 nvlist_t *tgt; 2505 boolean_t avail_spare, l2cache; 2506 libzfs_handle_t *hdl = zhp->zpool_hdl; 2507 2508 (void) snprintf(msg, sizeof (msg), 2509 dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2510 2511 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2512 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2513 NULL)) == 0) 2514 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2515 2516 if (avail_spare) 2517 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2518 2519 if (l2cache) 2520 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2521 2522 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2523 2524 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2525 return (0); 2526 2527 switch (errno) { 2528 2529 case ENOTSUP: 2530 /* 2531 * Can't detach from this type of vdev. 2532 */ 2533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 2534 "applicable to mirror and replacing vdevs")); 2535 (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2536 break; 2537 2538 case EBUSY: 2539 /* 2540 * There are no other replicas of this device. 2541 */ 2542 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2543 break; 2544 2545 default: 2546 (void) zpool_standard_error(hdl, errno, msg); 2547 } 2548 2549 return (-1); 2550 } 2551 2552 /* 2553 * Find a mirror vdev in the source nvlist. 2554 * 2555 * The mchild array contains a list of disks in one of the top-level mirrors 2556 * of the source pool. The schild array contains a list of disks that the 2557 * user specified on the command line. We loop over the mchild array to 2558 * see if any entry in the schild array matches. 2559 * 2560 * If a disk in the mchild array is found in the schild array, we return 2561 * the index of that entry. Otherwise we return -1. 2562 */ 2563 static int 2564 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 2565 nvlist_t **schild, uint_t schildren) 2566 { 2567 uint_t mc; 2568 2569 for (mc = 0; mc < mchildren; mc++) { 2570 uint_t sc; 2571 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 2572 mchild[mc], B_FALSE); 2573 2574 for (sc = 0; sc < schildren; sc++) { 2575 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 2576 schild[sc], B_FALSE); 2577 boolean_t result = (strcmp(mpath, spath) == 0); 2578 2579 free(spath); 2580 if (result) { 2581 free(mpath); 2582 return (mc); 2583 } 2584 } 2585 2586 free(mpath); 2587 } 2588 2589 return (-1); 2590 } 2591 2592 /* 2593 * Split a mirror pool. If newroot points to null, then a new nvlist 2594 * is generated and it is the responsibility of the caller to free it. 2595 */ 2596 int 2597 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 2598 nvlist_t *props, splitflags_t flags) 2599 { 2600 zfs_cmd_t zc = { 0 }; 2601 char msg[1024]; 2602 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 2603 nvlist_t **varray = NULL, *zc_props = NULL; 2604 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 2605 libzfs_handle_t *hdl = zhp->zpool_hdl; 2606 uint64_t vers; 2607 boolean_t freelist = B_FALSE, memory_err = B_TRUE; 2608 int retval = 0; 2609 2610 (void) snprintf(msg, sizeof (msg), 2611 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 2612 2613 if (!zpool_name_valid(hdl, B_FALSE, newname)) 2614 return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 2615 2616 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 2617 (void) fprintf(stderr, gettext("Internal error: unable to " 2618 "retrieve pool configuration\n")); 2619 return (-1); 2620 } 2621 2622 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 2623 == 0); 2624 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 2625 2626 if (props) { 2627 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2628 props, vers, B_TRUE, msg)) == NULL) 2629 return (-1); 2630 } 2631 2632 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 2633 &children) != 0) { 2634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2635 "Source pool is missing vdev tree")); 2636 if (zc_props) 2637 nvlist_free(zc_props); 2638 return (-1); 2639 } 2640 2641 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 2642 vcount = 0; 2643 2644 if (*newroot == NULL || 2645 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 2646 &newchild, &newchildren) != 0) 2647 newchildren = 0; 2648 2649 for (c = 0; c < children; c++) { 2650 uint64_t is_log = B_FALSE, is_hole = B_FALSE; 2651 char *type; 2652 nvlist_t **mchild, *vdev; 2653 uint_t mchildren; 2654 int entry; 2655 2656 /* 2657 * Unlike cache & spares, slogs are stored in the 2658 * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 2659 */ 2660 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 2661 &is_log); 2662 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 2663 &is_hole); 2664 if (is_log || is_hole) { 2665 /* 2666 * Create a hole vdev and put it in the config. 2667 */ 2668 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 2669 goto out; 2670 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 2671 VDEV_TYPE_HOLE) != 0) 2672 goto out; 2673 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 2674 1) != 0) 2675 goto out; 2676 if (lastlog == 0) 2677 lastlog = vcount; 2678 varray[vcount++] = vdev; 2679 continue; 2680 } 2681 lastlog = 0; 2682 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 2683 == 0); 2684 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 2685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2686 "Source pool must be composed only of mirrors\n")); 2687 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 2688 goto out; 2689 } 2690 2691 verify(nvlist_lookup_nvlist_array(child[c], 2692 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 2693 2694 /* find or add an entry for this top-level vdev */ 2695 if (newchildren > 0 && 2696 (entry = find_vdev_entry(zhp, mchild, mchildren, 2697 newchild, newchildren)) >= 0) { 2698 /* We found a disk that the user specified. */ 2699 vdev = mchild[entry]; 2700 ++found; 2701 } else { 2702 /* User didn't specify a disk for this vdev. */ 2703 vdev = mchild[mchildren - 1]; 2704 } 2705 2706 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 2707 goto out; 2708 } 2709 2710 /* did we find every disk the user specified? */ 2711 if (found != newchildren) { 2712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 2713 "include at most one disk from each mirror")); 2714 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 2715 goto out; 2716 } 2717 2718 /* Prepare the nvlist for populating. */ 2719 if (*newroot == NULL) { 2720 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 2721 goto out; 2722 freelist = B_TRUE; 2723 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 2724 VDEV_TYPE_ROOT) != 0) 2725 goto out; 2726 } else { 2727 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 2728 } 2729 2730 /* Add all the children we found */ 2731 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 2732 lastlog == 0 ? vcount : lastlog) != 0) 2733 goto out; 2734 2735 /* 2736 * If we're just doing a dry run, exit now with success. 2737 */ 2738 if (flags.dryrun) { 2739 memory_err = B_FALSE; 2740 freelist = B_FALSE; 2741 goto out; 2742 } 2743 2744 /* now build up the config list & call the ioctl */ 2745 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 2746 goto out; 2747 2748 if (nvlist_add_nvlist(newconfig, 2749 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 2750 nvlist_add_string(newconfig, 2751 ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 2752 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 2753 goto out; 2754 2755 /* 2756 * The new pool is automatically part of the namespace unless we 2757 * explicitly export it. 2758 */ 2759 if (!flags.import) 2760 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 2761 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2762 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 2763 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 2764 goto out; 2765 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 2766 goto out; 2767 2768 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 2769 retval = zpool_standard_error(hdl, errno, msg); 2770 goto out; 2771 } 2772 2773 freelist = B_FALSE; 2774 memory_err = B_FALSE; 2775 2776 out: 2777 if (varray != NULL) { 2778 int v; 2779 2780 for (v = 0; v < vcount; v++) 2781 nvlist_free(varray[v]); 2782 free(varray); 2783 } 2784 zcmd_free_nvlists(&zc); 2785 if (zc_props) 2786 nvlist_free(zc_props); 2787 if (newconfig) 2788 nvlist_free(newconfig); 2789 if (freelist) { 2790 nvlist_free(*newroot); 2791 *newroot = NULL; 2792 } 2793 2794 if (retval != 0) 2795 return (retval); 2796 2797 if (memory_err) 2798 return (no_memory(hdl)); 2799 2800 return (0); 2801 } 2802 2803 /* 2804 * Remove the given device. Currently, this is supported only for hot spares 2805 * and level 2 cache devices. 2806 */ 2807 int 2808 zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 2809 { 2810 zfs_cmd_t zc = { 0 }; 2811 char msg[1024]; 2812 nvlist_t *tgt; 2813 boolean_t avail_spare, l2cache, islog; 2814 libzfs_handle_t *hdl = zhp->zpool_hdl; 2815 uint64_t version; 2816 2817 (void) snprintf(msg, sizeof (msg), 2818 dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 2819 2820 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2821 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2822 &islog)) == 0) 2823 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2824 /* 2825 * XXX - this should just go away. 2826 */ 2827 if (!avail_spare && !l2cache && !islog) { 2828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2829 "only inactive hot spares, cache, top-level, " 2830 "or log devices can be removed")); 2831 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2832 } 2833 2834 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 2835 if (islog && version < SPA_VERSION_HOLES) { 2836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2837 "pool must be upgrade to support log removal")); 2838 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 2839 } 2840 2841 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2842 2843 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 2844 return (0); 2845 2846 return (zpool_standard_error(hdl, errno, msg)); 2847 } 2848 2849 /* 2850 * Clear the errors for the pool, or the particular device if specified. 2851 */ 2852 int 2853 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 2854 { 2855 zfs_cmd_t zc = { 0 }; 2856 char msg[1024]; 2857 nvlist_t *tgt; 2858 zpool_rewind_policy_t policy; 2859 boolean_t avail_spare, l2cache; 2860 libzfs_handle_t *hdl = zhp->zpool_hdl; 2861 nvlist_t *nvi = NULL; 2862 int error; 2863 2864 if (path) 2865 (void) snprintf(msg, sizeof (msg), 2866 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2867 path); 2868 else 2869 (void) snprintf(msg, sizeof (msg), 2870 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2871 zhp->zpool_name); 2872 2873 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2874 if (path) { 2875 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 2876 &l2cache, NULL)) == 0) 2877 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2878 2879 /* 2880 * Don't allow error clearing for hot spares. Do allow 2881 * error clearing for l2cache devices. 2882 */ 2883 if (avail_spare) 2884 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2885 2886 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 2887 &zc.zc_guid) == 0); 2888 } 2889 2890 zpool_get_rewind_policy(rewindnvl, &policy); 2891 zc.zc_cookie = policy.zrp_request; 2892 2893 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 2894 return (-1); 2895 2896 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 2897 return (-1); 2898 2899 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 2900 errno == ENOMEM) { 2901 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2902 zcmd_free_nvlists(&zc); 2903 return (-1); 2904 } 2905 } 2906 2907 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 2908 errno != EPERM && errno != EACCES)) { 2909 if (policy.zrp_request & 2910 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2911 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 2912 zpool_rewind_exclaim(hdl, zc.zc_name, 2913 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 2914 nvi); 2915 nvlist_free(nvi); 2916 } 2917 zcmd_free_nvlists(&zc); 2918 return (0); 2919 } 2920 2921 zcmd_free_nvlists(&zc); 2922 return (zpool_standard_error(hdl, errno, msg)); 2923 } 2924 2925 /* 2926 * Similar to zpool_clear(), but takes a GUID (used by fmd). 2927 */ 2928 int 2929 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 2930 { 2931 zfs_cmd_t zc = { 0 }; 2932 char msg[1024]; 2933 libzfs_handle_t *hdl = zhp->zpool_hdl; 2934 2935 (void) snprintf(msg, sizeof (msg), 2936 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 2937 guid); 2938 2939 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2940 zc.zc_guid = guid; 2941 zc.zc_cookie = ZPOOL_NO_REWIND; 2942 2943 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 2944 return (0); 2945 2946 return (zpool_standard_error(hdl, errno, msg)); 2947 } 2948 2949 /* 2950 * Convert from a devid string to a path. 2951 */ 2952 static char * 2953 devid_to_path(char *devid_str) 2954 { 2955 ddi_devid_t devid; 2956 char *minor; 2957 char *path; 2958 devid_nmlist_t *list = NULL; 2959 int ret; 2960 2961 if (devid_str_decode(devid_str, &devid, &minor) != 0) 2962 return (NULL); 2963 2964 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 2965 2966 devid_str_free(minor); 2967 devid_free(devid); 2968 2969 if (ret != 0) 2970 return (NULL); 2971 2972 if ((path = strdup(list[0].devname)) == NULL) 2973 return (NULL); 2974 2975 devid_free_nmlist(list); 2976 2977 return (path); 2978 } 2979 2980 /* 2981 * Convert from a path to a devid string. 2982 */ 2983 static char * 2984 path_to_devid(const char *path) 2985 { 2986 int fd; 2987 ddi_devid_t devid; 2988 char *minor, *ret; 2989 2990 if ((fd = open(path, O_RDONLY)) < 0) 2991 return (NULL); 2992 2993 minor = NULL; 2994 ret = NULL; 2995 if (devid_get(fd, &devid) == 0) { 2996 if (devid_get_minor_name(fd, &minor) == 0) 2997 ret = devid_str_encode(devid, minor); 2998 if (minor != NULL) 2999 devid_str_free(minor); 3000 devid_free(devid); 3001 } 3002 (void) close(fd); 3003 3004 return (ret); 3005 } 3006 3007 /* 3008 * Issue the necessary ioctl() to update the stored path value for the vdev. We 3009 * ignore any failure here, since a common case is for an unprivileged user to 3010 * type 'zpool status', and we'll display the correct information anyway. 3011 */ 3012 static void 3013 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3014 { 3015 zfs_cmd_t zc = { 0 }; 3016 3017 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3018 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3019 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3020 &zc.zc_guid) == 0); 3021 3022 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3023 } 3024 3025 /* 3026 * Given a vdev, return the name to display in iostat. If the vdev has a path, 3027 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3028 * We also check if this is a whole disk, in which case we strip off the 3029 * trailing 's0' slice name. 3030 * 3031 * This routine is also responsible for identifying when disks have been 3032 * reconfigured in a new location. The kernel will have opened the device by 3033 * devid, but the path will still refer to the old location. To catch this, we 3034 * first do a path -> devid translation (which is fast for the common case). If 3035 * the devid matches, we're done. If not, we do a reverse devid -> path 3036 * translation and issue the appropriate ioctl() to update the path of the vdev. 3037 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3038 * of these checks. 3039 */ 3040 char * 3041 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 3042 boolean_t verbose) 3043 { 3044 char *path, *devid; 3045 uint64_t value; 3046 char buf[64]; 3047 vdev_stat_t *vs; 3048 uint_t vsc; 3049 3050 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3051 &value) == 0) { 3052 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3053 &value) == 0); 3054 (void) snprintf(buf, sizeof (buf), "%llu", 3055 (u_longlong_t)value); 3056 path = buf; 3057 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3058 3059 /* 3060 * If the device is dead (faulted, offline, etc) then don't 3061 * bother opening it. Otherwise we may be forcing the user to 3062 * open a misbehaving device, which can have undesirable 3063 * effects. 3064 */ 3065 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 3066 (uint64_t **)&vs, &vsc) != 0 || 3067 vs->vs_state >= VDEV_STATE_DEGRADED) && 3068 zhp != NULL && 3069 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3070 /* 3071 * Determine if the current path is correct. 3072 */ 3073 char *newdevid = path_to_devid(path); 3074 3075 if (newdevid == NULL || 3076 strcmp(devid, newdevid) != 0) { 3077 char *newpath; 3078 3079 if ((newpath = devid_to_path(devid)) != NULL) { 3080 /* 3081 * Update the path appropriately. 3082 */ 3083 set_path(zhp, nv, newpath); 3084 if (nvlist_add_string(nv, 3085 ZPOOL_CONFIG_PATH, newpath) == 0) 3086 verify(nvlist_lookup_string(nv, 3087 ZPOOL_CONFIG_PATH, 3088 &path) == 0); 3089 free(newpath); 3090 } 3091 } 3092 3093 if (newdevid) 3094 devid_str_free(newdevid); 3095 } 3096 3097 if (strncmp(path, "/dev/dsk/", 9) == 0) 3098 path += 9; 3099 3100 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3101 &value) == 0 && value) { 3102 int pathlen = strlen(path); 3103 char *tmp = zfs_strdup(hdl, path); 3104 3105 /* 3106 * If it starts with c#, and ends with "s0", chop 3107 * the "s0" off, or if it ends with "s0/old", remove 3108 * the "s0" from the middle. 3109 */ 3110 if (CTD_CHECK(tmp)) { 3111 if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 3112 tmp[pathlen - 2] = '\0'; 3113 } else if (pathlen > 6 && 3114 strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 3115 (void) strcpy(&tmp[pathlen - 6], 3116 "/old"); 3117 } 3118 } 3119 return (tmp); 3120 } 3121 } else { 3122 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 3123 3124 /* 3125 * If it's a raidz device, we need to stick in the parity level. 3126 */ 3127 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 3128 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 3129 &value) == 0); 3130 (void) snprintf(buf, sizeof (buf), "%s%llu", path, 3131 (u_longlong_t)value); 3132 path = buf; 3133 } 3134 3135 /* 3136 * We identify each top-level vdev by using a <type-id> 3137 * naming convention. 3138 */ 3139 if (verbose) { 3140 uint64_t id; 3141 3142 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 3143 &id) == 0); 3144 (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 3145 (u_longlong_t)id); 3146 path = buf; 3147 } 3148 } 3149 3150 return (zfs_strdup(hdl, path)); 3151 } 3152 3153 static int 3154 zbookmark_compare(const void *a, const void *b) 3155 { 3156 return (memcmp(a, b, sizeof (zbookmark_t))); 3157 } 3158 3159 /* 3160 * Retrieve the persistent error log, uniquify the members, and return to the 3161 * caller. 3162 */ 3163 int 3164 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3165 { 3166 zfs_cmd_t zc = { 0 }; 3167 uint64_t count; 3168 zbookmark_t *zb = NULL; 3169 int i; 3170 3171 /* 3172 * Retrieve the raw error list from the kernel. If the number of errors 3173 * has increased, allocate more space and continue until we get the 3174 * entire list. 3175 */ 3176 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3177 &count) == 0); 3178 if (count == 0) 3179 return (0); 3180 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 3181 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 3182 return (-1); 3183 zc.zc_nvlist_dst_size = count; 3184 (void) strcpy(zc.zc_name, zhp->zpool_name); 3185 for (;;) { 3186 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 3187 &zc) != 0) { 3188 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3189 if (errno == ENOMEM) { 3190 count = zc.zc_nvlist_dst_size; 3191 if ((zc.zc_nvlist_dst = (uintptr_t) 3192 zfs_alloc(zhp->zpool_hdl, count * 3193 sizeof (zbookmark_t))) == (uintptr_t)NULL) 3194 return (-1); 3195 } else { 3196 return (-1); 3197 } 3198 } else { 3199 break; 3200 } 3201 } 3202 3203 /* 3204 * Sort the resulting bookmarks. This is a little confusing due to the 3205 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3206 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3207 * _not_ copied as part of the process. So we point the start of our 3208 * array appropriate and decrement the total number of elements. 3209 */ 3210 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 3211 zc.zc_nvlist_dst_size; 3212 count -= zc.zc_nvlist_dst_size; 3213 3214 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 3215 3216 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3217 3218 /* 3219 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3220 */ 3221 for (i = 0; i < count; i++) { 3222 nvlist_t *nv; 3223 3224 /* ignoring zb_blkid and zb_level for now */ 3225 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3226 zb[i-1].zb_object == zb[i].zb_object) 3227 continue; 3228 3229 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 3230 goto nomem; 3231 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 3232 zb[i].zb_objset) != 0) { 3233 nvlist_free(nv); 3234 goto nomem; 3235 } 3236 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 3237 zb[i].zb_object) != 0) { 3238 nvlist_free(nv); 3239 goto nomem; 3240 } 3241 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 3242 nvlist_free(nv); 3243 goto nomem; 3244 } 3245 nvlist_free(nv); 3246 } 3247 3248 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3249 return (0); 3250 3251 nomem: 3252 free((void *)(uintptr_t)zc.zc_nvlist_dst); 3253 return (no_memory(zhp->zpool_hdl)); 3254 } 3255 3256 /* 3257 * Upgrade a ZFS pool to the latest on-disk version. 3258 */ 3259 int 3260 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3261 { 3262 zfs_cmd_t zc = { 0 }; 3263 libzfs_handle_t *hdl = zhp->zpool_hdl; 3264 3265 (void) strcpy(zc.zc_name, zhp->zpool_name); 3266 zc.zc_cookie = new_version; 3267 3268 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3269 return (zpool_standard_error_fmt(hdl, errno, 3270 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 3271 zhp->zpool_name)); 3272 return (0); 3273 } 3274 3275 void 3276 zpool_set_history_str(const char *subcommand, int argc, char **argv, 3277 char *history_str) 3278 { 3279 int i; 3280 3281 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 3282 for (i = 1; i < argc; i++) { 3283 if (strlen(history_str) + 1 + strlen(argv[i]) > 3284 HIS_MAX_RECORD_LEN) 3285 break; 3286 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 3287 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 3288 } 3289 } 3290 3291 /* 3292 * Stage command history for logging. 3293 */ 3294 int 3295 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 3296 { 3297 if (history_str == NULL) 3298 return (EINVAL); 3299 3300 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 3301 return (EINVAL); 3302 3303 if (hdl->libzfs_log_str != NULL) 3304 free(hdl->libzfs_log_str); 3305 3306 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 3307 return (no_memory(hdl)); 3308 3309 return (0); 3310 } 3311 3312 /* 3313 * Perform ioctl to get some command history of a pool. 3314 * 3315 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 3316 * logical offset of the history buffer to start reading from. 3317 * 3318 * Upon return, 'off' is the next logical offset to read from and 3319 * 'len' is the actual amount of bytes read into 'buf'. 3320 */ 3321 static int 3322 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 3323 { 3324 zfs_cmd_t zc = { 0 }; 3325 libzfs_handle_t *hdl = zhp->zpool_hdl; 3326 3327 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3328 3329 zc.zc_history = (uint64_t)(uintptr_t)buf; 3330 zc.zc_history_len = *len; 3331 zc.zc_history_offset = *off; 3332 3333 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 3334 switch (errno) { 3335 case EPERM: 3336 return (zfs_error_fmt(hdl, EZFS_PERM, 3337 dgettext(TEXT_DOMAIN, 3338 "cannot show history for pool '%s'"), 3339 zhp->zpool_name)); 3340 case ENOENT: 3341 return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 3342 dgettext(TEXT_DOMAIN, "cannot get history for pool " 3343 "'%s'"), zhp->zpool_name)); 3344 case ENOTSUP: 3345 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3346 dgettext(TEXT_DOMAIN, "cannot get history for pool " 3347 "'%s', pool must be upgraded"), zhp->zpool_name)); 3348 default: 3349 return (zpool_standard_error_fmt(hdl, errno, 3350 dgettext(TEXT_DOMAIN, 3351 "cannot get history for '%s'"), zhp->zpool_name)); 3352 } 3353 } 3354 3355 *len = zc.zc_history_len; 3356 *off = zc.zc_history_offset; 3357 3358 return (0); 3359 } 3360 3361 /* 3362 * Process the buffer of nvlists, unpacking and storing each nvlist record 3363 * into 'records'. 'leftover' is set to the number of bytes that weren't 3364 * processed as there wasn't a complete record. 3365 */ 3366 int 3367 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 3368 nvlist_t ***records, uint_t *numrecords) 3369 { 3370 uint64_t reclen; 3371 nvlist_t *nv; 3372 int i; 3373 3374 while (bytes_read > sizeof (reclen)) { 3375 3376 /* get length of packed record (stored as little endian) */ 3377 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 3378 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 3379 3380 if (bytes_read < sizeof (reclen) + reclen) 3381 break; 3382 3383 /* unpack record */ 3384 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 3385 return (ENOMEM); 3386 bytes_read -= sizeof (reclen) + reclen; 3387 buf += sizeof (reclen) + reclen; 3388 3389 /* add record to nvlist array */ 3390 (*numrecords)++; 3391 if (ISP2(*numrecords + 1)) { 3392 *records = realloc(*records, 3393 *numrecords * 2 * sizeof (nvlist_t *)); 3394 } 3395 (*records)[*numrecords - 1] = nv; 3396 } 3397 3398 *leftover = bytes_read; 3399 return (0); 3400 } 3401 3402 #define HIS_BUF_LEN (128*1024) 3403 3404 /* 3405 * Retrieve the command history of a pool. 3406 */ 3407 int 3408 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 3409 { 3410 char buf[HIS_BUF_LEN]; 3411 uint64_t off = 0; 3412 nvlist_t **records = NULL; 3413 uint_t numrecords = 0; 3414 int err, i; 3415 3416 do { 3417 uint64_t bytes_read = sizeof (buf); 3418 uint64_t leftover; 3419 3420 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 3421 break; 3422 3423 /* if nothing else was read in, we're at EOF, just return */ 3424 if (!bytes_read) 3425 break; 3426 3427 if ((err = zpool_history_unpack(buf, bytes_read, 3428 &leftover, &records, &numrecords)) != 0) 3429 break; 3430 off -= leftover; 3431 3432 /* CONSTCOND */ 3433 } while (1); 3434 3435 if (!err) { 3436 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 3437 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 3438 records, numrecords) == 0); 3439 } 3440 for (i = 0; i < numrecords; i++) 3441 nvlist_free(records[i]); 3442 free(records); 3443 3444 return (err); 3445 } 3446 3447 void 3448 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 3449 char *pathname, size_t len) 3450 { 3451 zfs_cmd_t zc = { 0 }; 3452 boolean_t mounted = B_FALSE; 3453 char *mntpnt = NULL; 3454 char dsname[MAXNAMELEN]; 3455 3456 if (dsobj == 0) { 3457 /* special case for the MOS */ 3458 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 3459 return; 3460 } 3461 3462 /* get the dataset's name */ 3463 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3464 zc.zc_obj = dsobj; 3465 if (ioctl(zhp->zpool_hdl->libzfs_fd, 3466 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 3467 /* just write out a path of two object numbers */ 3468 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 3469 dsobj, obj); 3470 return; 3471 } 3472 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 3473 3474 /* find out if the dataset is mounted */ 3475 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 3476 3477 /* get the corrupted object's path */ 3478 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 3479 zc.zc_obj = obj; 3480 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 3481 &zc) == 0) { 3482 if (mounted) { 3483 (void) snprintf(pathname, len, "%s%s", mntpnt, 3484 zc.zc_value); 3485 } else { 3486 (void) snprintf(pathname, len, "%s:%s", 3487 dsname, zc.zc_value); 3488 } 3489 } else { 3490 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 3491 } 3492 free(mntpnt); 3493 } 3494 3495 /* 3496 * Read the EFI label from the config, if a label does not exist then 3497 * pass back the error to the caller. If the caller has passed a non-NULL 3498 * diskaddr argument then we set it to the starting address of the EFI 3499 * partition. 3500 */ 3501 static int 3502 read_efi_label(nvlist_t *config, diskaddr_t *sb) 3503 { 3504 char *path; 3505 int fd; 3506 char diskname[MAXPATHLEN]; 3507 int err = -1; 3508 3509 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 3510 return (err); 3511 3512 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 3513 strrchr(path, '/')); 3514 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 3515 struct dk_gpt *vtoc; 3516 3517 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 3518 if (sb != NULL) 3519 *sb = vtoc->efi_parts[0].p_start; 3520 efi_free(vtoc); 3521 } 3522 (void) close(fd); 3523 } 3524 return (err); 3525 } 3526 3527 /* 3528 * determine where a partition starts on a disk in the current 3529 * configuration 3530 */ 3531 static diskaddr_t 3532 find_start_block(nvlist_t *config) 3533 { 3534 nvlist_t **child; 3535 uint_t c, children; 3536 diskaddr_t sb = MAXOFFSET_T; 3537 uint64_t wholedisk; 3538 3539 if (nvlist_lookup_nvlist_array(config, 3540 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 3541 if (nvlist_lookup_uint64(config, 3542 ZPOOL_CONFIG_WHOLE_DISK, 3543 &wholedisk) != 0 || !wholedisk) { 3544 return (MAXOFFSET_T); 3545 } 3546 if (read_efi_label(config, &sb) < 0) 3547 sb = MAXOFFSET_T; 3548 return (sb); 3549 } 3550 3551 for (c = 0; c < children; c++) { 3552 sb = find_start_block(child[c]); 3553 if (sb != MAXOFFSET_T) { 3554 return (sb); 3555 } 3556 } 3557 return (MAXOFFSET_T); 3558 } 3559 3560 /* 3561 * Label an individual disk. The name provided is the short name, 3562 * stripped of any leading /dev path. 3563 */ 3564 int 3565 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 3566 { 3567 char path[MAXPATHLEN]; 3568 struct dk_gpt *vtoc; 3569 int fd; 3570 size_t resv = EFI_MIN_RESV_SIZE; 3571 uint64_t slice_size; 3572 diskaddr_t start_block; 3573 char errbuf[1024]; 3574 3575 /* prepare an error message just in case */ 3576 (void) snprintf(errbuf, sizeof (errbuf), 3577 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3578 3579 if (zhp) { 3580 nvlist_t *nvroot; 3581 3582 if (pool_is_bootable(zhp)) { 3583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3584 "EFI labeled devices are not supported on root " 3585 "pools.")); 3586 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3587 } 3588 3589 verify(nvlist_lookup_nvlist(zhp->zpool_config, 3590 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3591 3592 if (zhp->zpool_start_block == 0) 3593 start_block = find_start_block(nvroot); 3594 else 3595 start_block = zhp->zpool_start_block; 3596 zhp->zpool_start_block = start_block; 3597 } else { 3598 /* new pool */ 3599 start_block = NEW_START_BLOCK; 3600 } 3601 3602 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 3603 BACKUP_SLICE); 3604 3605 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 3606 /* 3607 * This shouldn't happen. We've long since verified that this 3608 * is a valid device. 3609 */ 3610 zfs_error_aux(hdl, 3611 dgettext(TEXT_DOMAIN, "unable to open device")); 3612 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 3613 } 3614 3615 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 3616 /* 3617 * The only way this can fail is if we run out of memory, or we 3618 * were unable to read the disk's capacity 3619 */ 3620 if (errno == ENOMEM) 3621 (void) no_memory(hdl); 3622 3623 (void) close(fd); 3624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3625 "unable to read disk capacity"), name); 3626 3627 return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 3628 } 3629 3630 slice_size = vtoc->efi_last_u_lba + 1; 3631 slice_size -= EFI_MIN_RESV_SIZE; 3632 if (start_block == MAXOFFSET_T) 3633 start_block = NEW_START_BLOCK; 3634 slice_size -= start_block; 3635 3636 vtoc->efi_parts[0].p_start = start_block; 3637 vtoc->efi_parts[0].p_size = slice_size; 3638 3639 /* 3640 * Why we use V_USR: V_BACKUP confuses users, and is considered 3641 * disposable by some EFI utilities (since EFI doesn't have a backup 3642 * slice). V_UNASSIGNED is supposed to be used only for zero size 3643 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 3644 * etc. were all pretty specific. V_USR is as close to reality as we 3645 * can get, in the absence of V_OTHER. 3646 */ 3647 vtoc->efi_parts[0].p_tag = V_USR; 3648 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 3649 3650 vtoc->efi_parts[8].p_start = slice_size + start_block; 3651 vtoc->efi_parts[8].p_size = resv; 3652 vtoc->efi_parts[8].p_tag = V_RESERVED; 3653 3654 if (efi_write(fd, vtoc) != 0) { 3655 /* 3656 * Some block drivers (like pcata) may not support EFI 3657 * GPT labels. Print out a helpful error message dir- 3658 * ecting the user to manually label the disk and give 3659 * a specific slice. 3660 */ 3661 (void) close(fd); 3662 efi_free(vtoc); 3663 3664 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3665 "try using fdisk(1M) and then provide a specific slice")); 3666 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 3667 } 3668 3669 (void) close(fd); 3670 efi_free(vtoc); 3671 return (0); 3672 } 3673 3674 static boolean_t 3675 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3676 { 3677 char *type; 3678 nvlist_t **child; 3679 uint_t children, c; 3680 3681 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3682 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 3683 strcmp(type, VDEV_TYPE_FILE) == 0 || 3684 strcmp(type, VDEV_TYPE_LOG) == 0 || 3685 strcmp(type, VDEV_TYPE_HOLE) == 0 || 3686 strcmp(type, VDEV_TYPE_MISSING) == 0) { 3687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3688 "vdev type '%s' is not supported"), type); 3689 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3690 return (B_FALSE); 3691 } 3692 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3693 &child, &children) == 0) { 3694 for (c = 0; c < children; c++) { 3695 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3696 return (B_FALSE); 3697 } 3698 } 3699 return (B_TRUE); 3700 } 3701 3702 /* 3703 * check if this zvol is allowable for use as a dump device; zero if 3704 * it is, > 0 if it isn't, < 0 if it isn't a zvol 3705 */ 3706 int 3707 zvol_check_dump_config(char *arg) 3708 { 3709 zpool_handle_t *zhp = NULL; 3710 nvlist_t *config, *nvroot; 3711 char *p, *volname; 3712 nvlist_t **top; 3713 uint_t toplevels; 3714 libzfs_handle_t *hdl; 3715 char errbuf[1024]; 3716 char poolname[ZPOOL_MAXNAMELEN]; 3717 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3718 int ret = 1; 3719 3720 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3721 return (-1); 3722 } 3723 3724 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3725 "dump is not supported on device '%s'"), arg); 3726 3727 if ((hdl = libzfs_init()) == NULL) 3728 return (1); 3729 libzfs_print_on_error(hdl, B_TRUE); 3730 3731 volname = arg + pathlen; 3732 3733 /* check the configuration of the pool */ 3734 if ((p = strchr(volname, '/')) == NULL) { 3735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3736 "malformed dataset name")); 3737 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3738 return (1); 3739 } else if (p - volname >= ZFS_MAXNAMELEN) { 3740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3741 "dataset name is too long")); 3742 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 3743 return (1); 3744 } else { 3745 (void) strncpy(poolname, volname, p - volname); 3746 poolname[p - volname] = '\0'; 3747 } 3748 3749 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 3750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3751 "could not open pool '%s'"), poolname); 3752 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 3753 goto out; 3754 } 3755 config = zpool_get_config(zhp, NULL); 3756 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3757 &nvroot) != 0) { 3758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3759 "could not obtain vdev configuration for '%s'"), poolname); 3760 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3761 goto out; 3762 } 3763 3764 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3765 &top, &toplevels) == 0); 3766 if (toplevels != 1) { 3767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3768 "'%s' has multiple top level vdevs"), poolname); 3769 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3770 goto out; 3771 } 3772 3773 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 3774 goto out; 3775 } 3776 ret = 0; 3777 3778 out: 3779 if (zhp) 3780 zpool_close(zhp); 3781 libzfs_fini(hdl); 3782 return (ret); 3783 } 3784