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 if (props) 3245 nvlist_free(props); 3246 usage(B_FALSE); 3247 } 3248 break; 3249 case 'n': 3250 flags.dryrun = B_TRUE; 3251 break; 3252 case 'o': 3253 if ((propval = strchr(optarg, '=')) != NULL) { 3254 *propval = '\0'; 3255 propval++; 3256 if (add_prop_list(optarg, propval, 3257 &props, B_TRUE) != 0) { 3258 if (props) 3259 nvlist_free(props); 3260 usage(B_FALSE); 3261 } 3262 } else { 3263 mntopts = optarg; 3264 } 3265 break; 3266 case ':': 3267 (void) fprintf(stderr, gettext("missing argument for " 3268 "'%c' option\n"), optopt); 3269 usage(B_FALSE); 3270 break; 3271 case '?': 3272 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3273 optopt); 3274 usage(B_FALSE); 3275 break; 3276 } 3277 } 3278 3279 if (!flags.import && mntopts != NULL) { 3280 (void) fprintf(stderr, gettext("setting mntopts is only " 3281 "valid when importing the pool\n")); 3282 usage(B_FALSE); 3283 } 3284 3285 argc -= optind; 3286 argv += optind; 3287 3288 if (argc < 1) { 3289 (void) fprintf(stderr, gettext("Missing pool name\n")); 3290 usage(B_FALSE); 3291 } 3292 if (argc < 2) { 3293 (void) fprintf(stderr, gettext("Missing new pool name\n")); 3294 usage(B_FALSE); 3295 } 3296 3297 srcpool = argv[0]; 3298 newpool = argv[1]; 3299 3300 argc -= 2; 3301 argv += 2; 3302 3303 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) 3304 return (1); 3305 3306 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 3307 if (config == NULL) { 3308 ret = 1; 3309 } else { 3310 if (flags.dryrun) { 3311 (void) printf(gettext("would create '%s' with the " 3312 "following layout:\n\n"), newpool); 3313 print_vdev_tree(NULL, newpool, config, 0, B_FALSE); 3314 } 3315 nvlist_free(config); 3316 } 3317 3318 zpool_close(zhp); 3319 3320 if (ret != 0 || flags.dryrun || !flags.import) 3321 return (ret); 3322 3323 /* 3324 * The split was successful. Now we need to open the new 3325 * pool and import it. 3326 */ 3327 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) 3328 return (1); 3329 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3330 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 3331 ret = 1; 3332 (void) fprintf(stderr, gettext("Split was successful, but " 3333 "the datasets could not all be mounted\n")); 3334 (void) fprintf(stderr, gettext("Try doing '%s' with a " 3335 "different altroot\n"), "zpool import"); 3336 } 3337 zpool_close(zhp); 3338 3339 return (ret); 3340 } 3341 3342 3343 3344 /* 3345 * zpool online <pool> <device> ... 3346 */ 3347 int 3348 zpool_do_online(int argc, char **argv) 3349 { 3350 int c, i; 3351 char *poolname; 3352 zpool_handle_t *zhp; 3353 int ret = 0; 3354 vdev_state_t newstate; 3355 int flags = 0; 3356 3357 /* check options */ 3358 while ((c = getopt(argc, argv, "et")) != -1) { 3359 switch (c) { 3360 case 'e': 3361 flags |= ZFS_ONLINE_EXPAND; 3362 break; 3363 case 't': 3364 case '?': 3365 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3366 optopt); 3367 usage(B_FALSE); 3368 } 3369 } 3370 3371 argc -= optind; 3372 argv += optind; 3373 3374 /* get pool name and check number of arguments */ 3375 if (argc < 1) { 3376 (void) fprintf(stderr, gettext("missing pool name\n")); 3377 usage(B_FALSE); 3378 } 3379 if (argc < 2) { 3380 (void) fprintf(stderr, gettext("missing device name\n")); 3381 usage(B_FALSE); 3382 } 3383 3384 poolname = argv[0]; 3385 3386 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3387 return (1); 3388 3389 for (i = 1; i < argc; i++) { 3390 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 3391 if (newstate != VDEV_STATE_HEALTHY) { 3392 (void) printf(gettext("warning: device '%s' " 3393 "onlined, but remains in faulted state\n"), 3394 argv[i]); 3395 if (newstate == VDEV_STATE_FAULTED) 3396 (void) printf(gettext("use 'zpool " 3397 "clear' to restore a faulted " 3398 "device\n")); 3399 else 3400 (void) printf(gettext("use 'zpool " 3401 "replace' to replace devices " 3402 "that are no longer present\n")); 3403 } 3404 } else { 3405 ret = 1; 3406 } 3407 } 3408 3409 zpool_close(zhp); 3410 3411 return (ret); 3412 } 3413 3414 /* 3415 * zpool offline [-ft] <pool> <device> ... 3416 * 3417 * -f Force the device into the offline state, even if doing 3418 * so would appear to compromise pool availability. 3419 * (not supported yet) 3420 * 3421 * -t Only take the device off-line temporarily. The offline 3422 * state will not be persistent across reboots. 3423 */ 3424 /* ARGSUSED */ 3425 int 3426 zpool_do_offline(int argc, char **argv) 3427 { 3428 int c, i; 3429 char *poolname; 3430 zpool_handle_t *zhp; 3431 int ret = 0; 3432 boolean_t istmp = B_FALSE; 3433 3434 /* check options */ 3435 while ((c = getopt(argc, argv, "ft")) != -1) { 3436 switch (c) { 3437 case 't': 3438 istmp = B_TRUE; 3439 break; 3440 case 'f': 3441 case '?': 3442 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3443 optopt); 3444 usage(B_FALSE); 3445 } 3446 } 3447 3448 argc -= optind; 3449 argv += optind; 3450 3451 /* get pool name and check number of arguments */ 3452 if (argc < 1) { 3453 (void) fprintf(stderr, gettext("missing pool name\n")); 3454 usage(B_FALSE); 3455 } 3456 if (argc < 2) { 3457 (void) fprintf(stderr, gettext("missing device name\n")); 3458 usage(B_FALSE); 3459 } 3460 3461 poolname = argv[0]; 3462 3463 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3464 return (1); 3465 3466 for (i = 1; i < argc; i++) { 3467 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 3468 ret = 1; 3469 } 3470 3471 zpool_close(zhp); 3472 3473 return (ret); 3474 } 3475 3476 /* 3477 * zpool clear <pool> [device] 3478 * 3479 * Clear all errors associated with a pool or a particular device. 3480 */ 3481 int 3482 zpool_do_clear(int argc, char **argv) 3483 { 3484 int c; 3485 int ret = 0; 3486 boolean_t dryrun = B_FALSE; 3487 boolean_t do_rewind = B_FALSE; 3488 boolean_t xtreme_rewind = B_FALSE; 3489 uint32_t rewind_policy = ZPOOL_NO_REWIND; 3490 nvlist_t *policy = NULL; 3491 zpool_handle_t *zhp; 3492 char *pool, *device; 3493 3494 /* check options */ 3495 while ((c = getopt(argc, argv, "FnX")) != -1) { 3496 switch (c) { 3497 case 'F': 3498 do_rewind = B_TRUE; 3499 break; 3500 case 'n': 3501 dryrun = B_TRUE; 3502 break; 3503 case 'X': 3504 xtreme_rewind = B_TRUE; 3505 break; 3506 case '?': 3507 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3508 optopt); 3509 usage(B_FALSE); 3510 } 3511 } 3512 3513 argc -= optind; 3514 argv += optind; 3515 3516 if (argc < 1) { 3517 (void) fprintf(stderr, gettext("missing pool name\n")); 3518 usage(B_FALSE); 3519 } 3520 3521 if (argc > 2) { 3522 (void) fprintf(stderr, gettext("too many arguments\n")); 3523 usage(B_FALSE); 3524 } 3525 3526 if ((dryrun || xtreme_rewind) && !do_rewind) { 3527 (void) fprintf(stderr, 3528 gettext("-n or -X only meaningful with -F\n")); 3529 usage(B_FALSE); 3530 } 3531 if (dryrun) 3532 rewind_policy = ZPOOL_TRY_REWIND; 3533 else if (do_rewind) 3534 rewind_policy = ZPOOL_DO_REWIND; 3535 if (xtreme_rewind) 3536 rewind_policy |= ZPOOL_EXTREME_REWIND; 3537 3538 /* In future, further rewind policy choices can be passed along here */ 3539 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3540 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 3541 return (1); 3542 3543 pool = argv[0]; 3544 device = argc == 2 ? argv[1] : NULL; 3545 3546 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 3547 nvlist_free(policy); 3548 return (1); 3549 } 3550 3551 if (zpool_clear(zhp, device, policy) != 0) 3552 ret = 1; 3553 3554 zpool_close(zhp); 3555 3556 nvlist_free(policy); 3557 3558 return (ret); 3559 } 3560 3561 /* 3562 * zpool reguid <pool> 3563 */ 3564 int 3565 zpool_do_reguid(int argc, char **argv) 3566 { 3567 int c; 3568 char *poolname; 3569 zpool_handle_t *zhp; 3570 int ret = 0; 3571 3572 /* check options */ 3573 while ((c = getopt(argc, argv, "")) != -1) { 3574 switch (c) { 3575 case '?': 3576 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3577 optopt); 3578 usage(B_FALSE); 3579 } 3580 } 3581 3582 argc -= optind; 3583 argv += optind; 3584 3585 /* get pool name and check number of arguments */ 3586 if (argc < 1) { 3587 (void) fprintf(stderr, gettext("missing pool name\n")); 3588 usage(B_FALSE); 3589 } 3590 3591 if (argc > 1) { 3592 (void) fprintf(stderr, gettext("too many arguments\n")); 3593 usage(B_FALSE); 3594 } 3595 3596 poolname = argv[0]; 3597 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3598 return (1); 3599 3600 ret = zpool_reguid(zhp); 3601 3602 zpool_close(zhp); 3603 return (ret); 3604 } 3605 3606 3607 /* 3608 * zpool reopen <pool> 3609 * 3610 * Reopen the pool so that the kernel can update the sizes of all vdevs. 3611 */ 3612 int 3613 zpool_do_reopen(int argc, char **argv) 3614 { 3615 int c; 3616 int ret = 0; 3617 zpool_handle_t *zhp; 3618 char *pool; 3619 3620 /* check options */ 3621 while ((c = getopt(argc, argv, "")) != -1) { 3622 switch (c) { 3623 case '?': 3624 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3625 optopt); 3626 usage(B_FALSE); 3627 } 3628 } 3629 3630 argc--; 3631 argv++; 3632 3633 if (argc < 1) { 3634 (void) fprintf(stderr, gettext("missing pool name\n")); 3635 usage(B_FALSE); 3636 } 3637 3638 if (argc > 1) { 3639 (void) fprintf(stderr, gettext("too many arguments\n")); 3640 usage(B_FALSE); 3641 } 3642 3643 pool = argv[0]; 3644 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) 3645 return (1); 3646 3647 ret = zpool_reopen(zhp); 3648 zpool_close(zhp); 3649 return (ret); 3650 } 3651 3652 typedef struct scrub_cbdata { 3653 int cb_type; 3654 int cb_argc; 3655 char **cb_argv; 3656 } scrub_cbdata_t; 3657 3658 int 3659 scrub_callback(zpool_handle_t *zhp, void *data) 3660 { 3661 scrub_cbdata_t *cb = data; 3662 int err; 3663 3664 /* 3665 * Ignore faulted pools. 3666 */ 3667 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 3668 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " 3669 "currently unavailable\n"), zpool_get_name(zhp)); 3670 return (1); 3671 } 3672 3673 err = zpool_scan(zhp, cb->cb_type); 3674 3675 return (err != 0); 3676 } 3677 3678 /* 3679 * zpool scrub [-s] <pool> ... 3680 * 3681 * -s Stop. Stops any in-progress scrub. 3682 */ 3683 int 3684 zpool_do_scrub(int argc, char **argv) 3685 { 3686 int c; 3687 scrub_cbdata_t cb; 3688 3689 cb.cb_type = POOL_SCAN_SCRUB; 3690 3691 /* check options */ 3692 while ((c = getopt(argc, argv, "s")) != -1) { 3693 switch (c) { 3694 case 's': 3695 cb.cb_type = POOL_SCAN_NONE; 3696 break; 3697 case '?': 3698 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3699 optopt); 3700 usage(B_FALSE); 3701 } 3702 } 3703 3704 cb.cb_argc = argc; 3705 cb.cb_argv = argv; 3706 argc -= optind; 3707 argv += optind; 3708 3709 if (argc < 1) { 3710 (void) fprintf(stderr, gettext("missing pool name argument\n")); 3711 usage(B_FALSE); 3712 } 3713 3714 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); 3715 } 3716 3717 typedef struct status_cbdata { 3718 int cb_count; 3719 boolean_t cb_allpools; 3720 boolean_t cb_verbose; 3721 boolean_t cb_explain; 3722 boolean_t cb_first; 3723 boolean_t cb_dedup_stats; 3724 } status_cbdata_t; 3725 3726 /* 3727 * Print out detailed scrub status. 3728 */ 3729 void 3730 print_scan_status(pool_scan_stat_t *ps) 3731 { 3732 time_t start, end; 3733 uint64_t elapsed, mins_left, hours_left; 3734 uint64_t pass_exam, examined, total; 3735 uint_t rate; 3736 double fraction_done; 3737 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 3738 3739 (void) printf(gettext(" scan: ")); 3740 3741 /* If there's never been a scan, there's not much to say. */ 3742 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 3743 ps->pss_func >= POOL_SCAN_FUNCS) { 3744 (void) printf(gettext("none requested\n")); 3745 return; 3746 } 3747 3748 start = ps->pss_start_time; 3749 end = ps->pss_end_time; 3750 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); 3751 3752 assert(ps->pss_func == POOL_SCAN_SCRUB || 3753 ps->pss_func == POOL_SCAN_RESILVER); 3754 /* 3755 * Scan is finished or canceled. 3756 */ 3757 if (ps->pss_state == DSS_FINISHED) { 3758 uint64_t minutes_taken = (end - start) / 60; 3759 char *fmt = NULL; 3760 3761 if (ps->pss_func == POOL_SCAN_SCRUB) { 3762 fmt = gettext("scrub repaired %s in %lluh%um with " 3763 "%llu errors on %s"); 3764 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3765 fmt = gettext("resilvered %s in %lluh%um with " 3766 "%llu errors on %s"); 3767 } 3768 /* LINTED */ 3769 (void) printf(fmt, processed_buf, 3770 (u_longlong_t)(minutes_taken / 60), 3771 (uint_t)(minutes_taken % 60), 3772 (u_longlong_t)ps->pss_errors, 3773 ctime((time_t *)&end)); 3774 return; 3775 } else if (ps->pss_state == DSS_CANCELED) { 3776 if (ps->pss_func == POOL_SCAN_SCRUB) { 3777 (void) printf(gettext("scrub canceled on %s"), 3778 ctime(&end)); 3779 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3780 (void) printf(gettext("resilver canceled on %s"), 3781 ctime(&end)); 3782 } 3783 return; 3784 } 3785 3786 assert(ps->pss_state == DSS_SCANNING); 3787 3788 /* 3789 * Scan is in progress. 3790 */ 3791 if (ps->pss_func == POOL_SCAN_SCRUB) { 3792 (void) printf(gettext("scrub in progress since %s"), 3793 ctime(&start)); 3794 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3795 (void) printf(gettext("resilver in progress since %s"), 3796 ctime(&start)); 3797 } 3798 3799 examined = ps->pss_examined ? ps->pss_examined : 1; 3800 total = ps->pss_to_examine; 3801 fraction_done = (double)examined / total; 3802 3803 /* elapsed time for this pass */ 3804 elapsed = time(NULL) - ps->pss_pass_start; 3805 elapsed = elapsed ? elapsed : 1; 3806 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; 3807 rate = pass_exam / elapsed; 3808 rate = rate ? rate : 1; 3809 mins_left = ((total - examined) / rate) / 60; 3810 hours_left = mins_left / 60; 3811 3812 zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); 3813 zfs_nicenum(total, total_buf, sizeof (total_buf)); 3814 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 3815 3816 /* 3817 * do not print estimated time if hours_left is more than 30 days 3818 */ 3819 (void) printf(gettext(" %s scanned out of %s at %s/s"), 3820 examined_buf, total_buf, rate_buf); 3821 if (hours_left < (30 * 24)) { 3822 (void) printf(gettext(", %lluh%um to go\n"), 3823 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 3824 } else { 3825 (void) printf(gettext( 3826 ", (scan is slow, no estimated time)\n")); 3827 } 3828 3829 if (ps->pss_func == POOL_SCAN_RESILVER) { 3830 (void) printf(gettext(" %s resilvered, %.2f%% done\n"), 3831 processed_buf, 100 * fraction_done); 3832 } else if (ps->pss_func == POOL_SCAN_SCRUB) { 3833 (void) printf(gettext(" %s repaired, %.2f%% done\n"), 3834 processed_buf, 100 * fraction_done); 3835 } 3836 } 3837 3838 static void 3839 print_error_log(zpool_handle_t *zhp) 3840 { 3841 nvlist_t *nverrlist = NULL; 3842 nvpair_t *elem; 3843 char *pathname; 3844 size_t len = MAXPATHLEN * 2; 3845 3846 if (zpool_get_errlog(zhp, &nverrlist) != 0) { 3847 (void) printf("errors: List of errors unavailable " 3848 "(insufficient privileges)\n"); 3849 return; 3850 } 3851 3852 (void) printf("errors: Permanent errors have been " 3853 "detected in the following files:\n\n"); 3854 3855 pathname = safe_malloc(len); 3856 elem = NULL; 3857 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 3858 nvlist_t *nv; 3859 uint64_t dsobj, obj; 3860 3861 verify(nvpair_value_nvlist(elem, &nv) == 0); 3862 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 3863 &dsobj) == 0); 3864 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 3865 &obj) == 0); 3866 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 3867 (void) printf("%7s %s\n", "", pathname); 3868 } 3869 free(pathname); 3870 nvlist_free(nverrlist); 3871 } 3872 3873 static void 3874 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, 3875 int namewidth) 3876 { 3877 uint_t i; 3878 char *name; 3879 3880 if (nspares == 0) 3881 return; 3882 3883 (void) printf(gettext("\tspares\n")); 3884 3885 for (i = 0; i < nspares; i++) { 3886 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); 3887 print_status_config(zhp, name, spares[i], 3888 namewidth, 2, B_TRUE); 3889 free(name); 3890 } 3891 } 3892 3893 static void 3894 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, 3895 int namewidth) 3896 { 3897 uint_t i; 3898 char *name; 3899 3900 if (nl2cache == 0) 3901 return; 3902 3903 (void) printf(gettext("\tcache\n")); 3904 3905 for (i = 0; i < nl2cache; i++) { 3906 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); 3907 print_status_config(zhp, name, l2cache[i], 3908 namewidth, 2, B_FALSE); 3909 free(name); 3910 } 3911 } 3912 3913 static void 3914 print_dedup_stats(nvlist_t *config) 3915 { 3916 ddt_histogram_t *ddh; 3917 ddt_stat_t *dds; 3918 ddt_object_t *ddo; 3919 uint_t c; 3920 3921 /* 3922 * If the pool was faulted then we may not have been able to 3923 * obtain the config. Otherwise, if we have anything in the dedup 3924 * table continue processing the stats. 3925 */ 3926 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 3927 (uint64_t **)&ddo, &c) != 0) 3928 return; 3929 3930 (void) printf("\n"); 3931 (void) printf(gettext(" dedup: ")); 3932 if (ddo->ddo_count == 0) { 3933 (void) printf(gettext("no DDT entries\n")); 3934 return; 3935 } 3936 3937 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", 3938 (u_longlong_t)ddo->ddo_count, 3939 (u_longlong_t)ddo->ddo_dspace, 3940 (u_longlong_t)ddo->ddo_mspace); 3941 3942 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 3943 (uint64_t **)&dds, &c) == 0); 3944 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 3945 (uint64_t **)&ddh, &c) == 0); 3946 zpool_dump_ddt(dds, ddh); 3947 } 3948 3949 /* 3950 * Display a summary of pool status. Displays a summary such as: 3951 * 3952 * pool: tank 3953 * status: DEGRADED 3954 * reason: One or more devices ... 3955 * see: http://illumos.org/msg/ZFS-xxxx-01 3956 * config: 3957 * mirror DEGRADED 3958 * c1t0d0 OK 3959 * c2t0d0 UNAVAIL 3960 * 3961 * When given the '-v' option, we print out the complete config. If the '-e' 3962 * option is specified, then we print out error rate information as well. 3963 */ 3964 int 3965 status_callback(zpool_handle_t *zhp, void *data) 3966 { 3967 status_cbdata_t *cbp = data; 3968 nvlist_t *config, *nvroot; 3969 char *msgid; 3970 int reason; 3971 const char *health; 3972 uint_t c; 3973 vdev_stat_t *vs; 3974 3975 config = zpool_get_config(zhp, NULL); 3976 reason = zpool_get_status(zhp, &msgid); 3977 3978 cbp->cb_count++; 3979 3980 /* 3981 * If we were given 'zpool status -x', only report those pools with 3982 * problems. 3983 */ 3984 if (cbp->cb_explain && 3985 (reason == ZPOOL_STATUS_OK || 3986 reason == ZPOOL_STATUS_VERSION_OLDER || 3987 reason == ZPOOL_STATUS_FEAT_DISABLED)) { 3988 if (!cbp->cb_allpools) { 3989 (void) printf(gettext("pool '%s' is healthy\n"), 3990 zpool_get_name(zhp)); 3991 if (cbp->cb_first) 3992 cbp->cb_first = B_FALSE; 3993 } 3994 return (0); 3995 } 3996 3997 if (cbp->cb_first) 3998 cbp->cb_first = B_FALSE; 3999 else 4000 (void) printf("\n"); 4001 4002 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4003 &nvroot) == 0); 4004 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 4005 (uint64_t **)&vs, &c) == 0); 4006 health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 4007 4008 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); 4009 (void) printf(gettext(" state: %s\n"), health); 4010 4011 switch (reason) { 4012 case ZPOOL_STATUS_MISSING_DEV_R: 4013 (void) printf(gettext("status: One or more devices could not " 4014 "be opened. Sufficient replicas exist for\n\tthe pool to " 4015 "continue functioning in a degraded state.\n")); 4016 (void) printf(gettext("action: Attach the missing device and " 4017 "online it using 'zpool online'.\n")); 4018 break; 4019 4020 case ZPOOL_STATUS_MISSING_DEV_NR: 4021 (void) printf(gettext("status: One or more devices could not " 4022 "be opened. There are insufficient\n\treplicas for the " 4023 "pool to continue functioning.\n")); 4024 (void) printf(gettext("action: Attach the missing device and " 4025 "online it using 'zpool online'.\n")); 4026 break; 4027 4028 case ZPOOL_STATUS_CORRUPT_LABEL_R: 4029 (void) printf(gettext("status: One or more devices could not " 4030 "be used because the label is missing or\n\tinvalid. " 4031 "Sufficient replicas exist for the pool to continue\n\t" 4032 "functioning in a degraded state.\n")); 4033 (void) printf(gettext("action: Replace the device using " 4034 "'zpool replace'.\n")); 4035 break; 4036 4037 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 4038 (void) printf(gettext("status: One or more devices could not " 4039 "be used because the label is missing \n\tor invalid. " 4040 "There are insufficient replicas for the pool to " 4041 "continue\n\tfunctioning.\n")); 4042 zpool_explain_recover(zpool_get_handle(zhp), 4043 zpool_get_name(zhp), reason, config); 4044 break; 4045 4046 case ZPOOL_STATUS_FAILING_DEV: 4047 (void) printf(gettext("status: One or more devices has " 4048 "experienced an unrecoverable error. An\n\tattempt was " 4049 "made to correct the error. Applications are " 4050 "unaffected.\n")); 4051 (void) printf(gettext("action: Determine if the device needs " 4052 "to be replaced, and clear the errors\n\tusing " 4053 "'zpool clear' or replace the device with 'zpool " 4054 "replace'.\n")); 4055 break; 4056 4057 case ZPOOL_STATUS_OFFLINE_DEV: 4058 (void) printf(gettext("status: One or more devices has " 4059 "been taken offline by the administrator.\n\tSufficient " 4060 "replicas exist for the pool to continue functioning in " 4061 "a\n\tdegraded state.\n")); 4062 (void) printf(gettext("action: Online the device using " 4063 "'zpool online' or replace the device with\n\t'zpool " 4064 "replace'.\n")); 4065 break; 4066 4067 case ZPOOL_STATUS_REMOVED_DEV: 4068 (void) printf(gettext("status: One or more devices has " 4069 "been removed by the administrator.\n\tSufficient " 4070 "replicas exist for the pool to continue functioning in " 4071 "a\n\tdegraded state.\n")); 4072 (void) printf(gettext("action: Online the device using " 4073 "'zpool online' or replace the device with\n\t'zpool " 4074 "replace'.\n")); 4075 break; 4076 4077 case ZPOOL_STATUS_RESILVERING: 4078 (void) printf(gettext("status: One or more devices is " 4079 "currently being resilvered. The pool will\n\tcontinue " 4080 "to function, possibly in a degraded state.\n")); 4081 (void) printf(gettext("action: Wait for the resilver to " 4082 "complete.\n")); 4083 break; 4084 4085 case ZPOOL_STATUS_CORRUPT_DATA: 4086 (void) printf(gettext("status: One or more devices has " 4087 "experienced an error resulting in data\n\tcorruption. " 4088 "Applications may be affected.\n")); 4089 (void) printf(gettext("action: Restore the file in question " 4090 "if possible. Otherwise restore the\n\tentire pool from " 4091 "backup.\n")); 4092 break; 4093 4094 case ZPOOL_STATUS_CORRUPT_POOL: 4095 (void) printf(gettext("status: The pool metadata is corrupted " 4096 "and the pool cannot be opened.\n")); 4097 zpool_explain_recover(zpool_get_handle(zhp), 4098 zpool_get_name(zhp), reason, config); 4099 break; 4100 4101 case ZPOOL_STATUS_VERSION_OLDER: 4102 (void) printf(gettext("status: The pool is formatted using a " 4103 "legacy on-disk format. The pool can\n\tstill be used, " 4104 "but some features are unavailable.\n")); 4105 (void) printf(gettext("action: Upgrade the pool using 'zpool " 4106 "upgrade'. Once this is done, the\n\tpool will no longer " 4107 "be accessible on software that does not support feature\n" 4108 "\tflags.\n")); 4109 break; 4110 4111 case ZPOOL_STATUS_VERSION_NEWER: 4112 (void) printf(gettext("status: The pool has been upgraded to a " 4113 "newer, incompatible on-disk version.\n\tThe pool cannot " 4114 "be accessed on this system.\n")); 4115 (void) printf(gettext("action: Access the pool from a system " 4116 "running more recent software, or\n\trestore the pool from " 4117 "backup.\n")); 4118 break; 4119 4120 case ZPOOL_STATUS_FEAT_DISABLED: 4121 (void) printf(gettext("status: Some supported features are not " 4122 "enabled on the pool. The pool can\n\tstill be used, but " 4123 "some features are unavailable.\n")); 4124 (void) printf(gettext("action: Enable all features using " 4125 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 4126 "longer be accessible by software that does not support\n\t" 4127 "the features. See zpool-features(5) for details.\n")); 4128 break; 4129 4130 case ZPOOL_STATUS_UNSUP_FEAT_READ: 4131 (void) printf(gettext("status: The pool cannot be accessed on " 4132 "this system because it uses the\n\tfollowing feature(s) " 4133 "not supported on this system:\n")); 4134 zpool_print_unsup_feat(config); 4135 (void) printf("\n"); 4136 (void) printf(gettext("action: Access the pool from a system " 4137 "that supports the required feature(s),\n\tor restore the " 4138 "pool from backup.\n")); 4139 break; 4140 4141 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 4142 (void) printf(gettext("status: The pool can only be accessed " 4143 "in read-only mode on this system. It\n\tcannot be " 4144 "accessed in read-write mode because it uses the " 4145 "following\n\tfeature(s) not supported on this system:\n")); 4146 zpool_print_unsup_feat(config); 4147 (void) printf("\n"); 4148 (void) printf(gettext("action: The pool cannot be accessed in " 4149 "read-write mode. Import the pool with\n" 4150 "\t\"-o readonly=on\", access the pool from a system that " 4151 "supports the\n\trequired feature(s), or restore the " 4152 "pool from backup.\n")); 4153 break; 4154 4155 case ZPOOL_STATUS_FAULTED_DEV_R: 4156 (void) printf(gettext("status: One or more devices are " 4157 "faulted in response to persistent errors.\n\tSufficient " 4158 "replicas exist for the pool to continue functioning " 4159 "in a\n\tdegraded state.\n")); 4160 (void) printf(gettext("action: Replace the faulted device, " 4161 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 4162 break; 4163 4164 case ZPOOL_STATUS_FAULTED_DEV_NR: 4165 (void) printf(gettext("status: One or more devices are " 4166 "faulted in response to persistent errors. There are " 4167 "insufficient replicas for the pool to\n\tcontinue " 4168 "functioning.\n")); 4169 (void) printf(gettext("action: Destroy and re-create the pool " 4170 "from a backup source. Manually marking the device\n" 4171 "\trepaired using 'zpool clear' may allow some data " 4172 "to be recovered.\n")); 4173 break; 4174 4175 case ZPOOL_STATUS_IO_FAILURE_WAIT: 4176 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 4177 (void) printf(gettext("status: One or more devices are " 4178 "faulted in response to IO failures.\n")); 4179 (void) printf(gettext("action: Make sure the affected devices " 4180 "are connected, then run 'zpool clear'.\n")); 4181 break; 4182 4183 case ZPOOL_STATUS_BAD_LOG: 4184 (void) printf(gettext("status: An intent log record " 4185 "could not be read.\n" 4186 "\tWaiting for adminstrator intervention to fix the " 4187 "faulted pool.\n")); 4188 (void) printf(gettext("action: Either restore the affected " 4189 "device(s) and run 'zpool online',\n" 4190 "\tor ignore the intent log records by running " 4191 "'zpool clear'.\n")); 4192 break; 4193 4194 default: 4195 /* 4196 * The remaining errors can't actually be generated, yet. 4197 */ 4198 assert(reason == ZPOOL_STATUS_OK); 4199 } 4200 4201 if (msgid != NULL) 4202 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 4203 msgid); 4204 4205 if (config != NULL) { 4206 int namewidth; 4207 uint64_t nerr; 4208 nvlist_t **spares, **l2cache; 4209 uint_t nspares, nl2cache; 4210 pool_scan_stat_t *ps = NULL; 4211 4212 (void) nvlist_lookup_uint64_array(nvroot, 4213 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); 4214 print_scan_status(ps); 4215 4216 namewidth = max_width(zhp, nvroot, 0, 0); 4217 if (namewidth < 10) 4218 namewidth = 10; 4219 4220 (void) printf(gettext("config:\n\n")); 4221 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, 4222 "NAME", "STATE", "READ", "WRITE", "CKSUM"); 4223 print_status_config(zhp, zpool_get_name(zhp), nvroot, 4224 namewidth, 0, B_FALSE); 4225 4226 if (num_logs(nvroot) > 0) 4227 print_logs(zhp, nvroot, namewidth, B_TRUE); 4228 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4229 &l2cache, &nl2cache) == 0) 4230 print_l2cache(zhp, l2cache, nl2cache, namewidth); 4231 4232 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 4233 &spares, &nspares) == 0) 4234 print_spares(zhp, spares, nspares, namewidth); 4235 4236 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 4237 &nerr) == 0) { 4238 nvlist_t *nverrlist = NULL; 4239 4240 /* 4241 * If the approximate error count is small, get a 4242 * precise count by fetching the entire log and 4243 * uniquifying the results. 4244 */ 4245 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 4246 zpool_get_errlog(zhp, &nverrlist) == 0) { 4247 nvpair_t *elem; 4248 4249 elem = NULL; 4250 nerr = 0; 4251 while ((elem = nvlist_next_nvpair(nverrlist, 4252 elem)) != NULL) { 4253 nerr++; 4254 } 4255 } 4256 nvlist_free(nverrlist); 4257 4258 (void) printf("\n"); 4259 4260 if (nerr == 0) 4261 (void) printf(gettext("errors: No known data " 4262 "errors\n")); 4263 else if (!cbp->cb_verbose) 4264 (void) printf(gettext("errors: %llu data " 4265 "errors, use '-v' for a list\n"), 4266 (u_longlong_t)nerr); 4267 else 4268 print_error_log(zhp); 4269 } 4270 4271 if (cbp->cb_dedup_stats) 4272 print_dedup_stats(config); 4273 } else { 4274 (void) printf(gettext("config: The configuration cannot be " 4275 "determined.\n")); 4276 } 4277 4278 return (0); 4279 } 4280 4281 /* 4282 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] 4283 * 4284 * -v Display complete error logs 4285 * -x Display only pools with potential problems 4286 * -D Display dedup status (undocumented) 4287 * -T Display a timestamp in date(1) or Unix format 4288 * 4289 * Describes the health status of all pools or some subset. 4290 */ 4291 int 4292 zpool_do_status(int argc, char **argv) 4293 { 4294 int c; 4295 int ret; 4296 unsigned long interval = 0, count = 0; 4297 status_cbdata_t cb = { 0 }; 4298 4299 /* check options */ 4300 while ((c = getopt(argc, argv, "vxDT:")) != -1) { 4301 switch (c) { 4302 case 'v': 4303 cb.cb_verbose = B_TRUE; 4304 break; 4305 case 'x': 4306 cb.cb_explain = B_TRUE; 4307 break; 4308 case 'D': 4309 cb.cb_dedup_stats = B_TRUE; 4310 break; 4311 case 'T': 4312 get_timestamp_arg(*optarg); 4313 break; 4314 case '?': 4315 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4316 optopt); 4317 usage(B_FALSE); 4318 } 4319 } 4320 4321 argc -= optind; 4322 argv += optind; 4323 4324 get_interval_count(&argc, argv, &interval, &count); 4325 4326 if (argc == 0) 4327 cb.cb_allpools = B_TRUE; 4328 4329 cb.cb_first = B_TRUE; 4330 4331 for (;;) { 4332 if (timestamp_fmt != NODATE) 4333 print_timestamp(timestamp_fmt); 4334 4335 ret = for_each_pool(argc, argv, B_TRUE, NULL, 4336 status_callback, &cb); 4337 4338 if (argc == 0 && cb.cb_count == 0) 4339 (void) printf(gettext("no pools available\n")); 4340 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 4341 (void) printf(gettext("all pools are healthy\n")); 4342 4343 if (ret != 0) 4344 return (ret); 4345 4346 if (interval == 0) 4347 break; 4348 4349 if (count != 0 && --count == 0) 4350 break; 4351 4352 (void) sleep(interval); 4353 } 4354 4355 return (0); 4356 } 4357 4358 typedef struct upgrade_cbdata { 4359 int cb_first; 4360 int cb_argc; 4361 uint64_t cb_version; 4362 char **cb_argv; 4363 } upgrade_cbdata_t; 4364 4365 static int 4366 upgrade_version(zpool_handle_t *zhp, uint64_t version) 4367 { 4368 int ret; 4369 nvlist_t *config; 4370 uint64_t oldversion; 4371 4372 config = zpool_get_config(zhp, NULL); 4373 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4374 &oldversion) == 0); 4375 4376 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 4377 assert(oldversion < version); 4378 4379 ret = zpool_upgrade(zhp, version); 4380 if (ret != 0) 4381 return (ret); 4382 4383 if (version >= SPA_VERSION_FEATURES) { 4384 (void) printf(gettext("Successfully upgraded " 4385 "'%s' from version %llu to feature flags.\n"), 4386 zpool_get_name(zhp), oldversion); 4387 } else { 4388 (void) printf(gettext("Successfully upgraded " 4389 "'%s' from version %llu to version %llu.\n"), 4390 zpool_get_name(zhp), oldversion, version); 4391 } 4392 4393 return (0); 4394 } 4395 4396 static int 4397 upgrade_enable_all(zpool_handle_t *zhp, int *countp) 4398 { 4399 int i, ret, count; 4400 boolean_t firstff = B_TRUE; 4401 nvlist_t *enabled = zpool_get_features(zhp); 4402 4403 count = 0; 4404 for (i = 0; i < SPA_FEATURES; i++) { 4405 const char *fname = spa_feature_table[i].fi_uname; 4406 const char *fguid = spa_feature_table[i].fi_guid; 4407 if (!nvlist_exists(enabled, fguid)) { 4408 char *propname; 4409 verify(-1 != asprintf(&propname, "feature@%s", fname)); 4410 ret = zpool_set_prop(zhp, propname, 4411 ZFS_FEATURE_ENABLED); 4412 if (ret != 0) { 4413 free(propname); 4414 return (ret); 4415 } 4416 count++; 4417 4418 if (firstff) { 4419 (void) printf(gettext("Enabled the " 4420 "following features on '%s':\n"), 4421 zpool_get_name(zhp)); 4422 firstff = B_FALSE; 4423 } 4424 (void) printf(gettext(" %s\n"), fname); 4425 free(propname); 4426 } 4427 } 4428 4429 if (countp != NULL) 4430 *countp = count; 4431 return (0); 4432 } 4433 4434 static int 4435 upgrade_cb(zpool_handle_t *zhp, void *arg) 4436 { 4437 upgrade_cbdata_t *cbp = arg; 4438 nvlist_t *config; 4439 uint64_t version; 4440 boolean_t printnl = B_FALSE; 4441 int ret; 4442 4443 config = zpool_get_config(zhp, NULL); 4444 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4445 &version) == 0); 4446 4447 assert(SPA_VERSION_IS_SUPPORTED(version)); 4448 4449 if (version < cbp->cb_version) { 4450 cbp->cb_first = B_FALSE; 4451 ret = upgrade_version(zhp, cbp->cb_version); 4452 if (ret != 0) 4453 return (ret); 4454 printnl = B_TRUE; 4455 4456 /* 4457 * If they did "zpool upgrade -a", then we could 4458 * be doing ioctls to different pools. We need 4459 * to log this history once to each pool, and bypass 4460 * the normal history logging that happens in main(). 4461 */ 4462 (void) zpool_log_history(g_zfs, history_str); 4463 log_history = B_FALSE; 4464 } 4465 4466 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4467 int count; 4468 ret = upgrade_enable_all(zhp, &count); 4469 if (ret != 0) 4470 return (ret); 4471 4472 if (count > 0) { 4473 cbp->cb_first = B_FALSE; 4474 printnl = B_TRUE; 4475 } 4476 } 4477 4478 if (printnl) { 4479 (void) printf(gettext("\n")); 4480 } 4481 4482 return (0); 4483 } 4484 4485 static int 4486 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 4487 { 4488 upgrade_cbdata_t *cbp = arg; 4489 nvlist_t *config; 4490 uint64_t version; 4491 4492 config = zpool_get_config(zhp, NULL); 4493 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4494 &version) == 0); 4495 4496 assert(SPA_VERSION_IS_SUPPORTED(version)); 4497 4498 if (version < SPA_VERSION_FEATURES) { 4499 if (cbp->cb_first) { 4500 (void) printf(gettext("The following pools are " 4501 "formatted with legacy version numbers and can\n" 4502 "be upgraded to use feature flags. After " 4503 "being upgraded, these pools\nwill no " 4504 "longer be accessible by software that does not " 4505 "support feature\nflags.\n\n")); 4506 (void) printf(gettext("VER POOL\n")); 4507 (void) printf(gettext("--- ------------\n")); 4508 cbp->cb_first = B_FALSE; 4509 } 4510 4511 (void) printf("%2llu %s\n", (u_longlong_t)version, 4512 zpool_get_name(zhp)); 4513 } 4514 4515 return (0); 4516 } 4517 4518 static int 4519 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 4520 { 4521 upgrade_cbdata_t *cbp = arg; 4522 nvlist_t *config; 4523 uint64_t version; 4524 4525 config = zpool_get_config(zhp, NULL); 4526 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4527 &version) == 0); 4528 4529 if (version >= SPA_VERSION_FEATURES) { 4530 int i; 4531 boolean_t poolfirst = B_TRUE; 4532 nvlist_t *enabled = zpool_get_features(zhp); 4533 4534 for (i = 0; i < SPA_FEATURES; i++) { 4535 const char *fguid = spa_feature_table[i].fi_guid; 4536 const char *fname = spa_feature_table[i].fi_uname; 4537 if (!nvlist_exists(enabled, fguid)) { 4538 if (cbp->cb_first) { 4539 (void) printf(gettext("\nSome " 4540 "supported features are not " 4541 "enabled on the following pools. " 4542 "Once a\nfeature is enabled the " 4543 "pool may become incompatible with " 4544 "software\nthat does not support " 4545 "the feature. See " 4546 "zpool-features(5) for " 4547 "details.\n\n")); 4548 (void) printf(gettext("POOL " 4549 "FEATURE\n")); 4550 (void) printf(gettext("------" 4551 "---------\n")); 4552 cbp->cb_first = B_FALSE; 4553 } 4554 4555 if (poolfirst) { 4556 (void) printf(gettext("%s\n"), 4557 zpool_get_name(zhp)); 4558 poolfirst = B_FALSE; 4559 } 4560 4561 (void) printf(gettext(" %s\n"), fname); 4562 } 4563 } 4564 } 4565 4566 return (0); 4567 } 4568 4569 /* ARGSUSED */ 4570 static int 4571 upgrade_one(zpool_handle_t *zhp, void *data) 4572 { 4573 boolean_t printnl = B_FALSE; 4574 upgrade_cbdata_t *cbp = data; 4575 uint64_t cur_version; 4576 int ret; 4577 4578 if (strcmp("log", zpool_get_name(zhp)) == 0) { 4579 (void) printf(gettext("'log' is now a reserved word\n" 4580 "Pool 'log' must be renamed using export and import" 4581 " to upgrade.\n")); 4582 return (1); 4583 } 4584 4585 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4586 if (cur_version > cbp->cb_version) { 4587 (void) printf(gettext("Pool '%s' is already formatted " 4588 "using more current version '%llu'.\n\n"), 4589 zpool_get_name(zhp), cur_version); 4590 return (0); 4591 } 4592 4593 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 4594 (void) printf(gettext("Pool '%s' is already formatted " 4595 "using version %llu.\n\n"), zpool_get_name(zhp), 4596 cbp->cb_version); 4597 return (0); 4598 } 4599 4600 if (cur_version != cbp->cb_version) { 4601 printnl = B_TRUE; 4602 ret = upgrade_version(zhp, cbp->cb_version); 4603 if (ret != 0) 4604 return (ret); 4605 } 4606 4607 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4608 int count = 0; 4609 ret = upgrade_enable_all(zhp, &count); 4610 if (ret != 0) 4611 return (ret); 4612 4613 if (count != 0) { 4614 printnl = B_TRUE; 4615 } else if (cur_version == SPA_VERSION) { 4616 (void) printf(gettext("Pool '%s' already has all " 4617 "supported features enabled.\n"), 4618 zpool_get_name(zhp)); 4619 } 4620 } 4621 4622 if (printnl) { 4623 (void) printf(gettext("\n")); 4624 } 4625 4626 return (0); 4627 } 4628 4629 /* 4630 * zpool upgrade 4631 * zpool upgrade -v 4632 * zpool upgrade [-V version] <-a | pool ...> 4633 * 4634 * With no arguments, display downrev'd ZFS pool available for upgrade. 4635 * Individual pools can be upgraded by specifying the pool, and '-a' will 4636 * upgrade all pools. 4637 */ 4638 int 4639 zpool_do_upgrade(int argc, char **argv) 4640 { 4641 int c; 4642 upgrade_cbdata_t cb = { 0 }; 4643 int ret = 0; 4644 boolean_t showversions = B_FALSE; 4645 boolean_t upgradeall = B_FALSE; 4646 char *end; 4647 4648 4649 /* check options */ 4650 while ((c = getopt(argc, argv, ":avV:")) != -1) { 4651 switch (c) { 4652 case 'a': 4653 upgradeall = B_TRUE; 4654 break; 4655 case 'v': 4656 showversions = B_TRUE; 4657 break; 4658 case 'V': 4659 cb.cb_version = strtoll(optarg, &end, 10); 4660 if (*end != '\0' || 4661 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 4662 (void) fprintf(stderr, 4663 gettext("invalid version '%s'\n"), optarg); 4664 usage(B_FALSE); 4665 } 4666 break; 4667 case ':': 4668 (void) fprintf(stderr, gettext("missing argument for " 4669 "'%c' option\n"), optopt); 4670 usage(B_FALSE); 4671 break; 4672 case '?': 4673 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4674 optopt); 4675 usage(B_FALSE); 4676 } 4677 } 4678 4679 cb.cb_argc = argc; 4680 cb.cb_argv = argv; 4681 argc -= optind; 4682 argv += optind; 4683 4684 if (cb.cb_version == 0) { 4685 cb.cb_version = SPA_VERSION; 4686 } else if (!upgradeall && argc == 0) { 4687 (void) fprintf(stderr, gettext("-V option is " 4688 "incompatible with other arguments\n")); 4689 usage(B_FALSE); 4690 } 4691 4692 if (showversions) { 4693 if (upgradeall || argc != 0) { 4694 (void) fprintf(stderr, gettext("-v option is " 4695 "incompatible with other arguments\n")); 4696 usage(B_FALSE); 4697 } 4698 } else if (upgradeall) { 4699 if (argc != 0) { 4700 (void) fprintf(stderr, gettext("-a option should not " 4701 "be used along with a pool name\n")); 4702 usage(B_FALSE); 4703 } 4704 } 4705 4706 (void) printf(gettext("This system supports ZFS pool feature " 4707 "flags.\n\n")); 4708 if (showversions) { 4709 int i; 4710 4711 (void) printf(gettext("The following features are " 4712 "supported:\n\n")); 4713 (void) printf(gettext("FEAT DESCRIPTION\n")); 4714 (void) printf("----------------------------------------------" 4715 "---------------\n"); 4716 for (i = 0; i < SPA_FEATURES; i++) { 4717 zfeature_info_t *fi = &spa_feature_table[i]; 4718 const char *ro = 4719 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 4720 " (read-only compatible)" : ""; 4721 4722 (void) printf("%-37s%s\n", fi->fi_uname, ro); 4723 (void) printf(" %s\n", fi->fi_desc); 4724 } 4725 (void) printf("\n"); 4726 4727 (void) printf(gettext("The following legacy versions are also " 4728 "supported:\n\n")); 4729 (void) printf(gettext("VER DESCRIPTION\n")); 4730 (void) printf("--- -----------------------------------------" 4731 "---------------\n"); 4732 (void) printf(gettext(" 1 Initial ZFS version\n")); 4733 (void) printf(gettext(" 2 Ditto blocks " 4734 "(replicated metadata)\n")); 4735 (void) printf(gettext(" 3 Hot spares and double parity " 4736 "RAID-Z\n")); 4737 (void) printf(gettext(" 4 zpool history\n")); 4738 (void) printf(gettext(" 5 Compression using the gzip " 4739 "algorithm\n")); 4740 (void) printf(gettext(" 6 bootfs pool property\n")); 4741 (void) printf(gettext(" 7 Separate intent log devices\n")); 4742 (void) printf(gettext(" 8 Delegated administration\n")); 4743 (void) printf(gettext(" 9 refquota and refreservation " 4744 "properties\n")); 4745 (void) printf(gettext(" 10 Cache devices\n")); 4746 (void) printf(gettext(" 11 Improved scrub performance\n")); 4747 (void) printf(gettext(" 12 Snapshot properties\n")); 4748 (void) printf(gettext(" 13 snapused property\n")); 4749 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 4750 (void) printf(gettext(" 15 user/group space accounting\n")); 4751 (void) printf(gettext(" 16 stmf property support\n")); 4752 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 4753 (void) printf(gettext(" 18 Snapshot user holds\n")); 4754 (void) printf(gettext(" 19 Log device removal\n")); 4755 (void) printf(gettext(" 20 Compression using zle " 4756 "(zero-length encoding)\n")); 4757 (void) printf(gettext(" 21 Deduplication\n")); 4758 (void) printf(gettext(" 22 Received properties\n")); 4759 (void) printf(gettext(" 23 Slim ZIL\n")); 4760 (void) printf(gettext(" 24 System attributes\n")); 4761 (void) printf(gettext(" 25 Improved scrub stats\n")); 4762 (void) printf(gettext(" 26 Improved snapshot deletion " 4763 "performance\n")); 4764 (void) printf(gettext(" 27 Improved snapshot creation " 4765 "performance\n")); 4766 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 4767 (void) printf(gettext("\nFor more information on a particular " 4768 "version, including supported releases,\n")); 4769 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 4770 } else if (argc == 0 && upgradeall) { 4771 cb.cb_first = B_TRUE; 4772 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 4773 if (ret == 0 && cb.cb_first) { 4774 if (cb.cb_version == SPA_VERSION) { 4775 (void) printf(gettext("All pools are already " 4776 "formatted using feature flags.\n\n")); 4777 (void) printf(gettext("Every feature flags " 4778 "pool already has all supported features " 4779 "enabled.\n")); 4780 } else { 4781 (void) printf(gettext("All pools are already " 4782 "formatted with version %llu or higher.\n"), 4783 cb.cb_version); 4784 } 4785 } 4786 } else if (argc == 0) { 4787 cb.cb_first = B_TRUE; 4788 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 4789 assert(ret == 0); 4790 4791 if (cb.cb_first) { 4792 (void) printf(gettext("All pools are formatted " 4793 "using feature flags.\n\n")); 4794 } else { 4795 (void) printf(gettext("\nUse 'zpool upgrade -v' " 4796 "for a list of available legacy versions.\n")); 4797 } 4798 4799 cb.cb_first = B_TRUE; 4800 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 4801 assert(ret == 0); 4802 4803 if (cb.cb_first) { 4804 (void) printf(gettext("Every feature flags pool has " 4805 "all supported features enabled.\n")); 4806 } else { 4807 (void) printf(gettext("\n")); 4808 } 4809 } else { 4810 ret = for_each_pool(argc, argv, B_FALSE, NULL, 4811 upgrade_one, &cb); 4812 } 4813 4814 return (ret); 4815 } 4816 4817 typedef struct hist_cbdata { 4818 boolean_t first; 4819 boolean_t longfmt; 4820 boolean_t internal; 4821 } hist_cbdata_t; 4822 4823 /* 4824 * Print out the command history for a specific pool. 4825 */ 4826 static int 4827 get_history_one(zpool_handle_t *zhp, void *data) 4828 { 4829 nvlist_t *nvhis; 4830 nvlist_t **records; 4831 uint_t numrecords; 4832 int ret, i; 4833 hist_cbdata_t *cb = (hist_cbdata_t *)data; 4834 4835 cb->first = B_FALSE; 4836 4837 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 4838 4839 if ((ret = zpool_get_history(zhp, &nvhis)) != 0) 4840 return (ret); 4841 4842 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 4843 &records, &numrecords) == 0); 4844 for (i = 0; i < numrecords; i++) { 4845 nvlist_t *rec = records[i]; 4846 char tbuf[30] = ""; 4847 4848 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 4849 time_t tsec; 4850 struct tm t; 4851 4852 tsec = fnvlist_lookup_uint64(records[i], 4853 ZPOOL_HIST_TIME); 4854 (void) localtime_r(&tsec, &t); 4855 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 4856 } 4857 4858 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 4859 (void) printf("%s %s", tbuf, 4860 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 4861 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 4862 int ievent = 4863 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 4864 if (!cb->internal) 4865 continue; 4866 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 4867 (void) printf("%s unrecognized record:\n", 4868 tbuf); 4869 dump_nvlist(rec, 4); 4870 continue; 4871 } 4872 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 4873 zfs_history_event_names[ievent], 4874 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 4875 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 4876 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 4877 if (!cb->internal) 4878 continue; 4879 (void) printf("%s [txg:%lld] %s", tbuf, 4880 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 4881 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 4882 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 4883 (void) printf(" %s (%llu)", 4884 fnvlist_lookup_string(rec, 4885 ZPOOL_HIST_DSNAME), 4886 fnvlist_lookup_uint64(rec, 4887 ZPOOL_HIST_DSID)); 4888 } 4889 (void) printf(" %s", fnvlist_lookup_string(rec, 4890 ZPOOL_HIST_INT_STR)); 4891 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 4892 if (!cb->internal) 4893 continue; 4894 (void) printf("%s ioctl %s\n", tbuf, 4895 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 4896 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 4897 (void) printf(" input:\n"); 4898 dump_nvlist(fnvlist_lookup_nvlist(rec, 4899 ZPOOL_HIST_INPUT_NVL), 8); 4900 } 4901 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 4902 (void) printf(" output:\n"); 4903 dump_nvlist(fnvlist_lookup_nvlist(rec, 4904 ZPOOL_HIST_OUTPUT_NVL), 8); 4905 } 4906 } else { 4907 if (!cb->internal) 4908 continue; 4909 (void) printf("%s unrecognized record:\n", tbuf); 4910 dump_nvlist(rec, 4); 4911 } 4912 4913 if (!cb->longfmt) { 4914 (void) printf("\n"); 4915 continue; 4916 } 4917 (void) printf(" ["); 4918 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 4919 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 4920 struct passwd *pwd = getpwuid(who); 4921 (void) printf("user %d ", (int)who); 4922 if (pwd != NULL) 4923 (void) printf("(%s) ", pwd->pw_name); 4924 } 4925 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 4926 (void) printf("on %s", 4927 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 4928 } 4929 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 4930 (void) printf(":%s", 4931 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 4932 } 4933 (void) printf("]"); 4934 (void) printf("\n"); 4935 } 4936 (void) printf("\n"); 4937 nvlist_free(nvhis); 4938 4939 return (ret); 4940 } 4941 4942 /* 4943 * zpool history <pool> 4944 * 4945 * Displays the history of commands that modified pools. 4946 */ 4947 int 4948 zpool_do_history(int argc, char **argv) 4949 { 4950 hist_cbdata_t cbdata = { 0 }; 4951 int ret; 4952 int c; 4953 4954 cbdata.first = B_TRUE; 4955 /* check options */ 4956 while ((c = getopt(argc, argv, "li")) != -1) { 4957 switch (c) { 4958 case 'l': 4959 cbdata.longfmt = B_TRUE; 4960 break; 4961 case 'i': 4962 cbdata.internal = B_TRUE; 4963 break; 4964 case '?': 4965 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4966 optopt); 4967 usage(B_FALSE); 4968 } 4969 } 4970 argc -= optind; 4971 argv += optind; 4972 4973 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, 4974 &cbdata); 4975 4976 if (argc == 0 && cbdata.first == B_TRUE) { 4977 (void) printf(gettext("no pools available\n")); 4978 return (0); 4979 } 4980 4981 return (ret); 4982 } 4983 4984 static int 4985 get_callback(zpool_handle_t *zhp, void *data) 4986 { 4987 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 4988 char value[MAXNAMELEN]; 4989 zprop_source_t srctype; 4990 zprop_list_t *pl; 4991 4992 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 4993 4994 /* 4995 * Skip the special fake placeholder. This will also skip 4996 * over the name property when 'all' is specified. 4997 */ 4998 if (pl->pl_prop == ZPOOL_PROP_NAME && 4999 pl == cbp->cb_proplist) 5000 continue; 5001 5002 if (pl->pl_prop == ZPROP_INVAL && 5003 (zpool_prop_feature(pl->pl_user_prop) || 5004 zpool_prop_unsupported(pl->pl_user_prop))) { 5005 srctype = ZPROP_SRC_LOCAL; 5006 5007 if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 5008 value, sizeof (value)) == 0) { 5009 zprop_print_one_property(zpool_get_name(zhp), 5010 cbp, pl->pl_user_prop, value, srctype, 5011 NULL, NULL); 5012 } 5013 } else { 5014 if (zpool_get_prop(zhp, pl->pl_prop, value, 5015 sizeof (value), &srctype, cbp->cb_literal) != 0) 5016 continue; 5017 5018 zprop_print_one_property(zpool_get_name(zhp), cbp, 5019 zpool_prop_to_name(pl->pl_prop), value, srctype, 5020 NULL, NULL); 5021 } 5022 } 5023 return (0); 5024 } 5025 5026 /* 5027 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 5028 * 5029 * -H Scripted mode. Don't display headers, and separate properties 5030 * by a single tab. 5031 * -o List of columns to display. Defaults to 5032 * "name,property,value,source". 5033 * -p Diplay values in parsable (exact) format. 5034 * 5035 * Get properties of pools in the system. Output space statistics 5036 * for each one as well as other attributes. 5037 */ 5038 int 5039 zpool_do_get(int argc, char **argv) 5040 { 5041 zprop_get_cbdata_t cb = { 0 }; 5042 zprop_list_t fake_name = { 0 }; 5043 int ret; 5044 int c, i; 5045 char *value; 5046 5047 cb.cb_first = B_TRUE; 5048 5049 /* 5050 * Set up default columns and sources. 5051 */ 5052 cb.cb_sources = ZPROP_SRC_ALL; 5053 cb.cb_columns[0] = GET_COL_NAME; 5054 cb.cb_columns[1] = GET_COL_PROPERTY; 5055 cb.cb_columns[2] = GET_COL_VALUE; 5056 cb.cb_columns[3] = GET_COL_SOURCE; 5057 cb.cb_type = ZFS_TYPE_POOL; 5058 5059 /* check options */ 5060 while ((c = getopt(argc, argv, ":Hpo:")) != -1) { 5061 switch (c) { 5062 case 'p': 5063 cb.cb_literal = B_TRUE; 5064 break; 5065 case 'H': 5066 cb.cb_scripted = B_TRUE; 5067 break; 5068 case 'o': 5069 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 5070 i = 0; 5071 while (*optarg != '\0') { 5072 static char *col_subopts[] = 5073 { "name", "property", "value", "source", 5074 "all", NULL }; 5075 5076 if (i == ZFS_GET_NCOLS) { 5077 (void) fprintf(stderr, gettext("too " 5078 "many fields given to -o " 5079 "option\n")); 5080 usage(B_FALSE); 5081 } 5082 5083 switch (getsubopt(&optarg, col_subopts, 5084 &value)) { 5085 case 0: 5086 cb.cb_columns[i++] = GET_COL_NAME; 5087 break; 5088 case 1: 5089 cb.cb_columns[i++] = GET_COL_PROPERTY; 5090 break; 5091 case 2: 5092 cb.cb_columns[i++] = GET_COL_VALUE; 5093 break; 5094 case 3: 5095 cb.cb_columns[i++] = GET_COL_SOURCE; 5096 break; 5097 case 4: 5098 if (i > 0) { 5099 (void) fprintf(stderr, 5100 gettext("\"all\" conflicts " 5101 "with specific fields " 5102 "given to -o option\n")); 5103 usage(B_FALSE); 5104 } 5105 cb.cb_columns[0] = GET_COL_NAME; 5106 cb.cb_columns[1] = GET_COL_PROPERTY; 5107 cb.cb_columns[2] = GET_COL_VALUE; 5108 cb.cb_columns[3] = GET_COL_SOURCE; 5109 i = ZFS_GET_NCOLS; 5110 break; 5111 default: 5112 (void) fprintf(stderr, 5113 gettext("invalid column name " 5114 "'%s'\n"), value); 5115 usage(B_FALSE); 5116 } 5117 } 5118 break; 5119 case '?': 5120 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5121 optopt); 5122 usage(B_FALSE); 5123 } 5124 } 5125 5126 argc -= optind; 5127 argv += optind; 5128 5129 if (argc < 1) { 5130 (void) fprintf(stderr, gettext("missing property " 5131 "argument\n")); 5132 usage(B_FALSE); 5133 } 5134 5135 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, 5136 ZFS_TYPE_POOL) != 0) 5137 usage(B_FALSE); 5138 5139 argc--; 5140 argv++; 5141 5142 if (cb.cb_proplist != NULL) { 5143 fake_name.pl_prop = ZPOOL_PROP_NAME; 5144 fake_name.pl_width = strlen(gettext("NAME")); 5145 fake_name.pl_next = cb.cb_proplist; 5146 cb.cb_proplist = &fake_name; 5147 } 5148 5149 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, 5150 get_callback, &cb); 5151 5152 if (cb.cb_proplist == &fake_name) 5153 zprop_free_list(fake_name.pl_next); 5154 else 5155 zprop_free_list(cb.cb_proplist); 5156 5157 return (ret); 5158 } 5159 5160 typedef struct set_cbdata { 5161 char *cb_propname; 5162 char *cb_value; 5163 boolean_t cb_any_successful; 5164 } set_cbdata_t; 5165 5166 int 5167 set_callback(zpool_handle_t *zhp, void *data) 5168 { 5169 int error; 5170 set_cbdata_t *cb = (set_cbdata_t *)data; 5171 5172 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 5173 5174 if (!error) 5175 cb->cb_any_successful = B_TRUE; 5176 5177 return (error); 5178 } 5179 5180 int 5181 zpool_do_set(int argc, char **argv) 5182 { 5183 set_cbdata_t cb = { 0 }; 5184 int error; 5185 5186 if (argc > 1 && argv[1][0] == '-') { 5187 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5188 argv[1][1]); 5189 usage(B_FALSE); 5190 } 5191 5192 if (argc < 2) { 5193 (void) fprintf(stderr, gettext("missing property=value " 5194 "argument\n")); 5195 usage(B_FALSE); 5196 } 5197 5198 if (argc < 3) { 5199 (void) fprintf(stderr, gettext("missing pool name\n")); 5200 usage(B_FALSE); 5201 } 5202 5203 if (argc > 3) { 5204 (void) fprintf(stderr, gettext("too many pool names\n")); 5205 usage(B_FALSE); 5206 } 5207 5208 cb.cb_propname = argv[1]; 5209 cb.cb_value = strchr(cb.cb_propname, '='); 5210 if (cb.cb_value == NULL) { 5211 (void) fprintf(stderr, gettext("missing value in " 5212 "property=value argument\n")); 5213 usage(B_FALSE); 5214 } 5215 5216 *(cb.cb_value) = '\0'; 5217 cb.cb_value++; 5218 5219 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, 5220 set_callback, &cb); 5221 5222 return (error); 5223 } 5224 5225 static int 5226 find_command_idx(char *command, int *idx) 5227 { 5228 int i; 5229 5230 for (i = 0; i < NCOMMAND; i++) { 5231 if (command_table[i].name == NULL) 5232 continue; 5233 5234 if (strcmp(command, command_table[i].name) == 0) { 5235 *idx = i; 5236 return (0); 5237 } 5238 } 5239 return (1); 5240 } 5241 5242 int 5243 main(int argc, char **argv) 5244 { 5245 int ret = 0; 5246 int i; 5247 char *cmdname; 5248 5249 (void) setlocale(LC_ALL, ""); 5250 (void) textdomain(TEXT_DOMAIN); 5251 5252 if ((g_zfs = libzfs_init()) == NULL) { 5253 (void) fprintf(stderr, gettext("internal error: failed to " 5254 "initialize ZFS library\n")); 5255 return (1); 5256 } 5257 5258 libzfs_print_on_error(g_zfs, B_TRUE); 5259 5260 opterr = 0; 5261 5262 /* 5263 * Make sure the user has specified some command. 5264 */ 5265 if (argc < 2) { 5266 (void) fprintf(stderr, gettext("missing command\n")); 5267 usage(B_FALSE); 5268 } 5269 5270 cmdname = argv[1]; 5271 5272 /* 5273 * Special case '-?' 5274 */ 5275 if (strcmp(cmdname, "-?") == 0) 5276 usage(B_TRUE); 5277 5278 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 5279 5280 /* 5281 * Run the appropriate command. 5282 */ 5283 if (find_command_idx(cmdname, &i) == 0) { 5284 current_command = &command_table[i]; 5285 ret = command_table[i].func(argc - 1, argv + 1); 5286 } else if (strchr(cmdname, '=')) { 5287 verify(find_command_idx("set", &i) == 0); 5288 current_command = &command_table[i]; 5289 ret = command_table[i].func(argc, argv); 5290 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 5291 /* 5292 * 'freeze' is a vile debugging abomination, so we treat 5293 * it as such. 5294 */ 5295 char buf[16384]; 5296 int fd = open(ZFS_DEV, O_RDWR); 5297 (void) strcpy((void *)buf, argv[2]); 5298 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf)); 5299 } else { 5300 (void) fprintf(stderr, gettext("unrecognized " 5301 "command '%s'\n"), cmdname); 5302 usage(B_FALSE); 5303 } 5304 5305 if (ret == 0 && log_history) 5306 (void) zpool_log_history(g_zfs, history_str); 5307 5308 libzfs_fini(g_zfs); 5309 5310 /* 5311 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 5312 * for the purposes of running ::findleaks. 5313 */ 5314 if (getenv("ZFS_ABORT") != NULL) { 5315 (void) printf("dumping core by request\n"); 5316 abort(); 5317 } 5318 5319 return (ret); 5320 } 5321