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