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