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