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