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