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