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