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