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