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