1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 26 * Copyright (c) 2011, 2024 by Delphix. All rights reserved. 27 * Copyright (c) 2012 by Frederik Wessels. All rights reserved. 28 * Copyright (c) 2012 by Cyril Plisko. All rights reserved. 29 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved. 30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. 31 * Copyright (c) 2017 Datto Inc. 32 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. 33 * Copyright (c) 2017, Intel Corporation. 34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com> 35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> 36 * Copyright (c) 2021, 2023, Klara Inc. 37 * Copyright (c) 2021, 2025 Hewlett Packard Enterprise Development LP. 38 */ 39 40 #include <assert.h> 41 #include <ctype.h> 42 #include <dirent.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <getopt.h> 46 #include <libgen.h> 47 #include <libintl.h> 48 #include <libuutil.h> 49 #include <locale.h> 50 #include <pthread.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <thread_pool.h> 55 #include <time.h> 56 #include <unistd.h> 57 #include <pwd.h> 58 #include <zone.h> 59 #include <sys/wait.h> 60 #include <zfs_prop.h> 61 #include <sys/fs/zfs.h> 62 #include <sys/stat.h> 63 #include <sys/systeminfo.h> 64 #include <sys/fm/fs/zfs.h> 65 #include <sys/fm/util.h> 66 #include <sys/fm/protocol.h> 67 #include <sys/zfs_ioctl.h> 68 #include <sys/mount.h> 69 #include <sys/sysmacros.h> 70 #include <string.h> 71 #include <math.h> 72 73 #include <libzfs.h> 74 #include <libzutil.h> 75 76 #include "zpool_util.h" 77 #include "zfs_comutil.h" 78 #include "zfeature_common.h" 79 #include "zfs_valstr.h" 80 81 #include "statcommon.h" 82 83 libzfs_handle_t *g_zfs; 84 85 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */ 86 87 static int zpool_do_create(int, char **); 88 static int zpool_do_destroy(int, char **); 89 90 static int zpool_do_add(int, char **); 91 static int zpool_do_remove(int, char **); 92 static int zpool_do_labelclear(int, char **); 93 94 static int zpool_do_checkpoint(int, char **); 95 static int zpool_do_prefetch(int, char **); 96 97 static int zpool_do_list(int, char **); 98 static int zpool_do_iostat(int, char **); 99 static int zpool_do_status(int, char **); 100 101 static int zpool_do_online(int, char **); 102 static int zpool_do_offline(int, char **); 103 static int zpool_do_clear(int, char **); 104 static int zpool_do_reopen(int, char **); 105 106 static int zpool_do_reguid(int, char **); 107 108 static int zpool_do_attach(int, char **); 109 static int zpool_do_detach(int, char **); 110 static int zpool_do_replace(int, char **); 111 static int zpool_do_split(int, char **); 112 113 static int zpool_do_initialize(int, char **); 114 static int zpool_do_scrub(int, char **); 115 static int zpool_do_resilver(int, char **); 116 static int zpool_do_trim(int, char **); 117 118 static int zpool_do_import(int, char **); 119 static int zpool_do_export(int, char **); 120 121 static int zpool_do_upgrade(int, char **); 122 123 static int zpool_do_history(int, char **); 124 static int zpool_do_events(int, char **); 125 126 static int zpool_do_get(int, char **); 127 static int zpool_do_set(int, char **); 128 129 static int zpool_do_sync(int, char **); 130 131 static int zpool_do_version(int, char **); 132 133 static int zpool_do_wait(int, char **); 134 135 static int zpool_do_ddt_prune(int, char **); 136 137 static int zpool_do_help(int argc, char **argv); 138 139 static zpool_compat_status_t zpool_do_load_compat( 140 const char *, boolean_t *); 141 142 enum zpool_options { 143 ZPOOL_OPTION_POWER = 1024, 144 ZPOOL_OPTION_ALLOW_INUSE, 145 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH, 146 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH, 147 ZPOOL_OPTION_POOL_KEY_GUID, 148 ZPOOL_OPTION_JSON_NUMS_AS_INT, 149 ZPOOL_OPTION_JSON_FLAT_VDEVS 150 }; 151 152 /* 153 * These libumem hooks provide a reasonable set of defaults for the allocator's 154 * debugging facilities. 155 */ 156 157 #ifdef DEBUG 158 const char * 159 _umem_debug_init(void) 160 { 161 return ("default,verbose"); /* $UMEM_DEBUG setting */ 162 } 163 164 const char * 165 _umem_logging_init(void) 166 { 167 return ("fail,contents"); /* $UMEM_LOGGING setting */ 168 } 169 #endif 170 171 typedef enum { 172 HELP_ADD, 173 HELP_ATTACH, 174 HELP_CLEAR, 175 HELP_CREATE, 176 HELP_CHECKPOINT, 177 HELP_DDT_PRUNE, 178 HELP_DESTROY, 179 HELP_DETACH, 180 HELP_EXPORT, 181 HELP_HISTORY, 182 HELP_IMPORT, 183 HELP_IOSTAT, 184 HELP_LABELCLEAR, 185 HELP_LIST, 186 HELP_OFFLINE, 187 HELP_ONLINE, 188 HELP_PREFETCH, 189 HELP_REPLACE, 190 HELP_REMOVE, 191 HELP_INITIALIZE, 192 HELP_SCRUB, 193 HELP_RESILVER, 194 HELP_TRIM, 195 HELP_STATUS, 196 HELP_UPGRADE, 197 HELP_EVENTS, 198 HELP_GET, 199 HELP_SET, 200 HELP_SPLIT, 201 HELP_SYNC, 202 HELP_REGUID, 203 HELP_REOPEN, 204 HELP_VERSION, 205 HELP_WAIT 206 } zpool_help_t; 207 208 209 /* 210 * Flags for stats to display with "zpool iostats" 211 */ 212 enum iostat_type { 213 IOS_DEFAULT = 0, 214 IOS_LATENCY = 1, 215 IOS_QUEUES = 2, 216 IOS_L_HISTO = 3, 217 IOS_RQ_HISTO = 4, 218 IOS_COUNT, /* always last element */ 219 }; 220 221 /* iostat_type entries as bitmasks */ 222 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT) 223 #define IOS_LATENCY_M (1ULL << IOS_LATENCY) 224 #define IOS_QUEUES_M (1ULL << IOS_QUEUES) 225 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO) 226 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO) 227 228 /* Mask of all the histo bits */ 229 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M) 230 231 /* 232 * Lookup table for iostat flags to nvlist names. Basically a list 233 * of all the nvlists a flag requires. Also specifies the order in 234 * which data gets printed in zpool iostat. 235 */ 236 static const char *vsx_type_to_nvlist[IOS_COUNT][15] = { 237 [IOS_L_HISTO] = { 238 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 239 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 240 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 241 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 242 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, 243 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, 244 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, 245 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, 246 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, 247 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 248 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, 249 NULL}, 250 [IOS_LATENCY] = { 251 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 252 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 253 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 254 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 255 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 256 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, 257 NULL}, 258 [IOS_QUEUES] = { 259 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, 260 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, 261 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, 262 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, 263 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, 264 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, 265 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, 266 NULL}, 267 [IOS_RQ_HISTO] = { 268 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO, 269 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO, 270 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO, 271 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO, 272 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO, 273 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO, 274 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO, 275 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO, 276 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO, 277 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO, 278 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO, 279 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO, 280 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO, 281 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO, 282 NULL}, 283 }; 284 285 static const char *pool_scan_func_str[] = { 286 "NONE", 287 "SCRUB", 288 "RESILVER", 289 "ERRORSCRUB" 290 }; 291 292 static const char *pool_scan_state_str[] = { 293 "NONE", 294 "SCANNING", 295 "FINISHED", 296 "CANCELED", 297 "ERRORSCRUBBING" 298 }; 299 300 static const char *vdev_rebuild_state_str[] = { 301 "NONE", 302 "ACTIVE", 303 "CANCELED", 304 "COMPLETE" 305 }; 306 307 static const char *checkpoint_state_str[] = { 308 "NONE", 309 "EXISTS", 310 "DISCARDING" 311 }; 312 313 static const char *vdev_state_str[] = { 314 "UNKNOWN", 315 "CLOSED", 316 "OFFLINE", 317 "REMOVED", 318 "CANT_OPEN", 319 "FAULTED", 320 "DEGRADED", 321 "ONLINE" 322 }; 323 324 static const char *vdev_aux_str[] = { 325 "NONE", 326 "OPEN_FAILED", 327 "CORRUPT_DATA", 328 "NO_REPLICAS", 329 "BAD_GUID_SUM", 330 "TOO_SMALL", 331 "BAD_LABEL", 332 "VERSION_NEWER", 333 "VERSION_OLDER", 334 "UNSUP_FEAT", 335 "SPARED", 336 "ERR_EXCEEDED", 337 "IO_FAILURE", 338 "BAD_LOG", 339 "EXTERNAL", 340 "SPLIT_POOL", 341 "BAD_ASHIFT", 342 "EXTERNAL_PERSIST", 343 "ACTIVE", 344 "CHILDREN_OFFLINE", 345 "ASHIFT_TOO_BIG" 346 }; 347 348 static const char *vdev_init_state_str[] = { 349 "NONE", 350 "ACTIVE", 351 "CANCELED", 352 "SUSPENDED", 353 "COMPLETE" 354 }; 355 356 static const char *vdev_trim_state_str[] = { 357 "NONE", 358 "ACTIVE", 359 "CANCELED", 360 "SUSPENDED", 361 "COMPLETE" 362 }; 363 364 #define ZFS_NICE_TIMESTAMP 100 365 366 /* 367 * Given a cb->cb_flags with a histogram bit set, return the iostat_type. 368 * Right now, only one histo bit is ever set at one time, so we can 369 * just do a highbit64(a) 370 */ 371 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1) 372 373 typedef struct zpool_command { 374 const char *name; 375 int (*func)(int, char **); 376 zpool_help_t usage; 377 } zpool_command_t; 378 379 /* 380 * Master command table. Each ZFS command has a name, associated function, and 381 * usage message. The usage messages need to be internationalized, so we have 382 * to have a function to return the usage message based on a command index. 383 * 384 * These commands are organized according to how they are displayed in the usage 385 * message. An empty command (one with a NULL name) indicates an empty line in 386 * the generic usage message. 387 */ 388 static zpool_command_t command_table[] = { 389 { "version", zpool_do_version, HELP_VERSION }, 390 { NULL }, 391 { "create", zpool_do_create, HELP_CREATE }, 392 { "destroy", zpool_do_destroy, HELP_DESTROY }, 393 { NULL }, 394 { "add", zpool_do_add, HELP_ADD }, 395 { "remove", zpool_do_remove, HELP_REMOVE }, 396 { NULL }, 397 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, 398 { NULL }, 399 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT }, 400 { "prefetch", zpool_do_prefetch, HELP_PREFETCH }, 401 { NULL }, 402 { "list", zpool_do_list, HELP_LIST }, 403 { "iostat", zpool_do_iostat, HELP_IOSTAT }, 404 { "status", zpool_do_status, HELP_STATUS }, 405 { NULL }, 406 { "online", zpool_do_online, HELP_ONLINE }, 407 { "offline", zpool_do_offline, HELP_OFFLINE }, 408 { "clear", zpool_do_clear, HELP_CLEAR }, 409 { "reopen", zpool_do_reopen, HELP_REOPEN }, 410 { NULL }, 411 { "attach", zpool_do_attach, HELP_ATTACH }, 412 { "detach", zpool_do_detach, HELP_DETACH }, 413 { "replace", zpool_do_replace, HELP_REPLACE }, 414 { "split", zpool_do_split, HELP_SPLIT }, 415 { NULL }, 416 { "initialize", zpool_do_initialize, HELP_INITIALIZE }, 417 { "resilver", zpool_do_resilver, HELP_RESILVER }, 418 { "scrub", zpool_do_scrub, HELP_SCRUB }, 419 { "trim", zpool_do_trim, HELP_TRIM }, 420 { NULL }, 421 { "import", zpool_do_import, HELP_IMPORT }, 422 { "export", zpool_do_export, HELP_EXPORT }, 423 { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 424 { "reguid", zpool_do_reguid, HELP_REGUID }, 425 { NULL }, 426 { "history", zpool_do_history, HELP_HISTORY }, 427 { "events", zpool_do_events, HELP_EVENTS }, 428 { NULL }, 429 { "get", zpool_do_get, HELP_GET }, 430 { "set", zpool_do_set, HELP_SET }, 431 { "sync", zpool_do_sync, HELP_SYNC }, 432 { NULL }, 433 { "wait", zpool_do_wait, HELP_WAIT }, 434 { NULL }, 435 { "ddtprune", zpool_do_ddt_prune, HELP_DDT_PRUNE }, 436 }; 437 438 #define NCOMMAND (ARRAY_SIZE(command_table)) 439 440 #define VDEV_ALLOC_CLASS_LOGS "logs" 441 442 #define MAX_CMD_LEN 256 443 444 static zpool_command_t *current_command; 445 static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV); 446 static char history_str[HIS_MAX_RECORD_LEN]; 447 static boolean_t log_history = B_TRUE; 448 static uint_t timestamp_fmt = NODATE; 449 450 static const char * 451 get_usage(zpool_help_t idx) 452 { 453 switch (idx) { 454 case HELP_ADD: 455 return (gettext("\tadd [-afgLnP] [-o property=value] " 456 "<pool> <vdev> ...\n")); 457 case HELP_ATTACH: 458 return (gettext("\tattach [-fsw] [-o property=value] " 459 "<pool> <vdev> <new-device>\n")); 460 case HELP_CLEAR: 461 return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n")); 462 case HELP_CREATE: 463 return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 464 "\t [-O file-system-property=value] ... \n" 465 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 466 case HELP_CHECKPOINT: 467 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n")); 468 case HELP_DESTROY: 469 return (gettext("\tdestroy [-f] <pool>\n")); 470 case HELP_DETACH: 471 return (gettext("\tdetach <pool> <device>\n")); 472 case HELP_EXPORT: 473 return (gettext("\texport [-af] <pool> ...\n")); 474 case HELP_HISTORY: 475 return (gettext("\thistory [-il] [<pool>] ...\n")); 476 case HELP_IMPORT: 477 return (gettext("\timport [-d dir] [-D]\n" 478 "\timport [-o mntopts] [-o property=value] ... \n" 479 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " 480 "[-R root] [-F [-n]] -a\n" 481 "\timport [-o mntopts] [-o property=value] ... \n" 482 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " 483 "[-R root] [-F [-n]]\n" 484 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n")); 485 case HELP_IOSTAT: 486 return (gettext("\tiostat [[[-c [script1,script2,...]" 487 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n" 488 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]" 489 " [[-n] interval [count]]\n")); 490 case HELP_LABELCLEAR: 491 return (gettext("\tlabelclear [-f] <vdev>\n")); 492 case HELP_LIST: 493 return (gettext("\tlist [-gHLpPv] [-o property[,...]] [-j " 494 "[--json-int, --json-pool-key-guid]] ...\n" 495 "\t [-T d|u] [pool] [interval [count]]\n")); 496 case HELP_PREFETCH: 497 return (gettext("\tprefetch -t <type> [<type opts>] <pool>\n" 498 "\t -t ddt <pool>\n")); 499 case HELP_OFFLINE: 500 return (gettext("\toffline [--power]|[[-f][-t]] <pool> " 501 "<device> ...\n")); 502 case HELP_ONLINE: 503 return (gettext("\tonline [--power][-e] <pool> <device> " 504 "...\n")); 505 case HELP_REPLACE: 506 return (gettext("\treplace [-fsw] [-o property=value] " 507 "<pool> <device> [new-device]\n")); 508 case HELP_REMOVE: 509 return (gettext("\tremove [-npsw] <pool> <device> ...\n")); 510 case HELP_REOPEN: 511 return (gettext("\treopen [-n] <pool>\n")); 512 case HELP_INITIALIZE: 513 return (gettext("\tinitialize [-c | -s | -u] [-w] <-a | <pool> " 514 "[<device> ...]>\n")); 515 case HELP_SCRUB: 516 return (gettext("\tscrub [-e | -s | -p | -C | -E | -S] [-w] " 517 "<-a | <pool> [<pool> ...]>\n")); 518 case HELP_RESILVER: 519 return (gettext("\tresilver <pool> ...\n")); 520 case HELP_TRIM: 521 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] " 522 "<-a | <pool> [<device> ...]>\n")); 523 case HELP_STATUS: 524 return (gettext("\tstatus [-DdegiLPpstvx] " 525 "[-c script1[,script2,...]] ...\n" 526 "\t [-j|--json [--json-flat-vdevs] [--json-int] " 527 "[--json-pool-key-guid]] ...\n" 528 "\t [-T d|u] [--power] [pool] [interval [count]]\n")); 529 case HELP_UPGRADE: 530 return (gettext("\tupgrade\n" 531 "\tupgrade -v\n" 532 "\tupgrade [-V version] <-a | pool ...>\n")); 533 case HELP_EVENTS: 534 return (gettext("\tevents [-vHf [pool] | -c]\n")); 535 case HELP_GET: 536 return (gettext("\tget [-Hp] [-j [--json-int, " 537 "--json-pool-key-guid]] ...\n" 538 "\t [-o \"all\" | field[,...]] " 539 "<\"all\" | property[,...]> <pool> ...\n")); 540 case HELP_SET: 541 return (gettext("\tset <property=value> <pool>\n" 542 "\tset <vdev_property=value> <pool> <vdev>\n")); 543 case HELP_SPLIT: 544 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n" 545 "\t [-o property=value] <pool> <newpool> " 546 "[<device> ...]\n")); 547 case HELP_REGUID: 548 return (gettext("\treguid [-g guid] <pool>\n")); 549 case HELP_SYNC: 550 return (gettext("\tsync [pool] ...\n")); 551 case HELP_VERSION: 552 return (gettext("\tversion [-j]\n")); 553 case HELP_WAIT: 554 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] " 555 "<pool> [interval]\n")); 556 case HELP_DDT_PRUNE: 557 return (gettext("\tddtprune -d|-p <amount> <pool>\n")); 558 default: 559 __builtin_unreachable(); 560 } 561 } 562 563 /* 564 * Callback routine that will print out a pool property value. 565 */ 566 static int 567 print_pool_prop_cb(int prop, void *cb) 568 { 569 FILE *fp = cb; 570 571 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop)); 572 573 if (zpool_prop_readonly(prop)) 574 (void) fprintf(fp, " NO "); 575 else 576 (void) fprintf(fp, " YES "); 577 578 if (zpool_prop_values(prop) == NULL) 579 (void) fprintf(fp, "-\n"); 580 else 581 (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 582 583 return (ZPROP_CONT); 584 } 585 586 /* 587 * Callback routine that will print out a vdev property value. 588 */ 589 static int 590 print_vdev_prop_cb(int prop, void *cb) 591 { 592 FILE *fp = cb; 593 594 (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop)); 595 596 if (vdev_prop_readonly(prop)) 597 (void) fprintf(fp, " NO "); 598 else 599 (void) fprintf(fp, " YES "); 600 601 if (vdev_prop_values(prop) == NULL) 602 (void) fprintf(fp, "-\n"); 603 else 604 (void) fprintf(fp, "%s\n", vdev_prop_values(prop)); 605 606 return (ZPROP_CONT); 607 } 608 609 /* 610 * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like 611 * '/dev/disk/by-vdev/L5'. 612 */ 613 static const char * 614 vdev_name_to_path(zpool_handle_t *zhp, char *vdev) 615 { 616 nvlist_t *vdev_nv = zpool_find_vdev(zhp, vdev, NULL, NULL, NULL); 617 if (vdev_nv == NULL) { 618 return (NULL); 619 } 620 return (fnvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH)); 621 } 622 623 static int 624 zpool_power_on(zpool_handle_t *zhp, char *vdev) 625 { 626 return (zpool_power(zhp, vdev, B_TRUE)); 627 } 628 629 static int 630 zpool_power_on_and_disk_wait(zpool_handle_t *zhp, char *vdev) 631 { 632 int rc; 633 634 rc = zpool_power_on(zhp, vdev); 635 if (rc != 0) 636 return (rc); 637 638 zpool_disk_wait(vdev_name_to_path(zhp, vdev)); 639 640 return (0); 641 } 642 643 static int 644 zpool_power_on_pool_and_wait_for_devices(zpool_handle_t *zhp) 645 { 646 nvlist_t *nv; 647 const char *path = NULL; 648 int rc; 649 650 /* Power up all the devices first */ 651 FOR_EACH_REAL_LEAF_VDEV(zhp, nv) { 652 path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH); 653 if (path != NULL) { 654 rc = zpool_power_on(zhp, (char *)path); 655 if (rc != 0) { 656 return (rc); 657 } 658 } 659 } 660 661 /* 662 * Wait for their devices to show up. Since we powered them on 663 * at roughly the same time, they should all come online around 664 * the same time. 665 */ 666 FOR_EACH_REAL_LEAF_VDEV(zhp, nv) { 667 path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH); 668 zpool_disk_wait(path); 669 } 670 671 return (0); 672 } 673 674 static int 675 zpool_power_off(zpool_handle_t *zhp, char *vdev) 676 { 677 return (zpool_power(zhp, vdev, B_FALSE)); 678 } 679 680 /* 681 * Display usage message. If we're inside a command, display only the usage for 682 * that command. Otherwise, iterate over the entire command table and display 683 * a complete usage message. 684 */ 685 static __attribute__((noreturn)) void 686 usage(boolean_t requested) 687 { 688 FILE *fp = requested ? stdout : stderr; 689 690 if (current_command == NULL) { 691 int i; 692 693 (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 694 (void) fprintf(fp, 695 gettext("where 'command' is one of the following:\n\n")); 696 697 for (i = 0; i < NCOMMAND; i++) { 698 if (command_table[i].name == NULL) 699 (void) fprintf(fp, "\n"); 700 else 701 (void) fprintf(fp, "%s", 702 get_usage(command_table[i].usage)); 703 } 704 705 (void) fprintf(fp, 706 gettext("\nFor further help on a command or topic, " 707 "run: %s\n"), "zpool help [<topic>]"); 708 } else { 709 (void) fprintf(fp, gettext("usage:\n")); 710 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 711 } 712 713 if (current_command != NULL && 714 current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) && 715 ((strcmp(current_command->name, "set") == 0) || 716 (strcmp(current_command->name, "get") == 0) || 717 (strcmp(current_command->name, "list") == 0))) { 718 719 (void) fprintf(fp, "%s", 720 gettext("\nthe following properties are supported:\n")); 721 722 (void) fprintf(fp, "\n\t%-19s %s %s\n\n", 723 "PROPERTY", "EDIT", "VALUES"); 724 725 /* Iterate over all properties */ 726 if (current_prop_type == ZFS_TYPE_POOL) { 727 (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE, 728 B_TRUE, current_prop_type); 729 730 (void) fprintf(fp, "\t%-19s ", "feature@..."); 731 (void) fprintf(fp, "YES " 732 "disabled | enabled | active\n"); 733 734 (void) fprintf(fp, gettext("\nThe feature@ properties " 735 "must be appended with a feature name.\n" 736 "See zpool-features(7).\n")); 737 } else if (current_prop_type == ZFS_TYPE_VDEV) { 738 (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE, 739 B_TRUE, current_prop_type); 740 } 741 } 742 743 /* 744 * See comments at end of main(). 745 */ 746 if (getenv("ZFS_ABORT") != NULL) { 747 (void) printf("dumping core by request\n"); 748 abort(); 749 } 750 751 exit(requested ? 0 : 2); 752 } 753 754 /* 755 * zpool initialize [-c | -s | -u] [-w] <-a | pool> [<vdev> ...] 756 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool 757 * if none specified. 758 * 759 * -a Use all pools. 760 * -c Cancel. Ends active initializing. 761 * -s Suspend. Initializing can then be restarted with no flags. 762 * -u Uninitialize. Clears initialization state. 763 * -w Wait. Blocks until initializing has completed. 764 */ 765 int 766 zpool_do_initialize(int argc, char **argv) 767 { 768 int c; 769 char *poolname; 770 zpool_handle_t *zhp; 771 int err = 0; 772 boolean_t wait = B_FALSE; 773 boolean_t initialize_all = B_FALSE; 774 775 struct option long_options[] = { 776 {"cancel", no_argument, NULL, 'c'}, 777 {"suspend", no_argument, NULL, 's'}, 778 {"uninit", no_argument, NULL, 'u'}, 779 {"wait", no_argument, NULL, 'w'}, 780 {"all", no_argument, NULL, 'a'}, 781 {0, 0, 0, 0} 782 }; 783 784 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START; 785 while ((c = getopt_long(argc, argv, "acsuw", long_options, 786 NULL)) != -1) { 787 switch (c) { 788 case 'a': 789 initialize_all = B_TRUE; 790 break; 791 case 'c': 792 if (cmd_type != POOL_INITIALIZE_START && 793 cmd_type != POOL_INITIALIZE_CANCEL) { 794 (void) fprintf(stderr, gettext("-c cannot be " 795 "combined with other options\n")); 796 usage(B_FALSE); 797 } 798 cmd_type = POOL_INITIALIZE_CANCEL; 799 break; 800 case 's': 801 if (cmd_type != POOL_INITIALIZE_START && 802 cmd_type != POOL_INITIALIZE_SUSPEND) { 803 (void) fprintf(stderr, gettext("-s cannot be " 804 "combined with other options\n")); 805 usage(B_FALSE); 806 } 807 cmd_type = POOL_INITIALIZE_SUSPEND; 808 break; 809 case 'u': 810 if (cmd_type != POOL_INITIALIZE_START && 811 cmd_type != POOL_INITIALIZE_UNINIT) { 812 (void) fprintf(stderr, gettext("-u cannot be " 813 "combined with other options\n")); 814 usage(B_FALSE); 815 } 816 cmd_type = POOL_INITIALIZE_UNINIT; 817 break; 818 case 'w': 819 wait = B_TRUE; 820 break; 821 case '?': 822 if (optopt != 0) { 823 (void) fprintf(stderr, 824 gettext("invalid option '%c'\n"), optopt); 825 } else { 826 (void) fprintf(stderr, 827 gettext("invalid option '%s'\n"), 828 argv[optind - 1]); 829 } 830 usage(B_FALSE); 831 } 832 } 833 834 argc -= optind; 835 argv += optind; 836 837 initialize_cbdata_t cbdata = { 838 .wait = wait, 839 .cmd_type = cmd_type 840 }; 841 842 if (initialize_all && argc > 0) { 843 (void) fprintf(stderr, gettext("-a cannot be combined with " 844 "individual pools or vdevs\n")); 845 usage(B_FALSE); 846 } 847 848 if (argc < 1 && !initialize_all) { 849 (void) fprintf(stderr, gettext("missing pool name argument\n")); 850 usage(B_FALSE); 851 return (-1); 852 } 853 854 if (wait && (cmd_type != POOL_INITIALIZE_START)) { 855 (void) fprintf(stderr, gettext("-w cannot be used with -c, -s" 856 "or -u\n")); 857 usage(B_FALSE); 858 } 859 860 if (argc == 0 && initialize_all) { 861 /* Initilize each pool recursively */ 862 err = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 863 B_FALSE, zpool_initialize_one, &cbdata); 864 return (err); 865 } else if (argc == 1) { 866 /* no individual leaf vdevs specified, initialize the pool */ 867 poolname = argv[0]; 868 zhp = zpool_open(g_zfs, poolname); 869 if (zhp == NULL) 870 return (-1); 871 err = zpool_initialize_one(zhp, &cbdata); 872 } else { 873 /* individual leaf vdevs specified, initialize them */ 874 poolname = argv[0]; 875 zhp = zpool_open(g_zfs, poolname); 876 if (zhp == NULL) 877 return (-1); 878 nvlist_t *vdevs = fnvlist_alloc(); 879 for (int i = 1; i < argc; i++) { 880 fnvlist_add_boolean(vdevs, argv[i]); 881 } 882 if (wait) 883 err = zpool_initialize_wait(zhp, cmd_type, vdevs); 884 else 885 err = zpool_initialize(zhp, cmd_type, vdevs); 886 fnvlist_free(vdevs); 887 } 888 889 zpool_close(zhp); 890 891 return (err); 892 } 893 894 /* 895 * print a pool vdev config for dry runs 896 */ 897 static void 898 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 899 const char *match, int name_flags) 900 { 901 nvlist_t **child; 902 uint_t c, children; 903 char *vname; 904 boolean_t printed = B_FALSE; 905 906 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 907 &child, &children) != 0) { 908 if (name != NULL) 909 (void) printf("\t%*s%s\n", indent, "", name); 910 return; 911 } 912 913 for (c = 0; c < children; c++) { 914 uint64_t is_log = B_FALSE, is_hole = B_FALSE; 915 const char *class = ""; 916 917 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 918 &is_hole); 919 920 if (is_hole == B_TRUE) { 921 continue; 922 } 923 924 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 925 &is_log); 926 if (is_log) 927 class = VDEV_ALLOC_BIAS_LOG; 928 (void) nvlist_lookup_string(child[c], 929 ZPOOL_CONFIG_ALLOCATION_BIAS, &class); 930 if (strcmp(match, class) != 0) 931 continue; 932 933 if (!printed && name != NULL) { 934 (void) printf("\t%*s%s\n", indent, "", name); 935 printed = B_TRUE; 936 } 937 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags); 938 print_vdev_tree(zhp, vname, child[c], indent + 2, "", 939 name_flags); 940 free(vname); 941 } 942 } 943 944 /* 945 * Print the list of l2cache devices for dry runs. 946 */ 947 static void 948 print_cache_list(nvlist_t *nv, int indent) 949 { 950 nvlist_t **child; 951 uint_t c, children; 952 953 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 954 &child, &children) == 0 && children > 0) { 955 (void) printf("\t%*s%s\n", indent, "", "cache"); 956 } else { 957 return; 958 } 959 for (c = 0; c < children; c++) { 960 char *vname; 961 962 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); 963 (void) printf("\t%*s%s\n", indent + 2, "", vname); 964 free(vname); 965 } 966 } 967 968 /* 969 * Print the list of spares for dry runs. 970 */ 971 static void 972 print_spare_list(nvlist_t *nv, int indent) 973 { 974 nvlist_t **child; 975 uint_t c, children; 976 977 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 978 &child, &children) == 0 && children > 0) { 979 (void) printf("\t%*s%s\n", indent, "", "spares"); 980 } else { 981 return; 982 } 983 for (c = 0; c < children; c++) { 984 char *vname; 985 986 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); 987 (void) printf("\t%*s%s\n", indent + 2, "", vname); 988 free(vname); 989 } 990 } 991 992 typedef struct spare_cbdata { 993 uint64_t cb_guid; 994 zpool_handle_t *cb_zhp; 995 } spare_cbdata_t; 996 997 static boolean_t 998 find_vdev(nvlist_t *nv, uint64_t search) 999 { 1000 uint64_t guid; 1001 nvlist_t **child; 1002 uint_t c, children; 1003 1004 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1005 search == guid) 1006 return (B_TRUE); 1007 1008 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1009 &child, &children) == 0) { 1010 for (c = 0; c < children; c++) 1011 if (find_vdev(child[c], search)) 1012 return (B_TRUE); 1013 } 1014 1015 return (B_FALSE); 1016 } 1017 1018 static int 1019 find_spare(zpool_handle_t *zhp, void *data) 1020 { 1021 spare_cbdata_t *cbp = data; 1022 nvlist_t *config, *nvroot; 1023 1024 config = zpool_get_config(zhp, NULL); 1025 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1026 &nvroot) == 0); 1027 1028 if (find_vdev(nvroot, cbp->cb_guid)) { 1029 cbp->cb_zhp = zhp; 1030 return (1); 1031 } 1032 1033 zpool_close(zhp); 1034 return (0); 1035 } 1036 1037 static void 1038 nice_num_str_nvlist(nvlist_t *item, const char *key, uint64_t value, 1039 boolean_t literal, boolean_t as_int, int format) 1040 { 1041 char buf[256]; 1042 if (literal) { 1043 if (!as_int) 1044 snprintf(buf, 256, "%llu", (u_longlong_t)value); 1045 } else { 1046 switch (format) { 1047 case ZFS_NICENUM_1024: 1048 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_1024); 1049 break; 1050 case ZFS_NICENUM_BYTES: 1051 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_BYTES); 1052 break; 1053 case ZFS_NICENUM_TIME: 1054 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_TIME); 1055 break; 1056 case ZFS_NICE_TIMESTAMP: 1057 format_timestamp(value, buf, 256); 1058 break; 1059 default: 1060 fprintf(stderr, "Invalid number format"); 1061 exit(1); 1062 } 1063 } 1064 if (as_int) 1065 fnvlist_add_uint64(item, key, value); 1066 else 1067 fnvlist_add_string(item, key, buf); 1068 } 1069 1070 /* 1071 * Generates an nvlist with output version for every command based on params. 1072 * Purpose of this is to add a version of JSON output, considering the schema 1073 * format might be updated for each command in future. 1074 * 1075 * Schema: 1076 * 1077 * "output_version": { 1078 * "command": string, 1079 * "vers_major": integer, 1080 * "vers_minor": integer, 1081 * } 1082 */ 1083 static nvlist_t * 1084 zpool_json_schema(int maj_v, int min_v) 1085 { 1086 char cmd[MAX_CMD_LEN]; 1087 nvlist_t *sch = fnvlist_alloc(); 1088 nvlist_t *ov = fnvlist_alloc(); 1089 1090 snprintf(cmd, MAX_CMD_LEN, "zpool %s", current_command->name); 1091 fnvlist_add_string(ov, "command", cmd); 1092 fnvlist_add_uint32(ov, "vers_major", maj_v); 1093 fnvlist_add_uint32(ov, "vers_minor", min_v); 1094 fnvlist_add_nvlist(sch, "output_version", ov); 1095 fnvlist_free(ov); 1096 return (sch); 1097 } 1098 1099 static void 1100 fill_pool_info(nvlist_t *list, zpool_handle_t *zhp, boolean_t addtype, 1101 boolean_t as_int) 1102 { 1103 nvlist_t *config = zpool_get_config(zhp, NULL); 1104 uint64_t guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID); 1105 uint64_t txg = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG); 1106 1107 fnvlist_add_string(list, "name", zpool_get_name(zhp)); 1108 if (addtype) 1109 fnvlist_add_string(list, "type", "POOL"); 1110 fnvlist_add_string(list, "state", zpool_get_state_str(zhp)); 1111 if (as_int) { 1112 if (guid) 1113 fnvlist_add_uint64(list, ZPOOL_CONFIG_POOL_GUID, guid); 1114 if (txg) 1115 fnvlist_add_uint64(list, ZPOOL_CONFIG_POOL_TXG, txg); 1116 fnvlist_add_uint64(list, "spa_version", SPA_VERSION); 1117 fnvlist_add_uint64(list, "zpl_version", ZPL_VERSION); 1118 } else { 1119 char value[ZFS_MAXPROPLEN]; 1120 if (guid) { 1121 snprintf(value, ZFS_MAXPROPLEN, "%llu", 1122 (u_longlong_t)guid); 1123 fnvlist_add_string(list, ZPOOL_CONFIG_POOL_GUID, value); 1124 } 1125 if (txg) { 1126 snprintf(value, ZFS_MAXPROPLEN, "%llu", 1127 (u_longlong_t)txg); 1128 fnvlist_add_string(list, ZPOOL_CONFIG_POOL_TXG, value); 1129 } 1130 fnvlist_add_string(list, "spa_version", SPA_VERSION_STRING); 1131 fnvlist_add_string(list, "zpl_version", ZPL_VERSION_STRING); 1132 } 1133 } 1134 1135 static void 1136 used_by_other(zpool_handle_t *zhp, nvlist_t *nvdev, nvlist_t *list) 1137 { 1138 spare_cbdata_t spare_cb; 1139 verify(nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_GUID, 1140 &spare_cb.cb_guid) == 0); 1141 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { 1142 if (strcmp(zpool_get_name(spare_cb.cb_zhp), 1143 zpool_get_name(zhp)) != 0) { 1144 fnvlist_add_string(list, "used_by", 1145 zpool_get_name(spare_cb.cb_zhp)); 1146 } 1147 zpool_close(spare_cb.cb_zhp); 1148 } 1149 } 1150 1151 static void 1152 fill_vdev_info(nvlist_t *list, zpool_handle_t *zhp, char *name, 1153 boolean_t addtype, boolean_t as_int) 1154 { 1155 boolean_t l2c = B_FALSE; 1156 const char *path, *phys, *devid, *bias = NULL; 1157 uint64_t hole = 0, log = 0, spare = 0; 1158 vdev_stat_t *vs; 1159 uint_t c; 1160 nvlist_t *nvdev; 1161 nvlist_t *nvdev_parent = NULL; 1162 char *_name; 1163 1164 if (strcmp(name, zpool_get_name(zhp)) != 0) 1165 _name = name; 1166 else 1167 _name = (char *)"root-0"; 1168 1169 nvdev = zpool_find_vdev(zhp, _name, NULL, &l2c, NULL); 1170 1171 fnvlist_add_string(list, "name", name); 1172 if (addtype) 1173 fnvlist_add_string(list, "type", "VDEV"); 1174 if (nvdev) { 1175 const char *type = fnvlist_lookup_string(nvdev, 1176 ZPOOL_CONFIG_TYPE); 1177 if (type) 1178 fnvlist_add_string(list, "vdev_type", type); 1179 uint64_t guid = fnvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_GUID); 1180 if (guid) { 1181 if (as_int) { 1182 fnvlist_add_uint64(list, "guid", guid); 1183 } else { 1184 char buf[ZFS_MAXPROPLEN]; 1185 snprintf(buf, ZFS_MAXPROPLEN, "%llu", 1186 (u_longlong_t)guid); 1187 fnvlist_add_string(list, "guid", buf); 1188 } 1189 } 1190 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_PATH, &path) == 0) 1191 fnvlist_add_string(list, "path", path); 1192 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_PHYS_PATH, 1193 &phys) == 0) 1194 fnvlist_add_string(list, "phys_path", phys); 1195 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_DEVID, 1196 &devid) == 0) 1197 fnvlist_add_string(list, "devid", devid); 1198 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_LOG, &log); 1199 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_SPARE, 1200 &spare); 1201 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_HOLE, &hole); 1202 if (hole) 1203 fnvlist_add_string(list, "class", VDEV_TYPE_HOLE); 1204 else if (l2c) 1205 fnvlist_add_string(list, "class", VDEV_TYPE_L2CACHE); 1206 else if (spare) 1207 fnvlist_add_string(list, "class", VDEV_TYPE_SPARE); 1208 else if (log) 1209 fnvlist_add_string(list, "class", VDEV_TYPE_LOG); 1210 else { 1211 (void) nvlist_lookup_string(nvdev, 1212 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 1213 if (bias != NULL) 1214 fnvlist_add_string(list, "class", bias); 1215 else { 1216 nvdev_parent = NULL; 1217 nvdev_parent = zpool_find_parent_vdev(zhp, 1218 _name, NULL, NULL, NULL); 1219 1220 /* 1221 * With a mirrored special device, the parent 1222 * "mirror" vdev will have 1223 * ZPOOL_CONFIG_ALLOCATION_BIAS set to "special" 1224 * not the leaf vdevs. If we're a leaf vdev 1225 * in that case we need to look at our parent 1226 * to see if they're "special" to know if we 1227 * are "special" too. 1228 */ 1229 if (nvdev_parent) { 1230 (void) nvlist_lookup_string( 1231 nvdev_parent, 1232 ZPOOL_CONFIG_ALLOCATION_BIAS, 1233 &bias); 1234 } 1235 if (bias != NULL) 1236 fnvlist_add_string(list, "class", bias); 1237 else 1238 fnvlist_add_string(list, "class", 1239 "normal"); 1240 } 1241 } 1242 if (nvlist_lookup_uint64_array(nvdev, ZPOOL_CONFIG_VDEV_STATS, 1243 (uint64_t **)&vs, &c) == 0) { 1244 fnvlist_add_string(list, "state", 1245 vdev_state_str[vs->vs_state]); 1246 } 1247 } 1248 } 1249 1250 static boolean_t 1251 prop_list_contains_feature(nvlist_t *proplist) 1252 { 1253 nvpair_t *nvp; 1254 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 1255 nvp = nvlist_next_nvpair(proplist, nvp)) { 1256 if (zpool_prop_feature(nvpair_name(nvp))) 1257 return (B_TRUE); 1258 } 1259 return (B_FALSE); 1260 } 1261 1262 /* 1263 * Add a property pair (name, string-value) into a property nvlist. 1264 */ 1265 static int 1266 add_prop_list(const char *propname, const char *propval, nvlist_t **props, 1267 boolean_t poolprop) 1268 { 1269 zpool_prop_t prop = ZPOOL_PROP_INVAL; 1270 nvlist_t *proplist; 1271 const char *normnm; 1272 const char *strval; 1273 1274 if (*props == NULL && 1275 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 1276 (void) fprintf(stderr, 1277 gettext("internal error: out of memory\n")); 1278 return (1); 1279 } 1280 1281 proplist = *props; 1282 1283 if (poolprop) { 1284 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 1285 const char *cname = 1286 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY); 1287 1288 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL && 1289 (!zpool_prop_feature(propname) && 1290 !zpool_prop_vdev(propname))) { 1291 (void) fprintf(stderr, gettext("property '%s' is " 1292 "not a valid pool or vdev property\n"), propname); 1293 return (2); 1294 } 1295 1296 /* 1297 * feature@ properties and version should not be specified 1298 * at the same time. 1299 */ 1300 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) && 1301 nvlist_exists(proplist, vname)) || 1302 (prop == ZPOOL_PROP_VERSION && 1303 prop_list_contains_feature(proplist))) { 1304 (void) fprintf(stderr, gettext("'feature@' and " 1305 "'version' properties cannot be specified " 1306 "together\n")); 1307 return (2); 1308 } 1309 1310 /* 1311 * if version is specified, only "legacy" compatibility 1312 * may be requested 1313 */ 1314 if ((prop == ZPOOL_PROP_COMPATIBILITY && 1315 strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 && 1316 nvlist_exists(proplist, vname)) || 1317 (prop == ZPOOL_PROP_VERSION && 1318 nvlist_exists(proplist, cname) && 1319 strcmp(fnvlist_lookup_string(proplist, cname), 1320 ZPOOL_COMPAT_LEGACY) != 0)) { 1321 (void) fprintf(stderr, gettext("when 'version' is " 1322 "specified, the 'compatibility' feature may only " 1323 "be set to '" ZPOOL_COMPAT_LEGACY "'\n")); 1324 return (2); 1325 } 1326 1327 if (zpool_prop_feature(propname) || zpool_prop_vdev(propname)) 1328 normnm = propname; 1329 else 1330 normnm = zpool_prop_to_name(prop); 1331 } else { 1332 zfs_prop_t fsprop = zfs_name_to_prop(propname); 1333 1334 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM, 1335 B_FALSE)) { 1336 normnm = zfs_prop_to_name(fsprop); 1337 } else if (zfs_prop_user(propname) || 1338 zfs_prop_userquota(propname)) { 1339 normnm = propname; 1340 } else { 1341 (void) fprintf(stderr, gettext("property '%s' is " 1342 "not a valid filesystem property\n"), propname); 1343 return (2); 1344 } 1345 } 1346 1347 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 1348 prop != ZPOOL_PROP_CACHEFILE) { 1349 (void) fprintf(stderr, gettext("property '%s' " 1350 "specified multiple times\n"), propname); 1351 return (2); 1352 } 1353 1354 if (nvlist_add_string(proplist, normnm, propval) != 0) { 1355 (void) fprintf(stderr, gettext("internal " 1356 "error: out of memory\n")); 1357 return (1); 1358 } 1359 1360 return (0); 1361 } 1362 1363 /* 1364 * Set a default property pair (name, string-value) in a property nvlist 1365 */ 1366 static int 1367 add_prop_list_default(const char *propname, const char *propval, 1368 nvlist_t **props) 1369 { 1370 const char *pval; 1371 1372 if (nvlist_lookup_string(*props, propname, &pval) == 0) 1373 return (0); 1374 1375 return (add_prop_list(propname, propval, props, B_TRUE)); 1376 } 1377 1378 /* 1379 * zpool add [-afgLnP] [-o property=value] <pool> <vdev> ... 1380 * 1381 * -a Disable the ashift validation checks 1382 * -f Force addition of devices, even if they appear in use 1383 * -g Display guid for individual vdev name. 1384 * -L Follow links when resolving vdev path name. 1385 * -n Do not add the devices, but display the resulting layout if 1386 * they were to be added. 1387 * -o Set property=value. 1388 * -P Display full path for vdev name. 1389 * 1390 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 1391 * handled by make_root_vdev(), which constructs the nvlist needed to pass to 1392 * libzfs. 1393 */ 1394 int 1395 zpool_do_add(int argc, char **argv) 1396 { 1397 boolean_t check_replication = B_TRUE; 1398 boolean_t check_inuse = B_TRUE; 1399 boolean_t dryrun = B_FALSE; 1400 boolean_t check_ashift = B_TRUE; 1401 boolean_t force = B_FALSE; 1402 int name_flags = 0; 1403 int c; 1404 nvlist_t *nvroot; 1405 char *poolname; 1406 int ret; 1407 zpool_handle_t *zhp; 1408 nvlist_t *config; 1409 nvlist_t *props = NULL; 1410 char *propval; 1411 1412 struct option long_options[] = { 1413 {"allow-in-use", no_argument, NULL, ZPOOL_OPTION_ALLOW_INUSE}, 1414 {"allow-replication-mismatch", no_argument, NULL, 1415 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH}, 1416 {"allow-ashift-mismatch", no_argument, NULL, 1417 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH}, 1418 {0, 0, 0, 0} 1419 }; 1420 1421 /* check options */ 1422 while ((c = getopt_long(argc, argv, "fgLno:P", long_options, NULL)) 1423 != -1) { 1424 switch (c) { 1425 case 'f': 1426 force = B_TRUE; 1427 break; 1428 case 'g': 1429 name_flags |= VDEV_NAME_GUID; 1430 break; 1431 case 'L': 1432 name_flags |= VDEV_NAME_FOLLOW_LINKS; 1433 break; 1434 case 'n': 1435 dryrun = B_TRUE; 1436 break; 1437 case 'o': 1438 if ((propval = strchr(optarg, '=')) == NULL) { 1439 (void) fprintf(stderr, gettext("missing " 1440 "'=' for -o option\n")); 1441 usage(B_FALSE); 1442 } 1443 *propval = '\0'; 1444 propval++; 1445 1446 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 1447 (add_prop_list(optarg, propval, &props, B_TRUE))) 1448 usage(B_FALSE); 1449 break; 1450 case 'P': 1451 name_flags |= VDEV_NAME_PATH; 1452 break; 1453 case ZPOOL_OPTION_ALLOW_INUSE: 1454 check_inuse = B_FALSE; 1455 break; 1456 case ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH: 1457 check_replication = B_FALSE; 1458 break; 1459 case ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH: 1460 check_ashift = B_FALSE; 1461 break; 1462 case '?': 1463 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1464 optopt); 1465 usage(B_FALSE); 1466 } 1467 } 1468 1469 argc -= optind; 1470 argv += optind; 1471 1472 /* get pool name and check number of arguments */ 1473 if (argc < 1) { 1474 (void) fprintf(stderr, gettext("missing pool name argument\n")); 1475 usage(B_FALSE); 1476 } 1477 if (argc < 2) { 1478 (void) fprintf(stderr, gettext("missing vdev specification\n")); 1479 usage(B_FALSE); 1480 } 1481 1482 if (force) { 1483 if (!check_inuse || !check_replication || !check_ashift) { 1484 (void) fprintf(stderr, gettext("'-f' option is not " 1485 "allowed with '--allow-replication-mismatch', " 1486 "'--allow-ashift-mismatch', or " 1487 "'--allow-in-use'\n")); 1488 usage(B_FALSE); 1489 } 1490 check_inuse = B_FALSE; 1491 check_replication = B_FALSE; 1492 check_ashift = B_FALSE; 1493 } 1494 1495 poolname = argv[0]; 1496 1497 argc--; 1498 argv++; 1499 1500 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 1501 return (1); 1502 1503 if ((config = zpool_get_config(zhp, NULL)) == NULL) { 1504 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 1505 poolname); 1506 zpool_close(zhp); 1507 return (1); 1508 } 1509 1510 /* unless manually specified use "ashift" pool property (if set) */ 1511 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 1512 int intval; 1513 zprop_source_t src; 1514 char strval[ZPOOL_MAXPROPLEN]; 1515 1516 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 1517 if (src != ZPROP_SRC_DEFAULT) { 1518 (void) sprintf(strval, "%" PRId32, intval); 1519 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 1520 &props, B_TRUE) == 0); 1521 } 1522 } 1523 1524 /* pass off to make_root_vdev for processing */ 1525 nvroot = make_root_vdev(zhp, props, !check_inuse, 1526 check_replication, B_FALSE, dryrun, argc, argv); 1527 if (nvroot == NULL) { 1528 zpool_close(zhp); 1529 return (1); 1530 } 1531 1532 if (dryrun) { 1533 nvlist_t *poolnvroot; 1534 nvlist_t **l2child, **sparechild; 1535 uint_t l2children, sparechildren, c; 1536 char *vname; 1537 boolean_t hadcache = B_FALSE, hadspare = B_FALSE; 1538 1539 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1540 &poolnvroot) == 0); 1541 1542 (void) printf(gettext("would update '%s' to the following " 1543 "configuration:\n\n"), zpool_get_name(zhp)); 1544 1545 /* print original main pool and new tree */ 1546 print_vdev_tree(zhp, poolname, poolnvroot, 0, "", 1547 name_flags | VDEV_NAME_TYPE_ID); 1548 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags); 1549 1550 /* print other classes: 'dedup', 'special', and 'log' */ 1551 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) { 1552 print_vdev_tree(zhp, "dedup", poolnvroot, 0, 1553 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1554 print_vdev_tree(zhp, NULL, nvroot, 0, 1555 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1556 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) { 1557 print_vdev_tree(zhp, "dedup", nvroot, 0, 1558 VDEV_ALLOC_BIAS_DEDUP, name_flags); 1559 } 1560 1561 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) { 1562 print_vdev_tree(zhp, "special", poolnvroot, 0, 1563 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1564 print_vdev_tree(zhp, NULL, nvroot, 0, 1565 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1566 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) { 1567 print_vdev_tree(zhp, "special", nvroot, 0, 1568 VDEV_ALLOC_BIAS_SPECIAL, name_flags); 1569 } 1570 1571 if (num_logs(poolnvroot) > 0) { 1572 print_vdev_tree(zhp, "logs", poolnvroot, 0, 1573 VDEV_ALLOC_BIAS_LOG, name_flags); 1574 print_vdev_tree(zhp, NULL, nvroot, 0, 1575 VDEV_ALLOC_BIAS_LOG, name_flags); 1576 } else if (num_logs(nvroot) > 0) { 1577 print_vdev_tree(zhp, "logs", nvroot, 0, 1578 VDEV_ALLOC_BIAS_LOG, name_flags); 1579 } 1580 1581 /* Do the same for the caches */ 1582 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE, 1583 &l2child, &l2children) == 0 && l2children) { 1584 hadcache = B_TRUE; 1585 (void) printf(gettext("\tcache\n")); 1586 for (c = 0; c < l2children; c++) { 1587 vname = zpool_vdev_name(g_zfs, NULL, 1588 l2child[c], name_flags); 1589 (void) printf("\t %s\n", vname); 1590 free(vname); 1591 } 1592 } 1593 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1594 &l2child, &l2children) == 0 && l2children) { 1595 if (!hadcache) 1596 (void) printf(gettext("\tcache\n")); 1597 for (c = 0; c < l2children; c++) { 1598 vname = zpool_vdev_name(g_zfs, NULL, 1599 l2child[c], name_flags); 1600 (void) printf("\t %s\n", vname); 1601 free(vname); 1602 } 1603 } 1604 /* And finally the spares */ 1605 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES, 1606 &sparechild, &sparechildren) == 0 && sparechildren > 0) { 1607 hadspare = B_TRUE; 1608 (void) printf(gettext("\tspares\n")); 1609 for (c = 0; c < sparechildren; c++) { 1610 vname = zpool_vdev_name(g_zfs, NULL, 1611 sparechild[c], name_flags); 1612 (void) printf("\t %s\n", vname); 1613 free(vname); 1614 } 1615 } 1616 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1617 &sparechild, &sparechildren) == 0 && sparechildren > 0) { 1618 if (!hadspare) 1619 (void) printf(gettext("\tspares\n")); 1620 for (c = 0; c < sparechildren; c++) { 1621 vname = zpool_vdev_name(g_zfs, NULL, 1622 sparechild[c], name_flags); 1623 (void) printf("\t %s\n", vname); 1624 free(vname); 1625 } 1626 } 1627 1628 ret = 0; 1629 } else { 1630 ret = (zpool_add(zhp, nvroot, check_ashift) != 0); 1631 } 1632 1633 nvlist_free(props); 1634 nvlist_free(nvroot); 1635 zpool_close(zhp); 1636 1637 return (ret); 1638 } 1639 1640 /* 1641 * zpool remove [-npsw] <pool> <vdev> ... 1642 * 1643 * Removes the given vdev from the pool. 1644 */ 1645 int 1646 zpool_do_remove(int argc, char **argv) 1647 { 1648 char *poolname; 1649 int i, ret = 0; 1650 zpool_handle_t *zhp = NULL; 1651 boolean_t stop = B_FALSE; 1652 int c; 1653 boolean_t noop = B_FALSE; 1654 boolean_t parsable = B_FALSE; 1655 boolean_t wait = B_FALSE; 1656 1657 /* check options */ 1658 while ((c = getopt(argc, argv, "npsw")) != -1) { 1659 switch (c) { 1660 case 'n': 1661 noop = B_TRUE; 1662 break; 1663 case 'p': 1664 parsable = B_TRUE; 1665 break; 1666 case 's': 1667 stop = B_TRUE; 1668 break; 1669 case 'w': 1670 wait = B_TRUE; 1671 break; 1672 case '?': 1673 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1674 optopt); 1675 usage(B_FALSE); 1676 } 1677 } 1678 1679 argc -= optind; 1680 argv += optind; 1681 1682 /* get pool name and check number of arguments */ 1683 if (argc < 1) { 1684 (void) fprintf(stderr, gettext("missing pool name argument\n")); 1685 usage(B_FALSE); 1686 } 1687 1688 poolname = argv[0]; 1689 1690 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 1691 return (1); 1692 1693 if (stop && noop) { 1694 zpool_close(zhp); 1695 (void) fprintf(stderr, gettext("stop request ignored\n")); 1696 return (0); 1697 } 1698 1699 if (stop) { 1700 if (argc > 1) { 1701 (void) fprintf(stderr, gettext("too many arguments\n")); 1702 usage(B_FALSE); 1703 } 1704 if (zpool_vdev_remove_cancel(zhp) != 0) 1705 ret = 1; 1706 if (wait) { 1707 (void) fprintf(stderr, gettext("invalid option " 1708 "combination: -w cannot be used with -s\n")); 1709 usage(B_FALSE); 1710 } 1711 } else { 1712 if (argc < 2) { 1713 (void) fprintf(stderr, gettext("missing device\n")); 1714 usage(B_FALSE); 1715 } 1716 1717 for (i = 1; i < argc; i++) { 1718 if (noop) { 1719 uint64_t size; 1720 1721 if (zpool_vdev_indirect_size(zhp, argv[i], 1722 &size) != 0) { 1723 ret = 1; 1724 break; 1725 } 1726 if (parsable) { 1727 (void) printf("%s %llu\n", 1728 argv[i], (unsigned long long)size); 1729 } else { 1730 char valstr[32]; 1731 zfs_nicenum(size, valstr, 1732 sizeof (valstr)); 1733 (void) printf("Memory that will be " 1734 "used after removing %s: %s\n", 1735 argv[i], valstr); 1736 } 1737 } else { 1738 if (zpool_vdev_remove(zhp, argv[i]) != 0) 1739 ret = 1; 1740 } 1741 } 1742 1743 if (ret == 0 && wait) 1744 ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE); 1745 } 1746 zpool_close(zhp); 1747 1748 return (ret); 1749 } 1750 1751 /* 1752 * Return 1 if a vdev is active (being used in a pool) 1753 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool) 1754 * 1755 * This is useful for checking if a disk in an active pool is offlined or 1756 * faulted. 1757 */ 1758 static int 1759 vdev_is_active(char *vdev_path) 1760 { 1761 int fd; 1762 fd = open(vdev_path, O_EXCL); 1763 if (fd < 0) { 1764 return (1); /* cant open O_EXCL - disk is active */ 1765 } 1766 1767 close(fd); 1768 return (0); /* disk is inactive in the pool */ 1769 } 1770 1771 /* 1772 * zpool labelclear [-f] <vdev> 1773 * 1774 * -f Force clearing the label for the vdevs which are members of 1775 * the exported or foreign pools. 1776 * 1777 * Verifies that the vdev is not active and zeros out the label information 1778 * on the device. 1779 */ 1780 int 1781 zpool_do_labelclear(int argc, char **argv) 1782 { 1783 char vdev[MAXPATHLEN]; 1784 char *name = NULL; 1785 int c, fd, ret = 0; 1786 nvlist_t *config; 1787 pool_state_t state; 1788 boolean_t inuse = B_FALSE; 1789 boolean_t force = B_FALSE; 1790 1791 /* check options */ 1792 while ((c = getopt(argc, argv, "f")) != -1) { 1793 switch (c) { 1794 case 'f': 1795 force = B_TRUE; 1796 break; 1797 default: 1798 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1799 optopt); 1800 usage(B_FALSE); 1801 } 1802 } 1803 1804 argc -= optind; 1805 argv += optind; 1806 1807 /* get vdev name */ 1808 if (argc < 1) { 1809 (void) fprintf(stderr, gettext("missing vdev name\n")); 1810 usage(B_FALSE); 1811 } 1812 if (argc > 1) { 1813 (void) fprintf(stderr, gettext("too many arguments\n")); 1814 usage(B_FALSE); 1815 } 1816 1817 (void) strlcpy(vdev, argv[0], sizeof (vdev)); 1818 1819 /* 1820 * If we cannot open an absolute path, we quit. 1821 * Otherwise if the provided vdev name doesn't point to a file, 1822 * try prepending expected disk paths and partition numbers. 1823 */ 1824 if ((fd = open(vdev, O_RDWR)) < 0) { 1825 int error; 1826 if (vdev[0] == '/') { 1827 (void) fprintf(stderr, gettext("failed to open " 1828 "%s: %s\n"), vdev, strerror(errno)); 1829 return (1); 1830 } 1831 1832 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN); 1833 if (error == 0 && zfs_dev_is_whole_disk(vdev)) { 1834 if (zfs_append_partition(vdev, MAXPATHLEN) == -1) 1835 error = ENOENT; 1836 } 1837 1838 if (error || ((fd = open(vdev, O_RDWR)) < 0)) { 1839 if (errno == ENOENT) { 1840 (void) fprintf(stderr, gettext( 1841 "failed to find device %s, try " 1842 "specifying absolute path instead\n"), 1843 argv[0]); 1844 return (1); 1845 } 1846 1847 (void) fprintf(stderr, gettext("failed to open %s:" 1848 " %s\n"), vdev, strerror(errno)); 1849 return (1); 1850 } 1851 } 1852 1853 /* 1854 * Flush all dirty pages for the block device. This should not be 1855 * fatal when the device does not support BLKFLSBUF as would be the 1856 * case for a file vdev. 1857 */ 1858 if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY)) 1859 (void) fprintf(stderr, gettext("failed to invalidate " 1860 "cache for %s: %s\n"), vdev, strerror(errno)); 1861 1862 if (zpool_read_label(fd, &config, NULL) != 0) { 1863 (void) fprintf(stderr, 1864 gettext("failed to read label from %s\n"), vdev); 1865 ret = 1; 1866 goto errout; 1867 } 1868 nvlist_free(config); 1869 1870 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse); 1871 if (ret != 0) { 1872 (void) fprintf(stderr, 1873 gettext("failed to check state for %s\n"), vdev); 1874 ret = 1; 1875 goto errout; 1876 } 1877 1878 if (!inuse) 1879 goto wipe_label; 1880 1881 switch (state) { 1882 default: 1883 case POOL_STATE_ACTIVE: 1884 case POOL_STATE_SPARE: 1885 case POOL_STATE_L2CACHE: 1886 /* 1887 * We allow the user to call 'zpool offline -f' 1888 * on an offlined disk in an active pool. We can check if 1889 * the disk is online by calling vdev_is_active(). 1890 */ 1891 if (force && !vdev_is_active(vdev)) 1892 break; 1893 1894 (void) fprintf(stderr, gettext( 1895 "%s is a member (%s) of pool \"%s\""), 1896 vdev, zpool_pool_state_to_name(state), name); 1897 1898 if (force) { 1899 (void) fprintf(stderr, gettext( 1900 ". Offline the disk first to clear its label.")); 1901 } 1902 printf("\n"); 1903 ret = 1; 1904 goto errout; 1905 1906 case POOL_STATE_EXPORTED: 1907 if (force) 1908 break; 1909 (void) fprintf(stderr, gettext( 1910 "use '-f' to override the following error:\n" 1911 "%s is a member of exported pool \"%s\"\n"), 1912 vdev, name); 1913 ret = 1; 1914 goto errout; 1915 1916 case POOL_STATE_POTENTIALLY_ACTIVE: 1917 if (force) 1918 break; 1919 (void) fprintf(stderr, gettext( 1920 "use '-f' to override the following error:\n" 1921 "%s is a member of potentially active pool \"%s\"\n"), 1922 vdev, name); 1923 ret = 1; 1924 goto errout; 1925 1926 case POOL_STATE_DESTROYED: 1927 /* inuse should never be set for a destroyed pool */ 1928 assert(0); 1929 break; 1930 } 1931 1932 wipe_label: 1933 ret = zpool_clear_label(fd); 1934 if (ret != 0) { 1935 (void) fprintf(stderr, 1936 gettext("failed to clear label for %s\n"), vdev); 1937 } 1938 1939 errout: 1940 free(name); 1941 (void) close(fd); 1942 1943 return (ret); 1944 } 1945 1946 /* 1947 * zpool create [-fnd] [-o property=value] ... 1948 * [-O file-system-property=value] ... 1949 * [-R root] [-m mountpoint] <pool> <dev> ... 1950 * 1951 * -f Force creation, even if devices appear in use 1952 * -n Do not create the pool, but display the resulting layout if it 1953 * were to be created. 1954 * -R Create a pool under an alternate root 1955 * -m Set default mountpoint for the root dataset. By default it's 1956 * '/<pool>' 1957 * -o Set property=value. 1958 * -o Set feature@feature=enabled|disabled. 1959 * -d Don't automatically enable all supported pool features 1960 * (individual features can be enabled with -o). 1961 * -O Set fsproperty=value in the pool's root file system 1962 * 1963 * Creates the named pool according to the given vdev specification. The 1964 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c. 1965 * Once we get the nvlist back from make_root_vdev(), we either print out the 1966 * contents (if '-n' was specified), or pass it to libzfs to do the creation. 1967 */ 1968 int 1969 zpool_do_create(int argc, char **argv) 1970 { 1971 boolean_t force = B_FALSE; 1972 boolean_t dryrun = B_FALSE; 1973 boolean_t enable_pool_features = B_TRUE; 1974 1975 int c; 1976 nvlist_t *nvroot = NULL; 1977 char *poolname; 1978 char *tname = NULL; 1979 int ret = 1; 1980 char *altroot = NULL; 1981 char *compat = NULL; 1982 char *mountpoint = NULL; 1983 nvlist_t *fsprops = NULL; 1984 nvlist_t *props = NULL; 1985 char *propval; 1986 1987 /* check options */ 1988 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) { 1989 switch (c) { 1990 case 'f': 1991 force = B_TRUE; 1992 break; 1993 case 'n': 1994 dryrun = B_TRUE; 1995 break; 1996 case 'd': 1997 enable_pool_features = B_FALSE; 1998 break; 1999 case 'R': 2000 altroot = optarg; 2001 if (add_prop_list(zpool_prop_to_name( 2002 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 2003 goto errout; 2004 if (add_prop_list_default(zpool_prop_to_name( 2005 ZPOOL_PROP_CACHEFILE), "none", &props)) 2006 goto errout; 2007 break; 2008 case 'm': 2009 /* Equivalent to -O mountpoint=optarg */ 2010 mountpoint = optarg; 2011 break; 2012 case 'o': 2013 if ((propval = strchr(optarg, '=')) == NULL) { 2014 (void) fprintf(stderr, gettext("missing " 2015 "'=' for -o option\n")); 2016 goto errout; 2017 } 2018 *propval = '\0'; 2019 propval++; 2020 2021 if (add_prop_list(optarg, propval, &props, B_TRUE)) 2022 goto errout; 2023 2024 /* 2025 * If the user is creating a pool that doesn't support 2026 * feature flags, don't enable any features. 2027 */ 2028 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 2029 char *end; 2030 u_longlong_t ver; 2031 2032 ver = strtoull(propval, &end, 0); 2033 if (*end == '\0' && 2034 ver < SPA_VERSION_FEATURES) { 2035 enable_pool_features = B_FALSE; 2036 } 2037 } 2038 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) 2039 altroot = propval; 2040 if (zpool_name_to_prop(optarg) == 2041 ZPOOL_PROP_COMPATIBILITY) 2042 compat = propval; 2043 break; 2044 case 'O': 2045 if ((propval = strchr(optarg, '=')) == NULL) { 2046 (void) fprintf(stderr, gettext("missing " 2047 "'=' for -O option\n")); 2048 goto errout; 2049 } 2050 *propval = '\0'; 2051 propval++; 2052 2053 /* 2054 * Mountpoints are checked and then added later. 2055 * Uniquely among properties, they can be specified 2056 * more than once, to avoid conflict with -m. 2057 */ 2058 if (0 == strcmp(optarg, 2059 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { 2060 mountpoint = propval; 2061 } else if (add_prop_list(optarg, propval, &fsprops, 2062 B_FALSE)) { 2063 goto errout; 2064 } 2065 break; 2066 case 't': 2067 /* 2068 * Sanity check temporary pool name. 2069 */ 2070 if (strchr(optarg, '/') != NULL) { 2071 (void) fprintf(stderr, gettext("cannot create " 2072 "'%s': invalid character '/' in temporary " 2073 "name\n"), optarg); 2074 (void) fprintf(stderr, gettext("use 'zfs " 2075 "create' to create a dataset\n")); 2076 goto errout; 2077 } 2078 2079 if (add_prop_list(zpool_prop_to_name( 2080 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE)) 2081 goto errout; 2082 if (add_prop_list_default(zpool_prop_to_name( 2083 ZPOOL_PROP_CACHEFILE), "none", &props)) 2084 goto errout; 2085 tname = optarg; 2086 break; 2087 case ':': 2088 (void) fprintf(stderr, gettext("missing argument for " 2089 "'%c' option\n"), optopt); 2090 goto badusage; 2091 case '?': 2092 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2093 optopt); 2094 goto badusage; 2095 } 2096 } 2097 2098 argc -= optind; 2099 argv += optind; 2100 2101 /* get pool name and check number of arguments */ 2102 if (argc < 1) { 2103 (void) fprintf(stderr, gettext("missing pool name argument\n")); 2104 goto badusage; 2105 } 2106 if (argc < 2) { 2107 (void) fprintf(stderr, gettext("missing vdev specification\n")); 2108 goto badusage; 2109 } 2110 2111 poolname = argv[0]; 2112 2113 /* 2114 * As a special case, check for use of '/' in the name, and direct the 2115 * user to use 'zfs create' instead. 2116 */ 2117 if (strchr(poolname, '/') != NULL) { 2118 (void) fprintf(stderr, gettext("cannot create '%s': invalid " 2119 "character '/' in pool name\n"), poolname); 2120 (void) fprintf(stderr, gettext("use 'zfs create' to " 2121 "create a dataset\n")); 2122 goto errout; 2123 } 2124 2125 /* pass off to make_root_vdev for bulk processing */ 2126 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, 2127 argc - 1, argv + 1); 2128 if (nvroot == NULL) 2129 goto errout; 2130 2131 /* make_root_vdev() allows 0 toplevel children if there are spares */ 2132 if (!zfs_allocatable_devs(nvroot)) { 2133 (void) fprintf(stderr, gettext("invalid vdev " 2134 "specification: at least one toplevel vdev must be " 2135 "specified\n")); 2136 goto errout; 2137 } 2138 2139 if (altroot != NULL && altroot[0] != '/') { 2140 (void) fprintf(stderr, gettext("invalid alternate root '%s': " 2141 "must be an absolute path\n"), altroot); 2142 goto errout; 2143 } 2144 2145 /* 2146 * Check the validity of the mountpoint and direct the user to use the 2147 * '-m' mountpoint option if it looks like its in use. 2148 */ 2149 if (mountpoint == NULL || 2150 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 2151 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { 2152 char buf[MAXPATHLEN]; 2153 DIR *dirp; 2154 2155 if (mountpoint && mountpoint[0] != '/') { 2156 (void) fprintf(stderr, gettext("invalid mountpoint " 2157 "'%s': must be an absolute path, 'legacy', or " 2158 "'none'\n"), mountpoint); 2159 goto errout; 2160 } 2161 2162 if (mountpoint == NULL) { 2163 if (altroot != NULL) 2164 (void) snprintf(buf, sizeof (buf), "%s/%s", 2165 altroot, poolname); 2166 else 2167 (void) snprintf(buf, sizeof (buf), "/%s", 2168 poolname); 2169 } else { 2170 if (altroot != NULL) 2171 (void) snprintf(buf, sizeof (buf), "%s%s", 2172 altroot, mountpoint); 2173 else 2174 (void) snprintf(buf, sizeof (buf), "%s", 2175 mountpoint); 2176 } 2177 2178 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 2179 (void) fprintf(stderr, gettext("mountpoint '%s' : " 2180 "%s\n"), buf, strerror(errno)); 2181 (void) fprintf(stderr, gettext("use '-m' " 2182 "option to provide a different default\n")); 2183 goto errout; 2184 } else if (dirp) { 2185 int count = 0; 2186 2187 while (count < 3 && readdir(dirp) != NULL) 2188 count++; 2189 (void) closedir(dirp); 2190 2191 if (count > 2) { 2192 (void) fprintf(stderr, gettext("mountpoint " 2193 "'%s' exists and is not empty\n"), buf); 2194 (void) fprintf(stderr, gettext("use '-m' " 2195 "option to provide a " 2196 "different default\n")); 2197 goto errout; 2198 } 2199 } 2200 } 2201 2202 /* 2203 * Now that the mountpoint's validity has been checked, ensure that 2204 * the property is set appropriately prior to creating the pool. 2205 */ 2206 if (mountpoint != NULL) { 2207 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 2208 mountpoint, &fsprops, B_FALSE); 2209 if (ret != 0) 2210 goto errout; 2211 } 2212 2213 ret = 1; 2214 if (dryrun) { 2215 /* 2216 * For a dry run invocation, print out a basic message and run 2217 * through all the vdevs in the list and print out in an 2218 * appropriate hierarchy. 2219 */ 2220 (void) printf(gettext("would create '%s' with the " 2221 "following layout:\n\n"), poolname); 2222 2223 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0); 2224 print_vdev_tree(NULL, "dedup", nvroot, 0, 2225 VDEV_ALLOC_BIAS_DEDUP, 0); 2226 print_vdev_tree(NULL, "special", nvroot, 0, 2227 VDEV_ALLOC_BIAS_SPECIAL, 0); 2228 print_vdev_tree(NULL, "logs", nvroot, 0, 2229 VDEV_ALLOC_BIAS_LOG, 0); 2230 print_cache_list(nvroot, 0); 2231 print_spare_list(nvroot, 0); 2232 2233 ret = 0; 2234 } else { 2235 /* 2236 * Load in feature set. 2237 * Note: if compatibility property not given, we'll have 2238 * NULL, which means 'all features'. 2239 */ 2240 boolean_t requested_features[SPA_FEATURES]; 2241 if (zpool_do_load_compat(compat, requested_features) != 2242 ZPOOL_COMPATIBILITY_OK) 2243 goto errout; 2244 2245 /* 2246 * props contains list of features to enable. 2247 * For each feature: 2248 * - remove it if feature@name=disabled 2249 * - leave it there if feature@name=enabled 2250 * - add it if: 2251 * - enable_pool_features (ie: no '-d' or '-o version') 2252 * - it's supported by the kernel module 2253 * - it's in the requested feature set 2254 * - warn if it's enabled but not in compat 2255 */ 2256 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 2257 char propname[MAXPATHLEN]; 2258 const char *propval; 2259 zfeature_info_t *feat = &spa_feature_table[i]; 2260 2261 (void) snprintf(propname, sizeof (propname), 2262 "feature@%s", feat->fi_uname); 2263 2264 if (!nvlist_lookup_string(props, propname, &propval)) { 2265 if (strcmp(propval, 2266 ZFS_FEATURE_DISABLED) == 0) { 2267 (void) nvlist_remove_all(props, 2268 propname); 2269 } else if (strcmp(propval, 2270 ZFS_FEATURE_ENABLED) == 0 && 2271 !requested_features[i]) { 2272 (void) fprintf(stderr, gettext( 2273 "Warning: feature \"%s\" enabled " 2274 "but is not in specified " 2275 "'compatibility' feature set.\n"), 2276 feat->fi_uname); 2277 } 2278 } else if ( 2279 enable_pool_features && 2280 feat->fi_zfs_mod_supported && 2281 requested_features[i]) { 2282 ret = add_prop_list(propname, 2283 ZFS_FEATURE_ENABLED, &props, B_TRUE); 2284 if (ret != 0) 2285 goto errout; 2286 } 2287 } 2288 2289 ret = 1; 2290 if (zpool_create(g_zfs, poolname, 2291 nvroot, props, fsprops) == 0) { 2292 zfs_handle_t *pool = zfs_open(g_zfs, 2293 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); 2294 if (pool != NULL) { 2295 if (zfs_mount(pool, NULL, 0) == 0) { 2296 ret = zfs_share(pool, NULL); 2297 zfs_commit_shares(NULL); 2298 } 2299 zfs_close(pool); 2300 } 2301 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 2302 (void) fprintf(stderr, gettext("pool name may have " 2303 "been omitted\n")); 2304 } 2305 } 2306 2307 errout: 2308 nvlist_free(nvroot); 2309 nvlist_free(fsprops); 2310 nvlist_free(props); 2311 return (ret); 2312 badusage: 2313 nvlist_free(fsprops); 2314 nvlist_free(props); 2315 usage(B_FALSE); 2316 return (2); 2317 } 2318 2319 /* 2320 * zpool destroy <pool> 2321 * 2322 * -f Forcefully unmount any datasets 2323 * 2324 * Destroy the given pool. Automatically unmounts any datasets in the pool. 2325 */ 2326 int 2327 zpool_do_destroy(int argc, char **argv) 2328 { 2329 boolean_t force = B_FALSE; 2330 int c; 2331 char *pool; 2332 zpool_handle_t *zhp; 2333 int ret; 2334 2335 /* check options */ 2336 while ((c = getopt(argc, argv, "f")) != -1) { 2337 switch (c) { 2338 case 'f': 2339 force = B_TRUE; 2340 break; 2341 case '?': 2342 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2343 optopt); 2344 usage(B_FALSE); 2345 } 2346 } 2347 2348 argc -= optind; 2349 argv += optind; 2350 2351 /* check arguments */ 2352 if (argc < 1) { 2353 (void) fprintf(stderr, gettext("missing pool argument\n")); 2354 usage(B_FALSE); 2355 } 2356 if (argc > 1) { 2357 (void) fprintf(stderr, gettext("too many arguments\n")); 2358 usage(B_FALSE); 2359 } 2360 2361 pool = argv[0]; 2362 2363 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 2364 /* 2365 * As a special case, check for use of '/' in the name, and 2366 * direct the user to use 'zfs destroy' instead. 2367 */ 2368 if (strchr(pool, '/') != NULL) 2369 (void) fprintf(stderr, gettext("use 'zfs destroy' to " 2370 "destroy a dataset\n")); 2371 return (1); 2372 } 2373 2374 if (zpool_disable_datasets(zhp, force) != 0) { 2375 (void) fprintf(stderr, gettext("could not destroy '%s': " 2376 "could not unmount datasets\n"), zpool_get_name(zhp)); 2377 zpool_close(zhp); 2378 return (1); 2379 } 2380 2381 /* The history must be logged as part of the export */ 2382 log_history = B_FALSE; 2383 2384 ret = (zpool_destroy(zhp, history_str) != 0); 2385 2386 zpool_close(zhp); 2387 2388 return (ret); 2389 } 2390 2391 typedef struct export_cbdata { 2392 tpool_t *tpool; 2393 pthread_mutex_t mnttab_lock; 2394 boolean_t force; 2395 boolean_t hardforce; 2396 int retval; 2397 } export_cbdata_t; 2398 2399 2400 typedef struct { 2401 char *aea_poolname; 2402 export_cbdata_t *aea_cbdata; 2403 } async_export_args_t; 2404 2405 /* 2406 * Export one pool 2407 */ 2408 static int 2409 zpool_export_one(zpool_handle_t *zhp, void *data) 2410 { 2411 export_cbdata_t *cb = data; 2412 2413 /* 2414 * zpool_disable_datasets() is not thread-safe for mnttab access. 2415 * So we serialize access here for 'zpool export -a' parallel case. 2416 */ 2417 if (cb->tpool != NULL) 2418 pthread_mutex_lock(&cb->mnttab_lock); 2419 2420 int retval = zpool_disable_datasets(zhp, cb->force); 2421 2422 if (cb->tpool != NULL) 2423 pthread_mutex_unlock(&cb->mnttab_lock); 2424 2425 if (retval) 2426 return (1); 2427 2428 if (cb->hardforce) { 2429 if (zpool_export_force(zhp, history_str) != 0) 2430 return (1); 2431 } else if (zpool_export(zhp, cb->force, history_str) != 0) { 2432 return (1); 2433 } 2434 2435 return (0); 2436 } 2437 2438 /* 2439 * Asynchronous export request 2440 */ 2441 static void 2442 zpool_export_task(void *arg) 2443 { 2444 async_export_args_t *aea = arg; 2445 2446 zpool_handle_t *zhp = zpool_open(g_zfs, aea->aea_poolname); 2447 if (zhp != NULL) { 2448 int ret = zpool_export_one(zhp, aea->aea_cbdata); 2449 if (ret != 0) 2450 aea->aea_cbdata->retval = ret; 2451 zpool_close(zhp); 2452 } else { 2453 aea->aea_cbdata->retval = 1; 2454 } 2455 2456 free(aea->aea_poolname); 2457 free(aea); 2458 } 2459 2460 /* 2461 * Process an export request in parallel 2462 */ 2463 static int 2464 zpool_export_one_async(zpool_handle_t *zhp, void *data) 2465 { 2466 tpool_t *tpool = ((export_cbdata_t *)data)->tpool; 2467 async_export_args_t *aea = safe_malloc(sizeof (async_export_args_t)); 2468 2469 /* save pool name since zhp will go out of scope */ 2470 aea->aea_poolname = strdup(zpool_get_name(zhp)); 2471 aea->aea_cbdata = data; 2472 2473 /* ship off actual export to another thread */ 2474 if (tpool_dispatch(tpool, zpool_export_task, (void *)aea) != 0) 2475 return (errno); /* unlikely */ 2476 else 2477 return (0); 2478 } 2479 2480 /* 2481 * zpool export [-f] <pool> ... 2482 * 2483 * -a Export all pools 2484 * -f Forcefully unmount datasets 2485 * 2486 * Export the given pools. By default, the command will attempt to cleanly 2487 * unmount any active datasets within the pool. If the '-f' flag is specified, 2488 * then the datasets will be forcefully unmounted. 2489 */ 2490 int 2491 zpool_do_export(int argc, char **argv) 2492 { 2493 export_cbdata_t cb; 2494 boolean_t do_all = B_FALSE; 2495 boolean_t force = B_FALSE; 2496 boolean_t hardforce = B_FALSE; 2497 int c, ret; 2498 2499 /* check options */ 2500 while ((c = getopt(argc, argv, "afF")) != -1) { 2501 switch (c) { 2502 case 'a': 2503 do_all = B_TRUE; 2504 break; 2505 case 'f': 2506 force = B_TRUE; 2507 break; 2508 case 'F': 2509 hardforce = B_TRUE; 2510 break; 2511 case '?': 2512 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2513 optopt); 2514 usage(B_FALSE); 2515 } 2516 } 2517 2518 cb.force = force; 2519 cb.hardforce = hardforce; 2520 cb.tpool = NULL; 2521 cb.retval = 0; 2522 argc -= optind; 2523 argv += optind; 2524 2525 /* The history will be logged as part of the export itself */ 2526 log_history = B_FALSE; 2527 2528 if (do_all) { 2529 if (argc != 0) { 2530 (void) fprintf(stderr, gettext("too many arguments\n")); 2531 usage(B_FALSE); 2532 } 2533 2534 cb.tpool = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), 2535 0, NULL); 2536 pthread_mutex_init(&cb.mnttab_lock, NULL); 2537 2538 /* Asynchronously call zpool_export_one using thread pool */ 2539 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 2540 B_FALSE, zpool_export_one_async, &cb); 2541 2542 tpool_wait(cb.tpool); 2543 tpool_destroy(cb.tpool); 2544 (void) pthread_mutex_destroy(&cb.mnttab_lock); 2545 2546 return (ret | cb.retval); 2547 } 2548 2549 /* check arguments */ 2550 if (argc < 1) { 2551 (void) fprintf(stderr, gettext("missing pool argument\n")); 2552 usage(B_FALSE); 2553 } 2554 2555 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 2556 B_FALSE, zpool_export_one, &cb); 2557 2558 return (ret); 2559 } 2560 2561 /* 2562 * Given a vdev configuration, determine the maximum width needed for the device 2563 * name column. 2564 */ 2565 static int 2566 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max, 2567 int name_flags) 2568 { 2569 static const char *const subtypes[] = 2570 {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN}; 2571 2572 char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags); 2573 max = MAX(strlen(name) + depth, max); 2574 free(name); 2575 2576 nvlist_t **child; 2577 uint_t children; 2578 for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i) 2579 if (nvlist_lookup_nvlist_array(nv, subtypes[i], 2580 &child, &children) == 0) 2581 for (uint_t c = 0; c < children; ++c) 2582 max = MAX(max_width(zhp, child[c], depth + 2, 2583 max, name_flags), max); 2584 2585 return (max); 2586 } 2587 2588 typedef struct status_cbdata { 2589 int cb_count; 2590 int cb_name_flags; 2591 int cb_namewidth; 2592 boolean_t cb_allpools; 2593 boolean_t cb_verbose; 2594 boolean_t cb_literal; 2595 boolean_t cb_explain; 2596 boolean_t cb_first; 2597 boolean_t cb_dedup_stats; 2598 boolean_t cb_print_unhealthy; 2599 boolean_t cb_print_status; 2600 boolean_t cb_print_slow_ios; 2601 boolean_t cb_print_dio_verify; 2602 boolean_t cb_print_vdev_init; 2603 boolean_t cb_print_vdev_trim; 2604 vdev_cmd_data_list_t *vcdl; 2605 boolean_t cb_print_power; 2606 boolean_t cb_json; 2607 boolean_t cb_flat_vdevs; 2608 nvlist_t *cb_jsobj; 2609 boolean_t cb_json_as_int; 2610 boolean_t cb_json_pool_key_guid; 2611 } status_cbdata_t; 2612 2613 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ 2614 static boolean_t 2615 is_blank_str(const char *str) 2616 { 2617 for (; str != NULL && *str != '\0'; ++str) 2618 if (!isblank(*str)) 2619 return (B_FALSE); 2620 return (B_TRUE); 2621 } 2622 2623 static void 2624 zpool_nvlist_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path, 2625 nvlist_t *item) 2626 { 2627 vdev_cmd_data_t *data; 2628 int i, j, k = 1; 2629 char tmp[256]; 2630 const char *val; 2631 2632 for (i = 0; i < vcdl->count; i++) { 2633 if ((strcmp(vcdl->data[i].path, path) != 0) || 2634 (strcmp(vcdl->data[i].pool, pool) != 0)) 2635 continue; 2636 2637 data = &vcdl->data[i]; 2638 for (j = 0; j < vcdl->uniq_cols_cnt; j++) { 2639 val = NULL; 2640 for (int k = 0; k < data->cols_cnt; k++) { 2641 if (strcmp(data->cols[k], 2642 vcdl->uniq_cols[j]) == 0) { 2643 val = data->lines[k]; 2644 break; 2645 } 2646 } 2647 if (val == NULL || is_blank_str(val)) 2648 val = "-"; 2649 fnvlist_add_string(item, vcdl->uniq_cols[j], val); 2650 } 2651 2652 for (j = data->cols_cnt; j < data->lines_cnt; j++) { 2653 if (data->lines[j]) { 2654 snprintf(tmp, 256, "extra_%d", k++); 2655 fnvlist_add_string(item, tmp, 2656 data->lines[j]); 2657 } 2658 } 2659 break; 2660 } 2661 } 2662 2663 /* Print command output lines for specific vdev in a specific pool */ 2664 static void 2665 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path) 2666 { 2667 vdev_cmd_data_t *data; 2668 int i, j; 2669 const char *val; 2670 2671 for (i = 0; i < vcdl->count; i++) { 2672 if ((strcmp(vcdl->data[i].path, path) != 0) || 2673 (strcmp(vcdl->data[i].pool, pool) != 0)) { 2674 /* Not the vdev we're looking for */ 2675 continue; 2676 } 2677 2678 data = &vcdl->data[i]; 2679 /* Print out all the output values for this vdev */ 2680 for (j = 0; j < vcdl->uniq_cols_cnt; j++) { 2681 val = NULL; 2682 /* Does this vdev have values for this column? */ 2683 for (int k = 0; k < data->cols_cnt; k++) { 2684 if (strcmp(data->cols[k], 2685 vcdl->uniq_cols[j]) == 0) { 2686 /* yes it does, record the value */ 2687 val = data->lines[k]; 2688 break; 2689 } 2690 } 2691 /* 2692 * Mark empty values with dashes to make output 2693 * awk-able. 2694 */ 2695 if (val == NULL || is_blank_str(val)) 2696 val = "-"; 2697 2698 printf("%*s", vcdl->uniq_cols_width[j], val); 2699 if (j < vcdl->uniq_cols_cnt - 1) 2700 fputs(" ", stdout); 2701 } 2702 2703 /* Print out any values that aren't in a column at the end */ 2704 for (j = data->cols_cnt; j < data->lines_cnt; j++) { 2705 /* Did we have any columns? If so print a spacer. */ 2706 if (vcdl->uniq_cols_cnt > 0) 2707 fputs(" ", stdout); 2708 2709 val = data->lines[j]; 2710 fputs(val ?: "", stdout); 2711 } 2712 break; 2713 } 2714 } 2715 2716 /* 2717 * Print vdev initialization status for leaves 2718 */ 2719 static void 2720 print_status_initialize(vdev_stat_t *vs, boolean_t verbose) 2721 { 2722 if (verbose) { 2723 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE || 2724 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED || 2725 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) && 2726 !vs->vs_scan_removing) { 2727 char zbuf[1024]; 2728 char tbuf[256]; 2729 2730 time_t t = vs->vs_initialize_action_time; 2731 int initialize_pct = 100; 2732 if (vs->vs_initialize_state != 2733 VDEV_INITIALIZE_COMPLETE) { 2734 initialize_pct = (vs->vs_initialize_bytes_done * 2735 100 / (vs->vs_initialize_bytes_est + 1)); 2736 } 2737 2738 (void) ctime_r(&t, tbuf); 2739 tbuf[24] = 0; 2740 2741 switch (vs->vs_initialize_state) { 2742 case VDEV_INITIALIZE_SUSPENDED: 2743 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2744 gettext("suspended, started at"), tbuf); 2745 break; 2746 case VDEV_INITIALIZE_ACTIVE: 2747 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2748 gettext("started at"), tbuf); 2749 break; 2750 case VDEV_INITIALIZE_COMPLETE: 2751 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2752 gettext("completed at"), tbuf); 2753 break; 2754 } 2755 2756 (void) printf(gettext(" (%d%% initialized%s)"), 2757 initialize_pct, zbuf); 2758 } else { 2759 (void) printf(gettext(" (uninitialized)")); 2760 } 2761 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) { 2762 (void) printf(gettext(" (initializing)")); 2763 } 2764 } 2765 2766 /* 2767 * Print vdev TRIM status for leaves 2768 */ 2769 static void 2770 print_status_trim(vdev_stat_t *vs, boolean_t verbose) 2771 { 2772 if (verbose) { 2773 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE || 2774 vs->vs_trim_state == VDEV_TRIM_SUSPENDED || 2775 vs->vs_trim_state == VDEV_TRIM_COMPLETE) && 2776 !vs->vs_scan_removing) { 2777 char zbuf[1024]; 2778 char tbuf[256]; 2779 2780 time_t t = vs->vs_trim_action_time; 2781 int trim_pct = 100; 2782 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) { 2783 trim_pct = (vs->vs_trim_bytes_done * 2784 100 / (vs->vs_trim_bytes_est + 1)); 2785 } 2786 2787 (void) ctime_r(&t, tbuf); 2788 tbuf[24] = 0; 2789 2790 switch (vs->vs_trim_state) { 2791 case VDEV_TRIM_SUSPENDED: 2792 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2793 gettext("suspended, started at"), tbuf); 2794 break; 2795 case VDEV_TRIM_ACTIVE: 2796 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2797 gettext("started at"), tbuf); 2798 break; 2799 case VDEV_TRIM_COMPLETE: 2800 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", 2801 gettext("completed at"), tbuf); 2802 break; 2803 } 2804 2805 (void) printf(gettext(" (%d%% trimmed%s)"), 2806 trim_pct, zbuf); 2807 } else if (vs->vs_trim_notsup) { 2808 (void) printf(gettext(" (trim unsupported)")); 2809 } else { 2810 (void) printf(gettext(" (untrimmed)")); 2811 } 2812 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) { 2813 (void) printf(gettext(" (trimming)")); 2814 } 2815 } 2816 2817 /* 2818 * Return the color associated with a health string. This includes returning 2819 * NULL for no color change. 2820 */ 2821 static const char * 2822 health_str_to_color(const char *health) 2823 { 2824 if (strcmp(health, gettext("FAULTED")) == 0 || 2825 strcmp(health, gettext("SUSPENDED")) == 0 || 2826 strcmp(health, gettext("UNAVAIL")) == 0) { 2827 return (ANSI_RED); 2828 } 2829 2830 if (strcmp(health, gettext("OFFLINE")) == 0 || 2831 strcmp(health, gettext("DEGRADED")) == 0 || 2832 strcmp(health, gettext("REMOVED")) == 0) { 2833 return (ANSI_YELLOW); 2834 } 2835 2836 return (NULL); 2837 } 2838 2839 /* 2840 * Called for each leaf vdev. Returns 0 if the vdev is healthy. 2841 * A vdev is unhealthy if any of the following are true: 2842 * 1) there are read, write, or checksum errors, 2843 * 2) its state is not ONLINE, or 2844 * 3) slow IO reporting was requested (-s) and there are slow IOs. 2845 */ 2846 static int 2847 vdev_health_check_cb(void *hdl_data, nvlist_t *nv, void *data) 2848 { 2849 status_cbdata_t *cb = data; 2850 vdev_stat_t *vs; 2851 uint_t vsc; 2852 (void) hdl_data; 2853 2854 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2855 (uint64_t **)&vs, &vsc) != 0) 2856 return (1); 2857 2858 if (vs->vs_checksum_errors || vs->vs_read_errors || 2859 vs->vs_write_errors || vs->vs_state != VDEV_STATE_HEALTHY) 2860 return (1); 2861 2862 if (cb->cb_print_slow_ios && vs->vs_slow_ios) 2863 return (1); 2864 2865 return (0); 2866 } 2867 2868 /* 2869 * Print out configuration state as requested by status_callback. 2870 */ 2871 static void 2872 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name, 2873 nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs) 2874 { 2875 nvlist_t **child, *root; 2876 uint_t c, i, vsc, children; 2877 pool_scan_stat_t *ps = NULL; 2878 vdev_stat_t *vs; 2879 char rbuf[6], wbuf[6], cbuf[6], dbuf[6]; 2880 char *vname; 2881 uint64_t notpresent; 2882 spare_cbdata_t spare_cb; 2883 const char *state; 2884 const char *type; 2885 const char *path = NULL; 2886 const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL, 2887 *scolor = NULL; 2888 2889 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2890 &child, &children) != 0) 2891 children = 0; 2892 2893 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2894 (uint64_t **)&vs, &vsc) == 0); 2895 2896 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 2897 2898 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) 2899 return; 2900 2901 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 2902 2903 if (isspare) { 2904 /* 2905 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 2906 * online drives. 2907 */ 2908 if (vs->vs_aux == VDEV_AUX_SPARED) 2909 state = gettext("INUSE"); 2910 else if (vs->vs_state == VDEV_STATE_HEALTHY) 2911 state = gettext("AVAIL"); 2912 } 2913 2914 /* 2915 * If '-e' is specified then top-level vdevs and their children 2916 * can be pruned if all of their leaves are healthy. 2917 */ 2918 if (cb->cb_print_unhealthy && depth > 0 && 2919 for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) { 2920 return; 2921 } 2922 2923 printf_color(health_str_to_color(state), 2924 "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth, 2925 name, state); 2926 2927 if (!isspare) { 2928 if (vs->vs_read_errors) 2929 rcolor = ANSI_RED; 2930 2931 if (vs->vs_write_errors) 2932 wcolor = ANSI_RED; 2933 2934 if (vs->vs_checksum_errors) 2935 ccolor = ANSI_RED; 2936 2937 if (vs->vs_slow_ios) 2938 scolor = ANSI_BLUE; 2939 2940 if (cb->cb_literal) { 2941 fputc(' ', stdout); 2942 printf_color(rcolor, "%5llu", 2943 (u_longlong_t)vs->vs_read_errors); 2944 fputc(' ', stdout); 2945 printf_color(wcolor, "%5llu", 2946 (u_longlong_t)vs->vs_write_errors); 2947 fputc(' ', stdout); 2948 printf_color(ccolor, "%5llu", 2949 (u_longlong_t)vs->vs_checksum_errors); 2950 } else { 2951 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 2952 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 2953 zfs_nicenum(vs->vs_checksum_errors, cbuf, 2954 sizeof (cbuf)); 2955 fputc(' ', stdout); 2956 printf_color(rcolor, "%5s", rbuf); 2957 fputc(' ', stdout); 2958 printf_color(wcolor, "%5s", wbuf); 2959 fputc(' ', stdout); 2960 printf_color(ccolor, "%5s", cbuf); 2961 } 2962 if (cb->cb_print_slow_ios) { 2963 if (children == 0) { 2964 /* Only leafs vdevs have slow IOs */ 2965 zfs_nicenum(vs->vs_slow_ios, rbuf, 2966 sizeof (rbuf)); 2967 } else { 2968 snprintf(rbuf, sizeof (rbuf), "-"); 2969 } 2970 2971 if (cb->cb_literal) 2972 printf_color(scolor, " %5llu", 2973 (u_longlong_t)vs->vs_slow_ios); 2974 else 2975 printf_color(scolor, " %5s", rbuf); 2976 } 2977 if (cb->cb_print_power) { 2978 if (children == 0) { 2979 /* Only leaf vdevs have physical slots */ 2980 switch (zpool_power_current_state(zhp, (char *) 2981 fnvlist_lookup_string(nv, 2982 ZPOOL_CONFIG_PATH))) { 2983 case 0: 2984 printf_color(ANSI_RED, " %5s", 2985 gettext("off")); 2986 break; 2987 case 1: 2988 printf(" %5s", gettext("on")); 2989 break; 2990 default: 2991 printf(" %5s", "-"); 2992 } 2993 } else { 2994 printf(" %5s", "-"); 2995 } 2996 } 2997 if (VDEV_STAT_VALID(vs_dio_verify_errors, vsc) && 2998 cb->cb_print_dio_verify) { 2999 zfs_nicenum(vs->vs_dio_verify_errors, dbuf, 3000 sizeof (dbuf)); 3001 3002 if (cb->cb_literal) 3003 printf(" %5llu", 3004 (u_longlong_t)vs->vs_dio_verify_errors); 3005 else 3006 printf(" %5s", dbuf); 3007 } 3008 } 3009 3010 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3011 ¬present) == 0) { 3012 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 3013 (void) printf(" %s %s", gettext("was"), path); 3014 } else if (vs->vs_aux != 0) { 3015 (void) printf(" "); 3016 color_start(ANSI_RED); 3017 switch (vs->vs_aux) { 3018 case VDEV_AUX_OPEN_FAILED: 3019 (void) printf(gettext("cannot open")); 3020 break; 3021 3022 case VDEV_AUX_BAD_GUID_SUM: 3023 (void) printf(gettext("missing device")); 3024 break; 3025 3026 case VDEV_AUX_NO_REPLICAS: 3027 (void) printf(gettext("insufficient replicas")); 3028 break; 3029 3030 case VDEV_AUX_VERSION_NEWER: 3031 (void) printf(gettext("newer version")); 3032 break; 3033 3034 case VDEV_AUX_UNSUP_FEAT: 3035 (void) printf(gettext("unsupported feature(s)")); 3036 break; 3037 3038 case VDEV_AUX_ASHIFT_TOO_BIG: 3039 (void) printf(gettext("unsupported minimum blocksize")); 3040 break; 3041 3042 case VDEV_AUX_SPARED: 3043 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3044 &spare_cb.cb_guid) == 0); 3045 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { 3046 if (strcmp(zpool_get_name(spare_cb.cb_zhp), 3047 zpool_get_name(zhp)) == 0) 3048 (void) printf(gettext("currently in " 3049 "use")); 3050 else 3051 (void) printf(gettext("in use by " 3052 "pool '%s'"), 3053 zpool_get_name(spare_cb.cb_zhp)); 3054 zpool_close(spare_cb.cb_zhp); 3055 } else { 3056 (void) printf(gettext("currently in use")); 3057 } 3058 break; 3059 3060 case VDEV_AUX_ERR_EXCEEDED: 3061 if (vs->vs_read_errors + vs->vs_write_errors + 3062 vs->vs_checksum_errors == 0 && children == 0 && 3063 vs->vs_slow_ios > 0) { 3064 (void) printf(gettext("too many slow I/Os")); 3065 } else { 3066 (void) printf(gettext("too many errors")); 3067 } 3068 break; 3069 3070 case VDEV_AUX_IO_FAILURE: 3071 (void) printf(gettext("experienced I/O failures")); 3072 break; 3073 3074 case VDEV_AUX_BAD_LOG: 3075 (void) printf(gettext("bad intent log")); 3076 break; 3077 3078 case VDEV_AUX_EXTERNAL: 3079 (void) printf(gettext("external device fault")); 3080 break; 3081 3082 case VDEV_AUX_SPLIT_POOL: 3083 (void) printf(gettext("split into new pool")); 3084 break; 3085 3086 case VDEV_AUX_ACTIVE: 3087 (void) printf(gettext("currently in use")); 3088 break; 3089 3090 case VDEV_AUX_CHILDREN_OFFLINE: 3091 (void) printf(gettext("all children offline")); 3092 break; 3093 3094 case VDEV_AUX_BAD_LABEL: 3095 (void) printf(gettext("invalid label")); 3096 break; 3097 3098 default: 3099 (void) printf(gettext("corrupted data")); 3100 break; 3101 } 3102 color_end(); 3103 } else if (children == 0 && !isspare && 3104 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && 3105 VDEV_STAT_VALID(vs_physical_ashift, vsc) && 3106 vs->vs_configured_ashift < vs->vs_physical_ashift) { 3107 (void) printf( 3108 gettext(" block size: %dB configured, %dB native"), 3109 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); 3110 } 3111 3112 if (vs->vs_scan_removing != 0) { 3113 (void) printf(gettext(" (removing)")); 3114 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) { 3115 (void) printf(gettext(" (non-allocating)")); 3116 } 3117 3118 /* The root vdev has the scrub/resilver stats */ 3119 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 3120 ZPOOL_CONFIG_VDEV_TREE); 3121 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS, 3122 (uint64_t **)&ps, &c); 3123 3124 /* 3125 * If you force fault a drive that's resilvering, its scan stats can 3126 * get frozen in time, giving the false impression that it's 3127 * being resilvered. That's why we check the state to see if the vdev 3128 * is healthy before reporting "resilvering" or "repairing". 3129 */ 3130 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 && 3131 vs->vs_state == VDEV_STATE_HEALTHY) { 3132 if (vs->vs_scan_processed != 0) { 3133 (void) printf(gettext(" (%s)"), 3134 (ps->pss_func == POOL_SCAN_RESILVER) ? 3135 "resilvering" : "repairing"); 3136 } else if (vs->vs_resilver_deferred) { 3137 (void) printf(gettext(" (awaiting resilver)")); 3138 } 3139 } 3140 3141 /* The top-level vdevs have the rebuild stats */ 3142 if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE && 3143 children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) { 3144 if (vs->vs_rebuild_processed != 0) { 3145 (void) printf(gettext(" (resilvering)")); 3146 } 3147 } 3148 3149 if (cb->vcdl != NULL) { 3150 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3151 printf(" "); 3152 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); 3153 } 3154 } 3155 3156 /* Display vdev initialization and trim status for leaves. */ 3157 if (children == 0) { 3158 print_status_initialize(vs, cb->cb_print_vdev_init); 3159 print_status_trim(vs, cb->cb_print_vdev_trim); 3160 } 3161 3162 (void) printf("\n"); 3163 3164 for (c = 0; c < children; c++) { 3165 uint64_t islog = B_FALSE, ishole = B_FALSE; 3166 3167 /* Don't print logs or holes here */ 3168 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3169 &islog); 3170 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 3171 &ishole); 3172 if (islog || ishole) 3173 continue; 3174 /* Only print normal classes here */ 3175 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 3176 continue; 3177 3178 /* Provide vdev_rebuild_stats to children if available */ 3179 if (vrs == NULL) { 3180 (void) nvlist_lookup_uint64_array(nv, 3181 ZPOOL_CONFIG_REBUILD_STATS, 3182 (uint64_t **)&vrs, &i); 3183 } 3184 3185 vname = zpool_vdev_name(g_zfs, zhp, child[c], 3186 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 3187 print_status_config(zhp, cb, vname, child[c], depth + 2, 3188 isspare, vrs); 3189 free(vname); 3190 } 3191 } 3192 3193 /* 3194 * Print the configuration of an exported pool. Iterate over all vdevs in the 3195 * pool, printing out the name and status for each one. 3196 */ 3197 static void 3198 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv, 3199 int depth) 3200 { 3201 nvlist_t **child; 3202 uint_t c, children; 3203 vdev_stat_t *vs; 3204 const char *type; 3205 char *vname; 3206 3207 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 3208 if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 3209 strcmp(type, VDEV_TYPE_HOLE) == 0) 3210 return; 3211 3212 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 3213 (uint64_t **)&vs, &c) == 0); 3214 3215 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name); 3216 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 3217 3218 if (vs->vs_aux != 0) { 3219 (void) printf(" "); 3220 3221 switch (vs->vs_aux) { 3222 case VDEV_AUX_OPEN_FAILED: 3223 (void) printf(gettext("cannot open")); 3224 break; 3225 3226 case VDEV_AUX_BAD_GUID_SUM: 3227 (void) printf(gettext("missing device")); 3228 break; 3229 3230 case VDEV_AUX_NO_REPLICAS: 3231 (void) printf(gettext("insufficient replicas")); 3232 break; 3233 3234 case VDEV_AUX_VERSION_NEWER: 3235 (void) printf(gettext("newer version")); 3236 break; 3237 3238 case VDEV_AUX_UNSUP_FEAT: 3239 (void) printf(gettext("unsupported feature(s)")); 3240 break; 3241 3242 case VDEV_AUX_ERR_EXCEEDED: 3243 (void) printf(gettext("too many errors")); 3244 break; 3245 3246 case VDEV_AUX_ACTIVE: 3247 (void) printf(gettext("currently in use")); 3248 break; 3249 3250 case VDEV_AUX_CHILDREN_OFFLINE: 3251 (void) printf(gettext("all children offline")); 3252 break; 3253 3254 case VDEV_AUX_BAD_LABEL: 3255 (void) printf(gettext("invalid label")); 3256 break; 3257 3258 default: 3259 (void) printf(gettext("corrupted data")); 3260 break; 3261 } 3262 } 3263 (void) printf("\n"); 3264 3265 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 3266 &child, &children) != 0) 3267 return; 3268 3269 for (c = 0; c < children; c++) { 3270 uint64_t is_log = B_FALSE; 3271 3272 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3273 &is_log); 3274 if (is_log) 3275 continue; 3276 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 3277 continue; 3278 3279 vname = zpool_vdev_name(g_zfs, NULL, child[c], 3280 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 3281 print_import_config(cb, vname, child[c], depth + 2); 3282 free(vname); 3283 } 3284 3285 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 3286 &child, &children) == 0) { 3287 (void) printf(gettext("\tcache\n")); 3288 for (c = 0; c < children; c++) { 3289 vname = zpool_vdev_name(g_zfs, NULL, child[c], 3290 cb->cb_name_flags); 3291 (void) printf("\t %s\n", vname); 3292 free(vname); 3293 } 3294 } 3295 3296 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 3297 &child, &children) == 0) { 3298 (void) printf(gettext("\tspares\n")); 3299 for (c = 0; c < children; c++) { 3300 vname = zpool_vdev_name(g_zfs, NULL, child[c], 3301 cb->cb_name_flags); 3302 (void) printf("\t %s\n", vname); 3303 free(vname); 3304 } 3305 } 3306 } 3307 3308 /* 3309 * Print specialized class vdevs. 3310 * 3311 * These are recorded as top level vdevs in the main pool child array 3312 * but with "is_log" set to 1 or an "alloc_bias" string. We use either 3313 * print_status_config() or print_import_config() to print the top level 3314 * class vdevs then any of their children (eg mirrored slogs) are printed 3315 * recursively - which works because only the top level vdev is marked. 3316 */ 3317 static void 3318 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 3319 const char *class) 3320 { 3321 uint_t c, children; 3322 nvlist_t **child; 3323 boolean_t printed = B_FALSE; 3324 3325 assert(zhp != NULL || !cb->cb_verbose); 3326 3327 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 3328 &children) != 0) 3329 return; 3330 3331 for (c = 0; c < children; c++) { 3332 uint64_t is_log = B_FALSE; 3333 const char *bias = NULL; 3334 const char *type = NULL; 3335 3336 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3337 &is_log); 3338 3339 if (is_log) { 3340 bias = (char *)VDEV_ALLOC_CLASS_LOGS; 3341 } else { 3342 (void) nvlist_lookup_string(child[c], 3343 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 3344 (void) nvlist_lookup_string(child[c], 3345 ZPOOL_CONFIG_TYPE, &type); 3346 } 3347 3348 if (bias == NULL || strcmp(bias, class) != 0) 3349 continue; 3350 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 3351 continue; 3352 3353 if (!printed) { 3354 (void) printf("\t%s\t\n", gettext(class)); 3355 printed = B_TRUE; 3356 } 3357 3358 char *name = zpool_vdev_name(g_zfs, zhp, child[c], 3359 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 3360 if (cb->cb_print_status) 3361 print_status_config(zhp, cb, name, child[c], 2, 3362 B_FALSE, NULL); 3363 else 3364 print_import_config(cb, name, child[c], 2); 3365 free(name); 3366 } 3367 } 3368 3369 /* 3370 * Display the status for the given pool. 3371 */ 3372 static int 3373 show_import(nvlist_t *config, boolean_t report_error) 3374 { 3375 uint64_t pool_state; 3376 vdev_stat_t *vs; 3377 const char *name; 3378 uint64_t guid; 3379 uint64_t hostid = 0; 3380 const char *msgid; 3381 const char *hostname = "unknown"; 3382 nvlist_t *nvroot, *nvinfo; 3383 zpool_status_t reason; 3384 zpool_errata_t errata; 3385 const char *health; 3386 uint_t vsc; 3387 const char *comment; 3388 const char *indent; 3389 char buf[2048]; 3390 status_cbdata_t cb = { 0 }; 3391 3392 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 3393 &name) == 0); 3394 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 3395 &guid) == 0); 3396 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3397 &pool_state) == 0); 3398 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3399 &nvroot) == 0); 3400 3401 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 3402 (uint64_t **)&vs, &vsc) == 0); 3403 health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 3404 3405 reason = zpool_import_status(config, &msgid, &errata); 3406 3407 /* 3408 * If we're importing using a cachefile, then we won't report any 3409 * errors unless we are in the scan phase of the import. 3410 */ 3411 if (reason != ZPOOL_STATUS_OK && !report_error) 3412 return (reason); 3413 3414 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) { 3415 indent = " "; 3416 } else { 3417 comment = NULL; 3418 indent = ""; 3419 } 3420 3421 (void) printf(gettext("%s pool: %s\n"), indent, name); 3422 (void) printf(gettext("%s id: %llu\n"), indent, (u_longlong_t)guid); 3423 (void) printf(gettext("%s state: %s"), indent, health); 3424 if (pool_state == POOL_STATE_DESTROYED) 3425 (void) printf(gettext(" (DESTROYED)")); 3426 (void) printf("\n"); 3427 3428 if (reason != ZPOOL_STATUS_OK) { 3429 (void) printf("%s", indent); 3430 printf_color(ANSI_BOLD, gettext("status: ")); 3431 } 3432 switch (reason) { 3433 case ZPOOL_STATUS_MISSING_DEV_R: 3434 case ZPOOL_STATUS_MISSING_DEV_NR: 3435 case ZPOOL_STATUS_BAD_GUID_SUM: 3436 printf_color(ANSI_YELLOW, gettext("One or more devices are " 3437 "missing from the system.\n")); 3438 break; 3439 3440 case ZPOOL_STATUS_CORRUPT_LABEL_R: 3441 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 3442 printf_color(ANSI_YELLOW, gettext("One or more devices " 3443 "contains corrupted data.\n")); 3444 break; 3445 3446 case ZPOOL_STATUS_CORRUPT_DATA: 3447 printf_color(ANSI_YELLOW, gettext("The pool data is " 3448 "corrupted.\n")); 3449 break; 3450 3451 case ZPOOL_STATUS_OFFLINE_DEV: 3452 printf_color(ANSI_YELLOW, gettext("One or more devices " 3453 "are offlined.\n")); 3454 break; 3455 3456 case ZPOOL_STATUS_CORRUPT_POOL: 3457 printf_color(ANSI_YELLOW, gettext("The pool metadata is " 3458 "corrupted.\n")); 3459 break; 3460 3461 case ZPOOL_STATUS_VERSION_OLDER: 3462 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 3463 "a legacy on-disk version.\n")); 3464 break; 3465 3466 case ZPOOL_STATUS_VERSION_NEWER: 3467 printf_color(ANSI_YELLOW, gettext("The pool is formatted using " 3468 "an incompatible version.\n")); 3469 break; 3470 3471 case ZPOOL_STATUS_FEAT_DISABLED: 3472 printf_color(ANSI_YELLOW, gettext("Some supported " 3473 "features are not enabled on the pool.\n" 3474 "\t%s(Note that they may be intentionally disabled if the\n" 3475 "\t%s'compatibility' property is set.)\n"), indent, indent); 3476 break; 3477 3478 case ZPOOL_STATUS_COMPATIBILITY_ERR: 3479 printf_color(ANSI_YELLOW, gettext("Error reading or parsing " 3480 "the file(s) indicated by the 'compatibility'\n" 3481 "\t%sproperty.\n"), indent); 3482 break; 3483 3484 case ZPOOL_STATUS_INCOMPATIBLE_FEAT: 3485 printf_color(ANSI_YELLOW, gettext("One or more features " 3486 "are enabled on the pool despite not being\n" 3487 "\t%srequested by the 'compatibility' property.\n"), 3488 indent); 3489 break; 3490 3491 case ZPOOL_STATUS_UNSUP_FEAT_READ: 3492 printf_color(ANSI_YELLOW, gettext("The pool uses the following " 3493 "feature(s) not supported on this system:\n")); 3494 color_start(ANSI_YELLOW); 3495 zpool_collect_unsup_feat(config, buf, 2048); 3496 (void) printf("%s", buf); 3497 color_end(); 3498 break; 3499 3500 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 3501 printf_color(ANSI_YELLOW, gettext("The pool can only be " 3502 "accessed in read-only mode on this system. It\n" 3503 "\t%scannot be accessed in read-write mode because it uses " 3504 "the following\n" 3505 "\t%sfeature(s) not supported on this system:\n"), 3506 indent, indent); 3507 color_start(ANSI_YELLOW); 3508 zpool_collect_unsup_feat(config, buf, 2048); 3509 (void) printf("%s", buf); 3510 color_end(); 3511 break; 3512 3513 case ZPOOL_STATUS_HOSTID_ACTIVE: 3514 printf_color(ANSI_YELLOW, gettext("The pool is currently " 3515 "imported by another system.\n")); 3516 break; 3517 3518 case ZPOOL_STATUS_HOSTID_REQUIRED: 3519 printf_color(ANSI_YELLOW, gettext("The pool has the " 3520 "multihost property on. It cannot\n" 3521 "\t%sbe safely imported when the system hostid is not " 3522 "set.\n"), indent); 3523 break; 3524 3525 case ZPOOL_STATUS_HOSTID_MISMATCH: 3526 printf_color(ANSI_YELLOW, gettext("The pool was last accessed " 3527 "by another system.\n")); 3528 break; 3529 3530 case ZPOOL_STATUS_FAULTED_DEV_R: 3531 case ZPOOL_STATUS_FAULTED_DEV_NR: 3532 printf_color(ANSI_YELLOW, gettext("One or more devices are " 3533 "faulted.\n")); 3534 break; 3535 3536 case ZPOOL_STATUS_BAD_LOG: 3537 printf_color(ANSI_YELLOW, gettext("An intent log record cannot " 3538 "be read.\n")); 3539 break; 3540 3541 case ZPOOL_STATUS_RESILVERING: 3542 case ZPOOL_STATUS_REBUILDING: 3543 printf_color(ANSI_YELLOW, gettext("One or more devices were " 3544 "being resilvered.\n")); 3545 break; 3546 3547 case ZPOOL_STATUS_ERRATA: 3548 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), 3549 errata); 3550 break; 3551 3552 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 3553 printf_color(ANSI_YELLOW, gettext("One or more devices are " 3554 "configured to use a non-native block size.\n" 3555 "\t%sExpect reduced performance.\n"), indent); 3556 break; 3557 3558 default: 3559 /* 3560 * No other status can be seen when importing pools. 3561 */ 3562 assert(reason == ZPOOL_STATUS_OK); 3563 } 3564 3565 /* 3566 * Print out an action according to the overall state of the pool. 3567 */ 3568 if (vs->vs_state != VDEV_STATE_HEALTHY || 3569 reason != ZPOOL_STATUS_ERRATA || errata != ZPOOL_ERRATA_NONE) { 3570 (void) printf("%s", indent); 3571 (void) printf(gettext("action: ")); 3572 } 3573 if (vs->vs_state == VDEV_STATE_HEALTHY) { 3574 if (reason == ZPOOL_STATUS_VERSION_OLDER || 3575 reason == ZPOOL_STATUS_FEAT_DISABLED) { 3576 (void) printf(gettext("The pool can be imported using " 3577 "its name or numeric identifier, though\n" 3578 "\t%ssome features will not be available without " 3579 "an explicit 'zpool upgrade'.\n"), indent); 3580 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) { 3581 (void) printf(gettext("The pool can be imported using " 3582 "its name or numeric\n" 3583 "\t%sidentifier, though the file(s) indicated by " 3584 "its 'compatibility'\n" 3585 "\t%sproperty cannot be parsed at this time.\n"), 3586 indent, indent); 3587 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 3588 (void) printf(gettext("The pool can be imported using " 3589 "its name or numeric identifier and\n" 3590 "\t%sthe '-f' flag.\n"), indent); 3591 } else if (reason == ZPOOL_STATUS_ERRATA) { 3592 switch (errata) { 3593 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 3594 (void) printf(gettext("The pool can be " 3595 "imported using its name or numeric " 3596 "identifier,\n" 3597 "\t%showever there is a compatibility " 3598 "issue which should be corrected\n" 3599 "\t%sby running 'zpool scrub'\n"), 3600 indent, indent); 3601 break; 3602 3603 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: 3604 (void) printf(gettext("The pool cannot be " 3605 "imported with this version of ZFS due to\n" 3606 "\t%san active asynchronous destroy. " 3607 "Revert to an earlier version\n" 3608 "\t%sand allow the destroy to complete " 3609 "before updating.\n"), indent, indent); 3610 break; 3611 3612 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 3613 (void) printf(gettext("Existing encrypted " 3614 "datasets contain an on-disk " 3615 "incompatibility, which\n" 3616 "\t%sneeds to be corrected. Backup these " 3617 "datasets to new encrypted datasets\n" 3618 "\t%sand destroy the old ones.\n"), 3619 indent, indent); 3620 break; 3621 3622 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 3623 (void) printf(gettext("Existing encrypted " 3624 "snapshots and bookmarks contain an " 3625 "on-disk\n" 3626 "\t%sincompatibility. This may cause " 3627 "on-disk corruption if they are used\n" 3628 "\t%swith 'zfs recv'. To correct the " 3629 "issue, enable the bookmark_v2 feature.\n" 3630 "\t%sNo additional action is needed if " 3631 "there are no encrypted snapshots or\n" 3632 "\t%sbookmarks. If preserving the " 3633 "encrypted snapshots and bookmarks is\n" 3634 "\t%srequired, use a non-raw send to " 3635 "backup and restore them. Alternately,\n" 3636 "\t%sthey may be removed to resolve the " 3637 "incompatibility.\n"), indent, indent, 3638 indent, indent, indent, indent); 3639 break; 3640 default: 3641 /* 3642 * All errata must contain an action message. 3643 */ 3644 assert(errata == ZPOOL_ERRATA_NONE); 3645 } 3646 } else { 3647 (void) printf(gettext("The pool can be imported using " 3648 "its name or numeric identifier.\n")); 3649 } 3650 } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 3651 (void) printf(gettext("The pool can be imported despite " 3652 "missing or damaged devices. The\n" 3653 "\t%sfault tolerance of the pool may be compromised if " 3654 "imported.\n"), indent); 3655 } else { 3656 switch (reason) { 3657 case ZPOOL_STATUS_VERSION_NEWER: 3658 (void) printf(gettext("The pool cannot be imported. " 3659 "Access the pool on a system running newer\n" 3660 "\t%ssoftware, or recreate the pool from " 3661 "backup.\n"), indent); 3662 break; 3663 case ZPOOL_STATUS_UNSUP_FEAT_READ: 3664 (void) printf(gettext("The pool cannot be imported. " 3665 "Access the pool on a system that supports\n" 3666 "\t%sthe required feature(s), or recreate the pool " 3667 "from backup.\n"), indent); 3668 break; 3669 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 3670 (void) printf(gettext("The pool cannot be imported in " 3671 "read-write mode. Import the pool with\n" 3672 "\t%s'-o readonly=on', access the pool on a system " 3673 "that supports the\n" 3674 "\t%srequired feature(s), or recreate the pool " 3675 "from backup.\n"), indent, indent); 3676 break; 3677 case ZPOOL_STATUS_MISSING_DEV_R: 3678 case ZPOOL_STATUS_MISSING_DEV_NR: 3679 case ZPOOL_STATUS_BAD_GUID_SUM: 3680 (void) printf(gettext("The pool cannot be imported. " 3681 "Attach the missing\n" 3682 "\t%sdevices and try again.\n"), indent); 3683 break; 3684 case ZPOOL_STATUS_HOSTID_ACTIVE: 3685 VERIFY0(nvlist_lookup_nvlist(config, 3686 ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); 3687 3688 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 3689 hostname = fnvlist_lookup_string(nvinfo, 3690 ZPOOL_CONFIG_MMP_HOSTNAME); 3691 3692 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 3693 hostid = fnvlist_lookup_uint64(nvinfo, 3694 ZPOOL_CONFIG_MMP_HOSTID); 3695 3696 (void) printf(gettext("The pool must be exported from " 3697 "%s (hostid=%"PRIx64")\n" 3698 "\t%sbefore it can be safely imported.\n"), 3699 hostname, hostid, indent); 3700 break; 3701 case ZPOOL_STATUS_HOSTID_REQUIRED: 3702 (void) printf(gettext("Set a unique system hostid with " 3703 "the zgenhostid(8) command.\n")); 3704 break; 3705 default: 3706 (void) printf(gettext("The pool cannot be imported due " 3707 "to damaged devices or data.\n")); 3708 } 3709 } 3710 3711 /* Print the comment attached to the pool. */ 3712 if (comment != NULL) 3713 (void) printf(gettext("comment: %s\n"), comment); 3714 3715 /* 3716 * If the state is "closed" or "can't open", and the aux state 3717 * is "corrupt data": 3718 */ 3719 if ((vs->vs_state == VDEV_STATE_CLOSED || 3720 vs->vs_state == VDEV_STATE_CANT_OPEN) && 3721 vs->vs_aux == VDEV_AUX_CORRUPT_DATA) { 3722 if (pool_state == POOL_STATE_DESTROYED) 3723 (void) printf(gettext("\t%sThe pool was destroyed, " 3724 "but can be imported using the '-Df' flags.\n"), 3725 indent); 3726 else if (pool_state != POOL_STATE_EXPORTED) 3727 (void) printf(gettext("\t%sThe pool may be active on " 3728 "another system, but can be imported using\n" 3729 "\t%sthe '-f' flag.\n"), indent, indent); 3730 } 3731 3732 if (msgid != NULL) { 3733 (void) printf(gettext("%s see: " 3734 "https://openzfs.github.io/openzfs-docs/msg/%s\n"), 3735 indent, msgid); 3736 } 3737 3738 (void) printf(gettext("%sconfig:\n\n"), indent); 3739 3740 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), 3741 VDEV_NAME_TYPE_ID); 3742 if (cb.cb_namewidth < 10) 3743 cb.cb_namewidth = 10; 3744 3745 print_import_config(&cb, name, nvroot, 0); 3746 3747 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); 3748 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 3749 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); 3750 3751 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 3752 (void) printf(gettext("\n\t%sAdditional devices are known to " 3753 "be part of this pool, though their\n" 3754 "\t%sexact configuration cannot be determined.\n"), 3755 indent, indent); 3756 } 3757 return (0); 3758 } 3759 3760 static boolean_t 3761 zfs_force_import_required(nvlist_t *config) 3762 { 3763 uint64_t state; 3764 uint64_t hostid = 0; 3765 nvlist_t *nvinfo; 3766 3767 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); 3768 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3769 3770 /* 3771 * The hostid on LOAD_INFO comes from the MOS label via 3772 * spa_tryimport(). If its not there then we're likely talking to an 3773 * older kernel, so use the top one, which will be from the label 3774 * discovered in zpool_find_import(), or if a cachefile is in use, the 3775 * local hostid. 3776 */ 3777 if (nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_HOSTID, &hostid) != 0) 3778 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, 3779 &hostid); 3780 3781 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) 3782 return (B_TRUE); 3783 3784 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { 3785 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, 3786 ZPOOL_CONFIG_MMP_STATE); 3787 3788 if (mmp_state != MMP_STATE_INACTIVE) 3789 return (B_TRUE); 3790 } 3791 3792 return (B_FALSE); 3793 } 3794 3795 /* 3796 * Perform the import for the given configuration. This passes the heavy 3797 * lifting off to zpool_import_props(), and then mounts the datasets contained 3798 * within the pool. 3799 */ 3800 static int 3801 do_import(nvlist_t *config, const char *newname, const char *mntopts, 3802 nvlist_t *props, int flags, uint_t mntthreads) 3803 { 3804 int ret = 0; 3805 int ms_status = 0; 3806 zpool_handle_t *zhp; 3807 const char *name; 3808 uint64_t version; 3809 3810 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); 3811 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); 3812 3813 if (!SPA_VERSION_IS_SUPPORTED(version)) { 3814 (void) fprintf(stderr, gettext("cannot import '%s': pool " 3815 "is formatted using an unsupported ZFS version\n"), name); 3816 return (1); 3817 } else if (zfs_force_import_required(config) && 3818 !(flags & ZFS_IMPORT_ANY_HOST)) { 3819 mmp_state_t mmp_state = MMP_STATE_INACTIVE; 3820 nvlist_t *nvinfo; 3821 3822 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 3823 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) 3824 mmp_state = fnvlist_lookup_uint64(nvinfo, 3825 ZPOOL_CONFIG_MMP_STATE); 3826 3827 if (mmp_state == MMP_STATE_ACTIVE) { 3828 const char *hostname = "<unknown>"; 3829 uint64_t hostid = 0; 3830 3831 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) 3832 hostname = fnvlist_lookup_string(nvinfo, 3833 ZPOOL_CONFIG_MMP_HOSTNAME); 3834 3835 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) 3836 hostid = fnvlist_lookup_uint64(nvinfo, 3837 ZPOOL_CONFIG_MMP_HOSTID); 3838 3839 (void) fprintf(stderr, gettext("cannot import '%s': " 3840 "pool is imported on %s (hostid: " 3841 "0x%"PRIx64")\nExport the pool on the other " 3842 "system, then run 'zpool import'.\n"), 3843 name, hostname, hostid); 3844 } else if (mmp_state == MMP_STATE_NO_HOSTID) { 3845 (void) fprintf(stderr, gettext("Cannot import '%s': " 3846 "pool has the multihost property on and the\n" 3847 "system's hostid is not set. Set a unique hostid " 3848 "with the zgenhostid(8) command.\n"), name); 3849 } else { 3850 const char *hostname = "<unknown>"; 3851 time_t timestamp = 0; 3852 uint64_t hostid = 0; 3853 3854 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTNAME)) 3855 hostname = fnvlist_lookup_string(nvinfo, 3856 ZPOOL_CONFIG_HOSTNAME); 3857 else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) 3858 hostname = fnvlist_lookup_string(config, 3859 ZPOOL_CONFIG_HOSTNAME); 3860 3861 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) 3862 timestamp = fnvlist_lookup_uint64(config, 3863 ZPOOL_CONFIG_TIMESTAMP); 3864 3865 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTID)) 3866 hostid = fnvlist_lookup_uint64(nvinfo, 3867 ZPOOL_CONFIG_HOSTID); 3868 else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) 3869 hostid = fnvlist_lookup_uint64(config, 3870 ZPOOL_CONFIG_HOSTID); 3871 3872 (void) fprintf(stderr, gettext("cannot import '%s': " 3873 "pool was previously in use from another system.\n" 3874 "Last accessed by %s (hostid=%"PRIx64") at %s" 3875 "The pool can be imported, use 'zpool import -f' " 3876 "to import the pool.\n"), name, hostname, 3877 hostid, ctime(×tamp)); 3878 } 3879 3880 return (1); 3881 } 3882 3883 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 3884 return (1); 3885 3886 if (newname != NULL) 3887 name = newname; 3888 3889 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 3890 return (1); 3891 3892 /* 3893 * Loading keys is best effort. We don't want to return immediately 3894 * if it fails but we do want to give the error to the caller. 3895 */ 3896 if (flags & ZFS_IMPORT_LOAD_KEYS && 3897 zfs_crypto_attempt_load_keys(g_zfs, name) != 0) 3898 ret = 1; 3899 3900 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3901 !(flags & ZFS_IMPORT_ONLY)) { 3902 ms_status = zpool_enable_datasets(zhp, mntopts, 0, mntthreads); 3903 if (ms_status == EZFS_SHAREFAILED) { 3904 (void) fprintf(stderr, gettext("Import was " 3905 "successful, but unable to share some datasets\n")); 3906 } else if (ms_status == EZFS_MOUNTFAILED) { 3907 (void) fprintf(stderr, gettext("Import was " 3908 "successful, but unable to mount some datasets\n")); 3909 } 3910 } 3911 3912 zpool_close(zhp); 3913 return (ret); 3914 } 3915 3916 typedef struct import_parameters { 3917 nvlist_t *ip_config; 3918 const char *ip_mntopts; 3919 nvlist_t *ip_props; 3920 int ip_flags; 3921 uint_t ip_mntthreads; 3922 int *ip_err; 3923 } import_parameters_t; 3924 3925 static void 3926 do_import_task(void *arg) 3927 { 3928 import_parameters_t *ip = arg; 3929 *ip->ip_err |= do_import(ip->ip_config, NULL, ip->ip_mntopts, 3930 ip->ip_props, ip->ip_flags, ip->ip_mntthreads); 3931 free(ip); 3932 } 3933 3934 3935 static int 3936 import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags, 3937 char *orig_name, char *new_name, importargs_t *import) 3938 { 3939 nvlist_t *config = NULL; 3940 nvlist_t *found_config = NULL; 3941 uint64_t pool_state; 3942 boolean_t pool_specified = (import->poolname != NULL || 3943 import->guid != 0); 3944 uint_t npools = 0; 3945 3946 3947 tpool_t *tp = NULL; 3948 if (import->do_all) { 3949 tp = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), 3950 0, NULL); 3951 } 3952 3953 /* 3954 * At this point we have a list of import candidate configs. Even if 3955 * we were searching by pool name or guid, we still need to 3956 * post-process the list to deal with pool state and possible 3957 * duplicate names. 3958 */ 3959 int err = 0; 3960 nvpair_t *elem = NULL; 3961 boolean_t first = B_TRUE; 3962 if (!pool_specified && import->do_all) { 3963 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) 3964 npools++; 3965 } 3966 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3967 3968 verify(nvpair_value_nvlist(elem, &config) == 0); 3969 3970 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3971 &pool_state) == 0); 3972 if (!import->do_destroyed && 3973 pool_state == POOL_STATE_DESTROYED) 3974 continue; 3975 if (import->do_destroyed && 3976 pool_state != POOL_STATE_DESTROYED) 3977 continue; 3978 3979 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 3980 import->policy) == 0); 3981 3982 if (!pool_specified) { 3983 if (first) 3984 first = B_FALSE; 3985 else if (!import->do_all) 3986 (void) fputc('\n', stdout); 3987 3988 if (import->do_all) { 3989 import_parameters_t *ip = safe_malloc( 3990 sizeof (import_parameters_t)); 3991 3992 ip->ip_config = config; 3993 ip->ip_mntopts = mntopts; 3994 ip->ip_props = props; 3995 ip->ip_flags = flags; 3996 ip->ip_mntthreads = mount_tp_nthr / npools; 3997 ip->ip_err = &err; 3998 3999 (void) tpool_dispatch(tp, do_import_task, 4000 (void *)ip); 4001 } else { 4002 /* 4003 * If we're importing from cachefile, then 4004 * we don't want to report errors until we 4005 * are in the scan phase of the import. If 4006 * we get an error, then we return that error 4007 * to invoke the scan phase. 4008 */ 4009 if (import->cachefile && !import->scan) 4010 err = show_import(config, B_FALSE); 4011 else 4012 (void) show_import(config, B_TRUE); 4013 } 4014 } else if (import->poolname != NULL) { 4015 const char *name; 4016 4017 /* 4018 * We are searching for a pool based on name. 4019 */ 4020 verify(nvlist_lookup_string(config, 4021 ZPOOL_CONFIG_POOL_NAME, &name) == 0); 4022 4023 if (strcmp(name, import->poolname) == 0) { 4024 if (found_config != NULL) { 4025 (void) fprintf(stderr, gettext( 4026 "cannot import '%s': more than " 4027 "one matching pool\n"), 4028 import->poolname); 4029 (void) fprintf(stderr, gettext( 4030 "import by numeric ID instead\n")); 4031 err = B_TRUE; 4032 } 4033 found_config = config; 4034 } 4035 } else { 4036 uint64_t guid; 4037 4038 /* 4039 * Search for a pool by guid. 4040 */ 4041 verify(nvlist_lookup_uint64(config, 4042 ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 4043 4044 if (guid == import->guid) 4045 found_config = config; 4046 } 4047 } 4048 if (import->do_all) { 4049 tpool_wait(tp); 4050 tpool_destroy(tp); 4051 } 4052 4053 /* 4054 * If we were searching for a specific pool, verify that we found a 4055 * pool, and then do the import. 4056 */ 4057 if (pool_specified && err == 0) { 4058 if (found_config == NULL) { 4059 (void) fprintf(stderr, gettext("cannot import '%s': " 4060 "no such pool available\n"), orig_name); 4061 err = B_TRUE; 4062 } else { 4063 err |= do_import(found_config, new_name, 4064 mntopts, props, flags, mount_tp_nthr); 4065 } 4066 } 4067 4068 /* 4069 * If we were just looking for pools, report an error if none were 4070 * found. 4071 */ 4072 if (!pool_specified && first) 4073 (void) fprintf(stderr, 4074 gettext("no pools available to import\n")); 4075 return (err); 4076 } 4077 4078 typedef struct target_exists_args { 4079 const char *poolname; 4080 uint64_t poolguid; 4081 } target_exists_args_t; 4082 4083 static int 4084 name_or_guid_exists(zpool_handle_t *zhp, void *data) 4085 { 4086 target_exists_args_t *args = data; 4087 nvlist_t *config = zpool_get_config(zhp, NULL); 4088 int found = 0; 4089 4090 if (config == NULL) 4091 return (0); 4092 4093 if (args->poolname != NULL) { 4094 const char *pool_name; 4095 4096 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 4097 &pool_name) == 0); 4098 if (strcmp(pool_name, args->poolname) == 0) 4099 found = 1; 4100 } else { 4101 uint64_t pool_guid; 4102 4103 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 4104 &pool_guid) == 0); 4105 if (pool_guid == args->poolguid) 4106 found = 1; 4107 } 4108 zpool_close(zhp); 4109 4110 return (found); 4111 } 4112 /* 4113 * zpool checkpoint <pool> 4114 * checkpoint --discard <pool> 4115 * 4116 * -d Discard the checkpoint from a checkpointed 4117 * --discard pool. 4118 * 4119 * -w Wait for discarding a checkpoint to complete. 4120 * --wait 4121 * 4122 * Checkpoints the specified pool, by taking a "snapshot" of its 4123 * current state. A pool can only have one checkpoint at a time. 4124 */ 4125 int 4126 zpool_do_checkpoint(int argc, char **argv) 4127 { 4128 boolean_t discard, wait; 4129 char *pool; 4130 zpool_handle_t *zhp; 4131 int c, err; 4132 4133 struct option long_options[] = { 4134 {"discard", no_argument, NULL, 'd'}, 4135 {"wait", no_argument, NULL, 'w'}, 4136 {0, 0, 0, 0} 4137 }; 4138 4139 discard = B_FALSE; 4140 wait = B_FALSE; 4141 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) { 4142 switch (c) { 4143 case 'd': 4144 discard = B_TRUE; 4145 break; 4146 case 'w': 4147 wait = B_TRUE; 4148 break; 4149 case '?': 4150 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4151 optopt); 4152 usage(B_FALSE); 4153 } 4154 } 4155 4156 if (wait && !discard) { 4157 (void) fprintf(stderr, gettext("--wait only valid when " 4158 "--discard also specified\n")); 4159 usage(B_FALSE); 4160 } 4161 4162 argc -= optind; 4163 argv += optind; 4164 4165 if (argc < 1) { 4166 (void) fprintf(stderr, gettext("missing pool argument\n")); 4167 usage(B_FALSE); 4168 } 4169 4170 if (argc > 1) { 4171 (void) fprintf(stderr, gettext("too many arguments\n")); 4172 usage(B_FALSE); 4173 } 4174 4175 pool = argv[0]; 4176 4177 if ((zhp = zpool_open(g_zfs, pool)) == NULL) { 4178 /* As a special case, check for use of '/' in the name */ 4179 if (strchr(pool, '/') != NULL) 4180 (void) fprintf(stderr, gettext("'zpool checkpoint' " 4181 "doesn't work on datasets. To save the state " 4182 "of a dataset from a specific point in time " 4183 "please use 'zfs snapshot'\n")); 4184 return (1); 4185 } 4186 4187 if (discard) { 4188 err = (zpool_discard_checkpoint(zhp) != 0); 4189 if (err == 0 && wait) 4190 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD); 4191 } else { 4192 err = (zpool_checkpoint(zhp) != 0); 4193 } 4194 4195 zpool_close(zhp); 4196 4197 return (err); 4198 } 4199 4200 #define CHECKPOINT_OPT 1024 4201 4202 /* 4203 * zpool prefetch <type> [<type opts>] <pool> 4204 * 4205 * Prefetchs a particular type of data in the specified pool. 4206 */ 4207 int 4208 zpool_do_prefetch(int argc, char **argv) 4209 { 4210 int c; 4211 char *poolname; 4212 char *typestr = NULL; 4213 zpool_prefetch_type_t type; 4214 zpool_handle_t *zhp; 4215 int err = 0; 4216 4217 while ((c = getopt(argc, argv, "t:")) != -1) { 4218 switch (c) { 4219 case 't': 4220 typestr = optarg; 4221 break; 4222 case ':': 4223 (void) fprintf(stderr, gettext("missing argument for " 4224 "'%c' option\n"), optopt); 4225 usage(B_FALSE); 4226 break; 4227 case '?': 4228 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4229 optopt); 4230 usage(B_FALSE); 4231 } 4232 } 4233 argc -= optind; 4234 argv += optind; 4235 4236 if (argc < 1) { 4237 (void) fprintf(stderr, gettext("missing pool name argument\n")); 4238 usage(B_FALSE); 4239 } 4240 4241 if (argc > 1) { 4242 (void) fprintf(stderr, gettext("too many arguments\n")); 4243 usage(B_FALSE); 4244 } 4245 4246 poolname = argv[0]; 4247 4248 argc--; 4249 argv++; 4250 4251 if (strcmp(typestr, "ddt") == 0) { 4252 type = ZPOOL_PREFETCH_DDT; 4253 } else { 4254 (void) fprintf(stderr, gettext("unsupported prefetch type\n")); 4255 usage(B_FALSE); 4256 } 4257 4258 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 4259 return (1); 4260 4261 err = zpool_prefetch(zhp, type); 4262 4263 zpool_close(zhp); 4264 4265 return (err); 4266 } 4267 4268 /* 4269 * zpool import [-d dir] [-D] 4270 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 4271 * [-d dir | -c cachefile | -s] [-f] -a 4272 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] 4273 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id> 4274 * [newpool] 4275 * 4276 * -c Read pool information from a cachefile instead of searching 4277 * devices. If importing from a cachefile config fails, then 4278 * fallback to searching for devices only in the directories that 4279 * exist in the cachefile. 4280 * 4281 * -d Scan in a specific directory, other than /dev/. More than 4282 * one directory can be specified using multiple '-d' options. 4283 * 4284 * -D Scan for previously destroyed pools or import all or only 4285 * specified destroyed pools. 4286 * 4287 * -R Temporarily import the pool, with all mountpoints relative to 4288 * the given root. The pool will remain exported when the machine 4289 * is rebooted. 4290 * 4291 * -V Import even in the presence of faulted vdevs. This is an 4292 * intentionally undocumented option for testing purposes, and 4293 * treats the pool configuration as complete, leaving any bad 4294 * vdevs in the FAULTED state. In other words, it does verbatim 4295 * import. 4296 * 4297 * -f Force import, even if it appears that the pool is active. 4298 * 4299 * -F Attempt rewind if necessary. 4300 * 4301 * -n See if rewind would work, but don't actually rewind. 4302 * 4303 * -N Import the pool but don't mount datasets. 4304 * 4305 * -T Specify a starting txg to use for import. This option is 4306 * intentionally undocumented option for testing purposes. 4307 * 4308 * -a Import all pools found. 4309 * 4310 * -l Load encryption keys while importing. 4311 * 4312 * -o Set property=value and/or temporary mount options (without '='). 4313 * 4314 * -s Scan using the default search path, the libblkid cache will 4315 * not be consulted. 4316 * 4317 * --rewind-to-checkpoint 4318 * Import the pool and revert back to the checkpoint. 4319 * 4320 * The import command scans for pools to import, and import pools based on pool 4321 * name and GUID. The pool can also be renamed as part of the import process. 4322 */ 4323 int 4324 zpool_do_import(int argc, char **argv) 4325 { 4326 char **searchdirs = NULL; 4327 char *env, *envdup = NULL; 4328 int nsearch = 0; 4329 int c; 4330 int err = 0; 4331 nvlist_t *pools = NULL; 4332 boolean_t do_all = B_FALSE; 4333 boolean_t do_destroyed = B_FALSE; 4334 char *mntopts = NULL; 4335 uint64_t searchguid = 0; 4336 char *searchname = NULL; 4337 char *propval; 4338 nvlist_t *policy = NULL; 4339 nvlist_t *props = NULL; 4340 int flags = ZFS_IMPORT_NORMAL; 4341 uint32_t rewind_policy = ZPOOL_NO_REWIND; 4342 boolean_t dryrun = B_FALSE; 4343 boolean_t do_rewind = B_FALSE; 4344 boolean_t xtreme_rewind = B_FALSE; 4345 boolean_t do_scan = B_FALSE; 4346 boolean_t pool_exists = B_FALSE; 4347 uint64_t txg = -1ULL; 4348 char *cachefile = NULL; 4349 importargs_t idata = { 0 }; 4350 char *endptr; 4351 4352 struct option long_options[] = { 4353 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, 4354 {0, 0, 0, 0} 4355 }; 4356 4357 /* check options */ 4358 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", 4359 long_options, NULL)) != -1) { 4360 switch (c) { 4361 case 'a': 4362 do_all = B_TRUE; 4363 break; 4364 case 'c': 4365 cachefile = optarg; 4366 break; 4367 case 'd': 4368 searchdirs = safe_realloc(searchdirs, 4369 (nsearch + 1) * sizeof (char *)); 4370 searchdirs[nsearch++] = optarg; 4371 break; 4372 case 'D': 4373 do_destroyed = B_TRUE; 4374 break; 4375 case 'f': 4376 flags |= ZFS_IMPORT_ANY_HOST; 4377 break; 4378 case 'F': 4379 do_rewind = B_TRUE; 4380 break; 4381 case 'l': 4382 flags |= ZFS_IMPORT_LOAD_KEYS; 4383 break; 4384 case 'm': 4385 flags |= ZFS_IMPORT_MISSING_LOG; 4386 break; 4387 case 'n': 4388 dryrun = B_TRUE; 4389 break; 4390 case 'N': 4391 flags |= ZFS_IMPORT_ONLY; 4392 break; 4393 case 'o': 4394 if ((propval = strchr(optarg, '=')) != NULL) { 4395 *propval = '\0'; 4396 propval++; 4397 if (add_prop_list(optarg, propval, 4398 &props, B_TRUE)) 4399 goto error; 4400 } else { 4401 mntopts = optarg; 4402 } 4403 break; 4404 case 'R': 4405 if (add_prop_list(zpool_prop_to_name( 4406 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 4407 goto error; 4408 if (add_prop_list_default(zpool_prop_to_name( 4409 ZPOOL_PROP_CACHEFILE), "none", &props)) 4410 goto error; 4411 break; 4412 case 's': 4413 do_scan = B_TRUE; 4414 break; 4415 case 't': 4416 flags |= ZFS_IMPORT_TEMP_NAME; 4417 if (add_prop_list_default(zpool_prop_to_name( 4418 ZPOOL_PROP_CACHEFILE), "none", &props)) 4419 goto error; 4420 break; 4421 4422 case 'T': 4423 errno = 0; 4424 txg = strtoull(optarg, &endptr, 0); 4425 if (errno != 0 || *endptr != '\0') { 4426 (void) fprintf(stderr, 4427 gettext("invalid txg value\n")); 4428 usage(B_FALSE); 4429 } 4430 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 4431 break; 4432 case 'V': 4433 flags |= ZFS_IMPORT_VERBATIM; 4434 break; 4435 case 'X': 4436 xtreme_rewind = B_TRUE; 4437 break; 4438 case CHECKPOINT_OPT: 4439 flags |= ZFS_IMPORT_CHECKPOINT; 4440 break; 4441 case ':': 4442 (void) fprintf(stderr, gettext("missing argument for " 4443 "'%c' option\n"), optopt); 4444 usage(B_FALSE); 4445 break; 4446 case '?': 4447 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4448 optopt); 4449 usage(B_FALSE); 4450 } 4451 } 4452 4453 argc -= optind; 4454 argv += optind; 4455 4456 if (cachefile && nsearch != 0) { 4457 (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 4458 usage(B_FALSE); 4459 } 4460 4461 if (cachefile && do_scan) { 4462 (void) fprintf(stderr, gettext("-c is incompatible with -s\n")); 4463 usage(B_FALSE); 4464 } 4465 4466 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { 4467 (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); 4468 usage(B_FALSE); 4469 } 4470 4471 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { 4472 (void) fprintf(stderr, gettext("-l is only meaningful during " 4473 "an import\n")); 4474 usage(B_FALSE); 4475 } 4476 4477 if ((dryrun || xtreme_rewind) && !do_rewind) { 4478 (void) fprintf(stderr, 4479 gettext("-n or -X only meaningful with -F\n")); 4480 usage(B_FALSE); 4481 } 4482 if (dryrun) 4483 rewind_policy = ZPOOL_TRY_REWIND; 4484 else if (do_rewind) 4485 rewind_policy = ZPOOL_DO_REWIND; 4486 if (xtreme_rewind) 4487 rewind_policy |= ZPOOL_EXTREME_REWIND; 4488 4489 /* In the future, we can capture further policy and include it here */ 4490 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 4491 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || 4492 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 4493 rewind_policy) != 0) 4494 goto error; 4495 4496 /* check argument count */ 4497 if (do_all) { 4498 if (argc != 0) { 4499 (void) fprintf(stderr, gettext("too many arguments\n")); 4500 usage(B_FALSE); 4501 } 4502 } else { 4503 if (argc > 2) { 4504 (void) fprintf(stderr, gettext("too many arguments\n")); 4505 usage(B_FALSE); 4506 } 4507 } 4508 4509 /* 4510 * Check for the effective uid. We do this explicitly here because 4511 * otherwise any attempt to discover pools will silently fail. 4512 */ 4513 if (argc == 0 && geteuid() != 0) { 4514 (void) fprintf(stderr, gettext("cannot " 4515 "discover pools: permission denied\n")); 4516 4517 free(searchdirs); 4518 nvlist_free(props); 4519 nvlist_free(policy); 4520 return (1); 4521 } 4522 4523 /* 4524 * Depending on the arguments given, we do one of the following: 4525 * 4526 * <none> Iterate through all pools and display information about 4527 * each one. 4528 * 4529 * -a Iterate through all pools and try to import each one. 4530 * 4531 * <id> Find the pool that corresponds to the given GUID/pool 4532 * name and import that one. 4533 * 4534 * -D Above options applies only to destroyed pools. 4535 */ 4536 if (argc != 0) { 4537 char *endptr; 4538 4539 errno = 0; 4540 searchguid = strtoull(argv[0], &endptr, 10); 4541 if (errno != 0 || *endptr != '\0') { 4542 searchname = argv[0]; 4543 searchguid = 0; 4544 } 4545 4546 /* 4547 * User specified a name or guid. Ensure it's unique. 4548 */ 4549 target_exists_args_t search = {searchname, searchguid}; 4550 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); 4551 } 4552 4553 /* 4554 * Check the environment for the preferred search path. 4555 */ 4556 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { 4557 char *dir, *tmp = NULL; 4558 4559 envdup = strdup(env); 4560 4561 for (dir = strtok_r(envdup, ":", &tmp); 4562 dir != NULL; 4563 dir = strtok_r(NULL, ":", &tmp)) { 4564 searchdirs = safe_realloc(searchdirs, 4565 (nsearch + 1) * sizeof (char *)); 4566 searchdirs[nsearch++] = dir; 4567 } 4568 } 4569 4570 idata.path = searchdirs; 4571 idata.paths = nsearch; 4572 idata.poolname = searchname; 4573 idata.guid = searchguid; 4574 idata.cachefile = cachefile; 4575 idata.scan = do_scan; 4576 idata.policy = policy; 4577 idata.do_destroyed = do_destroyed; 4578 idata.do_all = do_all; 4579 4580 libpc_handle_t lpch = { 4581 .lpc_lib_handle = g_zfs, 4582 .lpc_ops = &libzfs_config_ops, 4583 .lpc_printerr = B_TRUE 4584 }; 4585 pools = zpool_search_import(&lpch, &idata); 4586 4587 if (pools != NULL && pool_exists && 4588 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 4589 (void) fprintf(stderr, gettext("cannot import '%s': " 4590 "a pool with that name already exists\n"), 4591 argv[0]); 4592 (void) fprintf(stderr, gettext("use the form '%s " 4593 "<pool | id> <newpool>' to give it a new name\n"), 4594 "zpool import"); 4595 err = 1; 4596 } else if (pools == NULL && pool_exists) { 4597 (void) fprintf(stderr, gettext("cannot import '%s': " 4598 "a pool with that name is already created/imported,\n"), 4599 argv[0]); 4600 (void) fprintf(stderr, gettext("and no additional pools " 4601 "with that name were found\n")); 4602 err = 1; 4603 } else if (pools == NULL) { 4604 if (argc != 0) { 4605 (void) fprintf(stderr, gettext("cannot import '%s': " 4606 "no such pool available\n"), argv[0]); 4607 } 4608 err = 1; 4609 } 4610 4611 if (err == 1) { 4612 free(searchdirs); 4613 free(envdup); 4614 nvlist_free(policy); 4615 nvlist_free(pools); 4616 nvlist_free(props); 4617 return (1); 4618 } 4619 4620 err = import_pools(pools, props, mntopts, flags, 4621 argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL, &idata); 4622 4623 /* 4624 * If we're using the cachefile and we failed to import, then 4625 * fallback to scanning the directory for pools that match 4626 * those in the cachefile. 4627 */ 4628 if (err != 0 && cachefile != NULL) { 4629 (void) printf(gettext("cachefile import failed, retrying\n")); 4630 4631 /* 4632 * We use the scan flag to gather the directories that exist 4633 * in the cachefile. If we need to fallback to searching for 4634 * the pool config, we will only search devices in these 4635 * directories. 4636 */ 4637 idata.scan = B_TRUE; 4638 nvlist_free(pools); 4639 pools = zpool_search_import(&lpch, &idata); 4640 4641 err = import_pools(pools, props, mntopts, flags, 4642 argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL, 4643 &idata); 4644 } 4645 4646 error: 4647 nvlist_free(props); 4648 nvlist_free(pools); 4649 nvlist_free(policy); 4650 free(searchdirs); 4651 free(envdup); 4652 4653 return (err ? 1 : 0); 4654 } 4655 4656 /* 4657 * zpool sync [-f] [pool] ... 4658 * 4659 * -f (undocumented) force uberblock (and config including zpool cache file) 4660 * update. 4661 * 4662 * Sync the specified pool(s). 4663 * Without arguments "zpool sync" will sync all pools. 4664 * This command initiates TXG sync(s) and will return after the TXG(s) commit. 4665 * 4666 */ 4667 static int 4668 zpool_do_sync(int argc, char **argv) 4669 { 4670 int ret; 4671 boolean_t force = B_FALSE; 4672 4673 /* check options */ 4674 while ((ret = getopt(argc, argv, "f")) != -1) { 4675 switch (ret) { 4676 case 'f': 4677 force = B_TRUE; 4678 break; 4679 case '?': 4680 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4681 optopt); 4682 usage(B_FALSE); 4683 } 4684 } 4685 4686 argc -= optind; 4687 argv += optind; 4688 4689 /* if argc == 0 we will execute zpool_sync_one on all pools */ 4690 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, 4691 B_FALSE, zpool_sync_one, &force); 4692 4693 return (ret); 4694 } 4695 4696 typedef struct iostat_cbdata { 4697 uint64_t cb_flags; 4698 int cb_namewidth; 4699 int cb_iteration; 4700 boolean_t cb_verbose; 4701 boolean_t cb_literal; 4702 boolean_t cb_scripted; 4703 zpool_list_t *cb_list; 4704 vdev_cmd_data_list_t *vcdl; 4705 vdev_cbdata_t cb_vdevs; 4706 } iostat_cbdata_t; 4707 4708 /* iostat labels */ 4709 typedef struct name_and_columns { 4710 const char *name; /* Column name */ 4711 unsigned int columns; /* Center name to this number of columns */ 4712 } name_and_columns_t; 4713 4714 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */ 4715 4716 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = 4717 { 4718 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, 4719 {NULL}}, 4720 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 4721 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1}, 4722 {NULL}}, 4723 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, 4724 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, 4725 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}}, 4726 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, 4727 {"asyncq_wait", 2}, {NULL}}, 4728 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, 4729 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, 4730 {"trim", 2}, {"rebuild", 2}, {NULL}}, 4731 }; 4732 4733 /* Shorthand - if "columns" field not set, default to 1 column */ 4734 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = 4735 { 4736 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, 4737 {"write"}, {NULL}}, 4738 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 4739 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"}, 4740 {NULL}}, 4741 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, 4742 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, 4743 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}}, 4744 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, 4745 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"}, 4746 {NULL}}, 4747 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, 4748 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, 4749 {"ind"}, {"agg"}, {NULL}}, 4750 }; 4751 4752 static const char *histo_to_title[] = { 4753 [IOS_L_HISTO] = "latency", 4754 [IOS_RQ_HISTO] = "req_size", 4755 }; 4756 4757 /* 4758 * Return the number of labels in a null-terminated name_and_columns_t 4759 * array. 4760 * 4761 */ 4762 static unsigned int 4763 label_array_len(const name_and_columns_t *labels) 4764 { 4765 int i = 0; 4766 4767 while (labels[i].name) 4768 i++; 4769 4770 return (i); 4771 } 4772 4773 /* 4774 * Return the number of strings in a null-terminated string array. 4775 * For example: 4776 * 4777 * const char foo[] = {"bar", "baz", NULL} 4778 * 4779 * returns 2 4780 */ 4781 static uint64_t 4782 str_array_len(const char *array[]) 4783 { 4784 uint64_t i = 0; 4785 while (array[i]) 4786 i++; 4787 4788 return (i); 4789 } 4790 4791 4792 /* 4793 * Return a default column width for default/latency/queue columns. This does 4794 * not include histograms, which have their columns autosized. 4795 */ 4796 static unsigned int 4797 default_column_width(iostat_cbdata_t *cb, enum iostat_type type) 4798 { 4799 unsigned long column_width = 5; /* Normal niceprint */ 4800 static unsigned long widths[] = { 4801 /* 4802 * Choose some sane default column sizes for printing the 4803 * raw numbers. 4804 */ 4805 [IOS_DEFAULT] = 15, /* 1PB capacity */ 4806 [IOS_LATENCY] = 10, /* 1B ns = 10sec */ 4807 [IOS_QUEUES] = 6, /* 1M queue entries */ 4808 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */ 4809 [IOS_RQ_HISTO] = 6, /* 1M queue entries */ 4810 }; 4811 4812 if (cb->cb_literal) 4813 column_width = widths[type]; 4814 4815 return (column_width); 4816 } 4817 4818 /* 4819 * Print the column labels, i.e: 4820 * 4821 * capacity operations bandwidth 4822 * alloc free read write read write ... 4823 * 4824 * If force_column_width is set, use it for the column width. If not set, use 4825 * the default column width. 4826 */ 4827 static void 4828 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, 4829 const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) 4830 { 4831 int i, idx, s; 4832 int text_start, rw_column_width, spaces_to_end; 4833 uint64_t flags = cb->cb_flags; 4834 uint64_t f; 4835 unsigned int column_width = force_column_width; 4836 4837 /* For each bit set in flags */ 4838 for (f = flags; f; f &= ~(1ULL << idx)) { 4839 idx = lowbit64(f) - 1; 4840 if (!force_column_width) 4841 column_width = default_column_width(cb, idx); 4842 /* Print our top labels centered over "read write" label. */ 4843 for (i = 0; i < label_array_len(labels[idx]); i++) { 4844 const char *name = labels[idx][i].name; 4845 /* 4846 * We treat labels[][].columns == 0 as shorthand 4847 * for one column. It makes writing out the label 4848 * tables more concise. 4849 */ 4850 unsigned int columns = MAX(1, labels[idx][i].columns); 4851 unsigned int slen = strlen(name); 4852 4853 rw_column_width = (column_width * columns) + 4854 (2 * (columns - 1)); 4855 4856 text_start = (int)((rw_column_width) / columns - 4857 slen / columns); 4858 if (text_start < 0) 4859 text_start = 0; 4860 4861 printf(" "); /* Two spaces between columns */ 4862 4863 /* Space from beginning of column to label */ 4864 for (s = 0; s < text_start; s++) 4865 printf(" "); 4866 4867 printf("%s", name); 4868 4869 /* Print space after label to end of column */ 4870 spaces_to_end = rw_column_width - text_start - slen; 4871 if (spaces_to_end < 0) 4872 spaces_to_end = 0; 4873 4874 for (s = 0; s < spaces_to_end; s++) 4875 printf(" "); 4876 } 4877 } 4878 } 4879 4880 4881 /* 4882 * print_cmd_columns - Print custom column titles from -c 4883 * 4884 * If the user specified the "zpool status|iostat -c" then print their custom 4885 * column titles in the header. For example, print_cmd_columns() would print 4886 * the " col1 col2" part of this: 4887 * 4888 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' 4889 * ... 4890 * capacity operations bandwidth 4891 * pool alloc free read write read write col1 col2 4892 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 4893 * mypool 269K 1008M 0 0 107 946 4894 * mirror 269K 1008M 0 0 107 946 4895 * sdb - - 0 0 102 473 val1 val2 4896 * sdc - - 0 0 5 473 val1 val2 4897 * ---------- ----- ----- ----- ----- ----- ----- ---- ---- 4898 */ 4899 static void 4900 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) 4901 { 4902 int i, j; 4903 vdev_cmd_data_t *data = &vcdl->data[0]; 4904 4905 if (vcdl->count == 0 || data == NULL) 4906 return; 4907 4908 /* 4909 * Each vdev cmd should have the same column names unless the user did 4910 * something weird with their cmd. Just take the column names from the 4911 * first vdev and assume it works for all of them. 4912 */ 4913 for (i = 0; i < vcdl->uniq_cols_cnt; i++) { 4914 printf(" "); 4915 if (use_dashes) { 4916 for (j = 0; j < vcdl->uniq_cols_width[i]; j++) 4917 printf("-"); 4918 } else { 4919 printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i], 4920 vcdl->uniq_cols[i]); 4921 } 4922 } 4923 } 4924 4925 4926 /* 4927 * Utility function to print out a line of dashes like: 4928 * 4929 * -------------------------------- ----- ----- ----- ----- ----- 4930 * 4931 * ...or a dashed named-row line like: 4932 * 4933 * logs - - - - - 4934 * 4935 * @cb: iostat data 4936 * 4937 * @force_column_width If non-zero, use the value as the column width. 4938 * Otherwise use the default column widths. 4939 * 4940 * @name: Print a dashed named-row line starting 4941 * with @name. Otherwise, print a regular 4942 * dashed line. 4943 */ 4944 static void 4945 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, 4946 const char *name) 4947 { 4948 int i; 4949 unsigned int namewidth; 4950 uint64_t flags = cb->cb_flags; 4951 uint64_t f; 4952 int idx; 4953 const name_and_columns_t *labels; 4954 const char *title; 4955 4956 4957 if (cb->cb_flags & IOS_ANYHISTO_M) { 4958 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 4959 } else if (cb->cb_vdevs.cb_names_count) { 4960 title = "vdev"; 4961 } else { 4962 title = "pool"; 4963 } 4964 4965 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 4966 name ? strlen(name) : 0); 4967 4968 4969 if (name) { 4970 printf("%-*s", namewidth, name); 4971 } else { 4972 for (i = 0; i < namewidth; i++) 4973 (void) printf("-"); 4974 } 4975 4976 /* For each bit in flags */ 4977 for (f = flags; f; f &= ~(1ULL << idx)) { 4978 unsigned int column_width; 4979 idx = lowbit64(f) - 1; 4980 if (force_column_width) 4981 column_width = force_column_width; 4982 else 4983 column_width = default_column_width(cb, idx); 4984 4985 labels = iostat_bottom_labels[idx]; 4986 for (i = 0; i < label_array_len(labels); i++) { 4987 if (name) 4988 printf(" %*s-", column_width - 1, " "); 4989 else 4990 printf(" %.*s", column_width, 4991 "--------------------"); 4992 } 4993 } 4994 } 4995 4996 4997 static void 4998 print_iostat_separator_impl(iostat_cbdata_t *cb, 4999 unsigned int force_column_width) 5000 { 5001 print_iostat_dashes(cb, force_column_width, NULL); 5002 } 5003 5004 static void 5005 print_iostat_separator(iostat_cbdata_t *cb) 5006 { 5007 print_iostat_separator_impl(cb, 0); 5008 } 5009 5010 static void 5011 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, 5012 const char *histo_vdev_name) 5013 { 5014 unsigned int namewidth; 5015 const char *title; 5016 5017 color_start(ANSI_BOLD); 5018 5019 if (cb->cb_flags & IOS_ANYHISTO_M) { 5020 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; 5021 } else if (cb->cb_vdevs.cb_names_count) { 5022 title = "vdev"; 5023 } else { 5024 title = "pool"; 5025 } 5026 5027 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), 5028 histo_vdev_name ? strlen(histo_vdev_name) : 0); 5029 5030 if (histo_vdev_name) 5031 printf("%-*s", namewidth, histo_vdev_name); 5032 else 5033 printf("%*s", namewidth, ""); 5034 5035 5036 print_iostat_labels(cb, force_column_width, iostat_top_labels); 5037 printf("\n"); 5038 5039 printf("%-*s", namewidth, title); 5040 5041 print_iostat_labels(cb, force_column_width, iostat_bottom_labels); 5042 if (cb->vcdl != NULL) 5043 print_cmd_columns(cb->vcdl, 0); 5044 5045 printf("\n"); 5046 5047 print_iostat_separator_impl(cb, force_column_width); 5048 5049 if (cb->vcdl != NULL) 5050 print_cmd_columns(cb->vcdl, 1); 5051 5052 color_end(); 5053 5054 printf("\n"); 5055 } 5056 5057 static void 5058 print_iostat_header(iostat_cbdata_t *cb) 5059 { 5060 print_iostat_header_impl(cb, 0, NULL); 5061 } 5062 5063 /* 5064 * Prints a size string (i.e. 120M) with the suffix ("M") colored 5065 * by order of magnitude. Uses column_size to add padding. 5066 */ 5067 static void 5068 print_stat_color(const char *statbuf, unsigned int column_size) 5069 { 5070 fputs(" ", stdout); 5071 size_t len = strlen(statbuf); 5072 while (len < column_size) { 5073 fputc(' ', stdout); 5074 column_size--; 5075 } 5076 if (*statbuf == '0') { 5077 color_start(ANSI_GRAY); 5078 fputc('0', stdout); 5079 } else { 5080 for (; *statbuf; statbuf++) { 5081 if (*statbuf == 'K') color_start(ANSI_GREEN); 5082 else if (*statbuf == 'M') color_start(ANSI_YELLOW); 5083 else if (*statbuf == 'G') color_start(ANSI_RED); 5084 else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE); 5085 else if (*statbuf == 'P') color_start(ANSI_MAGENTA); 5086 else if (*statbuf == 'E') color_start(ANSI_CYAN); 5087 fputc(*statbuf, stdout); 5088 if (--column_size <= 0) 5089 break; 5090 } 5091 } 5092 color_end(); 5093 } 5094 5095 /* 5096 * Display a single statistic. 5097 */ 5098 static void 5099 print_one_stat(uint64_t value, enum zfs_nicenum_format format, 5100 unsigned int column_size, boolean_t scripted) 5101 { 5102 char buf[64]; 5103 5104 zfs_nicenum_format(value, buf, sizeof (buf), format); 5105 5106 if (scripted) 5107 printf("\t%s", buf); 5108 else 5109 print_stat_color(buf, column_size); 5110 } 5111 5112 /* 5113 * Calculate the default vdev stats 5114 * 5115 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting 5116 * stats into calcvs. 5117 */ 5118 static void 5119 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, 5120 vdev_stat_t *calcvs) 5121 { 5122 int i; 5123 5124 memcpy(calcvs, newvs, sizeof (*calcvs)); 5125 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) 5126 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); 5127 5128 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) 5129 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); 5130 } 5131 5132 /* 5133 * Internal representation of the extended iostats data. 5134 * 5135 * The extended iostat stats are exported in nvlists as either uint64_t arrays 5136 * or single uint64_t's. We make both look like arrays to make them easier 5137 * to process. In order to make single uint64_t's look like arrays, we set 5138 * __data to the stat data, and then set *data = &__data with count = 1. Then, 5139 * we can just use *data and count. 5140 */ 5141 struct stat_array { 5142 uint64_t *data; 5143 uint_t count; /* Number of entries in data[] */ 5144 uint64_t __data; /* Only used when data is a single uint64_t */ 5145 }; 5146 5147 static uint64_t 5148 stat_histo_max(struct stat_array *nva, unsigned int len) 5149 { 5150 uint64_t max = 0; 5151 int i; 5152 for (i = 0; i < len; i++) 5153 max = MAX(max, array64_max(nva[i].data, nva[i].count)); 5154 5155 return (max); 5156 } 5157 5158 /* 5159 * Helper function to lookup a uint64_t array or uint64_t value and store its 5160 * data as a stat_array. If the nvpair is a single uint64_t value, then we make 5161 * it look like a one element array to make it easier to process. 5162 */ 5163 static int 5164 nvpair64_to_stat_array(nvlist_t *nvl, const char *name, 5165 struct stat_array *nva) 5166 { 5167 nvpair_t *tmp; 5168 int ret; 5169 5170 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); 5171 switch (nvpair_type(tmp)) { 5172 case DATA_TYPE_UINT64_ARRAY: 5173 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); 5174 break; 5175 case DATA_TYPE_UINT64: 5176 ret = nvpair_value_uint64(tmp, &nva->__data); 5177 nva->data = &nva->__data; 5178 nva->count = 1; 5179 break; 5180 default: 5181 /* Not a uint64_t */ 5182 ret = EINVAL; 5183 break; 5184 } 5185 5186 return (ret); 5187 } 5188 5189 /* 5190 * Given a list of nvlist names, look up the extended stats in newnv and oldnv, 5191 * subtract them, and return the results in a newly allocated stat_array. 5192 * You must free the returned array after you are done with it with 5193 * free_calc_stats(). 5194 * 5195 * Additionally, you can set "oldnv" to NULL if you simply want the newnv 5196 * values. 5197 */ 5198 static struct stat_array * 5199 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, 5200 nvlist_t *newnv) 5201 { 5202 nvlist_t *oldnvx = NULL, *newnvx; 5203 struct stat_array *oldnva, *newnva, *calcnva; 5204 int i, j; 5205 unsigned int alloc_size = (sizeof (struct stat_array)) * len; 5206 5207 /* Extract our extended stats nvlist from the main list */ 5208 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, 5209 &newnvx) == 0); 5210 if (oldnv) { 5211 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, 5212 &oldnvx) == 0); 5213 } 5214 5215 newnva = safe_malloc(alloc_size); 5216 oldnva = safe_malloc(alloc_size); 5217 calcnva = safe_malloc(alloc_size); 5218 5219 for (j = 0; j < len; j++) { 5220 verify(nvpair64_to_stat_array(newnvx, names[j], 5221 &newnva[j]) == 0); 5222 calcnva[j].count = newnva[j].count; 5223 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); 5224 calcnva[j].data = safe_malloc(alloc_size); 5225 memcpy(calcnva[j].data, newnva[j].data, alloc_size); 5226 5227 if (oldnvx) { 5228 verify(nvpair64_to_stat_array(oldnvx, names[j], 5229 &oldnva[j]) == 0); 5230 for (i = 0; i < oldnva[j].count; i++) 5231 calcnva[j].data[i] -= oldnva[j].data[i]; 5232 } 5233 } 5234 free(newnva); 5235 free(oldnva); 5236 return (calcnva); 5237 } 5238 5239 static void 5240 free_calc_stats(struct stat_array *nva, unsigned int len) 5241 { 5242 int i; 5243 for (i = 0; i < len; i++) 5244 free(nva[i].data); 5245 5246 free(nva); 5247 } 5248 5249 static void 5250 print_iostat_histo(struct stat_array *nva, unsigned int len, 5251 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, 5252 double scale) 5253 { 5254 int i, j; 5255 char buf[6]; 5256 uint64_t val; 5257 enum zfs_nicenum_format format; 5258 unsigned int buckets; 5259 unsigned int start_bucket; 5260 5261 if (cb->cb_literal) 5262 format = ZFS_NICENUM_RAW; 5263 else 5264 format = ZFS_NICENUM_1024; 5265 5266 /* All these histos are the same size, so just use nva[0].count */ 5267 buckets = nva[0].count; 5268 5269 if (cb->cb_flags & IOS_RQ_HISTO_M) { 5270 /* Start at 512 - req size should never be lower than this */ 5271 start_bucket = 9; 5272 } else { 5273 start_bucket = 0; 5274 } 5275 5276 for (j = start_bucket; j < buckets; j++) { 5277 /* Print histogram bucket label */ 5278 if (cb->cb_flags & IOS_L_HISTO_M) { 5279 /* Ending range of this bucket */ 5280 val = (1UL << (j + 1)) - 1; 5281 zfs_nicetime(val, buf, sizeof (buf)); 5282 } else { 5283 /* Request size (starting range of bucket) */ 5284 val = (1UL << j); 5285 zfs_nicenum(val, buf, sizeof (buf)); 5286 } 5287 5288 if (cb->cb_scripted) 5289 printf("%llu", (u_longlong_t)val); 5290 else 5291 printf("%-*s", namewidth, buf); 5292 5293 /* Print the values on the line */ 5294 for (i = 0; i < len; i++) { 5295 print_one_stat(nva[i].data[j] * scale, format, 5296 column_width, cb->cb_scripted); 5297 } 5298 printf("\n"); 5299 } 5300 } 5301 5302 static void 5303 print_solid_separator(unsigned int length) 5304 { 5305 while (length--) 5306 printf("-"); 5307 printf("\n"); 5308 } 5309 5310 static void 5311 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, 5312 nvlist_t *newnv, double scale, const char *name) 5313 { 5314 unsigned int column_width; 5315 unsigned int namewidth; 5316 unsigned int entire_width; 5317 enum iostat_type type; 5318 struct stat_array *nva; 5319 const char **names; 5320 unsigned int names_len; 5321 5322 /* What type of histo are we? */ 5323 type = IOS_HISTO_IDX(cb->cb_flags); 5324 5325 /* Get NULL-terminated array of nvlist names for our histo */ 5326 names = vsx_type_to_nvlist[type]; 5327 names_len = str_array_len(names); /* num of names */ 5328 5329 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); 5330 5331 if (cb->cb_literal) { 5332 column_width = MAX(5, 5333 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); 5334 } else { 5335 column_width = 5; 5336 } 5337 5338 namewidth = MAX(cb->cb_namewidth, 5339 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); 5340 5341 /* 5342 * Calculate the entire line width of what we're printing. The 5343 * +2 is for the two spaces between columns: 5344 */ 5345 /* read write */ 5346 /* ----- ----- */ 5347 /* |___| <---------- column_width */ 5348 /* */ 5349 /* |__________| <--- entire_width */ 5350 /* */ 5351 entire_width = namewidth + (column_width + 2) * 5352 label_array_len(iostat_bottom_labels[type]); 5353 5354 if (cb->cb_scripted) 5355 printf("%s\n", name); 5356 else 5357 print_iostat_header_impl(cb, column_width, name); 5358 5359 print_iostat_histo(nva, names_len, cb, column_width, 5360 namewidth, scale); 5361 5362 free_calc_stats(nva, names_len); 5363 if (!cb->cb_scripted) 5364 print_solid_separator(entire_width); 5365 } 5366 5367 /* 5368 * Calculate the average latency of a power-of-two latency histogram 5369 */ 5370 static uint64_t 5371 single_histo_average(uint64_t *histo, unsigned int buckets) 5372 { 5373 int i; 5374 uint64_t count = 0, total = 0; 5375 5376 for (i = 0; i < buckets; i++) { 5377 /* 5378 * Our buckets are power-of-two latency ranges. Use the 5379 * midpoint latency of each bucket to calculate the average. 5380 * For example: 5381 * 5382 * Bucket Midpoint 5383 * 8ns-15ns: 12ns 5384 * 16ns-31ns: 24ns 5385 * ... 5386 */ 5387 if (histo[i] != 0) { 5388 total += histo[i] * (((1UL << i) + ((1UL << i)/2))); 5389 count += histo[i]; 5390 } 5391 } 5392 5393 /* Prevent divide by zero */ 5394 return (count == 0 ? 0 : total / count); 5395 } 5396 5397 static void 5398 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv) 5399 { 5400 const char *names[] = { 5401 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, 5402 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, 5403 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, 5404 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, 5405 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, 5406 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, 5407 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, 5408 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, 5409 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, 5410 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, 5411 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE, 5412 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, 5413 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE, 5414 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, 5415 }; 5416 5417 struct stat_array *nva; 5418 5419 unsigned int column_width = default_column_width(cb, IOS_QUEUES); 5420 enum zfs_nicenum_format format; 5421 5422 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); 5423 5424 if (cb->cb_literal) 5425 format = ZFS_NICENUM_RAW; 5426 else 5427 format = ZFS_NICENUM_1024; 5428 5429 for (int i = 0; i < ARRAY_SIZE(names); i++) { 5430 uint64_t val = nva[i].data[0]; 5431 print_one_stat(val, format, column_width, cb->cb_scripted); 5432 } 5433 5434 free_calc_stats(nva, ARRAY_SIZE(names)); 5435 } 5436 5437 static void 5438 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, 5439 nvlist_t *newnv) 5440 { 5441 int i; 5442 uint64_t val; 5443 const char *names[] = { 5444 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, 5445 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, 5446 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, 5447 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, 5448 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, 5449 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, 5450 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, 5451 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, 5452 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, 5453 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, 5454 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, 5455 }; 5456 struct stat_array *nva; 5457 5458 unsigned int column_width = default_column_width(cb, IOS_LATENCY); 5459 enum zfs_nicenum_format format; 5460 5461 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); 5462 5463 if (cb->cb_literal) 5464 format = ZFS_NICENUM_RAWTIME; 5465 else 5466 format = ZFS_NICENUM_TIME; 5467 5468 /* Print our avg latencies on the line */ 5469 for (i = 0; i < ARRAY_SIZE(names); i++) { 5470 /* Compute average latency for a latency histo */ 5471 val = single_histo_average(nva[i].data, nva[i].count); 5472 print_one_stat(val, format, column_width, cb->cb_scripted); 5473 } 5474 free_calc_stats(nva, ARRAY_SIZE(names)); 5475 } 5476 5477 /* 5478 * Print default statistics (capacity/operations/bandwidth) 5479 */ 5480 static void 5481 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) 5482 { 5483 unsigned int column_width = default_column_width(cb, IOS_DEFAULT); 5484 enum zfs_nicenum_format format; 5485 char na; /* char to print for "not applicable" values */ 5486 5487 if (cb->cb_literal) { 5488 format = ZFS_NICENUM_RAW; 5489 na = '0'; 5490 } else { 5491 format = ZFS_NICENUM_1024; 5492 na = '-'; 5493 } 5494 5495 /* only toplevel vdevs have capacity stats */ 5496 if (vs->vs_space == 0) { 5497 if (cb->cb_scripted) 5498 printf("\t%c\t%c", na, na); 5499 else 5500 printf(" %*c %*c", column_width, na, column_width, 5501 na); 5502 } else { 5503 print_one_stat(vs->vs_alloc, format, column_width, 5504 cb->cb_scripted); 5505 print_one_stat(vs->vs_space - vs->vs_alloc, format, 5506 column_width, cb->cb_scripted); 5507 } 5508 5509 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), 5510 format, column_width, cb->cb_scripted); 5511 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), 5512 format, column_width, cb->cb_scripted); 5513 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), 5514 format, column_width, cb->cb_scripted); 5515 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), 5516 format, column_width, cb->cb_scripted); 5517 } 5518 5519 static const char *const class_name[] = { 5520 VDEV_ALLOC_BIAS_DEDUP, 5521 VDEV_ALLOC_BIAS_SPECIAL, 5522 VDEV_ALLOC_CLASS_LOGS 5523 }; 5524 5525 /* 5526 * Print out all the statistics for the given vdev. This can either be the 5527 * toplevel configuration, or called recursively. If 'name' is NULL, then this 5528 * is a verbose output, and we don't want to display the toplevel pool stats. 5529 * 5530 * Returns the number of stat lines printed. 5531 */ 5532 static unsigned int 5533 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 5534 nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 5535 { 5536 nvlist_t **oldchild, **newchild; 5537 uint_t c, children, oldchildren; 5538 vdev_stat_t *oldvs, *newvs, *calcvs; 5539 vdev_stat_t zerovs = { 0 }; 5540 char *vname; 5541 int i; 5542 int ret = 0; 5543 uint64_t tdelta; 5544 double scale; 5545 5546 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 5547 return (ret); 5548 5549 calcvs = safe_malloc(sizeof (*calcvs)); 5550 5551 if (oldnv != NULL) { 5552 verify(nvlist_lookup_uint64_array(oldnv, 5553 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 5554 } else { 5555 oldvs = &zerovs; 5556 } 5557 5558 /* Do we only want to see a specific vdev? */ 5559 for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) { 5560 /* Yes we do. Is this the vdev? */ 5561 if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) { 5562 /* 5563 * This is our vdev. Since it is the only vdev we 5564 * will be displaying, make depth = 0 so that it 5565 * doesn't get indented. 5566 */ 5567 depth = 0; 5568 break; 5569 } 5570 } 5571 5572 if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) { 5573 /* Couldn't match the name */ 5574 goto children; 5575 } 5576 5577 5578 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 5579 (uint64_t **)&newvs, &c) == 0); 5580 5581 /* 5582 * Print the vdev name unless it's is a histogram. Histograms 5583 * display the vdev name in the header itself. 5584 */ 5585 if (!(cb->cb_flags & IOS_ANYHISTO_M)) { 5586 if (cb->cb_scripted) { 5587 printf("%s", name); 5588 } else { 5589 if (strlen(name) + depth > cb->cb_namewidth) 5590 (void) printf("%*s%s", depth, "", name); 5591 else 5592 (void) printf("%*s%s%*s", depth, "", name, 5593 (int)(cb->cb_namewidth - strlen(name) - 5594 depth), ""); 5595 } 5596 } 5597 5598 /* Calculate our scaling factor */ 5599 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 5600 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { 5601 /* 5602 * If we specify printing histograms with no time interval, then 5603 * print the histogram numbers over the entire lifetime of the 5604 * vdev. 5605 */ 5606 scale = 1; 5607 } else { 5608 if (tdelta == 0) 5609 scale = 1.0; 5610 else 5611 scale = (double)NANOSEC / tdelta; 5612 } 5613 5614 if (cb->cb_flags & IOS_DEFAULT_M) { 5615 calc_default_iostats(oldvs, newvs, calcvs); 5616 print_iostat_default(calcvs, cb, scale); 5617 } 5618 if (cb->cb_flags & IOS_LATENCY_M) 5619 print_iostat_latency(cb, oldnv, newnv); 5620 if (cb->cb_flags & IOS_QUEUES_M) 5621 print_iostat_queues(cb, newnv); 5622 if (cb->cb_flags & IOS_ANYHISTO_M) { 5623 printf("\n"); 5624 print_iostat_histos(cb, oldnv, newnv, scale, name); 5625 } 5626 5627 if (cb->vcdl != NULL) { 5628 const char *path; 5629 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, 5630 &path) == 0) { 5631 printf(" "); 5632 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); 5633 } 5634 } 5635 5636 if (!(cb->cb_flags & IOS_ANYHISTO_M)) 5637 printf("\n"); 5638 5639 ret++; 5640 5641 children: 5642 5643 free(calcvs); 5644 5645 if (!cb->cb_verbose) 5646 return (ret); 5647 5648 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 5649 &newchild, &children) != 0) 5650 return (ret); 5651 5652 if (oldnv) { 5653 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 5654 &oldchild, &oldchildren) != 0) 5655 return (ret); 5656 5657 children = MIN(oldchildren, children); 5658 } 5659 5660 /* 5661 * print normal top-level devices 5662 */ 5663 for (c = 0; c < children; c++) { 5664 uint64_t ishole = B_FALSE, islog = B_FALSE; 5665 5666 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 5667 &ishole); 5668 5669 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 5670 &islog); 5671 5672 if (ishole || islog) 5673 continue; 5674 5675 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 5676 continue; 5677 5678 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 5679 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); 5680 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 5681 newchild[c], cb, depth + 2); 5682 free(vname); 5683 } 5684 5685 /* 5686 * print all other top-level devices 5687 */ 5688 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { 5689 boolean_t printed = B_FALSE; 5690 5691 for (c = 0; c < children; c++) { 5692 uint64_t islog = B_FALSE; 5693 const char *bias = NULL; 5694 const char *type = NULL; 5695 5696 (void) nvlist_lookup_uint64(newchild[c], 5697 ZPOOL_CONFIG_IS_LOG, &islog); 5698 if (islog) { 5699 bias = VDEV_ALLOC_CLASS_LOGS; 5700 } else { 5701 (void) nvlist_lookup_string(newchild[c], 5702 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 5703 (void) nvlist_lookup_string(newchild[c], 5704 ZPOOL_CONFIG_TYPE, &type); 5705 } 5706 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 5707 continue; 5708 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 5709 continue; 5710 5711 if (!printed) { 5712 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && 5713 !cb->cb_scripted && 5714 !cb->cb_vdevs.cb_names) { 5715 print_iostat_dashes(cb, 0, 5716 class_name[n]); 5717 } 5718 printf("\n"); 5719 printed = B_TRUE; 5720 } 5721 5722 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 5723 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); 5724 ret += print_vdev_stats(zhp, vname, oldnv ? 5725 oldchild[c] : NULL, newchild[c], cb, depth + 2); 5726 free(vname); 5727 } 5728 } 5729 5730 /* 5731 * Include level 2 ARC devices in iostat output 5732 */ 5733 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 5734 &newchild, &children) != 0) 5735 return (ret); 5736 5737 if (oldnv) { 5738 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 5739 &oldchild, &oldchildren) != 0) 5740 return (ret); 5741 5742 children = MIN(oldchildren, children); 5743 } 5744 5745 if (children > 0) { 5746 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && 5747 !cb->cb_vdevs.cb_names) { 5748 print_iostat_dashes(cb, 0, "cache"); 5749 } 5750 printf("\n"); 5751 5752 for (c = 0; c < children; c++) { 5753 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 5754 cb->cb_vdevs.cb_name_flags); 5755 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] 5756 : NULL, newchild[c], cb, depth + 2); 5757 free(vname); 5758 } 5759 } 5760 5761 return (ret); 5762 } 5763 5764 static int 5765 refresh_iostat(zpool_handle_t *zhp, void *data) 5766 { 5767 iostat_cbdata_t *cb = data; 5768 boolean_t missing; 5769 5770 /* 5771 * If the pool has disappeared, remove it from the list and continue. 5772 */ 5773 if (zpool_refresh_stats(zhp, &missing) != 0) 5774 return (-1); 5775 5776 if (missing) 5777 pool_list_remove(cb->cb_list, zhp); 5778 5779 return (0); 5780 } 5781 5782 /* 5783 * Callback to print out the iostats for the given pool. 5784 */ 5785 static int 5786 print_iostat(zpool_handle_t *zhp, void *data) 5787 { 5788 iostat_cbdata_t *cb = data; 5789 nvlist_t *oldconfig, *newconfig; 5790 nvlist_t *oldnvroot, *newnvroot; 5791 int ret; 5792 5793 newconfig = zpool_get_config(zhp, &oldconfig); 5794 5795 if (cb->cb_iteration == 1) 5796 oldconfig = NULL; 5797 5798 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 5799 &newnvroot) == 0); 5800 5801 if (oldconfig == NULL) 5802 oldnvroot = NULL; 5803 else 5804 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 5805 &oldnvroot) == 0); 5806 5807 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, 5808 cb, 0); 5809 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && 5810 !cb->cb_scripted && cb->cb_verbose && 5811 !cb->cb_vdevs.cb_names_count) { 5812 print_iostat_separator(cb); 5813 if (cb->vcdl != NULL) { 5814 print_cmd_columns(cb->vcdl, 1); 5815 } 5816 printf("\n"); 5817 } 5818 5819 return (ret); 5820 } 5821 5822 static int 5823 get_columns(void) 5824 { 5825 struct winsize ws; 5826 int columns = 80; 5827 int error; 5828 5829 if (isatty(STDOUT_FILENO)) { 5830 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); 5831 if (error == 0) 5832 columns = ws.ws_col; 5833 } else { 5834 columns = 999; 5835 } 5836 5837 return (columns); 5838 } 5839 5840 /* 5841 * Return the required length of the pool/vdev name column. The minimum 5842 * allowed width and output formatting flags must be provided. 5843 */ 5844 static int 5845 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) 5846 { 5847 nvlist_t *config, *nvroot; 5848 int width = min_width; 5849 5850 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 5851 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5852 &nvroot) == 0); 5853 size_t poolname_len = strlen(zpool_get_name(zhp)); 5854 if (verbose == B_FALSE) { 5855 width = MAX(poolname_len, min_width); 5856 } else { 5857 width = MAX(poolname_len, 5858 max_width(zhp, nvroot, 0, min_width, flags)); 5859 } 5860 } 5861 5862 return (width); 5863 } 5864 5865 /* 5866 * Parse the input string, get the 'interval' and 'count' value if there is one. 5867 */ 5868 static void 5869 get_interval_count(int *argcp, char **argv, float *iv, 5870 unsigned long *cnt) 5871 { 5872 float interval = 0; 5873 unsigned long count = 0; 5874 int argc = *argcp; 5875 5876 /* 5877 * Determine if the last argument is an integer or a pool name 5878 */ 5879 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 5880 char *end; 5881 5882 errno = 0; 5883 interval = strtof(argv[argc - 1], &end); 5884 5885 if (*end == '\0' && errno == 0) { 5886 if (interval == 0) { 5887 (void) fprintf(stderr, gettext( 5888 "interval cannot be zero\n")); 5889 usage(B_FALSE); 5890 } 5891 /* 5892 * Ignore the last parameter 5893 */ 5894 argc--; 5895 } else { 5896 /* 5897 * If this is not a valid number, just plow on. The 5898 * user will get a more informative error message later 5899 * on. 5900 */ 5901 interval = 0; 5902 } 5903 } 5904 5905 /* 5906 * If the last argument is also an integer, then we have both a count 5907 * and an interval. 5908 */ 5909 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 5910 char *end; 5911 5912 errno = 0; 5913 count = interval; 5914 interval = strtof(argv[argc - 1], &end); 5915 5916 if (*end == '\0' && errno == 0) { 5917 if (interval == 0) { 5918 (void) fprintf(stderr, gettext( 5919 "interval cannot be zero\n")); 5920 usage(B_FALSE); 5921 } 5922 5923 /* 5924 * Ignore the last parameter 5925 */ 5926 argc--; 5927 } else { 5928 interval = 0; 5929 } 5930 } 5931 5932 *iv = interval; 5933 *cnt = count; 5934 *argcp = argc; 5935 } 5936 5937 static void 5938 get_timestamp_arg(char c) 5939 { 5940 if (c == 'u') 5941 timestamp_fmt = UDATE; 5942 else if (c == 'd') 5943 timestamp_fmt = DDATE; 5944 else 5945 usage(B_FALSE); 5946 } 5947 5948 /* 5949 * Return stat flags that are supported by all pools by both the module and 5950 * zpool iostat. "*data" should be initialized to all 0xFFs before running. 5951 * It will get ANDed down until only the flags that are supported on all pools 5952 * remain. 5953 */ 5954 static int 5955 get_stat_flags_cb(zpool_handle_t *zhp, void *data) 5956 { 5957 uint64_t *mask = data; 5958 nvlist_t *config, *nvroot, *nvx; 5959 uint64_t flags = 0; 5960 int i, j; 5961 5962 config = zpool_get_config(zhp, NULL); 5963 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5964 &nvroot) == 0); 5965 5966 /* Default stats are always supported, but for completeness.. */ 5967 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) 5968 flags |= IOS_DEFAULT_M; 5969 5970 /* Get our extended stats nvlist from the main list */ 5971 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, 5972 &nvx) != 0) { 5973 /* 5974 * No extended stats; they're probably running an older 5975 * module. No big deal, we support that too. 5976 */ 5977 goto end; 5978 } 5979 5980 /* For each extended stat, make sure all its nvpairs are supported */ 5981 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { 5982 if (!vsx_type_to_nvlist[j][0]) 5983 continue; 5984 5985 /* Start off by assuming the flag is supported, then check */ 5986 flags |= (1ULL << j); 5987 for (i = 0; vsx_type_to_nvlist[j][i]; i++) { 5988 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { 5989 /* flag isn't supported */ 5990 flags = flags & ~(1ULL << j); 5991 break; 5992 } 5993 } 5994 } 5995 end: 5996 *mask = *mask & flags; 5997 return (0); 5998 } 5999 6000 /* 6001 * Return a bitmask of stats that are supported on all pools by both the module 6002 * and zpool iostat. 6003 */ 6004 static uint64_t 6005 get_stat_flags(zpool_list_t *list) 6006 { 6007 uint64_t mask = -1; 6008 6009 /* 6010 * get_stat_flags_cb() will lop off bits from "mask" until only the 6011 * flags that are supported on all pools remain. 6012 */ 6013 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); 6014 return (mask); 6015 } 6016 6017 /* 6018 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise. 6019 */ 6020 static int 6021 is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data) 6022 { 6023 uint64_t guid; 6024 vdev_cbdata_t *cb = cb_data; 6025 zpool_handle_t *zhp = zhp_data; 6026 6027 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 6028 return (0); 6029 6030 return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0])); 6031 } 6032 6033 /* 6034 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise. 6035 */ 6036 static int 6037 is_vdev(zpool_handle_t *zhp, void *cb_data) 6038 { 6039 return (for_each_vdev(zhp, is_vdev_cb, cb_data)); 6040 } 6041 6042 /* 6043 * Check if vdevs are in a pool 6044 * 6045 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise 6046 * return 0. If pool_name is NULL, then search all pools. 6047 */ 6048 static int 6049 are_vdevs_in_pool(int argc, char **argv, char *pool_name, 6050 vdev_cbdata_t *cb) 6051 { 6052 char **tmp_name; 6053 int ret = 0; 6054 int i; 6055 int pool_count = 0; 6056 6057 if ((argc == 0) || !*argv) 6058 return (0); 6059 6060 if (pool_name) 6061 pool_count = 1; 6062 6063 /* Temporarily hijack cb_names for a second... */ 6064 tmp_name = cb->cb_names; 6065 6066 /* Go though our list of prospective vdev names */ 6067 for (i = 0; i < argc; i++) { 6068 cb->cb_names = argv + i; 6069 6070 /* Is this name a vdev in our pools? */ 6071 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, 6072 ZFS_TYPE_POOL, B_FALSE, is_vdev, cb); 6073 if (!ret) { 6074 /* No match */ 6075 break; 6076 } 6077 } 6078 6079 cb->cb_names = tmp_name; 6080 6081 return (ret); 6082 } 6083 6084 static int 6085 is_pool_cb(zpool_handle_t *zhp, void *data) 6086 { 6087 char *name = data; 6088 if (strcmp(name, zpool_get_name(zhp)) == 0) 6089 return (1); 6090 6091 return (0); 6092 } 6093 6094 /* 6095 * Do we have a pool named *name? If so, return 1, otherwise 0. 6096 */ 6097 static int 6098 is_pool(char *name) 6099 { 6100 return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE, 6101 is_pool_cb, name)); 6102 } 6103 6104 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ 6105 static int 6106 are_all_pools(int argc, char **argv) 6107 { 6108 if ((argc == 0) || !*argv) 6109 return (0); 6110 6111 while (--argc >= 0) 6112 if (!is_pool(argv[argc])) 6113 return (0); 6114 6115 return (1); 6116 } 6117 6118 /* 6119 * Helper function to print out vdev/pool names we can't resolve. Used for an 6120 * error message. 6121 */ 6122 static void 6123 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, 6124 vdev_cbdata_t *cb) 6125 { 6126 int i; 6127 char *name; 6128 char *str; 6129 for (i = 0; i < argc; i++) { 6130 name = argv[i]; 6131 6132 if (is_pool(name)) 6133 str = gettext("pool"); 6134 else if (are_vdevs_in_pool(1, &name, pool_name, cb)) 6135 str = gettext("vdev in this pool"); 6136 else if (are_vdevs_in_pool(1, &name, NULL, cb)) 6137 str = gettext("vdev in another pool"); 6138 else 6139 str = gettext("unknown"); 6140 6141 fprintf(stderr, "\t%s (%s)\n", name, str); 6142 } 6143 } 6144 6145 /* 6146 * Same as get_interval_count(), but with additional checks to not misinterpret 6147 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in 6148 * cb.cb_vdevs.cb_name_flags. 6149 */ 6150 static void 6151 get_interval_count_filter_guids(int *argc, char **argv, float *interval, 6152 unsigned long *count, iostat_cbdata_t *cb) 6153 { 6154 int argc_for_interval = 0; 6155 6156 /* Is the last arg an interval value? Or a guid? */ 6157 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, 6158 &cb->cb_vdevs)) { 6159 /* 6160 * The last arg is not a guid, so it's probably an 6161 * interval value. 6162 */ 6163 argc_for_interval++; 6164 6165 if (*argc >= 2 && 6166 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, 6167 &cb->cb_vdevs)) { 6168 /* 6169 * The 2nd to last arg is not a guid, so it's probably 6170 * an interval value. 6171 */ 6172 argc_for_interval++; 6173 } 6174 } 6175 6176 /* Point to our list of possible intervals */ 6177 char **tmpargv = &argv[*argc - argc_for_interval]; 6178 6179 *argc = *argc - argc_for_interval; 6180 get_interval_count(&argc_for_interval, tmpargv, 6181 interval, count); 6182 } 6183 6184 /* 6185 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or 6186 * if we were unable to determine its size. 6187 */ 6188 static int 6189 terminal_height(void) 6190 { 6191 struct winsize win; 6192 6193 if (isatty(STDOUT_FILENO) == 0) 6194 return (-1); 6195 6196 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) 6197 return (win.ws_row); 6198 6199 return (-1); 6200 } 6201 6202 /* 6203 * Run one of the zpool status/iostat -c scripts with the help (-h) option and 6204 * print the result. 6205 * 6206 * name: Short name of the script ('iostat'). 6207 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); 6208 */ 6209 static void 6210 print_zpool_script_help(char *name, char *path) 6211 { 6212 char *argv[] = {path, (char *)"-h", NULL}; 6213 char **lines = NULL; 6214 int lines_cnt = 0; 6215 int rc; 6216 6217 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, 6218 &lines_cnt); 6219 if (rc != 0 || lines == NULL || lines_cnt <= 0) { 6220 if (lines != NULL) 6221 libzfs_free_str_array(lines, lines_cnt); 6222 return; 6223 } 6224 6225 for (int i = 0; i < lines_cnt; i++) 6226 if (!is_blank_str(lines[i])) 6227 printf(" %-14s %s\n", name, lines[i]); 6228 6229 libzfs_free_str_array(lines, lines_cnt); 6230 } 6231 6232 /* 6233 * Go though the zpool status/iostat -c scripts in the user's path, run their 6234 * help option (-h), and print out the results. 6235 */ 6236 static void 6237 print_zpool_dir_scripts(char *dirpath) 6238 { 6239 DIR *dir; 6240 struct dirent *ent; 6241 char fullpath[MAXPATHLEN]; 6242 struct stat dir_stat; 6243 6244 if ((dir = opendir(dirpath)) != NULL) { 6245 /* print all the files and directories within directory */ 6246 while ((ent = readdir(dir)) != NULL) { 6247 if (snprintf(fullpath, sizeof (fullpath), "%s/%s", 6248 dirpath, ent->d_name) >= sizeof (fullpath)) { 6249 (void) fprintf(stderr, 6250 gettext("internal error: " 6251 "ZPOOL_SCRIPTS_PATH too large.\n")); 6252 exit(1); 6253 } 6254 6255 /* Print the scripts */ 6256 if (stat(fullpath, &dir_stat) == 0) 6257 if (dir_stat.st_mode & S_IXUSR && 6258 S_ISREG(dir_stat.st_mode)) 6259 print_zpool_script_help(ent->d_name, 6260 fullpath); 6261 } 6262 closedir(dir); 6263 } 6264 } 6265 6266 /* 6267 * Print out help text for all zpool status/iostat -c scripts. 6268 */ 6269 static void 6270 print_zpool_script_list(const char *subcommand) 6271 { 6272 char *dir, *sp, *tmp; 6273 6274 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); 6275 6276 sp = zpool_get_cmd_search_path(); 6277 if (sp == NULL) 6278 return; 6279 6280 for (dir = strtok_r(sp, ":", &tmp); 6281 dir != NULL; 6282 dir = strtok_r(NULL, ":", &tmp)) 6283 print_zpool_dir_scripts(dir); 6284 6285 free(sp); 6286 } 6287 6288 /* 6289 * Set the minimum pool/vdev name column width. The width must be at least 10, 6290 * but may be as large as the column width - 42 so it still fits on one line. 6291 * NOTE: 42 is the width of the default capacity/operations/bandwidth output 6292 */ 6293 static int 6294 get_namewidth_iostat(zpool_handle_t *zhp, void *data) 6295 { 6296 iostat_cbdata_t *cb = data; 6297 int width, available_width; 6298 6299 /* 6300 * get_namewidth() returns the maximum width of any name in that column 6301 * for any pool/vdev/device line that will be output. 6302 */ 6303 width = get_namewidth(zhp, cb->cb_namewidth, 6304 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); 6305 6306 /* 6307 * The width we are calculating is the width of the header and also the 6308 * padding width for names that are less than maximum width. The stats 6309 * take up 42 characters, so the width available for names is: 6310 */ 6311 available_width = get_columns() - 42; 6312 6313 /* 6314 * If the maximum width fits on a screen, then great! Make everything 6315 * line up by justifying all lines to the same width. If that max 6316 * width is larger than what's available, the name plus stats won't fit 6317 * on one line, and justifying to that width would cause every line to 6318 * wrap on the screen. We only want lines with long names to wrap. 6319 * Limit the padding to what won't wrap. 6320 */ 6321 if (width > available_width) 6322 width = available_width; 6323 6324 /* 6325 * And regardless of whatever the screen width is (get_columns can 6326 * return 0 if the width is not known or less than 42 for a narrow 6327 * terminal) have the width be a minimum of 10. 6328 */ 6329 if (width < 10) 6330 width = 10; 6331 6332 /* Save the calculated width */ 6333 cb->cb_namewidth = width; 6334 6335 return (0); 6336 } 6337 6338 /* 6339 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] 6340 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] 6341 * [interval [count]] 6342 * 6343 * -c CMD For each vdev, run command CMD 6344 * -g Display guid for individual vdev name. 6345 * -L Follow links when resolving vdev path name. 6346 * -P Display full path for vdev name. 6347 * -v Display statistics for individual vdevs 6348 * -h Display help 6349 * -p Display values in parsable (exact) format. 6350 * -H Scripted mode. Don't display headers, and separate properties 6351 * by a single tab. 6352 * -l Display average latency 6353 * -q Display queue depths 6354 * -w Display latency histograms 6355 * -r Display request size histogram 6356 * -T Display a timestamp in date(1) or Unix format 6357 * -n Only print headers once 6358 * 6359 * This command can be tricky because we want to be able to deal with pool 6360 * creation/destruction as well as vdev configuration changes. The bulk of this 6361 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 6362 * on pool_list_update() to detect the addition of new pools. Configuration 6363 * changes are all handled within libzfs. 6364 */ 6365 int 6366 zpool_do_iostat(int argc, char **argv) 6367 { 6368 int c; 6369 int ret; 6370 int npools; 6371 float interval = 0; 6372 unsigned long count = 0; 6373 zpool_list_t *list; 6374 boolean_t verbose = B_FALSE; 6375 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; 6376 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; 6377 boolean_t omit_since_boot = B_FALSE; 6378 boolean_t guid = B_FALSE; 6379 boolean_t follow_links = B_FALSE; 6380 boolean_t full_name = B_FALSE; 6381 boolean_t headers_once = B_FALSE; 6382 iostat_cbdata_t cb = { 0 }; 6383 char *cmd = NULL; 6384 6385 /* Used for printing error message */ 6386 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', 6387 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; 6388 6389 uint64_t unsupported_flags; 6390 6391 /* check options */ 6392 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { 6393 switch (c) { 6394 case 'c': 6395 if (cmd != NULL) { 6396 fprintf(stderr, 6397 gettext("Can't set -c flag twice\n")); 6398 exit(1); 6399 } 6400 6401 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 6402 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 6403 fprintf(stderr, gettext( 6404 "Can't run -c, disabled by " 6405 "ZPOOL_SCRIPTS_ENABLED.\n")); 6406 exit(1); 6407 } 6408 6409 if ((getuid() <= 0 || geteuid() <= 0) && 6410 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 6411 fprintf(stderr, gettext( 6412 "Can't run -c with root privileges " 6413 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 6414 exit(1); 6415 } 6416 cmd = optarg; 6417 verbose = B_TRUE; 6418 break; 6419 case 'g': 6420 guid = B_TRUE; 6421 break; 6422 case 'L': 6423 follow_links = B_TRUE; 6424 break; 6425 case 'P': 6426 full_name = B_TRUE; 6427 break; 6428 case 'T': 6429 get_timestamp_arg(*optarg); 6430 break; 6431 case 'v': 6432 verbose = B_TRUE; 6433 break; 6434 case 'p': 6435 parsable = B_TRUE; 6436 break; 6437 case 'l': 6438 latency = B_TRUE; 6439 break; 6440 case 'q': 6441 queues = B_TRUE; 6442 break; 6443 case 'H': 6444 scripted = B_TRUE; 6445 break; 6446 case 'w': 6447 l_histo = B_TRUE; 6448 break; 6449 case 'r': 6450 rq_histo = B_TRUE; 6451 break; 6452 case 'y': 6453 omit_since_boot = B_TRUE; 6454 break; 6455 case 'n': 6456 headers_once = B_TRUE; 6457 break; 6458 case 'h': 6459 usage(B_FALSE); 6460 break; 6461 case '?': 6462 if (optopt == 'c') { 6463 print_zpool_script_list("iostat"); 6464 exit(0); 6465 } else { 6466 fprintf(stderr, 6467 gettext("invalid option '%c'\n"), optopt); 6468 } 6469 usage(B_FALSE); 6470 } 6471 } 6472 6473 argc -= optind; 6474 argv += optind; 6475 6476 cb.cb_literal = parsable; 6477 cb.cb_scripted = scripted; 6478 6479 if (guid) 6480 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID; 6481 if (follow_links) 6482 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 6483 if (full_name) 6484 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH; 6485 cb.cb_iteration = 0; 6486 cb.cb_namewidth = 0; 6487 cb.cb_verbose = verbose; 6488 6489 /* Get our interval and count values (if any) */ 6490 if (guid) { 6491 get_interval_count_filter_guids(&argc, argv, &interval, 6492 &count, &cb); 6493 } else { 6494 get_interval_count(&argc, argv, &interval, &count); 6495 } 6496 6497 if (argc == 0) { 6498 /* No args, so just print the defaults. */ 6499 } else if (are_all_pools(argc, argv)) { 6500 /* All the args are pool names */ 6501 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) { 6502 /* All the args are vdevs */ 6503 cb.cb_vdevs.cb_names = argv; 6504 cb.cb_vdevs.cb_names_count = argc; 6505 argc = 0; /* No pools to process */ 6506 } else if (are_all_pools(1, argv)) { 6507 /* The first arg is a pool name */ 6508 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], 6509 &cb.cb_vdevs)) { 6510 /* ...and the rest are vdev names */ 6511 cb.cb_vdevs.cb_names = argv + 1; 6512 cb.cb_vdevs.cb_names_count = argc - 1; 6513 argc = 1; /* One pool to process */ 6514 } else { 6515 fprintf(stderr, gettext("Expected either a list of ")); 6516 fprintf(stderr, gettext("pools, or list of vdevs in")); 6517 fprintf(stderr, " \"%s\", ", argv[0]); 6518 fprintf(stderr, gettext("but got:\n")); 6519 error_list_unresolved_vdevs(argc - 1, argv + 1, 6520 argv[0], &cb.cb_vdevs); 6521 fprintf(stderr, "\n"); 6522 usage(B_FALSE); 6523 return (1); 6524 } 6525 } else { 6526 /* 6527 * The args don't make sense. The first arg isn't a pool name, 6528 * nor are all the args vdevs. 6529 */ 6530 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); 6531 fprintf(stderr, "\n"); 6532 return (1); 6533 } 6534 6535 if (cb.cb_vdevs.cb_names_count != 0) { 6536 /* 6537 * If user specified vdevs, it implies verbose. 6538 */ 6539 cb.cb_verbose = B_TRUE; 6540 } 6541 6542 /* 6543 * Construct the list of all interesting pools. 6544 */ 6545 ret = 0; 6546 if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable, 6547 &ret)) == NULL) 6548 return (1); 6549 6550 if (pool_list_count(list) == 0 && argc != 0) { 6551 pool_list_free(list); 6552 return (1); 6553 } 6554 6555 if (pool_list_count(list) == 0 && interval == 0) { 6556 pool_list_free(list); 6557 (void) fprintf(stderr, gettext("no pools available\n")); 6558 return (1); 6559 } 6560 6561 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { 6562 pool_list_free(list); 6563 (void) fprintf(stderr, 6564 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); 6565 usage(B_FALSE); 6566 return (1); 6567 } 6568 6569 if (l_histo && rq_histo) { 6570 pool_list_free(list); 6571 (void) fprintf(stderr, 6572 gettext("Only one of [-r|-w] can be passed at a time\n")); 6573 usage(B_FALSE); 6574 return (1); 6575 } 6576 6577 /* 6578 * Enter the main iostat loop. 6579 */ 6580 cb.cb_list = list; 6581 6582 if (l_histo) { 6583 /* 6584 * Histograms tables look out of place when you try to display 6585 * them with the other stats, so make a rule that you can only 6586 * print histograms by themselves. 6587 */ 6588 cb.cb_flags = IOS_L_HISTO_M; 6589 } else if (rq_histo) { 6590 cb.cb_flags = IOS_RQ_HISTO_M; 6591 } else { 6592 cb.cb_flags = IOS_DEFAULT_M; 6593 if (latency) 6594 cb.cb_flags |= IOS_LATENCY_M; 6595 if (queues) 6596 cb.cb_flags |= IOS_QUEUES_M; 6597 } 6598 6599 /* 6600 * See if the module supports all the stats we want to display. 6601 */ 6602 unsupported_flags = cb.cb_flags & ~get_stat_flags(list); 6603 if (unsupported_flags) { 6604 uint64_t f; 6605 int idx; 6606 fprintf(stderr, 6607 gettext("The loaded zfs module doesn't support:")); 6608 6609 /* for each bit set in unsupported_flags */ 6610 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { 6611 idx = lowbit64(f) - 1; 6612 fprintf(stderr, " -%c", flag_to_arg[idx]); 6613 } 6614 6615 fprintf(stderr, ". Try running a newer module.\n"); 6616 pool_list_free(list); 6617 6618 return (1); 6619 } 6620 6621 for (;;) { 6622 if ((npools = pool_list_count(list)) == 0) 6623 (void) fprintf(stderr, gettext("no pools available\n")); 6624 else { 6625 /* 6626 * If this is the first iteration and -y was supplied 6627 * we skip any printing. 6628 */ 6629 boolean_t skip = (omit_since_boot && 6630 cb.cb_iteration == 0); 6631 6632 /* 6633 * Refresh all statistics. This is done as an 6634 * explicit step before calculating the maximum name 6635 * width, so that any * configuration changes are 6636 * properly accounted for. 6637 */ 6638 (void) pool_list_iter(list, B_FALSE, refresh_iostat, 6639 &cb); 6640 6641 /* 6642 * Iterate over all pools to determine the maximum width 6643 * for the pool / device name column across all pools. 6644 */ 6645 cb.cb_namewidth = 0; 6646 (void) pool_list_iter(list, B_FALSE, 6647 get_namewidth_iostat, &cb); 6648 6649 if (timestamp_fmt != NODATE) 6650 print_timestamp(timestamp_fmt); 6651 6652 if (cmd != NULL && cb.cb_verbose && 6653 !(cb.cb_flags & IOS_ANYHISTO_M)) { 6654 cb.vcdl = all_pools_for_each_vdev_run(argc, 6655 argv, cmd, g_zfs, cb.cb_vdevs.cb_names, 6656 cb.cb_vdevs.cb_names_count, 6657 cb.cb_vdevs.cb_name_flags); 6658 } else { 6659 cb.vcdl = NULL; 6660 } 6661 6662 6663 /* 6664 * Check terminal size so we can print headers 6665 * even when terminal window has its height 6666 * changed. 6667 */ 6668 int winheight = terminal_height(); 6669 /* 6670 * Are we connected to TTY? If not, headers_once 6671 * should be true, to avoid breaking scripts. 6672 */ 6673 if (winheight < 0) 6674 headers_once = B_TRUE; 6675 6676 /* 6677 * If it's the first time and we're not skipping it, 6678 * or either skip or verbose mode, print the header. 6679 * 6680 * The histogram code explicitly prints its header on 6681 * every vdev, so skip this for histograms. 6682 */ 6683 if (((++cb.cb_iteration == 1 && !skip) || 6684 (skip != verbose) || 6685 (!headers_once && 6686 (cb.cb_iteration % winheight) == 0)) && 6687 (!(cb.cb_flags & IOS_ANYHISTO_M)) && 6688 !cb.cb_scripted) 6689 print_iostat_header(&cb); 6690 6691 if (skip) { 6692 (void) fflush(stdout); 6693 (void) fsleep(interval); 6694 continue; 6695 } 6696 6697 pool_list_iter(list, B_FALSE, print_iostat, &cb); 6698 6699 /* 6700 * If there's more than one pool, and we're not in 6701 * verbose mode (which prints a separator for us), 6702 * then print a separator. 6703 * 6704 * In addition, if we're printing specific vdevs then 6705 * we also want an ending separator. 6706 */ 6707 if (((npools > 1 && !verbose && 6708 !(cb.cb_flags & IOS_ANYHISTO_M)) || 6709 (!(cb.cb_flags & IOS_ANYHISTO_M) && 6710 cb.cb_vdevs.cb_names_count)) && 6711 !cb.cb_scripted) { 6712 print_iostat_separator(&cb); 6713 if (cb.vcdl != NULL) 6714 print_cmd_columns(cb.vcdl, 1); 6715 printf("\n"); 6716 } 6717 6718 if (cb.vcdl != NULL) 6719 free_vdev_cmd_data_list(cb.vcdl); 6720 6721 } 6722 6723 if (interval == 0) 6724 break; 6725 6726 if (count != 0 && --count == 0) 6727 break; 6728 6729 (void) fflush(stdout); 6730 (void) fsleep(interval); 6731 } 6732 6733 pool_list_free(list); 6734 6735 return (ret); 6736 } 6737 6738 typedef struct list_cbdata { 6739 boolean_t cb_verbose; 6740 int cb_name_flags; 6741 int cb_namewidth; 6742 boolean_t cb_json; 6743 boolean_t cb_scripted; 6744 zprop_list_t *cb_proplist; 6745 boolean_t cb_literal; 6746 nvlist_t *cb_jsobj; 6747 boolean_t cb_json_as_int; 6748 boolean_t cb_json_pool_key_guid; 6749 } list_cbdata_t; 6750 6751 6752 /* 6753 * Given a list of columns to display, output appropriate headers for each one. 6754 */ 6755 static void 6756 print_header(list_cbdata_t *cb) 6757 { 6758 zprop_list_t *pl = cb->cb_proplist; 6759 char headerbuf[ZPOOL_MAXPROPLEN]; 6760 const char *header; 6761 boolean_t first = B_TRUE; 6762 boolean_t right_justify; 6763 size_t width = 0; 6764 6765 for (; pl != NULL; pl = pl->pl_next) { 6766 width = pl->pl_width; 6767 if (first && cb->cb_verbose) { 6768 /* 6769 * Reset the width to accommodate the verbose listing 6770 * of devices. 6771 */ 6772 width = cb->cb_namewidth; 6773 } 6774 6775 if (!first) 6776 (void) fputs(" ", stdout); 6777 else 6778 first = B_FALSE; 6779 6780 right_justify = B_FALSE; 6781 if (pl->pl_prop != ZPROP_USERPROP) { 6782 header = zpool_prop_column_name(pl->pl_prop); 6783 right_justify = zpool_prop_align_right(pl->pl_prop); 6784 } else { 6785 int i; 6786 6787 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 6788 headerbuf[i] = toupper(pl->pl_user_prop[i]); 6789 headerbuf[i] = '\0'; 6790 header = headerbuf; 6791 } 6792 6793 if (pl->pl_next == NULL && !right_justify) 6794 (void) fputs(header, stdout); 6795 else if (right_justify) 6796 (void) printf("%*s", (int)width, header); 6797 else 6798 (void) printf("%-*s", (int)width, header); 6799 } 6800 6801 (void) fputc('\n', stdout); 6802 } 6803 6804 /* 6805 * Given a pool and a list of properties, print out all the properties according 6806 * to the described layout. Used by zpool_do_list(). 6807 */ 6808 static void 6809 collect_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 6810 { 6811 zprop_list_t *pl = cb->cb_proplist; 6812 boolean_t first = B_TRUE; 6813 char property[ZPOOL_MAXPROPLEN]; 6814 const char *propstr; 6815 boolean_t right_justify; 6816 size_t width; 6817 zprop_source_t sourcetype = ZPROP_SRC_NONE; 6818 nvlist_t *item, *d, *props; 6819 item = d = props = NULL; 6820 6821 if (cb->cb_json) { 6822 item = fnvlist_alloc(); 6823 props = fnvlist_alloc(); 6824 d = fnvlist_lookup_nvlist(cb->cb_jsobj, "pools"); 6825 if (d == NULL) { 6826 fprintf(stderr, "pools obj not found.\n"); 6827 exit(1); 6828 } 6829 fill_pool_info(item, zhp, B_TRUE, cb->cb_json_as_int); 6830 } 6831 6832 for (; pl != NULL; pl = pl->pl_next) { 6833 6834 width = pl->pl_width; 6835 if (first && cb->cb_verbose) { 6836 /* 6837 * Reset the width to accommodate the verbose listing 6838 * of devices. 6839 */ 6840 width = cb->cb_namewidth; 6841 } 6842 6843 if (!cb->cb_json && !first) { 6844 if (cb->cb_scripted) 6845 (void) fputc('\t', stdout); 6846 else 6847 (void) fputs(" ", stdout); 6848 } else { 6849 first = B_FALSE; 6850 } 6851 6852 right_justify = B_FALSE; 6853 if (pl->pl_prop != ZPROP_USERPROP) { 6854 if (zpool_get_prop(zhp, pl->pl_prop, property, 6855 sizeof (property), &sourcetype, 6856 cb->cb_literal) != 0) 6857 propstr = "-"; 6858 else 6859 propstr = property; 6860 6861 right_justify = zpool_prop_align_right(pl->pl_prop); 6862 } else if ((zpool_prop_feature(pl->pl_user_prop) || 6863 zpool_prop_unsupported(pl->pl_user_prop)) && 6864 zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 6865 sizeof (property)) == 0) { 6866 propstr = property; 6867 sourcetype = ZPROP_SRC_LOCAL; 6868 } else if (zfs_prop_user(pl->pl_user_prop) && 6869 zpool_get_userprop(zhp, pl->pl_user_prop, property, 6870 sizeof (property), &sourcetype) == 0) { 6871 propstr = property; 6872 } else { 6873 propstr = "-"; 6874 } 6875 6876 if (cb->cb_json) { 6877 if (pl->pl_prop == ZPOOL_PROP_NAME) 6878 continue; 6879 const char *prop_name; 6880 if (pl->pl_prop != ZPROP_USERPROP) 6881 prop_name = zpool_prop_to_name(pl->pl_prop); 6882 else 6883 prop_name = pl->pl_user_prop; 6884 (void) zprop_nvlist_one_property( 6885 prop_name, propstr, 6886 sourcetype, NULL, NULL, props, cb->cb_json_as_int); 6887 } else { 6888 /* 6889 * If this is being called in scripted mode, or if this 6890 * is the last column and it is left-justified, don't 6891 * include a width format specifier. 6892 */ 6893 if (cb->cb_scripted || (pl->pl_next == NULL && 6894 !right_justify)) 6895 (void) fputs(propstr, stdout); 6896 else if (right_justify) 6897 (void) printf("%*s", (int)width, propstr); 6898 else 6899 (void) printf("%-*s", (int)width, propstr); 6900 } 6901 } 6902 6903 if (cb->cb_json) { 6904 fnvlist_add_nvlist(item, "properties", props); 6905 if (cb->cb_json_pool_key_guid) { 6906 char pool_guid[256]; 6907 uint64_t guid = fnvlist_lookup_uint64( 6908 zpool_get_config(zhp, NULL), 6909 ZPOOL_CONFIG_POOL_GUID); 6910 snprintf(pool_guid, 256, "%llu", 6911 (u_longlong_t)guid); 6912 fnvlist_add_nvlist(d, pool_guid, item); 6913 } else { 6914 fnvlist_add_nvlist(d, zpool_get_name(zhp), 6915 item); 6916 } 6917 fnvlist_free(props); 6918 fnvlist_free(item); 6919 } else 6920 (void) fputc('\n', stdout); 6921 } 6922 6923 static void 6924 collect_vdev_prop(zpool_prop_t prop, uint64_t value, const char *str, 6925 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format, 6926 boolean_t json, nvlist_t *nvl, boolean_t as_int) 6927 { 6928 char propval[64]; 6929 boolean_t fixed; 6930 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 6931 6932 switch (prop) { 6933 case ZPOOL_PROP_SIZE: 6934 case ZPOOL_PROP_EXPANDSZ: 6935 case ZPOOL_PROP_CHECKPOINT: 6936 case ZPOOL_PROP_DEDUPRATIO: 6937 case ZPOOL_PROP_DEDUPCACHED: 6938 if (value == 0) 6939 (void) strlcpy(propval, "-", sizeof (propval)); 6940 else 6941 zfs_nicenum_format(value, propval, sizeof (propval), 6942 format); 6943 break; 6944 case ZPOOL_PROP_FRAGMENTATION: 6945 if (value == ZFS_FRAG_INVALID) { 6946 (void) strlcpy(propval, "-", sizeof (propval)); 6947 } else if (format == ZFS_NICENUM_RAW) { 6948 (void) snprintf(propval, sizeof (propval), "%llu", 6949 (unsigned long long)value); 6950 } else { 6951 (void) snprintf(propval, sizeof (propval), "%llu%%", 6952 (unsigned long long)value); 6953 } 6954 break; 6955 case ZPOOL_PROP_CAPACITY: 6956 /* capacity value is in parts-per-10,000 (aka permyriad) */ 6957 if (format == ZFS_NICENUM_RAW) 6958 (void) snprintf(propval, sizeof (propval), "%llu", 6959 (unsigned long long)value / 100); 6960 else 6961 (void) snprintf(propval, sizeof (propval), 6962 value < 1000 ? "%1.2f%%" : value < 10000 ? 6963 "%2.1f%%" : "%3.0f%%", value / 100.0); 6964 break; 6965 case ZPOOL_PROP_HEALTH: 6966 width = 8; 6967 (void) strlcpy(propval, str, sizeof (propval)); 6968 break; 6969 default: 6970 zfs_nicenum_format(value, propval, sizeof (propval), format); 6971 } 6972 6973 if (!valid) 6974 (void) strlcpy(propval, "-", sizeof (propval)); 6975 6976 if (json) { 6977 zprop_nvlist_one_property(zpool_prop_to_name(prop), propval, 6978 ZPROP_SRC_NONE, NULL, NULL, nvl, as_int); 6979 } else { 6980 if (scripted) 6981 (void) printf("\t%s", propval); 6982 else 6983 (void) printf(" %*s", (int)width, propval); 6984 } 6985 } 6986 6987 /* 6988 * print static default line per vdev 6989 * not compatible with '-o' <proplist> option 6990 */ 6991 static void 6992 collect_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 6993 list_cbdata_t *cb, int depth, boolean_t isspare, nvlist_t *item) 6994 { 6995 nvlist_t **child; 6996 vdev_stat_t *vs; 6997 uint_t c, children = 0; 6998 char *vname; 6999 boolean_t scripted = cb->cb_scripted; 7000 uint64_t islog = B_FALSE; 7001 nvlist_t *props, *ent, *ch, *obj, *l2c, *sp; 7002 props = ent = ch = obj = sp = l2c = NULL; 7003 const char *dashes = "%-*s - - - - " 7004 "- - - - -\n"; 7005 7006 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 7007 (uint64_t **)&vs, &c) == 0); 7008 7009 if (name != NULL) { 7010 boolean_t toplevel = (vs->vs_space != 0); 7011 uint64_t cap; 7012 enum zfs_nicenum_format format; 7013 const char *state; 7014 7015 if (cb->cb_literal) 7016 format = ZFS_NICENUM_RAW; 7017 else 7018 format = ZFS_NICENUM_1024; 7019 7020 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 7021 return; 7022 7023 if (cb->cb_json) { 7024 props = fnvlist_alloc(); 7025 ent = fnvlist_alloc(); 7026 fill_vdev_info(ent, zhp, (char *)name, B_FALSE, 7027 cb->cb_json_as_int); 7028 } else { 7029 if (scripted) 7030 (void) printf("\t%s", name); 7031 else if (strlen(name) + depth > cb->cb_namewidth) 7032 (void) printf("%*s%s", depth, "", name); 7033 else 7034 (void) printf("%*s%s%*s", depth, "", name, 7035 (int)(cb->cb_namewidth - strlen(name) - 7036 depth), ""); 7037 } 7038 7039 /* 7040 * Print the properties for the individual vdevs. Some 7041 * properties are only applicable to toplevel vdevs. The 7042 * 'toplevel' boolean value is passed to the print_one_column() 7043 * to indicate that the value is valid. 7044 */ 7045 if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace) { 7046 collect_vdev_prop(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL, 7047 scripted, B_TRUE, format, cb->cb_json, props, 7048 cb->cb_json_as_int); 7049 } else { 7050 collect_vdev_prop(ZPOOL_PROP_SIZE, vs->vs_space, NULL, 7051 scripted, toplevel, format, cb->cb_json, props, 7052 cb->cb_json_as_int); 7053 } 7054 collect_vdev_prop(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, 7055 scripted, toplevel, format, cb->cb_json, props, 7056 cb->cb_json_as_int); 7057 collect_vdev_prop(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, 7058 NULL, scripted, toplevel, format, cb->cb_json, props, 7059 cb->cb_json_as_int); 7060 collect_vdev_prop(ZPOOL_PROP_CHECKPOINT, 7061 vs->vs_checkpoint_space, NULL, scripted, toplevel, format, 7062 cb->cb_json, props, cb->cb_json_as_int); 7063 collect_vdev_prop(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, 7064 scripted, B_TRUE, format, cb->cb_json, props, 7065 cb->cb_json_as_int); 7066 collect_vdev_prop(ZPOOL_PROP_FRAGMENTATION, 7067 vs->vs_fragmentation, NULL, scripted, 7068 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), 7069 format, cb->cb_json, props, cb->cb_json_as_int); 7070 cap = (vs->vs_space == 0) ? 0 : 7071 (vs->vs_alloc * 10000 / vs->vs_space); 7072 collect_vdev_prop(ZPOOL_PROP_CAPACITY, cap, NULL, 7073 scripted, toplevel, format, cb->cb_json, props, 7074 cb->cb_json_as_int); 7075 collect_vdev_prop(ZPOOL_PROP_DEDUPRATIO, 0, NULL, 7076 scripted, toplevel, format, cb->cb_json, props, 7077 cb->cb_json_as_int); 7078 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 7079 if (isspare) { 7080 if (vs->vs_aux == VDEV_AUX_SPARED) 7081 state = "INUSE"; 7082 else if (vs->vs_state == VDEV_STATE_HEALTHY) 7083 state = "AVAIL"; 7084 } 7085 collect_vdev_prop(ZPOOL_PROP_HEALTH, 0, state, scripted, 7086 B_TRUE, format, cb->cb_json, props, cb->cb_json_as_int); 7087 7088 if (cb->cb_json) { 7089 fnvlist_add_nvlist(ent, "properties", props); 7090 fnvlist_free(props); 7091 } else 7092 (void) fputc('\n', stdout); 7093 } 7094 7095 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 7096 &child, &children) != 0) { 7097 if (cb->cb_json) { 7098 fnvlist_add_nvlist(item, name, ent); 7099 fnvlist_free(ent); 7100 } 7101 return; 7102 } 7103 7104 if (cb->cb_json) { 7105 ch = fnvlist_alloc(); 7106 } 7107 7108 /* list the normal vdevs first */ 7109 for (c = 0; c < children; c++) { 7110 uint64_t ishole = B_FALSE; 7111 7112 if (nvlist_lookup_uint64(child[c], 7113 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 7114 continue; 7115 7116 if (nvlist_lookup_uint64(child[c], 7117 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) 7118 continue; 7119 7120 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 7121 continue; 7122 7123 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7124 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 7125 7126 if (name == NULL || cb->cb_json != B_TRUE) 7127 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7128 B_FALSE, item); 7129 else if (cb->cb_json) { 7130 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7131 B_FALSE, ch); 7132 } 7133 free(vname); 7134 } 7135 7136 if (cb->cb_json) { 7137 if (!nvlist_empty(ch)) 7138 fnvlist_add_nvlist(ent, "vdevs", ch); 7139 fnvlist_free(ch); 7140 } 7141 7142 /* list the classes: 'logs', 'dedup', and 'special' */ 7143 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { 7144 boolean_t printed = B_FALSE; 7145 if (cb->cb_json) 7146 obj = fnvlist_alloc(); 7147 for (c = 0; c < children; c++) { 7148 const char *bias = NULL; 7149 const char *type = NULL; 7150 7151 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 7152 &islog) == 0 && islog) { 7153 bias = VDEV_ALLOC_CLASS_LOGS; 7154 } else { 7155 (void) nvlist_lookup_string(child[c], 7156 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 7157 (void) nvlist_lookup_string(child[c], 7158 ZPOOL_CONFIG_TYPE, &type); 7159 } 7160 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 7161 continue; 7162 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 7163 continue; 7164 7165 if (!printed && !cb->cb_json) { 7166 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7167 (void) printf(dashes, cb->cb_namewidth, 7168 class_name[n]); 7169 printed = B_TRUE; 7170 } 7171 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7172 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 7173 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7174 B_FALSE, obj); 7175 free(vname); 7176 } 7177 if (cb->cb_json) { 7178 if (!nvlist_empty(obj)) 7179 fnvlist_add_nvlist(item, class_name[n], obj); 7180 fnvlist_free(obj); 7181 } 7182 } 7183 7184 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 7185 &child, &children) == 0 && children > 0) { 7186 if (cb->cb_json) { 7187 l2c = fnvlist_alloc(); 7188 } else { 7189 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7190 (void) printf(dashes, cb->cb_namewidth, "cache"); 7191 } 7192 for (c = 0; c < children; c++) { 7193 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7194 cb->cb_name_flags); 7195 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7196 B_FALSE, l2c); 7197 free(vname); 7198 } 7199 if (cb->cb_json) { 7200 if (!nvlist_empty(l2c)) 7201 fnvlist_add_nvlist(item, "l2cache", l2c); 7202 fnvlist_free(l2c); 7203 } 7204 } 7205 7206 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, 7207 &children) == 0 && children > 0) { 7208 if (cb->cb_json) { 7209 sp = fnvlist_alloc(); 7210 } else { 7211 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7212 (void) printf(dashes, cb->cb_namewidth, "spare"); 7213 } 7214 for (c = 0; c < children; c++) { 7215 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7216 cb->cb_name_flags); 7217 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7218 B_TRUE, sp); 7219 free(vname); 7220 } 7221 if (cb->cb_json) { 7222 if (!nvlist_empty(sp)) 7223 fnvlist_add_nvlist(item, "spares", sp); 7224 fnvlist_free(sp); 7225 } 7226 } 7227 7228 if (name != NULL && cb->cb_json) { 7229 fnvlist_add_nvlist(item, name, ent); 7230 fnvlist_free(ent); 7231 } 7232 } 7233 7234 /* 7235 * Generic callback function to list a pool. 7236 */ 7237 static int 7238 list_callback(zpool_handle_t *zhp, void *data) 7239 { 7240 nvlist_t *p, *d, *nvdevs; 7241 uint64_t guid; 7242 char pool_guid[256]; 7243 const char *pool_name = zpool_get_name(zhp); 7244 list_cbdata_t *cbp = data; 7245 p = d = nvdevs = NULL; 7246 7247 collect_pool(zhp, cbp); 7248 7249 if (cbp->cb_verbose) { 7250 nvlist_t *config, *nvroot; 7251 config = zpool_get_config(zhp, NULL); 7252 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 7253 &nvroot) == 0); 7254 if (cbp->cb_json) { 7255 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, 7256 "pools"); 7257 if (cbp->cb_json_pool_key_guid) { 7258 guid = fnvlist_lookup_uint64(config, 7259 ZPOOL_CONFIG_POOL_GUID); 7260 snprintf(pool_guid, 256, "%llu", 7261 (u_longlong_t)guid); 7262 p = fnvlist_lookup_nvlist(d, pool_guid); 7263 } else { 7264 p = fnvlist_lookup_nvlist(d, pool_name); 7265 } 7266 nvdevs = fnvlist_alloc(); 7267 } 7268 collect_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE, nvdevs); 7269 if (cbp->cb_json) { 7270 fnvlist_add_nvlist(p, "vdevs", nvdevs); 7271 if (cbp->cb_json_pool_key_guid) 7272 fnvlist_add_nvlist(d, pool_guid, p); 7273 else 7274 fnvlist_add_nvlist(d, pool_name, p); 7275 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d); 7276 fnvlist_free(nvdevs); 7277 } 7278 } 7279 7280 return (0); 7281 } 7282 7283 /* 7284 * Set the minimum pool/vdev name column width. The width must be at least 9, 7285 * but may be as large as needed. 7286 */ 7287 static int 7288 get_namewidth_list(zpool_handle_t *zhp, void *data) 7289 { 7290 list_cbdata_t *cb = data; 7291 int width; 7292 7293 width = get_namewidth(zhp, cb->cb_namewidth, 7294 cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); 7295 7296 if (width < 9) 7297 width = 9; 7298 7299 cb->cb_namewidth = width; 7300 7301 return (0); 7302 } 7303 7304 /* 7305 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 7306 * 7307 * -g Display guid for individual vdev name. 7308 * -H Scripted mode. Don't display headers, and separate properties 7309 * by a single tab. 7310 * -L Follow links when resolving vdev path name. 7311 * -o List of properties to display. Defaults to 7312 * "name,size,allocated,free,expandsize,fragmentation,capacity," 7313 * "dedupratio,health,altroot" 7314 * -p Display values in parsable (exact) format. 7315 * -P Display full path for vdev name. 7316 * -T Display a timestamp in date(1) or Unix format 7317 * -j Display the output in JSON format 7318 * --json-int Display the numbers as integer instead of strings. 7319 * --json-pool-key-guid Set pool GUID as key for pool objects. 7320 * 7321 * List all pools in the system, whether or not they're healthy. Output space 7322 * statistics for each one, as well as health status summary. 7323 */ 7324 int 7325 zpool_do_list(int argc, char **argv) 7326 { 7327 int c; 7328 int ret = 0; 7329 list_cbdata_t cb = { 0 }; 7330 static char default_props[] = 7331 "name,size,allocated,free,checkpoint,expandsize,fragmentation," 7332 "capacity,dedupratio,health,altroot"; 7333 char *props = default_props; 7334 float interval = 0; 7335 unsigned long count = 0; 7336 zpool_list_t *list; 7337 boolean_t first = B_TRUE; 7338 nvlist_t *data = NULL; 7339 current_prop_type = ZFS_TYPE_POOL; 7340 7341 struct option long_options[] = { 7342 {"json", no_argument, NULL, 'j'}, 7343 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 7344 {"json-pool-key-guid", no_argument, NULL, 7345 ZPOOL_OPTION_POOL_KEY_GUID}, 7346 {0, 0, 0, 0} 7347 }; 7348 7349 /* check options */ 7350 while ((c = getopt_long(argc, argv, ":gjHLo:pPT:v", long_options, 7351 NULL)) != -1) { 7352 switch (c) { 7353 case 'g': 7354 cb.cb_name_flags |= VDEV_NAME_GUID; 7355 break; 7356 case 'H': 7357 cb.cb_scripted = B_TRUE; 7358 break; 7359 case 'L': 7360 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 7361 break; 7362 case 'o': 7363 props = optarg; 7364 break; 7365 case 'P': 7366 cb.cb_name_flags |= VDEV_NAME_PATH; 7367 break; 7368 case 'p': 7369 cb.cb_literal = B_TRUE; 7370 break; 7371 case 'j': 7372 cb.cb_json = B_TRUE; 7373 break; 7374 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 7375 cb.cb_json_as_int = B_TRUE; 7376 cb.cb_literal = B_TRUE; 7377 break; 7378 case ZPOOL_OPTION_POOL_KEY_GUID: 7379 cb.cb_json_pool_key_guid = B_TRUE; 7380 break; 7381 case 'T': 7382 get_timestamp_arg(*optarg); 7383 break; 7384 case 'v': 7385 cb.cb_verbose = B_TRUE; 7386 cb.cb_namewidth = 8; /* 8 until precalc is avail */ 7387 break; 7388 case ':': 7389 (void) fprintf(stderr, gettext("missing argument for " 7390 "'%c' option\n"), optopt); 7391 usage(B_FALSE); 7392 break; 7393 case '?': 7394 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7395 optopt); 7396 usage(B_FALSE); 7397 } 7398 } 7399 7400 argc -= optind; 7401 argv += optind; 7402 7403 if (!cb.cb_json && cb.cb_json_as_int) { 7404 (void) fprintf(stderr, gettext("'--json-int' only works with" 7405 " '-j' option\n")); 7406 usage(B_FALSE); 7407 } 7408 7409 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 7410 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 7411 " works with '-j' option\n")); 7412 usage(B_FALSE); 7413 } 7414 7415 get_interval_count(&argc, argv, &interval, &count); 7416 7417 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 7418 usage(B_FALSE); 7419 7420 for (;;) { 7421 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 7422 ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL) 7423 return (1); 7424 7425 if (pool_list_count(list) == 0) 7426 break; 7427 7428 if (cb.cb_json) { 7429 cb.cb_jsobj = zpool_json_schema(0, 1); 7430 data = fnvlist_alloc(); 7431 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 7432 fnvlist_free(data); 7433 } 7434 7435 cb.cb_namewidth = 0; 7436 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); 7437 7438 if (timestamp_fmt != NODATE) { 7439 if (cb.cb_json) { 7440 if (cb.cb_json_as_int) { 7441 fnvlist_add_uint64(cb.cb_jsobj, "time", 7442 time(NULL)); 7443 } else { 7444 char ts[128]; 7445 get_timestamp(timestamp_fmt, ts, 128); 7446 fnvlist_add_string(cb.cb_jsobj, "time", 7447 ts); 7448 } 7449 } else 7450 print_timestamp(timestamp_fmt); 7451 } 7452 7453 if (!cb.cb_scripted && (first || cb.cb_verbose) && 7454 !cb.cb_json) { 7455 print_header(&cb); 7456 first = B_FALSE; 7457 } 7458 ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 7459 7460 if (ret == 0 && cb.cb_json) 7461 zcmd_print_json(cb.cb_jsobj); 7462 else if (ret != 0 && cb.cb_json) 7463 nvlist_free(cb.cb_jsobj); 7464 7465 if (interval == 0) 7466 break; 7467 7468 if (count != 0 && --count == 0) 7469 break; 7470 7471 pool_list_free(list); 7472 7473 (void) fflush(stdout); 7474 (void) fsleep(interval); 7475 } 7476 7477 if (argc == 0 && !cb.cb_scripted && !cb.cb_json && 7478 pool_list_count(list) == 0) { 7479 (void) printf(gettext("no pools available\n")); 7480 ret = 0; 7481 } 7482 7483 pool_list_free(list); 7484 zprop_free_list(cb.cb_proplist); 7485 return (ret); 7486 } 7487 7488 static int 7489 zpool_do_attach_or_replace(int argc, char **argv, int replacing) 7490 { 7491 boolean_t force = B_FALSE; 7492 boolean_t rebuild = B_FALSE; 7493 boolean_t wait = B_FALSE; 7494 int c; 7495 nvlist_t *nvroot; 7496 char *poolname, *old_disk, *new_disk; 7497 zpool_handle_t *zhp; 7498 nvlist_t *props = NULL; 7499 char *propval; 7500 int ret; 7501 7502 /* check options */ 7503 while ((c = getopt(argc, argv, "fo:sw")) != -1) { 7504 switch (c) { 7505 case 'f': 7506 force = B_TRUE; 7507 break; 7508 case 'o': 7509 if ((propval = strchr(optarg, '=')) == NULL) { 7510 (void) fprintf(stderr, gettext("missing " 7511 "'=' for -o option\n")); 7512 usage(B_FALSE); 7513 } 7514 *propval = '\0'; 7515 propval++; 7516 7517 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 7518 (add_prop_list(optarg, propval, &props, B_TRUE))) 7519 usage(B_FALSE); 7520 break; 7521 case 's': 7522 rebuild = B_TRUE; 7523 break; 7524 case 'w': 7525 wait = B_TRUE; 7526 break; 7527 case '?': 7528 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7529 optopt); 7530 usage(B_FALSE); 7531 } 7532 } 7533 7534 argc -= optind; 7535 argv += optind; 7536 7537 /* get pool name and check number of arguments */ 7538 if (argc < 1) { 7539 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7540 usage(B_FALSE); 7541 } 7542 7543 poolname = argv[0]; 7544 7545 if (argc < 2) { 7546 (void) fprintf(stderr, 7547 gettext("missing <device> specification\n")); 7548 usage(B_FALSE); 7549 } 7550 7551 old_disk = argv[1]; 7552 7553 if (argc < 3) { 7554 if (!replacing) { 7555 (void) fprintf(stderr, 7556 gettext("missing <new_device> specification\n")); 7557 usage(B_FALSE); 7558 } 7559 new_disk = old_disk; 7560 argc -= 1; 7561 argv += 1; 7562 } else { 7563 new_disk = argv[2]; 7564 argc -= 2; 7565 argv += 2; 7566 } 7567 7568 if (argc > 1) { 7569 (void) fprintf(stderr, gettext("too many arguments\n")); 7570 usage(B_FALSE); 7571 } 7572 7573 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7574 nvlist_free(props); 7575 return (1); 7576 } 7577 7578 if (zpool_get_config(zhp, NULL) == NULL) { 7579 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 7580 poolname); 7581 zpool_close(zhp); 7582 nvlist_free(props); 7583 return (1); 7584 } 7585 7586 /* unless manually specified use "ashift" pool property (if set) */ 7587 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 7588 int intval; 7589 zprop_source_t src; 7590 char strval[ZPOOL_MAXPROPLEN]; 7591 7592 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 7593 if (src != ZPROP_SRC_DEFAULT) { 7594 (void) sprintf(strval, "%" PRId32, intval); 7595 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 7596 &props, B_TRUE) == 0); 7597 } 7598 } 7599 7600 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, 7601 argc, argv); 7602 if (nvroot == NULL) { 7603 zpool_close(zhp); 7604 nvlist_free(props); 7605 return (1); 7606 } 7607 7608 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, 7609 rebuild); 7610 7611 if (ret == 0 && wait) { 7612 zpool_wait_activity_t activity = ZPOOL_WAIT_RESILVER; 7613 char raidz_prefix[] = "raidz"; 7614 if (replacing) { 7615 activity = ZPOOL_WAIT_REPLACE; 7616 } else if (strncmp(old_disk, 7617 raidz_prefix, strlen(raidz_prefix)) == 0) { 7618 activity = ZPOOL_WAIT_RAIDZ_EXPAND; 7619 } 7620 ret = zpool_wait(zhp, activity); 7621 } 7622 7623 nvlist_free(props); 7624 nvlist_free(nvroot); 7625 zpool_close(zhp); 7626 7627 return (ret); 7628 } 7629 7630 /* 7631 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> 7632 * 7633 * -f Force attach, even if <new_device> appears to be in use. 7634 * -s Use sequential instead of healing reconstruction for resilver. 7635 * -o Set property=value. 7636 * -w Wait for replacing to complete before returning 7637 * 7638 * Replace <device> with <new_device>. 7639 */ 7640 int 7641 zpool_do_replace(int argc, char **argv) 7642 { 7643 return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 7644 } 7645 7646 /* 7647 * zpool attach [-fsw] [-o property=value] <pool> <vdev> <new_device> 7648 * 7649 * -f Force attach, even if <new_device> appears to be in use. 7650 * -s Use sequential instead of healing reconstruction for resilver. 7651 * -o Set property=value. 7652 * -w Wait for resilvering (mirror) or expansion (raidz) to complete 7653 * before returning. 7654 * 7655 * Attach <new_device> to a <vdev>, where the vdev can be of type 7656 * device, mirror or raidz. If <vdev> is not part of a mirror, then <vdev> will 7657 * be transformed into a mirror of <vdev> and <new_device>. When a mirror 7658 * is involved, <new_device> will begin life with a DTL of [0, now], and will 7659 * immediately begin to resilver itself. For the raidz case, a expansion will 7660 * commence and reflow the raidz data across all the disks including the 7661 * <new_device>. 7662 */ 7663 int 7664 zpool_do_attach(int argc, char **argv) 7665 { 7666 return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 7667 } 7668 7669 /* 7670 * zpool detach [-f] <pool> <device> 7671 * 7672 * -f Force detach of <device>, even if DTLs argue against it 7673 * (not supported yet) 7674 * 7675 * Detach a device from a mirror. The operation will be refused if <device> 7676 * is the last device in the mirror, or if the DTLs indicate that this device 7677 * has the only valid copy of some data. 7678 */ 7679 int 7680 zpool_do_detach(int argc, char **argv) 7681 { 7682 int c; 7683 char *poolname, *path; 7684 zpool_handle_t *zhp; 7685 int ret; 7686 7687 /* check options */ 7688 while ((c = getopt(argc, argv, "")) != -1) { 7689 switch (c) { 7690 case '?': 7691 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7692 optopt); 7693 usage(B_FALSE); 7694 } 7695 } 7696 7697 argc -= optind; 7698 argv += optind; 7699 7700 /* get pool name and check number of arguments */ 7701 if (argc < 1) { 7702 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7703 usage(B_FALSE); 7704 } 7705 7706 if (argc < 2) { 7707 (void) fprintf(stderr, 7708 gettext("missing <device> specification\n")); 7709 usage(B_FALSE); 7710 } 7711 7712 poolname = argv[0]; 7713 path = argv[1]; 7714 7715 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 7716 return (1); 7717 7718 ret = zpool_vdev_detach(zhp, path); 7719 7720 zpool_close(zhp); 7721 7722 return (ret); 7723 } 7724 7725 /* 7726 * zpool split [-gLnP] [-o prop=val] ... 7727 * [-o mntopt] ... 7728 * [-R altroot] <pool> <newpool> [<device> ...] 7729 * 7730 * -g Display guid for individual vdev name. 7731 * -L Follow links when resolving vdev path name. 7732 * -n Do not split the pool, but display the resulting layout if 7733 * it were to be split. 7734 * -o Set property=value, or set mount options. 7735 * -P Display full path for vdev name. 7736 * -R Mount the split-off pool under an alternate root. 7737 * -l Load encryption keys while importing. 7738 * 7739 * Splits the named pool and gives it the new pool name. Devices to be split 7740 * off may be listed, provided that no more than one device is specified 7741 * per top-level vdev mirror. The newly split pool is left in an exported 7742 * state unless -R is specified. 7743 * 7744 * Restrictions: the top-level of the pool pool must only be made up of 7745 * mirrors; all devices in the pool must be healthy; no device may be 7746 * undergoing a resilvering operation. 7747 */ 7748 int 7749 zpool_do_split(int argc, char **argv) 7750 { 7751 char *srcpool, *newpool, *propval; 7752 char *mntopts = NULL; 7753 splitflags_t flags; 7754 int c, ret = 0; 7755 int ms_status = 0; 7756 boolean_t loadkeys = B_FALSE; 7757 zpool_handle_t *zhp; 7758 nvlist_t *config, *props = NULL; 7759 7760 flags.dryrun = B_FALSE; 7761 flags.import = B_FALSE; 7762 flags.name_flags = 0; 7763 7764 /* check options */ 7765 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { 7766 switch (c) { 7767 case 'g': 7768 flags.name_flags |= VDEV_NAME_GUID; 7769 break; 7770 case 'L': 7771 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; 7772 break; 7773 case 'R': 7774 flags.import = B_TRUE; 7775 if (add_prop_list( 7776 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 7777 &props, B_TRUE) != 0) { 7778 nvlist_free(props); 7779 usage(B_FALSE); 7780 } 7781 break; 7782 case 'l': 7783 loadkeys = B_TRUE; 7784 break; 7785 case 'n': 7786 flags.dryrun = B_TRUE; 7787 break; 7788 case 'o': 7789 if ((propval = strchr(optarg, '=')) != NULL) { 7790 *propval = '\0'; 7791 propval++; 7792 if (add_prop_list(optarg, propval, 7793 &props, B_TRUE) != 0) { 7794 nvlist_free(props); 7795 usage(B_FALSE); 7796 } 7797 } else { 7798 mntopts = optarg; 7799 } 7800 break; 7801 case 'P': 7802 flags.name_flags |= VDEV_NAME_PATH; 7803 break; 7804 case ':': 7805 (void) fprintf(stderr, gettext("missing argument for " 7806 "'%c' option\n"), optopt); 7807 usage(B_FALSE); 7808 break; 7809 case '?': 7810 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7811 optopt); 7812 usage(B_FALSE); 7813 break; 7814 } 7815 } 7816 7817 if (!flags.import && mntopts != NULL) { 7818 (void) fprintf(stderr, gettext("setting mntopts is only " 7819 "valid when importing the pool\n")); 7820 usage(B_FALSE); 7821 } 7822 7823 if (!flags.import && loadkeys) { 7824 (void) fprintf(stderr, gettext("loading keys is only " 7825 "valid when importing the pool\n")); 7826 usage(B_FALSE); 7827 } 7828 7829 argc -= optind; 7830 argv += optind; 7831 7832 if (argc < 1) { 7833 (void) fprintf(stderr, gettext("Missing pool name\n")); 7834 usage(B_FALSE); 7835 } 7836 if (argc < 2) { 7837 (void) fprintf(stderr, gettext("Missing new pool name\n")); 7838 usage(B_FALSE); 7839 } 7840 7841 srcpool = argv[0]; 7842 newpool = argv[1]; 7843 7844 argc -= 2; 7845 argv += 2; 7846 7847 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { 7848 nvlist_free(props); 7849 return (1); 7850 } 7851 7852 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 7853 if (config == NULL) { 7854 ret = 1; 7855 } else { 7856 if (flags.dryrun) { 7857 (void) printf(gettext("would create '%s' with the " 7858 "following layout:\n\n"), newpool); 7859 print_vdev_tree(NULL, newpool, config, 0, "", 7860 flags.name_flags); 7861 print_vdev_tree(NULL, "dedup", config, 0, 7862 VDEV_ALLOC_BIAS_DEDUP, 0); 7863 print_vdev_tree(NULL, "special", config, 0, 7864 VDEV_ALLOC_BIAS_SPECIAL, 0); 7865 } 7866 } 7867 7868 zpool_close(zhp); 7869 7870 if (ret != 0 || flags.dryrun || !flags.import) { 7871 nvlist_free(config); 7872 nvlist_free(props); 7873 return (ret); 7874 } 7875 7876 /* 7877 * The split was successful. Now we need to open the new 7878 * pool and import it. 7879 */ 7880 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { 7881 nvlist_free(config); 7882 nvlist_free(props); 7883 return (1); 7884 } 7885 7886 if (loadkeys) { 7887 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); 7888 if (ret != 0) 7889 ret = 1; 7890 } 7891 7892 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) { 7893 ms_status = zpool_enable_datasets(zhp, mntopts, 0, 7894 mount_tp_nthr); 7895 if (ms_status == EZFS_SHAREFAILED) { 7896 (void) fprintf(stderr, gettext("Split was successful, " 7897 "datasets are mounted but sharing of some datasets " 7898 "has failed\n")); 7899 } else if (ms_status == EZFS_MOUNTFAILED) { 7900 (void) fprintf(stderr, gettext("Split was successful" 7901 ", but some datasets could not be mounted\n")); 7902 (void) fprintf(stderr, gettext("Try doing '%s' with a " 7903 "different altroot\n"), "zpool import"); 7904 } 7905 } 7906 zpool_close(zhp); 7907 nvlist_free(config); 7908 nvlist_free(props); 7909 7910 return (ret); 7911 } 7912 7913 7914 /* 7915 * zpool online [--power] <pool> <device> ... 7916 * 7917 * --power: Power on the enclosure slot to the drive (if possible) 7918 */ 7919 int 7920 zpool_do_online(int argc, char **argv) 7921 { 7922 int c, i; 7923 char *poolname; 7924 zpool_handle_t *zhp; 7925 int ret = 0; 7926 vdev_state_t newstate; 7927 int flags = 0; 7928 boolean_t is_power_on = B_FALSE; 7929 struct option long_options[] = { 7930 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 7931 {0, 0, 0, 0} 7932 }; 7933 7934 /* check options */ 7935 while ((c = getopt_long(argc, argv, "e", long_options, NULL)) != -1) { 7936 switch (c) { 7937 case 'e': 7938 flags |= ZFS_ONLINE_EXPAND; 7939 break; 7940 case ZPOOL_OPTION_POWER: 7941 is_power_on = B_TRUE; 7942 break; 7943 case '?': 7944 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7945 optopt); 7946 usage(B_FALSE); 7947 } 7948 } 7949 7950 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) 7951 is_power_on = B_TRUE; 7952 7953 argc -= optind; 7954 argv += optind; 7955 7956 /* get pool name and check number of arguments */ 7957 if (argc < 1) { 7958 (void) fprintf(stderr, gettext("missing pool name\n")); 7959 usage(B_FALSE); 7960 } 7961 if (argc < 2) { 7962 (void) fprintf(stderr, gettext("missing device name\n")); 7963 usage(B_FALSE); 7964 } 7965 7966 poolname = argv[0]; 7967 7968 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7969 (void) fprintf(stderr, gettext("failed to open pool " 7970 "\"%s\""), poolname); 7971 return (1); 7972 } 7973 7974 for (i = 1; i < argc; i++) { 7975 vdev_state_t oldstate; 7976 boolean_t avail_spare, l2cache; 7977 int rc; 7978 7979 if (is_power_on) { 7980 rc = zpool_power_on_and_disk_wait(zhp, argv[i]); 7981 if (rc == ENOTSUP) { 7982 (void) fprintf(stderr, 7983 gettext("Power control not supported\n")); 7984 } 7985 if (rc != 0) 7986 return (rc); 7987 } 7988 7989 nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare, 7990 &l2cache, NULL); 7991 if (tgt == NULL) { 7992 ret = 1; 7993 (void) fprintf(stderr, gettext("couldn't find device " 7994 "\"%s\" in pool \"%s\"\n"), argv[i], poolname); 7995 continue; 7996 } 7997 uint_t vsc; 7998 oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt, 7999 ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state; 8000 if ((rc = zpool_vdev_online(zhp, argv[i], flags, 8001 &newstate)) == 0) { 8002 if (newstate != VDEV_STATE_HEALTHY) { 8003 (void) printf(gettext("warning: device '%s' " 8004 "onlined, but remains in faulted state\n"), 8005 argv[i]); 8006 if (newstate == VDEV_STATE_FAULTED) 8007 (void) printf(gettext("use 'zpool " 8008 "clear' to restore a faulted " 8009 "device\n")); 8010 else 8011 (void) printf(gettext("use 'zpool " 8012 "replace' to replace devices " 8013 "that are no longer present\n")); 8014 if ((flags & ZFS_ONLINE_EXPAND)) { 8015 (void) printf(gettext("%s: failed " 8016 "to expand usable space on " 8017 "unhealthy device '%s'\n"), 8018 (oldstate >= VDEV_STATE_DEGRADED ? 8019 "error" : "warning"), argv[i]); 8020 if (oldstate >= VDEV_STATE_DEGRADED) { 8021 ret = 1; 8022 break; 8023 } 8024 } 8025 } 8026 } else { 8027 (void) fprintf(stderr, gettext("Failed to online " 8028 "\"%s\" in pool \"%s\": %d\n"), 8029 argv[i], poolname, rc); 8030 ret = 1; 8031 } 8032 } 8033 8034 zpool_close(zhp); 8035 8036 return (ret); 8037 } 8038 8039 /* 8040 * zpool offline [-ft]|[--power] <pool> <device> ... 8041 * 8042 * 8043 * -f Force the device into a faulted state. 8044 * 8045 * -t Only take the device off-line temporarily. The offline/faulted 8046 * state will not be persistent across reboots. 8047 * 8048 * --power Power off the enclosure slot to the drive (if possible) 8049 */ 8050 int 8051 zpool_do_offline(int argc, char **argv) 8052 { 8053 int c, i; 8054 char *poolname; 8055 zpool_handle_t *zhp; 8056 int ret = 0; 8057 boolean_t istmp = B_FALSE; 8058 boolean_t fault = B_FALSE; 8059 boolean_t is_power_off = B_FALSE; 8060 8061 struct option long_options[] = { 8062 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 8063 {0, 0, 0, 0} 8064 }; 8065 8066 /* check options */ 8067 while ((c = getopt_long(argc, argv, "ft", long_options, NULL)) != -1) { 8068 switch (c) { 8069 case 'f': 8070 fault = B_TRUE; 8071 break; 8072 case 't': 8073 istmp = B_TRUE; 8074 break; 8075 case ZPOOL_OPTION_POWER: 8076 is_power_off = B_TRUE; 8077 break; 8078 case '?': 8079 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8080 optopt); 8081 usage(B_FALSE); 8082 } 8083 } 8084 8085 if (is_power_off && fault) { 8086 (void) fprintf(stderr, 8087 gettext("-0 and -f cannot be used together\n")); 8088 usage(B_FALSE); 8089 return (1); 8090 } 8091 8092 if (is_power_off && istmp) { 8093 (void) fprintf(stderr, 8094 gettext("-0 and -t cannot be used together\n")); 8095 usage(B_FALSE); 8096 return (1); 8097 } 8098 8099 argc -= optind; 8100 argv += optind; 8101 8102 /* get pool name and check number of arguments */ 8103 if (argc < 1) { 8104 (void) fprintf(stderr, gettext("missing pool name\n")); 8105 usage(B_FALSE); 8106 } 8107 if (argc < 2) { 8108 (void) fprintf(stderr, gettext("missing device name\n")); 8109 usage(B_FALSE); 8110 } 8111 8112 poolname = argv[0]; 8113 8114 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 8115 (void) fprintf(stderr, gettext("failed to open pool " 8116 "\"%s\""), poolname); 8117 return (1); 8118 } 8119 8120 for (i = 1; i < argc; i++) { 8121 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); 8122 if (is_power_off) { 8123 /* 8124 * Note: we have to power off first, then set REMOVED, 8125 * or else zpool_vdev_set_removed_state() returns 8126 * EAGAIN. 8127 */ 8128 ret = zpool_power_off(zhp, argv[i]); 8129 if (ret != 0) { 8130 (void) fprintf(stderr, "%s %s %d\n", 8131 gettext("unable to power off slot for"), 8132 argv[i], ret); 8133 } 8134 zpool_vdev_set_removed_state(zhp, guid, VDEV_AUX_NONE); 8135 8136 } else if (fault) { 8137 vdev_aux_t aux; 8138 if (istmp == B_FALSE) { 8139 /* Force the fault to persist across imports */ 8140 aux = VDEV_AUX_EXTERNAL_PERSIST; 8141 } else { 8142 aux = VDEV_AUX_EXTERNAL; 8143 } 8144 8145 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) 8146 ret = 1; 8147 } else { 8148 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 8149 ret = 1; 8150 } 8151 } 8152 8153 zpool_close(zhp); 8154 8155 return (ret); 8156 } 8157 8158 /* 8159 * zpool clear [-nF]|[--power] <pool> [device] 8160 * 8161 * Clear all errors associated with a pool or a particular device. 8162 */ 8163 int 8164 zpool_do_clear(int argc, char **argv) 8165 { 8166 int c; 8167 int ret = 0; 8168 boolean_t dryrun = B_FALSE; 8169 boolean_t do_rewind = B_FALSE; 8170 boolean_t xtreme_rewind = B_FALSE; 8171 boolean_t is_power_on = B_FALSE; 8172 uint32_t rewind_policy = ZPOOL_NO_REWIND; 8173 nvlist_t *policy = NULL; 8174 zpool_handle_t *zhp; 8175 char *pool, *device; 8176 8177 struct option long_options[] = { 8178 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 8179 {0, 0, 0, 0} 8180 }; 8181 8182 /* check options */ 8183 while ((c = getopt_long(argc, argv, "FnX", long_options, 8184 NULL)) != -1) { 8185 switch (c) { 8186 case 'F': 8187 do_rewind = B_TRUE; 8188 break; 8189 case 'n': 8190 dryrun = B_TRUE; 8191 break; 8192 case 'X': 8193 xtreme_rewind = B_TRUE; 8194 break; 8195 case ZPOOL_OPTION_POWER: 8196 is_power_on = B_TRUE; 8197 break; 8198 case '?': 8199 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8200 optopt); 8201 usage(B_FALSE); 8202 } 8203 } 8204 8205 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) 8206 is_power_on = B_TRUE; 8207 8208 argc -= optind; 8209 argv += optind; 8210 8211 if (argc < 1) { 8212 (void) fprintf(stderr, gettext("missing pool name\n")); 8213 usage(B_FALSE); 8214 } 8215 8216 if (argc > 2) { 8217 (void) fprintf(stderr, gettext("too many arguments\n")); 8218 usage(B_FALSE); 8219 } 8220 8221 if ((dryrun || xtreme_rewind) && !do_rewind) { 8222 (void) fprintf(stderr, 8223 gettext("-n or -X only meaningful with -F\n")); 8224 usage(B_FALSE); 8225 } 8226 if (dryrun) 8227 rewind_policy = ZPOOL_TRY_REWIND; 8228 else if (do_rewind) 8229 rewind_policy = ZPOOL_DO_REWIND; 8230 if (xtreme_rewind) 8231 rewind_policy |= ZPOOL_EXTREME_REWIND; 8232 8233 /* In future, further rewind policy choices can be passed along here */ 8234 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 8235 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 8236 rewind_policy) != 0) { 8237 return (1); 8238 } 8239 8240 pool = argv[0]; 8241 device = argc == 2 ? argv[1] : NULL; 8242 8243 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 8244 nvlist_free(policy); 8245 return (1); 8246 } 8247 8248 if (is_power_on) { 8249 if (device == NULL) { 8250 zpool_power_on_pool_and_wait_for_devices(zhp); 8251 } else { 8252 zpool_power_on_and_disk_wait(zhp, device); 8253 } 8254 } 8255 8256 if (zpool_clear(zhp, device, policy) != 0) 8257 ret = 1; 8258 8259 zpool_close(zhp); 8260 8261 nvlist_free(policy); 8262 8263 return (ret); 8264 } 8265 8266 /* 8267 * zpool reguid [-g <guid>] <pool> 8268 */ 8269 int 8270 zpool_do_reguid(int argc, char **argv) 8271 { 8272 uint64_t guid; 8273 uint64_t *guidp = NULL; 8274 int c; 8275 char *endptr; 8276 char *poolname; 8277 zpool_handle_t *zhp; 8278 int ret = 0; 8279 8280 /* check options */ 8281 while ((c = getopt(argc, argv, "g:")) != -1) { 8282 switch (c) { 8283 case 'g': 8284 errno = 0; 8285 guid = strtoull(optarg, &endptr, 10); 8286 if (errno != 0 || *endptr != '\0') { 8287 (void) fprintf(stderr, 8288 gettext("invalid GUID: %s\n"), optarg); 8289 usage(B_FALSE); 8290 } 8291 guidp = &guid; 8292 break; 8293 case '?': 8294 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8295 optopt); 8296 usage(B_FALSE); 8297 } 8298 } 8299 8300 argc -= optind; 8301 argv += optind; 8302 8303 /* get pool name and check number of arguments */ 8304 if (argc < 1) { 8305 (void) fprintf(stderr, gettext("missing pool name\n")); 8306 usage(B_FALSE); 8307 } 8308 8309 if (argc > 1) { 8310 (void) fprintf(stderr, gettext("too many arguments\n")); 8311 usage(B_FALSE); 8312 } 8313 8314 poolname = argv[0]; 8315 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 8316 return (1); 8317 8318 ret = zpool_set_guid(zhp, guidp); 8319 8320 zpool_close(zhp); 8321 return (ret); 8322 } 8323 8324 8325 /* 8326 * zpool reopen <pool> 8327 * 8328 * Reopen the pool so that the kernel can update the sizes of all vdevs. 8329 */ 8330 int 8331 zpool_do_reopen(int argc, char **argv) 8332 { 8333 int c; 8334 int ret = 0; 8335 boolean_t scrub_restart = B_TRUE; 8336 8337 /* check options */ 8338 while ((c = getopt(argc, argv, "n")) != -1) { 8339 switch (c) { 8340 case 'n': 8341 scrub_restart = B_FALSE; 8342 break; 8343 case '?': 8344 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8345 optopt); 8346 usage(B_FALSE); 8347 } 8348 } 8349 8350 argc -= optind; 8351 argv += optind; 8352 8353 /* if argc == 0 we will execute zpool_reopen_one on all pools */ 8354 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8355 B_FALSE, zpool_reopen_one, &scrub_restart); 8356 8357 return (ret); 8358 } 8359 8360 typedef struct scrub_cbdata { 8361 int cb_type; 8362 pool_scrub_cmd_t cb_scrub_cmd; 8363 time_t cb_date_start; 8364 time_t cb_date_end; 8365 } scrub_cbdata_t; 8366 8367 static boolean_t 8368 zpool_has_checkpoint(zpool_handle_t *zhp) 8369 { 8370 nvlist_t *config, *nvroot; 8371 8372 config = zpool_get_config(zhp, NULL); 8373 8374 if (config != NULL) { 8375 pool_checkpoint_stat_t *pcs = NULL; 8376 uint_t c; 8377 8378 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 8379 (void) nvlist_lookup_uint64_array(nvroot, 8380 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 8381 8382 if (pcs == NULL || pcs->pcs_state == CS_NONE) 8383 return (B_FALSE); 8384 8385 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || 8386 pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 8387 return (B_TRUE); 8388 } 8389 8390 return (B_FALSE); 8391 } 8392 8393 static int 8394 scrub_callback(zpool_handle_t *zhp, void *data) 8395 { 8396 scrub_cbdata_t *cb = data; 8397 int err; 8398 8399 /* 8400 * Ignore faulted pools. 8401 */ 8402 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 8403 (void) fprintf(stderr, gettext("cannot scan '%s': pool is " 8404 "currently unavailable\n"), zpool_get_name(zhp)); 8405 return (1); 8406 } 8407 8408 err = zpool_scan_range(zhp, cb->cb_type, cb->cb_scrub_cmd, 8409 cb->cb_date_start, cb->cb_date_end); 8410 if (err == 0 && zpool_has_checkpoint(zhp) && 8411 cb->cb_type == POOL_SCAN_SCRUB) { 8412 (void) printf(gettext("warning: will not scrub state that " 8413 "belongs to the checkpoint of pool '%s'\n"), 8414 zpool_get_name(zhp)); 8415 } 8416 8417 return (err != 0); 8418 } 8419 8420 static int 8421 wait_callback(zpool_handle_t *zhp, void *data) 8422 { 8423 zpool_wait_activity_t *act = data; 8424 return (zpool_wait(zhp, *act)); 8425 } 8426 8427 static time_t 8428 date_string_to_sec(const char *timestr, boolean_t rounding) 8429 { 8430 struct tm tm = {0}; 8431 int adjustment = rounding ? 1 : 0; 8432 8433 /* Allow mktime to determine timezone. */ 8434 tm.tm_isdst = -1; 8435 8436 if (strptime(timestr, "%Y-%m-%d %H:%M", &tm) == NULL) { 8437 if (strptime(timestr, "%Y-%m-%d", &tm) == NULL) { 8438 fprintf(stderr, gettext("Failed to parse the date.\n")); 8439 usage(B_FALSE); 8440 } 8441 adjustment *= 24 * 60 * 60; 8442 } else { 8443 adjustment *= 60; 8444 } 8445 8446 return (mktime(&tm) + adjustment); 8447 } 8448 8449 /* 8450 * zpool scrub [-e | -s | -p | -C | -E | -S] [-w] [-a | <pool> ...] 8451 * 8452 * -a Scrub all pools. 8453 * -e Only scrub blocks in the error log. 8454 * -E End date of scrub. 8455 * -S Start date of scrub. 8456 * -s Stop. Stops any in-progress scrub. 8457 * -p Pause. Pause in-progress scrub. 8458 * -w Wait. Blocks until scrub has completed. 8459 * -C Scrub from last saved txg. 8460 */ 8461 int 8462 zpool_do_scrub(int argc, char **argv) 8463 { 8464 int c; 8465 scrub_cbdata_t cb; 8466 boolean_t wait = B_FALSE; 8467 int error; 8468 8469 cb.cb_type = POOL_SCAN_SCRUB; 8470 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8471 cb.cb_date_start = cb.cb_date_end = 0; 8472 8473 boolean_t is_error_scrub = B_FALSE; 8474 boolean_t is_pause = B_FALSE; 8475 boolean_t is_stop = B_FALSE; 8476 boolean_t is_txg_continue = B_FALSE; 8477 boolean_t scrub_all = B_FALSE; 8478 8479 /* check options */ 8480 while ((c = getopt(argc, argv, "aspweCE:S:")) != -1) { 8481 switch (c) { 8482 case 'a': 8483 scrub_all = B_TRUE; 8484 break; 8485 case 'e': 8486 is_error_scrub = B_TRUE; 8487 break; 8488 case 'E': 8489 /* 8490 * Round the date. It's better to scrub more data than 8491 * less. This also makes the date inclusive. 8492 */ 8493 cb.cb_date_end = date_string_to_sec(optarg, B_TRUE); 8494 break; 8495 case 's': 8496 is_stop = B_TRUE; 8497 break; 8498 case 'S': 8499 cb.cb_date_start = date_string_to_sec(optarg, B_FALSE); 8500 break; 8501 case 'p': 8502 is_pause = B_TRUE; 8503 break; 8504 case 'w': 8505 wait = B_TRUE; 8506 break; 8507 case 'C': 8508 is_txg_continue = B_TRUE; 8509 break; 8510 case '?': 8511 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8512 optopt); 8513 usage(B_FALSE); 8514 } 8515 } 8516 8517 if (is_pause && is_stop) { 8518 (void) fprintf(stderr, gettext("invalid option " 8519 "combination: -s and -p are mutually exclusive\n")); 8520 usage(B_FALSE); 8521 } else if (is_pause && is_txg_continue) { 8522 (void) fprintf(stderr, gettext("invalid option " 8523 "combination: -p and -C are mutually exclusive\n")); 8524 usage(B_FALSE); 8525 } else if (is_stop && is_txg_continue) { 8526 (void) fprintf(stderr, gettext("invalid option " 8527 "combination: -s and -C are mutually exclusive\n")); 8528 usage(B_FALSE); 8529 } else if (is_error_scrub && is_txg_continue) { 8530 (void) fprintf(stderr, gettext("invalid option " 8531 "combination: -e and -C are mutually exclusive\n")); 8532 usage(B_FALSE); 8533 } else { 8534 if (is_error_scrub) 8535 cb.cb_type = POOL_SCAN_ERRORSCRUB; 8536 8537 if (is_pause) { 8538 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; 8539 } else if (is_stop) { 8540 cb.cb_type = POOL_SCAN_NONE; 8541 } else if (is_txg_continue) { 8542 cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG; 8543 } else { 8544 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8545 } 8546 } 8547 8548 if ((cb.cb_date_start != 0 || cb.cb_date_end != 0) && 8549 cb.cb_scrub_cmd != POOL_SCRUB_NORMAL) { 8550 (void) fprintf(stderr, gettext("invalid option combination: " 8551 "start/end date is available only with normal scrub\n")); 8552 usage(B_FALSE); 8553 } 8554 if (cb.cb_date_start != 0 && cb.cb_date_end != 0 && 8555 cb.cb_date_start > cb.cb_date_end) { 8556 (void) fprintf(stderr, gettext("invalid arguments: " 8557 "end date has to be later than start date\n")); 8558 usage(B_FALSE); 8559 } 8560 8561 if (wait && (cb.cb_type == POOL_SCAN_NONE || 8562 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { 8563 (void) fprintf(stderr, gettext("invalid option combination: " 8564 "-w cannot be used with -p or -s\n")); 8565 usage(B_FALSE); 8566 } 8567 8568 argc -= optind; 8569 argv += optind; 8570 8571 if (argc < 1 && !scrub_all) { 8572 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8573 usage(B_FALSE); 8574 } 8575 8576 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8577 B_FALSE, scrub_callback, &cb); 8578 8579 if (wait && !error) { 8580 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; 8581 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8582 B_FALSE, wait_callback, &act); 8583 } 8584 8585 return (error); 8586 } 8587 8588 /* 8589 * zpool resilver <pool> ... 8590 * 8591 * Restarts any in-progress resilver 8592 */ 8593 int 8594 zpool_do_resilver(int argc, char **argv) 8595 { 8596 int c; 8597 scrub_cbdata_t cb; 8598 8599 cb.cb_type = POOL_SCAN_RESILVER; 8600 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8601 cb.cb_date_start = cb.cb_date_end = 0; 8602 8603 /* check options */ 8604 while ((c = getopt(argc, argv, "")) != -1) { 8605 switch (c) { 8606 case '?': 8607 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8608 optopt); 8609 usage(B_FALSE); 8610 } 8611 } 8612 8613 argc -= optind; 8614 argv += optind; 8615 8616 if (argc < 1) { 8617 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8618 usage(B_FALSE); 8619 } 8620 8621 return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8622 B_FALSE, scrub_callback, &cb)); 8623 } 8624 8625 /* 8626 * zpool trim [-d] [-r <rate>] [-c | -s] <-a | pool> [<device> ...] 8627 * 8628 * -a Trim all pools. 8629 * -c Cancel. Ends any in-progress trim. 8630 * -d Secure trim. Requires kernel and device support. 8631 * -r <rate> Sets the TRIM rate in bytes (per second). Supports 8632 * adding a multiplier suffix such as 'k' or 'm'. 8633 * -s Suspend. TRIM can then be restarted with no flags. 8634 * -w Wait. Blocks until trimming has completed. 8635 */ 8636 int 8637 zpool_do_trim(int argc, char **argv) 8638 { 8639 struct option long_options[] = { 8640 {"cancel", no_argument, NULL, 'c'}, 8641 {"secure", no_argument, NULL, 'd'}, 8642 {"rate", required_argument, NULL, 'r'}, 8643 {"suspend", no_argument, NULL, 's'}, 8644 {"wait", no_argument, NULL, 'w'}, 8645 {"all", no_argument, NULL, 'a'}, 8646 {0, 0, 0, 0} 8647 }; 8648 8649 pool_trim_func_t cmd_type = POOL_TRIM_START; 8650 uint64_t rate = 0; 8651 boolean_t secure = B_FALSE; 8652 boolean_t wait = B_FALSE; 8653 boolean_t trimall = B_FALSE; 8654 int error; 8655 8656 int c; 8657 while ((c = getopt_long(argc, argv, "acdr:sw", long_options, NULL)) 8658 != -1) { 8659 switch (c) { 8660 case 'a': 8661 trimall = B_TRUE; 8662 break; 8663 case 'c': 8664 if (cmd_type != POOL_TRIM_START && 8665 cmd_type != POOL_TRIM_CANCEL) { 8666 (void) fprintf(stderr, gettext("-c cannot be " 8667 "combined with other options\n")); 8668 usage(B_FALSE); 8669 } 8670 cmd_type = POOL_TRIM_CANCEL; 8671 break; 8672 case 'd': 8673 if (cmd_type != POOL_TRIM_START) { 8674 (void) fprintf(stderr, gettext("-d cannot be " 8675 "combined with the -c or -s options\n")); 8676 usage(B_FALSE); 8677 } 8678 secure = B_TRUE; 8679 break; 8680 case 'r': 8681 if (cmd_type != POOL_TRIM_START) { 8682 (void) fprintf(stderr, gettext("-r cannot be " 8683 "combined with the -c or -s options\n")); 8684 usage(B_FALSE); 8685 } 8686 if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) { 8687 (void) fprintf(stderr, "%s: %s\n", 8688 gettext("invalid value for rate"), 8689 libzfs_error_description(g_zfs)); 8690 usage(B_FALSE); 8691 } 8692 break; 8693 case 's': 8694 if (cmd_type != POOL_TRIM_START && 8695 cmd_type != POOL_TRIM_SUSPEND) { 8696 (void) fprintf(stderr, gettext("-s cannot be " 8697 "combined with other options\n")); 8698 usage(B_FALSE); 8699 } 8700 cmd_type = POOL_TRIM_SUSPEND; 8701 break; 8702 case 'w': 8703 wait = B_TRUE; 8704 break; 8705 case '?': 8706 if (optopt != 0) { 8707 (void) fprintf(stderr, 8708 gettext("invalid option '%c'\n"), optopt); 8709 } else { 8710 (void) fprintf(stderr, 8711 gettext("invalid option '%s'\n"), 8712 argv[optind - 1]); 8713 } 8714 usage(B_FALSE); 8715 } 8716 } 8717 8718 argc -= optind; 8719 argv += optind; 8720 8721 trimflags_t trim_flags = { 8722 .secure = secure, 8723 .rate = rate, 8724 .wait = wait, 8725 }; 8726 8727 trim_cbdata_t cbdata = { 8728 .trim_flags = trim_flags, 8729 .cmd_type = cmd_type 8730 }; 8731 8732 if (argc < 1 && !trimall) { 8733 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8734 usage(B_FALSE); 8735 return (-1); 8736 } 8737 8738 if (wait && (cmd_type != POOL_TRIM_START)) { 8739 (void) fprintf(stderr, gettext("-w cannot be used with -c or " 8740 "-s options\n")); 8741 usage(B_FALSE); 8742 } 8743 8744 if (trimall && argc > 0) { 8745 (void) fprintf(stderr, gettext("-a cannot be combined with " 8746 "individual zpools or vdevs\n")); 8747 usage(B_FALSE); 8748 } 8749 8750 if (argc == 0 && trimall) { 8751 cbdata.trim_flags.fullpool = B_TRUE; 8752 /* Trim each pool recursively */ 8753 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8754 B_FALSE, zpool_trim_one, &cbdata); 8755 } else if (argc == 1) { 8756 char *poolname = argv[0]; 8757 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 8758 if (zhp == NULL) 8759 return (-1); 8760 /* no individual leaf vdevs specified, so add them all */ 8761 error = zpool_trim_one(zhp, &cbdata); 8762 zpool_close(zhp); 8763 } else { 8764 char *poolname = argv[0]; 8765 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 8766 if (zhp == NULL) 8767 return (-1); 8768 /* leaf vdevs specified, trim only those */ 8769 cbdata.trim_flags.fullpool = B_FALSE; 8770 nvlist_t *vdevs = fnvlist_alloc(); 8771 for (int i = 1; i < argc; i++) { 8772 fnvlist_add_boolean(vdevs, argv[i]); 8773 } 8774 error = zpool_trim(zhp, cbdata.cmd_type, vdevs, 8775 &cbdata.trim_flags); 8776 fnvlist_free(vdevs); 8777 zpool_close(zhp); 8778 } 8779 8780 return (error); 8781 } 8782 8783 /* 8784 * Converts a total number of seconds to a human readable string broken 8785 * down in to days/hours/minutes/seconds. 8786 */ 8787 static void 8788 secs_to_dhms(uint64_t total, char *buf) 8789 { 8790 uint64_t days = total / 60 / 60 / 24; 8791 uint64_t hours = (total / 60 / 60) % 24; 8792 uint64_t mins = (total / 60) % 60; 8793 uint64_t secs = (total % 60); 8794 8795 if (days > 0) { 8796 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", 8797 (u_longlong_t)days, (u_longlong_t)hours, 8798 (u_longlong_t)mins, (u_longlong_t)secs); 8799 } else { 8800 (void) sprintf(buf, "%02llu:%02llu:%02llu", 8801 (u_longlong_t)hours, (u_longlong_t)mins, 8802 (u_longlong_t)secs); 8803 } 8804 } 8805 8806 /* 8807 * Print out detailed error scrub status. 8808 */ 8809 static void 8810 print_err_scrub_status(pool_scan_stat_t *ps) 8811 { 8812 time_t start, end, pause; 8813 uint64_t total_secs_left; 8814 uint64_t secs_left, mins_left, hours_left, days_left; 8815 uint64_t examined, to_be_examined; 8816 8817 if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) { 8818 return; 8819 } 8820 8821 (void) printf(gettext(" scrub: ")); 8822 8823 start = ps->pss_error_scrub_start; 8824 end = ps->pss_error_scrub_end; 8825 pause = ps->pss_pass_error_scrub_pause; 8826 examined = ps->pss_error_scrub_examined; 8827 to_be_examined = ps->pss_error_scrub_to_be_examined; 8828 8829 assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB); 8830 8831 if (ps->pss_error_scrub_state == DSS_FINISHED) { 8832 total_secs_left = end - start; 8833 days_left = total_secs_left / 60 / 60 / 24; 8834 hours_left = (total_secs_left / 60 / 60) % 24; 8835 mins_left = (total_secs_left / 60) % 60; 8836 secs_left = (total_secs_left % 60); 8837 8838 (void) printf(gettext("scrubbed %llu error blocks in %llu days " 8839 "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined, 8840 (u_longlong_t)days_left, (u_longlong_t)hours_left, 8841 (u_longlong_t)mins_left, (u_longlong_t)secs_left, 8842 ctime(&end)); 8843 8844 return; 8845 } else if (ps->pss_error_scrub_state == DSS_CANCELED) { 8846 (void) printf(gettext("error scrub canceled on %s"), 8847 ctime(&end)); 8848 return; 8849 } 8850 assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING); 8851 8852 /* Error scrub is in progress. */ 8853 if (pause == 0) { 8854 (void) printf(gettext("error scrub in progress since %s"), 8855 ctime(&start)); 8856 } else { 8857 (void) printf(gettext("error scrub paused since %s"), 8858 ctime(&pause)); 8859 (void) printf(gettext("\terror scrub started on %s"), 8860 ctime(&start)); 8861 } 8862 8863 double fraction_done = (double)examined / (to_be_examined + examined); 8864 (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error" 8865 " blocks"), 100 * fraction_done, (u_longlong_t)examined); 8866 8867 (void) printf("\n"); 8868 } 8869 8870 /* 8871 * Print out detailed scrub status. 8872 */ 8873 static void 8874 print_scan_scrub_resilver_status(pool_scan_stat_t *ps) 8875 { 8876 time_t start, end, pause; 8877 uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i; 8878 uint64_t elapsed, scan_rate, issue_rate; 8879 double fraction_done; 8880 char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7]; 8881 char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32]; 8882 8883 printf(" "); 8884 printf_color(ANSI_BOLD, gettext("scan:")); 8885 printf(" "); 8886 8887 /* If there's never been a scan, there's not much to say. */ 8888 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 8889 ps->pss_func >= POOL_SCAN_FUNCS) { 8890 (void) printf(gettext("none requested\n")); 8891 return; 8892 } 8893 8894 start = ps->pss_start_time; 8895 end = ps->pss_end_time; 8896 pause = ps->pss_pass_scrub_pause; 8897 8898 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); 8899 8900 int is_resilver = ps->pss_func == POOL_SCAN_RESILVER; 8901 int is_scrub = ps->pss_func == POOL_SCAN_SCRUB; 8902 assert(is_resilver || is_scrub); 8903 8904 /* Scan is finished or canceled. */ 8905 if (ps->pss_state == DSS_FINISHED) { 8906 secs_to_dhms(end - start, time_buf); 8907 8908 if (is_scrub) { 8909 (void) printf(gettext("scrub repaired %s " 8910 "in %s with %llu errors on %s"), processed_buf, 8911 time_buf, (u_longlong_t)ps->pss_errors, 8912 ctime(&end)); 8913 } else if (is_resilver) { 8914 (void) printf(gettext("resilvered %s " 8915 "in %s with %llu errors on %s"), processed_buf, 8916 time_buf, (u_longlong_t)ps->pss_errors, 8917 ctime(&end)); 8918 } 8919 return; 8920 } else if (ps->pss_state == DSS_CANCELED) { 8921 if (is_scrub) { 8922 (void) printf(gettext("scrub canceled on %s"), 8923 ctime(&end)); 8924 } else if (is_resilver) { 8925 (void) printf(gettext("resilver canceled on %s"), 8926 ctime(&end)); 8927 } 8928 return; 8929 } 8930 8931 assert(ps->pss_state == DSS_SCANNING); 8932 8933 /* Scan is in progress. Resilvers can't be paused. */ 8934 if (is_scrub) { 8935 if (pause == 0) { 8936 (void) printf(gettext("scrub in progress since %s"), 8937 ctime(&start)); 8938 } else { 8939 (void) printf(gettext("scrub paused since %s"), 8940 ctime(&pause)); 8941 (void) printf(gettext("\tscrub started on %s"), 8942 ctime(&start)); 8943 } 8944 } else if (is_resilver) { 8945 (void) printf(gettext("resilver in progress since %s"), 8946 ctime(&start)); 8947 } 8948 8949 scanned = ps->pss_examined; 8950 pass_scanned = ps->pss_pass_exam; 8951 issued = ps->pss_issued; 8952 pass_issued = ps->pss_pass_issued; 8953 total_s = ps->pss_to_examine; 8954 total_i = ps->pss_to_examine - ps->pss_skipped; 8955 8956 /* we are only done with a block once we have issued the IO for it */ 8957 fraction_done = (double)issued / total_i; 8958 8959 /* elapsed time for this pass, rounding up to 1 if it's 0 */ 8960 elapsed = time(NULL) - ps->pss_pass_start; 8961 elapsed -= ps->pss_pass_scrub_spent_paused; 8962 elapsed = (elapsed != 0) ? elapsed : 1; 8963 8964 scan_rate = pass_scanned / elapsed; 8965 issue_rate = pass_issued / elapsed; 8966 8967 /* format all of the numbers we will be reporting */ 8968 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); 8969 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); 8970 zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf)); 8971 zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf)); 8972 8973 /* do not print estimated time if we have a paused scrub */ 8974 (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf); 8975 if (pause == 0 && scan_rate > 0) { 8976 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); 8977 (void) printf(gettext(" at %s/s"), srate_buf); 8978 } 8979 (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf); 8980 if (pause == 0 && issue_rate > 0) { 8981 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); 8982 (void) printf(gettext(" at %s/s"), irate_buf); 8983 } 8984 (void) printf(gettext("\n")); 8985 8986 if (is_resilver) { 8987 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 8988 processed_buf, 100 * fraction_done); 8989 } else if (is_scrub) { 8990 (void) printf(gettext("\t%s repaired, %.2f%% done"), 8991 processed_buf, 100 * fraction_done); 8992 } 8993 8994 if (pause == 0) { 8995 /* 8996 * Only provide an estimate iff: 8997 * 1) we haven't yet issued all we expected, and 8998 * 2) the issue rate exceeds 10 MB/s, and 8999 * 3) it's either: 9000 * a) a resilver which has started repairs, or 9001 * b) a scrub which has entered the issue phase. 9002 */ 9003 if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 && 9004 ((is_resilver && ps->pss_processed > 0) || 9005 (is_scrub && issued > 0))) { 9006 secs_to_dhms((total_i - issued) / issue_rate, time_buf); 9007 (void) printf(gettext(", %s to go\n"), time_buf); 9008 } else { 9009 (void) printf(gettext(", no estimated " 9010 "completion time\n")); 9011 } 9012 } else { 9013 (void) printf(gettext("\n")); 9014 } 9015 } 9016 9017 static void 9018 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name) 9019 { 9020 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) 9021 return; 9022 9023 printf(" "); 9024 printf_color(ANSI_BOLD, gettext("scan:")); 9025 printf(" "); 9026 9027 uint64_t bytes_scanned = vrs->vrs_bytes_scanned; 9028 uint64_t bytes_issued = vrs->vrs_bytes_issued; 9029 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; 9030 uint64_t bytes_est_s = vrs->vrs_bytes_est; 9031 uint64_t bytes_est_i = vrs->vrs_bytes_est; 9032 if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8) 9033 bytes_est_i -= vrs->vrs_pass_bytes_skipped; 9034 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / 9035 (vrs->vrs_pass_time_ms + 1)) * 1000; 9036 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / 9037 (vrs->vrs_pass_time_ms + 1)) * 1000; 9038 double scan_pct = MIN((double)bytes_scanned * 100 / 9039 (bytes_est_s + 1), 100); 9040 9041 /* Format all of the numbers we will be reporting */ 9042 char bytes_scanned_buf[7], bytes_issued_buf[7]; 9043 char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7]; 9044 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; 9045 zfs_nicebytes(bytes_scanned, bytes_scanned_buf, 9046 sizeof (bytes_scanned_buf)); 9047 zfs_nicebytes(bytes_issued, bytes_issued_buf, 9048 sizeof (bytes_issued_buf)); 9049 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, 9050 sizeof (bytes_rebuilt_buf)); 9051 zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf)); 9052 zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf)); 9053 9054 time_t start = vrs->vrs_start_time; 9055 time_t end = vrs->vrs_end_time; 9056 9057 /* Rebuild is finished or canceled. */ 9058 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { 9059 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); 9060 (void) printf(gettext("resilvered (%s) %s in %s " 9061 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, 9062 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); 9063 return; 9064 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { 9065 (void) printf(gettext("resilver (%s) canceled on %s"), 9066 vdev_name, ctime(&end)); 9067 return; 9068 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9069 (void) printf(gettext("resilver (%s) in progress since %s"), 9070 vdev_name, ctime(&start)); 9071 } 9072 9073 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); 9074 9075 (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf, 9076 bytes_est_s_buf); 9077 if (scan_rate > 0) { 9078 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); 9079 (void) printf(gettext(" at %s/s"), scan_rate_buf); 9080 } 9081 (void) printf(gettext(", %s / %s issued"), bytes_issued_buf, 9082 bytes_est_i_buf); 9083 if (issue_rate > 0) { 9084 zfs_nicebytes(issue_rate, issue_rate_buf, 9085 sizeof (issue_rate_buf)); 9086 (void) printf(gettext(" at %s/s"), issue_rate_buf); 9087 } 9088 (void) printf(gettext("\n")); 9089 9090 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 9091 bytes_rebuilt_buf, scan_pct); 9092 9093 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9094 if (bytes_est_s >= bytes_scanned && 9095 scan_rate >= 10 * 1024 * 1024) { 9096 secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate, 9097 time_buf); 9098 (void) printf(gettext(", %s to go\n"), time_buf); 9099 } else { 9100 (void) printf(gettext(", no estimated " 9101 "completion time\n")); 9102 } 9103 } else { 9104 (void) printf(gettext("\n")); 9105 } 9106 } 9107 9108 /* 9109 * Print rebuild status for top-level vdevs. 9110 */ 9111 static void 9112 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) 9113 { 9114 nvlist_t **child; 9115 uint_t children; 9116 9117 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9118 &child, &children) != 0) 9119 children = 0; 9120 9121 for (uint_t c = 0; c < children; c++) { 9122 vdev_rebuild_stat_t *vrs; 9123 uint_t i; 9124 9125 if (nvlist_lookup_uint64_array(child[c], 9126 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 9127 char *name = zpool_vdev_name(g_zfs, zhp, 9128 child[c], VDEV_NAME_TYPE_ID); 9129 print_rebuild_status_impl(vrs, i, name); 9130 free(name); 9131 } 9132 } 9133 } 9134 9135 /* 9136 * As we don't scrub checkpointed blocks, we want to warn the user that we 9137 * skipped scanning some blocks if a checkpoint exists or existed at any 9138 * time during the scan. If a sequential instead of healing reconstruction 9139 * was performed then the blocks were reconstructed. However, their checksums 9140 * have not been verified so we still print the warning. 9141 */ 9142 static void 9143 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) 9144 { 9145 if (ps == NULL || pcs == NULL) 9146 return; 9147 9148 if (pcs->pcs_state == CS_NONE || 9149 pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 9150 return; 9151 9152 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); 9153 9154 if (ps->pss_state == DSS_NONE) 9155 return; 9156 9157 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && 9158 ps->pss_end_time < pcs->pcs_start_time) 9159 return; 9160 9161 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { 9162 (void) printf(gettext(" scan warning: skipped blocks " 9163 "that are only referenced by the checkpoint.\n")); 9164 } else { 9165 assert(ps->pss_state == DSS_SCANNING); 9166 (void) printf(gettext(" scan warning: skipping blocks " 9167 "that are only referenced by the checkpoint.\n")); 9168 } 9169 } 9170 9171 /* 9172 * Returns B_TRUE if there is an active rebuild in progress. Otherwise, 9173 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for 9174 * the last completed (or cancelled) rebuild. 9175 */ 9176 static boolean_t 9177 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) 9178 { 9179 nvlist_t **child; 9180 uint_t children; 9181 boolean_t rebuilding = B_FALSE; 9182 uint64_t end_time = 0; 9183 9184 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9185 &child, &children) != 0) 9186 children = 0; 9187 9188 for (uint_t c = 0; c < children; c++) { 9189 vdev_rebuild_stat_t *vrs; 9190 uint_t i; 9191 9192 if (nvlist_lookup_uint64_array(child[c], 9193 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 9194 9195 if (vrs->vrs_end_time > end_time) 9196 end_time = vrs->vrs_end_time; 9197 9198 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9199 rebuilding = B_TRUE; 9200 end_time = 0; 9201 break; 9202 } 9203 } 9204 } 9205 9206 if (rebuild_end_time != NULL) 9207 *rebuild_end_time = end_time; 9208 9209 return (rebuilding); 9210 } 9211 9212 static void 9213 vdev_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9214 int depth, boolean_t isspare, char *parent, nvlist_t *item) 9215 { 9216 nvlist_t *vds, **child, *ch = NULL; 9217 uint_t vsc, children; 9218 vdev_stat_t *vs; 9219 char *vname; 9220 uint64_t notpresent; 9221 const char *type, *path; 9222 9223 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 9224 &child, &children) != 0) 9225 children = 0; 9226 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 9227 (uint64_t **)&vs, &vsc) == 0); 9228 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 9229 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) 9230 return; 9231 9232 if (cb->cb_print_unhealthy && depth > 0 && 9233 for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) { 9234 return; 9235 } 9236 vname = zpool_vdev_name(g_zfs, zhp, nv, 9237 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 9238 vds = fnvlist_alloc(); 9239 fill_vdev_info(vds, zhp, vname, B_FALSE, cb->cb_json_as_int); 9240 if (cb->cb_flat_vdevs && parent != NULL) { 9241 fnvlist_add_string(vds, "parent", parent); 9242 } 9243 9244 if (isspare) { 9245 if (vs->vs_aux == VDEV_AUX_SPARED) { 9246 fnvlist_add_string(vds, "state", "INUSE"); 9247 used_by_other(zhp, nv, vds); 9248 } else if (vs->vs_state == VDEV_STATE_HEALTHY) 9249 fnvlist_add_string(vds, "state", "AVAIL"); 9250 } else { 9251 if (vs->vs_alloc) { 9252 nice_num_str_nvlist(vds, "alloc_space", vs->vs_alloc, 9253 cb->cb_literal, cb->cb_json_as_int, 9254 ZFS_NICENUM_BYTES); 9255 } 9256 if (vs->vs_space) { 9257 nice_num_str_nvlist(vds, "total_space", vs->vs_space, 9258 cb->cb_literal, cb->cb_json_as_int, 9259 ZFS_NICENUM_BYTES); 9260 } 9261 if (vs->vs_dspace) { 9262 nice_num_str_nvlist(vds, "def_space", vs->vs_dspace, 9263 cb->cb_literal, cb->cb_json_as_int, 9264 ZFS_NICENUM_BYTES); 9265 } 9266 if (vs->vs_rsize) { 9267 nice_num_str_nvlist(vds, "rep_dev_size", vs->vs_rsize, 9268 cb->cb_literal, cb->cb_json_as_int, 9269 ZFS_NICENUM_BYTES); 9270 } 9271 if (vs->vs_esize) { 9272 nice_num_str_nvlist(vds, "ex_dev_size", vs->vs_esize, 9273 cb->cb_literal, cb->cb_json_as_int, 9274 ZFS_NICENUM_BYTES); 9275 } 9276 if (vs->vs_self_healed) { 9277 nice_num_str_nvlist(vds, "self_healed", 9278 vs->vs_self_healed, cb->cb_literal, 9279 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9280 } 9281 if (vs->vs_pspace) { 9282 nice_num_str_nvlist(vds, "phys_space", vs->vs_pspace, 9283 cb->cb_literal, cb->cb_json_as_int, 9284 ZFS_NICENUM_BYTES); 9285 } 9286 nice_num_str_nvlist(vds, "read_errors", vs->vs_read_errors, 9287 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9288 nice_num_str_nvlist(vds, "write_errors", vs->vs_write_errors, 9289 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9290 nice_num_str_nvlist(vds, "checksum_errors", 9291 vs->vs_checksum_errors, cb->cb_literal, 9292 cb->cb_json_as_int, ZFS_NICENUM_1024); 9293 if (vs->vs_scan_processed) { 9294 nice_num_str_nvlist(vds, "scan_processed", 9295 vs->vs_scan_processed, cb->cb_literal, 9296 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9297 } 9298 if (vs->vs_checkpoint_space) { 9299 nice_num_str_nvlist(vds, "checkpoint_space", 9300 vs->vs_checkpoint_space, cb->cb_literal, 9301 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9302 } 9303 if (vs->vs_resilver_deferred) { 9304 nice_num_str_nvlist(vds, "resilver_deferred", 9305 vs->vs_resilver_deferred, B_TRUE, 9306 cb->cb_json_as_int, ZFS_NICENUM_1024); 9307 } 9308 if (children == 0) { 9309 nice_num_str_nvlist(vds, "slow_ios", vs->vs_slow_ios, 9310 cb->cb_literal, cb->cb_json_as_int, 9311 ZFS_NICENUM_1024); 9312 } 9313 if (cb->cb_print_power) { 9314 if (children == 0) { 9315 /* Only leaf vdevs have physical slots */ 9316 switch (zpool_power_current_state(zhp, (char *) 9317 fnvlist_lookup_string(nv, 9318 ZPOOL_CONFIG_PATH))) { 9319 case 0: 9320 fnvlist_add_string(vds, "power_state", 9321 "off"); 9322 break; 9323 case 1: 9324 fnvlist_add_string(vds, "power_state", 9325 "on"); 9326 break; 9327 default: 9328 fnvlist_add_string(vds, "power_state", 9329 "-"); 9330 } 9331 } else { 9332 fnvlist_add_string(vds, "power_state", "-"); 9333 } 9334 } 9335 } 9336 9337 if (cb->cb_print_dio_verify) { 9338 nice_num_str_nvlist(vds, "dio_verify_errors", 9339 vs->vs_dio_verify_errors, cb->cb_literal, 9340 cb->cb_json_as_int, ZFS_NICENUM_1024); 9341 } 9342 9343 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 9344 ¬present) == 0) { 9345 nice_num_str_nvlist(vds, ZPOOL_CONFIG_NOT_PRESENT, 9346 1, B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9347 fnvlist_add_string(vds, "was", 9348 fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH)); 9349 } else if (vs->vs_aux != VDEV_AUX_NONE) { 9350 fnvlist_add_string(vds, "aux", vdev_aux_str[vs->vs_aux]); 9351 } else if (children == 0 && !isspare && 9352 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && 9353 VDEV_STAT_VALID(vs_physical_ashift, vsc) && 9354 vs->vs_configured_ashift < vs->vs_physical_ashift) { 9355 nice_num_str_nvlist(vds, "configured_ashift", 9356 vs->vs_configured_ashift, B_TRUE, cb->cb_json_as_int, 9357 ZFS_NICENUM_1024); 9358 nice_num_str_nvlist(vds, "physical_ashift", 9359 vs->vs_physical_ashift, B_TRUE, cb->cb_json_as_int, 9360 ZFS_NICENUM_1024); 9361 } 9362 if (vs->vs_scan_removing != 0) { 9363 nice_num_str_nvlist(vds, "removing", vs->vs_scan_removing, 9364 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9365 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) { 9366 nice_num_str_nvlist(vds, "noalloc", vs->vs_noalloc, 9367 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9368 } 9369 9370 if (cb->vcdl != NULL) { 9371 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 9372 zpool_nvlist_cmd(cb->vcdl, zpool_get_name(zhp), 9373 path, vds); 9374 } 9375 } 9376 9377 if (children == 0) { 9378 if (cb->cb_print_vdev_init) { 9379 if (vs->vs_initialize_state != 0) { 9380 uint64_t st = vs->vs_initialize_state; 9381 fnvlist_add_string(vds, "init_state", 9382 vdev_init_state_str[st]); 9383 nice_num_str_nvlist(vds, "initialized", 9384 vs->vs_initialize_bytes_done, 9385 cb->cb_literal, cb->cb_json_as_int, 9386 ZFS_NICENUM_BYTES); 9387 nice_num_str_nvlist(vds, "to_initialize", 9388 vs->vs_initialize_bytes_est, 9389 cb->cb_literal, cb->cb_json_as_int, 9390 ZFS_NICENUM_BYTES); 9391 nice_num_str_nvlist(vds, "init_time", 9392 vs->vs_initialize_action_time, 9393 cb->cb_literal, cb->cb_json_as_int, 9394 ZFS_NICE_TIMESTAMP); 9395 nice_num_str_nvlist(vds, "init_errors", 9396 vs->vs_initialize_errors, 9397 cb->cb_literal, cb->cb_json_as_int, 9398 ZFS_NICENUM_1024); 9399 } else { 9400 fnvlist_add_string(vds, "init_state", 9401 "UNINITIALIZED"); 9402 } 9403 } 9404 if (cb->cb_print_vdev_trim) { 9405 if (vs->vs_trim_notsup == 0) { 9406 if (vs->vs_trim_state != 0) { 9407 uint64_t st = vs->vs_trim_state; 9408 fnvlist_add_string(vds, "trim_state", 9409 vdev_trim_state_str[st]); 9410 nice_num_str_nvlist(vds, "trimmed", 9411 vs->vs_trim_bytes_done, 9412 cb->cb_literal, cb->cb_json_as_int, 9413 ZFS_NICENUM_BYTES); 9414 nice_num_str_nvlist(vds, "to_trim", 9415 vs->vs_trim_bytes_est, 9416 cb->cb_literal, cb->cb_json_as_int, 9417 ZFS_NICENUM_BYTES); 9418 nice_num_str_nvlist(vds, "trim_time", 9419 vs->vs_trim_action_time, 9420 cb->cb_literal, cb->cb_json_as_int, 9421 ZFS_NICE_TIMESTAMP); 9422 nice_num_str_nvlist(vds, "trim_errors", 9423 vs->vs_trim_errors, 9424 cb->cb_literal, cb->cb_json_as_int, 9425 ZFS_NICENUM_1024); 9426 } else 9427 fnvlist_add_string(vds, "trim_state", 9428 "UNTRIMMED"); 9429 } 9430 nice_num_str_nvlist(vds, "trim_notsup", 9431 vs->vs_trim_notsup, B_TRUE, 9432 cb->cb_json_as_int, ZFS_NICENUM_1024); 9433 } 9434 } else { 9435 ch = fnvlist_alloc(); 9436 } 9437 9438 if (cb->cb_flat_vdevs && children == 0) { 9439 fnvlist_add_nvlist(item, vname, vds); 9440 } 9441 9442 for (int c = 0; c < children; c++) { 9443 uint64_t islog = B_FALSE, ishole = B_FALSE; 9444 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 9445 &islog); 9446 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 9447 &ishole); 9448 if (islog || ishole) 9449 continue; 9450 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 9451 continue; 9452 if (cb->cb_flat_vdevs) { 9453 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare, 9454 vname, item); 9455 } 9456 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare, 9457 vname, ch); 9458 } 9459 9460 if (ch != NULL) { 9461 if (!nvlist_empty(ch)) 9462 fnvlist_add_nvlist(vds, "vdevs", ch); 9463 fnvlist_free(ch); 9464 } 9465 fnvlist_add_nvlist(item, vname, vds); 9466 fnvlist_free(vds); 9467 free(vname); 9468 } 9469 9470 static void 9471 class_vdevs_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9472 const char *class, nvlist_t *item) 9473 { 9474 uint_t c, children; 9475 nvlist_t **child; 9476 nvlist_t *class_obj = NULL; 9477 9478 if (!cb->cb_flat_vdevs) 9479 class_obj = fnvlist_alloc(); 9480 9481 assert(zhp != NULL || !cb->cb_verbose); 9482 9483 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 9484 &children) != 0) 9485 return; 9486 9487 for (c = 0; c < children; c++) { 9488 uint64_t is_log = B_FALSE; 9489 const char *bias = NULL; 9490 const char *type = NULL; 9491 char *name = zpool_vdev_name(g_zfs, zhp, child[c], 9492 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 9493 9494 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 9495 &is_log); 9496 9497 if (is_log) { 9498 bias = (char *)VDEV_ALLOC_CLASS_LOGS; 9499 } else { 9500 (void) nvlist_lookup_string(child[c], 9501 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 9502 (void) nvlist_lookup_string(child[c], 9503 ZPOOL_CONFIG_TYPE, &type); 9504 } 9505 9506 if (bias == NULL || strcmp(bias, class) != 0) 9507 continue; 9508 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 9509 continue; 9510 9511 if (cb->cb_flat_vdevs) { 9512 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE, 9513 NULL, item); 9514 } else { 9515 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE, 9516 NULL, class_obj); 9517 } 9518 free(name); 9519 } 9520 if (!cb->cb_flat_vdevs) { 9521 if (!nvlist_empty(class_obj)) 9522 fnvlist_add_nvlist(item, class, class_obj); 9523 fnvlist_free(class_obj); 9524 } 9525 } 9526 9527 static void 9528 l2cache_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9529 nvlist_t *item) 9530 { 9531 nvlist_t *l2c = NULL, **l2cache; 9532 uint_t nl2cache; 9533 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 9534 &l2cache, &nl2cache) == 0) { 9535 if (nl2cache == 0) 9536 return; 9537 if (!cb->cb_flat_vdevs) 9538 l2c = fnvlist_alloc(); 9539 for (int i = 0; i < nl2cache; i++) { 9540 if (cb->cb_flat_vdevs) { 9541 vdev_stats_nvlist(zhp, cb, l2cache[i], 2, 9542 B_FALSE, NULL, item); 9543 } else { 9544 vdev_stats_nvlist(zhp, cb, l2cache[i], 2, 9545 B_FALSE, NULL, l2c); 9546 } 9547 } 9548 } 9549 if (!cb->cb_flat_vdevs) { 9550 if (!nvlist_empty(l2c)) 9551 fnvlist_add_nvlist(item, "l2cache", l2c); 9552 fnvlist_free(l2c); 9553 } 9554 } 9555 9556 static void 9557 spares_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9558 nvlist_t *item) 9559 { 9560 nvlist_t *sp = NULL, **spares; 9561 uint_t nspares; 9562 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 9563 &spares, &nspares) == 0) { 9564 if (nspares == 0) 9565 return; 9566 if (!cb->cb_flat_vdevs) 9567 sp = fnvlist_alloc(); 9568 for (int i = 0; i < nspares; i++) { 9569 if (cb->cb_flat_vdevs) { 9570 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE, 9571 NULL, item); 9572 } else { 9573 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE, 9574 NULL, sp); 9575 } 9576 } 9577 } 9578 if (!cb->cb_flat_vdevs) { 9579 if (!nvlist_empty(sp)) 9580 fnvlist_add_nvlist(item, "spares", sp); 9581 fnvlist_free(sp); 9582 } 9583 } 9584 9585 static void 9586 errors_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item) 9587 { 9588 uint64_t nerr; 9589 nvlist_t *config = zpool_get_config(zhp, NULL); 9590 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 9591 &nerr) == 0) { 9592 nice_num_str_nvlist(item, ZPOOL_CONFIG_ERRCOUNT, nerr, 9593 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9594 if (nerr != 0 && cb->cb_verbose) { 9595 nvlist_t *nverrlist = NULL; 9596 if (zpool_get_errlog(zhp, &nverrlist) == 0) { 9597 int i = 0; 9598 int count = 0; 9599 size_t len = MAXPATHLEN * 2; 9600 nvpair_t *elem = NULL; 9601 9602 for (nvpair_t *pair = 9603 nvlist_next_nvpair(nverrlist, NULL); 9604 pair != NULL; 9605 pair = nvlist_next_nvpair(nverrlist, pair)) 9606 count++; 9607 char **errl = (char **)malloc( 9608 count * sizeof (char *)); 9609 9610 while ((elem = nvlist_next_nvpair(nverrlist, 9611 elem)) != NULL) { 9612 nvlist_t *nv; 9613 uint64_t dsobj, obj; 9614 9615 verify(nvpair_value_nvlist(elem, 9616 &nv) == 0); 9617 verify(nvlist_lookup_uint64(nv, 9618 ZPOOL_ERR_DATASET, &dsobj) == 0); 9619 verify(nvlist_lookup_uint64(nv, 9620 ZPOOL_ERR_OBJECT, &obj) == 0); 9621 errl[i] = safe_malloc(len); 9622 zpool_obj_to_path(zhp, dsobj, obj, 9623 errl[i++], len); 9624 } 9625 nvlist_free(nverrlist); 9626 fnvlist_add_string_array(item, "errlist", 9627 (const char **)errl, count); 9628 for (int i = 0; i < count; ++i) 9629 free(errl[i]); 9630 free(errl); 9631 } else 9632 fnvlist_add_string(item, "errlist", 9633 strerror(errno)); 9634 } 9635 } 9636 } 9637 9638 static void 9639 ddt_stats_nvlist(ddt_stat_t *dds, status_cbdata_t *cb, nvlist_t *item) 9640 { 9641 nice_num_str_nvlist(item, "blocks", dds->dds_blocks, 9642 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9643 nice_num_str_nvlist(item, "logical_size", dds->dds_lsize, 9644 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9645 nice_num_str_nvlist(item, "physical_size", dds->dds_psize, 9646 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9647 nice_num_str_nvlist(item, "deflated_size", dds->dds_dsize, 9648 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9649 nice_num_str_nvlist(item, "ref_blocks", dds->dds_ref_blocks, 9650 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9651 nice_num_str_nvlist(item, "ref_lsize", dds->dds_ref_lsize, 9652 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9653 nice_num_str_nvlist(item, "ref_psize", dds->dds_ref_psize, 9654 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9655 nice_num_str_nvlist(item, "ref_dsize", dds->dds_ref_dsize, 9656 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9657 } 9658 9659 static void 9660 dedup_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item) 9661 { 9662 nvlist_t *config; 9663 if (cb->cb_dedup_stats) { 9664 ddt_histogram_t *ddh; 9665 ddt_stat_t *dds; 9666 ddt_object_t *ddo; 9667 nvlist_t *ddt_stat, *ddt_obj, *dedup; 9668 uint_t c; 9669 uint64_t cspace_prop; 9670 9671 config = zpool_get_config(zhp, NULL); 9672 if (nvlist_lookup_uint64_array(config, 9673 ZPOOL_CONFIG_DDT_OBJ_STATS, (uint64_t **)&ddo, &c) != 0) 9674 return; 9675 9676 dedup = fnvlist_alloc(); 9677 ddt_obj = fnvlist_alloc(); 9678 nice_num_str_nvlist(dedup, "obj_count", ddo->ddo_count, 9679 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9680 if (ddo->ddo_count == 0) { 9681 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS, 9682 ddt_obj); 9683 fnvlist_add_nvlist(item, "dedup_stats", dedup); 9684 fnvlist_free(ddt_obj); 9685 fnvlist_free(dedup); 9686 return; 9687 } else { 9688 nice_num_str_nvlist(dedup, "dspace", ddo->ddo_dspace, 9689 cb->cb_literal, cb->cb_json_as_int, 9690 ZFS_NICENUM_1024); 9691 nice_num_str_nvlist(dedup, "mspace", ddo->ddo_mspace, 9692 cb->cb_literal, cb->cb_json_as_int, 9693 ZFS_NICENUM_1024); 9694 /* 9695 * Squash cached size into in-core size to handle race. 9696 * Only include cached size if it is available. 9697 */ 9698 cspace_prop = zpool_get_prop_int(zhp, 9699 ZPOOL_PROP_DEDUPCACHED, NULL); 9700 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace); 9701 nice_num_str_nvlist(dedup, "cspace", cspace_prop, 9702 cb->cb_literal, cb->cb_json_as_int, 9703 ZFS_NICENUM_1024); 9704 } 9705 9706 ddt_stat = fnvlist_alloc(); 9707 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 9708 (uint64_t **)&dds, &c) == 0) { 9709 nvlist_t *total = fnvlist_alloc(); 9710 if (dds->dds_blocks == 0) 9711 fnvlist_add_string(total, "blocks", "0"); 9712 else 9713 ddt_stats_nvlist(dds, cb, total); 9714 fnvlist_add_nvlist(ddt_stat, "total", total); 9715 fnvlist_free(total); 9716 } 9717 if (nvlist_lookup_uint64_array(config, 9718 ZPOOL_CONFIG_DDT_HISTOGRAM, (uint64_t **)&ddh, &c) == 0) { 9719 nvlist_t *hist = fnvlist_alloc(); 9720 nvlist_t *entry = NULL; 9721 char buf[16]; 9722 for (int h = 0; h < 64; h++) { 9723 if (ddh->ddh_stat[h].dds_blocks != 0) { 9724 entry = fnvlist_alloc(); 9725 ddt_stats_nvlist(&ddh->ddh_stat[h], cb, 9726 entry); 9727 snprintf(buf, 16, "%d", h); 9728 fnvlist_add_nvlist(hist, buf, entry); 9729 fnvlist_free(entry); 9730 } 9731 } 9732 if (!nvlist_empty(hist)) 9733 fnvlist_add_nvlist(ddt_stat, "histogram", hist); 9734 fnvlist_free(hist); 9735 } 9736 9737 if (!nvlist_empty(ddt_obj)) { 9738 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS, 9739 ddt_obj); 9740 } 9741 fnvlist_free(ddt_obj); 9742 if (!nvlist_empty(ddt_stat)) { 9743 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_STATS, 9744 ddt_stat); 9745 } 9746 fnvlist_free(ddt_stat); 9747 if (!nvlist_empty(dedup)) 9748 fnvlist_add_nvlist(item, "dedup_stats", dedup); 9749 fnvlist_free(dedup); 9750 } 9751 } 9752 9753 static void 9754 raidz_expand_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9755 nvlist_t *nvroot, nvlist_t *item) 9756 { 9757 uint_t c; 9758 pool_raidz_expand_stat_t *pres = NULL; 9759 if (nvlist_lookup_uint64_array(nvroot, 9760 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c) == 0) { 9761 nvlist_t **child; 9762 uint_t children; 9763 nvlist_t *nv = fnvlist_alloc(); 9764 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9765 &child, &children) == 0); 9766 assert(pres->pres_expanding_vdev < children); 9767 char *name = 9768 zpool_vdev_name(g_zfs, zhp, 9769 child[pres->pres_expanding_vdev], 0); 9770 fill_vdev_info(nv, zhp, name, B_FALSE, cb->cb_json_as_int); 9771 fnvlist_add_string(nv, "state", 9772 pool_scan_state_str[pres->pres_state]); 9773 nice_num_str_nvlist(nv, "expanding_vdev", 9774 pres->pres_expanding_vdev, B_TRUE, cb->cb_json_as_int, 9775 ZFS_NICENUM_1024); 9776 nice_num_str_nvlist(nv, "start_time", pres->pres_start_time, 9777 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9778 nice_num_str_nvlist(nv, "end_time", pres->pres_end_time, 9779 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9780 nice_num_str_nvlist(nv, "to_reflow", pres->pres_to_reflow, 9781 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9782 nice_num_str_nvlist(nv, "reflowed", pres->pres_reflowed, 9783 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9784 nice_num_str_nvlist(nv, "waiting_for_resilver", 9785 pres->pres_waiting_for_resilver, B_TRUE, 9786 cb->cb_json_as_int, ZFS_NICENUM_1024); 9787 fnvlist_add_nvlist(item, ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, nv); 9788 fnvlist_free(nv); 9789 free(name); 9790 } 9791 } 9792 9793 static void 9794 checkpoint_status_nvlist(nvlist_t *nvroot, status_cbdata_t *cb, 9795 nvlist_t *item) 9796 { 9797 uint_t c; 9798 pool_checkpoint_stat_t *pcs = NULL; 9799 if (nvlist_lookup_uint64_array(nvroot, 9800 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c) == 0) { 9801 nvlist_t *nv = fnvlist_alloc(); 9802 fnvlist_add_string(nv, "state", 9803 checkpoint_state_str[pcs->pcs_state]); 9804 nice_num_str_nvlist(nv, "start_time", 9805 pcs->pcs_start_time, cb->cb_literal, cb->cb_json_as_int, 9806 ZFS_NICE_TIMESTAMP); 9807 nice_num_str_nvlist(nv, "space", 9808 pcs->pcs_space, cb->cb_literal, cb->cb_json_as_int, 9809 ZFS_NICENUM_BYTES); 9810 fnvlist_add_nvlist(item, ZPOOL_CONFIG_CHECKPOINT_STATS, nv); 9811 fnvlist_free(nv); 9812 } 9813 } 9814 9815 static void 9816 removal_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9817 nvlist_t *nvroot, nvlist_t *item) 9818 { 9819 uint_t c; 9820 pool_removal_stat_t *prs = NULL; 9821 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_REMOVAL_STATS, 9822 (uint64_t **)&prs, &c) == 0) { 9823 if (prs->prs_state != DSS_NONE) { 9824 nvlist_t **child; 9825 uint_t children; 9826 verify(nvlist_lookup_nvlist_array(nvroot, 9827 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0); 9828 assert(prs->prs_removing_vdev < children); 9829 char *vdev_name = zpool_vdev_name(g_zfs, zhp, 9830 child[prs->prs_removing_vdev], B_TRUE); 9831 nvlist_t *nv = fnvlist_alloc(); 9832 fill_vdev_info(nv, zhp, vdev_name, B_FALSE, 9833 cb->cb_json_as_int); 9834 fnvlist_add_string(nv, "state", 9835 pool_scan_state_str[prs->prs_state]); 9836 nice_num_str_nvlist(nv, "removing_vdev", 9837 prs->prs_removing_vdev, B_TRUE, cb->cb_json_as_int, 9838 ZFS_NICENUM_1024); 9839 nice_num_str_nvlist(nv, "start_time", 9840 prs->prs_start_time, cb->cb_literal, 9841 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9842 nice_num_str_nvlist(nv, "end_time", prs->prs_end_time, 9843 cb->cb_literal, cb->cb_json_as_int, 9844 ZFS_NICE_TIMESTAMP); 9845 nice_num_str_nvlist(nv, "to_copy", prs->prs_to_copy, 9846 cb->cb_literal, cb->cb_json_as_int, 9847 ZFS_NICENUM_BYTES); 9848 nice_num_str_nvlist(nv, "copied", prs->prs_copied, 9849 cb->cb_literal, cb->cb_json_as_int, 9850 ZFS_NICENUM_BYTES); 9851 nice_num_str_nvlist(nv, "mapping_memory", 9852 prs->prs_mapping_memory, cb->cb_literal, 9853 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9854 fnvlist_add_nvlist(item, 9855 ZPOOL_CONFIG_REMOVAL_STATS, nv); 9856 fnvlist_free(nv); 9857 free(vdev_name); 9858 } 9859 } 9860 } 9861 9862 static void 9863 scan_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9864 nvlist_t *nvroot, nvlist_t *item) 9865 { 9866 pool_scan_stat_t *ps = NULL; 9867 uint_t c; 9868 nvlist_t *scan = fnvlist_alloc(); 9869 nvlist_t **child; 9870 uint_t children; 9871 9872 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 9873 (uint64_t **)&ps, &c) == 0) { 9874 fnvlist_add_string(scan, "function", 9875 pool_scan_func_str[ps->pss_func]); 9876 fnvlist_add_string(scan, "state", 9877 pool_scan_state_str[ps->pss_state]); 9878 nice_num_str_nvlist(scan, "start_time", ps->pss_start_time, 9879 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9880 nice_num_str_nvlist(scan, "end_time", ps->pss_end_time, 9881 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9882 nice_num_str_nvlist(scan, "to_examine", ps->pss_to_examine, 9883 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9884 nice_num_str_nvlist(scan, "examined", ps->pss_examined, 9885 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9886 nice_num_str_nvlist(scan, "skipped", ps->pss_skipped, 9887 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9888 nice_num_str_nvlist(scan, "processed", ps->pss_processed, 9889 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9890 nice_num_str_nvlist(scan, "errors", ps->pss_errors, 9891 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9892 nice_num_str_nvlist(scan, "bytes_per_scan", ps->pss_pass_exam, 9893 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9894 nice_num_str_nvlist(scan, "pass_start", ps->pss_pass_start, 9895 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9896 nice_num_str_nvlist(scan, "scrub_pause", 9897 ps->pss_pass_scrub_pause, cb->cb_literal, 9898 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9899 nice_num_str_nvlist(scan, "scrub_spent_paused", 9900 ps->pss_pass_scrub_spent_paused, 9901 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9902 nice_num_str_nvlist(scan, "issued_bytes_per_scan", 9903 ps->pss_pass_issued, cb->cb_literal, 9904 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9905 nice_num_str_nvlist(scan, "issued", ps->pss_issued, 9906 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9907 if (ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB && 9908 ps->pss_error_scrub_start > ps->pss_start_time) { 9909 fnvlist_add_string(scan, "err_scrub_func", 9910 pool_scan_func_str[ps->pss_error_scrub_func]); 9911 fnvlist_add_string(scan, "err_scrub_state", 9912 pool_scan_state_str[ps->pss_error_scrub_state]); 9913 nice_num_str_nvlist(scan, "err_scrub_start_time", 9914 ps->pss_error_scrub_start, 9915 cb->cb_literal, cb->cb_json_as_int, 9916 ZFS_NICE_TIMESTAMP); 9917 nice_num_str_nvlist(scan, "err_scrub_end_time", 9918 ps->pss_error_scrub_end, 9919 cb->cb_literal, cb->cb_json_as_int, 9920 ZFS_NICE_TIMESTAMP); 9921 nice_num_str_nvlist(scan, "err_scrub_examined", 9922 ps->pss_error_scrub_examined, 9923 cb->cb_literal, cb->cb_json_as_int, 9924 ZFS_NICENUM_1024); 9925 nice_num_str_nvlist(scan, "err_scrub_to_examine", 9926 ps->pss_error_scrub_to_be_examined, 9927 cb->cb_literal, cb->cb_json_as_int, 9928 ZFS_NICENUM_1024); 9929 nice_num_str_nvlist(scan, "err_scrub_pause", 9930 ps->pss_pass_error_scrub_pause, 9931 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9932 } 9933 } 9934 9935 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9936 &child, &children) == 0) { 9937 vdev_rebuild_stat_t *vrs; 9938 uint_t i; 9939 char *name; 9940 nvlist_t *nv; 9941 nvlist_t *rebuild = fnvlist_alloc(); 9942 uint64_t st; 9943 for (uint_t c = 0; c < children; c++) { 9944 if (nvlist_lookup_uint64_array(child[c], 9945 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, 9946 &i) == 0) { 9947 if (vrs->vrs_state != VDEV_REBUILD_NONE) { 9948 nv = fnvlist_alloc(); 9949 name = zpool_vdev_name(g_zfs, zhp, 9950 child[c], VDEV_NAME_TYPE_ID); 9951 fill_vdev_info(nv, zhp, name, B_FALSE, 9952 cb->cb_json_as_int); 9953 st = vrs->vrs_state; 9954 fnvlist_add_string(nv, "state", 9955 vdev_rebuild_state_str[st]); 9956 nice_num_str_nvlist(nv, "start_time", 9957 vrs->vrs_start_time, cb->cb_literal, 9958 cb->cb_json_as_int, 9959 ZFS_NICE_TIMESTAMP); 9960 nice_num_str_nvlist(nv, "end_time", 9961 vrs->vrs_end_time, cb->cb_literal, 9962 cb->cb_json_as_int, 9963 ZFS_NICE_TIMESTAMP); 9964 nice_num_str_nvlist(nv, "scan_time", 9965 vrs->vrs_scan_time_ms * 1000000, 9966 cb->cb_literal, cb->cb_json_as_int, 9967 ZFS_NICENUM_TIME); 9968 nice_num_str_nvlist(nv, "scanned", 9969 vrs->vrs_bytes_scanned, 9970 cb->cb_literal, cb->cb_json_as_int, 9971 ZFS_NICENUM_BYTES); 9972 nice_num_str_nvlist(nv, "issued", 9973 vrs->vrs_bytes_issued, 9974 cb->cb_literal, cb->cb_json_as_int, 9975 ZFS_NICENUM_BYTES); 9976 nice_num_str_nvlist(nv, "rebuilt", 9977 vrs->vrs_bytes_rebuilt, 9978 cb->cb_literal, cb->cb_json_as_int, 9979 ZFS_NICENUM_BYTES); 9980 nice_num_str_nvlist(nv, "to_scan", 9981 vrs->vrs_bytes_est, cb->cb_literal, 9982 cb->cb_json_as_int, 9983 ZFS_NICENUM_BYTES); 9984 nice_num_str_nvlist(nv, "errors", 9985 vrs->vrs_errors, cb->cb_literal, 9986 cb->cb_json_as_int, 9987 ZFS_NICENUM_1024); 9988 nice_num_str_nvlist(nv, "pass_time", 9989 vrs->vrs_pass_time_ms * 1000000, 9990 cb->cb_literal, cb->cb_json_as_int, 9991 ZFS_NICENUM_TIME); 9992 nice_num_str_nvlist(nv, "pass_scanned", 9993 vrs->vrs_pass_bytes_scanned, 9994 cb->cb_literal, cb->cb_json_as_int, 9995 ZFS_NICENUM_BYTES); 9996 nice_num_str_nvlist(nv, "pass_issued", 9997 vrs->vrs_pass_bytes_issued, 9998 cb->cb_literal, cb->cb_json_as_int, 9999 ZFS_NICENUM_BYTES); 10000 nice_num_str_nvlist(nv, "pass_skipped", 10001 vrs->vrs_pass_bytes_skipped, 10002 cb->cb_literal, cb->cb_json_as_int, 10003 ZFS_NICENUM_BYTES); 10004 fnvlist_add_nvlist(rebuild, name, nv); 10005 free(name); 10006 } 10007 } 10008 } 10009 if (!nvlist_empty(rebuild)) 10010 fnvlist_add_nvlist(scan, "rebuild_stats", rebuild); 10011 fnvlist_free(rebuild); 10012 } 10013 10014 if (!nvlist_empty(scan)) 10015 fnvlist_add_nvlist(item, ZPOOL_CONFIG_SCAN_STATS, scan); 10016 fnvlist_free(scan); 10017 } 10018 10019 /* 10020 * Print the scan status. 10021 */ 10022 static void 10023 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) 10024 { 10025 uint64_t rebuild_end_time = 0, resilver_end_time = 0; 10026 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; 10027 boolean_t have_errorscrub = B_FALSE; 10028 boolean_t active_resilver = B_FALSE; 10029 pool_checkpoint_stat_t *pcs = NULL; 10030 pool_scan_stat_t *ps = NULL; 10031 uint_t c; 10032 time_t scrub_start = 0, errorscrub_start = 0; 10033 10034 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 10035 (uint64_t **)&ps, &c) == 0) { 10036 if (ps->pss_func == POOL_SCAN_RESILVER) { 10037 resilver_end_time = ps->pss_end_time; 10038 active_resilver = (ps->pss_state == DSS_SCANNING); 10039 } 10040 10041 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); 10042 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); 10043 scrub_start = ps->pss_start_time; 10044 if (c > offsetof(pool_scan_stat_t, 10045 pss_pass_error_scrub_pause) / 8) { 10046 have_errorscrub = (ps->pss_error_scrub_func == 10047 POOL_SCAN_ERRORSCRUB); 10048 errorscrub_start = ps->pss_error_scrub_start; 10049 } 10050 } 10051 10052 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); 10053 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); 10054 10055 /* Always print the scrub status when available. */ 10056 if (have_scrub && scrub_start > errorscrub_start) 10057 print_scan_scrub_resilver_status(ps); 10058 else if (have_errorscrub && errorscrub_start >= scrub_start) 10059 print_err_scrub_status(ps); 10060 10061 /* 10062 * When there is an active resilver or rebuild print its status. 10063 * Otherwise print the status of the last resilver or rebuild. 10064 */ 10065 if (active_resilver || (!active_rebuild && have_resilver && 10066 resilver_end_time && resilver_end_time > rebuild_end_time)) { 10067 print_scan_scrub_resilver_status(ps); 10068 } else if (active_rebuild || (!active_resilver && have_rebuild && 10069 rebuild_end_time && rebuild_end_time > resilver_end_time)) { 10070 print_rebuild_status(zhp, nvroot); 10071 } 10072 10073 (void) nvlist_lookup_uint64_array(nvroot, 10074 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10075 print_checkpoint_scan_warning(ps, pcs); 10076 } 10077 10078 /* 10079 * Print out detailed removal status. 10080 */ 10081 static void 10082 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) 10083 { 10084 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 10085 time_t start, end; 10086 nvlist_t *config, *nvroot; 10087 nvlist_t **child; 10088 uint_t children; 10089 char *vdev_name; 10090 10091 if (prs == NULL || prs->prs_state == DSS_NONE) 10092 return; 10093 10094 /* 10095 * Determine name of vdev. 10096 */ 10097 config = zpool_get_config(zhp, NULL); 10098 nvroot = fnvlist_lookup_nvlist(config, 10099 ZPOOL_CONFIG_VDEV_TREE); 10100 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 10101 &child, &children) == 0); 10102 assert(prs->prs_removing_vdev < children); 10103 vdev_name = zpool_vdev_name(g_zfs, zhp, 10104 child[prs->prs_removing_vdev], B_TRUE); 10105 10106 printf_color(ANSI_BOLD, gettext("remove: ")); 10107 10108 start = prs->prs_start_time; 10109 end = prs->prs_end_time; 10110 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); 10111 10112 /* 10113 * Removal is finished or canceled. 10114 */ 10115 if (prs->prs_state == DSS_FINISHED) { 10116 uint64_t minutes_taken = (end - start) / 60; 10117 10118 (void) printf(gettext("Removal of vdev %llu copied %s " 10119 "in %lluh%um, completed on %s"), 10120 (longlong_t)prs->prs_removing_vdev, 10121 copied_buf, 10122 (u_longlong_t)(minutes_taken / 60), 10123 (uint_t)(minutes_taken % 60), 10124 ctime((time_t *)&end)); 10125 } else if (prs->prs_state == DSS_CANCELED) { 10126 (void) printf(gettext("Removal of %s canceled on %s"), 10127 vdev_name, ctime(&end)); 10128 } else { 10129 uint64_t copied, total, elapsed, rate, mins_left, hours_left; 10130 double fraction_done; 10131 10132 assert(prs->prs_state == DSS_SCANNING); 10133 10134 /* 10135 * Removal is in progress. 10136 */ 10137 (void) printf(gettext( 10138 "Evacuation of %s in progress since %s"), 10139 vdev_name, ctime(&start)); 10140 10141 copied = prs->prs_copied > 0 ? prs->prs_copied : 1; 10142 total = prs->prs_to_copy; 10143 fraction_done = (double)copied / total; 10144 10145 /* elapsed time for this pass */ 10146 elapsed = time(NULL) - prs->prs_start_time; 10147 elapsed = elapsed > 0 ? elapsed : 1; 10148 rate = copied / elapsed; 10149 rate = rate > 0 ? rate : 1; 10150 mins_left = ((total - copied) / rate) / 60; 10151 hours_left = mins_left / 60; 10152 10153 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 10154 zfs_nicenum(total, total_buf, sizeof (total_buf)); 10155 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 10156 10157 /* 10158 * do not print estimated time if hours_left is more than 10159 * 30 days 10160 */ 10161 (void) printf(gettext( 10162 "\t%s copied out of %s at %s/s, %.2f%% done"), 10163 examined_buf, total_buf, rate_buf, 100 * fraction_done); 10164 if (hours_left < (30 * 24)) { 10165 (void) printf(gettext(", %lluh%um to go\n"), 10166 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 10167 } else { 10168 (void) printf(gettext( 10169 ", (copy is slow, no estimated time)\n")); 10170 } 10171 } 10172 free(vdev_name); 10173 10174 if (prs->prs_mapping_memory > 0) { 10175 char mem_buf[7]; 10176 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); 10177 (void) printf(gettext( 10178 "\t%s memory used for removed device mappings\n"), 10179 mem_buf); 10180 } 10181 } 10182 10183 /* 10184 * Print out detailed raidz expansion status. 10185 */ 10186 static void 10187 print_raidz_expand_status(zpool_handle_t *zhp, pool_raidz_expand_stat_t *pres) 10188 { 10189 char copied_buf[7]; 10190 10191 if (pres == NULL || pres->pres_state == DSS_NONE) 10192 return; 10193 10194 /* 10195 * Determine name of vdev. 10196 */ 10197 nvlist_t *config = zpool_get_config(zhp, NULL); 10198 nvlist_t *nvroot = fnvlist_lookup_nvlist(config, 10199 ZPOOL_CONFIG_VDEV_TREE); 10200 nvlist_t **child; 10201 uint_t children; 10202 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 10203 &child, &children) == 0); 10204 assert(pres->pres_expanding_vdev < children); 10205 10206 printf_color(ANSI_BOLD, gettext("expand: ")); 10207 10208 time_t start = pres->pres_start_time; 10209 time_t end = pres->pres_end_time; 10210 char *vname = 10211 zpool_vdev_name(g_zfs, zhp, child[pres->pres_expanding_vdev], 0); 10212 zfs_nicenum(pres->pres_reflowed, copied_buf, sizeof (copied_buf)); 10213 10214 /* 10215 * Expansion is finished or canceled. 10216 */ 10217 if (pres->pres_state == DSS_FINISHED) { 10218 char time_buf[32]; 10219 secs_to_dhms(end - start, time_buf); 10220 10221 (void) printf(gettext("expanded %s-%u copied %s in %s, " 10222 "on %s"), vname, (int)pres->pres_expanding_vdev, 10223 copied_buf, time_buf, ctime((time_t *)&end)); 10224 } else { 10225 char examined_buf[7], total_buf[7], rate_buf[7]; 10226 uint64_t copied, total, elapsed, rate, secs_left; 10227 double fraction_done; 10228 10229 assert(pres->pres_state == DSS_SCANNING); 10230 10231 /* 10232 * Expansion is in progress. 10233 */ 10234 (void) printf(gettext( 10235 "expansion of %s-%u in progress since %s"), 10236 vname, (int)pres->pres_expanding_vdev, ctime(&start)); 10237 10238 copied = pres->pres_reflowed > 0 ? pres->pres_reflowed : 1; 10239 total = pres->pres_to_reflow; 10240 fraction_done = (double)copied / total; 10241 10242 /* elapsed time for this pass */ 10243 elapsed = time(NULL) - pres->pres_start_time; 10244 elapsed = elapsed > 0 ? elapsed : 1; 10245 rate = copied / elapsed; 10246 rate = rate > 0 ? rate : 1; 10247 secs_left = (total - copied) / rate; 10248 10249 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 10250 zfs_nicenum(total, total_buf, sizeof (total_buf)); 10251 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 10252 10253 /* 10254 * do not print estimated time if hours_left is more than 10255 * 30 days 10256 */ 10257 (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"), 10258 examined_buf, total_buf, rate_buf, 100 * fraction_done); 10259 if (pres->pres_waiting_for_resilver) { 10260 (void) printf(gettext(", paused for resilver or " 10261 "clear\n")); 10262 } else if (secs_left < (30 * 24 * 3600)) { 10263 char time_buf[32]; 10264 secs_to_dhms(secs_left, time_buf); 10265 (void) printf(gettext(", %s to go\n"), time_buf); 10266 } else { 10267 (void) printf(gettext( 10268 ", (copy is slow, no estimated time)\n")); 10269 } 10270 } 10271 free(vname); 10272 } 10273 static void 10274 print_checkpoint_status(pool_checkpoint_stat_t *pcs) 10275 { 10276 time_t start; 10277 char space_buf[7]; 10278 10279 if (pcs == NULL || pcs->pcs_state == CS_NONE) 10280 return; 10281 10282 (void) printf(gettext("checkpoint: ")); 10283 10284 start = pcs->pcs_start_time; 10285 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); 10286 10287 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { 10288 char *date = ctime(&start); 10289 10290 /* 10291 * ctime() adds a newline at the end of the generated 10292 * string, thus the weird format specifier and the 10293 * strlen() call used to chop it off from the output. 10294 */ 10295 (void) printf(gettext("created %.*s, consumes %s\n"), 10296 (int)(strlen(date) - 1), date, space_buf); 10297 return; 10298 } 10299 10300 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 10301 10302 (void) printf(gettext("discarding, %s remaining.\n"), 10303 space_buf); 10304 } 10305 10306 static void 10307 print_error_log(zpool_handle_t *zhp) 10308 { 10309 nvlist_t *nverrlist = NULL; 10310 nvpair_t *elem; 10311 char *pathname; 10312 size_t len = MAXPATHLEN * 2; 10313 10314 if (zpool_get_errlog(zhp, &nverrlist) != 0) 10315 return; 10316 10317 (void) printf("errors: Permanent errors have been " 10318 "detected in the following files:\n\n"); 10319 10320 pathname = safe_malloc(len); 10321 elem = NULL; 10322 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 10323 nvlist_t *nv; 10324 uint64_t dsobj, obj; 10325 10326 verify(nvpair_value_nvlist(elem, &nv) == 0); 10327 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 10328 &dsobj) == 0); 10329 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 10330 &obj) == 0); 10331 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 10332 (void) printf("%7s %s\n", "", pathname); 10333 } 10334 free(pathname); 10335 nvlist_free(nverrlist); 10336 } 10337 10338 static void 10339 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, 10340 uint_t nspares) 10341 { 10342 uint_t i; 10343 char *name; 10344 10345 if (nspares == 0) 10346 return; 10347 10348 (void) printf(gettext("\tspares\n")); 10349 10350 for (i = 0; i < nspares; i++) { 10351 name = zpool_vdev_name(g_zfs, zhp, spares[i], 10352 cb->cb_name_flags); 10353 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); 10354 free(name); 10355 } 10356 } 10357 10358 static void 10359 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, 10360 uint_t nl2cache) 10361 { 10362 uint_t i; 10363 char *name; 10364 10365 if (nl2cache == 0) 10366 return; 10367 10368 (void) printf(gettext("\tcache\n")); 10369 10370 for (i = 0; i < nl2cache; i++) { 10371 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], 10372 cb->cb_name_flags); 10373 print_status_config(zhp, cb, name, l2cache[i], 2, 10374 B_FALSE, NULL); 10375 free(name); 10376 } 10377 } 10378 10379 static void 10380 print_dedup_stats(zpool_handle_t *zhp, nvlist_t *config, boolean_t literal) 10381 { 10382 ddt_histogram_t *ddh; 10383 ddt_stat_t *dds; 10384 ddt_object_t *ddo; 10385 uint_t c; 10386 /* Extra space provided for literal display */ 10387 char dspace[32], mspace[32], cspace[32]; 10388 uint64_t cspace_prop; 10389 enum zfs_nicenum_format format; 10390 zprop_source_t src; 10391 10392 /* 10393 * If the pool was faulted then we may not have been able to 10394 * obtain the config. Otherwise, if we have anything in the dedup 10395 * table continue processing the stats. 10396 */ 10397 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 10398 (uint64_t **)&ddo, &c) != 0) 10399 return; 10400 10401 (void) printf("\n"); 10402 (void) printf(gettext(" dedup: ")); 10403 if (ddo->ddo_count == 0) { 10404 (void) printf(gettext("no DDT entries\n")); 10405 return; 10406 } 10407 10408 /* 10409 * Squash cached size into in-core size to handle race. 10410 * Only include cached size if it is available. 10411 */ 10412 cspace_prop = zpool_get_prop_int(zhp, ZPOOL_PROP_DEDUPCACHED, &src); 10413 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace); 10414 format = literal ? ZFS_NICENUM_RAW : ZFS_NICENUM_1024; 10415 zfs_nicenum_format(cspace_prop, cspace, sizeof (cspace), format); 10416 zfs_nicenum_format(ddo->ddo_dspace, dspace, sizeof (dspace), format); 10417 zfs_nicenum_format(ddo->ddo_mspace, mspace, sizeof (mspace), format); 10418 (void) printf("DDT entries %llu, size %s on disk, %s in core", 10419 (u_longlong_t)ddo->ddo_count, 10420 dspace, 10421 mspace); 10422 if (src != ZPROP_SRC_DEFAULT) { 10423 (void) printf(", %s cached (%.02f%%)", 10424 cspace, 10425 (double)cspace_prop / (double)ddo->ddo_mspace * 100.0); 10426 } 10427 (void) printf("\n"); 10428 10429 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 10430 (uint64_t **)&dds, &c) == 0); 10431 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 10432 (uint64_t **)&ddh, &c) == 0); 10433 zpool_dump_ddt(dds, ddh); 10434 } 10435 10436 #define ST_SIZE 4096 10437 #define AC_SIZE 2048 10438 10439 static void 10440 print_status_reason(zpool_handle_t *zhp, status_cbdata_t *cbp, 10441 zpool_status_t reason, zpool_errata_t errata, nvlist_t *item) 10442 { 10443 char status[ST_SIZE]; 10444 char action[AC_SIZE]; 10445 memset(status, 0, ST_SIZE); 10446 memset(action, 0, AC_SIZE); 10447 10448 switch (reason) { 10449 case ZPOOL_STATUS_MISSING_DEV_R: 10450 snprintf(status, ST_SIZE, gettext("One or more devices could " 10451 "not be opened. Sufficient replicas exist for\n\tthe pool " 10452 "to continue functioning in a degraded state.\n")); 10453 snprintf(action, AC_SIZE, gettext("Attach the missing device " 10454 "and online it using 'zpool online'.\n")); 10455 break; 10456 10457 case ZPOOL_STATUS_MISSING_DEV_NR: 10458 snprintf(status, ST_SIZE, gettext("One or more devices could " 10459 "not be opened. There are insufficient\n\treplicas for the" 10460 " pool to continue functioning.\n")); 10461 snprintf(action, AC_SIZE, gettext("Attach the missing device " 10462 "and online it using 'zpool online'.\n")); 10463 break; 10464 10465 case ZPOOL_STATUS_CORRUPT_LABEL_R: 10466 snprintf(status, ST_SIZE, gettext("One or more devices could " 10467 "not be used because the label is missing or\n\tinvalid. " 10468 "Sufficient replicas exist for the pool to continue\n\t" 10469 "functioning in a degraded state.\n")); 10470 snprintf(action, AC_SIZE, gettext("Replace the device using " 10471 "'zpool replace'.\n")); 10472 break; 10473 10474 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 10475 snprintf(status, ST_SIZE, gettext("One or more devices could " 10476 "not be used because the label is missing \n\tor invalid. " 10477 "There are insufficient replicas for the pool to " 10478 "continue\n\tfunctioning.\n")); 10479 zpool_explain_recover(zpool_get_handle(zhp), 10480 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL), 10481 action, AC_SIZE); 10482 break; 10483 10484 case ZPOOL_STATUS_FAILING_DEV: 10485 snprintf(status, ST_SIZE, gettext("One or more devices has " 10486 "experienced an unrecoverable error. An\n\tattempt was " 10487 "made to correct the error. Applications are " 10488 "unaffected.\n")); 10489 snprintf(action, AC_SIZE, gettext("Determine if the " 10490 "device needs to be replaced, and clear the errors\n\tusing" 10491 " 'zpool clear' or replace the device with 'zpool " 10492 "replace'.\n")); 10493 break; 10494 10495 case ZPOOL_STATUS_OFFLINE_DEV: 10496 snprintf(status, ST_SIZE, gettext("One or more devices has " 10497 "been taken offline by the administrator.\n\tSufficient " 10498 "replicas exist for the pool to continue functioning in " 10499 "a\n\tdegraded state.\n")); 10500 snprintf(action, AC_SIZE, gettext("Online the device " 10501 "using 'zpool online' or replace the device with\n\t'zpool " 10502 "replace'.\n")); 10503 break; 10504 10505 case ZPOOL_STATUS_REMOVED_DEV: 10506 snprintf(status, ST_SIZE, gettext("One or more devices have " 10507 "been removed.\n\tSufficient replicas exist for the pool " 10508 "to continue functioning in a\n\tdegraded state.\n")); 10509 snprintf(action, AC_SIZE, gettext("Online the device " 10510 "using zpool online' or replace the device with\n\t'zpool " 10511 "replace'.\n")); 10512 break; 10513 10514 case ZPOOL_STATUS_RESILVERING: 10515 case ZPOOL_STATUS_REBUILDING: 10516 snprintf(status, ST_SIZE, gettext("One or more devices is " 10517 "currently being resilvered. The pool will\n\tcontinue " 10518 "to function, possibly in a degraded state.\n")); 10519 snprintf(action, AC_SIZE, gettext("Wait for the resilver to " 10520 "complete.\n")); 10521 break; 10522 10523 case ZPOOL_STATUS_REBUILD_SCRUB: 10524 snprintf(status, ST_SIZE, gettext("One or more devices have " 10525 "been sequentially resilvered, scrubbing\n\tthe pool " 10526 "is recommended.\n")); 10527 snprintf(action, AC_SIZE, gettext("Use 'zpool scrub' to " 10528 "verify all data checksums.\n")); 10529 break; 10530 10531 case ZPOOL_STATUS_CORRUPT_DATA: 10532 snprintf(status, ST_SIZE, gettext("One or more devices has " 10533 "experienced an error resulting in data\n\tcorruption. " 10534 "Applications may be affected.\n")); 10535 snprintf(action, AC_SIZE, gettext("Restore the file in question" 10536 " if possible. Otherwise restore the\n\tentire pool from " 10537 "backup.\n")); 10538 break; 10539 10540 case ZPOOL_STATUS_CORRUPT_POOL: 10541 snprintf(status, ST_SIZE, gettext("The pool metadata is " 10542 "corrupted and the pool cannot be opened.\n")); 10543 zpool_explain_recover(zpool_get_handle(zhp), 10544 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL), 10545 action, AC_SIZE); 10546 break; 10547 10548 case ZPOOL_STATUS_VERSION_OLDER: 10549 snprintf(status, ST_SIZE, gettext("The pool is formatted using " 10550 "a legacy on-disk format. The pool can\n\tstill be used, " 10551 "but some features are unavailable.\n")); 10552 snprintf(action, AC_SIZE, gettext("Upgrade the pool using " 10553 "'zpool upgrade'. Once this is done, the\n\tpool will no " 10554 "longer be accessible on software that does not support\n\t" 10555 "feature flags.\n")); 10556 break; 10557 10558 case ZPOOL_STATUS_VERSION_NEWER: 10559 snprintf(status, ST_SIZE, gettext("The pool has been upgraded " 10560 "to a newer, incompatible on-disk version.\n\tThe pool " 10561 "cannot be accessed on this system.\n")); 10562 snprintf(action, AC_SIZE, gettext("Access the pool from a " 10563 "system running more recent software, or\n\trestore the " 10564 "pool from backup.\n")); 10565 break; 10566 10567 case ZPOOL_STATUS_FEAT_DISABLED: 10568 snprintf(status, ST_SIZE, gettext("Some supported and " 10569 "requested features are not enabled on the pool.\n\t" 10570 "The pool can still be used, but some features are " 10571 "unavailable.\n")); 10572 snprintf(action, AC_SIZE, gettext("Enable all features using " 10573 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 10574 "longer be accessible by software that does not support\n\t" 10575 "the features. See zpool-features(7) for details.\n")); 10576 break; 10577 10578 case ZPOOL_STATUS_COMPATIBILITY_ERR: 10579 snprintf(status, ST_SIZE, gettext("This pool has a " 10580 "compatibility list specified, but it could not be\n\t" 10581 "read/parsed at this time. The pool can still be used, " 10582 "but this\n\tshould be investigated.\n")); 10583 snprintf(action, AC_SIZE, gettext("Check the value of the " 10584 "'compatibility' property against the\n\t" 10585 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " 10586 ZPOOL_DATA_COMPAT_D ".\n")); 10587 break; 10588 10589 case ZPOOL_STATUS_INCOMPATIBLE_FEAT: 10590 snprintf(status, ST_SIZE, gettext("One or more features " 10591 "are enabled on the pool despite not being\n\t" 10592 "requested by the 'compatibility' property.\n")); 10593 snprintf(action, AC_SIZE, gettext("Consider setting " 10594 "'compatibility' to an appropriate value, or\n\t" 10595 "adding needed features to the relevant file in\n\t" 10596 ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n")); 10597 break; 10598 10599 case ZPOOL_STATUS_UNSUP_FEAT_READ: 10600 snprintf(status, ST_SIZE, gettext("The pool cannot be accessed " 10601 "on this system because it uses the\n\tfollowing feature(s)" 10602 " not supported on this system:\n")); 10603 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status, 10604 1024); 10605 snprintf(action, AC_SIZE, gettext("Access the pool from a " 10606 "system that supports the required feature(s),\n\tor " 10607 "restore the pool from backup.\n")); 10608 break; 10609 10610 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 10611 snprintf(status, ST_SIZE, gettext("The pool can only be " 10612 "accessed in read-only mode on this system. It\n\tcannot be" 10613 " accessed in read-write mode because it uses the " 10614 "following\n\tfeature(s) not supported on this system:\n")); 10615 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status, 10616 1024); 10617 snprintf(action, AC_SIZE, gettext("The pool cannot be accessed " 10618 "in read-write mode. Import the pool with\n" 10619 "\t\"-o readonly=on\", access the pool from a system that " 10620 "supports the\n\trequired feature(s), or restore the " 10621 "pool from backup.\n")); 10622 break; 10623 10624 case ZPOOL_STATUS_FAULTED_DEV_R: 10625 snprintf(status, ST_SIZE, gettext("One or more devices are " 10626 "faulted in response to persistent errors.\n\tSufficient " 10627 "replicas exist for the pool to continue functioning " 10628 "in a\n\tdegraded state.\n")); 10629 snprintf(action, AC_SIZE, gettext("Replace the faulted device, " 10630 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 10631 break; 10632 10633 case ZPOOL_STATUS_FAULTED_DEV_NR: 10634 snprintf(status, ST_SIZE, gettext("One or more devices are " 10635 "faulted in response to persistent errors. There are " 10636 "insufficient replicas for the pool to\n\tcontinue " 10637 "functioning.\n")); 10638 snprintf(action, AC_SIZE, gettext("Destroy and re-create the " 10639 "pool from a backup source. Manually marking the device\n" 10640 "\trepaired using 'zpool clear' may allow some data " 10641 "to be recovered.\n")); 10642 break; 10643 10644 case ZPOOL_STATUS_IO_FAILURE_MMP: 10645 snprintf(status, ST_SIZE, gettext("The pool is suspended " 10646 "because multihost writes failed or were delayed;\n\t" 10647 "another system could import the pool undetected.\n")); 10648 snprintf(action, AC_SIZE, gettext("Make sure the pool's devices" 10649 " are connected, then reboot your system and\n\timport the " 10650 "pool or run 'zpool clear' to resume the pool.\n")); 10651 break; 10652 10653 case ZPOOL_STATUS_IO_FAILURE_WAIT: 10654 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 10655 snprintf(status, ST_SIZE, gettext("One or more devices are " 10656 "faulted in response to IO failures.\n")); 10657 snprintf(action, AC_SIZE, gettext("Make sure the affected " 10658 "devices are connected, then run 'zpool clear'.\n")); 10659 break; 10660 10661 case ZPOOL_STATUS_BAD_LOG: 10662 snprintf(status, ST_SIZE, gettext("An intent log record " 10663 "could not be read.\n" 10664 "\tWaiting for administrator intervention to fix the " 10665 "faulted pool.\n")); 10666 snprintf(action, AC_SIZE, gettext("Either restore the affected " 10667 "device(s) and run 'zpool online',\n" 10668 "\tor ignore the intent log records by running " 10669 "'zpool clear'.\n")); 10670 break; 10671 10672 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 10673 snprintf(status, ST_SIZE, gettext("One or more devices are " 10674 "configured to use a non-native block size.\n" 10675 "\tExpect reduced performance.\n")); 10676 snprintf(action, AC_SIZE, gettext("Replace affected devices " 10677 "with devices that support the\n\tconfigured block size, " 10678 "or migrate data to a properly configured\n\tpool.\n")); 10679 break; 10680 10681 case ZPOOL_STATUS_HOSTID_MISMATCH: 10682 snprintf(status, ST_SIZE, gettext("Mismatch between pool hostid" 10683 " and system hostid on imported pool.\n\tThis pool was " 10684 "previously imported into a system with a different " 10685 "hostid,\n\tand then was verbatim imported into this " 10686 "system.\n")); 10687 snprintf(action, AC_SIZE, gettext("Export this pool on all " 10688 "systems on which it is imported.\n" 10689 "\tThen import it to correct the mismatch.\n")); 10690 break; 10691 10692 case ZPOOL_STATUS_ERRATA: 10693 snprintf(status, ST_SIZE, gettext("Errata #%d detected.\n"), 10694 errata); 10695 switch (errata) { 10696 case ZPOOL_ERRATA_NONE: 10697 break; 10698 10699 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 10700 snprintf(action, AC_SIZE, gettext("To correct the issue" 10701 " run 'zpool scrub'.\n")); 10702 break; 10703 10704 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 10705 (void) strlcat(status, gettext("\tExisting encrypted " 10706 "datasets contain an on-disk incompatibility\n\t " 10707 "which needs to be corrected.\n"), ST_SIZE); 10708 snprintf(action, AC_SIZE, gettext("To correct the issue" 10709 " backup existing encrypted datasets to new\n\t" 10710 "encrypted datasets and destroy the old ones. " 10711 "'zfs mount -o ro' can\n\tbe used to temporarily " 10712 "mount existing encrypted datasets readonly.\n")); 10713 break; 10714 10715 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 10716 (void) strlcat(status, gettext("\tExisting encrypted " 10717 "snapshots and bookmarks contain an on-disk\n\t" 10718 "incompatibility. This may cause on-disk " 10719 "corruption if they are used\n\twith " 10720 "'zfs recv'.\n"), ST_SIZE); 10721 snprintf(action, AC_SIZE, gettext("To correct the" 10722 "issue, enable the bookmark_v2 feature. No " 10723 "additional\n\taction is needed if there are no " 10724 "encrypted snapshots or bookmarks.\n\tIf preserving" 10725 "the encrypted snapshots and bookmarks is required," 10726 " use\n\ta non-raw send to backup and restore them." 10727 " Alternately, they may be\n\tremoved to resolve " 10728 "the incompatibility.\n")); 10729 break; 10730 10731 default: 10732 /* 10733 * All errata which allow the pool to be imported 10734 * must contain an action message. 10735 */ 10736 assert(0); 10737 } 10738 break; 10739 10740 default: 10741 /* 10742 * The remaining errors can't actually be generated, yet. 10743 */ 10744 assert(reason == ZPOOL_STATUS_OK); 10745 } 10746 10747 if (status[0] != 0) { 10748 if (cbp->cb_json) 10749 fnvlist_add_string(item, "status", status); 10750 else { 10751 printf_color(ANSI_BOLD, gettext("status: ")); 10752 printf_color(ANSI_YELLOW, status); 10753 } 10754 } 10755 10756 if (action[0] != 0) { 10757 if (cbp->cb_json) 10758 fnvlist_add_string(item, "action", action); 10759 else { 10760 printf_color(ANSI_BOLD, gettext("action: ")); 10761 printf_color(ANSI_YELLOW, action); 10762 } 10763 } 10764 } 10765 10766 static int 10767 status_callback_json(zpool_handle_t *zhp, void *data) 10768 { 10769 status_cbdata_t *cbp = data; 10770 nvlist_t *config, *nvroot; 10771 const char *msgid; 10772 char pool_guid[256]; 10773 char msgbuf[256]; 10774 uint64_t guid; 10775 zpool_status_t reason; 10776 zpool_errata_t errata; 10777 uint_t c; 10778 vdev_stat_t *vs; 10779 nvlist_t *item, *d, *load_info, *vds; 10780 10781 /* If dedup stats were requested, also fetch dedupcached. */ 10782 if (cbp->cb_dedup_stats > 1) 10783 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME); 10784 reason = zpool_get_status(zhp, &msgid, &errata); 10785 /* 10786 * If we were given 'zpool status -x', only report those pools with 10787 * problems. 10788 */ 10789 if (cbp->cb_explain && 10790 (reason == ZPOOL_STATUS_OK || 10791 reason == ZPOOL_STATUS_VERSION_OLDER || 10792 reason == ZPOOL_STATUS_FEAT_DISABLED || 10793 reason == ZPOOL_STATUS_COMPATIBILITY_ERR || 10794 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { 10795 return (0); 10796 } 10797 10798 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools"); 10799 item = fnvlist_alloc(); 10800 vds = fnvlist_alloc(); 10801 fill_pool_info(item, zhp, B_FALSE, cbp->cb_json_as_int); 10802 config = zpool_get_config(zhp, NULL); 10803 10804 if (config != NULL) { 10805 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10806 verify(nvlist_lookup_uint64_array(nvroot, 10807 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &c) == 0); 10808 if (cbp->cb_json_pool_key_guid) { 10809 guid = fnvlist_lookup_uint64(config, 10810 ZPOOL_CONFIG_POOL_GUID); 10811 snprintf(pool_guid, 256, "%llu", (u_longlong_t)guid); 10812 } 10813 cbp->cb_count++; 10814 10815 print_status_reason(zhp, cbp, reason, errata, item); 10816 if (msgid != NULL) { 10817 snprintf(msgbuf, 256, 10818 "https://openzfs.github.io/openzfs-docs/msg/%s", 10819 msgid); 10820 fnvlist_add_string(item, "msgid", msgid); 10821 fnvlist_add_string(item, "moreinfo", msgbuf); 10822 } 10823 10824 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 10825 &load_info) == 0) { 10826 fnvlist_add_nvlist(item, ZPOOL_CONFIG_LOAD_INFO, 10827 load_info); 10828 } 10829 10830 scan_status_nvlist(zhp, cbp, nvroot, item); 10831 removal_status_nvlist(zhp, cbp, nvroot, item); 10832 checkpoint_status_nvlist(nvroot, cbp, item); 10833 raidz_expand_status_nvlist(zhp, cbp, nvroot, item); 10834 vdev_stats_nvlist(zhp, cbp, nvroot, 0, B_FALSE, NULL, vds); 10835 if (cbp->cb_flat_vdevs) { 10836 class_vdevs_nvlist(zhp, cbp, nvroot, 10837 VDEV_ALLOC_BIAS_DEDUP, vds); 10838 class_vdevs_nvlist(zhp, cbp, nvroot, 10839 VDEV_ALLOC_BIAS_SPECIAL, vds); 10840 class_vdevs_nvlist(zhp, cbp, nvroot, 10841 VDEV_ALLOC_CLASS_LOGS, vds); 10842 l2cache_nvlist(zhp, cbp, nvroot, vds); 10843 spares_nvlist(zhp, cbp, nvroot, vds); 10844 10845 fnvlist_add_nvlist(item, "vdevs", vds); 10846 fnvlist_free(vds); 10847 } else { 10848 fnvlist_add_nvlist(item, "vdevs", vds); 10849 fnvlist_free(vds); 10850 10851 class_vdevs_nvlist(zhp, cbp, nvroot, 10852 VDEV_ALLOC_BIAS_DEDUP, item); 10853 class_vdevs_nvlist(zhp, cbp, nvroot, 10854 VDEV_ALLOC_BIAS_SPECIAL, item); 10855 class_vdevs_nvlist(zhp, cbp, nvroot, 10856 VDEV_ALLOC_CLASS_LOGS, item); 10857 l2cache_nvlist(zhp, cbp, nvroot, item); 10858 spares_nvlist(zhp, cbp, nvroot, item); 10859 } 10860 dedup_stats_nvlist(zhp, cbp, item); 10861 errors_nvlist(zhp, cbp, item); 10862 } 10863 if (cbp->cb_json_pool_key_guid) { 10864 fnvlist_add_nvlist(d, pool_guid, item); 10865 } else { 10866 fnvlist_add_nvlist(d, zpool_get_name(zhp), 10867 item); 10868 } 10869 fnvlist_free(item); 10870 return (0); 10871 } 10872 10873 /* 10874 * Display a summary of pool status. Displays a summary such as: 10875 * 10876 * pool: tank 10877 * status: DEGRADED 10878 * reason: One or more devices ... 10879 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 10880 * config: 10881 * mirror DEGRADED 10882 * c1t0d0 OK 10883 * c2t0d0 UNAVAIL 10884 * 10885 * When given the '-v' option, we print out the complete config. If the '-e' 10886 * option is specified, then we print out error rate information as well. 10887 */ 10888 static int 10889 status_callback(zpool_handle_t *zhp, void *data) 10890 { 10891 status_cbdata_t *cbp = data; 10892 nvlist_t *config, *nvroot; 10893 const char *msgid; 10894 zpool_status_t reason; 10895 zpool_errata_t errata; 10896 const char *health; 10897 uint_t c; 10898 vdev_stat_t *vs; 10899 10900 /* If dedup stats were requested, also fetch dedupcached. */ 10901 if (cbp->cb_dedup_stats > 1) 10902 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME); 10903 10904 config = zpool_get_config(zhp, NULL); 10905 reason = zpool_get_status(zhp, &msgid, &errata); 10906 10907 cbp->cb_count++; 10908 10909 /* 10910 * If we were given 'zpool status -x', only report those pools with 10911 * problems. 10912 */ 10913 if (cbp->cb_explain && 10914 (reason == ZPOOL_STATUS_OK || 10915 reason == ZPOOL_STATUS_VERSION_OLDER || 10916 reason == ZPOOL_STATUS_FEAT_DISABLED || 10917 reason == ZPOOL_STATUS_COMPATIBILITY_ERR || 10918 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { 10919 if (!cbp->cb_allpools) { 10920 (void) printf(gettext("pool '%s' is healthy\n"), 10921 zpool_get_name(zhp)); 10922 if (cbp->cb_first) 10923 cbp->cb_first = B_FALSE; 10924 } 10925 return (0); 10926 } 10927 10928 if (cbp->cb_first) 10929 cbp->cb_first = B_FALSE; 10930 else 10931 (void) printf("\n"); 10932 10933 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10934 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 10935 (uint64_t **)&vs, &c) == 0); 10936 10937 health = zpool_get_state_str(zhp); 10938 10939 printf(" "); 10940 printf_color(ANSI_BOLD, gettext("pool:")); 10941 printf(" %s\n", zpool_get_name(zhp)); 10942 fputc(' ', stdout); 10943 printf_color(ANSI_BOLD, gettext("state: ")); 10944 10945 printf_color(health_str_to_color(health), "%s", health); 10946 10947 fputc('\n', stdout); 10948 print_status_reason(zhp, cbp, reason, errata, NULL); 10949 10950 if (msgid != NULL) { 10951 printf(" "); 10952 printf_color(ANSI_BOLD, gettext("see:")); 10953 printf(gettext( 10954 " https://openzfs.github.io/openzfs-docs/msg/%s\n"), 10955 msgid); 10956 } 10957 10958 if (config != NULL) { 10959 uint64_t nerr; 10960 nvlist_t **spares, **l2cache; 10961 uint_t nspares, nl2cache; 10962 10963 print_scan_status(zhp, nvroot); 10964 10965 pool_removal_stat_t *prs = NULL; 10966 (void) nvlist_lookup_uint64_array(nvroot, 10967 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 10968 print_removal_status(zhp, prs); 10969 10970 pool_checkpoint_stat_t *pcs = NULL; 10971 (void) nvlist_lookup_uint64_array(nvroot, 10972 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10973 print_checkpoint_status(pcs); 10974 10975 pool_raidz_expand_stat_t *pres = NULL; 10976 (void) nvlist_lookup_uint64_array(nvroot, 10977 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); 10978 print_raidz_expand_status(zhp, pres); 10979 10980 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, 10981 cbp->cb_name_flags | VDEV_NAME_TYPE_ID); 10982 if (cbp->cb_namewidth < 10) 10983 cbp->cb_namewidth = 10; 10984 10985 color_start(ANSI_BOLD); 10986 (void) printf(gettext("config:\n\n")); 10987 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), 10988 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", 10989 "CKSUM"); 10990 color_end(); 10991 10992 if (cbp->cb_print_slow_ios) { 10993 printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); 10994 } 10995 10996 if (cbp->cb_print_power) { 10997 printf_color(ANSI_BOLD, " %5s", gettext("POWER")); 10998 } 10999 11000 if (cbp->cb_print_dio_verify) { 11001 printf_color(ANSI_BOLD, " %5s", gettext("DIO")); 11002 } 11003 11004 if (cbp->vcdl != NULL) 11005 print_cmd_columns(cbp->vcdl, 0); 11006 11007 printf("\n"); 11008 11009 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, 11010 B_FALSE, NULL); 11011 11012 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); 11013 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 11014 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); 11015 11016 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 11017 &l2cache, &nl2cache) == 0) 11018 print_l2cache(zhp, cbp, l2cache, nl2cache); 11019 11020 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 11021 &spares, &nspares) == 0) 11022 print_spares(zhp, cbp, spares, nspares); 11023 11024 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 11025 &nerr) == 0) { 11026 (void) printf("\n"); 11027 if (nerr == 0) { 11028 (void) printf(gettext( 11029 "errors: No known data errors\n")); 11030 } else if (!cbp->cb_verbose) { 11031 color_start(ANSI_RED); 11032 (void) printf(gettext("errors: %llu data " 11033 "errors, use '-v' for a list\n"), 11034 (u_longlong_t)nerr); 11035 color_end(); 11036 } else { 11037 print_error_log(zhp); 11038 } 11039 } 11040 11041 if (cbp->cb_dedup_stats) 11042 print_dedup_stats(zhp, config, cbp->cb_literal); 11043 } else { 11044 (void) printf(gettext("config: The configuration cannot be " 11045 "determined.\n")); 11046 } 11047 11048 return (0); 11049 } 11050 11051 /* 11052 * zpool status [-dDegiLpPstvx] [-c [script1,script2,...]] ... 11053 * [-j|--json [--json-flat-vdevs] [--json-int] ... 11054 * [--json-pool-key-guid]] [--power] [-T d|u] ... 11055 * [pool] [interval [count]] 11056 * 11057 * -c CMD For each vdev, run command CMD 11058 * -D Display dedup status (undocumented) 11059 * -d Display Direct I/O write verify errors 11060 * -e Display only unhealthy vdevs 11061 * -g Display guid for individual vdev name. 11062 * -i Display vdev initialization status. 11063 * -j [...] Display output in JSON format 11064 * --json-flat-vdevs Display vdevs in flat hierarchy 11065 * --json-int Display numbers in integer format instead of string 11066 * --json-pool-key-guid Use pool GUID as key for pool objects 11067 * -L Follow links when resolving vdev path name. 11068 * -P Display full path for vdev name. 11069 * -p Display values in parsable (exact) format. 11070 * --power Display vdev enclosure slot power status 11071 * -s Display slow IOs column. 11072 * -T Display a timestamp in date(1) or Unix format 11073 * -t Display vdev TRIM status. 11074 * -v Display complete error logs 11075 * -x Display only pools with potential problems 11076 * 11077 * Describes the health status of all pools or some subset. 11078 */ 11079 int 11080 zpool_do_status(int argc, char **argv) 11081 { 11082 int c; 11083 int ret; 11084 float interval = 0; 11085 unsigned long count = 0; 11086 status_cbdata_t cb = { 0 }; 11087 nvlist_t *data; 11088 char *cmd = NULL; 11089 11090 struct option long_options[] = { 11091 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 11092 {"json", no_argument, NULL, 'j'}, 11093 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 11094 {"json-flat-vdevs", no_argument, NULL, 11095 ZPOOL_OPTION_JSON_FLAT_VDEVS}, 11096 {"json-pool-key-guid", no_argument, NULL, 11097 ZPOOL_OPTION_POOL_KEY_GUID}, 11098 {0, 0, 0, 0} 11099 }; 11100 11101 /* check options */ 11102 while ((c = getopt_long(argc, argv, "c:jdDegiLpPstT:vx", long_options, 11103 NULL)) != -1) { 11104 switch (c) { 11105 case 'c': 11106 if (cmd != NULL) { 11107 fprintf(stderr, 11108 gettext("Can't set -c flag twice\n")); 11109 exit(1); 11110 } 11111 11112 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 11113 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 11114 fprintf(stderr, gettext( 11115 "Can't run -c, disabled by " 11116 "ZPOOL_SCRIPTS_ENABLED.\n")); 11117 exit(1); 11118 } 11119 11120 if ((getuid() <= 0 || geteuid() <= 0) && 11121 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 11122 fprintf(stderr, gettext( 11123 "Can't run -c with root privileges " 11124 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 11125 exit(1); 11126 } 11127 cmd = optarg; 11128 break; 11129 case 'd': 11130 cb.cb_print_dio_verify = B_TRUE; 11131 break; 11132 case 'D': 11133 if (++cb.cb_dedup_stats > 2) 11134 cb.cb_dedup_stats = 2; 11135 break; 11136 case 'e': 11137 cb.cb_print_unhealthy = B_TRUE; 11138 break; 11139 case 'g': 11140 cb.cb_name_flags |= VDEV_NAME_GUID; 11141 break; 11142 case 'i': 11143 cb.cb_print_vdev_init = B_TRUE; 11144 break; 11145 case 'L': 11146 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 11147 break; 11148 case 'p': 11149 cb.cb_literal = B_TRUE; 11150 break; 11151 case 'P': 11152 cb.cb_name_flags |= VDEV_NAME_PATH; 11153 break; 11154 case 's': 11155 cb.cb_print_slow_ios = B_TRUE; 11156 break; 11157 case 't': 11158 cb.cb_print_vdev_trim = B_TRUE; 11159 break; 11160 case 'T': 11161 get_timestamp_arg(*optarg); 11162 break; 11163 case 'v': 11164 cb.cb_verbose = B_TRUE; 11165 break; 11166 case 'j': 11167 cb.cb_json = B_TRUE; 11168 break; 11169 case 'x': 11170 cb.cb_explain = B_TRUE; 11171 break; 11172 case ZPOOL_OPTION_POWER: 11173 cb.cb_print_power = B_TRUE; 11174 break; 11175 case ZPOOL_OPTION_JSON_FLAT_VDEVS: 11176 cb.cb_flat_vdevs = B_TRUE; 11177 break; 11178 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 11179 cb.cb_json_as_int = B_TRUE; 11180 cb.cb_literal = B_TRUE; 11181 break; 11182 case ZPOOL_OPTION_POOL_KEY_GUID: 11183 cb.cb_json_pool_key_guid = B_TRUE; 11184 break; 11185 case '?': 11186 if (optopt == 'c') { 11187 print_zpool_script_list("status"); 11188 exit(0); 11189 } else { 11190 fprintf(stderr, 11191 gettext("invalid option '%c'\n"), optopt); 11192 } 11193 usage(B_FALSE); 11194 } 11195 } 11196 11197 argc -= optind; 11198 argv += optind; 11199 11200 get_interval_count(&argc, argv, &interval, &count); 11201 11202 if (argc == 0) 11203 cb.cb_allpools = B_TRUE; 11204 11205 cb.cb_first = B_TRUE; 11206 cb.cb_print_status = B_TRUE; 11207 11208 if (cb.cb_flat_vdevs && !cb.cb_json) { 11209 fprintf(stderr, gettext("'--json-flat-vdevs' only works with" 11210 " '-j' option\n")); 11211 usage(B_FALSE); 11212 } 11213 11214 if (cb.cb_json_as_int && !cb.cb_json) { 11215 (void) fprintf(stderr, gettext("'--json-int' only works with" 11216 " '-j' option\n")); 11217 usage(B_FALSE); 11218 } 11219 11220 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 11221 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 11222 " works with '-j' option\n")); 11223 usage(B_FALSE); 11224 } 11225 11226 for (;;) { 11227 if (cb.cb_json) { 11228 cb.cb_jsobj = zpool_json_schema(0, 1); 11229 data = fnvlist_alloc(); 11230 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 11231 fnvlist_free(data); 11232 } 11233 11234 if (timestamp_fmt != NODATE) { 11235 if (cb.cb_json) { 11236 if (cb.cb_json_as_int) { 11237 fnvlist_add_uint64(cb.cb_jsobj, "time", 11238 time(NULL)); 11239 } else { 11240 char ts[128]; 11241 get_timestamp(timestamp_fmt, ts, 128); 11242 fnvlist_add_string(cb.cb_jsobj, "time", 11243 ts); 11244 } 11245 } else 11246 print_timestamp(timestamp_fmt); 11247 } 11248 11249 if (cmd != NULL) 11250 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, 11251 NULL, NULL, 0, 0); 11252 11253 if (cb.cb_json) { 11254 ret = for_each_pool(argc, argv, B_TRUE, NULL, 11255 ZFS_TYPE_POOL, cb.cb_literal, 11256 status_callback_json, &cb); 11257 } else { 11258 ret = for_each_pool(argc, argv, B_TRUE, NULL, 11259 ZFS_TYPE_POOL, cb.cb_literal, 11260 status_callback, &cb); 11261 } 11262 11263 if (cb.vcdl != NULL) 11264 free_vdev_cmd_data_list(cb.vcdl); 11265 11266 if (cb.cb_json) { 11267 if (ret == 0) 11268 zcmd_print_json(cb.cb_jsobj); 11269 else 11270 nvlist_free(cb.cb_jsobj); 11271 } else { 11272 if (argc == 0 && cb.cb_count == 0) { 11273 (void) fprintf(stderr, "%s", 11274 gettext("no pools available\n")); 11275 } else if (cb.cb_explain && cb.cb_first && 11276 cb.cb_allpools) { 11277 (void) printf("%s", 11278 gettext("all pools are healthy\n")); 11279 } 11280 } 11281 11282 if (ret != 0) 11283 return (ret); 11284 11285 if (interval == 0) 11286 break; 11287 11288 if (count != 0 && --count == 0) 11289 break; 11290 11291 (void) fflush(stdout); 11292 (void) fsleep(interval); 11293 } 11294 11295 return (0); 11296 } 11297 11298 typedef struct upgrade_cbdata { 11299 int cb_first; 11300 int cb_argc; 11301 uint64_t cb_version; 11302 char **cb_argv; 11303 } upgrade_cbdata_t; 11304 11305 static int 11306 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) 11307 { 11308 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 11309 int *count = (int *)unsupp_fs; 11310 11311 if (zfs_version > ZPL_VERSION) { 11312 (void) printf(gettext("%s (v%d) is not supported by this " 11313 "implementation of ZFS.\n"), 11314 zfs_get_name(zhp), zfs_version); 11315 (*count)++; 11316 } 11317 11318 zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs); 11319 11320 zfs_close(zhp); 11321 11322 return (0); 11323 } 11324 11325 static int 11326 upgrade_version(zpool_handle_t *zhp, uint64_t version) 11327 { 11328 int ret; 11329 nvlist_t *config; 11330 uint64_t oldversion; 11331 int unsupp_fs = 0; 11332 11333 config = zpool_get_config(zhp, NULL); 11334 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11335 &oldversion) == 0); 11336 11337 char compat[ZFS_MAXPROPLEN]; 11338 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 11339 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 11340 compat[0] = '\0'; 11341 11342 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 11343 assert(oldversion < version); 11344 11345 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); 11346 if (ret != 0) 11347 return (ret); 11348 11349 if (unsupp_fs) { 11350 (void) fprintf(stderr, gettext("Upgrade not performed due " 11351 "to %d unsupported filesystems (max v%d).\n"), 11352 unsupp_fs, (int)ZPL_VERSION); 11353 return (1); 11354 } 11355 11356 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) { 11357 (void) fprintf(stderr, gettext("Upgrade not performed because " 11358 "'compatibility' property set to '" 11359 ZPOOL_COMPAT_LEGACY "'.\n")); 11360 return (1); 11361 } 11362 11363 ret = zpool_upgrade(zhp, version); 11364 if (ret != 0) 11365 return (ret); 11366 11367 if (version >= SPA_VERSION_FEATURES) { 11368 (void) printf(gettext("Successfully upgraded " 11369 "'%s' from version %llu to feature flags.\n"), 11370 zpool_get_name(zhp), (u_longlong_t)oldversion); 11371 } else { 11372 (void) printf(gettext("Successfully upgraded " 11373 "'%s' from version %llu to version %llu.\n"), 11374 zpool_get_name(zhp), (u_longlong_t)oldversion, 11375 (u_longlong_t)version); 11376 } 11377 11378 return (0); 11379 } 11380 11381 static int 11382 upgrade_enable_all(zpool_handle_t *zhp, int *countp) 11383 { 11384 int i, ret, count; 11385 boolean_t firstff = B_TRUE; 11386 nvlist_t *enabled = zpool_get_features(zhp); 11387 11388 char compat[ZFS_MAXPROPLEN]; 11389 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 11390 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 11391 compat[0] = '\0'; 11392 11393 boolean_t requested_features[SPA_FEATURES]; 11394 if (zpool_do_load_compat(compat, requested_features) != 11395 ZPOOL_COMPATIBILITY_OK) 11396 return (-1); 11397 11398 count = 0; 11399 for (i = 0; i < SPA_FEATURES; i++) { 11400 const char *fname = spa_feature_table[i].fi_uname; 11401 const char *fguid = spa_feature_table[i].fi_guid; 11402 11403 if (!spa_feature_table[i].fi_zfs_mod_supported || 11404 (spa_feature_table[i].fi_flags & ZFEATURE_FLAG_NO_UPGRADE)) 11405 continue; 11406 11407 if (!nvlist_exists(enabled, fguid) && requested_features[i]) { 11408 char *propname; 11409 verify(-1 != asprintf(&propname, "feature@%s", fname)); 11410 ret = zpool_set_prop(zhp, propname, 11411 ZFS_FEATURE_ENABLED); 11412 if (ret != 0) { 11413 free(propname); 11414 return (ret); 11415 } 11416 count++; 11417 11418 if (firstff) { 11419 (void) printf(gettext("Enabled the " 11420 "following features on '%s':\n"), 11421 zpool_get_name(zhp)); 11422 firstff = B_FALSE; 11423 } 11424 (void) printf(gettext(" %s\n"), fname); 11425 free(propname); 11426 } 11427 } 11428 11429 if (countp != NULL) 11430 *countp = count; 11431 return (0); 11432 } 11433 11434 static int 11435 upgrade_cb(zpool_handle_t *zhp, void *arg) 11436 { 11437 upgrade_cbdata_t *cbp = arg; 11438 nvlist_t *config; 11439 uint64_t version; 11440 boolean_t modified_pool = B_FALSE; 11441 int ret; 11442 11443 config = zpool_get_config(zhp, NULL); 11444 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11445 &version) == 0); 11446 11447 assert(SPA_VERSION_IS_SUPPORTED(version)); 11448 11449 if (version < cbp->cb_version) { 11450 cbp->cb_first = B_FALSE; 11451 ret = upgrade_version(zhp, cbp->cb_version); 11452 if (ret != 0) 11453 return (ret); 11454 modified_pool = B_TRUE; 11455 11456 /* 11457 * If they did "zpool upgrade -a", then we could 11458 * be doing ioctls to different pools. We need 11459 * to log this history once to each pool, and bypass 11460 * the normal history logging that happens in main(). 11461 */ 11462 (void) zpool_log_history(g_zfs, history_str); 11463 log_history = B_FALSE; 11464 } 11465 11466 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 11467 int count; 11468 ret = upgrade_enable_all(zhp, &count); 11469 if (ret != 0) 11470 return (ret); 11471 11472 if (count > 0) { 11473 cbp->cb_first = B_FALSE; 11474 modified_pool = B_TRUE; 11475 } 11476 } 11477 11478 if (modified_pool) { 11479 (void) printf("\n"); 11480 (void) after_zpool_upgrade(zhp); 11481 } 11482 11483 return (0); 11484 } 11485 11486 static int 11487 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 11488 { 11489 upgrade_cbdata_t *cbp = arg; 11490 nvlist_t *config; 11491 uint64_t version; 11492 11493 config = zpool_get_config(zhp, NULL); 11494 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11495 &version) == 0); 11496 11497 assert(SPA_VERSION_IS_SUPPORTED(version)); 11498 11499 if (version < SPA_VERSION_FEATURES) { 11500 if (cbp->cb_first) { 11501 (void) printf(gettext("The following pools are " 11502 "formatted with legacy version numbers and can\n" 11503 "be upgraded to use feature flags. After " 11504 "being upgraded, these pools\nwill no " 11505 "longer be accessible by software that does not " 11506 "support feature\nflags.\n\n" 11507 "Note that setting a pool's 'compatibility' " 11508 "feature to '" ZPOOL_COMPAT_LEGACY "' will\n" 11509 "inhibit upgrades.\n\n")); 11510 (void) printf(gettext("VER POOL\n")); 11511 (void) printf(gettext("--- ------------\n")); 11512 cbp->cb_first = B_FALSE; 11513 } 11514 11515 (void) printf("%2llu %s\n", (u_longlong_t)version, 11516 zpool_get_name(zhp)); 11517 } 11518 11519 return (0); 11520 } 11521 11522 static int 11523 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 11524 { 11525 upgrade_cbdata_t *cbp = arg; 11526 nvlist_t *config; 11527 uint64_t version; 11528 11529 config = zpool_get_config(zhp, NULL); 11530 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11531 &version) == 0); 11532 11533 if (version >= SPA_VERSION_FEATURES) { 11534 int i; 11535 boolean_t poolfirst = B_TRUE; 11536 nvlist_t *enabled = zpool_get_features(zhp); 11537 11538 for (i = 0; i < SPA_FEATURES; i++) { 11539 const char *fguid = spa_feature_table[i].fi_guid; 11540 const char *fname = spa_feature_table[i].fi_uname; 11541 11542 if (!spa_feature_table[i].fi_zfs_mod_supported) 11543 continue; 11544 11545 if (!nvlist_exists(enabled, fguid)) { 11546 if (cbp->cb_first) { 11547 (void) printf(gettext("\nSome " 11548 "supported features are not " 11549 "enabled on the following pools. " 11550 "Once a\nfeature is enabled the " 11551 "pool may become incompatible with " 11552 "software\nthat does not support " 11553 "the feature. See " 11554 "zpool-features(7) for " 11555 "details.\n\n" 11556 "Note that the pool " 11557 "'compatibility' feature can be " 11558 "used to inhibit\nfeature " 11559 "upgrades.\n\n" 11560 "Features marked with (*) are not " 11561 "applied automatically on upgrade, " 11562 "and\nmust be applied explicitly " 11563 "with zpool-set(7).\n\n")); 11564 (void) printf(gettext("POOL " 11565 "FEATURE\n")); 11566 (void) printf(gettext("------" 11567 "---------\n")); 11568 cbp->cb_first = B_FALSE; 11569 } 11570 11571 if (poolfirst) { 11572 (void) printf(gettext("%s\n"), 11573 zpool_get_name(zhp)); 11574 poolfirst = B_FALSE; 11575 } 11576 11577 (void) printf(gettext(" %s%s\n"), fname, 11578 spa_feature_table[i].fi_flags & 11579 ZFEATURE_FLAG_NO_UPGRADE ? "(*)" : ""); 11580 } 11581 /* 11582 * If they did "zpool upgrade -a", then we could 11583 * be doing ioctls to different pools. We need 11584 * to log this history once to each pool, and bypass 11585 * the normal history logging that happens in main(). 11586 */ 11587 (void) zpool_log_history(g_zfs, history_str); 11588 log_history = B_FALSE; 11589 } 11590 } 11591 11592 return (0); 11593 } 11594 11595 static int 11596 upgrade_one(zpool_handle_t *zhp, void *data) 11597 { 11598 boolean_t modified_pool = B_FALSE; 11599 upgrade_cbdata_t *cbp = data; 11600 uint64_t cur_version; 11601 int ret; 11602 11603 if (strcmp("log", zpool_get_name(zhp)) == 0) { 11604 (void) fprintf(stderr, gettext("'log' is now a reserved word\n" 11605 "Pool 'log' must be renamed using export and import" 11606 " to upgrade.\n")); 11607 return (1); 11608 } 11609 11610 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 11611 if (cur_version > cbp->cb_version) { 11612 (void) printf(gettext("Pool '%s' is already formatted " 11613 "using more current version '%llu'.\n\n"), 11614 zpool_get_name(zhp), (u_longlong_t)cur_version); 11615 return (0); 11616 } 11617 11618 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 11619 (void) printf(gettext("Pool '%s' is already formatted " 11620 "using version %llu.\n\n"), zpool_get_name(zhp), 11621 (u_longlong_t)cbp->cb_version); 11622 return (0); 11623 } 11624 11625 if (cur_version != cbp->cb_version) { 11626 modified_pool = B_TRUE; 11627 ret = upgrade_version(zhp, cbp->cb_version); 11628 if (ret != 0) 11629 return (ret); 11630 } 11631 11632 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 11633 int count = 0; 11634 ret = upgrade_enable_all(zhp, &count); 11635 if (ret != 0) 11636 return (ret); 11637 11638 if (count != 0) { 11639 modified_pool = B_TRUE; 11640 } else if (cur_version == SPA_VERSION) { 11641 (void) printf(gettext("Pool '%s' already has all " 11642 "supported and requested features enabled.\n"), 11643 zpool_get_name(zhp)); 11644 } 11645 } 11646 11647 if (modified_pool) { 11648 (void) printf("\n"); 11649 (void) after_zpool_upgrade(zhp); 11650 } 11651 11652 return (0); 11653 } 11654 11655 /* 11656 * zpool upgrade 11657 * zpool upgrade -v 11658 * zpool upgrade [-V version] <-a | pool ...> 11659 * 11660 * With no arguments, display downrev'd ZFS pool available for upgrade. 11661 * Individual pools can be upgraded by specifying the pool, and '-a' will 11662 * upgrade all pools. 11663 */ 11664 int 11665 zpool_do_upgrade(int argc, char **argv) 11666 { 11667 int c; 11668 upgrade_cbdata_t cb = { 0 }; 11669 int ret = 0; 11670 boolean_t showversions = B_FALSE; 11671 boolean_t upgradeall = B_FALSE; 11672 char *end; 11673 11674 11675 /* check options */ 11676 while ((c = getopt(argc, argv, ":avV:")) != -1) { 11677 switch (c) { 11678 case 'a': 11679 upgradeall = B_TRUE; 11680 break; 11681 case 'v': 11682 showversions = B_TRUE; 11683 break; 11684 case 'V': 11685 cb.cb_version = strtoll(optarg, &end, 10); 11686 if (*end != '\0' || 11687 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 11688 (void) fprintf(stderr, 11689 gettext("invalid version '%s'\n"), optarg); 11690 usage(B_FALSE); 11691 } 11692 break; 11693 case ':': 11694 (void) fprintf(stderr, gettext("missing argument for " 11695 "'%c' option\n"), optopt); 11696 usage(B_FALSE); 11697 break; 11698 case '?': 11699 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 11700 optopt); 11701 usage(B_FALSE); 11702 } 11703 } 11704 11705 cb.cb_argc = argc; 11706 cb.cb_argv = argv; 11707 argc -= optind; 11708 argv += optind; 11709 11710 if (cb.cb_version == 0) { 11711 cb.cb_version = SPA_VERSION; 11712 } else if (!upgradeall && argc == 0) { 11713 (void) fprintf(stderr, gettext("-V option is " 11714 "incompatible with other arguments\n")); 11715 usage(B_FALSE); 11716 } 11717 11718 if (showversions) { 11719 if (upgradeall || argc != 0) { 11720 (void) fprintf(stderr, gettext("-v option is " 11721 "incompatible with other arguments\n")); 11722 usage(B_FALSE); 11723 } 11724 } else if (upgradeall) { 11725 if (argc != 0) { 11726 (void) fprintf(stderr, gettext("-a option should not " 11727 "be used along with a pool name\n")); 11728 usage(B_FALSE); 11729 } 11730 } 11731 11732 (void) printf("%s", gettext("This system supports ZFS pool feature " 11733 "flags.\n\n")); 11734 if (showversions) { 11735 int i; 11736 11737 (void) printf(gettext("The following features are " 11738 "supported:\n\n")); 11739 (void) printf(gettext("FEAT DESCRIPTION\n")); 11740 (void) printf("----------------------------------------------" 11741 "---------------\n"); 11742 for (i = 0; i < SPA_FEATURES; i++) { 11743 zfeature_info_t *fi = &spa_feature_table[i]; 11744 if (!fi->fi_zfs_mod_supported) 11745 continue; 11746 const char *ro = 11747 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 11748 " (read-only compatible)" : ""; 11749 11750 (void) printf("%-37s%s\n", fi->fi_uname, ro); 11751 (void) printf(" %s\n", fi->fi_desc); 11752 } 11753 (void) printf("\n"); 11754 11755 (void) printf(gettext("The following legacy versions are also " 11756 "supported:\n\n")); 11757 (void) printf(gettext("VER DESCRIPTION\n")); 11758 (void) printf("--- -----------------------------------------" 11759 "---------------\n"); 11760 (void) printf(gettext(" 1 Initial ZFS version\n")); 11761 (void) printf(gettext(" 2 Ditto blocks " 11762 "(replicated metadata)\n")); 11763 (void) printf(gettext(" 3 Hot spares and double parity " 11764 "RAID-Z\n")); 11765 (void) printf(gettext(" 4 zpool history\n")); 11766 (void) printf(gettext(" 5 Compression using the gzip " 11767 "algorithm\n")); 11768 (void) printf(gettext(" 6 bootfs pool property\n")); 11769 (void) printf(gettext(" 7 Separate intent log devices\n")); 11770 (void) printf(gettext(" 8 Delegated administration\n")); 11771 (void) printf(gettext(" 9 refquota and refreservation " 11772 "properties\n")); 11773 (void) printf(gettext(" 10 Cache devices\n")); 11774 (void) printf(gettext(" 11 Improved scrub performance\n")); 11775 (void) printf(gettext(" 12 Snapshot properties\n")); 11776 (void) printf(gettext(" 13 snapused property\n")); 11777 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 11778 (void) printf(gettext(" 15 user/group space accounting\n")); 11779 (void) printf(gettext(" 16 stmf property support\n")); 11780 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 11781 (void) printf(gettext(" 18 Snapshot user holds\n")); 11782 (void) printf(gettext(" 19 Log device removal\n")); 11783 (void) printf(gettext(" 20 Compression using zle " 11784 "(zero-length encoding)\n")); 11785 (void) printf(gettext(" 21 Deduplication\n")); 11786 (void) printf(gettext(" 22 Received properties\n")); 11787 (void) printf(gettext(" 23 Slim ZIL\n")); 11788 (void) printf(gettext(" 24 System attributes\n")); 11789 (void) printf(gettext(" 25 Improved scrub stats\n")); 11790 (void) printf(gettext(" 26 Improved snapshot deletion " 11791 "performance\n")); 11792 (void) printf(gettext(" 27 Improved snapshot creation " 11793 "performance\n")); 11794 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 11795 (void) printf(gettext("\nFor more information on a particular " 11796 "version, including supported releases,\n")); 11797 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 11798 } else if (argc == 0 && upgradeall) { 11799 cb.cb_first = B_TRUE; 11800 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 11801 if (ret == 0 && cb.cb_first) { 11802 if (cb.cb_version == SPA_VERSION) { 11803 (void) printf(gettext("All pools are already " 11804 "formatted using feature flags.\n\n")); 11805 (void) printf(gettext("Every feature flags " 11806 "pool already has all supported and " 11807 "requested features enabled.\n")); 11808 } else { 11809 (void) printf(gettext("All pools are already " 11810 "formatted with version %llu or higher.\n"), 11811 (u_longlong_t)cb.cb_version); 11812 } 11813 } 11814 } else if (argc == 0) { 11815 cb.cb_first = B_TRUE; 11816 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 11817 assert(ret == 0); 11818 11819 if (cb.cb_first) { 11820 (void) printf(gettext("All pools are formatted " 11821 "using feature flags.\n\n")); 11822 } else { 11823 (void) printf(gettext("\nUse 'zpool upgrade -v' " 11824 "for a list of available legacy versions.\n")); 11825 } 11826 11827 cb.cb_first = B_TRUE; 11828 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 11829 assert(ret == 0); 11830 11831 if (cb.cb_first) { 11832 (void) printf(gettext("Every feature flags pool has " 11833 "all supported and requested features enabled.\n")); 11834 } else { 11835 (void) printf(gettext("\n")); 11836 } 11837 } else { 11838 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, 11839 B_FALSE, upgrade_one, &cb); 11840 } 11841 11842 return (ret); 11843 } 11844 11845 typedef struct hist_cbdata { 11846 boolean_t first; 11847 boolean_t longfmt; 11848 boolean_t internal; 11849 } hist_cbdata_t; 11850 11851 static void 11852 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) 11853 { 11854 nvlist_t **records; 11855 uint_t numrecords; 11856 int i; 11857 11858 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 11859 &records, &numrecords) == 0); 11860 for (i = 0; i < numrecords; i++) { 11861 nvlist_t *rec = records[i]; 11862 char tbuf[64] = ""; 11863 11864 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 11865 time_t tsec; 11866 struct tm t; 11867 11868 tsec = fnvlist_lookup_uint64(records[i], 11869 ZPOOL_HIST_TIME); 11870 (void) localtime_r(&tsec, &t); 11871 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 11872 } 11873 11874 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { 11875 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], 11876 ZPOOL_HIST_ELAPSED_NS); 11877 (void) snprintf(tbuf + strlen(tbuf), 11878 sizeof (tbuf) - strlen(tbuf), 11879 " (%lldms)", (long long)elapsed_ns / 1000 / 1000); 11880 } 11881 11882 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 11883 (void) printf("%s %s", tbuf, 11884 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 11885 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 11886 int ievent = 11887 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 11888 if (!cb->internal) 11889 continue; 11890 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 11891 (void) printf("%s unrecognized record:\n", 11892 tbuf); 11893 dump_nvlist(rec, 4); 11894 continue; 11895 } 11896 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 11897 zfs_history_event_names[ievent], 11898 (longlong_t)fnvlist_lookup_uint64( 11899 rec, ZPOOL_HIST_TXG), 11900 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 11901 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 11902 if (!cb->internal) 11903 continue; 11904 (void) printf("%s [txg:%lld] %s", tbuf, 11905 (longlong_t)fnvlist_lookup_uint64( 11906 rec, ZPOOL_HIST_TXG), 11907 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 11908 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 11909 (void) printf(" %s (%llu)", 11910 fnvlist_lookup_string(rec, 11911 ZPOOL_HIST_DSNAME), 11912 (u_longlong_t)fnvlist_lookup_uint64(rec, 11913 ZPOOL_HIST_DSID)); 11914 } 11915 (void) printf(" %s", fnvlist_lookup_string(rec, 11916 ZPOOL_HIST_INT_STR)); 11917 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 11918 if (!cb->internal) 11919 continue; 11920 (void) printf("%s ioctl %s\n", tbuf, 11921 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 11922 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 11923 (void) printf(" input:\n"); 11924 dump_nvlist(fnvlist_lookup_nvlist(rec, 11925 ZPOOL_HIST_INPUT_NVL), 8); 11926 } 11927 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 11928 (void) printf(" output:\n"); 11929 dump_nvlist(fnvlist_lookup_nvlist(rec, 11930 ZPOOL_HIST_OUTPUT_NVL), 8); 11931 } 11932 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { 11933 (void) printf(" output nvlist omitted; " 11934 "original size: %lldKB\n", 11935 (longlong_t)fnvlist_lookup_int64(rec, 11936 ZPOOL_HIST_OUTPUT_SIZE) / 1024); 11937 } 11938 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { 11939 (void) printf(" errno: %lld\n", 11940 (longlong_t)fnvlist_lookup_int64(rec, 11941 ZPOOL_HIST_ERRNO)); 11942 } 11943 } else { 11944 if (!cb->internal) 11945 continue; 11946 (void) printf("%s unrecognized record:\n", tbuf); 11947 dump_nvlist(rec, 4); 11948 } 11949 11950 if (!cb->longfmt) { 11951 (void) printf("\n"); 11952 continue; 11953 } 11954 (void) printf(" ["); 11955 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 11956 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 11957 struct passwd *pwd = getpwuid(who); 11958 (void) printf("user %d ", (int)who); 11959 if (pwd != NULL) 11960 (void) printf("(%s) ", pwd->pw_name); 11961 } 11962 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 11963 (void) printf("on %s", 11964 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 11965 } 11966 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 11967 (void) printf(":%s", 11968 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 11969 } 11970 11971 (void) printf("]"); 11972 (void) printf("\n"); 11973 } 11974 } 11975 11976 /* 11977 * Print out the command history for a specific pool. 11978 */ 11979 static int 11980 get_history_one(zpool_handle_t *zhp, void *data) 11981 { 11982 nvlist_t *nvhis; 11983 int ret; 11984 hist_cbdata_t *cb = (hist_cbdata_t *)data; 11985 uint64_t off = 0; 11986 boolean_t eof = B_FALSE; 11987 11988 cb->first = B_FALSE; 11989 11990 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 11991 11992 while (!eof) { 11993 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) 11994 return (ret); 11995 11996 print_history_records(nvhis, cb); 11997 nvlist_free(nvhis); 11998 } 11999 (void) printf("\n"); 12000 12001 return (ret); 12002 } 12003 12004 /* 12005 * zpool history <pool> 12006 * 12007 * Displays the history of commands that modified pools. 12008 */ 12009 int 12010 zpool_do_history(int argc, char **argv) 12011 { 12012 hist_cbdata_t cbdata = { 0 }; 12013 int ret; 12014 int c; 12015 12016 cbdata.first = B_TRUE; 12017 /* check options */ 12018 while ((c = getopt(argc, argv, "li")) != -1) { 12019 switch (c) { 12020 case 'l': 12021 cbdata.longfmt = B_TRUE; 12022 break; 12023 case 'i': 12024 cbdata.internal = B_TRUE; 12025 break; 12026 case '?': 12027 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12028 optopt); 12029 usage(B_FALSE); 12030 } 12031 } 12032 argc -= optind; 12033 argv += optind; 12034 12035 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, 12036 B_FALSE, get_history_one, &cbdata); 12037 12038 if (argc == 0 && cbdata.first == B_TRUE) { 12039 (void) fprintf(stderr, gettext("no pools available\n")); 12040 return (0); 12041 } 12042 12043 return (ret); 12044 } 12045 12046 typedef struct ev_opts { 12047 int verbose; 12048 int scripted; 12049 int follow; 12050 int clear; 12051 char poolname[ZFS_MAX_DATASET_NAME_LEN]; 12052 } ev_opts_t; 12053 12054 static void 12055 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) 12056 { 12057 char ctime_str[26], str[32]; 12058 const char *ptr; 12059 int64_t *tv; 12060 uint_t n; 12061 12062 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); 12063 memset(str, ' ', 32); 12064 (void) ctime_r((const time_t *)&tv[0], ctime_str); 12065 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ 12066 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ 12067 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ 12068 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ 12069 if (opts->scripted) 12070 (void) printf(gettext("%s\t"), str); 12071 else 12072 (void) printf(gettext("%s "), str); 12073 12074 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); 12075 (void) printf(gettext("%s\n"), ptr); 12076 } 12077 12078 static void 12079 zpool_do_events_nvprint(nvlist_t *nvl, int depth) 12080 { 12081 nvpair_t *nvp; 12082 static char flagstr[256]; 12083 12084 for (nvp = nvlist_next_nvpair(nvl, NULL); 12085 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { 12086 12087 data_type_t type = nvpair_type(nvp); 12088 const char *name = nvpair_name(nvp); 12089 12090 boolean_t b; 12091 uint8_t i8; 12092 uint16_t i16; 12093 uint32_t i32; 12094 uint64_t i64; 12095 const char *str; 12096 nvlist_t *cnv; 12097 12098 printf(gettext("%*s%s = "), depth, "", name); 12099 12100 switch (type) { 12101 case DATA_TYPE_BOOLEAN: 12102 printf(gettext("%s"), "1"); 12103 break; 12104 12105 case DATA_TYPE_BOOLEAN_VALUE: 12106 (void) nvpair_value_boolean_value(nvp, &b); 12107 printf(gettext("%s"), b ? "1" : "0"); 12108 break; 12109 12110 case DATA_TYPE_BYTE: 12111 (void) nvpair_value_byte(nvp, &i8); 12112 printf(gettext("0x%x"), i8); 12113 break; 12114 12115 case DATA_TYPE_INT8: 12116 (void) nvpair_value_int8(nvp, (void *)&i8); 12117 printf(gettext("0x%x"), i8); 12118 break; 12119 12120 case DATA_TYPE_UINT8: 12121 (void) nvpair_value_uint8(nvp, &i8); 12122 printf(gettext("0x%x"), i8); 12123 break; 12124 12125 case DATA_TYPE_INT16: 12126 (void) nvpair_value_int16(nvp, (void *)&i16); 12127 printf(gettext("0x%x"), i16); 12128 break; 12129 12130 case DATA_TYPE_UINT16: 12131 (void) nvpair_value_uint16(nvp, &i16); 12132 printf(gettext("0x%x"), i16); 12133 break; 12134 12135 case DATA_TYPE_INT32: 12136 (void) nvpair_value_int32(nvp, (void *)&i32); 12137 printf(gettext("0x%x"), i32); 12138 break; 12139 12140 case DATA_TYPE_UINT32: 12141 (void) nvpair_value_uint32(nvp, &i32); 12142 if (strcmp(name, 12143 FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE) == 0 || 12144 strcmp(name, 12145 FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE) == 0) { 12146 zfs_valstr_zio_stage(i32, flagstr, 12147 sizeof (flagstr)); 12148 printf(gettext("0x%x [%s]"), i32, flagstr); 12149 } else if (strcmp(name, 12150 FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE) == 0) { 12151 zfs_valstr_zio_type(i32, flagstr, 12152 sizeof (flagstr)); 12153 printf(gettext("0x%x [%s]"), i32, flagstr); 12154 } else if (strcmp(name, 12155 FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY) == 0) { 12156 zfs_valstr_zio_priority(i32, flagstr, 12157 sizeof (flagstr)); 12158 printf(gettext("0x%x [%s]"), i32, flagstr); 12159 } else { 12160 printf(gettext("0x%x"), i32); 12161 } 12162 break; 12163 12164 case DATA_TYPE_INT64: 12165 (void) nvpair_value_int64(nvp, (void *)&i64); 12166 printf(gettext("0x%llx"), (u_longlong_t)i64); 12167 break; 12168 12169 case DATA_TYPE_UINT64: 12170 (void) nvpair_value_uint64(nvp, &i64); 12171 /* 12172 * translate vdev state values to readable 12173 * strings to aide zpool events consumers 12174 */ 12175 if (strcmp(name, 12176 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || 12177 strcmp(name, 12178 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { 12179 printf(gettext("\"%s\" (0x%llx)"), 12180 zpool_state_to_name(i64, VDEV_AUX_NONE), 12181 (u_longlong_t)i64); 12182 } else if (strcmp(name, 12183 FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS) == 0) { 12184 zfs_valstr_zio_flag(i64, flagstr, 12185 sizeof (flagstr)); 12186 printf(gettext("0x%llx [%s]"), 12187 (u_longlong_t)i64, flagstr); 12188 } else { 12189 printf(gettext("0x%llx"), (u_longlong_t)i64); 12190 } 12191 break; 12192 12193 case DATA_TYPE_HRTIME: 12194 (void) nvpair_value_hrtime(nvp, (void *)&i64); 12195 printf(gettext("0x%llx"), (u_longlong_t)i64); 12196 break; 12197 12198 case DATA_TYPE_STRING: 12199 (void) nvpair_value_string(nvp, &str); 12200 printf(gettext("\"%s\""), str ? str : "<NULL>"); 12201 break; 12202 12203 case DATA_TYPE_NVLIST: 12204 printf(gettext("(embedded nvlist)\n")); 12205 (void) nvpair_value_nvlist(nvp, &cnv); 12206 zpool_do_events_nvprint(cnv, depth + 8); 12207 printf(gettext("%*s(end %s)"), depth, "", name); 12208 break; 12209 12210 case DATA_TYPE_NVLIST_ARRAY: { 12211 nvlist_t **val; 12212 uint_t i, nelem; 12213 12214 (void) nvpair_value_nvlist_array(nvp, &val, &nelem); 12215 printf(gettext("(%d embedded nvlists)\n"), nelem); 12216 for (i = 0; i < nelem; i++) { 12217 printf(gettext("%*s%s[%d] = %s\n"), 12218 depth, "", name, i, "(embedded nvlist)"); 12219 zpool_do_events_nvprint(val[i], depth + 8); 12220 printf(gettext("%*s(end %s[%i])\n"), 12221 depth, "", name, i); 12222 } 12223 printf(gettext("%*s(end %s)\n"), depth, "", name); 12224 } 12225 break; 12226 12227 case DATA_TYPE_INT8_ARRAY: { 12228 int8_t *val; 12229 uint_t i, nelem; 12230 12231 (void) nvpair_value_int8_array(nvp, &val, &nelem); 12232 for (i = 0; i < nelem; i++) 12233 printf(gettext("0x%x "), val[i]); 12234 12235 break; 12236 } 12237 12238 case DATA_TYPE_UINT8_ARRAY: { 12239 uint8_t *val; 12240 uint_t i, nelem; 12241 12242 (void) nvpair_value_uint8_array(nvp, &val, &nelem); 12243 for (i = 0; i < nelem; i++) 12244 printf(gettext("0x%x "), val[i]); 12245 12246 break; 12247 } 12248 12249 case DATA_TYPE_INT16_ARRAY: { 12250 int16_t *val; 12251 uint_t i, nelem; 12252 12253 (void) nvpair_value_int16_array(nvp, &val, &nelem); 12254 for (i = 0; i < nelem; i++) 12255 printf(gettext("0x%x "), val[i]); 12256 12257 break; 12258 } 12259 12260 case DATA_TYPE_UINT16_ARRAY: { 12261 uint16_t *val; 12262 uint_t i, nelem; 12263 12264 (void) nvpair_value_uint16_array(nvp, &val, &nelem); 12265 for (i = 0; i < nelem; i++) 12266 printf(gettext("0x%x "), val[i]); 12267 12268 break; 12269 } 12270 12271 case DATA_TYPE_INT32_ARRAY: { 12272 int32_t *val; 12273 uint_t i, nelem; 12274 12275 (void) nvpair_value_int32_array(nvp, &val, &nelem); 12276 for (i = 0; i < nelem; i++) 12277 printf(gettext("0x%x "), val[i]); 12278 12279 break; 12280 } 12281 12282 case DATA_TYPE_UINT32_ARRAY: { 12283 uint32_t *val; 12284 uint_t i, nelem; 12285 12286 (void) nvpair_value_uint32_array(nvp, &val, &nelem); 12287 for (i = 0; i < nelem; i++) 12288 printf(gettext("0x%x "), val[i]); 12289 12290 break; 12291 } 12292 12293 case DATA_TYPE_INT64_ARRAY: { 12294 int64_t *val; 12295 uint_t i, nelem; 12296 12297 (void) nvpair_value_int64_array(nvp, &val, &nelem); 12298 for (i = 0; i < nelem; i++) 12299 printf(gettext("0x%llx "), 12300 (u_longlong_t)val[i]); 12301 12302 break; 12303 } 12304 12305 case DATA_TYPE_UINT64_ARRAY: { 12306 uint64_t *val; 12307 uint_t i, nelem; 12308 12309 (void) nvpair_value_uint64_array(nvp, &val, &nelem); 12310 for (i = 0; i < nelem; i++) 12311 printf(gettext("0x%llx "), 12312 (u_longlong_t)val[i]); 12313 12314 break; 12315 } 12316 12317 case DATA_TYPE_STRING_ARRAY: { 12318 const char **str; 12319 uint_t i, nelem; 12320 12321 (void) nvpair_value_string_array(nvp, &str, &nelem); 12322 for (i = 0; i < nelem; i++) 12323 printf(gettext("\"%s\" "), 12324 str[i] ? str[i] : "<NULL>"); 12325 12326 break; 12327 } 12328 12329 case DATA_TYPE_BOOLEAN_ARRAY: 12330 case DATA_TYPE_BYTE_ARRAY: 12331 case DATA_TYPE_DOUBLE: 12332 case DATA_TYPE_DONTCARE: 12333 case DATA_TYPE_UNKNOWN: 12334 printf(gettext("<unknown>")); 12335 break; 12336 } 12337 12338 printf(gettext("\n")); 12339 } 12340 } 12341 12342 static int 12343 zpool_do_events_next(ev_opts_t *opts) 12344 { 12345 nvlist_t *nvl; 12346 int zevent_fd, ret, dropped; 12347 const char *pool; 12348 12349 zevent_fd = open(ZFS_DEV, O_RDWR); 12350 VERIFY(zevent_fd >= 0); 12351 12352 if (!opts->scripted) 12353 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); 12354 12355 while (1) { 12356 ret = zpool_events_next(g_zfs, &nvl, &dropped, 12357 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); 12358 if (ret || nvl == NULL) 12359 break; 12360 12361 if (dropped > 0) 12362 (void) printf(gettext("dropped %d events\n"), dropped); 12363 12364 if (strlen(opts->poolname) > 0 && 12365 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && 12366 strcmp(opts->poolname, pool) != 0) 12367 continue; 12368 12369 zpool_do_events_short(nvl, opts); 12370 12371 if (opts->verbose) { 12372 zpool_do_events_nvprint(nvl, 8); 12373 printf(gettext("\n")); 12374 } 12375 (void) fflush(stdout); 12376 12377 nvlist_free(nvl); 12378 } 12379 12380 VERIFY0(close(zevent_fd)); 12381 12382 return (ret); 12383 } 12384 12385 static int 12386 zpool_do_events_clear(void) 12387 { 12388 int count, ret; 12389 12390 ret = zpool_events_clear(g_zfs, &count); 12391 if (!ret) 12392 (void) printf(gettext("cleared %d events\n"), count); 12393 12394 return (ret); 12395 } 12396 12397 /* 12398 * zpool events [-vHf [pool] | -c] 12399 * 12400 * Displays events logs by ZFS. 12401 */ 12402 int 12403 zpool_do_events(int argc, char **argv) 12404 { 12405 ev_opts_t opts = { 0 }; 12406 int ret; 12407 int c; 12408 12409 /* check options */ 12410 while ((c = getopt(argc, argv, "vHfc")) != -1) { 12411 switch (c) { 12412 case 'v': 12413 opts.verbose = 1; 12414 break; 12415 case 'H': 12416 opts.scripted = 1; 12417 break; 12418 case 'f': 12419 opts.follow = 1; 12420 break; 12421 case 'c': 12422 opts.clear = 1; 12423 break; 12424 case '?': 12425 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12426 optopt); 12427 usage(B_FALSE); 12428 } 12429 } 12430 argc -= optind; 12431 argv += optind; 12432 12433 if (argc > 1) { 12434 (void) fprintf(stderr, gettext("too many arguments\n")); 12435 usage(B_FALSE); 12436 } else if (argc == 1) { 12437 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); 12438 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { 12439 (void) fprintf(stderr, 12440 gettext("invalid pool name '%s'\n"), opts.poolname); 12441 usage(B_FALSE); 12442 } 12443 } 12444 12445 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && 12446 opts.clear) { 12447 (void) fprintf(stderr, 12448 gettext("invalid options combined with -c\n")); 12449 usage(B_FALSE); 12450 } 12451 12452 if (opts.clear) 12453 ret = zpool_do_events_clear(); 12454 else 12455 ret = zpool_do_events_next(&opts); 12456 12457 return (ret); 12458 } 12459 12460 static int 12461 get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data) 12462 { 12463 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12464 char value[ZFS_MAXPROPLEN]; 12465 zprop_source_t srctype; 12466 nvlist_t *props, *item, *d; 12467 props = item = d = NULL; 12468 12469 if (cbp->cb_json) { 12470 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "vdevs"); 12471 if (d == NULL) { 12472 fprintf(stderr, "vdevs obj not found.\n"); 12473 exit(1); 12474 } 12475 props = fnvlist_alloc(); 12476 } 12477 12478 for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL; 12479 pl = pl->pl_next) { 12480 char *prop_name; 12481 /* 12482 * If the first property is pool name, it is a special 12483 * placeholder that we can skip. This will also skip 12484 * over the name property when 'all' is specified. 12485 */ 12486 if (pl->pl_prop == ZPOOL_PROP_NAME && 12487 pl == cbp->cb_proplist) 12488 continue; 12489 12490 if (pl->pl_prop == ZPROP_INVAL) { 12491 prop_name = pl->pl_user_prop; 12492 } else { 12493 prop_name = (char *)vdev_prop_to_name(pl->pl_prop); 12494 } 12495 if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop, 12496 prop_name, value, sizeof (value), &srctype, 12497 cbp->cb_literal) == 0) { 12498 zprop_collect_property(vdevname, cbp, prop_name, 12499 value, srctype, NULL, NULL, props); 12500 } 12501 } 12502 12503 if (cbp->cb_json) { 12504 if (!nvlist_empty(props)) { 12505 item = fnvlist_alloc(); 12506 fill_vdev_info(item, zhp, vdevname, B_TRUE, 12507 cbp->cb_json_as_int); 12508 fnvlist_add_nvlist(item, "properties", props); 12509 fnvlist_add_nvlist(d, vdevname, item); 12510 fnvlist_add_nvlist(cbp->cb_jsobj, "vdevs", d); 12511 fnvlist_free(item); 12512 } 12513 fnvlist_free(props); 12514 } 12515 12516 return (0); 12517 } 12518 12519 static int 12520 get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data) 12521 { 12522 zpool_handle_t *zhp = zhp_data; 12523 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12524 char *vdevname; 12525 const char *type; 12526 int ret; 12527 12528 /* 12529 * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the 12530 * pool name for display purposes, which is not desired. Fallback to 12531 * zpool_vdev_name() when not dealing with the root vdev. 12532 */ 12533 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE); 12534 if (zhp != NULL && strcmp(type, "root") == 0) 12535 vdevname = strdup("root-0"); 12536 else 12537 vdevname = zpool_vdev_name(g_zfs, zhp, nv, 12538 cbp->cb_vdevs.cb_name_flags); 12539 12540 (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist); 12541 12542 ret = get_callback_vdev(zhp, vdevname, data); 12543 12544 free(vdevname); 12545 12546 return (ret); 12547 } 12548 12549 static int 12550 get_callback(zpool_handle_t *zhp, void *data) 12551 { 12552 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12553 char value[ZFS_MAXPROPLEN]; 12554 zprop_source_t srctype; 12555 zprop_list_t *pl; 12556 int vid; 12557 int err = 0; 12558 nvlist_t *props, *item, *d; 12559 props = item = d = NULL; 12560 12561 if (cbp->cb_type == ZFS_TYPE_VDEV) { 12562 if (cbp->cb_json) { 12563 nvlist_t *pool = fnvlist_alloc(); 12564 fill_pool_info(pool, zhp, B_FALSE, cbp->cb_json_as_int); 12565 fnvlist_add_nvlist(cbp->cb_jsobj, "pool", pool); 12566 fnvlist_free(pool); 12567 } 12568 12569 if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) { 12570 for_each_vdev(zhp, get_callback_vdev_cb, data); 12571 } else { 12572 /* Adjust column widths for vdev properties */ 12573 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; 12574 vid++) { 12575 vdev_expand_proplist(zhp, 12576 cbp->cb_vdevs.cb_names[vid], 12577 &cbp->cb_proplist); 12578 } 12579 /* Display the properties */ 12580 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; 12581 vid++) { 12582 get_callback_vdev(zhp, 12583 cbp->cb_vdevs.cb_names[vid], data); 12584 } 12585 } 12586 } else { 12587 assert(cbp->cb_type == ZFS_TYPE_POOL); 12588 if (cbp->cb_json) { 12589 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools"); 12590 if (d == NULL) { 12591 fprintf(stderr, "pools obj not found.\n"); 12592 exit(1); 12593 } 12594 props = fnvlist_alloc(); 12595 } 12596 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 12597 /* 12598 * Skip the special fake placeholder. This will also 12599 * skip over the name property when 'all' is specified. 12600 */ 12601 if (pl->pl_prop == ZPOOL_PROP_NAME && 12602 pl == cbp->cb_proplist) 12603 continue; 12604 12605 if (pl->pl_prop == ZPROP_INVAL && 12606 zfs_prop_user(pl->pl_user_prop)) { 12607 srctype = ZPROP_SRC_LOCAL; 12608 12609 if (zpool_get_userprop(zhp, pl->pl_user_prop, 12610 value, sizeof (value), &srctype) != 0) 12611 continue; 12612 12613 err = zprop_collect_property( 12614 zpool_get_name(zhp), cbp, pl->pl_user_prop, 12615 value, srctype, NULL, NULL, props); 12616 } else if (pl->pl_prop == ZPROP_INVAL && 12617 (zpool_prop_feature(pl->pl_user_prop) || 12618 zpool_prop_unsupported(pl->pl_user_prop))) { 12619 srctype = ZPROP_SRC_LOCAL; 12620 12621 if (zpool_prop_get_feature(zhp, 12622 pl->pl_user_prop, value, 12623 sizeof (value)) == 0) { 12624 err = zprop_collect_property( 12625 zpool_get_name(zhp), cbp, 12626 pl->pl_user_prop, value, srctype, 12627 NULL, NULL, props); 12628 } 12629 } else { 12630 if (zpool_get_prop(zhp, pl->pl_prop, value, 12631 sizeof (value), &srctype, 12632 cbp->cb_literal) != 0) 12633 continue; 12634 12635 err = zprop_collect_property( 12636 zpool_get_name(zhp), cbp, 12637 zpool_prop_to_name(pl->pl_prop), 12638 value, srctype, NULL, NULL, props); 12639 } 12640 if (err != 0) 12641 return (err); 12642 } 12643 12644 if (cbp->cb_json) { 12645 if (!nvlist_empty(props)) { 12646 item = fnvlist_alloc(); 12647 fill_pool_info(item, zhp, B_TRUE, 12648 cbp->cb_json_as_int); 12649 fnvlist_add_nvlist(item, "properties", props); 12650 if (cbp->cb_json_pool_key_guid) { 12651 char buf[256]; 12652 uint64_t guid = fnvlist_lookup_uint64( 12653 zpool_get_config(zhp, NULL), 12654 ZPOOL_CONFIG_POOL_GUID); 12655 snprintf(buf, 256, "%llu", 12656 (u_longlong_t)guid); 12657 fnvlist_add_nvlist(d, buf, item); 12658 } else { 12659 const char *name = zpool_get_name(zhp); 12660 fnvlist_add_nvlist(d, name, item); 12661 } 12662 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d); 12663 fnvlist_free(item); 12664 } 12665 fnvlist_free(props); 12666 } 12667 } 12668 12669 return (0); 12670 } 12671 12672 /* 12673 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 12674 * 12675 * -H Scripted mode. Don't display headers, and separate properties 12676 * by a single tab. 12677 * -o List of columns to display. Defaults to 12678 * "name,property,value,source". 12679 * -p Display values in parsable (exact) format. 12680 * -j Display output in JSON format. 12681 * --json-int Display numbers as integers instead of strings. 12682 * --json-pool-key-guid Set pool GUID as key for pool objects. 12683 * 12684 * Get properties of pools in the system. Output space statistics 12685 * for each one as well as other attributes. 12686 */ 12687 int 12688 zpool_do_get(int argc, char **argv) 12689 { 12690 zprop_get_cbdata_t cb = { 0 }; 12691 zprop_list_t fake_name = { 0 }; 12692 int ret; 12693 int c, i; 12694 char *propstr = NULL; 12695 char *vdev = NULL; 12696 nvlist_t *data = NULL; 12697 12698 cb.cb_first = B_TRUE; 12699 12700 /* 12701 * Set up default columns and sources. 12702 */ 12703 cb.cb_sources = ZPROP_SRC_ALL; 12704 cb.cb_columns[0] = GET_COL_NAME; 12705 cb.cb_columns[1] = GET_COL_PROPERTY; 12706 cb.cb_columns[2] = GET_COL_VALUE; 12707 cb.cb_columns[3] = GET_COL_SOURCE; 12708 cb.cb_type = ZFS_TYPE_POOL; 12709 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; 12710 current_prop_type = cb.cb_type; 12711 12712 struct option long_options[] = { 12713 {"json", no_argument, NULL, 'j'}, 12714 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 12715 {"json-pool-key-guid", no_argument, NULL, 12716 ZPOOL_OPTION_POOL_KEY_GUID}, 12717 {0, 0, 0, 0} 12718 }; 12719 12720 /* check options */ 12721 while ((c = getopt_long(argc, argv, ":jHpo:", long_options, 12722 NULL)) != -1) { 12723 switch (c) { 12724 case 'p': 12725 cb.cb_literal = B_TRUE; 12726 break; 12727 case 'H': 12728 cb.cb_scripted = B_TRUE; 12729 break; 12730 case 'j': 12731 cb.cb_json = B_TRUE; 12732 cb.cb_jsobj = zpool_json_schema(0, 1); 12733 data = fnvlist_alloc(); 12734 break; 12735 case ZPOOL_OPTION_POOL_KEY_GUID: 12736 cb.cb_json_pool_key_guid = B_TRUE; 12737 break; 12738 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 12739 cb.cb_json_as_int = B_TRUE; 12740 cb.cb_literal = B_TRUE; 12741 break; 12742 case 'o': 12743 memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); 12744 i = 0; 12745 12746 for (char *tok; (tok = strsep(&optarg, ",")); ) { 12747 static const char *const col_opts[] = 12748 { "name", "property", "value", "source", 12749 "all" }; 12750 static const zfs_get_column_t col_cols[] = 12751 { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE, 12752 GET_COL_SOURCE }; 12753 12754 if (i == ZFS_GET_NCOLS - 1) { 12755 (void) fprintf(stderr, gettext("too " 12756 "many fields given to -o " 12757 "option\n")); 12758 usage(B_FALSE); 12759 } 12760 12761 for (c = 0; c < ARRAY_SIZE(col_opts); ++c) 12762 if (strcmp(tok, col_opts[c]) == 0) 12763 goto found; 12764 12765 (void) fprintf(stderr, 12766 gettext("invalid column name '%s'\n"), tok); 12767 usage(B_FALSE); 12768 12769 found: 12770 if (c >= 4) { 12771 if (i > 0) { 12772 (void) fprintf(stderr, 12773 gettext("\"all\" conflicts " 12774 "with specific fields " 12775 "given to -o option\n")); 12776 usage(B_FALSE); 12777 } 12778 12779 memcpy(cb.cb_columns, col_cols, 12780 sizeof (col_cols)); 12781 i = ZFS_GET_NCOLS - 1; 12782 } else 12783 cb.cb_columns[i++] = col_cols[c]; 12784 } 12785 break; 12786 case '?': 12787 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12788 optopt); 12789 usage(B_FALSE); 12790 } 12791 } 12792 12793 argc -= optind; 12794 argv += optind; 12795 12796 if (!cb.cb_json && cb.cb_json_as_int) { 12797 (void) fprintf(stderr, gettext("'--json-int' only works with" 12798 " '-j' option\n")); 12799 usage(B_FALSE); 12800 } 12801 12802 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 12803 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 12804 " works with '-j' option\n")); 12805 usage(B_FALSE); 12806 } 12807 12808 if (argc < 1) { 12809 (void) fprintf(stderr, gettext("missing property " 12810 "argument\n")); 12811 usage(B_FALSE); 12812 } 12813 12814 /* Properties list is needed later by zprop_get_list() */ 12815 propstr = argv[0]; 12816 12817 argc--; 12818 argv++; 12819 12820 if (argc == 0) { 12821 /* No args, so just print the defaults. */ 12822 } else if (are_all_pools(argc, argv)) { 12823 /* All the args are pool names */ 12824 } else if (are_all_pools(1, argv)) { 12825 /* The first arg is a pool name */ 12826 if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) || 12827 (argc == 2 && strcmp(argv[1], "root") == 0) || 12828 are_vdevs_in_pool(argc - 1, argv + 1, argv[0], 12829 &cb.cb_vdevs)) { 12830 12831 if (strcmp(argv[1], "root") == 0) 12832 vdev = strdup("root-0"); 12833 12834 /* ... and the rest are vdev names */ 12835 if (vdev == NULL) 12836 cb.cb_vdevs.cb_names = argv + 1; 12837 else 12838 cb.cb_vdevs.cb_names = &vdev; 12839 12840 cb.cb_vdevs.cb_names_count = argc - 1; 12841 cb.cb_type = ZFS_TYPE_VDEV; 12842 argc = 1; /* One pool to process */ 12843 } else { 12844 if (cb.cb_json) { 12845 nvlist_free(cb.cb_jsobj); 12846 nvlist_free(data); 12847 } 12848 fprintf(stderr, gettext("Expected a list of vdevs in" 12849 " \"%s\", but got:\n"), argv[0]); 12850 error_list_unresolved_vdevs(argc - 1, argv + 1, 12851 argv[0], &cb.cb_vdevs); 12852 fprintf(stderr, "\n"); 12853 usage(B_FALSE); 12854 return (1); 12855 } 12856 } else { 12857 if (cb.cb_json) { 12858 nvlist_free(cb.cb_jsobj); 12859 nvlist_free(data); 12860 } 12861 /* 12862 * The first arg isn't the name of a valid pool. 12863 */ 12864 fprintf(stderr, gettext("Cannot get properties of %s: " 12865 "no such pool available.\n"), argv[0]); 12866 return (1); 12867 } 12868 12869 if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist, 12870 cb.cb_type) != 0) { 12871 /* Use correct list of valid properties (pool or vdev) */ 12872 current_prop_type = cb.cb_type; 12873 usage(B_FALSE); 12874 } 12875 12876 if (cb.cb_proplist != NULL) { 12877 fake_name.pl_prop = ZPOOL_PROP_NAME; 12878 fake_name.pl_width = strlen(gettext("NAME")); 12879 fake_name.pl_next = cb.cb_proplist; 12880 cb.cb_proplist = &fake_name; 12881 } 12882 12883 if (cb.cb_json) { 12884 if (cb.cb_type == ZFS_TYPE_VDEV) 12885 fnvlist_add_nvlist(cb.cb_jsobj, "vdevs", data); 12886 else 12887 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 12888 fnvlist_free(data); 12889 } 12890 12891 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type, 12892 cb.cb_literal, get_callback, &cb); 12893 12894 if (ret == 0 && cb.cb_json) 12895 zcmd_print_json(cb.cb_jsobj); 12896 else if (ret != 0 && cb.cb_json) 12897 nvlist_free(cb.cb_jsobj); 12898 12899 if (cb.cb_proplist == &fake_name) 12900 zprop_free_list(fake_name.pl_next); 12901 else 12902 zprop_free_list(cb.cb_proplist); 12903 12904 if (vdev != NULL) 12905 free(vdev); 12906 12907 return (ret); 12908 } 12909 12910 typedef struct set_cbdata { 12911 char *cb_propname; 12912 char *cb_value; 12913 zfs_type_t cb_type; 12914 vdev_cbdata_t cb_vdevs; 12915 boolean_t cb_any_successful; 12916 } set_cbdata_t; 12917 12918 static int 12919 set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb) 12920 { 12921 int error; 12922 12923 /* Check if we have out-of-bounds features */ 12924 if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) { 12925 boolean_t features[SPA_FEATURES]; 12926 if (zpool_do_load_compat(cb->cb_value, features) != 12927 ZPOOL_COMPATIBILITY_OK) 12928 return (-1); 12929 12930 nvlist_t *enabled = zpool_get_features(zhp); 12931 spa_feature_t i; 12932 for (i = 0; i < SPA_FEATURES; i++) { 12933 const char *fguid = spa_feature_table[i].fi_guid; 12934 if (nvlist_exists(enabled, fguid) && !features[i]) 12935 break; 12936 } 12937 if (i < SPA_FEATURES) 12938 (void) fprintf(stderr, gettext("Warning: one or " 12939 "more features already enabled on pool '%s'\n" 12940 "are not present in this compatibility set.\n"), 12941 zpool_get_name(zhp)); 12942 } 12943 12944 /* if we're setting a feature, check it's in compatibility set */ 12945 if (zpool_prop_feature(cb->cb_propname) && 12946 strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) { 12947 char *fname = strchr(cb->cb_propname, '@') + 1; 12948 spa_feature_t f; 12949 12950 if (zfeature_lookup_name(fname, &f) == 0) { 12951 char compat[ZFS_MAXPROPLEN]; 12952 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, 12953 compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 12954 compat[0] = '\0'; 12955 12956 boolean_t features[SPA_FEATURES]; 12957 if (zpool_do_load_compat(compat, features) != 12958 ZPOOL_COMPATIBILITY_OK) { 12959 (void) fprintf(stderr, gettext("Error: " 12960 "cannot enable feature '%s' on pool '%s'\n" 12961 "because the pool's 'compatibility' " 12962 "property cannot be parsed.\n"), 12963 fname, zpool_get_name(zhp)); 12964 return (-1); 12965 } 12966 12967 if (!features[f]) { 12968 (void) fprintf(stderr, gettext("Error: " 12969 "cannot enable feature '%s' on pool '%s'\n" 12970 "as it is not specified in this pool's " 12971 "current compatibility set.\n" 12972 "Consider setting 'compatibility' to a " 12973 "less restrictive set, or to 'off'.\n"), 12974 fname, zpool_get_name(zhp)); 12975 return (-1); 12976 } 12977 } 12978 } 12979 12980 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 12981 12982 return (error); 12983 } 12984 12985 static int 12986 set_callback(zpool_handle_t *zhp, void *data) 12987 { 12988 int error; 12989 set_cbdata_t *cb = (set_cbdata_t *)data; 12990 12991 if (cb->cb_type == ZFS_TYPE_VDEV) { 12992 error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names, 12993 cb->cb_propname, cb->cb_value); 12994 } else { 12995 assert(cb->cb_type == ZFS_TYPE_POOL); 12996 error = set_pool_callback(zhp, cb); 12997 } 12998 12999 cb->cb_any_successful = !error; 13000 return (error); 13001 } 13002 13003 int 13004 zpool_do_set(int argc, char **argv) 13005 { 13006 set_cbdata_t cb = { 0 }; 13007 int error; 13008 char *vdev = NULL; 13009 13010 current_prop_type = ZFS_TYPE_POOL; 13011 if (argc > 1 && argv[1][0] == '-') { 13012 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13013 argv[1][1]); 13014 usage(B_FALSE); 13015 } 13016 13017 if (argc < 2) { 13018 (void) fprintf(stderr, gettext("missing property=value " 13019 "argument\n")); 13020 usage(B_FALSE); 13021 } 13022 13023 if (argc < 3) { 13024 (void) fprintf(stderr, gettext("missing pool name\n")); 13025 usage(B_FALSE); 13026 } 13027 13028 if (argc > 4) { 13029 (void) fprintf(stderr, gettext("too many pool names\n")); 13030 usage(B_FALSE); 13031 } 13032 13033 cb.cb_propname = argv[1]; 13034 cb.cb_type = ZFS_TYPE_POOL; 13035 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; 13036 cb.cb_value = strchr(cb.cb_propname, '='); 13037 if (cb.cb_value == NULL) { 13038 (void) fprintf(stderr, gettext("missing value in " 13039 "property=value argument\n")); 13040 usage(B_FALSE); 13041 } 13042 13043 *(cb.cb_value) = '\0'; 13044 cb.cb_value++; 13045 argc -= 2; 13046 argv += 2; 13047 13048 /* argv[0] is pool name */ 13049 if (!is_pool(argv[0])) { 13050 (void) fprintf(stderr, 13051 gettext("cannot open '%s': is not a pool\n"), argv[0]); 13052 return (EINVAL); 13053 } 13054 13055 /* argv[1], when supplied, is vdev name */ 13056 if (argc == 2) { 13057 13058 if (strcmp(argv[1], "root") == 0) 13059 vdev = strdup("root-0"); 13060 else 13061 vdev = strdup(argv[1]); 13062 13063 if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) { 13064 (void) fprintf(stderr, gettext( 13065 "cannot find '%s' in '%s': device not in pool\n"), 13066 vdev, argv[0]); 13067 free(vdev); 13068 return (EINVAL); 13069 } 13070 cb.cb_vdevs.cb_names = &vdev; 13071 cb.cb_vdevs.cb_names_count = 1; 13072 cb.cb_type = ZFS_TYPE_VDEV; 13073 } 13074 13075 error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 13076 B_FALSE, set_callback, &cb); 13077 13078 if (vdev != NULL) 13079 free(vdev); 13080 13081 return (error); 13082 } 13083 13084 /* Add up the total number of bytes left to initialize/trim across all vdevs */ 13085 static uint64_t 13086 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) 13087 { 13088 uint64_t bytes_remaining; 13089 nvlist_t **child; 13090 uint_t c, children; 13091 vdev_stat_t *vs; 13092 13093 assert(activity == ZPOOL_WAIT_INITIALIZE || 13094 activity == ZPOOL_WAIT_TRIM); 13095 13096 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 13097 (uint64_t **)&vs, &c) == 0); 13098 13099 if (activity == ZPOOL_WAIT_INITIALIZE && 13100 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) 13101 bytes_remaining = vs->vs_initialize_bytes_est - 13102 vs->vs_initialize_bytes_done; 13103 else if (activity == ZPOOL_WAIT_TRIM && 13104 vs->vs_trim_state == VDEV_TRIM_ACTIVE) 13105 bytes_remaining = vs->vs_trim_bytes_est - 13106 vs->vs_trim_bytes_done; 13107 else 13108 bytes_remaining = 0; 13109 13110 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13111 &child, &children) != 0) 13112 children = 0; 13113 13114 for (c = 0; c < children; c++) 13115 bytes_remaining += vdev_activity_remaining(child[c], activity); 13116 13117 return (bytes_remaining); 13118 } 13119 13120 /* Add up the total number of bytes left to rebuild across top-level vdevs */ 13121 static uint64_t 13122 vdev_activity_top_remaining(nvlist_t *nv) 13123 { 13124 uint64_t bytes_remaining = 0; 13125 nvlist_t **child; 13126 uint_t children; 13127 int error; 13128 13129 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13130 &child, &children) != 0) 13131 children = 0; 13132 13133 for (uint_t c = 0; c < children; c++) { 13134 vdev_rebuild_stat_t *vrs; 13135 uint_t i; 13136 13137 error = nvlist_lookup_uint64_array(child[c], 13138 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); 13139 if (error == 0) { 13140 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 13141 bytes_remaining += (vrs->vrs_bytes_est - 13142 vrs->vrs_bytes_rebuilt); 13143 } 13144 } 13145 } 13146 13147 return (bytes_remaining); 13148 } 13149 13150 /* Whether any vdevs are 'spare' or 'replacing' vdevs */ 13151 static boolean_t 13152 vdev_any_spare_replacing(nvlist_t *nv) 13153 { 13154 nvlist_t **child; 13155 uint_t c, children; 13156 const char *vdev_type; 13157 13158 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); 13159 13160 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || 13161 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || 13162 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { 13163 return (B_TRUE); 13164 } 13165 13166 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13167 &child, &children) != 0) 13168 children = 0; 13169 13170 for (c = 0; c < children; c++) { 13171 if (vdev_any_spare_replacing(child[c])) 13172 return (B_TRUE); 13173 } 13174 13175 return (B_FALSE); 13176 } 13177 13178 typedef struct wait_data { 13179 char *wd_poolname; 13180 boolean_t wd_scripted; 13181 boolean_t wd_exact; 13182 boolean_t wd_headers_once; 13183 boolean_t wd_should_exit; 13184 /* Which activities to wait for */ 13185 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; 13186 float wd_interval; 13187 pthread_cond_t wd_cv; 13188 pthread_mutex_t wd_mutex; 13189 } wait_data_t; 13190 13191 /* 13192 * Print to stdout a single line, containing one column for each activity that 13193 * we are waiting for specifying how many bytes of work are left for that 13194 * activity. 13195 */ 13196 static void 13197 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) 13198 { 13199 nvlist_t *config, *nvroot; 13200 uint_t c; 13201 int i; 13202 pool_checkpoint_stat_t *pcs = NULL; 13203 pool_scan_stat_t *pss = NULL; 13204 pool_removal_stat_t *prs = NULL; 13205 pool_raidz_expand_stat_t *pres = NULL; 13206 const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE", 13207 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"}; 13208 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; 13209 13210 /* Calculate the width of each column */ 13211 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13212 /* 13213 * Make sure we have enough space in the col for pretty-printed 13214 * numbers and for the column header, and then leave a couple 13215 * spaces between cols for readability. 13216 */ 13217 col_widths[i] = MAX(strlen(headers[i]), 6) + 2; 13218 } 13219 13220 if (timestamp_fmt != NODATE) 13221 print_timestamp(timestamp_fmt); 13222 13223 /* Print header if appropriate */ 13224 int term_height = terminal_height(); 13225 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && 13226 row % (term_height-1) == 0); 13227 if (!wd->wd_scripted && (row == 0 || reprint_header)) { 13228 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13229 if (wd->wd_enabled[i]) 13230 (void) printf("%*s", col_widths[i], headers[i]); 13231 } 13232 (void) fputc('\n', stdout); 13233 } 13234 13235 /* Bytes of work remaining in each activity */ 13236 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; 13237 13238 bytes_rem[ZPOOL_WAIT_FREE] = 13239 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); 13240 13241 config = zpool_get_config(zhp, NULL); 13242 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 13243 13244 (void) nvlist_lookup_uint64_array(nvroot, 13245 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 13246 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 13247 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; 13248 13249 (void) nvlist_lookup_uint64_array(nvroot, 13250 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 13251 if (prs != NULL && prs->prs_state == DSS_SCANNING) 13252 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - 13253 prs->prs_copied; 13254 13255 (void) nvlist_lookup_uint64_array(nvroot, 13256 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); 13257 if (pss != NULL && pss->pss_state == DSS_SCANNING && 13258 pss->pss_pass_scrub_pause == 0) { 13259 int64_t rem = pss->pss_to_examine - pss->pss_issued; 13260 if (pss->pss_func == POOL_SCAN_SCRUB) 13261 bytes_rem[ZPOOL_WAIT_SCRUB] = rem; 13262 else 13263 bytes_rem[ZPOOL_WAIT_RESILVER] = rem; 13264 } else if (check_rebuilding(nvroot, NULL)) { 13265 bytes_rem[ZPOOL_WAIT_RESILVER] = 13266 vdev_activity_top_remaining(nvroot); 13267 } 13268 13269 (void) nvlist_lookup_uint64_array(nvroot, 13270 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); 13271 if (pres != NULL && pres->pres_state == DSS_SCANNING) { 13272 int64_t rem = pres->pres_to_reflow - pres->pres_reflowed; 13273 bytes_rem[ZPOOL_WAIT_RAIDZ_EXPAND] = rem; 13274 } 13275 13276 bytes_rem[ZPOOL_WAIT_INITIALIZE] = 13277 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); 13278 bytes_rem[ZPOOL_WAIT_TRIM] = 13279 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); 13280 13281 /* 13282 * A replace finishes after resilvering finishes, so the amount of work 13283 * left for a replace is the same as for resilvering. 13284 * 13285 * It isn't quite correct to say that if we have any 'spare' or 13286 * 'replacing' vdevs and a resilver is happening, then a replace is in 13287 * progress, like we do here. When a hot spare is used, the faulted vdev 13288 * is not removed after the hot spare is resilvered, so parent 'spare' 13289 * vdev is not removed either. So we could have a 'spare' vdev, but be 13290 * resilvering for a different reason. However, we use it as a heuristic 13291 * because we don't have access to the DTLs, which could tell us whether 13292 * or not we have really finished resilvering a hot spare. 13293 */ 13294 if (vdev_any_spare_replacing(nvroot)) 13295 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; 13296 13297 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13298 char buf[64]; 13299 if (!wd->wd_enabled[i]) 13300 continue; 13301 13302 if (wd->wd_exact) { 13303 (void) snprintf(buf, sizeof (buf), "%" PRIi64, 13304 bytes_rem[i]); 13305 } else { 13306 zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); 13307 } 13308 13309 if (wd->wd_scripted) 13310 (void) printf(i == 0 ? "%s" : "\t%s", buf); 13311 else 13312 (void) printf(" %*s", col_widths[i] - 1, buf); 13313 } 13314 (void) printf("\n"); 13315 (void) fflush(stdout); 13316 } 13317 13318 static void * 13319 wait_status_thread(void *arg) 13320 { 13321 wait_data_t *wd = (wait_data_t *)arg; 13322 zpool_handle_t *zhp; 13323 13324 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) 13325 return (void *)(1); 13326 13327 for (int row = 0; ; row++) { 13328 boolean_t missing; 13329 struct timespec timeout; 13330 int ret = 0; 13331 (void) clock_gettime(CLOCK_REALTIME, &timeout); 13332 13333 if (zpool_refresh_stats(zhp, &missing) != 0 || missing || 13334 zpool_props_refresh(zhp) != 0) { 13335 zpool_close(zhp); 13336 return (void *)(uintptr_t)(missing ? 0 : 1); 13337 } 13338 13339 print_wait_status_row(wd, zhp, row); 13340 13341 timeout.tv_sec += floor(wd->wd_interval); 13342 long nanos = timeout.tv_nsec + 13343 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; 13344 if (nanos >= NANOSEC) { 13345 timeout.tv_sec++; 13346 timeout.tv_nsec = nanos - NANOSEC; 13347 } else { 13348 timeout.tv_nsec = nanos; 13349 } 13350 pthread_mutex_lock(&wd->wd_mutex); 13351 if (!wd->wd_should_exit) 13352 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, 13353 &timeout); 13354 pthread_mutex_unlock(&wd->wd_mutex); 13355 if (ret == 0) { 13356 break; /* signaled by main thread */ 13357 } else if (ret != ETIMEDOUT) { 13358 (void) fprintf(stderr, gettext("pthread_cond_timedwait " 13359 "failed: %s\n"), strerror(ret)); 13360 zpool_close(zhp); 13361 return (void *)(uintptr_t)(1); 13362 } 13363 } 13364 13365 zpool_close(zhp); 13366 return (void *)(0); 13367 } 13368 13369 int 13370 zpool_do_wait(int argc, char **argv) 13371 { 13372 boolean_t verbose = B_FALSE; 13373 int c, i; 13374 unsigned long count; 13375 pthread_t status_thr; 13376 int error = 0; 13377 zpool_handle_t *zhp; 13378 13379 wait_data_t wd; 13380 wd.wd_scripted = B_FALSE; 13381 wd.wd_exact = B_FALSE; 13382 wd.wd_headers_once = B_FALSE; 13383 wd.wd_should_exit = B_FALSE; 13384 13385 pthread_mutex_init(&wd.wd_mutex, NULL); 13386 pthread_cond_init(&wd.wd_cv, NULL); 13387 13388 /* By default, wait for all types of activity. */ 13389 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) 13390 wd.wd_enabled[i] = B_TRUE; 13391 13392 while ((c = getopt(argc, argv, "HpT:t:")) != -1) { 13393 switch (c) { 13394 case 'H': 13395 wd.wd_scripted = B_TRUE; 13396 break; 13397 case 'n': 13398 wd.wd_headers_once = B_TRUE; 13399 break; 13400 case 'p': 13401 wd.wd_exact = B_TRUE; 13402 break; 13403 case 'T': 13404 get_timestamp_arg(*optarg); 13405 break; 13406 case 't': 13407 /* Reset activities array */ 13408 memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled)); 13409 13410 for (char *tok; (tok = strsep(&optarg, ",")); ) { 13411 static const char *const col_opts[] = { 13412 "discard", "free", "initialize", "replace", 13413 "remove", "resilver", "scrub", "trim", 13414 "raidz_expand" }; 13415 13416 for (i = 0; i < ARRAY_SIZE(col_opts); ++i) 13417 if (strcmp(tok, col_opts[i]) == 0) { 13418 wd.wd_enabled[i] = B_TRUE; 13419 goto found; 13420 } 13421 13422 (void) fprintf(stderr, 13423 gettext("invalid activity '%s'\n"), tok); 13424 usage(B_FALSE); 13425 found:; 13426 } 13427 break; 13428 case '?': 13429 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13430 optopt); 13431 usage(B_FALSE); 13432 } 13433 } 13434 13435 argc -= optind; 13436 argv += optind; 13437 13438 get_interval_count(&argc, argv, &wd.wd_interval, &count); 13439 if (count != 0) { 13440 /* This subcmd only accepts an interval, not a count */ 13441 (void) fprintf(stderr, gettext("too many arguments\n")); 13442 usage(B_FALSE); 13443 } 13444 13445 if (wd.wd_interval != 0) 13446 verbose = B_TRUE; 13447 13448 if (argc < 1) { 13449 (void) fprintf(stderr, gettext("missing 'pool' argument\n")); 13450 usage(B_FALSE); 13451 } 13452 if (argc > 1) { 13453 (void) fprintf(stderr, gettext("too many arguments\n")); 13454 usage(B_FALSE); 13455 } 13456 13457 wd.wd_poolname = argv[0]; 13458 13459 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) 13460 return (1); 13461 13462 if (verbose) { 13463 /* 13464 * We use a separate thread for printing status updates because 13465 * the main thread will call lzc_wait(), which blocks as long 13466 * as an activity is in progress, which can be a long time. 13467 */ 13468 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) 13469 != 0) { 13470 (void) fprintf(stderr, gettext("failed to create status" 13471 "thread: %s\n"), strerror(errno)); 13472 zpool_close(zhp); 13473 return (1); 13474 } 13475 } 13476 13477 /* 13478 * Loop over all activities that we are supposed to wait for until none 13479 * of them are in progress. Note that this means we can end up waiting 13480 * for more activities to complete than just those that were in progress 13481 * when we began waiting; if an activity we are interested in begins 13482 * while we are waiting for another activity, we will wait for both to 13483 * complete before exiting. 13484 */ 13485 for (;;) { 13486 boolean_t missing = B_FALSE; 13487 boolean_t any_waited = B_FALSE; 13488 13489 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13490 boolean_t waited; 13491 13492 if (!wd.wd_enabled[i]) 13493 continue; 13494 13495 error = zpool_wait_status(zhp, i, &missing, &waited); 13496 if (error != 0 || missing) 13497 break; 13498 13499 any_waited = (any_waited || waited); 13500 } 13501 13502 if (error != 0 || missing || !any_waited) 13503 break; 13504 } 13505 13506 zpool_close(zhp); 13507 13508 if (verbose) { 13509 uintptr_t status; 13510 pthread_mutex_lock(&wd.wd_mutex); 13511 wd.wd_should_exit = B_TRUE; 13512 pthread_cond_signal(&wd.wd_cv); 13513 pthread_mutex_unlock(&wd.wd_mutex); 13514 (void) pthread_join(status_thr, (void *)&status); 13515 if (status != 0) 13516 error = status; 13517 } 13518 13519 pthread_mutex_destroy(&wd.wd_mutex); 13520 pthread_cond_destroy(&wd.wd_cv); 13521 return (error); 13522 } 13523 13524 /* 13525 * zpool ddtprune -d|-p <amount> <pool> 13526 * 13527 * -d <days> Prune entries <days> old and older 13528 * -p <percent> Prune <percent> amount of entries 13529 * 13530 * Prune single reference entries from DDT to satisfy the amount specified. 13531 */ 13532 int 13533 zpool_do_ddt_prune(int argc, char **argv) 13534 { 13535 zpool_ddt_prune_unit_t unit = ZPOOL_DDT_PRUNE_NONE; 13536 uint64_t amount = 0; 13537 zpool_handle_t *zhp; 13538 char *endptr; 13539 int c; 13540 13541 while ((c = getopt(argc, argv, "d:p:")) != -1) { 13542 switch (c) { 13543 case 'd': 13544 if (unit == ZPOOL_DDT_PRUNE_PERCENTAGE) { 13545 (void) fprintf(stderr, gettext("-d cannot be " 13546 "combined with -p option\n")); 13547 usage(B_FALSE); 13548 } 13549 errno = 0; 13550 amount = strtoull(optarg, &endptr, 0); 13551 if (errno != 0 || *endptr != '\0' || amount == 0) { 13552 (void) fprintf(stderr, 13553 gettext("invalid days value\n")); 13554 usage(B_FALSE); 13555 } 13556 amount *= 86400; /* convert days to seconds */ 13557 unit = ZPOOL_DDT_PRUNE_AGE; 13558 break; 13559 case 'p': 13560 if (unit == ZPOOL_DDT_PRUNE_AGE) { 13561 (void) fprintf(stderr, gettext("-p cannot be " 13562 "combined with -d option\n")); 13563 usage(B_FALSE); 13564 } 13565 errno = 0; 13566 amount = strtoull(optarg, &endptr, 0); 13567 if (errno != 0 || *endptr != '\0' || 13568 amount == 0 || amount > 100) { 13569 (void) fprintf(stderr, 13570 gettext("invalid percentage value\n")); 13571 usage(B_FALSE); 13572 } 13573 unit = ZPOOL_DDT_PRUNE_PERCENTAGE; 13574 break; 13575 case '?': 13576 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13577 optopt); 13578 usage(B_FALSE); 13579 } 13580 } 13581 argc -= optind; 13582 argv += optind; 13583 13584 if (unit == ZPOOL_DDT_PRUNE_NONE) { 13585 (void) fprintf(stderr, 13586 gettext("missing amount option (-d|-p <value>)\n")); 13587 usage(B_FALSE); 13588 } else if (argc < 1) { 13589 (void) fprintf(stderr, gettext("missing pool argument\n")); 13590 usage(B_FALSE); 13591 } else if (argc > 1) { 13592 (void) fprintf(stderr, gettext("too many arguments\n")); 13593 usage(B_FALSE); 13594 } 13595 zhp = zpool_open(g_zfs, argv[0]); 13596 if (zhp == NULL) 13597 return (-1); 13598 13599 int error = zpool_ddt_prune(zhp, unit, amount); 13600 13601 zpool_close(zhp); 13602 13603 return (error); 13604 } 13605 13606 static int 13607 find_command_idx(const char *command, int *idx) 13608 { 13609 for (int i = 0; i < NCOMMAND; ++i) { 13610 if (command_table[i].name == NULL) 13611 continue; 13612 13613 if (strcmp(command, command_table[i].name) == 0) { 13614 *idx = i; 13615 return (0); 13616 } 13617 } 13618 return (1); 13619 } 13620 13621 /* 13622 * Display version message 13623 */ 13624 static int 13625 zpool_do_version(int argc, char **argv) 13626 { 13627 int c; 13628 nvlist_t *jsobj = NULL, *zfs_ver = NULL; 13629 boolean_t json = B_FALSE; 13630 13631 struct option long_options[] = { 13632 {"json", no_argument, NULL, 'j'}, 13633 }; 13634 13635 while ((c = getopt_long(argc, argv, "j", long_options, NULL)) != -1) { 13636 switch (c) { 13637 case 'j': 13638 json = B_TRUE; 13639 jsobj = zpool_json_schema(0, 1); 13640 break; 13641 case '?': 13642 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13643 optopt); 13644 usage(B_FALSE); 13645 } 13646 } 13647 13648 argc -= optind; 13649 if (argc != 0) { 13650 (void) fprintf(stderr, "too many arguments\n"); 13651 usage(B_FALSE); 13652 } 13653 13654 if (json) { 13655 zfs_ver = zfs_version_nvlist(); 13656 if (zfs_ver) { 13657 fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver); 13658 zcmd_print_json(jsobj); 13659 fnvlist_free(zfs_ver); 13660 return (0); 13661 } else 13662 return (-1); 13663 } else 13664 return (zfs_version_print() != 0); 13665 } 13666 13667 /* Display documentation */ 13668 static int 13669 zpool_do_help(int argc, char **argv) 13670 { 13671 char page[MAXNAMELEN]; 13672 if (argc < 3 || strcmp(argv[2], "zpool") == 0) 13673 strcpy(page, "zpool"); 13674 else if (strcmp(argv[2], "concepts") == 0 || 13675 strcmp(argv[2], "props") == 0) 13676 snprintf(page, sizeof (page), "zpool%s", argv[2]); 13677 else 13678 snprintf(page, sizeof (page), "zpool-%s", argv[2]); 13679 13680 execlp("man", "man", page, NULL); 13681 13682 fprintf(stderr, "couldn't run man program: %s", strerror(errno)); 13683 return (-1); 13684 } 13685 13686 /* 13687 * Do zpool_load_compat() and print error message on failure 13688 */ 13689 static zpool_compat_status_t 13690 zpool_do_load_compat(const char *compat, boolean_t *list) 13691 { 13692 char report[1024]; 13693 13694 zpool_compat_status_t ret; 13695 13696 ret = zpool_load_compat(compat, list, report, 1024); 13697 switch (ret) { 13698 13699 case ZPOOL_COMPATIBILITY_OK: 13700 break; 13701 13702 case ZPOOL_COMPATIBILITY_NOFILES: 13703 case ZPOOL_COMPATIBILITY_BADFILE: 13704 case ZPOOL_COMPATIBILITY_BADTOKEN: 13705 (void) fprintf(stderr, "Error: %s\n", report); 13706 break; 13707 13708 case ZPOOL_COMPATIBILITY_WARNTOKEN: 13709 (void) fprintf(stderr, "Warning: %s\n", report); 13710 ret = ZPOOL_COMPATIBILITY_OK; 13711 break; 13712 } 13713 return (ret); 13714 } 13715 13716 int 13717 main(int argc, char **argv) 13718 { 13719 int ret = 0; 13720 int i = 0; 13721 char *cmdname; 13722 char **newargv; 13723 13724 (void) setlocale(LC_ALL, ""); 13725 (void) setlocale(LC_NUMERIC, "C"); 13726 (void) textdomain(TEXT_DOMAIN); 13727 srand(time(NULL)); 13728 13729 opterr = 0; 13730 13731 /* 13732 * Make sure the user has specified some command. 13733 */ 13734 if (argc < 2) { 13735 (void) fprintf(stderr, gettext("missing command\n")); 13736 usage(B_FALSE); 13737 } 13738 13739 cmdname = argv[1]; 13740 13741 /* 13742 * Special case '-?' 13743 */ 13744 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) 13745 usage(B_TRUE); 13746 13747 /* 13748 * Special case '-V|--version' 13749 */ 13750 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) 13751 return (zfs_version_print() != 0); 13752 13753 /* 13754 * Special case 'help' 13755 */ 13756 if (strcmp(cmdname, "help") == 0) 13757 return (zpool_do_help(argc, argv)); 13758 13759 if ((g_zfs = libzfs_init()) == NULL) { 13760 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); 13761 return (1); 13762 } 13763 13764 libzfs_print_on_error(g_zfs, B_TRUE); 13765 13766 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 13767 13768 /* 13769 * Many commands modify input strings for string parsing reasons. 13770 * We create a copy to protect the original argv. 13771 */ 13772 newargv = safe_malloc((argc + 1) * sizeof (newargv[0])); 13773 for (i = 0; i < argc; i++) 13774 newargv[i] = strdup(argv[i]); 13775 newargv[argc] = NULL; 13776 13777 /* 13778 * Run the appropriate command. 13779 */ 13780 if (find_command_idx(cmdname, &i) == 0) { 13781 current_command = &command_table[i]; 13782 ret = command_table[i].func(argc - 1, newargv + 1); 13783 } else if (strchr(cmdname, '=')) { 13784 verify(find_command_idx("set", &i) == 0); 13785 current_command = &command_table[i]; 13786 ret = command_table[i].func(argc, newargv); 13787 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 13788 /* 13789 * 'freeze' is a vile debugging abomination, so we treat 13790 * it as such. 13791 */ 13792 zfs_cmd_t zc = {"\0"}; 13793 13794 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 13795 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); 13796 if (ret != 0) { 13797 (void) fprintf(stderr, 13798 gettext("failed to freeze pool: %d\n"), errno); 13799 ret = 1; 13800 } 13801 13802 log_history = 0; 13803 } else { 13804 (void) fprintf(stderr, gettext("unrecognized " 13805 "command '%s'\n"), cmdname); 13806 usage(B_FALSE); 13807 ret = 1; 13808 } 13809 13810 for (i = 0; i < argc; i++) 13811 free(newargv[i]); 13812 free(newargv); 13813 13814 if (ret == 0 && log_history) 13815 (void) zpool_log_history(g_zfs, history_str); 13816 13817 libzfs_fini(g_zfs); 13818 13819 /* 13820 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 13821 * for the purposes of running ::findleaks. 13822 */ 13823 if (getenv("ZFS_ABORT") != NULL) { 13824 (void) printf("dumping core by request\n"); 13825 abort(); 13826 } 13827 13828 return (ret); 13829 } 13830