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, 2020 by Delphix. All rights reserved. 26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved. 27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved. 28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved. 29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. 30 * Copyright (c) 2017 Datto Inc. 31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. 32 * Copyright (c) 2017, Intel Corporation. 33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com> 34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> 35 */ 36 37 #include <assert.h> 38 #include <ctype.h> 39 #include <dirent.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <getopt.h> 43 #include <libgen.h> 44 #include <libintl.h> 45 #include <libuutil.h> 46 #include <locale.h> 47 #include <pthread.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <strings.h> 52 #include <time.h> 53 #include <unistd.h> 54 #include <pwd.h> 55 #include <zone.h> 56 #include <sys/wait.h> 57 #include <zfs_prop.h> 58 #include <sys/fs/zfs.h> 59 #include <sys/stat.h> 60 #include <sys/systeminfo.h> 61 #include <sys/fm/fs/zfs.h> 62 #include <sys/fm/util.h> 63 #include <sys/fm/protocol.h> 64 #include <sys/zfs_ioctl.h> 65 #include <sys/mount.h> 66 #include <sys/sysmacros.h> 67 68 #include <math.h> 69 70 #include <libzfs.h> 71 #include <libzutil.h> 72 73 #include "zpool_util.h" 74 #include "zfs_comutil.h" 75 #include "zfeature_common.h" 76 77 #include "statcommon.h" 78 79 libzfs_handle_t *g_zfs; 80 81 static int zpool_do_create(int, char **); 82 static int zpool_do_destroy(int, char **); 83 84 static int zpool_do_add(int, char **); 85 static int zpool_do_remove(int, char **); 86 static int zpool_do_labelclear(int, char **); 87 88 static int zpool_do_checkpoint(int, char **); 89 90 static int zpool_do_list(int, char **); 91 static int zpool_do_iostat(int, char **); 92 static int zpool_do_status(int, char **); 93 94 static int zpool_do_online(int, char **); 95 static int zpool_do_offline(int, char **); 96 static int zpool_do_clear(int, char **); 97 static int zpool_do_reopen(int, char **); 98 99 static int zpool_do_reguid(int, char **); 100 101 static int zpool_do_attach(int, char **); 102 static int zpool_do_detach(int, char **); 103 static int zpool_do_replace(int, char **); 104 static int zpool_do_split(int, char **); 105 106 static int zpool_do_initialize(int, char **); 107 static int zpool_do_scrub(int, char **); 108 static int zpool_do_resilver(int, char **); 109 static int zpool_do_trim(int, char **); 110 111 static int zpool_do_import(int, char **); 112 static int zpool_do_export(int, char **); 113 114 static int zpool_do_upgrade(int, char **); 115 116 static int zpool_do_history(int, char **); 117 static int zpool_do_events(int, char **); 118 119 static int zpool_do_get(int, char **); 120 static int zpool_do_set(int, char **); 121 122 static int zpool_do_sync(int, char **); 123 124 static int zpool_do_version(int, char **); 125 126 static int zpool_do_wait(int, char **); 127 128 static zpool_compat_status_t zpool_do_load_compat( 129 const char *, boolean_t *); 130 131 /* 132 * These libumem hooks provide a reasonable set of defaults for the allocator's 133 * debugging facilities. 134 */ 135 136 #ifdef DEBUG 137 const char * 138 _umem_debug_init(void) 139 { 140 return ("default,verbose"); /* $UMEM_DEBUG setting */ 141 } 142 143 const char * 144 _umem_logging_init(void) 145 { 146 return ("fail,contents"); /* $UMEM_LOGGING setting */ 147 } 148 #endif 149 150 typedef enum { 151 HELP_ADD, 152 HELP_ATTACH, 153 HELP_CLEAR, 154 HELP_CREATE, 155 HELP_CHECKPOINT, 156 HELP_DESTROY, 157 HELP_DETACH, 158 HELP_EXPORT, 159 HELP_HISTORY, 160 HELP_IMPORT, 161 HELP_IOSTAT, 162 HELP_LABELCLEAR, 163 HELP_LIST, 164 HELP_OFFLINE, 165 HELP_ONLINE, 166 HELP_REPLACE, 167 HELP_REMOVE, 168 HELP_INITIALIZE, 169 HELP_SCRUB, 170 HELP_RESILVER, 171 HELP_TRIM, 172 HELP_STATUS, 173 HELP_UPGRADE, 174 HELP_EVENTS, 175 HELP_GET, 176 HELP_SET, 177 HELP_SPLIT, 178 HELP_SYNC, 179 HELP_REGUID, 180 HELP_REOPEN, 181 HELP_VERSION, 182 HELP_WAIT 183 } zpool_help_t; 184 185 186 /* 187 * Flags for stats to display with "zpool iostats" 188 */ 189 enum iostat_type { 190 IOS_DEFAULT = 0, 191 IOS_LATENCY = 1, 192 IOS_QUEUES = 2, 193 IOS_L_HISTO = 3, 194 IOS_RQ_HISTO = 4, 195 IOS_COUNT, /* always last element */ 196 }; 197 198 /* iostat_type entries as bitmasks */ 199 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT) 200 #define IOS_LATENCY_M (1ULL << IOS_LATENCY) 201 #define IOS_QUEUES_M (1ULL << IOS_QUEUES) 202 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO) 203 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO) 204 205 /* Mask of all the histo bits */ 206 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M) 207 208 /* 209 * Lookup table for iostat flags to nvlist names. Basically a list 210 * of all the nvlists a flag requires. Also specifies the order in 211 * which data gets printed in zpool iostat. 212 */ 213 static const char *vsx_type_to_nvlist[IOS_COUNT][13] = { 214 [IOS_L_HISTO] = { 215 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 216 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 217 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 218 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 219 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, 220 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, 221 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, 222 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, 223 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, 224 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 225 NULL}, 226 [IOS_LATENCY] = { 227 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 228 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 229 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 230 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 231 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 232 NULL}, 233 [IOS_QUEUES] = { 234 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, 235 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, 236 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, 237 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, 238 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, 239 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, 240 NULL}, 241 [IOS_RQ_HISTO] = { 242 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO, 243 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO, 244 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO, 245 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO, 246 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO, 247 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO, 248 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO, 249 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO, 250 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO, 251 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO, 252 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO, 253 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO, 254 NULL}, 255 }; 256 257 258 /* 259 * Given a cb->cb_flags with a histogram bit set, return the iostat_type. 260 * Right now, only one histo bit is ever set at one time, so we can 261 * just do a highbit64(a) 262 */ 263 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1) 264 265 typedef struct zpool_command { 266 const char *name; 267 int (*func)(int, char **); 268 zpool_help_t usage; 269 } zpool_command_t; 270 271 /* 272 * Master command table. Each ZFS command has a name, associated function, and 273 * usage message. The usage messages need to be internationalized, so we have 274 * to have a function to return the usage message based on a command index. 275 * 276 * These commands are organized according to how they are displayed in the usage 277 * message. An empty command (one with a NULL name) indicates an empty line in 278 * the generic usage message. 279 */ 280 static zpool_command_t command_table[] = { 281 { "version", zpool_do_version, HELP_VERSION }, 282 { NULL }, 283 { "create", zpool_do_create, HELP_CREATE }, 284 { "destroy", zpool_do_destroy, HELP_DESTROY }, 285 { NULL }, 286 { "add", zpool_do_add, HELP_ADD }, 287 { "remove", zpool_do_remove, HELP_REMOVE }, 288 { NULL }, 289 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, 290 { NULL }, 291 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT }, 292 { NULL }, 293 { "list", zpool_do_list, HELP_LIST }, 294 { "iostat", zpool_do_iostat, HELP_IOSTAT }, 295 { "status", zpool_do_status, HELP_STATUS }, 296 { NULL }, 297 { "online", zpool_do_online, HELP_ONLINE }, 298 { "offline", zpool_do_offline, HELP_OFFLINE }, 299 { "clear", zpool_do_clear, HELP_CLEAR }, 300 { "reopen", zpool_do_reopen, HELP_REOPEN }, 301 { NULL }, 302 { "attach", zpool_do_attach, HELP_ATTACH }, 303 { "detach", zpool_do_detach, HELP_DETACH }, 304 { "replace", zpool_do_replace, HELP_REPLACE }, 305 { "split", zpool_do_split, HELP_SPLIT }, 306 { NULL }, 307 { "initialize", zpool_do_initialize, HELP_INITIALIZE }, 308 { "resilver", zpool_do_resilver, HELP_RESILVER }, 309 { "scrub", zpool_do_scrub, HELP_SCRUB }, 310 { "trim", zpool_do_trim, HELP_TRIM }, 311 { NULL }, 312 { "import", zpool_do_import, HELP_IMPORT }, 313 { "export", zpool_do_export, HELP_EXPORT }, 314 { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 315 { "reguid", zpool_do_reguid, HELP_REGUID }, 316 { NULL }, 317 { "history", zpool_do_history, HELP_HISTORY }, 318 { "events", zpool_do_events, HELP_EVENTS }, 319 { NULL }, 320 { "get", zpool_do_get, HELP_GET }, 321 { "set", zpool_do_set, HELP_SET }, 322 { "sync", zpool_do_sync, HELP_SYNC }, 323 { NULL }, 324 { "wait", zpool_do_wait, HELP_WAIT }, 325 }; 326 327 #define NCOMMAND (ARRAY_SIZE(command_table)) 328 329 #define VDEV_ALLOC_CLASS_LOGS "logs" 330 331 static zpool_command_t *current_command; 332 static char history_str[HIS_MAX_RECORD_LEN]; 333 static boolean_t log_history = B_TRUE; 334 static uint_t timestamp_fmt = NODATE; 335 336 static const char * 337 get_usage(zpool_help_t idx) 338 { 339 switch (idx) { 340 case HELP_ADD: 341 return (gettext("\tadd [-fgLnP] [-o property=value] " 342 "<pool> <vdev> ...\n")); 343 case HELP_ATTACH: 344 return (gettext("\tattach [-fsw] [-o property=value] " 345 "<pool> <device> <new-device>\n")); 346 case HELP_CLEAR: 347 return (gettext("\tclear [-nF] <pool> [device]\n")); 348 case HELP_CREATE: 349 return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 350 "\t [-O file-system-property=value] ... \n" 351 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 352 case HELP_CHECKPOINT: 353 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n")); 354 case HELP_DESTROY: 355 return (gettext("\tdestroy [-f] <pool>\n")); 356 case HELP_DETACH: 357 return (gettext("\tdetach <pool> <device>\n")); 358 case HELP_EXPORT: 359 return (gettext("\texport [-af] <pool> ...\n")); 360 case HELP_HISTORY: 361 return (gettext("\thistory [-il] [<pool>] ...\n")); 362 case HELP_IMPORT: 363 return (gettext("\timport [-d dir] [-D]\n" 364 "\timport [-o mntopts] [-o property=value] ... \n" 365 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " 366 "[-R root] [-F [-n]] -a\n" 367 "\timport [-o mntopts] [-o property=value] ... \n" 368 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " 369 "[-R root] [-F [-n]]\n" 370 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n")); 371 case HELP_IOSTAT: 372 return (gettext("\tiostat [[[-c [script1,script2,...]" 373 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n" 374 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]" 375 " [[-n] interval [count]]\n")); 376 case HELP_LABELCLEAR: 377 return (gettext("\tlabelclear [-f] <vdev>\n")); 378 case HELP_LIST: 379 return (gettext("\tlist [-gHLpPv] [-o property[,...]] " 380 "[-T d|u] [pool] ... \n" 381 "\t [interval [count]]\n")); 382 case HELP_OFFLINE: 383 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n")); 384 case HELP_ONLINE: 385 return (gettext("\tonline [-e] <pool> <device> ...\n")); 386 case HELP_REPLACE: 387 return (gettext("\treplace [-fsw] [-o property=value] " 388 "<pool> <device> [new-device]\n")); 389 case HELP_REMOVE: 390 return (gettext("\tremove [-npsw] <pool> <device> ...\n")); 391 case HELP_REOPEN: 392 return (gettext("\treopen [-n] <pool>\n")); 393 case HELP_INITIALIZE: 394 return (gettext("\tinitialize [-c | -s] [-w] <pool> " 395 "[<device> ...]\n")); 396 case HELP_SCRUB: 397 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n")); 398 case HELP_RESILVER: 399 return (gettext("\tresilver <pool> ...\n")); 400 case HELP_TRIM: 401 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> " 402 "[<device> ...]\n")); 403 case HELP_STATUS: 404 return (gettext("\tstatus [-c [script1,script2,...]] " 405 "[-igLpPstvxD] [-T d|u] [pool] ... \n" 406 "\t [interval [count]]\n")); 407 case HELP_UPGRADE: 408 return (gettext("\tupgrade\n" 409 "\tupgrade -v\n" 410 "\tupgrade [-V version] <-a | pool ...>\n")); 411 case HELP_EVENTS: 412 return (gettext("\tevents [-vHf [pool] | -c]\n")); 413 case HELP_GET: 414 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " 415 "<\"all\" | property[,...]> <pool> ...\n")); 416 case HELP_SET: 417 return (gettext("\tset <property=value> <pool> \n")); 418 case HELP_SPLIT: 419 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n" 420 "\t [-o property=value] <pool> <newpool> " 421 "[<device> ...]\n")); 422 case HELP_REGUID: 423 return (gettext("\treguid <pool>\n")); 424 case HELP_SYNC: 425 return (gettext("\tsync [pool] ...\n")); 426 case HELP_VERSION: 427 return (gettext("\tversion\n")); 428 case HELP_WAIT: 429 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] " 430 "<pool> [interval]\n")); 431 } 432 433 abort(); 434 /* NOTREACHED */ 435 } 436 437 static void 438 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res) 439 { 440 uint_t children = 0; 441 nvlist_t **child; 442 uint_t i; 443 444 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 445 &child, &children); 446 447 if (children == 0) { 448 char *path = zpool_vdev_name(g_zfs, zhp, nvroot, 449 VDEV_NAME_PATH); 450 451 if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 && 452 strcmp(path, VDEV_TYPE_HOLE) != 0) 453 fnvlist_add_boolean(res, path); 454 455 free(path); 456 return; 457 } 458 459 for (i = 0; i < children; i++) { 460 zpool_collect_leaves(zhp, child[i], res); 461 } 462 } 463 464 /* 465 * Callback routine that will print out a pool property value. 466 */ 467 static int 468 print_prop_cb(int prop, void *cb) 469 { 470 FILE *fp = cb; 471 472 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop)); 473 474 if (zpool_prop_readonly(prop)) 475 (void) fprintf(fp, " NO "); 476 else 477 (void) fprintf(fp, " YES "); 478 479 if (zpool_prop_values(prop) == NULL) 480 (void) fprintf(fp, "-\n"); 481 else 482 (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 483 484 return (ZPROP_CONT); 485 } 486 487 /* 488 * Display usage message. If we're inside a command, display only the usage for 489 * that command. Otherwise, iterate over the entire command table and display 490 * a complete usage message. 491 */ 492 static void 493 usage(boolean_t requested) 494 { 495 FILE *fp = requested ? stdout : stderr; 496 497 if (current_command == NULL) { 498 int i; 499 500 (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 501 (void) fprintf(fp, 502 gettext("where 'command' is one of the following:\n\n")); 503 504 for (i = 0; i < NCOMMAND; i++) { 505 if (command_table[i].name == NULL) 506 (void) fprintf(fp, "\n"); 507 else 508 (void) fprintf(fp, "%s", 509 get_usage(command_table[i].usage)); 510 } 511 } else { 512 (void) fprintf(fp, gettext("usage:\n")); 513 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 514 } 515 516 if (current_command != NULL && 517 ((strcmp(current_command->name, "set") == 0) || 518 (strcmp(current_command->name, "get") == 0) || 519 (strcmp(current_command->name, "list") == 0))) { 520 521 (void) fprintf(fp, 522 gettext("\nthe following properties are supported:\n")); 523 524 (void) fprintf(fp, "\n\t%-19s %s %s\n\n", 525 "PROPERTY", "EDIT", "VALUES"); 526 527 /* Iterate over all properties */ 528 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, 529 ZFS_TYPE_POOL); 530 531 (void) fprintf(fp, "\t%-19s ", "feature@..."); 532 (void) fprintf(fp, "YES disabled | enabled | active\n"); 533 534 (void) fprintf(fp, gettext("\nThe feature@ properties must be " 535 "appended with a feature name.\nSee zpool-features(5).\n")); 536 } 537 538 /* 539 * See comments at end of main(). 540 */ 541 if (getenv("ZFS_ABORT") != NULL) { 542 (void) printf("dumping core by request\n"); 543 abort(); 544 } 545 546 exit(requested ? 0 : 2); 547 } 548 549 /* 550 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...] 551 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool 552 * if none specified. 553 * 554 * -c Cancel. Ends active initializing. 555 * -s Suspend. Initializing can then be restarted with no flags. 556 * -w Wait. Blocks until initializing has completed. 557 */ 558 int 559 zpool_do_initialize(int argc, char **argv) 560 { 561 int c; 562 char *poolname; 563 zpool_handle_t *zhp; 564 nvlist_t *vdevs; 565 int err = 0; 566 boolean_t wait = B_FALSE; 567 568 struct option long_options[] = { 569 {"cancel", no_argument, NULL, 'c'}, 570 {"suspend", no_argument, NULL, 's'}, 571 {"wait", no_argument, NULL, 'w'}, 572 {0, 0, 0, 0} 573 }; 574 575 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START; 576 while ((c = getopt_long(argc, argv, "csw", long_options, NULL)) != -1) { 577 switch (c) { 578 case 'c': 579 if (cmd_type != POOL_INITIALIZE_START && 580 cmd_type != POOL_INITIALIZE_CANCEL) { 581 (void) fprintf(stderr, gettext("-c cannot be " 582 "combined with other options\n")); 583 usage(B_FALSE); 584 } 585 cmd_type = POOL_INITIALIZE_CANCEL; 586 break; 587 case 's': 588 if (cmd_type != POOL_INITIALIZE_START && 589 cmd_type != POOL_INITIALIZE_SUSPEND) { 590 (void) fprintf(stderr, gettext("-s cannot be " 591 "combined with other options\n")); 592 usage(B_FALSE); 593 } 594 cmd_type = POOL_INITIALIZE_SUSPEND; 595 break; 596 case 'w': 597 wait = B_TRUE; 598 break; 599 case '?': 600 if (optopt != 0) { 601 (void) fprintf(stderr, 602 gettext("invalid option '%c'\n"), optopt); 603 } else { 604 (void) fprintf(stderr, 605 gettext("invalid option '%s'\n"), 606 argv[optind - 1]); 607 } 608 usage(B_FALSE); 609 } 610 } 611 612 argc -= optind; 613 argv += optind; 614 615 if (argc < 1) { 616 (void) fprintf(stderr, gettext("missing pool name argument\n")); 617 usage(B_FALSE); 618 return (-1); 619 } 620 621 if (wait && (cmd_type != POOL_INITIALIZE_START)) { 622 (void) fprintf(stderr, gettext("-w cannot be used with -c or " 623 "-s\n")); 624 usage(B_FALSE); 625 } 626 627 poolname = argv[0]; 628 zhp = zpool_open(g_zfs, poolname); 629 if (zhp == NULL) 630 return (-1); 631 632 vdevs = fnvlist_alloc(); 633 if (argc == 1) { 634 /* no individual leaf vdevs specified, so add them all */ 635 nvlist_t *config = zpool_get_config(zhp, NULL); 636 nvlist_t *nvroot = fnvlist_lookup_nvlist(config, 637 ZPOOL_CONFIG_VDEV_TREE); 638 zpool_collect_leaves(zhp, nvroot, vdevs); 639 } else { 640 for (int i = 1; i < argc; i++) { 641 fnvlist_add_boolean(vdevs, argv[i]); 642 } 643 } 644 645 if (wait) 646 err = zpool_initialize_wait(zhp, cmd_type, vdevs); 647 else 648 err = zpool_initialize(zhp, cmd_type, vdevs); 649 650 fnvlist_free(vdevs); 651 zpool_close(zhp); 652 653 return (err); 654 } 655 656 /* 657 * print a pool vdev config for dry runs 658 */ 659 static void 660 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 661 const char *match, int name_flags) 662 { 663 nvlist_t **child; 664 uint_t c, children; 665 char *vname; 666 boolean_t printed = B_FALSE; 667 668 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 669 &child, &children) != 0) { 670 if (name != NULL) 671 (void) printf("\t%*s%s\n", indent, "", name); 672 return; 673 } 674 675 for (c = 0; c < children; c++) { 676 uint64_t is_log = B_FALSE, is_hole = B_FALSE; 677 char *class = ""; 678 679 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 680 &is_hole); 681 682 if (is_hole == B_TRUE) { 683 continue; 684 } 685 686 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 687 &is_log); 688 if (is_log) 689 class = VDEV_ALLOC_BIAS_LOG; 690 (void) nvlist_lookup_string(child[c], 691 ZPOOL_CONFIG_ALLOCATION_BIAS, &class); 692 if (strcmp(match, class) != 0) 693 continue; 694 695 if (!printed && name != NULL) { 696 (void) printf("\t%*s%s\n", indent, "", name); 697 printed = B_TRUE; 698 } 699 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags); 700 print_vdev_tree(zhp, vname, child[c], indent + 2, "", 701 name_flags); 702 free(vname); 703 } 704 } 705 706 /* 707 * Print the list of l2cache devices for dry runs. 708 */ 709 static void 710 print_cache_list(nvlist_t *nv, int indent) 711 { 712 nvlist_t **child; 713 uint_t c, children; 714 715 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 716 &child, &children) == 0 && children > 0) { 717 (void) printf("\t%*s%s\n", indent, "", "cache"); 718 } else { 719 return; 720 } 721 for (c = 0; c < children; c++) { 722 char *vname; 723 724 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); 725 (void) printf("\t%*s%s\n", indent + 2, "", vname); 726 free(vname); 727 } 728 } 729 730 /* 731 * Print the list of spares for dry runs. 732 */ 733 static void 734 print_spare_list(nvlist_t *nv, int indent) 735 { 736 nvlist_t **child; 737 uint_t c, children; 738 739 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 740 &child, &children) == 0 && children > 0) { 741 (void) printf("\t%*s%s\n", indent, "", "spares"); 742 } else { 743 return; 744 } 745 for (c = 0; c < children; c++) { 746 char *vname; 747 748 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); 749 (void) printf("\t%*s%s\n", indent + 2, "", vname); 750 free(vname); 751 } 752 } 753 754 static boolean_t 755 prop_list_contains_feature(nvlist_t *proplist) 756 { 757 nvpair_t *nvp; 758 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 759 nvp = nvlist_next_nvpair(proplist, nvp)) { 760 if (zpool_prop_feature(nvpair_name(nvp))) 761 return (B_TRUE); 762 } 763 return (B_FALSE); 764 } 765 766 /* 767 * Add a property pair (name, string-value) into a property nvlist. 768 */ 769 static int 770 add_prop_list(const char *propname, char *propval, nvlist_t **props, 771 boolean_t poolprop) 772 { 773 zpool_prop_t prop = ZPOOL_PROP_INVAL; 774 nvlist_t *proplist; 775 const char *normnm; 776 char *strval; 777 778 if (*props == NULL && 779 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 780 (void) fprintf(stderr, 781 gettext("internal error: out of memory\n")); 782 return (1); 783 } 784 785 proplist = *props; 786 787 if (poolprop) { 788 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 789 const char *fname = 790 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY); 791 792 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL && 793 !zpool_prop_feature(propname)) { 794 (void) fprintf(stderr, gettext("property '%s' is " 795 "not a valid pool property\n"), propname); 796 return (2); 797 } 798 799 /* 800 * feature@ properties and version should not be specified 801 * at the same time. 802 */ 803 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) && 804 nvlist_exists(proplist, vname)) || 805 (prop == ZPOOL_PROP_VERSION && 806 prop_list_contains_feature(proplist))) { 807 (void) fprintf(stderr, gettext("'feature@' and " 808 "'version' properties cannot be specified " 809 "together\n")); 810 return (2); 811 } 812 813 /* 814 * compatibility property and version should not be specified 815 * at the same time. 816 */ 817 if ((prop == ZPOOL_PROP_COMPATIBILITY && 818 nvlist_exists(proplist, vname)) || 819 (prop == ZPOOL_PROP_VERSION && 820 nvlist_exists(proplist, fname))) { 821 (void) fprintf(stderr, gettext("'compatibility' and " 822 "'version' properties cannot be specified " 823 "together\n")); 824 return (2); 825 } 826 827 if (zpool_prop_feature(propname)) 828 normnm = propname; 829 else 830 normnm = zpool_prop_to_name(prop); 831 } else { 832 zfs_prop_t fsprop = zfs_name_to_prop(propname); 833 834 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM, 835 B_FALSE)) { 836 normnm = zfs_prop_to_name(fsprop); 837 } else if (zfs_prop_user(propname) || 838 zfs_prop_userquota(propname)) { 839 normnm = propname; 840 } else { 841 (void) fprintf(stderr, gettext("property '%s' is " 842 "not a valid filesystem property\n"), propname); 843 return (2); 844 } 845 } 846 847 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 848 prop != ZPOOL_PROP_CACHEFILE) { 849 (void) fprintf(stderr, gettext("property '%s' " 850 "specified multiple times\n"), propname); 851 return (2); 852 } 853 854 if (nvlist_add_string(proplist, normnm, propval) != 0) { 855 (void) fprintf(stderr, gettext("internal " 856 "error: out of memory\n")); 857 return (1); 858 } 859 860 return (0); 861 } 862 863 /* 864 * Set a default property pair (name, string-value) in a property nvlist 865 */ 866 static int 867 add_prop_list_default(const char *propname, char *propval, nvlist_t **props, 868 boolean_t poolprop) 869 { 870 char *pval; 871 872 if (nvlist_lookup_string(*props, propname, &pval) == 0) 873 return (0); 874 875 return (add_prop_list(propname, propval, props, B_TRUE)); 876 } 877 878 /* 879 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ... 880 * 881 * -f Force addition of devices, even if they appear in use 882 * -g Display guid for individual vdev name. 883 * -L Follow links when resolving vdev path name. 884 * -n Do not add the devices, but display the resulting layout if 885 * they were to be added. 886 * -o Set property=value. 887 * -P Display full path for vdev name. 888 * 889 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 890 * handled by make_root_vdev(), which constructs the nvlist needed to pass to 891 * libzfs. 892 */ 893 int 894 zpool_do_add(int argc, char **argv) 895 { 896 boolean_t force = B_FALSE; 897 boolean_t dryrun = B_FALSE; 898 int name_flags = 0; 899 int c; 900 nvlist_t *nvroot; 901 char *poolname; 902 int ret; 903 zpool_handle_t *zhp; 904 nvlist_t *config; 905 nvlist_t *props = NULL; 906 char *propval; 907 908 /* check options */ 909 while ((c = getopt(argc, argv, "fgLno:P")) != -1) { 910 switch (c) { 911 case 'f': 912 force = B_TRUE; 913 break; 914 case 'g': 915 name_flags |= VDEV_NAME_GUID; 916 break; 917 case 'L': 918 name_flags |= VDEV_NAME_FOLLOW_LINKS; 919 break; 920 case 'n': 921 dryrun = B_TRUE; 922 break; 923 case 'o': 924 if ((propval = strchr(optarg, '=')) == NULL) { 925 (void) fprintf(stderr, gettext("missing " 926 "'=' for -o option\n")); 927 usage(B_FALSE); 928 } 929 *propval = '\0'; 930 propval++; 931 932 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 933 (add_prop_list(optarg, propval, &props, B_TRUE))) 934 usage(B_FALSE); 935 break; 936 case 'P': 937 name_flags |= VDEV_NAME_PATH; 938 break; 939 case '?': 940 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 941 optopt); 942 usage(B_FALSE); 943 } 944 } 945 946 argc -= optind; 947 argv += optind; 948 949 /* get pool name and check number of arguments */ 950 if (argc < 1) { 951 (void) fprintf(stderr, gettext("missing pool name argument\n")); 952 usage(B_FALSE); 953 } 954 if (argc < 2) { 955 (void) fprintf(stderr, gettext("missing vdev specification\n")); 956 usage(B_FALSE); 957 } 958 959 poolname = argv[0]; 960 961 argc--; 962 argv++; 963 964 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 965 return (1); 966 967 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 968 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 969 poolname); 970 zpool_close(zhp); 971 return (1); 972 } 973 974 /* unless manually specified use "ashift" pool property (if set) */ 975 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 976 int intval; 977 zprop_source_t src; 978 char strval[ZPOOL_MAXPROPLEN]; 979 980 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 981 if (src != ZPROP_SRC_DEFAULT) { 982 (void) sprintf(strval, "%" PRId32, intval); 983 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 984 &props, B_TRUE) == 0); 985 } 986 } 987 988 /* pass off to make_root_vdev for processing */ 989 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun, 990 argc, argv); 991 if (nvroot == NULL) { 992 zpool_close(zhp); 993 return (1); 994 } 995 996 if (dryrun) { 997 nvlist_t *poolnvroot; 998 nvlist_t **l2child, **sparechild; 999 uint_t l2children, sparechildren, c; 1000 char *vname; 1001 boolean_t hadcache = B_FALSE, hadspare = B_FALSE; 1002 1003 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1004 &poolnvroot) == 0); 1005 1006 (void) printf(gettext("would update '%s' to the following " 1007 "configuration:\n\n"), zpool_get_name(zhp)); 1008 1009 /* print original main pool and new tree */ 1010 print_vdev_tree(zhp, poolname, poolnvroot, 0, "", 1011 name_flags | VDEV_NAME_TYPE_ID); 1012 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags); 1013 1014 /* print other classes: 'dedup', 'special', and 'log' */ 1015 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) { 1016 print_vdev_tree(zhp, "dedup", poolnvroot, 0, 1017 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1018 print_vdev_tree(zhp, NULL, nvroot, 0, 1019 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1020 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) { 1021 print_vdev_tree(zhp, "dedup", nvroot, 0, 1022 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1023 } 1024 1025 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) { 1026 print_vdev_tree(zhp, "special", poolnvroot, 0, 1027 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1028 print_vdev_tree(zhp, NULL, nvroot, 0, 1029 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1030 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) { 1031 print_vdev_tree(zhp, "special", nvroot, 0, 1032 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1033 } 1034 1035 if (num_logs(poolnvroot) > 0) { 1036 print_vdev_tree(zhp, "logs", poolnvroot, 0, 1037 VDEV_ALLOC_BIAS_LOG, name_flags); 1038 print_vdev_tree(zhp, NULL, nvroot, 0, 1039 VDEV_ALLOC_BIAS_LOG, name_flags); 1040 } else if (num_logs(nvroot) > 0) { 1041 print_vdev_tree(zhp, "logs", nvroot, 0, 1042 VDEV_ALLOC_BIAS_LOG, name_flags); 1043 } 1044 1045 /* Do the same for the caches */ 1046 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE, 1047 &l2child, &l2children) == 0 && l2children) { 1048 hadcache = B_TRUE; 1049 (void) printf(gettext("\tcache\n")); 1050 for (c = 0; c < l2children; c++) { 1051 vname = zpool_vdev_name(g_zfs, NULL, 1052 l2child[c], name_flags); 1053 (void) printf("\t %s\n", vname); 1054 free(vname); 1055 } 1056 } 1057 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1058 &l2child, &l2children) == 0 && l2children) { 1059 if (!hadcache) 1060 (void) printf(gettext("\tcache\n")); 1061 for (c = 0; c < l2children; c++) { 1062 vname = zpool_vdev_name(g_zfs, NULL, 1063 l2child[c], name_flags); 1064 (void) printf("\t %s\n", vname); 1065 free(vname); 1066 } 1067 } 1068 /* And finaly the spares */ 1069 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES, 1070 &sparechild, &sparechildren) == 0 && sparechildren > 0) { 1071 hadspare = B_TRUE; 1072 (void) printf(gettext("\tspares\n")); 1073 for (c = 0; c < sparechildren; c++) { 1074 vname = zpool_vdev_name(g_zfs, NULL, 1075 sparechild[c], name_flags); 1076 (void) printf("\t %s\n", vname); 1077 free(vname); 1078 } 1079 } 1080 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1081 &sparechild, &sparechildren) == 0 && sparechildren > 0) { 1082 if (!hadspare) 1083 (void) printf(gettext("\tspares\n")); 1084 for (c = 0; c < sparechildren; c++) { 1085 vname = zpool_vdev_name(g_zfs, NULL, 1086 sparechild[c], name_flags); 1087 (void) printf("\t %s\n", vname); 1088 free(vname); 1089 } 1090 } 1091 1092 ret = 0; 1093 } else { 1094 ret = (zpool_add(zhp, nvroot) != 0); 1095 } 1096 1097 nvlist_free(props); 1098 nvlist_free(nvroot); 1099 zpool_close(zhp); 1100 1101 return (ret); 1102 } 1103 1104 /* 1105 * zpool remove [-npsw] <pool> <vdev> ... 1106 * 1107 * Removes the given vdev from the pool. 1108 */ 1109 int 1110 zpool_do_remove(int argc, char **argv) 1111 { 1112 char *poolname; 1113 int i, ret = 0; 1114 zpool_handle_t *zhp = NULL; 1115 boolean_t stop = B_FALSE; 1116 int c; 1117 boolean_t noop = B_FALSE; 1118 boolean_t parsable = B_FALSE; 1119 boolean_t wait = B_FALSE; 1120 1121 /* check options */ 1122 while ((c = getopt(argc, argv, "npsw")) != -1) { 1123 switch (c) { 1124 case 'n': 1125 noop = B_TRUE; 1126 break; 1127 case 'p': 1128 parsable = B_TRUE; 1129 break; 1130 case 's': 1131 stop = B_TRUE; 1132 break; 1133 case 'w': 1134 wait = B_TRUE; 1135 break; 1136 case '?': 1137 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1138 optopt); 1139 usage(B_FALSE); 1140 } 1141 } 1142 1143 argc -= optind; 1144 argv += optind; 1145 1146 /* get pool name and check number of arguments */ 1147 if (argc < 1) { 1148 (void) fprintf(stderr, gettext("missing pool name argument\n")); 1149 usage(B_FALSE); 1150 } 1151 1152 poolname = argv[0]; 1153 1154 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 1155 return (1); 1156 1157 if (stop && noop) { 1158 (void) fprintf(stderr, gettext("stop request ignored\n")); 1159 return (0); 1160 } 1161 1162 if (stop) { 1163 if (argc > 1) { 1164 (void) fprintf(stderr, gettext("too many arguments\n")); 1165 usage(B_FALSE); 1166 } 1167 if (zpool_vdev_remove_cancel(zhp) != 0) 1168 ret = 1; 1169 if (wait) { 1170 (void) fprintf(stderr, gettext("invalid option " 1171 "combination: -w cannot be used with -s\n")); 1172 usage(B_FALSE); 1173 } 1174 } else { 1175 if (argc < 2) { 1176 (void) fprintf(stderr, gettext("missing device\n")); 1177 usage(B_FALSE); 1178 } 1179 1180 for (i = 1; i < argc; i++) { 1181 if (noop) { 1182 uint64_t size; 1183 1184 if (zpool_vdev_indirect_size(zhp, argv[i], 1185 &size) != 0) { 1186 ret = 1; 1187 break; 1188 } 1189 if (parsable) { 1190 (void) printf("%s %llu\n", 1191 argv[i], (unsigned long long)size); 1192 } else { 1193 char valstr[32]; 1194 zfs_nicenum(size, valstr, 1195 sizeof (valstr)); 1196 (void) printf("Memory that will be " 1197 "used after removing %s: %s\n", 1198 argv[i], valstr); 1199 } 1200 } else { 1201 if (zpool_vdev_remove(zhp, argv[i]) != 0) 1202 ret = 1; 1203 } 1204 } 1205 1206 if (ret == 0 && wait) 1207 ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE); 1208 } 1209 zpool_close(zhp); 1210 1211 return (ret); 1212 } 1213 1214 /* 1215 * zpool labelclear [-f] <vdev> 1216 * 1217 * -f Force clearing the label for the vdevs which are members of 1218 * the exported or foreign pools. 1219 * 1220 * Verifies that the vdev is not active and zeros out the label information 1221 * on the device. 1222 */ 1223 int 1224 zpool_do_labelclear(int argc, char **argv) 1225 { 1226 char vdev[MAXPATHLEN]; 1227 char *name = NULL; 1228 struct stat st; 1229 int c, fd = -1, ret = 0; 1230 nvlist_t *config; 1231 pool_state_t state; 1232 boolean_t inuse = B_FALSE; 1233 boolean_t force = B_FALSE; 1234 1235 /* check options */ 1236 while ((c = getopt(argc, argv, "f")) != -1) { 1237 switch (c) { 1238 case 'f': 1239 force = B_TRUE; 1240 break; 1241 default: 1242 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1243 optopt); 1244 usage(B_FALSE); 1245 } 1246 } 1247 1248 argc -= optind; 1249 argv += optind; 1250 1251 /* get vdev name */ 1252 if (argc < 1) { 1253 (void) fprintf(stderr, gettext("missing vdev name\n")); 1254 usage(B_FALSE); 1255 } 1256 if (argc > 1) { 1257 (void) fprintf(stderr, gettext("too many arguments\n")); 1258 usage(B_FALSE); 1259 } 1260 1261 /* 1262 * Check if we were given absolute path and use it as is. 1263 * Otherwise if the provided vdev name doesn't point to a file, 1264 * try prepending expected disk paths and partition numbers. 1265 */ 1266 (void) strlcpy(vdev, argv[0], sizeof (vdev)); 1267 if (vdev[0] != '/' && stat(vdev, &st) != 0) { 1268 int error; 1269 1270 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN); 1271 if (error == 0 && zfs_dev_is_whole_disk(vdev)) { 1272 if (zfs_append_partition(vdev, MAXPATHLEN) == -1) 1273 error = ENOENT; 1274 } 1275 1276 if (error || (stat(vdev, &st) != 0)) { 1277 (void) fprintf(stderr, gettext( 1278 "failed to find device %s, try specifying absolute " 1279 "path instead\n"), argv[0]); 1280 return (1); 1281 } 1282 } 1283 1284 if ((fd = open(vdev, O_RDWR)) < 0) { 1285 (void) fprintf(stderr, gettext("failed to open %s: %s\n"), 1286 vdev, strerror(errno)); 1287 return (1); 1288 } 1289 1290 /* 1291 * Flush all dirty pages for the block device. This should not be 1292 * fatal when the device does not support BLKFLSBUF as would be the 1293 * case for a file vdev. 1294 */ 1295 if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY)) 1296 (void) fprintf(stderr, gettext("failed to invalidate " 1297 "cache for %s: %s\n"), vdev, strerror(errno)); 1298 1299 if (zpool_read_label(fd, &config, NULL) != 0) { 1300 (void) fprintf(stderr, 1301 gettext("failed to read label from %s\n"), vdev); 1302 ret = 1; 1303 goto errout; 1304 } 1305 nvlist_free(config); 1306 1307 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse); 1308 if (ret != 0) { 1309 (void) fprintf(stderr, 1310 gettext("failed to check state for %s\n"), vdev); 1311 ret = 1; 1312 goto errout; 1313 } 1314 1315 if (!inuse) 1316 goto wipe_label; 1317 1318 switch (state) { 1319 default: 1320 case POOL_STATE_ACTIVE: 1321 case POOL_STATE_SPARE: 1322 case POOL_STATE_L2CACHE: 1323 (void) fprintf(stderr, gettext( 1324 "%s is a member (%s) of pool \"%s\"\n"), 1325 vdev, zpool_pool_state_to_name(state), name); 1326 ret = 1; 1327 goto errout; 1328 1329 case POOL_STATE_EXPORTED: 1330 if (force) 1331 break; 1332 (void) fprintf(stderr, gettext( 1333 "use '-f' to override the following error:\n" 1334 "%s is a member of exported pool \"%s\"\n"), 1335 vdev, name); 1336 ret = 1; 1337 goto errout; 1338 1339 case POOL_STATE_POTENTIALLY_ACTIVE: 1340 if (force) 1341 break; 1342 (void) fprintf(stderr, gettext( 1343 "use '-f' to override the following error:\n" 1344 "%s is a member of potentially active pool \"%s\"\n"), 1345 vdev, name); 1346 ret = 1; 1347 goto errout; 1348 1349 case POOL_STATE_DESTROYED: 1350 /* inuse should never be set for a destroyed pool */ 1351 assert(0); 1352 break; 1353 } 1354 1355 wipe_label: 1356 ret = zpool_clear_label(fd); 1357 if (ret != 0) { 1358 (void) fprintf(stderr, 1359 gettext("failed to clear label for %s\n"), vdev); 1360 } 1361 1362 errout: 1363 free(name); 1364 (void) close(fd); 1365 1366 return (ret); 1367 } 1368 1369 /* 1370 * zpool create [-fnd] [-o property=value] ... 1371 * [-O file-system-property=value] ... 1372 * [-R root] [-m mountpoint] <pool> <dev> ... 1373 * 1374 * -f Force creation, even if devices appear in use 1375 * -n Do not create the pool, but display the resulting layout if it 1376 * were to be created. 1377 * -R Create a pool under an alternate root 1378 * -m Set default mountpoint for the root dataset. By default it's 1379 * '/<pool>' 1380 * -o Set property=value. 1381 * -o Set feature@feature=enabled|disabled. 1382 * -d Don't automatically enable all supported pool features 1383 * (individual features can be enabled with -o). 1384 * -O Set fsproperty=value in the pool's root file system 1385 * 1386 * Creates the named pool according to the given vdev specification. The 1387 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c. 1388 * Once we get the nvlist back from make_root_vdev(), we either print out the 1389 * contents (if '-n' was specified), or pass it to libzfs to do the creation. 1390 */ 1391 int 1392 zpool_do_create(int argc, char **argv) 1393 { 1394 boolean_t force = B_FALSE; 1395 boolean_t dryrun = B_FALSE; 1396 boolean_t enable_pool_features = B_TRUE; 1397 1398 int c; 1399 nvlist_t *nvroot = NULL; 1400 char *poolname; 1401 char *tname = NULL; 1402 int ret = 1; 1403 char *altroot = NULL; 1404 char *compat = NULL; 1405 char *mountpoint = NULL; 1406 nvlist_t *fsprops = NULL; 1407 nvlist_t *props = NULL; 1408 char *propval; 1409 1410 /* check options */ 1411 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) { 1412 switch (c) { 1413 case 'f': 1414 force = B_TRUE; 1415 break; 1416 case 'n': 1417 dryrun = B_TRUE; 1418 break; 1419 case 'd': 1420 enable_pool_features = B_FALSE; 1421 break; 1422 case 'R': 1423 altroot = optarg; 1424 if (add_prop_list(zpool_prop_to_name( 1425 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 1426 goto errout; 1427 if (add_prop_list_default(zpool_prop_to_name( 1428 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 1429 goto errout; 1430 break; 1431 case 'm': 1432 /* Equivalent to -O mountpoint=optarg */ 1433 mountpoint = optarg; 1434 break; 1435 case 'o': 1436 if ((propval = strchr(optarg, '=')) == NULL) { 1437 (void) fprintf(stderr, gettext("missing " 1438 "'=' for -o option\n")); 1439 goto errout; 1440 } 1441 *propval = '\0'; 1442 propval++; 1443 1444 if (add_prop_list(optarg, propval, &props, B_TRUE)) 1445 goto errout; 1446 1447 /* 1448 * If the user is creating a pool that doesn't support 1449 * feature flags, don't enable any features. 1450 */ 1451 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 1452 char *end; 1453 u_longlong_t ver; 1454 1455 ver = strtoull(propval, &end, 10); 1456 if (*end == '\0' && 1457 ver < SPA_VERSION_FEATURES) { 1458 enable_pool_features = B_FALSE; 1459 } 1460 } 1461 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) 1462 altroot = propval; 1463 if (zpool_name_to_prop(optarg) == 1464 ZPOOL_PROP_COMPATIBILITY) 1465 compat = propval; 1466 break; 1467 case 'O': 1468 if ((propval = strchr(optarg, '=')) == NULL) { 1469 (void) fprintf(stderr, gettext("missing " 1470 "'=' for -O option\n")); 1471 goto errout; 1472 } 1473 *propval = '\0'; 1474 propval++; 1475 1476 /* 1477 * Mountpoints are checked and then added later. 1478 * Uniquely among properties, they can be specified 1479 * more than once, to avoid conflict with -m. 1480 */ 1481 if (0 == strcmp(optarg, 1482 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { 1483 mountpoint = propval; 1484 } else if (add_prop_list(optarg, propval, &fsprops, 1485 B_FALSE)) { 1486 goto errout; 1487 } 1488 break; 1489 case 't': 1490 /* 1491 * Sanity check temporary pool name. 1492 */ 1493 if (strchr(optarg, '/') != NULL) { 1494 (void) fprintf(stderr, gettext("cannot create " 1495 "'%s': invalid character '/' in temporary " 1496 "name\n"), optarg); 1497 (void) fprintf(stderr, gettext("use 'zfs " 1498 "create' to create a dataset\n")); 1499 goto errout; 1500 } 1501 1502 if (add_prop_list(zpool_prop_to_name( 1503 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE)) 1504 goto errout; 1505 if (add_prop_list_default(zpool_prop_to_name( 1506 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 1507 goto errout; 1508 tname = optarg; 1509 break; 1510 case ':': 1511 (void) fprintf(stderr, gettext("missing argument for " 1512 "'%c' option\n"), optopt); 1513 goto badusage; 1514 case '?': 1515 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1516 optopt); 1517 goto badusage; 1518 } 1519 } 1520 1521 argc -= optind; 1522 argv += optind; 1523 1524 /* get pool name and check number of arguments */ 1525 if (argc < 1) { 1526 (void) fprintf(stderr, gettext("missing pool name argument\n")); 1527 goto badusage; 1528 } 1529 if (argc < 2) { 1530 (void) fprintf(stderr, gettext("missing vdev specification\n")); 1531 goto badusage; 1532 } 1533 1534 poolname = argv[0]; 1535 1536 /* 1537 * As a special case, check for use of '/' in the name, and direct the 1538 * user to use 'zfs create' instead. 1539 */ 1540 if (strchr(poolname, '/') != NULL) { 1541 (void) fprintf(stderr, gettext("cannot create '%s': invalid " 1542 "character '/' in pool name\n"), poolname); 1543 (void) fprintf(stderr, gettext("use 'zfs create' to " 1544 "create a dataset\n")); 1545 goto errout; 1546 } 1547 1548 /* pass off to make_root_vdev for bulk processing */ 1549 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, 1550 argc - 1, argv + 1); 1551 if (nvroot == NULL) 1552 goto errout; 1553 1554 /* make_root_vdev() allows 0 toplevel children if there are spares */ 1555 if (!zfs_allocatable_devs(nvroot)) { 1556 (void) fprintf(stderr, gettext("invalid vdev " 1557 "specification: at least one toplevel vdev must be " 1558 "specified\n")); 1559 goto errout; 1560 } 1561 1562 if (altroot != NULL && altroot[0] != '/') { 1563 (void) fprintf(stderr, gettext("invalid alternate root '%s': " 1564 "must be an absolute path\n"), altroot); 1565 goto errout; 1566 } 1567 1568 /* 1569 * Check the validity of the mountpoint and direct the user to use the 1570 * '-m' mountpoint option if it looks like its in use. 1571 */ 1572 if (mountpoint == NULL || 1573 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 1574 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { 1575 char buf[MAXPATHLEN]; 1576 DIR *dirp; 1577 1578 if (mountpoint && mountpoint[0] != '/') { 1579 (void) fprintf(stderr, gettext("invalid mountpoint " 1580 "'%s': must be an absolute path, 'legacy', or " 1581 "'none'\n"), mountpoint); 1582 goto errout; 1583 } 1584 1585 if (mountpoint == NULL) { 1586 if (altroot != NULL) 1587 (void) snprintf(buf, sizeof (buf), "%s/%s", 1588 altroot, poolname); 1589 else 1590 (void) snprintf(buf, sizeof (buf), "/%s", 1591 poolname); 1592 } else { 1593 if (altroot != NULL) 1594 (void) snprintf(buf, sizeof (buf), "%s%s", 1595 altroot, mountpoint); 1596 else 1597 (void) snprintf(buf, sizeof (buf), "%s", 1598 mountpoint); 1599 } 1600 1601 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 1602 (void) fprintf(stderr, gettext("mountpoint '%s' : " 1603 "%s\n"), buf, strerror(errno)); 1604 (void) fprintf(stderr, gettext("use '-m' " 1605 "option to provide a different default\n")); 1606 goto errout; 1607 } else if (dirp) { 1608 int count = 0; 1609 1610 while (count < 3 && readdir(dirp) != NULL) 1611 count++; 1612 (void) closedir(dirp); 1613 1614 if (count > 2) { 1615 (void) fprintf(stderr, gettext("mountpoint " 1616 "'%s' exists and is not empty\n"), buf); 1617 (void) fprintf(stderr, gettext("use '-m' " 1618 "option to provide a " 1619 "different default\n")); 1620 goto errout; 1621 } 1622 } 1623 } 1624 1625 /* 1626 * Now that the mountpoint's validity has been checked, ensure that 1627 * the property is set appropriately prior to creating the pool. 1628 */ 1629 if (mountpoint != NULL) { 1630 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1631 mountpoint, &fsprops, B_FALSE); 1632 if (ret != 0) 1633 goto errout; 1634 } 1635 1636 ret = 1; 1637 if (dryrun) { 1638 /* 1639 * For a dry run invocation, print out a basic message and run 1640 * through all the vdevs in the list and print out in an 1641 * appropriate hierarchy. 1642 */ 1643 (void) printf(gettext("would create '%s' with the " 1644 "following layout:\n\n"), poolname); 1645 1646 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0); 1647 print_vdev_tree(NULL, "dedup", nvroot, 0, 1648 VDEV_ALLOC_BIAS_DEDUP, 0); 1649 print_vdev_tree(NULL, "special", nvroot, 0, 1650 VDEV_ALLOC_BIAS_SPECIAL, 0); 1651 print_vdev_tree(NULL, "logs", nvroot, 0, 1652 VDEV_ALLOC_BIAS_LOG, 0); 1653 print_cache_list(nvroot, 0); 1654 print_spare_list(nvroot, 0); 1655 1656 ret = 0; 1657 } else { 1658 /* 1659 * Load in feature set. 1660 * Note: if compatibility property not given, we'll have 1661 * NULL, which means 'all features'. 1662 */ 1663 boolean_t requested_features[SPA_FEATURES]; 1664 if (zpool_do_load_compat(compat, requested_features) != 1665 ZPOOL_COMPATIBILITY_OK) 1666 goto errout; 1667 1668 /* 1669 * props contains list of features to enable. 1670 * For each feature: 1671 * - remove it if feature@name=disabled 1672 * - leave it there if feature@name=enabled 1673 * - add it if: 1674 * - enable_pool_features (ie: no '-d' or '-o version') 1675 * - it's supported by the kernel module 1676 * - it's in the requested feature set 1677 */ 1678 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 1679 char propname[MAXPATHLEN]; 1680 char *propval; 1681 zfeature_info_t *feat = &spa_feature_table[i]; 1682 1683 (void) snprintf(propname, sizeof (propname), 1684 "feature@%s", feat->fi_uname); 1685 1686 if (!nvlist_lookup_string(props, propname, &propval)) { 1687 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0) 1688 (void) nvlist_remove_all(props, 1689 propname); 1690 } else if ( 1691 enable_pool_features && 1692 feat->fi_zfs_mod_supported && 1693 requested_features[i]) { 1694 ret = add_prop_list(propname, 1695 ZFS_FEATURE_ENABLED, &props, B_TRUE); 1696 if (ret != 0) 1697 goto errout; 1698 } 1699 } 1700 1701 ret = 1; 1702 if (zpool_create(g_zfs, poolname, 1703 nvroot, props, fsprops) == 0) { 1704 zfs_handle_t *pool = zfs_open(g_zfs, 1705 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); 1706 if (pool != NULL) { 1707 if (zfs_mount(pool, NULL, 0) == 0) { 1708 ret = zfs_shareall(pool); 1709 zfs_commit_all_shares(); 1710 } 1711 zfs_close(pool); 1712 } 1713 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 1714 (void) fprintf(stderr, gettext("pool name may have " 1715 "been omitted\n")); 1716 } 1717 } 1718 1719 errout: 1720 nvlist_free(nvroot); 1721 nvlist_free(fsprops); 1722 nvlist_free(props); 1723 return (ret); 1724 badusage: 1725 nvlist_free(fsprops); 1726 nvlist_free(props); 1727 usage(B_FALSE); 1728 return (2); 1729 } 1730 1731 /* 1732 * zpool destroy <pool> 1733 * 1734 * -f Forcefully unmount any datasets 1735 * 1736 * Destroy the given pool. Automatically unmounts any datasets in the pool. 1737 */ 1738 int 1739 zpool_do_destroy(int argc, char **argv) 1740 { 1741 boolean_t force = B_FALSE; 1742 int c; 1743 char *pool; 1744 zpool_handle_t *zhp; 1745 int ret; 1746 1747 /* check options */ 1748 while ((c = getopt(argc, argv, "f")) != -1) { 1749 switch (c) { 1750 case 'f': 1751 force = B_TRUE; 1752 break; 1753 case '?': 1754 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1755 optopt); 1756 usage(B_FALSE); 1757 } 1758 } 1759 1760 argc -= optind; 1761 argv += optind; 1762 1763 /* check arguments */ 1764 if (argc < 1) { 1765 (void) fprintf(stderr, gettext("missing pool argument\n")); 1766 usage(B_FALSE); 1767 } 1768 if (argc > 1) { 1769 (void) fprintf(stderr, gettext("too many arguments\n")); 1770 usage(B_FALSE); 1771 } 1772 1773 pool = argv[0]; 1774 1775 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 1776 /* 1777 * As a special case, check for use of '/' in the name, and 1778 * direct the user to use 'zfs destroy' instead. 1779 */ 1780 if (strchr(pool, '/') != NULL) 1781 (void) fprintf(stderr, gettext("use 'zfs destroy' to " 1782 "destroy a dataset\n")); 1783 return (1); 1784 } 1785 1786 if (zpool_disable_datasets(zhp, force) != 0) { 1787 (void) fprintf(stderr, gettext("could not destroy '%s': " 1788 "could not unmount datasets\n"), zpool_get_name(zhp)); 1789 zpool_close(zhp); 1790 return (1); 1791 } 1792 1793 /* The history must be logged as part of the export */ 1794 log_history = B_FALSE; 1795 1796 ret = (zpool_destroy(zhp, history_str) != 0); 1797 1798 zpool_close(zhp); 1799 1800 return (ret); 1801 } 1802 1803 typedef struct export_cbdata { 1804 boolean_t force; 1805 boolean_t hardforce; 1806 } export_cbdata_t; 1807 1808 /* 1809 * Export one pool 1810 */ 1811 static int 1812 zpool_export_one(zpool_handle_t *zhp, void *data) 1813 { 1814 export_cbdata_t *cb = data; 1815 1816 if (zpool_disable_datasets(zhp, cb->force) != 0) 1817 return (1); 1818 1819 /* The history must be logged as part of the export */ 1820 log_history = B_FALSE; 1821 1822 if (cb->hardforce) { 1823 if (zpool_export_force(zhp, history_str) != 0) 1824 return (1); 1825 } else if (zpool_export(zhp, cb->force, history_str) != 0) { 1826 return (1); 1827 } 1828 1829 return (0); 1830 } 1831 1832 /* 1833 * zpool export [-f] <pool> ... 1834 * 1835 * -a Export all pools 1836 * -f Forcefully unmount datasets 1837 * 1838 * Export the given pools. By default, the command will attempt to cleanly 1839 * unmount any active datasets within the pool. If the '-f' flag is specified, 1840 * then the datasets will be forcefully unmounted. 1841 */ 1842 int 1843 zpool_do_export(int argc, char **argv) 1844 { 1845 export_cbdata_t cb; 1846 boolean_t do_all = B_FALSE; 1847 boolean_t force = B_FALSE; 1848 boolean_t hardforce = B_FALSE; 1849 int c, ret; 1850 1851 /* check options */ 1852 while ((c = getopt(argc, argv, "afF")) != -1) { 1853 switch (c) { 1854 case 'a': 1855 do_all = B_TRUE; 1856 break; 1857 case 'f': 1858 force = B_TRUE; 1859 break; 1860 case 'F': 1861 hardforce = B_TRUE; 1862 break; 1863 case '?': 1864 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1865 optopt); 1866 usage(B_FALSE); 1867 } 1868 } 1869 1870 cb.force = force; 1871 cb.hardforce = hardforce; 1872 argc -= optind; 1873 argv += optind; 1874 1875 if (do_all) { 1876 if (argc != 0) { 1877 (void) fprintf(stderr, gettext("too many arguments\n")); 1878 usage(B_FALSE); 1879 } 1880 1881 return (for_each_pool(argc, argv, B_TRUE, NULL, 1882 B_FALSE, zpool_export_one, &cb)); 1883 } 1884 1885 /* check arguments */ 1886 if (argc < 1) { 1887 (void) fprintf(stderr, gettext("missing pool argument\n")); 1888 usage(B_FALSE); 1889 } 1890 1891 ret = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, zpool_export_one, 1892 &cb); 1893 1894 return (ret); 1895 } 1896 1897 /* 1898 * Given a vdev configuration, determine the maximum width needed for the device 1899 * name column. 1900 */ 1901 static int 1902 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max, 1903 int name_flags) 1904 { 1905 char *name; 1906 nvlist_t **child; 1907 uint_t c, children; 1908 int ret; 1909 1910 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags); 1911 if (strlen(name) + depth > max) 1912 max = strlen(name) + depth; 1913 1914 free(name); 1915 1916 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1917 &child, &children) == 0) { 1918 for (c = 0; c < children; c++) 1919 if ((ret = max_width(zhp, child[c], depth + 2, 1920 max, name_flags)) > max) 1921 max = ret; 1922 } 1923 1924 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1925 &child, &children) == 0) { 1926 for (c = 0; c < children; c++) 1927 if ((ret = max_width(zhp, child[c], depth + 2, 1928 max, name_flags)) > max) 1929 max = ret; 1930 } 1931 1932 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1933 &child, &children) == 0) { 1934 for (c = 0; c < children; c++) 1935 if ((ret = max_width(zhp, child[c], depth + 2, 1936 max, name_flags)) > max) 1937 max = ret; 1938 } 1939 1940 return (max); 1941 } 1942 1943 typedef struct spare_cbdata { 1944 uint64_t cb_guid; 1945 zpool_handle_t *cb_zhp; 1946 } spare_cbdata_t; 1947 1948 static boolean_t 1949 find_vdev(nvlist_t *nv, uint64_t search) 1950 { 1951 uint64_t guid; 1952 nvlist_t **child; 1953 uint_t c, children; 1954 1955 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1956 search == guid) 1957 return (B_TRUE); 1958 1959 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1960 &child, &children) == 0) { 1961 for (c = 0; c < children; c++) 1962 if (find_vdev(child[c], search)) 1963 return (B_TRUE); 1964 } 1965 1966 return (B_FALSE); 1967 } 1968 1969 static int 1970 find_spare(zpool_handle_t *zhp, void *data) 1971 { 1972 spare_cbdata_t *cbp = data; 1973 nvlist_t *config, *nvroot; 1974 1975 config = zpool_get_config(zhp, NULL); 1976 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1977 &nvroot) == 0); 1978 1979 if (find_vdev(nvroot, cbp->cb_guid)) { 1980 cbp->cb_zhp = zhp; 1981 return (1); 1982 } 1983 1984 zpool_close(zhp); 1985 return (0); 1986 } 1987 1988 typedef struct status_cbdata { 1989 int cb_count; 1990 int cb_name_flags; 1991 int cb_namewidth; 1992 boolean_t cb_allpools; 1993 boolean_t cb_verbose; 1994 boolean_t cb_literal; 1995 boolean_t cb_explain; 1996 boolean_t cb_first; 1997 boolean_t cb_dedup_stats; 1998 boolean_t cb_print_status; 1999 boolean_t cb_print_slow_ios; 2000 boolean_t cb_print_vdev_init; 2001 boolean_t cb_print_vdev_trim; 2002 vdev_cmd_data_list_t *vcdl; 2003 } status_cbdata_t; 2004 2005 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ 2006 static int 2007 is_blank_str(char *str) 2008 { 2009 while (str != NULL && *str != '\0') { 2010 if (!isblank(*str)) 2011 return (0); 2012 str++; 2013 } 2014 return (1); 2015 } 2016 2017 /* Print command output lines for specific vdev in a specific pool */ 2018 static void 2019 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path) 2020 { 2021 vdev_cmd_data_t *data; 2022 int i, j; 2023 char *val; 2024 2025 for (i = 0; i < vcdl->count; i++) { 2026 if ((strcmp(vcdl->data[i].path, path) != 0) || 2027 (strcmp(vcdl->data[i].pool, pool) != 0)) { 2028 /* Not the vdev we're looking for */ 2029 continue; 2030 } 2031 2032 data = &vcdl->data[i]; 2033 /* Print out all the output values for this vdev */ 2034 for (j = 0; j < vcdl->uniq_cols_cnt; j++) { 2035 val = NULL; 2036 /* Does this vdev have values for this column? */ 2037 for (int k = 0; k < data->cols_cnt; k++) { 2038 if (strcmp(data->cols[k], 2039 vcdl->uniq_cols[j]) == 0) { 2040 /* yes it does, record the value */ 2041 val = data->lines[k]; 2042 break; 2043 } 2044 } 2045 /* 2046 * Mark empty values with dashes to make output 2047 * awk-able. 2048 */ 2049 if (val == NULL || is_blank_str(val)) 2050 val = "-"; 2051 2052 printf("%*s", vcdl->uniq_cols_width[j], val); 2053 if (j < vcdl->uniq_cols_cnt - 1) 2054 printf(" "); 2055 } 2056 2057 /* Print out any values that aren't in a column at the end */ 2058 for (j = data->cols_cnt; j < data->lines_cnt; j++) { 2059 /* Did we have any columns? If so print a spacer. */ 2060 if (vcdl->uniq_cols_cnt > 0) 2061 printf(" "); 2062 2063 val = data->lines[j]; 2064 printf("%s", val ? val : ""); 2065 } 2066 break; 2067 } 2068 } 2069 2070 /* 2071 * Print vdev initialization status for leaves 2072 */ 2073 static void 2074 print_status_initialize(vdev_stat_t *vs, boolean_t verbose) 2075 { 2076 if (verbose) { 2077 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE || 2078 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED || 2079 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) && 2080 !vs->vs_scan_removing) { 2081 char zbuf[1024]; 2082 char tbuf[256]; 2083 struct tm zaction_ts; 2084 2085 time_t t = vs->vs_initialize_action_time; 2086 int initialize_pct = 100; 2087 if (vs->vs_initialize_state != 2088 VDEV_INITIALIZE_COMPLETE) { 2089 initialize_pct = (vs->vs_initialize_bytes_done * 2090 100 / (vs->vs_initialize_bytes_est + 1)); 2091 } 2092 2093 (void) localtime_r(&t, &zaction_ts); 2094 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts); 2095 2096 switch (vs->vs_initialize_state) { 2097 case VDEV_INITIALIZE_SUSPENDED: 2098 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2099 gettext("suspended, started at"), tbuf); 2100 break; 2101 case VDEV_INITIALIZE_ACTIVE: 2102 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2103 gettext("started at"), tbuf); 2104 break; 2105 case VDEV_INITIALIZE_COMPLETE: 2106 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2107 gettext("completed at"), tbuf); 2108 break; 2109 } 2110 2111 (void) printf(gettext(" (%d%% initialized%s)"), 2112 initialize_pct, zbuf); 2113 } else { 2114 (void) printf(gettext(" (uninitialized)")); 2115 } 2116 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) { 2117 (void) printf(gettext(" (initializing)")); 2118 } 2119 } 2120 2121 /* 2122 * Print vdev TRIM status for leaves 2123 */ 2124 static void 2125 print_status_trim(vdev_stat_t *vs, boolean_t verbose) 2126 { 2127 if (verbose) { 2128 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE || 2129 vs->vs_trim_state == VDEV_TRIM_SUSPENDED || 2130 vs->vs_trim_state == VDEV_TRIM_COMPLETE) && 2131 !vs->vs_scan_removing) { 2132 char zbuf[1024]; 2133 char tbuf[256]; 2134 struct tm zaction_ts; 2135 2136 time_t t = vs->vs_trim_action_time; 2137 int trim_pct = 100; 2138 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) { 2139 trim_pct = (vs->vs_trim_bytes_done * 2140 100 / (vs->vs_trim_bytes_est + 1)); 2141 } 2142 2143 (void) localtime_r(&t, &zaction_ts); 2144 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts); 2145 2146 switch (vs->vs_trim_state) { 2147 case VDEV_TRIM_SUSPENDED: 2148 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2149 gettext("suspended, started at"), tbuf); 2150 break; 2151 case VDEV_TRIM_ACTIVE: 2152 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2153 gettext("started at"), tbuf); 2154 break; 2155 case VDEV_TRIM_COMPLETE: 2156 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2157 gettext("completed at"), tbuf); 2158 break; 2159 } 2160 2161 (void) printf(gettext(" (%d%% trimmed%s)"), 2162 trim_pct, zbuf); 2163 } else if (vs->vs_trim_notsup) { 2164 (void) printf(gettext(" (trim unsupported)")); 2165 } else { 2166 (void) printf(gettext(" (untrimmed)")); 2167 } 2168 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) { 2169 (void) printf(gettext(" (trimming)")); 2170 } 2171 } 2172 2173 /* 2174 * Return the color associated with a health string. This includes returning 2175 * NULL for no color change. 2176 */ 2177 static char * 2178 health_str_to_color(const char *health) 2179 { 2180 if (strcmp(health, gettext("FAULTED")) == 0 || 2181 strcmp(health, gettext("SUSPENDED")) == 0 || 2182 strcmp(health, gettext("UNAVAIL")) == 0) { 2183 return (ANSI_RED); 2184 } 2185 2186 if (strcmp(health, gettext("OFFLINE")) == 0 || 2187 strcmp(health, gettext("DEGRADED")) == 0 || 2188 strcmp(health, gettext("REMOVED")) == 0) { 2189 return (ANSI_YELLOW); 2190 } 2191 2192 return (NULL); 2193 } 2194 2195 /* 2196 * Print out configuration state as requested by status_callback. 2197 */ 2198 static void 2199 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name, 2200 nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs) 2201 { 2202 nvlist_t **child, *root; 2203 uint_t c, i, vsc, children; 2204 pool_scan_stat_t *ps = NULL; 2205 vdev_stat_t *vs; 2206 char rbuf[6], wbuf[6], cbuf[6]; 2207 char *vname; 2208 uint64_t notpresent; 2209 spare_cbdata_t spare_cb; 2210 const char *state; 2211 char *type; 2212 char *path = NULL; 2213 char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL; 2214 2215 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2216 &child, &children) != 0) 2217 children = 0; 2218 2219 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2220 (uint64_t **)&vs, &vsc) == 0); 2221 2222 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 2223 2224 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) 2225 return; 2226 2227 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 2228 2229 if (isspare) { 2230 /* 2231 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 2232 * online drives. 2233 */ 2234 if (vs->vs_aux == VDEV_AUX_SPARED) 2235 state = gettext("INUSE"); 2236 else if (vs->vs_state == VDEV_STATE_HEALTHY) 2237 state = gettext("AVAIL"); 2238 } 2239 2240 printf_color(health_str_to_color(state), 2241 "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth, 2242 name, state); 2243 2244 if (!isspare) { 2245 if (vs->vs_read_errors) 2246 rcolor = ANSI_RED; 2247 2248 if (vs->vs_write_errors) 2249 wcolor = ANSI_RED; 2250 2251 if (vs->vs_checksum_errors) 2252 ccolor = ANSI_RED; 2253 2254 if (cb->cb_literal) { 2255 printf(" "); 2256 printf_color(rcolor, "%5llu", 2257 (u_longlong_t)vs->vs_read_errors); 2258 printf(" "); 2259 printf_color(wcolor, "%5llu", 2260 (u_longlong_t)vs->vs_write_errors); 2261 printf(" "); 2262 printf_color(ccolor, "%5llu", 2263 (u_longlong_t)vs->vs_checksum_errors); 2264 } else { 2265 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 2266 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 2267 zfs_nicenum(vs->vs_checksum_errors, cbuf, 2268 sizeof (cbuf)); 2269 printf(" "); 2270 printf_color(rcolor, "%5s", rbuf); 2271 printf(" "); 2272 printf_color(wcolor, "%5s", wbuf); 2273 printf(" "); 2274 printf_color(ccolor, "%5s", cbuf); 2275 } 2276 if (cb->cb_print_slow_ios) { 2277 if (children == 0) { 2278 /* Only leafs vdevs have slow IOs */ 2279 zfs_nicenum(vs->vs_slow_ios, rbuf, 2280 sizeof (rbuf)); 2281 } else { 2282 snprintf(rbuf, sizeof (rbuf), "-"); 2283 } 2284 2285 if (cb->cb_literal) 2286 printf(" %5llu", (u_longlong_t)vs->vs_slow_ios); 2287 else 2288 printf(" %5s", rbuf); 2289 } 2290 } 2291 2292 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 2293 ¬present) == 0) { 2294 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 2295 (void) printf(" %s %s", gettext("was"), path); 2296 } else if (vs->vs_aux != 0) { 2297 (void) printf(" "); 2298 color_start(ANSI_RED); 2299 switch (vs->vs_aux) { 2300 case VDEV_AUX_OPEN_FAILED: 2301 (void) printf(gettext("cannot open")); 2302 break; 2303 2304 case VDEV_AUX_BAD_GUID_SUM: 2305 (void) printf(gettext("missing device")); 2306 break; 2307 2308 case VDEV_AUX_NO_REPLICAS: 2309 (void) printf(gettext("insufficient replicas")); 2310 break; 2311 2312 case VDEV_AUX_VERSION_NEWER: 2313 (void) printf(gettext("newer version")); 2314 break; 2315 2316 case VDEV_AUX_UNSUP_FEAT: 2317 (void) printf(gettext("unsupported feature(s)")); 2318 break; 2319 2320 case VDEV_AUX_ASHIFT_TOO_BIG: 2321 (void) printf(gettext("unsupported minimum blocksize")); 2322 break; 2323 2324 case VDEV_AUX_SPARED: 2325 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2326 &spare_cb.cb_guid) == 0); 2327 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { 2328 if (strcmp(zpool_get_name(spare_cb.cb_zhp), 2329 zpool_get_name(zhp)) == 0) 2330 (void) printf(gettext("currently in " 2331 "use")); 2332 else 2333 (void) printf(gettext("in use by " 2334 "pool '%s'"), 2335 zpool_get_name(spare_cb.cb_zhp)); 2336 zpool_close(spare_cb.cb_zhp); 2337 } else { 2338 (void) printf(gettext("currently in use")); 2339 } 2340 break; 2341 2342 case VDEV_AUX_ERR_EXCEEDED: 2343 (void) printf(gettext("too many errors")); 2344 break; 2345 2346 case VDEV_AUX_IO_FAILURE: 2347 (void) printf(gettext("experienced I/O failures")); 2348 break; 2349 2350 case VDEV_AUX_BAD_LOG: 2351 (void) printf(gettext("bad intent log")); 2352 break; 2353 2354 case VDEV_AUX_EXTERNAL: 2355 (void) printf(gettext("external device fault")); 2356 break; 2357 2358 case VDEV_AUX_SPLIT_POOL: 2359 (void) printf(gettext("split into new pool")); 2360 break; 2361 2362 case VDEV_AUX_ACTIVE: 2363 (void) printf(gettext("currently in use")); 2364 break; 2365 2366 case VDEV_AUX_CHILDREN_OFFLINE: 2367 (void) printf(gettext("all children offline")); 2368 break; 2369 2370 default: 2371 (void) printf(gettext("corrupted data")); 2372 break; 2373 } 2374 color_end(); 2375 } else if (children == 0 && !isspare && 2376 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && 2377 VDEV_STAT_VALID(vs_physical_ashift, vsc) && 2378 vs->vs_configured_ashift < vs->vs_physical_ashift) { 2379 (void) printf( 2380 gettext(" block size: %dB configured, %dB native"), 2381 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); 2382 } 2383 2384 /* The root vdev has the scrub/resilver stats */ 2385 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2386 ZPOOL_CONFIG_VDEV_TREE); 2387 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS, 2388 (uint64_t **)&ps, &c); 2389 2390 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) { 2391 if (vs->vs_scan_processed != 0) { 2392 (void) printf(gettext(" (%s)"), 2393 (ps->pss_func == POOL_SCAN_RESILVER) ? 2394 "resilvering" : "repairing"); 2395 } else if (vs->vs_resilver_deferred) { 2396 (void) printf(gettext(" (awaiting resilver)")); 2397 } 2398 } 2399 2400 /* The top-level vdevs have the rebuild stats */ 2401 if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE && 2402 children == 0) { 2403 if (vs->vs_rebuild_processed != 0) { 2404 (void) printf(gettext(" (resilvering)")); 2405 } 2406 } 2407 2408 if (cb->vcdl != NULL) { 2409 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 2410 printf(" "); 2411 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); 2412 } 2413 } 2414 2415 /* Display vdev initialization and trim status for leaves. */ 2416 if (children == 0) { 2417 print_status_initialize(vs, cb->cb_print_vdev_init); 2418 print_status_trim(vs, cb->cb_print_vdev_trim); 2419 } 2420 2421 (void) printf("\n"); 2422 2423 for (c = 0; c < children; c++) { 2424 uint64_t islog = B_FALSE, ishole = B_FALSE; 2425 2426 /* Don't print logs or holes here */ 2427 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 2428 &islog); 2429 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 2430 &ishole); 2431 if (islog || ishole) 2432 continue; 2433 /* Only print normal classes here */ 2434 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 2435 continue; 2436 2437 /* Provide vdev_rebuild_stats to children if available */ 2438 if (vrs == NULL) { 2439 (void) nvlist_lookup_uint64_array(nv, 2440 ZPOOL_CONFIG_REBUILD_STATS, 2441 (uint64_t **)&vrs, &i); 2442 } 2443 2444 vname = zpool_vdev_name(g_zfs, zhp, child[c], 2445 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 2446 print_status_config(zhp, cb, vname, child[c], depth + 2, 2447 isspare, vrs); 2448 free(vname); 2449 } 2450 } 2451 2452 /* 2453 * Print the configuration of an exported pool. Iterate over all vdevs in the 2454 * pool, printing out the name and status for each one. 2455 */ 2456 static void 2457 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv, 2458 int depth) 2459 { 2460 nvlist_t **child; 2461 uint_t c, children; 2462 vdev_stat_t *vs; 2463 char *type, *vname; 2464 2465 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 2466 if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 2467 strcmp(type, VDEV_TYPE_HOLE) == 0) 2468 return; 2469 2470 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2471 (uint64_t **)&vs, &c) == 0); 2472 2473 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name); 2474 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 2475 2476 if (vs->vs_aux != 0) { 2477 (void) printf(" "); 2478 2479 switch (vs->vs_aux) { 2480 case VDEV_AUX_OPEN_FAILED: 2481 (void) printf(gettext("cannot open")); 2482 break; 2483 2484 case VDEV_AUX_BAD_GUID_SUM: 2485 (void) printf(gettext("missing device")); 2486 break; 2487 2488 case VDEV_AUX_NO_REPLICAS: 2489 (void) printf(gettext("insufficient replicas")); 2490 break; 2491 2492 case VDEV_AUX_VERSION_NEWER: 2493 (void) printf(gettext("newer version")); 2494 break; 2495 2496 case VDEV_AUX_UNSUP_FEAT: 2497 (void) printf(gettext("unsupported feature(s)")); 2498 break; 2499 2500 case VDEV_AUX_ERR_EXCEEDED: 2501 (void) printf(gettext("too many errors")); 2502 break; 2503 2504 case VDEV_AUX_ACTIVE: 2505 (void) printf(gettext("currently in use")); 2506 break; 2507 2508 case VDEV_AUX_CHILDREN_OFFLINE: 2509 (void) printf(gettext("all children offline")); 2510 break; 2511 2512 default: 2513 (void) printf(gettext("corrupted data")); 2514 break; 2515 } 2516 } 2517 (void) printf("\n"); 2518 2519 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2520 &child, &children) != 0) 2521 return; 2522 2523 for (c = 0; c < children; c++) { 2524 uint64_t is_log = B_FALSE; 2525 2526 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 2527 &is_log); 2528 if (is_log) 2529 continue; 2530 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 2531 continue; 2532 2533 vname = zpool_vdev_name(g_zfs, NULL, child[c], 2534 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 2535 print_import_config(cb, vname, child[c], depth + 2); 2536 free(vname); 2537 } 2538 2539 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2540 &child, &children) == 0) { 2541 (void) printf(gettext("\tcache\n")); 2542 for (c = 0; c < children; c++) { 2543 vname = zpool_vdev_name(g_zfs, NULL, child[c], 2544 cb->cb_name_flags); 2545 (void) printf("\t %s\n", vname); 2546 free(vname); 2547 } 2548 } 2549 2550 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 2551 &child, &children) == 0) { 2552 (void) printf(gettext("\tspares\n")); 2553 for (c = 0; c < children; c++) { 2554 vname = zpool_vdev_name(g_zfs, NULL, child[c], 2555 cb->cb_name_flags); 2556 (void) printf("\t %s\n", vname); 2557 free(vname); 2558 } 2559 } 2560 } 2561 2562 /* 2563 * Print specialized class vdevs. 2564 * 2565 * These are recorded as top level vdevs in the main pool child array 2566 * but with "is_log" set to 1 or an "alloc_bias" string. We use either 2567 * print_status_config() or print_import_config() to print the top level 2568 * class vdevs then any of their children (eg mirrored slogs) are printed 2569 * recursively - which works because only the top level vdev is marked. 2570 */ 2571 static void 2572 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 2573 const char *class) 2574 { 2575 uint_t c, children; 2576 nvlist_t **child; 2577 boolean_t printed = B_FALSE; 2578 2579 assert(zhp != NULL || !cb->cb_verbose); 2580 2581 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 2582 &children) != 0) 2583 return; 2584 2585 for (c = 0; c < children; c++) { 2586 uint64_t is_log = B_FALSE; 2587 char *bias = NULL; 2588 char *type = NULL; 2589 2590 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 2591 &is_log); 2592 2593 if (is_log) { 2594 bias = VDEV_ALLOC_CLASS_LOGS; 2595 } else { 2596 (void) nvlist_lookup_string(child[c], 2597 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 2598 (void) nvlist_lookup_string(child[c], 2599 ZPOOL_CONFIG_TYPE, &type); 2600 } 2601 2602 if (bias == NULL || strcmp(bias, class) != 0) 2603 continue; 2604 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 2605 continue; 2606 2607 if (!printed) { 2608 (void) printf("\t%s\t\n", gettext(class)); 2609 printed = B_TRUE; 2610 } 2611 2612 char *name = zpool_vdev_name(g_zfs, zhp, child[c], 2613 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 2614 if (cb->cb_print_status) 2615 print_status_config(zhp, cb, name, child[c], 2, 2616 B_FALSE, NULL); 2617 else 2618 print_import_config(cb, name, child[c], 2); 2619 free(name); 2620 } 2621 } 2622 2623 /* 2624 * Display the status for the given pool. 2625 */ 2626 static void 2627 show_import(nvlist_t *config) 2628 { 2629 uint64_t pool_state; 2630 vdev_stat_t *vs; 2631 char *name; 2632 uint64_t guid; 2633 uint64_t hostid = 0; 2634 char *msgid; 2635 char *hostname = "unknown"; 2636 nvlist_t *nvroot, *nvinfo; 2637 zpool_status_t reason; 2638 zpool_errata_t errata; 2639 const char *health; 2640 uint_t vsc; 2641 char *comment; 2642 status_cbdata_t cb = { 0 }; 2643 2644 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 2645 &name) == 0); 2646 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 2647 &guid) == 0); 2648 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2649 &pool_state) == 0); 2650 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2651 &nvroot) == 0); 2652 2653 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 2654 (uint64_t **)&vs, &vsc) == 0); 2655 health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 2656 2657 reason = zpool_import_status(config, &msgid, &errata); 2658 2659 (void) printf(gettext(" pool: %s\n"), name); 2660 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 2661 (void) printf(gettext(" state: %s"), health); 2662 if (pool_state == POOL_STATE_DESTROYED) 2663 (void) printf(gettext(" (DESTROYED)")); 2664 (void) printf("\n"); 2665 2666 switch (reason) { 2667 case ZPOOL_STATUS_MISSING_DEV_R: 2668 case ZPOOL_STATUS_MISSING_DEV_NR: 2669 case ZPOOL_STATUS_BAD_GUID_SUM: 2670 printf_color(ANSI_BOLD, gettext("status: ")); 2671 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2672 "missing from the system.\n")); 2673 break; 2674 2675 case ZPOOL_STATUS_CORRUPT_LABEL_R: 2676 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 2677 printf_color(ANSI_BOLD, gettext("status: ")); 2678 printf_color(ANSI_YELLOW, gettext("One or more devices contains" 2679 " corrupted data.\n")); 2680 break; 2681 2682 case ZPOOL_STATUS_CORRUPT_DATA: 2683 (void) printf( 2684 gettext(" status: The pool data is corrupted.\n")); 2685 break; 2686 2687 case ZPOOL_STATUS_OFFLINE_DEV: 2688 printf_color(ANSI_BOLD, gettext("status: ")); 2689 printf_color(ANSI_YELLOW, gettext("One or more devices " 2690 "are offlined.\n")); 2691 break; 2692 2693 case ZPOOL_STATUS_CORRUPT_POOL: 2694 printf_color(ANSI_BOLD, gettext("status: ")); 2695 printf_color(ANSI_YELLOW, gettext("The pool metadata is " 2696 "corrupted.\n")); 2697 break; 2698 2699 case ZPOOL_STATUS_VERSION_OLDER: 2700 printf_color(ANSI_BOLD, gettext("status: ")); 2701 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 2702 "a legacy on-disk version.\n")); 2703 break; 2704 2705 case ZPOOL_STATUS_VERSION_NEWER: 2706 printf_color(ANSI_BOLD, gettext("status: ")); 2707 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 2708 "an incompatible version.\n")); 2709 break; 2710 2711 case ZPOOL_STATUS_FEAT_DISABLED: 2712 printf_color(ANSI_BOLD, gettext("status: ")); 2713 printf_color(ANSI_YELLOW, gettext("Some supported and " 2714 "requested features are not enabled on the pool.\n")); 2715 break; 2716 2717 case ZPOOL_STATUS_COMPATIBILITY_ERR: 2718 printf_color(ANSI_BOLD, gettext("status: ")); 2719 printf_color(ANSI_YELLOW, gettext("Error reading or parsing " 2720 "the file(s) indicated by the 'compatibility'\n" 2721 "property.\n")); 2722 break; 2723 2724 case ZPOOL_STATUS_UNSUP_FEAT_READ: 2725 printf_color(ANSI_BOLD, gettext("status: ")); 2726 printf_color(ANSI_YELLOW, gettext("The pool uses the following " 2727 "feature(s) not supported on this system:\n")); 2728 color_start(ANSI_YELLOW); 2729 zpool_print_unsup_feat(config); 2730 color_end(); 2731 break; 2732 2733 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 2734 printf_color(ANSI_BOLD, gettext("status: ")); 2735 printf_color(ANSI_YELLOW, gettext("The pool can only be " 2736 "accessed in read-only mode on this system. It\n\tcannot be" 2737 " accessed in read-write mode because it uses the " 2738 "following\n\tfeature(s) not supported on this system:\n")); 2739 color_start(ANSI_YELLOW); 2740 zpool_print_unsup_feat(config); 2741 color_end(); 2742 break; 2743 2744 case ZPOOL_STATUS_HOSTID_ACTIVE: 2745 printf_color(ANSI_BOLD, gettext("status: ")); 2746 printf_color(ANSI_YELLOW, gettext("The pool is currently " 2747 "imported by another system.\n")); 2748 break; 2749 2750 case ZPOOL_STATUS_HOSTID_REQUIRED: 2751 printf_color(ANSI_BOLD, gettext("status: ")); 2752 printf_color(ANSI_YELLOW, gettext("The pool has the " 2753 "multihost property on. It cannot\n\tbe safely imported " 2754 "when the system hostid is not set.\n")); 2755 break; 2756 2757 case ZPOOL_STATUS_HOSTID_MISMATCH: 2758 printf_color(ANSI_BOLD, gettext("status: ")); 2759 printf_color(ANSI_YELLOW, gettext("The pool was last accessed " 2760 "by another system.\n")); 2761 break; 2762 2763 case ZPOOL_STATUS_FAULTED_DEV_R: 2764 case ZPOOL_STATUS_FAULTED_DEV_NR: 2765 printf_color(ANSI_BOLD, gettext("status: ")); 2766 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2767 "faulted.\n")); 2768 break; 2769 2770 case ZPOOL_STATUS_BAD_LOG: 2771 printf_color(ANSI_BOLD, gettext("status: ")); 2772 printf_color(ANSI_YELLOW, gettext("An intent log record cannot " 2773 "be read.\n")); 2774 break; 2775 2776 case ZPOOL_STATUS_RESILVERING: 2777 case ZPOOL_STATUS_REBUILDING: 2778 printf_color(ANSI_BOLD, gettext("status: ")); 2779 printf_color(ANSI_YELLOW, gettext("One or more devices were " 2780 "being resilvered.\n")); 2781 break; 2782 2783 case ZPOOL_STATUS_ERRATA: 2784 printf_color(ANSI_BOLD, gettext("status: ")); 2785 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), 2786 errata); 2787 break; 2788 2789 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 2790 printf_color(ANSI_BOLD, gettext("status: ")); 2791 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2792 "configured to use a non-native block size.\n" 2793 "\tExpect reduced performance.\n")); 2794 break; 2795 2796 default: 2797 /* 2798 * No other status can be seen when importing pools. 2799 */ 2800 assert(reason == ZPOOL_STATUS_OK); 2801 } 2802 2803 /* 2804 * Print out an action according to the overall state of the pool. 2805 */ 2806 if (vs->vs_state == VDEV_STATE_HEALTHY) { 2807 if (reason == ZPOOL_STATUS_VERSION_OLDER || 2808 reason == ZPOOL_STATUS_FEAT_DISABLED) { 2809 (void) printf(gettext(" action: The pool can be " 2810 "imported using its name or numeric identifier, " 2811 "though\n\tsome features will not be available " 2812 "without an explicit 'zpool upgrade'.\n")); 2813 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) { 2814 (void) printf(gettext(" action: The pool can be " 2815 "imported using its name or numeric\n\tidentifier, " 2816 "though the file(s) indicated by its " 2817 "'compatibility'\n\tproperty cannot be parsed at " 2818 "this time.\n")); 2819 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 2820 (void) printf(gettext(" action: The pool can be " 2821 "imported using its name or numeric " 2822 "identifier and\n\tthe '-f' flag.\n")); 2823 } else if (reason == ZPOOL_STATUS_ERRATA) { 2824 switch (errata) { 2825 case ZPOOL_ERRATA_NONE: 2826 break; 2827 2828 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 2829 (void) printf(gettext(" action: The pool can " 2830 "be imported using its name or numeric " 2831 "identifier,\n\thowever there is a compat" 2832 "ibility issue which should be corrected" 2833 "\n\tby running 'zpool scrub'\n")); 2834 break; 2835 2836 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: 2837 (void) printf(gettext(" action: The pool can" 2838 "not be imported with this version of ZFS " 2839 "due to\n\tan active asynchronous destroy. " 2840 "Revert to an earlier version\n\tand " 2841 "allow the destroy to complete before " 2842 "updating.\n")); 2843 break; 2844 2845 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 2846 (void) printf(gettext(" action: Existing " 2847 "encrypted datasets contain an on-disk " 2848 "incompatibility, which\n\tneeds to be " 2849 "corrected. Backup these datasets to new " 2850 "encrypted datasets\n\tand destroy the " 2851 "old ones.\n")); 2852 break; 2853 2854 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 2855 (void) printf(gettext(" action: Existing " 2856 "encrypted snapshots and bookmarks contain " 2857 "an on-disk\n\tincompatibility. This may " 2858 "cause on-disk corruption if they are used" 2859 "\n\twith 'zfs recv'. To correct the " 2860 "issue, enable the bookmark_v2 feature.\n\t" 2861 "No additional action is needed if there " 2862 "are no encrypted snapshots or\n\t" 2863 "bookmarks. If preserving the encrypted " 2864 "snapshots and bookmarks is\n\trequired, " 2865 "use a non-raw send to backup and restore " 2866 "them. Alternately,\n\tthey may be removed" 2867 " to resolve the incompatibility.\n")); 2868 break; 2869 default: 2870 /* 2871 * All errata must contain an action message. 2872 */ 2873 assert(0); 2874 } 2875 } else { 2876 (void) printf(gettext(" action: The pool can be " 2877 "imported using its name or numeric " 2878 "identifier.\n")); 2879 } 2880 } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 2881 (void) printf(gettext(" action: The pool can be imported " 2882 "despite missing or damaged devices. The\n\tfault " 2883 "tolerance of the pool may be compromised if imported.\n")); 2884 } else { 2885 switch (reason) { 2886 case ZPOOL_STATUS_VERSION_NEWER: 2887 (void) printf(gettext(" action: The pool cannot be " 2888 "imported. Access the pool on a system running " 2889 "newer\n\tsoftware, or recreate the pool from " 2890 "backup.\n")); 2891 break; 2892 case ZPOOL_STATUS_UNSUP_FEAT_READ: 2893 printf_color(ANSI_BOLD, gettext("action: ")); 2894 printf_color(ANSI_YELLOW, gettext("The pool cannot be " 2895 "imported. Access the pool on a system that " 2896 "supports\n\tthe required feature(s), or recreate " 2897 "the pool from backup.\n")); 2898 break; 2899 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 2900 printf_color(ANSI_BOLD, gettext("action: ")); 2901 printf_color(ANSI_YELLOW, gettext("The pool cannot be " 2902 "imported in read-write mode. Import the pool " 2903 "with\n" 2904 "\t\"-o readonly=on\", access the pool on a system " 2905 "that supports the\n\trequired feature(s), or " 2906 "recreate the pool from backup.\n")); 2907 break; 2908 case ZPOOL_STATUS_MISSING_DEV_R: 2909 case ZPOOL_STATUS_MISSING_DEV_NR: 2910 case ZPOOL_STATUS_BAD_GUID_SUM: 2911 (void) printf(gettext(" action: The pool cannot be " 2912 "imported. Attach the missing\n\tdevices and try " 2913 "again.\n")); 2914 break; 2915 case ZPOOL_STATUS_HOSTID_ACTIVE: 2916 VERIFY0(nvlist_lookup_nvlist(config, 2917 ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); 2918 2919 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 2920 hostname = fnvlist_lookup_string(nvinfo, 2921 ZPOOL_CONFIG_MMP_HOSTNAME); 2922 2923 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 2924 hostid = fnvlist_lookup_uint64(nvinfo, 2925 ZPOOL_CONFIG_MMP_HOSTID); 2926 2927 (void) printf(gettext(" action: The pool must be " 2928 "exported from %s (hostid=%lx)\n\tbefore it " 2929 "can be safely imported.\n"), hostname, 2930 (unsigned long) hostid); 2931 break; 2932 case ZPOOL_STATUS_HOSTID_REQUIRED: 2933 (void) printf(gettext(" action: Set a unique system " 2934 "hostid with the zgenhostid(8) command.\n")); 2935 break; 2936 default: 2937 (void) printf(gettext(" action: The pool cannot be " 2938 "imported due to damaged devices or data.\n")); 2939 } 2940 } 2941 2942 /* Print the comment attached to the pool. */ 2943 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 2944 (void) printf(gettext("comment: %s\n"), comment); 2945 2946 /* 2947 * If the state is "closed" or "can't open", and the aux state 2948 * is "corrupt data": 2949 */ 2950 if (((vs->vs_state == VDEV_STATE_CLOSED) || 2951 (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 2952 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 2953 if (pool_state == POOL_STATE_DESTROYED) 2954 (void) printf(gettext("\tThe pool was destroyed, " 2955 "but can be imported using the '-Df' flags.\n")); 2956 else if (pool_state != POOL_STATE_EXPORTED) 2957 (void) printf(gettext("\tThe pool may be active on " 2958 "another system, but can be imported using\n\t" 2959 "the '-f' flag.\n")); 2960 } 2961 2962 if (msgid != NULL) { 2963 (void) printf(gettext( 2964 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"), 2965 msgid); 2966 } 2967 2968 (void) printf(gettext(" config:\n\n")); 2969 2970 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), 2971 VDEV_NAME_TYPE_ID); 2972 if (cb.cb_namewidth < 10) 2973 cb.cb_namewidth = 10; 2974 2975 print_import_config(&cb, name, nvroot, 0); 2976 2977 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); 2978 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 2979 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); 2980 2981 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 2982 (void) printf(gettext("\n\tAdditional devices are known to " 2983 "be part of this pool, though their\n\texact " 2984 "configuration cannot be determined.\n")); 2985 } 2986 } 2987 2988 static boolean_t 2989 zfs_force_import_required(nvlist_t *config) 2990 { 2991 uint64_t state; 2992 uint64_t hostid = 0; 2993 nvlist_t *nvinfo; 2994 2995 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); 2996 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid); 2997 2998 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) 2999 return (B_TRUE); 3000 3001 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3002 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { 3003 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, 3004 ZPOOL_CONFIG_MMP_STATE); 3005 3006 if (mmp_state != MMP_STATE_INACTIVE) 3007 return (B_TRUE); 3008 } 3009 3010 return (B_FALSE); 3011 } 3012 3013 /* 3014 * Perform the import for the given configuration. This passes the heavy 3015 * lifting off to zpool_import_props(), and then mounts the datasets contained 3016 * within the pool. 3017 */ 3018 static int 3019 do_import(nvlist_t *config, const char *newname, const char *mntopts, 3020 nvlist_t *props, int flags) 3021 { 3022 int ret = 0; 3023 zpool_handle_t *zhp; 3024 char *name; 3025 uint64_t version; 3026 3027 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); 3028 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); 3029 3030 if (!SPA_VERSION_IS_SUPPORTED(version)) { 3031 (void) fprintf(stderr, gettext("cannot import '%s': pool " 3032 "is formatted using an unsupported ZFS version\n"), name); 3033 return (1); 3034 } else if (zfs_force_import_required(config) && 3035 !(flags & ZFS_IMPORT_ANY_HOST)) { 3036 mmp_state_t mmp_state = MMP_STATE_INACTIVE; 3037 nvlist_t *nvinfo; 3038 3039 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3040 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) 3041 mmp_state = fnvlist_lookup_uint64(nvinfo, 3042 ZPOOL_CONFIG_MMP_STATE); 3043 3044 if (mmp_state == MMP_STATE_ACTIVE) { 3045 char *hostname = "<unknown>"; 3046 uint64_t hostid = 0; 3047 3048 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 3049 hostname = fnvlist_lookup_string(nvinfo, 3050 ZPOOL_CONFIG_MMP_HOSTNAME); 3051 3052 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 3053 hostid = fnvlist_lookup_uint64(nvinfo, 3054 ZPOOL_CONFIG_MMP_HOSTID); 3055 3056 (void) fprintf(stderr, gettext("cannot import '%s': " 3057 "pool is imported on %s (hostid: " 3058 "0x%lx)\nExport the pool on the other system, " 3059 "then run 'zpool import'.\n"), 3060 name, hostname, (unsigned long) hostid); 3061 } else if (mmp_state == MMP_STATE_NO_HOSTID) { 3062 (void) fprintf(stderr, gettext("Cannot import '%s': " 3063 "pool has the multihost property on and the\n" 3064 "system's hostid is not set. Set a unique hostid " 3065 "with the zgenhostid(8) command.\n"), name); 3066 } else { 3067 char *hostname = "<unknown>"; 3068 uint64_t timestamp = 0; 3069 uint64_t hostid = 0; 3070 3071 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) 3072 hostname = fnvlist_lookup_string(config, 3073 ZPOOL_CONFIG_HOSTNAME); 3074 3075 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) 3076 timestamp = fnvlist_lookup_uint64(config, 3077 ZPOOL_CONFIG_TIMESTAMP); 3078 3079 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) 3080 hostid = fnvlist_lookup_uint64(config, 3081 ZPOOL_CONFIG_HOSTID); 3082 3083 (void) fprintf(stderr, gettext("cannot import '%s': " 3084 "pool was previously in use from another system.\n" 3085 "Last accessed by %s (hostid=%lx) at %s" 3086 "The pool can be imported, use 'zpool import -f' " 3087 "to import the pool.\n"), name, hostname, 3088 (unsigned long)hostid, ctime((time_t *)×tamp)); 3089 } 3090 3091 return (1); 3092 } 3093 3094 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 3095 return (1); 3096 3097 if (newname != NULL) 3098 name = (char *)newname; 3099 3100 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 3101 return (1); 3102 3103 /* 3104 * Loading keys is best effort. We don't want to return immediately 3105 * if it fails but we do want to give the error to the caller. 3106 */ 3107 if (flags & ZFS_IMPORT_LOAD_KEYS) { 3108 ret = zfs_crypto_attempt_load_keys(g_zfs, name); 3109 if (ret != 0) 3110 ret = 1; 3111 } 3112 3113 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3114 !(flags & ZFS_IMPORT_ONLY) && 3115 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 3116 zpool_close(zhp); 3117 return (1); 3118 } 3119 3120 zpool_close(zhp); 3121 return (ret); 3122 } 3123 3124 typedef struct target_exists_args { 3125 const char *poolname; 3126 uint64_t poolguid; 3127 } target_exists_args_t; 3128 3129 static int 3130 name_or_guid_exists(zpool_handle_t *zhp, void *data) 3131 { 3132 target_exists_args_t *args = data; 3133 nvlist_t *config = zpool_get_config(zhp, NULL); 3134 int found = 0; 3135 3136 if (config == NULL) 3137 return (0); 3138 3139 if (args->poolname != NULL) { 3140 char *pool_name; 3141 3142 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 3143 &pool_name) == 0); 3144 if (strcmp(pool_name, args->poolname) == 0) 3145 found = 1; 3146 } else { 3147 uint64_t pool_guid; 3148 3149 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 3150 &pool_guid) == 0); 3151 if (pool_guid == args->poolguid) 3152 found = 1; 3153 } 3154 zpool_close(zhp); 3155 3156 return (found); 3157 } 3158 /* 3159 * zpool checkpoint <pool> 3160 * checkpoint --discard <pool> 3161 * 3162 * -d Discard the checkpoint from a checkpointed 3163 * --discard pool. 3164 * 3165 * -w Wait for discarding a checkpoint to complete. 3166 * --wait 3167 * 3168 * Checkpoints the specified pool, by taking a "snapshot" of its 3169 * current state. A pool can only have one checkpoint at a time. 3170 */ 3171 int 3172 zpool_do_checkpoint(int argc, char **argv) 3173 { 3174 boolean_t discard, wait; 3175 char *pool; 3176 zpool_handle_t *zhp; 3177 int c, err; 3178 3179 struct option long_options[] = { 3180 {"discard", no_argument, NULL, 'd'}, 3181 {"wait", no_argument, NULL, 'w'}, 3182 {0, 0, 0, 0} 3183 }; 3184 3185 discard = B_FALSE; 3186 wait = B_FALSE; 3187 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) { 3188 switch (c) { 3189 case 'd': 3190 discard = B_TRUE; 3191 break; 3192 case 'w': 3193 wait = B_TRUE; 3194 break; 3195 case '?': 3196 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3197 optopt); 3198 usage(B_FALSE); 3199 } 3200 } 3201 3202 if (wait && !discard) { 3203 (void) fprintf(stderr, gettext("--wait only valid when " 3204 "--discard also specified\n")); 3205 usage(B_FALSE); 3206 } 3207 3208 argc -= optind; 3209 argv += optind; 3210 3211 if (argc < 1) { 3212 (void) fprintf(stderr, gettext("missing pool argument\n")); 3213 usage(B_FALSE); 3214 } 3215 3216 if (argc > 1) { 3217 (void) fprintf(stderr, gettext("too many arguments\n")); 3218 usage(B_FALSE); 3219 } 3220 3221 pool = argv[0]; 3222 3223 if ((zhp = zpool_open(g_zfs, pool)) == NULL) { 3224 /* As a special case, check for use of '/' in the name */ 3225 if (strchr(pool, '/') != NULL) 3226 (void) fprintf(stderr, gettext("'zpool checkpoint' " 3227 "doesn't work on datasets. To save the state " 3228 "of a dataset from a specific point in time " 3229 "please use 'zfs snapshot'\n")); 3230 return (1); 3231 } 3232 3233 if (discard) { 3234 err = (zpool_discard_checkpoint(zhp) != 0); 3235 if (err == 0 && wait) 3236 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD); 3237 } else { 3238 err = (zpool_checkpoint(zhp) != 0); 3239 } 3240 3241 zpool_close(zhp); 3242 3243 return (err); 3244 } 3245 3246 #define CHECKPOINT_OPT 1024 3247 3248 /* 3249 * zpool import [-d dir] [-D] 3250 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 3251 * [-d dir | -c cachefile] [-f] -a 3252 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 3253 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] 3254 * 3255 * -c Read pool information from a cachefile instead of searching 3256 * devices. 3257 * 3258 * -d Scan in a specific directory, other than /dev/. More than 3259 * one directory can be specified using multiple '-d' options. 3260 * 3261 * -D Scan for previously destroyed pools or import all or only 3262 * specified destroyed pools. 3263 * 3264 * -R Temporarily import the pool, with all mountpoints relative to 3265 * the given root. The pool will remain exported when the machine 3266 * is rebooted. 3267 * 3268 * -V Import even in the presence of faulted vdevs. This is an 3269 * intentionally undocumented option for testing purposes, and 3270 * treats the pool configuration as complete, leaving any bad 3271 * vdevs in the FAULTED state. In other words, it does verbatim 3272 * import. 3273 * 3274 * -f Force import, even if it appears that the pool is active. 3275 * 3276 * -F Attempt rewind if necessary. 3277 * 3278 * -n See if rewind would work, but don't actually rewind. 3279 * 3280 * -N Import the pool but don't mount datasets. 3281 * 3282 * -T Specify a starting txg to use for import. This option is 3283 * intentionally undocumented option for testing purposes. 3284 * 3285 * -a Import all pools found. 3286 * 3287 * -l Load encryption keys while importing. 3288 * 3289 * -o Set property=value and/or temporary mount options (without '='). 3290 * 3291 * -s Scan using the default search path, the libblkid cache will 3292 * not be consulted. 3293 * 3294 * --rewind-to-checkpoint 3295 * Import the pool and revert back to the checkpoint. 3296 * 3297 * The import command scans for pools to import, and import pools based on pool 3298 * name and GUID. The pool can also be renamed as part of the import process. 3299 */ 3300 int 3301 zpool_do_import(int argc, char **argv) 3302 { 3303 char **searchdirs = NULL; 3304 char *env, *envdup = NULL; 3305 int nsearch = 0; 3306 int c; 3307 int err = 0; 3308 nvlist_t *pools = NULL; 3309 boolean_t do_all = B_FALSE; 3310 boolean_t do_destroyed = B_FALSE; 3311 char *mntopts = NULL; 3312 nvpair_t *elem; 3313 nvlist_t *config; 3314 uint64_t searchguid = 0; 3315 char *searchname = NULL; 3316 char *propval; 3317 nvlist_t *found_config; 3318 nvlist_t *policy = NULL; 3319 nvlist_t *props = NULL; 3320 boolean_t first; 3321 int flags = ZFS_IMPORT_NORMAL; 3322 uint32_t rewind_policy = ZPOOL_NO_REWIND; 3323 boolean_t dryrun = B_FALSE; 3324 boolean_t do_rewind = B_FALSE; 3325 boolean_t xtreme_rewind = B_FALSE; 3326 boolean_t do_scan = B_FALSE; 3327 boolean_t pool_exists = B_FALSE; 3328 uint64_t pool_state, txg = -1ULL; 3329 char *cachefile = NULL; 3330 importargs_t idata = { 0 }; 3331 char *endptr; 3332 3333 struct option long_options[] = { 3334 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, 3335 {0, 0, 0, 0} 3336 }; 3337 3338 /* check options */ 3339 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", 3340 long_options, NULL)) != -1) { 3341 switch (c) { 3342 case 'a': 3343 do_all = B_TRUE; 3344 break; 3345 case 'c': 3346 cachefile = optarg; 3347 break; 3348 case 'd': 3349 if (searchdirs == NULL) { 3350 searchdirs = safe_malloc(sizeof (char *)); 3351 } else { 3352 char **tmp = safe_malloc((nsearch + 1) * 3353 sizeof (char *)); 3354 bcopy(searchdirs, tmp, nsearch * 3355 sizeof (char *)); 3356 free(searchdirs); 3357 searchdirs = tmp; 3358 } 3359 searchdirs[nsearch++] = optarg; 3360 break; 3361 case 'D': 3362 do_destroyed = B_TRUE; 3363 break; 3364 case 'f': 3365 flags |= ZFS_IMPORT_ANY_HOST; 3366 break; 3367 case 'F': 3368 do_rewind = B_TRUE; 3369 break; 3370 case 'l': 3371 flags |= ZFS_IMPORT_LOAD_KEYS; 3372 break; 3373 case 'm': 3374 flags |= ZFS_IMPORT_MISSING_LOG; 3375 break; 3376 case 'n': 3377 dryrun = B_TRUE; 3378 break; 3379 case 'N': 3380 flags |= ZFS_IMPORT_ONLY; 3381 break; 3382 case 'o': 3383 if ((propval = strchr(optarg, '=')) != NULL) { 3384 *propval = '\0'; 3385 propval++; 3386 if (add_prop_list(optarg, propval, 3387 &props, B_TRUE)) 3388 goto error; 3389 } else { 3390 mntopts = optarg; 3391 } 3392 break; 3393 case 'R': 3394 if (add_prop_list(zpool_prop_to_name( 3395 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 3396 goto error; 3397 if (add_prop_list_default(zpool_prop_to_name( 3398 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 3399 goto error; 3400 break; 3401 case 's': 3402 do_scan = B_TRUE; 3403 break; 3404 case 't': 3405 flags |= ZFS_IMPORT_TEMP_NAME; 3406 if (add_prop_list_default(zpool_prop_to_name( 3407 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 3408 goto error; 3409 break; 3410 3411 case 'T': 3412 errno = 0; 3413 txg = strtoull(optarg, &endptr, 0); 3414 if (errno != 0 || *endptr != '\0') { 3415 (void) fprintf(stderr, 3416 gettext("invalid txg value\n")); 3417 usage(B_FALSE); 3418 } 3419 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 3420 break; 3421 case 'V': 3422 flags |= ZFS_IMPORT_VERBATIM; 3423 break; 3424 case 'X': 3425 xtreme_rewind = B_TRUE; 3426 break; 3427 case CHECKPOINT_OPT: 3428 flags |= ZFS_IMPORT_CHECKPOINT; 3429 break; 3430 case ':': 3431 (void) fprintf(stderr, gettext("missing argument for " 3432 "'%c' option\n"), optopt); 3433 usage(B_FALSE); 3434 break; 3435 case '?': 3436 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3437 optopt); 3438 usage(B_FALSE); 3439 } 3440 } 3441 3442 argc -= optind; 3443 argv += optind; 3444 3445 if (cachefile && nsearch != 0) { 3446 (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 3447 usage(B_FALSE); 3448 } 3449 3450 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { 3451 (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); 3452 usage(B_FALSE); 3453 } 3454 3455 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { 3456 (void) fprintf(stderr, gettext("-l is only meaningful during " 3457 "an import\n")); 3458 usage(B_FALSE); 3459 } 3460 3461 if ((dryrun || xtreme_rewind) && !do_rewind) { 3462 (void) fprintf(stderr, 3463 gettext("-n or -X only meaningful with -F\n")); 3464 usage(B_FALSE); 3465 } 3466 if (dryrun) 3467 rewind_policy = ZPOOL_TRY_REWIND; 3468 else if (do_rewind) 3469 rewind_policy = ZPOOL_DO_REWIND; 3470 if (xtreme_rewind) 3471 rewind_policy |= ZPOOL_EXTREME_REWIND; 3472 3473 /* In the future, we can capture further policy and include it here */ 3474 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3475 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || 3476 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 3477 rewind_policy) != 0) 3478 goto error; 3479 3480 /* check argument count */ 3481 if (do_all) { 3482 if (argc != 0) { 3483 (void) fprintf(stderr, gettext("too many arguments\n")); 3484 usage(B_FALSE); 3485 } 3486 } else { 3487 if (argc > 2) { 3488 (void) fprintf(stderr, gettext("too many arguments\n")); 3489 usage(B_FALSE); 3490 } 3491 } 3492 3493 /* 3494 * Check for the effective uid. We do this explicitly here because 3495 * otherwise any attempt to discover pools will silently fail. 3496 */ 3497 if (argc == 0 && geteuid() != 0) { 3498 (void) fprintf(stderr, gettext("cannot " 3499 "discover pools: permission denied\n")); 3500 if (searchdirs != NULL) 3501 free(searchdirs); 3502 3503 nvlist_free(props); 3504 nvlist_free(policy); 3505 return (1); 3506 } 3507 3508 /* 3509 * Depending on the arguments given, we do one of the following: 3510 * 3511 * <none> Iterate through all pools and display information about 3512 * each one. 3513 * 3514 * -a Iterate through all pools and try to import each one. 3515 * 3516 * <id> Find the pool that corresponds to the given GUID/pool 3517 * name and import that one. 3518 * 3519 * -D Above options applies only to destroyed pools. 3520 */ 3521 if (argc != 0) { 3522 char *endptr; 3523 3524 errno = 0; 3525 searchguid = strtoull(argv[0], &endptr, 10); 3526 if (errno != 0 || *endptr != '\0') { 3527 searchname = argv[0]; 3528 searchguid = 0; 3529 } 3530 found_config = NULL; 3531 3532 /* 3533 * User specified a name or guid. Ensure it's unique. 3534 */ 3535 target_exists_args_t search = {searchname, searchguid}; 3536 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); 3537 } 3538 3539 /* 3540 * Check the environment for the preferred search path. 3541 */ 3542 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { 3543 char *dir; 3544 3545 envdup = strdup(env); 3546 3547 dir = strtok(envdup, ":"); 3548 while (dir != NULL) { 3549 if (searchdirs == NULL) { 3550 searchdirs = safe_malloc(sizeof (char *)); 3551 } else { 3552 char **tmp = safe_malloc((nsearch + 1) * 3553 sizeof (char *)); 3554 bcopy(searchdirs, tmp, nsearch * 3555 sizeof (char *)); 3556 free(searchdirs); 3557 searchdirs = tmp; 3558 } 3559 searchdirs[nsearch++] = dir; 3560 dir = strtok(NULL, ":"); 3561 } 3562 } 3563 3564 idata.path = searchdirs; 3565 idata.paths = nsearch; 3566 idata.poolname = searchname; 3567 idata.guid = searchguid; 3568 idata.cachefile = cachefile; 3569 idata.scan = do_scan; 3570 idata.policy = policy; 3571 3572 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops); 3573 3574 if (pools != NULL && pool_exists && 3575 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 3576 (void) fprintf(stderr, gettext("cannot import '%s': " 3577 "a pool with that name already exists\n"), 3578 argv[0]); 3579 (void) fprintf(stderr, gettext("use the form '%s " 3580 "<pool | id> <newpool>' to give it a new name\n"), 3581 "zpool import"); 3582 err = 1; 3583 } else if (pools == NULL && pool_exists) { 3584 (void) fprintf(stderr, gettext("cannot import '%s': " 3585 "a pool with that name is already created/imported,\n"), 3586 argv[0]); 3587 (void) fprintf(stderr, gettext("and no additional pools " 3588 "with that name were found\n")); 3589 err = 1; 3590 } else if (pools == NULL) { 3591 if (argc != 0) { 3592 (void) fprintf(stderr, gettext("cannot import '%s': " 3593 "no such pool available\n"), argv[0]); 3594 } 3595 err = 1; 3596 } 3597 3598 if (err == 1) { 3599 if (searchdirs != NULL) 3600 free(searchdirs); 3601 if (envdup != NULL) 3602 free(envdup); 3603 nvlist_free(policy); 3604 nvlist_free(pools); 3605 nvlist_free(props); 3606 return (1); 3607 } 3608 3609 /* 3610 * At this point we have a list of import candidate configs. Even if 3611 * we were searching by pool name or guid, we still need to 3612 * post-process the list to deal with pool state and possible 3613 * duplicate names. 3614 */ 3615 err = 0; 3616 elem = NULL; 3617 first = B_TRUE; 3618 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3619 3620 verify(nvpair_value_nvlist(elem, &config) == 0); 3621 3622 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3623 &pool_state) == 0); 3624 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 3625 continue; 3626 if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 3627 continue; 3628 3629 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 3630 policy) == 0); 3631 3632 if (argc == 0) { 3633 if (first) 3634 first = B_FALSE; 3635 else if (!do_all) 3636 (void) printf("\n"); 3637 3638 if (do_all) { 3639 err |= do_import(config, NULL, mntopts, 3640 props, flags); 3641 } else { 3642 show_import(config); 3643 } 3644 } else if (searchname != NULL) { 3645 char *name; 3646 3647 /* 3648 * We are searching for a pool based on name. 3649 */ 3650 verify(nvlist_lookup_string(config, 3651 ZPOOL_CONFIG_POOL_NAME, &name) == 0); 3652 3653 if (strcmp(name, searchname) == 0) { 3654 if (found_config != NULL) { 3655 (void) fprintf(stderr, gettext( 3656 "cannot import '%s': more than " 3657 "one matching pool\n"), searchname); 3658 (void) fprintf(stderr, gettext( 3659 "import by numeric ID instead\n")); 3660 err = B_TRUE; 3661 } 3662 found_config = config; 3663 } 3664 } else { 3665 uint64_t guid; 3666 3667 /* 3668 * Search for a pool by guid. 3669 */ 3670 verify(nvlist_lookup_uint64(config, 3671 ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 3672 3673 if (guid == searchguid) 3674 found_config = config; 3675 } 3676 } 3677 3678 /* 3679 * If we were searching for a specific pool, verify that we found a 3680 * pool, and then do the import. 3681 */ 3682 if (argc != 0 && err == 0) { 3683 if (found_config == NULL) { 3684 (void) fprintf(stderr, gettext("cannot import '%s': " 3685 "no such pool available\n"), argv[0]); 3686 err = B_TRUE; 3687 } else { 3688 err |= do_import(found_config, argc == 1 ? NULL : 3689 argv[1], mntopts, props, flags); 3690 } 3691 } 3692 3693 /* 3694 * If we were just looking for pools, report an error if none were 3695 * found. 3696 */ 3697 if (argc == 0 && first) 3698 (void) fprintf(stderr, 3699 gettext("no pools available to import\n")); 3700 3701 error: 3702 nvlist_free(props); 3703 nvlist_free(pools); 3704 nvlist_free(policy); 3705 if (searchdirs != NULL) 3706 free(searchdirs); 3707 if (envdup != NULL) 3708 free(envdup); 3709 3710 return (err ? 1 : 0); 3711 } 3712 3713 /* 3714 * zpool sync [-f] [pool] ... 3715 * 3716 * -f (undocumented) force uberblock (and config including zpool cache file) 3717 * update. 3718 * 3719 * Sync the specified pool(s). 3720 * Without arguments "zpool sync" will sync all pools. 3721 * This command initiates TXG sync(s) and will return after the TXG(s) commit. 3722 * 3723 */ 3724 static int 3725 zpool_do_sync(int argc, char **argv) 3726 { 3727 int ret; 3728 boolean_t force = B_FALSE; 3729 3730 /* check options */ 3731 while ((ret = getopt(argc, argv, "f")) != -1) { 3732 switch (ret) { 3733 case 'f': 3734 force = B_TRUE; 3735 break; 3736 case '?': 3737 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3738 optopt); 3739 usage(B_FALSE); 3740 } 3741 } 3742 3743 argc -= optind; 3744 argv += optind; 3745 3746 /* if argc == 0 we will execute zpool_sync_one on all pools */ 3747 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, zpool_sync_one, 3748 &force); 3749 3750 return (ret); 3751 } 3752 3753 typedef struct iostat_cbdata { 3754 uint64_t cb_flags; 3755 int cb_name_flags; 3756 int cb_namewidth; 3757 int cb_iteration; 3758 char **cb_vdev_names; /* Only show these vdevs */ 3759 unsigned int cb_vdev_names_count; 3760 boolean_t cb_verbose; 3761 boolean_t cb_literal; 3762 boolean_t cb_scripted; 3763 zpool_list_t *cb_list; 3764 vdev_cmd_data_list_t *vcdl; 3765 } iostat_cbdata_t; 3766 3767 /* iostat labels */ 3768 typedef struct name_and_columns { 3769 const char *name; /* Column name */ 3770 unsigned int columns; /* Center name to this number of columns */ 3771 } name_and_columns_t; 3772 3773 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */ 3774 3775 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = 3776 { 3777 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, 3778 {NULL}}, 3779 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 3780 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL}}, 3781 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, 3782 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, 3783 {"trimq_write", 2}, {NULL}}, 3784 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 3785 {"asyncq_wait", 2}, {NULL}}, 3786 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, 3787 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, 3788 {"trim", 2}, {NULL}}, 3789 }; 3790 3791 /* Shorthand - if "columns" field not set, default to 1 column */ 3792 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = 3793 { 3794 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, 3795 {"write"}, {NULL}}, 3796 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 3797 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL}}, 3798 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, 3799 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, 3800 {"pend"}, {"activ"}, {NULL}}, 3801 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 3802 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL}}, 3803 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, 3804 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}}, 3805 }; 3806 3807 static const char *histo_to_title[] = { 3808 [IOS_L_HISTO] = "latency", 3809 [IOS_RQ_HISTO] = "req_size", 3810 }; 3811 3812 /* 3813 * Return the number of labels in a null-terminated name_and_columns_t 3814 * array. 3815 * 3816 */ 3817 static unsigned int 3818 label_array_len(const name_and_columns_t *labels) 3819 { 3820 int i = 0; 3821 3822 while (labels[i].name) 3823 i++; 3824 3825 return (i); 3826 } 3827 3828 /* 3829 * Return the number of strings in a null-terminated string array. 3830 * For example: 3831 * 3832 * const char foo[] = {"bar", "baz", NULL} 3833 * 3834 * returns 2 3835 */ 3836 static uint64_t 3837 str_array_len(const char *array[]) 3838 { 3839 uint64_t i = 0; 3840 while (array[i]) 3841 i++; 3842 3843 return (i); 3844 } 3845 3846 3847 /* 3848 * Return a default column width for default/latency/queue columns. This does 3849 * not include histograms, which have their columns autosized. 3850 */ 3851 static unsigned int 3852 default_column_width(iostat_cbdata_t *cb, enum iostat_type type) 3853 { 3854 unsigned long column_width = 5; /* Normal niceprint */ 3855 static unsigned long widths[] = { 3856 /* 3857 * Choose some sane default column sizes for printing the 3858 * raw numbers. 3859 */ 3860 [IOS_DEFAULT] = 15, /* 1PB capacity */ 3861 [IOS_LATENCY] = 10, /* 1B ns = 10sec */ 3862 [IOS_QUEUES] = 6, /* 1M queue entries */ 3863 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */ 3864 [IOS_RQ_HISTO] = 6, /* 1M queue entries */ 3865 }; 3866 3867 if (cb->cb_literal) 3868 column_width = widths[type]; 3869 3870 return (column_width); 3871 } 3872 3873 /* 3874 * Print the column labels, i.e: 3875 * 3876 * capacity operations bandwidth 3877 * alloc free read write read write ... 3878 * 3879 * If force_column_width is set, use it for the column width. If not set, use 3880 * the default column width. 3881 */ 3882 static void 3883 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, 3884 const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) 3885 { 3886 int i, idx, s; 3887 int text_start, rw_column_width, spaces_to_end; 3888 uint64_t flags = cb->cb_flags; 3889 uint64_t f; 3890 unsigned int column_width = force_column_width; 3891 3892 /* For each bit set in flags */ 3893 for (f = flags; f; f &= ~(1ULL << idx)) { 3894 idx = lowbit64(f) - 1; 3895 if (!force_column_width) 3896 column_width = default_column_width(cb, idx); 3897 /* Print our top labels centered over "read write" label. */ 3898 for (i = 0; i < label_array_len(labels[idx]); i++) { 3899 const char *name = labels[idx][i].name; 3900 /* 3901 * We treat labels[][].columns == 0 as shorthand 3902 * for one column. It makes writing out the label 3903 * tables more concise. 3904 */ 3905 unsigned int columns = MAX(1, labels[idx][i].columns); 3906 unsigned int slen = strlen(name); 3907 3908 rw_column_width = (column_width * columns) + 3909 (2 * (columns - 1)); 3910 3911 text_start = (int)((rw_column_width) / columns - 3912 slen / columns); 3913 if (text_start < 0) 3914 text_start = 0; 3915 3916 printf(" "); /* Two spaces between columns */ 3917 3918 /* Space from beginning of column to label */ 3919 for (s = 0; s < text_start; s++) 3920 printf(" "); 3921 3922 printf("%s", name); 3923 3924 /* Print space after label to end of column */ 3925 spaces_to_end = rw_column_width - text_start - slen; 3926 if (spaces_to_end < 0) 3927 spaces_to_end = 0; 3928 3929 for (s = 0; s < spaces_to_end; s++) 3930 printf(" "); 3931 } 3932 } 3933 } 3934 3935 3936 /* 3937 * print_cmd_columns - Print custom column titles from -c 3938 * 3939 * If the user specified the "zpool status|iostat -c" then print their custom 3940 * column titles in the header. For example, print_cmd_columns() would print 3941 * the " col1 col2" part of this: 3942 * 3943 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' 3944 * ... 3945 * capacity operations bandwidth 3946 * pool alloc free read write read write col1 col2 3947 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 3948 * mypool 269K 1008M 0 0 107 946 3949 * mirror 269K 1008M 0 0 107 946 3950 * sdb - - 0 0 102 473 val1 val2 3951 * sdc - - 0 0 5 473 val1 val2 3952 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 3953 */ 3954 static void 3955 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) 3956 { 3957 int i, j; 3958 vdev_cmd_data_t *data = &vcdl->data[0]; 3959 3960 if (vcdl->count == 0 || data == NULL) 3961 return; 3962 3963 /* 3964 * Each vdev cmd should have the same column names unless the user did 3965 * something weird with their cmd. Just take the column names from the 3966 * first vdev and assume it works for all of them. 3967 */ 3968 for (i = 0; i < vcdl->uniq_cols_cnt; i++) { 3969 printf(" "); 3970 if (use_dashes) { 3971 for (j = 0; j < vcdl->uniq_cols_width[i]; j++) 3972 printf("-"); 3973 } else { 3974 printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i], 3975 vcdl->uniq_cols[i]); 3976 } 3977 } 3978 } 3979 3980 3981 /* 3982 * Utility function to print out a line of dashes like: 3983 * 3984 * -------------------------------- ----- ----- ----- ----- ----- 3985 * 3986 * ...or a dashed named-row line like: 3987 * 3988 * logs - - - - - 3989 * 3990 * @cb: iostat data 3991 * 3992 * @force_column_width If non-zero, use the value as the column width. 3993 * Otherwise use the default column widths. 3994 * 3995 * @name: Print a dashed named-row line starting 3996 * with @name. Otherwise, print a regular 3997 * dashed line. 3998 */ 3999 static void 4000 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, 4001 const char *name) 4002 { 4003 int i; 4004 unsigned int namewidth; 4005 uint64_t flags = cb->cb_flags; 4006 uint64_t f; 4007 int idx; 4008 const name_and_columns_t *labels; 4009 const char *title; 4010 4011 4012 if (cb->cb_flags & IOS_ANYHISTO_M) { 4013 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 4014 } else if (cb->cb_vdev_names_count) { 4015 title = "vdev"; 4016 } else { 4017 title = "pool"; 4018 } 4019 4020 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 4021 name ? strlen(name) : 0); 4022 4023 4024 if (name) { 4025 printf("%-*s", namewidth, name); 4026 } else { 4027 for (i = 0; i < namewidth; i++) 4028 (void) printf("-"); 4029 } 4030 4031 /* For each bit in flags */ 4032 for (f = flags; f; f &= ~(1ULL << idx)) { 4033 unsigned int column_width; 4034 idx = lowbit64(f) - 1; 4035 if (force_column_width) 4036 column_width = force_column_width; 4037 else 4038 column_width = default_column_width(cb, idx); 4039 4040 labels = iostat_bottom_labels[idx]; 4041 for (i = 0; i < label_array_len(labels); i++) { 4042 if (name) 4043 printf(" %*s-", column_width - 1, " "); 4044 else 4045 printf(" %.*s", column_width, 4046 "--------------------"); 4047 } 4048 } 4049 } 4050 4051 4052 static void 4053 print_iostat_separator_impl(iostat_cbdata_t *cb, 4054 unsigned int force_column_width) 4055 { 4056 print_iostat_dashes(cb, force_column_width, NULL); 4057 } 4058 4059 static void 4060 print_iostat_separator(iostat_cbdata_t *cb) 4061 { 4062 print_iostat_separator_impl(cb, 0); 4063 } 4064 4065 static void 4066 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, 4067 const char *histo_vdev_name) 4068 { 4069 unsigned int namewidth; 4070 const char *title; 4071 4072 if (cb->cb_flags & IOS_ANYHISTO_M) { 4073 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 4074 } else if (cb->cb_vdev_names_count) { 4075 title = "vdev"; 4076 } else { 4077 title = "pool"; 4078 } 4079 4080 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 4081 histo_vdev_name ? strlen(histo_vdev_name) : 0); 4082 4083 if (histo_vdev_name) 4084 printf("%-*s", namewidth, histo_vdev_name); 4085 else 4086 printf("%*s", namewidth, ""); 4087 4088 4089 print_iostat_labels(cb, force_column_width, iostat_top_labels); 4090 printf("\n"); 4091 4092 printf("%-*s", namewidth, title); 4093 4094 print_iostat_labels(cb, force_column_width, iostat_bottom_labels); 4095 if (cb->vcdl != NULL) 4096 print_cmd_columns(cb->vcdl, 0); 4097 4098 printf("\n"); 4099 4100 print_iostat_separator_impl(cb, force_column_width); 4101 4102 if (cb->vcdl != NULL) 4103 print_cmd_columns(cb->vcdl, 1); 4104 4105 printf("\n"); 4106 } 4107 4108 static void 4109 print_iostat_header(iostat_cbdata_t *cb) 4110 { 4111 print_iostat_header_impl(cb, 0, NULL); 4112 } 4113 4114 4115 /* 4116 * Display a single statistic. 4117 */ 4118 static void 4119 print_one_stat(uint64_t value, enum zfs_nicenum_format format, 4120 unsigned int column_size, boolean_t scripted) 4121 { 4122 char buf[64]; 4123 4124 zfs_nicenum_format(value, buf, sizeof (buf), format); 4125 4126 if (scripted) 4127 printf("\t%s", buf); 4128 else 4129 printf(" %*s", column_size, buf); 4130 } 4131 4132 /* 4133 * Calculate the default vdev stats 4134 * 4135 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting 4136 * stats into calcvs. 4137 */ 4138 static void 4139 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, 4140 vdev_stat_t *calcvs) 4141 { 4142 int i; 4143 4144 memcpy(calcvs, newvs, sizeof (*calcvs)); 4145 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) 4146 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); 4147 4148 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) 4149 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); 4150 } 4151 4152 /* 4153 * Internal representation of the extended iostats data. 4154 * 4155 * The extended iostat stats are exported in nvlists as either uint64_t arrays 4156 * or single uint64_t's. We make both look like arrays to make them easier 4157 * to process. In order to make single uint64_t's look like arrays, we set 4158 * __data to the stat data, and then set *data = &__data with count = 1. Then, 4159 * we can just use *data and count. 4160 */ 4161 struct stat_array { 4162 uint64_t *data; 4163 uint_t count; /* Number of entries in data[] */ 4164 uint64_t __data; /* Only used when data is a single uint64_t */ 4165 }; 4166 4167 static uint64_t 4168 stat_histo_max(struct stat_array *nva, unsigned int len) 4169 { 4170 uint64_t max = 0; 4171 int i; 4172 for (i = 0; i < len; i++) 4173 max = MAX(max, array64_max(nva[i].data, nva[i].count)); 4174 4175 return (max); 4176 } 4177 4178 /* 4179 * Helper function to lookup a uint64_t array or uint64_t value and store its 4180 * data as a stat_array. If the nvpair is a single uint64_t value, then we make 4181 * it look like a one element array to make it easier to process. 4182 */ 4183 static int 4184 nvpair64_to_stat_array(nvlist_t *nvl, const char *name, 4185 struct stat_array *nva) 4186 { 4187 nvpair_t *tmp; 4188 int ret; 4189 4190 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); 4191 switch (nvpair_type(tmp)) { 4192 case DATA_TYPE_UINT64_ARRAY: 4193 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); 4194 break; 4195 case DATA_TYPE_UINT64: 4196 ret = nvpair_value_uint64(tmp, &nva->__data); 4197 nva->data = &nva->__data; 4198 nva->count = 1; 4199 break; 4200 default: 4201 /* Not a uint64_t */ 4202 ret = EINVAL; 4203 break; 4204 } 4205 4206 return (ret); 4207 } 4208 4209 /* 4210 * Given a list of nvlist names, look up the extended stats in newnv and oldnv, 4211 * subtract them, and return the results in a newly allocated stat_array. 4212 * You must free the returned array after you are done with it with 4213 * free_calc_stats(). 4214 * 4215 * Additionally, you can set "oldnv" to NULL if you simply want the newnv 4216 * values. 4217 */ 4218 static struct stat_array * 4219 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, 4220 nvlist_t *newnv) 4221 { 4222 nvlist_t *oldnvx = NULL, *newnvx; 4223 struct stat_array *oldnva, *newnva, *calcnva; 4224 int i, j; 4225 unsigned int alloc_size = (sizeof (struct stat_array)) * len; 4226 4227 /* Extract our extended stats nvlist from the main list */ 4228 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, 4229 &newnvx) == 0); 4230 if (oldnv) { 4231 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, 4232 &oldnvx) == 0); 4233 } 4234 4235 newnva = safe_malloc(alloc_size); 4236 oldnva = safe_malloc(alloc_size); 4237 calcnva = safe_malloc(alloc_size); 4238 4239 for (j = 0; j < len; j++) { 4240 verify(nvpair64_to_stat_array(newnvx, names[j], 4241 &newnva[j]) == 0); 4242 calcnva[j].count = newnva[j].count; 4243 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); 4244 calcnva[j].data = safe_malloc(alloc_size); 4245 memcpy(calcnva[j].data, newnva[j].data, alloc_size); 4246 4247 if (oldnvx) { 4248 verify(nvpair64_to_stat_array(oldnvx, names[j], 4249 &oldnva[j]) == 0); 4250 for (i = 0; i < oldnva[j].count; i++) 4251 calcnva[j].data[i] -= oldnva[j].data[i]; 4252 } 4253 } 4254 free(newnva); 4255 free(oldnva); 4256 return (calcnva); 4257 } 4258 4259 static void 4260 free_calc_stats(struct stat_array *nva, unsigned int len) 4261 { 4262 int i; 4263 for (i = 0; i < len; i++) 4264 free(nva[i].data); 4265 4266 free(nva); 4267 } 4268 4269 static void 4270 print_iostat_histo(struct stat_array *nva, unsigned int len, 4271 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, 4272 double scale) 4273 { 4274 int i, j; 4275 char buf[6]; 4276 uint64_t val; 4277 enum zfs_nicenum_format format; 4278 unsigned int buckets; 4279 unsigned int start_bucket; 4280 4281 if (cb->cb_literal) 4282 format = ZFS_NICENUM_RAW; 4283 else 4284 format = ZFS_NICENUM_1024; 4285 4286 /* All these histos are the same size, so just use nva[0].count */ 4287 buckets = nva[0].count; 4288 4289 if (cb->cb_flags & IOS_RQ_HISTO_M) { 4290 /* Start at 512 - req size should never be lower than this */ 4291 start_bucket = 9; 4292 } else { 4293 start_bucket = 0; 4294 } 4295 4296 for (j = start_bucket; j < buckets; j++) { 4297 /* Print histogram bucket label */ 4298 if (cb->cb_flags & IOS_L_HISTO_M) { 4299 /* Ending range of this bucket */ 4300 val = (1UL << (j + 1)) - 1; 4301 zfs_nicetime(val, buf, sizeof (buf)); 4302 } else { 4303 /* Request size (starting range of bucket) */ 4304 val = (1UL << j); 4305 zfs_nicenum(val, buf, sizeof (buf)); 4306 } 4307 4308 if (cb->cb_scripted) 4309 printf("%llu", (u_longlong_t)val); 4310 else 4311 printf("%-*s", namewidth, buf); 4312 4313 /* Print the values on the line */ 4314 for (i = 0; i < len; i++) { 4315 print_one_stat(nva[i].data[j] * scale, format, 4316 column_width, cb->cb_scripted); 4317 } 4318 printf("\n"); 4319 } 4320 } 4321 4322 static void 4323 print_solid_separator(unsigned int length) 4324 { 4325 while (length--) 4326 printf("-"); 4327 printf("\n"); 4328 } 4329 4330 static void 4331 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, 4332 nvlist_t *newnv, double scale, const char *name) 4333 { 4334 unsigned int column_width; 4335 unsigned int namewidth; 4336 unsigned int entire_width; 4337 enum iostat_type type; 4338 struct stat_array *nva; 4339 const char **names; 4340 unsigned int names_len; 4341 4342 /* What type of histo are we? */ 4343 type = IOS_HISTO_IDX(cb->cb_flags); 4344 4345 /* Get NULL-terminated array of nvlist names for our histo */ 4346 names = vsx_type_to_nvlist[type]; 4347 names_len = str_array_len(names); /* num of names */ 4348 4349 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); 4350 4351 if (cb->cb_literal) { 4352 column_width = MAX(5, 4353 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); 4354 } else { 4355 column_width = 5; 4356 } 4357 4358 namewidth = MAX(cb->cb_namewidth, 4359 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); 4360 4361 /* 4362 * Calculate the entire line width of what we're printing. The 4363 * +2 is for the two spaces between columns: 4364 */ 4365 /* read write */ 4366 /* ----- ----- */ 4367 /* |___| <---------- column_width */ 4368 /* */ 4369 /* |__________| <--- entire_width */ 4370 /* */ 4371 entire_width = namewidth + (column_width + 2) * 4372 label_array_len(iostat_bottom_labels[type]); 4373 4374 if (cb->cb_scripted) 4375 printf("%s\n", name); 4376 else 4377 print_iostat_header_impl(cb, column_width, name); 4378 4379 print_iostat_histo(nva, names_len, cb, column_width, 4380 namewidth, scale); 4381 4382 free_calc_stats(nva, names_len); 4383 if (!cb->cb_scripted) 4384 print_solid_separator(entire_width); 4385 } 4386 4387 /* 4388 * Calculate the average latency of a power-of-two latency histogram 4389 */ 4390 static uint64_t 4391 single_histo_average(uint64_t *histo, unsigned int buckets) 4392 { 4393 int i; 4394 uint64_t count = 0, total = 0; 4395 4396 for (i = 0; i < buckets; i++) { 4397 /* 4398 * Our buckets are power-of-two latency ranges. Use the 4399 * midpoint latency of each bucket to calculate the average. 4400 * For example: 4401 * 4402 * Bucket Midpoint 4403 * 8ns-15ns: 12ns 4404 * 16ns-31ns: 24ns 4405 * ... 4406 */ 4407 if (histo[i] != 0) { 4408 total += histo[i] * (((1UL << i) + ((1UL << i)/2))); 4409 count += histo[i]; 4410 } 4411 } 4412 4413 /* Prevent divide by zero */ 4414 return (count == 0 ? 0 : total / count); 4415 } 4416 4417 static void 4418 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv, 4419 nvlist_t *newnv) 4420 { 4421 int i; 4422 uint64_t val; 4423 const char *names[] = { 4424 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, 4425 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, 4426 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, 4427 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, 4428 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, 4429 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, 4430 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, 4431 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, 4432 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, 4433 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, 4434 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE, 4435 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, 4436 }; 4437 4438 struct stat_array *nva; 4439 4440 unsigned int column_width = default_column_width(cb, IOS_QUEUES); 4441 enum zfs_nicenum_format format; 4442 4443 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); 4444 4445 if (cb->cb_literal) 4446 format = ZFS_NICENUM_RAW; 4447 else 4448 format = ZFS_NICENUM_1024; 4449 4450 for (i = 0; i < ARRAY_SIZE(names); i++) { 4451 val = nva[i].data[0]; 4452 print_one_stat(val, format, column_width, cb->cb_scripted); 4453 } 4454 4455 free_calc_stats(nva, ARRAY_SIZE(names)); 4456 } 4457 4458 static void 4459 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, 4460 nvlist_t *newnv) 4461 { 4462 int i; 4463 uint64_t val; 4464 const char *names[] = { 4465 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 4466 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 4467 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 4468 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 4469 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, 4470 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, 4471 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, 4472 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, 4473 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, 4474 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 4475 }; 4476 struct stat_array *nva; 4477 4478 unsigned int column_width = default_column_width(cb, IOS_LATENCY); 4479 enum zfs_nicenum_format format; 4480 4481 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); 4482 4483 if (cb->cb_literal) 4484 format = ZFS_NICENUM_RAWTIME; 4485 else 4486 format = ZFS_NICENUM_TIME; 4487 4488 /* Print our avg latencies on the line */ 4489 for (i = 0; i < ARRAY_SIZE(names); i++) { 4490 /* Compute average latency for a latency histo */ 4491 val = single_histo_average(nva[i].data, nva[i].count); 4492 print_one_stat(val, format, column_width, cb->cb_scripted); 4493 } 4494 free_calc_stats(nva, ARRAY_SIZE(names)); 4495 } 4496 4497 /* 4498 * Print default statistics (capacity/operations/bandwidth) 4499 */ 4500 static void 4501 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) 4502 { 4503 unsigned int column_width = default_column_width(cb, IOS_DEFAULT); 4504 enum zfs_nicenum_format format; 4505 char na; /* char to print for "not applicable" values */ 4506 4507 if (cb->cb_literal) { 4508 format = ZFS_NICENUM_RAW; 4509 na = '0'; 4510 } else { 4511 format = ZFS_NICENUM_1024; 4512 na = '-'; 4513 } 4514 4515 /* only toplevel vdevs have capacity stats */ 4516 if (vs->vs_space == 0) { 4517 if (cb->cb_scripted) 4518 printf("\t%c\t%c", na, na); 4519 else 4520 printf(" %*c %*c", column_width, na, column_width, 4521 na); 4522 } else { 4523 print_one_stat(vs->vs_alloc, format, column_width, 4524 cb->cb_scripted); 4525 print_one_stat(vs->vs_space - vs->vs_alloc, format, 4526 column_width, cb->cb_scripted); 4527 } 4528 4529 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), 4530 format, column_width, cb->cb_scripted); 4531 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), 4532 format, column_width, cb->cb_scripted); 4533 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), 4534 format, column_width, cb->cb_scripted); 4535 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), 4536 format, column_width, cb->cb_scripted); 4537 } 4538 4539 static const char *class_name[] = { 4540 VDEV_ALLOC_BIAS_DEDUP, 4541 VDEV_ALLOC_BIAS_SPECIAL, 4542 VDEV_ALLOC_CLASS_LOGS 4543 }; 4544 4545 /* 4546 * Print out all the statistics for the given vdev. This can either be the 4547 * toplevel configuration, or called recursively. If 'name' is NULL, then this 4548 * is a verbose output, and we don't want to display the toplevel pool stats. 4549 * 4550 * Returns the number of stat lines printed. 4551 */ 4552 static unsigned int 4553 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 4554 nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 4555 { 4556 nvlist_t **oldchild, **newchild; 4557 uint_t c, children, oldchildren; 4558 vdev_stat_t *oldvs, *newvs, *calcvs; 4559 vdev_stat_t zerovs = { 0 }; 4560 char *vname; 4561 int i; 4562 int ret = 0; 4563 uint64_t tdelta; 4564 double scale; 4565 4566 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 4567 return (ret); 4568 4569 calcvs = safe_malloc(sizeof (*calcvs)); 4570 4571 if (oldnv != NULL) { 4572 verify(nvlist_lookup_uint64_array(oldnv, 4573 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 4574 } else { 4575 oldvs = &zerovs; 4576 } 4577 4578 /* Do we only want to see a specific vdev? */ 4579 for (i = 0; i < cb->cb_vdev_names_count; i++) { 4580 /* Yes we do. Is this the vdev? */ 4581 if (strcmp(name, cb->cb_vdev_names[i]) == 0) { 4582 /* 4583 * This is our vdev. Since it is the only vdev we 4584 * will be displaying, make depth = 0 so that it 4585 * doesn't get indented. 4586 */ 4587 depth = 0; 4588 break; 4589 } 4590 } 4591 4592 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) { 4593 /* Couldn't match the name */ 4594 goto children; 4595 } 4596 4597 4598 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 4599 (uint64_t **)&newvs, &c) == 0); 4600 4601 /* 4602 * Print the vdev name unless it's is a histogram. Histograms 4603 * display the vdev name in the header itself. 4604 */ 4605 if (!(cb->cb_flags & IOS_ANYHISTO_M)) { 4606 if (cb->cb_scripted) { 4607 printf("%s", name); 4608 } else { 4609 if (strlen(name) + depth > cb->cb_namewidth) 4610 (void) printf("%*s%s", depth, "", name); 4611 else 4612 (void) printf("%*s%s%*s", depth, "", name, 4613 (int)(cb->cb_namewidth - strlen(name) - 4614 depth), ""); 4615 } 4616 } 4617 4618 /* Calculate our scaling factor */ 4619 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 4620 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { 4621 /* 4622 * If we specify printing histograms with no time interval, then 4623 * print the histogram numbers over the entire lifetime of the 4624 * vdev. 4625 */ 4626 scale = 1; 4627 } else { 4628 if (tdelta == 0) 4629 scale = 1.0; 4630 else 4631 scale = (double)NANOSEC / tdelta; 4632 } 4633 4634 if (cb->cb_flags & IOS_DEFAULT_M) { 4635 calc_default_iostats(oldvs, newvs, calcvs); 4636 print_iostat_default(calcvs, cb, scale); 4637 } 4638 if (cb->cb_flags & IOS_LATENCY_M) 4639 print_iostat_latency(cb, oldnv, newnv); 4640 if (cb->cb_flags & IOS_QUEUES_M) 4641 print_iostat_queues(cb, oldnv, newnv); 4642 if (cb->cb_flags & IOS_ANYHISTO_M) { 4643 printf("\n"); 4644 print_iostat_histos(cb, oldnv, newnv, scale, name); 4645 } 4646 4647 if (cb->vcdl != NULL) { 4648 char *path; 4649 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, 4650 &path) == 0) { 4651 printf(" "); 4652 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); 4653 } 4654 } 4655 4656 if (!(cb->cb_flags & IOS_ANYHISTO_M)) 4657 printf("\n"); 4658 4659 ret++; 4660 4661 children: 4662 4663 free(calcvs); 4664 4665 if (!cb->cb_verbose) 4666 return (ret); 4667 4668 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 4669 &newchild, &children) != 0) 4670 return (ret); 4671 4672 if (oldnv) { 4673 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 4674 &oldchild, &oldchildren) != 0) 4675 return (ret); 4676 4677 children = MIN(oldchildren, children); 4678 } 4679 4680 /* 4681 * print normal top-level devices 4682 */ 4683 for (c = 0; c < children; c++) { 4684 uint64_t ishole = B_FALSE, islog = B_FALSE; 4685 4686 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 4687 &ishole); 4688 4689 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 4690 &islog); 4691 4692 if (ishole || islog) 4693 continue; 4694 4695 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 4696 continue; 4697 4698 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4699 cb->cb_name_flags); 4700 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 4701 newchild[c], cb, depth + 2); 4702 free(vname); 4703 } 4704 4705 /* 4706 * print all other top-level devices 4707 */ 4708 for (uint_t n = 0; n < 3; n++) { 4709 boolean_t printed = B_FALSE; 4710 4711 for (c = 0; c < children; c++) { 4712 uint64_t islog = B_FALSE; 4713 char *bias = NULL; 4714 char *type = NULL; 4715 4716 (void) nvlist_lookup_uint64(newchild[c], 4717 ZPOOL_CONFIG_IS_LOG, &islog); 4718 if (islog) { 4719 bias = VDEV_ALLOC_CLASS_LOGS; 4720 } else { 4721 (void) nvlist_lookup_string(newchild[c], 4722 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 4723 (void) nvlist_lookup_string(newchild[c], 4724 ZPOOL_CONFIG_TYPE, &type); 4725 } 4726 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 4727 continue; 4728 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 4729 continue; 4730 4731 if (!printed) { 4732 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && 4733 !cb->cb_scripted && !cb->cb_vdev_names) { 4734 print_iostat_dashes(cb, 0, 4735 class_name[n]); 4736 } 4737 printf("\n"); 4738 printed = B_TRUE; 4739 } 4740 4741 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4742 cb->cb_name_flags); 4743 ret += print_vdev_stats(zhp, vname, oldnv ? 4744 oldchild[c] : NULL, newchild[c], cb, depth + 2); 4745 free(vname); 4746 } 4747 } 4748 4749 /* 4750 * Include level 2 ARC devices in iostat output 4751 */ 4752 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 4753 &newchild, &children) != 0) 4754 return (ret); 4755 4756 if (oldnv) { 4757 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 4758 &oldchild, &oldchildren) != 0) 4759 return (ret); 4760 4761 children = MIN(oldchildren, children); 4762 } 4763 4764 if (children > 0) { 4765 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && 4766 !cb->cb_vdev_names) { 4767 print_iostat_dashes(cb, 0, "cache"); 4768 } 4769 printf("\n"); 4770 4771 for (c = 0; c < children; c++) { 4772 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4773 cb->cb_name_flags); 4774 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] 4775 : NULL, newchild[c], cb, depth + 2); 4776 free(vname); 4777 } 4778 } 4779 4780 return (ret); 4781 } 4782 4783 static int 4784 refresh_iostat(zpool_handle_t *zhp, void *data) 4785 { 4786 iostat_cbdata_t *cb = data; 4787 boolean_t missing; 4788 4789 /* 4790 * If the pool has disappeared, remove it from the list and continue. 4791 */ 4792 if (zpool_refresh_stats(zhp, &missing) != 0) 4793 return (-1); 4794 4795 if (missing) 4796 pool_list_remove(cb->cb_list, zhp); 4797 4798 return (0); 4799 } 4800 4801 /* 4802 * Callback to print out the iostats for the given pool. 4803 */ 4804 static int 4805 print_iostat(zpool_handle_t *zhp, void *data) 4806 { 4807 iostat_cbdata_t *cb = data; 4808 nvlist_t *oldconfig, *newconfig; 4809 nvlist_t *oldnvroot, *newnvroot; 4810 int ret; 4811 4812 newconfig = zpool_get_config(zhp, &oldconfig); 4813 4814 if (cb->cb_iteration == 1) 4815 oldconfig = NULL; 4816 4817 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 4818 &newnvroot) == 0); 4819 4820 if (oldconfig == NULL) 4821 oldnvroot = NULL; 4822 else 4823 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 4824 &oldnvroot) == 0); 4825 4826 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, 4827 cb, 0); 4828 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && 4829 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) { 4830 print_iostat_separator(cb); 4831 if (cb->vcdl != NULL) { 4832 print_cmd_columns(cb->vcdl, 1); 4833 } 4834 printf("\n"); 4835 } 4836 4837 return (ret); 4838 } 4839 4840 static int 4841 get_columns(void) 4842 { 4843 struct winsize ws; 4844 int columns = 80; 4845 int error; 4846 4847 if (isatty(STDOUT_FILENO)) { 4848 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); 4849 if (error == 0) 4850 columns = ws.ws_col; 4851 } else { 4852 columns = 999; 4853 } 4854 4855 return (columns); 4856 } 4857 4858 /* 4859 * Return the required length of the pool/vdev name column. The minimum 4860 * allowed width and output formatting flags must be provided. 4861 */ 4862 static int 4863 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) 4864 { 4865 nvlist_t *config, *nvroot; 4866 int width = min_width; 4867 4868 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 4869 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4870 &nvroot) == 0); 4871 unsigned int poolname_len = strlen(zpool_get_name(zhp)); 4872 if (verbose == B_FALSE) { 4873 width = MAX(poolname_len, min_width); 4874 } else { 4875 width = MAX(poolname_len, 4876 max_width(zhp, nvroot, 0, min_width, flags)); 4877 } 4878 } 4879 4880 return (width); 4881 } 4882 4883 /* 4884 * Parse the input string, get the 'interval' and 'count' value if there is one. 4885 */ 4886 static void 4887 get_interval_count(int *argcp, char **argv, float *iv, 4888 unsigned long *cnt) 4889 { 4890 float interval = 0; 4891 unsigned long count = 0; 4892 int argc = *argcp; 4893 4894 /* 4895 * Determine if the last argument is an integer or a pool name 4896 */ 4897 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 4898 char *end; 4899 4900 errno = 0; 4901 interval = strtof(argv[argc - 1], &end); 4902 4903 if (*end == '\0' && errno == 0) { 4904 if (interval == 0) { 4905 (void) fprintf(stderr, gettext("interval " 4906 "cannot be zero\n")); 4907 usage(B_FALSE); 4908 } 4909 /* 4910 * Ignore the last parameter 4911 */ 4912 argc--; 4913 } else { 4914 /* 4915 * If this is not a valid number, just plow on. The 4916 * user will get a more informative error message later 4917 * on. 4918 */ 4919 interval = 0; 4920 } 4921 } 4922 4923 /* 4924 * If the last argument is also an integer, then we have both a count 4925 * and an interval. 4926 */ 4927 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 4928 char *end; 4929 4930 errno = 0; 4931 count = interval; 4932 interval = strtof(argv[argc - 1], &end); 4933 4934 if (*end == '\0' && errno == 0) { 4935 if (interval == 0) { 4936 (void) fprintf(stderr, gettext("interval " 4937 "cannot be zero\n")); 4938 usage(B_FALSE); 4939 } 4940 4941 /* 4942 * Ignore the last parameter 4943 */ 4944 argc--; 4945 } else { 4946 interval = 0; 4947 } 4948 } 4949 4950 *iv = interval; 4951 *cnt = count; 4952 *argcp = argc; 4953 } 4954 4955 static void 4956 get_timestamp_arg(char c) 4957 { 4958 if (c == 'u') 4959 timestamp_fmt = UDATE; 4960 else if (c == 'd') 4961 timestamp_fmt = DDATE; 4962 else 4963 usage(B_FALSE); 4964 } 4965 4966 /* 4967 * Return stat flags that are supported by all pools by both the module and 4968 * zpool iostat. "*data" should be initialized to all 0xFFs before running. 4969 * It will get ANDed down until only the flags that are supported on all pools 4970 * remain. 4971 */ 4972 static int 4973 get_stat_flags_cb(zpool_handle_t *zhp, void *data) 4974 { 4975 uint64_t *mask = data; 4976 nvlist_t *config, *nvroot, *nvx; 4977 uint64_t flags = 0; 4978 int i, j; 4979 4980 config = zpool_get_config(zhp, NULL); 4981 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4982 &nvroot) == 0); 4983 4984 /* Default stats are always supported, but for completeness.. */ 4985 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) 4986 flags |= IOS_DEFAULT_M; 4987 4988 /* Get our extended stats nvlist from the main list */ 4989 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, 4990 &nvx) != 0) { 4991 /* 4992 * No extended stats; they're probably running an older 4993 * module. No big deal, we support that too. 4994 */ 4995 goto end; 4996 } 4997 4998 /* For each extended stat, make sure all its nvpairs are supported */ 4999 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { 5000 if (!vsx_type_to_nvlist[j][0]) 5001 continue; 5002 5003 /* Start off by assuming the flag is supported, then check */ 5004 flags |= (1ULL << j); 5005 for (i = 0; vsx_type_to_nvlist[j][i]; i++) { 5006 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { 5007 /* flag isn't supported */ 5008 flags = flags & ~(1ULL << j); 5009 break; 5010 } 5011 } 5012 } 5013 end: 5014 *mask = *mask & flags; 5015 return (0); 5016 } 5017 5018 /* 5019 * Return a bitmask of stats that are supported on all pools by both the module 5020 * and zpool iostat. 5021 */ 5022 static uint64_t 5023 get_stat_flags(zpool_list_t *list) 5024 { 5025 uint64_t mask = -1; 5026 5027 /* 5028 * get_stat_flags_cb() will lop off bits from "mask" until only the 5029 * flags that are supported on all pools remain. 5030 */ 5031 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); 5032 return (mask); 5033 } 5034 5035 /* 5036 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise. 5037 */ 5038 static int 5039 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data) 5040 { 5041 iostat_cbdata_t *cb = cb_data; 5042 char *name = NULL; 5043 int ret = 0; 5044 5045 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags); 5046 5047 if (strcmp(name, cb->cb_vdev_names[0]) == 0) 5048 ret = 1; /* match */ 5049 free(name); 5050 5051 return (ret); 5052 } 5053 5054 /* 5055 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise. 5056 */ 5057 static int 5058 is_vdev(zpool_handle_t *zhp, void *cb_data) 5059 { 5060 return (for_each_vdev(zhp, is_vdev_cb, cb_data)); 5061 } 5062 5063 /* 5064 * Check if vdevs are in a pool 5065 * 5066 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise 5067 * return 0. If pool_name is NULL, then search all pools. 5068 */ 5069 static int 5070 are_vdevs_in_pool(int argc, char **argv, char *pool_name, 5071 iostat_cbdata_t *cb) 5072 { 5073 char **tmp_name; 5074 int ret = 0; 5075 int i; 5076 int pool_count = 0; 5077 5078 if ((argc == 0) || !*argv) 5079 return (0); 5080 5081 if (pool_name) 5082 pool_count = 1; 5083 5084 /* Temporarily hijack cb_vdev_names for a second... */ 5085 tmp_name = cb->cb_vdev_names; 5086 5087 /* Go though our list of prospective vdev names */ 5088 for (i = 0; i < argc; i++) { 5089 cb->cb_vdev_names = argv + i; 5090 5091 /* Is this name a vdev in our pools? */ 5092 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, 5093 B_FALSE, is_vdev, cb); 5094 if (!ret) { 5095 /* No match */ 5096 break; 5097 } 5098 } 5099 5100 cb->cb_vdev_names = tmp_name; 5101 5102 return (ret); 5103 } 5104 5105 static int 5106 is_pool_cb(zpool_handle_t *zhp, void *data) 5107 { 5108 char *name = data; 5109 if (strcmp(name, zpool_get_name(zhp)) == 0) 5110 return (1); 5111 5112 return (0); 5113 } 5114 5115 /* 5116 * Do we have a pool named *name? If so, return 1, otherwise 0. 5117 */ 5118 static int 5119 is_pool(char *name) 5120 { 5121 return (for_each_pool(0, NULL, B_TRUE, NULL, B_FALSE, is_pool_cb, 5122 name)); 5123 } 5124 5125 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ 5126 static int 5127 are_all_pools(int argc, char **argv) 5128 { 5129 if ((argc == 0) || !*argv) 5130 return (0); 5131 5132 while (--argc >= 0) 5133 if (!is_pool(argv[argc])) 5134 return (0); 5135 5136 return (1); 5137 } 5138 5139 /* 5140 * Helper function to print out vdev/pool names we can't resolve. Used for an 5141 * error message. 5142 */ 5143 static void 5144 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, 5145 iostat_cbdata_t *cb) 5146 { 5147 int i; 5148 char *name; 5149 char *str; 5150 for (i = 0; i < argc; i++) { 5151 name = argv[i]; 5152 5153 if (is_pool(name)) 5154 str = gettext("pool"); 5155 else if (are_vdevs_in_pool(1, &name, pool_name, cb)) 5156 str = gettext("vdev in this pool"); 5157 else if (are_vdevs_in_pool(1, &name, NULL, cb)) 5158 str = gettext("vdev in another pool"); 5159 else 5160 str = gettext("unknown"); 5161 5162 fprintf(stderr, "\t%s (%s)\n", name, str); 5163 } 5164 } 5165 5166 /* 5167 * Same as get_interval_count(), but with additional checks to not misinterpret 5168 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in 5169 * cb.cb_name_flags. 5170 */ 5171 static void 5172 get_interval_count_filter_guids(int *argc, char **argv, float *interval, 5173 unsigned long *count, iostat_cbdata_t *cb) 5174 { 5175 char **tmpargv = argv; 5176 int argc_for_interval = 0; 5177 5178 /* Is the last arg an interval value? Or a guid? */ 5179 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) { 5180 /* 5181 * The last arg is not a guid, so it's probably an 5182 * interval value. 5183 */ 5184 argc_for_interval++; 5185 5186 if (*argc >= 2 && 5187 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) { 5188 /* 5189 * The 2nd to last arg is not a guid, so it's probably 5190 * an interval value. 5191 */ 5192 argc_for_interval++; 5193 } 5194 } 5195 5196 /* Point to our list of possible intervals */ 5197 tmpargv = &argv[*argc - argc_for_interval]; 5198 5199 *argc = *argc - argc_for_interval; 5200 get_interval_count(&argc_for_interval, tmpargv, 5201 interval, count); 5202 } 5203 5204 /* 5205 * Floating point sleep(). Allows you to pass in a floating point value for 5206 * seconds. 5207 */ 5208 static void 5209 fsleep(float sec) 5210 { 5211 struct timespec req; 5212 req.tv_sec = floor(sec); 5213 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC; 5214 nanosleep(&req, NULL); 5215 } 5216 5217 /* 5218 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or 5219 * if we were unable to determine its size. 5220 */ 5221 static int 5222 terminal_height(void) 5223 { 5224 struct winsize win; 5225 5226 if (isatty(STDOUT_FILENO) == 0) 5227 return (-1); 5228 5229 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) 5230 return (win.ws_row); 5231 5232 return (-1); 5233 } 5234 5235 /* 5236 * Run one of the zpool status/iostat -c scripts with the help (-h) option and 5237 * print the result. 5238 * 5239 * name: Short name of the script ('iostat'). 5240 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); 5241 */ 5242 static void 5243 print_zpool_script_help(char *name, char *path) 5244 { 5245 char *argv[] = {path, "-h", NULL}; 5246 char **lines = NULL; 5247 int lines_cnt = 0; 5248 int rc; 5249 5250 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, 5251 &lines_cnt); 5252 if (rc != 0 || lines == NULL || lines_cnt <= 0) { 5253 if (lines != NULL) 5254 libzfs_free_str_array(lines, lines_cnt); 5255 return; 5256 } 5257 5258 for (int i = 0; i < lines_cnt; i++) 5259 if (!is_blank_str(lines[i])) 5260 printf(" %-14s %s\n", name, lines[i]); 5261 5262 libzfs_free_str_array(lines, lines_cnt); 5263 } 5264 5265 /* 5266 * Go though the zpool status/iostat -c scripts in the user's path, run their 5267 * help option (-h), and print out the results. 5268 */ 5269 static void 5270 print_zpool_dir_scripts(char *dirpath) 5271 { 5272 DIR *dir; 5273 struct dirent *ent; 5274 char fullpath[MAXPATHLEN]; 5275 struct stat dir_stat; 5276 5277 if ((dir = opendir(dirpath)) != NULL) { 5278 /* print all the files and directories within directory */ 5279 while ((ent = readdir(dir)) != NULL) { 5280 sprintf(fullpath, "%s/%s", dirpath, ent->d_name); 5281 5282 /* Print the scripts */ 5283 if (stat(fullpath, &dir_stat) == 0) 5284 if (dir_stat.st_mode & S_IXUSR && 5285 S_ISREG(dir_stat.st_mode)) 5286 print_zpool_script_help(ent->d_name, 5287 fullpath); 5288 } 5289 closedir(dir); 5290 } 5291 } 5292 5293 /* 5294 * Print out help text for all zpool status/iostat -c scripts. 5295 */ 5296 static void 5297 print_zpool_script_list(char *subcommand) 5298 { 5299 char *dir, *sp; 5300 5301 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); 5302 5303 sp = zpool_get_cmd_search_path(); 5304 if (sp == NULL) 5305 return; 5306 5307 dir = strtok(sp, ":"); 5308 while (dir != NULL) { 5309 print_zpool_dir_scripts(dir); 5310 dir = strtok(NULL, ":"); 5311 } 5312 5313 free(sp); 5314 } 5315 5316 /* 5317 * Set the minimum pool/vdev name column width. The width must be at least 10, 5318 * but may be as large as the column width - 42 so it still fits on one line. 5319 * NOTE: 42 is the width of the default capacity/operations/bandwidth output 5320 */ 5321 static int 5322 get_namewidth_iostat(zpool_handle_t *zhp, void *data) 5323 { 5324 iostat_cbdata_t *cb = data; 5325 int width, available_width; 5326 5327 /* 5328 * get_namewidth() returns the maximum width of any name in that column 5329 * for any pool/vdev/device line that will be output. 5330 */ 5331 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, 5332 cb->cb_verbose); 5333 5334 /* 5335 * The width we are calculating is the width of the header and also the 5336 * padding width for names that are less than maximum width. The stats 5337 * take up 42 characters, so the width available for names is: 5338 */ 5339 available_width = get_columns() - 42; 5340 5341 /* 5342 * If the maximum width fits on a screen, then great! Make everything 5343 * line up by justifying all lines to the same width. If that max 5344 * width is larger than what's available, the name plus stats won't fit 5345 * on one line, and justifying to that width would cause every line to 5346 * wrap on the screen. We only want lines with long names to wrap. 5347 * Limit the padding to what won't wrap. 5348 */ 5349 if (width > available_width) 5350 width = available_width; 5351 5352 /* 5353 * And regardless of whatever the screen width is (get_columns can 5354 * return 0 if the width is not known or less than 42 for a narrow 5355 * terminal) have the width be a minimum of 10. 5356 */ 5357 if (width < 10) 5358 width = 10; 5359 5360 /* Save the calculated width */ 5361 cb->cb_namewidth = width; 5362 5363 return (0); 5364 } 5365 5366 /* 5367 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] 5368 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] 5369 * [interval [count]] 5370 * 5371 * -c CMD For each vdev, run command CMD 5372 * -g Display guid for individual vdev name. 5373 * -L Follow links when resolving vdev path name. 5374 * -P Display full path for vdev name. 5375 * -v Display statistics for individual vdevs 5376 * -h Display help 5377 * -p Display values in parsable (exact) format. 5378 * -H Scripted mode. Don't display headers, and separate properties 5379 * by a single tab. 5380 * -l Display average latency 5381 * -q Display queue depths 5382 * -w Display latency histograms 5383 * -r Display request size histogram 5384 * -T Display a timestamp in date(1) or Unix format 5385 * -n Only print headers once 5386 * 5387 * This command can be tricky because we want to be able to deal with pool 5388 * creation/destruction as well as vdev configuration changes. The bulk of this 5389 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 5390 * on pool_list_update() to detect the addition of new pools. Configuration 5391 * changes are all handled within libzfs. 5392 */ 5393 int 5394 zpool_do_iostat(int argc, char **argv) 5395 { 5396 int c; 5397 int ret; 5398 int npools; 5399 float interval = 0; 5400 unsigned long count = 0; 5401 int winheight = 24; 5402 zpool_list_t *list; 5403 boolean_t verbose = B_FALSE; 5404 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; 5405 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; 5406 boolean_t omit_since_boot = B_FALSE; 5407 boolean_t guid = B_FALSE; 5408 boolean_t follow_links = B_FALSE; 5409 boolean_t full_name = B_FALSE; 5410 boolean_t headers_once = B_FALSE; 5411 iostat_cbdata_t cb = { 0 }; 5412 char *cmd = NULL; 5413 5414 /* Used for printing error message */ 5415 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', 5416 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; 5417 5418 uint64_t unsupported_flags; 5419 5420 /* check options */ 5421 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { 5422 switch (c) { 5423 case 'c': 5424 if (cmd != NULL) { 5425 fprintf(stderr, 5426 gettext("Can't set -c flag twice\n")); 5427 exit(1); 5428 } 5429 5430 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 5431 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 5432 fprintf(stderr, gettext( 5433 "Can't run -c, disabled by " 5434 "ZPOOL_SCRIPTS_ENABLED.\n")); 5435 exit(1); 5436 } 5437 5438 if ((getuid() <= 0 || geteuid() <= 0) && 5439 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 5440 fprintf(stderr, gettext( 5441 "Can't run -c with root privileges " 5442 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 5443 exit(1); 5444 } 5445 cmd = optarg; 5446 verbose = B_TRUE; 5447 break; 5448 case 'g': 5449 guid = B_TRUE; 5450 break; 5451 case 'L': 5452 follow_links = B_TRUE; 5453 break; 5454 case 'P': 5455 full_name = B_TRUE; 5456 break; 5457 case 'T': 5458 get_timestamp_arg(*optarg); 5459 break; 5460 case 'v': 5461 verbose = B_TRUE; 5462 break; 5463 case 'p': 5464 parsable = B_TRUE; 5465 break; 5466 case 'l': 5467 latency = B_TRUE; 5468 break; 5469 case 'q': 5470 queues = B_TRUE; 5471 break; 5472 case 'H': 5473 scripted = B_TRUE; 5474 break; 5475 case 'w': 5476 l_histo = B_TRUE; 5477 break; 5478 case 'r': 5479 rq_histo = B_TRUE; 5480 break; 5481 case 'y': 5482 omit_since_boot = B_TRUE; 5483 break; 5484 case 'n': 5485 headers_once = B_TRUE; 5486 break; 5487 case 'h': 5488 usage(B_FALSE); 5489 break; 5490 case '?': 5491 if (optopt == 'c') { 5492 print_zpool_script_list("iostat"); 5493 exit(0); 5494 } else { 5495 fprintf(stderr, 5496 gettext("invalid option '%c'\n"), optopt); 5497 } 5498 usage(B_FALSE); 5499 } 5500 } 5501 5502 argc -= optind; 5503 argv += optind; 5504 5505 cb.cb_literal = parsable; 5506 cb.cb_scripted = scripted; 5507 5508 if (guid) 5509 cb.cb_name_flags |= VDEV_NAME_GUID; 5510 if (follow_links) 5511 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 5512 if (full_name) 5513 cb.cb_name_flags |= VDEV_NAME_PATH; 5514 cb.cb_iteration = 0; 5515 cb.cb_namewidth = 0; 5516 cb.cb_verbose = verbose; 5517 5518 /* Get our interval and count values (if any) */ 5519 if (guid) { 5520 get_interval_count_filter_guids(&argc, argv, &interval, 5521 &count, &cb); 5522 } else { 5523 get_interval_count(&argc, argv, &interval, &count); 5524 } 5525 5526 if (argc == 0) { 5527 /* No args, so just print the defaults. */ 5528 } else if (are_all_pools(argc, argv)) { 5529 /* All the args are pool names */ 5530 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) { 5531 /* All the args are vdevs */ 5532 cb.cb_vdev_names = argv; 5533 cb.cb_vdev_names_count = argc; 5534 argc = 0; /* No pools to process */ 5535 } else if (are_all_pools(1, argv)) { 5536 /* The first arg is a pool name */ 5537 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) { 5538 /* ...and the rest are vdev names */ 5539 cb.cb_vdev_names = argv + 1; 5540 cb.cb_vdev_names_count = argc - 1; 5541 argc = 1; /* One pool to process */ 5542 } else { 5543 fprintf(stderr, gettext("Expected either a list of ")); 5544 fprintf(stderr, gettext("pools, or list of vdevs in")); 5545 fprintf(stderr, " \"%s\", ", argv[0]); 5546 fprintf(stderr, gettext("but got:\n")); 5547 error_list_unresolved_vdevs(argc - 1, argv + 1, 5548 argv[0], &cb); 5549 fprintf(stderr, "\n"); 5550 usage(B_FALSE); 5551 return (1); 5552 } 5553 } else { 5554 /* 5555 * The args don't make sense. The first arg isn't a pool name, 5556 * nor are all the args vdevs. 5557 */ 5558 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); 5559 fprintf(stderr, "\n"); 5560 return (1); 5561 } 5562 5563 if (cb.cb_vdev_names_count != 0) { 5564 /* 5565 * If user specified vdevs, it implies verbose. 5566 */ 5567 cb.cb_verbose = B_TRUE; 5568 } 5569 5570 /* 5571 * Construct the list of all interesting pools. 5572 */ 5573 ret = 0; 5574 if ((list = pool_list_get(argc, argv, NULL, parsable, &ret)) == NULL) 5575 return (1); 5576 5577 if (pool_list_count(list) == 0 && argc != 0) { 5578 pool_list_free(list); 5579 return (1); 5580 } 5581 5582 if (pool_list_count(list) == 0 && interval == 0) { 5583 pool_list_free(list); 5584 (void) fprintf(stderr, gettext("no pools available\n")); 5585 return (1); 5586 } 5587 5588 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { 5589 pool_list_free(list); 5590 (void) fprintf(stderr, 5591 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); 5592 usage(B_FALSE); 5593 return (1); 5594 } 5595 5596 if (l_histo && rq_histo) { 5597 pool_list_free(list); 5598 (void) fprintf(stderr, 5599 gettext("Only one of [-r|-w] can be passed at a time\n")); 5600 usage(B_FALSE); 5601 return (1); 5602 } 5603 5604 /* 5605 * Enter the main iostat loop. 5606 */ 5607 cb.cb_list = list; 5608 5609 if (l_histo) { 5610 /* 5611 * Histograms tables look out of place when you try to display 5612 * them with the other stats, so make a rule that you can only 5613 * print histograms by themselves. 5614 */ 5615 cb.cb_flags = IOS_L_HISTO_M; 5616 } else if (rq_histo) { 5617 cb.cb_flags = IOS_RQ_HISTO_M; 5618 } else { 5619 cb.cb_flags = IOS_DEFAULT_M; 5620 if (latency) 5621 cb.cb_flags |= IOS_LATENCY_M; 5622 if (queues) 5623 cb.cb_flags |= IOS_QUEUES_M; 5624 } 5625 5626 /* 5627 * See if the module supports all the stats we want to display. 5628 */ 5629 unsupported_flags = cb.cb_flags & ~get_stat_flags(list); 5630 if (unsupported_flags) { 5631 uint64_t f; 5632 int idx; 5633 fprintf(stderr, 5634 gettext("The loaded zfs module doesn't support:")); 5635 5636 /* for each bit set in unsupported_flags */ 5637 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { 5638 idx = lowbit64(f) - 1; 5639 fprintf(stderr, " -%c", flag_to_arg[idx]); 5640 } 5641 5642 fprintf(stderr, ". Try running a newer module.\n"); 5643 pool_list_free(list); 5644 5645 return (1); 5646 } 5647 5648 for (;;) { 5649 if ((npools = pool_list_count(list)) == 0) 5650 (void) fprintf(stderr, gettext("no pools available\n")); 5651 else { 5652 /* 5653 * If this is the first iteration and -y was supplied 5654 * we skip any printing. 5655 */ 5656 boolean_t skip = (omit_since_boot && 5657 cb.cb_iteration == 0); 5658 5659 /* 5660 * Refresh all statistics. This is done as an 5661 * explicit step before calculating the maximum name 5662 * width, so that any * configuration changes are 5663 * properly accounted for. 5664 */ 5665 (void) pool_list_iter(list, B_FALSE, refresh_iostat, 5666 &cb); 5667 5668 /* 5669 * Iterate over all pools to determine the maximum width 5670 * for the pool / device name column across all pools. 5671 */ 5672 cb.cb_namewidth = 0; 5673 (void) pool_list_iter(list, B_FALSE, 5674 get_namewidth_iostat, &cb); 5675 5676 if (timestamp_fmt != NODATE) 5677 print_timestamp(timestamp_fmt); 5678 5679 if (cmd != NULL && cb.cb_verbose && 5680 !(cb.cb_flags & IOS_ANYHISTO_M)) { 5681 cb.vcdl = all_pools_for_each_vdev_run(argc, 5682 argv, cmd, g_zfs, cb.cb_vdev_names, 5683 cb.cb_vdev_names_count, cb.cb_name_flags); 5684 } else { 5685 cb.vcdl = NULL; 5686 } 5687 5688 5689 /* 5690 * Check terminal size so we can print headers 5691 * even when terminal window has its height 5692 * changed. 5693 */ 5694 winheight = terminal_height(); 5695 /* 5696 * Are we connected to TTY? If not, headers_once 5697 * should be true, to avoid breaking scripts. 5698 */ 5699 if (winheight < 0) 5700 headers_once = B_TRUE; 5701 5702 /* 5703 * If it's the first time and we're not skipping it, 5704 * or either skip or verbose mode, print the header. 5705 * 5706 * The histogram code explicitly prints its header on 5707 * every vdev, so skip this for histograms. 5708 */ 5709 if (((++cb.cb_iteration == 1 && !skip) || 5710 (skip != verbose) || 5711 (!headers_once && 5712 (cb.cb_iteration % winheight) == 0)) && 5713 (!(cb.cb_flags & IOS_ANYHISTO_M)) && 5714 !cb.cb_scripted) 5715 print_iostat_header(&cb); 5716 5717 if (skip) { 5718 (void) fsleep(interval); 5719 continue; 5720 } 5721 5722 pool_list_iter(list, B_FALSE, print_iostat, &cb); 5723 5724 /* 5725 * If there's more than one pool, and we're not in 5726 * verbose mode (which prints a separator for us), 5727 * then print a separator. 5728 * 5729 * In addition, if we're printing specific vdevs then 5730 * we also want an ending separator. 5731 */ 5732 if (((npools > 1 && !verbose && 5733 !(cb.cb_flags & IOS_ANYHISTO_M)) || 5734 (!(cb.cb_flags & IOS_ANYHISTO_M) && 5735 cb.cb_vdev_names_count)) && 5736 !cb.cb_scripted) { 5737 print_iostat_separator(&cb); 5738 if (cb.vcdl != NULL) 5739 print_cmd_columns(cb.vcdl, 1); 5740 printf("\n"); 5741 } 5742 5743 if (cb.vcdl != NULL) 5744 free_vdev_cmd_data_list(cb.vcdl); 5745 5746 } 5747 5748 /* 5749 * Flush the output so that redirection to a file isn't buffered 5750 * indefinitely. 5751 */ 5752 (void) fflush(stdout); 5753 5754 if (interval == 0) 5755 break; 5756 5757 if (count != 0 && --count == 0) 5758 break; 5759 5760 (void) fsleep(interval); 5761 } 5762 5763 pool_list_free(list); 5764 5765 return (ret); 5766 } 5767 5768 typedef struct list_cbdata { 5769 boolean_t cb_verbose; 5770 int cb_name_flags; 5771 int cb_namewidth; 5772 boolean_t cb_scripted; 5773 zprop_list_t *cb_proplist; 5774 boolean_t cb_literal; 5775 } list_cbdata_t; 5776 5777 5778 /* 5779 * Given a list of columns to display, output appropriate headers for each one. 5780 */ 5781 static void 5782 print_header(list_cbdata_t *cb) 5783 { 5784 zprop_list_t *pl = cb->cb_proplist; 5785 char headerbuf[ZPOOL_MAXPROPLEN]; 5786 const char *header; 5787 boolean_t first = B_TRUE; 5788 boolean_t right_justify; 5789 size_t width = 0; 5790 5791 for (; pl != NULL; pl = pl->pl_next) { 5792 width = pl->pl_width; 5793 if (first && cb->cb_verbose) { 5794 /* 5795 * Reset the width to accommodate the verbose listing 5796 * of devices. 5797 */ 5798 width = cb->cb_namewidth; 5799 } 5800 5801 if (!first) 5802 (void) printf(" "); 5803 else 5804 first = B_FALSE; 5805 5806 right_justify = B_FALSE; 5807 if (pl->pl_prop != ZPROP_INVAL) { 5808 header = zpool_prop_column_name(pl->pl_prop); 5809 right_justify = zpool_prop_align_right(pl->pl_prop); 5810 } else { 5811 int i; 5812 5813 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 5814 headerbuf[i] = toupper(pl->pl_user_prop[i]); 5815 headerbuf[i] = '\0'; 5816 header = headerbuf; 5817 } 5818 5819 if (pl->pl_next == NULL && !right_justify) 5820 (void) printf("%s", header); 5821 else if (right_justify) 5822 (void) printf("%*s", (int)width, header); 5823 else 5824 (void) printf("%-*s", (int)width, header); 5825 } 5826 5827 (void) printf("\n"); 5828 } 5829 5830 /* 5831 * Given a pool and a list of properties, print out all the properties according 5832 * to the described layout. Used by zpool_do_list(). 5833 */ 5834 static void 5835 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 5836 { 5837 zprop_list_t *pl = cb->cb_proplist; 5838 boolean_t first = B_TRUE; 5839 char property[ZPOOL_MAXPROPLEN]; 5840 char *propstr; 5841 boolean_t right_justify; 5842 size_t width; 5843 5844 for (; pl != NULL; pl = pl->pl_next) { 5845 5846 width = pl->pl_width; 5847 if (first && cb->cb_verbose) { 5848 /* 5849 * Reset the width to accommodate the verbose listing 5850 * of devices. 5851 */ 5852 width = cb->cb_namewidth; 5853 } 5854 5855 if (!first) { 5856 if (cb->cb_scripted) 5857 (void) printf("\t"); 5858 else 5859 (void) printf(" "); 5860 } else { 5861 first = B_FALSE; 5862 } 5863 5864 right_justify = B_FALSE; 5865 if (pl->pl_prop != ZPROP_INVAL) { 5866 if (zpool_get_prop(zhp, pl->pl_prop, property, 5867 sizeof (property), NULL, cb->cb_literal) != 0) 5868 propstr = "-"; 5869 else 5870 propstr = property; 5871 5872 right_justify = zpool_prop_align_right(pl->pl_prop); 5873 } else if ((zpool_prop_feature(pl->pl_user_prop) || 5874 zpool_prop_unsupported(pl->pl_user_prop)) && 5875 zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 5876 sizeof (property)) == 0) { 5877 propstr = property; 5878 } else { 5879 propstr = "-"; 5880 } 5881 5882 5883 /* 5884 * If this is being called in scripted mode, or if this is the 5885 * last column and it is left-justified, don't include a width 5886 * format specifier. 5887 */ 5888 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 5889 (void) printf("%s", propstr); 5890 else if (right_justify) 5891 (void) printf("%*s", (int)width, propstr); 5892 else 5893 (void) printf("%-*s", (int)width, propstr); 5894 } 5895 5896 (void) printf("\n"); 5897 } 5898 5899 static void 5900 print_one_column(zpool_prop_t prop, uint64_t value, const char *str, 5901 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format) 5902 { 5903 char propval[64]; 5904 boolean_t fixed; 5905 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 5906 5907 switch (prop) { 5908 case ZPOOL_PROP_EXPANDSZ: 5909 case ZPOOL_PROP_CHECKPOINT: 5910 case ZPOOL_PROP_DEDUPRATIO: 5911 if (value == 0) 5912 (void) strlcpy(propval, "-", sizeof (propval)); 5913 else 5914 zfs_nicenum_format(value, propval, sizeof (propval), 5915 format); 5916 break; 5917 case ZPOOL_PROP_FRAGMENTATION: 5918 if (value == ZFS_FRAG_INVALID) { 5919 (void) strlcpy(propval, "-", sizeof (propval)); 5920 } else if (format == ZFS_NICENUM_RAW) { 5921 (void) snprintf(propval, sizeof (propval), "%llu", 5922 (unsigned long long)value); 5923 } else { 5924 (void) snprintf(propval, sizeof (propval), "%llu%%", 5925 (unsigned long long)value); 5926 } 5927 break; 5928 case ZPOOL_PROP_CAPACITY: 5929 /* capacity value is in parts-per-10,000 (aka permyriad) */ 5930 if (format == ZFS_NICENUM_RAW) 5931 (void) snprintf(propval, sizeof (propval), "%llu", 5932 (unsigned long long)value / 100); 5933 else 5934 (void) snprintf(propval, sizeof (propval), 5935 value < 1000 ? "%1.2f%%" : value < 10000 ? 5936 "%2.1f%%" : "%3.0f%%", value / 100.0); 5937 break; 5938 case ZPOOL_PROP_HEALTH: 5939 width = 8; 5940 snprintf(propval, sizeof (propval), "%-*s", (int)width, str); 5941 break; 5942 default: 5943 zfs_nicenum_format(value, propval, sizeof (propval), format); 5944 } 5945 5946 if (!valid) 5947 (void) strlcpy(propval, "-", sizeof (propval)); 5948 5949 if (scripted) 5950 (void) printf("\t%s", propval); 5951 else 5952 (void) printf(" %*s", (int)width, propval); 5953 } 5954 5955 /* 5956 * print static default line per vdev 5957 * not compatible with '-o' <proplist> option 5958 */ 5959 static void 5960 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 5961 list_cbdata_t *cb, int depth, boolean_t isspare) 5962 { 5963 nvlist_t **child; 5964 vdev_stat_t *vs; 5965 uint_t c, children; 5966 char *vname; 5967 boolean_t scripted = cb->cb_scripted; 5968 uint64_t islog = B_FALSE; 5969 char *dashes = "%-*s - - - - " 5970 "- - - - -\n"; 5971 5972 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 5973 (uint64_t **)&vs, &c) == 0); 5974 5975 if (name != NULL) { 5976 boolean_t toplevel = (vs->vs_space != 0); 5977 uint64_t cap; 5978 enum zfs_nicenum_format format; 5979 const char *state; 5980 5981 if (cb->cb_literal) 5982 format = ZFS_NICENUM_RAW; 5983 else 5984 format = ZFS_NICENUM_1024; 5985 5986 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 5987 return; 5988 5989 if (scripted) 5990 (void) printf("\t%s", name); 5991 else if (strlen(name) + depth > cb->cb_namewidth) 5992 (void) printf("%*s%s", depth, "", name); 5993 else 5994 (void) printf("%*s%s%*s", depth, "", name, 5995 (int)(cb->cb_namewidth - strlen(name) - depth), ""); 5996 5997 /* 5998 * Print the properties for the individual vdevs. Some 5999 * properties are only applicable to toplevel vdevs. The 6000 * 'toplevel' boolean value is passed to the print_one_column() 6001 * to indicate that the value is valid. 6002 */ 6003 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted, 6004 toplevel, format); 6005 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, 6006 scripted, toplevel, format); 6007 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, 6008 NULL, scripted, toplevel, format); 6009 print_one_column(ZPOOL_PROP_CHECKPOINT, 6010 vs->vs_checkpoint_space, NULL, scripted, toplevel, format); 6011 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, 6012 scripted, B_TRUE, format); 6013 print_one_column(ZPOOL_PROP_FRAGMENTATION, 6014 vs->vs_fragmentation, NULL, scripted, 6015 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), 6016 format); 6017 cap = (vs->vs_space == 0) ? 0 : 6018 (vs->vs_alloc * 10000 / vs->vs_space); 6019 print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL, 6020 scripted, toplevel, format); 6021 print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL, 6022 scripted, toplevel, format); 6023 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 6024 if (isspare) { 6025 if (vs->vs_aux == VDEV_AUX_SPARED) 6026 state = "INUSE"; 6027 else if (vs->vs_state == VDEV_STATE_HEALTHY) 6028 state = "AVAIL"; 6029 } 6030 print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted, 6031 B_TRUE, format); 6032 (void) printf("\n"); 6033 } 6034 6035 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 6036 &child, &children) != 0) 6037 return; 6038 6039 /* list the normal vdevs first */ 6040 for (c = 0; c < children; c++) { 6041 uint64_t ishole = B_FALSE; 6042 6043 if (nvlist_lookup_uint64(child[c], 6044 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 6045 continue; 6046 6047 if (nvlist_lookup_uint64(child[c], 6048 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) 6049 continue; 6050 6051 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 6052 continue; 6053 6054 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6055 cb->cb_name_flags); 6056 print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE); 6057 free(vname); 6058 } 6059 6060 /* list the classes: 'logs', 'dedup', and 'special' */ 6061 for (uint_t n = 0; n < 3; n++) { 6062 boolean_t printed = B_FALSE; 6063 6064 for (c = 0; c < children; c++) { 6065 char *bias = NULL; 6066 char *type = NULL; 6067 6068 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 6069 &islog) == 0 && islog) { 6070 bias = VDEV_ALLOC_CLASS_LOGS; 6071 } else { 6072 (void) nvlist_lookup_string(child[c], 6073 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 6074 (void) nvlist_lookup_string(child[c], 6075 ZPOOL_CONFIG_TYPE, &type); 6076 } 6077 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 6078 continue; 6079 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 6080 continue; 6081 6082 if (!printed) { 6083 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6084 (void) printf(dashes, cb->cb_namewidth, 6085 class_name[n]); 6086 printed = B_TRUE; 6087 } 6088 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6089 cb->cb_name_flags); 6090 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6091 B_FALSE); 6092 free(vname); 6093 } 6094 } 6095 6096 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 6097 &child, &children) == 0 && children > 0) { 6098 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6099 (void) printf(dashes, cb->cb_namewidth, "cache"); 6100 for (c = 0; c < children; c++) { 6101 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6102 cb->cb_name_flags); 6103 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6104 B_FALSE); 6105 free(vname); 6106 } 6107 } 6108 6109 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, 6110 &children) == 0 && children > 0) { 6111 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6112 (void) printf(dashes, cb->cb_namewidth, "spare"); 6113 for (c = 0; c < children; c++) { 6114 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6115 cb->cb_name_flags); 6116 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6117 B_TRUE); 6118 free(vname); 6119 } 6120 } 6121 } 6122 6123 /* 6124 * Generic callback function to list a pool. 6125 */ 6126 static int 6127 list_callback(zpool_handle_t *zhp, void *data) 6128 { 6129 list_cbdata_t *cbp = data; 6130 6131 print_pool(zhp, cbp); 6132 6133 if (cbp->cb_verbose) { 6134 nvlist_t *config, *nvroot; 6135 6136 config = zpool_get_config(zhp, NULL); 6137 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 6138 &nvroot) == 0); 6139 print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE); 6140 } 6141 6142 return (0); 6143 } 6144 6145 /* 6146 * Set the minimum pool/vdev name column width. The width must be at least 9, 6147 * but may be as large as needed. 6148 */ 6149 static int 6150 get_namewidth_list(zpool_handle_t *zhp, void *data) 6151 { 6152 list_cbdata_t *cb = data; 6153 int width; 6154 6155 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, 6156 cb->cb_verbose); 6157 6158 if (width < 9) 6159 width = 9; 6160 6161 cb->cb_namewidth = width; 6162 6163 return (0); 6164 } 6165 6166 /* 6167 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 6168 * 6169 * -g Display guid for individual vdev name. 6170 * -H Scripted mode. Don't display headers, and separate properties 6171 * by a single tab. 6172 * -L Follow links when resolving vdev path name. 6173 * -o List of properties to display. Defaults to 6174 * "name,size,allocated,free,expandsize,fragmentation,capacity," 6175 * "dedupratio,health,altroot" 6176 * -p Display values in parsable (exact) format. 6177 * -P Display full path for vdev name. 6178 * -T Display a timestamp in date(1) or Unix format 6179 * 6180 * List all pools in the system, whether or not they're healthy. Output space 6181 * statistics for each one, as well as health status summary. 6182 */ 6183 int 6184 zpool_do_list(int argc, char **argv) 6185 { 6186 int c; 6187 int ret = 0; 6188 list_cbdata_t cb = { 0 }; 6189 static char default_props[] = 6190 "name,size,allocated,free,checkpoint,expandsize,fragmentation," 6191 "capacity,dedupratio,health,altroot"; 6192 char *props = default_props; 6193 float interval = 0; 6194 unsigned long count = 0; 6195 zpool_list_t *list; 6196 boolean_t first = B_TRUE; 6197 6198 /* check options */ 6199 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) { 6200 switch (c) { 6201 case 'g': 6202 cb.cb_name_flags |= VDEV_NAME_GUID; 6203 break; 6204 case 'H': 6205 cb.cb_scripted = B_TRUE; 6206 break; 6207 case 'L': 6208 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 6209 break; 6210 case 'o': 6211 props = optarg; 6212 break; 6213 case 'P': 6214 cb.cb_name_flags |= VDEV_NAME_PATH; 6215 break; 6216 case 'p': 6217 cb.cb_literal = B_TRUE; 6218 break; 6219 case 'T': 6220 get_timestamp_arg(*optarg); 6221 break; 6222 case 'v': 6223 cb.cb_verbose = B_TRUE; 6224 cb.cb_namewidth = 8; /* 8 until precalc is avail */ 6225 break; 6226 case ':': 6227 (void) fprintf(stderr, gettext("missing argument for " 6228 "'%c' option\n"), optopt); 6229 usage(B_FALSE); 6230 break; 6231 case '?': 6232 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6233 optopt); 6234 usage(B_FALSE); 6235 } 6236 } 6237 6238 argc -= optind; 6239 argv += optind; 6240 6241 get_interval_count(&argc, argv, &interval, &count); 6242 6243 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 6244 usage(B_FALSE); 6245 6246 for (;;) { 6247 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 6248 cb.cb_literal, &ret)) == NULL) 6249 return (1); 6250 6251 if (pool_list_count(list) == 0) 6252 break; 6253 6254 cb.cb_namewidth = 0; 6255 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); 6256 6257 if (timestamp_fmt != NODATE) 6258 print_timestamp(timestamp_fmt); 6259 6260 if (!cb.cb_scripted && (first || cb.cb_verbose)) { 6261 print_header(&cb); 6262 first = B_FALSE; 6263 } 6264 ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 6265 6266 if (interval == 0) 6267 break; 6268 6269 if (count != 0 && --count == 0) 6270 break; 6271 6272 pool_list_free(list); 6273 (void) fsleep(interval); 6274 } 6275 6276 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 6277 (void) printf(gettext("no pools available\n")); 6278 ret = 0; 6279 } 6280 6281 pool_list_free(list); 6282 zprop_free_list(cb.cb_proplist); 6283 return (ret); 6284 } 6285 6286 static int 6287 zpool_do_attach_or_replace(int argc, char **argv, int replacing) 6288 { 6289 boolean_t force = B_FALSE; 6290 boolean_t rebuild = B_FALSE; 6291 boolean_t wait = B_FALSE; 6292 int c; 6293 nvlist_t *nvroot; 6294 char *poolname, *old_disk, *new_disk; 6295 zpool_handle_t *zhp; 6296 nvlist_t *props = NULL; 6297 char *propval; 6298 int ret; 6299 6300 /* check options */ 6301 while ((c = getopt(argc, argv, "fo:sw")) != -1) { 6302 switch (c) { 6303 case 'f': 6304 force = B_TRUE; 6305 break; 6306 case 'o': 6307 if ((propval = strchr(optarg, '=')) == NULL) { 6308 (void) fprintf(stderr, gettext("missing " 6309 "'=' for -o option\n")); 6310 usage(B_FALSE); 6311 } 6312 *propval = '\0'; 6313 propval++; 6314 6315 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 6316 (add_prop_list(optarg, propval, &props, B_TRUE))) 6317 usage(B_FALSE); 6318 break; 6319 case 's': 6320 rebuild = B_TRUE; 6321 break; 6322 case 'w': 6323 wait = B_TRUE; 6324 break; 6325 case '?': 6326 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6327 optopt); 6328 usage(B_FALSE); 6329 } 6330 } 6331 6332 argc -= optind; 6333 argv += optind; 6334 6335 /* get pool name and check number of arguments */ 6336 if (argc < 1) { 6337 (void) fprintf(stderr, gettext("missing pool name argument\n")); 6338 usage(B_FALSE); 6339 } 6340 6341 poolname = argv[0]; 6342 6343 if (argc < 2) { 6344 (void) fprintf(stderr, 6345 gettext("missing <device> specification\n")); 6346 usage(B_FALSE); 6347 } 6348 6349 old_disk = argv[1]; 6350 6351 if (argc < 3) { 6352 if (!replacing) { 6353 (void) fprintf(stderr, 6354 gettext("missing <new_device> specification\n")); 6355 usage(B_FALSE); 6356 } 6357 new_disk = old_disk; 6358 argc -= 1; 6359 argv += 1; 6360 } else { 6361 new_disk = argv[2]; 6362 argc -= 2; 6363 argv += 2; 6364 } 6365 6366 if (argc > 1) { 6367 (void) fprintf(stderr, gettext("too many arguments\n")); 6368 usage(B_FALSE); 6369 } 6370 6371 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 6372 nvlist_free(props); 6373 return (1); 6374 } 6375 6376 if (zpool_get_config(zhp, NULL) == NULL) { 6377 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 6378 poolname); 6379 zpool_close(zhp); 6380 nvlist_free(props); 6381 return (1); 6382 } 6383 6384 /* unless manually specified use "ashift" pool property (if set) */ 6385 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 6386 int intval; 6387 zprop_source_t src; 6388 char strval[ZPOOL_MAXPROPLEN]; 6389 6390 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 6391 if (src != ZPROP_SRC_DEFAULT) { 6392 (void) sprintf(strval, "%" PRId32, intval); 6393 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 6394 &props, B_TRUE) == 0); 6395 } 6396 } 6397 6398 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, 6399 argc, argv); 6400 if (nvroot == NULL) { 6401 zpool_close(zhp); 6402 nvlist_free(props); 6403 return (1); 6404 } 6405 6406 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, 6407 rebuild); 6408 6409 if (ret == 0 && wait) 6410 ret = zpool_wait(zhp, 6411 replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER); 6412 6413 nvlist_free(props); 6414 nvlist_free(nvroot); 6415 zpool_close(zhp); 6416 6417 return (ret); 6418 } 6419 6420 /* 6421 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> 6422 * 6423 * -f Force attach, even if <new_device> appears to be in use. 6424 * -s Use sequential instead of healing reconstruction for resilver. 6425 * -o Set property=value. 6426 * -w Wait for replacing to complete before returning 6427 * 6428 * Replace <device> with <new_device>. 6429 */ 6430 /* ARGSUSED */ 6431 int 6432 zpool_do_replace(int argc, char **argv) 6433 { 6434 return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 6435 } 6436 6437 /* 6438 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device> 6439 * 6440 * -f Force attach, even if <new_device> appears to be in use. 6441 * -s Use sequential instead of healing reconstruction for resilver. 6442 * -o Set property=value. 6443 * -w Wait for resilvering to complete before returning 6444 * 6445 * Attach <new_device> to the mirror containing <device>. If <device> is not 6446 * part of a mirror, then <device> will be transformed into a mirror of 6447 * <device> and <new_device>. In either case, <new_device> will begin life 6448 * with a DTL of [0, now], and will immediately begin to resilver itself. 6449 */ 6450 int 6451 zpool_do_attach(int argc, char **argv) 6452 { 6453 return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 6454 } 6455 6456 /* 6457 * zpool detach [-f] <pool> <device> 6458 * 6459 * -f Force detach of <device>, even if DTLs argue against it 6460 * (not supported yet) 6461 * 6462 * Detach a device from a mirror. The operation will be refused if <device> 6463 * is the last device in the mirror, or if the DTLs indicate that this device 6464 * has the only valid copy of some data. 6465 */ 6466 /* ARGSUSED */ 6467 int 6468 zpool_do_detach(int argc, char **argv) 6469 { 6470 int c; 6471 char *poolname, *path; 6472 zpool_handle_t *zhp; 6473 int ret; 6474 6475 /* check options */ 6476 while ((c = getopt(argc, argv, "")) != -1) { 6477 switch (c) { 6478 case '?': 6479 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6480 optopt); 6481 usage(B_FALSE); 6482 } 6483 } 6484 6485 argc -= optind; 6486 argv += optind; 6487 6488 /* get pool name and check number of arguments */ 6489 if (argc < 1) { 6490 (void) fprintf(stderr, gettext("missing pool name argument\n")); 6491 usage(B_FALSE); 6492 } 6493 6494 if (argc < 2) { 6495 (void) fprintf(stderr, 6496 gettext("missing <device> specification\n")); 6497 usage(B_FALSE); 6498 } 6499 6500 poolname = argv[0]; 6501 path = argv[1]; 6502 6503 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6504 return (1); 6505 6506 ret = zpool_vdev_detach(zhp, path); 6507 6508 zpool_close(zhp); 6509 6510 return (ret); 6511 } 6512 6513 /* 6514 * zpool split [-gLnP] [-o prop=val] ... 6515 * [-o mntopt] ... 6516 * [-R altroot] <pool> <newpool> [<device> ...] 6517 * 6518 * -g Display guid for individual vdev name. 6519 * -L Follow links when resolving vdev path name. 6520 * -n Do not split the pool, but display the resulting layout if 6521 * it were to be split. 6522 * -o Set property=value, or set mount options. 6523 * -P Display full path for vdev name. 6524 * -R Mount the split-off pool under an alternate root. 6525 * -l Load encryption keys while importing. 6526 * 6527 * Splits the named pool and gives it the new pool name. Devices to be split 6528 * off may be listed, provided that no more than one device is specified 6529 * per top-level vdev mirror. The newly split pool is left in an exported 6530 * state unless -R is specified. 6531 * 6532 * Restrictions: the top-level of the pool pool must only be made up of 6533 * mirrors; all devices in the pool must be healthy; no device may be 6534 * undergoing a resilvering operation. 6535 */ 6536 int 6537 zpool_do_split(int argc, char **argv) 6538 { 6539 char *srcpool, *newpool, *propval; 6540 char *mntopts = NULL; 6541 splitflags_t flags; 6542 int c, ret = 0; 6543 boolean_t loadkeys = B_FALSE; 6544 zpool_handle_t *zhp; 6545 nvlist_t *config, *props = NULL; 6546 6547 flags.dryrun = B_FALSE; 6548 flags.import = B_FALSE; 6549 flags.name_flags = 0; 6550 6551 /* check options */ 6552 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { 6553 switch (c) { 6554 case 'g': 6555 flags.name_flags |= VDEV_NAME_GUID; 6556 break; 6557 case 'L': 6558 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; 6559 break; 6560 case 'R': 6561 flags.import = B_TRUE; 6562 if (add_prop_list( 6563 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 6564 &props, B_TRUE) != 0) { 6565 nvlist_free(props); 6566 usage(B_FALSE); 6567 } 6568 break; 6569 case 'l': 6570 loadkeys = B_TRUE; 6571 break; 6572 case 'n': 6573 flags.dryrun = B_TRUE; 6574 break; 6575 case 'o': 6576 if ((propval = strchr(optarg, '=')) != NULL) { 6577 *propval = '\0'; 6578 propval++; 6579 if (add_prop_list(optarg, propval, 6580 &props, B_TRUE) != 0) { 6581 nvlist_free(props); 6582 usage(B_FALSE); 6583 } 6584 } else { 6585 mntopts = optarg; 6586 } 6587 break; 6588 case 'P': 6589 flags.name_flags |= VDEV_NAME_PATH; 6590 break; 6591 case ':': 6592 (void) fprintf(stderr, gettext("missing argument for " 6593 "'%c' option\n"), optopt); 6594 usage(B_FALSE); 6595 break; 6596 case '?': 6597 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6598 optopt); 6599 usage(B_FALSE); 6600 break; 6601 } 6602 } 6603 6604 if (!flags.import && mntopts != NULL) { 6605 (void) fprintf(stderr, gettext("setting mntopts is only " 6606 "valid when importing the pool\n")); 6607 usage(B_FALSE); 6608 } 6609 6610 if (!flags.import && loadkeys) { 6611 (void) fprintf(stderr, gettext("loading keys is only " 6612 "valid when importing the pool\n")); 6613 usage(B_FALSE); 6614 } 6615 6616 argc -= optind; 6617 argv += optind; 6618 6619 if (argc < 1) { 6620 (void) fprintf(stderr, gettext("Missing pool name\n")); 6621 usage(B_FALSE); 6622 } 6623 if (argc < 2) { 6624 (void) fprintf(stderr, gettext("Missing new pool name\n")); 6625 usage(B_FALSE); 6626 } 6627 6628 srcpool = argv[0]; 6629 newpool = argv[1]; 6630 6631 argc -= 2; 6632 argv += 2; 6633 6634 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { 6635 nvlist_free(props); 6636 return (1); 6637 } 6638 6639 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 6640 if (config == NULL) { 6641 ret = 1; 6642 } else { 6643 if (flags.dryrun) { 6644 (void) printf(gettext("would create '%s' with the " 6645 "following layout:\n\n"), newpool); 6646 print_vdev_tree(NULL, newpool, config, 0, "", 6647 flags.name_flags); 6648 print_vdev_tree(NULL, "dedup", config, 0, 6649 VDEV_ALLOC_BIAS_DEDUP, 0); 6650 print_vdev_tree(NULL, "special", config, 0, 6651 VDEV_ALLOC_BIAS_SPECIAL, 0); 6652 } 6653 } 6654 6655 zpool_close(zhp); 6656 6657 if (ret != 0 || flags.dryrun || !flags.import) { 6658 nvlist_free(config); 6659 nvlist_free(props); 6660 return (ret); 6661 } 6662 6663 /* 6664 * The split was successful. Now we need to open the new 6665 * pool and import it. 6666 */ 6667 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { 6668 nvlist_free(config); 6669 nvlist_free(props); 6670 return (1); 6671 } 6672 6673 if (loadkeys) { 6674 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); 6675 if (ret != 0) 6676 ret = 1; 6677 } 6678 6679 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 6680 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 6681 ret = 1; 6682 (void) fprintf(stderr, gettext("Split was successful, but " 6683 "the datasets could not all be mounted\n")); 6684 (void) fprintf(stderr, gettext("Try doing '%s' with a " 6685 "different altroot\n"), "zpool import"); 6686 } 6687 zpool_close(zhp); 6688 nvlist_free(config); 6689 nvlist_free(props); 6690 6691 return (ret); 6692 } 6693 6694 6695 6696 /* 6697 * zpool online <pool> <device> ... 6698 */ 6699 int 6700 zpool_do_online(int argc, char **argv) 6701 { 6702 int c, i; 6703 char *poolname; 6704 zpool_handle_t *zhp; 6705 int ret = 0; 6706 vdev_state_t newstate; 6707 int flags = 0; 6708 6709 /* check options */ 6710 while ((c = getopt(argc, argv, "e")) != -1) { 6711 switch (c) { 6712 case 'e': 6713 flags |= ZFS_ONLINE_EXPAND; 6714 break; 6715 case '?': 6716 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6717 optopt); 6718 usage(B_FALSE); 6719 } 6720 } 6721 6722 argc -= optind; 6723 argv += optind; 6724 6725 /* get pool name and check number of arguments */ 6726 if (argc < 1) { 6727 (void) fprintf(stderr, gettext("missing pool name\n")); 6728 usage(B_FALSE); 6729 } 6730 if (argc < 2) { 6731 (void) fprintf(stderr, gettext("missing device name\n")); 6732 usage(B_FALSE); 6733 } 6734 6735 poolname = argv[0]; 6736 6737 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6738 return (1); 6739 6740 for (i = 1; i < argc; i++) { 6741 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 6742 if (newstate != VDEV_STATE_HEALTHY) { 6743 (void) printf(gettext("warning: device '%s' " 6744 "onlined, but remains in faulted state\n"), 6745 argv[i]); 6746 if (newstate == VDEV_STATE_FAULTED) 6747 (void) printf(gettext("use 'zpool " 6748 "clear' to restore a faulted " 6749 "device\n")); 6750 else 6751 (void) printf(gettext("use 'zpool " 6752 "replace' to replace devices " 6753 "that are no longer present\n")); 6754 } 6755 } else { 6756 ret = 1; 6757 } 6758 } 6759 6760 zpool_close(zhp); 6761 6762 return (ret); 6763 } 6764 6765 /* 6766 * zpool offline [-ft] <pool> <device> ... 6767 * 6768 * -f Force the device into a faulted state. 6769 * 6770 * -t Only take the device off-line temporarily. The offline/faulted 6771 * state will not be persistent across reboots. 6772 */ 6773 /* ARGSUSED */ 6774 int 6775 zpool_do_offline(int argc, char **argv) 6776 { 6777 int c, i; 6778 char *poolname; 6779 zpool_handle_t *zhp; 6780 int ret = 0; 6781 boolean_t istmp = B_FALSE; 6782 boolean_t fault = B_FALSE; 6783 6784 /* check options */ 6785 while ((c = getopt(argc, argv, "ft")) != -1) { 6786 switch (c) { 6787 case 'f': 6788 fault = B_TRUE; 6789 break; 6790 case 't': 6791 istmp = B_TRUE; 6792 break; 6793 case '?': 6794 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6795 optopt); 6796 usage(B_FALSE); 6797 } 6798 } 6799 6800 argc -= optind; 6801 argv += optind; 6802 6803 /* get pool name and check number of arguments */ 6804 if (argc < 1) { 6805 (void) fprintf(stderr, gettext("missing pool name\n")); 6806 usage(B_FALSE); 6807 } 6808 if (argc < 2) { 6809 (void) fprintf(stderr, gettext("missing device name\n")); 6810 usage(B_FALSE); 6811 } 6812 6813 poolname = argv[0]; 6814 6815 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6816 return (1); 6817 6818 for (i = 1; i < argc; i++) { 6819 if (fault) { 6820 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); 6821 vdev_aux_t aux; 6822 if (istmp == B_FALSE) { 6823 /* Force the fault to persist across imports */ 6824 aux = VDEV_AUX_EXTERNAL_PERSIST; 6825 } else { 6826 aux = VDEV_AUX_EXTERNAL; 6827 } 6828 6829 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) 6830 ret = 1; 6831 } else { 6832 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 6833 ret = 1; 6834 } 6835 } 6836 6837 zpool_close(zhp); 6838 6839 return (ret); 6840 } 6841 6842 /* 6843 * zpool clear <pool> [device] 6844 * 6845 * Clear all errors associated with a pool or a particular device. 6846 */ 6847 int 6848 zpool_do_clear(int argc, char **argv) 6849 { 6850 int c; 6851 int ret = 0; 6852 boolean_t dryrun = B_FALSE; 6853 boolean_t do_rewind = B_FALSE; 6854 boolean_t xtreme_rewind = B_FALSE; 6855 uint32_t rewind_policy = ZPOOL_NO_REWIND; 6856 nvlist_t *policy = NULL; 6857 zpool_handle_t *zhp; 6858 char *pool, *device; 6859 6860 /* check options */ 6861 while ((c = getopt(argc, argv, "FnX")) != -1) { 6862 switch (c) { 6863 case 'F': 6864 do_rewind = B_TRUE; 6865 break; 6866 case 'n': 6867 dryrun = B_TRUE; 6868 break; 6869 case 'X': 6870 xtreme_rewind = B_TRUE; 6871 break; 6872 case '?': 6873 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6874 optopt); 6875 usage(B_FALSE); 6876 } 6877 } 6878 6879 argc -= optind; 6880 argv += optind; 6881 6882 if (argc < 1) { 6883 (void) fprintf(stderr, gettext("missing pool name\n")); 6884 usage(B_FALSE); 6885 } 6886 6887 if (argc > 2) { 6888 (void) fprintf(stderr, gettext("too many arguments\n")); 6889 usage(B_FALSE); 6890 } 6891 6892 if ((dryrun || xtreme_rewind) && !do_rewind) { 6893 (void) fprintf(stderr, 6894 gettext("-n or -X only meaningful with -F\n")); 6895 usage(B_FALSE); 6896 } 6897 if (dryrun) 6898 rewind_policy = ZPOOL_TRY_REWIND; 6899 else if (do_rewind) 6900 rewind_policy = ZPOOL_DO_REWIND; 6901 if (xtreme_rewind) 6902 rewind_policy |= ZPOOL_EXTREME_REWIND; 6903 6904 /* In future, further rewind policy choices can be passed along here */ 6905 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 6906 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 6907 rewind_policy) != 0) { 6908 return (1); 6909 } 6910 6911 pool = argv[0]; 6912 device = argc == 2 ? argv[1] : NULL; 6913 6914 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 6915 nvlist_free(policy); 6916 return (1); 6917 } 6918 6919 if (zpool_clear(zhp, device, policy) != 0) 6920 ret = 1; 6921 6922 zpool_close(zhp); 6923 6924 nvlist_free(policy); 6925 6926 return (ret); 6927 } 6928 6929 /* 6930 * zpool reguid <pool> 6931 */ 6932 int 6933 zpool_do_reguid(int argc, char **argv) 6934 { 6935 int c; 6936 char *poolname; 6937 zpool_handle_t *zhp; 6938 int ret = 0; 6939 6940 /* check options */ 6941 while ((c = getopt(argc, argv, "")) != -1) { 6942 switch (c) { 6943 case '?': 6944 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6945 optopt); 6946 usage(B_FALSE); 6947 } 6948 } 6949 6950 argc -= optind; 6951 argv += optind; 6952 6953 /* get pool name and check number of arguments */ 6954 if (argc < 1) { 6955 (void) fprintf(stderr, gettext("missing pool name\n")); 6956 usage(B_FALSE); 6957 } 6958 6959 if (argc > 1) { 6960 (void) fprintf(stderr, gettext("too many arguments\n")); 6961 usage(B_FALSE); 6962 } 6963 6964 poolname = argv[0]; 6965 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6966 return (1); 6967 6968 ret = zpool_reguid(zhp); 6969 6970 zpool_close(zhp); 6971 return (ret); 6972 } 6973 6974 6975 /* 6976 * zpool reopen <pool> 6977 * 6978 * Reopen the pool so that the kernel can update the sizes of all vdevs. 6979 */ 6980 int 6981 zpool_do_reopen(int argc, char **argv) 6982 { 6983 int c; 6984 int ret = 0; 6985 boolean_t scrub_restart = B_TRUE; 6986 6987 /* check options */ 6988 while ((c = getopt(argc, argv, "n")) != -1) { 6989 switch (c) { 6990 case 'n': 6991 scrub_restart = B_FALSE; 6992 break; 6993 case '?': 6994 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6995 optopt); 6996 usage(B_FALSE); 6997 } 6998 } 6999 7000 argc -= optind; 7001 argv += optind; 7002 7003 /* if argc == 0 we will execute zpool_reopen_one on all pools */ 7004 ret = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, zpool_reopen_one, 7005 &scrub_restart); 7006 7007 return (ret); 7008 } 7009 7010 typedef struct scrub_cbdata { 7011 int cb_type; 7012 pool_scrub_cmd_t cb_scrub_cmd; 7013 } scrub_cbdata_t; 7014 7015 static boolean_t 7016 zpool_has_checkpoint(zpool_handle_t *zhp) 7017 { 7018 nvlist_t *config, *nvroot; 7019 7020 config = zpool_get_config(zhp, NULL); 7021 7022 if (config != NULL) { 7023 pool_checkpoint_stat_t *pcs = NULL; 7024 uint_t c; 7025 7026 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 7027 (void) nvlist_lookup_uint64_array(nvroot, 7028 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 7029 7030 if (pcs == NULL || pcs->pcs_state == CS_NONE) 7031 return (B_FALSE); 7032 7033 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || 7034 pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 7035 return (B_TRUE); 7036 } 7037 7038 return (B_FALSE); 7039 } 7040 7041 static int 7042 scrub_callback(zpool_handle_t *zhp, void *data) 7043 { 7044 scrub_cbdata_t *cb = data; 7045 int err; 7046 7047 /* 7048 * Ignore faulted pools. 7049 */ 7050 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 7051 (void) fprintf(stderr, gettext("cannot scan '%s': pool is " 7052 "currently unavailable\n"), zpool_get_name(zhp)); 7053 return (1); 7054 } 7055 7056 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd); 7057 7058 if (err == 0 && zpool_has_checkpoint(zhp) && 7059 cb->cb_type == POOL_SCAN_SCRUB) { 7060 (void) printf(gettext("warning: will not scrub state that " 7061 "belongs to the checkpoint of pool '%s'\n"), 7062 zpool_get_name(zhp)); 7063 } 7064 7065 return (err != 0); 7066 } 7067 7068 static int 7069 wait_callback(zpool_handle_t *zhp, void *data) 7070 { 7071 zpool_wait_activity_t *act = data; 7072 return (zpool_wait(zhp, *act)); 7073 } 7074 7075 /* 7076 * zpool scrub [-s | -p] [-w] <pool> ... 7077 * 7078 * -s Stop. Stops any in-progress scrub. 7079 * -p Pause. Pause in-progress scrub. 7080 * -w Wait. Blocks until scrub has completed. 7081 */ 7082 int 7083 zpool_do_scrub(int argc, char **argv) 7084 { 7085 int c; 7086 scrub_cbdata_t cb; 7087 boolean_t wait = B_FALSE; 7088 int error; 7089 7090 cb.cb_type = POOL_SCAN_SCRUB; 7091 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 7092 7093 /* check options */ 7094 while ((c = getopt(argc, argv, "spw")) != -1) { 7095 switch (c) { 7096 case 's': 7097 cb.cb_type = POOL_SCAN_NONE; 7098 break; 7099 case 'p': 7100 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; 7101 break; 7102 case 'w': 7103 wait = B_TRUE; 7104 break; 7105 case '?': 7106 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7107 optopt); 7108 usage(B_FALSE); 7109 } 7110 } 7111 7112 if (cb.cb_type == POOL_SCAN_NONE && 7113 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) { 7114 (void) fprintf(stderr, gettext("invalid option combination: " 7115 "-s and -p are mutually exclusive\n")); 7116 usage(B_FALSE); 7117 } 7118 7119 if (wait && (cb.cb_type == POOL_SCAN_NONE || 7120 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { 7121 (void) fprintf(stderr, gettext("invalid option combination: " 7122 "-w cannot be used with -p or -s\n")); 7123 usage(B_FALSE); 7124 } 7125 7126 argc -= optind; 7127 argv += optind; 7128 7129 if (argc < 1) { 7130 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7131 usage(B_FALSE); 7132 } 7133 7134 error = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7135 scrub_callback, &cb); 7136 7137 if (wait && !error) { 7138 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; 7139 error = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7140 wait_callback, &act); 7141 } 7142 7143 return (error); 7144 } 7145 7146 /* 7147 * zpool resilver <pool> ... 7148 * 7149 * Restarts any in-progress resilver 7150 */ 7151 int 7152 zpool_do_resilver(int argc, char **argv) 7153 { 7154 int c; 7155 scrub_cbdata_t cb; 7156 7157 cb.cb_type = POOL_SCAN_RESILVER; 7158 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 7159 7160 /* check options */ 7161 while ((c = getopt(argc, argv, "")) != -1) { 7162 switch (c) { 7163 case '?': 7164 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7165 optopt); 7166 usage(B_FALSE); 7167 } 7168 } 7169 7170 argc -= optind; 7171 argv += optind; 7172 7173 if (argc < 1) { 7174 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7175 usage(B_FALSE); 7176 } 7177 7178 return (for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7179 scrub_callback, &cb)); 7180 } 7181 7182 /* 7183 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...] 7184 * 7185 * -c Cancel. Ends any in-progress trim. 7186 * -d Secure trim. Requires kernel and device support. 7187 * -r <rate> Sets the TRIM rate in bytes (per second). Supports 7188 * adding a multiplier suffix such as 'k' or 'm'. 7189 * -s Suspend. TRIM can then be restarted with no flags. 7190 * -w Wait. Blocks until trimming has completed. 7191 */ 7192 int 7193 zpool_do_trim(int argc, char **argv) 7194 { 7195 struct option long_options[] = { 7196 {"cancel", no_argument, NULL, 'c'}, 7197 {"secure", no_argument, NULL, 'd'}, 7198 {"rate", required_argument, NULL, 'r'}, 7199 {"suspend", no_argument, NULL, 's'}, 7200 {"wait", no_argument, NULL, 'w'}, 7201 {0, 0, 0, 0} 7202 }; 7203 7204 pool_trim_func_t cmd_type = POOL_TRIM_START; 7205 uint64_t rate = 0; 7206 boolean_t secure = B_FALSE; 7207 boolean_t wait = B_FALSE; 7208 7209 int c; 7210 while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL)) 7211 != -1) { 7212 switch (c) { 7213 case 'c': 7214 if (cmd_type != POOL_TRIM_START && 7215 cmd_type != POOL_TRIM_CANCEL) { 7216 (void) fprintf(stderr, gettext("-c cannot be " 7217 "combined with other options\n")); 7218 usage(B_FALSE); 7219 } 7220 cmd_type = POOL_TRIM_CANCEL; 7221 break; 7222 case 'd': 7223 if (cmd_type != POOL_TRIM_START) { 7224 (void) fprintf(stderr, gettext("-d cannot be " 7225 "combined with the -c or -s options\n")); 7226 usage(B_FALSE); 7227 } 7228 secure = B_TRUE; 7229 break; 7230 case 'r': 7231 if (cmd_type != POOL_TRIM_START) { 7232 (void) fprintf(stderr, gettext("-r cannot be " 7233 "combined with the -c or -s options\n")); 7234 usage(B_FALSE); 7235 } 7236 if (zfs_nicestrtonum(NULL, optarg, &rate) == -1) { 7237 (void) fprintf(stderr, 7238 gettext("invalid value for rate\n")); 7239 usage(B_FALSE); 7240 } 7241 break; 7242 case 's': 7243 if (cmd_type != POOL_TRIM_START && 7244 cmd_type != POOL_TRIM_SUSPEND) { 7245 (void) fprintf(stderr, gettext("-s cannot be " 7246 "combined with other options\n")); 7247 usage(B_FALSE); 7248 } 7249 cmd_type = POOL_TRIM_SUSPEND; 7250 break; 7251 case 'w': 7252 wait = B_TRUE; 7253 break; 7254 case '?': 7255 if (optopt != 0) { 7256 (void) fprintf(stderr, 7257 gettext("invalid option '%c'\n"), optopt); 7258 } else { 7259 (void) fprintf(stderr, 7260 gettext("invalid option '%s'\n"), 7261 argv[optind - 1]); 7262 } 7263 usage(B_FALSE); 7264 } 7265 } 7266 7267 argc -= optind; 7268 argv += optind; 7269 7270 if (argc < 1) { 7271 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7272 usage(B_FALSE); 7273 return (-1); 7274 } 7275 7276 if (wait && (cmd_type != POOL_TRIM_START)) { 7277 (void) fprintf(stderr, gettext("-w cannot be used with -c or " 7278 "-s\n")); 7279 usage(B_FALSE); 7280 } 7281 7282 char *poolname = argv[0]; 7283 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 7284 if (zhp == NULL) 7285 return (-1); 7286 7287 trimflags_t trim_flags = { 7288 .secure = secure, 7289 .rate = rate, 7290 .wait = wait, 7291 }; 7292 7293 nvlist_t *vdevs = fnvlist_alloc(); 7294 if (argc == 1) { 7295 /* no individual leaf vdevs specified, so add them all */ 7296 nvlist_t *config = zpool_get_config(zhp, NULL); 7297 nvlist_t *nvroot = fnvlist_lookup_nvlist(config, 7298 ZPOOL_CONFIG_VDEV_TREE); 7299 zpool_collect_leaves(zhp, nvroot, vdevs); 7300 trim_flags.fullpool = B_TRUE; 7301 } else { 7302 trim_flags.fullpool = B_FALSE; 7303 for (int i = 1; i < argc; i++) { 7304 fnvlist_add_boolean(vdevs, argv[i]); 7305 } 7306 } 7307 7308 int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags); 7309 7310 fnvlist_free(vdevs); 7311 zpool_close(zhp); 7312 7313 return (error); 7314 } 7315 7316 /* 7317 * Converts a total number of seconds to a human readable string broken 7318 * down in to days/hours/minutes/seconds. 7319 */ 7320 static void 7321 secs_to_dhms(uint64_t total, char *buf) 7322 { 7323 uint64_t days = total / 60 / 60 / 24; 7324 uint64_t hours = (total / 60 / 60) % 24; 7325 uint64_t mins = (total / 60) % 60; 7326 uint64_t secs = (total % 60); 7327 7328 if (days > 0) { 7329 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", 7330 (u_longlong_t)days, (u_longlong_t)hours, 7331 (u_longlong_t)mins, (u_longlong_t)secs); 7332 } else { 7333 (void) sprintf(buf, "%02llu:%02llu:%02llu", 7334 (u_longlong_t)hours, (u_longlong_t)mins, 7335 (u_longlong_t)secs); 7336 } 7337 } 7338 7339 /* 7340 * Print out detailed scrub status. 7341 */ 7342 static void 7343 print_scan_scrub_resilver_status(pool_scan_stat_t *ps) 7344 { 7345 time_t start, end, pause; 7346 uint64_t pass_scanned, scanned, pass_issued, issued, total; 7347 uint64_t elapsed, scan_rate, issue_rate; 7348 double fraction_done; 7349 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7]; 7350 char srate_buf[7], irate_buf[7], time_buf[32]; 7351 7352 printf(" "); 7353 printf_color(ANSI_BOLD, gettext("scan:")); 7354 printf(" "); 7355 7356 /* If there's never been a scan, there's not much to say. */ 7357 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 7358 ps->pss_func >= POOL_SCAN_FUNCS) { 7359 (void) printf(gettext("none requested\n")); 7360 return; 7361 } 7362 7363 start = ps->pss_start_time; 7364 end = ps->pss_end_time; 7365 pause = ps->pss_pass_scrub_pause; 7366 7367 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); 7368 7369 assert(ps->pss_func == POOL_SCAN_SCRUB || 7370 ps->pss_func == POOL_SCAN_RESILVER); 7371 7372 /* Scan is finished or canceled. */ 7373 if (ps->pss_state == DSS_FINISHED) { 7374 secs_to_dhms(end - start, time_buf); 7375 7376 if (ps->pss_func == POOL_SCAN_SCRUB) { 7377 (void) printf(gettext("scrub repaired %s " 7378 "in %s with %llu errors on %s"), processed_buf, 7379 time_buf, (u_longlong_t)ps->pss_errors, 7380 ctime(&end)); 7381 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7382 (void) printf(gettext("resilvered %s " 7383 "in %s with %llu errors on %s"), processed_buf, 7384 time_buf, (u_longlong_t)ps->pss_errors, 7385 ctime(&end)); 7386 } 7387 return; 7388 } else if (ps->pss_state == DSS_CANCELED) { 7389 if (ps->pss_func == POOL_SCAN_SCRUB) { 7390 (void) printf(gettext("scrub canceled on %s"), 7391 ctime(&end)); 7392 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7393 (void) printf(gettext("resilver canceled on %s"), 7394 ctime(&end)); 7395 } 7396 return; 7397 } 7398 7399 assert(ps->pss_state == DSS_SCANNING); 7400 7401 /* Scan is in progress. Resilvers can't be paused. */ 7402 if (ps->pss_func == POOL_SCAN_SCRUB) { 7403 if (pause == 0) { 7404 (void) printf(gettext("scrub in progress since %s"), 7405 ctime(&start)); 7406 } else { 7407 (void) printf(gettext("scrub paused since %s"), 7408 ctime(&pause)); 7409 (void) printf(gettext("\tscrub started on %s"), 7410 ctime(&start)); 7411 } 7412 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7413 (void) printf(gettext("resilver in progress since %s"), 7414 ctime(&start)); 7415 } 7416 7417 scanned = ps->pss_examined; 7418 pass_scanned = ps->pss_pass_exam; 7419 issued = ps->pss_issued; 7420 pass_issued = ps->pss_pass_issued; 7421 total = ps->pss_to_examine; 7422 7423 /* we are only done with a block once we have issued the IO for it */ 7424 fraction_done = (double)issued / total; 7425 7426 /* elapsed time for this pass, rounding up to 1 if it's 0 */ 7427 elapsed = time(NULL) - ps->pss_pass_start; 7428 elapsed -= ps->pss_pass_scrub_spent_paused; 7429 elapsed = (elapsed != 0) ? elapsed : 1; 7430 7431 scan_rate = pass_scanned / elapsed; 7432 issue_rate = pass_issued / elapsed; 7433 uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ? 7434 ((total - issued) / issue_rate) : UINT64_MAX; 7435 secs_to_dhms(total_secs_left, time_buf); 7436 7437 /* format all of the numbers we will be reporting */ 7438 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); 7439 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); 7440 zfs_nicebytes(total, total_buf, sizeof (total_buf)); 7441 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); 7442 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); 7443 7444 /* do not print estimated time if we have a paused scrub */ 7445 if (pause == 0) { 7446 (void) printf(gettext("\t%s scanned at %s/s, " 7447 "%s issued at %s/s, %s total\n"), 7448 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf); 7449 } else { 7450 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"), 7451 scanned_buf, issued_buf, total_buf); 7452 } 7453 7454 if (ps->pss_func == POOL_SCAN_RESILVER) { 7455 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 7456 processed_buf, 100 * fraction_done); 7457 } else if (ps->pss_func == POOL_SCAN_SCRUB) { 7458 (void) printf(gettext("\t%s repaired, %.2f%% done"), 7459 processed_buf, 100 * fraction_done); 7460 } 7461 7462 if (pause == 0) { 7463 if (total_secs_left != UINT64_MAX && 7464 issue_rate >= 10 * 1024 * 1024) { 7465 (void) printf(gettext(", %s to go\n"), time_buf); 7466 } else { 7467 (void) printf(gettext(", no estimated " 7468 "completion time\n")); 7469 } 7470 } else { 7471 (void) printf(gettext("\n")); 7472 } 7473 } 7474 7475 static void 7476 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name) 7477 { 7478 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) 7479 return; 7480 7481 printf(" "); 7482 printf_color(ANSI_BOLD, gettext("scan:")); 7483 printf(" "); 7484 7485 uint64_t bytes_scanned = vrs->vrs_bytes_scanned; 7486 uint64_t bytes_issued = vrs->vrs_bytes_issued; 7487 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; 7488 uint64_t bytes_est = vrs->vrs_bytes_est; 7489 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / 7490 (vrs->vrs_pass_time_ms + 1)) * 1000; 7491 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / 7492 (vrs->vrs_pass_time_ms + 1)) * 1000; 7493 double scan_pct = MIN((double)bytes_scanned * 100 / 7494 (bytes_est + 1), 100); 7495 7496 /* Format all of the numbers we will be reporting */ 7497 char bytes_scanned_buf[7], bytes_issued_buf[7]; 7498 char bytes_rebuilt_buf[7], bytes_est_buf[7]; 7499 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; 7500 zfs_nicebytes(bytes_scanned, bytes_scanned_buf, 7501 sizeof (bytes_scanned_buf)); 7502 zfs_nicebytes(bytes_issued, bytes_issued_buf, 7503 sizeof (bytes_issued_buf)); 7504 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, 7505 sizeof (bytes_rebuilt_buf)); 7506 zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf)); 7507 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); 7508 zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf)); 7509 7510 time_t start = vrs->vrs_start_time; 7511 time_t end = vrs->vrs_end_time; 7512 7513 /* Rebuild is finished or canceled. */ 7514 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { 7515 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); 7516 (void) printf(gettext("resilvered (%s) %s in %s " 7517 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, 7518 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); 7519 return; 7520 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { 7521 (void) printf(gettext("resilver (%s) canceled on %s"), 7522 vdev_name, ctime(&end)); 7523 return; 7524 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7525 (void) printf(gettext("resilver (%s) in progress since %s"), 7526 vdev_name, ctime(&start)); 7527 } 7528 7529 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); 7530 7531 secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) / 7532 MAX(scan_rate, 1), time_buf); 7533 7534 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, " 7535 "%s total\n"), bytes_scanned_buf, scan_rate_buf, 7536 bytes_issued_buf, issue_rate_buf, bytes_est_buf); 7537 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 7538 bytes_rebuilt_buf, scan_pct); 7539 7540 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7541 if (scan_rate >= 10 * 1024 * 1024) { 7542 (void) printf(gettext(", %s to go\n"), time_buf); 7543 } else { 7544 (void) printf(gettext(", no estimated " 7545 "completion time\n")); 7546 } 7547 } else { 7548 (void) printf(gettext("\n")); 7549 } 7550 } 7551 7552 /* 7553 * Print rebuild status for top-level vdevs. 7554 */ 7555 static void 7556 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) 7557 { 7558 nvlist_t **child; 7559 uint_t children; 7560 7561 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7562 &child, &children) != 0) 7563 children = 0; 7564 7565 for (uint_t c = 0; c < children; c++) { 7566 vdev_rebuild_stat_t *vrs; 7567 uint_t i; 7568 7569 if (nvlist_lookup_uint64_array(child[c], 7570 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 7571 char *name = zpool_vdev_name(g_zfs, zhp, 7572 child[c], VDEV_NAME_TYPE_ID); 7573 print_rebuild_status_impl(vrs, name); 7574 free(name); 7575 } 7576 } 7577 } 7578 7579 /* 7580 * As we don't scrub checkpointed blocks, we want to warn the user that we 7581 * skipped scanning some blocks if a checkpoint exists or existed at any 7582 * time during the scan. If a sequential instead of healing reconstruction 7583 * was performed then the blocks were reconstructed. However, their checksums 7584 * have not been verified so we still print the warning. 7585 */ 7586 static void 7587 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) 7588 { 7589 if (ps == NULL || pcs == NULL) 7590 return; 7591 7592 if (pcs->pcs_state == CS_NONE || 7593 pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 7594 return; 7595 7596 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); 7597 7598 if (ps->pss_state == DSS_NONE) 7599 return; 7600 7601 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && 7602 ps->pss_end_time < pcs->pcs_start_time) 7603 return; 7604 7605 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { 7606 (void) printf(gettext(" scan warning: skipped blocks " 7607 "that are only referenced by the checkpoint.\n")); 7608 } else { 7609 assert(ps->pss_state == DSS_SCANNING); 7610 (void) printf(gettext(" scan warning: skipping blocks " 7611 "that are only referenced by the checkpoint.\n")); 7612 } 7613 } 7614 7615 /* 7616 * Returns B_TRUE if there is an active rebuild in progress. Otherwise, 7617 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for 7618 * the last completed (or cancelled) rebuild. 7619 */ 7620 static boolean_t 7621 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) 7622 { 7623 nvlist_t **child; 7624 uint_t children; 7625 boolean_t rebuilding = B_FALSE; 7626 uint64_t end_time = 0; 7627 7628 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7629 &child, &children) != 0) 7630 children = 0; 7631 7632 for (uint_t c = 0; c < children; c++) { 7633 vdev_rebuild_stat_t *vrs; 7634 uint_t i; 7635 7636 if (nvlist_lookup_uint64_array(child[c], 7637 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 7638 7639 if (vrs->vrs_end_time > end_time) 7640 end_time = vrs->vrs_end_time; 7641 7642 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7643 rebuilding = B_TRUE; 7644 end_time = 0; 7645 break; 7646 } 7647 } 7648 } 7649 7650 if (rebuild_end_time != NULL) 7651 *rebuild_end_time = end_time; 7652 7653 return (rebuilding); 7654 } 7655 7656 /* 7657 * Print the scan status. 7658 */ 7659 static void 7660 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) 7661 { 7662 uint64_t rebuild_end_time = 0, resilver_end_time = 0; 7663 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; 7664 boolean_t active_resilver = B_FALSE; 7665 pool_checkpoint_stat_t *pcs = NULL; 7666 pool_scan_stat_t *ps = NULL; 7667 uint_t c; 7668 7669 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 7670 (uint64_t **)&ps, &c) == 0) { 7671 if (ps->pss_func == POOL_SCAN_RESILVER) { 7672 resilver_end_time = ps->pss_end_time; 7673 active_resilver = (ps->pss_state == DSS_SCANNING); 7674 } 7675 7676 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); 7677 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); 7678 } 7679 7680 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); 7681 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); 7682 7683 /* Always print the scrub status when available. */ 7684 if (have_scrub) 7685 print_scan_scrub_resilver_status(ps); 7686 7687 /* 7688 * When there is an active resilver or rebuild print its status. 7689 * Otherwise print the status of the last resilver or rebuild. 7690 */ 7691 if (active_resilver || (!active_rebuild && have_resilver && 7692 resilver_end_time && resilver_end_time > rebuild_end_time)) { 7693 print_scan_scrub_resilver_status(ps); 7694 } else if (active_rebuild || (!active_resilver && have_rebuild && 7695 rebuild_end_time && rebuild_end_time > resilver_end_time)) { 7696 print_rebuild_status(zhp, nvroot); 7697 } 7698 7699 (void) nvlist_lookup_uint64_array(nvroot, 7700 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 7701 print_checkpoint_scan_warning(ps, pcs); 7702 } 7703 7704 /* 7705 * Print out detailed removal status. 7706 */ 7707 static void 7708 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) 7709 { 7710 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 7711 time_t start, end; 7712 nvlist_t *config, *nvroot; 7713 nvlist_t **child; 7714 uint_t children; 7715 char *vdev_name; 7716 7717 if (prs == NULL || prs->prs_state == DSS_NONE) 7718 return; 7719 7720 /* 7721 * Determine name of vdev. 7722 */ 7723 config = zpool_get_config(zhp, NULL); 7724 nvroot = fnvlist_lookup_nvlist(config, 7725 ZPOOL_CONFIG_VDEV_TREE); 7726 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7727 &child, &children) == 0); 7728 assert(prs->prs_removing_vdev < children); 7729 vdev_name = zpool_vdev_name(g_zfs, zhp, 7730 child[prs->prs_removing_vdev], B_TRUE); 7731 7732 printf_color(ANSI_BOLD, gettext("remove: ")); 7733 7734 start = prs->prs_start_time; 7735 end = prs->prs_end_time; 7736 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); 7737 7738 /* 7739 * Removal is finished or canceled. 7740 */ 7741 if (prs->prs_state == DSS_FINISHED) { 7742 uint64_t minutes_taken = (end - start) / 60; 7743 7744 (void) printf(gettext("Removal of vdev %llu copied %s " 7745 "in %lluh%um, completed on %s"), 7746 (longlong_t)prs->prs_removing_vdev, 7747 copied_buf, 7748 (u_longlong_t)(minutes_taken / 60), 7749 (uint_t)(minutes_taken % 60), 7750 ctime((time_t *)&end)); 7751 } else if (prs->prs_state == DSS_CANCELED) { 7752 (void) printf(gettext("Removal of %s canceled on %s"), 7753 vdev_name, ctime(&end)); 7754 } else { 7755 uint64_t copied, total, elapsed, mins_left, hours_left; 7756 double fraction_done; 7757 uint_t rate; 7758 7759 assert(prs->prs_state == DSS_SCANNING); 7760 7761 /* 7762 * Removal is in progress. 7763 */ 7764 (void) printf(gettext( 7765 "Evacuation of %s in progress since %s"), 7766 vdev_name, ctime(&start)); 7767 7768 copied = prs->prs_copied > 0 ? prs->prs_copied : 1; 7769 total = prs->prs_to_copy; 7770 fraction_done = (double)copied / total; 7771 7772 /* elapsed time for this pass */ 7773 elapsed = time(NULL) - prs->prs_start_time; 7774 elapsed = elapsed > 0 ? elapsed : 1; 7775 rate = copied / elapsed; 7776 rate = rate > 0 ? rate : 1; 7777 mins_left = ((total - copied) / rate) / 60; 7778 hours_left = mins_left / 60; 7779 7780 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 7781 zfs_nicenum(total, total_buf, sizeof (total_buf)); 7782 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 7783 7784 /* 7785 * do not print estimated time if hours_left is more than 7786 * 30 days 7787 */ 7788 (void) printf(gettext(" %s copied out of %s at %s/s, " 7789 "%.2f%% done"), 7790 examined_buf, total_buf, rate_buf, 100 * fraction_done); 7791 if (hours_left < (30 * 24)) { 7792 (void) printf(gettext(", %lluh%um to go\n"), 7793 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 7794 } else { 7795 (void) printf(gettext( 7796 ", (copy is slow, no estimated time)\n")); 7797 } 7798 } 7799 free(vdev_name); 7800 7801 if (prs->prs_mapping_memory > 0) { 7802 char mem_buf[7]; 7803 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); 7804 (void) printf(gettext(" %s memory used for " 7805 "removed device mappings\n"), 7806 mem_buf); 7807 } 7808 } 7809 7810 static void 7811 print_checkpoint_status(pool_checkpoint_stat_t *pcs) 7812 { 7813 time_t start; 7814 char space_buf[7]; 7815 7816 if (pcs == NULL || pcs->pcs_state == CS_NONE) 7817 return; 7818 7819 (void) printf(gettext("checkpoint: ")); 7820 7821 start = pcs->pcs_start_time; 7822 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); 7823 7824 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { 7825 char *date = ctime(&start); 7826 7827 /* 7828 * ctime() adds a newline at the end of the generated 7829 * string, thus the weird format specifier and the 7830 * strlen() call used to chop it off from the output. 7831 */ 7832 (void) printf(gettext("created %.*s, consumes %s\n"), 7833 (int)(strlen(date) - 1), date, space_buf); 7834 return; 7835 } 7836 7837 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 7838 7839 (void) printf(gettext("discarding, %s remaining.\n"), 7840 space_buf); 7841 } 7842 7843 static void 7844 print_error_log(zpool_handle_t *zhp) 7845 { 7846 nvlist_t *nverrlist = NULL; 7847 nvpair_t *elem; 7848 char *pathname; 7849 size_t len = MAXPATHLEN * 2; 7850 7851 if (zpool_get_errlog(zhp, &nverrlist) != 0) 7852 return; 7853 7854 (void) printf("errors: Permanent errors have been " 7855 "detected in the following files:\n\n"); 7856 7857 pathname = safe_malloc(len); 7858 elem = NULL; 7859 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 7860 nvlist_t *nv; 7861 uint64_t dsobj, obj; 7862 7863 verify(nvpair_value_nvlist(elem, &nv) == 0); 7864 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 7865 &dsobj) == 0); 7866 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 7867 &obj) == 0); 7868 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 7869 (void) printf("%7s %s\n", "", pathname); 7870 } 7871 free(pathname); 7872 nvlist_free(nverrlist); 7873 } 7874 7875 static void 7876 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, 7877 uint_t nspares) 7878 { 7879 uint_t i; 7880 char *name; 7881 7882 if (nspares == 0) 7883 return; 7884 7885 (void) printf(gettext("\tspares\n")); 7886 7887 for (i = 0; i < nspares; i++) { 7888 name = zpool_vdev_name(g_zfs, zhp, spares[i], 7889 cb->cb_name_flags); 7890 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); 7891 free(name); 7892 } 7893 } 7894 7895 static void 7896 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, 7897 uint_t nl2cache) 7898 { 7899 uint_t i; 7900 char *name; 7901 7902 if (nl2cache == 0) 7903 return; 7904 7905 (void) printf(gettext("\tcache\n")); 7906 7907 for (i = 0; i < nl2cache; i++) { 7908 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], 7909 cb->cb_name_flags); 7910 print_status_config(zhp, cb, name, l2cache[i], 2, 7911 B_FALSE, NULL); 7912 free(name); 7913 } 7914 } 7915 7916 static void 7917 print_dedup_stats(nvlist_t *config) 7918 { 7919 ddt_histogram_t *ddh; 7920 ddt_stat_t *dds; 7921 ddt_object_t *ddo; 7922 uint_t c; 7923 char dspace[6], mspace[6]; 7924 7925 /* 7926 * If the pool was faulted then we may not have been able to 7927 * obtain the config. Otherwise, if we have anything in the dedup 7928 * table continue processing the stats. 7929 */ 7930 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 7931 (uint64_t **)&ddo, &c) != 0) 7932 return; 7933 7934 (void) printf("\n"); 7935 (void) printf(gettext(" dedup: ")); 7936 if (ddo->ddo_count == 0) { 7937 (void) printf(gettext("no DDT entries\n")); 7938 return; 7939 } 7940 7941 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace)); 7942 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace)); 7943 (void) printf("DDT entries %llu, size %s on disk, %s in core\n", 7944 (u_longlong_t)ddo->ddo_count, 7945 dspace, 7946 mspace); 7947 7948 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 7949 (uint64_t **)&dds, &c) == 0); 7950 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 7951 (uint64_t **)&ddh, &c) == 0); 7952 zpool_dump_ddt(dds, ddh); 7953 } 7954 7955 /* 7956 * Display a summary of pool status. Displays a summary such as: 7957 * 7958 * pool: tank 7959 * status: DEGRADED 7960 * reason: One or more devices ... 7961 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 7962 * config: 7963 * mirror DEGRADED 7964 * c1t0d0 OK 7965 * c2t0d0 UNAVAIL 7966 * 7967 * When given the '-v' option, we print out the complete config. If the '-e' 7968 * option is specified, then we print out error rate information as well. 7969 */ 7970 static int 7971 status_callback(zpool_handle_t *zhp, void *data) 7972 { 7973 status_cbdata_t *cbp = data; 7974 nvlist_t *config, *nvroot; 7975 char *msgid; 7976 zpool_status_t reason; 7977 zpool_errata_t errata; 7978 const char *health; 7979 uint_t c; 7980 vdev_stat_t *vs; 7981 7982 config = zpool_get_config(zhp, NULL); 7983 reason = zpool_get_status(zhp, &msgid, &errata); 7984 7985 cbp->cb_count++; 7986 7987 /* 7988 * If we were given 'zpool status -x', only report those pools with 7989 * problems. 7990 */ 7991 if (cbp->cb_explain && 7992 (reason == ZPOOL_STATUS_OK || 7993 reason == ZPOOL_STATUS_VERSION_OLDER || 7994 reason == ZPOOL_STATUS_FEAT_DISABLED || 7995 reason == ZPOOL_STATUS_COMPATIBILITY_ERR)) { 7996 if (!cbp->cb_allpools) { 7997 (void) printf(gettext("pool '%s' is healthy\n"), 7998 zpool_get_name(zhp)); 7999 if (cbp->cb_first) 8000 cbp->cb_first = B_FALSE; 8001 } 8002 return (0); 8003 } 8004 8005 if (cbp->cb_first) 8006 cbp->cb_first = B_FALSE; 8007 else 8008 (void) printf("\n"); 8009 8010 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 8011 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 8012 (uint64_t **)&vs, &c) == 0); 8013 8014 health = zpool_get_state_str(zhp); 8015 8016 printf(" "); 8017 printf_color(ANSI_BOLD, gettext("pool:")); 8018 printf(" %s\n", zpool_get_name(zhp)); 8019 printf(" "); 8020 printf_color(ANSI_BOLD, gettext("state: ")); 8021 8022 printf_color(health_str_to_color(health), "%s", health); 8023 8024 printf("\n"); 8025 8026 switch (reason) { 8027 case ZPOOL_STATUS_MISSING_DEV_R: 8028 printf_color(ANSI_BOLD, gettext("status: ")); 8029 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8030 "not be opened. Sufficient replicas exist for\n\tthe pool " 8031 "to continue functioning in a degraded state.\n")); 8032 printf_color(ANSI_BOLD, gettext("action: ")); 8033 printf_color(ANSI_YELLOW, gettext("Attach the missing device " 8034 "and online it using 'zpool online'.\n")); 8035 break; 8036 8037 case ZPOOL_STATUS_MISSING_DEV_NR: 8038 printf_color(ANSI_BOLD, gettext("status: ")); 8039 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8040 "not be opened. There are insufficient\n\treplicas for the" 8041 " pool to continue functioning.\n")); 8042 printf_color(ANSI_BOLD, gettext("action: ")); 8043 printf_color(ANSI_YELLOW, gettext("Attach the missing device " 8044 "and online it using 'zpool online'.\n")); 8045 break; 8046 8047 case ZPOOL_STATUS_CORRUPT_LABEL_R: 8048 printf_color(ANSI_BOLD, gettext("status: ")); 8049 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8050 "not be used because the label is missing or\n\tinvalid. " 8051 "Sufficient replicas exist for the pool to continue\n\t" 8052 "functioning in a degraded state.\n")); 8053 printf_color(ANSI_BOLD, gettext("action: ")); 8054 printf_color(ANSI_YELLOW, gettext("Replace the device using " 8055 "'zpool replace'.\n")); 8056 break; 8057 8058 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 8059 printf_color(ANSI_BOLD, gettext("status: ")); 8060 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8061 "not be used because the label is missing \n\tor invalid. " 8062 "There are insufficient replicas for the pool to " 8063 "continue\n\tfunctioning.\n")); 8064 zpool_explain_recover(zpool_get_handle(zhp), 8065 zpool_get_name(zhp), reason, config); 8066 break; 8067 8068 case ZPOOL_STATUS_FAILING_DEV: 8069 printf_color(ANSI_BOLD, gettext("status: ")); 8070 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8071 "experienced an unrecoverable error. An\n\tattempt was " 8072 "made to correct the error. Applications are " 8073 "unaffected.\n")); 8074 printf_color(ANSI_BOLD, gettext("action: ")); 8075 printf_color(ANSI_YELLOW, gettext("Determine if the " 8076 "device needs to be replaced, and clear the errors\n\tusing" 8077 " 'zpool clear' or replace the device with 'zpool " 8078 "replace'.\n")); 8079 break; 8080 8081 case ZPOOL_STATUS_OFFLINE_DEV: 8082 printf_color(ANSI_BOLD, gettext("status: ")); 8083 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8084 "been taken offline by the administrator.\n\tSufficient " 8085 "replicas exist for the pool to continue functioning in " 8086 "a\n\tdegraded state.\n")); 8087 printf_color(ANSI_BOLD, gettext("action: ")); 8088 printf_color(ANSI_YELLOW, gettext("Online the device " 8089 "using 'zpool online' or replace the device with\n\t'zpool " 8090 "replace'.\n")); 8091 break; 8092 8093 case ZPOOL_STATUS_REMOVED_DEV: 8094 printf_color(ANSI_BOLD, gettext("status: ")); 8095 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8096 "been removed by the administrator.\n\tSufficient " 8097 "replicas exist for the pool to continue functioning in " 8098 "a\n\tdegraded state.\n")); 8099 printf_color(ANSI_BOLD, gettext("action: ")); 8100 printf_color(ANSI_YELLOW, gettext("Online the device " 8101 "using zpool online' or replace the device with\n\t'zpool " 8102 "replace'.\n")); 8103 break; 8104 8105 case ZPOOL_STATUS_RESILVERING: 8106 case ZPOOL_STATUS_REBUILDING: 8107 printf_color(ANSI_BOLD, gettext("status: ")); 8108 printf_color(ANSI_YELLOW, gettext("One or more devices is " 8109 "currently being resilvered. The pool will\n\tcontinue " 8110 "to function, possibly in a degraded state.\n")); 8111 printf_color(ANSI_BOLD, gettext("action: ")); 8112 printf_color(ANSI_YELLOW, gettext("Wait for the resilver to " 8113 "complete.\n")); 8114 break; 8115 8116 case ZPOOL_STATUS_REBUILD_SCRUB: 8117 printf_color(ANSI_BOLD, gettext("status: ")); 8118 printf_color(ANSI_YELLOW, gettext("One or more devices have " 8119 "been sequentially resilvered, scrubbing\n\tthe pool " 8120 "is recommended.\n")); 8121 printf_color(ANSI_BOLD, gettext("action: ")); 8122 printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to " 8123 "verify all data checksums.\n")); 8124 break; 8125 8126 case ZPOOL_STATUS_CORRUPT_DATA: 8127 printf_color(ANSI_BOLD, gettext("status: ")); 8128 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8129 "experienced an error resulting in data\n\tcorruption. " 8130 "Applications may be affected.\n")); 8131 printf_color(ANSI_BOLD, gettext("action: ")); 8132 printf_color(ANSI_YELLOW, gettext("Restore the file in question" 8133 " if possible. Otherwise restore the\n\tentire pool from " 8134 "backup.\n")); 8135 break; 8136 8137 case ZPOOL_STATUS_CORRUPT_POOL: 8138 printf_color(ANSI_BOLD, gettext("status: ")); 8139 printf_color(ANSI_YELLOW, gettext("The pool metadata is " 8140 "corrupted and the pool cannot be opened.\n")); 8141 zpool_explain_recover(zpool_get_handle(zhp), 8142 zpool_get_name(zhp), reason, config); 8143 break; 8144 8145 case ZPOOL_STATUS_VERSION_OLDER: 8146 printf_color(ANSI_BOLD, gettext("status: ")); 8147 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 8148 "a legacy on-disk format. The pool can\n\tstill be used, " 8149 "but some features are unavailable.\n")); 8150 printf_color(ANSI_BOLD, gettext("action: ")); 8151 printf_color(ANSI_YELLOW, gettext("Upgrade the pool using " 8152 "'zpool upgrade'. Once this is done, the\n\tpool will no " 8153 "longer be accessible on software that does not support\n\t" 8154 "feature flags.\n")); 8155 break; 8156 8157 case ZPOOL_STATUS_VERSION_NEWER: 8158 printf_color(ANSI_BOLD, gettext("status: ")); 8159 printf_color(ANSI_YELLOW, gettext("The pool has been upgraded " 8160 "to a newer, incompatible on-disk version.\n\tThe pool " 8161 "cannot be accessed on this system.\n")); 8162 printf_color(ANSI_BOLD, gettext("action: ")); 8163 printf_color(ANSI_YELLOW, gettext("Access the pool from a " 8164 "system running more recent software, or\n\trestore the " 8165 "pool from backup.\n")); 8166 break; 8167 8168 case ZPOOL_STATUS_FEAT_DISABLED: 8169 printf_color(ANSI_BOLD, gettext("status: ")); 8170 printf_color(ANSI_YELLOW, gettext("Some supported and " 8171 "requested features are not enabled on the pool.\n\t" 8172 "The pool can still be used, but some features are " 8173 "unavailable.\n")); 8174 printf_color(ANSI_BOLD, gettext("action: ")); 8175 printf_color(ANSI_YELLOW, gettext("Enable all features using " 8176 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 8177 "longer be accessible by software that does not support\n\t" 8178 "the features. See zpool-features(5) for details.\n")); 8179 break; 8180 8181 case ZPOOL_STATUS_COMPATIBILITY_ERR: 8182 printf_color(ANSI_BOLD, gettext("status: ")); 8183 printf_color(ANSI_YELLOW, gettext("This pool has a " 8184 "compatibility list specified, but it could not be\n\t" 8185 "read/parsed at this time. The pool can still be used, " 8186 "but this\n\tshould be investigated.\n")); 8187 printf_color(ANSI_BOLD, gettext("action: ")); 8188 printf_color(ANSI_YELLOW, gettext("Check the value of the " 8189 "'compatibility' property against the\n\t" 8190 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " 8191 ZPOOL_DATA_COMPAT_D ".\n")); 8192 break; 8193 8194 case ZPOOL_STATUS_UNSUP_FEAT_READ: 8195 printf_color(ANSI_BOLD, gettext("status: ")); 8196 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " 8197 "on this system because it uses the\n\tfollowing feature(s)" 8198 " not supported on this system:\n")); 8199 zpool_print_unsup_feat(config); 8200 (void) printf("\n"); 8201 printf_color(ANSI_BOLD, gettext("action: ")); 8202 printf_color(ANSI_YELLOW, gettext("Access the pool from a " 8203 "system that supports the required feature(s),\n\tor " 8204 "restore the pool from backup.\n")); 8205 break; 8206 8207 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 8208 printf_color(ANSI_BOLD, gettext("status: ")); 8209 printf_color(ANSI_YELLOW, gettext("The pool can only be " 8210 "accessed in read-only mode on this system. It\n\tcannot be" 8211 " accessed in read-write mode because it uses the " 8212 "following\n\tfeature(s) not supported on this system:\n")); 8213 zpool_print_unsup_feat(config); 8214 (void) printf("\n"); 8215 printf_color(ANSI_BOLD, gettext("action: ")); 8216 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " 8217 "in read-write mode. Import the pool with\n" 8218 "\t\"-o readonly=on\", access the pool from a system that " 8219 "supports the\n\trequired feature(s), or restore the " 8220 "pool from backup.\n")); 8221 break; 8222 8223 case ZPOOL_STATUS_FAULTED_DEV_R: 8224 printf_color(ANSI_BOLD, gettext("status: ")); 8225 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8226 "faulted in response to persistent errors.\n\tSufficient " 8227 "replicas exist for the pool to continue functioning " 8228 "in a\n\tdegraded state.\n")); 8229 printf_color(ANSI_BOLD, gettext("action: ")); 8230 printf_color(ANSI_YELLOW, gettext("Replace the faulted device, " 8231 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 8232 break; 8233 8234 case ZPOOL_STATUS_FAULTED_DEV_NR: 8235 printf_color(ANSI_BOLD, gettext("status: ")); 8236 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8237 "faulted in response to persistent errors. There are " 8238 "insufficient replicas for the pool to\n\tcontinue " 8239 "functioning.\n")); 8240 printf_color(ANSI_BOLD, gettext("action: ")); 8241 printf_color(ANSI_YELLOW, gettext("Destroy and re-create the " 8242 "pool from a backup source. Manually marking the device\n" 8243 "\trepaired using 'zpool clear' may allow some data " 8244 "to be recovered.\n")); 8245 break; 8246 8247 case ZPOOL_STATUS_IO_FAILURE_MMP: 8248 printf_color(ANSI_BOLD, gettext("status: ")); 8249 printf_color(ANSI_YELLOW, gettext("The pool is suspended " 8250 "because multihost writes failed or were delayed;\n\t" 8251 "another system could import the pool undetected.\n")); 8252 printf_color(ANSI_BOLD, gettext("action: ")); 8253 printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices" 8254 " are connected, then reboot your system and\n\timport the " 8255 "pool.\n")); 8256 break; 8257 8258 case ZPOOL_STATUS_IO_FAILURE_WAIT: 8259 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 8260 printf_color(ANSI_BOLD, gettext("status: ")); 8261 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8262 "faulted in response to IO failures.\n")); 8263 printf_color(ANSI_BOLD, gettext("action: ")); 8264 printf_color(ANSI_YELLOW, gettext("Make sure the affected " 8265 "devices are connected, then run 'zpool clear'.\n")); 8266 break; 8267 8268 case ZPOOL_STATUS_BAD_LOG: 8269 printf_color(ANSI_BOLD, gettext("status: ")); 8270 printf_color(ANSI_YELLOW, gettext("An intent log record " 8271 "could not be read.\n" 8272 "\tWaiting for administrator intervention to fix the " 8273 "faulted pool.\n")); 8274 printf_color(ANSI_BOLD, gettext("action: ")); 8275 printf_color(ANSI_YELLOW, gettext("Either restore the affected " 8276 "device(s) and run 'zpool online',\n" 8277 "\tor ignore the intent log records by running " 8278 "'zpool clear'.\n")); 8279 break; 8280 8281 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 8282 (void) printf(gettext("status: One or more devices are " 8283 "configured to use a non-native block size.\n" 8284 "\tExpect reduced performance.\n")); 8285 (void) printf(gettext("action: Replace affected devices with " 8286 "devices that support the\n\tconfigured block size, or " 8287 "migrate data to a properly configured\n\tpool.\n")); 8288 break; 8289 8290 case ZPOOL_STATUS_HOSTID_MISMATCH: 8291 printf_color(ANSI_BOLD, gettext("status: ")); 8292 printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid" 8293 " and system hostid on imported pool.\n\tThis pool was " 8294 "previously imported into a system with a different " 8295 "hostid,\n\tand then was verbatim imported into this " 8296 "system.\n")); 8297 printf_color(ANSI_BOLD, gettext("action: ")); 8298 printf_color(ANSI_YELLOW, gettext("Export this pool on all " 8299 "systems on which it is imported.\n" 8300 "\tThen import it to correct the mismatch.\n")); 8301 break; 8302 8303 case ZPOOL_STATUS_ERRATA: 8304 printf_color(ANSI_BOLD, gettext("status: ")); 8305 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), 8306 errata); 8307 8308 switch (errata) { 8309 case ZPOOL_ERRATA_NONE: 8310 break; 8311 8312 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 8313 printf_color(ANSI_BOLD, gettext("action: ")); 8314 printf_color(ANSI_YELLOW, gettext("To correct the issue" 8315 " run 'zpool scrub'.\n")); 8316 break; 8317 8318 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 8319 (void) printf(gettext("\tExisting encrypted datasets " 8320 "contain an on-disk incompatibility\n\twhich " 8321 "needs to be corrected.\n")); 8322 printf_color(ANSI_BOLD, gettext("action: ")); 8323 printf_color(ANSI_YELLOW, gettext("To correct the issue" 8324 " backup existing encrypted datasets to new\n\t" 8325 "encrypted datasets and destroy the old ones. " 8326 "'zfs mount -o ro' can\n\tbe used to temporarily " 8327 "mount existing encrypted datasets readonly.\n")); 8328 break; 8329 8330 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 8331 (void) printf(gettext("\tExisting encrypted snapshots " 8332 "and bookmarks contain an on-disk\n\tincompat" 8333 "ibility. This may cause on-disk corruption if " 8334 "they are used\n\twith 'zfs recv'.\n")); 8335 printf_color(ANSI_BOLD, gettext("action: ")); 8336 printf_color(ANSI_YELLOW, gettext("To correct the" 8337 "issue, enable the bookmark_v2 feature. No " 8338 "additional\n\taction is needed if there are no " 8339 "encrypted snapshots or bookmarks.\n\tIf preserving" 8340 "the encrypted snapshots and bookmarks is required," 8341 " use\n\ta non-raw send to backup and restore them." 8342 " Alternately, they may be\n\tremoved to resolve " 8343 "the incompatibility.\n")); 8344 break; 8345 8346 default: 8347 /* 8348 * All errata which allow the pool to be imported 8349 * must contain an action message. 8350 */ 8351 assert(0); 8352 } 8353 break; 8354 8355 default: 8356 /* 8357 * The remaining errors can't actually be generated, yet. 8358 */ 8359 assert(reason == ZPOOL_STATUS_OK); 8360 } 8361 8362 if (msgid != NULL) { 8363 printf(" "); 8364 printf_color(ANSI_BOLD, gettext("see:")); 8365 printf(gettext( 8366 " https://openzfs.github.io/openzfs-docs/msg/%s\n"), 8367 msgid); 8368 } 8369 8370 if (config != NULL) { 8371 uint64_t nerr; 8372 nvlist_t **spares, **l2cache; 8373 uint_t nspares, nl2cache; 8374 pool_checkpoint_stat_t *pcs = NULL; 8375 pool_removal_stat_t *prs = NULL; 8376 8377 print_scan_status(zhp, nvroot); 8378 8379 (void) nvlist_lookup_uint64_array(nvroot, 8380 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 8381 print_removal_status(zhp, prs); 8382 8383 (void) nvlist_lookup_uint64_array(nvroot, 8384 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 8385 print_checkpoint_status(pcs); 8386 8387 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, 8388 cbp->cb_name_flags | VDEV_NAME_TYPE_ID); 8389 if (cbp->cb_namewidth < 10) 8390 cbp->cb_namewidth = 10; 8391 8392 color_start(ANSI_BOLD); 8393 (void) printf(gettext("config:\n\n")); 8394 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), 8395 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", 8396 "CKSUM"); 8397 color_end(); 8398 8399 if (cbp->cb_print_slow_ios) { 8400 printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); 8401 } 8402 8403 if (cbp->vcdl != NULL) 8404 print_cmd_columns(cbp->vcdl, 0); 8405 8406 printf("\n"); 8407 8408 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, 8409 B_FALSE, NULL); 8410 8411 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); 8412 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 8413 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); 8414 8415 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 8416 &l2cache, &nl2cache) == 0) 8417 print_l2cache(zhp, cbp, l2cache, nl2cache); 8418 8419 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 8420 &spares, &nspares) == 0) 8421 print_spares(zhp, cbp, spares, nspares); 8422 8423 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 8424 &nerr) == 0) { 8425 nvlist_t *nverrlist = NULL; 8426 8427 /* 8428 * If the approximate error count is small, get a 8429 * precise count by fetching the entire log and 8430 * uniquifying the results. 8431 */ 8432 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 8433 zpool_get_errlog(zhp, &nverrlist) == 0) { 8434 nvpair_t *elem; 8435 8436 elem = NULL; 8437 nerr = 0; 8438 while ((elem = nvlist_next_nvpair(nverrlist, 8439 elem)) != NULL) { 8440 nerr++; 8441 } 8442 } 8443 nvlist_free(nverrlist); 8444 8445 (void) printf("\n"); 8446 8447 if (nerr == 0) 8448 (void) printf(gettext("errors: No known data " 8449 "errors\n")); 8450 else if (!cbp->cb_verbose) 8451 (void) printf(gettext("errors: %llu data " 8452 "errors, use '-v' for a list\n"), 8453 (u_longlong_t)nerr); 8454 else 8455 print_error_log(zhp); 8456 } 8457 8458 if (cbp->cb_dedup_stats) 8459 print_dedup_stats(config); 8460 } else { 8461 (void) printf(gettext("config: The configuration cannot be " 8462 "determined.\n")); 8463 } 8464 8465 return (0); 8466 } 8467 8468 /* 8469 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ... 8470 * [interval [count]] 8471 * 8472 * -c CMD For each vdev, run command CMD 8473 * -i Display vdev initialization status. 8474 * -g Display guid for individual vdev name. 8475 * -L Follow links when resolving vdev path name. 8476 * -p Display values in parsable (exact) format. 8477 * -P Display full path for vdev name. 8478 * -s Display slow IOs column. 8479 * -v Display complete error logs 8480 * -x Display only pools with potential problems 8481 * -D Display dedup status (undocumented) 8482 * -t Display vdev TRIM status. 8483 * -T Display a timestamp in date(1) or Unix format 8484 * 8485 * Describes the health status of all pools or some subset. 8486 */ 8487 int 8488 zpool_do_status(int argc, char **argv) 8489 { 8490 int c; 8491 int ret; 8492 float interval = 0; 8493 unsigned long count = 0; 8494 status_cbdata_t cb = { 0 }; 8495 char *cmd = NULL; 8496 8497 /* check options */ 8498 while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) { 8499 switch (c) { 8500 case 'c': 8501 if (cmd != NULL) { 8502 fprintf(stderr, 8503 gettext("Can't set -c flag twice\n")); 8504 exit(1); 8505 } 8506 8507 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 8508 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 8509 fprintf(stderr, gettext( 8510 "Can't run -c, disabled by " 8511 "ZPOOL_SCRIPTS_ENABLED.\n")); 8512 exit(1); 8513 } 8514 8515 if ((getuid() <= 0 || geteuid() <= 0) && 8516 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 8517 fprintf(stderr, gettext( 8518 "Can't run -c with root privileges " 8519 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 8520 exit(1); 8521 } 8522 cmd = optarg; 8523 break; 8524 case 'i': 8525 cb.cb_print_vdev_init = B_TRUE; 8526 break; 8527 case 'g': 8528 cb.cb_name_flags |= VDEV_NAME_GUID; 8529 break; 8530 case 'L': 8531 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 8532 break; 8533 case 'p': 8534 cb.cb_literal = B_TRUE; 8535 break; 8536 case 'P': 8537 cb.cb_name_flags |= VDEV_NAME_PATH; 8538 break; 8539 case 's': 8540 cb.cb_print_slow_ios = B_TRUE; 8541 break; 8542 case 'v': 8543 cb.cb_verbose = B_TRUE; 8544 break; 8545 case 'x': 8546 cb.cb_explain = B_TRUE; 8547 break; 8548 case 'D': 8549 cb.cb_dedup_stats = B_TRUE; 8550 break; 8551 case 't': 8552 cb.cb_print_vdev_trim = B_TRUE; 8553 break; 8554 case 'T': 8555 get_timestamp_arg(*optarg); 8556 break; 8557 case '?': 8558 if (optopt == 'c') { 8559 print_zpool_script_list("status"); 8560 exit(0); 8561 } else { 8562 fprintf(stderr, 8563 gettext("invalid option '%c'\n"), optopt); 8564 } 8565 usage(B_FALSE); 8566 } 8567 } 8568 8569 argc -= optind; 8570 argv += optind; 8571 8572 get_interval_count(&argc, argv, &interval, &count); 8573 8574 if (argc == 0) 8575 cb.cb_allpools = B_TRUE; 8576 8577 cb.cb_first = B_TRUE; 8578 cb.cb_print_status = B_TRUE; 8579 8580 for (;;) { 8581 if (timestamp_fmt != NODATE) 8582 print_timestamp(timestamp_fmt); 8583 8584 if (cmd != NULL) 8585 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, 8586 NULL, NULL, 0, 0); 8587 8588 ret = for_each_pool(argc, argv, B_TRUE, NULL, cb.cb_literal, 8589 status_callback, &cb); 8590 8591 if (cb.vcdl != NULL) 8592 free_vdev_cmd_data_list(cb.vcdl); 8593 8594 if (argc == 0 && cb.cb_count == 0) 8595 (void) fprintf(stderr, gettext("no pools available\n")); 8596 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 8597 (void) printf(gettext("all pools are healthy\n")); 8598 8599 if (ret != 0) 8600 return (ret); 8601 8602 if (interval == 0) 8603 break; 8604 8605 if (count != 0 && --count == 0) 8606 break; 8607 8608 (void) fsleep(interval); 8609 } 8610 8611 return (0); 8612 } 8613 8614 typedef struct upgrade_cbdata { 8615 int cb_first; 8616 int cb_argc; 8617 uint64_t cb_version; 8618 char **cb_argv; 8619 } upgrade_cbdata_t; 8620 8621 static int 8622 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) 8623 { 8624 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8625 int *count = (int *)unsupp_fs; 8626 8627 if (zfs_version > ZPL_VERSION) { 8628 (void) printf(gettext("%s (v%d) is not supported by this " 8629 "implementation of ZFS.\n"), 8630 zfs_get_name(zhp), zfs_version); 8631 (*count)++; 8632 } 8633 8634 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs); 8635 8636 zfs_close(zhp); 8637 8638 return (0); 8639 } 8640 8641 static int 8642 upgrade_version(zpool_handle_t *zhp, uint64_t version) 8643 { 8644 int ret; 8645 nvlist_t *config; 8646 uint64_t oldversion; 8647 int unsupp_fs = 0; 8648 8649 config = zpool_get_config(zhp, NULL); 8650 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8651 &oldversion) == 0); 8652 8653 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 8654 assert(oldversion < version); 8655 8656 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); 8657 if (ret != 0) 8658 return (ret); 8659 8660 if (unsupp_fs) { 8661 (void) fprintf(stderr, gettext("Upgrade not performed due " 8662 "to %d unsupported filesystems (max v%d).\n"), 8663 unsupp_fs, (int)ZPL_VERSION); 8664 return (1); 8665 } 8666 8667 ret = zpool_upgrade(zhp, version); 8668 if (ret != 0) 8669 return (ret); 8670 8671 if (version >= SPA_VERSION_FEATURES) { 8672 (void) printf(gettext("Successfully upgraded " 8673 "'%s' from version %llu to feature flags.\n"), 8674 zpool_get_name(zhp), (u_longlong_t)oldversion); 8675 } else { 8676 (void) printf(gettext("Successfully upgraded " 8677 "'%s' from version %llu to version %llu.\n"), 8678 zpool_get_name(zhp), (u_longlong_t)oldversion, 8679 (u_longlong_t)version); 8680 } 8681 8682 return (0); 8683 } 8684 8685 static int 8686 upgrade_enable_all(zpool_handle_t *zhp, int *countp) 8687 { 8688 int i, ret, count; 8689 boolean_t firstff = B_TRUE; 8690 nvlist_t *enabled = zpool_get_features(zhp); 8691 8692 char compat[ZFS_MAXPROPLEN]; 8693 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 8694 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 8695 compat[0] = '\0'; 8696 8697 boolean_t requested_features[SPA_FEATURES]; 8698 if (zpool_do_load_compat(compat, requested_features) != 8699 ZPOOL_COMPATIBILITY_OK) 8700 return (-1); 8701 8702 count = 0; 8703 for (i = 0; i < SPA_FEATURES; i++) { 8704 const char *fname = spa_feature_table[i].fi_uname; 8705 const char *fguid = spa_feature_table[i].fi_guid; 8706 8707 if (!spa_feature_table[i].fi_zfs_mod_supported) 8708 continue; 8709 8710 if (!nvlist_exists(enabled, fguid) && requested_features[i]) { 8711 char *propname; 8712 verify(-1 != asprintf(&propname, "feature@%s", fname)); 8713 ret = zpool_set_prop(zhp, propname, 8714 ZFS_FEATURE_ENABLED); 8715 if (ret != 0) { 8716 free(propname); 8717 return (ret); 8718 } 8719 count++; 8720 8721 if (firstff) { 8722 (void) printf(gettext("Enabled the " 8723 "following features on '%s':\n"), 8724 zpool_get_name(zhp)); 8725 firstff = B_FALSE; 8726 } 8727 (void) printf(gettext(" %s\n"), fname); 8728 free(propname); 8729 } 8730 } 8731 8732 if (countp != NULL) 8733 *countp = count; 8734 return (0); 8735 } 8736 8737 static int 8738 upgrade_cb(zpool_handle_t *zhp, void *arg) 8739 { 8740 upgrade_cbdata_t *cbp = arg; 8741 nvlist_t *config; 8742 uint64_t version; 8743 boolean_t printnl = B_FALSE; 8744 int ret; 8745 8746 config = zpool_get_config(zhp, NULL); 8747 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8748 &version) == 0); 8749 8750 assert(SPA_VERSION_IS_SUPPORTED(version)); 8751 8752 if (version < cbp->cb_version) { 8753 cbp->cb_first = B_FALSE; 8754 ret = upgrade_version(zhp, cbp->cb_version); 8755 if (ret != 0) 8756 return (ret); 8757 printnl = B_TRUE; 8758 8759 /* 8760 * If they did "zpool upgrade -a", then we could 8761 * be doing ioctls to different pools. We need 8762 * to log this history once to each pool, and bypass 8763 * the normal history logging that happens in main(). 8764 */ 8765 (void) zpool_log_history(g_zfs, history_str); 8766 log_history = B_FALSE; 8767 } 8768 8769 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 8770 int count; 8771 ret = upgrade_enable_all(zhp, &count); 8772 if (ret != 0) 8773 return (ret); 8774 8775 if (count > 0) { 8776 cbp->cb_first = B_FALSE; 8777 printnl = B_TRUE; 8778 } 8779 } 8780 8781 if (printnl) { 8782 (void) printf(gettext("\n")); 8783 } 8784 8785 return (0); 8786 } 8787 8788 static int 8789 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 8790 { 8791 upgrade_cbdata_t *cbp = arg; 8792 nvlist_t *config; 8793 uint64_t version; 8794 8795 config = zpool_get_config(zhp, NULL); 8796 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8797 &version) == 0); 8798 8799 assert(SPA_VERSION_IS_SUPPORTED(version)); 8800 8801 if (version < SPA_VERSION_FEATURES) { 8802 if (cbp->cb_first) { 8803 (void) printf(gettext("The following pools are " 8804 "formatted with legacy version numbers and can\n" 8805 "be upgraded to use feature flags. After " 8806 "being upgraded, these pools\nwill no " 8807 "longer be accessible by software that does not " 8808 "support feature\nflags.\n\n")); 8809 (void) printf(gettext("VER POOL\n")); 8810 (void) printf(gettext("--- ------------\n")); 8811 cbp->cb_first = B_FALSE; 8812 } 8813 8814 (void) printf("%2llu %s\n", (u_longlong_t)version, 8815 zpool_get_name(zhp)); 8816 } 8817 8818 return (0); 8819 } 8820 8821 static int 8822 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 8823 { 8824 upgrade_cbdata_t *cbp = arg; 8825 nvlist_t *config; 8826 uint64_t version; 8827 8828 config = zpool_get_config(zhp, NULL); 8829 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8830 &version) == 0); 8831 8832 if (version >= SPA_VERSION_FEATURES) { 8833 int i; 8834 boolean_t poolfirst = B_TRUE; 8835 nvlist_t *enabled = zpool_get_features(zhp); 8836 8837 for (i = 0; i < SPA_FEATURES; i++) { 8838 const char *fguid = spa_feature_table[i].fi_guid; 8839 const char *fname = spa_feature_table[i].fi_uname; 8840 8841 if (!spa_feature_table[i].fi_zfs_mod_supported) 8842 continue; 8843 8844 if (!nvlist_exists(enabled, fguid)) { 8845 if (cbp->cb_first) { 8846 (void) printf(gettext("\nSome " 8847 "supported features are not " 8848 "enabled on the following pools. " 8849 "Once a\nfeature is enabled the " 8850 "pool may become incompatible with " 8851 "software\nthat does not support " 8852 "the feature. See " 8853 "zpool-features(5) for " 8854 "details.\n\n")); 8855 (void) printf(gettext("POOL " 8856 "FEATURE\n")); 8857 (void) printf(gettext("------" 8858 "---------\n")); 8859 cbp->cb_first = B_FALSE; 8860 } 8861 8862 if (poolfirst) { 8863 (void) printf(gettext("%s\n"), 8864 zpool_get_name(zhp)); 8865 poolfirst = B_FALSE; 8866 } 8867 8868 (void) printf(gettext(" %s\n"), fname); 8869 } 8870 /* 8871 * If they did "zpool upgrade -a", then we could 8872 * be doing ioctls to different pools. We need 8873 * to log this history once to each pool, and bypass 8874 * the normal history logging that happens in main(). 8875 */ 8876 (void) zpool_log_history(g_zfs, history_str); 8877 log_history = B_FALSE; 8878 } 8879 } 8880 8881 return (0); 8882 } 8883 8884 /* ARGSUSED */ 8885 static int 8886 upgrade_one(zpool_handle_t *zhp, void *data) 8887 { 8888 boolean_t printnl = B_FALSE; 8889 upgrade_cbdata_t *cbp = data; 8890 uint64_t cur_version; 8891 int ret; 8892 8893 if (strcmp("log", zpool_get_name(zhp)) == 0) { 8894 (void) fprintf(stderr, gettext("'log' is now a reserved word\n" 8895 "Pool 'log' must be renamed using export and import" 8896 " to upgrade.\n")); 8897 return (1); 8898 } 8899 8900 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 8901 if (cur_version > cbp->cb_version) { 8902 (void) printf(gettext("Pool '%s' is already formatted " 8903 "using more current version '%llu'.\n\n"), 8904 zpool_get_name(zhp), (u_longlong_t)cur_version); 8905 return (0); 8906 } 8907 8908 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 8909 (void) printf(gettext("Pool '%s' is already formatted " 8910 "using version %llu.\n\n"), zpool_get_name(zhp), 8911 (u_longlong_t)cbp->cb_version); 8912 return (0); 8913 } 8914 8915 if (cur_version != cbp->cb_version) { 8916 printnl = B_TRUE; 8917 ret = upgrade_version(zhp, cbp->cb_version); 8918 if (ret != 0) 8919 return (ret); 8920 } 8921 8922 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 8923 int count = 0; 8924 ret = upgrade_enable_all(zhp, &count); 8925 if (ret != 0) 8926 return (ret); 8927 8928 if (count != 0) { 8929 printnl = B_TRUE; 8930 } else if (cur_version == SPA_VERSION) { 8931 (void) printf(gettext("Pool '%s' already has all " 8932 "supported and requested features enabled.\n"), 8933 zpool_get_name(zhp)); 8934 } 8935 } 8936 8937 if (printnl) { 8938 (void) printf(gettext("\n")); 8939 } 8940 8941 return (0); 8942 } 8943 8944 /* 8945 * zpool upgrade 8946 * zpool upgrade -v 8947 * zpool upgrade [-V version] <-a | pool ...> 8948 * 8949 * With no arguments, display downrev'd ZFS pool available for upgrade. 8950 * Individual pools can be upgraded by specifying the pool, and '-a' will 8951 * upgrade all pools. 8952 */ 8953 int 8954 zpool_do_upgrade(int argc, char **argv) 8955 { 8956 int c; 8957 upgrade_cbdata_t cb = { 0 }; 8958 int ret = 0; 8959 boolean_t showversions = B_FALSE; 8960 boolean_t upgradeall = B_FALSE; 8961 char *end; 8962 8963 8964 /* check options */ 8965 while ((c = getopt(argc, argv, ":avV:")) != -1) { 8966 switch (c) { 8967 case 'a': 8968 upgradeall = B_TRUE; 8969 break; 8970 case 'v': 8971 showversions = B_TRUE; 8972 break; 8973 case 'V': 8974 cb.cb_version = strtoll(optarg, &end, 10); 8975 if (*end != '\0' || 8976 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 8977 (void) fprintf(stderr, 8978 gettext("invalid version '%s'\n"), optarg); 8979 usage(B_FALSE); 8980 } 8981 break; 8982 case ':': 8983 (void) fprintf(stderr, gettext("missing argument for " 8984 "'%c' option\n"), optopt); 8985 usage(B_FALSE); 8986 break; 8987 case '?': 8988 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8989 optopt); 8990 usage(B_FALSE); 8991 } 8992 } 8993 8994 cb.cb_argc = argc; 8995 cb.cb_argv = argv; 8996 argc -= optind; 8997 argv += optind; 8998 8999 if (cb.cb_version == 0) { 9000 cb.cb_version = SPA_VERSION; 9001 } else if (!upgradeall && argc == 0) { 9002 (void) fprintf(stderr, gettext("-V option is " 9003 "incompatible with other arguments\n")); 9004 usage(B_FALSE); 9005 } 9006 9007 if (showversions) { 9008 if (upgradeall || argc != 0) { 9009 (void) fprintf(stderr, gettext("-v option is " 9010 "incompatible with other arguments\n")); 9011 usage(B_FALSE); 9012 } 9013 } else if (upgradeall) { 9014 if (argc != 0) { 9015 (void) fprintf(stderr, gettext("-a option should not " 9016 "be used along with a pool name\n")); 9017 usage(B_FALSE); 9018 } 9019 } 9020 9021 (void) printf(gettext("This system supports ZFS pool feature " 9022 "flags.\n\n")); 9023 if (showversions) { 9024 int i; 9025 9026 (void) printf(gettext("The following features are " 9027 "supported:\n\n")); 9028 (void) printf(gettext("FEAT DESCRIPTION\n")); 9029 (void) printf("----------------------------------------------" 9030 "---------------\n"); 9031 for (i = 0; i < SPA_FEATURES; i++) { 9032 zfeature_info_t *fi = &spa_feature_table[i]; 9033 if (!fi->fi_zfs_mod_supported) 9034 continue; 9035 const char *ro = 9036 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 9037 " (read-only compatible)" : ""; 9038 9039 (void) printf("%-37s%s\n", fi->fi_uname, ro); 9040 (void) printf(" %s\n", fi->fi_desc); 9041 } 9042 (void) printf("\n"); 9043 9044 (void) printf(gettext("The following legacy versions are also " 9045 "supported:\n\n")); 9046 (void) printf(gettext("VER DESCRIPTION\n")); 9047 (void) printf("--- -----------------------------------------" 9048 "---------------\n"); 9049 (void) printf(gettext(" 1 Initial ZFS version\n")); 9050 (void) printf(gettext(" 2 Ditto blocks " 9051 "(replicated metadata)\n")); 9052 (void) printf(gettext(" 3 Hot spares and double parity " 9053 "RAID-Z\n")); 9054 (void) printf(gettext(" 4 zpool history\n")); 9055 (void) printf(gettext(" 5 Compression using the gzip " 9056 "algorithm\n")); 9057 (void) printf(gettext(" 6 bootfs pool property\n")); 9058 (void) printf(gettext(" 7 Separate intent log devices\n")); 9059 (void) printf(gettext(" 8 Delegated administration\n")); 9060 (void) printf(gettext(" 9 refquota and refreservation " 9061 "properties\n")); 9062 (void) printf(gettext(" 10 Cache devices\n")); 9063 (void) printf(gettext(" 11 Improved scrub performance\n")); 9064 (void) printf(gettext(" 12 Snapshot properties\n")); 9065 (void) printf(gettext(" 13 snapused property\n")); 9066 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 9067 (void) printf(gettext(" 15 user/group space accounting\n")); 9068 (void) printf(gettext(" 16 stmf property support\n")); 9069 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 9070 (void) printf(gettext(" 18 Snapshot user holds\n")); 9071 (void) printf(gettext(" 19 Log device removal\n")); 9072 (void) printf(gettext(" 20 Compression using zle " 9073 "(zero-length encoding)\n")); 9074 (void) printf(gettext(" 21 Deduplication\n")); 9075 (void) printf(gettext(" 22 Received properties\n")); 9076 (void) printf(gettext(" 23 Slim ZIL\n")); 9077 (void) printf(gettext(" 24 System attributes\n")); 9078 (void) printf(gettext(" 25 Improved scrub stats\n")); 9079 (void) printf(gettext(" 26 Improved snapshot deletion " 9080 "performance\n")); 9081 (void) printf(gettext(" 27 Improved snapshot creation " 9082 "performance\n")); 9083 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 9084 (void) printf(gettext("\nFor more information on a particular " 9085 "version, including supported releases,\n")); 9086 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 9087 } else if (argc == 0 && upgradeall) { 9088 cb.cb_first = B_TRUE; 9089 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 9090 if (ret == 0 && cb.cb_first) { 9091 if (cb.cb_version == SPA_VERSION) { 9092 (void) printf(gettext("All pools are already " 9093 "formatted using feature flags.\n\n")); 9094 (void) printf(gettext("Every feature flags " 9095 "pool already has all supported and " 9096 "requested features enabled.\n")); 9097 } else { 9098 (void) printf(gettext("All pools are already " 9099 "formatted with version %llu or higher.\n"), 9100 (u_longlong_t)cb.cb_version); 9101 } 9102 } 9103 } else if (argc == 0) { 9104 cb.cb_first = B_TRUE; 9105 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 9106 assert(ret == 0); 9107 9108 if (cb.cb_first) { 9109 (void) printf(gettext("All pools are formatted " 9110 "using feature flags.\n\n")); 9111 } else { 9112 (void) printf(gettext("\nUse 'zpool upgrade -v' " 9113 "for a list of available legacy versions.\n")); 9114 } 9115 9116 cb.cb_first = B_TRUE; 9117 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 9118 assert(ret == 0); 9119 9120 if (cb.cb_first) { 9121 (void) printf(gettext("Every feature flags pool has " 9122 "all supported and requested features enabled.\n")); 9123 } else { 9124 (void) printf(gettext("\n")); 9125 } 9126 } else { 9127 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, 9128 upgrade_one, &cb); 9129 } 9130 9131 return (ret); 9132 } 9133 9134 typedef struct hist_cbdata { 9135 boolean_t first; 9136 boolean_t longfmt; 9137 boolean_t internal; 9138 } hist_cbdata_t; 9139 9140 static void 9141 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) 9142 { 9143 nvlist_t **records; 9144 uint_t numrecords; 9145 int i; 9146 9147 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 9148 &records, &numrecords) == 0); 9149 for (i = 0; i < numrecords; i++) { 9150 nvlist_t *rec = records[i]; 9151 char tbuf[64] = ""; 9152 9153 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 9154 time_t tsec; 9155 struct tm t; 9156 9157 tsec = fnvlist_lookup_uint64(records[i], 9158 ZPOOL_HIST_TIME); 9159 (void) localtime_r(&tsec, &t); 9160 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 9161 } 9162 9163 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { 9164 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], 9165 ZPOOL_HIST_ELAPSED_NS); 9166 (void) snprintf(tbuf + strlen(tbuf), 9167 sizeof (tbuf) - strlen(tbuf), 9168 " (%lldms)", (long long)elapsed_ns / 1000 / 1000); 9169 } 9170 9171 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 9172 (void) printf("%s %s", tbuf, 9173 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 9174 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 9175 int ievent = 9176 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 9177 if (!cb->internal) 9178 continue; 9179 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 9180 (void) printf("%s unrecognized record:\n", 9181 tbuf); 9182 dump_nvlist(rec, 4); 9183 continue; 9184 } 9185 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 9186 zfs_history_event_names[ievent], 9187 (longlong_t)fnvlist_lookup_uint64( 9188 rec, ZPOOL_HIST_TXG), 9189 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 9190 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 9191 if (!cb->internal) 9192 continue; 9193 (void) printf("%s [txg:%lld] %s", tbuf, 9194 (longlong_t)fnvlist_lookup_uint64( 9195 rec, ZPOOL_HIST_TXG), 9196 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 9197 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 9198 (void) printf(" %s (%llu)", 9199 fnvlist_lookup_string(rec, 9200 ZPOOL_HIST_DSNAME), 9201 (u_longlong_t)fnvlist_lookup_uint64(rec, 9202 ZPOOL_HIST_DSID)); 9203 } 9204 (void) printf(" %s", fnvlist_lookup_string(rec, 9205 ZPOOL_HIST_INT_STR)); 9206 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 9207 if (!cb->internal) 9208 continue; 9209 (void) printf("%s ioctl %s\n", tbuf, 9210 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 9211 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 9212 (void) printf(" input:\n"); 9213 dump_nvlist(fnvlist_lookup_nvlist(rec, 9214 ZPOOL_HIST_INPUT_NVL), 8); 9215 } 9216 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 9217 (void) printf(" output:\n"); 9218 dump_nvlist(fnvlist_lookup_nvlist(rec, 9219 ZPOOL_HIST_OUTPUT_NVL), 8); 9220 } 9221 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { 9222 (void) printf(" output nvlist omitted; " 9223 "original size: %lldKB\n", 9224 (longlong_t)fnvlist_lookup_int64(rec, 9225 ZPOOL_HIST_OUTPUT_SIZE) / 1024); 9226 } 9227 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { 9228 (void) printf(" errno: %lld\n", 9229 (longlong_t)fnvlist_lookup_int64(rec, 9230 ZPOOL_HIST_ERRNO)); 9231 } 9232 } else { 9233 if (!cb->internal) 9234 continue; 9235 (void) printf("%s unrecognized record:\n", tbuf); 9236 dump_nvlist(rec, 4); 9237 } 9238 9239 if (!cb->longfmt) { 9240 (void) printf("\n"); 9241 continue; 9242 } 9243 (void) printf(" ["); 9244 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 9245 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 9246 struct passwd *pwd = getpwuid(who); 9247 (void) printf("user %d ", (int)who); 9248 if (pwd != NULL) 9249 (void) printf("(%s) ", pwd->pw_name); 9250 } 9251 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 9252 (void) printf("on %s", 9253 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 9254 } 9255 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 9256 (void) printf(":%s", 9257 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 9258 } 9259 9260 (void) printf("]"); 9261 (void) printf("\n"); 9262 } 9263 } 9264 9265 /* 9266 * Print out the command history for a specific pool. 9267 */ 9268 static int 9269 get_history_one(zpool_handle_t *zhp, void *data) 9270 { 9271 nvlist_t *nvhis; 9272 int ret; 9273 hist_cbdata_t *cb = (hist_cbdata_t *)data; 9274 uint64_t off = 0; 9275 boolean_t eof = B_FALSE; 9276 9277 cb->first = B_FALSE; 9278 9279 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 9280 9281 while (!eof) { 9282 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) 9283 return (ret); 9284 9285 print_history_records(nvhis, cb); 9286 nvlist_free(nvhis); 9287 } 9288 (void) printf("\n"); 9289 9290 return (ret); 9291 } 9292 9293 /* 9294 * zpool history <pool> 9295 * 9296 * Displays the history of commands that modified pools. 9297 */ 9298 int 9299 zpool_do_history(int argc, char **argv) 9300 { 9301 hist_cbdata_t cbdata = { 0 }; 9302 int ret; 9303 int c; 9304 9305 cbdata.first = B_TRUE; 9306 /* check options */ 9307 while ((c = getopt(argc, argv, "li")) != -1) { 9308 switch (c) { 9309 case 'l': 9310 cbdata.longfmt = B_TRUE; 9311 break; 9312 case 'i': 9313 cbdata.internal = B_TRUE; 9314 break; 9315 case '?': 9316 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9317 optopt); 9318 usage(B_FALSE); 9319 } 9320 } 9321 argc -= optind; 9322 argv += optind; 9323 9324 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, get_history_one, 9325 &cbdata); 9326 9327 if (argc == 0 && cbdata.first == B_TRUE) { 9328 (void) fprintf(stderr, gettext("no pools available\n")); 9329 return (0); 9330 } 9331 9332 return (ret); 9333 } 9334 9335 typedef struct ev_opts { 9336 int verbose; 9337 int scripted; 9338 int follow; 9339 int clear; 9340 char poolname[ZFS_MAX_DATASET_NAME_LEN]; 9341 } ev_opts_t; 9342 9343 static void 9344 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) 9345 { 9346 char ctime_str[26], str[32], *ptr; 9347 int64_t *tv; 9348 uint_t n; 9349 9350 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); 9351 memset(str, ' ', 32); 9352 (void) ctime_r((const time_t *)&tv[0], ctime_str); 9353 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ 9354 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ 9355 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ 9356 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ 9357 if (opts->scripted) 9358 (void) printf(gettext("%s\t"), str); 9359 else 9360 (void) printf(gettext("%s "), str); 9361 9362 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); 9363 (void) printf(gettext("%s\n"), ptr); 9364 } 9365 9366 static void 9367 zpool_do_events_nvprint(nvlist_t *nvl, int depth) 9368 { 9369 nvpair_t *nvp; 9370 9371 for (nvp = nvlist_next_nvpair(nvl, NULL); 9372 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { 9373 9374 data_type_t type = nvpair_type(nvp); 9375 const char *name = nvpair_name(nvp); 9376 9377 boolean_t b; 9378 uint8_t i8; 9379 uint16_t i16; 9380 uint32_t i32; 9381 uint64_t i64; 9382 char *str; 9383 nvlist_t *cnv; 9384 9385 printf(gettext("%*s%s = "), depth, "", name); 9386 9387 switch (type) { 9388 case DATA_TYPE_BOOLEAN: 9389 printf(gettext("%s"), "1"); 9390 break; 9391 9392 case DATA_TYPE_BOOLEAN_VALUE: 9393 (void) nvpair_value_boolean_value(nvp, &b); 9394 printf(gettext("%s"), b ? "1" : "0"); 9395 break; 9396 9397 case DATA_TYPE_BYTE: 9398 (void) nvpair_value_byte(nvp, &i8); 9399 printf(gettext("0x%x"), i8); 9400 break; 9401 9402 case DATA_TYPE_INT8: 9403 (void) nvpair_value_int8(nvp, (void *)&i8); 9404 printf(gettext("0x%x"), i8); 9405 break; 9406 9407 case DATA_TYPE_UINT8: 9408 (void) nvpair_value_uint8(nvp, &i8); 9409 printf(gettext("0x%x"), i8); 9410 break; 9411 9412 case DATA_TYPE_INT16: 9413 (void) nvpair_value_int16(nvp, (void *)&i16); 9414 printf(gettext("0x%x"), i16); 9415 break; 9416 9417 case DATA_TYPE_UINT16: 9418 (void) nvpair_value_uint16(nvp, &i16); 9419 printf(gettext("0x%x"), i16); 9420 break; 9421 9422 case DATA_TYPE_INT32: 9423 (void) nvpair_value_int32(nvp, (void *)&i32); 9424 printf(gettext("0x%x"), i32); 9425 break; 9426 9427 case DATA_TYPE_UINT32: 9428 (void) nvpair_value_uint32(nvp, &i32); 9429 printf(gettext("0x%x"), i32); 9430 break; 9431 9432 case DATA_TYPE_INT64: 9433 (void) nvpair_value_int64(nvp, (void *)&i64); 9434 printf(gettext("0x%llx"), (u_longlong_t)i64); 9435 break; 9436 9437 case DATA_TYPE_UINT64: 9438 (void) nvpair_value_uint64(nvp, &i64); 9439 /* 9440 * translate vdev state values to readable 9441 * strings to aide zpool events consumers 9442 */ 9443 if (strcmp(name, 9444 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || 9445 strcmp(name, 9446 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { 9447 printf(gettext("\"%s\" (0x%llx)"), 9448 zpool_state_to_name(i64, VDEV_AUX_NONE), 9449 (u_longlong_t)i64); 9450 } else { 9451 printf(gettext("0x%llx"), (u_longlong_t)i64); 9452 } 9453 break; 9454 9455 case DATA_TYPE_HRTIME: 9456 (void) nvpair_value_hrtime(nvp, (void *)&i64); 9457 printf(gettext("0x%llx"), (u_longlong_t)i64); 9458 break; 9459 9460 case DATA_TYPE_STRING: 9461 (void) nvpair_value_string(nvp, &str); 9462 printf(gettext("\"%s\""), str ? str : "<NULL>"); 9463 break; 9464 9465 case DATA_TYPE_NVLIST: 9466 printf(gettext("(embedded nvlist)\n")); 9467 (void) nvpair_value_nvlist(nvp, &cnv); 9468 zpool_do_events_nvprint(cnv, depth + 8); 9469 printf(gettext("%*s(end %s)"), depth, "", name); 9470 break; 9471 9472 case DATA_TYPE_NVLIST_ARRAY: { 9473 nvlist_t **val; 9474 uint_t i, nelem; 9475 9476 (void) nvpair_value_nvlist_array(nvp, &val, &nelem); 9477 printf(gettext("(%d embedded nvlists)\n"), nelem); 9478 for (i = 0; i < nelem; i++) { 9479 printf(gettext("%*s%s[%d] = %s\n"), 9480 depth, "", name, i, "(embedded nvlist)"); 9481 zpool_do_events_nvprint(val[i], depth + 8); 9482 printf(gettext("%*s(end %s[%i])\n"), 9483 depth, "", name, i); 9484 } 9485 printf(gettext("%*s(end %s)\n"), depth, "", name); 9486 } 9487 break; 9488 9489 case DATA_TYPE_INT8_ARRAY: { 9490 int8_t *val; 9491 uint_t i, nelem; 9492 9493 (void) nvpair_value_int8_array(nvp, &val, &nelem); 9494 for (i = 0; i < nelem; i++) 9495 printf(gettext("0x%x "), val[i]); 9496 9497 break; 9498 } 9499 9500 case DATA_TYPE_UINT8_ARRAY: { 9501 uint8_t *val; 9502 uint_t i, nelem; 9503 9504 (void) nvpair_value_uint8_array(nvp, &val, &nelem); 9505 for (i = 0; i < nelem; i++) 9506 printf(gettext("0x%x "), val[i]); 9507 9508 break; 9509 } 9510 9511 case DATA_TYPE_INT16_ARRAY: { 9512 int16_t *val; 9513 uint_t i, nelem; 9514 9515 (void) nvpair_value_int16_array(nvp, &val, &nelem); 9516 for (i = 0; i < nelem; i++) 9517 printf(gettext("0x%x "), val[i]); 9518 9519 break; 9520 } 9521 9522 case DATA_TYPE_UINT16_ARRAY: { 9523 uint16_t *val; 9524 uint_t i, nelem; 9525 9526 (void) nvpair_value_uint16_array(nvp, &val, &nelem); 9527 for (i = 0; i < nelem; i++) 9528 printf(gettext("0x%x "), val[i]); 9529 9530 break; 9531 } 9532 9533 case DATA_TYPE_INT32_ARRAY: { 9534 int32_t *val; 9535 uint_t i, nelem; 9536 9537 (void) nvpair_value_int32_array(nvp, &val, &nelem); 9538 for (i = 0; i < nelem; i++) 9539 printf(gettext("0x%x "), val[i]); 9540 9541 break; 9542 } 9543 9544 case DATA_TYPE_UINT32_ARRAY: { 9545 uint32_t *val; 9546 uint_t i, nelem; 9547 9548 (void) nvpair_value_uint32_array(nvp, &val, &nelem); 9549 for (i = 0; i < nelem; i++) 9550 printf(gettext("0x%x "), val[i]); 9551 9552 break; 9553 } 9554 9555 case DATA_TYPE_INT64_ARRAY: { 9556 int64_t *val; 9557 uint_t i, nelem; 9558 9559 (void) nvpair_value_int64_array(nvp, &val, &nelem); 9560 for (i = 0; i < nelem; i++) 9561 printf(gettext("0x%llx "), 9562 (u_longlong_t)val[i]); 9563 9564 break; 9565 } 9566 9567 case DATA_TYPE_UINT64_ARRAY: { 9568 uint64_t *val; 9569 uint_t i, nelem; 9570 9571 (void) nvpair_value_uint64_array(nvp, &val, &nelem); 9572 for (i = 0; i < nelem; i++) 9573 printf(gettext("0x%llx "), 9574 (u_longlong_t)val[i]); 9575 9576 break; 9577 } 9578 9579 case DATA_TYPE_STRING_ARRAY: { 9580 char **str; 9581 uint_t i, nelem; 9582 9583 (void) nvpair_value_string_array(nvp, &str, &nelem); 9584 for (i = 0; i < nelem; i++) 9585 printf(gettext("\"%s\" "), 9586 str[i] ? str[i] : "<NULL>"); 9587 9588 break; 9589 } 9590 9591 case DATA_TYPE_BOOLEAN_ARRAY: 9592 case DATA_TYPE_BYTE_ARRAY: 9593 case DATA_TYPE_DOUBLE: 9594 case DATA_TYPE_DONTCARE: 9595 case DATA_TYPE_UNKNOWN: 9596 printf(gettext("<unknown>")); 9597 break; 9598 } 9599 9600 printf(gettext("\n")); 9601 } 9602 } 9603 9604 static int 9605 zpool_do_events_next(ev_opts_t *opts) 9606 { 9607 nvlist_t *nvl; 9608 int zevent_fd, ret, dropped; 9609 char *pool; 9610 9611 zevent_fd = open(ZFS_DEV, O_RDWR); 9612 VERIFY(zevent_fd >= 0); 9613 9614 if (!opts->scripted) 9615 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); 9616 9617 while (1) { 9618 ret = zpool_events_next(g_zfs, &nvl, &dropped, 9619 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); 9620 if (ret || nvl == NULL) 9621 break; 9622 9623 if (dropped > 0) 9624 (void) printf(gettext("dropped %d events\n"), dropped); 9625 9626 if (strlen(opts->poolname) > 0 && 9627 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && 9628 strcmp(opts->poolname, pool) != 0) 9629 continue; 9630 9631 zpool_do_events_short(nvl, opts); 9632 9633 if (opts->verbose) { 9634 zpool_do_events_nvprint(nvl, 8); 9635 printf(gettext("\n")); 9636 } 9637 (void) fflush(stdout); 9638 9639 nvlist_free(nvl); 9640 } 9641 9642 VERIFY(0 == close(zevent_fd)); 9643 9644 return (ret); 9645 } 9646 9647 static int 9648 zpool_do_events_clear(ev_opts_t *opts) 9649 { 9650 int count, ret; 9651 9652 ret = zpool_events_clear(g_zfs, &count); 9653 if (!ret) 9654 (void) printf(gettext("cleared %d events\n"), count); 9655 9656 return (ret); 9657 } 9658 9659 /* 9660 * zpool events [-vHf [pool] | -c] 9661 * 9662 * Displays events logs by ZFS. 9663 */ 9664 int 9665 zpool_do_events(int argc, char **argv) 9666 { 9667 ev_opts_t opts = { 0 }; 9668 int ret; 9669 int c; 9670 9671 /* check options */ 9672 while ((c = getopt(argc, argv, "vHfc")) != -1) { 9673 switch (c) { 9674 case 'v': 9675 opts.verbose = 1; 9676 break; 9677 case 'H': 9678 opts.scripted = 1; 9679 break; 9680 case 'f': 9681 opts.follow = 1; 9682 break; 9683 case 'c': 9684 opts.clear = 1; 9685 break; 9686 case '?': 9687 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9688 optopt); 9689 usage(B_FALSE); 9690 } 9691 } 9692 argc -= optind; 9693 argv += optind; 9694 9695 if (argc > 1) { 9696 (void) fprintf(stderr, gettext("too many arguments\n")); 9697 usage(B_FALSE); 9698 } else if (argc == 1) { 9699 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); 9700 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { 9701 (void) fprintf(stderr, 9702 gettext("invalid pool name '%s'\n"), opts.poolname); 9703 usage(B_FALSE); 9704 } 9705 } 9706 9707 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && 9708 opts.clear) { 9709 (void) fprintf(stderr, 9710 gettext("invalid options combined with -c\n")); 9711 usage(B_FALSE); 9712 } 9713 9714 if (opts.clear) 9715 ret = zpool_do_events_clear(&opts); 9716 else 9717 ret = zpool_do_events_next(&opts); 9718 9719 return (ret); 9720 } 9721 9722 static int 9723 get_callback(zpool_handle_t *zhp, void *data) 9724 { 9725 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 9726 char value[MAXNAMELEN]; 9727 zprop_source_t srctype; 9728 zprop_list_t *pl; 9729 9730 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 9731 9732 /* 9733 * Skip the special fake placeholder. This will also skip 9734 * over the name property when 'all' is specified. 9735 */ 9736 if (pl->pl_prop == ZPOOL_PROP_NAME && 9737 pl == cbp->cb_proplist) 9738 continue; 9739 9740 if (pl->pl_prop == ZPROP_INVAL && 9741 (zpool_prop_feature(pl->pl_user_prop) || 9742 zpool_prop_unsupported(pl->pl_user_prop))) { 9743 srctype = ZPROP_SRC_LOCAL; 9744 9745 if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 9746 value, sizeof (value)) == 0) { 9747 zprop_print_one_property(zpool_get_name(zhp), 9748 cbp, pl->pl_user_prop, value, srctype, 9749 NULL, NULL); 9750 } 9751 } else { 9752 if (zpool_get_prop(zhp, pl->pl_prop, value, 9753 sizeof (value), &srctype, cbp->cb_literal) != 0) 9754 continue; 9755 9756 zprop_print_one_property(zpool_get_name(zhp), cbp, 9757 zpool_prop_to_name(pl->pl_prop), value, srctype, 9758 NULL, NULL); 9759 } 9760 } 9761 return (0); 9762 } 9763 9764 /* 9765 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 9766 * 9767 * -H Scripted mode. Don't display headers, and separate properties 9768 * by a single tab. 9769 * -o List of columns to display. Defaults to 9770 * "name,property,value,source". 9771 * -p Display values in parsable (exact) format. 9772 * 9773 * Get properties of pools in the system. Output space statistics 9774 * for each one as well as other attributes. 9775 */ 9776 int 9777 zpool_do_get(int argc, char **argv) 9778 { 9779 zprop_get_cbdata_t cb = { 0 }; 9780 zprop_list_t fake_name = { 0 }; 9781 int ret; 9782 int c, i; 9783 char *value; 9784 9785 cb.cb_first = B_TRUE; 9786 9787 /* 9788 * Set up default columns and sources. 9789 */ 9790 cb.cb_sources = ZPROP_SRC_ALL; 9791 cb.cb_columns[0] = GET_COL_NAME; 9792 cb.cb_columns[1] = GET_COL_PROPERTY; 9793 cb.cb_columns[2] = GET_COL_VALUE; 9794 cb.cb_columns[3] = GET_COL_SOURCE; 9795 cb.cb_type = ZFS_TYPE_POOL; 9796 9797 /* check options */ 9798 while ((c = getopt(argc, argv, ":Hpo:")) != -1) { 9799 switch (c) { 9800 case 'p': 9801 cb.cb_literal = B_TRUE; 9802 break; 9803 case 'H': 9804 cb.cb_scripted = B_TRUE; 9805 break; 9806 case 'o': 9807 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 9808 i = 0; 9809 while (*optarg != '\0') { 9810 static char *col_subopts[] = 9811 { "name", "property", "value", "source", 9812 "all", NULL }; 9813 9814 if (i == ZFS_GET_NCOLS) { 9815 (void) fprintf(stderr, gettext("too " 9816 "many fields given to -o " 9817 "option\n")); 9818 usage(B_FALSE); 9819 } 9820 9821 switch (getsubopt(&optarg, col_subopts, 9822 &value)) { 9823 case 0: 9824 cb.cb_columns[i++] = GET_COL_NAME; 9825 break; 9826 case 1: 9827 cb.cb_columns[i++] = GET_COL_PROPERTY; 9828 break; 9829 case 2: 9830 cb.cb_columns[i++] = GET_COL_VALUE; 9831 break; 9832 case 3: 9833 cb.cb_columns[i++] = GET_COL_SOURCE; 9834 break; 9835 case 4: 9836 if (i > 0) { 9837 (void) fprintf(stderr, 9838 gettext("\"all\" conflicts " 9839 "with specific fields " 9840 "given to -o option\n")); 9841 usage(B_FALSE); 9842 } 9843 cb.cb_columns[0] = GET_COL_NAME; 9844 cb.cb_columns[1] = GET_COL_PROPERTY; 9845 cb.cb_columns[2] = GET_COL_VALUE; 9846 cb.cb_columns[3] = GET_COL_SOURCE; 9847 i = ZFS_GET_NCOLS; 9848 break; 9849 default: 9850 (void) fprintf(stderr, 9851 gettext("invalid column name " 9852 "'%s'\n"), value); 9853 usage(B_FALSE); 9854 } 9855 } 9856 break; 9857 case '?': 9858 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9859 optopt); 9860 usage(B_FALSE); 9861 } 9862 } 9863 9864 argc -= optind; 9865 argv += optind; 9866 9867 if (argc < 1) { 9868 (void) fprintf(stderr, gettext("missing property " 9869 "argument\n")); 9870 usage(B_FALSE); 9871 } 9872 9873 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, 9874 ZFS_TYPE_POOL) != 0) 9875 usage(B_FALSE); 9876 9877 argc--; 9878 argv++; 9879 9880 if (cb.cb_proplist != NULL) { 9881 fake_name.pl_prop = ZPOOL_PROP_NAME; 9882 fake_name.pl_width = strlen(gettext("NAME")); 9883 fake_name.pl_next = cb.cb_proplist; 9884 cb.cb_proplist = &fake_name; 9885 } 9886 9887 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_literal, 9888 get_callback, &cb); 9889 9890 if (cb.cb_proplist == &fake_name) 9891 zprop_free_list(fake_name.pl_next); 9892 else 9893 zprop_free_list(cb.cb_proplist); 9894 9895 return (ret); 9896 } 9897 9898 typedef struct set_cbdata { 9899 char *cb_propname; 9900 char *cb_value; 9901 boolean_t cb_any_successful; 9902 } set_cbdata_t; 9903 9904 static int 9905 set_callback(zpool_handle_t *zhp, void *data) 9906 { 9907 int error; 9908 set_cbdata_t *cb = (set_cbdata_t *)data; 9909 9910 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 9911 9912 if (!error) 9913 cb->cb_any_successful = B_TRUE; 9914 9915 return (error); 9916 } 9917 9918 int 9919 zpool_do_set(int argc, char **argv) 9920 { 9921 set_cbdata_t cb = { 0 }; 9922 int error; 9923 9924 if (argc > 1 && argv[1][0] == '-') { 9925 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9926 argv[1][1]); 9927 usage(B_FALSE); 9928 } 9929 9930 if (argc < 2) { 9931 (void) fprintf(stderr, gettext("missing property=value " 9932 "argument\n")); 9933 usage(B_FALSE); 9934 } 9935 9936 if (argc < 3) { 9937 (void) fprintf(stderr, gettext("missing pool name\n")); 9938 usage(B_FALSE); 9939 } 9940 9941 if (argc > 3) { 9942 (void) fprintf(stderr, gettext("too many pool names\n")); 9943 usage(B_FALSE); 9944 } 9945 9946 cb.cb_propname = argv[1]; 9947 cb.cb_value = strchr(cb.cb_propname, '='); 9948 if (cb.cb_value == NULL) { 9949 (void) fprintf(stderr, gettext("missing value in " 9950 "property=value argument\n")); 9951 usage(B_FALSE); 9952 } 9953 9954 *(cb.cb_value) = '\0'; 9955 cb.cb_value++; 9956 9957 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, B_FALSE, 9958 set_callback, &cb); 9959 9960 return (error); 9961 } 9962 9963 /* Add up the total number of bytes left to initialize/trim across all vdevs */ 9964 static uint64_t 9965 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) 9966 { 9967 uint64_t bytes_remaining; 9968 nvlist_t **child; 9969 uint_t c, children; 9970 vdev_stat_t *vs; 9971 9972 assert(activity == ZPOOL_WAIT_INITIALIZE || 9973 activity == ZPOOL_WAIT_TRIM); 9974 9975 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 9976 (uint64_t **)&vs, &c) == 0); 9977 9978 if (activity == ZPOOL_WAIT_INITIALIZE && 9979 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) 9980 bytes_remaining = vs->vs_initialize_bytes_est - 9981 vs->vs_initialize_bytes_done; 9982 else if (activity == ZPOOL_WAIT_TRIM && 9983 vs->vs_trim_state == VDEV_TRIM_ACTIVE) 9984 bytes_remaining = vs->vs_trim_bytes_est - 9985 vs->vs_trim_bytes_done; 9986 else 9987 bytes_remaining = 0; 9988 9989 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 9990 &child, &children) != 0) 9991 children = 0; 9992 9993 for (c = 0; c < children; c++) 9994 bytes_remaining += vdev_activity_remaining(child[c], activity); 9995 9996 return (bytes_remaining); 9997 } 9998 9999 /* Add up the total number of bytes left to rebuild across top-level vdevs */ 10000 static uint64_t 10001 vdev_activity_top_remaining(nvlist_t *nv) 10002 { 10003 uint64_t bytes_remaining = 0; 10004 nvlist_t **child; 10005 uint_t children; 10006 int error; 10007 10008 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 10009 &child, &children) != 0) 10010 children = 0; 10011 10012 for (uint_t c = 0; c < children; c++) { 10013 vdev_rebuild_stat_t *vrs; 10014 uint_t i; 10015 10016 error = nvlist_lookup_uint64_array(child[c], 10017 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); 10018 if (error == 0) { 10019 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 10020 bytes_remaining += (vrs->vrs_bytes_est - 10021 vrs->vrs_bytes_rebuilt); 10022 } 10023 } 10024 } 10025 10026 return (bytes_remaining); 10027 } 10028 10029 /* Whether any vdevs are 'spare' or 'replacing' vdevs */ 10030 static boolean_t 10031 vdev_any_spare_replacing(nvlist_t *nv) 10032 { 10033 nvlist_t **child; 10034 uint_t c, children; 10035 char *vdev_type; 10036 10037 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); 10038 10039 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || 10040 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || 10041 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { 10042 return (B_TRUE); 10043 } 10044 10045 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 10046 &child, &children) != 0) 10047 children = 0; 10048 10049 for (c = 0; c < children; c++) { 10050 if (vdev_any_spare_replacing(child[c])) 10051 return (B_TRUE); 10052 } 10053 10054 return (B_FALSE); 10055 } 10056 10057 typedef struct wait_data { 10058 char *wd_poolname; 10059 boolean_t wd_scripted; 10060 boolean_t wd_exact; 10061 boolean_t wd_headers_once; 10062 boolean_t wd_should_exit; 10063 /* Which activities to wait for */ 10064 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; 10065 float wd_interval; 10066 pthread_cond_t wd_cv; 10067 pthread_mutex_t wd_mutex; 10068 } wait_data_t; 10069 10070 /* 10071 * Print to stdout a single line, containing one column for each activity that 10072 * we are waiting for specifying how many bytes of work are left for that 10073 * activity. 10074 */ 10075 static void 10076 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) 10077 { 10078 nvlist_t *config, *nvroot; 10079 uint_t c; 10080 int i; 10081 pool_checkpoint_stat_t *pcs = NULL; 10082 pool_scan_stat_t *pss = NULL; 10083 pool_removal_stat_t *prs = NULL; 10084 char *headers[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE", 10085 "REMOVE", "RESILVER", "SCRUB", "TRIM"}; 10086 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; 10087 10088 /* Calculate the width of each column */ 10089 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10090 /* 10091 * Make sure we have enough space in the col for pretty-printed 10092 * numbers and for the column header, and then leave a couple 10093 * spaces between cols for readability. 10094 */ 10095 col_widths[i] = MAX(strlen(headers[i]), 6) + 2; 10096 } 10097 10098 /* Print header if appropriate */ 10099 int term_height = terminal_height(); 10100 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && 10101 row % (term_height-1) == 0); 10102 if (!wd->wd_scripted && (row == 0 || reprint_header)) { 10103 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10104 if (wd->wd_enabled[i]) 10105 (void) printf("%*s", col_widths[i], headers[i]); 10106 } 10107 (void) printf("\n"); 10108 } 10109 10110 /* Bytes of work remaining in each activity */ 10111 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; 10112 10113 bytes_rem[ZPOOL_WAIT_FREE] = 10114 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); 10115 10116 config = zpool_get_config(zhp, NULL); 10117 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10118 10119 (void) nvlist_lookup_uint64_array(nvroot, 10120 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10121 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 10122 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; 10123 10124 (void) nvlist_lookup_uint64_array(nvroot, 10125 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 10126 if (prs != NULL && prs->prs_state == DSS_SCANNING) 10127 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - 10128 prs->prs_copied; 10129 10130 (void) nvlist_lookup_uint64_array(nvroot, 10131 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); 10132 if (pss != NULL && pss->pss_state == DSS_SCANNING && 10133 pss->pss_pass_scrub_pause == 0) { 10134 int64_t rem = pss->pss_to_examine - pss->pss_issued; 10135 if (pss->pss_func == POOL_SCAN_SCRUB) 10136 bytes_rem[ZPOOL_WAIT_SCRUB] = rem; 10137 else 10138 bytes_rem[ZPOOL_WAIT_RESILVER] = rem; 10139 } else if (check_rebuilding(nvroot, NULL)) { 10140 bytes_rem[ZPOOL_WAIT_RESILVER] = 10141 vdev_activity_top_remaining(nvroot); 10142 } 10143 10144 bytes_rem[ZPOOL_WAIT_INITIALIZE] = 10145 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); 10146 bytes_rem[ZPOOL_WAIT_TRIM] = 10147 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); 10148 10149 /* 10150 * A replace finishes after resilvering finishes, so the amount of work 10151 * left for a replace is the same as for resilvering. 10152 * 10153 * It isn't quite correct to say that if we have any 'spare' or 10154 * 'replacing' vdevs and a resilver is happening, then a replace is in 10155 * progress, like we do here. When a hot spare is used, the faulted vdev 10156 * is not removed after the hot spare is resilvered, so parent 'spare' 10157 * vdev is not removed either. So we could have a 'spare' vdev, but be 10158 * resilvering for a different reason. However, we use it as a heuristic 10159 * because we don't have access to the DTLs, which could tell us whether 10160 * or not we have really finished resilvering a hot spare. 10161 */ 10162 if (vdev_any_spare_replacing(nvroot)) 10163 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; 10164 10165 if (timestamp_fmt != NODATE) 10166 print_timestamp(timestamp_fmt); 10167 10168 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10169 char buf[64]; 10170 if (!wd->wd_enabled[i]) 10171 continue; 10172 10173 if (wd->wd_exact) 10174 (void) snprintf(buf, sizeof (buf), "%" PRIi64, 10175 bytes_rem[i]); 10176 else 10177 zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); 10178 10179 if (wd->wd_scripted) 10180 (void) printf(i == 0 ? "%s" : "\t%s", buf); 10181 else 10182 (void) printf(" %*s", col_widths[i] - 1, buf); 10183 } 10184 (void) printf("\n"); 10185 (void) fflush(stdout); 10186 } 10187 10188 static void * 10189 wait_status_thread(void *arg) 10190 { 10191 wait_data_t *wd = (wait_data_t *)arg; 10192 zpool_handle_t *zhp; 10193 10194 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) 10195 return (void *)(1); 10196 10197 for (int row = 0; ; row++) { 10198 boolean_t missing; 10199 struct timespec timeout; 10200 int ret = 0; 10201 (void) clock_gettime(CLOCK_REALTIME, &timeout); 10202 10203 if (zpool_refresh_stats(zhp, &missing) != 0 || missing || 10204 zpool_props_refresh(zhp) != 0) { 10205 zpool_close(zhp); 10206 return (void *)(uintptr_t)(missing ? 0 : 1); 10207 } 10208 10209 print_wait_status_row(wd, zhp, row); 10210 10211 timeout.tv_sec += floor(wd->wd_interval); 10212 long nanos = timeout.tv_nsec + 10213 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; 10214 if (nanos >= NANOSEC) { 10215 timeout.tv_sec++; 10216 timeout.tv_nsec = nanos - NANOSEC; 10217 } else { 10218 timeout.tv_nsec = nanos; 10219 } 10220 pthread_mutex_lock(&wd->wd_mutex); 10221 if (!wd->wd_should_exit) 10222 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, 10223 &timeout); 10224 pthread_mutex_unlock(&wd->wd_mutex); 10225 if (ret == 0) { 10226 break; /* signaled by main thread */ 10227 } else if (ret != ETIMEDOUT) { 10228 (void) fprintf(stderr, gettext("pthread_cond_timedwait " 10229 "failed: %s\n"), strerror(ret)); 10230 zpool_close(zhp); 10231 return (void *)(uintptr_t)(1); 10232 } 10233 } 10234 10235 zpool_close(zhp); 10236 return (void *)(0); 10237 } 10238 10239 int 10240 zpool_do_wait(int argc, char **argv) 10241 { 10242 boolean_t verbose = B_FALSE; 10243 int c; 10244 char *value; 10245 int i; 10246 unsigned long count; 10247 pthread_t status_thr; 10248 int error = 0; 10249 zpool_handle_t *zhp; 10250 10251 wait_data_t wd; 10252 wd.wd_scripted = B_FALSE; 10253 wd.wd_exact = B_FALSE; 10254 wd.wd_headers_once = B_FALSE; 10255 wd.wd_should_exit = B_FALSE; 10256 10257 pthread_mutex_init(&wd.wd_mutex, NULL); 10258 pthread_cond_init(&wd.wd_cv, NULL); 10259 10260 /* By default, wait for all types of activity. */ 10261 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) 10262 wd.wd_enabled[i] = B_TRUE; 10263 10264 while ((c = getopt(argc, argv, "HpT:t:")) != -1) { 10265 switch (c) { 10266 case 'H': 10267 wd.wd_scripted = B_TRUE; 10268 break; 10269 case 'n': 10270 wd.wd_headers_once = B_TRUE; 10271 break; 10272 case 'p': 10273 wd.wd_exact = B_TRUE; 10274 break; 10275 case 'T': 10276 get_timestamp_arg(*optarg); 10277 break; 10278 case 't': 10279 { 10280 static char *col_subopts[] = { "discard", "free", 10281 "initialize", "replace", "remove", "resilver", 10282 "scrub", "trim", NULL }; 10283 10284 /* Reset activities array */ 10285 bzero(&wd.wd_enabled, sizeof (wd.wd_enabled)); 10286 while (*optarg != '\0') { 10287 int activity = getsubopt(&optarg, col_subopts, 10288 &value); 10289 10290 if (activity < 0) { 10291 (void) fprintf(stderr, 10292 gettext("invalid activity '%s'\n"), 10293 value); 10294 usage(B_FALSE); 10295 } 10296 10297 wd.wd_enabled[activity] = B_TRUE; 10298 } 10299 break; 10300 } 10301 case '?': 10302 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 10303 optopt); 10304 usage(B_FALSE); 10305 } 10306 } 10307 10308 argc -= optind; 10309 argv += optind; 10310 10311 get_interval_count(&argc, argv, &wd.wd_interval, &count); 10312 if (count != 0) { 10313 /* This subcmd only accepts an interval, not a count */ 10314 (void) fprintf(stderr, gettext("too many arguments\n")); 10315 usage(B_FALSE); 10316 } 10317 10318 if (wd.wd_interval != 0) 10319 verbose = B_TRUE; 10320 10321 if (argc < 1) { 10322 (void) fprintf(stderr, gettext("missing 'pool' argument\n")); 10323 usage(B_FALSE); 10324 } 10325 if (argc > 1) { 10326 (void) fprintf(stderr, gettext("too many arguments\n")); 10327 usage(B_FALSE); 10328 } 10329 10330 wd.wd_poolname = argv[0]; 10331 10332 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) 10333 return (1); 10334 10335 if (verbose) { 10336 /* 10337 * We use a separate thread for printing status updates because 10338 * the main thread will call lzc_wait(), which blocks as long 10339 * as an activity is in progress, which can be a long time. 10340 */ 10341 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) 10342 != 0) { 10343 (void) fprintf(stderr, gettext("failed to create status" 10344 "thread: %s\n"), strerror(errno)); 10345 zpool_close(zhp); 10346 return (1); 10347 } 10348 } 10349 10350 /* 10351 * Loop over all activities that we are supposed to wait for until none 10352 * of them are in progress. Note that this means we can end up waiting 10353 * for more activities to complete than just those that were in progress 10354 * when we began waiting; if an activity we are interested in begins 10355 * while we are waiting for another activity, we will wait for both to 10356 * complete before exiting. 10357 */ 10358 for (;;) { 10359 boolean_t missing = B_FALSE; 10360 boolean_t any_waited = B_FALSE; 10361 10362 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10363 boolean_t waited; 10364 10365 if (!wd.wd_enabled[i]) 10366 continue; 10367 10368 error = zpool_wait_status(zhp, i, &missing, &waited); 10369 if (error != 0 || missing) 10370 break; 10371 10372 any_waited = (any_waited || waited); 10373 } 10374 10375 if (error != 0 || missing || !any_waited) 10376 break; 10377 } 10378 10379 zpool_close(zhp); 10380 10381 if (verbose) { 10382 uintptr_t status; 10383 pthread_mutex_lock(&wd.wd_mutex); 10384 wd.wd_should_exit = B_TRUE; 10385 pthread_cond_signal(&wd.wd_cv); 10386 pthread_mutex_unlock(&wd.wd_mutex); 10387 (void) pthread_join(status_thr, (void *)&status); 10388 if (status != 0) 10389 error = status; 10390 } 10391 10392 pthread_mutex_destroy(&wd.wd_mutex); 10393 pthread_cond_destroy(&wd.wd_cv); 10394 return (error); 10395 } 10396 10397 static int 10398 find_command_idx(char *command, int *idx) 10399 { 10400 int i; 10401 10402 for (i = 0; i < NCOMMAND; i++) { 10403 if (command_table[i].name == NULL) 10404 continue; 10405 10406 if (strcmp(command, command_table[i].name) == 0) { 10407 *idx = i; 10408 return (0); 10409 } 10410 } 10411 return (1); 10412 } 10413 10414 /* 10415 * Display version message 10416 */ 10417 static int 10418 zpool_do_version(int argc, char **argv) 10419 { 10420 if (zfs_version_print() == -1) 10421 return (1); 10422 10423 return (0); 10424 } 10425 10426 /* 10427 * Do zpool_load_compat() and print error message on failure 10428 */ 10429 static zpool_compat_status_t 10430 zpool_do_load_compat(const char *compat, boolean_t *list) 10431 { 10432 char badword[ZFS_MAXPROPLEN]; 10433 char badfile[MAXPATHLEN]; 10434 zpool_compat_status_t ret; 10435 10436 switch (ret = zpool_load_compat(compat, list, badword, badfile)) { 10437 case ZPOOL_COMPATIBILITY_OK: 10438 break; 10439 case ZPOOL_COMPATIBILITY_READERR: 10440 (void) fprintf(stderr, gettext("error reading compatibility " 10441 "file '%s'\n"), badfile); 10442 break; 10443 case ZPOOL_COMPATIBILITY_BADFILE: 10444 (void) fprintf(stderr, gettext("compatibility file '%s' " 10445 "too large or not newline-terminated\n"), badfile); 10446 break; 10447 case ZPOOL_COMPATIBILITY_BADWORD: 10448 (void) fprintf(stderr, gettext("unknown feature '%s' in " 10449 "compatibility file '%s'\n"), badword, badfile); 10450 break; 10451 case ZPOOL_COMPATIBILITY_NOFILES: 10452 (void) fprintf(stderr, gettext("no compatibility files " 10453 "specified\n")); 10454 break; 10455 } 10456 return (ret); 10457 } 10458 10459 int 10460 main(int argc, char **argv) 10461 { 10462 int ret = 0; 10463 int i = 0; 10464 char *cmdname; 10465 char **newargv; 10466 10467 (void) setlocale(LC_ALL, ""); 10468 (void) setlocale(LC_NUMERIC, "C"); 10469 (void) textdomain(TEXT_DOMAIN); 10470 srand(time(NULL)); 10471 10472 opterr = 0; 10473 10474 /* 10475 * Make sure the user has specified some command. 10476 */ 10477 if (argc < 2) { 10478 (void) fprintf(stderr, gettext("missing command\n")); 10479 usage(B_FALSE); 10480 } 10481 10482 cmdname = argv[1]; 10483 10484 /* 10485 * Special case '-?' 10486 */ 10487 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) 10488 usage(B_TRUE); 10489 10490 /* 10491 * Special case '-V|--version' 10492 */ 10493 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) 10494 return (zpool_do_version(argc, argv)); 10495 10496 if ((g_zfs = libzfs_init()) == NULL) { 10497 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); 10498 return (1); 10499 } 10500 10501 libzfs_print_on_error(g_zfs, B_TRUE); 10502 10503 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 10504 10505 /* 10506 * Many commands modify input strings for string parsing reasons. 10507 * We create a copy to protect the original argv. 10508 */ 10509 newargv = malloc((argc + 1) * sizeof (newargv[0])); 10510 for (i = 0; i < argc; i++) 10511 newargv[i] = strdup(argv[i]); 10512 newargv[argc] = NULL; 10513 10514 /* 10515 * Run the appropriate command. 10516 */ 10517 if (find_command_idx(cmdname, &i) == 0) { 10518 current_command = &command_table[i]; 10519 ret = command_table[i].func(argc - 1, newargv + 1); 10520 } else if (strchr(cmdname, '=')) { 10521 verify(find_command_idx("set", &i) == 0); 10522 current_command = &command_table[i]; 10523 ret = command_table[i].func(argc, newargv); 10524 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 10525 /* 10526 * 'freeze' is a vile debugging abomination, so we treat 10527 * it as such. 10528 */ 10529 zfs_cmd_t zc = {"\0"}; 10530 10531 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 10532 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); 10533 if (ret != 0) { 10534 (void) fprintf(stderr, 10535 gettext("failed to freeze pool: %d\n"), errno); 10536 ret = 1; 10537 } 10538 10539 log_history = 0; 10540 } else { 10541 (void) fprintf(stderr, gettext("unrecognized " 10542 "command '%s'\n"), cmdname); 10543 usage(B_FALSE); 10544 ret = 1; 10545 } 10546 10547 for (i = 0; i < argc; i++) 10548 free(newargv[i]); 10549 free(newargv); 10550 10551 if (ret == 0 && log_history) 10552 (void) zpool_log_history(g_zfs, history_str); 10553 10554 libzfs_fini(g_zfs); 10555 10556 /* 10557 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 10558 * for the purposes of running ::findleaks. 10559 */ 10560 if (getenv("ZFS_ABORT") != NULL) { 10561 (void) printf("dumping core by request\n"); 10562 abort(); 10563 } 10564 10565 return (ret); 10566 } 10567