1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22/* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2012 by Delphix. All rights reserved. 26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved. 27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved. 29 */ 30 31#include <solaris.h> 32#include <assert.h> 33#include <ctype.h> 34#include <dirent.h> 35#include <errno.h> 36#include <fcntl.h> 37#include <libgen.h> 38#include <libintl.h> 39#include <libuutil.h> 40#include <locale.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <strings.h> 45#include <unistd.h> 46#include <priv.h> 47#include <pwd.h> 48#include <zone.h> 49#include <sys/time.h> 50#include <zfs_prop.h> 51#include <sys/fs/zfs.h> 52#include <sys/stat.h> 53 54#include <libzfs.h> 55 56#include "zpool_util.h" 57#include "zfs_comutil.h" 58#include "zfeature_common.h" 59 60#include "statcommon.h" 61 62static int zpool_do_create(int, char **); 63static int zpool_do_destroy(int, char **); 64 65static int zpool_do_add(int, char **); 66static int zpool_do_remove(int, char **); 67static int zpool_do_labelclear(int, char **); 68 69static int zpool_do_list(int, char **); 70static int zpool_do_iostat(int, char **); 71static int zpool_do_status(int, char **); 72 73static int zpool_do_online(int, char **); 74static int zpool_do_offline(int, char **); 75static int zpool_do_clear(int, char **); 76static int zpool_do_reopen(int, char **); 77 78static int zpool_do_reguid(int, char **); 79 80static int zpool_do_attach(int, char **); 81static int zpool_do_detach(int, char **); 82static int zpool_do_replace(int, char **); 83static int zpool_do_split(int, char **); 84 85static int zpool_do_scrub(int, char **); 86 87static int zpool_do_import(int, char **); 88static int zpool_do_export(int, char **); 89 90static int zpool_do_upgrade(int, char **); 91 92static int zpool_do_history(int, char **); 93 94static int zpool_do_get(int, char **); 95static int zpool_do_set(int, char **); 96 97/* 98 * These libumem hooks provide a reasonable set of defaults for the allocator's 99 * debugging facilities. 100 */ 101 102#ifdef DEBUG 103const char * 104_umem_debug_init(void) 105{ 106 return ("default,verbose"); /* $UMEM_DEBUG setting */ 107} 108 109const char * 110_umem_logging_init(void) 111{ 112 return ("fail,contents"); /* $UMEM_LOGGING setting */ 113} 114#endif 115 116typedef enum { 117 HELP_ADD, 118 HELP_ATTACH, 119 HELP_CLEAR, 120 HELP_CREATE, 121 HELP_DESTROY, 122 HELP_DETACH, 123 HELP_EXPORT, 124 HELP_HISTORY, 125 HELP_IMPORT, 126 HELP_IOSTAT, 127 HELP_LABELCLEAR, 128 HELP_LIST, 129 HELP_OFFLINE, 130 HELP_ONLINE, 131 HELP_REPLACE, 132 HELP_REMOVE, 133 HELP_SCRUB, 134 HELP_STATUS, 135 HELP_UPGRADE, 136 HELP_GET, 137 HELP_SET, 138 HELP_SPLIT, 139 HELP_REGUID, 140 HELP_REOPEN 141} zpool_help_t; 142 143 144typedef struct zpool_command { 145 const char *name; 146 int (*func)(int, char **); 147 zpool_help_t usage; 148} zpool_command_t; 149 150/* 151 * Master command table. Each ZFS command has a name, associated function, and 152 * usage message. The usage messages need to be internationalized, so we have 153 * to have a function to return the usage message based on a command index. 154 * 155 * These commands are organized according to how they are displayed in the usage 156 * message. An empty command (one with a NULL name) indicates an empty line in 157 * the generic usage message. 158 */ 159static zpool_command_t command_table[] = { 160 { "create", zpool_do_create, HELP_CREATE }, 161 { "destroy", zpool_do_destroy, HELP_DESTROY }, 162 { NULL }, 163 { "add", zpool_do_add, HELP_ADD }, 164 { "remove", zpool_do_remove, HELP_REMOVE }, 165 { NULL }, 166 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, 167 { NULL }, 168 { "list", zpool_do_list, HELP_LIST }, 169 { "iostat", zpool_do_iostat, HELP_IOSTAT }, 170 { "status", zpool_do_status, HELP_STATUS }, 171 { NULL }, 172 { "online", zpool_do_online, HELP_ONLINE }, 173 { "offline", zpool_do_offline, HELP_OFFLINE }, 174 { "clear", zpool_do_clear, HELP_CLEAR }, 175 { "reopen", zpool_do_reopen, HELP_REOPEN }, 176 { NULL }, 177 { "attach", zpool_do_attach, HELP_ATTACH }, 178 { "detach", zpool_do_detach, HELP_DETACH }, 179 { "replace", zpool_do_replace, HELP_REPLACE }, 180 { "split", zpool_do_split, HELP_SPLIT }, 181 { NULL }, 182 { "scrub", zpool_do_scrub, HELP_SCRUB }, 183 { NULL }, 184 { "import", zpool_do_import, HELP_IMPORT }, 185 { "export", zpool_do_export, HELP_EXPORT }, 186 { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 187 { "reguid", zpool_do_reguid, HELP_REGUID }, 188 { NULL }, 189 { "history", zpool_do_history, HELP_HISTORY }, 190 { "get", zpool_do_get, HELP_GET }, 191 { "set", zpool_do_set, HELP_SET }, 192}; 193 194#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 195 196static zpool_command_t *current_command; 197static char history_str[HIS_MAX_RECORD_LEN]; 198static boolean_t log_history = B_TRUE; 199static uint_t timestamp_fmt = NODATE; 200 201static const char * 202get_usage(zpool_help_t idx) { 203 switch (idx) { 204 case HELP_ADD: 205 return (gettext("\tadd [-fn] <pool> <vdev> ...\n")); 206 case HELP_ATTACH: 207 return (gettext("\tattach [-f] <pool> <device> " 208 "<new-device>\n")); 209 case HELP_CLEAR: 210 return (gettext("\tclear [-nF] <pool> [device]\n")); 211 case HELP_CREATE: 212 return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 213 "\t [-O file-system-property=value] ... \n" 214 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 215 case HELP_DESTROY: 216 return (gettext("\tdestroy [-f] <pool>\n")); 217 case HELP_DETACH: 218 return (gettext("\tdetach <pool> <device>\n")); 219 case HELP_EXPORT: 220 return (gettext("\texport [-f] <pool> ...\n")); 221 case HELP_HISTORY: 222 return (gettext("\thistory [-il] [<pool>] ...\n")); 223 case HELP_IMPORT: 224 return (gettext("\timport [-d dir] [-D]\n" 225 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n" 226 "\timport [-o mntopts] [-o property=value] ... \n" 227 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 228 "[-R root] [-F [-n]] -a\n" 229 "\timport [-o mntopts] [-o property=value] ... \n" 230 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 231 "[-R root] [-F [-n]]\n" 232 "\t <pool | id> [newpool]\n")); 233 case HELP_IOSTAT: 234 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval " 235 "[count]]\n")); 236 case HELP_LABELCLEAR: 237 return (gettext("\tlabelclear [-f] <vdev>\n")); 238 case HELP_LIST: 239 return (gettext("\tlist [-Hv] [-o property[,...]] " 240 "[-T d|u] [pool] ... [interval [count]]\n")); 241 case HELP_OFFLINE: 242 return (gettext("\toffline [-t] <pool> <device> ...\n")); 243 case HELP_ONLINE: 244 return (gettext("\tonline [-e] <pool> <device> ...\n")); 245 case HELP_REPLACE: 246 return (gettext("\treplace [-f] <pool> <device> " 247 "[new-device]\n")); 248 case HELP_REMOVE: 249 return (gettext("\tremove <pool> <device> ...\n")); 250 case HELP_REOPEN: 251 return (""); /* Undocumented command */ 252 case HELP_SCRUB: 253 return (gettext("\tscrub [-s] <pool> ...\n")); 254 case HELP_STATUS: 255 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval " 256 "[count]]\n")); 257 case HELP_UPGRADE: 258 return (gettext("\tupgrade [-v]\n" 259 "\tupgrade [-V version] <-a | pool ...>\n")); 260 case HELP_GET: 261 return (gettext("\tget <\"all\" | property[,...]> " 262 "<pool> ...\n")); 263 case HELP_SET: 264 return (gettext("\tset <property=value> <pool> \n")); 265 case HELP_SPLIT: 266 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n" 267 "\t [-o property=value] <pool> <newpool> " 268 "[<device> ...]\n")); 269 case HELP_REGUID: 270 return (gettext("\treguid <pool>\n")); 271 } 272 273 abort(); 274 /* NOTREACHED */ 275} 276 277 278/* 279 * Callback routine that will print out a pool property value. 280 */ 281static int 282print_prop_cb(int prop, void *cb) 283{ 284 FILE *fp = cb; 285 286 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop)); 287 288 if (zpool_prop_readonly(prop)) 289 (void) fprintf(fp, " NO "); 290 else 291 (void) fprintf(fp, " YES "); 292 293 if (zpool_prop_values(prop) == NULL) 294 (void) fprintf(fp, "-\n"); 295 else 296 (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 297 298 return (ZPROP_CONT); 299} 300 301/* 302 * Display usage message. If we're inside a command, display only the usage for 303 * that command. Otherwise, iterate over the entire command table and display 304 * a complete usage message. 305 */ 306void 307usage(boolean_t requested) 308{ 309 FILE *fp = requested ? stdout : stderr; 310 311 if (current_command == NULL) { 312 int i; 313 314 (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 315 (void) fprintf(fp, 316 gettext("where 'command' is one of the following:\n\n")); 317 318 for (i = 0; i < NCOMMAND; i++) { 319 if (command_table[i].name == NULL) 320 (void) fprintf(fp, "\n"); 321 else 322 (void) fprintf(fp, "%s", 323 get_usage(command_table[i].usage)); 324 } 325 } else { 326 (void) fprintf(fp, gettext("usage:\n")); 327 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 328 } 329 330 if (current_command != NULL && 331 ((strcmp(current_command->name, "set") == 0) || 332 (strcmp(current_command->name, "get") == 0) || 333 (strcmp(current_command->name, "list") == 0))) { 334 335 (void) fprintf(fp, 336 gettext("\nthe following properties are supported:\n")); 337 338 (void) fprintf(fp, "\n\t%-15s %s %s\n\n", 339 "PROPERTY", "EDIT", "VALUES"); 340 341 /* Iterate over all properties */ 342 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, 343 ZFS_TYPE_POOL); 344 345 (void) fprintf(fp, "\t%-15s ", "feature@..."); 346 (void) fprintf(fp, "YES disabled | enabled | active\n"); 347 348 (void) fprintf(fp, gettext("\nThe feature@ properties must be " 349 "appended with a feature name.\nSee zpool-features(7).\n")); 350 } 351 352 /* 353 * See comments at end of main(). 354 */ 355 if (getenv("ZFS_ABORT") != NULL) { 356 (void) printf("dumping core by request\n"); 357 abort(); 358 } 359 360 exit(requested ? 0 : 2); 361} 362 363void 364print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 365 boolean_t print_logs) 366{ 367 nvlist_t **child; 368 uint_t c, children; 369 char *vname; 370 371 if (name != NULL) 372 (void) printf("\t%*s%s\n", indent, "", name); 373 374 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 375 &child, &children) != 0) 376 return; 377 378 for (c = 0; c < children; c++) { 379 uint64_t is_log = B_FALSE; 380 381 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 382 &is_log); 383 if ((is_log && !print_logs) || (!is_log && print_logs)) 384 continue; 385 386 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 387 print_vdev_tree(zhp, vname, child[c], indent + 2, 388 B_FALSE); 389 free(vname); 390 } 391} 392 393static boolean_t 394prop_list_contains_feature(nvlist_t *proplist) 395{ 396 nvpair_t *nvp; 397 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 398 nvp = nvlist_next_nvpair(proplist, nvp)) { 399 if (zpool_prop_feature(nvpair_name(nvp))) 400 return (B_TRUE); 401 } 402 return (B_FALSE); 403} 404 405/* 406 * Add a property pair (name, string-value) into a property nvlist. 407 */ 408static int 409add_prop_list(const char *propname, char *propval, nvlist_t **props, 410 boolean_t poolprop) 411{ 412 zpool_prop_t prop = ZPROP_INVAL; 413 zfs_prop_t fprop; 414 nvlist_t *proplist; 415 const char *normnm; 416 char *strval; 417 418 if (*props == NULL && 419 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 420 (void) fprintf(stderr, 421 gettext("internal error: out of memory\n")); 422 return (1); 423 } 424 425 proplist = *props; 426 427 if (poolprop) { 428 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 429 430 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL && 431 !zpool_prop_feature(propname)) { 432 (void) fprintf(stderr, gettext("property '%s' is " 433 "not a valid pool property\n"), propname); 434 return (2); 435 } 436 437 /* 438 * feature@ properties and version should not be specified 439 * at the same time. 440 */ 441 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) && 442 nvlist_exists(proplist, vname)) || 443 (prop == ZPOOL_PROP_VERSION && 444 prop_list_contains_feature(proplist))) { 445 (void) fprintf(stderr, gettext("'feature@' and " 446 "'version' properties cannot be specified " 447 "together\n")); 448 return (2); 449 } 450 451 452 if (zpool_prop_feature(propname)) 453 normnm = propname; 454 else 455 normnm = zpool_prop_to_name(prop); 456 } else { 457 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 458 normnm = zfs_prop_to_name(fprop); 459 } else { 460 normnm = propname; 461 } 462 } 463 464 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 465 prop != ZPOOL_PROP_CACHEFILE) { 466 (void) fprintf(stderr, gettext("property '%s' " 467 "specified multiple times\n"), propname); 468 return (2); 469 } 470 471 if (nvlist_add_string(proplist, normnm, propval) != 0) { 472 (void) fprintf(stderr, gettext("internal " 473 "error: out of memory\n")); 474 return (1); 475 } 476 477 return (0); 478} 479 480/* 481 * zpool add [-fn] <pool> <vdev> ... 482 * 483 * -f Force addition of devices, even if they appear in use 484 * -n Do not add the devices, but display the resulting layout if 485 * they were to be added. 486 * 487 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 488 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to 489 * libzfs. 490 */ 491int 492zpool_do_add(int argc, char **argv) 493{ 494 boolean_t force = B_FALSE; 495 boolean_t dryrun = B_FALSE; 496 int c; 497 nvlist_t *nvroot; 498 char *poolname; 499 int ret; 500 zpool_handle_t *zhp; 501 nvlist_t *config; 502 503 /* check options */ 504 while ((c = getopt(argc, argv, "fn")) != -1) { 505 switch (c) { 506 case 'f': 507 force = B_TRUE; 508 break; 509 case 'n': 510 dryrun = B_TRUE; 511 break; 512 case '?': 513 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 514 optopt); 515 usage(B_FALSE); 516 } 517 } 518 519 argc -= optind; 520 argv += optind; 521 522 /* get pool name and check number of arguments */ 523 if (argc < 1) { 524 (void) fprintf(stderr, gettext("missing pool name argument\n")); 525 usage(B_FALSE); 526 } 527 if (argc < 2) { 528 (void) fprintf(stderr, gettext("missing vdev specification\n")); 529 usage(B_FALSE); 530 } 531 532 poolname = argv[0]; 533 534 argc--; 535 argv++; 536 537 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 538 return (1); 539 540 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 541 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 542 poolname); 543 zpool_close(zhp); 544 return (1); 545 } 546 547 /* pass off to get_vdev_spec for processing */ 548 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun, 549 argc, argv); 550 if (nvroot == NULL) { 551 zpool_close(zhp); 552 return (1); 553 } 554 555 if (dryrun) { 556 nvlist_t *poolnvroot; 557 558 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 559 &poolnvroot) == 0); 560 561 (void) printf(gettext("would update '%s' to the following " 562 "configuration:\n"), zpool_get_name(zhp)); 563 564 /* print original main pool and new tree */ 565 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE); 566 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE); 567 568 /* Do the same for the logs */ 569 if (num_logs(poolnvroot) > 0) { 570 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE); 571 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE); 572 } else if (num_logs(nvroot) > 0) { 573 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE); 574 } 575 576 ret = 0; 577 } else { 578 ret = (zpool_add(zhp, nvroot) != 0); 579 } 580 581 nvlist_free(nvroot); 582 zpool_close(zhp); 583 584 return (ret); 585} 586 587/* 588 * zpool remove <pool> <vdev> ... 589 * 590 * Removes the given vdev from the pool. Currently, this supports removing 591 * spares, cache, and log devices from the pool. 592 */ 593int 594zpool_do_remove(int argc, char **argv) 595{ 596 char *poolname; 597 int i, ret = 0; 598 zpool_handle_t *zhp; 599 600 argc--; 601 argv++; 602 603 /* get pool name and check number of arguments */ 604 if (argc < 1) { 605 (void) fprintf(stderr, gettext("missing pool name argument\n")); 606 usage(B_FALSE); 607 } 608 if (argc < 2) { 609 (void) fprintf(stderr, gettext("missing device\n")); 610 usage(B_FALSE); 611 } 612 613 poolname = argv[0]; 614 615 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 616 return (1); 617 618 for (i = 1; i < argc; i++) { 619 if (zpool_vdev_remove(zhp, argv[i]) != 0) 620 ret = 1; 621 } 622 623 return (ret); 624} 625 626/* 627 * zpool labelclear <vdev> 628 * 629 * Verifies that the vdev is not active and zeros out the label information 630 * on the device. 631 */ 632int 633zpool_do_labelclear(int argc, char **argv) 634{ 635 char *vdev, *name; 636 int c, fd = -1, ret = 0; 637 pool_state_t state; 638 boolean_t inuse = B_FALSE; 639 boolean_t force = B_FALSE; 640 641 /* check options */ 642 while ((c = getopt(argc, argv, "f")) != -1) { 643 switch (c) { 644 case 'f': 645 force = B_TRUE; 646 break; 647 default: 648 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 649 optopt); 650 usage(B_FALSE); 651 } 652 } 653 654 argc -= optind; 655 argv += optind; 656 657 /* get vdev name */ 658 if (argc < 1) { 659 (void) fprintf(stderr, gettext("missing vdev device name\n")); 660 usage(B_FALSE); 661 } 662 663 vdev = argv[0]; 664 if ((fd = open(vdev, O_RDWR)) < 0) { 665 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev); 666 return (B_FALSE); 667 } 668 669 name = NULL; 670 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) { 671 if (force) 672 goto wipe_label; 673 674 (void) fprintf(stderr, 675 gettext("Unable to determine pool state for %s\n" 676 "Use -f to force the clearing any label data\n"), vdev); 677 678 return (1); 679 } 680 681 if (inuse) { 682 switch (state) { 683 default: 684 case POOL_STATE_ACTIVE: 685 case POOL_STATE_SPARE: 686 case POOL_STATE_L2CACHE: 687 (void) fprintf(stderr, 688gettext("labelclear operation failed.\n" 689 "\tVdev %s is a member (%s), of pool \"%s\".\n" 690 "\tTo remove label information from this device, export or destroy\n" 691 "\tthe pool, or remove %s from the configuration of this pool\n" 692 "\tand retry the labelclear operation\n"), 693 vdev, zpool_pool_state_to_name(state), name, vdev); 694 ret = 1; 695 goto errout; 696 697 case POOL_STATE_EXPORTED: 698 if (force) 699 break; 700 701 (void) fprintf(stderr, 702gettext("labelclear operation failed.\n" 703 "\tVdev %s is a member of the exported pool \"%s\".\n" 704 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n" 705 "\tinformation.\n"), 706 vdev, name, vdev); 707 ret = 1; 708 goto errout; 709 710 case POOL_STATE_POTENTIALLY_ACTIVE: 711 if (force) 712 break; 713 714 (void) fprintf(stderr, 715gettext("labelclear operation failed.\n" 716 "\tVdev %s is a member of the pool \"%s\".\n" 717 "\tThis pool is unknown to this system, but may be active on\n" 718 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n" 719 "\tremoval of label information.\n"), 720 vdev, name, vdev); 721 ret = 1; 722 goto errout; 723 724 case POOL_STATE_DESTROYED: 725 /* inuse should never be set for a destoryed pool... */ 726 break; 727 } 728 } 729 730wipe_label: 731 if (zpool_clear_label(fd) != 0) { 732 (void) fprintf(stderr, 733 gettext("Label clear failed on vdev %s\n"), vdev); 734 ret = 1; 735 } 736 737errout: 738 close(fd); 739 if (name != NULL) 740 free(name); 741 742 return (ret); 743} 744 745/* 746 * zpool create [-fnd] [-o property=value] ... 747 * [-O file-system-property=value] ... 748 * [-R root] [-m mountpoint] <pool> <dev> ... 749 * 750 * -f Force creation, even if devices appear in use 751 * -n Do not create the pool, but display the resulting layout if it 752 * were to be created. 753 * -R Create a pool under an alternate root 754 * -m Set default mountpoint for the root dataset. By default it's 755 * '/<pool>' 756 * -o Set property=value. 757 * -d Don't automatically enable all supported pool features 758 * (individual features can be enabled with -o). 759 * -O Set fsproperty=value in the pool's root file system 760 * 761 * Creates the named pool according to the given vdev specification. The 762 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once 763 * we get the nvlist back from get_vdev_spec(), we either print out the contents 764 * (if '-n' was specified), or pass it to libzfs to do the creation. 765 */ 766int 767zpool_do_create(int argc, char **argv) 768{ 769 boolean_t force = B_FALSE; 770 boolean_t dryrun = B_FALSE; 771 boolean_t enable_all_pool_feat = B_TRUE; 772 int c; 773 nvlist_t *nvroot = NULL; 774 char *poolname; 775 int ret = 1; 776 char *altroot = NULL; 777 char *mountpoint = NULL; 778 nvlist_t *fsprops = NULL; 779 nvlist_t *props = NULL; 780 char *propval; 781 782 /* check options */ 783 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) { 784 switch (c) { 785 case 'f': 786 force = B_TRUE; 787 break; 788 case 'n': 789 dryrun = B_TRUE; 790 break; 791 case 'd': 792 enable_all_pool_feat = B_FALSE; 793 break; 794 case 'R': 795 altroot = optarg; 796 if (add_prop_list(zpool_prop_to_name( 797 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 798 goto errout; 799 if (nvlist_lookup_string(props, 800 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 801 &propval) == 0) 802 break; 803 if (add_prop_list(zpool_prop_to_name( 804 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 805 goto errout; 806 break; 807 case 'm': 808 /* Equivalent to -O mountpoint=optarg */ 809 mountpoint = optarg; 810 break; 811 case 'o': 812 if ((propval = strchr(optarg, '=')) == NULL) { 813 (void) fprintf(stderr, gettext("missing " 814 "'=' for -o option\n")); 815 goto errout; 816 } 817 *propval = '\0'; 818 propval++; 819 820 if (add_prop_list(optarg, propval, &props, B_TRUE)) 821 goto errout; 822 823 /* 824 * If the user is creating a pool that doesn't support 825 * feature flags, don't enable any features. 826 */ 827 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 828 char *end; 829 u_longlong_t ver; 830 831 ver = strtoull(propval, &end, 10); 832 if (*end == '\0' && 833 ver < SPA_VERSION_FEATURES) { 834 enable_all_pool_feat = B_FALSE; 835 } 836 } 837 break; 838 case 'O': 839 if ((propval = strchr(optarg, '=')) == NULL) { 840 (void) fprintf(stderr, gettext("missing " 841 "'=' for -O option\n")); 842 goto errout; 843 } 844 *propval = '\0'; 845 propval++; 846 847 /* 848 * Mountpoints are checked and then added later. 849 * Uniquely among properties, they can be specified 850 * more than once, to avoid conflict with -m. 851 */ 852 if (0 == strcmp(optarg, 853 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { 854 mountpoint = propval; 855 } else if (add_prop_list(optarg, propval, &fsprops, 856 B_FALSE)) { 857 goto errout; 858 } 859 break; 860 case ':': 861 (void) fprintf(stderr, gettext("missing argument for " 862 "'%c' option\n"), optopt); 863 goto badusage; 864 case '?': 865 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 866 optopt); 867 goto badusage; 868 } 869 } 870 871 argc -= optind; 872 argv += optind; 873 874 /* get pool name and check number of arguments */ 875 if (argc < 1) { 876 (void) fprintf(stderr, gettext("missing pool name argument\n")); 877 goto badusage; 878 } 879 if (argc < 2) { 880 (void) fprintf(stderr, gettext("missing vdev specification\n")); 881 goto badusage; 882 } 883 884 poolname = argv[0]; 885 886 /* 887 * As a special case, check for use of '/' in the name, and direct the 888 * user to use 'zfs create' instead. 889 */ 890 if (strchr(poolname, '/') != NULL) { 891 (void) fprintf(stderr, gettext("cannot create '%s': invalid " 892 "character '/' in pool name\n"), poolname); 893 (void) fprintf(stderr, gettext("use 'zfs create' to " 894 "create a dataset\n")); 895 goto errout; 896 } 897 898 /* pass off to get_vdev_spec for bulk processing */ 899 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun, 900 argc - 1, argv + 1); 901 if (nvroot == NULL) 902 goto errout; 903 904 /* make_root_vdev() allows 0 toplevel children if there are spares */ 905 if (!zfs_allocatable_devs(nvroot)) { 906 (void) fprintf(stderr, gettext("invalid vdev " 907 "specification: at least one toplevel vdev must be " 908 "specified\n")); 909 goto errout; 910 } 911 912 if (altroot != NULL && altroot[0] != '/') { 913 (void) fprintf(stderr, gettext("invalid alternate root '%s': " 914 "must be an absolute path\n"), altroot); 915 goto errout; 916 } 917 918 /* 919 * Check the validity of the mountpoint and direct the user to use the 920 * '-m' mountpoint option if it looks like its in use. 921 * Ignore the checks if the '-f' option is given. 922 */ 923 if (!force && (mountpoint == NULL || 924 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 925 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) { 926 char buf[MAXPATHLEN]; 927 DIR *dirp; 928 929 if (mountpoint && mountpoint[0] != '/') { 930 (void) fprintf(stderr, gettext("invalid mountpoint " 931 "'%s': must be an absolute path, 'legacy', or " 932 "'none'\n"), mountpoint); 933 goto errout; 934 } 935 936 if (mountpoint == NULL) { 937 if (altroot != NULL) 938 (void) snprintf(buf, sizeof (buf), "%s/%s", 939 altroot, poolname); 940 else 941 (void) snprintf(buf, sizeof (buf), "/%s", 942 poolname); 943 } else { 944 if (altroot != NULL) 945 (void) snprintf(buf, sizeof (buf), "%s%s", 946 altroot, mountpoint); 947 else 948 (void) snprintf(buf, sizeof (buf), "%s", 949 mountpoint); 950 } 951 952 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 953 (void) fprintf(stderr, gettext("mountpoint '%s' : " 954 "%s\n"), buf, strerror(errno)); 955 (void) fprintf(stderr, gettext("use '-m' " 956 "option to provide a different default\n")); 957 goto errout; 958 } else if (dirp) { 959 int count = 0; 960 961 while (count < 3 && readdir(dirp) != NULL) 962 count++; 963 (void) closedir(dirp); 964 965 if (count > 2) { 966 (void) fprintf(stderr, gettext("mountpoint " 967 "'%s' exists and is not empty\n"), buf); 968 (void) fprintf(stderr, gettext("use '-m' " 969 "option to provide a " 970 "different default\n")); 971 goto errout; 972 } 973 } 974 } 975 976 /* 977 * Now that the mountpoint's validity has been checked, ensure that 978 * the property is set appropriately prior to creating the pool. 979 */ 980 if (mountpoint != NULL) { 981 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 982 mountpoint, &fsprops, B_FALSE); 983 if (ret != 0) 984 goto errout; 985 } 986 987 ret = 1; 988 if (dryrun) { 989 /* 990 * For a dry run invocation, print out a basic message and run 991 * through all the vdevs in the list and print out in an 992 * appropriate hierarchy. 993 */ 994 (void) printf(gettext("would create '%s' with the " 995 "following layout:\n\n"), poolname); 996 997 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE); 998 if (num_logs(nvroot) > 0) 999 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE); 1000 1001 ret = 0; 1002 } else { 1003 /* 1004 * Hand off to libzfs. 1005 */ 1006 if (enable_all_pool_feat) { 1007 int i; 1008 for (i = 0; i < SPA_FEATURES; i++) { 1009 char propname[MAXPATHLEN]; 1010 zfeature_info_t *feat = &spa_feature_table[i]; 1011 1012 (void) snprintf(propname, sizeof (propname), 1013 "feature@%s", feat->fi_uname); 1014 1015 /* 1016 * Skip feature if user specified it manually 1017 * on the command line. 1018 */ 1019 if (nvlist_exists(props, propname)) 1020 continue; 1021 1022 ret = add_prop_list(propname, 1023 ZFS_FEATURE_ENABLED, &props, B_TRUE); 1024 if (ret != 0) 1025 goto errout; 1026 } 1027 } 1028 1029 ret = 1; 1030 if (zpool_create(g_zfs, poolname, 1031 nvroot, props, fsprops) == 0) { 1032 zfs_handle_t *pool = zfs_open(g_zfs, poolname, 1033 ZFS_TYPE_FILESYSTEM); 1034 if (pool != NULL) { 1035 if (zfs_mount(pool, NULL, 0) == 0) 1036 ret = zfs_shareall(pool); 1037 zfs_close(pool); 1038 } 1039 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 1040 (void) fprintf(stderr, gettext("pool name may have " 1041 "been omitted\n")); 1042 } 1043 } 1044 1045errout: 1046 nvlist_free(nvroot); 1047 nvlist_free(fsprops); 1048 nvlist_free(props); 1049 return (ret); 1050badusage: 1051 nvlist_free(fsprops); 1052 nvlist_free(props); 1053 usage(B_FALSE); 1054 return (2); 1055} 1056 1057/* 1058 * zpool destroy <pool> 1059 * 1060 * -f Forcefully unmount any datasets 1061 * 1062 * Destroy the given pool. Automatically unmounts any datasets in the pool. 1063 */ 1064int 1065zpool_do_destroy(int argc, char **argv) 1066{ 1067 boolean_t force = B_FALSE; 1068 int c; 1069 char *pool; 1070 zpool_handle_t *zhp; 1071 int ret; 1072 1073 /* check options */ 1074 while ((c = getopt(argc, argv, "f")) != -1) { 1075 switch (c) { 1076 case 'f': 1077 force = B_TRUE; 1078 break; 1079 case '?': 1080 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1081 optopt); 1082 usage(B_FALSE); 1083 } 1084 } 1085 1086 argc -= optind; 1087 argv += optind; 1088 1089 /* check arguments */ 1090 if (argc < 1) { 1091 (void) fprintf(stderr, gettext("missing pool argument\n")); 1092 usage(B_FALSE); 1093 } 1094 if (argc > 1) { 1095 (void) fprintf(stderr, gettext("too many arguments\n")); 1096 usage(B_FALSE); 1097 } 1098 1099 pool = argv[0]; 1100 1101 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 1102 /* 1103 * As a special case, check for use of '/' in the name, and 1104 * direct the user to use 'zfs destroy' instead. 1105 */ 1106 if (strchr(pool, '/') != NULL) 1107 (void) fprintf(stderr, gettext("use 'zfs destroy' to " 1108 "destroy a dataset\n")); 1109 return (1); 1110 } 1111 1112 if (zpool_disable_datasets(zhp, force) != 0) { 1113 (void) fprintf(stderr, gettext("could not destroy '%s': " 1114 "could not unmount datasets\n"), zpool_get_name(zhp)); 1115 return (1); 1116 } 1117 1118 /* The history must be logged as part of the export */ 1119 log_history = B_FALSE; 1120 1121 ret = (zpool_destroy(zhp, history_str) != 0); 1122 1123 zpool_close(zhp); 1124 1125 return (ret); 1126} 1127 1128/* 1129 * zpool export [-f] <pool> ... 1130 * 1131 * -f Forcefully unmount datasets 1132 * 1133 * Export the given pools. By default, the command will attempt to cleanly 1134 * unmount any active datasets within the pool. If the '-f' flag is specified, 1135 * then the datasets will be forcefully unmounted. 1136 */ 1137int 1138zpool_do_export(int argc, char **argv) 1139{ 1140 boolean_t force = B_FALSE; 1141 boolean_t hardforce = B_FALSE; 1142 int c; 1143 zpool_handle_t *zhp; 1144 int ret; 1145 int i; 1146 1147 /* check options */ 1148 while ((c = getopt(argc, argv, "fF")) != -1) { 1149 switch (c) { 1150 case 'f': 1151 force = B_TRUE; 1152 break; 1153 case 'F': 1154 hardforce = B_TRUE; 1155 break; 1156 case '?': 1157 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1158 optopt); 1159 usage(B_FALSE); 1160 } 1161 } 1162 1163 argc -= optind; 1164 argv += optind; 1165 1166 /* check arguments */ 1167 if (argc < 1) { 1168 (void) fprintf(stderr, gettext("missing pool argument\n")); 1169 usage(B_FALSE); 1170 } 1171 1172 ret = 0; 1173 for (i = 0; i < argc; i++) { 1174 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) { 1175 ret = 1; 1176 continue; 1177 } 1178 1179 if (zpool_disable_datasets(zhp, force) != 0) { 1180 ret = 1; 1181 zpool_close(zhp); 1182 continue; 1183 } 1184 1185 /* The history must be logged as part of the export */ 1186 log_history = B_FALSE; 1187 1188 if (hardforce) { 1189 if (zpool_export_force(zhp, history_str) != 0) 1190 ret = 1; 1191 } else if (zpool_export(zhp, force, history_str) != 0) { 1192 ret = 1; 1193 } 1194 1195 zpool_close(zhp); 1196 } 1197 1198 return (ret); 1199} 1200 1201/* 1202 * Given a vdev configuration, determine the maximum width needed for the device 1203 * name column. 1204 */ 1205static int 1206max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max) 1207{ 1208 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE); 1209 nvlist_t **child; 1210 uint_t c, children; 1211 int ret; 1212 1213 if (strlen(name) + depth > max) 1214 max = strlen(name) + depth; 1215 1216 free(name); 1217 1218 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1219 &child, &children) == 0) { 1220 for (c = 0; c < children; c++) 1221 if ((ret = max_width(zhp, child[c], depth + 2, 1222 max)) > max) 1223 max = ret; 1224 } 1225 1226 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1227 &child, &children) == 0) { 1228 for (c = 0; c < children; c++) 1229 if ((ret = max_width(zhp, child[c], depth + 2, 1230 max)) > max) 1231 max = ret; 1232 } 1233 1234 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1235 &child, &children) == 0) { 1236 for (c = 0; c < children; c++) 1237 if ((ret = max_width(zhp, child[c], depth + 2, 1238 max)) > max) 1239 max = ret; 1240 } 1241 1242 1243 return (max); 1244} 1245 1246typedef struct spare_cbdata { 1247 uint64_t cb_guid; 1248 zpool_handle_t *cb_zhp; 1249} spare_cbdata_t; 1250 1251static boolean_t 1252find_vdev(nvlist_t *nv, uint64_t search) 1253{ 1254 uint64_t guid; 1255 nvlist_t **child; 1256 uint_t c, children; 1257 1258 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1259 search == guid) 1260 return (B_TRUE); 1261 1262 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1263 &child, &children) == 0) { 1264 for (c = 0; c < children; c++) 1265 if (find_vdev(child[c], search)) 1266 return (B_TRUE); 1267 } 1268 1269 return (B_FALSE); 1270} 1271 1272static int 1273find_spare(zpool_handle_t *zhp, void *data) 1274{ 1275 spare_cbdata_t *cbp = data; 1276 nvlist_t *config, *nvroot; 1277 1278 config = zpool_get_config(zhp, NULL); 1279 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1280 &nvroot) == 0); 1281 1282 if (find_vdev(nvroot, cbp->cb_guid)) { 1283 cbp->cb_zhp = zhp; 1284 return (1); 1285 } 1286 1287 zpool_close(zhp); 1288 return (0); 1289} 1290 1291/* 1292 * Print out configuration state as requested by status_callback. 1293 */ 1294void 1295print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 1296 int namewidth, int depth, boolean_t isspare) 1297{ 1298 nvlist_t **child; 1299 uint_t c, vsc, children; 1300 pool_scan_stat_t *ps = NULL; 1301 vdev_stat_t *vs; 1302 char rbuf[6], wbuf[6], cbuf[6]; 1303 char *vname; 1304 uint64_t notpresent; 1305 uint64_t ashift; 1306 spare_cbdata_t cb; 1307 const char *state; 1308 1309 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1310 &child, &children) != 0) 1311 children = 0; 1312 1313 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1314 (uint64_t **)&vs, &vsc) == 0); 1315 1316 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1317 if (isspare) { 1318 /* 1319 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 1320 * online drives. 1321 */ 1322 if (vs->vs_aux == VDEV_AUX_SPARED) 1323 state = "INUSE"; 1324 else if (vs->vs_state == VDEV_STATE_HEALTHY) 1325 state = "AVAIL"; 1326 } 1327 1328 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth, 1329 name, state); 1330 1331 if (!isspare) { 1332 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 1333 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 1334 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf)); 1335 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf); 1336 } 1337 1338 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1339 ¬present) == 0 || 1340 vs->vs_state <= VDEV_STATE_CANT_OPEN) { 1341 char *path; 1342 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) 1343 (void) printf(" was %s", path); 1344 } else if (vs->vs_aux != 0) { 1345 (void) printf(" "); 1346 1347 switch (vs->vs_aux) { 1348 case VDEV_AUX_OPEN_FAILED: 1349 (void) printf(gettext("cannot open")); 1350 break; 1351 1352 case VDEV_AUX_BAD_GUID_SUM: 1353 (void) printf(gettext("missing device")); 1354 break; 1355 1356 case VDEV_AUX_NO_REPLICAS: 1357 (void) printf(gettext("insufficient replicas")); 1358 break; 1359 1360 case VDEV_AUX_VERSION_NEWER: 1361 (void) printf(gettext("newer version")); 1362 break; 1363 1364 case VDEV_AUX_UNSUP_FEAT: 1365 (void) printf(gettext("unsupported feature(s)")); 1366 break; 1367 1368 case VDEV_AUX_ASHIFT_TOO_BIG: 1369 (void) printf(gettext("unsupported minimum blocksize")); 1370 break; 1371 1372 case VDEV_AUX_SPARED: 1373 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1374 &cb.cb_guid) == 0); 1375 if (zpool_iter(g_zfs, find_spare, &cb) == 1) { 1376 if (strcmp(zpool_get_name(cb.cb_zhp), 1377 zpool_get_name(zhp)) == 0) 1378 (void) printf(gettext("currently in " 1379 "use")); 1380 else 1381 (void) printf(gettext("in use by " 1382 "pool '%s'"), 1383 zpool_get_name(cb.cb_zhp)); 1384 zpool_close(cb.cb_zhp); 1385 } else { 1386 (void) printf(gettext("currently in use")); 1387 } 1388 break; 1389 1390 case VDEV_AUX_ERR_EXCEEDED: 1391 (void) printf(gettext("too many errors")); 1392 break; 1393 1394 case VDEV_AUX_IO_FAILURE: 1395 (void) printf(gettext("experienced I/O failures")); 1396 break; 1397 1398 case VDEV_AUX_BAD_LOG: 1399 (void) printf(gettext("bad intent log")); 1400 break; 1401 1402 case VDEV_AUX_EXTERNAL: 1403 (void) printf(gettext("external device fault")); 1404 break; 1405 1406 case VDEV_AUX_SPLIT_POOL: 1407 (void) printf(gettext("split into new pool")); 1408 break; 1409 1410 default: 1411 (void) printf(gettext("corrupted data")); 1412 break; 1413 } 1414 } else if (children == 0 && !isspare && 1415 VDEV_STAT_VALID(vs_physical_ashift, vsc) && 1416 vs->vs_configured_ashift < vs->vs_physical_ashift) { 1417 (void) printf( 1418 gettext(" block size: %dB configured, %dB native"), 1419 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); 1420 } 1421 1422 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, 1423 (uint64_t **)&ps, &c); 1424 1425 if (ps && ps->pss_state == DSS_SCANNING && 1426 vs->vs_scan_processed != 0 && children == 0) { 1427 (void) printf(gettext(" (%s)"), 1428 (ps->pss_func == POOL_SCAN_RESILVER) ? 1429 "resilvering" : "repairing"); 1430 } 1431 1432 (void) printf("\n"); 1433 1434 for (c = 0; c < children; c++) { 1435 uint64_t islog = B_FALSE, ishole = B_FALSE; 1436 1437 /* Don't print logs or holes here */ 1438 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1439 &islog); 1440 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 1441 &ishole); 1442 if (islog || ishole) 1443 continue; 1444 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1445 print_status_config(zhp, vname, child[c], 1446 namewidth, depth + 2, isspare); 1447 free(vname); 1448 } 1449} 1450 1451 1452/* 1453 * Print the configuration of an exported pool. Iterate over all vdevs in the 1454 * pool, printing out the name and status for each one. 1455 */ 1456void 1457print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth) 1458{ 1459 nvlist_t **child; 1460 uint_t c, children; 1461 vdev_stat_t *vs; 1462 char *type, *vname; 1463 1464 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1465 if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 1466 strcmp(type, VDEV_TYPE_HOLE) == 0) 1467 return; 1468 1469 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1470 (uint64_t **)&vs, &c) == 0); 1471 1472 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name); 1473 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 1474 1475 if (vs->vs_aux != 0) { 1476 (void) printf(" "); 1477 1478 switch (vs->vs_aux) { 1479 case VDEV_AUX_OPEN_FAILED: 1480 (void) printf(gettext("cannot open")); 1481 break; 1482 1483 case VDEV_AUX_BAD_GUID_SUM: 1484 (void) printf(gettext("missing device")); 1485 break; 1486 1487 case VDEV_AUX_NO_REPLICAS: 1488 (void) printf(gettext("insufficient replicas")); 1489 break; 1490 1491 case VDEV_AUX_VERSION_NEWER: 1492 (void) printf(gettext("newer version")); 1493 break; 1494 1495 case VDEV_AUX_UNSUP_FEAT: 1496 (void) printf(gettext("unsupported feature(s)")); 1497 break; 1498 1499 case VDEV_AUX_ERR_EXCEEDED: 1500 (void) printf(gettext("too many errors")); 1501 break; 1502 1503 default: 1504 (void) printf(gettext("corrupted data")); 1505 break; 1506 } 1507 } 1508 (void) printf("\n"); 1509 1510 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1511 &child, &children) != 0) 1512 return; 1513 1514 for (c = 0; c < children; c++) { 1515 uint64_t is_log = B_FALSE; 1516 1517 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1518 &is_log); 1519 if (is_log) 1520 continue; 1521 1522 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE); 1523 print_import_config(vname, child[c], namewidth, depth + 2); 1524 free(vname); 1525 } 1526 1527 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1528 &child, &children) == 0) { 1529 (void) printf(gettext("\tcache\n")); 1530 for (c = 0; c < children; c++) { 1531 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1532 (void) printf("\t %s\n", vname); 1533 free(vname); 1534 } 1535 } 1536 1537 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1538 &child, &children) == 0) { 1539 (void) printf(gettext("\tspares\n")); 1540 for (c = 0; c < children; c++) { 1541 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1542 (void) printf("\t %s\n", vname); 1543 free(vname); 1544 } 1545 } 1546} 1547 1548/* 1549 * Print log vdevs. 1550 * Logs are recorded as top level vdevs in the main pool child array 1551 * but with "is_log" set to 1. We use either print_status_config() or 1552 * print_import_config() to print the top level logs then any log 1553 * children (eg mirrored slogs) are printed recursively - which 1554 * works because only the top level vdev is marked "is_log" 1555 */ 1556static void 1557print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose) 1558{ 1559 uint_t c, children; 1560 nvlist_t **child; 1561 1562 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 1563 &children) != 0) 1564 return; 1565 1566 (void) printf(gettext("\tlogs\n")); 1567 1568 for (c = 0; c < children; c++) { 1569 uint64_t is_log = B_FALSE; 1570 char *name; 1571 1572 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1573 &is_log); 1574 if (!is_log) 1575 continue; 1576 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1577 if (verbose) 1578 print_status_config(zhp, name, child[c], namewidth, 1579 2, B_FALSE); 1580 else 1581 print_import_config(name, child[c], namewidth, 2); 1582 free(name); 1583 } 1584} 1585 1586/* 1587 * Display the status for the given pool. 1588 */ 1589static void 1590show_import(nvlist_t *config) 1591{ 1592 uint64_t pool_state; 1593 vdev_stat_t *vs; 1594 char *name; 1595 uint64_t guid; 1596 char *msgid; 1597 nvlist_t *nvroot; 1598 int reason; 1599 const char *health; 1600 uint_t vsc; 1601 int namewidth; 1602 char *comment; 1603 1604 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1605 &name) == 0); 1606 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1607 &guid) == 0); 1608 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1609 &pool_state) == 0); 1610 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1611 &nvroot) == 0); 1612 1613 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 1614 (uint64_t **)&vs, &vsc) == 0); 1615 health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1616 1617 reason = zpool_import_status(config, &msgid); 1618 1619 (void) printf(gettext(" pool: %s\n"), name); 1620 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 1621 (void) printf(gettext(" state: %s"), health); 1622 if (pool_state == POOL_STATE_DESTROYED) 1623 (void) printf(gettext(" (DESTROYED)")); 1624 (void) printf("\n"); 1625 1626 switch (reason) { 1627 case ZPOOL_STATUS_MISSING_DEV_R: 1628 case ZPOOL_STATUS_MISSING_DEV_NR: 1629 case ZPOOL_STATUS_BAD_GUID_SUM: 1630 (void) printf(gettext(" status: One or more devices are " 1631 "missing from the system.\n")); 1632 break; 1633 1634 case ZPOOL_STATUS_CORRUPT_LABEL_R: 1635 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 1636 (void) printf(gettext(" status: One or more devices contains " 1637 "corrupted data.\n")); 1638 break; 1639 1640 case ZPOOL_STATUS_CORRUPT_DATA: 1641 (void) printf( 1642 gettext(" status: The pool data is corrupted.\n")); 1643 break; 1644 1645 case ZPOOL_STATUS_OFFLINE_DEV: 1646 (void) printf(gettext(" status: One or more devices " 1647 "are offlined.\n")); 1648 break; 1649 1650 case ZPOOL_STATUS_CORRUPT_POOL: 1651 (void) printf(gettext(" status: The pool metadata is " 1652 "corrupted.\n")); 1653 break; 1654 1655 case ZPOOL_STATUS_VERSION_OLDER: 1656 (void) printf(gettext(" status: The pool is formatted using a " 1657 "legacy on-disk version.\n")); 1658 break; 1659 1660 case ZPOOL_STATUS_VERSION_NEWER: 1661 (void) printf(gettext(" status: The pool is formatted using an " 1662 "incompatible version.\n")); 1663 break; 1664 1665 case ZPOOL_STATUS_FEAT_DISABLED: 1666 (void) printf(gettext(" status: Some supported features are " 1667 "not enabled on the pool.\n")); 1668 break; 1669 1670 case ZPOOL_STATUS_UNSUP_FEAT_READ: 1671 (void) printf(gettext("status: The pool uses the following " 1672 "feature(s) not supported on this sytem:\n")); 1673 zpool_print_unsup_feat(config); 1674 break; 1675 1676 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1677 (void) printf(gettext("status: The pool can only be accessed " 1678 "in read-only mode on this system. It\n\tcannot be " 1679 "accessed in read-write mode because it uses the " 1680 "following\n\tfeature(s) not supported on this system:\n")); 1681 zpool_print_unsup_feat(config); 1682 break; 1683 1684 case ZPOOL_STATUS_HOSTID_MISMATCH: 1685 (void) printf(gettext(" status: The pool was last accessed by " 1686 "another system.\n")); 1687 break; 1688 1689 case ZPOOL_STATUS_FAULTED_DEV_R: 1690 case ZPOOL_STATUS_FAULTED_DEV_NR: 1691 (void) printf(gettext(" status: One or more devices are " 1692 "faulted.\n")); 1693 break; 1694 1695 case ZPOOL_STATUS_BAD_LOG: 1696 (void) printf(gettext(" status: An intent log record cannot be " 1697 "read.\n")); 1698 break; 1699 1700 case ZPOOL_STATUS_RESILVERING: 1701 (void) printf(gettext(" status: One or more devices were being " 1702 "resilvered.\n")); 1703 break; 1704 1705 default: 1706 /* 1707 * No other status can be seen when importing pools. 1708 */ 1709 assert(reason == ZPOOL_STATUS_OK); 1710 } 1711 1712 /* 1713 * Print out an action according to the overall state of the pool. 1714 */ 1715 if (vs->vs_state == VDEV_STATE_HEALTHY) { 1716 if (reason == ZPOOL_STATUS_VERSION_OLDER || 1717 reason == ZPOOL_STATUS_FEAT_DISABLED) { 1718 (void) printf(gettext(" action: The pool can be " 1719 "imported using its name or numeric identifier, " 1720 "though\n\tsome features will not be available " 1721 "without an explicit 'zpool upgrade'.\n")); 1722 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 1723 (void) printf(gettext(" action: The pool can be " 1724 "imported using its name or numeric " 1725 "identifier and\n\tthe '-f' flag.\n")); 1726 } else { 1727 (void) printf(gettext(" action: The pool can be " 1728 "imported using its name or numeric " 1729 "identifier.\n")); 1730 } 1731 } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 1732 (void) printf(gettext(" action: The pool can be imported " 1733 "despite missing or damaged devices. The\n\tfault " 1734 "tolerance of the pool may be compromised if imported.\n")); 1735 } else { 1736 switch (reason) { 1737 case ZPOOL_STATUS_VERSION_NEWER: 1738 (void) printf(gettext(" action: The pool cannot be " 1739 "imported. Access the pool on a system running " 1740 "newer\n\tsoftware, or recreate the pool from " 1741 "backup.\n")); 1742 break; 1743 case ZPOOL_STATUS_UNSUP_FEAT_READ: 1744 (void) printf(gettext("action: The pool cannot be " 1745 "imported. Access the pool on a system that " 1746 "supports\n\tthe required feature(s), or recreate " 1747 "the pool from backup.\n")); 1748 break; 1749 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1750 (void) printf(gettext("action: The pool cannot be " 1751 "imported in read-write mode. Import the pool " 1752 "with\n" 1753 "\t\"-o readonly=on\", access the pool on a system " 1754 "that supports the\n\trequired feature(s), or " 1755 "recreate the pool from backup.\n")); 1756 break; 1757 case ZPOOL_STATUS_MISSING_DEV_R: 1758 case ZPOOL_STATUS_MISSING_DEV_NR: 1759 case ZPOOL_STATUS_BAD_GUID_SUM: 1760 (void) printf(gettext(" action: The pool cannot be " 1761 "imported. Attach the missing\n\tdevices and try " 1762 "again.\n")); 1763 break; 1764 default: 1765 (void) printf(gettext(" action: The pool cannot be " 1766 "imported due to damaged devices or data.\n")); 1767 } 1768 } 1769 1770 /* Print the comment attached to the pool. */ 1771 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 1772 (void) printf(gettext("comment: %s\n"), comment); 1773 1774 /* 1775 * If the state is "closed" or "can't open", and the aux state 1776 * is "corrupt data": 1777 */ 1778 if (((vs->vs_state == VDEV_STATE_CLOSED) || 1779 (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 1780 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 1781 if (pool_state == POOL_STATE_DESTROYED) 1782 (void) printf(gettext("\tThe pool was destroyed, " 1783 "but can be imported using the '-Df' flags.\n")); 1784 else if (pool_state != POOL_STATE_EXPORTED) 1785 (void) printf(gettext("\tThe pool may be active on " 1786 "another system, but can be imported using\n\t" 1787 "the '-f' flag.\n")); 1788 } 1789 1790 if (msgid != NULL) 1791 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 1792 msgid); 1793 1794 (void) printf(gettext(" config:\n\n")); 1795 1796 namewidth = max_width(NULL, nvroot, 0, 0); 1797 if (namewidth < 10) 1798 namewidth = 10; 1799 1800 print_import_config(name, nvroot, namewidth, 0); 1801 if (num_logs(nvroot) > 0) 1802 print_logs(NULL, nvroot, namewidth, B_FALSE); 1803 1804 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 1805 (void) printf(gettext("\n\tAdditional devices are known to " 1806 "be part of this pool, though their\n\texact " 1807 "configuration cannot be determined.\n")); 1808 } 1809} 1810 1811/* 1812 * Perform the import for the given configuration. This passes the heavy 1813 * lifting off to zpool_import_props(), and then mounts the datasets contained 1814 * within the pool. 1815 */ 1816static int 1817do_import(nvlist_t *config, const char *newname, const char *mntopts, 1818 nvlist_t *props, int flags) 1819{ 1820 zpool_handle_t *zhp; 1821 char *name; 1822 uint64_t state; 1823 uint64_t version; 1824 1825 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1826 &name) == 0); 1827 1828 verify(nvlist_lookup_uint64(config, 1829 ZPOOL_CONFIG_POOL_STATE, &state) == 0); 1830 verify(nvlist_lookup_uint64(config, 1831 ZPOOL_CONFIG_VERSION, &version) == 0); 1832 if (!SPA_VERSION_IS_SUPPORTED(version)) { 1833 (void) fprintf(stderr, gettext("cannot import '%s': pool " 1834 "is formatted using an unsupported ZFS version\n"), name); 1835 return (1); 1836 } else if (state != POOL_STATE_EXPORTED && 1837 !(flags & ZFS_IMPORT_ANY_HOST)) { 1838 uint64_t hostid; 1839 1840 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, 1841 &hostid) == 0) { 1842 if ((unsigned long)hostid != gethostid()) { 1843 char *hostname; 1844 uint64_t timestamp; 1845 time_t t; 1846 1847 verify(nvlist_lookup_string(config, 1848 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 1849 verify(nvlist_lookup_uint64(config, 1850 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0); 1851 t = timestamp; 1852 (void) fprintf(stderr, gettext("cannot import " 1853 "'%s': pool may be in use from other " 1854 "system, it was last accessed by %s " 1855 "(hostid: 0x%lx) on %s"), name, hostname, 1856 (unsigned long)hostid, 1857 asctime(localtime(&t))); 1858 (void) fprintf(stderr, gettext("use '-f' to " 1859 "import anyway\n")); 1860 return (1); 1861 } 1862 } else { 1863 (void) fprintf(stderr, gettext("cannot import '%s': " 1864 "pool may be in use from other system\n"), name); 1865 (void) fprintf(stderr, gettext("use '-f' to import " 1866 "anyway\n")); 1867 return (1); 1868 } 1869 } 1870 1871 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 1872 return (1); 1873 1874 if (newname != NULL) 1875 name = (char *)newname; 1876 1877 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 1878 return (1); 1879 1880 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 1881 !(flags & ZFS_IMPORT_ONLY) && 1882 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 1883 zpool_close(zhp); 1884 return (1); 1885 } 1886 1887 zpool_close(zhp); 1888 return (0); 1889} 1890 1891/* 1892 * zpool import [-d dir] [-D] 1893 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1894 * [-d dir | -c cachefile] [-f] -a 1895 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1896 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] 1897 * 1898 * -c Read pool information from a cachefile instead of searching 1899 * devices. 1900 * 1901 * -d Scan in a specific directory, other than /dev/dsk. More than 1902 * one directory can be specified using multiple '-d' options. 1903 * 1904 * -D Scan for previously destroyed pools or import all or only 1905 * specified destroyed pools. 1906 * 1907 * -R Temporarily import the pool, with all mountpoints relative to 1908 * the given root. The pool will remain exported when the machine 1909 * is rebooted. 1910 * 1911 * -V Import even in the presence of faulted vdevs. This is an 1912 * intentionally undocumented option for testing purposes, and 1913 * treats the pool configuration as complete, leaving any bad 1914 * vdevs in the FAULTED state. In other words, it does verbatim 1915 * import. 1916 * 1917 * -f Force import, even if it appears that the pool is active. 1918 * 1919 * -F Attempt rewind if necessary. 1920 * 1921 * -n See if rewind would work, but don't actually rewind. 1922 * 1923 * -N Import the pool but don't mount datasets. 1924 * 1925 * -T Specify a starting txg to use for import. This option is 1926 * intentionally undocumented option for testing purposes. 1927 * 1928 * -a Import all pools found. 1929 * 1930 * -o Set property=value and/or temporary mount options (without '='). 1931 * 1932 * The import command scans for pools to import, and import pools based on pool 1933 * name and GUID. The pool can also be renamed as part of the import process. 1934 */ 1935int 1936zpool_do_import(int argc, char **argv) 1937{ 1938 char **searchdirs = NULL; 1939 int nsearch = 0; 1940 int c; 1941 int err = 0; 1942 nvlist_t *pools = NULL; 1943 boolean_t do_all = B_FALSE; 1944 boolean_t do_destroyed = B_FALSE; 1945 char *mntopts = NULL; 1946 nvpair_t *elem; 1947 nvlist_t *config; 1948 uint64_t searchguid = 0; 1949 char *searchname = NULL; 1950 char *propval; 1951 nvlist_t *found_config; 1952 nvlist_t *policy = NULL; 1953 nvlist_t *props = NULL; 1954 boolean_t first; 1955 int flags = ZFS_IMPORT_NORMAL; 1956 uint32_t rewind_policy = ZPOOL_NO_REWIND; 1957 boolean_t dryrun = B_FALSE; 1958 boolean_t do_rewind = B_FALSE; 1959 boolean_t xtreme_rewind = B_FALSE; 1960 uint64_t pool_state, txg = -1ULL; 1961 char *cachefile = NULL; 1962 importargs_t idata = { 0 }; 1963 char *endptr; 1964 1965 /* check options */ 1966 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) { 1967 switch (c) { 1968 case 'a': 1969 do_all = B_TRUE; 1970 break; 1971 case 'c': 1972 cachefile = optarg; 1973 break; 1974 case 'd': 1975 if (searchdirs == NULL) { 1976 searchdirs = safe_malloc(sizeof (char *)); 1977 } else { 1978 char **tmp = safe_malloc((nsearch + 1) * 1979 sizeof (char *)); 1980 bcopy(searchdirs, tmp, nsearch * 1981 sizeof (char *)); 1982 free(searchdirs); 1983 searchdirs = tmp; 1984 } 1985 searchdirs[nsearch++] = optarg; 1986 break; 1987 case 'D': 1988 do_destroyed = B_TRUE; 1989 break; 1990 case 'f': 1991 flags |= ZFS_IMPORT_ANY_HOST; 1992 break; 1993 case 'F': 1994 do_rewind = B_TRUE; 1995 break; 1996 case 'm': 1997 flags |= ZFS_IMPORT_MISSING_LOG; 1998 break; 1999 case 'n': 2000 dryrun = B_TRUE; 2001 break; 2002 case 'N': 2003 flags |= ZFS_IMPORT_ONLY; 2004 break; 2005 case 'o': 2006 if ((propval = strchr(optarg, '=')) != NULL) { 2007 *propval = '\0'; 2008 propval++; 2009 if (add_prop_list(optarg, propval, 2010 &props, B_TRUE)) 2011 goto error; 2012 } else { 2013 mntopts = optarg; 2014 } 2015 break; 2016 case 'R': 2017 if (add_prop_list(zpool_prop_to_name( 2018 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 2019 goto error; 2020 if (nvlist_lookup_string(props, 2021 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 2022 &propval) == 0) 2023 break; 2024 if (add_prop_list(zpool_prop_to_name( 2025 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 2026 goto error; 2027 break; 2028 case 'T': 2029 errno = 0; 2030 txg = strtoull(optarg, &endptr, 10); 2031 if (errno != 0 || *endptr != '\0') { 2032 (void) fprintf(stderr, 2033 gettext("invalid txg value\n")); 2034 usage(B_FALSE); 2035 } 2036 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 2037 break; 2038 case 'V': 2039 flags |= ZFS_IMPORT_VERBATIM; 2040 break; 2041 case 'X': 2042 xtreme_rewind = B_TRUE; 2043 break; 2044 case ':': 2045 (void) fprintf(stderr, gettext("missing argument for " 2046 "'%c' option\n"), optopt); 2047 usage(B_FALSE); 2048 break; 2049 case '?': 2050 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2051 optopt); 2052 usage(B_FALSE); 2053 } 2054 } 2055 2056 argc -= optind; 2057 argv += optind; 2058 2059 if (cachefile && nsearch != 0) { 2060 (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 2061 usage(B_FALSE); 2062 } 2063 2064 if ((dryrun || xtreme_rewind) && !do_rewind) { 2065 (void) fprintf(stderr, 2066 gettext("-n or -X only meaningful with -F\n")); 2067 usage(B_FALSE); 2068 } 2069 if (dryrun) 2070 rewind_policy = ZPOOL_TRY_REWIND; 2071 else if (do_rewind) 2072 rewind_policy = ZPOOL_DO_REWIND; 2073 if (xtreme_rewind) 2074 rewind_policy |= ZPOOL_EXTREME_REWIND; 2075 2076 /* In the future, we can capture further policy and include it here */ 2077 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 2078 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 || 2079 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 2080 goto error; 2081 2082 if (searchdirs == NULL) { 2083 searchdirs = safe_malloc(sizeof (char *)); 2084 searchdirs[0] = "/dev"; 2085 nsearch = 1; 2086 } 2087 2088 /* check argument count */ 2089 if (do_all) { 2090 if (argc != 0) { 2091 (void) fprintf(stderr, gettext("too many arguments\n")); 2092 usage(B_FALSE); 2093 } 2094 } else { 2095 if (argc > 2) { 2096 (void) fprintf(stderr, gettext("too many arguments\n")); 2097 usage(B_FALSE); 2098 } 2099 2100 /* 2101 * Check for the SYS_CONFIG privilege. We do this explicitly 2102 * here because otherwise any attempt to discover pools will 2103 * silently fail. 2104 */ 2105 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) { 2106 (void) fprintf(stderr, gettext("cannot " 2107 "discover pools: permission denied\n")); 2108 free(searchdirs); 2109 nvlist_free(policy); 2110 return (1); 2111 } 2112 } 2113 2114 /* 2115 * Depending on the arguments given, we do one of the following: 2116 * 2117 * <none> Iterate through all pools and display information about 2118 * each one. 2119 * 2120 * -a Iterate through all pools and try to import each one. 2121 * 2122 * <id> Find the pool that corresponds to the given GUID/pool 2123 * name and import that one. 2124 * 2125 * -D Above options applies only to destroyed pools. 2126 */ 2127 if (argc != 0) { 2128 char *endptr; 2129 2130 errno = 0; 2131 searchguid = strtoull(argv[0], &endptr, 10); 2132 if (errno != 0 || *endptr != '\0') { 2133 searchname = argv[0]; 2134 searchguid = 0; 2135 } 2136 found_config = NULL; 2137 2138 /* 2139 * User specified a name or guid. Ensure it's unique. 2140 */ 2141 idata.unique = B_TRUE; 2142 } 2143 2144 2145 idata.path = searchdirs; 2146 idata.paths = nsearch; 2147 idata.poolname = searchname; 2148 idata.guid = searchguid; 2149 idata.cachefile = cachefile; 2150 2151 pools = zpool_search_import(g_zfs, &idata); 2152 2153 if (pools != NULL && idata.exists && 2154 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 2155 (void) fprintf(stderr, gettext("cannot import '%s': " 2156 "a pool with that name already exists\n"), 2157 argv[0]); 2158 (void) fprintf(stderr, gettext("use the form '%s " 2159 "<pool | id> <newpool>' to give it a new name\n"), 2160 "zpool import"); 2161 err = 1; 2162 } else if (pools == NULL && idata.exists) { 2163 (void) fprintf(stderr, gettext("cannot import '%s': " 2164 "a pool with that name is already created/imported,\n"), 2165 argv[0]); 2166 (void) fprintf(stderr, gettext("and no additional pools " 2167 "with that name were found\n")); 2168 err = 1; 2169 } else if (pools == NULL) { 2170 if (argc != 0) { 2171 (void) fprintf(stderr, gettext("cannot import '%s': " 2172 "no such pool available\n"), argv[0]); 2173 } 2174 err = 1; 2175 } 2176 2177 if (err == 1) { 2178 free(searchdirs); 2179 nvlist_free(policy); 2180 return (1); 2181 } 2182 2183 /* 2184 * At this point we have a list of import candidate configs. Even if 2185 * we were searching by pool name or guid, we still need to 2186 * post-process the list to deal with pool state and possible 2187 * duplicate names. 2188 */ 2189 err = 0; 2190 elem = NULL; 2191 first = B_TRUE; 2192 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 2193 2194 verify(nvpair_value_nvlist(elem, &config) == 0); 2195 2196 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2197 &pool_state) == 0); 2198 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 2199 continue; 2200 if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 2201 continue; 2202 2203 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY, 2204 policy) == 0); 2205 2206 if (argc == 0) { 2207 if (first) 2208 first = B_FALSE; 2209 else if (!do_all) 2210 (void) printf("\n"); 2211 2212 if (do_all) { 2213 err |= do_import(config, NULL, mntopts, 2214 props, flags); 2215 } else { 2216 show_import(config); 2217 } 2218 } else if (searchname != NULL) { 2219 char *name; 2220 2221 /* 2222 * We are searching for a pool based on name. 2223 */ 2224 verify(nvlist_lookup_string(config, 2225 ZPOOL_CONFIG_POOL_NAME, &name) == 0); 2226 2227 if (strcmp(name, searchname) == 0) { 2228 if (found_config != NULL) { 2229 (void) fprintf(stderr, gettext( 2230 "cannot import '%s': more than " 2231 "one matching pool\n"), searchname); 2232 (void) fprintf(stderr, gettext( 2233 "import by numeric ID instead\n")); 2234 err = B_TRUE; 2235 } 2236 found_config = config; 2237 } 2238 } else { 2239 uint64_t guid; 2240 2241 /* 2242 * Search for a pool by guid. 2243 */ 2244 verify(nvlist_lookup_uint64(config, 2245 ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 2246 2247 if (guid == searchguid) 2248 found_config = config; 2249 } 2250 } 2251 2252 /* 2253 * If we were searching for a specific pool, verify that we found a 2254 * pool, and then do the import. 2255 */ 2256 if (argc != 0 && err == 0) { 2257 if (found_config == NULL) { 2258 (void) fprintf(stderr, gettext("cannot import '%s': " 2259 "no such pool available\n"), argv[0]); 2260 err = B_TRUE; 2261 } else { 2262 err |= do_import(found_config, argc == 1 ? NULL : 2263 argv[1], mntopts, props, flags); 2264 } 2265 } 2266 2267 /* 2268 * If we were just looking for pools, report an error if none were 2269 * found. 2270 */ 2271 if (argc == 0 && first) 2272 (void) fprintf(stderr, 2273 gettext("no pools available to import\n")); 2274 2275error: 2276 nvlist_free(props); 2277 nvlist_free(pools); 2278 nvlist_free(policy); 2279 free(searchdirs); 2280 2281 return (err ? 1 : 0); 2282} 2283 2284typedef struct iostat_cbdata { 2285 boolean_t cb_verbose; 2286 int cb_namewidth; 2287 int cb_iteration; 2288 zpool_list_t *cb_list; 2289} iostat_cbdata_t; 2290 2291static void 2292print_iostat_separator(iostat_cbdata_t *cb) 2293{ 2294 int i = 0; 2295 2296 for (i = 0; i < cb->cb_namewidth; i++) 2297 (void) printf("-"); 2298 (void) printf(" ----- ----- ----- ----- ----- -----\n"); 2299} 2300 2301static void 2302print_iostat_header(iostat_cbdata_t *cb) 2303{ 2304 (void) printf("%*s capacity operations bandwidth\n", 2305 cb->cb_namewidth, ""); 2306 (void) printf("%-*s alloc free read write read write\n", 2307 cb->cb_namewidth, "pool"); 2308 print_iostat_separator(cb); 2309} 2310 2311/* 2312 * Display a single statistic. 2313 */ 2314static void 2315print_one_stat(uint64_t value) 2316{ 2317 char buf[64]; 2318 2319 zfs_nicenum(value, buf, sizeof (buf)); 2320 (void) printf(" %5s", buf); 2321} 2322 2323/* 2324 * Print out all the statistics for the given vdev. This can either be the 2325 * toplevel configuration, or called recursively. If 'name' is NULL, then this 2326 * is a verbose output, and we don't want to display the toplevel pool stats. 2327 */ 2328void 2329print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 2330 nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 2331{ 2332 nvlist_t **oldchild, **newchild; 2333 uint_t c, children; 2334 vdev_stat_t *oldvs, *newvs; 2335 vdev_stat_t zerovs = { 0 }; 2336 uint64_t tdelta; 2337 double scale; 2338 char *vname; 2339 2340 if (oldnv != NULL) { 2341 verify(nvlist_lookup_uint64_array(oldnv, 2342 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 2343 } else { 2344 oldvs = &zerovs; 2345 } 2346 2347 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 2348 (uint64_t **)&newvs, &c) == 0); 2349 2350 if (strlen(name) + depth > cb->cb_namewidth) 2351 (void) printf("%*s%s", depth, "", name); 2352 else 2353 (void) printf("%*s%s%*s", depth, "", name, 2354 (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2355 2356 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 2357 2358 if (tdelta == 0) 2359 scale = 1.0; 2360 else 2361 scale = (double)NANOSEC / tdelta; 2362 2363 /* only toplevel vdevs have capacity stats */ 2364 if (newvs->vs_space == 0) { 2365 (void) printf(" - -"); 2366 } else { 2367 print_one_stat(newvs->vs_alloc); 2368 print_one_stat(newvs->vs_space - newvs->vs_alloc); 2369 } 2370 2371 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] - 2372 oldvs->vs_ops[ZIO_TYPE_READ]))); 2373 2374 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] - 2375 oldvs->vs_ops[ZIO_TYPE_WRITE]))); 2376 2377 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] - 2378 oldvs->vs_bytes[ZIO_TYPE_READ]))); 2379 2380 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] - 2381 oldvs->vs_bytes[ZIO_TYPE_WRITE]))); 2382 2383 (void) printf("\n"); 2384 2385 if (!cb->cb_verbose) 2386 return; 2387 2388 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 2389 &newchild, &children) != 0) 2390 return; 2391 2392 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 2393 &oldchild, &c) != 0) 2394 return; 2395 2396 for (c = 0; c < children; c++) { 2397 uint64_t ishole = B_FALSE, islog = B_FALSE; 2398 2399 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 2400 &ishole); 2401 2402 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 2403 &islog); 2404 2405 if (ishole || islog) 2406 continue; 2407 2408 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE); 2409 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2410 newchild[c], cb, depth + 2); 2411 free(vname); 2412 } 2413 2414 /* 2415 * Log device section 2416 */ 2417 2418 if (num_logs(newnv) > 0) { 2419 (void) printf("%-*s - - - - - " 2420 "-\n", cb->cb_namewidth, "logs"); 2421 2422 for (c = 0; c < children; c++) { 2423 uint64_t islog = B_FALSE; 2424 (void) nvlist_lookup_uint64(newchild[c], 2425 ZPOOL_CONFIG_IS_LOG, &islog); 2426 2427 if (islog) { 2428 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 2429 B_FALSE); 2430 print_vdev_stats(zhp, vname, oldnv ? 2431 oldchild[c] : NULL, newchild[c], 2432 cb, depth + 2); 2433 free(vname); 2434 } 2435 } 2436 2437 } 2438 2439 /* 2440 * Include level 2 ARC devices in iostat output 2441 */ 2442 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 2443 &newchild, &children) != 0) 2444 return; 2445 2446 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 2447 &oldchild, &c) != 0) 2448 return; 2449 2450 if (children > 0) { 2451 (void) printf("%-*s - - - - - " 2452 "-\n", cb->cb_namewidth, "cache"); 2453 for (c = 0; c < children; c++) { 2454 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 2455 B_FALSE); 2456 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2457 newchild[c], cb, depth + 2); 2458 free(vname); 2459 } 2460 } 2461} 2462 2463static int 2464refresh_iostat(zpool_handle_t *zhp, void *data) 2465{ 2466 iostat_cbdata_t *cb = data; 2467 boolean_t missing; 2468 2469 /* 2470 * If the pool has disappeared, remove it from the list and continue. 2471 */ 2472 if (zpool_refresh_stats(zhp, &missing) != 0) 2473 return (-1); 2474 2475 if (missing) 2476 pool_list_remove(cb->cb_list, zhp); 2477 2478 return (0); 2479} 2480 2481/* 2482 * Callback to print out the iostats for the given pool. 2483 */ 2484int 2485print_iostat(zpool_handle_t *zhp, void *data) 2486{ 2487 iostat_cbdata_t *cb = data; 2488 nvlist_t *oldconfig, *newconfig; 2489 nvlist_t *oldnvroot, *newnvroot; 2490 2491 newconfig = zpool_get_config(zhp, &oldconfig); 2492 2493 if (cb->cb_iteration == 1) 2494 oldconfig = NULL; 2495 2496 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 2497 &newnvroot) == 0); 2498 2499 if (oldconfig == NULL) 2500 oldnvroot = NULL; 2501 else 2502 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 2503 &oldnvroot) == 0); 2504 2505 /* 2506 * Print out the statistics for the pool. 2507 */ 2508 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0); 2509 2510 if (cb->cb_verbose) 2511 print_iostat_separator(cb); 2512 2513 return (0); 2514} 2515 2516int 2517get_namewidth(zpool_handle_t *zhp, void *data) 2518{ 2519 iostat_cbdata_t *cb = data; 2520 nvlist_t *config, *nvroot; 2521 2522 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 2523 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2524 &nvroot) == 0); 2525 if (!cb->cb_verbose) 2526 cb->cb_namewidth = strlen(zpool_get_name(zhp)); 2527 else 2528 cb->cb_namewidth = max_width(zhp, nvroot, 0, 2529 cb->cb_namewidth); 2530 } 2531 2532 /* 2533 * The width must fall into the range [10,38]. The upper limit is the 2534 * maximum we can have and still fit in 80 columns. 2535 */ 2536 if (cb->cb_namewidth < 10) 2537 cb->cb_namewidth = 10; 2538 if (cb->cb_namewidth > 38) 2539 cb->cb_namewidth = 38; 2540 2541 return (0); 2542} 2543 2544/* 2545 * Parse the input string, get the 'interval' and 'count' value if there is one. 2546 */ 2547static void 2548get_interval_count(int *argcp, char **argv, unsigned long *iv, 2549 unsigned long *cnt) 2550{ 2551 unsigned long interval = 0, count = 0; 2552 int argc = *argcp, errno; 2553 2554 /* 2555 * Determine if the last argument is an integer or a pool name 2556 */ 2557 if (argc > 0 && isdigit(argv[argc - 1][0])) { 2558 char *end; 2559 2560 errno = 0; 2561 interval = strtoul(argv[argc - 1], &end, 10); 2562 2563 if (*end == '\0' && errno == 0) { 2564 if (interval == 0) { 2565 (void) fprintf(stderr, gettext("interval " 2566 "cannot be zero\n")); 2567 usage(B_FALSE); 2568 } 2569 /* 2570 * Ignore the last parameter 2571 */ 2572 argc--; 2573 } else { 2574 /* 2575 * If this is not a valid number, just plow on. The 2576 * user will get a more informative error message later 2577 * on. 2578 */ 2579 interval = 0; 2580 } 2581 } 2582 2583 /* 2584 * If the last argument is also an integer, then we have both a count 2585 * and an interval. 2586 */ 2587 if (argc > 0 && isdigit(argv[argc - 1][0])) { 2588 char *end; 2589 2590 errno = 0; 2591 count = interval; 2592 interval = strtoul(argv[argc - 1], &end, 10); 2593 2594 if (*end == '\0' && errno == 0) { 2595 if (interval == 0) { 2596 (void) fprintf(stderr, gettext("interval " 2597 "cannot be zero\n")); 2598 usage(B_FALSE); 2599 } 2600 2601 /* 2602 * Ignore the last parameter 2603 */ 2604 argc--; 2605 } else { 2606 interval = 0; 2607 } 2608 } 2609 2610 *iv = interval; 2611 *cnt = count; 2612 *argcp = argc; 2613} 2614 2615static void 2616get_timestamp_arg(char c) 2617{ 2618 if (c == 'u') 2619 timestamp_fmt = UDATE; 2620 else if (c == 'd') 2621 timestamp_fmt = DDATE; 2622 else 2623 usage(B_FALSE); 2624} 2625 2626/* 2627 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]] 2628 * 2629 * -v Display statistics for individual vdevs 2630 * -T Display a timestamp in date(1) or Unix format 2631 * 2632 * This command can be tricky because we want to be able to deal with pool 2633 * creation/destruction as well as vdev configuration changes. The bulk of this 2634 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 2635 * on pool_list_update() to detect the addition of new pools. Configuration 2636 * changes are all handled within libzfs. 2637 */ 2638int 2639zpool_do_iostat(int argc, char **argv) 2640{ 2641 int c; 2642 int ret; 2643 int npools; 2644 unsigned long interval = 0, count = 0; 2645 zpool_list_t *list; 2646 boolean_t verbose = B_FALSE; 2647 iostat_cbdata_t cb; 2648 2649 /* check options */ 2650 while ((c = getopt(argc, argv, "T:v")) != -1) { 2651 switch (c) { 2652 case 'T': 2653 get_timestamp_arg(*optarg); 2654 break; 2655 case 'v': 2656 verbose = B_TRUE; 2657 break; 2658 case '?': 2659 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2660 optopt); 2661 usage(B_FALSE); 2662 } 2663 } 2664 2665 argc -= optind; 2666 argv += optind; 2667 2668 get_interval_count(&argc, argv, &interval, &count); 2669 2670 /* 2671 * Construct the list of all interesting pools. 2672 */ 2673 ret = 0; 2674 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) 2675 return (1); 2676 2677 if (pool_list_count(list) == 0 && argc != 0) { 2678 pool_list_free(list); 2679 return (1); 2680 } 2681 2682 if (pool_list_count(list) == 0 && interval == 0) { 2683 pool_list_free(list); 2684 (void) fprintf(stderr, gettext("no pools available\n")); 2685 return (1); 2686 } 2687 2688 /* 2689 * Enter the main iostat loop. 2690 */ 2691 cb.cb_list = list; 2692 cb.cb_verbose = verbose; 2693 cb.cb_iteration = 0; 2694 cb.cb_namewidth = 0; 2695 2696 for (;;) { 2697 pool_list_update(list); 2698 2699 if ((npools = pool_list_count(list)) == 0) 2700 break; 2701 2702 /* 2703 * Refresh all statistics. This is done as an explicit step 2704 * before calculating the maximum name width, so that any 2705 * configuration changes are properly accounted for. 2706 */ 2707 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb); 2708 2709 /* 2710 * Iterate over all pools to determine the maximum width 2711 * for the pool / device name column across all pools. 2712 */ 2713 cb.cb_namewidth = 0; 2714 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 2715 2716 if (timestamp_fmt != NODATE) 2717 print_timestamp(timestamp_fmt); 2718 2719 /* 2720 * If it's the first time, or verbose mode, print the header. 2721 */ 2722 if (++cb.cb_iteration == 1 || verbose) 2723 print_iostat_header(&cb); 2724 2725 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb); 2726 2727 /* 2728 * If there's more than one pool, and we're not in verbose mode 2729 * (which prints a separator for us), then print a separator. 2730 */ 2731 if (npools > 1 && !verbose) 2732 print_iostat_separator(&cb); 2733 2734 if (verbose) 2735 (void) printf("\n"); 2736 2737 /* 2738 * Flush the output so that redirection to a file isn't buffered 2739 * indefinitely. 2740 */ 2741 (void) fflush(stdout); 2742 2743 if (interval == 0) 2744 break; 2745 2746 if (count != 0 && --count == 0) 2747 break; 2748 2749 (void) sleep(interval); 2750 } 2751 2752 pool_list_free(list); 2753 2754 return (ret); 2755} 2756 2757typedef struct list_cbdata { 2758 boolean_t cb_verbose; 2759 int cb_namewidth; 2760 boolean_t cb_scripted; 2761 zprop_list_t *cb_proplist; 2762} list_cbdata_t; 2763 2764/* 2765 * Given a list of columns to display, output appropriate headers for each one. 2766 */ 2767static void 2768print_header(list_cbdata_t *cb) 2769{ 2770 zprop_list_t *pl = cb->cb_proplist; 2771 char headerbuf[ZPOOL_MAXPROPLEN]; 2772 const char *header; 2773 boolean_t first = B_TRUE; 2774 boolean_t right_justify; 2775 size_t width = 0; 2776 2777 for (; pl != NULL; pl = pl->pl_next) { 2778 width = pl->pl_width; 2779 if (first && cb->cb_verbose) { 2780 /* 2781 * Reset the width to accommodate the verbose listing 2782 * of devices. 2783 */ 2784 width = cb->cb_namewidth; 2785 } 2786 2787 if (!first) 2788 (void) printf(" "); 2789 else 2790 first = B_FALSE; 2791 2792 right_justify = B_FALSE; 2793 if (pl->pl_prop != ZPROP_INVAL) { 2794 header = zpool_prop_column_name(pl->pl_prop); 2795 right_justify = zpool_prop_align_right(pl->pl_prop); 2796 } else { 2797 int i; 2798 2799 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2800 headerbuf[i] = toupper(pl->pl_user_prop[i]); 2801 headerbuf[i] = '\0'; 2802 header = headerbuf; 2803 } 2804 2805 if (pl->pl_next == NULL && !right_justify) 2806 (void) printf("%s", header); 2807 else if (right_justify) 2808 (void) printf("%*s", width, header); 2809 else 2810 (void) printf("%-*s", width, header); 2811 2812 } 2813 2814 (void) printf("\n"); 2815} 2816 2817/* 2818 * Given a pool and a list of properties, print out all the properties according 2819 * to the described layout. 2820 */ 2821static void 2822print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 2823{ 2824 zprop_list_t *pl = cb->cb_proplist; 2825 boolean_t first = B_TRUE; 2826 char property[ZPOOL_MAXPROPLEN]; 2827 char *propstr; 2828 boolean_t right_justify; 2829 size_t width; 2830 2831 for (; pl != NULL; pl = pl->pl_next) { 2832 2833 width = pl->pl_width; 2834 if (first && cb->cb_verbose) { 2835 /* 2836 * Reset the width to accommodate the verbose listing 2837 * of devices. 2838 */ 2839 width = cb->cb_namewidth; 2840 } 2841 2842 if (!first) { 2843 if (cb->cb_scripted) 2844 (void) printf("\t"); 2845 else 2846 (void) printf(" "); 2847 } else { 2848 first = B_FALSE; 2849 } 2850 2851 right_justify = B_FALSE; 2852 if (pl->pl_prop != ZPROP_INVAL) { 2853 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ && 2854 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0) 2855 propstr = "-"; 2856 else if (zpool_get_prop(zhp, pl->pl_prop, property, 2857 sizeof (property), NULL) != 0) 2858 propstr = "-"; 2859 else 2860 propstr = property; 2861 2862 right_justify = zpool_prop_align_right(pl->pl_prop); 2863 } else if ((zpool_prop_feature(pl->pl_user_prop) || 2864 zpool_prop_unsupported(pl->pl_user_prop)) && 2865 zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 2866 sizeof (property)) == 0) { 2867 propstr = property; 2868 } else { 2869 propstr = "-"; 2870 } 2871 2872 2873 /* 2874 * If this is being called in scripted mode, or if this is the 2875 * last column and it is left-justified, don't include a width 2876 * format specifier. 2877 */ 2878 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 2879 (void) printf("%s", propstr); 2880 else if (right_justify) 2881 (void) printf("%*s", width, propstr); 2882 else 2883 (void) printf("%-*s", width, propstr); 2884 } 2885 2886 (void) printf("\n"); 2887} 2888 2889static void 2890print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted) 2891{ 2892 char propval[64]; 2893 boolean_t fixed; 2894 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 2895 2896 zfs_nicenum(value, propval, sizeof (propval)); 2897 2898 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0) 2899 (void) strlcpy(propval, "-", sizeof (propval)); 2900 2901 if (scripted) 2902 (void) printf("\t%s", propval); 2903 else 2904 (void) printf(" %*s", width, propval); 2905} 2906 2907void 2908print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 2909 list_cbdata_t *cb, int depth) 2910{ 2911 nvlist_t **child; 2912 vdev_stat_t *vs; 2913 uint_t c, children; 2914 char *vname; 2915 boolean_t scripted = cb->cb_scripted; 2916 2917 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2918 (uint64_t **)&vs, &c) == 0); 2919 2920 if (name != NULL) { 2921 if (scripted) 2922 (void) printf("\t%s", name); 2923 else if (strlen(name) + depth > cb->cb_namewidth) 2924 (void) printf("%*s%s", depth, "", name); 2925 else 2926 (void) printf("%*s%s%*s", depth, "", name, 2927 (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2928 2929 /* only toplevel vdevs have capacity stats */ 2930 if (vs->vs_space == 0) { 2931 if (scripted) 2932 (void) printf("\t-\t-\t-"); 2933 else 2934 (void) printf(" - - -"); 2935 } else { 2936 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, 2937 scripted); 2938 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc, 2939 scripted); 2940 print_one_column(ZPOOL_PROP_FREE, 2941 vs->vs_space - vs->vs_alloc, scripted); 2942 } 2943 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, 2944 scripted); 2945 (void) printf("\n"); 2946 } 2947 2948 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2949 &child, &children) != 0) 2950 return; 2951 2952 for (c = 0; c < children; c++) { 2953 uint64_t ishole = B_FALSE; 2954 2955 if (nvlist_lookup_uint64(child[c], 2956 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 2957 continue; 2958 2959 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 2960 print_list_stats(zhp, vname, child[c], cb, depth + 2); 2961 free(vname); 2962 } 2963 2964 /* 2965 * Include level 2 ARC devices in iostat output 2966 */ 2967 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2968 &child, &children) != 0) 2969 return; 2970 2971 if (children > 0) { 2972 (void) printf("%-*s - - - - - " 2973 "-\n", cb->cb_namewidth, "cache"); 2974 for (c = 0; c < children; c++) { 2975 vname = zpool_vdev_name(g_zfs, zhp, child[c], 2976 B_FALSE); 2977 print_list_stats(zhp, vname, child[c], cb, depth + 2); 2978 free(vname); 2979 } 2980 } 2981} 2982 2983 2984/* 2985 * Generic callback function to list a pool. 2986 */ 2987int 2988list_callback(zpool_handle_t *zhp, void *data) 2989{ 2990 list_cbdata_t *cbp = data; 2991 nvlist_t *config; 2992 nvlist_t *nvroot; 2993 2994 config = zpool_get_config(zhp, NULL); 2995 2996 print_pool(zhp, cbp); 2997 if (!cbp->cb_verbose) 2998 return (0); 2999 3000 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3001 &nvroot) == 0); 3002 print_list_stats(zhp, NULL, nvroot, cbp, 0); 3003 3004 return (0); 3005} 3006 3007/* 3008 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 3009 * 3010 * -H Scripted mode. Don't display headers, and separate properties 3011 * by a single tab. 3012 * -o List of properties to display. Defaults to 3013 * "name,size,allocated,free,capacity,health,altroot" 3014 * -T Display a timestamp in date(1) or Unix format 3015 * 3016 * List all pools in the system, whether or not they're healthy. Output space 3017 * statistics for each one, as well as health status summary. 3018 */ 3019int 3020zpool_do_list(int argc, char **argv) 3021{ 3022 int c; 3023 int ret; 3024 list_cbdata_t cb = { 0 }; 3025 static char default_props[] = 3026 "name,size,allocated,free,capacity,dedupratio," 3027 "health,altroot"; 3028 char *props = default_props; 3029 unsigned long interval = 0, count = 0; 3030 zpool_list_t *list; 3031 boolean_t first = B_TRUE; 3032 3033 /* check options */ 3034 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) { 3035 switch (c) { 3036 case 'H': 3037 cb.cb_scripted = B_TRUE; 3038 break; 3039 case 'o': 3040 props = optarg; 3041 break; 3042 case 'T': 3043 get_timestamp_arg(*optarg); 3044 break; 3045 case 'v': 3046 cb.cb_verbose = B_TRUE; 3047 break; 3048 case ':': 3049 (void) fprintf(stderr, gettext("missing argument for " 3050 "'%c' option\n"), optopt); 3051 usage(B_FALSE); 3052 break; 3053 case '?': 3054 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3055 optopt); 3056 usage(B_FALSE); 3057 } 3058 } 3059 3060 argc -= optind; 3061 argv += optind; 3062 3063 get_interval_count(&argc, argv, &interval, &count); 3064 3065 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 3066 usage(B_FALSE); 3067 3068 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL) 3069 return (1); 3070 3071 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 3072 (void) printf(gettext("no pools available\n")); 3073 zprop_free_list(cb.cb_proplist); 3074 return (0); 3075 } 3076 3077 for (;;) { 3078 pool_list_update(list); 3079 3080 if (pool_list_count(list) == 0) 3081 break; 3082 3083 cb.cb_namewidth = 0; 3084 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 3085 3086 if (timestamp_fmt != NODATE) 3087 print_timestamp(timestamp_fmt); 3088 3089 if (!cb.cb_scripted && (first || cb.cb_verbose)) { 3090 print_header(&cb); 3091 first = B_FALSE; 3092 } 3093 ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 3094 3095 if (interval == 0) 3096 break; 3097 3098 if (count != 0 && --count == 0) 3099 break; 3100 3101 (void) sleep(interval); 3102 } 3103 3104 zprop_free_list(cb.cb_proplist); 3105 return (ret); 3106} 3107 3108static nvlist_t * 3109zpool_get_vdev_by_name(nvlist_t *nv, char *name) 3110{ 3111 nvlist_t **child; 3112 uint_t c, children; 3113 nvlist_t *match; 3114 char *path; 3115 3116 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 3117 &child, &children) != 0) { 3118 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 3119 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 3120 name += sizeof(_PATH_DEV) - 1; 3121 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 3122 path += sizeof(_PATH_DEV) - 1; 3123 if (strcmp(name, path) == 0) 3124 return (nv); 3125 return (NULL); 3126 } 3127 3128 for (c = 0; c < children; c++) 3129 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL) 3130 return (match); 3131 3132 return (NULL); 3133} 3134 3135static int 3136zpool_do_attach_or_replace(int argc, char **argv, int replacing) 3137{ 3138 boolean_t force = B_FALSE; 3139 int c; 3140 nvlist_t *nvroot; 3141 char *poolname, *old_disk, *new_disk; 3142 zpool_handle_t *zhp; 3143 int ret; 3144 3145 /* check options */ 3146 while ((c = getopt(argc, argv, "f")) != -1) { 3147 switch (c) { 3148 case 'f': 3149 force = B_TRUE; 3150 break; 3151 case '?': 3152 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3153 optopt); 3154 usage(B_FALSE); 3155 } 3156 } 3157 3158 argc -= optind; 3159 argv += optind; 3160 3161 /* get pool name and check number of arguments */ 3162 if (argc < 1) { 3163 (void) fprintf(stderr, gettext("missing pool name argument\n")); 3164 usage(B_FALSE); 3165 } 3166 3167 poolname = argv[0]; 3168 3169 if (argc < 2) { 3170 (void) fprintf(stderr, 3171 gettext("missing <device> specification\n")); 3172 usage(B_FALSE); 3173 } 3174 3175 old_disk = argv[1]; 3176 3177 if (argc < 3) { 3178 if (!replacing) { 3179 (void) fprintf(stderr, 3180 gettext("missing <new_device> specification\n")); 3181 usage(B_FALSE); 3182 } 3183 new_disk = old_disk; 3184 argc -= 1; 3185 argv += 1; 3186 } else { 3187 new_disk = argv[2]; 3188 argc -= 2; 3189 argv += 2; 3190 } 3191 3192 if (argc > 1) { 3193 (void) fprintf(stderr, gettext("too many arguments\n")); 3194 usage(B_FALSE); 3195 } 3196 3197 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3198 return (1); 3199 3200 if (zpool_get_config(zhp, NULL) == NULL) { 3201 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 3202 poolname); 3203 zpool_close(zhp); 3204 return (1); 3205 } 3206 3207 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE, 3208 argc, argv); 3209 if (nvroot == NULL) { 3210 zpool_close(zhp); 3211 return (1); 3212 } 3213 3214 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); 3215 3216 nvlist_free(nvroot); 3217 zpool_close(zhp); 3218 3219 return (ret); 3220} 3221 3222/* 3223 * zpool replace [-f] <pool> <device> <new_device> 3224 * 3225 * -f Force attach, even if <new_device> appears to be in use. 3226 * 3227 * Replace <device> with <new_device>. 3228 */ 3229/* ARGSUSED */ 3230int 3231zpool_do_replace(int argc, char **argv) 3232{ 3233 return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 3234} 3235 3236/* 3237 * zpool attach [-f] <pool> <device> <new_device> 3238 * 3239 * -f Force attach, even if <new_device> appears to be in use. 3240 * 3241 * Attach <new_device> to the mirror containing <device>. If <device> is not 3242 * part of a mirror, then <device> will be transformed into a mirror of 3243 * <device> and <new_device>. In either case, <new_device> will begin life 3244 * with a DTL of [0, now], and will immediately begin to resilver itself. 3245 */ 3246int 3247zpool_do_attach(int argc, char **argv) 3248{ 3249 return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 3250} 3251 3252/* 3253 * zpool detach [-f] <pool> <device> 3254 * 3255 * -f Force detach of <device>, even if DTLs argue against it 3256 * (not supported yet) 3257 * 3258 * Detach a device from a mirror. The operation will be refused if <device> 3259 * is the last device in the mirror, or if the DTLs indicate that this device 3260 * has the only valid copy of some data. 3261 */ 3262/* ARGSUSED */ 3263int 3264zpool_do_detach(int argc, char **argv) 3265{ 3266 int c; 3267 char *poolname, *path; 3268 zpool_handle_t *zhp; 3269 int ret; 3270 3271 /* check options */ 3272 while ((c = getopt(argc, argv, "f")) != -1) { 3273 switch (c) { 3274 case 'f': 3275 case '?': 3276 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3277 optopt); 3278 usage(B_FALSE); 3279 } 3280 } 3281 3282 argc -= optind; 3283 argv += optind; 3284 3285 /* get pool name and check number of arguments */ 3286 if (argc < 1) { 3287 (void) fprintf(stderr, gettext("missing pool name argument\n")); 3288 usage(B_FALSE); 3289 } 3290 3291 if (argc < 2) { 3292 (void) fprintf(stderr, 3293 gettext("missing <device> specification\n")); 3294 usage(B_FALSE); 3295 } 3296 3297 poolname = argv[0]; 3298 path = argv[1]; 3299 3300 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3301 return (1); 3302 3303 ret = zpool_vdev_detach(zhp, path); 3304 3305 zpool_close(zhp); 3306 3307 return (ret); 3308} 3309 3310/* 3311 * zpool split [-n] [-o prop=val] ... 3312 * [-o mntopt] ... 3313 * [-R altroot] <pool> <newpool> [<device> ...] 3314 * 3315 * -n Do not split the pool, but display the resulting layout if 3316 * it were to be split. 3317 * -o Set property=value, or set mount options. 3318 * -R Mount the split-off pool under an alternate root. 3319 * 3320 * Splits the named pool and gives it the new pool name. Devices to be split 3321 * off may be listed, provided that no more than one device is specified 3322 * per top-level vdev mirror. The newly split pool is left in an exported 3323 * state unless -R is specified. 3324 * 3325 * Restrictions: the top-level of the pool pool must only be made up of 3326 * mirrors; all devices in the pool must be healthy; no device may be 3327 * undergoing a resilvering operation. 3328 */ 3329int 3330zpool_do_split(int argc, char **argv) 3331{ 3332 char *srcpool, *newpool, *propval; 3333 char *mntopts = NULL; 3334 splitflags_t flags; 3335 int c, ret = 0; 3336 zpool_handle_t *zhp; 3337 nvlist_t *config, *props = NULL; 3338 3339 flags.dryrun = B_FALSE; 3340 flags.import = B_FALSE; 3341 3342 /* check options */ 3343 while ((c = getopt(argc, argv, ":R:no:")) != -1) { 3344 switch (c) { 3345 case 'R': 3346 flags.import = B_TRUE; 3347 if (add_prop_list( 3348 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 3349 &props, B_TRUE) != 0) { 3350 if (props) 3351 nvlist_free(props); 3352 usage(B_FALSE); 3353 } 3354 break; 3355 case 'n': 3356 flags.dryrun = B_TRUE; 3357 break; 3358 case 'o': 3359 if ((propval = strchr(optarg, '=')) != NULL) { 3360 *propval = '\0'; 3361 propval++; 3362 if (add_prop_list(optarg, propval, 3363 &props, B_TRUE) != 0) { 3364 if (props) 3365 nvlist_free(props); 3366 usage(B_FALSE); 3367 } 3368 } else { 3369 mntopts = optarg; 3370 } 3371 break; 3372 case ':': 3373 (void) fprintf(stderr, gettext("missing argument for " 3374 "'%c' option\n"), optopt); 3375 usage(B_FALSE); 3376 break; 3377 case '?': 3378 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3379 optopt); 3380 usage(B_FALSE); 3381 break; 3382 } 3383 } 3384 3385 if (!flags.import && mntopts != NULL) { 3386 (void) fprintf(stderr, gettext("setting mntopts is only " 3387 "valid when importing the pool\n")); 3388 usage(B_FALSE); 3389 } 3390 3391 argc -= optind; 3392 argv += optind; 3393 3394 if (argc < 1) { 3395 (void) fprintf(stderr, gettext("Missing pool name\n")); 3396 usage(B_FALSE); 3397 } 3398 if (argc < 2) { 3399 (void) fprintf(stderr, gettext("Missing new pool name\n")); 3400 usage(B_FALSE); 3401 } 3402 3403 srcpool = argv[0]; 3404 newpool = argv[1]; 3405 3406 argc -= 2; 3407 argv += 2; 3408 3409 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) 3410 return (1); 3411 3412 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 3413 if (config == NULL) { 3414 ret = 1; 3415 } else { 3416 if (flags.dryrun) { 3417 (void) printf(gettext("would create '%s' with the " 3418 "following layout:\n\n"), newpool); 3419 print_vdev_tree(NULL, newpool, config, 0, B_FALSE); 3420 } 3421 nvlist_free(config); 3422 } 3423 3424 zpool_close(zhp); 3425 3426 if (ret != 0 || flags.dryrun || !flags.import) 3427 return (ret); 3428 3429 /* 3430 * The split was successful. Now we need to open the new 3431 * pool and import it. 3432 */ 3433 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) 3434 return (1); 3435 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3436 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 3437 ret = 1; 3438 (void) fprintf(stderr, gettext("Split was successful, but " 3439 "the datasets could not all be mounted\n")); 3440 (void) fprintf(stderr, gettext("Try doing '%s' with a " 3441 "different altroot\n"), "zpool import"); 3442 } 3443 zpool_close(zhp); 3444 3445 return (ret); 3446} 3447 3448 3449 3450/* 3451 * zpool online <pool> <device> ... 3452 */ 3453int 3454zpool_do_online(int argc, char **argv) 3455{ 3456 int c, i; 3457 char *poolname; 3458 zpool_handle_t *zhp; 3459 int ret = 0; 3460 vdev_state_t newstate; 3461 int flags = 0; 3462 3463 /* check options */ 3464 while ((c = getopt(argc, argv, "et")) != -1) { 3465 switch (c) { 3466 case 'e': 3467 flags |= ZFS_ONLINE_EXPAND; 3468 break; 3469 case 't': 3470 case '?': 3471 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3472 optopt); 3473 usage(B_FALSE); 3474 } 3475 } 3476 3477 argc -= optind; 3478 argv += optind; 3479 3480 /* get pool name and check number of arguments */ 3481 if (argc < 1) { 3482 (void) fprintf(stderr, gettext("missing pool name\n")); 3483 usage(B_FALSE); 3484 } 3485 if (argc < 2) { 3486 (void) fprintf(stderr, gettext("missing device name\n")); 3487 usage(B_FALSE); 3488 } 3489 3490 poolname = argv[0]; 3491 3492 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3493 return (1); 3494 3495 for (i = 1; i < argc; i++) { 3496 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 3497 if (newstate != VDEV_STATE_HEALTHY) { 3498 (void) printf(gettext("warning: device '%s' " 3499 "onlined, but remains in faulted state\n"), 3500 argv[i]); 3501 if (newstate == VDEV_STATE_FAULTED) 3502 (void) printf(gettext("use 'zpool " 3503 "clear' to restore a faulted " 3504 "device\n")); 3505 else 3506 (void) printf(gettext("use 'zpool " 3507 "replace' to replace devices " 3508 "that are no longer present\n")); 3509 } 3510 } else { 3511 ret = 1; 3512 } 3513 } 3514 3515 zpool_close(zhp); 3516 3517 return (ret); 3518} 3519 3520/* 3521 * zpool offline [-ft] <pool> <device> ... 3522 * 3523 * -f Force the device into the offline state, even if doing 3524 * so would appear to compromise pool availability. 3525 * (not supported yet) 3526 * 3527 * -t Only take the device off-line temporarily. The offline 3528 * state will not be persistent across reboots. 3529 */ 3530/* ARGSUSED */ 3531int 3532zpool_do_offline(int argc, char **argv) 3533{ 3534 int c, i; 3535 char *poolname; 3536 zpool_handle_t *zhp; 3537 int ret = 0; 3538 boolean_t istmp = B_FALSE; 3539 3540 /* check options */ 3541 while ((c = getopt(argc, argv, "ft")) != -1) { 3542 switch (c) { 3543 case 't': 3544 istmp = B_TRUE; 3545 break; 3546 case 'f': 3547 case '?': 3548 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3549 optopt); 3550 usage(B_FALSE); 3551 } 3552 } 3553 3554 argc -= optind; 3555 argv += optind; 3556 3557 /* get pool name and check number of arguments */ 3558 if (argc < 1) { 3559 (void) fprintf(stderr, gettext("missing pool name\n")); 3560 usage(B_FALSE); 3561 } 3562 if (argc < 2) { 3563 (void) fprintf(stderr, gettext("missing device name\n")); 3564 usage(B_FALSE); 3565 } 3566 3567 poolname = argv[0]; 3568 3569 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3570 return (1); 3571 3572 for (i = 1; i < argc; i++) { 3573 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 3574 ret = 1; 3575 } 3576 3577 zpool_close(zhp); 3578 3579 return (ret); 3580} 3581 3582/* 3583 * zpool clear <pool> [device] 3584 * 3585 * Clear all errors associated with a pool or a particular device. 3586 */ 3587int 3588zpool_do_clear(int argc, char **argv) 3589{ 3590 int c; 3591 int ret = 0; 3592 boolean_t dryrun = B_FALSE; 3593 boolean_t do_rewind = B_FALSE; 3594 boolean_t xtreme_rewind = B_FALSE; 3595 uint32_t rewind_policy = ZPOOL_NO_REWIND; 3596 nvlist_t *policy = NULL; 3597 zpool_handle_t *zhp; 3598 char *pool, *device; 3599 3600 /* check options */ 3601 while ((c = getopt(argc, argv, "FnX")) != -1) { 3602 switch (c) { 3603 case 'F': 3604 do_rewind = B_TRUE; 3605 break; 3606 case 'n': 3607 dryrun = B_TRUE; 3608 break; 3609 case 'X': 3610 xtreme_rewind = B_TRUE; 3611 break; 3612 case '?': 3613 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3614 optopt); 3615 usage(B_FALSE); 3616 } 3617 } 3618 3619 argc -= optind; 3620 argv += optind; 3621 3622 if (argc < 1) { 3623 (void) fprintf(stderr, gettext("missing pool name\n")); 3624 usage(B_FALSE); 3625 } 3626 3627 if (argc > 2) { 3628 (void) fprintf(stderr, gettext("too many arguments\n")); 3629 usage(B_FALSE); 3630 } 3631 3632 if ((dryrun || xtreme_rewind) && !do_rewind) { 3633 (void) fprintf(stderr, 3634 gettext("-n or -X only meaningful with -F\n")); 3635 usage(B_FALSE); 3636 } 3637 if (dryrun) 3638 rewind_policy = ZPOOL_TRY_REWIND; 3639 else if (do_rewind) 3640 rewind_policy = ZPOOL_DO_REWIND; 3641 if (xtreme_rewind) 3642 rewind_policy |= ZPOOL_EXTREME_REWIND; 3643 3644 /* In future, further rewind policy choices can be passed along here */ 3645 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3646 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 3647 return (1); 3648 3649 pool = argv[0]; 3650 device = argc == 2 ? argv[1] : NULL; 3651 3652 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 3653 nvlist_free(policy); 3654 return (1); 3655 } 3656 3657 if (zpool_clear(zhp, device, policy) != 0) 3658 ret = 1; 3659 3660 zpool_close(zhp); 3661 3662 nvlist_free(policy); 3663 3664 return (ret); 3665} 3666 3667/* 3668 * zpool reguid <pool> 3669 */ 3670int 3671zpool_do_reguid(int argc, char **argv) 3672{ 3673 int c; 3674 char *poolname; 3675 zpool_handle_t *zhp; 3676 int ret = 0; 3677 3678 /* check options */ 3679 while ((c = getopt(argc, argv, "")) != -1) { 3680 switch (c) { 3681 case '?': 3682 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3683 optopt); 3684 usage(B_FALSE); 3685 } 3686 } 3687 3688 argc -= optind; 3689 argv += optind; 3690 3691 /* get pool name and check number of arguments */ 3692 if (argc < 1) { 3693 (void) fprintf(stderr, gettext("missing pool name\n")); 3694 usage(B_FALSE); 3695 } 3696 3697 if (argc > 1) { 3698 (void) fprintf(stderr, gettext("too many arguments\n")); 3699 usage(B_FALSE); 3700 } 3701 3702 poolname = argv[0]; 3703 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3704 return (1); 3705 3706 ret = zpool_reguid(zhp); 3707 3708 zpool_close(zhp); 3709 return (ret); 3710} 3711 3712 3713/* 3714 * zpool reopen <pool> 3715 * 3716 * Reopen the pool so that the kernel can update the sizes of all vdevs. 3717 * 3718 * NOTE: This command is currently undocumented. If the command is ever 3719 * exposed then the appropriate usage() messages will need to be made. 3720 */ 3721int 3722zpool_do_reopen(int argc, char **argv) 3723{ 3724 int ret = 0; 3725 zpool_handle_t *zhp; 3726 char *pool; 3727 3728 argc--; 3729 argv++; 3730 3731 if (argc != 1) 3732 return (2); 3733 3734 pool = argv[0]; 3735 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) 3736 return (1); 3737 3738 ret = zpool_reopen(zhp); 3739 zpool_close(zhp); 3740 return (ret); 3741} 3742 3743typedef struct scrub_cbdata { 3744 int cb_type; 3745 int cb_argc; 3746 char **cb_argv; 3747} scrub_cbdata_t; 3748 3749int 3750scrub_callback(zpool_handle_t *zhp, void *data) 3751{ 3752 scrub_cbdata_t *cb = data; 3753 int err; 3754 3755 /* 3756 * Ignore faulted pools. 3757 */ 3758 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 3759 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " 3760 "currently unavailable\n"), zpool_get_name(zhp)); 3761 return (1); 3762 } 3763 3764 err = zpool_scan(zhp, cb->cb_type); 3765 3766 return (err != 0); 3767} 3768 3769/* 3770 * zpool scrub [-s] <pool> ... 3771 * 3772 * -s Stop. Stops any in-progress scrub. 3773 */ 3774int 3775zpool_do_scrub(int argc, char **argv) 3776{ 3777 int c; 3778 scrub_cbdata_t cb; 3779 3780 cb.cb_type = POOL_SCAN_SCRUB; 3781 3782 /* check options */ 3783 while ((c = getopt(argc, argv, "s")) != -1) { 3784 switch (c) { 3785 case 's': 3786 cb.cb_type = POOL_SCAN_NONE; 3787 break; 3788 case '?': 3789 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3790 optopt); 3791 usage(B_FALSE); 3792 } 3793 } 3794 3795 cb.cb_argc = argc; 3796 cb.cb_argv = argv; 3797 argc -= optind; 3798 argv += optind; 3799 3800 if (argc < 1) { 3801 (void) fprintf(stderr, gettext("missing pool name argument\n")); 3802 usage(B_FALSE); 3803 } 3804 3805 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); 3806} 3807 3808typedef struct status_cbdata { 3809 int cb_count; 3810 boolean_t cb_allpools; 3811 boolean_t cb_verbose; 3812 boolean_t cb_explain; 3813 boolean_t cb_first; 3814 boolean_t cb_dedup_stats; 3815} status_cbdata_t; 3816 3817/* 3818 * Print out detailed scrub status. 3819 */ 3820void 3821print_scan_status(pool_scan_stat_t *ps) 3822{ 3823 time_t start, end; 3824 uint64_t elapsed, mins_left, hours_left; 3825 uint64_t pass_exam, examined, total; 3826 uint_t rate; 3827 double fraction_done; 3828 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 3829 3830 (void) printf(gettext(" scan: ")); 3831 3832 /* If there's never been a scan, there's not much to say. */ 3833 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 3834 ps->pss_func >= POOL_SCAN_FUNCS) { 3835 (void) printf(gettext("none requested\n")); 3836 return; 3837 } 3838 3839 start = ps->pss_start_time; 3840 end = ps->pss_end_time; 3841 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); 3842 3843 assert(ps->pss_func == POOL_SCAN_SCRUB || 3844 ps->pss_func == POOL_SCAN_RESILVER); 3845 /* 3846 * Scan is finished or canceled. 3847 */ 3848 if (ps->pss_state == DSS_FINISHED) { 3849 uint64_t minutes_taken = (end - start) / 60; 3850 char *fmt; 3851 3852 if (ps->pss_func == POOL_SCAN_SCRUB) { 3853 fmt = gettext("scrub repaired %s in %lluh%um with " 3854 "%llu errors on %s"); 3855 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3856 fmt = gettext("resilvered %s in %lluh%um with " 3857 "%llu errors on %s"); 3858 } 3859 /* LINTED */ 3860 (void) printf(fmt, processed_buf, 3861 (u_longlong_t)(minutes_taken / 60), 3862 (uint_t)(minutes_taken % 60), 3863 (u_longlong_t)ps->pss_errors, 3864 ctime((time_t *)&end)); 3865 return; 3866 } else if (ps->pss_state == DSS_CANCELED) { 3867 if (ps->pss_func == POOL_SCAN_SCRUB) { 3868 (void) printf(gettext("scrub canceled on %s"), 3869 ctime(&end)); 3870 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3871 (void) printf(gettext("resilver canceled on %s"), 3872 ctime(&end)); 3873 } 3874 return; 3875 } 3876 3877 assert(ps->pss_state == DSS_SCANNING); 3878 3879 /* 3880 * Scan is in progress. 3881 */ 3882 if (ps->pss_func == POOL_SCAN_SCRUB) { 3883 (void) printf(gettext("scrub in progress since %s"), 3884 ctime(&start)); 3885 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3886 (void) printf(gettext("resilver in progress since %s"), 3887 ctime(&start)); 3888 } 3889 3890 examined = ps->pss_examined ? ps->pss_examined : 1; 3891 total = ps->pss_to_examine; 3892 fraction_done = (double)examined / total; 3893 3894 /* elapsed time for this pass */ 3895 elapsed = time(NULL) - ps->pss_pass_start; 3896 elapsed = elapsed ? elapsed : 1; 3897 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; 3898 rate = pass_exam / elapsed; 3899 rate = rate ? rate : 1; 3900 mins_left = ((total - examined) / rate) / 60; 3901 hours_left = mins_left / 60; 3902 3903 zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); 3904 zfs_nicenum(total, total_buf, sizeof (total_buf)); 3905 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 3906 3907 /* 3908 * do not print estimated time if hours_left is more than 30 days 3909 */ 3910 (void) printf(gettext(" %s scanned out of %s at %s/s"), 3911 examined_buf, total_buf, rate_buf); 3912 if (hours_left < (30 * 24)) { 3913 (void) printf(gettext(", %lluh%um to go\n"), 3914 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 3915 } else { 3916 (void) printf(gettext( 3917 ", (scan is slow, no estimated time)\n")); 3918 } 3919 3920 if (ps->pss_func == POOL_SCAN_RESILVER) { 3921 (void) printf(gettext(" %s resilvered, %.2f%% done\n"), 3922 processed_buf, 100 * fraction_done); 3923 } else if (ps->pss_func == POOL_SCAN_SCRUB) { 3924 (void) printf(gettext(" %s repaired, %.2f%% done\n"), 3925 processed_buf, 100 * fraction_done); 3926 } 3927} 3928 3929static void 3930print_error_log(zpool_handle_t *zhp) 3931{ 3932 nvlist_t *nverrlist = NULL; 3933 nvpair_t *elem; 3934 char *pathname; 3935 size_t len = MAXPATHLEN * 2; 3936 3937 if (zpool_get_errlog(zhp, &nverrlist) != 0) { 3938 (void) printf("errors: List of errors unavailable " 3939 "(insufficient privileges)\n"); 3940 return; 3941 } 3942 3943 (void) printf("errors: Permanent errors have been " 3944 "detected in the following files:\n\n"); 3945 3946 pathname = safe_malloc(len); 3947 elem = NULL; 3948 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 3949 nvlist_t *nv; 3950 uint64_t dsobj, obj; 3951 3952 verify(nvpair_value_nvlist(elem, &nv) == 0); 3953 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 3954 &dsobj) == 0); 3955 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 3956 &obj) == 0); 3957 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 3958 (void) printf("%7s %s\n", "", pathname); 3959 } 3960 free(pathname); 3961 nvlist_free(nverrlist); 3962} 3963 3964static void 3965print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, 3966 int namewidth) 3967{ 3968 uint_t i; 3969 char *name; 3970 3971 if (nspares == 0) 3972 return; 3973 3974 (void) printf(gettext("\tspares\n")); 3975 3976 for (i = 0; i < nspares; i++) { 3977 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); 3978 print_status_config(zhp, name, spares[i], 3979 namewidth, 2, B_TRUE); 3980 free(name); 3981 } 3982} 3983 3984static void 3985print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, 3986 int namewidth) 3987{ 3988 uint_t i; 3989 char *name; 3990 3991 if (nl2cache == 0) 3992 return; 3993 3994 (void) printf(gettext("\tcache\n")); 3995 3996 for (i = 0; i < nl2cache; i++) { 3997 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); 3998 print_status_config(zhp, name, l2cache[i], 3999 namewidth, 2, B_FALSE); 4000 free(name); 4001 } 4002} 4003 4004static void 4005print_dedup_stats(nvlist_t *config) 4006{ 4007 ddt_histogram_t *ddh; 4008 ddt_stat_t *dds; 4009 ddt_object_t *ddo; 4010 uint_t c; 4011 4012 /* 4013 * If the pool was faulted then we may not have been able to 4014 * obtain the config. Otherwise, if we have anything in the dedup 4015 * table continue processing the stats. 4016 */ 4017 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 4018 (uint64_t **)&ddo, &c) != 0) 4019 return; 4020 4021 (void) printf("\n"); 4022 (void) printf(gettext(" dedup: ")); 4023 if (ddo->ddo_count == 0) { 4024 (void) printf(gettext("no DDT entries\n")); 4025 return; 4026 } 4027 4028 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", 4029 (u_longlong_t)ddo->ddo_count, 4030 (u_longlong_t)ddo->ddo_dspace, 4031 (u_longlong_t)ddo->ddo_mspace); 4032 4033 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 4034 (uint64_t **)&dds, &c) == 0); 4035 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 4036 (uint64_t **)&ddh, &c) == 0); 4037 zpool_dump_ddt(dds, ddh); 4038} 4039 4040/* 4041 * Display a summary of pool status. Displays a summary such as: 4042 * 4043 * pool: tank 4044 * status: DEGRADED 4045 * reason: One or more devices ... 4046 * see: http://illumos.org/msg/ZFS-xxxx-01 4047 * config: 4048 * mirror DEGRADED 4049 * c1t0d0 OK 4050 * c2t0d0 UNAVAIL 4051 * 4052 * When given the '-v' option, we print out the complete config. If the '-e' 4053 * option is specified, then we print out error rate information as well. 4054 */ 4055int 4056status_callback(zpool_handle_t *zhp, void *data) 4057{ 4058 status_cbdata_t *cbp = data; 4059 nvlist_t *config, *nvroot; 4060 char *msgid; 4061 int reason; 4062 const char *health; 4063 uint_t c; 4064 vdev_stat_t *vs; 4065 4066 config = zpool_get_config(zhp, NULL); 4067 reason = zpool_get_status(zhp, &msgid); 4068 4069 cbp->cb_count++; 4070 4071 /* 4072 * If we were given 'zpool status -x', only report those pools with 4073 * problems. 4074 */ 4075 if (cbp->cb_explain && 4076 (reason == ZPOOL_STATUS_OK || 4077 reason == ZPOOL_STATUS_VERSION_OLDER || 4078 reason == ZPOOL_STATUS_FEAT_DISABLED)) { 4079 if (!cbp->cb_allpools) { 4080 (void) printf(gettext("pool '%s' is healthy\n"), 4081 zpool_get_name(zhp)); 4082 if (cbp->cb_first) 4083 cbp->cb_first = B_FALSE; 4084 } 4085 return (0); 4086 } 4087 4088 if (cbp->cb_first) 4089 cbp->cb_first = B_FALSE; 4090 else 4091 (void) printf("\n"); 4092 4093 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4094 &nvroot) == 0); 4095 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 4096 (uint64_t **)&vs, &c) == 0); 4097 health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 4098 4099 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); 4100 (void) printf(gettext(" state: %s\n"), health); 4101 4102 switch (reason) { 4103 case ZPOOL_STATUS_MISSING_DEV_R: 4104 (void) printf(gettext("status: One or more devices could not " 4105 "be opened. Sufficient replicas exist for\n\tthe pool to " 4106 "continue functioning in a degraded state.\n")); 4107 (void) printf(gettext("action: Attach the missing device and " 4108 "online it using 'zpool online'.\n")); 4109 break; 4110 4111 case ZPOOL_STATUS_MISSING_DEV_NR: 4112 (void) printf(gettext("status: One or more devices could not " 4113 "be opened. There are insufficient\n\treplicas for the " 4114 "pool to continue functioning.\n")); 4115 (void) printf(gettext("action: Attach the missing device and " 4116 "online it using 'zpool online'.\n")); 4117 break; 4118 4119 case ZPOOL_STATUS_CORRUPT_LABEL_R: 4120 (void) printf(gettext("status: One or more devices could not " 4121 "be used because the label is missing or\n\tinvalid. " 4122 "Sufficient replicas exist for the pool to continue\n\t" 4123 "functioning in a degraded state.\n")); 4124 (void) printf(gettext("action: Replace the device using " 4125 "'zpool replace'.\n")); 4126 break; 4127 4128 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 4129 (void) printf(gettext("status: One or more devices could not " 4130 "be used because the label is missing \n\tor invalid. " 4131 "There are insufficient replicas for the pool to " 4132 "continue\n\tfunctioning.\n")); 4133 zpool_explain_recover(zpool_get_handle(zhp), 4134 zpool_get_name(zhp), reason, config); 4135 break; 4136 4137 case ZPOOL_STATUS_FAILING_DEV: 4138 (void) printf(gettext("status: One or more devices has " 4139 "experienced an unrecoverable error. An\n\tattempt was " 4140 "made to correct the error. Applications are " 4141 "unaffected.\n")); 4142 (void) printf(gettext("action: Determine if the device needs " 4143 "to be replaced, and clear the errors\n\tusing " 4144 "'zpool clear' or replace the device with 'zpool " 4145 "replace'.\n")); 4146 break; 4147 4148 case ZPOOL_STATUS_OFFLINE_DEV: 4149 (void) printf(gettext("status: One or more devices has " 4150 "been taken offline by the administrator.\n\tSufficient " 4151 "replicas exist for the pool to continue functioning in " 4152 "a\n\tdegraded state.\n")); 4153 (void) printf(gettext("action: Online the device using " 4154 "'zpool online' or replace the device with\n\t'zpool " 4155 "replace'.\n")); 4156 break; 4157 4158 case ZPOOL_STATUS_REMOVED_DEV: 4159 (void) printf(gettext("status: One or more devices has " 4160 "been removed by the administrator.\n\tSufficient " 4161 "replicas exist for the pool to continue functioning in " 4162 "a\n\tdegraded state.\n")); 4163 (void) printf(gettext("action: Online the device using " 4164 "'zpool online' or replace the device with\n\t'zpool " 4165 "replace'.\n")); 4166 break; 4167 4168 case ZPOOL_STATUS_RESILVERING: 4169 (void) printf(gettext("status: One or more devices is " 4170 "currently being resilvered. The pool will\n\tcontinue " 4171 "to function, possibly in a degraded state.\n")); 4172 (void) printf(gettext("action: Wait for the resilver to " 4173 "complete.\n")); 4174 break; 4175 4176 case ZPOOL_STATUS_CORRUPT_DATA: 4177 (void) printf(gettext("status: One or more devices has " 4178 "experienced an error resulting in data\n\tcorruption. " 4179 "Applications may be affected.\n")); 4180 (void) printf(gettext("action: Restore the file in question " 4181 "if possible. Otherwise restore the\n\tentire pool from " 4182 "backup.\n")); 4183 break; 4184 4185 case ZPOOL_STATUS_CORRUPT_POOL: 4186 (void) printf(gettext("status: The pool metadata is corrupted " 4187 "and the pool cannot be opened.\n")); 4188 zpool_explain_recover(zpool_get_handle(zhp), 4189 zpool_get_name(zhp), reason, config); 4190 break; 4191 4192 case ZPOOL_STATUS_VERSION_OLDER: 4193 (void) printf(gettext("status: The pool is formatted using a " 4194 "legacy on-disk format. The pool can\n\tstill be used, " 4195 "but some features are unavailable.\n")); 4196 (void) printf(gettext("action: Upgrade the pool using 'zpool " 4197 "upgrade'. Once this is done, the\n\tpool will no longer " 4198 "be accessible on software that does not support feature\n" 4199 "\tflags.\n")); 4200 break; 4201 4202 case ZPOOL_STATUS_VERSION_NEWER: 4203 (void) printf(gettext("status: The pool has been upgraded to a " 4204 "newer, incompatible on-disk version.\n\tThe pool cannot " 4205 "be accessed on this system.\n")); 4206 (void) printf(gettext("action: Access the pool from a system " 4207 "running more recent software, or\n\trestore the pool from " 4208 "backup.\n")); 4209 break; 4210 4211 case ZPOOL_STATUS_FEAT_DISABLED: 4212 (void) printf(gettext("status: Some supported features are not " 4213 "enabled on the pool. The pool can\n\tstill be used, but " 4214 "some features are unavailable.\n")); 4215 (void) printf(gettext("action: Enable all features using " 4216 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 4217 "longer be accessible by software that does not support\n\t" 4218 "the features. See zpool-features(7) for details.\n")); 4219 break; 4220 4221 case ZPOOL_STATUS_UNSUP_FEAT_READ: 4222 (void) printf(gettext("status: The pool cannot be accessed on " 4223 "this system because it uses the\n\tfollowing feature(s) " 4224 "not supported on this system:\n")); 4225 zpool_print_unsup_feat(config); 4226 (void) printf("\n"); 4227 (void) printf(gettext("action: Access the pool from a system " 4228 "that supports the required feature(s),\n\tor restore the " 4229 "pool from backup.\n")); 4230 break; 4231 4232 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 4233 (void) printf(gettext("status: The pool can only be accessed " 4234 "in read-only mode on this system. It\n\tcannot be " 4235 "accessed in read-write mode because it uses the " 4236 "following\n\tfeature(s) not supported on this system:\n")); 4237 zpool_print_unsup_feat(config); 4238 (void) printf("\n"); 4239 (void) printf(gettext("action: The pool cannot be accessed in " 4240 "read-write mode. Import the pool with\n" 4241 "\t\"-o readonly=on\", access the pool from a system that " 4242 "supports the\n\trequired feature(s), or restore the " 4243 "pool from backup.\n")); 4244 break; 4245 4246 case ZPOOL_STATUS_FAULTED_DEV_R: 4247 (void) printf(gettext("status: One or more devices are " 4248 "faulted in response to persistent errors.\n\tSufficient " 4249 "replicas exist for the pool to continue functioning " 4250 "in a\n\tdegraded state.\n")); 4251 (void) printf(gettext("action: Replace the faulted device, " 4252 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 4253 break; 4254 4255 case ZPOOL_STATUS_FAULTED_DEV_NR: 4256 (void) printf(gettext("status: One or more devices are " 4257 "faulted in response to persistent errors. There are " 4258 "insufficient replicas for the pool to\n\tcontinue " 4259 "functioning.\n")); 4260 (void) printf(gettext("action: Destroy and re-create the pool " 4261 "from a backup source. Manually marking the device\n" 4262 "\trepaired using 'zpool clear' may allow some data " 4263 "to be recovered.\n")); 4264 break; 4265 4266 case ZPOOL_STATUS_IO_FAILURE_WAIT: 4267 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 4268 (void) printf(gettext("status: One or more devices are " 4269 "faulted in response to IO failures.\n")); 4270 (void) printf(gettext("action: Make sure the affected devices " 4271 "are connected, then run 'zpool clear'.\n")); 4272 break; 4273 4274 case ZPOOL_STATUS_BAD_LOG: 4275 (void) printf(gettext("status: An intent log record " 4276 "could not be read.\n" 4277 "\tWaiting for adminstrator intervention to fix the " 4278 "faulted pool.\n")); 4279 (void) printf(gettext("action: Either restore the affected " 4280 "device(s) and run 'zpool online',\n" 4281 "\tor ignore the intent log records by running " 4282 "'zpool clear'.\n")); 4283 break; 4284 4285 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 4286 (void) printf(gettext("status: One or more devices are " 4287 "configured to use a non-native block size.\n" 4288 "\tExpect reduced performance.\n")); 4289 (void) printf(gettext("action: Replace affected devices with " 4290 "devices that support the\n\tconfigured block size, or " 4291 "migrate data to a properly configured\n\tpool.\n")); 4292 break; 4293 4294 default: 4295 /* 4296 * The remaining errors can't actually be generated, yet. 4297 */ 4298 assert(reason == ZPOOL_STATUS_OK); 4299 } 4300 4301 if (msgid != NULL) 4302 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 4303 msgid); 4304 4305 if (config != NULL) { 4306 int namewidth; 4307 uint64_t nerr; 4308 nvlist_t **spares, **l2cache; 4309 uint_t nspares, nl2cache; 4310 pool_scan_stat_t *ps = NULL; 4311 4312 (void) nvlist_lookup_uint64_array(nvroot, 4313 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); 4314 print_scan_status(ps); 4315 4316 namewidth = max_width(zhp, nvroot, 0, 0); 4317 if (namewidth < 10) 4318 namewidth = 10; 4319 4320 (void) printf(gettext("config:\n\n")); 4321 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, 4322 "NAME", "STATE", "READ", "WRITE", "CKSUM"); 4323 print_status_config(zhp, zpool_get_name(zhp), nvroot, 4324 namewidth, 0, B_FALSE); 4325 4326 if (num_logs(nvroot) > 0) 4327 print_logs(zhp, nvroot, namewidth, B_TRUE); 4328 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4329 &l2cache, &nl2cache) == 0) 4330 print_l2cache(zhp, l2cache, nl2cache, namewidth); 4331 4332 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 4333 &spares, &nspares) == 0) 4334 print_spares(zhp, spares, nspares, namewidth); 4335 4336 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 4337 &nerr) == 0) { 4338 nvlist_t *nverrlist = NULL; 4339 4340 /* 4341 * If the approximate error count is small, get a 4342 * precise count by fetching the entire log and 4343 * uniquifying the results. 4344 */ 4345 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 4346 zpool_get_errlog(zhp, &nverrlist) == 0) { 4347 nvpair_t *elem; 4348 4349 elem = NULL; 4350 nerr = 0; 4351 while ((elem = nvlist_next_nvpair(nverrlist, 4352 elem)) != NULL) { 4353 nerr++; 4354 } 4355 } 4356 nvlist_free(nverrlist); 4357 4358 (void) printf("\n"); 4359 4360 if (nerr == 0) 4361 (void) printf(gettext("errors: No known data " 4362 "errors\n")); 4363 else if (!cbp->cb_verbose) 4364 (void) printf(gettext("errors: %llu data " 4365 "errors, use '-v' for a list\n"), 4366 (u_longlong_t)nerr); 4367 else 4368 print_error_log(zhp); 4369 } 4370 4371 if (cbp->cb_dedup_stats) 4372 print_dedup_stats(config); 4373 } else { 4374 (void) printf(gettext("config: The configuration cannot be " 4375 "determined.\n")); 4376 } 4377 4378 return (0); 4379} 4380 4381/* 4382 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] 4383 * 4384 * -v Display complete error logs 4385 * -x Display only pools with potential problems 4386 * -D Display dedup status (undocumented) 4387 * -T Display a timestamp in date(1) or Unix format 4388 * 4389 * Describes the health status of all pools or some subset. 4390 */ 4391int 4392zpool_do_status(int argc, char **argv) 4393{ 4394 int c; 4395 int ret; 4396 unsigned long interval = 0, count = 0; 4397 status_cbdata_t cb = { 0 }; 4398 4399 /* check options */ 4400 while ((c = getopt(argc, argv, "vxDT:")) != -1) { 4401 switch (c) { 4402 case 'v': 4403 cb.cb_verbose = B_TRUE; 4404 break; 4405 case 'x': 4406 cb.cb_explain = B_TRUE; 4407 break; 4408 case 'D': 4409 cb.cb_dedup_stats = B_TRUE; 4410 break; 4411 case 'T': 4412 get_timestamp_arg(*optarg); 4413 break; 4414 case '?': 4415 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4416 optopt); 4417 usage(B_FALSE); 4418 } 4419 } 4420 4421 argc -= optind; 4422 argv += optind; 4423 4424 get_interval_count(&argc, argv, &interval, &count); 4425 4426 if (argc == 0) 4427 cb.cb_allpools = B_TRUE; 4428 4429 cb.cb_first = B_TRUE; 4430 4431 for (;;) { 4432 if (timestamp_fmt != NODATE) 4433 print_timestamp(timestamp_fmt); 4434 4435 ret = for_each_pool(argc, argv, B_TRUE, NULL, 4436 status_callback, &cb); 4437 4438 if (argc == 0 && cb.cb_count == 0) 4439 (void) printf(gettext("no pools available\n")); 4440 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 4441 (void) printf(gettext("all pools are healthy\n")); 4442 4443 if (ret != 0) 4444 return (ret); 4445 4446 if (interval == 0) 4447 break; 4448 4449 if (count != 0 && --count == 0) 4450 break; 4451 4452 (void) sleep(interval); 4453 } 4454 4455 return (0); 4456} 4457 4458typedef struct upgrade_cbdata { 4459 int cb_first; 4460 char cb_poolname[ZPOOL_MAXNAMELEN]; 4461 int cb_argc; 4462 uint64_t cb_version; 4463 char **cb_argv; 4464} upgrade_cbdata_t; 4465 4466#ifdef __FreeBSD__ 4467static int 4468is_root_pool(zpool_handle_t *zhp) 4469{ 4470 static struct statfs sfs; 4471 static char *poolname = NULL; 4472 static boolean_t stated = B_FALSE; 4473 char *slash; 4474 4475 if (!stated) { 4476 stated = B_TRUE; 4477 if (statfs("/", &sfs) == -1) { 4478 (void) fprintf(stderr, 4479 "Unable to stat root file system: %s.\n", 4480 strerror(errno)); 4481 return (0); 4482 } 4483 if (strcmp(sfs.f_fstypename, "zfs") != 0) 4484 return (0); 4485 poolname = sfs.f_mntfromname; 4486 if ((slash = strchr(poolname, '/')) != NULL) 4487 *slash = '\0'; 4488 } 4489 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0); 4490} 4491 4492static void 4493root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) { 4494 4495 if (poolname[0] == '\0' && is_root_pool(zhp)) 4496 (void) strlcpy(poolname, zpool_get_name(zhp), size); 4497} 4498#endif /* FreeBSD */ 4499 4500static int 4501upgrade_version(zpool_handle_t *zhp, uint64_t version) 4502{ 4503 int ret; 4504 nvlist_t *config; 4505 uint64_t oldversion; 4506 4507 config = zpool_get_config(zhp, NULL); 4508 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4509 &oldversion) == 0); 4510 4511 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 4512 assert(oldversion < version); 4513 4514 ret = zpool_upgrade(zhp, version); 4515 if (ret != 0) 4516 return (ret); 4517 4518 if (version >= SPA_VERSION_FEATURES) { 4519 (void) printf(gettext("Successfully upgraded " 4520 "'%s' from version %llu to feature flags.\n"), 4521 zpool_get_name(zhp), oldversion); 4522 } else { 4523 (void) printf(gettext("Successfully upgraded " 4524 "'%s' from version %llu to version %llu.\n"), 4525 zpool_get_name(zhp), oldversion, version); 4526 } 4527 4528 return (0); 4529} 4530 4531static int 4532upgrade_enable_all(zpool_handle_t *zhp, int *countp) 4533{ 4534 int i, ret, count; 4535 boolean_t firstff = B_TRUE; 4536 nvlist_t *enabled = zpool_get_features(zhp); 4537 4538 count = 0; 4539 for (i = 0; i < SPA_FEATURES; i++) { 4540 const char *fname = spa_feature_table[i].fi_uname; 4541 const char *fguid = spa_feature_table[i].fi_guid; 4542 if (!nvlist_exists(enabled, fguid)) { 4543 char *propname; 4544 verify(-1 != asprintf(&propname, "feature@%s", fname)); 4545 ret = zpool_set_prop(zhp, propname, 4546 ZFS_FEATURE_ENABLED); 4547 if (ret != 0) { 4548 free(propname); 4549 return (ret); 4550 } 4551 count++; 4552 4553 if (firstff) { 4554 (void) printf(gettext("Enabled the " 4555 "following features on '%s':\n"), 4556 zpool_get_name(zhp)); 4557 firstff = B_FALSE; 4558 } 4559 (void) printf(gettext(" %s\n"), fname); 4560 free(propname); 4561 } 4562 } 4563 4564 if (countp != NULL) 4565 *countp = count; 4566 return (0); 4567} 4568 4569static int 4570upgrade_cb(zpool_handle_t *zhp, void *arg) 4571{ 4572 upgrade_cbdata_t *cbp = arg; 4573 nvlist_t *config; 4574 uint64_t version; 4575 boolean_t printnl = B_FALSE; 4576 int ret; 4577 4578 config = zpool_get_config(zhp, NULL); 4579 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4580 &version) == 0); 4581 4582 assert(SPA_VERSION_IS_SUPPORTED(version)); 4583 4584 if (version < cbp->cb_version) { 4585 cbp->cb_first = B_FALSE; 4586 ret = upgrade_version(zhp, cbp->cb_version); 4587 if (ret != 0) 4588 return (ret); 4589#ifdef __FreeBSD__ 4590 root_pool_upgrade_check(zhp, cbp->cb_poolname, 4591 sizeof(cbp->cb_poolname)); 4592#endif /* ___FreeBSD__ */ 4593 printnl = B_TRUE; 4594 4595#ifdef illumos 4596 /* 4597 * If they did "zpool upgrade -a", then we could 4598 * be doing ioctls to different pools. We need 4599 * to log this history once to each pool, and bypass 4600 * the normal history logging that happens in main(). 4601 */ 4602 (void) zpool_log_history(g_zfs, history_str); 4603 log_history = B_FALSE; 4604#endif 4605 } 4606 4607 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4608 int count; 4609 ret = upgrade_enable_all(zhp, &count); 4610 if (ret != 0) 4611 return (ret); 4612 4613 if (count > 0) { 4614 cbp->cb_first = B_FALSE; 4615 printnl = B_TRUE; 4616 /* 4617 * If they did "zpool upgrade -a", then we could 4618 * be doing ioctls to different pools. We need 4619 * to log this history once to each pool, and bypass 4620 * the normal history logging that happens in main(). 4621 */ 4622 (void) zpool_log_history(g_zfs, history_str); 4623 log_history = B_FALSE; 4624 } 4625 } 4626 4627 if (printnl) { 4628 (void) printf(gettext("\n")); 4629 } 4630 4631 return (0); 4632} 4633 4634static int 4635upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 4636{ 4637 upgrade_cbdata_t *cbp = arg; 4638 nvlist_t *config; 4639 uint64_t version; 4640 4641 config = zpool_get_config(zhp, NULL); 4642 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4643 &version) == 0); 4644 4645 assert(SPA_VERSION_IS_SUPPORTED(version)); 4646 4647 if (version < SPA_VERSION_FEATURES) { 4648 if (cbp->cb_first) { 4649 (void) printf(gettext("The following pools are " 4650 "formatted with legacy version numbers and can\n" 4651 "be upgraded to use feature flags. After " 4652 "being upgraded, these pools\nwill no " 4653 "longer be accessible by software that does not " 4654 "support feature\nflags.\n\n")); 4655 (void) printf(gettext("VER POOL\n")); 4656 (void) printf(gettext("--- ------------\n")); 4657 cbp->cb_first = B_FALSE; 4658 } 4659 4660 (void) printf("%2llu %s\n", (u_longlong_t)version, 4661 zpool_get_name(zhp)); 4662 } 4663 4664 return (0); 4665} 4666 4667static int 4668upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 4669{ 4670 upgrade_cbdata_t *cbp = arg; 4671 nvlist_t *config; 4672 uint64_t version; 4673 4674 config = zpool_get_config(zhp, NULL); 4675 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4676 &version) == 0); 4677 4678 if (version >= SPA_VERSION_FEATURES) { 4679 int i; 4680 boolean_t poolfirst = B_TRUE; 4681 nvlist_t *enabled = zpool_get_features(zhp); 4682 4683 for (i = 0; i < SPA_FEATURES; i++) { 4684 const char *fguid = spa_feature_table[i].fi_guid; 4685 const char *fname = spa_feature_table[i].fi_uname; 4686 if (!nvlist_exists(enabled, fguid)) { 4687 if (cbp->cb_first) { 4688 (void) printf(gettext("\nSome " 4689 "supported features are not " 4690 "enabled on the following pools. " 4691 "Once a\nfeature is enabled the " 4692 "pool may become incompatible with " 4693 "software\nthat does not support " 4694 "the feature. See " 4695 "zpool-features(7) for " 4696 "details.\n\n")); 4697 (void) printf(gettext("POOL " 4698 "FEATURE\n")); 4699 (void) printf(gettext("------" 4700 "---------\n")); 4701 cbp->cb_first = B_FALSE; 4702 } 4703 4704 if (poolfirst) { 4705 (void) printf(gettext("%s\n"), 4706 zpool_get_name(zhp)); 4707 poolfirst = B_FALSE; 4708 } 4709 4710 (void) printf(gettext(" %s\n"), fname); 4711 } 4712 } 4713 } 4714 4715 return (0); 4716} 4717 4718/* ARGSUSED */ 4719static int 4720upgrade_one(zpool_handle_t *zhp, void *data) 4721{ 4722 boolean_t printnl = B_FALSE; 4723 upgrade_cbdata_t *cbp = data; 4724 uint64_t cur_version; 4725 int ret; 4726 4727 if (strcmp("log", zpool_get_name(zhp)) == 0) { 4728 (void) printf(gettext("'log' is now a reserved word\n" 4729 "Pool 'log' must be renamed using export and import" 4730 " to upgrade.\n")); 4731 return (1); 4732 } 4733 4734 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4735 if (cur_version > cbp->cb_version) { 4736 (void) printf(gettext("Pool '%s' is already formatted " 4737 "using more current version '%llu'.\n\n"), 4738 zpool_get_name(zhp), cur_version); 4739 return (0); 4740 } 4741 4742 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 4743 (void) printf(gettext("Pool '%s' is already formatted " 4744 "using version %llu.\n\n"), zpool_get_name(zhp), 4745 cbp->cb_version); 4746 return (0); 4747 } 4748 4749 if (cur_version != cbp->cb_version) { 4750 printnl = B_TRUE; 4751 ret = upgrade_version(zhp, cbp->cb_version); 4752 if (ret != 0) 4753 return (ret); 4754#ifdef __FreeBSD__ 4755 root_pool_upgrade_check(zhp, cbp->cb_poolname, 4756 sizeof(cbp->cb_poolname)); 4757#endif /* ___FreeBSD__ */ 4758 } 4759 4760 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4761 int count = 0; 4762 ret = upgrade_enable_all(zhp, &count); 4763 if (ret != 0) 4764 return (ret); 4765 4766 if (count != 0) { 4767 printnl = B_TRUE; 4768#ifdef __FreeBSD__ 4769 root_pool_upgrade_check(zhp, cbp->cb_poolname, 4770 sizeof(cbp->cb_poolname)); 4771#endif /* __FreeBSD __*/ 4772 } else if (cur_version == SPA_VERSION) { 4773 (void) printf(gettext("Pool '%s' already has all " 4774 "supported features enabled.\n"), 4775 zpool_get_name(zhp)); 4776 } 4777 } 4778 4779 if (printnl) { 4780 (void) printf(gettext("\n")); 4781 } 4782 4783 return (0); 4784} 4785 4786/* 4787 * zpool upgrade 4788 * zpool upgrade -v 4789 * zpool upgrade [-V version] <-a | pool ...> 4790 * 4791 * With no arguments, display downrev'd ZFS pool available for upgrade. 4792 * Individual pools can be upgraded by specifying the pool, and '-a' will 4793 * upgrade all pools. 4794 */ 4795int 4796zpool_do_upgrade(int argc, char **argv) 4797{ 4798 int c; 4799 upgrade_cbdata_t cb = { 0 }; 4800 int ret = 0; 4801 boolean_t showversions = B_FALSE; 4802 boolean_t upgradeall = B_FALSE; 4803 char *end; 4804 4805 4806 /* check options */ 4807 while ((c = getopt(argc, argv, ":avV:")) != -1) { 4808 switch (c) { 4809 case 'a': 4810 upgradeall = B_TRUE; 4811 break; 4812 case 'v': 4813 showversions = B_TRUE; 4814 break; 4815 case 'V': 4816 cb.cb_version = strtoll(optarg, &end, 10); 4817 if (*end != '\0' || 4818 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 4819 (void) fprintf(stderr, 4820 gettext("invalid version '%s'\n"), optarg); 4821 usage(B_FALSE); 4822 } 4823 break; 4824 case ':': 4825 (void) fprintf(stderr, gettext("missing argument for " 4826 "'%c' option\n"), optopt); 4827 usage(B_FALSE); 4828 break; 4829 case '?': 4830 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4831 optopt); 4832 usage(B_FALSE); 4833 } 4834 } 4835 4836 cb.cb_argc = argc; 4837 cb.cb_argv = argv; 4838 argc -= optind; 4839 argv += optind; 4840 4841 if (cb.cb_version == 0) { 4842 cb.cb_version = SPA_VERSION; 4843 } else if (!upgradeall && argc == 0) { 4844 (void) fprintf(stderr, gettext("-V option is " 4845 "incompatible with other arguments\n")); 4846 usage(B_FALSE); 4847 } 4848 4849 if (showversions) { 4850 if (upgradeall || argc != 0) { 4851 (void) fprintf(stderr, gettext("-v option is " 4852 "incompatible with other arguments\n")); 4853 usage(B_FALSE); 4854 } 4855 } else if (upgradeall) { 4856 if (argc != 0) { 4857 (void) fprintf(stderr, gettext("-a option should not " 4858 "be used along with a pool name\n")); 4859 usage(B_FALSE); 4860 } 4861 } 4862 4863 (void) printf(gettext("This system supports ZFS pool feature " 4864 "flags.\n\n")); 4865 if (showversions) { 4866 int i; 4867 4868 (void) printf(gettext("The following features are " 4869 "supported:\n\n")); 4870 (void) printf(gettext("FEAT DESCRIPTION\n")); 4871 (void) printf("----------------------------------------------" 4872 "---------------\n"); 4873 for (i = 0; i < SPA_FEATURES; i++) { 4874 zfeature_info_t *fi = &spa_feature_table[i]; 4875 const char *ro = fi->fi_can_readonly ? 4876 " (read-only compatible)" : ""; 4877 4878 (void) printf("%-37s%s\n", fi->fi_uname, ro); 4879 (void) printf(" %s\n", fi->fi_desc); 4880 } 4881 (void) printf("\n"); 4882 4883 (void) printf(gettext("The following legacy versions are also " 4884 "supported:\n\n")); 4885 (void) printf(gettext("VER DESCRIPTION\n")); 4886 (void) printf("--- -----------------------------------------" 4887 "---------------\n"); 4888 (void) printf(gettext(" 1 Initial ZFS version\n")); 4889 (void) printf(gettext(" 2 Ditto blocks " 4890 "(replicated metadata)\n")); 4891 (void) printf(gettext(" 3 Hot spares and double parity " 4892 "RAID-Z\n")); 4893 (void) printf(gettext(" 4 zpool history\n")); 4894 (void) printf(gettext(" 5 Compression using the gzip " 4895 "algorithm\n")); 4896 (void) printf(gettext(" 6 bootfs pool property\n")); 4897 (void) printf(gettext(" 7 Separate intent log devices\n")); 4898 (void) printf(gettext(" 8 Delegated administration\n")); 4899 (void) printf(gettext(" 9 refquota and refreservation " 4900 "properties\n")); 4901 (void) printf(gettext(" 10 Cache devices\n")); 4902 (void) printf(gettext(" 11 Improved scrub performance\n")); 4903 (void) printf(gettext(" 12 Snapshot properties\n")); 4904 (void) printf(gettext(" 13 snapused property\n")); 4905 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 4906 (void) printf(gettext(" 15 user/group space accounting\n")); 4907 (void) printf(gettext(" 16 stmf property support\n")); 4908 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 4909 (void) printf(gettext(" 18 Snapshot user holds\n")); 4910 (void) printf(gettext(" 19 Log device removal\n")); 4911 (void) printf(gettext(" 20 Compression using zle " 4912 "(zero-length encoding)\n")); 4913 (void) printf(gettext(" 21 Deduplication\n")); 4914 (void) printf(gettext(" 22 Received properties\n")); 4915 (void) printf(gettext(" 23 Slim ZIL\n")); 4916 (void) printf(gettext(" 24 System attributes\n")); 4917 (void) printf(gettext(" 25 Improved scrub stats\n")); 4918 (void) printf(gettext(" 26 Improved snapshot deletion " 4919 "performance\n")); 4920 (void) printf(gettext(" 27 Improved snapshot creation " 4921 "performance\n")); 4922 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 4923 (void) printf(gettext("\nFor more information on a particular " 4924 "version, including supported releases,\n")); 4925 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 4926 } else if (argc == 0 && upgradeall) { 4927 cb.cb_first = B_TRUE; 4928 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 4929 if (ret == 0 && cb.cb_first) { 4930 if (cb.cb_version == SPA_VERSION) { 4931 (void) printf(gettext("All pools are already " 4932 "formatted using feature flags.\n\n")); 4933 (void) printf(gettext("Every feature flags " 4934 "pool already has all supported features " 4935 "enabled.\n")); 4936 } else { 4937 (void) printf(gettext("All pools are already " 4938 "formatted with version %llu or higher.\n"), 4939 cb.cb_version); 4940 } 4941 } 4942 } else if (argc == 0) { 4943 cb.cb_first = B_TRUE; 4944 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 4945 assert(ret == 0); 4946 4947 if (cb.cb_first) { 4948 (void) printf(gettext("All pools are formatted " 4949 "using feature flags.\n\n")); 4950 } else { 4951 (void) printf(gettext("\nUse 'zpool upgrade -v' " 4952 "for a list of available legacy versions.\n")); 4953 } 4954 4955 cb.cb_first = B_TRUE; 4956 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 4957 assert(ret == 0); 4958 4959 if (cb.cb_first) { 4960 (void) printf(gettext("Every feature flags pool has " 4961 "all supported features enabled.\n")); 4962 } else { 4963 (void) printf(gettext("\n")); 4964 } 4965 } else { 4966 ret = for_each_pool(argc, argv, B_FALSE, NULL, 4967 upgrade_one, &cb); 4968 } 4969 4970 if (cb.cb_poolname[0] != '\0') { 4971 (void) printf( 4972 "If you boot from pool '%s', don't forget to update boot code.\n" 4973 "Assuming you use GPT partitioning and da0 is your boot disk\n" 4974 "the following command will do it:\n" 4975 "\n" 4976 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n", 4977 cb.cb_poolname); 4978 } 4979 4980 return (ret); 4981} 4982 4983typedef struct hist_cbdata { 4984 boolean_t first; 4985 boolean_t longfmt; 4986 boolean_t internal; 4987} hist_cbdata_t; 4988 4989/* 4990 * Print out the command history for a specific pool. 4991 */ 4992static int 4993get_history_one(zpool_handle_t *zhp, void *data) 4994{ 4995 nvlist_t *nvhis; 4996 nvlist_t **records; 4997 uint_t numrecords; 4998 int ret, i; 4999 hist_cbdata_t *cb = (hist_cbdata_t *)data; 5000 5001 cb->first = B_FALSE; 5002 5003 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 5004 5005 if ((ret = zpool_get_history(zhp, &nvhis)) != 0) 5006 return (ret); 5007 5008 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 5009 &records, &numrecords) == 0); 5010 for (i = 0; i < numrecords; i++) { 5011 nvlist_t *rec = records[i]; 5012 char tbuf[30] = ""; 5013 5014 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 5015 time_t tsec; 5016 struct tm t; 5017 5018 tsec = fnvlist_lookup_uint64(records[i], 5019 ZPOOL_HIST_TIME); 5020 (void) localtime_r(&tsec, &t); 5021 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 5022 } 5023 5024 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 5025 (void) printf("%s %s", tbuf, 5026 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 5027 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 5028 int ievent = 5029 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 5030 if (!cb->internal) 5031 continue; 5032 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 5033 (void) printf("%s unrecognized record:\n", 5034 tbuf); 5035 dump_nvlist(rec, 4); 5036 continue; 5037 } 5038 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 5039 zfs_history_event_names[ievent], 5040 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 5041 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 5042 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 5043 if (!cb->internal) 5044 continue; 5045 (void) printf("%s [txg:%lld] %s", tbuf, 5046 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 5047 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 5048 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 5049 (void) printf(" %s (%llu)", 5050 fnvlist_lookup_string(rec, 5051 ZPOOL_HIST_DSNAME), 5052 fnvlist_lookup_uint64(rec, 5053 ZPOOL_HIST_DSID)); 5054 } 5055 (void) printf(" %s", fnvlist_lookup_string(rec, 5056 ZPOOL_HIST_INT_STR)); 5057 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 5058 if (!cb->internal) 5059 continue; 5060 (void) printf("%s ioctl %s\n", tbuf, 5061 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 5062 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 5063 (void) printf(" input:\n"); 5064 dump_nvlist(fnvlist_lookup_nvlist(rec, 5065 ZPOOL_HIST_INPUT_NVL), 8); 5066 } 5067 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 5068 (void) printf(" output:\n"); 5069 dump_nvlist(fnvlist_lookup_nvlist(rec, 5070 ZPOOL_HIST_OUTPUT_NVL), 8); 5071 } 5072 } else { 5073 if (!cb->internal) 5074 continue; 5075 (void) printf("%s unrecognized record:\n", tbuf); 5076 dump_nvlist(rec, 4); 5077 } 5078 5079 if (!cb->longfmt) { 5080 (void) printf("\n"); 5081 continue; 5082 } 5083 (void) printf(" ["); 5084 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 5085 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 5086 struct passwd *pwd = getpwuid(who); 5087 (void) printf("user %d ", (int)who); 5088 if (pwd != NULL) 5089 (void) printf("(%s) ", pwd->pw_name); 5090 } 5091 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 5092 (void) printf("on %s", 5093 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 5094 } 5095 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 5096 (void) printf(":%s", 5097 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 5098 } 5099 (void) printf("]"); 5100 (void) printf("\n"); 5101 } 5102 (void) printf("\n"); 5103 nvlist_free(nvhis); 5104 5105 return (ret); 5106} 5107 5108/* 5109 * zpool history <pool> 5110 * 5111 * Displays the history of commands that modified pools. 5112 */ 5113int 5114zpool_do_history(int argc, char **argv) 5115{ 5116 hist_cbdata_t cbdata = { 0 }; 5117 int ret; 5118 int c; 5119 5120 cbdata.first = B_TRUE; 5121 /* check options */ 5122 while ((c = getopt(argc, argv, "li")) != -1) { 5123 switch (c) { 5124 case 'l': 5125 cbdata.longfmt = B_TRUE; 5126 break; 5127 case 'i': 5128 cbdata.internal = B_TRUE; 5129 break; 5130 case '?': 5131 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5132 optopt); 5133 usage(B_FALSE); 5134 } 5135 } 5136 argc -= optind; 5137 argv += optind; 5138 5139 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, 5140 &cbdata); 5141 5142 if (argc == 0 && cbdata.first == B_TRUE) { 5143 (void) printf(gettext("no pools available\n")); 5144 return (0); 5145 } 5146 5147 return (ret); 5148} 5149 5150static int 5151get_callback(zpool_handle_t *zhp, void *data) 5152{ 5153 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 5154 char value[MAXNAMELEN]; 5155 zprop_source_t srctype; 5156 zprop_list_t *pl; 5157 5158 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 5159 5160 /* 5161 * Skip the special fake placeholder. This will also skip 5162 * over the name property when 'all' is specified. 5163 */ 5164 if (pl->pl_prop == ZPOOL_PROP_NAME && 5165 pl == cbp->cb_proplist) 5166 continue; 5167 5168 if (pl->pl_prop == ZPROP_INVAL && 5169 (zpool_prop_feature(pl->pl_user_prop) || 5170 zpool_prop_unsupported(pl->pl_user_prop))) { 5171 srctype = ZPROP_SRC_LOCAL; 5172 5173 if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 5174 value, sizeof (value)) == 0) { 5175 zprop_print_one_property(zpool_get_name(zhp), 5176 cbp, pl->pl_user_prop, value, srctype, 5177 NULL, NULL); 5178 } 5179 } else { 5180 if (zpool_get_prop(zhp, pl->pl_prop, value, 5181 sizeof (value), &srctype) != 0) 5182 continue; 5183 5184 zprop_print_one_property(zpool_get_name(zhp), cbp, 5185 zpool_prop_to_name(pl->pl_prop), value, srctype, 5186 NULL, NULL); 5187 } 5188 } 5189 return (0); 5190} 5191 5192int 5193zpool_do_get(int argc, char **argv) 5194{ 5195 zprop_get_cbdata_t cb = { 0 }; 5196 zprop_list_t fake_name = { 0 }; 5197 int ret; 5198 5199 if (argc < 2) { 5200 (void) fprintf(stderr, gettext("missing property " 5201 "argument\n")); 5202 usage(B_FALSE); 5203 } 5204 5205 cb.cb_first = B_TRUE; 5206 cb.cb_sources = ZPROP_SRC_ALL; 5207 cb.cb_columns[0] = GET_COL_NAME; 5208 cb.cb_columns[1] = GET_COL_PROPERTY; 5209 cb.cb_columns[2] = GET_COL_VALUE; 5210 cb.cb_columns[3] = GET_COL_SOURCE; 5211 cb.cb_type = ZFS_TYPE_POOL; 5212 5213 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist, 5214 ZFS_TYPE_POOL) != 0) 5215 usage(B_FALSE); 5216 5217 if (cb.cb_proplist != NULL) { 5218 fake_name.pl_prop = ZPOOL_PROP_NAME; 5219 fake_name.pl_width = strlen(gettext("NAME")); 5220 fake_name.pl_next = cb.cb_proplist; 5221 cb.cb_proplist = &fake_name; 5222 } 5223 5224 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist, 5225 get_callback, &cb); 5226 5227 if (cb.cb_proplist == &fake_name) 5228 zprop_free_list(fake_name.pl_next); 5229 else 5230 zprop_free_list(cb.cb_proplist); 5231 5232 return (ret); 5233} 5234 5235typedef struct set_cbdata { 5236 char *cb_propname; 5237 char *cb_value; 5238 boolean_t cb_any_successful; 5239} set_cbdata_t; 5240 5241int 5242set_callback(zpool_handle_t *zhp, void *data) 5243{ 5244 int error; 5245 set_cbdata_t *cb = (set_cbdata_t *)data; 5246 5247 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 5248 5249 if (!error) 5250 cb->cb_any_successful = B_TRUE; 5251 5252 return (error); 5253} 5254 5255int 5256zpool_do_set(int argc, char **argv) 5257{ 5258 set_cbdata_t cb = { 0 }; 5259 int error; 5260 5261 if (argc > 1 && argv[1][0] == '-') { 5262 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5263 argv[1][1]); 5264 usage(B_FALSE); 5265 } 5266 5267 if (argc < 2) { 5268 (void) fprintf(stderr, gettext("missing property=value " 5269 "argument\n")); 5270 usage(B_FALSE); 5271 } 5272 5273 if (argc < 3) { 5274 (void) fprintf(stderr, gettext("missing pool name\n")); 5275 usage(B_FALSE); 5276 } 5277 5278 if (argc > 3) { 5279 (void) fprintf(stderr, gettext("too many pool names\n")); 5280 usage(B_FALSE); 5281 } 5282 5283 cb.cb_propname = argv[1]; 5284 cb.cb_value = strchr(cb.cb_propname, '='); 5285 if (cb.cb_value == NULL) { 5286 (void) fprintf(stderr, gettext("missing value in " 5287 "property=value argument\n")); 5288 usage(B_FALSE); 5289 } 5290 5291 *(cb.cb_value) = '\0'; 5292 cb.cb_value++; 5293 5294 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, 5295 set_callback, &cb); 5296 5297 return (error); 5298} 5299 5300static int 5301find_command_idx(char *command, int *idx) 5302{ 5303 int i; 5304 5305 for (i = 0; i < NCOMMAND; i++) { 5306 if (command_table[i].name == NULL) 5307 continue; 5308 5309 if (strcmp(command, command_table[i].name) == 0) { 5310 *idx = i; 5311 return (0); 5312 } 5313 } 5314 return (1); 5315} 5316 5317int 5318main(int argc, char **argv) 5319{ 5320 int ret; 5321 int i; 5322 char *cmdname; 5323 5324 (void) setlocale(LC_ALL, ""); 5325 (void) textdomain(TEXT_DOMAIN); 5326 5327 if ((g_zfs = libzfs_init()) == NULL) { 5328 (void) fprintf(stderr, gettext("internal error: failed to " 5329 "initialize ZFS library\n")); 5330 return (1); 5331 } 5332 5333 libzfs_print_on_error(g_zfs, B_TRUE); 5334 5335 opterr = 0; 5336 5337 /* 5338 * Make sure the user has specified some command. 5339 */ 5340 if (argc < 2) { 5341 (void) fprintf(stderr, gettext("missing command\n")); 5342 usage(B_FALSE); 5343 } 5344 5345 cmdname = argv[1]; 5346 5347 /* 5348 * Special case '-?' 5349 */ 5350 if (strcmp(cmdname, "-?") == 0) 5351 usage(B_TRUE); 5352 5353 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 5354 5355 /* 5356 * Run the appropriate command. 5357 */ 5358 if (find_command_idx(cmdname, &i) == 0) { 5359 current_command = &command_table[i]; 5360 ret = command_table[i].func(argc - 1, argv + 1); 5361 } else if (strchr(cmdname, '=')) { 5362 verify(find_command_idx("set", &i) == 0); 5363 current_command = &command_table[i]; 5364 ret = command_table[i].func(argc, argv); 5365 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 5366 /* 5367 * 'freeze' is a vile debugging abomination, so we treat 5368 * it as such. 5369 */ 5370 zfs_cmd_t zc = { 0 }; 5371 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 5372 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc)); 5373 } else { 5374 (void) fprintf(stderr, gettext("unrecognized " 5375 "command '%s'\n"), cmdname); 5376 usage(B_FALSE); 5377 } 5378 5379 if (ret == 0 && log_history) 5380 (void) zpool_log_history(g_zfs, history_str); 5381 5382 libzfs_fini(g_zfs); 5383 5384 /* 5385 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 5386 * for the purposes of running ::findleaks. 5387 */ 5388 if (getenv("ZFS_ABORT") != NULL) { 5389 (void) printf("dumping core by request\n"); 5390 abort(); 5391 } 5392 5393 return (ret); 5394} 5395