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