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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <alloca.h> 28 #include <assert.h> 29 #include <ctype.h> 30 #include <errno.h> 31 #include <devid.h> 32 #include <dirent.h> 33 #include <fcntl.h> 34 #include <libintl.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <zone.h> 40 #include <sys/efi_partition.h> 41 #include <sys/vtoc.h> 42 #include <sys/zfs_ioctl.h> 43 #include <sys/zio.h> 44 #include <strings.h> 45 46 #include "zfs_namecheck.h" 47 #include "zfs_prop.h" 48 #include "libzfs_impl.h" 49 50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 51 52 #if defined(__i386) || defined(__amd64) 53 #define BOOTCMD "installgrub(1M)" 54 #else 55 #define BOOTCMD "installboot(1M)" 56 #endif 57 58 /* 59 * ==================================================================== 60 * zpool property functions 61 * ==================================================================== 62 */ 63 64 static int 65 zpool_get_all_props(zpool_handle_t *zhp) 66 { 67 zfs_cmd_t zc = { 0 }; 68 libzfs_handle_t *hdl = zhp->zpool_hdl; 69 70 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 71 72 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 73 return (-1); 74 75 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 76 if (errno == ENOMEM) { 77 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 78 zcmd_free_nvlists(&zc); 79 return (-1); 80 } 81 } else { 82 zcmd_free_nvlists(&zc); 83 return (-1); 84 } 85 } 86 87 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 88 zcmd_free_nvlists(&zc); 89 return (-1); 90 } 91 92 zcmd_free_nvlists(&zc); 93 94 return (0); 95 } 96 97 static int 98 zpool_props_refresh(zpool_handle_t *zhp) 99 { 100 nvlist_t *old_props; 101 102 old_props = zhp->zpool_props; 103 104 if (zpool_get_all_props(zhp) != 0) 105 return (-1); 106 107 nvlist_free(old_props); 108 return (0); 109 } 110 111 static char * 112 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 113 zprop_source_t *src) 114 { 115 nvlist_t *nv, *nvl; 116 uint64_t ival; 117 char *value; 118 zprop_source_t source; 119 120 nvl = zhp->zpool_props; 121 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 122 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 123 source = ival; 124 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 125 } else { 126 source = ZPROP_SRC_DEFAULT; 127 if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 128 value = "-"; 129 } 130 131 if (src) 132 *src = source; 133 134 return (value); 135 } 136 137 uint64_t 138 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 139 { 140 nvlist_t *nv, *nvl; 141 uint64_t value; 142 zprop_source_t source; 143 144 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 145 /* 146 * zpool_get_all_props() has most likely failed because 147 * the pool is faulted, but if all we need is the top level 148 * vdev's guid then get it from the zhp config nvlist. 149 */ 150 if ((prop == ZPOOL_PROP_GUID) && 151 (nvlist_lookup_nvlist(zhp->zpool_config, 152 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 153 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 154 == 0)) { 155 return (value); 156 } 157 return (zpool_prop_default_numeric(prop)); 158 } 159 160 nvl = zhp->zpool_props; 161 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 162 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 163 source = value; 164 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 165 } else { 166 source = ZPROP_SRC_DEFAULT; 167 value = zpool_prop_default_numeric(prop); 168 } 169 170 if (src) 171 *src = source; 172 173 return (value); 174 } 175 176 /* 177 * Map VDEV STATE to printed strings. 178 */ 179 char * 180 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 181 { 182 switch (state) { 183 case VDEV_STATE_CLOSED: 184 case VDEV_STATE_OFFLINE: 185 return (gettext("OFFLINE")); 186 case VDEV_STATE_REMOVED: 187 return (gettext("REMOVED")); 188 case VDEV_STATE_CANT_OPEN: 189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 190 return (gettext("FAULTED")); 191 else 192 return (gettext("UNAVAIL")); 193 case VDEV_STATE_FAULTED: 194 return (gettext("FAULTED")); 195 case VDEV_STATE_DEGRADED: 196 return (gettext("DEGRADED")); 197 case VDEV_STATE_HEALTHY: 198 return (gettext("ONLINE")); 199 } 200 201 return (gettext("UNKNOWN")); 202 } 203 204 /* 205 * Get a zpool property value for 'prop' and return the value in 206 * a pre-allocated buffer. 207 */ 208 int 209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 210 zprop_source_t *srctype) 211 { 212 uint64_t intval; 213 const char *strval; 214 zprop_source_t src = ZPROP_SRC_NONE; 215 nvlist_t *nvroot; 216 vdev_stat_t *vs; 217 uint_t vsc; 218 219 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 220 switch (prop) { 221 case ZPOOL_PROP_NAME: 222 (void) strlcpy(buf, zpool_get_name(zhp), len); 223 break; 224 225 case ZPOOL_PROP_HEALTH: 226 (void) strlcpy(buf, "FAULTED", len); 227 break; 228 229 case ZPOOL_PROP_GUID: 230 intval = zpool_get_prop_int(zhp, prop, &src); 231 (void) snprintf(buf, len, "%llu", intval); 232 break; 233 234 case ZPOOL_PROP_ALTROOT: 235 case ZPOOL_PROP_CACHEFILE: 236 if (zhp->zpool_props != NULL || 237 zpool_get_all_props(zhp) == 0) { 238 (void) strlcpy(buf, 239 zpool_get_prop_string(zhp, prop, &src), 240 len); 241 if (srctype != NULL) 242 *srctype = src; 243 return (0); 244 } 245 /* FALLTHROUGH */ 246 default: 247 (void) strlcpy(buf, "-", len); 248 break; 249 } 250 251 if (srctype != NULL) 252 *srctype = src; 253 return (0); 254 } 255 256 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 257 prop != ZPOOL_PROP_NAME) 258 return (-1); 259 260 switch (zpool_prop_get_type(prop)) { 261 case PROP_TYPE_STRING: 262 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 263 len); 264 break; 265 266 case PROP_TYPE_NUMBER: 267 intval = zpool_get_prop_int(zhp, prop, &src); 268 269 switch (prop) { 270 case ZPOOL_PROP_SIZE: 271 case ZPOOL_PROP_USED: 272 case ZPOOL_PROP_AVAILABLE: 273 (void) zfs_nicenum(intval, buf, len); 274 break; 275 276 case ZPOOL_PROP_CAPACITY: 277 (void) snprintf(buf, len, "%llu%%", 278 (u_longlong_t)intval); 279 break; 280 281 case ZPOOL_PROP_HEALTH: 282 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 283 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 284 verify(nvlist_lookup_uint64_array(nvroot, 285 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 286 287 (void) strlcpy(buf, zpool_state_to_name(intval, 288 vs->vs_aux), len); 289 break; 290 default: 291 (void) snprintf(buf, len, "%llu", intval); 292 } 293 break; 294 295 case PROP_TYPE_INDEX: 296 intval = zpool_get_prop_int(zhp, prop, &src); 297 if (zpool_prop_index_to_string(prop, intval, &strval) 298 != 0) 299 return (-1); 300 (void) strlcpy(buf, strval, len); 301 break; 302 303 default: 304 abort(); 305 } 306 307 if (srctype) 308 *srctype = src; 309 310 return (0); 311 } 312 313 /* 314 * Check if the bootfs name has the same pool name as it is set to. 315 * Assuming bootfs is a valid dataset name. 316 */ 317 static boolean_t 318 bootfs_name_valid(const char *pool, char *bootfs) 319 { 320 int len = strlen(pool); 321 322 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 323 return (B_FALSE); 324 325 if (strncmp(pool, bootfs, len) == 0 && 326 (bootfs[len] == '/' || bootfs[len] == '\0')) 327 return (B_TRUE); 328 329 return (B_FALSE); 330 } 331 332 /* 333 * Inspect the configuration to determine if any of the devices contain 334 * an EFI label. 335 */ 336 static boolean_t 337 pool_uses_efi(nvlist_t *config) 338 { 339 nvlist_t **child; 340 uint_t c, children; 341 342 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 343 &child, &children) != 0) 344 return (read_efi_label(config, NULL) >= 0); 345 346 for (c = 0; c < children; c++) { 347 if (pool_uses_efi(child[c])) 348 return (B_TRUE); 349 } 350 return (B_FALSE); 351 } 352 353 static boolean_t 354 pool_is_bootable(zpool_handle_t *zhp) 355 { 356 char bootfs[ZPOOL_MAXNAMELEN]; 357 358 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 359 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 360 sizeof (bootfs)) != 0); 361 } 362 363 364 /* 365 * Given an nvlist of zpool properties to be set, validate that they are 366 * correct, and parse any numeric properties (index, boolean, etc) if they are 367 * specified as strings. 368 */ 369 static nvlist_t * 370 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 371 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 372 { 373 nvpair_t *elem; 374 nvlist_t *retprops; 375 zpool_prop_t prop; 376 char *strval; 377 uint64_t intval; 378 char *slash; 379 struct stat64 statbuf; 380 zpool_handle_t *zhp; 381 nvlist_t *nvroot; 382 383 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 384 (void) no_memory(hdl); 385 return (NULL); 386 } 387 388 elem = NULL; 389 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 390 const char *propname = nvpair_name(elem); 391 392 /* 393 * Make sure this property is valid and applies to this type. 394 */ 395 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 396 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 397 "invalid property '%s'"), propname); 398 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 399 goto error; 400 } 401 402 if (zpool_prop_readonly(prop)) { 403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 404 "is readonly"), propname); 405 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 406 goto error; 407 } 408 409 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 410 &strval, &intval, errbuf) != 0) 411 goto error; 412 413 /* 414 * Perform additional checking for specific properties. 415 */ 416 switch (prop) { 417 case ZPOOL_PROP_VERSION: 418 if (intval < version || intval > SPA_VERSION) { 419 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 420 "property '%s' number %d is invalid."), 421 propname, intval); 422 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 423 goto error; 424 } 425 break; 426 427 case ZPOOL_PROP_BOOTFS: 428 if (create_or_import) { 429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 430 "property '%s' cannot be set at creation " 431 "or import time"), propname); 432 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 433 goto error; 434 } 435 436 if (version < SPA_VERSION_BOOTFS) { 437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 438 "pool must be upgraded to support " 439 "'%s' property"), propname); 440 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 441 goto error; 442 } 443 444 /* 445 * bootfs property value has to be a dataset name and 446 * the dataset has to be in the same pool as it sets to. 447 */ 448 if (strval[0] != '\0' && !bootfs_name_valid(poolname, 449 strval)) { 450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 451 "is an invalid name"), strval); 452 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 453 goto error; 454 } 455 456 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 458 "could not open pool '%s'"), poolname); 459 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 460 goto error; 461 } 462 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 463 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 464 465 /* 466 * bootfs property cannot be set on a disk which has 467 * been EFI labeled. 468 */ 469 if (pool_uses_efi(nvroot)) { 470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 471 "property '%s' not supported on " 472 "EFI labeled devices"), propname); 473 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 474 zpool_close(zhp); 475 goto error; 476 } 477 zpool_close(zhp); 478 break; 479 480 case ZPOOL_PROP_ALTROOT: 481 if (!create_or_import) { 482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 483 "property '%s' can only be set during pool " 484 "creation or import"), propname); 485 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 486 goto error; 487 } 488 489 if (strval[0] != '/') { 490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 491 "bad alternate root '%s'"), strval); 492 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 493 goto error; 494 } 495 break; 496 497 case ZPOOL_PROP_CACHEFILE: 498 if (strval[0] == '\0') 499 break; 500 501 if (strcmp(strval, "none") == 0) 502 break; 503 504 if (strval[0] != '/') { 505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 506 "property '%s' must be empty, an " 507 "absolute path, or 'none'"), propname); 508 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 509 goto error; 510 } 511 512 slash = strrchr(strval, '/'); 513 514 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 515 strcmp(slash, "/..") == 0) { 516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 517 "'%s' is not a valid file"), strval); 518 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 519 goto error; 520 } 521 522 *slash = '\0'; 523 524 if (strval[0] != '\0' && 525 (stat64(strval, &statbuf) != 0 || 526 !S_ISDIR(statbuf.st_mode))) { 527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 528 "'%s' is not a valid directory"), 529 strval); 530 (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 531 goto error; 532 } 533 534 *slash = '/'; 535 break; 536 } 537 } 538 539 return (retprops); 540 error: 541 nvlist_free(retprops); 542 return (NULL); 543 } 544 545 /* 546 * Set zpool property : propname=propval. 547 */ 548 int 549 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 550 { 551 zfs_cmd_t zc = { 0 }; 552 int ret = -1; 553 char errbuf[1024]; 554 nvlist_t *nvl = NULL; 555 nvlist_t *realprops; 556 uint64_t version; 557 558 (void) snprintf(errbuf, sizeof (errbuf), 559 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 560 zhp->zpool_name); 561 562 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 563 return (no_memory(zhp->zpool_hdl)); 564 565 if (nvlist_add_string(nvl, propname, propval) != 0) { 566 nvlist_free(nvl); 567 return (no_memory(zhp->zpool_hdl)); 568 } 569 570 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 571 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 572 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 573 nvlist_free(nvl); 574 return (-1); 575 } 576 577 nvlist_free(nvl); 578 nvl = realprops; 579 580 /* 581 * Execute the corresponding ioctl() to set this property. 582 */ 583 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 584 585 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 586 nvlist_free(nvl); 587 return (-1); 588 } 589 590 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 591 592 zcmd_free_nvlists(&zc); 593 nvlist_free(nvl); 594 595 if (ret) 596 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 597 else 598 (void) zpool_props_refresh(zhp); 599 600 return (ret); 601 } 602 603 int 604 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 605 { 606 libzfs_handle_t *hdl = zhp->zpool_hdl; 607 zprop_list_t *entry; 608 char buf[ZFS_MAXPROPLEN]; 609 610 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 611 return (-1); 612 613 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 614 615 if (entry->pl_fixed) 616 continue; 617 618 if (entry->pl_prop != ZPROP_INVAL && 619 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 620 NULL) == 0) { 621 if (strlen(buf) > entry->pl_width) 622 entry->pl_width = strlen(buf); 623 } 624 } 625 626 return (0); 627 } 628 629 630 /* 631 * Validate the given pool name, optionally putting an extended error message in 632 * 'buf'. 633 */ 634 boolean_t 635 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 636 { 637 namecheck_err_t why; 638 char what; 639 int ret; 640 641 ret = pool_namecheck(pool, &why, &what); 642 643 /* 644 * The rules for reserved pool names were extended at a later point. 645 * But we need to support users with existing pools that may now be 646 * invalid. So we only check for this expanded set of names during a 647 * create (or import), and only in userland. 648 */ 649 if (ret == 0 && !isopen && 650 (strncmp(pool, "mirror", 6) == 0 || 651 strncmp(pool, "raidz", 5) == 0 || 652 strncmp(pool, "spare", 5) == 0 || 653 strcmp(pool, "log") == 0)) { 654 if (hdl != NULL) 655 zfs_error_aux(hdl, 656 dgettext(TEXT_DOMAIN, "name is reserved")); 657 return (B_FALSE); 658 } 659 660 661 if (ret != 0) { 662 if (hdl != NULL) { 663 switch (why) { 664 case NAME_ERR_TOOLONG: 665 zfs_error_aux(hdl, 666 dgettext(TEXT_DOMAIN, "name is too long")); 667 break; 668 669 case NAME_ERR_INVALCHAR: 670 zfs_error_aux(hdl, 671 dgettext(TEXT_DOMAIN, "invalid character " 672 "'%c' in pool name"), what); 673 break; 674 675 case NAME_ERR_NOLETTER: 676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 677 "name must begin with a letter")); 678 break; 679 680 case NAME_ERR_RESERVED: 681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 682 "name is reserved")); 683 break; 684 685 case NAME_ERR_DISKLIKE: 686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 687 "pool name is reserved")); 688 break; 689 690 case NAME_ERR_LEADING_SLASH: 691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 692 "leading slash in name")); 693 break; 694 695 case NAME_ERR_EMPTY_COMPONENT: 696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 697 "empty component in name")); 698 break; 699 700 case NAME_ERR_TRAILING_SLASH: 701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 702 "trailing slash in name")); 703 break; 704 705 case NAME_ERR_MULTIPLE_AT: 706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 707 "multiple '@' delimiters in name")); 708 break; 709 710 } 711 } 712 return (B_FALSE); 713 } 714 715 return (B_TRUE); 716 } 717 718 /* 719 * Open a handle to the given pool, even if the pool is currently in the FAULTED 720 * state. 721 */ 722 zpool_handle_t * 723 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 724 { 725 zpool_handle_t *zhp; 726 boolean_t missing; 727 728 /* 729 * Make sure the pool name is valid. 730 */ 731 if (!zpool_name_valid(hdl, B_TRUE, pool)) { 732 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 733 dgettext(TEXT_DOMAIN, "cannot open '%s'"), 734 pool); 735 return (NULL); 736 } 737 738 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 739 return (NULL); 740 741 zhp->zpool_hdl = hdl; 742 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 743 744 if (zpool_refresh_stats(zhp, &missing) != 0) { 745 zpool_close(zhp); 746 return (NULL); 747 } 748 749 if (missing) { 750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 751 (void) zfs_error_fmt(hdl, EZFS_NOENT, 752 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 753 zpool_close(zhp); 754 return (NULL); 755 } 756 757 return (zhp); 758 } 759 760 /* 761 * Like the above, but silent on error. Used when iterating over pools (because 762 * the configuration cache may be out of date). 763 */ 764 int 765 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 766 { 767 zpool_handle_t *zhp; 768 boolean_t missing; 769 770 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 771 return (-1); 772 773 zhp->zpool_hdl = hdl; 774 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 775 776 if (zpool_refresh_stats(zhp, &missing) != 0) { 777 zpool_close(zhp); 778 return (-1); 779 } 780 781 if (missing) { 782 zpool_close(zhp); 783 *ret = NULL; 784 return (0); 785 } 786 787 *ret = zhp; 788 return (0); 789 } 790 791 /* 792 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 793 * state. 794 */ 795 zpool_handle_t * 796 zpool_open(libzfs_handle_t *hdl, const char *pool) 797 { 798 zpool_handle_t *zhp; 799 800 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 801 return (NULL); 802 803 if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 804 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 805 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 806 zpool_close(zhp); 807 return (NULL); 808 } 809 810 return (zhp); 811 } 812 813 /* 814 * Close the handle. Simply frees the memory associated with the handle. 815 */ 816 void 817 zpool_close(zpool_handle_t *zhp) 818 { 819 if (zhp->zpool_config) 820 nvlist_free(zhp->zpool_config); 821 if (zhp->zpool_old_config) 822 nvlist_free(zhp->zpool_old_config); 823 if (zhp->zpool_props) 824 nvlist_free(zhp->zpool_props); 825 free(zhp); 826 } 827 828 /* 829 * Return the name of the pool. 830 */ 831 const char * 832 zpool_get_name(zpool_handle_t *zhp) 833 { 834 return (zhp->zpool_name); 835 } 836 837 838 /* 839 * Return the state of the pool (ACTIVE or UNAVAILABLE) 840 */ 841 int 842 zpool_get_state(zpool_handle_t *zhp) 843 { 844 return (zhp->zpool_state); 845 } 846 847 /* 848 * Create the named pool, using the provided vdev list. It is assumed 849 * that the consumer has already validated the contents of the nvlist, so we 850 * don't have to worry about error semantics. 851 */ 852 int 853 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 854 nvlist_t *props, nvlist_t *fsprops) 855 { 856 zfs_cmd_t zc = { 0 }; 857 nvlist_t *zc_fsprops = NULL; 858 nvlist_t *zc_props = NULL; 859 char msg[1024]; 860 char *altroot; 861 int ret = -1; 862 863 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 864 "cannot create '%s'"), pool); 865 866 if (!zpool_name_valid(hdl, B_FALSE, pool)) 867 return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 868 869 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 870 return (-1); 871 872 if (props) { 873 if ((zc_props = zpool_valid_proplist(hdl, pool, props, 874 SPA_VERSION_1, B_TRUE, msg)) == NULL) { 875 goto create_failed; 876 } 877 } 878 879 if (fsprops) { 880 uint64_t zoned; 881 char *zonestr; 882 883 zoned = ((nvlist_lookup_string(fsprops, 884 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 885 strcmp(zonestr, "on") == 0); 886 887 if ((zc_fsprops = zfs_valid_proplist(hdl, 888 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 889 goto create_failed; 890 } 891 if (!zc_props && 892 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 893 goto create_failed; 894 } 895 if (nvlist_add_nvlist(zc_props, 896 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 897 goto create_failed; 898 } 899 } 900 901 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 902 goto create_failed; 903 904 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 905 906 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 907 908 zcmd_free_nvlists(&zc); 909 nvlist_free(zc_props); 910 nvlist_free(zc_fsprops); 911 912 switch (errno) { 913 case EBUSY: 914 /* 915 * This can happen if the user has specified the same 916 * device multiple times. We can't reliably detect this 917 * until we try to add it and see we already have a 918 * label. 919 */ 920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 921 "one or more vdevs refer to the same device")); 922 return (zfs_error(hdl, EZFS_BADDEV, msg)); 923 924 case EOVERFLOW: 925 /* 926 * This occurs when one of the devices is below 927 * SPA_MINDEVSIZE. Unfortunately, we can't detect which 928 * device was the problem device since there's no 929 * reliable way to determine device size from userland. 930 */ 931 { 932 char buf[64]; 933 934 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 935 936 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 937 "one or more devices is less than the " 938 "minimum size (%s)"), buf); 939 } 940 return (zfs_error(hdl, EZFS_BADDEV, msg)); 941 942 case ENOSPC: 943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 944 "one or more devices is out of space")); 945 return (zfs_error(hdl, EZFS_BADDEV, msg)); 946 947 case ENOTBLK: 948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 949 "cache device must be a disk or disk slice")); 950 return (zfs_error(hdl, EZFS_BADDEV, msg)); 951 952 default: 953 return (zpool_standard_error(hdl, errno, msg)); 954 } 955 } 956 957 /* 958 * If this is an alternate root pool, then we automatically set the 959 * mountpoint of the root dataset to be '/'. 960 */ 961 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 962 &altroot) == 0) { 963 zfs_handle_t *zhp; 964 965 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 966 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 967 "/") == 0); 968 969 zfs_close(zhp); 970 } 971 972 create_failed: 973 zcmd_free_nvlists(&zc); 974 nvlist_free(zc_props); 975 nvlist_free(zc_fsprops); 976 return (ret); 977 } 978 979 /* 980 * Destroy the given pool. It is up to the caller to ensure that there are no 981 * datasets left in the pool. 982 */ 983 int 984 zpool_destroy(zpool_handle_t *zhp) 985 { 986 zfs_cmd_t zc = { 0 }; 987 zfs_handle_t *zfp = NULL; 988 libzfs_handle_t *hdl = zhp->zpool_hdl; 989 char msg[1024]; 990 991 if (zhp->zpool_state == POOL_STATE_ACTIVE && 992 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 993 ZFS_TYPE_FILESYSTEM)) == NULL) 994 return (-1); 995 996 if (zpool_remove_zvol_links(zhp) != 0) 997 return (-1); 998 999 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1000 1001 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 1002 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1003 "cannot destroy '%s'"), zhp->zpool_name); 1004 1005 if (errno == EROFS) { 1006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1007 "one or more devices is read only")); 1008 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1009 } else { 1010 (void) zpool_standard_error(hdl, errno, msg); 1011 } 1012 1013 if (zfp) 1014 zfs_close(zfp); 1015 return (-1); 1016 } 1017 1018 if (zfp) { 1019 remove_mountpoint(zfp); 1020 zfs_close(zfp); 1021 } 1022 1023 return (0); 1024 } 1025 1026 /* 1027 * Add the given vdevs to the pool. The caller must have already performed the 1028 * necessary verification to ensure that the vdev specification is well-formed. 1029 */ 1030 int 1031 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1032 { 1033 zfs_cmd_t zc = { 0 }; 1034 int ret; 1035 libzfs_handle_t *hdl = zhp->zpool_hdl; 1036 char msg[1024]; 1037 nvlist_t **spares, **l2cache; 1038 uint_t nspares, nl2cache; 1039 1040 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1041 "cannot add to '%s'"), zhp->zpool_name); 1042 1043 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1044 SPA_VERSION_SPARES && 1045 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1046 &spares, &nspares) == 0) { 1047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1048 "upgraded to add hot spares")); 1049 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1050 } 1051 1052 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 1053 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 1054 uint64_t s; 1055 1056 for (s = 0; s < nspares; s++) { 1057 char *path; 1058 1059 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 1060 &path) == 0 && pool_uses_efi(spares[s])) { 1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1062 "device '%s' contains an EFI label and " 1063 "cannot be used on root pools."), 1064 zpool_vdev_name(hdl, NULL, spares[s])); 1065 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1066 } 1067 } 1068 } 1069 1070 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1071 SPA_VERSION_L2CACHE && 1072 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1073 &l2cache, &nl2cache) == 0) { 1074 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1075 "upgraded to add cache devices")); 1076 return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1077 } 1078 1079 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1080 return (-1); 1081 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1082 1083 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1084 switch (errno) { 1085 case EBUSY: 1086 /* 1087 * This can happen if the user has specified the same 1088 * device multiple times. We can't reliably detect this 1089 * until we try to add it and see we already have a 1090 * label. 1091 */ 1092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1093 "one or more vdevs refer to the same device")); 1094 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1095 break; 1096 1097 case EOVERFLOW: 1098 /* 1099 * This occurrs when one of the devices is below 1100 * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1101 * device was the problem device since there's no 1102 * reliable way to determine device size from userland. 1103 */ 1104 { 1105 char buf[64]; 1106 1107 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1108 1109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1110 "device is less than the minimum " 1111 "size (%s)"), buf); 1112 } 1113 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1114 break; 1115 1116 case ENOTSUP: 1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1118 "pool must be upgraded to add these vdevs")); 1119 (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1120 break; 1121 1122 case EDOM: 1123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1124 "root pool can not have multiple vdevs" 1125 " or separate logs")); 1126 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1127 break; 1128 1129 case ENOTBLK: 1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1131 "cache device must be a disk or disk slice")); 1132 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1133 break; 1134 1135 default: 1136 (void) zpool_standard_error(hdl, errno, msg); 1137 } 1138 1139 ret = -1; 1140 } else { 1141 ret = 0; 1142 } 1143 1144 zcmd_free_nvlists(&zc); 1145 1146 return (ret); 1147 } 1148 1149 /* 1150 * Exports the pool from the system. The caller must ensure that there are no 1151 * mounted datasets in the pool. 1152 */ 1153 int 1154 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1155 { 1156 zfs_cmd_t zc = { 0 }; 1157 char msg[1024]; 1158 1159 if (zpool_remove_zvol_links(zhp) != 0) 1160 return (-1); 1161 1162 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1163 "cannot export '%s'"), zhp->zpool_name); 1164 1165 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1166 zc.zc_cookie = force; 1167 zc.zc_guid = hardforce; 1168 1169 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 1170 switch (errno) { 1171 case EXDEV: 1172 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 1173 "use '-f' to override the following errors:\n" 1174 "'%s' has an active shared spare which could be" 1175 " used by other pools once '%s' is exported."), 1176 zhp->zpool_name, zhp->zpool_name); 1177 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 1178 msg)); 1179 default: 1180 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 1181 msg)); 1182 } 1183 } 1184 1185 return (0); 1186 } 1187 1188 int 1189 zpool_export(zpool_handle_t *zhp, boolean_t force) 1190 { 1191 return (zpool_export_common(zhp, force, B_FALSE)); 1192 } 1193 1194 int 1195 zpool_export_force(zpool_handle_t *zhp) 1196 { 1197 return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1198 } 1199 1200 /* 1201 * zpool_import() is a contracted interface. Should be kept the same 1202 * if possible. 1203 * 1204 * Applications should use zpool_import_props() to import a pool with 1205 * new properties value to be set. 1206 */ 1207 int 1208 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1209 char *altroot) 1210 { 1211 nvlist_t *props = NULL; 1212 int ret; 1213 1214 if (altroot != NULL) { 1215 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1216 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1217 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1218 newname)); 1219 } 1220 1221 if (nvlist_add_string(props, 1222 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1223 nvlist_add_string(props, 1224 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1225 nvlist_free(props); 1226 return (zfs_error_fmt(hdl, EZFS_NOMEM, 1227 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1228 newname)); 1229 } 1230 } 1231 1232 ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 1233 if (props) 1234 nvlist_free(props); 1235 return (ret); 1236 } 1237 1238 /* 1239 * Import the given pool using the known configuration and a list of 1240 * properties to be set. The configuration should have come from 1241 * zpool_find_import(). The 'newname' parameters control whether the pool 1242 * is imported with a different name. 1243 */ 1244 int 1245 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1246 nvlist_t *props, boolean_t importfaulted) 1247 { 1248 zfs_cmd_t zc = { 0 }; 1249 char *thename; 1250 char *origname; 1251 int ret; 1252 char errbuf[1024]; 1253 1254 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1255 &origname) == 0); 1256 1257 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1258 "cannot import pool '%s'"), origname); 1259 1260 if (newname != NULL) { 1261 if (!zpool_name_valid(hdl, B_FALSE, newname)) 1262 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 1263 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1264 newname)); 1265 thename = (char *)newname; 1266 } else { 1267 thename = origname; 1268 } 1269 1270 if (props) { 1271 uint64_t version; 1272 1273 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1274 &version) == 0); 1275 1276 if ((props = zpool_valid_proplist(hdl, origname, 1277 props, version, B_TRUE, errbuf)) == NULL) { 1278 return (-1); 1279 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1280 nvlist_free(props); 1281 return (-1); 1282 } 1283 } 1284 1285 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1286 1287 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1288 &zc.zc_guid) == 0); 1289 1290 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1291 nvlist_free(props); 1292 return (-1); 1293 } 1294 1295 zc.zc_cookie = (uint64_t)importfaulted; 1296 ret = 0; 1297 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1298 char desc[1024]; 1299 if (newname == NULL) 1300 (void) snprintf(desc, sizeof (desc), 1301 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1302 thename); 1303 else 1304 (void) snprintf(desc, sizeof (desc), 1305 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1306 origname, thename); 1307 1308 switch (errno) { 1309 case ENOTSUP: 1310 /* 1311 * Unsupported version. 1312 */ 1313 (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1314 break; 1315 1316 case EINVAL: 1317 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1318 break; 1319 1320 default: 1321 (void) zpool_standard_error(hdl, errno, desc); 1322 } 1323 1324 ret = -1; 1325 } else { 1326 zpool_handle_t *zhp; 1327 1328 /* 1329 * This should never fail, but play it safe anyway. 1330 */ 1331 if (zpool_open_silent(hdl, thename, &zhp) != 0) { 1332 ret = -1; 1333 } else if (zhp != NULL) { 1334 ret = zpool_create_zvol_links(zhp); 1335 zpool_close(zhp); 1336 } 1337 1338 } 1339 1340 zcmd_free_nvlists(&zc); 1341 nvlist_free(props); 1342 1343 return (ret); 1344 } 1345 1346 /* 1347 * Scrub the pool. 1348 */ 1349 int 1350 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1351 { 1352 zfs_cmd_t zc = { 0 }; 1353 char msg[1024]; 1354 libzfs_handle_t *hdl = zhp->zpool_hdl; 1355 1356 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1357 zc.zc_cookie = type; 1358 1359 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1360 return (0); 1361 1362 (void) snprintf(msg, sizeof (msg), 1363 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1364 1365 if (errno == EBUSY) 1366 return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1367 else 1368 return (zpool_standard_error(hdl, errno, msg)); 1369 } 1370 1371 /* 1372 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1373 * spare; but FALSE if its an INUSE spare. 1374 */ 1375 static nvlist_t * 1376 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 1377 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 1378 { 1379 uint_t c, children; 1380 nvlist_t **child; 1381 uint64_t theguid, present; 1382 char *path; 1383 uint64_t wholedisk = 0; 1384 nvlist_t *ret; 1385 uint64_t is_log; 1386 1387 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 1388 1389 if (search == NULL && 1390 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 1391 /* 1392 * If the device has never been present since import, the only 1393 * reliable way to match the vdev is by GUID. 1394 */ 1395 if (theguid == guid) 1396 return (nv); 1397 } else if (search != NULL && 1398 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 1399 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1400 &wholedisk); 1401 if (wholedisk) { 1402 /* 1403 * For whole disks, the internal path has 's0', but the 1404 * path passed in by the user doesn't. 1405 */ 1406 if (strlen(search) == strlen(path) - 2 && 1407 strncmp(search, path, strlen(search)) == 0) 1408 return (nv); 1409 } else if (strcmp(search, path) == 0) { 1410 return (nv); 1411 } 1412 } 1413 1414 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1415 &child, &children) != 0) 1416 return (NULL); 1417 1418 for (c = 0; c < children; c++) { 1419 if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1420 avail_spare, l2cache, NULL)) != NULL) { 1421 /* 1422 * The 'is_log' value is only set for the toplevel 1423 * vdev, not the leaf vdevs. So we always lookup the 1424 * log device from the root of the vdev tree (where 1425 * 'log' is non-NULL). 1426 */ 1427 if (log != NULL && 1428 nvlist_lookup_uint64(child[c], 1429 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 1430 is_log) { 1431 *log = B_TRUE; 1432 } 1433 return (ret); 1434 } 1435 } 1436 1437 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1438 &child, &children) == 0) { 1439 for (c = 0; c < children; c++) { 1440 if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1441 avail_spare, l2cache, NULL)) != NULL) { 1442 *avail_spare = B_TRUE; 1443 return (ret); 1444 } 1445 } 1446 } 1447 1448 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1449 &child, &children) == 0) { 1450 for (c = 0; c < children; c++) { 1451 if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1452 avail_spare, l2cache, NULL)) != NULL) { 1453 *l2cache = B_TRUE; 1454 return (ret); 1455 } 1456 } 1457 } 1458 1459 return (NULL); 1460 } 1461 1462 nvlist_t * 1463 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1464 boolean_t *l2cache, boolean_t *log) 1465 { 1466 char buf[MAXPATHLEN]; 1467 const char *search; 1468 char *end; 1469 nvlist_t *nvroot; 1470 uint64_t guid; 1471 1472 guid = strtoull(path, &end, 10); 1473 if (guid != 0 && *end == '\0') { 1474 search = NULL; 1475 } else if (path[0] != '/') { 1476 (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1477 search = buf; 1478 } else { 1479 search = path; 1480 } 1481 1482 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1483 &nvroot) == 0); 1484 1485 *avail_spare = B_FALSE; 1486 *l2cache = B_FALSE; 1487 if (log != NULL) 1488 *log = B_FALSE; 1489 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 1490 l2cache, log)); 1491 } 1492 1493 static int 1494 vdev_online(nvlist_t *nv) 1495 { 1496 uint64_t ival; 1497 1498 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 1499 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 1500 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 1501 return (0); 1502 1503 return (1); 1504 } 1505 1506 /* 1507 * Helper function for zpool_get_config_physpath(). 1508 */ 1509 static int 1510 vdev_get_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 1511 size_t *bytes_written) 1512 { 1513 size_t bytes_left, pos, rsz; 1514 char *tmppath; 1515 const char *format; 1516 1517 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 1518 &tmppath) != 0) 1519 return (EZFS_NODEVICE); 1520 1521 pos = *bytes_written; 1522 bytes_left = physpath_size - pos; 1523 format = (pos == 0) ? "%s" : " %s"; 1524 1525 rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 1526 *bytes_written += rsz; 1527 1528 if (rsz >= bytes_left) { 1529 /* if physpath was not copied properly, clear it */ 1530 if (bytes_left != 0) { 1531 physpath[pos] = 0; 1532 } 1533 return (EZFS_NOSPC); 1534 } 1535 return (0); 1536 } 1537 1538 /* 1539 * Get phys_path for a root pool config. 1540 * Return 0 on success; non-zero on failure. 1541 */ 1542 static int 1543 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 1544 { 1545 size_t rsz; 1546 nvlist_t *vdev_root; 1547 nvlist_t **child; 1548 nvlist_t **child2; 1549 uint_t count; 1550 char *type; 1551 int j, ret; 1552 1553 rsz = 0; 1554 1555 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1556 &vdev_root) != 0) 1557 return (EZFS_INVALCONFIG); 1558 1559 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 1560 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 1561 &child, &count) != 0) 1562 return (EZFS_INVALCONFIG); 1563 1564 /* 1565 * root pool can not have EFI labeled disks and can only have 1566 * a single top-level vdev. 1567 */ 1568 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 1569 pool_uses_efi(vdev_root)) 1570 return (EZFS_POOL_INVALARG); 1571 1572 if (nvlist_lookup_string(child[0], ZPOOL_CONFIG_TYPE, &type) != 0) 1573 return (EZFS_INVALCONFIG); 1574 1575 if (strcmp(type, VDEV_TYPE_DISK) == 0) { 1576 if (vdev_online(child[0])) { 1577 if ((ret = vdev_get_physpath(child[0], physpath, 1578 phypath_size, &rsz)) != 0) 1579 return (ret); 1580 } 1581 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) { 1582 1583 if (nvlist_lookup_nvlist_array(child[0], 1584 ZPOOL_CONFIG_CHILDREN, &child2, &count) != 0) 1585 return (EZFS_INVALCONFIG); 1586 1587 for (j = 0; j < count; j++) { 1588 if (nvlist_lookup_string(child2[j], ZPOOL_CONFIG_TYPE, 1589 &type) != 0) 1590 return (EZFS_INVALCONFIG); 1591 1592 if (strcmp(type, VDEV_TYPE_DISK) != 0) 1593 return (EZFS_POOL_INVALARG); 1594 1595 if (vdev_online(child2[j])) { 1596 ret = vdev_get_physpath(child2[j], 1597 physpath, phypath_size, &rsz); 1598 1599 if (ret == EZFS_NOSPC) 1600 return (ret); 1601 } 1602 } 1603 } else { 1604 return (EZFS_POOL_INVALARG); 1605 } 1606 1607 /* No online devices */ 1608 if (rsz == 0) 1609 return (EZFS_NODEVICE); 1610 1611 return (0); 1612 } 1613 1614 /* 1615 * Get phys_path for a root pool 1616 * Return 0 on success; non-zero on failure. 1617 */ 1618 int 1619 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 1620 { 1621 return (zpool_get_config_physpath(zhp->zpool_config, physpath, 1622 phypath_size)); 1623 } 1624 1625 /* 1626 * Returns TRUE if the given guid corresponds to the given type. 1627 * This is used to check for hot spares (INUSE or not), and level 2 cache 1628 * devices. 1629 */ 1630 static boolean_t 1631 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 1632 { 1633 uint64_t target_guid; 1634 nvlist_t *nvroot; 1635 nvlist_t **list; 1636 uint_t count; 1637 int i; 1638 1639 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1640 &nvroot) == 0); 1641 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 1642 for (i = 0; i < count; i++) { 1643 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 1644 &target_guid) == 0); 1645 if (guid == target_guid) 1646 return (B_TRUE); 1647 } 1648 } 1649 1650 return (B_FALSE); 1651 } 1652 1653 /* 1654 * Bring the specified vdev online. The 'flags' parameter is a set of the 1655 * ZFS_ONLINE_* flags. 1656 */ 1657 int 1658 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 1659 vdev_state_t *newstate) 1660 { 1661 zfs_cmd_t zc = { 0 }; 1662 char msg[1024]; 1663 nvlist_t *tgt; 1664 boolean_t avail_spare, l2cache; 1665 libzfs_handle_t *hdl = zhp->zpool_hdl; 1666 1667 (void) snprintf(msg, sizeof (msg), 1668 dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1669 1670 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1671 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 1672 NULL)) == NULL) 1673 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1674 1675 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1676 1677 if (avail_spare || 1678 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 1679 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1680 1681 zc.zc_cookie = VDEV_STATE_ONLINE; 1682 zc.zc_obj = flags; 1683 1684 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 1685 return (zpool_standard_error(hdl, errno, msg)); 1686 1687 *newstate = zc.zc_cookie; 1688 return (0); 1689 } 1690 1691 /* 1692 * Take the specified vdev offline 1693 */ 1694 int 1695 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1696 { 1697 zfs_cmd_t zc = { 0 }; 1698 char msg[1024]; 1699 nvlist_t *tgt; 1700 boolean_t avail_spare, l2cache; 1701 libzfs_handle_t *hdl = zhp->zpool_hdl; 1702 1703 (void) snprintf(msg, sizeof (msg), 1704 dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 1705 1706 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1707 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 1708 NULL)) == NULL) 1709 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1710 1711 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1712 1713 if (avail_spare || 1714 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 1715 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1716 1717 zc.zc_cookie = VDEV_STATE_OFFLINE; 1718 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 1719 1720 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1721 return (0); 1722 1723 switch (errno) { 1724 case EBUSY: 1725 1726 /* 1727 * There are no other replicas of this device. 1728 */ 1729 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 1730 1731 default: 1732 return (zpool_standard_error(hdl, errno, msg)); 1733 } 1734 } 1735 1736 /* 1737 * Mark the given vdev faulted. 1738 */ 1739 int 1740 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 1741 { 1742 zfs_cmd_t zc = { 0 }; 1743 char msg[1024]; 1744 libzfs_handle_t *hdl = zhp->zpool_hdl; 1745 1746 (void) snprintf(msg, sizeof (msg), 1747 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 1748 1749 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1750 zc.zc_guid = guid; 1751 zc.zc_cookie = VDEV_STATE_FAULTED; 1752 1753 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1754 return (0); 1755 1756 switch (errno) { 1757 case EBUSY: 1758 1759 /* 1760 * There are no other replicas of this device. 1761 */ 1762 return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 1763 1764 default: 1765 return (zpool_standard_error(hdl, errno, msg)); 1766 } 1767 1768 } 1769 1770 /* 1771 * Mark the given vdev degraded. 1772 */ 1773 int 1774 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 1775 { 1776 zfs_cmd_t zc = { 0 }; 1777 char msg[1024]; 1778 libzfs_handle_t *hdl = zhp->zpool_hdl; 1779 1780 (void) snprintf(msg, sizeof (msg), 1781 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 1782 1783 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1784 zc.zc_guid = guid; 1785 zc.zc_cookie = VDEV_STATE_DEGRADED; 1786 1787 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1788 return (0); 1789 1790 return (zpool_standard_error(hdl, errno, msg)); 1791 } 1792 1793 /* 1794 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 1795 * a hot spare. 1796 */ 1797 static boolean_t 1798 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 1799 { 1800 nvlist_t **child; 1801 uint_t c, children; 1802 char *type; 1803 1804 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 1805 &children) == 0) { 1806 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 1807 &type) == 0); 1808 1809 if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 1810 children == 2 && child[which] == tgt) 1811 return (B_TRUE); 1812 1813 for (c = 0; c < children; c++) 1814 if (is_replacing_spare(child[c], tgt, which)) 1815 return (B_TRUE); 1816 } 1817 1818 return (B_FALSE); 1819 } 1820 1821 /* 1822 * Attach new_disk (fully described by nvroot) to old_disk. 1823 * If 'replacing' is specified, the new disk will replace the old one. 1824 */ 1825 int 1826 zpool_vdev_attach(zpool_handle_t *zhp, 1827 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1828 { 1829 zfs_cmd_t zc = { 0 }; 1830 char msg[1024]; 1831 int ret; 1832 nvlist_t *tgt; 1833 boolean_t avail_spare, l2cache, islog; 1834 uint64_t val; 1835 char *path, *newname; 1836 nvlist_t **child; 1837 uint_t children; 1838 nvlist_t *config_root; 1839 libzfs_handle_t *hdl = zhp->zpool_hdl; 1840 boolean_t rootpool = pool_is_bootable(zhp); 1841 1842 if (replacing) 1843 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1844 "cannot replace %s with %s"), old_disk, new_disk); 1845 else 1846 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1847 "cannot attach %s to %s"), new_disk, old_disk); 1848 1849 /* 1850 * If this is a root pool, make sure that we're not attaching an 1851 * EFI labeled device. 1852 */ 1853 if (rootpool && pool_uses_efi(nvroot)) { 1854 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1855 "EFI labeled devices are not supported on root pools.")); 1856 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1857 } 1858 1859 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1860 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 1861 &islog)) == 0) 1862 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1863 1864 if (avail_spare) 1865 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1866 1867 if (l2cache) 1868 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 1869 1870 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1871 zc.zc_cookie = replacing; 1872 1873 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1874 &child, &children) != 0 || children != 1) { 1875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1876 "new device must be a single disk")); 1877 return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 1878 } 1879 1880 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 1881 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 1882 1883 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 1884 return (-1); 1885 1886 /* 1887 * If the target is a hot spare that has been swapped in, we can only 1888 * replace it with another hot spare. 1889 */ 1890 if (replacing && 1891 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 1892 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 1893 NULL) == NULL || !avail_spare) && 1894 is_replacing_spare(config_root, tgt, 1)) { 1895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1896 "can only be replaced by another hot spare")); 1897 free(newname); 1898 return (zfs_error(hdl, EZFS_BADTARGET, msg)); 1899 } 1900 1901 /* 1902 * If we are attempting to replace a spare, it canot be applied to an 1903 * already spared device. 1904 */ 1905 if (replacing && 1906 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 1907 zpool_find_vdev(zhp, newname, &avail_spare, 1908 &l2cache, NULL) != NULL && avail_spare && 1909 is_replacing_spare(config_root, tgt, 0)) { 1910 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1911 "device has already been replaced with a spare")); 1912 free(newname); 1913 return (zfs_error(hdl, EZFS_BADTARGET, msg)); 1914 } 1915 1916 free(newname); 1917 1918 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1919 return (-1); 1920 1921 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1922 1923 zcmd_free_nvlists(&zc); 1924 1925 if (ret == 0) { 1926 if (rootpool) { 1927 /* 1928 * XXX - This should be removed once we can 1929 * automatically install the bootblocks on the 1930 * newly attached disk. 1931 */ 1932 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 1933 "be sure to invoke %s to make '%s' bootable.\n"), 1934 BOOTCMD, new_disk); 1935 } 1936 return (0); 1937 } 1938 1939 switch (errno) { 1940 case ENOTSUP: 1941 /* 1942 * Can't attach to or replace this type of vdev. 1943 */ 1944 if (replacing) { 1945 if (islog) 1946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1947 "cannot replace a log with a spare")); 1948 else 1949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1950 "cannot replace a replacing device")); 1951 } else { 1952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1953 "can only attach to mirrors and top-level " 1954 "disks")); 1955 } 1956 (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1957 break; 1958 1959 case EINVAL: 1960 /* 1961 * The new device must be a single disk. 1962 */ 1963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1964 "new device must be a single disk")); 1965 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1966 break; 1967 1968 case EBUSY: 1969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 1970 new_disk); 1971 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1972 break; 1973 1974 case EOVERFLOW: 1975 /* 1976 * The new device is too small. 1977 */ 1978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1979 "device is too small")); 1980 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1981 break; 1982 1983 case EDOM: 1984 /* 1985 * The new device has a different alignment requirement. 1986 */ 1987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1988 "devices have different sector alignment")); 1989 (void) zfs_error(hdl, EZFS_BADDEV, msg); 1990 break; 1991 1992 case ENAMETOOLONG: 1993 /* 1994 * The resulting top-level vdev spec won't fit in the label. 1995 */ 1996 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1997 break; 1998 1999 default: 2000 (void) zpool_standard_error(hdl, errno, msg); 2001 } 2002 2003 return (-1); 2004 } 2005 2006 /* 2007 * Detach the specified device. 2008 */ 2009 int 2010 zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2011 { 2012 zfs_cmd_t zc = { 0 }; 2013 char msg[1024]; 2014 nvlist_t *tgt; 2015 boolean_t avail_spare, l2cache; 2016 libzfs_handle_t *hdl = zhp->zpool_hdl; 2017 2018 (void) snprintf(msg, sizeof (msg), 2019 dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2020 2021 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2022 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2023 NULL)) == 0) 2024 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2025 2026 if (avail_spare) 2027 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2028 2029 if (l2cache) 2030 return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2031 2032 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2033 2034 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2035 return (0); 2036 2037 switch (errno) { 2038 2039 case ENOTSUP: 2040 /* 2041 * Can't detach from this type of vdev. 2042 */ 2043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 2044 "applicable to mirror and replacing vdevs")); 2045 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2046 break; 2047 2048 case EBUSY: 2049 /* 2050 * There are no other replicas of this device. 2051 */ 2052 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2053 break; 2054 2055 default: 2056 (void) zpool_standard_error(hdl, errno, msg); 2057 } 2058 2059 return (-1); 2060 } 2061 2062 /* 2063 * Remove the given device. Currently, this is supported only for hot spares 2064 * and level 2 cache devices. 2065 */ 2066 int 2067 zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 2068 { 2069 zfs_cmd_t zc = { 0 }; 2070 char msg[1024]; 2071 nvlist_t *tgt; 2072 boolean_t avail_spare, l2cache; 2073 libzfs_handle_t *hdl = zhp->zpool_hdl; 2074 2075 (void) snprintf(msg, sizeof (msg), 2076 dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 2077 2078 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2079 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2080 NULL)) == 0) 2081 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2082 2083 if (!avail_spare && !l2cache) { 2084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2085 "only inactive hot spares or cache devices " 2086 "can be removed")); 2087 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2088 } 2089 2090 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2091 2092 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 2093 return (0); 2094 2095 return (zpool_standard_error(hdl, errno, msg)); 2096 } 2097 2098 /* 2099 * Clear the errors for the pool, or the particular device if specified. 2100 */ 2101 int 2102 zpool_clear(zpool_handle_t *zhp, const char *path) 2103 { 2104 zfs_cmd_t zc = { 0 }; 2105 char msg[1024]; 2106 nvlist_t *tgt; 2107 boolean_t avail_spare, l2cache; 2108 libzfs_handle_t *hdl = zhp->zpool_hdl; 2109 2110 if (path) 2111 (void) snprintf(msg, sizeof (msg), 2112 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2113 path); 2114 else 2115 (void) snprintf(msg, sizeof (msg), 2116 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2117 zhp->zpool_name); 2118 2119 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2120 if (path) { 2121 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 2122 &l2cache, NULL)) == 0) 2123 return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2124 2125 /* 2126 * Don't allow error clearing for hot spares. Do allow 2127 * error clearing for l2cache devices. 2128 */ 2129 if (avail_spare) 2130 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2131 2132 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 2133 &zc.zc_guid) == 0); 2134 } 2135 2136 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 2137 return (0); 2138 2139 return (zpool_standard_error(hdl, errno, msg)); 2140 } 2141 2142 /* 2143 * Similar to zpool_clear(), but takes a GUID (used by fmd). 2144 */ 2145 int 2146 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 2147 { 2148 zfs_cmd_t zc = { 0 }; 2149 char msg[1024]; 2150 libzfs_handle_t *hdl = zhp->zpool_hdl; 2151 2152 (void) snprintf(msg, sizeof (msg), 2153 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 2154 guid); 2155 2156 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2157 zc.zc_guid = guid; 2158 2159 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 2160 return (0); 2161 2162 return (zpool_standard_error(hdl, errno, msg)); 2163 } 2164 2165 /* 2166 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 2167 * hierarchy. 2168 */ 2169 int 2170 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 2171 void *data) 2172 { 2173 libzfs_handle_t *hdl = zhp->zpool_hdl; 2174 char (*paths)[MAXPATHLEN]; 2175 size_t size = 4; 2176 int curr, fd, base, ret = 0; 2177 DIR *dirp; 2178 struct dirent *dp; 2179 struct stat st; 2180 2181 if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 2182 return (errno == ENOENT ? 0 : -1); 2183 2184 if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 2185 int err = errno; 2186 (void) close(base); 2187 return (err == ENOENT ? 0 : -1); 2188 } 2189 2190 /* 2191 * Oddly this wasn't a directory -- ignore that failure since we 2192 * know there are no links lower in the (non-existant) hierarchy. 2193 */ 2194 if (!S_ISDIR(st.st_mode)) { 2195 (void) close(base); 2196 return (0); 2197 } 2198 2199 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 2200 (void) close(base); 2201 return (-1); 2202 } 2203 2204 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 2205 curr = 0; 2206 2207 while (curr >= 0) { 2208 if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 2209 goto err; 2210 2211 if (S_ISDIR(st.st_mode)) { 2212 if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 2213 goto err; 2214 2215 if ((dirp = fdopendir(fd)) == NULL) { 2216 (void) close(fd); 2217 goto err; 2218 } 2219 2220 while ((dp = readdir(dirp)) != NULL) { 2221 if (dp->d_name[0] == '.') 2222 continue; 2223 2224 if (curr + 1 == size) { 2225 paths = zfs_realloc(hdl, paths, 2226 size * sizeof (paths[0]), 2227 size * 2 * sizeof (paths[0])); 2228 if (paths == NULL) { 2229 (void) closedir(dirp); 2230 (void) close(fd); 2231 goto err; 2232 } 2233 2234 size *= 2; 2235 } 2236 2237 (void) strlcpy(paths[curr + 1], paths[curr], 2238 sizeof (paths[curr + 1])); 2239 (void) strlcat(paths[curr], "/", 2240 sizeof (paths[curr])); 2241 (void) strlcat(paths[curr], dp->d_name, 2242 sizeof (paths[curr])); 2243 curr++; 2244 } 2245 2246 (void) closedir(dirp); 2247 2248 } else { 2249 if ((ret = cb(paths[curr], data)) != 0) 2250 break; 2251 } 2252 2253 curr--; 2254 } 2255 2256 free(paths); 2257 (void) close(base); 2258 2259 return (ret); 2260 2261 err: 2262 free(paths); 2263 (void) close(base); 2264 return (-1); 2265 } 2266 2267 typedef struct zvol_cb { 2268 zpool_handle_t *zcb_pool; 2269 boolean_t zcb_create; 2270 } zvol_cb_t; 2271 2272 /*ARGSUSED*/ 2273 static int 2274 do_zvol_create(zfs_handle_t *zhp, void *data) 2275 { 2276 int ret = 0; 2277 2278 if (ZFS_IS_VOLUME(zhp)) { 2279 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 2280 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 2281 } 2282 2283 if (ret == 0) 2284 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2285 2286 zfs_close(zhp); 2287 2288 return (ret); 2289 } 2290 2291 /* 2292 * Iterate over all zvols in the pool and make any necessary minor nodes. 2293 */ 2294 int 2295 zpool_create_zvol_links(zpool_handle_t *zhp) 2296 { 2297 zfs_handle_t *zfp; 2298 int ret; 2299 2300 /* 2301 * If the pool is unavailable, just return success. 2302 */ 2303 if ((zfp = make_dataset_handle(zhp->zpool_hdl, 2304 zhp->zpool_name)) == NULL) 2305 return (0); 2306 2307 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2308 2309 zfs_close(zfp); 2310 return (ret); 2311 } 2312 2313 static int 2314 do_zvol_remove(const char *dataset, void *data) 2315 { 2316 zpool_handle_t *zhp = data; 2317 2318 return (zvol_remove_link(zhp->zpool_hdl, dataset)); 2319 } 2320 2321 /* 2322 * Iterate over all zvols in the pool and remove any minor nodes. We iterate 2323 * by examining the /dev links so that a corrupted pool doesn't impede this 2324 * operation. 2325 */ 2326 int 2327 zpool_remove_zvol_links(zpool_handle_t *zhp) 2328 { 2329 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2330 } 2331 2332 /* 2333 * Convert from a devid string to a path. 2334 */ 2335 static char * 2336 devid_to_path(char *devid_str) 2337 { 2338 ddi_devid_t devid; 2339 char *minor; 2340 char *path; 2341 devid_nmlist_t *list = NULL; 2342 int ret; 2343 2344 if (devid_str_decode(devid_str, &devid, &minor) != 0) 2345 return (NULL); 2346 2347 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 2348 2349 devid_str_free(minor); 2350 devid_free(devid); 2351 2352 if (ret != 0) 2353 return (NULL); 2354 2355 if ((path = strdup(list[0].devname)) == NULL) 2356 return (NULL); 2357 2358 devid_free_nmlist(list); 2359 2360 return (path); 2361 } 2362 2363 /* 2364 * Convert from a path to a devid string. 2365 */ 2366 static char * 2367 path_to_devid(const char *path) 2368 { 2369 int fd; 2370 ddi_devid_t devid; 2371 char *minor, *ret; 2372 2373 if ((fd = open(path, O_RDONLY)) < 0) 2374 return (NULL); 2375 2376 minor = NULL; 2377 ret = NULL; 2378 if (devid_get(fd, &devid) == 0) { 2379 if (devid_get_minor_name(fd, &minor) == 0) 2380 ret = devid_str_encode(devid, minor); 2381 if (minor != NULL) 2382 devid_str_free(minor); 2383 devid_free(devid); 2384 } 2385 (void) close(fd); 2386 2387 return (ret); 2388 } 2389 2390 /* 2391 * Issue the necessary ioctl() to update the stored path value for the vdev. We 2392 * ignore any failure here, since a common case is for an unprivileged user to 2393 * type 'zpool status', and we'll display the correct information anyway. 2394 */ 2395 static void 2396 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 2397 { 2398 zfs_cmd_t zc = { 0 }; 2399 2400 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2401 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 2402 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2403 &zc.zc_guid) == 0); 2404 2405 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 2406 } 2407 2408 /* 2409 * Given a vdev, return the name to display in iostat. If the vdev has a path, 2410 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 2411 * We also check if this is a whole disk, in which case we strip off the 2412 * trailing 's0' slice name. 2413 * 2414 * This routine is also responsible for identifying when disks have been 2415 * reconfigured in a new location. The kernel will have opened the device by 2416 * devid, but the path will still refer to the old location. To catch this, we 2417 * first do a path -> devid translation (which is fast for the common case). If 2418 * the devid matches, we're done. If not, we do a reverse devid -> path 2419 * translation and issue the appropriate ioctl() to update the path of the vdev. 2420 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 2421 * of these checks. 2422 */ 2423 char * 2424 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 2425 { 2426 char *path, *devid; 2427 uint64_t value; 2428 char buf[64]; 2429 vdev_stat_t *vs; 2430 uint_t vsc; 2431 2432 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 2433 &value) == 0) { 2434 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2435 &value) == 0); 2436 (void) snprintf(buf, sizeof (buf), "%llu", 2437 (u_longlong_t)value); 2438 path = buf; 2439 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 2440 2441 /* 2442 * If the device is dead (faulted, offline, etc) then don't 2443 * bother opening it. Otherwise we may be forcing the user to 2444 * open a misbehaving device, which can have undesirable 2445 * effects. 2446 */ 2447 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 2448 (uint64_t **)&vs, &vsc) != 0 || 2449 vs->vs_state >= VDEV_STATE_DEGRADED) && 2450 zhp != NULL && 2451 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 2452 /* 2453 * Determine if the current path is correct. 2454 */ 2455 char *newdevid = path_to_devid(path); 2456 2457 if (newdevid == NULL || 2458 strcmp(devid, newdevid) != 0) { 2459 char *newpath; 2460 2461 if ((newpath = devid_to_path(devid)) != NULL) { 2462 /* 2463 * Update the path appropriately. 2464 */ 2465 set_path(zhp, nv, newpath); 2466 if (nvlist_add_string(nv, 2467 ZPOOL_CONFIG_PATH, newpath) == 0) 2468 verify(nvlist_lookup_string(nv, 2469 ZPOOL_CONFIG_PATH, 2470 &path) == 0); 2471 free(newpath); 2472 } 2473 } 2474 2475 if (newdevid) 2476 devid_str_free(newdevid); 2477 } 2478 2479 if (strncmp(path, "/dev/dsk/", 9) == 0) 2480 path += 9; 2481 2482 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2483 &value) == 0 && value) { 2484 char *tmp = zfs_strdup(hdl, path); 2485 if (tmp == NULL) 2486 return (NULL); 2487 tmp[strlen(path) - 2] = '\0'; 2488 return (tmp); 2489 } 2490 } else { 2491 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 2492 2493 /* 2494 * If it's a raidz device, we need to stick in the parity level. 2495 */ 2496 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 2497 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 2498 &value) == 0); 2499 (void) snprintf(buf, sizeof (buf), "%s%llu", path, 2500 (u_longlong_t)value); 2501 path = buf; 2502 } 2503 } 2504 2505 return (zfs_strdup(hdl, path)); 2506 } 2507 2508 static int 2509 zbookmark_compare(const void *a, const void *b) 2510 { 2511 return (memcmp(a, b, sizeof (zbookmark_t))); 2512 } 2513 2514 /* 2515 * Retrieve the persistent error log, uniquify the members, and return to the 2516 * caller. 2517 */ 2518 int 2519 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 2520 { 2521 zfs_cmd_t zc = { 0 }; 2522 uint64_t count; 2523 zbookmark_t *zb = NULL; 2524 int i; 2525 2526 /* 2527 * Retrieve the raw error list from the kernel. If the number of errors 2528 * has increased, allocate more space and continue until we get the 2529 * entire list. 2530 */ 2531 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 2532 &count) == 0); 2533 if (count == 0) 2534 return (0); 2535 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 2536 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 2537 return (-1); 2538 zc.zc_nvlist_dst_size = count; 2539 (void) strcpy(zc.zc_name, zhp->zpool_name); 2540 for (;;) { 2541 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 2542 &zc) != 0) { 2543 free((void *)(uintptr_t)zc.zc_nvlist_dst); 2544 if (errno == ENOMEM) { 2545 count = zc.zc_nvlist_dst_size; 2546 if ((zc.zc_nvlist_dst = (uintptr_t) 2547 zfs_alloc(zhp->zpool_hdl, count * 2548 sizeof (zbookmark_t))) == (uintptr_t)NULL) 2549 return (-1); 2550 } else { 2551 return (-1); 2552 } 2553 } else { 2554 break; 2555 } 2556 } 2557 2558 /* 2559 * Sort the resulting bookmarks. This is a little confusing due to the 2560 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 2561 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 2562 * _not_ copied as part of the process. So we point the start of our 2563 * array appropriate and decrement the total number of elements. 2564 */ 2565 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 2566 zc.zc_nvlist_dst_size; 2567 count -= zc.zc_nvlist_dst_size; 2568 2569 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 2570 2571 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 2572 2573 /* 2574 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 2575 */ 2576 for (i = 0; i < count; i++) { 2577 nvlist_t *nv; 2578 2579 /* ignoring zb_blkid and zb_level for now */ 2580 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 2581 zb[i-1].zb_object == zb[i].zb_object) 2582 continue; 2583 2584 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 2585 goto nomem; 2586 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 2587 zb[i].zb_objset) != 0) { 2588 nvlist_free(nv); 2589 goto nomem; 2590 } 2591 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 2592 zb[i].zb_object) != 0) { 2593 nvlist_free(nv); 2594 goto nomem; 2595 } 2596 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 2597 nvlist_free(nv); 2598 goto nomem; 2599 } 2600 nvlist_free(nv); 2601 } 2602 2603 free((void *)(uintptr_t)zc.zc_nvlist_dst); 2604 return (0); 2605 2606 nomem: 2607 free((void *)(uintptr_t)zc.zc_nvlist_dst); 2608 return (no_memory(zhp->zpool_hdl)); 2609 } 2610 2611 /* 2612 * Upgrade a ZFS pool to the latest on-disk version. 2613 */ 2614 int 2615 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 2616 { 2617 zfs_cmd_t zc = { 0 }; 2618 libzfs_handle_t *hdl = zhp->zpool_hdl; 2619 2620 (void) strcpy(zc.zc_name, zhp->zpool_name); 2621 zc.zc_cookie = new_version; 2622 2623 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 2624 return (zpool_standard_error_fmt(hdl, errno, 2625 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 2626 zhp->zpool_name)); 2627 return (0); 2628 } 2629 2630 void 2631 zpool_set_history_str(const char *subcommand, int argc, char **argv, 2632 char *history_str) 2633 { 2634 int i; 2635 2636 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 2637 for (i = 1; i < argc; i++) { 2638 if (strlen(history_str) + 1 + strlen(argv[i]) > 2639 HIS_MAX_RECORD_LEN) 2640 break; 2641 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 2642 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 2643 } 2644 } 2645 2646 /* 2647 * Stage command history for logging. 2648 */ 2649 int 2650 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 2651 { 2652 if (history_str == NULL) 2653 return (EINVAL); 2654 2655 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 2656 return (EINVAL); 2657 2658 if (hdl->libzfs_log_str != NULL) 2659 free(hdl->libzfs_log_str); 2660 2661 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 2662 return (no_memory(hdl)); 2663 2664 return (0); 2665 } 2666 2667 /* 2668 * Perform ioctl to get some command history of a pool. 2669 * 2670 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 2671 * logical offset of the history buffer to start reading from. 2672 * 2673 * Upon return, 'off' is the next logical offset to read from and 2674 * 'len' is the actual amount of bytes read into 'buf'. 2675 */ 2676 static int 2677 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 2678 { 2679 zfs_cmd_t zc = { 0 }; 2680 libzfs_handle_t *hdl = zhp->zpool_hdl; 2681 2682 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2683 2684 zc.zc_history = (uint64_t)(uintptr_t)buf; 2685 zc.zc_history_len = *len; 2686 zc.zc_history_offset = *off; 2687 2688 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 2689 switch (errno) { 2690 case EPERM: 2691 return (zfs_error_fmt(hdl, EZFS_PERM, 2692 dgettext(TEXT_DOMAIN, 2693 "cannot show history for pool '%s'"), 2694 zhp->zpool_name)); 2695 case ENOENT: 2696 return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 2697 dgettext(TEXT_DOMAIN, "cannot get history for pool " 2698 "'%s'"), zhp->zpool_name)); 2699 case ENOTSUP: 2700 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 2701 dgettext(TEXT_DOMAIN, "cannot get history for pool " 2702 "'%s', pool must be upgraded"), zhp->zpool_name)); 2703 default: 2704 return (zpool_standard_error_fmt(hdl, errno, 2705 dgettext(TEXT_DOMAIN, 2706 "cannot get history for '%s'"), zhp->zpool_name)); 2707 } 2708 } 2709 2710 *len = zc.zc_history_len; 2711 *off = zc.zc_history_offset; 2712 2713 return (0); 2714 } 2715 2716 /* 2717 * Process the buffer of nvlists, unpacking and storing each nvlist record 2718 * into 'records'. 'leftover' is set to the number of bytes that weren't 2719 * processed as there wasn't a complete record. 2720 */ 2721 static int 2722 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 2723 nvlist_t ***records, uint_t *numrecords) 2724 { 2725 uint64_t reclen; 2726 nvlist_t *nv; 2727 int i; 2728 2729 while (bytes_read > sizeof (reclen)) { 2730 2731 /* get length of packed record (stored as little endian) */ 2732 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 2733 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 2734 2735 if (bytes_read < sizeof (reclen) + reclen) 2736 break; 2737 2738 /* unpack record */ 2739 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 2740 return (ENOMEM); 2741 bytes_read -= sizeof (reclen) + reclen; 2742 buf += sizeof (reclen) + reclen; 2743 2744 /* add record to nvlist array */ 2745 (*numrecords)++; 2746 if (ISP2(*numrecords + 1)) { 2747 *records = realloc(*records, 2748 *numrecords * 2 * sizeof (nvlist_t *)); 2749 } 2750 (*records)[*numrecords - 1] = nv; 2751 } 2752 2753 *leftover = bytes_read; 2754 return (0); 2755 } 2756 2757 #define HIS_BUF_LEN (128*1024) 2758 2759 /* 2760 * Retrieve the command history of a pool. 2761 */ 2762 int 2763 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 2764 { 2765 char buf[HIS_BUF_LEN]; 2766 uint64_t off = 0; 2767 nvlist_t **records = NULL; 2768 uint_t numrecords = 0; 2769 int err, i; 2770 2771 do { 2772 uint64_t bytes_read = sizeof (buf); 2773 uint64_t leftover; 2774 2775 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 2776 break; 2777 2778 /* if nothing else was read in, we're at EOF, just return */ 2779 if (!bytes_read) 2780 break; 2781 2782 if ((err = zpool_history_unpack(buf, bytes_read, 2783 &leftover, &records, &numrecords)) != 0) 2784 break; 2785 off -= leftover; 2786 2787 /* CONSTCOND */ 2788 } while (1); 2789 2790 if (!err) { 2791 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 2792 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 2793 records, numrecords) == 0); 2794 } 2795 for (i = 0; i < numrecords; i++) 2796 nvlist_free(records[i]); 2797 free(records); 2798 2799 return (err); 2800 } 2801 2802 void 2803 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 2804 char *pathname, size_t len) 2805 { 2806 zfs_cmd_t zc = { 0 }; 2807 boolean_t mounted = B_FALSE; 2808 char *mntpnt = NULL; 2809 char dsname[MAXNAMELEN]; 2810 2811 if (dsobj == 0) { 2812 /* special case for the MOS */ 2813 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 2814 return; 2815 } 2816 2817 /* get the dataset's name */ 2818 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2819 zc.zc_obj = dsobj; 2820 if (ioctl(zhp->zpool_hdl->libzfs_fd, 2821 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 2822 /* just write out a path of two object numbers */ 2823 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 2824 dsobj, obj); 2825 return; 2826 } 2827 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 2828 2829 /* find out if the dataset is mounted */ 2830 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 2831 2832 /* get the corrupted object's path */ 2833 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 2834 zc.zc_obj = obj; 2835 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 2836 &zc) == 0) { 2837 if (mounted) { 2838 (void) snprintf(pathname, len, "%s%s", mntpnt, 2839 zc.zc_value); 2840 } else { 2841 (void) snprintf(pathname, len, "%s:%s", 2842 dsname, zc.zc_value); 2843 } 2844 } else { 2845 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 2846 } 2847 free(mntpnt); 2848 } 2849 2850 #define RDISK_ROOT "/dev/rdsk" 2851 #define BACKUP_SLICE "s2" 2852 /* 2853 * Don't start the slice at the default block of 34; many storage 2854 * devices will use a stripe width of 128k, so start there instead. 2855 */ 2856 #define NEW_START_BLOCK 256 2857 2858 /* 2859 * Read the EFI label from the config, if a label does not exist then 2860 * pass back the error to the caller. If the caller has passed a non-NULL 2861 * diskaddr argument then we set it to the starting address of the EFI 2862 * partition. 2863 */ 2864 static int 2865 read_efi_label(nvlist_t *config, diskaddr_t *sb) 2866 { 2867 char *path; 2868 int fd; 2869 char diskname[MAXPATHLEN]; 2870 int err = -1; 2871 2872 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 2873 return (err); 2874 2875 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 2876 strrchr(path, '/')); 2877 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 2878 struct dk_gpt *vtoc; 2879 2880 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 2881 if (sb != NULL) 2882 *sb = vtoc->efi_parts[0].p_start; 2883 efi_free(vtoc); 2884 } 2885 (void) close(fd); 2886 } 2887 return (err); 2888 } 2889 2890 /* 2891 * determine where a partition starts on a disk in the current 2892 * configuration 2893 */ 2894 static diskaddr_t 2895 find_start_block(nvlist_t *config) 2896 { 2897 nvlist_t **child; 2898 uint_t c, children; 2899 diskaddr_t sb = MAXOFFSET_T; 2900 uint64_t wholedisk; 2901 2902 if (nvlist_lookup_nvlist_array(config, 2903 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 2904 if (nvlist_lookup_uint64(config, 2905 ZPOOL_CONFIG_WHOLE_DISK, 2906 &wholedisk) != 0 || !wholedisk) { 2907 return (MAXOFFSET_T); 2908 } 2909 if (read_efi_label(config, &sb) < 0) 2910 sb = MAXOFFSET_T; 2911 return (sb); 2912 } 2913 2914 for (c = 0; c < children; c++) { 2915 sb = find_start_block(child[c]); 2916 if (sb != MAXOFFSET_T) { 2917 return (sb); 2918 } 2919 } 2920 return (MAXOFFSET_T); 2921 } 2922 2923 /* 2924 * Label an individual disk. The name provided is the short name, 2925 * stripped of any leading /dev path. 2926 */ 2927 int 2928 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 2929 { 2930 char path[MAXPATHLEN]; 2931 struct dk_gpt *vtoc; 2932 int fd; 2933 size_t resv = EFI_MIN_RESV_SIZE; 2934 uint64_t slice_size; 2935 diskaddr_t start_block; 2936 char errbuf[1024]; 2937 2938 /* prepare an error message just in case */ 2939 (void) snprintf(errbuf, sizeof (errbuf), 2940 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 2941 2942 if (zhp) { 2943 nvlist_t *nvroot; 2944 2945 if (pool_is_bootable(zhp)) { 2946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2947 "EFI labeled devices are not supported on root " 2948 "pools.")); 2949 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 2950 } 2951 2952 verify(nvlist_lookup_nvlist(zhp->zpool_config, 2953 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2954 2955 if (zhp->zpool_start_block == 0) 2956 start_block = find_start_block(nvroot); 2957 else 2958 start_block = zhp->zpool_start_block; 2959 zhp->zpool_start_block = start_block; 2960 } else { 2961 /* new pool */ 2962 start_block = NEW_START_BLOCK; 2963 } 2964 2965 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 2966 BACKUP_SLICE); 2967 2968 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2969 /* 2970 * This shouldn't happen. We've long since verified that this 2971 * is a valid device. 2972 */ 2973 zfs_error_aux(hdl, 2974 dgettext(TEXT_DOMAIN, "unable to open device")); 2975 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2976 } 2977 2978 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 2979 /* 2980 * The only way this can fail is if we run out of memory, or we 2981 * were unable to read the disk's capacity 2982 */ 2983 if (errno == ENOMEM) 2984 (void) no_memory(hdl); 2985 2986 (void) close(fd); 2987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2988 "unable to read disk capacity"), name); 2989 2990 return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2991 } 2992 2993 slice_size = vtoc->efi_last_u_lba + 1; 2994 slice_size -= EFI_MIN_RESV_SIZE; 2995 if (start_block == MAXOFFSET_T) 2996 start_block = NEW_START_BLOCK; 2997 slice_size -= start_block; 2998 2999 vtoc->efi_parts[0].p_start = start_block; 3000 vtoc->efi_parts[0].p_size = slice_size; 3001 3002 /* 3003 * Why we use V_USR: V_BACKUP confuses users, and is considered 3004 * disposable by some EFI utilities (since EFI doesn't have a backup 3005 * slice). V_UNASSIGNED is supposed to be used only for zero size 3006 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 3007 * etc. were all pretty specific. V_USR is as close to reality as we 3008 * can get, in the absence of V_OTHER. 3009 */ 3010 vtoc->efi_parts[0].p_tag = V_USR; 3011 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 3012 3013 vtoc->efi_parts[8].p_start = slice_size + start_block; 3014 vtoc->efi_parts[8].p_size = resv; 3015 vtoc->efi_parts[8].p_tag = V_RESERVED; 3016 3017 if (efi_write(fd, vtoc) != 0) { 3018 /* 3019 * Some block drivers (like pcata) may not support EFI 3020 * GPT labels. Print out a helpful error message dir- 3021 * ecting the user to manually label the disk and give 3022 * a specific slice. 3023 */ 3024 (void) close(fd); 3025 efi_free(vtoc); 3026 3027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3028 "try using fdisk(1M) and then provide a specific slice")); 3029 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 3030 } 3031 3032 (void) close(fd); 3033 efi_free(vtoc); 3034 return (0); 3035 } 3036 3037 static boolean_t 3038 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3039 { 3040 char *type; 3041 nvlist_t **child; 3042 uint_t children, c; 3043 3044 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3045 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 3046 strcmp(type, VDEV_TYPE_FILE) == 0 || 3047 strcmp(type, VDEV_TYPE_LOG) == 0 || 3048 strcmp(type, VDEV_TYPE_MISSING) == 0) { 3049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3050 "vdev type '%s' is not supported"), type); 3051 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3052 return (B_FALSE); 3053 } 3054 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3055 &child, &children) == 0) { 3056 for (c = 0; c < children; c++) { 3057 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3058 return (B_FALSE); 3059 } 3060 } 3061 return (B_TRUE); 3062 } 3063 3064 /* 3065 * check if this zvol is allowable for use as a dump device; zero if 3066 * it is, > 0 if it isn't, < 0 if it isn't a zvol 3067 */ 3068 int 3069 zvol_check_dump_config(char *arg) 3070 { 3071 zpool_handle_t *zhp = NULL; 3072 nvlist_t *config, *nvroot; 3073 char *p, *volname; 3074 nvlist_t **top; 3075 uint_t toplevels; 3076 libzfs_handle_t *hdl; 3077 char errbuf[1024]; 3078 char poolname[ZPOOL_MAXNAMELEN]; 3079 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3080 int ret = 1; 3081 3082 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3083 return (-1); 3084 } 3085 3086 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3087 "dump is not supported on device '%s'"), arg); 3088 3089 if ((hdl = libzfs_init()) == NULL) 3090 return (1); 3091 libzfs_print_on_error(hdl, B_TRUE); 3092 3093 volname = arg + pathlen; 3094 3095 /* check the configuration of the pool */ 3096 if ((p = strchr(volname, '/')) == NULL) { 3097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3098 "malformed dataset name")); 3099 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3100 return (1); 3101 } else if (p - volname >= ZFS_MAXNAMELEN) { 3102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3103 "dataset name is too long")); 3104 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 3105 return (1); 3106 } else { 3107 (void) strncpy(poolname, volname, p - volname); 3108 poolname[p - volname] = '\0'; 3109 } 3110 3111 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 3112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3113 "could not open pool '%s'"), poolname); 3114 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 3115 goto out; 3116 } 3117 config = zpool_get_config(zhp, NULL); 3118 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3119 &nvroot) != 0) { 3120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3121 "could not obtain vdev configuration for '%s'"), poolname); 3122 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3123 goto out; 3124 } 3125 3126 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3127 &top, &toplevels) == 0); 3128 if (toplevels != 1) { 3129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3130 "'%s' has multiple top level vdevs"), poolname); 3131 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3132 goto out; 3133 } 3134 3135 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 3136 goto out; 3137 } 3138 ret = 0; 3139 3140 out: 3141 if (zhp) 3142 zpool_close(zhp); 3143 libzfs_fini(hdl); 3144 return (ret); 3145 } 3146