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