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 ctime_r((time_t *)&rewindto, timestr) != NULL) { 1904 timestr[24] = 0; 1905 if (dryrun) { 1906 (void) printf(dgettext(TEXT_DOMAIN, 1907 "Would be able to return %s " 1908 "to its state as of %s.\n"), 1909 name, timestr); 1910 } else { 1911 (void) printf(dgettext(TEXT_DOMAIN, 1912 "Pool %s returned to its state as of %s.\n"), 1913 name, timestr); 1914 } 1915 if (loss > 120) { 1916 (void) printf(dgettext(TEXT_DOMAIN, 1917 "%s approximately %lld "), 1918 dryrun ? "Would discard" : "Discarded", 1919 ((longlong_t)loss + 30) / 60); 1920 (void) printf(dgettext(TEXT_DOMAIN, 1921 "minutes of transactions.\n")); 1922 } else if (loss > 0) { 1923 (void) printf(dgettext(TEXT_DOMAIN, 1924 "%s approximately %lld "), 1925 dryrun ? "Would discard" : "Discarded", 1926 (longlong_t)loss); 1927 (void) printf(dgettext(TEXT_DOMAIN, 1928 "seconds of transactions.\n")); 1929 } 1930 } 1931 } 1932 1933 void 1934 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1935 nvlist_t *config) 1936 { 1937 nvlist_t *nv = NULL; 1938 int64_t loss = -1; 1939 uint64_t edata = UINT64_MAX; 1940 uint64_t rewindto; 1941 struct tm t; 1942 char timestr[128]; 1943 1944 if (!hdl->libzfs_printerr) 1945 return; 1946 1947 if (reason >= 0) 1948 (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1949 else 1950 (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1951 1952 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1953 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1954 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 1955 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1956 goto no_info; 1957 1958 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1959 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1960 &edata); 1961 1962 (void) printf(dgettext(TEXT_DOMAIN, 1963 "Recovery is possible, but will result in some data loss.\n")); 1964 1965 if (localtime_r((time_t *)&rewindto, &t) != NULL && 1966 ctime_r((time_t *)&rewindto, timestr) != NULL) { 1967 timestr[24] = 0; 1968 (void) printf(dgettext(TEXT_DOMAIN, 1969 "\tReturning the pool to its state as of %s\n" 1970 "\tshould correct the problem. "), 1971 timestr); 1972 } else { 1973 (void) printf(dgettext(TEXT_DOMAIN, 1974 "\tReverting the pool to an earlier state " 1975 "should correct the problem.\n\t")); 1976 } 1977 1978 if (loss > 120) { 1979 (void) printf(dgettext(TEXT_DOMAIN, 1980 "Approximately %lld minutes of data\n" 1981 "\tmust be discarded, irreversibly. "), 1982 ((longlong_t)loss + 30) / 60); 1983 } else if (loss > 0) { 1984 (void) printf(dgettext(TEXT_DOMAIN, 1985 "Approximately %lld seconds of data\n" 1986 "\tmust be discarded, irreversibly. "), 1987 (longlong_t)loss); 1988 } 1989 if (edata != 0 && edata != UINT64_MAX) { 1990 if (edata == 1) { 1991 (void) printf(dgettext(TEXT_DOMAIN, 1992 "After rewind, at least\n" 1993 "\tone persistent user-data error will remain. ")); 1994 } else { 1995 (void) printf(dgettext(TEXT_DOMAIN, 1996 "After rewind, several\n" 1997 "\tpersistent user-data errors will remain. ")); 1998 } 1999 } 2000 (void) printf(dgettext(TEXT_DOMAIN, 2001 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 2002 reason >= 0 ? "clear" : "import", name); 2003 2004 (void) printf(dgettext(TEXT_DOMAIN, 2005 "A scrub of the pool\n" 2006 "\tis strongly recommended after recovery.\n")); 2007 return; 2008 2009 no_info: 2010 (void) printf(dgettext(TEXT_DOMAIN, 2011 "Destroy and re-create the pool from\n\ta backup source.\n")); 2012 } 2013 2014 /* 2015 * zpool_import() is a contracted interface. Should be kept the same 2016 * if possible. 2017 * 2018 * Applications should use zpool_import_props() to import a pool with 2019 * new properties value to be set. 2020 */ 2021 int 2022 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 2023 char *altroot) 2024 { 2025 nvlist_t *props = NULL; 2026 int ret; 2027 2028 if (altroot != NULL) { 2029 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 2030 return (zfs_error_fmt(hdl, EZFS_NOMEM, 2031 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 2032 newname)); 2033 } 2034 2035 if (nvlist_add_string(props, 2036 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 2037 nvlist_add_string(props, 2038 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 2039 nvlist_free(props); 2040 return (zfs_error_fmt(hdl, EZFS_NOMEM, 2041 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 2042 newname)); 2043 } 2044 } 2045 2046 ret = zpool_import_props(hdl, config, newname, props, 2047 ZFS_IMPORT_NORMAL); 2048 nvlist_free(props); 2049 return (ret); 2050 } 2051 2052 static void 2053 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 2054 int indent) 2055 { 2056 nvlist_t **child; 2057 uint_t c, children; 2058 char *vname; 2059 uint64_t is_log = 0; 2060 2061 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 2062 &is_log); 2063 2064 if (name != NULL) 2065 (void) printf("\t%*s%s%s\n", indent, "", name, 2066 is_log ? " [log]" : ""); 2067 2068 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2069 &child, &children) != 0) 2070 return; 2071 2072 for (c = 0; c < children; c++) { 2073 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID); 2074 print_vdev_tree(hdl, vname, child[c], indent + 2); 2075 free(vname); 2076 } 2077 } 2078 2079 void 2080 zpool_print_unsup_feat(nvlist_t *config) 2081 { 2082 nvlist_t *nvinfo, *unsup_feat; 2083 2084 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 2085 unsup_feat = fnvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT); 2086 2087 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); 2088 nvp != NULL; nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 2089 const char *desc = fnvpair_value_string(nvp); 2090 if (strlen(desc) > 0) 2091 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 2092 else 2093 (void) printf("\t%s\n", nvpair_name(nvp)); 2094 } 2095 } 2096 2097 /* 2098 * Import the given pool using the known configuration and a list of 2099 * properties to be set. The configuration should have come from 2100 * zpool_find_import(). The 'newname' parameters control whether the pool 2101 * is imported with a different name. 2102 */ 2103 int 2104 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 2105 nvlist_t *props, int flags) 2106 { 2107 zfs_cmd_t zc = {"\0"}; 2108 zpool_load_policy_t policy; 2109 nvlist_t *nv = NULL; 2110 nvlist_t *nvinfo = NULL; 2111 nvlist_t *missing = NULL; 2112 const char *thename; 2113 const char *origname; 2114 int ret; 2115 int error = 0; 2116 char errbuf[ERRBUFLEN]; 2117 2118 origname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); 2119 2120 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2121 "cannot import pool '%s'"), origname); 2122 2123 if (newname != NULL) { 2124 if (!zpool_name_valid(hdl, B_FALSE, newname)) 2125 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 2126 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 2127 newname)); 2128 thename = newname; 2129 } else { 2130 thename = origname; 2131 } 2132 2133 if (props != NULL) { 2134 uint64_t version; 2135 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 2136 2137 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); 2138 2139 if ((props = zpool_valid_proplist(hdl, origname, 2140 props, version, flags, errbuf)) == NULL) 2141 return (-1); 2142 zcmd_write_src_nvlist(hdl, &zc, props); 2143 nvlist_free(props); 2144 } 2145 2146 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 2147 2148 zc.zc_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID); 2149 2150 zcmd_write_conf_nvlist(hdl, &zc, config); 2151 zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2); 2152 2153 zc.zc_cookie = flags; 2154 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 2155 errno == ENOMEM) 2156 zcmd_expand_dst_nvlist(hdl, &zc); 2157 if (ret != 0) 2158 error = errno; 2159 2160 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 2161 2162 zcmd_free_nvlists(&zc); 2163 2164 zpool_get_load_policy(config, &policy); 2165 2166 if (error) { 2167 char desc[1024]; 2168 char aux[256]; 2169 2170 /* 2171 * Dry-run failed, but we print out what success 2172 * looks like if we found a best txg 2173 */ 2174 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) { 2175 zpool_rewind_exclaim(hdl, newname ? origname : thename, 2176 B_TRUE, nv); 2177 nvlist_free(nv); 2178 return (-1); 2179 } 2180 2181 if (newname == NULL) 2182 (void) snprintf(desc, sizeof (desc), 2183 dgettext(TEXT_DOMAIN, "cannot import '%s'"), 2184 thename); 2185 else 2186 (void) snprintf(desc, sizeof (desc), 2187 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 2188 origname, thename); 2189 2190 switch (error) { 2191 case ENOTSUP: 2192 if (nv != NULL && nvlist_lookup_nvlist(nv, 2193 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 2194 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 2195 (void) printf(dgettext(TEXT_DOMAIN, "This " 2196 "pool uses the following feature(s) not " 2197 "supported by this system:\n")); 2198 zpool_print_unsup_feat(nv); 2199 if (nvlist_exists(nvinfo, 2200 ZPOOL_CONFIG_CAN_RDONLY)) { 2201 (void) printf(dgettext(TEXT_DOMAIN, 2202 "All unsupported features are only " 2203 "required for writing to the pool." 2204 "\nThe pool can be imported using " 2205 "'-o readonly=on'.\n")); 2206 } 2207 } 2208 /* 2209 * Unsupported version. 2210 */ 2211 (void) zfs_error(hdl, EZFS_BADVERSION, desc); 2212 break; 2213 2214 case EREMOTEIO: 2215 if (nv != NULL && nvlist_lookup_nvlist(nv, 2216 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) { 2217 const char *hostname = "<unknown>"; 2218 uint64_t hostid = 0; 2219 mmp_state_t mmp_state; 2220 2221 mmp_state = fnvlist_lookup_uint64(nvinfo, 2222 ZPOOL_CONFIG_MMP_STATE); 2223 2224 if (nvlist_exists(nvinfo, 2225 ZPOOL_CONFIG_MMP_HOSTNAME)) 2226 hostname = fnvlist_lookup_string(nvinfo, 2227 ZPOOL_CONFIG_MMP_HOSTNAME); 2228 2229 if (nvlist_exists(nvinfo, 2230 ZPOOL_CONFIG_MMP_HOSTID)) 2231 hostid = fnvlist_lookup_uint64(nvinfo, 2232 ZPOOL_CONFIG_MMP_HOSTID); 2233 2234 if (mmp_state == MMP_STATE_ACTIVE) { 2235 (void) snprintf(aux, sizeof (aux), 2236 dgettext(TEXT_DOMAIN, "pool is imp" 2237 "orted on host '%s' (hostid=%lx).\n" 2238 "Export the pool on the other " 2239 "system, then run 'zpool import'."), 2240 hostname, (unsigned long) hostid); 2241 } else if (mmp_state == MMP_STATE_NO_HOSTID) { 2242 (void) snprintf(aux, sizeof (aux), 2243 dgettext(TEXT_DOMAIN, "pool has " 2244 "the multihost property on and " 2245 "the\nsystem's hostid is not set. " 2246 "Set a unique system hostid with " 2247 "the zgenhostid(8) command.\n")); 2248 } 2249 2250 (void) zfs_error_aux(hdl, "%s", aux); 2251 } 2252 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc); 2253 break; 2254 2255 case EINVAL: 2256 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 2257 break; 2258 2259 case EROFS: 2260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2261 "one or more devices is read only")); 2262 (void) zfs_error(hdl, EZFS_BADDEV, desc); 2263 break; 2264 2265 case ENXIO: 2266 if (nv && nvlist_lookup_nvlist(nv, 2267 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 2268 nvlist_lookup_nvlist(nvinfo, 2269 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 2270 (void) printf(dgettext(TEXT_DOMAIN, 2271 "The devices below are missing or " 2272 "corrupted, use '-m' to import the pool " 2273 "anyway:\n")); 2274 print_vdev_tree(hdl, NULL, missing, 2); 2275 (void) printf("\n"); 2276 } 2277 (void) zpool_standard_error(hdl, error, desc); 2278 break; 2279 2280 case EEXIST: 2281 (void) zpool_standard_error(hdl, error, desc); 2282 break; 2283 2284 case EBUSY: 2285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2286 "one or more devices are already in use\n")); 2287 (void) zfs_error(hdl, EZFS_BADDEV, desc); 2288 break; 2289 case ENAMETOOLONG: 2290 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2291 "new name of at least one dataset is longer than " 2292 "the maximum allowable length")); 2293 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 2294 break; 2295 default: 2296 (void) zpool_standard_error(hdl, error, desc); 2297 zpool_explain_recover(hdl, 2298 newname ? origname : thename, -error, nv); 2299 break; 2300 } 2301 2302 nvlist_free(nv); 2303 ret = -1; 2304 } else { 2305 zpool_handle_t *zhp; 2306 2307 /* 2308 * This should never fail, but play it safe anyway. 2309 */ 2310 if (zpool_open_silent(hdl, thename, &zhp) != 0) 2311 ret = -1; 2312 else if (zhp != NULL) 2313 zpool_close(zhp); 2314 if (policy.zlp_rewind & 2315 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2316 zpool_rewind_exclaim(hdl, newname ? origname : thename, 2317 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv); 2318 } 2319 nvlist_free(nv); 2320 } 2321 2322 return (ret); 2323 } 2324 2325 /* 2326 * Translate vdev names to guids. If a vdev_path is determined to be 2327 * unsuitable then a vd_errlist is allocated and the vdev path and errno 2328 * are added to it. 2329 */ 2330 static int 2331 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds, 2332 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist) 2333 { 2334 nvlist_t *errlist = NULL; 2335 int error = 0; 2336 2337 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL; 2338 elem = nvlist_next_nvpair(vds, elem)) { 2339 boolean_t spare, cache; 2340 2341 const char *vd_path = nvpair_name(elem); 2342 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache, 2343 NULL); 2344 2345 if ((tgt == NULL) || cache || spare) { 2346 if (errlist == NULL) { 2347 errlist = fnvlist_alloc(); 2348 error = EINVAL; 2349 } 2350 2351 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE : 2352 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE); 2353 fnvlist_add_int64(errlist, vd_path, err); 2354 continue; 2355 } 2356 2357 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 2358 fnvlist_add_uint64(vdev_guids, vd_path, guid); 2359 2360 char msg[MAXNAMELEN]; 2361 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid); 2362 fnvlist_add_string(guids_to_paths, msg, vd_path); 2363 } 2364 2365 if (error != 0) { 2366 verify(errlist != NULL); 2367 if (vd_errlist != NULL) 2368 *vd_errlist = errlist; 2369 else 2370 fnvlist_free(errlist); 2371 } 2372 2373 return (error); 2374 } 2375 2376 static int 2377 xlate_init_err(int err) 2378 { 2379 switch (err) { 2380 case ENODEV: 2381 return (EZFS_NODEVICE); 2382 case EINVAL: 2383 case EROFS: 2384 return (EZFS_BADDEV); 2385 case EBUSY: 2386 return (EZFS_INITIALIZING); 2387 case ESRCH: 2388 return (EZFS_NO_INITIALIZE); 2389 } 2390 return (err); 2391 } 2392 2393 /* 2394 * Begin, suspend, cancel, or uninit (clear) the initialization (initializing 2395 * of all free blocks) for the given vdevs in the given pool. 2396 */ 2397 static int 2398 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2399 nvlist_t *vds, boolean_t wait) 2400 { 2401 int err; 2402 2403 nvlist_t *vdev_guids = fnvlist_alloc(); 2404 nvlist_t *guids_to_paths = fnvlist_alloc(); 2405 nvlist_t *vd_errlist = NULL; 2406 nvlist_t *errlist; 2407 nvpair_t *elem; 2408 2409 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids, 2410 guids_to_paths, &vd_errlist); 2411 2412 if (err != 0) { 2413 verify(vd_errlist != NULL); 2414 goto list_errors; 2415 } 2416 2417 err = lzc_initialize(zhp->zpool_name, cmd_type, 2418 vdev_guids, &errlist); 2419 2420 if (err != 0) { 2421 if (errlist != NULL && nvlist_lookup_nvlist(errlist, 2422 ZPOOL_INITIALIZE_VDEVS, &vd_errlist) == 0) { 2423 goto list_errors; 2424 } 2425 2426 if (err == EINVAL && cmd_type == POOL_INITIALIZE_UNINIT) { 2427 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 2428 "uninitialize is not supported by kernel")); 2429 } 2430 2431 (void) zpool_standard_error(zhp->zpool_hdl, err, 2432 dgettext(TEXT_DOMAIN, "operation failed")); 2433 goto out; 2434 } 2435 2436 if (wait) { 2437 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL; 2438 elem = nvlist_next_nvpair(vdev_guids, elem)) { 2439 2440 uint64_t guid = fnvpair_value_uint64(elem); 2441 2442 err = lzc_wait_tag(zhp->zpool_name, 2443 ZPOOL_WAIT_INITIALIZE, guid, NULL); 2444 if (err != 0) { 2445 (void) zpool_standard_error_fmt(zhp->zpool_hdl, 2446 err, dgettext(TEXT_DOMAIN, "error " 2447 "waiting for '%s' to initialize"), 2448 nvpair_name(elem)); 2449 2450 goto out; 2451 } 2452 } 2453 } 2454 goto out; 2455 2456 list_errors: 2457 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL; 2458 elem = nvlist_next_nvpair(vd_errlist, elem)) { 2459 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem)); 2460 const char *path; 2461 2462 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem), 2463 &path) != 0) 2464 path = nvpair_name(elem); 2465 2466 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error, 2467 "cannot initialize '%s'", path); 2468 } 2469 2470 out: 2471 fnvlist_free(vdev_guids); 2472 fnvlist_free(guids_to_paths); 2473 2474 if (vd_errlist != NULL) 2475 fnvlist_free(vd_errlist); 2476 2477 return (err == 0 ? 0 : -1); 2478 } 2479 2480 int 2481 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2482 nvlist_t *vds) 2483 { 2484 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE)); 2485 } 2486 2487 int 2488 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2489 nvlist_t *vds) 2490 { 2491 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE)); 2492 } 2493 2494 static int 2495 xlate_trim_err(int err) 2496 { 2497 switch (err) { 2498 case ENODEV: 2499 return (EZFS_NODEVICE); 2500 case EINVAL: 2501 case EROFS: 2502 return (EZFS_BADDEV); 2503 case EBUSY: 2504 return (EZFS_TRIMMING); 2505 case ESRCH: 2506 return (EZFS_NO_TRIM); 2507 case EOPNOTSUPP: 2508 return (EZFS_TRIM_NOTSUP); 2509 } 2510 return (err); 2511 } 2512 2513 static int 2514 zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids) 2515 { 2516 int err; 2517 nvpair_t *elem; 2518 2519 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL; 2520 elem = nvlist_next_nvpair(vdev_guids, elem)) { 2521 2522 uint64_t guid = fnvpair_value_uint64(elem); 2523 2524 err = lzc_wait_tag(zhp->zpool_name, 2525 ZPOOL_WAIT_TRIM, guid, NULL); 2526 if (err != 0) { 2527 (void) zpool_standard_error_fmt(zhp->zpool_hdl, 2528 err, dgettext(TEXT_DOMAIN, "error " 2529 "waiting to trim '%s'"), nvpair_name(elem)); 2530 2531 return (err); 2532 } 2533 } 2534 return (0); 2535 } 2536 2537 /* 2538 * Check errlist and report any errors, omitting ones which should be 2539 * suppressed. Returns B_TRUE if any errors were reported. 2540 */ 2541 static boolean_t 2542 check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags, 2543 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist) 2544 { 2545 nvpair_t *elem; 2546 boolean_t reported_errs = B_FALSE; 2547 int num_vds = 0; 2548 int num_suppressed_errs = 0; 2549 2550 for (elem = nvlist_next_nvpair(vds, NULL); 2551 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) { 2552 num_vds++; 2553 } 2554 2555 for (elem = nvlist_next_nvpair(errlist, NULL); 2556 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) { 2557 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem)); 2558 const char *path; 2559 2560 /* 2561 * If only the pool was specified, and it was not a secure 2562 * trim then suppress warnings for individual vdevs which 2563 * do not support trimming. 2564 */ 2565 if (vd_error == EZFS_TRIM_NOTSUP && 2566 trim_flags->fullpool && 2567 !trim_flags->secure) { 2568 num_suppressed_errs++; 2569 continue; 2570 } 2571 2572 reported_errs = B_TRUE; 2573 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem), 2574 &path) != 0) 2575 path = nvpair_name(elem); 2576 2577 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error, 2578 "cannot trim '%s'", path); 2579 } 2580 2581 if (num_suppressed_errs == num_vds) { 2582 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 2583 "no devices in pool support trim operations")); 2584 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP, 2585 dgettext(TEXT_DOMAIN, "cannot trim"))); 2586 reported_errs = B_TRUE; 2587 } 2588 2589 return (reported_errs); 2590 } 2591 2592 /* 2593 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for 2594 * the given vdevs in the given pool. 2595 */ 2596 int 2597 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds, 2598 trimflags_t *trim_flags) 2599 { 2600 int err; 2601 int retval = 0; 2602 2603 nvlist_t *vdev_guids = fnvlist_alloc(); 2604 nvlist_t *guids_to_paths = fnvlist_alloc(); 2605 nvlist_t *errlist = NULL; 2606 2607 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids, 2608 guids_to_paths, &errlist); 2609 if (err != 0) { 2610 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist); 2611 retval = -1; 2612 goto out; 2613 } 2614 2615 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate, 2616 trim_flags->secure, vdev_guids, &errlist); 2617 if (err != 0) { 2618 nvlist_t *vd_errlist; 2619 if (errlist != NULL && nvlist_lookup_nvlist(errlist, 2620 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) { 2621 if (check_trim_errs(zhp, trim_flags, guids_to_paths, 2622 vds, vd_errlist)) { 2623 retval = -1; 2624 goto out; 2625 } 2626 } else { 2627 char errbuf[ERRBUFLEN]; 2628 2629 (void) snprintf(errbuf, sizeof (errbuf), 2630 dgettext(TEXT_DOMAIN, "operation failed")); 2631 zpool_standard_error(zhp->zpool_hdl, err, errbuf); 2632 retval = -1; 2633 goto out; 2634 } 2635 } 2636 2637 2638 if (trim_flags->wait) 2639 retval = zpool_trim_wait(zhp, vdev_guids); 2640 2641 out: 2642 if (errlist != NULL) 2643 fnvlist_free(errlist); 2644 fnvlist_free(vdev_guids); 2645 fnvlist_free(guids_to_paths); 2646 return (retval); 2647 } 2648 2649 /* 2650 * Scan the pool. 2651 */ 2652 int 2653 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) 2654 { 2655 char errbuf[ERRBUFLEN]; 2656 int err; 2657 libzfs_handle_t *hdl = zhp->zpool_hdl; 2658 2659 nvlist_t *args = fnvlist_alloc(); 2660 fnvlist_add_uint64(args, "scan_type", (uint64_t)func); 2661 fnvlist_add_uint64(args, "scan_command", (uint64_t)cmd); 2662 2663 err = lzc_scrub(ZFS_IOC_POOL_SCRUB, zhp->zpool_name, args, NULL); 2664 fnvlist_free(args); 2665 2666 if (err == 0) { 2667 return (0); 2668 } else if (err == ZFS_ERR_IOC_CMD_UNAVAIL) { 2669 zfs_cmd_t zc = {"\0"}; 2670 (void) strlcpy(zc.zc_name, zhp->zpool_name, 2671 sizeof (zc.zc_name)); 2672 zc.zc_cookie = func; 2673 zc.zc_flags = cmd; 2674 2675 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0) 2676 return (0); 2677 } 2678 2679 /* 2680 * An ECANCELED on a scrub means one of the following: 2681 * 1. we resumed a paused scrub. 2682 * 2. we resumed a paused error scrub. 2683 * 3. Error scrub is not run because of no error log. 2684 */ 2685 if (err == ECANCELED && (func == POOL_SCAN_SCRUB || 2686 func == POOL_SCAN_ERRORSCRUB) && cmd == POOL_SCRUB_NORMAL) 2687 return (0); 2688 /* 2689 * The following cases have been handled here: 2690 * 1. Paused a scrub/error scrub if there is none in progress. 2691 */ 2692 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == 2693 POOL_SCRUB_PAUSE) { 2694 return (0); 2695 } 2696 2697 ASSERT3U(func, >=, POOL_SCAN_NONE); 2698 ASSERT3U(func, <, POOL_SCAN_FUNCS); 2699 2700 if (func == POOL_SCAN_SCRUB || func == POOL_SCAN_ERRORSCRUB) { 2701 if (cmd == POOL_SCRUB_PAUSE) { 2702 (void) snprintf(errbuf, sizeof (errbuf), 2703 dgettext(TEXT_DOMAIN, "cannot pause scrubbing %s"), 2704 zhp->zpool_name); 2705 } else { 2706 assert(cmd == POOL_SCRUB_NORMAL); 2707 (void) snprintf(errbuf, sizeof (errbuf), 2708 dgettext(TEXT_DOMAIN, "cannot scrub %s"), 2709 zhp->zpool_name); 2710 } 2711 } else if (func == POOL_SCAN_RESILVER) { 2712 assert(cmd == POOL_SCRUB_NORMAL); 2713 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2714 "cannot restart resilver on %s"), zhp->zpool_name); 2715 } else if (func == POOL_SCAN_NONE) { 2716 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2717 "cannot cancel scrubbing %s"), zhp->zpool_name); 2718 } else { 2719 assert(!"unexpected result"); 2720 } 2721 2722 /* 2723 * With EBUSY, five cases are possible: 2724 * 2725 * Current state Requested 2726 * 1. Normal Scrub Running Normal Scrub or Error Scrub 2727 * 2. Normal Scrub Paused Error Scrub 2728 * 3. Normal Scrub Paused Pause Normal Scrub 2729 * 4. Error Scrub Running Normal Scrub or Error Scrub 2730 * 5. Error Scrub Paused Pause Error Scrub 2731 * 6. Resilvering Anything else 2732 */ 2733 if (err == EBUSY) { 2734 nvlist_t *nvroot; 2735 pool_scan_stat_t *ps = NULL; 2736 uint_t psc; 2737 2738 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config, 2739 ZPOOL_CONFIG_VDEV_TREE); 2740 (void) nvlist_lookup_uint64_array(nvroot, 2741 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 2742 if (ps && ps->pss_func == POOL_SCAN_SCRUB && 2743 ps->pss_state == DSS_SCANNING) { 2744 if (ps->pss_pass_scrub_pause == 0) { 2745 /* handles case 1 */ 2746 assert(cmd == POOL_SCRUB_NORMAL); 2747 return (zfs_error(hdl, EZFS_SCRUBBING, 2748 errbuf)); 2749 } else { 2750 if (func == POOL_SCAN_ERRORSCRUB) { 2751 /* handles case 2 */ 2752 ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL); 2753 return (zfs_error(hdl, 2754 EZFS_SCRUB_PAUSED_TO_CANCEL, 2755 errbuf)); 2756 } else { 2757 /* handles case 3 */ 2758 ASSERT3U(func, ==, POOL_SCAN_SCRUB); 2759 ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE); 2760 return (zfs_error(hdl, 2761 EZFS_SCRUB_PAUSED, errbuf)); 2762 } 2763 } 2764 } else if (ps && 2765 ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB && 2766 ps->pss_error_scrub_state == DSS_ERRORSCRUBBING) { 2767 if (ps->pss_pass_error_scrub_pause == 0) { 2768 /* handles case 4 */ 2769 ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL); 2770 return (zfs_error(hdl, EZFS_ERRORSCRUBBING, 2771 errbuf)); 2772 } else { 2773 /* handles case 5 */ 2774 ASSERT3U(func, ==, POOL_SCAN_ERRORSCRUB); 2775 ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE); 2776 return (zfs_error(hdl, EZFS_ERRORSCRUB_PAUSED, 2777 errbuf)); 2778 } 2779 } else { 2780 /* handles case 6 */ 2781 return (zfs_error(hdl, EZFS_RESILVERING, errbuf)); 2782 } 2783 } else if (err == ENOENT) { 2784 return (zfs_error(hdl, EZFS_NO_SCRUB, errbuf)); 2785 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) { 2786 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, errbuf)); 2787 } else { 2788 return (zpool_standard_error(hdl, err, errbuf)); 2789 } 2790 } 2791 2792 /* 2793 * Find a vdev that matches the search criteria specified. We use the 2794 * the nvpair name to determine how we should look for the device. 2795 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 2796 * spare; but FALSE if its an INUSE spare. 2797 */ 2798 static nvlist_t * 2799 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 2800 boolean_t *l2cache, boolean_t *log) 2801 { 2802 uint_t c, children; 2803 nvlist_t **child; 2804 nvlist_t *ret; 2805 uint64_t is_log; 2806 const char *srchkey; 2807 nvpair_t *pair = nvlist_next_nvpair(search, NULL); 2808 2809 /* Nothing to look for */ 2810 if (search == NULL || pair == NULL) 2811 return (NULL); 2812 2813 /* Obtain the key we will use to search */ 2814 srchkey = nvpair_name(pair); 2815 2816 switch (nvpair_type(pair)) { 2817 case DATA_TYPE_UINT64: 2818 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 2819 uint64_t srchval = fnvpair_value_uint64(pair); 2820 uint64_t theguid = fnvlist_lookup_uint64(nv, 2821 ZPOOL_CONFIG_GUID); 2822 if (theguid == srchval) 2823 return (nv); 2824 } 2825 break; 2826 2827 case DATA_TYPE_STRING: { 2828 const char *srchval, *val; 2829 2830 srchval = fnvpair_value_string(pair); 2831 if (nvlist_lookup_string(nv, srchkey, &val) != 0) 2832 break; 2833 2834 /* 2835 * Search for the requested value. Special cases: 2836 * 2837 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 2838 * "-part1", or "p1". The suffix is hidden from the user, 2839 * but included in the string, so this matches around it. 2840 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname() 2841 * is used to check all possible expanded paths. 2842 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 2843 * 2844 * Otherwise, all other searches are simple string compares. 2845 */ 2846 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) { 2847 uint64_t wholedisk = 0; 2848 2849 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2850 &wholedisk); 2851 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0) 2852 return (nv); 2853 2854 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0) { 2855 char *type, *idx, *end, *p; 2856 uint64_t id, vdev_id; 2857 2858 /* 2859 * Determine our vdev type, keeping in mind 2860 * that the srchval is composed of a type and 2861 * vdev id pair (i.e. mirror-4). 2862 */ 2863 if ((type = strdup(srchval)) == NULL) 2864 return (NULL); 2865 2866 if ((p = strrchr(type, '-')) == NULL) { 2867 free(type); 2868 break; 2869 } 2870 idx = p + 1; 2871 *p = '\0'; 2872 2873 /* 2874 * If the types don't match then keep looking. 2875 */ 2876 if (strncmp(val, type, strlen(val)) != 0) { 2877 free(type); 2878 break; 2879 } 2880 2881 verify(zpool_vdev_is_interior(type)); 2882 2883 id = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID); 2884 errno = 0; 2885 vdev_id = strtoull(idx, &end, 10); 2886 2887 /* 2888 * If we are looking for a raidz and a parity is 2889 * specified, make sure it matches. 2890 */ 2891 int rzlen = strlen(VDEV_TYPE_RAIDZ); 2892 assert(rzlen == strlen(VDEV_TYPE_DRAID)); 2893 int typlen = strlen(type); 2894 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 || 2895 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) && 2896 typlen != rzlen) { 2897 uint64_t vdev_parity; 2898 int parity = *(type + rzlen) - '0'; 2899 2900 if (parity <= 0 || parity > 3 || 2901 (typlen - rzlen) != 1) { 2902 /* 2903 * Nonsense parity specified, can 2904 * never match 2905 */ 2906 free(type); 2907 return (NULL); 2908 } 2909 vdev_parity = fnvlist_lookup_uint64(nv, 2910 ZPOOL_CONFIG_NPARITY); 2911 if ((int)vdev_parity != parity) { 2912 free(type); 2913 break; 2914 } 2915 } 2916 2917 free(type); 2918 if (errno != 0) 2919 return (NULL); 2920 2921 /* 2922 * Now verify that we have the correct vdev id. 2923 */ 2924 if (vdev_id == id) 2925 return (nv); 2926 } 2927 2928 /* 2929 * Common case 2930 */ 2931 if (strcmp(srchval, val) == 0) 2932 return (nv); 2933 break; 2934 } 2935 2936 default: 2937 break; 2938 } 2939 2940 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2941 &child, &children) != 0) 2942 return (NULL); 2943 2944 for (c = 0; c < children; c++) { 2945 if ((ret = vdev_to_nvlist_iter(child[c], search, 2946 avail_spare, l2cache, NULL)) != NULL) { 2947 /* 2948 * The 'is_log' value is only set for the toplevel 2949 * vdev, not the leaf vdevs. So we always lookup the 2950 * log device from the root of the vdev tree (where 2951 * 'log' is non-NULL). 2952 */ 2953 if (log != NULL && 2954 nvlist_lookup_uint64(child[c], 2955 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2956 is_log) { 2957 *log = B_TRUE; 2958 } 2959 return (ret); 2960 } 2961 } 2962 2963 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 2964 &child, &children) == 0) { 2965 for (c = 0; c < children; c++) { 2966 if ((ret = vdev_to_nvlist_iter(child[c], search, 2967 avail_spare, l2cache, NULL)) != NULL) { 2968 *avail_spare = B_TRUE; 2969 return (ret); 2970 } 2971 } 2972 } 2973 2974 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2975 &child, &children) == 0) { 2976 for (c = 0; c < children; c++) { 2977 if ((ret = vdev_to_nvlist_iter(child[c], search, 2978 avail_spare, l2cache, NULL)) != NULL) { 2979 *l2cache = B_TRUE; 2980 return (ret); 2981 } 2982 } 2983 } 2984 2985 return (NULL); 2986 } 2987 2988 /* 2989 * Given a physical path or guid, find the associated vdev. 2990 */ 2991 nvlist_t * 2992 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2993 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2994 { 2995 nvlist_t *search, *nvroot, *ret; 2996 uint64_t guid; 2997 char *end; 2998 2999 search = fnvlist_alloc(); 3000 3001 guid = strtoull(ppath, &end, 0); 3002 if (guid != 0 && *end == '\0') { 3003 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid); 3004 } else { 3005 fnvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath); 3006 } 3007 3008 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config, 3009 ZPOOL_CONFIG_VDEV_TREE); 3010 3011 *avail_spare = B_FALSE; 3012 *l2cache = B_FALSE; 3013 if (log != NULL) 3014 *log = B_FALSE; 3015 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 3016 fnvlist_free(search); 3017 3018 return (ret); 3019 } 3020 3021 /* 3022 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 3023 */ 3024 static boolean_t 3025 zpool_vdev_is_interior(const char *name) 3026 { 3027 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 3028 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 || 3029 strncmp(name, 3030 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 || 3031 strncmp(name, VDEV_TYPE_ROOT, strlen(VDEV_TYPE_ROOT)) == 0 || 3032 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 3033 return (B_TRUE); 3034 3035 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 && 3036 !zpool_is_draid_spare(name)) 3037 return (B_TRUE); 3038 3039 return (B_FALSE); 3040 } 3041 3042 /* 3043 * Lookup the nvlist for a given vdev. 3044 */ 3045 nvlist_t * 3046 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 3047 boolean_t *l2cache, boolean_t *log) 3048 { 3049 char *end; 3050 nvlist_t *nvroot, *search, *ret; 3051 uint64_t guid; 3052 boolean_t __avail_spare, __l2cache, __log; 3053 3054 search = fnvlist_alloc(); 3055 3056 guid = strtoull(path, &end, 0); 3057 if (guid != 0 && *end == '\0') { 3058 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid); 3059 } else if (zpool_vdev_is_interior(path)) { 3060 fnvlist_add_string(search, ZPOOL_CONFIG_TYPE, path); 3061 } else { 3062 fnvlist_add_string(search, ZPOOL_CONFIG_PATH, path); 3063 } 3064 3065 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config, 3066 ZPOOL_CONFIG_VDEV_TREE); 3067 3068 /* 3069 * User can pass NULL for avail_spare, l2cache, and log, but 3070 * we still need to provide variables to vdev_to_nvlist_iter(), so 3071 * just point them to junk variables here. 3072 */ 3073 if (!avail_spare) 3074 avail_spare = &__avail_spare; 3075 if (!l2cache) 3076 l2cache = &__l2cache; 3077 if (!log) 3078 log = &__log; 3079 3080 *avail_spare = B_FALSE; 3081 *l2cache = B_FALSE; 3082 if (log != NULL) 3083 *log = B_FALSE; 3084 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 3085 fnvlist_free(search); 3086 3087 return (ret); 3088 } 3089 3090 /* 3091 * Convert a vdev path to a GUID. Returns GUID or 0 on error. 3092 * 3093 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it 3094 * if the VDEV is a spare, l2cache, or log device. If they're NULL then 3095 * ignore them. 3096 */ 3097 static uint64_t 3098 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path, 3099 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log) 3100 { 3101 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE; 3102 nvlist_t *tgt; 3103 3104 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache, 3105 &log)) == NULL) 3106 return (0); 3107 3108 if (is_spare != NULL) 3109 *is_spare = spare; 3110 if (is_l2cache != NULL) 3111 *is_l2cache = l2cache; 3112 if (is_log != NULL) 3113 *is_log = log; 3114 3115 return (fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID)); 3116 } 3117 3118 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */ 3119 uint64_t 3120 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path) 3121 { 3122 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL)); 3123 } 3124 3125 /* 3126 * Bring the specified vdev online. The 'flags' parameter is a set of the 3127 * ZFS_ONLINE_* flags. 3128 */ 3129 int 3130 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 3131 vdev_state_t *newstate) 3132 { 3133 zfs_cmd_t zc = {"\0"}; 3134 char errbuf[ERRBUFLEN]; 3135 nvlist_t *tgt; 3136 boolean_t avail_spare, l2cache, islog; 3137 libzfs_handle_t *hdl = zhp->zpool_hdl; 3138 3139 if (flags & ZFS_ONLINE_EXPAND) { 3140 (void) snprintf(errbuf, sizeof (errbuf), 3141 dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 3142 } else { 3143 (void) snprintf(errbuf, sizeof (errbuf), 3144 dgettext(TEXT_DOMAIN, "cannot online %s"), path); 3145 } 3146 3147 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3148 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3149 &islog)) == NULL) 3150 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3151 3152 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3153 3154 if (!(flags & ZFS_ONLINE_SPARE) && avail_spare) 3155 return (zfs_error(hdl, EZFS_ISSPARE, errbuf)); 3156 3157 #ifndef __FreeBSD__ 3158 const char *pathname; 3159 if ((flags & ZFS_ONLINE_EXPAND || 3160 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) && 3161 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) { 3162 uint64_t wholedisk = 0; 3163 3164 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 3165 &wholedisk); 3166 3167 /* 3168 * XXX - L2ARC 1.0 devices can't support expansion. 3169 */ 3170 if (l2cache) { 3171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3172 "cannot expand cache devices")); 3173 return (zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf)); 3174 } 3175 3176 if (wholedisk) { 3177 const char *fullpath = path; 3178 char buf[MAXPATHLEN]; 3179 int error; 3180 3181 if (path[0] != '/') { 3182 error = zfs_resolve_shortname(path, buf, 3183 sizeof (buf)); 3184 if (error != 0) 3185 return (zfs_error(hdl, EZFS_NODEVICE, 3186 errbuf)); 3187 3188 fullpath = buf; 3189 } 3190 3191 error = zpool_relabel_disk(hdl, fullpath, errbuf); 3192 if (error != 0) 3193 return (error); 3194 } 3195 } 3196 #endif 3197 3198 zc.zc_cookie = VDEV_STATE_ONLINE; 3199 zc.zc_obj = flags; 3200 3201 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 3202 if (errno == EINVAL) { 3203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 3204 "from this pool into a new one. Use '%s' " 3205 "instead"), "zpool detach"); 3206 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, errbuf)); 3207 } 3208 return (zpool_standard_error(hdl, errno, errbuf)); 3209 } 3210 3211 *newstate = zc.zc_cookie; 3212 return (0); 3213 } 3214 3215 /* 3216 * Take the specified vdev offline 3217 */ 3218 int 3219 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 3220 { 3221 zfs_cmd_t zc = {"\0"}; 3222 char errbuf[ERRBUFLEN]; 3223 nvlist_t *tgt; 3224 boolean_t avail_spare, l2cache; 3225 libzfs_handle_t *hdl = zhp->zpool_hdl; 3226 3227 (void) snprintf(errbuf, sizeof (errbuf), 3228 dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 3229 3230 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3231 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3232 NULL)) == NULL) 3233 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3234 3235 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3236 3237 if (avail_spare) 3238 return (zfs_error(hdl, EZFS_ISSPARE, errbuf)); 3239 3240 zc.zc_cookie = VDEV_STATE_OFFLINE; 3241 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 3242 3243 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3244 return (0); 3245 3246 switch (errno) { 3247 case EBUSY: 3248 3249 /* 3250 * There are no other replicas of this device. 3251 */ 3252 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf)); 3253 3254 case EEXIST: 3255 /* 3256 * The log device has unplayed logs 3257 */ 3258 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, errbuf)); 3259 3260 default: 3261 return (zpool_standard_error(hdl, errno, errbuf)); 3262 } 3263 } 3264 3265 /* 3266 * Remove the specified vdev asynchronously from the configuration, so 3267 * that it may come ONLINE if reinserted. This is called from zed on 3268 * Udev remove event. 3269 * Note: We also have a similar function zpool_vdev_remove() that 3270 * removes the vdev from the pool. 3271 */ 3272 int 3273 zpool_vdev_remove_wanted(zpool_handle_t *zhp, const char *path) 3274 { 3275 zfs_cmd_t zc = {"\0"}; 3276 char errbuf[ERRBUFLEN]; 3277 nvlist_t *tgt; 3278 boolean_t avail_spare, l2cache; 3279 libzfs_handle_t *hdl = zhp->zpool_hdl; 3280 3281 (void) snprintf(errbuf, sizeof (errbuf), 3282 dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 3283 3284 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3285 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3286 NULL)) == NULL) 3287 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3288 3289 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3290 3291 zc.zc_cookie = VDEV_STATE_REMOVED; 3292 3293 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3294 return (0); 3295 3296 return (zpool_standard_error(hdl, errno, errbuf)); 3297 } 3298 3299 /* 3300 * Mark the given vdev faulted. 3301 */ 3302 int 3303 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 3304 { 3305 zfs_cmd_t zc = {"\0"}; 3306 char errbuf[ERRBUFLEN]; 3307 libzfs_handle_t *hdl = zhp->zpool_hdl; 3308 3309 (void) snprintf(errbuf, sizeof (errbuf), 3310 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid); 3311 3312 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3313 zc.zc_guid = guid; 3314 zc.zc_cookie = VDEV_STATE_FAULTED; 3315 zc.zc_obj = aux; 3316 3317 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3318 return (0); 3319 3320 switch (errno) { 3321 case EBUSY: 3322 3323 /* 3324 * There are no other replicas of this device. 3325 */ 3326 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf)); 3327 3328 default: 3329 return (zpool_standard_error(hdl, errno, errbuf)); 3330 } 3331 3332 } 3333 3334 /* 3335 * Generic set vdev state function 3336 */ 3337 static int 3338 zpool_vdev_set_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux, 3339 vdev_state_t state) 3340 { 3341 zfs_cmd_t zc = {"\0"}; 3342 char errbuf[ERRBUFLEN]; 3343 libzfs_handle_t *hdl = zhp->zpool_hdl; 3344 3345 (void) snprintf(errbuf, sizeof (errbuf), 3346 dgettext(TEXT_DOMAIN, "cannot set %s %llu"), 3347 zpool_state_to_name(state, aux), (u_longlong_t)guid); 3348 3349 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3350 zc.zc_guid = guid; 3351 zc.zc_cookie = state; 3352 zc.zc_obj = aux; 3353 3354 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3355 return (0); 3356 3357 return (zpool_standard_error(hdl, errno, errbuf)); 3358 } 3359 3360 /* 3361 * Mark the given vdev degraded. 3362 */ 3363 int 3364 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 3365 { 3366 return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_DEGRADED)); 3367 } 3368 3369 /* 3370 * Mark the given vdev as in a removed state (as if the device does not exist). 3371 * 3372 * This is different than zpool_vdev_remove() which does a removal of a device 3373 * from the pool (but the device does exist). 3374 */ 3375 int 3376 zpool_vdev_set_removed_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 3377 { 3378 return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_REMOVED)); 3379 } 3380 3381 /* 3382 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 3383 * a hot spare. 3384 */ 3385 static boolean_t 3386 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 3387 { 3388 nvlist_t **child; 3389 uint_t c, children; 3390 3391 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 3392 &children) == 0) { 3393 const char *type = fnvlist_lookup_string(search, 3394 ZPOOL_CONFIG_TYPE); 3395 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 || 3396 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) && 3397 children == 2 && child[which] == tgt) 3398 return (B_TRUE); 3399 3400 for (c = 0; c < children; c++) 3401 if (is_replacing_spare(child[c], tgt, which)) 3402 return (B_TRUE); 3403 } 3404 3405 return (B_FALSE); 3406 } 3407 3408 /* 3409 * Attach new_disk (fully described by nvroot) to old_disk. 3410 * If 'replacing' is specified, the new disk will replace the old one. 3411 */ 3412 int 3413 zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk, 3414 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild) 3415 { 3416 zfs_cmd_t zc = {"\0"}; 3417 char errbuf[ERRBUFLEN]; 3418 int ret; 3419 nvlist_t *tgt; 3420 boolean_t avail_spare, l2cache, islog; 3421 uint64_t val; 3422 char *newname; 3423 const char *type; 3424 nvlist_t **child; 3425 uint_t children; 3426 nvlist_t *config_root; 3427 libzfs_handle_t *hdl = zhp->zpool_hdl; 3428 3429 if (replacing) 3430 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3431 "cannot replace %s with %s"), old_disk, new_disk); 3432 else 3433 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3434 "cannot attach %s to %s"), new_disk, old_disk); 3435 3436 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3437 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 3438 &islog)) == NULL) 3439 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3440 3441 if (avail_spare) 3442 return (zfs_error(hdl, EZFS_ISSPARE, errbuf)); 3443 3444 if (l2cache) 3445 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf)); 3446 3447 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3448 zc.zc_cookie = replacing; 3449 zc.zc_simple = rebuild; 3450 3451 if (rebuild && 3452 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) { 3453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3454 "the loaded zfs module doesn't support device rebuilds")); 3455 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3456 } 3457 3458 type = fnvlist_lookup_string(tgt, ZPOOL_CONFIG_TYPE); 3459 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 && 3460 zfeature_lookup_guid("org.openzfs:raidz_expansion", NULL) != 0) { 3461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3462 "the loaded zfs module doesn't support raidz expansion")); 3463 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3464 } 3465 3466 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3467 &child, &children) != 0 || children != 1) { 3468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3469 "new device must be a single disk")); 3470 return (zfs_error(hdl, EZFS_INVALCONFIG, errbuf)); 3471 } 3472 3473 config_root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 3474 ZPOOL_CONFIG_VDEV_TREE); 3475 3476 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL) 3477 return (-1); 3478 3479 /* 3480 * If the target is a hot spare that has been swapped in, we can only 3481 * replace it with another hot spare. 3482 */ 3483 if (replacing && 3484 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 3485 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 3486 NULL) == NULL || !avail_spare) && 3487 is_replacing_spare(config_root, tgt, 1)) { 3488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3489 "can only be replaced by another hot spare")); 3490 free(newname); 3491 return (zfs_error(hdl, EZFS_BADTARGET, errbuf)); 3492 } 3493 3494 free(newname); 3495 3496 zcmd_write_conf_nvlist(hdl, &zc, nvroot); 3497 3498 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 3499 3500 zcmd_free_nvlists(&zc); 3501 3502 if (ret == 0) 3503 return (0); 3504 3505 switch (errno) { 3506 case ENOTSUP: 3507 /* 3508 * Can't attach to or replace this type of vdev. 3509 */ 3510 if (replacing) { 3511 uint64_t version = zpool_get_prop_int(zhp, 3512 ZPOOL_PROP_VERSION, NULL); 3513 3514 if (islog) { 3515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3516 "cannot replace a log with a spare")); 3517 } else if (rebuild) { 3518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3519 "only mirror and dRAID vdevs support " 3520 "sequential reconstruction")); 3521 } else if (zpool_is_draid_spare(new_disk)) { 3522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3523 "dRAID spares can only replace child " 3524 "devices in their parent's dRAID vdev")); 3525 } else if (version >= SPA_VERSION_MULTI_REPLACE) { 3526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3527 "already in replacing/spare config; wait " 3528 "for completion or use 'zpool detach'")); 3529 } else { 3530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3531 "cannot replace a replacing device")); 3532 } 3533 } else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 3534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3535 "raidz_expansion feature must be enabled " 3536 "in order to attach a device to raidz")); 3537 } else { 3538 char status[64] = {0}; 3539 zpool_prop_get_feature(zhp, 3540 "feature@device_rebuild", status, 63); 3541 if (rebuild && 3542 strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) { 3543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3544 "device_rebuild feature must be enabled " 3545 "in order to use sequential " 3546 "reconstruction")); 3547 } else { 3548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3549 "can only attach to mirrors and top-level " 3550 "disks")); 3551 } 3552 } 3553 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf); 3554 break; 3555 3556 case EINVAL: 3557 /* 3558 * The new device must be a single disk. 3559 */ 3560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3561 "new device must be a single disk")); 3562 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3563 break; 3564 3565 case EBUSY: 3566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 3567 new_disk); 3568 (void) zfs_error(hdl, EZFS_BADDEV, errbuf); 3569 break; 3570 3571 case EOVERFLOW: 3572 /* 3573 * The new device is too small. 3574 */ 3575 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3576 "device is too small")); 3577 (void) zfs_error(hdl, EZFS_BADDEV, errbuf); 3578 break; 3579 3580 case EDOM: 3581 /* 3582 * The new device has a different optimal sector size. 3583 */ 3584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3585 "new device has a different optimal sector size; use the " 3586 "option '-o ashift=N' to override the optimal size")); 3587 (void) zfs_error(hdl, EZFS_BADDEV, errbuf); 3588 break; 3589 3590 case ENAMETOOLONG: 3591 /* 3592 * The resulting top-level vdev spec won't fit in the label. 3593 */ 3594 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3595 break; 3596 3597 case ENXIO: 3598 /* 3599 * The existing raidz vdev has offline children 3600 */ 3601 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 3602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3603 "raidz vdev has devices that are are offline or " 3604 "being replaced")); 3605 (void) zfs_error(hdl, EZFS_BADDEV, errbuf); 3606 break; 3607 } else { 3608 (void) zpool_standard_error(hdl, errno, errbuf); 3609 } 3610 break; 3611 3612 case EADDRINUSE: 3613 /* 3614 * The boot reserved area is already being used (FreeBSD) 3615 */ 3616 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 3617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3618 "the reserved boot area needed for the expansion " 3619 "is already being used by a boot loader")); 3620 (void) zfs_error(hdl, EZFS_BADDEV, errbuf); 3621 } else { 3622 (void) zpool_standard_error(hdl, errno, errbuf); 3623 } 3624 break; 3625 default: 3626 (void) zpool_standard_error(hdl, errno, errbuf); 3627 } 3628 3629 return (-1); 3630 } 3631 3632 /* 3633 * Detach the specified device. 3634 */ 3635 int 3636 zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 3637 { 3638 zfs_cmd_t zc = {"\0"}; 3639 char errbuf[ERRBUFLEN]; 3640 nvlist_t *tgt; 3641 boolean_t avail_spare, l2cache; 3642 libzfs_handle_t *hdl = zhp->zpool_hdl; 3643 3644 (void) snprintf(errbuf, sizeof (errbuf), 3645 dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 3646 3647 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3648 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3649 NULL)) == NULL) 3650 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3651 3652 if (avail_spare) 3653 return (zfs_error(hdl, EZFS_ISSPARE, errbuf)); 3654 3655 if (l2cache) 3656 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf)); 3657 3658 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3659 3660 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 3661 return (0); 3662 3663 switch (errno) { 3664 3665 case ENOTSUP: 3666 /* 3667 * Can't detach from this type of vdev. 3668 */ 3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 3670 "applicable to mirror and replacing vdevs")); 3671 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf); 3672 break; 3673 3674 case EBUSY: 3675 /* 3676 * There are no other replicas of this device. 3677 */ 3678 (void) zfs_error(hdl, EZFS_NOREPLICAS, errbuf); 3679 break; 3680 3681 default: 3682 (void) zpool_standard_error(hdl, errno, errbuf); 3683 } 3684 3685 return (-1); 3686 } 3687 3688 /* 3689 * Find a mirror vdev in the source nvlist. 3690 * 3691 * The mchild array contains a list of disks in one of the top-level mirrors 3692 * of the source pool. The schild array contains a list of disks that the 3693 * user specified on the command line. We loop over the mchild array to 3694 * see if any entry in the schild array matches. 3695 * 3696 * If a disk in the mchild array is found in the schild array, we return 3697 * the index of that entry. Otherwise we return -1. 3698 */ 3699 static int 3700 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 3701 nvlist_t **schild, uint_t schildren) 3702 { 3703 uint_t mc; 3704 3705 for (mc = 0; mc < mchildren; mc++) { 3706 uint_t sc; 3707 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3708 mchild[mc], 0); 3709 3710 for (sc = 0; sc < schildren; sc++) { 3711 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3712 schild[sc], 0); 3713 boolean_t result = (strcmp(mpath, spath) == 0); 3714 3715 free(spath); 3716 if (result) { 3717 free(mpath); 3718 return (mc); 3719 } 3720 } 3721 3722 free(mpath); 3723 } 3724 3725 return (-1); 3726 } 3727 3728 /* 3729 * Split a mirror pool. If newroot points to null, then a new nvlist 3730 * is generated and it is the responsibility of the caller to free it. 3731 */ 3732 int 3733 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 3734 nvlist_t *props, splitflags_t flags) 3735 { 3736 zfs_cmd_t zc = {"\0"}; 3737 char errbuf[ERRBUFLEN]; 3738 const char *bias; 3739 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 3740 nvlist_t **varray = NULL, *zc_props = NULL; 3741 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 3742 libzfs_handle_t *hdl = zhp->zpool_hdl; 3743 uint64_t vers, readonly = B_FALSE; 3744 boolean_t freelist = B_FALSE, memory_err = B_TRUE; 3745 int retval = 0; 3746 3747 (void) snprintf(errbuf, sizeof (errbuf), 3748 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 3749 3750 if (!zpool_name_valid(hdl, B_FALSE, newname)) 3751 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3752 3753 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 3754 (void) fprintf(stderr, gettext("Internal error: unable to " 3755 "retrieve pool configuration\n")); 3756 return (-1); 3757 } 3758 3759 tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 3760 vers = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); 3761 3762 if (props) { 3763 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 3764 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 3765 props, vers, flags, errbuf)) == NULL) 3766 return (-1); 3767 (void) nvlist_lookup_uint64(zc_props, 3768 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3769 if (readonly) { 3770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3771 "property %s can only be set at import time"), 3772 zpool_prop_to_name(ZPOOL_PROP_READONLY)); 3773 return (-1); 3774 } 3775 } 3776 3777 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 3778 &children) != 0) { 3779 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3780 "Source pool is missing vdev tree")); 3781 nvlist_free(zc_props); 3782 return (-1); 3783 } 3784 3785 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 3786 vcount = 0; 3787 3788 if (*newroot == NULL || 3789 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 3790 &newchild, &newchildren) != 0) 3791 newchildren = 0; 3792 3793 for (c = 0; c < children; c++) { 3794 uint64_t is_log = B_FALSE, is_hole = B_FALSE; 3795 boolean_t is_special = B_FALSE, is_dedup = B_FALSE; 3796 const char *type; 3797 nvlist_t **mchild, *vdev; 3798 uint_t mchildren; 3799 int entry; 3800 3801 /* 3802 * Unlike cache & spares, slogs are stored in the 3803 * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 3804 */ 3805 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3806 &is_log); 3807 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 3808 &is_hole); 3809 if (is_log || is_hole) { 3810 /* 3811 * Create a hole vdev and put it in the config. 3812 */ 3813 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 3814 goto out; 3815 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 3816 VDEV_TYPE_HOLE) != 0) 3817 goto out; 3818 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 3819 1) != 0) 3820 goto out; 3821 if (lastlog == 0) 3822 lastlog = vcount; 3823 varray[vcount++] = vdev; 3824 continue; 3825 } 3826 lastlog = 0; 3827 type = fnvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE); 3828 3829 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) { 3830 vdev = child[c]; 3831 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 3832 goto out; 3833 continue; 3834 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 3835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3836 "Source pool must be composed only of mirrors\n")); 3837 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3838 goto out; 3839 } 3840 3841 if (nvlist_lookup_string(child[c], 3842 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) { 3843 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 3844 is_special = B_TRUE; 3845 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 3846 is_dedup = B_TRUE; 3847 } 3848 verify(nvlist_lookup_nvlist_array(child[c], 3849 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 3850 3851 /* find or add an entry for this top-level vdev */ 3852 if (newchildren > 0 && 3853 (entry = find_vdev_entry(zhp, mchild, mchildren, 3854 newchild, newchildren)) >= 0) { 3855 /* We found a disk that the user specified. */ 3856 vdev = mchild[entry]; 3857 ++found; 3858 } else { 3859 /* User didn't specify a disk for this vdev. */ 3860 vdev = mchild[mchildren - 1]; 3861 } 3862 3863 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 3864 goto out; 3865 3866 if (flags.dryrun != 0) { 3867 if (is_dedup == B_TRUE) { 3868 if (nvlist_add_string(varray[vcount - 1], 3869 ZPOOL_CONFIG_ALLOCATION_BIAS, 3870 VDEV_ALLOC_BIAS_DEDUP) != 0) 3871 goto out; 3872 } else if (is_special == B_TRUE) { 3873 if (nvlist_add_string(varray[vcount - 1], 3874 ZPOOL_CONFIG_ALLOCATION_BIAS, 3875 VDEV_ALLOC_BIAS_SPECIAL) != 0) 3876 goto out; 3877 } 3878 } 3879 } 3880 3881 /* did we find every disk the user specified? */ 3882 if (found != newchildren) { 3883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 3884 "include at most one disk from each mirror")); 3885 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3886 goto out; 3887 } 3888 3889 /* Prepare the nvlist for populating. */ 3890 if (*newroot == NULL) { 3891 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 3892 goto out; 3893 freelist = B_TRUE; 3894 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 3895 VDEV_TYPE_ROOT) != 0) 3896 goto out; 3897 } else { 3898 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 3899 } 3900 3901 /* Add all the children we found */ 3902 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 3903 (const nvlist_t **)varray, lastlog == 0 ? vcount : lastlog) != 0) 3904 goto out; 3905 3906 /* 3907 * If we're just doing a dry run, exit now with success. 3908 */ 3909 if (flags.dryrun) { 3910 memory_err = B_FALSE; 3911 freelist = B_FALSE; 3912 goto out; 3913 } 3914 3915 /* now build up the config list & call the ioctl */ 3916 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 3917 goto out; 3918 3919 if (nvlist_add_nvlist(newconfig, 3920 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 3921 nvlist_add_string(newconfig, 3922 ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 3923 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 3924 goto out; 3925 3926 /* 3927 * The new pool is automatically part of the namespace unless we 3928 * explicitly export it. 3929 */ 3930 if (!flags.import) 3931 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 3932 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3933 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 3934 zcmd_write_conf_nvlist(hdl, &zc, newconfig); 3935 if (zc_props != NULL) 3936 zcmd_write_src_nvlist(hdl, &zc, zc_props); 3937 3938 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 3939 retval = zpool_standard_error(hdl, errno, errbuf); 3940 goto out; 3941 } 3942 3943 freelist = B_FALSE; 3944 memory_err = B_FALSE; 3945 3946 out: 3947 if (varray != NULL) { 3948 int v; 3949 3950 for (v = 0; v < vcount; v++) 3951 nvlist_free(varray[v]); 3952 free(varray); 3953 } 3954 zcmd_free_nvlists(&zc); 3955 nvlist_free(zc_props); 3956 nvlist_free(newconfig); 3957 if (freelist) { 3958 nvlist_free(*newroot); 3959 *newroot = NULL; 3960 } 3961 3962 if (retval != 0) 3963 return (retval); 3964 3965 if (memory_err) 3966 return (no_memory(hdl)); 3967 3968 return (0); 3969 } 3970 3971 /* 3972 * Remove the given device. 3973 */ 3974 int 3975 zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 3976 { 3977 zfs_cmd_t zc = {"\0"}; 3978 char errbuf[ERRBUFLEN]; 3979 nvlist_t *tgt; 3980 boolean_t avail_spare, l2cache, islog; 3981 libzfs_handle_t *hdl = zhp->zpool_hdl; 3982 uint64_t version; 3983 3984 (void) snprintf(errbuf, sizeof (errbuf), 3985 dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 3986 3987 if (zpool_is_draid_spare(path)) { 3988 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3989 "dRAID spares cannot be removed")); 3990 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3991 } 3992 3993 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3994 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3995 &islog)) == NULL) 3996 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 3997 3998 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 3999 if (islog && version < SPA_VERSION_HOLES) { 4000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4001 "pool must be upgraded to support log removal")); 4002 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 4003 } 4004 4005 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 4006 4007 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 4008 return (0); 4009 4010 switch (errno) { 4011 4012 case EALREADY: 4013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4014 "removal for this vdev is already in progress.")); 4015 (void) zfs_error(hdl, EZFS_BUSY, errbuf); 4016 break; 4017 4018 case EINVAL: 4019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4020 "invalid config; all top-level vdevs must " 4021 "have the same sector size and not be raidz.")); 4022 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4023 break; 4024 4025 case EBUSY: 4026 if (islog) { 4027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4028 "Mount encrypted datasets to replay logs.")); 4029 } else { 4030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4031 "Pool busy; removal may already be in progress")); 4032 } 4033 (void) zfs_error(hdl, EZFS_BUSY, errbuf); 4034 break; 4035 4036 case EACCES: 4037 if (islog) { 4038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4039 "Mount encrypted datasets to replay logs.")); 4040 (void) zfs_error(hdl, EZFS_BUSY, errbuf); 4041 } else { 4042 (void) zpool_standard_error(hdl, errno, errbuf); 4043 } 4044 break; 4045 4046 default: 4047 (void) zpool_standard_error(hdl, errno, errbuf); 4048 } 4049 return (-1); 4050 } 4051 4052 int 4053 zpool_vdev_remove_cancel(zpool_handle_t *zhp) 4054 { 4055 zfs_cmd_t zc = {{0}}; 4056 char errbuf[ERRBUFLEN]; 4057 libzfs_handle_t *hdl = zhp->zpool_hdl; 4058 4059 (void) snprintf(errbuf, sizeof (errbuf), 4060 dgettext(TEXT_DOMAIN, "cannot cancel removal")); 4061 4062 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4063 zc.zc_cookie = 1; 4064 4065 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 4066 return (0); 4067 4068 return (zpool_standard_error(hdl, errno, errbuf)); 4069 } 4070 4071 int 4072 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path, 4073 uint64_t *sizep) 4074 { 4075 char errbuf[ERRBUFLEN]; 4076 nvlist_t *tgt; 4077 boolean_t avail_spare, l2cache, islog; 4078 libzfs_handle_t *hdl = zhp->zpool_hdl; 4079 4080 (void) snprintf(errbuf, sizeof (errbuf), 4081 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"), 4082 path); 4083 4084 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 4085 &islog)) == NULL) 4086 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 4087 4088 if (avail_spare || l2cache || islog) { 4089 *sizep = 0; 4090 return (0); 4091 } 4092 4093 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) { 4094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4095 "indirect size not available")); 4096 return (zfs_error(hdl, EINVAL, errbuf)); 4097 } 4098 return (0); 4099 } 4100 4101 /* 4102 * Clear the errors for the pool, or the particular device if specified. 4103 */ 4104 int 4105 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 4106 { 4107 zfs_cmd_t zc = {"\0"}; 4108 char errbuf[ERRBUFLEN]; 4109 nvlist_t *tgt; 4110 zpool_load_policy_t policy; 4111 boolean_t avail_spare, l2cache; 4112 libzfs_handle_t *hdl = zhp->zpool_hdl; 4113 nvlist_t *nvi = NULL; 4114 int error; 4115 4116 if (path) 4117 (void) snprintf(errbuf, sizeof (errbuf), 4118 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 4119 path); 4120 else 4121 (void) snprintf(errbuf, sizeof (errbuf), 4122 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 4123 zhp->zpool_name); 4124 4125 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4126 if (path) { 4127 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 4128 &l2cache, NULL)) == NULL) 4129 return (zfs_error(hdl, EZFS_NODEVICE, errbuf)); 4130 4131 /* 4132 * Don't allow error clearing for hot spares. Do allow 4133 * error clearing for l2cache devices. 4134 */ 4135 if (avail_spare) 4136 return (zfs_error(hdl, EZFS_ISSPARE, errbuf)); 4137 4138 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 4139 } 4140 4141 zpool_get_load_policy(rewindnvl, &policy); 4142 zc.zc_cookie = policy.zlp_rewind; 4143 4144 zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2); 4145 zcmd_write_src_nvlist(hdl, &zc, rewindnvl); 4146 4147 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 4148 errno == ENOMEM) 4149 zcmd_expand_dst_nvlist(hdl, &zc); 4150 4151 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) && 4152 errno != EPERM && errno != EACCES)) { 4153 if (policy.zlp_rewind & 4154 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 4155 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 4156 zpool_rewind_exclaim(hdl, zc.zc_name, 4157 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), 4158 nvi); 4159 nvlist_free(nvi); 4160 } 4161 zcmd_free_nvlists(&zc); 4162 return (0); 4163 } 4164 4165 zcmd_free_nvlists(&zc); 4166 return (zpool_standard_error(hdl, errno, errbuf)); 4167 } 4168 4169 /* 4170 * Similar to zpool_clear(), but takes a GUID (used by fmd). 4171 */ 4172 int 4173 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 4174 { 4175 zfs_cmd_t zc = {"\0"}; 4176 char errbuf[ERRBUFLEN]; 4177 libzfs_handle_t *hdl = zhp->zpool_hdl; 4178 4179 (void) snprintf(errbuf, sizeof (errbuf), 4180 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 4181 (u_longlong_t)guid); 4182 4183 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4184 zc.zc_guid = guid; 4185 zc.zc_cookie = ZPOOL_NO_REWIND; 4186 4187 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 4188 return (0); 4189 4190 return (zpool_standard_error(hdl, errno, errbuf)); 4191 } 4192 4193 /* 4194 * Change the GUID for a pool. 4195 */ 4196 int 4197 zpool_reguid(zpool_handle_t *zhp) 4198 { 4199 char errbuf[ERRBUFLEN]; 4200 libzfs_handle_t *hdl = zhp->zpool_hdl; 4201 zfs_cmd_t zc = {"\0"}; 4202 4203 (void) snprintf(errbuf, sizeof (errbuf), 4204 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 4205 4206 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4207 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 4208 return (0); 4209 4210 return (zpool_standard_error(hdl, errno, errbuf)); 4211 } 4212 4213 /* 4214 * Reopen the pool. 4215 */ 4216 int 4217 zpool_reopen_one(zpool_handle_t *zhp, void *data) 4218 { 4219 libzfs_handle_t *hdl = zpool_get_handle(zhp); 4220 const char *pool_name = zpool_get_name(zhp); 4221 boolean_t *scrub_restart = data; 4222 int error; 4223 4224 error = lzc_reopen(pool_name, *scrub_restart); 4225 if (error) { 4226 return (zpool_standard_error_fmt(hdl, error, 4227 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name)); 4228 } 4229 4230 return (0); 4231 } 4232 4233 /* call into libzfs_core to execute the sync IOCTL per pool */ 4234 int 4235 zpool_sync_one(zpool_handle_t *zhp, void *data) 4236 { 4237 int ret; 4238 libzfs_handle_t *hdl = zpool_get_handle(zhp); 4239 const char *pool_name = zpool_get_name(zhp); 4240 boolean_t *force = data; 4241 nvlist_t *innvl = fnvlist_alloc(); 4242 4243 fnvlist_add_boolean_value(innvl, "force", *force); 4244 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) { 4245 nvlist_free(innvl); 4246 return (zpool_standard_error_fmt(hdl, ret, 4247 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name)); 4248 } 4249 nvlist_free(innvl); 4250 4251 return (0); 4252 } 4253 4254 #define PATH_BUF_LEN 64 4255 4256 /* 4257 * Given a vdev, return the name to display in iostat. If the vdev has a path, 4258 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 4259 * We also check if this is a whole disk, in which case we strip off the 4260 * trailing 's0' slice name. 4261 * 4262 * This routine is also responsible for identifying when disks have been 4263 * reconfigured in a new location. The kernel will have opened the device by 4264 * devid, but the path will still refer to the old location. To catch this, we 4265 * first do a path -> devid translation (which is fast for the common case). If 4266 * the devid matches, we're done. If not, we do a reverse devid -> path 4267 * translation and issue the appropriate ioctl() to update the path of the vdev. 4268 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 4269 * of these checks. 4270 */ 4271 char * 4272 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 4273 int name_flags) 4274 { 4275 const char *type, *tpath; 4276 const char *path; 4277 uint64_t value; 4278 char buf[PATH_BUF_LEN]; 4279 char tmpbuf[PATH_BUF_LEN * 2]; 4280 4281 /* 4282 * vdev_name will be "root"/"root-0" for the root vdev, but it is the 4283 * zpool name that will be displayed to the user. 4284 */ 4285 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE); 4286 if (zhp != NULL && strcmp(type, "root") == 0) 4287 return (zfs_strdup(hdl, zpool_get_name(zhp))); 4288 4289 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH")) 4290 name_flags |= VDEV_NAME_PATH; 4291 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID")) 4292 name_flags |= VDEV_NAME_GUID; 4293 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS")) 4294 name_flags |= VDEV_NAME_FOLLOW_LINKS; 4295 4296 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 || 4297 name_flags & VDEV_NAME_GUID) { 4298 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value); 4299 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value); 4300 path = buf; 4301 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tpath) == 0) { 4302 path = tpath; 4303 4304 if (name_flags & VDEV_NAME_FOLLOW_LINKS) { 4305 char *rp = realpath(path, NULL); 4306 if (rp) { 4307 strlcpy(buf, rp, sizeof (buf)); 4308 path = buf; 4309 free(rp); 4310 } 4311 } 4312 4313 /* 4314 * For a block device only use the name. 4315 */ 4316 if ((strcmp(type, VDEV_TYPE_DISK) == 0) && 4317 !(name_flags & VDEV_NAME_PATH)) { 4318 path = zfs_strip_path(path); 4319 } 4320 4321 /* 4322 * Remove the partition from the path if this is a whole disk. 4323 */ 4324 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 && 4325 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value) 4326 == 0 && value && !(name_flags & VDEV_NAME_PATH)) { 4327 return (zfs_strip_partition(path)); 4328 } 4329 } else { 4330 path = type; 4331 4332 /* 4333 * If it's a raidz device, we need to stick in the parity level. 4334 */ 4335 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 4336 value = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY); 4337 (void) snprintf(buf, sizeof (buf), "%s%llu", path, 4338 (u_longlong_t)value); 4339 path = buf; 4340 } 4341 4342 /* 4343 * If it's a dRAID device, we add parity, groups, and spares. 4344 */ 4345 if (strcmp(path, VDEV_TYPE_DRAID) == 0) { 4346 uint64_t ndata, nparity, nspares; 4347 nvlist_t **child; 4348 uint_t children; 4349 4350 verify(nvlist_lookup_nvlist_array(nv, 4351 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0); 4352 nparity = fnvlist_lookup_uint64(nv, 4353 ZPOOL_CONFIG_NPARITY); 4354 ndata = fnvlist_lookup_uint64(nv, 4355 ZPOOL_CONFIG_DRAID_NDATA); 4356 nspares = fnvlist_lookup_uint64(nv, 4357 ZPOOL_CONFIG_DRAID_NSPARES); 4358 4359 path = zpool_draid_name(buf, sizeof (buf), ndata, 4360 nparity, nspares, children); 4361 } 4362 4363 /* 4364 * We identify each top-level vdev by using a <type-id> 4365 * naming convention. 4366 */ 4367 if (name_flags & VDEV_NAME_TYPE_ID) { 4368 uint64_t id = fnvlist_lookup_uint64(nv, 4369 ZPOOL_CONFIG_ID); 4370 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu", 4371 path, (u_longlong_t)id); 4372 path = tmpbuf; 4373 } 4374 } 4375 4376 return (zfs_strdup(hdl, path)); 4377 } 4378 4379 static int 4380 zbookmark_mem_compare(const void *a, const void *b) 4381 { 4382 return (memcmp(a, b, sizeof (zbookmark_phys_t))); 4383 } 4384 4385 /* 4386 * Retrieve the persistent error log, uniquify the members, and return to the 4387 * caller. 4388 */ 4389 int 4390 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 4391 { 4392 zfs_cmd_t zc = {"\0"}; 4393 libzfs_handle_t *hdl = zhp->zpool_hdl; 4394 zbookmark_phys_t *buf; 4395 uint64_t buflen = 10000; /* approx. 1MB of RAM */ 4396 4397 if (fnvlist_lookup_uint64(zhp->zpool_config, 4398 ZPOOL_CONFIG_ERRCOUNT) == 0) 4399 return (0); 4400 4401 /* 4402 * Retrieve the raw error list from the kernel. If it doesn't fit, 4403 * allocate a larger buffer and retry. 4404 */ 4405 (void) strcpy(zc.zc_name, zhp->zpool_name); 4406 for (;;) { 4407 buf = zfs_alloc(zhp->zpool_hdl, 4408 buflen * sizeof (zbookmark_phys_t)); 4409 zc.zc_nvlist_dst = (uintptr_t)buf; 4410 zc.zc_nvlist_dst_size = buflen; 4411 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG, 4412 &zc) != 0) { 4413 free(buf); 4414 if (errno == ENOMEM) { 4415 buflen *= 2; 4416 } else { 4417 return (zpool_standard_error_fmt(hdl, errno, 4418 dgettext(TEXT_DOMAIN, "errors: List of " 4419 "errors unavailable"))); 4420 } 4421 } else { 4422 break; 4423 } 4424 } 4425 4426 /* 4427 * Sort the resulting bookmarks. This is a little confusing due to the 4428 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 4429 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks 4430 * _not_ copied as part of the process. So we point the start of our 4431 * array appropriate and decrement the total number of elements. 4432 */ 4433 zbookmark_phys_t *zb = buf + zc.zc_nvlist_dst_size; 4434 uint64_t zblen = buflen - zc.zc_nvlist_dst_size; 4435 4436 qsort(zb, zblen, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 4437 4438 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 4439 4440 /* 4441 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 4442 */ 4443 for (uint64_t i = 0; i < zblen; i++) { 4444 nvlist_t *nv; 4445 4446 /* ignoring zb_blkid and zb_level for now */ 4447 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 4448 zb[i-1].zb_object == zb[i].zb_object) 4449 continue; 4450 4451 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 4452 goto nomem; 4453 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 4454 zb[i].zb_objset) != 0) { 4455 nvlist_free(nv); 4456 goto nomem; 4457 } 4458 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 4459 zb[i].zb_object) != 0) { 4460 nvlist_free(nv); 4461 goto nomem; 4462 } 4463 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 4464 nvlist_free(nv); 4465 goto nomem; 4466 } 4467 nvlist_free(nv); 4468 } 4469 4470 free(buf); 4471 return (0); 4472 4473 nomem: 4474 free(buf); 4475 return (no_memory(zhp->zpool_hdl)); 4476 } 4477 4478 /* 4479 * Upgrade a ZFS pool to the latest on-disk version. 4480 */ 4481 int 4482 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 4483 { 4484 zfs_cmd_t zc = {"\0"}; 4485 libzfs_handle_t *hdl = zhp->zpool_hdl; 4486 4487 (void) strcpy(zc.zc_name, zhp->zpool_name); 4488 zc.zc_cookie = new_version; 4489 4490 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 4491 return (zpool_standard_error_fmt(hdl, errno, 4492 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 4493 zhp->zpool_name)); 4494 return (0); 4495 } 4496 4497 void 4498 zfs_save_arguments(int argc, char **argv, char *string, int len) 4499 { 4500 int i; 4501 4502 (void) strlcpy(string, zfs_basename(argv[0]), len); 4503 for (i = 1; i < argc; i++) { 4504 (void) strlcat(string, " ", len); 4505 (void) strlcat(string, argv[i], len); 4506 } 4507 } 4508 4509 int 4510 zpool_log_history(libzfs_handle_t *hdl, const char *message) 4511 { 4512 zfs_cmd_t zc = {"\0"}; 4513 nvlist_t *args; 4514 4515 args = fnvlist_alloc(); 4516 fnvlist_add_string(args, "message", message); 4517 zcmd_write_src_nvlist(hdl, &zc, args); 4518 int err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc); 4519 nvlist_free(args); 4520 zcmd_free_nvlists(&zc); 4521 return (err); 4522 } 4523 4524 /* 4525 * Perform ioctl to get some command history of a pool. 4526 * 4527 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 4528 * logical offset of the history buffer to start reading from. 4529 * 4530 * Upon return, 'off' is the next logical offset to read from and 4531 * 'len' is the actual amount of bytes read into 'buf'. 4532 */ 4533 static int 4534 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 4535 { 4536 zfs_cmd_t zc = {"\0"}; 4537 libzfs_handle_t *hdl = zhp->zpool_hdl; 4538 4539 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4540 4541 zc.zc_history = (uint64_t)(uintptr_t)buf; 4542 zc.zc_history_len = *len; 4543 zc.zc_history_offset = *off; 4544 4545 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 4546 switch (errno) { 4547 case EPERM: 4548 return (zfs_error_fmt(hdl, EZFS_PERM, 4549 dgettext(TEXT_DOMAIN, 4550 "cannot show history for pool '%s'"), 4551 zhp->zpool_name)); 4552 case ENOENT: 4553 return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 4554 dgettext(TEXT_DOMAIN, "cannot get history for pool " 4555 "'%s'"), zhp->zpool_name)); 4556 case ENOTSUP: 4557 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 4558 dgettext(TEXT_DOMAIN, "cannot get history for pool " 4559 "'%s', pool must be upgraded"), zhp->zpool_name)); 4560 default: 4561 return (zpool_standard_error_fmt(hdl, errno, 4562 dgettext(TEXT_DOMAIN, 4563 "cannot get history for '%s'"), zhp->zpool_name)); 4564 } 4565 } 4566 4567 *len = zc.zc_history_len; 4568 *off = zc.zc_history_offset; 4569 4570 return (0); 4571 } 4572 4573 /* 4574 * Retrieve the command history of a pool. 4575 */ 4576 int 4577 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off, 4578 boolean_t *eof) 4579 { 4580 libzfs_handle_t *hdl = zhp->zpool_hdl; 4581 char *buf; 4582 int buflen = 128 * 1024; 4583 nvlist_t **records = NULL; 4584 uint_t numrecords = 0; 4585 int err = 0, i; 4586 uint64_t start = *off; 4587 4588 buf = zfs_alloc(hdl, buflen); 4589 4590 /* process about 1MiB a time */ 4591 while (*off - start < 1024 * 1024) { 4592 uint64_t bytes_read = buflen; 4593 uint64_t leftover; 4594 4595 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0) 4596 break; 4597 4598 /* if nothing else was read in, we're at EOF, just return */ 4599 if (!bytes_read) { 4600 *eof = B_TRUE; 4601 break; 4602 } 4603 4604 if ((err = zpool_history_unpack(buf, bytes_read, 4605 &leftover, &records, &numrecords)) != 0) { 4606 zpool_standard_error_fmt(hdl, err, 4607 dgettext(TEXT_DOMAIN, 4608 "cannot get history for '%s'"), zhp->zpool_name); 4609 break; 4610 } 4611 *off -= leftover; 4612 if (leftover == bytes_read) { 4613 /* 4614 * no progress made, because buffer is not big enough 4615 * to hold this record; resize and retry. 4616 */ 4617 buflen *= 2; 4618 free(buf); 4619 buf = zfs_alloc(hdl, buflen); 4620 } 4621 } 4622 4623 free(buf); 4624 4625 if (!err) { 4626 *nvhisp = fnvlist_alloc(); 4627 fnvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 4628 (const nvlist_t **)records, numrecords); 4629 } 4630 for (i = 0; i < numrecords; i++) 4631 nvlist_free(records[i]); 4632 free(records); 4633 4634 return (err); 4635 } 4636 4637 /* 4638 * Retrieve the next event given the passed 'zevent_fd' file descriptor. 4639 * If there is a new event available 'nvp' will contain a newly allocated 4640 * nvlist and 'dropped' will be set to the number of missed events since 4641 * the last call to this function. When 'nvp' is set to NULL it indicates 4642 * no new events are available. In either case the function returns 0 and 4643 * it is up to the caller to free 'nvp'. In the case of a fatal error the 4644 * function will return a non-zero value. When the function is called in 4645 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed), 4646 * it will not return until a new event is available. 4647 */ 4648 int 4649 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp, 4650 int *dropped, unsigned flags, int zevent_fd) 4651 { 4652 zfs_cmd_t zc = {"\0"}; 4653 int error = 0; 4654 4655 *nvp = NULL; 4656 *dropped = 0; 4657 zc.zc_cleanup_fd = zevent_fd; 4658 4659 if (flags & ZEVENT_NONBLOCK) 4660 zc.zc_guid = ZEVENT_NONBLOCK; 4661 4662 zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE); 4663 4664 retry: 4665 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) { 4666 switch (errno) { 4667 case ESHUTDOWN: 4668 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 4669 dgettext(TEXT_DOMAIN, "zfs shutdown")); 4670 goto out; 4671 case ENOENT: 4672 /* Blocking error case should not occur */ 4673 if (!(flags & ZEVENT_NONBLOCK)) 4674 error = zpool_standard_error_fmt(hdl, errno, 4675 dgettext(TEXT_DOMAIN, "cannot get event")); 4676 4677 goto out; 4678 case ENOMEM: 4679 zcmd_expand_dst_nvlist(hdl, &zc); 4680 goto retry; 4681 default: 4682 error = zpool_standard_error_fmt(hdl, errno, 4683 dgettext(TEXT_DOMAIN, "cannot get event")); 4684 goto out; 4685 } 4686 } 4687 4688 error = zcmd_read_dst_nvlist(hdl, &zc, nvp); 4689 if (error != 0) 4690 goto out; 4691 4692 *dropped = (int)zc.zc_cookie; 4693 out: 4694 zcmd_free_nvlists(&zc); 4695 4696 return (error); 4697 } 4698 4699 /* 4700 * Clear all events. 4701 */ 4702 int 4703 zpool_events_clear(libzfs_handle_t *hdl, int *count) 4704 { 4705 zfs_cmd_t zc = {"\0"}; 4706 4707 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0) 4708 return (zpool_standard_error(hdl, errno, 4709 dgettext(TEXT_DOMAIN, "cannot clear events"))); 4710 4711 if (count != NULL) 4712 *count = (int)zc.zc_cookie; /* # of events cleared */ 4713 4714 return (0); 4715 } 4716 4717 /* 4718 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for 4719 * the passed zevent_fd file handle. On success zero is returned, 4720 * otherwise -1 is returned and hdl->libzfs_error is set to the errno. 4721 */ 4722 int 4723 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd) 4724 { 4725 zfs_cmd_t zc = {"\0"}; 4726 int error = 0; 4727 4728 zc.zc_guid = eid; 4729 zc.zc_cleanup_fd = zevent_fd; 4730 4731 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) { 4732 switch (errno) { 4733 case ENOENT: 4734 error = zfs_error_fmt(hdl, EZFS_NOENT, 4735 dgettext(TEXT_DOMAIN, "cannot get event")); 4736 break; 4737 4738 case ENOMEM: 4739 error = zfs_error_fmt(hdl, EZFS_NOMEM, 4740 dgettext(TEXT_DOMAIN, "cannot get event")); 4741 break; 4742 4743 default: 4744 error = zpool_standard_error_fmt(hdl, errno, 4745 dgettext(TEXT_DOMAIN, "cannot get event")); 4746 break; 4747 } 4748 } 4749 4750 return (error); 4751 } 4752 4753 static void 4754 zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4755 char *pathname, size_t len, boolean_t always_unmounted) 4756 { 4757 zfs_cmd_t zc = {"\0"}; 4758 boolean_t mounted = B_FALSE; 4759 char *mntpnt = NULL; 4760 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 4761 4762 if (dsobj == 0) { 4763 /* special case for the MOS */ 4764 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", 4765 (longlong_t)obj); 4766 return; 4767 } 4768 4769 /* get the dataset's name */ 4770 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4771 zc.zc_obj = dsobj; 4772 if (zfs_ioctl(zhp->zpool_hdl, 4773 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 4774 /* just write out a path of two object numbers */ 4775 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 4776 (longlong_t)dsobj, (longlong_t)obj); 4777 return; 4778 } 4779 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 4780 4781 /* find out if the dataset is mounted */ 4782 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname, 4783 &mntpnt); 4784 4785 /* get the corrupted object's path */ 4786 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 4787 zc.zc_obj = obj; 4788 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH, 4789 &zc) == 0) { 4790 if (mounted) { 4791 (void) snprintf(pathname, len, "%s%s", mntpnt, 4792 zc.zc_value); 4793 } else { 4794 (void) snprintf(pathname, len, "%s:%s", 4795 dsname, zc.zc_value); 4796 } 4797 } else { 4798 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, 4799 (longlong_t)obj); 4800 } 4801 free(mntpnt); 4802 } 4803 4804 void 4805 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4806 char *pathname, size_t len) 4807 { 4808 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE); 4809 } 4810 4811 void 4812 zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4813 char *pathname, size_t len) 4814 { 4815 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE); 4816 } 4817 /* 4818 * Wait while the specified activity is in progress in the pool. 4819 */ 4820 int 4821 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity) 4822 { 4823 boolean_t missing; 4824 4825 int error = zpool_wait_status(zhp, activity, &missing, NULL); 4826 4827 if (missing) { 4828 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT, 4829 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"), 4830 zhp->zpool_name); 4831 return (ENOENT); 4832 } else { 4833 return (error); 4834 } 4835 } 4836 4837 /* 4838 * Wait for the given activity and return the status of the wait (whether or not 4839 * any waiting was done) in the 'waited' parameter. Non-existent pools are 4840 * reported via the 'missing' parameter, rather than by printing an error 4841 * message. This is convenient when this function is called in a loop over a 4842 * long period of time (as it is, for example, by zpool's wait cmd). In that 4843 * scenario, a pool being exported or destroyed should be considered a normal 4844 * event, so we don't want to print an error when we find that the pool doesn't 4845 * exist. 4846 */ 4847 int 4848 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity, 4849 boolean_t *missing, boolean_t *waited) 4850 { 4851 int error = lzc_wait(zhp->zpool_name, activity, waited); 4852 *missing = (error == ENOENT); 4853 if (*missing) 4854 return (0); 4855 4856 if (error != 0) { 4857 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4858 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"), 4859 zhp->zpool_name); 4860 } 4861 4862 return (error); 4863 } 4864 4865 int 4866 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap) 4867 { 4868 int error = lzc_set_bootenv(zhp->zpool_name, envmap); 4869 if (error != 0) { 4870 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4871 dgettext(TEXT_DOMAIN, 4872 "error setting bootenv in pool '%s'"), zhp->zpool_name); 4873 } 4874 4875 return (error); 4876 } 4877 4878 int 4879 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp) 4880 { 4881 nvlist_t *nvl; 4882 int error; 4883 4884 nvl = NULL; 4885 error = lzc_get_bootenv(zhp->zpool_name, &nvl); 4886 if (error != 0) { 4887 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4888 dgettext(TEXT_DOMAIN, 4889 "error getting bootenv in pool '%s'"), zhp->zpool_name); 4890 } else { 4891 *nvlp = nvl; 4892 } 4893 4894 return (error); 4895 } 4896 4897 /* 4898 * Attempt to read and parse feature file(s) (from "compatibility" property). 4899 * Files contain zpool feature names, comma or whitespace-separated. 4900 * Comments (# character to next newline) are discarded. 4901 * 4902 * Arguments: 4903 * compatibility : string containing feature filenames 4904 * features : either NULL or pointer to array of boolean 4905 * report : either NULL or pointer to string buffer 4906 * rlen : length of "report" buffer 4907 * 4908 * compatibility is NULL (unset), "", "off", "legacy", or list of 4909 * comma-separated filenames. filenames should either be absolute, 4910 * or relative to: 4911 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or 4912 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d). 4913 * (Unset), "" or "off" => enable all features 4914 * "legacy" => disable all features 4915 * 4916 * Any feature names read from files which match unames in spa_feature_table 4917 * will have the corresponding boolean set in the features array (if non-NULL). 4918 * If more than one feature set specified, only features present in *all* of 4919 * them will be set. 4920 * 4921 * "report" if not NULL will be populated with a suitable status message. 4922 * 4923 * Return values: 4924 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok 4925 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file 4926 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name 4927 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name 4928 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found 4929 */ 4930 zpool_compat_status_t 4931 zpool_load_compat(const char *compat, boolean_t *features, char *report, 4932 size_t rlen) 4933 { 4934 int sdirfd, ddirfd, featfd; 4935 struct stat fs; 4936 char *fc; 4937 char *ps, *ls, *ws; 4938 char *file, *line, *word; 4939 4940 char l_compat[ZFS_MAXPROPLEN]; 4941 4942 boolean_t ret_nofiles = B_TRUE; 4943 boolean_t ret_badfile = B_FALSE; 4944 boolean_t ret_badtoken = B_FALSE; 4945 boolean_t ret_warntoken = B_FALSE; 4946 4947 /* special cases (unset), "" and "off" => enable all features */ 4948 if (compat == NULL || compat[0] == '\0' || 4949 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) { 4950 if (features != NULL) 4951 for (uint_t i = 0; i < SPA_FEATURES; i++) 4952 features[i] = B_TRUE; 4953 if (report != NULL) 4954 strlcpy(report, gettext("all features enabled"), rlen); 4955 return (ZPOOL_COMPATIBILITY_OK); 4956 } 4957 4958 /* Final special case "legacy" => disable all features */ 4959 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) { 4960 if (features != NULL) 4961 for (uint_t i = 0; i < SPA_FEATURES; i++) 4962 features[i] = B_FALSE; 4963 if (report != NULL) 4964 strlcpy(report, gettext("all features disabled"), rlen); 4965 return (ZPOOL_COMPATIBILITY_OK); 4966 } 4967 4968 /* 4969 * Start with all true; will be ANDed with results from each file 4970 */ 4971 if (features != NULL) 4972 for (uint_t i = 0; i < SPA_FEATURES; i++) 4973 features[i] = B_TRUE; 4974 4975 char err_badfile[ZFS_MAXPROPLEN] = ""; 4976 char err_badtoken[ZFS_MAXPROPLEN] = ""; 4977 4978 /* 4979 * We ignore errors from the directory open() 4980 * as they're only needed if the filename is relative 4981 * which will be checked during the openat(). 4982 */ 4983 4984 /* O_PATH safer than O_RDONLY if system allows it */ 4985 #if defined(O_PATH) 4986 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH) 4987 #else 4988 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY) 4989 #endif 4990 4991 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS); 4992 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS); 4993 4994 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN); 4995 4996 for (file = strtok_r(l_compat, ",", &ps); 4997 file != NULL; 4998 file = strtok_r(NULL, ",", &ps)) { 4999 5000 boolean_t l_features[SPA_FEATURES]; 5001 5002 enum { Z_SYSCONF, Z_DATA } source; 5003 5004 /* try sysconfdir first, then datadir */ 5005 source = Z_SYSCONF; 5006 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) { 5007 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC); 5008 source = Z_DATA; 5009 } 5010 5011 /* File readable and correct size? */ 5012 if (featfd < 0 || 5013 fstat(featfd, &fs) < 0 || 5014 fs.st_size < 1 || 5015 fs.st_size > ZPOOL_COMPAT_MAXSIZE) { 5016 (void) close(featfd); 5017 strlcat(err_badfile, file, ZFS_MAXPROPLEN); 5018 strlcat(err_badfile, " ", ZFS_MAXPROPLEN); 5019 ret_badfile = B_TRUE; 5020 continue; 5021 } 5022 5023 /* Prefault the file if system allows */ 5024 #if defined(MAP_POPULATE) 5025 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE) 5026 #elif defined(MAP_PREFAULT_READ) 5027 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ) 5028 #else 5029 #define ZC_MMAP_FLAGS (MAP_PRIVATE) 5030 #endif 5031 5032 /* private mmap() so we can strtok safely */ 5033 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE, 5034 ZC_MMAP_FLAGS, featfd, 0); 5035 (void) close(featfd); 5036 5037 /* map ok, and last character == newline? */ 5038 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') { 5039 (void) munmap((void *) fc, fs.st_size); 5040 strlcat(err_badfile, file, ZFS_MAXPROPLEN); 5041 strlcat(err_badfile, " ", ZFS_MAXPROPLEN); 5042 ret_badfile = B_TRUE; 5043 continue; 5044 } 5045 5046 ret_nofiles = B_FALSE; 5047 5048 for (uint_t i = 0; i < SPA_FEATURES; i++) 5049 l_features[i] = B_FALSE; 5050 5051 /* replace final newline with NULL to ensure string ends */ 5052 fc[fs.st_size - 1] = '\0'; 5053 5054 for (line = strtok_r(fc, "\n", &ls); 5055 line != NULL; 5056 line = strtok_r(NULL, "\n", &ls)) { 5057 /* discard comments */ 5058 char *r = strchr(line, '#'); 5059 if (r != NULL) 5060 *r = '\0'; 5061 5062 for (word = strtok_r(line, ", \t", &ws); 5063 word != NULL; 5064 word = strtok_r(NULL, ", \t", &ws)) { 5065 /* Find matching feature name */ 5066 uint_t f; 5067 for (f = 0; f < SPA_FEATURES; f++) { 5068 zfeature_info_t *fi = 5069 &spa_feature_table[f]; 5070 if (strcmp(word, fi->fi_uname) == 0) { 5071 l_features[f] = B_TRUE; 5072 break; 5073 } 5074 } 5075 if (f < SPA_FEATURES) 5076 continue; 5077 5078 /* found an unrecognized word */ 5079 /* lightly sanitize it */ 5080 if (strlen(word) > 32) 5081 word[32] = '\0'; 5082 for (char *c = word; *c != '\0'; c++) 5083 if (!isprint(*c)) 5084 *c = '?'; 5085 5086 strlcat(err_badtoken, word, ZFS_MAXPROPLEN); 5087 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN); 5088 if (source == Z_SYSCONF) 5089 ret_badtoken = B_TRUE; 5090 else 5091 ret_warntoken = B_TRUE; 5092 } 5093 } 5094 (void) munmap((void *) fc, fs.st_size); 5095 5096 if (features != NULL) 5097 for (uint_t i = 0; i < SPA_FEATURES; i++) 5098 features[i] &= l_features[i]; 5099 } 5100 (void) close(sdirfd); 5101 (void) close(ddirfd); 5102 5103 /* Return the most serious error */ 5104 if (ret_badfile) { 5105 if (report != NULL) 5106 snprintf(report, rlen, gettext("could not read/" 5107 "parse feature file(s): %s"), err_badfile); 5108 return (ZPOOL_COMPATIBILITY_BADFILE); 5109 } 5110 if (ret_nofiles) { 5111 if (report != NULL) 5112 strlcpy(report, 5113 gettext("no valid compatibility files specified"), 5114 rlen); 5115 return (ZPOOL_COMPATIBILITY_NOFILES); 5116 } 5117 if (ret_badtoken) { 5118 if (report != NULL) 5119 snprintf(report, rlen, gettext("invalid feature " 5120 "name(s) in local compatibility files: %s"), 5121 err_badtoken); 5122 return (ZPOOL_COMPATIBILITY_BADTOKEN); 5123 } 5124 if (ret_warntoken) { 5125 if (report != NULL) 5126 snprintf(report, rlen, gettext("unrecognized feature " 5127 "name(s) in distribution compatibility files: %s"), 5128 err_badtoken); 5129 return (ZPOOL_COMPATIBILITY_WARNTOKEN); 5130 } 5131 if (report != NULL) 5132 strlcpy(report, gettext("compatibility set ok"), rlen); 5133 return (ZPOOL_COMPATIBILITY_OK); 5134 } 5135 5136 static int 5137 zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid) 5138 { 5139 nvlist_t *tgt; 5140 boolean_t avail_spare, l2cache; 5141 5142 verify(zhp != NULL); 5143 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 5144 char errbuf[ERRBUFLEN]; 5145 (void) snprintf(errbuf, sizeof (errbuf), 5146 dgettext(TEXT_DOMAIN, "pool is in an unavailable state")); 5147 return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf)); 5148 } 5149 5150 if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache, 5151 NULL)) == NULL) { 5152 char errbuf[ERRBUFLEN]; 5153 (void) snprintf(errbuf, sizeof (errbuf), 5154 dgettext(TEXT_DOMAIN, "can not find %s in %s"), 5155 vdevname, zhp->zpool_name); 5156 return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf)); 5157 } 5158 5159 *vdev_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 5160 return (0); 5161 } 5162 5163 /* 5164 * Get a vdev property value for 'prop' and return the value in 5165 * a pre-allocated buffer. 5166 */ 5167 int 5168 zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name, 5169 char *buf, size_t len, zprop_source_t *srctype, boolean_t literal) 5170 { 5171 nvlist_t *nv; 5172 const char *strval; 5173 uint64_t intval; 5174 zprop_source_t src = ZPROP_SRC_NONE; 5175 5176 if (prop == VDEV_PROP_USERPROP) { 5177 /* user property, prop_name must contain the property name */ 5178 assert(prop_name != NULL); 5179 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) { 5180 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE); 5181 strval = fnvlist_lookup_string(nv, ZPROP_VALUE); 5182 } else { 5183 /* user prop not found */ 5184 return (-1); 5185 } 5186 (void) strlcpy(buf, strval, len); 5187 if (srctype) 5188 *srctype = src; 5189 return (0); 5190 } 5191 5192 if (prop_name == NULL) 5193 prop_name = (char *)vdev_prop_to_name(prop); 5194 5195 switch (vdev_prop_get_type(prop)) { 5196 case PROP_TYPE_STRING: 5197 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) { 5198 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE); 5199 strval = fnvlist_lookup_string(nv, ZPROP_VALUE); 5200 } else { 5201 src = ZPROP_SRC_DEFAULT; 5202 if ((strval = vdev_prop_default_string(prop)) == NULL) 5203 strval = "-"; 5204 } 5205 (void) strlcpy(buf, strval, len); 5206 break; 5207 5208 case PROP_TYPE_NUMBER: 5209 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) { 5210 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE); 5211 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE); 5212 } else { 5213 src = ZPROP_SRC_DEFAULT; 5214 intval = vdev_prop_default_numeric(prop); 5215 } 5216 5217 switch (prop) { 5218 case VDEV_PROP_ASIZE: 5219 case VDEV_PROP_PSIZE: 5220 case VDEV_PROP_SIZE: 5221 case VDEV_PROP_BOOTSIZE: 5222 case VDEV_PROP_ALLOCATED: 5223 case VDEV_PROP_FREE: 5224 case VDEV_PROP_READ_ERRORS: 5225 case VDEV_PROP_WRITE_ERRORS: 5226 case VDEV_PROP_CHECKSUM_ERRORS: 5227 case VDEV_PROP_INITIALIZE_ERRORS: 5228 case VDEV_PROP_OPS_NULL: 5229 case VDEV_PROP_OPS_READ: 5230 case VDEV_PROP_OPS_WRITE: 5231 case VDEV_PROP_OPS_FREE: 5232 case VDEV_PROP_OPS_CLAIM: 5233 case VDEV_PROP_OPS_TRIM: 5234 case VDEV_PROP_BYTES_NULL: 5235 case VDEV_PROP_BYTES_READ: 5236 case VDEV_PROP_BYTES_WRITE: 5237 case VDEV_PROP_BYTES_FREE: 5238 case VDEV_PROP_BYTES_CLAIM: 5239 case VDEV_PROP_BYTES_TRIM: 5240 if (literal) { 5241 (void) snprintf(buf, len, "%llu", 5242 (u_longlong_t)intval); 5243 } else { 5244 (void) zfs_nicenum(intval, buf, len); 5245 } 5246 break; 5247 case VDEV_PROP_EXPANDSZ: 5248 if (intval == 0) { 5249 (void) strlcpy(buf, "-", len); 5250 } else if (literal) { 5251 (void) snprintf(buf, len, "%llu", 5252 (u_longlong_t)intval); 5253 } else { 5254 (void) zfs_nicenum(intval, buf, len); 5255 } 5256 break; 5257 case VDEV_PROP_CAPACITY: 5258 if (literal) { 5259 (void) snprintf(buf, len, "%llu", 5260 (u_longlong_t)intval); 5261 } else { 5262 (void) snprintf(buf, len, "%llu%%", 5263 (u_longlong_t)intval); 5264 } 5265 break; 5266 case VDEV_PROP_CHECKSUM_N: 5267 case VDEV_PROP_CHECKSUM_T: 5268 case VDEV_PROP_IO_N: 5269 case VDEV_PROP_IO_T: 5270 case VDEV_PROP_SLOW_IO_N: 5271 case VDEV_PROP_SLOW_IO_T: 5272 if (intval == UINT64_MAX) { 5273 (void) strlcpy(buf, "-", len); 5274 } else { 5275 (void) snprintf(buf, len, "%llu", 5276 (u_longlong_t)intval); 5277 } 5278 break; 5279 case VDEV_PROP_FRAGMENTATION: 5280 if (intval == UINT64_MAX) { 5281 (void) strlcpy(buf, "-", len); 5282 } else { 5283 (void) snprintf(buf, len, "%llu%%", 5284 (u_longlong_t)intval); 5285 } 5286 break; 5287 case VDEV_PROP_STATE: 5288 if (literal) { 5289 (void) snprintf(buf, len, "%llu", 5290 (u_longlong_t)intval); 5291 } else { 5292 (void) strlcpy(buf, zpool_state_to_name(intval, 5293 VDEV_AUX_NONE), len); 5294 } 5295 break; 5296 default: 5297 (void) snprintf(buf, len, "%llu", 5298 (u_longlong_t)intval); 5299 } 5300 break; 5301 5302 case PROP_TYPE_INDEX: 5303 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) { 5304 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE); 5305 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE); 5306 } else { 5307 src = ZPROP_SRC_DEFAULT; 5308 intval = vdev_prop_default_numeric(prop); 5309 /* Only use if provided by the RAIDZ VDEV above */ 5310 if (prop == VDEV_PROP_RAIDZ_EXPANDING) 5311 return (ENOENT); 5312 } 5313 if (vdev_prop_index_to_string(prop, intval, 5314 (const char **)&strval) != 0) 5315 return (-1); 5316 (void) strlcpy(buf, strval, len); 5317 break; 5318 5319 default: 5320 abort(); 5321 } 5322 5323 if (srctype) 5324 *srctype = src; 5325 5326 return (0); 5327 } 5328 5329 /* 5330 * Get a vdev property value for 'prop_name' and return the value in 5331 * a pre-allocated buffer. 5332 */ 5333 int 5334 zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop, 5335 char *prop_name, char *buf, size_t len, zprop_source_t *srctype, 5336 boolean_t literal) 5337 { 5338 nvlist_t *reqnvl, *reqprops; 5339 nvlist_t *retprops = NULL; 5340 uint64_t vdev_guid = 0; 5341 int ret; 5342 5343 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0) 5344 return (ret); 5345 5346 if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0) 5347 return (no_memory(zhp->zpool_hdl)); 5348 if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0) 5349 return (no_memory(zhp->zpool_hdl)); 5350 5351 fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid); 5352 5353 if (prop != VDEV_PROP_USERPROP) { 5354 /* prop_name overrides prop value */ 5355 if (prop_name != NULL) 5356 prop = vdev_name_to_prop(prop_name); 5357 else 5358 prop_name = (char *)vdev_prop_to_name(prop); 5359 assert(prop < VDEV_NUM_PROPS); 5360 } 5361 5362 assert(prop_name != NULL); 5363 if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) { 5364 nvlist_free(reqnvl); 5365 nvlist_free(reqprops); 5366 return (no_memory(zhp->zpool_hdl)); 5367 } 5368 5369 fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops); 5370 5371 ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops); 5372 5373 if (ret == 0) { 5374 ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf, 5375 len, srctype, literal); 5376 } else { 5377 char errbuf[ERRBUFLEN]; 5378 (void) snprintf(errbuf, sizeof (errbuf), 5379 dgettext(TEXT_DOMAIN, "cannot get vdev property %s from" 5380 " %s in %s"), prop_name, vdevname, zhp->zpool_name); 5381 (void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf); 5382 } 5383 5384 nvlist_free(reqnvl); 5385 nvlist_free(reqprops); 5386 nvlist_free(retprops); 5387 5388 return (ret); 5389 } 5390 5391 /* 5392 * Get all vdev properties 5393 */ 5394 int 5395 zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname, 5396 nvlist_t **outnvl) 5397 { 5398 nvlist_t *nvl = NULL; 5399 uint64_t vdev_guid = 0; 5400 int ret; 5401 5402 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0) 5403 return (ret); 5404 5405 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5406 return (no_memory(zhp->zpool_hdl)); 5407 5408 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid); 5409 5410 ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl); 5411 5412 nvlist_free(nvl); 5413 5414 if (ret) { 5415 char errbuf[ERRBUFLEN]; 5416 (void) snprintf(errbuf, sizeof (errbuf), 5417 dgettext(TEXT_DOMAIN, "cannot get vdev properties for" 5418 " %s in %s"), vdevname, zhp->zpool_name); 5419 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5420 } 5421 5422 return (ret); 5423 } 5424 5425 /* 5426 * Set vdev property 5427 */ 5428 int 5429 zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname, 5430 const char *propname, const char *propval) 5431 { 5432 int ret; 5433 nvlist_t *nvl = NULL; 5434 nvlist_t *outnvl = NULL; 5435 nvlist_t *props; 5436 nvlist_t *realprops; 5437 prop_flags_t flags = { 0 }; 5438 uint64_t version; 5439 uint64_t vdev_guid; 5440 5441 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0) 5442 return (ret); 5443 5444 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5445 return (no_memory(zhp->zpool_hdl)); 5446 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 5447 return (no_memory(zhp->zpool_hdl)); 5448 5449 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid); 5450 5451 if (nvlist_add_string(props, propname, propval) != 0) { 5452 nvlist_free(props); 5453 return (no_memory(zhp->zpool_hdl)); 5454 } 5455 5456 char errbuf[ERRBUFLEN]; 5457 (void) snprintf(errbuf, sizeof (errbuf), 5458 dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"), 5459 propname, vdevname, zhp->zpool_name); 5460 5461 flags.vdevprop = 1; 5462 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5463 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5464 zhp->zpool_name, props, version, flags, errbuf)) == NULL) { 5465 nvlist_free(props); 5466 nvlist_free(nvl); 5467 return (-1); 5468 } 5469 5470 nvlist_free(props); 5471 props = realprops; 5472 5473 fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props); 5474 5475 ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl); 5476 5477 nvlist_free(props); 5478 nvlist_free(nvl); 5479 nvlist_free(outnvl); 5480 5481 if (ret) 5482 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5483 5484 return (ret); 5485 } 5486