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