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 int 2627 show_import(nvlist_t *config, boolean_t report_error) 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 /* 2660 * If we're importing using a cachefile, then we won't report any 2661 * errors unless we are in the scan phase of the import. 2662 */ 2663 if (reason != ZPOOL_STATUS_OK && !report_error) 2664 return (reason); 2665 2666 (void) printf(gettext(" pool: %s\n"), name); 2667 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 2668 (void) printf(gettext(" state: %s"), health); 2669 if (pool_state == POOL_STATE_DESTROYED) 2670 (void) printf(gettext(" (DESTROYED)")); 2671 (void) printf("\n"); 2672 2673 switch (reason) { 2674 case ZPOOL_STATUS_MISSING_DEV_R: 2675 case ZPOOL_STATUS_MISSING_DEV_NR: 2676 case ZPOOL_STATUS_BAD_GUID_SUM: 2677 printf_color(ANSI_BOLD, gettext("status: ")); 2678 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2679 "missing from the system.\n")); 2680 break; 2681 2682 case ZPOOL_STATUS_CORRUPT_LABEL_R: 2683 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 2684 printf_color(ANSI_BOLD, gettext("status: ")); 2685 printf_color(ANSI_YELLOW, gettext("One or more devices contains" 2686 " corrupted data.\n")); 2687 break; 2688 2689 case ZPOOL_STATUS_CORRUPT_DATA: 2690 (void) printf( 2691 gettext(" status: The pool data is corrupted.\n")); 2692 break; 2693 2694 case ZPOOL_STATUS_OFFLINE_DEV: 2695 printf_color(ANSI_BOLD, gettext("status: ")); 2696 printf_color(ANSI_YELLOW, gettext("One or more devices " 2697 "are offlined.\n")); 2698 break; 2699 2700 case ZPOOL_STATUS_CORRUPT_POOL: 2701 printf_color(ANSI_BOLD, gettext("status: ")); 2702 printf_color(ANSI_YELLOW, gettext("The pool metadata is " 2703 "corrupted.\n")); 2704 break; 2705 2706 case ZPOOL_STATUS_VERSION_OLDER: 2707 printf_color(ANSI_BOLD, gettext("status: ")); 2708 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 2709 "a legacy on-disk version.\n")); 2710 break; 2711 2712 case ZPOOL_STATUS_VERSION_NEWER: 2713 printf_color(ANSI_BOLD, gettext("status: ")); 2714 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 2715 "an incompatible version.\n")); 2716 break; 2717 2718 case ZPOOL_STATUS_FEAT_DISABLED: 2719 printf_color(ANSI_BOLD, gettext("status: ")); 2720 printf_color(ANSI_YELLOW, gettext("Some supported and " 2721 "requested features are not enabled on the pool.\n")); 2722 break; 2723 2724 case ZPOOL_STATUS_COMPATIBILITY_ERR: 2725 printf_color(ANSI_BOLD, gettext("status: ")); 2726 printf_color(ANSI_YELLOW, gettext("Error reading or parsing " 2727 "the file(s) indicated by the 'compatibility'\n" 2728 "property.\n")); 2729 break; 2730 2731 case ZPOOL_STATUS_UNSUP_FEAT_READ: 2732 printf_color(ANSI_BOLD, gettext("status: ")); 2733 printf_color(ANSI_YELLOW, gettext("The pool uses the following " 2734 "feature(s) not supported on this system:\n")); 2735 color_start(ANSI_YELLOW); 2736 zpool_print_unsup_feat(config); 2737 color_end(); 2738 break; 2739 2740 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 2741 printf_color(ANSI_BOLD, gettext("status: ")); 2742 printf_color(ANSI_YELLOW, gettext("The pool can only be " 2743 "accessed in read-only mode on this system. It\n\tcannot be" 2744 " accessed in read-write mode because it uses the " 2745 "following\n\tfeature(s) not supported on this system:\n")); 2746 color_start(ANSI_YELLOW); 2747 zpool_print_unsup_feat(config); 2748 color_end(); 2749 break; 2750 2751 case ZPOOL_STATUS_HOSTID_ACTIVE: 2752 printf_color(ANSI_BOLD, gettext("status: ")); 2753 printf_color(ANSI_YELLOW, gettext("The pool is currently " 2754 "imported by another system.\n")); 2755 break; 2756 2757 case ZPOOL_STATUS_HOSTID_REQUIRED: 2758 printf_color(ANSI_BOLD, gettext("status: ")); 2759 printf_color(ANSI_YELLOW, gettext("The pool has the " 2760 "multihost property on. It cannot\n\tbe safely imported " 2761 "when the system hostid is not set.\n")); 2762 break; 2763 2764 case ZPOOL_STATUS_HOSTID_MISMATCH: 2765 printf_color(ANSI_BOLD, gettext("status: ")); 2766 printf_color(ANSI_YELLOW, gettext("The pool was last accessed " 2767 "by another system.\n")); 2768 break; 2769 2770 case ZPOOL_STATUS_FAULTED_DEV_R: 2771 case ZPOOL_STATUS_FAULTED_DEV_NR: 2772 printf_color(ANSI_BOLD, gettext("status: ")); 2773 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2774 "faulted.\n")); 2775 break; 2776 2777 case ZPOOL_STATUS_BAD_LOG: 2778 printf_color(ANSI_BOLD, gettext("status: ")); 2779 printf_color(ANSI_YELLOW, gettext("An intent log record cannot " 2780 "be read.\n")); 2781 break; 2782 2783 case ZPOOL_STATUS_RESILVERING: 2784 case ZPOOL_STATUS_REBUILDING: 2785 printf_color(ANSI_BOLD, gettext("status: ")); 2786 printf_color(ANSI_YELLOW, gettext("One or more devices were " 2787 "being resilvered.\n")); 2788 break; 2789 2790 case ZPOOL_STATUS_ERRATA: 2791 printf_color(ANSI_BOLD, gettext("status: ")); 2792 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), 2793 errata); 2794 break; 2795 2796 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 2797 printf_color(ANSI_BOLD, gettext("status: ")); 2798 printf_color(ANSI_YELLOW, gettext("One or more devices are " 2799 "configured to use a non-native block size.\n" 2800 "\tExpect reduced performance.\n")); 2801 break; 2802 2803 default: 2804 /* 2805 * No other status can be seen when importing pools. 2806 */ 2807 assert(reason == ZPOOL_STATUS_OK); 2808 } 2809 2810 /* 2811 * Print out an action according to the overall state of the pool. 2812 */ 2813 if (vs->vs_state == VDEV_STATE_HEALTHY) { 2814 if (reason == ZPOOL_STATUS_VERSION_OLDER || 2815 reason == ZPOOL_STATUS_FEAT_DISABLED) { 2816 (void) printf(gettext(" action: The pool can be " 2817 "imported using its name or numeric identifier, " 2818 "though\n\tsome features will not be available " 2819 "without an explicit 'zpool upgrade'.\n")); 2820 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) { 2821 (void) printf(gettext(" action: The pool can be " 2822 "imported using its name or numeric\n\tidentifier, " 2823 "though the file(s) indicated by its " 2824 "'compatibility'\n\tproperty cannot be parsed at " 2825 "this time.\n")); 2826 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 2827 (void) printf(gettext(" action: The pool can be " 2828 "imported using its name or numeric " 2829 "identifier and\n\tthe '-f' flag.\n")); 2830 } else if (reason == ZPOOL_STATUS_ERRATA) { 2831 switch (errata) { 2832 case ZPOOL_ERRATA_NONE: 2833 break; 2834 2835 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 2836 (void) printf(gettext(" action: The pool can " 2837 "be imported using its name or numeric " 2838 "identifier,\n\thowever there is a compat" 2839 "ibility issue which should be corrected" 2840 "\n\tby running 'zpool scrub'\n")); 2841 break; 2842 2843 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: 2844 (void) printf(gettext(" action: The pool can" 2845 "not be imported with this version of ZFS " 2846 "due to\n\tan active asynchronous destroy. " 2847 "Revert to an earlier version\n\tand " 2848 "allow the destroy to complete before " 2849 "updating.\n")); 2850 break; 2851 2852 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 2853 (void) printf(gettext(" action: Existing " 2854 "encrypted datasets contain an on-disk " 2855 "incompatibility, which\n\tneeds to be " 2856 "corrected. Backup these datasets to new " 2857 "encrypted datasets\n\tand destroy the " 2858 "old ones.\n")); 2859 break; 2860 2861 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 2862 (void) printf(gettext(" action: Existing " 2863 "encrypted snapshots and bookmarks contain " 2864 "an on-disk\n\tincompatibility. This may " 2865 "cause on-disk corruption if they are used" 2866 "\n\twith 'zfs recv'. To correct the " 2867 "issue, enable the bookmark_v2 feature.\n\t" 2868 "No additional action is needed if there " 2869 "are no encrypted snapshots or\n\t" 2870 "bookmarks. If preserving the encrypted " 2871 "snapshots and bookmarks is\n\trequired, " 2872 "use a non-raw send to backup and restore " 2873 "them. Alternately,\n\tthey may be removed" 2874 " to resolve the incompatibility.\n")); 2875 break; 2876 default: 2877 /* 2878 * All errata must contain an action message. 2879 */ 2880 assert(0); 2881 } 2882 } else { 2883 (void) printf(gettext(" action: The pool can be " 2884 "imported using its name or numeric " 2885 "identifier.\n")); 2886 } 2887 } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 2888 (void) printf(gettext(" action: The pool can be imported " 2889 "despite missing or damaged devices. The\n\tfault " 2890 "tolerance of the pool may be compromised if imported.\n")); 2891 } else { 2892 switch (reason) { 2893 case ZPOOL_STATUS_VERSION_NEWER: 2894 (void) printf(gettext(" action: The pool cannot be " 2895 "imported. Access the pool on a system running " 2896 "newer\n\tsoftware, or recreate the pool from " 2897 "backup.\n")); 2898 break; 2899 case ZPOOL_STATUS_UNSUP_FEAT_READ: 2900 printf_color(ANSI_BOLD, gettext("action: ")); 2901 printf_color(ANSI_YELLOW, gettext("The pool cannot be " 2902 "imported. Access the pool on a system that " 2903 "supports\n\tthe required feature(s), or recreate " 2904 "the pool from backup.\n")); 2905 break; 2906 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 2907 printf_color(ANSI_BOLD, gettext("action: ")); 2908 printf_color(ANSI_YELLOW, gettext("The pool cannot be " 2909 "imported in read-write mode. Import the pool " 2910 "with\n" 2911 "\t\"-o readonly=on\", access the pool on a system " 2912 "that supports the\n\trequired feature(s), or " 2913 "recreate the pool from backup.\n")); 2914 break; 2915 case ZPOOL_STATUS_MISSING_DEV_R: 2916 case ZPOOL_STATUS_MISSING_DEV_NR: 2917 case ZPOOL_STATUS_BAD_GUID_SUM: 2918 (void) printf(gettext(" action: The pool cannot be " 2919 "imported. Attach the missing\n\tdevices and try " 2920 "again.\n")); 2921 break; 2922 case ZPOOL_STATUS_HOSTID_ACTIVE: 2923 VERIFY0(nvlist_lookup_nvlist(config, 2924 ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); 2925 2926 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 2927 hostname = fnvlist_lookup_string(nvinfo, 2928 ZPOOL_CONFIG_MMP_HOSTNAME); 2929 2930 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 2931 hostid = fnvlist_lookup_uint64(nvinfo, 2932 ZPOOL_CONFIG_MMP_HOSTID); 2933 2934 (void) printf(gettext(" action: The pool must be " 2935 "exported from %s (hostid=%lx)\n\tbefore it " 2936 "can be safely imported.\n"), hostname, 2937 (unsigned long) hostid); 2938 break; 2939 case ZPOOL_STATUS_HOSTID_REQUIRED: 2940 (void) printf(gettext(" action: Set a unique system " 2941 "hostid with the zgenhostid(8) command.\n")); 2942 break; 2943 default: 2944 (void) printf(gettext(" action: The pool cannot be " 2945 "imported due to damaged devices or data.\n")); 2946 } 2947 } 2948 2949 /* Print the comment attached to the pool. */ 2950 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 2951 (void) printf(gettext("comment: %s\n"), comment); 2952 2953 /* 2954 * If the state is "closed" or "can't open", and the aux state 2955 * is "corrupt data": 2956 */ 2957 if (((vs->vs_state == VDEV_STATE_CLOSED) || 2958 (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 2959 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 2960 if (pool_state == POOL_STATE_DESTROYED) 2961 (void) printf(gettext("\tThe pool was destroyed, " 2962 "but can be imported using the '-Df' flags.\n")); 2963 else if (pool_state != POOL_STATE_EXPORTED) 2964 (void) printf(gettext("\tThe pool may be active on " 2965 "another system, but can be imported using\n\t" 2966 "the '-f' flag.\n")); 2967 } 2968 2969 if (msgid != NULL) { 2970 (void) printf(gettext( 2971 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"), 2972 msgid); 2973 } 2974 2975 (void) printf(gettext(" config:\n\n")); 2976 2977 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), 2978 VDEV_NAME_TYPE_ID); 2979 if (cb.cb_namewidth < 10) 2980 cb.cb_namewidth = 10; 2981 2982 print_import_config(&cb, name, nvroot, 0); 2983 2984 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); 2985 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 2986 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); 2987 2988 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 2989 (void) printf(gettext("\n\tAdditional devices are known to " 2990 "be part of this pool, though their\n\texact " 2991 "configuration cannot be determined.\n")); 2992 } 2993 return (0); 2994 } 2995 2996 static boolean_t 2997 zfs_force_import_required(nvlist_t *config) 2998 { 2999 uint64_t state; 3000 uint64_t hostid = 0; 3001 nvlist_t *nvinfo; 3002 3003 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); 3004 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid); 3005 3006 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) 3007 return (B_TRUE); 3008 3009 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3010 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { 3011 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, 3012 ZPOOL_CONFIG_MMP_STATE); 3013 3014 if (mmp_state != MMP_STATE_INACTIVE) 3015 return (B_TRUE); 3016 } 3017 3018 return (B_FALSE); 3019 } 3020 3021 /* 3022 * Perform the import for the given configuration. This passes the heavy 3023 * lifting off to zpool_import_props(), and then mounts the datasets contained 3024 * within the pool. 3025 */ 3026 static int 3027 do_import(nvlist_t *config, const char *newname, const char *mntopts, 3028 nvlist_t *props, int flags) 3029 { 3030 int ret = 0; 3031 zpool_handle_t *zhp; 3032 char *name; 3033 uint64_t version; 3034 3035 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); 3036 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); 3037 3038 if (!SPA_VERSION_IS_SUPPORTED(version)) { 3039 (void) fprintf(stderr, gettext("cannot import '%s': pool " 3040 "is formatted using an unsupported ZFS version\n"), name); 3041 return (1); 3042 } else if (zfs_force_import_required(config) && 3043 !(flags & ZFS_IMPORT_ANY_HOST)) { 3044 mmp_state_t mmp_state = MMP_STATE_INACTIVE; 3045 nvlist_t *nvinfo; 3046 3047 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3048 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) 3049 mmp_state = fnvlist_lookup_uint64(nvinfo, 3050 ZPOOL_CONFIG_MMP_STATE); 3051 3052 if (mmp_state == MMP_STATE_ACTIVE) { 3053 char *hostname = "<unknown>"; 3054 uint64_t hostid = 0; 3055 3056 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 3057 hostname = fnvlist_lookup_string(nvinfo, 3058 ZPOOL_CONFIG_MMP_HOSTNAME); 3059 3060 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 3061 hostid = fnvlist_lookup_uint64(nvinfo, 3062 ZPOOL_CONFIG_MMP_HOSTID); 3063 3064 (void) fprintf(stderr, gettext("cannot import '%s': " 3065 "pool is imported on %s (hostid: " 3066 "0x%lx)\nExport the pool on the other system, " 3067 "then run 'zpool import'.\n"), 3068 name, hostname, (unsigned long) hostid); 3069 } else if (mmp_state == MMP_STATE_NO_HOSTID) { 3070 (void) fprintf(stderr, gettext("Cannot import '%s': " 3071 "pool has the multihost property on and the\n" 3072 "system's hostid is not set. Set a unique hostid " 3073 "with the zgenhostid(8) command.\n"), name); 3074 } else { 3075 char *hostname = "<unknown>"; 3076 uint64_t timestamp = 0; 3077 uint64_t hostid = 0; 3078 3079 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) 3080 hostname = fnvlist_lookup_string(config, 3081 ZPOOL_CONFIG_HOSTNAME); 3082 3083 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) 3084 timestamp = fnvlist_lookup_uint64(config, 3085 ZPOOL_CONFIG_TIMESTAMP); 3086 3087 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) 3088 hostid = fnvlist_lookup_uint64(config, 3089 ZPOOL_CONFIG_HOSTID); 3090 3091 (void) fprintf(stderr, gettext("cannot import '%s': " 3092 "pool was previously in use from another system.\n" 3093 "Last accessed by %s (hostid=%lx) at %s" 3094 "The pool can be imported, use 'zpool import -f' " 3095 "to import the pool.\n"), name, hostname, 3096 (unsigned long)hostid, ctime((time_t *)×tamp)); 3097 } 3098 3099 return (1); 3100 } 3101 3102 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 3103 return (1); 3104 3105 if (newname != NULL) 3106 name = (char *)newname; 3107 3108 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 3109 return (1); 3110 3111 /* 3112 * Loading keys is best effort. We don't want to return immediately 3113 * if it fails but we do want to give the error to the caller. 3114 */ 3115 if (flags & ZFS_IMPORT_LOAD_KEYS) { 3116 ret = zfs_crypto_attempt_load_keys(g_zfs, name); 3117 if (ret != 0) 3118 ret = 1; 3119 } 3120 3121 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3122 !(flags & ZFS_IMPORT_ONLY) && 3123 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 3124 zpool_close(zhp); 3125 return (1); 3126 } 3127 3128 zpool_close(zhp); 3129 return (ret); 3130 } 3131 3132 static int 3133 import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags, 3134 char *orig_name, char *new_name, 3135 boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all, 3136 importargs_t *import) 3137 { 3138 nvlist_t *config = NULL; 3139 nvlist_t *found_config = NULL; 3140 uint64_t pool_state; 3141 3142 /* 3143 * At this point we have a list of import candidate configs. Even if 3144 * we were searching by pool name or guid, we still need to 3145 * post-process the list to deal with pool state and possible 3146 * duplicate names. 3147 */ 3148 int err = 0; 3149 nvpair_t *elem = NULL; 3150 boolean_t first = B_TRUE; 3151 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3152 3153 verify(nvpair_value_nvlist(elem, &config) == 0); 3154 3155 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3156 &pool_state) == 0); 3157 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 3158 continue; 3159 if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 3160 continue; 3161 3162 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 3163 import->policy) == 0); 3164 3165 if (!pool_specified) { 3166 if (first) 3167 first = B_FALSE; 3168 else if (!do_all) 3169 (void) printf("\n"); 3170 3171 if (do_all) { 3172 err |= do_import(config, NULL, mntopts, 3173 props, flags); 3174 } else { 3175 /* 3176 * If we're importing from cachefile, then 3177 * we don't want to report errors until we 3178 * are in the scan phase of the import. If 3179 * we get an error, then we return that error 3180 * to invoke the scan phase. 3181 */ 3182 if (import->cachefile && !import->scan) 3183 err = show_import(config, B_FALSE); 3184 else 3185 (void) show_import(config, B_TRUE); 3186 } 3187 } else if (import->poolname != NULL) { 3188 char *name; 3189 3190 /* 3191 * We are searching for a pool based on name. 3192 */ 3193 verify(nvlist_lookup_string(config, 3194 ZPOOL_CONFIG_POOL_NAME, &name) == 0); 3195 3196 if (strcmp(name, import->poolname) == 0) { 3197 if (found_config != NULL) { 3198 (void) fprintf(stderr, gettext( 3199 "cannot import '%s': more than " 3200 "one matching pool\n"), 3201 import->poolname); 3202 (void) fprintf(stderr, gettext( 3203 "import by numeric ID instead\n")); 3204 err = B_TRUE; 3205 } 3206 found_config = config; 3207 } 3208 } else { 3209 uint64_t guid; 3210 3211 /* 3212 * Search for a pool by guid. 3213 */ 3214 verify(nvlist_lookup_uint64(config, 3215 ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 3216 3217 if (guid == import->guid) 3218 found_config = config; 3219 } 3220 } 3221 3222 /* 3223 * If we were searching for a specific pool, verify that we found a 3224 * pool, and then do the import. 3225 */ 3226 if (pool_specified && err == 0) { 3227 if (found_config == NULL) { 3228 (void) fprintf(stderr, gettext("cannot import '%s': " 3229 "no such pool available\n"), orig_name); 3230 err = B_TRUE; 3231 } else { 3232 err |= do_import(found_config, new_name, 3233 mntopts, props, flags); 3234 } 3235 } 3236 3237 /* 3238 * If we were just looking for pools, report an error if none were 3239 * found. 3240 */ 3241 if (!pool_specified && first) 3242 (void) fprintf(stderr, 3243 gettext("no pools available to import\n")); 3244 return (err); 3245 } 3246 3247 typedef struct target_exists_args { 3248 const char *poolname; 3249 uint64_t poolguid; 3250 } target_exists_args_t; 3251 3252 static int 3253 name_or_guid_exists(zpool_handle_t *zhp, void *data) 3254 { 3255 target_exists_args_t *args = data; 3256 nvlist_t *config = zpool_get_config(zhp, NULL); 3257 int found = 0; 3258 3259 if (config == NULL) 3260 return (0); 3261 3262 if (args->poolname != NULL) { 3263 char *pool_name; 3264 3265 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 3266 &pool_name) == 0); 3267 if (strcmp(pool_name, args->poolname) == 0) 3268 found = 1; 3269 } else { 3270 uint64_t pool_guid; 3271 3272 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 3273 &pool_guid) == 0); 3274 if (pool_guid == args->poolguid) 3275 found = 1; 3276 } 3277 zpool_close(zhp); 3278 3279 return (found); 3280 } 3281 /* 3282 * zpool checkpoint <pool> 3283 * checkpoint --discard <pool> 3284 * 3285 * -d Discard the checkpoint from a checkpointed 3286 * --discard pool. 3287 * 3288 * -w Wait for discarding a checkpoint to complete. 3289 * --wait 3290 * 3291 * Checkpoints the specified pool, by taking a "snapshot" of its 3292 * current state. A pool can only have one checkpoint at a time. 3293 */ 3294 int 3295 zpool_do_checkpoint(int argc, char **argv) 3296 { 3297 boolean_t discard, wait; 3298 char *pool; 3299 zpool_handle_t *zhp; 3300 int c, err; 3301 3302 struct option long_options[] = { 3303 {"discard", no_argument, NULL, 'd'}, 3304 {"wait", no_argument, NULL, 'w'}, 3305 {0, 0, 0, 0} 3306 }; 3307 3308 discard = B_FALSE; 3309 wait = B_FALSE; 3310 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) { 3311 switch (c) { 3312 case 'd': 3313 discard = B_TRUE; 3314 break; 3315 case 'w': 3316 wait = B_TRUE; 3317 break; 3318 case '?': 3319 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3320 optopt); 3321 usage(B_FALSE); 3322 } 3323 } 3324 3325 if (wait && !discard) { 3326 (void) fprintf(stderr, gettext("--wait only valid when " 3327 "--discard also specified\n")); 3328 usage(B_FALSE); 3329 } 3330 3331 argc -= optind; 3332 argv += optind; 3333 3334 if (argc < 1) { 3335 (void) fprintf(stderr, gettext("missing pool argument\n")); 3336 usage(B_FALSE); 3337 } 3338 3339 if (argc > 1) { 3340 (void) fprintf(stderr, gettext("too many arguments\n")); 3341 usage(B_FALSE); 3342 } 3343 3344 pool = argv[0]; 3345 3346 if ((zhp = zpool_open(g_zfs, pool)) == NULL) { 3347 /* As a special case, check for use of '/' in the name */ 3348 if (strchr(pool, '/') != NULL) 3349 (void) fprintf(stderr, gettext("'zpool checkpoint' " 3350 "doesn't work on datasets. To save the state " 3351 "of a dataset from a specific point in time " 3352 "please use 'zfs snapshot'\n")); 3353 return (1); 3354 } 3355 3356 if (discard) { 3357 err = (zpool_discard_checkpoint(zhp) != 0); 3358 if (err == 0 && wait) 3359 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD); 3360 } else { 3361 err = (zpool_checkpoint(zhp) != 0); 3362 } 3363 3364 zpool_close(zhp); 3365 3366 return (err); 3367 } 3368 3369 #define CHECKPOINT_OPT 1024 3370 3371 /* 3372 * zpool import [-d dir] [-D] 3373 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 3374 * [-d dir | -c cachefile | -s] [-f] -a 3375 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 3376 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id> 3377 * [newpool] 3378 * 3379 * -c Read pool information from a cachefile instead of searching 3380 * devices. If importing from a cachefile config fails, then 3381 * fallback to searching for devices only in the directories that 3382 * exist in the cachefile. 3383 * 3384 * -d Scan in a specific directory, other than /dev/. More than 3385 * one directory can be specified using multiple '-d' options. 3386 * 3387 * -D Scan for previously destroyed pools or import all or only 3388 * specified destroyed pools. 3389 * 3390 * -R Temporarily import the pool, with all mountpoints relative to 3391 * the given root. The pool will remain exported when the machine 3392 * is rebooted. 3393 * 3394 * -V Import even in the presence of faulted vdevs. This is an 3395 * intentionally undocumented option for testing purposes, and 3396 * treats the pool configuration as complete, leaving any bad 3397 * vdevs in the FAULTED state. In other words, it does verbatim 3398 * import. 3399 * 3400 * -f Force import, even if it appears that the pool is active. 3401 * 3402 * -F Attempt rewind if necessary. 3403 * 3404 * -n See if rewind would work, but don't actually rewind. 3405 * 3406 * -N Import the pool but don't mount datasets. 3407 * 3408 * -T Specify a starting txg to use for import. This option is 3409 * intentionally undocumented option for testing purposes. 3410 * 3411 * -a Import all pools found. 3412 * 3413 * -l Load encryption keys while importing. 3414 * 3415 * -o Set property=value and/or temporary mount options (without '='). 3416 * 3417 * -s Scan using the default search path, the libblkid cache will 3418 * not be consulted. 3419 * 3420 * --rewind-to-checkpoint 3421 * Import the pool and revert back to the checkpoint. 3422 * 3423 * The import command scans for pools to import, and import pools based on pool 3424 * name and GUID. The pool can also be renamed as part of the import process. 3425 */ 3426 int 3427 zpool_do_import(int argc, char **argv) 3428 { 3429 char **searchdirs = NULL; 3430 char *env, *envdup = NULL; 3431 int nsearch = 0; 3432 int c; 3433 int err = 0; 3434 nvlist_t *pools = NULL; 3435 boolean_t do_all = B_FALSE; 3436 boolean_t do_destroyed = B_FALSE; 3437 char *mntopts = NULL; 3438 uint64_t searchguid = 0; 3439 char *searchname = NULL; 3440 char *propval; 3441 nvlist_t *policy = NULL; 3442 nvlist_t *props = NULL; 3443 int flags = ZFS_IMPORT_NORMAL; 3444 uint32_t rewind_policy = ZPOOL_NO_REWIND; 3445 boolean_t dryrun = B_FALSE; 3446 boolean_t do_rewind = B_FALSE; 3447 boolean_t xtreme_rewind = B_FALSE; 3448 boolean_t do_scan = B_FALSE; 3449 boolean_t pool_exists = B_FALSE; 3450 boolean_t pool_specified = B_FALSE; 3451 uint64_t txg = -1ULL; 3452 char *cachefile = NULL; 3453 importargs_t idata = { 0 }; 3454 char *endptr; 3455 3456 struct option long_options[] = { 3457 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, 3458 {0, 0, 0, 0} 3459 }; 3460 3461 /* check options */ 3462 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", 3463 long_options, NULL)) != -1) { 3464 switch (c) { 3465 case 'a': 3466 do_all = B_TRUE; 3467 break; 3468 case 'c': 3469 cachefile = optarg; 3470 break; 3471 case 'd': 3472 if (searchdirs == NULL) { 3473 searchdirs = safe_malloc(sizeof (char *)); 3474 } else { 3475 char **tmp = safe_malloc((nsearch + 1) * 3476 sizeof (char *)); 3477 bcopy(searchdirs, tmp, nsearch * 3478 sizeof (char *)); 3479 free(searchdirs); 3480 searchdirs = tmp; 3481 } 3482 searchdirs[nsearch++] = optarg; 3483 break; 3484 case 'D': 3485 do_destroyed = B_TRUE; 3486 break; 3487 case 'f': 3488 flags |= ZFS_IMPORT_ANY_HOST; 3489 break; 3490 case 'F': 3491 do_rewind = B_TRUE; 3492 break; 3493 case 'l': 3494 flags |= ZFS_IMPORT_LOAD_KEYS; 3495 break; 3496 case 'm': 3497 flags |= ZFS_IMPORT_MISSING_LOG; 3498 break; 3499 case 'n': 3500 dryrun = B_TRUE; 3501 break; 3502 case 'N': 3503 flags |= ZFS_IMPORT_ONLY; 3504 break; 3505 case 'o': 3506 if ((propval = strchr(optarg, '=')) != NULL) { 3507 *propval = '\0'; 3508 propval++; 3509 if (add_prop_list(optarg, propval, 3510 &props, B_TRUE)) 3511 goto error; 3512 } else { 3513 mntopts = optarg; 3514 } 3515 break; 3516 case 'R': 3517 if (add_prop_list(zpool_prop_to_name( 3518 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 3519 goto error; 3520 if (add_prop_list_default(zpool_prop_to_name( 3521 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 3522 goto error; 3523 break; 3524 case 's': 3525 do_scan = B_TRUE; 3526 break; 3527 case 't': 3528 flags |= ZFS_IMPORT_TEMP_NAME; 3529 if (add_prop_list_default(zpool_prop_to_name( 3530 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 3531 goto error; 3532 break; 3533 3534 case 'T': 3535 errno = 0; 3536 txg = strtoull(optarg, &endptr, 0); 3537 if (errno != 0 || *endptr != '\0') { 3538 (void) fprintf(stderr, 3539 gettext("invalid txg value\n")); 3540 usage(B_FALSE); 3541 } 3542 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 3543 break; 3544 case 'V': 3545 flags |= ZFS_IMPORT_VERBATIM; 3546 break; 3547 case 'X': 3548 xtreme_rewind = B_TRUE; 3549 break; 3550 case CHECKPOINT_OPT: 3551 flags |= ZFS_IMPORT_CHECKPOINT; 3552 break; 3553 case ':': 3554 (void) fprintf(stderr, gettext("missing argument for " 3555 "'%c' option\n"), optopt); 3556 usage(B_FALSE); 3557 break; 3558 case '?': 3559 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3560 optopt); 3561 usage(B_FALSE); 3562 } 3563 } 3564 3565 argc -= optind; 3566 argv += optind; 3567 3568 if (cachefile && nsearch != 0) { 3569 (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 3570 usage(B_FALSE); 3571 } 3572 3573 if (cachefile && do_scan) { 3574 (void) fprintf(stderr, gettext("-c is incompatible with -s\n")); 3575 usage(B_FALSE); 3576 } 3577 3578 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { 3579 (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); 3580 usage(B_FALSE); 3581 } 3582 3583 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { 3584 (void) fprintf(stderr, gettext("-l is only meaningful during " 3585 "an import\n")); 3586 usage(B_FALSE); 3587 } 3588 3589 if ((dryrun || xtreme_rewind) && !do_rewind) { 3590 (void) fprintf(stderr, 3591 gettext("-n or -X only meaningful with -F\n")); 3592 usage(B_FALSE); 3593 } 3594 if (dryrun) 3595 rewind_policy = ZPOOL_TRY_REWIND; 3596 else if (do_rewind) 3597 rewind_policy = ZPOOL_DO_REWIND; 3598 if (xtreme_rewind) 3599 rewind_policy |= ZPOOL_EXTREME_REWIND; 3600 3601 /* In the future, we can capture further policy and include it here */ 3602 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3603 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || 3604 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 3605 rewind_policy) != 0) 3606 goto error; 3607 3608 /* check argument count */ 3609 if (do_all) { 3610 if (argc != 0) { 3611 (void) fprintf(stderr, gettext("too many arguments\n")); 3612 usage(B_FALSE); 3613 } 3614 } else { 3615 if (argc > 2) { 3616 (void) fprintf(stderr, gettext("too many arguments\n")); 3617 usage(B_FALSE); 3618 } 3619 } 3620 3621 /* 3622 * Check for the effective uid. We do this explicitly here because 3623 * otherwise any attempt to discover pools will silently fail. 3624 */ 3625 if (argc == 0 && geteuid() != 0) { 3626 (void) fprintf(stderr, gettext("cannot " 3627 "discover pools: permission denied\n")); 3628 if (searchdirs != NULL) 3629 free(searchdirs); 3630 3631 nvlist_free(props); 3632 nvlist_free(policy); 3633 return (1); 3634 } 3635 3636 /* 3637 * Depending on the arguments given, we do one of the following: 3638 * 3639 * <none> Iterate through all pools and display information about 3640 * each one. 3641 * 3642 * -a Iterate through all pools and try to import each one. 3643 * 3644 * <id> Find the pool that corresponds to the given GUID/pool 3645 * name and import that one. 3646 * 3647 * -D Above options applies only to destroyed pools. 3648 */ 3649 if (argc != 0) { 3650 char *endptr; 3651 3652 errno = 0; 3653 searchguid = strtoull(argv[0], &endptr, 10); 3654 if (errno != 0 || *endptr != '\0') { 3655 searchname = argv[0]; 3656 searchguid = 0; 3657 } 3658 pool_specified = B_TRUE; 3659 3660 /* 3661 * User specified a name or guid. Ensure it's unique. 3662 */ 3663 target_exists_args_t search = {searchname, searchguid}; 3664 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); 3665 } 3666 3667 /* 3668 * Check the environment for the preferred search path. 3669 */ 3670 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { 3671 char *dir; 3672 3673 envdup = strdup(env); 3674 3675 dir = strtok(envdup, ":"); 3676 while (dir != NULL) { 3677 if (searchdirs == NULL) { 3678 searchdirs = safe_malloc(sizeof (char *)); 3679 } else { 3680 char **tmp = safe_malloc((nsearch + 1) * 3681 sizeof (char *)); 3682 bcopy(searchdirs, tmp, nsearch * 3683 sizeof (char *)); 3684 free(searchdirs); 3685 searchdirs = tmp; 3686 } 3687 searchdirs[nsearch++] = dir; 3688 dir = strtok(NULL, ":"); 3689 } 3690 } 3691 3692 idata.path = searchdirs; 3693 idata.paths = nsearch; 3694 idata.poolname = searchname; 3695 idata.guid = searchguid; 3696 idata.cachefile = cachefile; 3697 idata.scan = do_scan; 3698 idata.policy = policy; 3699 3700 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops); 3701 3702 if (pools != NULL && pool_exists && 3703 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 3704 (void) fprintf(stderr, gettext("cannot import '%s': " 3705 "a pool with that name already exists\n"), 3706 argv[0]); 3707 (void) fprintf(stderr, gettext("use the form '%s " 3708 "<pool | id> <newpool>' to give it a new name\n"), 3709 "zpool import"); 3710 err = 1; 3711 } else if (pools == NULL && pool_exists) { 3712 (void) fprintf(stderr, gettext("cannot import '%s': " 3713 "a pool with that name is already created/imported,\n"), 3714 argv[0]); 3715 (void) fprintf(stderr, gettext("and no additional pools " 3716 "with that name were found\n")); 3717 err = 1; 3718 } else if (pools == NULL) { 3719 if (argc != 0) { 3720 (void) fprintf(stderr, gettext("cannot import '%s': " 3721 "no such pool available\n"), argv[0]); 3722 } 3723 err = 1; 3724 } 3725 3726 if (err == 1) { 3727 if (searchdirs != NULL) 3728 free(searchdirs); 3729 if (envdup != NULL) 3730 free(envdup); 3731 nvlist_free(policy); 3732 nvlist_free(pools); 3733 nvlist_free(props); 3734 return (1); 3735 } 3736 3737 err = import_pools(pools, props, mntopts, flags, argv[0], 3738 argc == 1 ? NULL : argv[1], do_destroyed, pool_specified, 3739 do_all, &idata); 3740 3741 /* 3742 * If we're using the cachefile and we failed to import, then 3743 * fallback to scanning the directory for pools that match 3744 * those in the cachefile. 3745 */ 3746 if (err != 0 && cachefile != NULL) { 3747 (void) printf(gettext("cachefile import failed, retrying\n")); 3748 3749 /* 3750 * We use the scan flag to gather the directories that exist 3751 * in the cachefile. If we need to fallback to searching for 3752 * the pool config, we will only search devices in these 3753 * directories. 3754 */ 3755 idata.scan = B_TRUE; 3756 nvlist_free(pools); 3757 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops); 3758 3759 err = import_pools(pools, props, mntopts, flags, argv[0], 3760 argc == 1 ? NULL : argv[1], do_destroyed, pool_specified, 3761 do_all, &idata); 3762 } 3763 3764 error: 3765 nvlist_free(props); 3766 nvlist_free(pools); 3767 nvlist_free(policy); 3768 if (searchdirs != NULL) 3769 free(searchdirs); 3770 if (envdup != NULL) 3771 free(envdup); 3772 3773 return (err ? 1 : 0); 3774 } 3775 3776 /* 3777 * zpool sync [-f] [pool] ... 3778 * 3779 * -f (undocumented) force uberblock (and config including zpool cache file) 3780 * update. 3781 * 3782 * Sync the specified pool(s). 3783 * Without arguments "zpool sync" will sync all pools. 3784 * This command initiates TXG sync(s) and will return after the TXG(s) commit. 3785 * 3786 */ 3787 static int 3788 zpool_do_sync(int argc, char **argv) 3789 { 3790 int ret; 3791 boolean_t force = B_FALSE; 3792 3793 /* check options */ 3794 while ((ret = getopt(argc, argv, "f")) != -1) { 3795 switch (ret) { 3796 case 'f': 3797 force = B_TRUE; 3798 break; 3799 case '?': 3800 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3801 optopt); 3802 usage(B_FALSE); 3803 } 3804 } 3805 3806 argc -= optind; 3807 argv += optind; 3808 3809 /* if argc == 0 we will execute zpool_sync_one on all pools */ 3810 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, zpool_sync_one, 3811 &force); 3812 3813 return (ret); 3814 } 3815 3816 typedef struct iostat_cbdata { 3817 uint64_t cb_flags; 3818 int cb_name_flags; 3819 int cb_namewidth; 3820 int cb_iteration; 3821 char **cb_vdev_names; /* Only show these vdevs */ 3822 unsigned int cb_vdev_names_count; 3823 boolean_t cb_verbose; 3824 boolean_t cb_literal; 3825 boolean_t cb_scripted; 3826 zpool_list_t *cb_list; 3827 vdev_cmd_data_list_t *vcdl; 3828 } iostat_cbdata_t; 3829 3830 /* iostat labels */ 3831 typedef struct name_and_columns { 3832 const char *name; /* Column name */ 3833 unsigned int columns; /* Center name to this number of columns */ 3834 } name_and_columns_t; 3835 3836 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */ 3837 3838 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = 3839 { 3840 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, 3841 {NULL}}, 3842 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 3843 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL}}, 3844 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, 3845 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, 3846 {"trimq_write", 2}, {NULL}}, 3847 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 3848 {"asyncq_wait", 2}, {NULL}}, 3849 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, 3850 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, 3851 {"trim", 2}, {NULL}}, 3852 }; 3853 3854 /* Shorthand - if "columns" field not set, default to 1 column */ 3855 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = 3856 { 3857 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, 3858 {"write"}, {NULL}}, 3859 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 3860 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL}}, 3861 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, 3862 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, 3863 {"pend"}, {"activ"}, {NULL}}, 3864 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 3865 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL}}, 3866 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, 3867 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}}, 3868 }; 3869 3870 static const char *histo_to_title[] = { 3871 [IOS_L_HISTO] = "latency", 3872 [IOS_RQ_HISTO] = "req_size", 3873 }; 3874 3875 /* 3876 * Return the number of labels in a null-terminated name_and_columns_t 3877 * array. 3878 * 3879 */ 3880 static unsigned int 3881 label_array_len(const name_and_columns_t *labels) 3882 { 3883 int i = 0; 3884 3885 while (labels[i].name) 3886 i++; 3887 3888 return (i); 3889 } 3890 3891 /* 3892 * Return the number of strings in a null-terminated string array. 3893 * For example: 3894 * 3895 * const char foo[] = {"bar", "baz", NULL} 3896 * 3897 * returns 2 3898 */ 3899 static uint64_t 3900 str_array_len(const char *array[]) 3901 { 3902 uint64_t i = 0; 3903 while (array[i]) 3904 i++; 3905 3906 return (i); 3907 } 3908 3909 3910 /* 3911 * Return a default column width for default/latency/queue columns. This does 3912 * not include histograms, which have their columns autosized. 3913 */ 3914 static unsigned int 3915 default_column_width(iostat_cbdata_t *cb, enum iostat_type type) 3916 { 3917 unsigned long column_width = 5; /* Normal niceprint */ 3918 static unsigned long widths[] = { 3919 /* 3920 * Choose some sane default column sizes for printing the 3921 * raw numbers. 3922 */ 3923 [IOS_DEFAULT] = 15, /* 1PB capacity */ 3924 [IOS_LATENCY] = 10, /* 1B ns = 10sec */ 3925 [IOS_QUEUES] = 6, /* 1M queue entries */ 3926 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */ 3927 [IOS_RQ_HISTO] = 6, /* 1M queue entries */ 3928 }; 3929 3930 if (cb->cb_literal) 3931 column_width = widths[type]; 3932 3933 return (column_width); 3934 } 3935 3936 /* 3937 * Print the column labels, i.e: 3938 * 3939 * capacity operations bandwidth 3940 * alloc free read write read write ... 3941 * 3942 * If force_column_width is set, use it for the column width. If not set, use 3943 * the default column width. 3944 */ 3945 static void 3946 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, 3947 const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) 3948 { 3949 int i, idx, s; 3950 int text_start, rw_column_width, spaces_to_end; 3951 uint64_t flags = cb->cb_flags; 3952 uint64_t f; 3953 unsigned int column_width = force_column_width; 3954 3955 /* For each bit set in flags */ 3956 for (f = flags; f; f &= ~(1ULL << idx)) { 3957 idx = lowbit64(f) - 1; 3958 if (!force_column_width) 3959 column_width = default_column_width(cb, idx); 3960 /* Print our top labels centered over "read write" label. */ 3961 for (i = 0; i < label_array_len(labels[idx]); i++) { 3962 const char *name = labels[idx][i].name; 3963 /* 3964 * We treat labels[][].columns == 0 as shorthand 3965 * for one column. It makes writing out the label 3966 * tables more concise. 3967 */ 3968 unsigned int columns = MAX(1, labels[idx][i].columns); 3969 unsigned int slen = strlen(name); 3970 3971 rw_column_width = (column_width * columns) + 3972 (2 * (columns - 1)); 3973 3974 text_start = (int)((rw_column_width) / columns - 3975 slen / columns); 3976 if (text_start < 0) 3977 text_start = 0; 3978 3979 printf(" "); /* Two spaces between columns */ 3980 3981 /* Space from beginning of column to label */ 3982 for (s = 0; s < text_start; s++) 3983 printf(" "); 3984 3985 printf("%s", name); 3986 3987 /* Print space after label to end of column */ 3988 spaces_to_end = rw_column_width - text_start - slen; 3989 if (spaces_to_end < 0) 3990 spaces_to_end = 0; 3991 3992 for (s = 0; s < spaces_to_end; s++) 3993 printf(" "); 3994 } 3995 } 3996 } 3997 3998 3999 /* 4000 * print_cmd_columns - Print custom column titles from -c 4001 * 4002 * If the user specified the "zpool status|iostat -c" then print their custom 4003 * column titles in the header. For example, print_cmd_columns() would print 4004 * the " col1 col2" part of this: 4005 * 4006 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' 4007 * ... 4008 * capacity operations bandwidth 4009 * pool alloc free read write read write col1 col2 4010 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 4011 * mypool 269K 1008M 0 0 107 946 4012 * mirror 269K 1008M 0 0 107 946 4013 * sdb - - 0 0 102 473 val1 val2 4014 * sdc - - 0 0 5 473 val1 val2 4015 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 4016 */ 4017 static void 4018 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) 4019 { 4020 int i, j; 4021 vdev_cmd_data_t *data = &vcdl->data[0]; 4022 4023 if (vcdl->count == 0 || data == NULL) 4024 return; 4025 4026 /* 4027 * Each vdev cmd should have the same column names unless the user did 4028 * something weird with their cmd. Just take the column names from the 4029 * first vdev and assume it works for all of them. 4030 */ 4031 for (i = 0; i < vcdl->uniq_cols_cnt; i++) { 4032 printf(" "); 4033 if (use_dashes) { 4034 for (j = 0; j < vcdl->uniq_cols_width[i]; j++) 4035 printf("-"); 4036 } else { 4037 printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i], 4038 vcdl->uniq_cols[i]); 4039 } 4040 } 4041 } 4042 4043 4044 /* 4045 * Utility function to print out a line of dashes like: 4046 * 4047 * -------------------------------- ----- ----- ----- ----- ----- 4048 * 4049 * ...or a dashed named-row line like: 4050 * 4051 * logs - - - - - 4052 * 4053 * @cb: iostat data 4054 * 4055 * @force_column_width If non-zero, use the value as the column width. 4056 * Otherwise use the default column widths. 4057 * 4058 * @name: Print a dashed named-row line starting 4059 * with @name. Otherwise, print a regular 4060 * dashed line. 4061 */ 4062 static void 4063 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, 4064 const char *name) 4065 { 4066 int i; 4067 unsigned int namewidth; 4068 uint64_t flags = cb->cb_flags; 4069 uint64_t f; 4070 int idx; 4071 const name_and_columns_t *labels; 4072 const char *title; 4073 4074 4075 if (cb->cb_flags & IOS_ANYHISTO_M) { 4076 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 4077 } else if (cb->cb_vdev_names_count) { 4078 title = "vdev"; 4079 } else { 4080 title = "pool"; 4081 } 4082 4083 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 4084 name ? strlen(name) : 0); 4085 4086 4087 if (name) { 4088 printf("%-*s", namewidth, name); 4089 } else { 4090 for (i = 0; i < namewidth; i++) 4091 (void) printf("-"); 4092 } 4093 4094 /* For each bit in flags */ 4095 for (f = flags; f; f &= ~(1ULL << idx)) { 4096 unsigned int column_width; 4097 idx = lowbit64(f) - 1; 4098 if (force_column_width) 4099 column_width = force_column_width; 4100 else 4101 column_width = default_column_width(cb, idx); 4102 4103 labels = iostat_bottom_labels[idx]; 4104 for (i = 0; i < label_array_len(labels); i++) { 4105 if (name) 4106 printf(" %*s-", column_width - 1, " "); 4107 else 4108 printf(" %.*s", column_width, 4109 "--------------------"); 4110 } 4111 } 4112 } 4113 4114 4115 static void 4116 print_iostat_separator_impl(iostat_cbdata_t *cb, 4117 unsigned int force_column_width) 4118 { 4119 print_iostat_dashes(cb, force_column_width, NULL); 4120 } 4121 4122 static void 4123 print_iostat_separator(iostat_cbdata_t *cb) 4124 { 4125 print_iostat_separator_impl(cb, 0); 4126 } 4127 4128 static void 4129 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, 4130 const char *histo_vdev_name) 4131 { 4132 unsigned int namewidth; 4133 const char *title; 4134 4135 if (cb->cb_flags & IOS_ANYHISTO_M) { 4136 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 4137 } else if (cb->cb_vdev_names_count) { 4138 title = "vdev"; 4139 } else { 4140 title = "pool"; 4141 } 4142 4143 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 4144 histo_vdev_name ? strlen(histo_vdev_name) : 0); 4145 4146 if (histo_vdev_name) 4147 printf("%-*s", namewidth, histo_vdev_name); 4148 else 4149 printf("%*s", namewidth, ""); 4150 4151 4152 print_iostat_labels(cb, force_column_width, iostat_top_labels); 4153 printf("\n"); 4154 4155 printf("%-*s", namewidth, title); 4156 4157 print_iostat_labels(cb, force_column_width, iostat_bottom_labels); 4158 if (cb->vcdl != NULL) 4159 print_cmd_columns(cb->vcdl, 0); 4160 4161 printf("\n"); 4162 4163 print_iostat_separator_impl(cb, force_column_width); 4164 4165 if (cb->vcdl != NULL) 4166 print_cmd_columns(cb->vcdl, 1); 4167 4168 printf("\n"); 4169 } 4170 4171 static void 4172 print_iostat_header(iostat_cbdata_t *cb) 4173 { 4174 print_iostat_header_impl(cb, 0, NULL); 4175 } 4176 4177 4178 /* 4179 * Display a single statistic. 4180 */ 4181 static void 4182 print_one_stat(uint64_t value, enum zfs_nicenum_format format, 4183 unsigned int column_size, boolean_t scripted) 4184 { 4185 char buf[64]; 4186 4187 zfs_nicenum_format(value, buf, sizeof (buf), format); 4188 4189 if (scripted) 4190 printf("\t%s", buf); 4191 else 4192 printf(" %*s", column_size, buf); 4193 } 4194 4195 /* 4196 * Calculate the default vdev stats 4197 * 4198 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting 4199 * stats into calcvs. 4200 */ 4201 static void 4202 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, 4203 vdev_stat_t *calcvs) 4204 { 4205 int i; 4206 4207 memcpy(calcvs, newvs, sizeof (*calcvs)); 4208 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) 4209 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); 4210 4211 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) 4212 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); 4213 } 4214 4215 /* 4216 * Internal representation of the extended iostats data. 4217 * 4218 * The extended iostat stats are exported in nvlists as either uint64_t arrays 4219 * or single uint64_t's. We make both look like arrays to make them easier 4220 * to process. In order to make single uint64_t's look like arrays, we set 4221 * __data to the stat data, and then set *data = &__data with count = 1. Then, 4222 * we can just use *data and count. 4223 */ 4224 struct stat_array { 4225 uint64_t *data; 4226 uint_t count; /* Number of entries in data[] */ 4227 uint64_t __data; /* Only used when data is a single uint64_t */ 4228 }; 4229 4230 static uint64_t 4231 stat_histo_max(struct stat_array *nva, unsigned int len) 4232 { 4233 uint64_t max = 0; 4234 int i; 4235 for (i = 0; i < len; i++) 4236 max = MAX(max, array64_max(nva[i].data, nva[i].count)); 4237 4238 return (max); 4239 } 4240 4241 /* 4242 * Helper function to lookup a uint64_t array or uint64_t value and store its 4243 * data as a stat_array. If the nvpair is a single uint64_t value, then we make 4244 * it look like a one element array to make it easier to process. 4245 */ 4246 static int 4247 nvpair64_to_stat_array(nvlist_t *nvl, const char *name, 4248 struct stat_array *nva) 4249 { 4250 nvpair_t *tmp; 4251 int ret; 4252 4253 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); 4254 switch (nvpair_type(tmp)) { 4255 case DATA_TYPE_UINT64_ARRAY: 4256 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); 4257 break; 4258 case DATA_TYPE_UINT64: 4259 ret = nvpair_value_uint64(tmp, &nva->__data); 4260 nva->data = &nva->__data; 4261 nva->count = 1; 4262 break; 4263 default: 4264 /* Not a uint64_t */ 4265 ret = EINVAL; 4266 break; 4267 } 4268 4269 return (ret); 4270 } 4271 4272 /* 4273 * Given a list of nvlist names, look up the extended stats in newnv and oldnv, 4274 * subtract them, and return the results in a newly allocated stat_array. 4275 * You must free the returned array after you are done with it with 4276 * free_calc_stats(). 4277 * 4278 * Additionally, you can set "oldnv" to NULL if you simply want the newnv 4279 * values. 4280 */ 4281 static struct stat_array * 4282 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, 4283 nvlist_t *newnv) 4284 { 4285 nvlist_t *oldnvx = NULL, *newnvx; 4286 struct stat_array *oldnva, *newnva, *calcnva; 4287 int i, j; 4288 unsigned int alloc_size = (sizeof (struct stat_array)) * len; 4289 4290 /* Extract our extended stats nvlist from the main list */ 4291 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, 4292 &newnvx) == 0); 4293 if (oldnv) { 4294 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, 4295 &oldnvx) == 0); 4296 } 4297 4298 newnva = safe_malloc(alloc_size); 4299 oldnva = safe_malloc(alloc_size); 4300 calcnva = safe_malloc(alloc_size); 4301 4302 for (j = 0; j < len; j++) { 4303 verify(nvpair64_to_stat_array(newnvx, names[j], 4304 &newnva[j]) == 0); 4305 calcnva[j].count = newnva[j].count; 4306 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); 4307 calcnva[j].data = safe_malloc(alloc_size); 4308 memcpy(calcnva[j].data, newnva[j].data, alloc_size); 4309 4310 if (oldnvx) { 4311 verify(nvpair64_to_stat_array(oldnvx, names[j], 4312 &oldnva[j]) == 0); 4313 for (i = 0; i < oldnva[j].count; i++) 4314 calcnva[j].data[i] -= oldnva[j].data[i]; 4315 } 4316 } 4317 free(newnva); 4318 free(oldnva); 4319 return (calcnva); 4320 } 4321 4322 static void 4323 free_calc_stats(struct stat_array *nva, unsigned int len) 4324 { 4325 int i; 4326 for (i = 0; i < len; i++) 4327 free(nva[i].data); 4328 4329 free(nva); 4330 } 4331 4332 static void 4333 print_iostat_histo(struct stat_array *nva, unsigned int len, 4334 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, 4335 double scale) 4336 { 4337 int i, j; 4338 char buf[6]; 4339 uint64_t val; 4340 enum zfs_nicenum_format format; 4341 unsigned int buckets; 4342 unsigned int start_bucket; 4343 4344 if (cb->cb_literal) 4345 format = ZFS_NICENUM_RAW; 4346 else 4347 format = ZFS_NICENUM_1024; 4348 4349 /* All these histos are the same size, so just use nva[0].count */ 4350 buckets = nva[0].count; 4351 4352 if (cb->cb_flags & IOS_RQ_HISTO_M) { 4353 /* Start at 512 - req size should never be lower than this */ 4354 start_bucket = 9; 4355 } else { 4356 start_bucket = 0; 4357 } 4358 4359 for (j = start_bucket; j < buckets; j++) { 4360 /* Print histogram bucket label */ 4361 if (cb->cb_flags & IOS_L_HISTO_M) { 4362 /* Ending range of this bucket */ 4363 val = (1UL << (j + 1)) - 1; 4364 zfs_nicetime(val, buf, sizeof (buf)); 4365 } else { 4366 /* Request size (starting range of bucket) */ 4367 val = (1UL << j); 4368 zfs_nicenum(val, buf, sizeof (buf)); 4369 } 4370 4371 if (cb->cb_scripted) 4372 printf("%llu", (u_longlong_t)val); 4373 else 4374 printf("%-*s", namewidth, buf); 4375 4376 /* Print the values on the line */ 4377 for (i = 0; i < len; i++) { 4378 print_one_stat(nva[i].data[j] * scale, format, 4379 column_width, cb->cb_scripted); 4380 } 4381 printf("\n"); 4382 } 4383 } 4384 4385 static void 4386 print_solid_separator(unsigned int length) 4387 { 4388 while (length--) 4389 printf("-"); 4390 printf("\n"); 4391 } 4392 4393 static void 4394 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, 4395 nvlist_t *newnv, double scale, const char *name) 4396 { 4397 unsigned int column_width; 4398 unsigned int namewidth; 4399 unsigned int entire_width; 4400 enum iostat_type type; 4401 struct stat_array *nva; 4402 const char **names; 4403 unsigned int names_len; 4404 4405 /* What type of histo are we? */ 4406 type = IOS_HISTO_IDX(cb->cb_flags); 4407 4408 /* Get NULL-terminated array of nvlist names for our histo */ 4409 names = vsx_type_to_nvlist[type]; 4410 names_len = str_array_len(names); /* num of names */ 4411 4412 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); 4413 4414 if (cb->cb_literal) { 4415 column_width = MAX(5, 4416 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); 4417 } else { 4418 column_width = 5; 4419 } 4420 4421 namewidth = MAX(cb->cb_namewidth, 4422 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); 4423 4424 /* 4425 * Calculate the entire line width of what we're printing. The 4426 * +2 is for the two spaces between columns: 4427 */ 4428 /* read write */ 4429 /* ----- ----- */ 4430 /* |___| <---------- column_width */ 4431 /* */ 4432 /* |__________| <--- entire_width */ 4433 /* */ 4434 entire_width = namewidth + (column_width + 2) * 4435 label_array_len(iostat_bottom_labels[type]); 4436 4437 if (cb->cb_scripted) 4438 printf("%s\n", name); 4439 else 4440 print_iostat_header_impl(cb, column_width, name); 4441 4442 print_iostat_histo(nva, names_len, cb, column_width, 4443 namewidth, scale); 4444 4445 free_calc_stats(nva, names_len); 4446 if (!cb->cb_scripted) 4447 print_solid_separator(entire_width); 4448 } 4449 4450 /* 4451 * Calculate the average latency of a power-of-two latency histogram 4452 */ 4453 static uint64_t 4454 single_histo_average(uint64_t *histo, unsigned int buckets) 4455 { 4456 int i; 4457 uint64_t count = 0, total = 0; 4458 4459 for (i = 0; i < buckets; i++) { 4460 /* 4461 * Our buckets are power-of-two latency ranges. Use the 4462 * midpoint latency of each bucket to calculate the average. 4463 * For example: 4464 * 4465 * Bucket Midpoint 4466 * 8ns-15ns: 12ns 4467 * 16ns-31ns: 24ns 4468 * ... 4469 */ 4470 if (histo[i] != 0) { 4471 total += histo[i] * (((1UL << i) + ((1UL << i)/2))); 4472 count += histo[i]; 4473 } 4474 } 4475 4476 /* Prevent divide by zero */ 4477 return (count == 0 ? 0 : total / count); 4478 } 4479 4480 static void 4481 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv, 4482 nvlist_t *newnv) 4483 { 4484 int i; 4485 uint64_t val; 4486 const char *names[] = { 4487 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, 4488 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, 4489 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, 4490 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, 4491 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, 4492 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, 4493 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, 4494 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, 4495 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, 4496 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, 4497 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE, 4498 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, 4499 }; 4500 4501 struct stat_array *nva; 4502 4503 unsigned int column_width = default_column_width(cb, IOS_QUEUES); 4504 enum zfs_nicenum_format format; 4505 4506 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); 4507 4508 if (cb->cb_literal) 4509 format = ZFS_NICENUM_RAW; 4510 else 4511 format = ZFS_NICENUM_1024; 4512 4513 for (i = 0; i < ARRAY_SIZE(names); i++) { 4514 val = nva[i].data[0]; 4515 print_one_stat(val, format, column_width, cb->cb_scripted); 4516 } 4517 4518 free_calc_stats(nva, ARRAY_SIZE(names)); 4519 } 4520 4521 static void 4522 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, 4523 nvlist_t *newnv) 4524 { 4525 int i; 4526 uint64_t val; 4527 const char *names[] = { 4528 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 4529 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 4530 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 4531 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 4532 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, 4533 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, 4534 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, 4535 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, 4536 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, 4537 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 4538 }; 4539 struct stat_array *nva; 4540 4541 unsigned int column_width = default_column_width(cb, IOS_LATENCY); 4542 enum zfs_nicenum_format format; 4543 4544 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); 4545 4546 if (cb->cb_literal) 4547 format = ZFS_NICENUM_RAWTIME; 4548 else 4549 format = ZFS_NICENUM_TIME; 4550 4551 /* Print our avg latencies on the line */ 4552 for (i = 0; i < ARRAY_SIZE(names); i++) { 4553 /* Compute average latency for a latency histo */ 4554 val = single_histo_average(nva[i].data, nva[i].count); 4555 print_one_stat(val, format, column_width, cb->cb_scripted); 4556 } 4557 free_calc_stats(nva, ARRAY_SIZE(names)); 4558 } 4559 4560 /* 4561 * Print default statistics (capacity/operations/bandwidth) 4562 */ 4563 static void 4564 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) 4565 { 4566 unsigned int column_width = default_column_width(cb, IOS_DEFAULT); 4567 enum zfs_nicenum_format format; 4568 char na; /* char to print for "not applicable" values */ 4569 4570 if (cb->cb_literal) { 4571 format = ZFS_NICENUM_RAW; 4572 na = '0'; 4573 } else { 4574 format = ZFS_NICENUM_1024; 4575 na = '-'; 4576 } 4577 4578 /* only toplevel vdevs have capacity stats */ 4579 if (vs->vs_space == 0) { 4580 if (cb->cb_scripted) 4581 printf("\t%c\t%c", na, na); 4582 else 4583 printf(" %*c %*c", column_width, na, column_width, 4584 na); 4585 } else { 4586 print_one_stat(vs->vs_alloc, format, column_width, 4587 cb->cb_scripted); 4588 print_one_stat(vs->vs_space - vs->vs_alloc, format, 4589 column_width, cb->cb_scripted); 4590 } 4591 4592 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), 4593 format, column_width, cb->cb_scripted); 4594 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), 4595 format, column_width, cb->cb_scripted); 4596 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), 4597 format, column_width, cb->cb_scripted); 4598 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), 4599 format, column_width, cb->cb_scripted); 4600 } 4601 4602 static const char *class_name[] = { 4603 VDEV_ALLOC_BIAS_DEDUP, 4604 VDEV_ALLOC_BIAS_SPECIAL, 4605 VDEV_ALLOC_CLASS_LOGS 4606 }; 4607 4608 /* 4609 * Print out all the statistics for the given vdev. This can either be the 4610 * toplevel configuration, or called recursively. If 'name' is NULL, then this 4611 * is a verbose output, and we don't want to display the toplevel pool stats. 4612 * 4613 * Returns the number of stat lines printed. 4614 */ 4615 static unsigned int 4616 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 4617 nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 4618 { 4619 nvlist_t **oldchild, **newchild; 4620 uint_t c, children, oldchildren; 4621 vdev_stat_t *oldvs, *newvs, *calcvs; 4622 vdev_stat_t zerovs = { 0 }; 4623 char *vname; 4624 int i; 4625 int ret = 0; 4626 uint64_t tdelta; 4627 double scale; 4628 4629 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 4630 return (ret); 4631 4632 calcvs = safe_malloc(sizeof (*calcvs)); 4633 4634 if (oldnv != NULL) { 4635 verify(nvlist_lookup_uint64_array(oldnv, 4636 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 4637 } else { 4638 oldvs = &zerovs; 4639 } 4640 4641 /* Do we only want to see a specific vdev? */ 4642 for (i = 0; i < cb->cb_vdev_names_count; i++) { 4643 /* Yes we do. Is this the vdev? */ 4644 if (strcmp(name, cb->cb_vdev_names[i]) == 0) { 4645 /* 4646 * This is our vdev. Since it is the only vdev we 4647 * will be displaying, make depth = 0 so that it 4648 * doesn't get indented. 4649 */ 4650 depth = 0; 4651 break; 4652 } 4653 } 4654 4655 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) { 4656 /* Couldn't match the name */ 4657 goto children; 4658 } 4659 4660 4661 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 4662 (uint64_t **)&newvs, &c) == 0); 4663 4664 /* 4665 * Print the vdev name unless it's is a histogram. Histograms 4666 * display the vdev name in the header itself. 4667 */ 4668 if (!(cb->cb_flags & IOS_ANYHISTO_M)) { 4669 if (cb->cb_scripted) { 4670 printf("%s", name); 4671 } else { 4672 if (strlen(name) + depth > cb->cb_namewidth) 4673 (void) printf("%*s%s", depth, "", name); 4674 else 4675 (void) printf("%*s%s%*s", depth, "", name, 4676 (int)(cb->cb_namewidth - strlen(name) - 4677 depth), ""); 4678 } 4679 } 4680 4681 /* Calculate our scaling factor */ 4682 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 4683 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { 4684 /* 4685 * If we specify printing histograms with no time interval, then 4686 * print the histogram numbers over the entire lifetime of the 4687 * vdev. 4688 */ 4689 scale = 1; 4690 } else { 4691 if (tdelta == 0) 4692 scale = 1.0; 4693 else 4694 scale = (double)NANOSEC / tdelta; 4695 } 4696 4697 if (cb->cb_flags & IOS_DEFAULT_M) { 4698 calc_default_iostats(oldvs, newvs, calcvs); 4699 print_iostat_default(calcvs, cb, scale); 4700 } 4701 if (cb->cb_flags & IOS_LATENCY_M) 4702 print_iostat_latency(cb, oldnv, newnv); 4703 if (cb->cb_flags & IOS_QUEUES_M) 4704 print_iostat_queues(cb, oldnv, newnv); 4705 if (cb->cb_flags & IOS_ANYHISTO_M) { 4706 printf("\n"); 4707 print_iostat_histos(cb, oldnv, newnv, scale, name); 4708 } 4709 4710 if (cb->vcdl != NULL) { 4711 char *path; 4712 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, 4713 &path) == 0) { 4714 printf(" "); 4715 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); 4716 } 4717 } 4718 4719 if (!(cb->cb_flags & IOS_ANYHISTO_M)) 4720 printf("\n"); 4721 4722 ret++; 4723 4724 children: 4725 4726 free(calcvs); 4727 4728 if (!cb->cb_verbose) 4729 return (ret); 4730 4731 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 4732 &newchild, &children) != 0) 4733 return (ret); 4734 4735 if (oldnv) { 4736 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 4737 &oldchild, &oldchildren) != 0) 4738 return (ret); 4739 4740 children = MIN(oldchildren, children); 4741 } 4742 4743 /* 4744 * print normal top-level devices 4745 */ 4746 for (c = 0; c < children; c++) { 4747 uint64_t ishole = B_FALSE, islog = B_FALSE; 4748 4749 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 4750 &ishole); 4751 4752 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 4753 &islog); 4754 4755 if (ishole || islog) 4756 continue; 4757 4758 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 4759 continue; 4760 4761 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4762 cb->cb_name_flags); 4763 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 4764 newchild[c], cb, depth + 2); 4765 free(vname); 4766 } 4767 4768 /* 4769 * print all other top-level devices 4770 */ 4771 for (uint_t n = 0; n < 3; n++) { 4772 boolean_t printed = B_FALSE; 4773 4774 for (c = 0; c < children; c++) { 4775 uint64_t islog = B_FALSE; 4776 char *bias = NULL; 4777 char *type = NULL; 4778 4779 (void) nvlist_lookup_uint64(newchild[c], 4780 ZPOOL_CONFIG_IS_LOG, &islog); 4781 if (islog) { 4782 bias = VDEV_ALLOC_CLASS_LOGS; 4783 } else { 4784 (void) nvlist_lookup_string(newchild[c], 4785 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 4786 (void) nvlist_lookup_string(newchild[c], 4787 ZPOOL_CONFIG_TYPE, &type); 4788 } 4789 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 4790 continue; 4791 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 4792 continue; 4793 4794 if (!printed) { 4795 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && 4796 !cb->cb_scripted && !cb->cb_vdev_names) { 4797 print_iostat_dashes(cb, 0, 4798 class_name[n]); 4799 } 4800 printf("\n"); 4801 printed = B_TRUE; 4802 } 4803 4804 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4805 cb->cb_name_flags); 4806 ret += print_vdev_stats(zhp, vname, oldnv ? 4807 oldchild[c] : NULL, newchild[c], cb, depth + 2); 4808 free(vname); 4809 } 4810 } 4811 4812 /* 4813 * Include level 2 ARC devices in iostat output 4814 */ 4815 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 4816 &newchild, &children) != 0) 4817 return (ret); 4818 4819 if (oldnv) { 4820 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 4821 &oldchild, &oldchildren) != 0) 4822 return (ret); 4823 4824 children = MIN(oldchildren, children); 4825 } 4826 4827 if (children > 0) { 4828 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && 4829 !cb->cb_vdev_names) { 4830 print_iostat_dashes(cb, 0, "cache"); 4831 } 4832 printf("\n"); 4833 4834 for (c = 0; c < children; c++) { 4835 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 4836 cb->cb_name_flags); 4837 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] 4838 : NULL, newchild[c], cb, depth + 2); 4839 free(vname); 4840 } 4841 } 4842 4843 return (ret); 4844 } 4845 4846 static int 4847 refresh_iostat(zpool_handle_t *zhp, void *data) 4848 { 4849 iostat_cbdata_t *cb = data; 4850 boolean_t missing; 4851 4852 /* 4853 * If the pool has disappeared, remove it from the list and continue. 4854 */ 4855 if (zpool_refresh_stats(zhp, &missing) != 0) 4856 return (-1); 4857 4858 if (missing) 4859 pool_list_remove(cb->cb_list, zhp); 4860 4861 return (0); 4862 } 4863 4864 /* 4865 * Callback to print out the iostats for the given pool. 4866 */ 4867 static int 4868 print_iostat(zpool_handle_t *zhp, void *data) 4869 { 4870 iostat_cbdata_t *cb = data; 4871 nvlist_t *oldconfig, *newconfig; 4872 nvlist_t *oldnvroot, *newnvroot; 4873 int ret; 4874 4875 newconfig = zpool_get_config(zhp, &oldconfig); 4876 4877 if (cb->cb_iteration == 1) 4878 oldconfig = NULL; 4879 4880 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 4881 &newnvroot) == 0); 4882 4883 if (oldconfig == NULL) 4884 oldnvroot = NULL; 4885 else 4886 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 4887 &oldnvroot) == 0); 4888 4889 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, 4890 cb, 0); 4891 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && 4892 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) { 4893 print_iostat_separator(cb); 4894 if (cb->vcdl != NULL) { 4895 print_cmd_columns(cb->vcdl, 1); 4896 } 4897 printf("\n"); 4898 } 4899 4900 return (ret); 4901 } 4902 4903 static int 4904 get_columns(void) 4905 { 4906 struct winsize ws; 4907 int columns = 80; 4908 int error; 4909 4910 if (isatty(STDOUT_FILENO)) { 4911 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); 4912 if (error == 0) 4913 columns = ws.ws_col; 4914 } else { 4915 columns = 999; 4916 } 4917 4918 return (columns); 4919 } 4920 4921 /* 4922 * Return the required length of the pool/vdev name column. The minimum 4923 * allowed width and output formatting flags must be provided. 4924 */ 4925 static int 4926 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) 4927 { 4928 nvlist_t *config, *nvroot; 4929 int width = min_width; 4930 4931 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 4932 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4933 &nvroot) == 0); 4934 unsigned int poolname_len = strlen(zpool_get_name(zhp)); 4935 if (verbose == B_FALSE) { 4936 width = MAX(poolname_len, min_width); 4937 } else { 4938 width = MAX(poolname_len, 4939 max_width(zhp, nvroot, 0, min_width, flags)); 4940 } 4941 } 4942 4943 return (width); 4944 } 4945 4946 /* 4947 * Parse the input string, get the 'interval' and 'count' value if there is one. 4948 */ 4949 static void 4950 get_interval_count(int *argcp, char **argv, float *iv, 4951 unsigned long *cnt) 4952 { 4953 float interval = 0; 4954 unsigned long count = 0; 4955 int argc = *argcp; 4956 4957 /* 4958 * Determine if the last argument is an integer or a pool name 4959 */ 4960 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 4961 char *end; 4962 4963 errno = 0; 4964 interval = strtof(argv[argc - 1], &end); 4965 4966 if (*end == '\0' && errno == 0) { 4967 if (interval == 0) { 4968 (void) fprintf(stderr, gettext("interval " 4969 "cannot be zero\n")); 4970 usage(B_FALSE); 4971 } 4972 /* 4973 * Ignore the last parameter 4974 */ 4975 argc--; 4976 } else { 4977 /* 4978 * If this is not a valid number, just plow on. The 4979 * user will get a more informative error message later 4980 * on. 4981 */ 4982 interval = 0; 4983 } 4984 } 4985 4986 /* 4987 * If the last argument is also an integer, then we have both a count 4988 * and an interval. 4989 */ 4990 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 4991 char *end; 4992 4993 errno = 0; 4994 count = interval; 4995 interval = strtof(argv[argc - 1], &end); 4996 4997 if (*end == '\0' && errno == 0) { 4998 if (interval == 0) { 4999 (void) fprintf(stderr, gettext("interval " 5000 "cannot be zero\n")); 5001 usage(B_FALSE); 5002 } 5003 5004 /* 5005 * Ignore the last parameter 5006 */ 5007 argc--; 5008 } else { 5009 interval = 0; 5010 } 5011 } 5012 5013 *iv = interval; 5014 *cnt = count; 5015 *argcp = argc; 5016 } 5017 5018 static void 5019 get_timestamp_arg(char c) 5020 { 5021 if (c == 'u') 5022 timestamp_fmt = UDATE; 5023 else if (c == 'd') 5024 timestamp_fmt = DDATE; 5025 else 5026 usage(B_FALSE); 5027 } 5028 5029 /* 5030 * Return stat flags that are supported by all pools by both the module and 5031 * zpool iostat. "*data" should be initialized to all 0xFFs before running. 5032 * It will get ANDed down until only the flags that are supported on all pools 5033 * remain. 5034 */ 5035 static int 5036 get_stat_flags_cb(zpool_handle_t *zhp, void *data) 5037 { 5038 uint64_t *mask = data; 5039 nvlist_t *config, *nvroot, *nvx; 5040 uint64_t flags = 0; 5041 int i, j; 5042 5043 config = zpool_get_config(zhp, NULL); 5044 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5045 &nvroot) == 0); 5046 5047 /* Default stats are always supported, but for completeness.. */ 5048 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) 5049 flags |= IOS_DEFAULT_M; 5050 5051 /* Get our extended stats nvlist from the main list */ 5052 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, 5053 &nvx) != 0) { 5054 /* 5055 * No extended stats; they're probably running an older 5056 * module. No big deal, we support that too. 5057 */ 5058 goto end; 5059 } 5060 5061 /* For each extended stat, make sure all its nvpairs are supported */ 5062 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { 5063 if (!vsx_type_to_nvlist[j][0]) 5064 continue; 5065 5066 /* Start off by assuming the flag is supported, then check */ 5067 flags |= (1ULL << j); 5068 for (i = 0; vsx_type_to_nvlist[j][i]; i++) { 5069 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { 5070 /* flag isn't supported */ 5071 flags = flags & ~(1ULL << j); 5072 break; 5073 } 5074 } 5075 } 5076 end: 5077 *mask = *mask & flags; 5078 return (0); 5079 } 5080 5081 /* 5082 * Return a bitmask of stats that are supported on all pools by both the module 5083 * and zpool iostat. 5084 */ 5085 static uint64_t 5086 get_stat_flags(zpool_list_t *list) 5087 { 5088 uint64_t mask = -1; 5089 5090 /* 5091 * get_stat_flags_cb() will lop off bits from "mask" until only the 5092 * flags that are supported on all pools remain. 5093 */ 5094 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); 5095 return (mask); 5096 } 5097 5098 /* 5099 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise. 5100 */ 5101 static int 5102 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data) 5103 { 5104 iostat_cbdata_t *cb = cb_data; 5105 char *name = NULL; 5106 int ret = 0; 5107 5108 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags); 5109 5110 if (strcmp(name, cb->cb_vdev_names[0]) == 0) 5111 ret = 1; /* match */ 5112 free(name); 5113 5114 return (ret); 5115 } 5116 5117 /* 5118 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise. 5119 */ 5120 static int 5121 is_vdev(zpool_handle_t *zhp, void *cb_data) 5122 { 5123 return (for_each_vdev(zhp, is_vdev_cb, cb_data)); 5124 } 5125 5126 /* 5127 * Check if vdevs are in a pool 5128 * 5129 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise 5130 * return 0. If pool_name is NULL, then search all pools. 5131 */ 5132 static int 5133 are_vdevs_in_pool(int argc, char **argv, char *pool_name, 5134 iostat_cbdata_t *cb) 5135 { 5136 char **tmp_name; 5137 int ret = 0; 5138 int i; 5139 int pool_count = 0; 5140 5141 if ((argc == 0) || !*argv) 5142 return (0); 5143 5144 if (pool_name) 5145 pool_count = 1; 5146 5147 /* Temporarily hijack cb_vdev_names for a second... */ 5148 tmp_name = cb->cb_vdev_names; 5149 5150 /* Go though our list of prospective vdev names */ 5151 for (i = 0; i < argc; i++) { 5152 cb->cb_vdev_names = argv + i; 5153 5154 /* Is this name a vdev in our pools? */ 5155 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, 5156 B_FALSE, is_vdev, cb); 5157 if (!ret) { 5158 /* No match */ 5159 break; 5160 } 5161 } 5162 5163 cb->cb_vdev_names = tmp_name; 5164 5165 return (ret); 5166 } 5167 5168 static int 5169 is_pool_cb(zpool_handle_t *zhp, void *data) 5170 { 5171 char *name = data; 5172 if (strcmp(name, zpool_get_name(zhp)) == 0) 5173 return (1); 5174 5175 return (0); 5176 } 5177 5178 /* 5179 * Do we have a pool named *name? If so, return 1, otherwise 0. 5180 */ 5181 static int 5182 is_pool(char *name) 5183 { 5184 return (for_each_pool(0, NULL, B_TRUE, NULL, B_FALSE, is_pool_cb, 5185 name)); 5186 } 5187 5188 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ 5189 static int 5190 are_all_pools(int argc, char **argv) 5191 { 5192 if ((argc == 0) || !*argv) 5193 return (0); 5194 5195 while (--argc >= 0) 5196 if (!is_pool(argv[argc])) 5197 return (0); 5198 5199 return (1); 5200 } 5201 5202 /* 5203 * Helper function to print out vdev/pool names we can't resolve. Used for an 5204 * error message. 5205 */ 5206 static void 5207 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, 5208 iostat_cbdata_t *cb) 5209 { 5210 int i; 5211 char *name; 5212 char *str; 5213 for (i = 0; i < argc; i++) { 5214 name = argv[i]; 5215 5216 if (is_pool(name)) 5217 str = gettext("pool"); 5218 else if (are_vdevs_in_pool(1, &name, pool_name, cb)) 5219 str = gettext("vdev in this pool"); 5220 else if (are_vdevs_in_pool(1, &name, NULL, cb)) 5221 str = gettext("vdev in another pool"); 5222 else 5223 str = gettext("unknown"); 5224 5225 fprintf(stderr, "\t%s (%s)\n", name, str); 5226 } 5227 } 5228 5229 /* 5230 * Same as get_interval_count(), but with additional checks to not misinterpret 5231 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in 5232 * cb.cb_name_flags. 5233 */ 5234 static void 5235 get_interval_count_filter_guids(int *argc, char **argv, float *interval, 5236 unsigned long *count, iostat_cbdata_t *cb) 5237 { 5238 char **tmpargv = argv; 5239 int argc_for_interval = 0; 5240 5241 /* Is the last arg an interval value? Or a guid? */ 5242 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) { 5243 /* 5244 * The last arg is not a guid, so it's probably an 5245 * interval value. 5246 */ 5247 argc_for_interval++; 5248 5249 if (*argc >= 2 && 5250 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) { 5251 /* 5252 * The 2nd to last arg is not a guid, so it's probably 5253 * an interval value. 5254 */ 5255 argc_for_interval++; 5256 } 5257 } 5258 5259 /* Point to our list of possible intervals */ 5260 tmpargv = &argv[*argc - argc_for_interval]; 5261 5262 *argc = *argc - argc_for_interval; 5263 get_interval_count(&argc_for_interval, tmpargv, 5264 interval, count); 5265 } 5266 5267 /* 5268 * Floating point sleep(). Allows you to pass in a floating point value for 5269 * seconds. 5270 */ 5271 static void 5272 fsleep(float sec) 5273 { 5274 struct timespec req; 5275 req.tv_sec = floor(sec); 5276 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC; 5277 nanosleep(&req, NULL); 5278 } 5279 5280 /* 5281 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or 5282 * if we were unable to determine its size. 5283 */ 5284 static int 5285 terminal_height(void) 5286 { 5287 struct winsize win; 5288 5289 if (isatty(STDOUT_FILENO) == 0) 5290 return (-1); 5291 5292 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) 5293 return (win.ws_row); 5294 5295 return (-1); 5296 } 5297 5298 /* 5299 * Run one of the zpool status/iostat -c scripts with the help (-h) option and 5300 * print the result. 5301 * 5302 * name: Short name of the script ('iostat'). 5303 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); 5304 */ 5305 static void 5306 print_zpool_script_help(char *name, char *path) 5307 { 5308 char *argv[] = {path, "-h", NULL}; 5309 char **lines = NULL; 5310 int lines_cnt = 0; 5311 int rc; 5312 5313 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, 5314 &lines_cnt); 5315 if (rc != 0 || lines == NULL || lines_cnt <= 0) { 5316 if (lines != NULL) 5317 libzfs_free_str_array(lines, lines_cnt); 5318 return; 5319 } 5320 5321 for (int i = 0; i < lines_cnt; i++) 5322 if (!is_blank_str(lines[i])) 5323 printf(" %-14s %s\n", name, lines[i]); 5324 5325 libzfs_free_str_array(lines, lines_cnt); 5326 } 5327 5328 /* 5329 * Go though the zpool status/iostat -c scripts in the user's path, run their 5330 * help option (-h), and print out the results. 5331 */ 5332 static void 5333 print_zpool_dir_scripts(char *dirpath) 5334 { 5335 DIR *dir; 5336 struct dirent *ent; 5337 char fullpath[MAXPATHLEN]; 5338 struct stat dir_stat; 5339 5340 if ((dir = opendir(dirpath)) != NULL) { 5341 /* print all the files and directories within directory */ 5342 while ((ent = readdir(dir)) != NULL) { 5343 sprintf(fullpath, "%s/%s", dirpath, ent->d_name); 5344 5345 /* Print the scripts */ 5346 if (stat(fullpath, &dir_stat) == 0) 5347 if (dir_stat.st_mode & S_IXUSR && 5348 S_ISREG(dir_stat.st_mode)) 5349 print_zpool_script_help(ent->d_name, 5350 fullpath); 5351 } 5352 closedir(dir); 5353 } 5354 } 5355 5356 /* 5357 * Print out help text for all zpool status/iostat -c scripts. 5358 */ 5359 static void 5360 print_zpool_script_list(char *subcommand) 5361 { 5362 char *dir, *sp; 5363 5364 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); 5365 5366 sp = zpool_get_cmd_search_path(); 5367 if (sp == NULL) 5368 return; 5369 5370 dir = strtok(sp, ":"); 5371 while (dir != NULL) { 5372 print_zpool_dir_scripts(dir); 5373 dir = strtok(NULL, ":"); 5374 } 5375 5376 free(sp); 5377 } 5378 5379 /* 5380 * Set the minimum pool/vdev name column width. The width must be at least 10, 5381 * but may be as large as the column width - 42 so it still fits on one line. 5382 * NOTE: 42 is the width of the default capacity/operations/bandwidth output 5383 */ 5384 static int 5385 get_namewidth_iostat(zpool_handle_t *zhp, void *data) 5386 { 5387 iostat_cbdata_t *cb = data; 5388 int width, available_width; 5389 5390 /* 5391 * get_namewidth() returns the maximum width of any name in that column 5392 * for any pool/vdev/device line that will be output. 5393 */ 5394 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, 5395 cb->cb_verbose); 5396 5397 /* 5398 * The width we are calculating is the width of the header and also the 5399 * padding width for names that are less than maximum width. The stats 5400 * take up 42 characters, so the width available for names is: 5401 */ 5402 available_width = get_columns() - 42; 5403 5404 /* 5405 * If the maximum width fits on a screen, then great! Make everything 5406 * line up by justifying all lines to the same width. If that max 5407 * width is larger than what's available, the name plus stats won't fit 5408 * on one line, and justifying to that width would cause every line to 5409 * wrap on the screen. We only want lines with long names to wrap. 5410 * Limit the padding to what won't wrap. 5411 */ 5412 if (width > available_width) 5413 width = available_width; 5414 5415 /* 5416 * And regardless of whatever the screen width is (get_columns can 5417 * return 0 if the width is not known or less than 42 for a narrow 5418 * terminal) have the width be a minimum of 10. 5419 */ 5420 if (width < 10) 5421 width = 10; 5422 5423 /* Save the calculated width */ 5424 cb->cb_namewidth = width; 5425 5426 return (0); 5427 } 5428 5429 /* 5430 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] 5431 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] 5432 * [interval [count]] 5433 * 5434 * -c CMD For each vdev, run command CMD 5435 * -g Display guid for individual vdev name. 5436 * -L Follow links when resolving vdev path name. 5437 * -P Display full path for vdev name. 5438 * -v Display statistics for individual vdevs 5439 * -h Display help 5440 * -p Display values in parsable (exact) format. 5441 * -H Scripted mode. Don't display headers, and separate properties 5442 * by a single tab. 5443 * -l Display average latency 5444 * -q Display queue depths 5445 * -w Display latency histograms 5446 * -r Display request size histogram 5447 * -T Display a timestamp in date(1) or Unix format 5448 * -n Only print headers once 5449 * 5450 * This command can be tricky because we want to be able to deal with pool 5451 * creation/destruction as well as vdev configuration changes. The bulk of this 5452 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 5453 * on pool_list_update() to detect the addition of new pools. Configuration 5454 * changes are all handled within libzfs. 5455 */ 5456 int 5457 zpool_do_iostat(int argc, char **argv) 5458 { 5459 int c; 5460 int ret; 5461 int npools; 5462 float interval = 0; 5463 unsigned long count = 0; 5464 int winheight = 24; 5465 zpool_list_t *list; 5466 boolean_t verbose = B_FALSE; 5467 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; 5468 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; 5469 boolean_t omit_since_boot = B_FALSE; 5470 boolean_t guid = B_FALSE; 5471 boolean_t follow_links = B_FALSE; 5472 boolean_t full_name = B_FALSE; 5473 boolean_t headers_once = B_FALSE; 5474 iostat_cbdata_t cb = { 0 }; 5475 char *cmd = NULL; 5476 5477 /* Used for printing error message */ 5478 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', 5479 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; 5480 5481 uint64_t unsupported_flags; 5482 5483 /* check options */ 5484 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { 5485 switch (c) { 5486 case 'c': 5487 if (cmd != NULL) { 5488 fprintf(stderr, 5489 gettext("Can't set -c flag twice\n")); 5490 exit(1); 5491 } 5492 5493 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 5494 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 5495 fprintf(stderr, gettext( 5496 "Can't run -c, disabled by " 5497 "ZPOOL_SCRIPTS_ENABLED.\n")); 5498 exit(1); 5499 } 5500 5501 if ((getuid() <= 0 || geteuid() <= 0) && 5502 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 5503 fprintf(stderr, gettext( 5504 "Can't run -c with root privileges " 5505 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 5506 exit(1); 5507 } 5508 cmd = optarg; 5509 verbose = B_TRUE; 5510 break; 5511 case 'g': 5512 guid = B_TRUE; 5513 break; 5514 case 'L': 5515 follow_links = B_TRUE; 5516 break; 5517 case 'P': 5518 full_name = B_TRUE; 5519 break; 5520 case 'T': 5521 get_timestamp_arg(*optarg); 5522 break; 5523 case 'v': 5524 verbose = B_TRUE; 5525 break; 5526 case 'p': 5527 parsable = B_TRUE; 5528 break; 5529 case 'l': 5530 latency = B_TRUE; 5531 break; 5532 case 'q': 5533 queues = B_TRUE; 5534 break; 5535 case 'H': 5536 scripted = B_TRUE; 5537 break; 5538 case 'w': 5539 l_histo = B_TRUE; 5540 break; 5541 case 'r': 5542 rq_histo = B_TRUE; 5543 break; 5544 case 'y': 5545 omit_since_boot = B_TRUE; 5546 break; 5547 case 'n': 5548 headers_once = B_TRUE; 5549 break; 5550 case 'h': 5551 usage(B_FALSE); 5552 break; 5553 case '?': 5554 if (optopt == 'c') { 5555 print_zpool_script_list("iostat"); 5556 exit(0); 5557 } else { 5558 fprintf(stderr, 5559 gettext("invalid option '%c'\n"), optopt); 5560 } 5561 usage(B_FALSE); 5562 } 5563 } 5564 5565 argc -= optind; 5566 argv += optind; 5567 5568 cb.cb_literal = parsable; 5569 cb.cb_scripted = scripted; 5570 5571 if (guid) 5572 cb.cb_name_flags |= VDEV_NAME_GUID; 5573 if (follow_links) 5574 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 5575 if (full_name) 5576 cb.cb_name_flags |= VDEV_NAME_PATH; 5577 cb.cb_iteration = 0; 5578 cb.cb_namewidth = 0; 5579 cb.cb_verbose = verbose; 5580 5581 /* Get our interval and count values (if any) */ 5582 if (guid) { 5583 get_interval_count_filter_guids(&argc, argv, &interval, 5584 &count, &cb); 5585 } else { 5586 get_interval_count(&argc, argv, &interval, &count); 5587 } 5588 5589 if (argc == 0) { 5590 /* No args, so just print the defaults. */ 5591 } else if (are_all_pools(argc, argv)) { 5592 /* All the args are pool names */ 5593 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) { 5594 /* All the args are vdevs */ 5595 cb.cb_vdev_names = argv; 5596 cb.cb_vdev_names_count = argc; 5597 argc = 0; /* No pools to process */ 5598 } else if (are_all_pools(1, argv)) { 5599 /* The first arg is a pool name */ 5600 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) { 5601 /* ...and the rest are vdev names */ 5602 cb.cb_vdev_names = argv + 1; 5603 cb.cb_vdev_names_count = argc - 1; 5604 argc = 1; /* One pool to process */ 5605 } else { 5606 fprintf(stderr, gettext("Expected either a list of ")); 5607 fprintf(stderr, gettext("pools, or list of vdevs in")); 5608 fprintf(stderr, " \"%s\", ", argv[0]); 5609 fprintf(stderr, gettext("but got:\n")); 5610 error_list_unresolved_vdevs(argc - 1, argv + 1, 5611 argv[0], &cb); 5612 fprintf(stderr, "\n"); 5613 usage(B_FALSE); 5614 return (1); 5615 } 5616 } else { 5617 /* 5618 * The args don't make sense. The first arg isn't a pool name, 5619 * nor are all the args vdevs. 5620 */ 5621 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); 5622 fprintf(stderr, "\n"); 5623 return (1); 5624 } 5625 5626 if (cb.cb_vdev_names_count != 0) { 5627 /* 5628 * If user specified vdevs, it implies verbose. 5629 */ 5630 cb.cb_verbose = B_TRUE; 5631 } 5632 5633 /* 5634 * Construct the list of all interesting pools. 5635 */ 5636 ret = 0; 5637 if ((list = pool_list_get(argc, argv, NULL, parsable, &ret)) == NULL) 5638 return (1); 5639 5640 if (pool_list_count(list) == 0 && argc != 0) { 5641 pool_list_free(list); 5642 return (1); 5643 } 5644 5645 if (pool_list_count(list) == 0 && interval == 0) { 5646 pool_list_free(list); 5647 (void) fprintf(stderr, gettext("no pools available\n")); 5648 return (1); 5649 } 5650 5651 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { 5652 pool_list_free(list); 5653 (void) fprintf(stderr, 5654 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); 5655 usage(B_FALSE); 5656 return (1); 5657 } 5658 5659 if (l_histo && rq_histo) { 5660 pool_list_free(list); 5661 (void) fprintf(stderr, 5662 gettext("Only one of [-r|-w] can be passed at a time\n")); 5663 usage(B_FALSE); 5664 return (1); 5665 } 5666 5667 /* 5668 * Enter the main iostat loop. 5669 */ 5670 cb.cb_list = list; 5671 5672 if (l_histo) { 5673 /* 5674 * Histograms tables look out of place when you try to display 5675 * them with the other stats, so make a rule that you can only 5676 * print histograms by themselves. 5677 */ 5678 cb.cb_flags = IOS_L_HISTO_M; 5679 } else if (rq_histo) { 5680 cb.cb_flags = IOS_RQ_HISTO_M; 5681 } else { 5682 cb.cb_flags = IOS_DEFAULT_M; 5683 if (latency) 5684 cb.cb_flags |= IOS_LATENCY_M; 5685 if (queues) 5686 cb.cb_flags |= IOS_QUEUES_M; 5687 } 5688 5689 /* 5690 * See if the module supports all the stats we want to display. 5691 */ 5692 unsupported_flags = cb.cb_flags & ~get_stat_flags(list); 5693 if (unsupported_flags) { 5694 uint64_t f; 5695 int idx; 5696 fprintf(stderr, 5697 gettext("The loaded zfs module doesn't support:")); 5698 5699 /* for each bit set in unsupported_flags */ 5700 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { 5701 idx = lowbit64(f) - 1; 5702 fprintf(stderr, " -%c", flag_to_arg[idx]); 5703 } 5704 5705 fprintf(stderr, ". Try running a newer module.\n"); 5706 pool_list_free(list); 5707 5708 return (1); 5709 } 5710 5711 for (;;) { 5712 if ((npools = pool_list_count(list)) == 0) 5713 (void) fprintf(stderr, gettext("no pools available\n")); 5714 else { 5715 /* 5716 * If this is the first iteration and -y was supplied 5717 * we skip any printing. 5718 */ 5719 boolean_t skip = (omit_since_boot && 5720 cb.cb_iteration == 0); 5721 5722 /* 5723 * Refresh all statistics. This is done as an 5724 * explicit step before calculating the maximum name 5725 * width, so that any * configuration changes are 5726 * properly accounted for. 5727 */ 5728 (void) pool_list_iter(list, B_FALSE, refresh_iostat, 5729 &cb); 5730 5731 /* 5732 * Iterate over all pools to determine the maximum width 5733 * for the pool / device name column across all pools. 5734 */ 5735 cb.cb_namewidth = 0; 5736 (void) pool_list_iter(list, B_FALSE, 5737 get_namewidth_iostat, &cb); 5738 5739 if (timestamp_fmt != NODATE) 5740 print_timestamp(timestamp_fmt); 5741 5742 if (cmd != NULL && cb.cb_verbose && 5743 !(cb.cb_flags & IOS_ANYHISTO_M)) { 5744 cb.vcdl = all_pools_for_each_vdev_run(argc, 5745 argv, cmd, g_zfs, cb.cb_vdev_names, 5746 cb.cb_vdev_names_count, cb.cb_name_flags); 5747 } else { 5748 cb.vcdl = NULL; 5749 } 5750 5751 5752 /* 5753 * Check terminal size so we can print headers 5754 * even when terminal window has its height 5755 * changed. 5756 */ 5757 winheight = terminal_height(); 5758 /* 5759 * Are we connected to TTY? If not, headers_once 5760 * should be true, to avoid breaking scripts. 5761 */ 5762 if (winheight < 0) 5763 headers_once = B_TRUE; 5764 5765 /* 5766 * If it's the first time and we're not skipping it, 5767 * or either skip or verbose mode, print the header. 5768 * 5769 * The histogram code explicitly prints its header on 5770 * every vdev, so skip this for histograms. 5771 */ 5772 if (((++cb.cb_iteration == 1 && !skip) || 5773 (skip != verbose) || 5774 (!headers_once && 5775 (cb.cb_iteration % winheight) == 0)) && 5776 (!(cb.cb_flags & IOS_ANYHISTO_M)) && 5777 !cb.cb_scripted) 5778 print_iostat_header(&cb); 5779 5780 if (skip) { 5781 (void) fsleep(interval); 5782 continue; 5783 } 5784 5785 pool_list_iter(list, B_FALSE, print_iostat, &cb); 5786 5787 /* 5788 * If there's more than one pool, and we're not in 5789 * verbose mode (which prints a separator for us), 5790 * then print a separator. 5791 * 5792 * In addition, if we're printing specific vdevs then 5793 * we also want an ending separator. 5794 */ 5795 if (((npools > 1 && !verbose && 5796 !(cb.cb_flags & IOS_ANYHISTO_M)) || 5797 (!(cb.cb_flags & IOS_ANYHISTO_M) && 5798 cb.cb_vdev_names_count)) && 5799 !cb.cb_scripted) { 5800 print_iostat_separator(&cb); 5801 if (cb.vcdl != NULL) 5802 print_cmd_columns(cb.vcdl, 1); 5803 printf("\n"); 5804 } 5805 5806 if (cb.vcdl != NULL) 5807 free_vdev_cmd_data_list(cb.vcdl); 5808 5809 } 5810 5811 /* 5812 * Flush the output so that redirection to a file isn't buffered 5813 * indefinitely. 5814 */ 5815 (void) fflush(stdout); 5816 5817 if (interval == 0) 5818 break; 5819 5820 if (count != 0 && --count == 0) 5821 break; 5822 5823 (void) fsleep(interval); 5824 } 5825 5826 pool_list_free(list); 5827 5828 return (ret); 5829 } 5830 5831 typedef struct list_cbdata { 5832 boolean_t cb_verbose; 5833 int cb_name_flags; 5834 int cb_namewidth; 5835 boolean_t cb_scripted; 5836 zprop_list_t *cb_proplist; 5837 boolean_t cb_literal; 5838 } list_cbdata_t; 5839 5840 5841 /* 5842 * Given a list of columns to display, output appropriate headers for each one. 5843 */ 5844 static void 5845 print_header(list_cbdata_t *cb) 5846 { 5847 zprop_list_t *pl = cb->cb_proplist; 5848 char headerbuf[ZPOOL_MAXPROPLEN]; 5849 const char *header; 5850 boolean_t first = B_TRUE; 5851 boolean_t right_justify; 5852 size_t width = 0; 5853 5854 for (; pl != NULL; pl = pl->pl_next) { 5855 width = pl->pl_width; 5856 if (first && cb->cb_verbose) { 5857 /* 5858 * Reset the width to accommodate the verbose listing 5859 * of devices. 5860 */ 5861 width = cb->cb_namewidth; 5862 } 5863 5864 if (!first) 5865 (void) printf(" "); 5866 else 5867 first = B_FALSE; 5868 5869 right_justify = B_FALSE; 5870 if (pl->pl_prop != ZPROP_INVAL) { 5871 header = zpool_prop_column_name(pl->pl_prop); 5872 right_justify = zpool_prop_align_right(pl->pl_prop); 5873 } else { 5874 int i; 5875 5876 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 5877 headerbuf[i] = toupper(pl->pl_user_prop[i]); 5878 headerbuf[i] = '\0'; 5879 header = headerbuf; 5880 } 5881 5882 if (pl->pl_next == NULL && !right_justify) 5883 (void) printf("%s", header); 5884 else if (right_justify) 5885 (void) printf("%*s", (int)width, header); 5886 else 5887 (void) printf("%-*s", (int)width, header); 5888 } 5889 5890 (void) printf("\n"); 5891 } 5892 5893 /* 5894 * Given a pool and a list of properties, print out all the properties according 5895 * to the described layout. Used by zpool_do_list(). 5896 */ 5897 static void 5898 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 5899 { 5900 zprop_list_t *pl = cb->cb_proplist; 5901 boolean_t first = B_TRUE; 5902 char property[ZPOOL_MAXPROPLEN]; 5903 char *propstr; 5904 boolean_t right_justify; 5905 size_t width; 5906 5907 for (; pl != NULL; pl = pl->pl_next) { 5908 5909 width = pl->pl_width; 5910 if (first && cb->cb_verbose) { 5911 /* 5912 * Reset the width to accommodate the verbose listing 5913 * of devices. 5914 */ 5915 width = cb->cb_namewidth; 5916 } 5917 5918 if (!first) { 5919 if (cb->cb_scripted) 5920 (void) printf("\t"); 5921 else 5922 (void) printf(" "); 5923 } else { 5924 first = B_FALSE; 5925 } 5926 5927 right_justify = B_FALSE; 5928 if (pl->pl_prop != ZPROP_INVAL) { 5929 if (zpool_get_prop(zhp, pl->pl_prop, property, 5930 sizeof (property), NULL, cb->cb_literal) != 0) 5931 propstr = "-"; 5932 else 5933 propstr = property; 5934 5935 right_justify = zpool_prop_align_right(pl->pl_prop); 5936 } else if ((zpool_prop_feature(pl->pl_user_prop) || 5937 zpool_prop_unsupported(pl->pl_user_prop)) && 5938 zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 5939 sizeof (property)) == 0) { 5940 propstr = property; 5941 } else { 5942 propstr = "-"; 5943 } 5944 5945 5946 /* 5947 * If this is being called in scripted mode, or if this is the 5948 * last column and it is left-justified, don't include a width 5949 * format specifier. 5950 */ 5951 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 5952 (void) printf("%s", propstr); 5953 else if (right_justify) 5954 (void) printf("%*s", (int)width, propstr); 5955 else 5956 (void) printf("%-*s", (int)width, propstr); 5957 } 5958 5959 (void) printf("\n"); 5960 } 5961 5962 static void 5963 print_one_column(zpool_prop_t prop, uint64_t value, const char *str, 5964 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format) 5965 { 5966 char propval[64]; 5967 boolean_t fixed; 5968 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 5969 5970 switch (prop) { 5971 case ZPOOL_PROP_EXPANDSZ: 5972 case ZPOOL_PROP_CHECKPOINT: 5973 case ZPOOL_PROP_DEDUPRATIO: 5974 if (value == 0) 5975 (void) strlcpy(propval, "-", sizeof (propval)); 5976 else 5977 zfs_nicenum_format(value, propval, sizeof (propval), 5978 format); 5979 break; 5980 case ZPOOL_PROP_FRAGMENTATION: 5981 if (value == ZFS_FRAG_INVALID) { 5982 (void) strlcpy(propval, "-", sizeof (propval)); 5983 } else if (format == ZFS_NICENUM_RAW) { 5984 (void) snprintf(propval, sizeof (propval), "%llu", 5985 (unsigned long long)value); 5986 } else { 5987 (void) snprintf(propval, sizeof (propval), "%llu%%", 5988 (unsigned long long)value); 5989 } 5990 break; 5991 case ZPOOL_PROP_CAPACITY: 5992 /* capacity value is in parts-per-10,000 (aka permyriad) */ 5993 if (format == ZFS_NICENUM_RAW) 5994 (void) snprintf(propval, sizeof (propval), "%llu", 5995 (unsigned long long)value / 100); 5996 else 5997 (void) snprintf(propval, sizeof (propval), 5998 value < 1000 ? "%1.2f%%" : value < 10000 ? 5999 "%2.1f%%" : "%3.0f%%", value / 100.0); 6000 break; 6001 case ZPOOL_PROP_HEALTH: 6002 width = 8; 6003 snprintf(propval, sizeof (propval), "%-*s", (int)width, str); 6004 break; 6005 default: 6006 zfs_nicenum_format(value, propval, sizeof (propval), format); 6007 } 6008 6009 if (!valid) 6010 (void) strlcpy(propval, "-", sizeof (propval)); 6011 6012 if (scripted) 6013 (void) printf("\t%s", propval); 6014 else 6015 (void) printf(" %*s", (int)width, propval); 6016 } 6017 6018 /* 6019 * print static default line per vdev 6020 * not compatible with '-o' <proplist> option 6021 */ 6022 static void 6023 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 6024 list_cbdata_t *cb, int depth, boolean_t isspare) 6025 { 6026 nvlist_t **child; 6027 vdev_stat_t *vs; 6028 uint_t c, children; 6029 char *vname; 6030 boolean_t scripted = cb->cb_scripted; 6031 uint64_t islog = B_FALSE; 6032 char *dashes = "%-*s - - - - " 6033 "- - - - -\n"; 6034 6035 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 6036 (uint64_t **)&vs, &c) == 0); 6037 6038 if (name != NULL) { 6039 boolean_t toplevel = (vs->vs_space != 0); 6040 uint64_t cap; 6041 enum zfs_nicenum_format format; 6042 const char *state; 6043 6044 if (cb->cb_literal) 6045 format = ZFS_NICENUM_RAW; 6046 else 6047 format = ZFS_NICENUM_1024; 6048 6049 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 6050 return; 6051 6052 if (scripted) 6053 (void) printf("\t%s", name); 6054 else if (strlen(name) + depth > cb->cb_namewidth) 6055 (void) printf("%*s%s", depth, "", name); 6056 else 6057 (void) printf("%*s%s%*s", depth, "", name, 6058 (int)(cb->cb_namewidth - strlen(name) - depth), ""); 6059 6060 /* 6061 * Print the properties for the individual vdevs. Some 6062 * properties are only applicable to toplevel vdevs. The 6063 * 'toplevel' boolean value is passed to the print_one_column() 6064 * to indicate that the value is valid. 6065 */ 6066 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted, 6067 toplevel, format); 6068 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, 6069 scripted, toplevel, format); 6070 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, 6071 NULL, scripted, toplevel, format); 6072 print_one_column(ZPOOL_PROP_CHECKPOINT, 6073 vs->vs_checkpoint_space, NULL, scripted, toplevel, format); 6074 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, 6075 scripted, B_TRUE, format); 6076 print_one_column(ZPOOL_PROP_FRAGMENTATION, 6077 vs->vs_fragmentation, NULL, scripted, 6078 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), 6079 format); 6080 cap = (vs->vs_space == 0) ? 0 : 6081 (vs->vs_alloc * 10000 / vs->vs_space); 6082 print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL, 6083 scripted, toplevel, format); 6084 print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL, 6085 scripted, toplevel, format); 6086 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 6087 if (isspare) { 6088 if (vs->vs_aux == VDEV_AUX_SPARED) 6089 state = "INUSE"; 6090 else if (vs->vs_state == VDEV_STATE_HEALTHY) 6091 state = "AVAIL"; 6092 } 6093 print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted, 6094 B_TRUE, format); 6095 (void) printf("\n"); 6096 } 6097 6098 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 6099 &child, &children) != 0) 6100 return; 6101 6102 /* list the normal vdevs first */ 6103 for (c = 0; c < children; c++) { 6104 uint64_t ishole = B_FALSE; 6105 6106 if (nvlist_lookup_uint64(child[c], 6107 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 6108 continue; 6109 6110 if (nvlist_lookup_uint64(child[c], 6111 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) 6112 continue; 6113 6114 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 6115 continue; 6116 6117 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6118 cb->cb_name_flags); 6119 print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE); 6120 free(vname); 6121 } 6122 6123 /* list the classes: 'logs', 'dedup', and 'special' */ 6124 for (uint_t n = 0; n < 3; n++) { 6125 boolean_t printed = B_FALSE; 6126 6127 for (c = 0; c < children; c++) { 6128 char *bias = NULL; 6129 char *type = NULL; 6130 6131 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 6132 &islog) == 0 && islog) { 6133 bias = VDEV_ALLOC_CLASS_LOGS; 6134 } else { 6135 (void) nvlist_lookup_string(child[c], 6136 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 6137 (void) nvlist_lookup_string(child[c], 6138 ZPOOL_CONFIG_TYPE, &type); 6139 } 6140 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 6141 continue; 6142 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 6143 continue; 6144 6145 if (!printed) { 6146 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6147 (void) printf(dashes, cb->cb_namewidth, 6148 class_name[n]); 6149 printed = B_TRUE; 6150 } 6151 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6152 cb->cb_name_flags); 6153 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6154 B_FALSE); 6155 free(vname); 6156 } 6157 } 6158 6159 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 6160 &child, &children) == 0 && children > 0) { 6161 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6162 (void) printf(dashes, cb->cb_namewidth, "cache"); 6163 for (c = 0; c < children; c++) { 6164 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6165 cb->cb_name_flags); 6166 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6167 B_FALSE); 6168 free(vname); 6169 } 6170 } 6171 6172 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, 6173 &children) == 0 && children > 0) { 6174 /* LINTED E_SEC_PRINTF_VAR_FMT */ 6175 (void) printf(dashes, cb->cb_namewidth, "spare"); 6176 for (c = 0; c < children; c++) { 6177 vname = zpool_vdev_name(g_zfs, zhp, child[c], 6178 cb->cb_name_flags); 6179 print_list_stats(zhp, vname, child[c], cb, depth + 2, 6180 B_TRUE); 6181 free(vname); 6182 } 6183 } 6184 } 6185 6186 /* 6187 * Generic callback function to list a pool. 6188 */ 6189 static int 6190 list_callback(zpool_handle_t *zhp, void *data) 6191 { 6192 list_cbdata_t *cbp = data; 6193 6194 print_pool(zhp, cbp); 6195 6196 if (cbp->cb_verbose) { 6197 nvlist_t *config, *nvroot; 6198 6199 config = zpool_get_config(zhp, NULL); 6200 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 6201 &nvroot) == 0); 6202 print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE); 6203 } 6204 6205 return (0); 6206 } 6207 6208 /* 6209 * Set the minimum pool/vdev name column width. The width must be at least 9, 6210 * but may be as large as needed. 6211 */ 6212 static int 6213 get_namewidth_list(zpool_handle_t *zhp, void *data) 6214 { 6215 list_cbdata_t *cb = data; 6216 int width; 6217 6218 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, 6219 cb->cb_verbose); 6220 6221 if (width < 9) 6222 width = 9; 6223 6224 cb->cb_namewidth = width; 6225 6226 return (0); 6227 } 6228 6229 /* 6230 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 6231 * 6232 * -g Display guid for individual vdev name. 6233 * -H Scripted mode. Don't display headers, and separate properties 6234 * by a single tab. 6235 * -L Follow links when resolving vdev path name. 6236 * -o List of properties to display. Defaults to 6237 * "name,size,allocated,free,expandsize,fragmentation,capacity," 6238 * "dedupratio,health,altroot" 6239 * -p Display values in parsable (exact) format. 6240 * -P Display full path for vdev name. 6241 * -T Display a timestamp in date(1) or Unix format 6242 * 6243 * List all pools in the system, whether or not they're healthy. Output space 6244 * statistics for each one, as well as health status summary. 6245 */ 6246 int 6247 zpool_do_list(int argc, char **argv) 6248 { 6249 int c; 6250 int ret = 0; 6251 list_cbdata_t cb = { 0 }; 6252 static char default_props[] = 6253 "name,size,allocated,free,checkpoint,expandsize,fragmentation," 6254 "capacity,dedupratio,health,altroot"; 6255 char *props = default_props; 6256 float interval = 0; 6257 unsigned long count = 0; 6258 zpool_list_t *list; 6259 boolean_t first = B_TRUE; 6260 6261 /* check options */ 6262 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) { 6263 switch (c) { 6264 case 'g': 6265 cb.cb_name_flags |= VDEV_NAME_GUID; 6266 break; 6267 case 'H': 6268 cb.cb_scripted = B_TRUE; 6269 break; 6270 case 'L': 6271 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 6272 break; 6273 case 'o': 6274 props = optarg; 6275 break; 6276 case 'P': 6277 cb.cb_name_flags |= VDEV_NAME_PATH; 6278 break; 6279 case 'p': 6280 cb.cb_literal = B_TRUE; 6281 break; 6282 case 'T': 6283 get_timestamp_arg(*optarg); 6284 break; 6285 case 'v': 6286 cb.cb_verbose = B_TRUE; 6287 cb.cb_namewidth = 8; /* 8 until precalc is avail */ 6288 break; 6289 case ':': 6290 (void) fprintf(stderr, gettext("missing argument for " 6291 "'%c' option\n"), optopt); 6292 usage(B_FALSE); 6293 break; 6294 case '?': 6295 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6296 optopt); 6297 usage(B_FALSE); 6298 } 6299 } 6300 6301 argc -= optind; 6302 argv += optind; 6303 6304 get_interval_count(&argc, argv, &interval, &count); 6305 6306 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 6307 usage(B_FALSE); 6308 6309 for (;;) { 6310 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 6311 cb.cb_literal, &ret)) == NULL) 6312 return (1); 6313 6314 if (pool_list_count(list) == 0) 6315 break; 6316 6317 cb.cb_namewidth = 0; 6318 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); 6319 6320 if (timestamp_fmt != NODATE) 6321 print_timestamp(timestamp_fmt); 6322 6323 if (!cb.cb_scripted && (first || cb.cb_verbose)) { 6324 print_header(&cb); 6325 first = B_FALSE; 6326 } 6327 ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 6328 6329 if (interval == 0) 6330 break; 6331 6332 if (count != 0 && --count == 0) 6333 break; 6334 6335 pool_list_free(list); 6336 (void) fsleep(interval); 6337 } 6338 6339 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 6340 (void) printf(gettext("no pools available\n")); 6341 ret = 0; 6342 } 6343 6344 pool_list_free(list); 6345 zprop_free_list(cb.cb_proplist); 6346 return (ret); 6347 } 6348 6349 static int 6350 zpool_do_attach_or_replace(int argc, char **argv, int replacing) 6351 { 6352 boolean_t force = B_FALSE; 6353 boolean_t rebuild = B_FALSE; 6354 boolean_t wait = B_FALSE; 6355 int c; 6356 nvlist_t *nvroot; 6357 char *poolname, *old_disk, *new_disk; 6358 zpool_handle_t *zhp; 6359 nvlist_t *props = NULL; 6360 char *propval; 6361 int ret; 6362 6363 /* check options */ 6364 while ((c = getopt(argc, argv, "fo:sw")) != -1) { 6365 switch (c) { 6366 case 'f': 6367 force = B_TRUE; 6368 break; 6369 case 'o': 6370 if ((propval = strchr(optarg, '=')) == NULL) { 6371 (void) fprintf(stderr, gettext("missing " 6372 "'=' for -o option\n")); 6373 usage(B_FALSE); 6374 } 6375 *propval = '\0'; 6376 propval++; 6377 6378 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 6379 (add_prop_list(optarg, propval, &props, B_TRUE))) 6380 usage(B_FALSE); 6381 break; 6382 case 's': 6383 rebuild = B_TRUE; 6384 break; 6385 case 'w': 6386 wait = B_TRUE; 6387 break; 6388 case '?': 6389 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6390 optopt); 6391 usage(B_FALSE); 6392 } 6393 } 6394 6395 argc -= optind; 6396 argv += optind; 6397 6398 /* get pool name and check number of arguments */ 6399 if (argc < 1) { 6400 (void) fprintf(stderr, gettext("missing pool name argument\n")); 6401 usage(B_FALSE); 6402 } 6403 6404 poolname = argv[0]; 6405 6406 if (argc < 2) { 6407 (void) fprintf(stderr, 6408 gettext("missing <device> specification\n")); 6409 usage(B_FALSE); 6410 } 6411 6412 old_disk = argv[1]; 6413 6414 if (argc < 3) { 6415 if (!replacing) { 6416 (void) fprintf(stderr, 6417 gettext("missing <new_device> specification\n")); 6418 usage(B_FALSE); 6419 } 6420 new_disk = old_disk; 6421 argc -= 1; 6422 argv += 1; 6423 } else { 6424 new_disk = argv[2]; 6425 argc -= 2; 6426 argv += 2; 6427 } 6428 6429 if (argc > 1) { 6430 (void) fprintf(stderr, gettext("too many arguments\n")); 6431 usage(B_FALSE); 6432 } 6433 6434 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 6435 nvlist_free(props); 6436 return (1); 6437 } 6438 6439 if (zpool_get_config(zhp, NULL) == NULL) { 6440 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 6441 poolname); 6442 zpool_close(zhp); 6443 nvlist_free(props); 6444 return (1); 6445 } 6446 6447 /* unless manually specified use "ashift" pool property (if set) */ 6448 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 6449 int intval; 6450 zprop_source_t src; 6451 char strval[ZPOOL_MAXPROPLEN]; 6452 6453 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 6454 if (src != ZPROP_SRC_DEFAULT) { 6455 (void) sprintf(strval, "%" PRId32, intval); 6456 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 6457 &props, B_TRUE) == 0); 6458 } 6459 } 6460 6461 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, 6462 argc, argv); 6463 if (nvroot == NULL) { 6464 zpool_close(zhp); 6465 nvlist_free(props); 6466 return (1); 6467 } 6468 6469 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, 6470 rebuild); 6471 6472 if (ret == 0 && wait) 6473 ret = zpool_wait(zhp, 6474 replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER); 6475 6476 nvlist_free(props); 6477 nvlist_free(nvroot); 6478 zpool_close(zhp); 6479 6480 return (ret); 6481 } 6482 6483 /* 6484 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> 6485 * 6486 * -f Force attach, even if <new_device> appears to be in use. 6487 * -s Use sequential instead of healing reconstruction for resilver. 6488 * -o Set property=value. 6489 * -w Wait for replacing to complete before returning 6490 * 6491 * Replace <device> with <new_device>. 6492 */ 6493 /* ARGSUSED */ 6494 int 6495 zpool_do_replace(int argc, char **argv) 6496 { 6497 return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 6498 } 6499 6500 /* 6501 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device> 6502 * 6503 * -f Force attach, even if <new_device> appears to be in use. 6504 * -s Use sequential instead of healing reconstruction for resilver. 6505 * -o Set property=value. 6506 * -w Wait for resilvering to complete before returning 6507 * 6508 * Attach <new_device> to the mirror containing <device>. If <device> is not 6509 * part of a mirror, then <device> will be transformed into a mirror of 6510 * <device> and <new_device>. In either case, <new_device> will begin life 6511 * with a DTL of [0, now], and will immediately begin to resilver itself. 6512 */ 6513 int 6514 zpool_do_attach(int argc, char **argv) 6515 { 6516 return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 6517 } 6518 6519 /* 6520 * zpool detach [-f] <pool> <device> 6521 * 6522 * -f Force detach of <device>, even if DTLs argue against it 6523 * (not supported yet) 6524 * 6525 * Detach a device from a mirror. The operation will be refused if <device> 6526 * is the last device in the mirror, or if the DTLs indicate that this device 6527 * has the only valid copy of some data. 6528 */ 6529 /* ARGSUSED */ 6530 int 6531 zpool_do_detach(int argc, char **argv) 6532 { 6533 int c; 6534 char *poolname, *path; 6535 zpool_handle_t *zhp; 6536 int ret; 6537 6538 /* check options */ 6539 while ((c = getopt(argc, argv, "")) != -1) { 6540 switch (c) { 6541 case '?': 6542 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6543 optopt); 6544 usage(B_FALSE); 6545 } 6546 } 6547 6548 argc -= optind; 6549 argv += optind; 6550 6551 /* get pool name and check number of arguments */ 6552 if (argc < 1) { 6553 (void) fprintf(stderr, gettext("missing pool name argument\n")); 6554 usage(B_FALSE); 6555 } 6556 6557 if (argc < 2) { 6558 (void) fprintf(stderr, 6559 gettext("missing <device> specification\n")); 6560 usage(B_FALSE); 6561 } 6562 6563 poolname = argv[0]; 6564 path = argv[1]; 6565 6566 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6567 return (1); 6568 6569 ret = zpool_vdev_detach(zhp, path); 6570 6571 zpool_close(zhp); 6572 6573 return (ret); 6574 } 6575 6576 /* 6577 * zpool split [-gLnP] [-o prop=val] ... 6578 * [-o mntopt] ... 6579 * [-R altroot] <pool> <newpool> [<device> ...] 6580 * 6581 * -g Display guid for individual vdev name. 6582 * -L Follow links when resolving vdev path name. 6583 * -n Do not split the pool, but display the resulting layout if 6584 * it were to be split. 6585 * -o Set property=value, or set mount options. 6586 * -P Display full path for vdev name. 6587 * -R Mount the split-off pool under an alternate root. 6588 * -l Load encryption keys while importing. 6589 * 6590 * Splits the named pool and gives it the new pool name. Devices to be split 6591 * off may be listed, provided that no more than one device is specified 6592 * per top-level vdev mirror. The newly split pool is left in an exported 6593 * state unless -R is specified. 6594 * 6595 * Restrictions: the top-level of the pool pool must only be made up of 6596 * mirrors; all devices in the pool must be healthy; no device may be 6597 * undergoing a resilvering operation. 6598 */ 6599 int 6600 zpool_do_split(int argc, char **argv) 6601 { 6602 char *srcpool, *newpool, *propval; 6603 char *mntopts = NULL; 6604 splitflags_t flags; 6605 int c, ret = 0; 6606 boolean_t loadkeys = B_FALSE; 6607 zpool_handle_t *zhp; 6608 nvlist_t *config, *props = NULL; 6609 6610 flags.dryrun = B_FALSE; 6611 flags.import = B_FALSE; 6612 flags.name_flags = 0; 6613 6614 /* check options */ 6615 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { 6616 switch (c) { 6617 case 'g': 6618 flags.name_flags |= VDEV_NAME_GUID; 6619 break; 6620 case 'L': 6621 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; 6622 break; 6623 case 'R': 6624 flags.import = B_TRUE; 6625 if (add_prop_list( 6626 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 6627 &props, B_TRUE) != 0) { 6628 nvlist_free(props); 6629 usage(B_FALSE); 6630 } 6631 break; 6632 case 'l': 6633 loadkeys = B_TRUE; 6634 break; 6635 case 'n': 6636 flags.dryrun = B_TRUE; 6637 break; 6638 case 'o': 6639 if ((propval = strchr(optarg, '=')) != NULL) { 6640 *propval = '\0'; 6641 propval++; 6642 if (add_prop_list(optarg, propval, 6643 &props, B_TRUE) != 0) { 6644 nvlist_free(props); 6645 usage(B_FALSE); 6646 } 6647 } else { 6648 mntopts = optarg; 6649 } 6650 break; 6651 case 'P': 6652 flags.name_flags |= VDEV_NAME_PATH; 6653 break; 6654 case ':': 6655 (void) fprintf(stderr, gettext("missing argument for " 6656 "'%c' option\n"), optopt); 6657 usage(B_FALSE); 6658 break; 6659 case '?': 6660 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6661 optopt); 6662 usage(B_FALSE); 6663 break; 6664 } 6665 } 6666 6667 if (!flags.import && mntopts != NULL) { 6668 (void) fprintf(stderr, gettext("setting mntopts is only " 6669 "valid when importing the pool\n")); 6670 usage(B_FALSE); 6671 } 6672 6673 if (!flags.import && loadkeys) { 6674 (void) fprintf(stderr, gettext("loading keys is only " 6675 "valid when importing the pool\n")); 6676 usage(B_FALSE); 6677 } 6678 6679 argc -= optind; 6680 argv += optind; 6681 6682 if (argc < 1) { 6683 (void) fprintf(stderr, gettext("Missing pool name\n")); 6684 usage(B_FALSE); 6685 } 6686 if (argc < 2) { 6687 (void) fprintf(stderr, gettext("Missing new pool name\n")); 6688 usage(B_FALSE); 6689 } 6690 6691 srcpool = argv[0]; 6692 newpool = argv[1]; 6693 6694 argc -= 2; 6695 argv += 2; 6696 6697 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { 6698 nvlist_free(props); 6699 return (1); 6700 } 6701 6702 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 6703 if (config == NULL) { 6704 ret = 1; 6705 } else { 6706 if (flags.dryrun) { 6707 (void) printf(gettext("would create '%s' with the " 6708 "following layout:\n\n"), newpool); 6709 print_vdev_tree(NULL, newpool, config, 0, "", 6710 flags.name_flags); 6711 print_vdev_tree(NULL, "dedup", config, 0, 6712 VDEV_ALLOC_BIAS_DEDUP, 0); 6713 print_vdev_tree(NULL, "special", config, 0, 6714 VDEV_ALLOC_BIAS_SPECIAL, 0); 6715 } 6716 } 6717 6718 zpool_close(zhp); 6719 6720 if (ret != 0 || flags.dryrun || !flags.import) { 6721 nvlist_free(config); 6722 nvlist_free(props); 6723 return (ret); 6724 } 6725 6726 /* 6727 * The split was successful. Now we need to open the new 6728 * pool and import it. 6729 */ 6730 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { 6731 nvlist_free(config); 6732 nvlist_free(props); 6733 return (1); 6734 } 6735 6736 if (loadkeys) { 6737 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); 6738 if (ret != 0) 6739 ret = 1; 6740 } 6741 6742 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 6743 zpool_enable_datasets(zhp, mntopts, 0) != 0) { 6744 ret = 1; 6745 (void) fprintf(stderr, gettext("Split was successful, but " 6746 "the datasets could not all be mounted\n")); 6747 (void) fprintf(stderr, gettext("Try doing '%s' with a " 6748 "different altroot\n"), "zpool import"); 6749 } 6750 zpool_close(zhp); 6751 nvlist_free(config); 6752 nvlist_free(props); 6753 6754 return (ret); 6755 } 6756 6757 6758 6759 /* 6760 * zpool online <pool> <device> ... 6761 */ 6762 int 6763 zpool_do_online(int argc, char **argv) 6764 { 6765 int c, i; 6766 char *poolname; 6767 zpool_handle_t *zhp; 6768 int ret = 0; 6769 vdev_state_t newstate; 6770 int flags = 0; 6771 6772 /* check options */ 6773 while ((c = getopt(argc, argv, "e")) != -1) { 6774 switch (c) { 6775 case 'e': 6776 flags |= ZFS_ONLINE_EXPAND; 6777 break; 6778 case '?': 6779 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6780 optopt); 6781 usage(B_FALSE); 6782 } 6783 } 6784 6785 argc -= optind; 6786 argv += optind; 6787 6788 /* get pool name and check number of arguments */ 6789 if (argc < 1) { 6790 (void) fprintf(stderr, gettext("missing pool name\n")); 6791 usage(B_FALSE); 6792 } 6793 if (argc < 2) { 6794 (void) fprintf(stderr, gettext("missing device name\n")); 6795 usage(B_FALSE); 6796 } 6797 6798 poolname = argv[0]; 6799 6800 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6801 return (1); 6802 6803 for (i = 1; i < argc; i++) { 6804 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 6805 if (newstate != VDEV_STATE_HEALTHY) { 6806 (void) printf(gettext("warning: device '%s' " 6807 "onlined, but remains in faulted state\n"), 6808 argv[i]); 6809 if (newstate == VDEV_STATE_FAULTED) 6810 (void) printf(gettext("use 'zpool " 6811 "clear' to restore a faulted " 6812 "device\n")); 6813 else 6814 (void) printf(gettext("use 'zpool " 6815 "replace' to replace devices " 6816 "that are no longer present\n")); 6817 } 6818 } else { 6819 ret = 1; 6820 } 6821 } 6822 6823 zpool_close(zhp); 6824 6825 return (ret); 6826 } 6827 6828 /* 6829 * zpool offline [-ft] <pool> <device> ... 6830 * 6831 * -f Force the device into a faulted state. 6832 * 6833 * -t Only take the device off-line temporarily. The offline/faulted 6834 * state will not be persistent across reboots. 6835 */ 6836 /* ARGSUSED */ 6837 int 6838 zpool_do_offline(int argc, char **argv) 6839 { 6840 int c, i; 6841 char *poolname; 6842 zpool_handle_t *zhp; 6843 int ret = 0; 6844 boolean_t istmp = B_FALSE; 6845 boolean_t fault = B_FALSE; 6846 6847 /* check options */ 6848 while ((c = getopt(argc, argv, "ft")) != -1) { 6849 switch (c) { 6850 case 'f': 6851 fault = B_TRUE; 6852 break; 6853 case 't': 6854 istmp = B_TRUE; 6855 break; 6856 case '?': 6857 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6858 optopt); 6859 usage(B_FALSE); 6860 } 6861 } 6862 6863 argc -= optind; 6864 argv += optind; 6865 6866 /* get pool name and check number of arguments */ 6867 if (argc < 1) { 6868 (void) fprintf(stderr, gettext("missing pool name\n")); 6869 usage(B_FALSE); 6870 } 6871 if (argc < 2) { 6872 (void) fprintf(stderr, gettext("missing device name\n")); 6873 usage(B_FALSE); 6874 } 6875 6876 poolname = argv[0]; 6877 6878 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 6879 return (1); 6880 6881 for (i = 1; i < argc; i++) { 6882 if (fault) { 6883 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); 6884 vdev_aux_t aux; 6885 if (istmp == B_FALSE) { 6886 /* Force the fault to persist across imports */ 6887 aux = VDEV_AUX_EXTERNAL_PERSIST; 6888 } else { 6889 aux = VDEV_AUX_EXTERNAL; 6890 } 6891 6892 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) 6893 ret = 1; 6894 } else { 6895 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 6896 ret = 1; 6897 } 6898 } 6899 6900 zpool_close(zhp); 6901 6902 return (ret); 6903 } 6904 6905 /* 6906 * zpool clear <pool> [device] 6907 * 6908 * Clear all errors associated with a pool or a particular device. 6909 */ 6910 int 6911 zpool_do_clear(int argc, char **argv) 6912 { 6913 int c; 6914 int ret = 0; 6915 boolean_t dryrun = B_FALSE; 6916 boolean_t do_rewind = B_FALSE; 6917 boolean_t xtreme_rewind = B_FALSE; 6918 uint32_t rewind_policy = ZPOOL_NO_REWIND; 6919 nvlist_t *policy = NULL; 6920 zpool_handle_t *zhp; 6921 char *pool, *device; 6922 6923 /* check options */ 6924 while ((c = getopt(argc, argv, "FnX")) != -1) { 6925 switch (c) { 6926 case 'F': 6927 do_rewind = B_TRUE; 6928 break; 6929 case 'n': 6930 dryrun = B_TRUE; 6931 break; 6932 case 'X': 6933 xtreme_rewind = B_TRUE; 6934 break; 6935 case '?': 6936 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6937 optopt); 6938 usage(B_FALSE); 6939 } 6940 } 6941 6942 argc -= optind; 6943 argv += optind; 6944 6945 if (argc < 1) { 6946 (void) fprintf(stderr, gettext("missing pool name\n")); 6947 usage(B_FALSE); 6948 } 6949 6950 if (argc > 2) { 6951 (void) fprintf(stderr, gettext("too many arguments\n")); 6952 usage(B_FALSE); 6953 } 6954 6955 if ((dryrun || xtreme_rewind) && !do_rewind) { 6956 (void) fprintf(stderr, 6957 gettext("-n or -X only meaningful with -F\n")); 6958 usage(B_FALSE); 6959 } 6960 if (dryrun) 6961 rewind_policy = ZPOOL_TRY_REWIND; 6962 else if (do_rewind) 6963 rewind_policy = ZPOOL_DO_REWIND; 6964 if (xtreme_rewind) 6965 rewind_policy |= ZPOOL_EXTREME_REWIND; 6966 6967 /* In future, further rewind policy choices can be passed along here */ 6968 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 6969 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 6970 rewind_policy) != 0) { 6971 return (1); 6972 } 6973 6974 pool = argv[0]; 6975 device = argc == 2 ? argv[1] : NULL; 6976 6977 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 6978 nvlist_free(policy); 6979 return (1); 6980 } 6981 6982 if (zpool_clear(zhp, device, policy) != 0) 6983 ret = 1; 6984 6985 zpool_close(zhp); 6986 6987 nvlist_free(policy); 6988 6989 return (ret); 6990 } 6991 6992 /* 6993 * zpool reguid <pool> 6994 */ 6995 int 6996 zpool_do_reguid(int argc, char **argv) 6997 { 6998 int c; 6999 char *poolname; 7000 zpool_handle_t *zhp; 7001 int ret = 0; 7002 7003 /* check options */ 7004 while ((c = getopt(argc, argv, "")) != -1) { 7005 switch (c) { 7006 case '?': 7007 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7008 optopt); 7009 usage(B_FALSE); 7010 } 7011 } 7012 7013 argc -= optind; 7014 argv += optind; 7015 7016 /* get pool name and check number of arguments */ 7017 if (argc < 1) { 7018 (void) fprintf(stderr, gettext("missing pool name\n")); 7019 usage(B_FALSE); 7020 } 7021 7022 if (argc > 1) { 7023 (void) fprintf(stderr, gettext("too many arguments\n")); 7024 usage(B_FALSE); 7025 } 7026 7027 poolname = argv[0]; 7028 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 7029 return (1); 7030 7031 ret = zpool_reguid(zhp); 7032 7033 zpool_close(zhp); 7034 return (ret); 7035 } 7036 7037 7038 /* 7039 * zpool reopen <pool> 7040 * 7041 * Reopen the pool so that the kernel can update the sizes of all vdevs. 7042 */ 7043 int 7044 zpool_do_reopen(int argc, char **argv) 7045 { 7046 int c; 7047 int ret = 0; 7048 boolean_t scrub_restart = B_TRUE; 7049 7050 /* check options */ 7051 while ((c = getopt(argc, argv, "n")) != -1) { 7052 switch (c) { 7053 case 'n': 7054 scrub_restart = B_FALSE; 7055 break; 7056 case '?': 7057 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7058 optopt); 7059 usage(B_FALSE); 7060 } 7061 } 7062 7063 argc -= optind; 7064 argv += optind; 7065 7066 /* if argc == 0 we will execute zpool_reopen_one on all pools */ 7067 ret = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, zpool_reopen_one, 7068 &scrub_restart); 7069 7070 return (ret); 7071 } 7072 7073 typedef struct scrub_cbdata { 7074 int cb_type; 7075 pool_scrub_cmd_t cb_scrub_cmd; 7076 } scrub_cbdata_t; 7077 7078 static boolean_t 7079 zpool_has_checkpoint(zpool_handle_t *zhp) 7080 { 7081 nvlist_t *config, *nvroot; 7082 7083 config = zpool_get_config(zhp, NULL); 7084 7085 if (config != NULL) { 7086 pool_checkpoint_stat_t *pcs = NULL; 7087 uint_t c; 7088 7089 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 7090 (void) nvlist_lookup_uint64_array(nvroot, 7091 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 7092 7093 if (pcs == NULL || pcs->pcs_state == CS_NONE) 7094 return (B_FALSE); 7095 7096 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || 7097 pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 7098 return (B_TRUE); 7099 } 7100 7101 return (B_FALSE); 7102 } 7103 7104 static int 7105 scrub_callback(zpool_handle_t *zhp, void *data) 7106 { 7107 scrub_cbdata_t *cb = data; 7108 int err; 7109 7110 /* 7111 * Ignore faulted pools. 7112 */ 7113 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 7114 (void) fprintf(stderr, gettext("cannot scan '%s': pool is " 7115 "currently unavailable\n"), zpool_get_name(zhp)); 7116 return (1); 7117 } 7118 7119 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd); 7120 7121 if (err == 0 && zpool_has_checkpoint(zhp) && 7122 cb->cb_type == POOL_SCAN_SCRUB) { 7123 (void) printf(gettext("warning: will not scrub state that " 7124 "belongs to the checkpoint of pool '%s'\n"), 7125 zpool_get_name(zhp)); 7126 } 7127 7128 return (err != 0); 7129 } 7130 7131 static int 7132 wait_callback(zpool_handle_t *zhp, void *data) 7133 { 7134 zpool_wait_activity_t *act = data; 7135 return (zpool_wait(zhp, *act)); 7136 } 7137 7138 /* 7139 * zpool scrub [-s | -p] [-w] <pool> ... 7140 * 7141 * -s Stop. Stops any in-progress scrub. 7142 * -p Pause. Pause in-progress scrub. 7143 * -w Wait. Blocks until scrub has completed. 7144 */ 7145 int 7146 zpool_do_scrub(int argc, char **argv) 7147 { 7148 int c; 7149 scrub_cbdata_t cb; 7150 boolean_t wait = B_FALSE; 7151 int error; 7152 7153 cb.cb_type = POOL_SCAN_SCRUB; 7154 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 7155 7156 /* check options */ 7157 while ((c = getopt(argc, argv, "spw")) != -1) { 7158 switch (c) { 7159 case 's': 7160 cb.cb_type = POOL_SCAN_NONE; 7161 break; 7162 case 'p': 7163 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; 7164 break; 7165 case 'w': 7166 wait = B_TRUE; 7167 break; 7168 case '?': 7169 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7170 optopt); 7171 usage(B_FALSE); 7172 } 7173 } 7174 7175 if (cb.cb_type == POOL_SCAN_NONE && 7176 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) { 7177 (void) fprintf(stderr, gettext("invalid option combination: " 7178 "-s and -p are mutually exclusive\n")); 7179 usage(B_FALSE); 7180 } 7181 7182 if (wait && (cb.cb_type == POOL_SCAN_NONE || 7183 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { 7184 (void) fprintf(stderr, gettext("invalid option combination: " 7185 "-w cannot be used with -p or -s\n")); 7186 usage(B_FALSE); 7187 } 7188 7189 argc -= optind; 7190 argv += optind; 7191 7192 if (argc < 1) { 7193 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7194 usage(B_FALSE); 7195 } 7196 7197 error = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7198 scrub_callback, &cb); 7199 7200 if (wait && !error) { 7201 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; 7202 error = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7203 wait_callback, &act); 7204 } 7205 7206 return (error); 7207 } 7208 7209 /* 7210 * zpool resilver <pool> ... 7211 * 7212 * Restarts any in-progress resilver 7213 */ 7214 int 7215 zpool_do_resilver(int argc, char **argv) 7216 { 7217 int c; 7218 scrub_cbdata_t cb; 7219 7220 cb.cb_type = POOL_SCAN_RESILVER; 7221 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 7222 7223 /* check options */ 7224 while ((c = getopt(argc, argv, "")) != -1) { 7225 switch (c) { 7226 case '?': 7227 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7228 optopt); 7229 usage(B_FALSE); 7230 } 7231 } 7232 7233 argc -= optind; 7234 argv += optind; 7235 7236 if (argc < 1) { 7237 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7238 usage(B_FALSE); 7239 } 7240 7241 return (for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 7242 scrub_callback, &cb)); 7243 } 7244 7245 /* 7246 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...] 7247 * 7248 * -c Cancel. Ends any in-progress trim. 7249 * -d Secure trim. Requires kernel and device support. 7250 * -r <rate> Sets the TRIM rate in bytes (per second). Supports 7251 * adding a multiplier suffix such as 'k' or 'm'. 7252 * -s Suspend. TRIM can then be restarted with no flags. 7253 * -w Wait. Blocks until trimming has completed. 7254 */ 7255 int 7256 zpool_do_trim(int argc, char **argv) 7257 { 7258 struct option long_options[] = { 7259 {"cancel", no_argument, NULL, 'c'}, 7260 {"secure", no_argument, NULL, 'd'}, 7261 {"rate", required_argument, NULL, 'r'}, 7262 {"suspend", no_argument, NULL, 's'}, 7263 {"wait", no_argument, NULL, 'w'}, 7264 {0, 0, 0, 0} 7265 }; 7266 7267 pool_trim_func_t cmd_type = POOL_TRIM_START; 7268 uint64_t rate = 0; 7269 boolean_t secure = B_FALSE; 7270 boolean_t wait = B_FALSE; 7271 7272 int c; 7273 while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL)) 7274 != -1) { 7275 switch (c) { 7276 case 'c': 7277 if (cmd_type != POOL_TRIM_START && 7278 cmd_type != POOL_TRIM_CANCEL) { 7279 (void) fprintf(stderr, gettext("-c cannot be " 7280 "combined with other options\n")); 7281 usage(B_FALSE); 7282 } 7283 cmd_type = POOL_TRIM_CANCEL; 7284 break; 7285 case 'd': 7286 if (cmd_type != POOL_TRIM_START) { 7287 (void) fprintf(stderr, gettext("-d cannot be " 7288 "combined with the -c or -s options\n")); 7289 usage(B_FALSE); 7290 } 7291 secure = B_TRUE; 7292 break; 7293 case 'r': 7294 if (cmd_type != POOL_TRIM_START) { 7295 (void) fprintf(stderr, gettext("-r cannot be " 7296 "combined with the -c or -s options\n")); 7297 usage(B_FALSE); 7298 } 7299 if (zfs_nicestrtonum(NULL, optarg, &rate) == -1) { 7300 (void) fprintf(stderr, 7301 gettext("invalid value for rate\n")); 7302 usage(B_FALSE); 7303 } 7304 break; 7305 case 's': 7306 if (cmd_type != POOL_TRIM_START && 7307 cmd_type != POOL_TRIM_SUSPEND) { 7308 (void) fprintf(stderr, gettext("-s cannot be " 7309 "combined with other options\n")); 7310 usage(B_FALSE); 7311 } 7312 cmd_type = POOL_TRIM_SUSPEND; 7313 break; 7314 case 'w': 7315 wait = B_TRUE; 7316 break; 7317 case '?': 7318 if (optopt != 0) { 7319 (void) fprintf(stderr, 7320 gettext("invalid option '%c'\n"), optopt); 7321 } else { 7322 (void) fprintf(stderr, 7323 gettext("invalid option '%s'\n"), 7324 argv[optind - 1]); 7325 } 7326 usage(B_FALSE); 7327 } 7328 } 7329 7330 argc -= optind; 7331 argv += optind; 7332 7333 if (argc < 1) { 7334 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7335 usage(B_FALSE); 7336 return (-1); 7337 } 7338 7339 if (wait && (cmd_type != POOL_TRIM_START)) { 7340 (void) fprintf(stderr, gettext("-w cannot be used with -c or " 7341 "-s\n")); 7342 usage(B_FALSE); 7343 } 7344 7345 char *poolname = argv[0]; 7346 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 7347 if (zhp == NULL) 7348 return (-1); 7349 7350 trimflags_t trim_flags = { 7351 .secure = secure, 7352 .rate = rate, 7353 .wait = wait, 7354 }; 7355 7356 nvlist_t *vdevs = fnvlist_alloc(); 7357 if (argc == 1) { 7358 /* no individual leaf vdevs specified, so add them all */ 7359 nvlist_t *config = zpool_get_config(zhp, NULL); 7360 nvlist_t *nvroot = fnvlist_lookup_nvlist(config, 7361 ZPOOL_CONFIG_VDEV_TREE); 7362 zpool_collect_leaves(zhp, nvroot, vdevs); 7363 trim_flags.fullpool = B_TRUE; 7364 } else { 7365 trim_flags.fullpool = B_FALSE; 7366 for (int i = 1; i < argc; i++) { 7367 fnvlist_add_boolean(vdevs, argv[i]); 7368 } 7369 } 7370 7371 int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags); 7372 7373 fnvlist_free(vdevs); 7374 zpool_close(zhp); 7375 7376 return (error); 7377 } 7378 7379 /* 7380 * Converts a total number of seconds to a human readable string broken 7381 * down in to days/hours/minutes/seconds. 7382 */ 7383 static void 7384 secs_to_dhms(uint64_t total, char *buf) 7385 { 7386 uint64_t days = total / 60 / 60 / 24; 7387 uint64_t hours = (total / 60 / 60) % 24; 7388 uint64_t mins = (total / 60) % 60; 7389 uint64_t secs = (total % 60); 7390 7391 if (days > 0) { 7392 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", 7393 (u_longlong_t)days, (u_longlong_t)hours, 7394 (u_longlong_t)mins, (u_longlong_t)secs); 7395 } else { 7396 (void) sprintf(buf, "%02llu:%02llu:%02llu", 7397 (u_longlong_t)hours, (u_longlong_t)mins, 7398 (u_longlong_t)secs); 7399 } 7400 } 7401 7402 /* 7403 * Print out detailed scrub status. 7404 */ 7405 static void 7406 print_scan_scrub_resilver_status(pool_scan_stat_t *ps) 7407 { 7408 time_t start, end, pause; 7409 uint64_t pass_scanned, scanned, pass_issued, issued, total; 7410 uint64_t elapsed, scan_rate, issue_rate; 7411 double fraction_done; 7412 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7]; 7413 char srate_buf[7], irate_buf[7], time_buf[32]; 7414 7415 printf(" "); 7416 printf_color(ANSI_BOLD, gettext("scan:")); 7417 printf(" "); 7418 7419 /* If there's never been a scan, there's not much to say. */ 7420 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 7421 ps->pss_func >= POOL_SCAN_FUNCS) { 7422 (void) printf(gettext("none requested\n")); 7423 return; 7424 } 7425 7426 start = ps->pss_start_time; 7427 end = ps->pss_end_time; 7428 pause = ps->pss_pass_scrub_pause; 7429 7430 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); 7431 7432 assert(ps->pss_func == POOL_SCAN_SCRUB || 7433 ps->pss_func == POOL_SCAN_RESILVER); 7434 7435 /* Scan is finished or canceled. */ 7436 if (ps->pss_state == DSS_FINISHED) { 7437 secs_to_dhms(end - start, time_buf); 7438 7439 if (ps->pss_func == POOL_SCAN_SCRUB) { 7440 (void) printf(gettext("scrub repaired %s " 7441 "in %s with %llu errors on %s"), processed_buf, 7442 time_buf, (u_longlong_t)ps->pss_errors, 7443 ctime(&end)); 7444 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7445 (void) printf(gettext("resilvered %s " 7446 "in %s with %llu errors on %s"), processed_buf, 7447 time_buf, (u_longlong_t)ps->pss_errors, 7448 ctime(&end)); 7449 } 7450 return; 7451 } else if (ps->pss_state == DSS_CANCELED) { 7452 if (ps->pss_func == POOL_SCAN_SCRUB) { 7453 (void) printf(gettext("scrub canceled on %s"), 7454 ctime(&end)); 7455 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7456 (void) printf(gettext("resilver canceled on %s"), 7457 ctime(&end)); 7458 } 7459 return; 7460 } 7461 7462 assert(ps->pss_state == DSS_SCANNING); 7463 7464 /* Scan is in progress. Resilvers can't be paused. */ 7465 if (ps->pss_func == POOL_SCAN_SCRUB) { 7466 if (pause == 0) { 7467 (void) printf(gettext("scrub in progress since %s"), 7468 ctime(&start)); 7469 } else { 7470 (void) printf(gettext("scrub paused since %s"), 7471 ctime(&pause)); 7472 (void) printf(gettext("\tscrub started on %s"), 7473 ctime(&start)); 7474 } 7475 } else if (ps->pss_func == POOL_SCAN_RESILVER) { 7476 (void) printf(gettext("resilver in progress since %s"), 7477 ctime(&start)); 7478 } 7479 7480 scanned = ps->pss_examined; 7481 pass_scanned = ps->pss_pass_exam; 7482 issued = ps->pss_issued; 7483 pass_issued = ps->pss_pass_issued; 7484 total = ps->pss_to_examine; 7485 7486 /* we are only done with a block once we have issued the IO for it */ 7487 fraction_done = (double)issued / total; 7488 7489 /* elapsed time for this pass, rounding up to 1 if it's 0 */ 7490 elapsed = time(NULL) - ps->pss_pass_start; 7491 elapsed -= ps->pss_pass_scrub_spent_paused; 7492 elapsed = (elapsed != 0) ? elapsed : 1; 7493 7494 scan_rate = pass_scanned / elapsed; 7495 issue_rate = pass_issued / elapsed; 7496 uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ? 7497 ((total - issued) / issue_rate) : UINT64_MAX; 7498 secs_to_dhms(total_secs_left, time_buf); 7499 7500 /* format all of the numbers we will be reporting */ 7501 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); 7502 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); 7503 zfs_nicebytes(total, total_buf, sizeof (total_buf)); 7504 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); 7505 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); 7506 7507 /* do not print estimated time if we have a paused scrub */ 7508 if (pause == 0) { 7509 (void) printf(gettext("\t%s scanned at %s/s, " 7510 "%s issued at %s/s, %s total\n"), 7511 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf); 7512 } else { 7513 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"), 7514 scanned_buf, issued_buf, total_buf); 7515 } 7516 7517 if (ps->pss_func == POOL_SCAN_RESILVER) { 7518 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 7519 processed_buf, 100 * fraction_done); 7520 } else if (ps->pss_func == POOL_SCAN_SCRUB) { 7521 (void) printf(gettext("\t%s repaired, %.2f%% done"), 7522 processed_buf, 100 * fraction_done); 7523 } 7524 7525 if (pause == 0) { 7526 if (total_secs_left != UINT64_MAX && 7527 issue_rate >= 10 * 1024 * 1024) { 7528 (void) printf(gettext(", %s to go\n"), time_buf); 7529 } else { 7530 (void) printf(gettext(", no estimated " 7531 "completion time\n")); 7532 } 7533 } else { 7534 (void) printf(gettext("\n")); 7535 } 7536 } 7537 7538 static void 7539 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name) 7540 { 7541 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) 7542 return; 7543 7544 printf(" "); 7545 printf_color(ANSI_BOLD, gettext("scan:")); 7546 printf(" "); 7547 7548 uint64_t bytes_scanned = vrs->vrs_bytes_scanned; 7549 uint64_t bytes_issued = vrs->vrs_bytes_issued; 7550 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; 7551 uint64_t bytes_est = vrs->vrs_bytes_est; 7552 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / 7553 (vrs->vrs_pass_time_ms + 1)) * 1000; 7554 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / 7555 (vrs->vrs_pass_time_ms + 1)) * 1000; 7556 double scan_pct = MIN((double)bytes_scanned * 100 / 7557 (bytes_est + 1), 100); 7558 7559 /* Format all of the numbers we will be reporting */ 7560 char bytes_scanned_buf[7], bytes_issued_buf[7]; 7561 char bytes_rebuilt_buf[7], bytes_est_buf[7]; 7562 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; 7563 zfs_nicebytes(bytes_scanned, bytes_scanned_buf, 7564 sizeof (bytes_scanned_buf)); 7565 zfs_nicebytes(bytes_issued, bytes_issued_buf, 7566 sizeof (bytes_issued_buf)); 7567 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, 7568 sizeof (bytes_rebuilt_buf)); 7569 zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf)); 7570 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); 7571 zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf)); 7572 7573 time_t start = vrs->vrs_start_time; 7574 time_t end = vrs->vrs_end_time; 7575 7576 /* Rebuild is finished or canceled. */ 7577 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { 7578 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); 7579 (void) printf(gettext("resilvered (%s) %s in %s " 7580 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, 7581 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); 7582 return; 7583 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { 7584 (void) printf(gettext("resilver (%s) canceled on %s"), 7585 vdev_name, ctime(&end)); 7586 return; 7587 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7588 (void) printf(gettext("resilver (%s) in progress since %s"), 7589 vdev_name, ctime(&start)); 7590 } 7591 7592 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); 7593 7594 secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) / 7595 MAX(scan_rate, 1), time_buf); 7596 7597 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, " 7598 "%s total\n"), bytes_scanned_buf, scan_rate_buf, 7599 bytes_issued_buf, issue_rate_buf, bytes_est_buf); 7600 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 7601 bytes_rebuilt_buf, scan_pct); 7602 7603 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7604 if (scan_rate >= 10 * 1024 * 1024) { 7605 (void) printf(gettext(", %s to go\n"), time_buf); 7606 } else { 7607 (void) printf(gettext(", no estimated " 7608 "completion time\n")); 7609 } 7610 } else { 7611 (void) printf(gettext("\n")); 7612 } 7613 } 7614 7615 /* 7616 * Print rebuild status for top-level vdevs. 7617 */ 7618 static void 7619 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) 7620 { 7621 nvlist_t **child; 7622 uint_t children; 7623 7624 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7625 &child, &children) != 0) 7626 children = 0; 7627 7628 for (uint_t c = 0; c < children; c++) { 7629 vdev_rebuild_stat_t *vrs; 7630 uint_t i; 7631 7632 if (nvlist_lookup_uint64_array(child[c], 7633 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 7634 char *name = zpool_vdev_name(g_zfs, zhp, 7635 child[c], VDEV_NAME_TYPE_ID); 7636 print_rebuild_status_impl(vrs, name); 7637 free(name); 7638 } 7639 } 7640 } 7641 7642 /* 7643 * As we don't scrub checkpointed blocks, we want to warn the user that we 7644 * skipped scanning some blocks if a checkpoint exists or existed at any 7645 * time during the scan. If a sequential instead of healing reconstruction 7646 * was performed then the blocks were reconstructed. However, their checksums 7647 * have not been verified so we still print the warning. 7648 */ 7649 static void 7650 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) 7651 { 7652 if (ps == NULL || pcs == NULL) 7653 return; 7654 7655 if (pcs->pcs_state == CS_NONE || 7656 pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 7657 return; 7658 7659 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); 7660 7661 if (ps->pss_state == DSS_NONE) 7662 return; 7663 7664 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && 7665 ps->pss_end_time < pcs->pcs_start_time) 7666 return; 7667 7668 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { 7669 (void) printf(gettext(" scan warning: skipped blocks " 7670 "that are only referenced by the checkpoint.\n")); 7671 } else { 7672 assert(ps->pss_state == DSS_SCANNING); 7673 (void) printf(gettext(" scan warning: skipping blocks " 7674 "that are only referenced by the checkpoint.\n")); 7675 } 7676 } 7677 7678 /* 7679 * Returns B_TRUE if there is an active rebuild in progress. Otherwise, 7680 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for 7681 * the last completed (or cancelled) rebuild. 7682 */ 7683 static boolean_t 7684 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) 7685 { 7686 nvlist_t **child; 7687 uint_t children; 7688 boolean_t rebuilding = B_FALSE; 7689 uint64_t end_time = 0; 7690 7691 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7692 &child, &children) != 0) 7693 children = 0; 7694 7695 for (uint_t c = 0; c < children; c++) { 7696 vdev_rebuild_stat_t *vrs; 7697 uint_t i; 7698 7699 if (nvlist_lookup_uint64_array(child[c], 7700 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 7701 7702 if (vrs->vrs_end_time > end_time) 7703 end_time = vrs->vrs_end_time; 7704 7705 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 7706 rebuilding = B_TRUE; 7707 end_time = 0; 7708 break; 7709 } 7710 } 7711 } 7712 7713 if (rebuild_end_time != NULL) 7714 *rebuild_end_time = end_time; 7715 7716 return (rebuilding); 7717 } 7718 7719 /* 7720 * Print the scan status. 7721 */ 7722 static void 7723 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) 7724 { 7725 uint64_t rebuild_end_time = 0, resilver_end_time = 0; 7726 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; 7727 boolean_t active_resilver = B_FALSE; 7728 pool_checkpoint_stat_t *pcs = NULL; 7729 pool_scan_stat_t *ps = NULL; 7730 uint_t c; 7731 7732 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 7733 (uint64_t **)&ps, &c) == 0) { 7734 if (ps->pss_func == POOL_SCAN_RESILVER) { 7735 resilver_end_time = ps->pss_end_time; 7736 active_resilver = (ps->pss_state == DSS_SCANNING); 7737 } 7738 7739 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); 7740 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); 7741 } 7742 7743 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); 7744 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); 7745 7746 /* Always print the scrub status when available. */ 7747 if (have_scrub) 7748 print_scan_scrub_resilver_status(ps); 7749 7750 /* 7751 * When there is an active resilver or rebuild print its status. 7752 * Otherwise print the status of the last resilver or rebuild. 7753 */ 7754 if (active_resilver || (!active_rebuild && have_resilver && 7755 resilver_end_time && resilver_end_time > rebuild_end_time)) { 7756 print_scan_scrub_resilver_status(ps); 7757 } else if (active_rebuild || (!active_resilver && have_rebuild && 7758 rebuild_end_time && rebuild_end_time > resilver_end_time)) { 7759 print_rebuild_status(zhp, nvroot); 7760 } 7761 7762 (void) nvlist_lookup_uint64_array(nvroot, 7763 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 7764 print_checkpoint_scan_warning(ps, pcs); 7765 } 7766 7767 /* 7768 * Print out detailed removal status. 7769 */ 7770 static void 7771 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) 7772 { 7773 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 7774 time_t start, end; 7775 nvlist_t *config, *nvroot; 7776 nvlist_t **child; 7777 uint_t children; 7778 char *vdev_name; 7779 7780 if (prs == NULL || prs->prs_state == DSS_NONE) 7781 return; 7782 7783 /* 7784 * Determine name of vdev. 7785 */ 7786 config = zpool_get_config(zhp, NULL); 7787 nvroot = fnvlist_lookup_nvlist(config, 7788 ZPOOL_CONFIG_VDEV_TREE); 7789 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 7790 &child, &children) == 0); 7791 assert(prs->prs_removing_vdev < children); 7792 vdev_name = zpool_vdev_name(g_zfs, zhp, 7793 child[prs->prs_removing_vdev], B_TRUE); 7794 7795 printf_color(ANSI_BOLD, gettext("remove: ")); 7796 7797 start = prs->prs_start_time; 7798 end = prs->prs_end_time; 7799 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); 7800 7801 /* 7802 * Removal is finished or canceled. 7803 */ 7804 if (prs->prs_state == DSS_FINISHED) { 7805 uint64_t minutes_taken = (end - start) / 60; 7806 7807 (void) printf(gettext("Removal of vdev %llu copied %s " 7808 "in %lluh%um, completed on %s"), 7809 (longlong_t)prs->prs_removing_vdev, 7810 copied_buf, 7811 (u_longlong_t)(minutes_taken / 60), 7812 (uint_t)(minutes_taken % 60), 7813 ctime((time_t *)&end)); 7814 } else if (prs->prs_state == DSS_CANCELED) { 7815 (void) printf(gettext("Removal of %s canceled on %s"), 7816 vdev_name, ctime(&end)); 7817 } else { 7818 uint64_t copied, total, elapsed, mins_left, hours_left; 7819 double fraction_done; 7820 uint_t rate; 7821 7822 assert(prs->prs_state == DSS_SCANNING); 7823 7824 /* 7825 * Removal is in progress. 7826 */ 7827 (void) printf(gettext( 7828 "Evacuation of %s in progress since %s"), 7829 vdev_name, ctime(&start)); 7830 7831 copied = prs->prs_copied > 0 ? prs->prs_copied : 1; 7832 total = prs->prs_to_copy; 7833 fraction_done = (double)copied / total; 7834 7835 /* elapsed time for this pass */ 7836 elapsed = time(NULL) - prs->prs_start_time; 7837 elapsed = elapsed > 0 ? elapsed : 1; 7838 rate = copied / elapsed; 7839 rate = rate > 0 ? rate : 1; 7840 mins_left = ((total - copied) / rate) / 60; 7841 hours_left = mins_left / 60; 7842 7843 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 7844 zfs_nicenum(total, total_buf, sizeof (total_buf)); 7845 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 7846 7847 /* 7848 * do not print estimated time if hours_left is more than 7849 * 30 days 7850 */ 7851 (void) printf(gettext( 7852 "\t%s copied out of %s at %s/s, %.2f%% done"), 7853 examined_buf, total_buf, rate_buf, 100 * fraction_done); 7854 if (hours_left < (30 * 24)) { 7855 (void) printf(gettext(", %lluh%um to go\n"), 7856 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 7857 } else { 7858 (void) printf(gettext( 7859 ", (copy is slow, no estimated time)\n")); 7860 } 7861 } 7862 free(vdev_name); 7863 7864 if (prs->prs_mapping_memory > 0) { 7865 char mem_buf[7]; 7866 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); 7867 (void) printf(gettext( 7868 "\t%s memory used for removed device mappings\n"), 7869 mem_buf); 7870 } 7871 } 7872 7873 static void 7874 print_checkpoint_status(pool_checkpoint_stat_t *pcs) 7875 { 7876 time_t start; 7877 char space_buf[7]; 7878 7879 if (pcs == NULL || pcs->pcs_state == CS_NONE) 7880 return; 7881 7882 (void) printf(gettext("checkpoint: ")); 7883 7884 start = pcs->pcs_start_time; 7885 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); 7886 7887 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { 7888 char *date = ctime(&start); 7889 7890 /* 7891 * ctime() adds a newline at the end of the generated 7892 * string, thus the weird format specifier and the 7893 * strlen() call used to chop it off from the output. 7894 */ 7895 (void) printf(gettext("created %.*s, consumes %s\n"), 7896 (int)(strlen(date) - 1), date, space_buf); 7897 return; 7898 } 7899 7900 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 7901 7902 (void) printf(gettext("discarding, %s remaining.\n"), 7903 space_buf); 7904 } 7905 7906 static void 7907 print_error_log(zpool_handle_t *zhp) 7908 { 7909 nvlist_t *nverrlist = NULL; 7910 nvpair_t *elem; 7911 char *pathname; 7912 size_t len = MAXPATHLEN * 2; 7913 7914 if (zpool_get_errlog(zhp, &nverrlist) != 0) 7915 return; 7916 7917 (void) printf("errors: Permanent errors have been " 7918 "detected in the following files:\n\n"); 7919 7920 pathname = safe_malloc(len); 7921 elem = NULL; 7922 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 7923 nvlist_t *nv; 7924 uint64_t dsobj, obj; 7925 7926 verify(nvpair_value_nvlist(elem, &nv) == 0); 7927 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 7928 &dsobj) == 0); 7929 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 7930 &obj) == 0); 7931 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 7932 (void) printf("%7s %s\n", "", pathname); 7933 } 7934 free(pathname); 7935 nvlist_free(nverrlist); 7936 } 7937 7938 static void 7939 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, 7940 uint_t nspares) 7941 { 7942 uint_t i; 7943 char *name; 7944 7945 if (nspares == 0) 7946 return; 7947 7948 (void) printf(gettext("\tspares\n")); 7949 7950 for (i = 0; i < nspares; i++) { 7951 name = zpool_vdev_name(g_zfs, zhp, spares[i], 7952 cb->cb_name_flags); 7953 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); 7954 free(name); 7955 } 7956 } 7957 7958 static void 7959 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, 7960 uint_t nl2cache) 7961 { 7962 uint_t i; 7963 char *name; 7964 7965 if (nl2cache == 0) 7966 return; 7967 7968 (void) printf(gettext("\tcache\n")); 7969 7970 for (i = 0; i < nl2cache; i++) { 7971 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], 7972 cb->cb_name_flags); 7973 print_status_config(zhp, cb, name, l2cache[i], 2, 7974 B_FALSE, NULL); 7975 free(name); 7976 } 7977 } 7978 7979 static void 7980 print_dedup_stats(nvlist_t *config) 7981 { 7982 ddt_histogram_t *ddh; 7983 ddt_stat_t *dds; 7984 ddt_object_t *ddo; 7985 uint_t c; 7986 char dspace[6], mspace[6]; 7987 7988 /* 7989 * If the pool was faulted then we may not have been able to 7990 * obtain the config. Otherwise, if we have anything in the dedup 7991 * table continue processing the stats. 7992 */ 7993 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 7994 (uint64_t **)&ddo, &c) != 0) 7995 return; 7996 7997 (void) printf("\n"); 7998 (void) printf(gettext(" dedup: ")); 7999 if (ddo->ddo_count == 0) { 8000 (void) printf(gettext("no DDT entries\n")); 8001 return; 8002 } 8003 8004 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace)); 8005 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace)); 8006 (void) printf("DDT entries %llu, size %s on disk, %s in core\n", 8007 (u_longlong_t)ddo->ddo_count, 8008 dspace, 8009 mspace); 8010 8011 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 8012 (uint64_t **)&dds, &c) == 0); 8013 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 8014 (uint64_t **)&ddh, &c) == 0); 8015 zpool_dump_ddt(dds, ddh); 8016 } 8017 8018 /* 8019 * Display a summary of pool status. Displays a summary such as: 8020 * 8021 * pool: tank 8022 * status: DEGRADED 8023 * reason: One or more devices ... 8024 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 8025 * config: 8026 * mirror DEGRADED 8027 * c1t0d0 OK 8028 * c2t0d0 UNAVAIL 8029 * 8030 * When given the '-v' option, we print out the complete config. If the '-e' 8031 * option is specified, then we print out error rate information as well. 8032 */ 8033 static int 8034 status_callback(zpool_handle_t *zhp, void *data) 8035 { 8036 status_cbdata_t *cbp = data; 8037 nvlist_t *config, *nvroot; 8038 char *msgid; 8039 zpool_status_t reason; 8040 zpool_errata_t errata; 8041 const char *health; 8042 uint_t c; 8043 vdev_stat_t *vs; 8044 8045 config = zpool_get_config(zhp, NULL); 8046 reason = zpool_get_status(zhp, &msgid, &errata); 8047 8048 cbp->cb_count++; 8049 8050 /* 8051 * If we were given 'zpool status -x', only report those pools with 8052 * problems. 8053 */ 8054 if (cbp->cb_explain && 8055 (reason == ZPOOL_STATUS_OK || 8056 reason == ZPOOL_STATUS_VERSION_OLDER || 8057 reason == ZPOOL_STATUS_FEAT_DISABLED || 8058 reason == ZPOOL_STATUS_COMPATIBILITY_ERR)) { 8059 if (!cbp->cb_allpools) { 8060 (void) printf(gettext("pool '%s' is healthy\n"), 8061 zpool_get_name(zhp)); 8062 if (cbp->cb_first) 8063 cbp->cb_first = B_FALSE; 8064 } 8065 return (0); 8066 } 8067 8068 if (cbp->cb_first) 8069 cbp->cb_first = B_FALSE; 8070 else 8071 (void) printf("\n"); 8072 8073 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 8074 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 8075 (uint64_t **)&vs, &c) == 0); 8076 8077 health = zpool_get_state_str(zhp); 8078 8079 printf(" "); 8080 printf_color(ANSI_BOLD, gettext("pool:")); 8081 printf(" %s\n", zpool_get_name(zhp)); 8082 printf(" "); 8083 printf_color(ANSI_BOLD, gettext("state: ")); 8084 8085 printf_color(health_str_to_color(health), "%s", health); 8086 8087 printf("\n"); 8088 8089 switch (reason) { 8090 case ZPOOL_STATUS_MISSING_DEV_R: 8091 printf_color(ANSI_BOLD, gettext("status: ")); 8092 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8093 "not be opened. Sufficient replicas exist for\n\tthe pool " 8094 "to continue functioning in a degraded state.\n")); 8095 printf_color(ANSI_BOLD, gettext("action: ")); 8096 printf_color(ANSI_YELLOW, gettext("Attach the missing device " 8097 "and online it using 'zpool online'.\n")); 8098 break; 8099 8100 case ZPOOL_STATUS_MISSING_DEV_NR: 8101 printf_color(ANSI_BOLD, gettext("status: ")); 8102 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8103 "not be opened. There are insufficient\n\treplicas for the" 8104 " pool to continue functioning.\n")); 8105 printf_color(ANSI_BOLD, gettext("action: ")); 8106 printf_color(ANSI_YELLOW, gettext("Attach the missing device " 8107 "and online it using 'zpool online'.\n")); 8108 break; 8109 8110 case ZPOOL_STATUS_CORRUPT_LABEL_R: 8111 printf_color(ANSI_BOLD, gettext("status: ")); 8112 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8113 "not be used because the label is missing or\n\tinvalid. " 8114 "Sufficient replicas exist for the pool to continue\n\t" 8115 "functioning in a degraded state.\n")); 8116 printf_color(ANSI_BOLD, gettext("action: ")); 8117 printf_color(ANSI_YELLOW, gettext("Replace the device using " 8118 "'zpool replace'.\n")); 8119 break; 8120 8121 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 8122 printf_color(ANSI_BOLD, gettext("status: ")); 8123 printf_color(ANSI_YELLOW, gettext("One or more devices could " 8124 "not be used because the label is missing \n\tor invalid. " 8125 "There are insufficient replicas for the pool to " 8126 "continue\n\tfunctioning.\n")); 8127 zpool_explain_recover(zpool_get_handle(zhp), 8128 zpool_get_name(zhp), reason, config); 8129 break; 8130 8131 case ZPOOL_STATUS_FAILING_DEV: 8132 printf_color(ANSI_BOLD, gettext("status: ")); 8133 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8134 "experienced an unrecoverable error. An\n\tattempt was " 8135 "made to correct the error. Applications are " 8136 "unaffected.\n")); 8137 printf_color(ANSI_BOLD, gettext("action: ")); 8138 printf_color(ANSI_YELLOW, gettext("Determine if the " 8139 "device needs to be replaced, and clear the errors\n\tusing" 8140 " 'zpool clear' or replace the device with 'zpool " 8141 "replace'.\n")); 8142 break; 8143 8144 case ZPOOL_STATUS_OFFLINE_DEV: 8145 printf_color(ANSI_BOLD, gettext("status: ")); 8146 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8147 "been taken offline by the administrator.\n\tSufficient " 8148 "replicas exist for the pool to continue functioning in " 8149 "a\n\tdegraded state.\n")); 8150 printf_color(ANSI_BOLD, gettext("action: ")); 8151 printf_color(ANSI_YELLOW, gettext("Online the device " 8152 "using 'zpool online' or replace the device with\n\t'zpool " 8153 "replace'.\n")); 8154 break; 8155 8156 case ZPOOL_STATUS_REMOVED_DEV: 8157 printf_color(ANSI_BOLD, gettext("status: ")); 8158 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8159 "been removed by the administrator.\n\tSufficient " 8160 "replicas exist for the pool to continue functioning in " 8161 "a\n\tdegraded state.\n")); 8162 printf_color(ANSI_BOLD, gettext("action: ")); 8163 printf_color(ANSI_YELLOW, gettext("Online the device " 8164 "using zpool online' or replace the device with\n\t'zpool " 8165 "replace'.\n")); 8166 break; 8167 8168 case ZPOOL_STATUS_RESILVERING: 8169 case ZPOOL_STATUS_REBUILDING: 8170 printf_color(ANSI_BOLD, gettext("status: ")); 8171 printf_color(ANSI_YELLOW, gettext("One or more devices is " 8172 "currently being resilvered. The pool will\n\tcontinue " 8173 "to function, possibly in a degraded state.\n")); 8174 printf_color(ANSI_BOLD, gettext("action: ")); 8175 printf_color(ANSI_YELLOW, gettext("Wait for the resilver to " 8176 "complete.\n")); 8177 break; 8178 8179 case ZPOOL_STATUS_REBUILD_SCRUB: 8180 printf_color(ANSI_BOLD, gettext("status: ")); 8181 printf_color(ANSI_YELLOW, gettext("One or more devices have " 8182 "been sequentially resilvered, scrubbing\n\tthe pool " 8183 "is recommended.\n")); 8184 printf_color(ANSI_BOLD, gettext("action: ")); 8185 printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to " 8186 "verify all data checksums.\n")); 8187 break; 8188 8189 case ZPOOL_STATUS_CORRUPT_DATA: 8190 printf_color(ANSI_BOLD, gettext("status: ")); 8191 printf_color(ANSI_YELLOW, gettext("One or more devices has " 8192 "experienced an error resulting in data\n\tcorruption. " 8193 "Applications may be affected.\n")); 8194 printf_color(ANSI_BOLD, gettext("action: ")); 8195 printf_color(ANSI_YELLOW, gettext("Restore the file in question" 8196 " if possible. Otherwise restore the\n\tentire pool from " 8197 "backup.\n")); 8198 break; 8199 8200 case ZPOOL_STATUS_CORRUPT_POOL: 8201 printf_color(ANSI_BOLD, gettext("status: ")); 8202 printf_color(ANSI_YELLOW, gettext("The pool metadata is " 8203 "corrupted and the pool cannot be opened.\n")); 8204 zpool_explain_recover(zpool_get_handle(zhp), 8205 zpool_get_name(zhp), reason, config); 8206 break; 8207 8208 case ZPOOL_STATUS_VERSION_OLDER: 8209 printf_color(ANSI_BOLD, gettext("status: ")); 8210 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 8211 "a legacy on-disk format. The pool can\n\tstill be used, " 8212 "but some features are unavailable.\n")); 8213 printf_color(ANSI_BOLD, gettext("action: ")); 8214 printf_color(ANSI_YELLOW, gettext("Upgrade the pool using " 8215 "'zpool upgrade'. Once this is done, the\n\tpool will no " 8216 "longer be accessible on software that does not support\n\t" 8217 "feature flags.\n")); 8218 break; 8219 8220 case ZPOOL_STATUS_VERSION_NEWER: 8221 printf_color(ANSI_BOLD, gettext("status: ")); 8222 printf_color(ANSI_YELLOW, gettext("The pool has been upgraded " 8223 "to a newer, incompatible on-disk version.\n\tThe pool " 8224 "cannot be accessed on this system.\n")); 8225 printf_color(ANSI_BOLD, gettext("action: ")); 8226 printf_color(ANSI_YELLOW, gettext("Access the pool from a " 8227 "system running more recent software, or\n\trestore the " 8228 "pool from backup.\n")); 8229 break; 8230 8231 case ZPOOL_STATUS_FEAT_DISABLED: 8232 printf_color(ANSI_BOLD, gettext("status: ")); 8233 printf_color(ANSI_YELLOW, gettext("Some supported and " 8234 "requested features are not enabled on the pool.\n\t" 8235 "The pool can still be used, but some features are " 8236 "unavailable.\n")); 8237 printf_color(ANSI_BOLD, gettext("action: ")); 8238 printf_color(ANSI_YELLOW, gettext("Enable all features using " 8239 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 8240 "longer be accessible by software that does not support\n\t" 8241 "the features. See zpool-features(5) for details.\n")); 8242 break; 8243 8244 case ZPOOL_STATUS_COMPATIBILITY_ERR: 8245 printf_color(ANSI_BOLD, gettext("status: ")); 8246 printf_color(ANSI_YELLOW, gettext("This pool has a " 8247 "compatibility list specified, but it could not be\n\t" 8248 "read/parsed at this time. The pool can still be used, " 8249 "but this\n\tshould be investigated.\n")); 8250 printf_color(ANSI_BOLD, gettext("action: ")); 8251 printf_color(ANSI_YELLOW, gettext("Check the value of the " 8252 "'compatibility' property against the\n\t" 8253 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " 8254 ZPOOL_DATA_COMPAT_D ".\n")); 8255 break; 8256 8257 case ZPOOL_STATUS_UNSUP_FEAT_READ: 8258 printf_color(ANSI_BOLD, gettext("status: ")); 8259 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " 8260 "on this system because it uses the\n\tfollowing feature(s)" 8261 " not supported on this system:\n")); 8262 zpool_print_unsup_feat(config); 8263 (void) printf("\n"); 8264 printf_color(ANSI_BOLD, gettext("action: ")); 8265 printf_color(ANSI_YELLOW, gettext("Access the pool from a " 8266 "system that supports the required feature(s),\n\tor " 8267 "restore the pool from backup.\n")); 8268 break; 8269 8270 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 8271 printf_color(ANSI_BOLD, gettext("status: ")); 8272 printf_color(ANSI_YELLOW, gettext("The pool can only be " 8273 "accessed in read-only mode on this system. It\n\tcannot be" 8274 " accessed in read-write mode because it uses the " 8275 "following\n\tfeature(s) not supported on this system:\n")); 8276 zpool_print_unsup_feat(config); 8277 (void) printf("\n"); 8278 printf_color(ANSI_BOLD, gettext("action: ")); 8279 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " 8280 "in read-write mode. Import the pool with\n" 8281 "\t\"-o readonly=on\", access the pool from a system that " 8282 "supports the\n\trequired feature(s), or restore the " 8283 "pool from backup.\n")); 8284 break; 8285 8286 case ZPOOL_STATUS_FAULTED_DEV_R: 8287 printf_color(ANSI_BOLD, gettext("status: ")); 8288 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8289 "faulted in response to persistent errors.\n\tSufficient " 8290 "replicas exist for the pool to continue functioning " 8291 "in a\n\tdegraded state.\n")); 8292 printf_color(ANSI_BOLD, gettext("action: ")); 8293 printf_color(ANSI_YELLOW, gettext("Replace the faulted device, " 8294 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 8295 break; 8296 8297 case ZPOOL_STATUS_FAULTED_DEV_NR: 8298 printf_color(ANSI_BOLD, gettext("status: ")); 8299 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8300 "faulted in response to persistent errors. There are " 8301 "insufficient replicas for the pool to\n\tcontinue " 8302 "functioning.\n")); 8303 printf_color(ANSI_BOLD, gettext("action: ")); 8304 printf_color(ANSI_YELLOW, gettext("Destroy and re-create the " 8305 "pool from a backup source. Manually marking the device\n" 8306 "\trepaired using 'zpool clear' may allow some data " 8307 "to be recovered.\n")); 8308 break; 8309 8310 case ZPOOL_STATUS_IO_FAILURE_MMP: 8311 printf_color(ANSI_BOLD, gettext("status: ")); 8312 printf_color(ANSI_YELLOW, gettext("The pool is suspended " 8313 "because multihost writes failed or were delayed;\n\t" 8314 "another system could import the pool undetected.\n")); 8315 printf_color(ANSI_BOLD, gettext("action: ")); 8316 printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices" 8317 " are connected, then reboot your system and\n\timport the " 8318 "pool.\n")); 8319 break; 8320 8321 case ZPOOL_STATUS_IO_FAILURE_WAIT: 8322 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 8323 printf_color(ANSI_BOLD, gettext("status: ")); 8324 printf_color(ANSI_YELLOW, gettext("One or more devices are " 8325 "faulted in response to IO failures.\n")); 8326 printf_color(ANSI_BOLD, gettext("action: ")); 8327 printf_color(ANSI_YELLOW, gettext("Make sure the affected " 8328 "devices are connected, then run 'zpool clear'.\n")); 8329 break; 8330 8331 case ZPOOL_STATUS_BAD_LOG: 8332 printf_color(ANSI_BOLD, gettext("status: ")); 8333 printf_color(ANSI_YELLOW, gettext("An intent log record " 8334 "could not be read.\n" 8335 "\tWaiting for administrator intervention to fix the " 8336 "faulted pool.\n")); 8337 printf_color(ANSI_BOLD, gettext("action: ")); 8338 printf_color(ANSI_YELLOW, gettext("Either restore the affected " 8339 "device(s) and run 'zpool online',\n" 8340 "\tor ignore the intent log records by running " 8341 "'zpool clear'.\n")); 8342 break; 8343 8344 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 8345 (void) printf(gettext("status: One or more devices are " 8346 "configured to use a non-native block size.\n" 8347 "\tExpect reduced performance.\n")); 8348 (void) printf(gettext("action: Replace affected devices with " 8349 "devices that support the\n\tconfigured block size, or " 8350 "migrate data to a properly configured\n\tpool.\n")); 8351 break; 8352 8353 case ZPOOL_STATUS_HOSTID_MISMATCH: 8354 printf_color(ANSI_BOLD, gettext("status: ")); 8355 printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid" 8356 " and system hostid on imported pool.\n\tThis pool was " 8357 "previously imported into a system with a different " 8358 "hostid,\n\tand then was verbatim imported into this " 8359 "system.\n")); 8360 printf_color(ANSI_BOLD, gettext("action: ")); 8361 printf_color(ANSI_YELLOW, gettext("Export this pool on all " 8362 "systems on which it is imported.\n" 8363 "\tThen import it to correct the mismatch.\n")); 8364 break; 8365 8366 case ZPOOL_STATUS_ERRATA: 8367 printf_color(ANSI_BOLD, gettext("status: ")); 8368 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), 8369 errata); 8370 8371 switch (errata) { 8372 case ZPOOL_ERRATA_NONE: 8373 break; 8374 8375 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 8376 printf_color(ANSI_BOLD, gettext("action: ")); 8377 printf_color(ANSI_YELLOW, gettext("To correct the issue" 8378 " run 'zpool scrub'.\n")); 8379 break; 8380 8381 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 8382 (void) printf(gettext("\tExisting encrypted datasets " 8383 "contain an on-disk incompatibility\n\twhich " 8384 "needs to be corrected.\n")); 8385 printf_color(ANSI_BOLD, gettext("action: ")); 8386 printf_color(ANSI_YELLOW, gettext("To correct the issue" 8387 " backup existing encrypted datasets to new\n\t" 8388 "encrypted datasets and destroy the old ones. " 8389 "'zfs mount -o ro' can\n\tbe used to temporarily " 8390 "mount existing encrypted datasets readonly.\n")); 8391 break; 8392 8393 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 8394 (void) printf(gettext("\tExisting encrypted snapshots " 8395 "and bookmarks contain an on-disk\n\tincompat" 8396 "ibility. This may cause on-disk corruption if " 8397 "they are used\n\twith 'zfs recv'.\n")); 8398 printf_color(ANSI_BOLD, gettext("action: ")); 8399 printf_color(ANSI_YELLOW, gettext("To correct the" 8400 "issue, enable the bookmark_v2 feature. No " 8401 "additional\n\taction is needed if there are no " 8402 "encrypted snapshots or bookmarks.\n\tIf preserving" 8403 "the encrypted snapshots and bookmarks is required," 8404 " use\n\ta non-raw send to backup and restore them." 8405 " Alternately, they may be\n\tremoved to resolve " 8406 "the incompatibility.\n")); 8407 break; 8408 8409 default: 8410 /* 8411 * All errata which allow the pool to be imported 8412 * must contain an action message. 8413 */ 8414 assert(0); 8415 } 8416 break; 8417 8418 default: 8419 /* 8420 * The remaining errors can't actually be generated, yet. 8421 */ 8422 assert(reason == ZPOOL_STATUS_OK); 8423 } 8424 8425 if (msgid != NULL) { 8426 printf(" "); 8427 printf_color(ANSI_BOLD, gettext("see:")); 8428 printf(gettext( 8429 " https://openzfs.github.io/openzfs-docs/msg/%s\n"), 8430 msgid); 8431 } 8432 8433 if (config != NULL) { 8434 uint64_t nerr; 8435 nvlist_t **spares, **l2cache; 8436 uint_t nspares, nl2cache; 8437 pool_checkpoint_stat_t *pcs = NULL; 8438 pool_removal_stat_t *prs = NULL; 8439 8440 print_scan_status(zhp, nvroot); 8441 8442 (void) nvlist_lookup_uint64_array(nvroot, 8443 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 8444 print_removal_status(zhp, prs); 8445 8446 (void) nvlist_lookup_uint64_array(nvroot, 8447 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 8448 print_checkpoint_status(pcs); 8449 8450 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, 8451 cbp->cb_name_flags | VDEV_NAME_TYPE_ID); 8452 if (cbp->cb_namewidth < 10) 8453 cbp->cb_namewidth = 10; 8454 8455 color_start(ANSI_BOLD); 8456 (void) printf(gettext("config:\n\n")); 8457 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), 8458 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", 8459 "CKSUM"); 8460 color_end(); 8461 8462 if (cbp->cb_print_slow_ios) { 8463 printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); 8464 } 8465 8466 if (cbp->vcdl != NULL) 8467 print_cmd_columns(cbp->vcdl, 0); 8468 8469 printf("\n"); 8470 8471 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, 8472 B_FALSE, NULL); 8473 8474 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); 8475 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 8476 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); 8477 8478 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 8479 &l2cache, &nl2cache) == 0) 8480 print_l2cache(zhp, cbp, l2cache, nl2cache); 8481 8482 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 8483 &spares, &nspares) == 0) 8484 print_spares(zhp, cbp, spares, nspares); 8485 8486 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 8487 &nerr) == 0) { 8488 nvlist_t *nverrlist = NULL; 8489 8490 /* 8491 * If the approximate error count is small, get a 8492 * precise count by fetching the entire log and 8493 * uniquifying the results. 8494 */ 8495 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 8496 zpool_get_errlog(zhp, &nverrlist) == 0) { 8497 nvpair_t *elem; 8498 8499 elem = NULL; 8500 nerr = 0; 8501 while ((elem = nvlist_next_nvpair(nverrlist, 8502 elem)) != NULL) { 8503 nerr++; 8504 } 8505 } 8506 nvlist_free(nverrlist); 8507 8508 (void) printf("\n"); 8509 8510 if (nerr == 0) 8511 (void) printf(gettext("errors: No known data " 8512 "errors\n")); 8513 else if (!cbp->cb_verbose) 8514 (void) printf(gettext("errors: %llu data " 8515 "errors, use '-v' for a list\n"), 8516 (u_longlong_t)nerr); 8517 else 8518 print_error_log(zhp); 8519 } 8520 8521 if (cbp->cb_dedup_stats) 8522 print_dedup_stats(config); 8523 } else { 8524 (void) printf(gettext("config: The configuration cannot be " 8525 "determined.\n")); 8526 } 8527 8528 return (0); 8529 } 8530 8531 /* 8532 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ... 8533 * [interval [count]] 8534 * 8535 * -c CMD For each vdev, run command CMD 8536 * -i Display vdev initialization status. 8537 * -g Display guid for individual vdev name. 8538 * -L Follow links when resolving vdev path name. 8539 * -p Display values in parsable (exact) format. 8540 * -P Display full path for vdev name. 8541 * -s Display slow IOs column. 8542 * -v Display complete error logs 8543 * -x Display only pools with potential problems 8544 * -D Display dedup status (undocumented) 8545 * -t Display vdev TRIM status. 8546 * -T Display a timestamp in date(1) or Unix format 8547 * 8548 * Describes the health status of all pools or some subset. 8549 */ 8550 int 8551 zpool_do_status(int argc, char **argv) 8552 { 8553 int c; 8554 int ret; 8555 float interval = 0; 8556 unsigned long count = 0; 8557 status_cbdata_t cb = { 0 }; 8558 char *cmd = NULL; 8559 8560 /* check options */ 8561 while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) { 8562 switch (c) { 8563 case 'c': 8564 if (cmd != NULL) { 8565 fprintf(stderr, 8566 gettext("Can't set -c flag twice\n")); 8567 exit(1); 8568 } 8569 8570 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 8571 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 8572 fprintf(stderr, gettext( 8573 "Can't run -c, disabled by " 8574 "ZPOOL_SCRIPTS_ENABLED.\n")); 8575 exit(1); 8576 } 8577 8578 if ((getuid() <= 0 || geteuid() <= 0) && 8579 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 8580 fprintf(stderr, gettext( 8581 "Can't run -c with root privileges " 8582 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 8583 exit(1); 8584 } 8585 cmd = optarg; 8586 break; 8587 case 'i': 8588 cb.cb_print_vdev_init = B_TRUE; 8589 break; 8590 case 'g': 8591 cb.cb_name_flags |= VDEV_NAME_GUID; 8592 break; 8593 case 'L': 8594 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 8595 break; 8596 case 'p': 8597 cb.cb_literal = B_TRUE; 8598 break; 8599 case 'P': 8600 cb.cb_name_flags |= VDEV_NAME_PATH; 8601 break; 8602 case 's': 8603 cb.cb_print_slow_ios = B_TRUE; 8604 break; 8605 case 'v': 8606 cb.cb_verbose = B_TRUE; 8607 break; 8608 case 'x': 8609 cb.cb_explain = B_TRUE; 8610 break; 8611 case 'D': 8612 cb.cb_dedup_stats = B_TRUE; 8613 break; 8614 case 't': 8615 cb.cb_print_vdev_trim = B_TRUE; 8616 break; 8617 case 'T': 8618 get_timestamp_arg(*optarg); 8619 break; 8620 case '?': 8621 if (optopt == 'c') { 8622 print_zpool_script_list("status"); 8623 exit(0); 8624 } else { 8625 fprintf(stderr, 8626 gettext("invalid option '%c'\n"), optopt); 8627 } 8628 usage(B_FALSE); 8629 } 8630 } 8631 8632 argc -= optind; 8633 argv += optind; 8634 8635 get_interval_count(&argc, argv, &interval, &count); 8636 8637 if (argc == 0) 8638 cb.cb_allpools = B_TRUE; 8639 8640 cb.cb_first = B_TRUE; 8641 cb.cb_print_status = B_TRUE; 8642 8643 for (;;) { 8644 if (timestamp_fmt != NODATE) 8645 print_timestamp(timestamp_fmt); 8646 8647 if (cmd != NULL) 8648 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, 8649 NULL, NULL, 0, 0); 8650 8651 ret = for_each_pool(argc, argv, B_TRUE, NULL, cb.cb_literal, 8652 status_callback, &cb); 8653 8654 if (cb.vcdl != NULL) 8655 free_vdev_cmd_data_list(cb.vcdl); 8656 8657 if (argc == 0 && cb.cb_count == 0) 8658 (void) fprintf(stderr, gettext("no pools available\n")); 8659 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 8660 (void) printf(gettext("all pools are healthy\n")); 8661 8662 if (ret != 0) 8663 return (ret); 8664 8665 if (interval == 0) 8666 break; 8667 8668 if (count != 0 && --count == 0) 8669 break; 8670 8671 (void) fsleep(interval); 8672 } 8673 8674 return (0); 8675 } 8676 8677 typedef struct upgrade_cbdata { 8678 int cb_first; 8679 int cb_argc; 8680 uint64_t cb_version; 8681 char **cb_argv; 8682 } upgrade_cbdata_t; 8683 8684 static int 8685 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) 8686 { 8687 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8688 int *count = (int *)unsupp_fs; 8689 8690 if (zfs_version > ZPL_VERSION) { 8691 (void) printf(gettext("%s (v%d) is not supported by this " 8692 "implementation of ZFS.\n"), 8693 zfs_get_name(zhp), zfs_version); 8694 (*count)++; 8695 } 8696 8697 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs); 8698 8699 zfs_close(zhp); 8700 8701 return (0); 8702 } 8703 8704 static int 8705 upgrade_version(zpool_handle_t *zhp, uint64_t version) 8706 { 8707 int ret; 8708 nvlist_t *config; 8709 uint64_t oldversion; 8710 int unsupp_fs = 0; 8711 8712 config = zpool_get_config(zhp, NULL); 8713 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8714 &oldversion) == 0); 8715 8716 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 8717 assert(oldversion < version); 8718 8719 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); 8720 if (ret != 0) 8721 return (ret); 8722 8723 if (unsupp_fs) { 8724 (void) fprintf(stderr, gettext("Upgrade not performed due " 8725 "to %d unsupported filesystems (max v%d).\n"), 8726 unsupp_fs, (int)ZPL_VERSION); 8727 return (1); 8728 } 8729 8730 ret = zpool_upgrade(zhp, version); 8731 if (ret != 0) 8732 return (ret); 8733 8734 if (version >= SPA_VERSION_FEATURES) { 8735 (void) printf(gettext("Successfully upgraded " 8736 "'%s' from version %llu to feature flags.\n"), 8737 zpool_get_name(zhp), (u_longlong_t)oldversion); 8738 } else { 8739 (void) printf(gettext("Successfully upgraded " 8740 "'%s' from version %llu to version %llu.\n"), 8741 zpool_get_name(zhp), (u_longlong_t)oldversion, 8742 (u_longlong_t)version); 8743 } 8744 8745 return (0); 8746 } 8747 8748 static int 8749 upgrade_enable_all(zpool_handle_t *zhp, int *countp) 8750 { 8751 int i, ret, count; 8752 boolean_t firstff = B_TRUE; 8753 nvlist_t *enabled = zpool_get_features(zhp); 8754 8755 char compat[ZFS_MAXPROPLEN]; 8756 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 8757 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 8758 compat[0] = '\0'; 8759 8760 boolean_t requested_features[SPA_FEATURES]; 8761 if (zpool_do_load_compat(compat, requested_features) != 8762 ZPOOL_COMPATIBILITY_OK) 8763 return (-1); 8764 8765 count = 0; 8766 for (i = 0; i < SPA_FEATURES; i++) { 8767 const char *fname = spa_feature_table[i].fi_uname; 8768 const char *fguid = spa_feature_table[i].fi_guid; 8769 8770 if (!spa_feature_table[i].fi_zfs_mod_supported) 8771 continue; 8772 8773 if (!nvlist_exists(enabled, fguid) && requested_features[i]) { 8774 char *propname; 8775 verify(-1 != asprintf(&propname, "feature@%s", fname)); 8776 ret = zpool_set_prop(zhp, propname, 8777 ZFS_FEATURE_ENABLED); 8778 if (ret != 0) { 8779 free(propname); 8780 return (ret); 8781 } 8782 count++; 8783 8784 if (firstff) { 8785 (void) printf(gettext("Enabled the " 8786 "following features on '%s':\n"), 8787 zpool_get_name(zhp)); 8788 firstff = B_FALSE; 8789 } 8790 (void) printf(gettext(" %s\n"), fname); 8791 free(propname); 8792 } 8793 } 8794 8795 if (countp != NULL) 8796 *countp = count; 8797 return (0); 8798 } 8799 8800 static int 8801 upgrade_cb(zpool_handle_t *zhp, void *arg) 8802 { 8803 upgrade_cbdata_t *cbp = arg; 8804 nvlist_t *config; 8805 uint64_t version; 8806 boolean_t printnl = B_FALSE; 8807 int ret; 8808 8809 config = zpool_get_config(zhp, NULL); 8810 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8811 &version) == 0); 8812 8813 assert(SPA_VERSION_IS_SUPPORTED(version)); 8814 8815 if (version < cbp->cb_version) { 8816 cbp->cb_first = B_FALSE; 8817 ret = upgrade_version(zhp, cbp->cb_version); 8818 if (ret != 0) 8819 return (ret); 8820 printnl = B_TRUE; 8821 8822 /* 8823 * If they did "zpool upgrade -a", then we could 8824 * be doing ioctls to different pools. We need 8825 * to log this history once to each pool, and bypass 8826 * the normal history logging that happens in main(). 8827 */ 8828 (void) zpool_log_history(g_zfs, history_str); 8829 log_history = B_FALSE; 8830 } 8831 8832 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 8833 int count; 8834 ret = upgrade_enable_all(zhp, &count); 8835 if (ret != 0) 8836 return (ret); 8837 8838 if (count > 0) { 8839 cbp->cb_first = B_FALSE; 8840 printnl = B_TRUE; 8841 } 8842 } 8843 8844 if (printnl) { 8845 (void) printf(gettext("\n")); 8846 } 8847 8848 return (0); 8849 } 8850 8851 static int 8852 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 8853 { 8854 upgrade_cbdata_t *cbp = arg; 8855 nvlist_t *config; 8856 uint64_t version; 8857 8858 config = zpool_get_config(zhp, NULL); 8859 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8860 &version) == 0); 8861 8862 assert(SPA_VERSION_IS_SUPPORTED(version)); 8863 8864 if (version < SPA_VERSION_FEATURES) { 8865 if (cbp->cb_first) { 8866 (void) printf(gettext("The following pools are " 8867 "formatted with legacy version numbers and can\n" 8868 "be upgraded to use feature flags. After " 8869 "being upgraded, these pools\nwill no " 8870 "longer be accessible by software that does not " 8871 "support feature\nflags.\n\n")); 8872 (void) printf(gettext("VER POOL\n")); 8873 (void) printf(gettext("--- ------------\n")); 8874 cbp->cb_first = B_FALSE; 8875 } 8876 8877 (void) printf("%2llu %s\n", (u_longlong_t)version, 8878 zpool_get_name(zhp)); 8879 } 8880 8881 return (0); 8882 } 8883 8884 static int 8885 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 8886 { 8887 upgrade_cbdata_t *cbp = arg; 8888 nvlist_t *config; 8889 uint64_t version; 8890 8891 config = zpool_get_config(zhp, NULL); 8892 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 8893 &version) == 0); 8894 8895 if (version >= SPA_VERSION_FEATURES) { 8896 int i; 8897 boolean_t poolfirst = B_TRUE; 8898 nvlist_t *enabled = zpool_get_features(zhp); 8899 8900 for (i = 0; i < SPA_FEATURES; i++) { 8901 const char *fguid = spa_feature_table[i].fi_guid; 8902 const char *fname = spa_feature_table[i].fi_uname; 8903 8904 if (!spa_feature_table[i].fi_zfs_mod_supported) 8905 continue; 8906 8907 if (!nvlist_exists(enabled, fguid)) { 8908 if (cbp->cb_first) { 8909 (void) printf(gettext("\nSome " 8910 "supported features are not " 8911 "enabled on the following pools. " 8912 "Once a\nfeature is enabled the " 8913 "pool may become incompatible with " 8914 "software\nthat does not support " 8915 "the feature. See " 8916 "zpool-features(5) for " 8917 "details.\n\n")); 8918 (void) printf(gettext("POOL " 8919 "FEATURE\n")); 8920 (void) printf(gettext("------" 8921 "---------\n")); 8922 cbp->cb_first = B_FALSE; 8923 } 8924 8925 if (poolfirst) { 8926 (void) printf(gettext("%s\n"), 8927 zpool_get_name(zhp)); 8928 poolfirst = B_FALSE; 8929 } 8930 8931 (void) printf(gettext(" %s\n"), fname); 8932 } 8933 /* 8934 * If they did "zpool upgrade -a", then we could 8935 * be doing ioctls to different pools. We need 8936 * to log this history once to each pool, and bypass 8937 * the normal history logging that happens in main(). 8938 */ 8939 (void) zpool_log_history(g_zfs, history_str); 8940 log_history = B_FALSE; 8941 } 8942 } 8943 8944 return (0); 8945 } 8946 8947 /* ARGSUSED */ 8948 static int 8949 upgrade_one(zpool_handle_t *zhp, void *data) 8950 { 8951 boolean_t printnl = B_FALSE; 8952 upgrade_cbdata_t *cbp = data; 8953 uint64_t cur_version; 8954 int ret; 8955 8956 if (strcmp("log", zpool_get_name(zhp)) == 0) { 8957 (void) fprintf(stderr, gettext("'log' is now a reserved word\n" 8958 "Pool 'log' must be renamed using export and import" 8959 " to upgrade.\n")); 8960 return (1); 8961 } 8962 8963 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 8964 if (cur_version > cbp->cb_version) { 8965 (void) printf(gettext("Pool '%s' is already formatted " 8966 "using more current version '%llu'.\n\n"), 8967 zpool_get_name(zhp), (u_longlong_t)cur_version); 8968 return (0); 8969 } 8970 8971 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 8972 (void) printf(gettext("Pool '%s' is already formatted " 8973 "using version %llu.\n\n"), zpool_get_name(zhp), 8974 (u_longlong_t)cbp->cb_version); 8975 return (0); 8976 } 8977 8978 if (cur_version != cbp->cb_version) { 8979 printnl = B_TRUE; 8980 ret = upgrade_version(zhp, cbp->cb_version); 8981 if (ret != 0) 8982 return (ret); 8983 } 8984 8985 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 8986 int count = 0; 8987 ret = upgrade_enable_all(zhp, &count); 8988 if (ret != 0) 8989 return (ret); 8990 8991 if (count != 0) { 8992 printnl = B_TRUE; 8993 } else if (cur_version == SPA_VERSION) { 8994 (void) printf(gettext("Pool '%s' already has all " 8995 "supported and requested features enabled.\n"), 8996 zpool_get_name(zhp)); 8997 } 8998 } 8999 9000 if (printnl) { 9001 (void) printf(gettext("\n")); 9002 } 9003 9004 return (0); 9005 } 9006 9007 /* 9008 * zpool upgrade 9009 * zpool upgrade -v 9010 * zpool upgrade [-V version] <-a | pool ...> 9011 * 9012 * With no arguments, display downrev'd ZFS pool available for upgrade. 9013 * Individual pools can be upgraded by specifying the pool, and '-a' will 9014 * upgrade all pools. 9015 */ 9016 int 9017 zpool_do_upgrade(int argc, char **argv) 9018 { 9019 int c; 9020 upgrade_cbdata_t cb = { 0 }; 9021 int ret = 0; 9022 boolean_t showversions = B_FALSE; 9023 boolean_t upgradeall = B_FALSE; 9024 char *end; 9025 9026 9027 /* check options */ 9028 while ((c = getopt(argc, argv, ":avV:")) != -1) { 9029 switch (c) { 9030 case 'a': 9031 upgradeall = B_TRUE; 9032 break; 9033 case 'v': 9034 showversions = B_TRUE; 9035 break; 9036 case 'V': 9037 cb.cb_version = strtoll(optarg, &end, 10); 9038 if (*end != '\0' || 9039 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 9040 (void) fprintf(stderr, 9041 gettext("invalid version '%s'\n"), optarg); 9042 usage(B_FALSE); 9043 } 9044 break; 9045 case ':': 9046 (void) fprintf(stderr, gettext("missing argument for " 9047 "'%c' option\n"), optopt); 9048 usage(B_FALSE); 9049 break; 9050 case '?': 9051 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9052 optopt); 9053 usage(B_FALSE); 9054 } 9055 } 9056 9057 cb.cb_argc = argc; 9058 cb.cb_argv = argv; 9059 argc -= optind; 9060 argv += optind; 9061 9062 if (cb.cb_version == 0) { 9063 cb.cb_version = SPA_VERSION; 9064 } else if (!upgradeall && argc == 0) { 9065 (void) fprintf(stderr, gettext("-V option is " 9066 "incompatible with other arguments\n")); 9067 usage(B_FALSE); 9068 } 9069 9070 if (showversions) { 9071 if (upgradeall || argc != 0) { 9072 (void) fprintf(stderr, gettext("-v option is " 9073 "incompatible with other arguments\n")); 9074 usage(B_FALSE); 9075 } 9076 } else if (upgradeall) { 9077 if (argc != 0) { 9078 (void) fprintf(stderr, gettext("-a option should not " 9079 "be used along with a pool name\n")); 9080 usage(B_FALSE); 9081 } 9082 } 9083 9084 (void) printf(gettext("This system supports ZFS pool feature " 9085 "flags.\n\n")); 9086 if (showversions) { 9087 int i; 9088 9089 (void) printf(gettext("The following features are " 9090 "supported:\n\n")); 9091 (void) printf(gettext("FEAT DESCRIPTION\n")); 9092 (void) printf("----------------------------------------------" 9093 "---------------\n"); 9094 for (i = 0; i < SPA_FEATURES; i++) { 9095 zfeature_info_t *fi = &spa_feature_table[i]; 9096 if (!fi->fi_zfs_mod_supported) 9097 continue; 9098 const char *ro = 9099 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 9100 " (read-only compatible)" : ""; 9101 9102 (void) printf("%-37s%s\n", fi->fi_uname, ro); 9103 (void) printf(" %s\n", fi->fi_desc); 9104 } 9105 (void) printf("\n"); 9106 9107 (void) printf(gettext("The following legacy versions are also " 9108 "supported:\n\n")); 9109 (void) printf(gettext("VER DESCRIPTION\n")); 9110 (void) printf("--- -----------------------------------------" 9111 "---------------\n"); 9112 (void) printf(gettext(" 1 Initial ZFS version\n")); 9113 (void) printf(gettext(" 2 Ditto blocks " 9114 "(replicated metadata)\n")); 9115 (void) printf(gettext(" 3 Hot spares and double parity " 9116 "RAID-Z\n")); 9117 (void) printf(gettext(" 4 zpool history\n")); 9118 (void) printf(gettext(" 5 Compression using the gzip " 9119 "algorithm\n")); 9120 (void) printf(gettext(" 6 bootfs pool property\n")); 9121 (void) printf(gettext(" 7 Separate intent log devices\n")); 9122 (void) printf(gettext(" 8 Delegated administration\n")); 9123 (void) printf(gettext(" 9 refquota and refreservation " 9124 "properties\n")); 9125 (void) printf(gettext(" 10 Cache devices\n")); 9126 (void) printf(gettext(" 11 Improved scrub performance\n")); 9127 (void) printf(gettext(" 12 Snapshot properties\n")); 9128 (void) printf(gettext(" 13 snapused property\n")); 9129 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 9130 (void) printf(gettext(" 15 user/group space accounting\n")); 9131 (void) printf(gettext(" 16 stmf property support\n")); 9132 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 9133 (void) printf(gettext(" 18 Snapshot user holds\n")); 9134 (void) printf(gettext(" 19 Log device removal\n")); 9135 (void) printf(gettext(" 20 Compression using zle " 9136 "(zero-length encoding)\n")); 9137 (void) printf(gettext(" 21 Deduplication\n")); 9138 (void) printf(gettext(" 22 Received properties\n")); 9139 (void) printf(gettext(" 23 Slim ZIL\n")); 9140 (void) printf(gettext(" 24 System attributes\n")); 9141 (void) printf(gettext(" 25 Improved scrub stats\n")); 9142 (void) printf(gettext(" 26 Improved snapshot deletion " 9143 "performance\n")); 9144 (void) printf(gettext(" 27 Improved snapshot creation " 9145 "performance\n")); 9146 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 9147 (void) printf(gettext("\nFor more information on a particular " 9148 "version, including supported releases,\n")); 9149 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 9150 } else if (argc == 0 && upgradeall) { 9151 cb.cb_first = B_TRUE; 9152 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 9153 if (ret == 0 && cb.cb_first) { 9154 if (cb.cb_version == SPA_VERSION) { 9155 (void) printf(gettext("All pools are already " 9156 "formatted using feature flags.\n\n")); 9157 (void) printf(gettext("Every feature flags " 9158 "pool already has all supported and " 9159 "requested features enabled.\n")); 9160 } else { 9161 (void) printf(gettext("All pools are already " 9162 "formatted with version %llu or higher.\n"), 9163 (u_longlong_t)cb.cb_version); 9164 } 9165 } 9166 } else if (argc == 0) { 9167 cb.cb_first = B_TRUE; 9168 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 9169 assert(ret == 0); 9170 9171 if (cb.cb_first) { 9172 (void) printf(gettext("All pools are formatted " 9173 "using feature flags.\n\n")); 9174 } else { 9175 (void) printf(gettext("\nUse 'zpool upgrade -v' " 9176 "for a list of available legacy versions.\n")); 9177 } 9178 9179 cb.cb_first = B_TRUE; 9180 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 9181 assert(ret == 0); 9182 9183 if (cb.cb_first) { 9184 (void) printf(gettext("Every feature flags pool has " 9185 "all supported and requested features enabled.\n")); 9186 } else { 9187 (void) printf(gettext("\n")); 9188 } 9189 } else { 9190 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, 9191 upgrade_one, &cb); 9192 } 9193 9194 return (ret); 9195 } 9196 9197 typedef struct hist_cbdata { 9198 boolean_t first; 9199 boolean_t longfmt; 9200 boolean_t internal; 9201 } hist_cbdata_t; 9202 9203 static void 9204 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) 9205 { 9206 nvlist_t **records; 9207 uint_t numrecords; 9208 int i; 9209 9210 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 9211 &records, &numrecords) == 0); 9212 for (i = 0; i < numrecords; i++) { 9213 nvlist_t *rec = records[i]; 9214 char tbuf[64] = ""; 9215 9216 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 9217 time_t tsec; 9218 struct tm t; 9219 9220 tsec = fnvlist_lookup_uint64(records[i], 9221 ZPOOL_HIST_TIME); 9222 (void) localtime_r(&tsec, &t); 9223 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 9224 } 9225 9226 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { 9227 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], 9228 ZPOOL_HIST_ELAPSED_NS); 9229 (void) snprintf(tbuf + strlen(tbuf), 9230 sizeof (tbuf) - strlen(tbuf), 9231 " (%lldms)", (long long)elapsed_ns / 1000 / 1000); 9232 } 9233 9234 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 9235 (void) printf("%s %s", tbuf, 9236 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 9237 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 9238 int ievent = 9239 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 9240 if (!cb->internal) 9241 continue; 9242 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 9243 (void) printf("%s unrecognized record:\n", 9244 tbuf); 9245 dump_nvlist(rec, 4); 9246 continue; 9247 } 9248 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 9249 zfs_history_event_names[ievent], 9250 (longlong_t)fnvlist_lookup_uint64( 9251 rec, ZPOOL_HIST_TXG), 9252 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 9253 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 9254 if (!cb->internal) 9255 continue; 9256 (void) printf("%s [txg:%lld] %s", tbuf, 9257 (longlong_t)fnvlist_lookup_uint64( 9258 rec, ZPOOL_HIST_TXG), 9259 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 9260 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 9261 (void) printf(" %s (%llu)", 9262 fnvlist_lookup_string(rec, 9263 ZPOOL_HIST_DSNAME), 9264 (u_longlong_t)fnvlist_lookup_uint64(rec, 9265 ZPOOL_HIST_DSID)); 9266 } 9267 (void) printf(" %s", fnvlist_lookup_string(rec, 9268 ZPOOL_HIST_INT_STR)); 9269 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 9270 if (!cb->internal) 9271 continue; 9272 (void) printf("%s ioctl %s\n", tbuf, 9273 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 9274 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 9275 (void) printf(" input:\n"); 9276 dump_nvlist(fnvlist_lookup_nvlist(rec, 9277 ZPOOL_HIST_INPUT_NVL), 8); 9278 } 9279 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 9280 (void) printf(" output:\n"); 9281 dump_nvlist(fnvlist_lookup_nvlist(rec, 9282 ZPOOL_HIST_OUTPUT_NVL), 8); 9283 } 9284 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { 9285 (void) printf(" output nvlist omitted; " 9286 "original size: %lldKB\n", 9287 (longlong_t)fnvlist_lookup_int64(rec, 9288 ZPOOL_HIST_OUTPUT_SIZE) / 1024); 9289 } 9290 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { 9291 (void) printf(" errno: %lld\n", 9292 (longlong_t)fnvlist_lookup_int64(rec, 9293 ZPOOL_HIST_ERRNO)); 9294 } 9295 } else { 9296 if (!cb->internal) 9297 continue; 9298 (void) printf("%s unrecognized record:\n", tbuf); 9299 dump_nvlist(rec, 4); 9300 } 9301 9302 if (!cb->longfmt) { 9303 (void) printf("\n"); 9304 continue; 9305 } 9306 (void) printf(" ["); 9307 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 9308 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 9309 struct passwd *pwd = getpwuid(who); 9310 (void) printf("user %d ", (int)who); 9311 if (pwd != NULL) 9312 (void) printf("(%s) ", pwd->pw_name); 9313 } 9314 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 9315 (void) printf("on %s", 9316 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 9317 } 9318 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 9319 (void) printf(":%s", 9320 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 9321 } 9322 9323 (void) printf("]"); 9324 (void) printf("\n"); 9325 } 9326 } 9327 9328 /* 9329 * Print out the command history for a specific pool. 9330 */ 9331 static int 9332 get_history_one(zpool_handle_t *zhp, void *data) 9333 { 9334 nvlist_t *nvhis; 9335 int ret; 9336 hist_cbdata_t *cb = (hist_cbdata_t *)data; 9337 uint64_t off = 0; 9338 boolean_t eof = B_FALSE; 9339 9340 cb->first = B_FALSE; 9341 9342 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 9343 9344 while (!eof) { 9345 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) 9346 return (ret); 9347 9348 print_history_records(nvhis, cb); 9349 nvlist_free(nvhis); 9350 } 9351 (void) printf("\n"); 9352 9353 return (ret); 9354 } 9355 9356 /* 9357 * zpool history <pool> 9358 * 9359 * Displays the history of commands that modified pools. 9360 */ 9361 int 9362 zpool_do_history(int argc, char **argv) 9363 { 9364 hist_cbdata_t cbdata = { 0 }; 9365 int ret; 9366 int c; 9367 9368 cbdata.first = B_TRUE; 9369 /* check options */ 9370 while ((c = getopt(argc, argv, "li")) != -1) { 9371 switch (c) { 9372 case 'l': 9373 cbdata.longfmt = B_TRUE; 9374 break; 9375 case 'i': 9376 cbdata.internal = B_TRUE; 9377 break; 9378 case '?': 9379 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9380 optopt); 9381 usage(B_FALSE); 9382 } 9383 } 9384 argc -= optind; 9385 argv += optind; 9386 9387 ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, get_history_one, 9388 &cbdata); 9389 9390 if (argc == 0 && cbdata.first == B_TRUE) { 9391 (void) fprintf(stderr, gettext("no pools available\n")); 9392 return (0); 9393 } 9394 9395 return (ret); 9396 } 9397 9398 typedef struct ev_opts { 9399 int verbose; 9400 int scripted; 9401 int follow; 9402 int clear; 9403 char poolname[ZFS_MAX_DATASET_NAME_LEN]; 9404 } ev_opts_t; 9405 9406 static void 9407 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) 9408 { 9409 char ctime_str[26], str[32], *ptr; 9410 int64_t *tv; 9411 uint_t n; 9412 9413 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); 9414 memset(str, ' ', 32); 9415 (void) ctime_r((const time_t *)&tv[0], ctime_str); 9416 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ 9417 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ 9418 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ 9419 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ 9420 if (opts->scripted) 9421 (void) printf(gettext("%s\t"), str); 9422 else 9423 (void) printf(gettext("%s "), str); 9424 9425 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); 9426 (void) printf(gettext("%s\n"), ptr); 9427 } 9428 9429 static void 9430 zpool_do_events_nvprint(nvlist_t *nvl, int depth) 9431 { 9432 nvpair_t *nvp; 9433 9434 for (nvp = nvlist_next_nvpair(nvl, NULL); 9435 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { 9436 9437 data_type_t type = nvpair_type(nvp); 9438 const char *name = nvpair_name(nvp); 9439 9440 boolean_t b; 9441 uint8_t i8; 9442 uint16_t i16; 9443 uint32_t i32; 9444 uint64_t i64; 9445 char *str; 9446 nvlist_t *cnv; 9447 9448 printf(gettext("%*s%s = "), depth, "", name); 9449 9450 switch (type) { 9451 case DATA_TYPE_BOOLEAN: 9452 printf(gettext("%s"), "1"); 9453 break; 9454 9455 case DATA_TYPE_BOOLEAN_VALUE: 9456 (void) nvpair_value_boolean_value(nvp, &b); 9457 printf(gettext("%s"), b ? "1" : "0"); 9458 break; 9459 9460 case DATA_TYPE_BYTE: 9461 (void) nvpair_value_byte(nvp, &i8); 9462 printf(gettext("0x%x"), i8); 9463 break; 9464 9465 case DATA_TYPE_INT8: 9466 (void) nvpair_value_int8(nvp, (void *)&i8); 9467 printf(gettext("0x%x"), i8); 9468 break; 9469 9470 case DATA_TYPE_UINT8: 9471 (void) nvpair_value_uint8(nvp, &i8); 9472 printf(gettext("0x%x"), i8); 9473 break; 9474 9475 case DATA_TYPE_INT16: 9476 (void) nvpair_value_int16(nvp, (void *)&i16); 9477 printf(gettext("0x%x"), i16); 9478 break; 9479 9480 case DATA_TYPE_UINT16: 9481 (void) nvpair_value_uint16(nvp, &i16); 9482 printf(gettext("0x%x"), i16); 9483 break; 9484 9485 case DATA_TYPE_INT32: 9486 (void) nvpair_value_int32(nvp, (void *)&i32); 9487 printf(gettext("0x%x"), i32); 9488 break; 9489 9490 case DATA_TYPE_UINT32: 9491 (void) nvpair_value_uint32(nvp, &i32); 9492 printf(gettext("0x%x"), i32); 9493 break; 9494 9495 case DATA_TYPE_INT64: 9496 (void) nvpair_value_int64(nvp, (void *)&i64); 9497 printf(gettext("0x%llx"), (u_longlong_t)i64); 9498 break; 9499 9500 case DATA_TYPE_UINT64: 9501 (void) nvpair_value_uint64(nvp, &i64); 9502 /* 9503 * translate vdev state values to readable 9504 * strings to aide zpool events consumers 9505 */ 9506 if (strcmp(name, 9507 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || 9508 strcmp(name, 9509 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { 9510 printf(gettext("\"%s\" (0x%llx)"), 9511 zpool_state_to_name(i64, VDEV_AUX_NONE), 9512 (u_longlong_t)i64); 9513 } else { 9514 printf(gettext("0x%llx"), (u_longlong_t)i64); 9515 } 9516 break; 9517 9518 case DATA_TYPE_HRTIME: 9519 (void) nvpair_value_hrtime(nvp, (void *)&i64); 9520 printf(gettext("0x%llx"), (u_longlong_t)i64); 9521 break; 9522 9523 case DATA_TYPE_STRING: 9524 (void) nvpair_value_string(nvp, &str); 9525 printf(gettext("\"%s\""), str ? str : "<NULL>"); 9526 break; 9527 9528 case DATA_TYPE_NVLIST: 9529 printf(gettext("(embedded nvlist)\n")); 9530 (void) nvpair_value_nvlist(nvp, &cnv); 9531 zpool_do_events_nvprint(cnv, depth + 8); 9532 printf(gettext("%*s(end %s)"), depth, "", name); 9533 break; 9534 9535 case DATA_TYPE_NVLIST_ARRAY: { 9536 nvlist_t **val; 9537 uint_t i, nelem; 9538 9539 (void) nvpair_value_nvlist_array(nvp, &val, &nelem); 9540 printf(gettext("(%d embedded nvlists)\n"), nelem); 9541 for (i = 0; i < nelem; i++) { 9542 printf(gettext("%*s%s[%d] = %s\n"), 9543 depth, "", name, i, "(embedded nvlist)"); 9544 zpool_do_events_nvprint(val[i], depth + 8); 9545 printf(gettext("%*s(end %s[%i])\n"), 9546 depth, "", name, i); 9547 } 9548 printf(gettext("%*s(end %s)\n"), depth, "", name); 9549 } 9550 break; 9551 9552 case DATA_TYPE_INT8_ARRAY: { 9553 int8_t *val; 9554 uint_t i, nelem; 9555 9556 (void) nvpair_value_int8_array(nvp, &val, &nelem); 9557 for (i = 0; i < nelem; i++) 9558 printf(gettext("0x%x "), val[i]); 9559 9560 break; 9561 } 9562 9563 case DATA_TYPE_UINT8_ARRAY: { 9564 uint8_t *val; 9565 uint_t i, nelem; 9566 9567 (void) nvpair_value_uint8_array(nvp, &val, &nelem); 9568 for (i = 0; i < nelem; i++) 9569 printf(gettext("0x%x "), val[i]); 9570 9571 break; 9572 } 9573 9574 case DATA_TYPE_INT16_ARRAY: { 9575 int16_t *val; 9576 uint_t i, nelem; 9577 9578 (void) nvpair_value_int16_array(nvp, &val, &nelem); 9579 for (i = 0; i < nelem; i++) 9580 printf(gettext("0x%x "), val[i]); 9581 9582 break; 9583 } 9584 9585 case DATA_TYPE_UINT16_ARRAY: { 9586 uint16_t *val; 9587 uint_t i, nelem; 9588 9589 (void) nvpair_value_uint16_array(nvp, &val, &nelem); 9590 for (i = 0; i < nelem; i++) 9591 printf(gettext("0x%x "), val[i]); 9592 9593 break; 9594 } 9595 9596 case DATA_TYPE_INT32_ARRAY: { 9597 int32_t *val; 9598 uint_t i, nelem; 9599 9600 (void) nvpair_value_int32_array(nvp, &val, &nelem); 9601 for (i = 0; i < nelem; i++) 9602 printf(gettext("0x%x "), val[i]); 9603 9604 break; 9605 } 9606 9607 case DATA_TYPE_UINT32_ARRAY: { 9608 uint32_t *val; 9609 uint_t i, nelem; 9610 9611 (void) nvpair_value_uint32_array(nvp, &val, &nelem); 9612 for (i = 0; i < nelem; i++) 9613 printf(gettext("0x%x "), val[i]); 9614 9615 break; 9616 } 9617 9618 case DATA_TYPE_INT64_ARRAY: { 9619 int64_t *val; 9620 uint_t i, nelem; 9621 9622 (void) nvpair_value_int64_array(nvp, &val, &nelem); 9623 for (i = 0; i < nelem; i++) 9624 printf(gettext("0x%llx "), 9625 (u_longlong_t)val[i]); 9626 9627 break; 9628 } 9629 9630 case DATA_TYPE_UINT64_ARRAY: { 9631 uint64_t *val; 9632 uint_t i, nelem; 9633 9634 (void) nvpair_value_uint64_array(nvp, &val, &nelem); 9635 for (i = 0; i < nelem; i++) 9636 printf(gettext("0x%llx "), 9637 (u_longlong_t)val[i]); 9638 9639 break; 9640 } 9641 9642 case DATA_TYPE_STRING_ARRAY: { 9643 char **str; 9644 uint_t i, nelem; 9645 9646 (void) nvpair_value_string_array(nvp, &str, &nelem); 9647 for (i = 0; i < nelem; i++) 9648 printf(gettext("\"%s\" "), 9649 str[i] ? str[i] : "<NULL>"); 9650 9651 break; 9652 } 9653 9654 case DATA_TYPE_BOOLEAN_ARRAY: 9655 case DATA_TYPE_BYTE_ARRAY: 9656 case DATA_TYPE_DOUBLE: 9657 case DATA_TYPE_DONTCARE: 9658 case DATA_TYPE_UNKNOWN: 9659 printf(gettext("<unknown>")); 9660 break; 9661 } 9662 9663 printf(gettext("\n")); 9664 } 9665 } 9666 9667 static int 9668 zpool_do_events_next(ev_opts_t *opts) 9669 { 9670 nvlist_t *nvl; 9671 int zevent_fd, ret, dropped; 9672 char *pool; 9673 9674 zevent_fd = open(ZFS_DEV, O_RDWR); 9675 VERIFY(zevent_fd >= 0); 9676 9677 if (!opts->scripted) 9678 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); 9679 9680 while (1) { 9681 ret = zpool_events_next(g_zfs, &nvl, &dropped, 9682 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); 9683 if (ret || nvl == NULL) 9684 break; 9685 9686 if (dropped > 0) 9687 (void) printf(gettext("dropped %d events\n"), dropped); 9688 9689 if (strlen(opts->poolname) > 0 && 9690 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && 9691 strcmp(opts->poolname, pool) != 0) 9692 continue; 9693 9694 zpool_do_events_short(nvl, opts); 9695 9696 if (opts->verbose) { 9697 zpool_do_events_nvprint(nvl, 8); 9698 printf(gettext("\n")); 9699 } 9700 (void) fflush(stdout); 9701 9702 nvlist_free(nvl); 9703 } 9704 9705 VERIFY(0 == close(zevent_fd)); 9706 9707 return (ret); 9708 } 9709 9710 static int 9711 zpool_do_events_clear(ev_opts_t *opts) 9712 { 9713 int count, ret; 9714 9715 ret = zpool_events_clear(g_zfs, &count); 9716 if (!ret) 9717 (void) printf(gettext("cleared %d events\n"), count); 9718 9719 return (ret); 9720 } 9721 9722 /* 9723 * zpool events [-vHf [pool] | -c] 9724 * 9725 * Displays events logs by ZFS. 9726 */ 9727 int 9728 zpool_do_events(int argc, char **argv) 9729 { 9730 ev_opts_t opts = { 0 }; 9731 int ret; 9732 int c; 9733 9734 /* check options */ 9735 while ((c = getopt(argc, argv, "vHfc")) != -1) { 9736 switch (c) { 9737 case 'v': 9738 opts.verbose = 1; 9739 break; 9740 case 'H': 9741 opts.scripted = 1; 9742 break; 9743 case 'f': 9744 opts.follow = 1; 9745 break; 9746 case 'c': 9747 opts.clear = 1; 9748 break; 9749 case '?': 9750 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9751 optopt); 9752 usage(B_FALSE); 9753 } 9754 } 9755 argc -= optind; 9756 argv += optind; 9757 9758 if (argc > 1) { 9759 (void) fprintf(stderr, gettext("too many arguments\n")); 9760 usage(B_FALSE); 9761 } else if (argc == 1) { 9762 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); 9763 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { 9764 (void) fprintf(stderr, 9765 gettext("invalid pool name '%s'\n"), opts.poolname); 9766 usage(B_FALSE); 9767 } 9768 } 9769 9770 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && 9771 opts.clear) { 9772 (void) fprintf(stderr, 9773 gettext("invalid options combined with -c\n")); 9774 usage(B_FALSE); 9775 } 9776 9777 if (opts.clear) 9778 ret = zpool_do_events_clear(&opts); 9779 else 9780 ret = zpool_do_events_next(&opts); 9781 9782 return (ret); 9783 } 9784 9785 static int 9786 get_callback(zpool_handle_t *zhp, void *data) 9787 { 9788 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 9789 char value[MAXNAMELEN]; 9790 zprop_source_t srctype; 9791 zprop_list_t *pl; 9792 9793 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 9794 9795 /* 9796 * Skip the special fake placeholder. This will also skip 9797 * over the name property when 'all' is specified. 9798 */ 9799 if (pl->pl_prop == ZPOOL_PROP_NAME && 9800 pl == cbp->cb_proplist) 9801 continue; 9802 9803 if (pl->pl_prop == ZPROP_INVAL && 9804 (zpool_prop_feature(pl->pl_user_prop) || 9805 zpool_prop_unsupported(pl->pl_user_prop))) { 9806 srctype = ZPROP_SRC_LOCAL; 9807 9808 if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 9809 value, sizeof (value)) == 0) { 9810 zprop_print_one_property(zpool_get_name(zhp), 9811 cbp, pl->pl_user_prop, value, srctype, 9812 NULL, NULL); 9813 } 9814 } else { 9815 if (zpool_get_prop(zhp, pl->pl_prop, value, 9816 sizeof (value), &srctype, cbp->cb_literal) != 0) 9817 continue; 9818 9819 zprop_print_one_property(zpool_get_name(zhp), cbp, 9820 zpool_prop_to_name(pl->pl_prop), value, srctype, 9821 NULL, NULL); 9822 } 9823 } 9824 return (0); 9825 } 9826 9827 /* 9828 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 9829 * 9830 * -H Scripted mode. Don't display headers, and separate properties 9831 * by a single tab. 9832 * -o List of columns to display. Defaults to 9833 * "name,property,value,source". 9834 * -p Display values in parsable (exact) format. 9835 * 9836 * Get properties of pools in the system. Output space statistics 9837 * for each one as well as other attributes. 9838 */ 9839 int 9840 zpool_do_get(int argc, char **argv) 9841 { 9842 zprop_get_cbdata_t cb = { 0 }; 9843 zprop_list_t fake_name = { 0 }; 9844 int ret; 9845 int c, i; 9846 char *value; 9847 9848 cb.cb_first = B_TRUE; 9849 9850 /* 9851 * Set up default columns and sources. 9852 */ 9853 cb.cb_sources = ZPROP_SRC_ALL; 9854 cb.cb_columns[0] = GET_COL_NAME; 9855 cb.cb_columns[1] = GET_COL_PROPERTY; 9856 cb.cb_columns[2] = GET_COL_VALUE; 9857 cb.cb_columns[3] = GET_COL_SOURCE; 9858 cb.cb_type = ZFS_TYPE_POOL; 9859 9860 /* check options */ 9861 while ((c = getopt(argc, argv, ":Hpo:")) != -1) { 9862 switch (c) { 9863 case 'p': 9864 cb.cb_literal = B_TRUE; 9865 break; 9866 case 'H': 9867 cb.cb_scripted = B_TRUE; 9868 break; 9869 case 'o': 9870 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 9871 i = 0; 9872 while (*optarg != '\0') { 9873 static char *col_subopts[] = 9874 { "name", "property", "value", "source", 9875 "all", NULL }; 9876 9877 if (i == ZFS_GET_NCOLS) { 9878 (void) fprintf(stderr, gettext("too " 9879 "many fields given to -o " 9880 "option\n")); 9881 usage(B_FALSE); 9882 } 9883 9884 switch (getsubopt(&optarg, col_subopts, 9885 &value)) { 9886 case 0: 9887 cb.cb_columns[i++] = GET_COL_NAME; 9888 break; 9889 case 1: 9890 cb.cb_columns[i++] = GET_COL_PROPERTY; 9891 break; 9892 case 2: 9893 cb.cb_columns[i++] = GET_COL_VALUE; 9894 break; 9895 case 3: 9896 cb.cb_columns[i++] = GET_COL_SOURCE; 9897 break; 9898 case 4: 9899 if (i > 0) { 9900 (void) fprintf(stderr, 9901 gettext("\"all\" conflicts " 9902 "with specific fields " 9903 "given to -o option\n")); 9904 usage(B_FALSE); 9905 } 9906 cb.cb_columns[0] = GET_COL_NAME; 9907 cb.cb_columns[1] = GET_COL_PROPERTY; 9908 cb.cb_columns[2] = GET_COL_VALUE; 9909 cb.cb_columns[3] = GET_COL_SOURCE; 9910 i = ZFS_GET_NCOLS; 9911 break; 9912 default: 9913 (void) fprintf(stderr, 9914 gettext("invalid column name " 9915 "'%s'\n"), value); 9916 usage(B_FALSE); 9917 } 9918 } 9919 break; 9920 case '?': 9921 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9922 optopt); 9923 usage(B_FALSE); 9924 } 9925 } 9926 9927 argc -= optind; 9928 argv += optind; 9929 9930 if (argc < 1) { 9931 (void) fprintf(stderr, gettext("missing property " 9932 "argument\n")); 9933 usage(B_FALSE); 9934 } 9935 9936 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, 9937 ZFS_TYPE_POOL) != 0) 9938 usage(B_FALSE); 9939 9940 argc--; 9941 argv++; 9942 9943 if (cb.cb_proplist != NULL) { 9944 fake_name.pl_prop = ZPOOL_PROP_NAME; 9945 fake_name.pl_width = strlen(gettext("NAME")); 9946 fake_name.pl_next = cb.cb_proplist; 9947 cb.cb_proplist = &fake_name; 9948 } 9949 9950 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_literal, 9951 get_callback, &cb); 9952 9953 if (cb.cb_proplist == &fake_name) 9954 zprop_free_list(fake_name.pl_next); 9955 else 9956 zprop_free_list(cb.cb_proplist); 9957 9958 return (ret); 9959 } 9960 9961 typedef struct set_cbdata { 9962 char *cb_propname; 9963 char *cb_value; 9964 boolean_t cb_any_successful; 9965 } set_cbdata_t; 9966 9967 static int 9968 set_callback(zpool_handle_t *zhp, void *data) 9969 { 9970 int error; 9971 set_cbdata_t *cb = (set_cbdata_t *)data; 9972 9973 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 9974 9975 if (!error) 9976 cb->cb_any_successful = B_TRUE; 9977 9978 return (error); 9979 } 9980 9981 int 9982 zpool_do_set(int argc, char **argv) 9983 { 9984 set_cbdata_t cb = { 0 }; 9985 int error; 9986 9987 if (argc > 1 && argv[1][0] == '-') { 9988 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 9989 argv[1][1]); 9990 usage(B_FALSE); 9991 } 9992 9993 if (argc < 2) { 9994 (void) fprintf(stderr, gettext("missing property=value " 9995 "argument\n")); 9996 usage(B_FALSE); 9997 } 9998 9999 if (argc < 3) { 10000 (void) fprintf(stderr, gettext("missing pool name\n")); 10001 usage(B_FALSE); 10002 } 10003 10004 if (argc > 3) { 10005 (void) fprintf(stderr, gettext("too many pool names\n")); 10006 usage(B_FALSE); 10007 } 10008 10009 cb.cb_propname = argv[1]; 10010 cb.cb_value = strchr(cb.cb_propname, '='); 10011 if (cb.cb_value == NULL) { 10012 (void) fprintf(stderr, gettext("missing value in " 10013 "property=value argument\n")); 10014 usage(B_FALSE); 10015 } 10016 10017 *(cb.cb_value) = '\0'; 10018 cb.cb_value++; 10019 10020 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, B_FALSE, 10021 set_callback, &cb); 10022 10023 return (error); 10024 } 10025 10026 /* Add up the total number of bytes left to initialize/trim across all vdevs */ 10027 static uint64_t 10028 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) 10029 { 10030 uint64_t bytes_remaining; 10031 nvlist_t **child; 10032 uint_t c, children; 10033 vdev_stat_t *vs; 10034 10035 assert(activity == ZPOOL_WAIT_INITIALIZE || 10036 activity == ZPOOL_WAIT_TRIM); 10037 10038 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 10039 (uint64_t **)&vs, &c) == 0); 10040 10041 if (activity == ZPOOL_WAIT_INITIALIZE && 10042 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) 10043 bytes_remaining = vs->vs_initialize_bytes_est - 10044 vs->vs_initialize_bytes_done; 10045 else if (activity == ZPOOL_WAIT_TRIM && 10046 vs->vs_trim_state == VDEV_TRIM_ACTIVE) 10047 bytes_remaining = vs->vs_trim_bytes_est - 10048 vs->vs_trim_bytes_done; 10049 else 10050 bytes_remaining = 0; 10051 10052 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 10053 &child, &children) != 0) 10054 children = 0; 10055 10056 for (c = 0; c < children; c++) 10057 bytes_remaining += vdev_activity_remaining(child[c], activity); 10058 10059 return (bytes_remaining); 10060 } 10061 10062 /* Add up the total number of bytes left to rebuild across top-level vdevs */ 10063 static uint64_t 10064 vdev_activity_top_remaining(nvlist_t *nv) 10065 { 10066 uint64_t bytes_remaining = 0; 10067 nvlist_t **child; 10068 uint_t children; 10069 int error; 10070 10071 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 10072 &child, &children) != 0) 10073 children = 0; 10074 10075 for (uint_t c = 0; c < children; c++) { 10076 vdev_rebuild_stat_t *vrs; 10077 uint_t i; 10078 10079 error = nvlist_lookup_uint64_array(child[c], 10080 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); 10081 if (error == 0) { 10082 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 10083 bytes_remaining += (vrs->vrs_bytes_est - 10084 vrs->vrs_bytes_rebuilt); 10085 } 10086 } 10087 } 10088 10089 return (bytes_remaining); 10090 } 10091 10092 /* Whether any vdevs are 'spare' or 'replacing' vdevs */ 10093 static boolean_t 10094 vdev_any_spare_replacing(nvlist_t *nv) 10095 { 10096 nvlist_t **child; 10097 uint_t c, children; 10098 char *vdev_type; 10099 10100 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); 10101 10102 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || 10103 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || 10104 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { 10105 return (B_TRUE); 10106 } 10107 10108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 10109 &child, &children) != 0) 10110 children = 0; 10111 10112 for (c = 0; c < children; c++) { 10113 if (vdev_any_spare_replacing(child[c])) 10114 return (B_TRUE); 10115 } 10116 10117 return (B_FALSE); 10118 } 10119 10120 typedef struct wait_data { 10121 char *wd_poolname; 10122 boolean_t wd_scripted; 10123 boolean_t wd_exact; 10124 boolean_t wd_headers_once; 10125 boolean_t wd_should_exit; 10126 /* Which activities to wait for */ 10127 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; 10128 float wd_interval; 10129 pthread_cond_t wd_cv; 10130 pthread_mutex_t wd_mutex; 10131 } wait_data_t; 10132 10133 /* 10134 * Print to stdout a single line, containing one column for each activity that 10135 * we are waiting for specifying how many bytes of work are left for that 10136 * activity. 10137 */ 10138 static void 10139 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) 10140 { 10141 nvlist_t *config, *nvroot; 10142 uint_t c; 10143 int i; 10144 pool_checkpoint_stat_t *pcs = NULL; 10145 pool_scan_stat_t *pss = NULL; 10146 pool_removal_stat_t *prs = NULL; 10147 char *headers[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE", 10148 "REMOVE", "RESILVER", "SCRUB", "TRIM"}; 10149 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; 10150 10151 /* Calculate the width of each column */ 10152 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10153 /* 10154 * Make sure we have enough space in the col for pretty-printed 10155 * numbers and for the column header, and then leave a couple 10156 * spaces between cols for readability. 10157 */ 10158 col_widths[i] = MAX(strlen(headers[i]), 6) + 2; 10159 } 10160 10161 /* Print header if appropriate */ 10162 int term_height = terminal_height(); 10163 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && 10164 row % (term_height-1) == 0); 10165 if (!wd->wd_scripted && (row == 0 || reprint_header)) { 10166 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10167 if (wd->wd_enabled[i]) 10168 (void) printf("%*s", col_widths[i], headers[i]); 10169 } 10170 (void) printf("\n"); 10171 } 10172 10173 /* Bytes of work remaining in each activity */ 10174 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; 10175 10176 bytes_rem[ZPOOL_WAIT_FREE] = 10177 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); 10178 10179 config = zpool_get_config(zhp, NULL); 10180 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10181 10182 (void) nvlist_lookup_uint64_array(nvroot, 10183 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10184 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 10185 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; 10186 10187 (void) nvlist_lookup_uint64_array(nvroot, 10188 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 10189 if (prs != NULL && prs->prs_state == DSS_SCANNING) 10190 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - 10191 prs->prs_copied; 10192 10193 (void) nvlist_lookup_uint64_array(nvroot, 10194 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); 10195 if (pss != NULL && pss->pss_state == DSS_SCANNING && 10196 pss->pss_pass_scrub_pause == 0) { 10197 int64_t rem = pss->pss_to_examine - pss->pss_issued; 10198 if (pss->pss_func == POOL_SCAN_SCRUB) 10199 bytes_rem[ZPOOL_WAIT_SCRUB] = rem; 10200 else 10201 bytes_rem[ZPOOL_WAIT_RESILVER] = rem; 10202 } else if (check_rebuilding(nvroot, NULL)) { 10203 bytes_rem[ZPOOL_WAIT_RESILVER] = 10204 vdev_activity_top_remaining(nvroot); 10205 } 10206 10207 bytes_rem[ZPOOL_WAIT_INITIALIZE] = 10208 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); 10209 bytes_rem[ZPOOL_WAIT_TRIM] = 10210 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); 10211 10212 /* 10213 * A replace finishes after resilvering finishes, so the amount of work 10214 * left for a replace is the same as for resilvering. 10215 * 10216 * It isn't quite correct to say that if we have any 'spare' or 10217 * 'replacing' vdevs and a resilver is happening, then a replace is in 10218 * progress, like we do here. When a hot spare is used, the faulted vdev 10219 * is not removed after the hot spare is resilvered, so parent 'spare' 10220 * vdev is not removed either. So we could have a 'spare' vdev, but be 10221 * resilvering for a different reason. However, we use it as a heuristic 10222 * because we don't have access to the DTLs, which could tell us whether 10223 * or not we have really finished resilvering a hot spare. 10224 */ 10225 if (vdev_any_spare_replacing(nvroot)) 10226 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; 10227 10228 if (timestamp_fmt != NODATE) 10229 print_timestamp(timestamp_fmt); 10230 10231 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10232 char buf[64]; 10233 if (!wd->wd_enabled[i]) 10234 continue; 10235 10236 if (wd->wd_exact) 10237 (void) snprintf(buf, sizeof (buf), "%" PRIi64, 10238 bytes_rem[i]); 10239 else 10240 zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); 10241 10242 if (wd->wd_scripted) 10243 (void) printf(i == 0 ? "%s" : "\t%s", buf); 10244 else 10245 (void) printf(" %*s", col_widths[i] - 1, buf); 10246 } 10247 (void) printf("\n"); 10248 (void) fflush(stdout); 10249 } 10250 10251 static void * 10252 wait_status_thread(void *arg) 10253 { 10254 wait_data_t *wd = (wait_data_t *)arg; 10255 zpool_handle_t *zhp; 10256 10257 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) 10258 return (void *)(1); 10259 10260 for (int row = 0; ; row++) { 10261 boolean_t missing; 10262 struct timespec timeout; 10263 int ret = 0; 10264 (void) clock_gettime(CLOCK_REALTIME, &timeout); 10265 10266 if (zpool_refresh_stats(zhp, &missing) != 0 || missing || 10267 zpool_props_refresh(zhp) != 0) { 10268 zpool_close(zhp); 10269 return (void *)(uintptr_t)(missing ? 0 : 1); 10270 } 10271 10272 print_wait_status_row(wd, zhp, row); 10273 10274 timeout.tv_sec += floor(wd->wd_interval); 10275 long nanos = timeout.tv_nsec + 10276 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; 10277 if (nanos >= NANOSEC) { 10278 timeout.tv_sec++; 10279 timeout.tv_nsec = nanos - NANOSEC; 10280 } else { 10281 timeout.tv_nsec = nanos; 10282 } 10283 pthread_mutex_lock(&wd->wd_mutex); 10284 if (!wd->wd_should_exit) 10285 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, 10286 &timeout); 10287 pthread_mutex_unlock(&wd->wd_mutex); 10288 if (ret == 0) { 10289 break; /* signaled by main thread */ 10290 } else if (ret != ETIMEDOUT) { 10291 (void) fprintf(stderr, gettext("pthread_cond_timedwait " 10292 "failed: %s\n"), strerror(ret)); 10293 zpool_close(zhp); 10294 return (void *)(uintptr_t)(1); 10295 } 10296 } 10297 10298 zpool_close(zhp); 10299 return (void *)(0); 10300 } 10301 10302 int 10303 zpool_do_wait(int argc, char **argv) 10304 { 10305 boolean_t verbose = B_FALSE; 10306 int c; 10307 char *value; 10308 int i; 10309 unsigned long count; 10310 pthread_t status_thr; 10311 int error = 0; 10312 zpool_handle_t *zhp; 10313 10314 wait_data_t wd; 10315 wd.wd_scripted = B_FALSE; 10316 wd.wd_exact = B_FALSE; 10317 wd.wd_headers_once = B_FALSE; 10318 wd.wd_should_exit = B_FALSE; 10319 10320 pthread_mutex_init(&wd.wd_mutex, NULL); 10321 pthread_cond_init(&wd.wd_cv, NULL); 10322 10323 /* By default, wait for all types of activity. */ 10324 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) 10325 wd.wd_enabled[i] = B_TRUE; 10326 10327 while ((c = getopt(argc, argv, "HpT:t:")) != -1) { 10328 switch (c) { 10329 case 'H': 10330 wd.wd_scripted = B_TRUE; 10331 break; 10332 case 'n': 10333 wd.wd_headers_once = B_TRUE; 10334 break; 10335 case 'p': 10336 wd.wd_exact = B_TRUE; 10337 break; 10338 case 'T': 10339 get_timestamp_arg(*optarg); 10340 break; 10341 case 't': 10342 { 10343 static char *col_subopts[] = { "discard", "free", 10344 "initialize", "replace", "remove", "resilver", 10345 "scrub", "trim", NULL }; 10346 10347 /* Reset activities array */ 10348 bzero(&wd.wd_enabled, sizeof (wd.wd_enabled)); 10349 while (*optarg != '\0') { 10350 int activity = getsubopt(&optarg, col_subopts, 10351 &value); 10352 10353 if (activity < 0) { 10354 (void) fprintf(stderr, 10355 gettext("invalid activity '%s'\n"), 10356 value); 10357 usage(B_FALSE); 10358 } 10359 10360 wd.wd_enabled[activity] = B_TRUE; 10361 } 10362 break; 10363 } 10364 case '?': 10365 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 10366 optopt); 10367 usage(B_FALSE); 10368 } 10369 } 10370 10371 argc -= optind; 10372 argv += optind; 10373 10374 get_interval_count(&argc, argv, &wd.wd_interval, &count); 10375 if (count != 0) { 10376 /* This subcmd only accepts an interval, not a count */ 10377 (void) fprintf(stderr, gettext("too many arguments\n")); 10378 usage(B_FALSE); 10379 } 10380 10381 if (wd.wd_interval != 0) 10382 verbose = B_TRUE; 10383 10384 if (argc < 1) { 10385 (void) fprintf(stderr, gettext("missing 'pool' argument\n")); 10386 usage(B_FALSE); 10387 } 10388 if (argc > 1) { 10389 (void) fprintf(stderr, gettext("too many arguments\n")); 10390 usage(B_FALSE); 10391 } 10392 10393 wd.wd_poolname = argv[0]; 10394 10395 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) 10396 return (1); 10397 10398 if (verbose) { 10399 /* 10400 * We use a separate thread for printing status updates because 10401 * the main thread will call lzc_wait(), which blocks as long 10402 * as an activity is in progress, which can be a long time. 10403 */ 10404 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) 10405 != 0) { 10406 (void) fprintf(stderr, gettext("failed to create status" 10407 "thread: %s\n"), strerror(errno)); 10408 zpool_close(zhp); 10409 return (1); 10410 } 10411 } 10412 10413 /* 10414 * Loop over all activities that we are supposed to wait for until none 10415 * of them are in progress. Note that this means we can end up waiting 10416 * for more activities to complete than just those that were in progress 10417 * when we began waiting; if an activity we are interested in begins 10418 * while we are waiting for another activity, we will wait for both to 10419 * complete before exiting. 10420 */ 10421 for (;;) { 10422 boolean_t missing = B_FALSE; 10423 boolean_t any_waited = B_FALSE; 10424 10425 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 10426 boolean_t waited; 10427 10428 if (!wd.wd_enabled[i]) 10429 continue; 10430 10431 error = zpool_wait_status(zhp, i, &missing, &waited); 10432 if (error != 0 || missing) 10433 break; 10434 10435 any_waited = (any_waited || waited); 10436 } 10437 10438 if (error != 0 || missing || !any_waited) 10439 break; 10440 } 10441 10442 zpool_close(zhp); 10443 10444 if (verbose) { 10445 uintptr_t status; 10446 pthread_mutex_lock(&wd.wd_mutex); 10447 wd.wd_should_exit = B_TRUE; 10448 pthread_cond_signal(&wd.wd_cv); 10449 pthread_mutex_unlock(&wd.wd_mutex); 10450 (void) pthread_join(status_thr, (void *)&status); 10451 if (status != 0) 10452 error = status; 10453 } 10454 10455 pthread_mutex_destroy(&wd.wd_mutex); 10456 pthread_cond_destroy(&wd.wd_cv); 10457 return (error); 10458 } 10459 10460 static int 10461 find_command_idx(char *command, int *idx) 10462 { 10463 int i; 10464 10465 for (i = 0; i < NCOMMAND; i++) { 10466 if (command_table[i].name == NULL) 10467 continue; 10468 10469 if (strcmp(command, command_table[i].name) == 0) { 10470 *idx = i; 10471 return (0); 10472 } 10473 } 10474 return (1); 10475 } 10476 10477 /* 10478 * Display version message 10479 */ 10480 static int 10481 zpool_do_version(int argc, char **argv) 10482 { 10483 if (zfs_version_print() == -1) 10484 return (1); 10485 10486 return (0); 10487 } 10488 10489 /* 10490 * Do zpool_load_compat() and print error message on failure 10491 */ 10492 static zpool_compat_status_t 10493 zpool_do_load_compat(const char *compat, boolean_t *list) 10494 { 10495 char badword[ZFS_MAXPROPLEN]; 10496 char badfile[MAXPATHLEN]; 10497 zpool_compat_status_t ret; 10498 10499 switch (ret = zpool_load_compat(compat, list, badword, badfile)) { 10500 case ZPOOL_COMPATIBILITY_OK: 10501 break; 10502 case ZPOOL_COMPATIBILITY_READERR: 10503 (void) fprintf(stderr, gettext("error reading compatibility " 10504 "file '%s'\n"), badfile); 10505 break; 10506 case ZPOOL_COMPATIBILITY_BADFILE: 10507 (void) fprintf(stderr, gettext("compatibility file '%s' " 10508 "too large or not newline-terminated\n"), badfile); 10509 break; 10510 case ZPOOL_COMPATIBILITY_BADWORD: 10511 (void) fprintf(stderr, gettext("unknown feature '%s' in " 10512 "compatibility file '%s'\n"), badword, badfile); 10513 break; 10514 case ZPOOL_COMPATIBILITY_NOFILES: 10515 (void) fprintf(stderr, gettext("no compatibility files " 10516 "specified\n")); 10517 break; 10518 } 10519 return (ret); 10520 } 10521 10522 int 10523 main(int argc, char **argv) 10524 { 10525 int ret = 0; 10526 int i = 0; 10527 char *cmdname; 10528 char **newargv; 10529 10530 (void) setlocale(LC_ALL, ""); 10531 (void) setlocale(LC_NUMERIC, "C"); 10532 (void) textdomain(TEXT_DOMAIN); 10533 srand(time(NULL)); 10534 10535 opterr = 0; 10536 10537 /* 10538 * Make sure the user has specified some command. 10539 */ 10540 if (argc < 2) { 10541 (void) fprintf(stderr, gettext("missing command\n")); 10542 usage(B_FALSE); 10543 } 10544 10545 cmdname = argv[1]; 10546 10547 /* 10548 * Special case '-?' 10549 */ 10550 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) 10551 usage(B_TRUE); 10552 10553 /* 10554 * Special case '-V|--version' 10555 */ 10556 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) 10557 return (zpool_do_version(argc, argv)); 10558 10559 if ((g_zfs = libzfs_init()) == NULL) { 10560 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); 10561 return (1); 10562 } 10563 10564 libzfs_print_on_error(g_zfs, B_TRUE); 10565 10566 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 10567 10568 /* 10569 * Many commands modify input strings for string parsing reasons. 10570 * We create a copy to protect the original argv. 10571 */ 10572 newargv = malloc((argc + 1) * sizeof (newargv[0])); 10573 for (i = 0; i < argc; i++) 10574 newargv[i] = strdup(argv[i]); 10575 newargv[argc] = NULL; 10576 10577 /* 10578 * Run the appropriate command. 10579 */ 10580 if (find_command_idx(cmdname, &i) == 0) { 10581 current_command = &command_table[i]; 10582 ret = command_table[i].func(argc - 1, newargv + 1); 10583 } else if (strchr(cmdname, '=')) { 10584 verify(find_command_idx("set", &i) == 0); 10585 current_command = &command_table[i]; 10586 ret = command_table[i].func(argc, newargv); 10587 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 10588 /* 10589 * 'freeze' is a vile debugging abomination, so we treat 10590 * it as such. 10591 */ 10592 zfs_cmd_t zc = {"\0"}; 10593 10594 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 10595 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); 10596 if (ret != 0) { 10597 (void) fprintf(stderr, 10598 gettext("failed to freeze pool: %d\n"), errno); 10599 ret = 1; 10600 } 10601 10602 log_history = 0; 10603 } else { 10604 (void) fprintf(stderr, gettext("unrecognized " 10605 "command '%s'\n"), cmdname); 10606 usage(B_FALSE); 10607 ret = 1; 10608 } 10609 10610 for (i = 0; i < argc; i++) 10611 free(newargv[i]); 10612 free(newargv); 10613 10614 if (ret == 0 && log_history) 10615 (void) zpool_log_history(g_zfs, history_str); 10616 10617 libzfs_fini(g_zfs); 10618 10619 /* 10620 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 10621 * for the purposes of running ::findleaks. 10622 */ 10623 if (getenv("ZFS_ABORT") != NULL) { 10624 (void) printf("dumping core by request\n"); 10625 abort(); 10626 } 10627 10628 return (ret); 10629 } 10630