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, 2025, 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 /* 5765 * Callback to print out the iostats for the given pool. 5766 */ 5767 static int 5768 print_iostat(zpool_handle_t *zhp, void *data) 5769 { 5770 iostat_cbdata_t *cb = data; 5771 nvlist_t *oldconfig, *newconfig; 5772 nvlist_t *oldnvroot, *newnvroot; 5773 int ret; 5774 5775 newconfig = zpool_get_config(zhp, &oldconfig); 5776 5777 if (cb->cb_iteration == 1) 5778 oldconfig = NULL; 5779 5780 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 5781 &newnvroot) == 0); 5782 5783 if (oldconfig == NULL) 5784 oldnvroot = NULL; 5785 else 5786 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 5787 &oldnvroot) == 0); 5788 5789 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, 5790 cb, 0); 5791 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && 5792 !cb->cb_scripted && cb->cb_verbose && 5793 !cb->cb_vdevs.cb_names_count) { 5794 print_iostat_separator(cb); 5795 if (cb->vcdl != NULL) { 5796 print_cmd_columns(cb->vcdl, 1); 5797 } 5798 printf("\n"); 5799 } 5800 5801 return (ret); 5802 } 5803 5804 static int 5805 get_columns(void) 5806 { 5807 struct winsize ws; 5808 int columns = 80; 5809 int error; 5810 5811 if (isatty(STDOUT_FILENO)) { 5812 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); 5813 if (error == 0) 5814 columns = ws.ws_col; 5815 } else { 5816 columns = 999; 5817 } 5818 5819 return (columns); 5820 } 5821 5822 /* 5823 * Return the required length of the pool/vdev name column. The minimum 5824 * allowed width and output formatting flags must be provided. 5825 */ 5826 static int 5827 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) 5828 { 5829 nvlist_t *config, *nvroot; 5830 int width = min_width; 5831 5832 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 5833 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5834 &nvroot) == 0); 5835 size_t poolname_len = strlen(zpool_get_name(zhp)); 5836 if (verbose == B_FALSE) { 5837 width = MAX(poolname_len, min_width); 5838 } else { 5839 width = MAX(poolname_len, 5840 max_width(zhp, nvroot, 0, min_width, flags)); 5841 } 5842 } 5843 5844 return (width); 5845 } 5846 5847 /* 5848 * Parse the input string, get the 'interval' and 'count' value if there is one. 5849 */ 5850 static void 5851 get_interval_count(int *argcp, char **argv, float *iv, 5852 unsigned long *cnt) 5853 { 5854 float interval = 0; 5855 unsigned long count = 0; 5856 int argc = *argcp; 5857 5858 /* 5859 * Determine if the last argument is an integer or a pool name 5860 */ 5861 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 5862 char *end; 5863 5864 errno = 0; 5865 interval = strtof(argv[argc - 1], &end); 5866 5867 if (*end == '\0' && errno == 0) { 5868 if (interval == 0) { 5869 (void) fprintf(stderr, gettext( 5870 "interval cannot be zero\n")); 5871 usage(B_FALSE); 5872 } 5873 /* 5874 * Ignore the last parameter 5875 */ 5876 argc--; 5877 } else { 5878 /* 5879 * If this is not a valid number, just plow on. The 5880 * user will get a more informative error message later 5881 * on. 5882 */ 5883 interval = 0; 5884 } 5885 } 5886 5887 /* 5888 * If the last argument is also an integer, then we have both a count 5889 * and an interval. 5890 */ 5891 if (argc > 0 && zfs_isnumber(argv[argc - 1])) { 5892 char *end; 5893 5894 errno = 0; 5895 count = interval; 5896 interval = strtof(argv[argc - 1], &end); 5897 5898 if (*end == '\0' && errno == 0) { 5899 if (interval == 0) { 5900 (void) fprintf(stderr, gettext( 5901 "interval cannot be zero\n")); 5902 usage(B_FALSE); 5903 } 5904 5905 /* 5906 * Ignore the last parameter 5907 */ 5908 argc--; 5909 } else { 5910 interval = 0; 5911 } 5912 } 5913 5914 *iv = interval; 5915 *cnt = count; 5916 *argcp = argc; 5917 } 5918 5919 static void 5920 get_timestamp_arg(char c) 5921 { 5922 if (c == 'u') 5923 timestamp_fmt = UDATE; 5924 else if (c == 'd') 5925 timestamp_fmt = DDATE; 5926 else 5927 usage(B_FALSE); 5928 } 5929 5930 /* 5931 * Return stat flags that are supported by all pools by both the module and 5932 * zpool iostat. "*data" should be initialized to all 0xFFs before running. 5933 * It will get ANDed down until only the flags that are supported on all pools 5934 * remain. 5935 */ 5936 static int 5937 get_stat_flags_cb(zpool_handle_t *zhp, void *data) 5938 { 5939 uint64_t *mask = data; 5940 nvlist_t *config, *nvroot, *nvx; 5941 uint64_t flags = 0; 5942 int i, j; 5943 5944 config = zpool_get_config(zhp, NULL); 5945 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5946 &nvroot) == 0); 5947 5948 /* Default stats are always supported, but for completeness.. */ 5949 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) 5950 flags |= IOS_DEFAULT_M; 5951 5952 /* Get our extended stats nvlist from the main list */ 5953 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, 5954 &nvx) != 0) { 5955 /* 5956 * No extended stats; they're probably running an older 5957 * module. No big deal, we support that too. 5958 */ 5959 goto end; 5960 } 5961 5962 /* For each extended stat, make sure all its nvpairs are supported */ 5963 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { 5964 if (!vsx_type_to_nvlist[j][0]) 5965 continue; 5966 5967 /* Start off by assuming the flag is supported, then check */ 5968 flags |= (1ULL << j); 5969 for (i = 0; vsx_type_to_nvlist[j][i]; i++) { 5970 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { 5971 /* flag isn't supported */ 5972 flags = flags & ~(1ULL << j); 5973 break; 5974 } 5975 } 5976 } 5977 end: 5978 *mask = *mask & flags; 5979 return (0); 5980 } 5981 5982 /* 5983 * Return a bitmask of stats that are supported on all pools by both the module 5984 * and zpool iostat. 5985 */ 5986 static uint64_t 5987 get_stat_flags(zpool_list_t *list) 5988 { 5989 uint64_t mask = -1; 5990 5991 /* 5992 * get_stat_flags_cb() will lop off bits from "mask" until only the 5993 * flags that are supported on all pools remain. 5994 */ 5995 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); 5996 return (mask); 5997 } 5998 5999 /* 6000 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise. 6001 */ 6002 static int 6003 is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data) 6004 { 6005 uint64_t guid; 6006 vdev_cbdata_t *cb = cb_data; 6007 zpool_handle_t *zhp = zhp_data; 6008 6009 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 6010 return (0); 6011 6012 return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0])); 6013 } 6014 6015 /* 6016 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise. 6017 */ 6018 static int 6019 is_vdev(zpool_handle_t *zhp, void *cb_data) 6020 { 6021 return (for_each_vdev(zhp, is_vdev_cb, cb_data)); 6022 } 6023 6024 /* 6025 * Check if vdevs are in a pool 6026 * 6027 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise 6028 * return 0. If pool_name is NULL, then search all pools. 6029 */ 6030 static int 6031 are_vdevs_in_pool(int argc, char **argv, char *pool_name, 6032 vdev_cbdata_t *cb) 6033 { 6034 char **tmp_name; 6035 int ret = 0; 6036 int i; 6037 int pool_count = 0; 6038 6039 if ((argc == 0) || !*argv) 6040 return (0); 6041 6042 if (pool_name) 6043 pool_count = 1; 6044 6045 /* Temporarily hijack cb_names for a second... */ 6046 tmp_name = cb->cb_names; 6047 6048 /* Go though our list of prospective vdev names */ 6049 for (i = 0; i < argc; i++) { 6050 cb->cb_names = argv + i; 6051 6052 /* Is this name a vdev in our pools? */ 6053 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, 6054 ZFS_TYPE_POOL, B_FALSE, is_vdev, cb); 6055 if (!ret) { 6056 /* No match */ 6057 break; 6058 } 6059 } 6060 6061 cb->cb_names = tmp_name; 6062 6063 return (ret); 6064 } 6065 6066 static int 6067 is_pool_cb(zpool_handle_t *zhp, void *data) 6068 { 6069 char *name = data; 6070 if (strcmp(name, zpool_get_name(zhp)) == 0) 6071 return (1); 6072 6073 return (0); 6074 } 6075 6076 /* 6077 * Do we have a pool named *name? If so, return 1, otherwise 0. 6078 */ 6079 static int 6080 is_pool(char *name) 6081 { 6082 return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE, 6083 is_pool_cb, name)); 6084 } 6085 6086 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ 6087 static int 6088 are_all_pools(int argc, char **argv) 6089 { 6090 if ((argc == 0) || !*argv) 6091 return (0); 6092 6093 while (--argc >= 0) 6094 if (!is_pool(argv[argc])) 6095 return (0); 6096 6097 return (1); 6098 } 6099 6100 /* 6101 * Helper function to print out vdev/pool names we can't resolve. Used for an 6102 * error message. 6103 */ 6104 static void 6105 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, 6106 vdev_cbdata_t *cb) 6107 { 6108 int i; 6109 char *name; 6110 char *str; 6111 for (i = 0; i < argc; i++) { 6112 name = argv[i]; 6113 6114 if (is_pool(name)) 6115 str = gettext("pool"); 6116 else if (are_vdevs_in_pool(1, &name, pool_name, cb)) 6117 str = gettext("vdev in this pool"); 6118 else if (are_vdevs_in_pool(1, &name, NULL, cb)) 6119 str = gettext("vdev in another pool"); 6120 else 6121 str = gettext("unknown"); 6122 6123 fprintf(stderr, "\t%s (%s)\n", name, str); 6124 } 6125 } 6126 6127 /* 6128 * Same as get_interval_count(), but with additional checks to not misinterpret 6129 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in 6130 * cb.cb_vdevs.cb_name_flags. 6131 */ 6132 static void 6133 get_interval_count_filter_guids(int *argc, char **argv, float *interval, 6134 unsigned long *count, iostat_cbdata_t *cb) 6135 { 6136 int argc_for_interval = 0; 6137 6138 /* Is the last arg an interval value? Or a guid? */ 6139 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, 6140 &cb->cb_vdevs)) { 6141 /* 6142 * The last arg is not a guid, so it's probably an 6143 * interval value. 6144 */ 6145 argc_for_interval++; 6146 6147 if (*argc >= 2 && 6148 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, 6149 &cb->cb_vdevs)) { 6150 /* 6151 * The 2nd to last arg is not a guid, so it's probably 6152 * an interval value. 6153 */ 6154 argc_for_interval++; 6155 } 6156 } 6157 6158 /* Point to our list of possible intervals */ 6159 char **tmpargv = &argv[*argc - argc_for_interval]; 6160 6161 *argc = *argc - argc_for_interval; 6162 get_interval_count(&argc_for_interval, tmpargv, 6163 interval, count); 6164 } 6165 6166 /* 6167 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or 6168 * if we were unable to determine its size. 6169 */ 6170 static int 6171 terminal_height(void) 6172 { 6173 struct winsize win; 6174 6175 if (isatty(STDOUT_FILENO) == 0) 6176 return (-1); 6177 6178 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) 6179 return (win.ws_row); 6180 6181 return (-1); 6182 } 6183 6184 /* 6185 * Run one of the zpool status/iostat -c scripts with the help (-h) option and 6186 * print the result. 6187 * 6188 * name: Short name of the script ('iostat'). 6189 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); 6190 */ 6191 static void 6192 print_zpool_script_help(char *name, char *path) 6193 { 6194 char *argv[] = {path, (char *)"-h", NULL}; 6195 char **lines = NULL; 6196 int lines_cnt = 0; 6197 int rc; 6198 6199 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, 6200 &lines_cnt); 6201 if (rc != 0 || lines == NULL || lines_cnt <= 0) { 6202 if (lines != NULL) 6203 libzfs_free_str_array(lines, lines_cnt); 6204 return; 6205 } 6206 6207 for (int i = 0; i < lines_cnt; i++) 6208 if (!is_blank_str(lines[i])) 6209 printf(" %-14s %s\n", name, lines[i]); 6210 6211 libzfs_free_str_array(lines, lines_cnt); 6212 } 6213 6214 /* 6215 * Go though the zpool status/iostat -c scripts in the user's path, run their 6216 * help option (-h), and print out the results. 6217 */ 6218 static void 6219 print_zpool_dir_scripts(char *dirpath) 6220 { 6221 DIR *dir; 6222 struct dirent *ent; 6223 char fullpath[MAXPATHLEN]; 6224 struct stat dir_stat; 6225 6226 if ((dir = opendir(dirpath)) != NULL) { 6227 /* print all the files and directories within directory */ 6228 while ((ent = readdir(dir)) != NULL) { 6229 if (snprintf(fullpath, sizeof (fullpath), "%s/%s", 6230 dirpath, ent->d_name) >= sizeof (fullpath)) { 6231 (void) fprintf(stderr, 6232 gettext("internal error: " 6233 "ZPOOL_SCRIPTS_PATH too large.\n")); 6234 exit(1); 6235 } 6236 6237 /* Print the scripts */ 6238 if (stat(fullpath, &dir_stat) == 0) 6239 if (dir_stat.st_mode & S_IXUSR && 6240 S_ISREG(dir_stat.st_mode)) 6241 print_zpool_script_help(ent->d_name, 6242 fullpath); 6243 } 6244 closedir(dir); 6245 } 6246 } 6247 6248 /* 6249 * Print out help text for all zpool status/iostat -c scripts. 6250 */ 6251 static void 6252 print_zpool_script_list(const char *subcommand) 6253 { 6254 char *dir, *sp, *tmp; 6255 6256 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); 6257 6258 sp = zpool_get_cmd_search_path(); 6259 if (sp == NULL) 6260 return; 6261 6262 for (dir = strtok_r(sp, ":", &tmp); 6263 dir != NULL; 6264 dir = strtok_r(NULL, ":", &tmp)) 6265 print_zpool_dir_scripts(dir); 6266 6267 free(sp); 6268 } 6269 6270 /* 6271 * Set the minimum pool/vdev name column width. The width must be at least 10, 6272 * but may be as large as the column width - 42 so it still fits on one line. 6273 * NOTE: 42 is the width of the default capacity/operations/bandwidth output 6274 */ 6275 static int 6276 get_namewidth_iostat(zpool_handle_t *zhp, void *data) 6277 { 6278 iostat_cbdata_t *cb = data; 6279 int width, available_width; 6280 6281 /* 6282 * get_namewidth() returns the maximum width of any name in that column 6283 * for any pool/vdev/device line that will be output. 6284 */ 6285 width = get_namewidth(zhp, cb->cb_namewidth, 6286 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); 6287 6288 /* 6289 * The width we are calculating is the width of the header and also the 6290 * padding width for names that are less than maximum width. The stats 6291 * take up 42 characters, so the width available for names is: 6292 */ 6293 available_width = get_columns() - 42; 6294 6295 /* 6296 * If the maximum width fits on a screen, then great! Make everything 6297 * line up by justifying all lines to the same width. If that max 6298 * width is larger than what's available, the name plus stats won't fit 6299 * on one line, and justifying to that width would cause every line to 6300 * wrap on the screen. We only want lines with long names to wrap. 6301 * Limit the padding to what won't wrap. 6302 */ 6303 if (width > available_width) 6304 width = available_width; 6305 6306 /* 6307 * And regardless of whatever the screen width is (get_columns can 6308 * return 0 if the width is not known or less than 42 for a narrow 6309 * terminal) have the width be a minimum of 10. 6310 */ 6311 if (width < 10) 6312 width = 10; 6313 6314 /* Save the calculated width */ 6315 cb->cb_namewidth = width; 6316 6317 return (0); 6318 } 6319 6320 /* 6321 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] 6322 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] 6323 * [interval [count]] 6324 * 6325 * -c CMD For each vdev, run command CMD 6326 * -g Display guid for individual vdev name. 6327 * -L Follow links when resolving vdev path name. 6328 * -P Display full path for vdev name. 6329 * -v Display statistics for individual vdevs 6330 * -h Display help 6331 * -p Display values in parsable (exact) format. 6332 * -H Scripted mode. Don't display headers, and separate properties 6333 * by a single tab. 6334 * -l Display average latency 6335 * -q Display queue depths 6336 * -w Display latency histograms 6337 * -r Display request size histogram 6338 * -T Display a timestamp in date(1) or Unix format 6339 * -n Only print headers once 6340 * 6341 * This command can be tricky because we want to be able to deal with pool 6342 * creation/destruction as well as vdev configuration changes. The bulk of this 6343 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 6344 * on pool_list_refresh() to detect the addition and removal of pools. 6345 * Configuration changes are all handled within libzfs. 6346 */ 6347 int 6348 zpool_do_iostat(int argc, char **argv) 6349 { 6350 int c; 6351 int ret; 6352 float interval = 0; 6353 unsigned long count = 0; 6354 zpool_list_t *list; 6355 boolean_t verbose = B_FALSE; 6356 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; 6357 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; 6358 boolean_t omit_since_boot = B_FALSE; 6359 boolean_t guid = B_FALSE; 6360 boolean_t follow_links = B_FALSE; 6361 boolean_t full_name = B_FALSE; 6362 boolean_t headers_once = B_FALSE; 6363 iostat_cbdata_t cb = { 0 }; 6364 char *cmd = NULL; 6365 6366 /* Used for printing error message */ 6367 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', 6368 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; 6369 6370 uint64_t unsupported_flags; 6371 6372 /* check options */ 6373 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { 6374 switch (c) { 6375 case 'c': 6376 if (cmd != NULL) { 6377 fprintf(stderr, 6378 gettext("Can't set -c flag twice\n")); 6379 exit(1); 6380 } 6381 6382 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 6383 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 6384 fprintf(stderr, gettext( 6385 "Can't run -c, disabled by " 6386 "ZPOOL_SCRIPTS_ENABLED.\n")); 6387 exit(1); 6388 } 6389 6390 if ((getuid() <= 0 || geteuid() <= 0) && 6391 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 6392 fprintf(stderr, gettext( 6393 "Can't run -c with root privileges " 6394 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 6395 exit(1); 6396 } 6397 cmd = optarg; 6398 verbose = B_TRUE; 6399 break; 6400 case 'g': 6401 guid = B_TRUE; 6402 break; 6403 case 'L': 6404 follow_links = B_TRUE; 6405 break; 6406 case 'P': 6407 full_name = B_TRUE; 6408 break; 6409 case 'T': 6410 get_timestamp_arg(*optarg); 6411 break; 6412 case 'v': 6413 verbose = B_TRUE; 6414 break; 6415 case 'p': 6416 parsable = B_TRUE; 6417 break; 6418 case 'l': 6419 latency = B_TRUE; 6420 break; 6421 case 'q': 6422 queues = B_TRUE; 6423 break; 6424 case 'H': 6425 scripted = B_TRUE; 6426 break; 6427 case 'w': 6428 l_histo = B_TRUE; 6429 break; 6430 case 'r': 6431 rq_histo = B_TRUE; 6432 break; 6433 case 'y': 6434 omit_since_boot = B_TRUE; 6435 break; 6436 case 'n': 6437 headers_once = B_TRUE; 6438 break; 6439 case 'h': 6440 usage(B_FALSE); 6441 break; 6442 case '?': 6443 if (optopt == 'c') { 6444 print_zpool_script_list("iostat"); 6445 exit(0); 6446 } else { 6447 fprintf(stderr, 6448 gettext("invalid option '%c'\n"), optopt); 6449 } 6450 usage(B_FALSE); 6451 } 6452 } 6453 6454 argc -= optind; 6455 argv += optind; 6456 6457 cb.cb_literal = parsable; 6458 cb.cb_scripted = scripted; 6459 6460 if (guid) 6461 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID; 6462 if (follow_links) 6463 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 6464 if (full_name) 6465 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH; 6466 cb.cb_iteration = 0; 6467 cb.cb_namewidth = 0; 6468 cb.cb_verbose = verbose; 6469 6470 /* Get our interval and count values (if any) */ 6471 if (guid) { 6472 get_interval_count_filter_guids(&argc, argv, &interval, 6473 &count, &cb); 6474 } else { 6475 get_interval_count(&argc, argv, &interval, &count); 6476 } 6477 6478 if (argc == 0) { 6479 /* No args, so just print the defaults. */ 6480 } else if (are_all_pools(argc, argv)) { 6481 /* All the args are pool names */ 6482 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) { 6483 /* All the args are vdevs */ 6484 cb.cb_vdevs.cb_names = argv; 6485 cb.cb_vdevs.cb_names_count = argc; 6486 argc = 0; /* No pools to process */ 6487 } else if (are_all_pools(1, argv)) { 6488 /* The first arg is a pool name */ 6489 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], 6490 &cb.cb_vdevs)) { 6491 /* ...and the rest are vdev names */ 6492 cb.cb_vdevs.cb_names = argv + 1; 6493 cb.cb_vdevs.cb_names_count = argc - 1; 6494 argc = 1; /* One pool to process */ 6495 } else { 6496 fprintf(stderr, gettext("Expected either a list of ")); 6497 fprintf(stderr, gettext("pools, or list of vdevs in")); 6498 fprintf(stderr, " \"%s\", ", argv[0]); 6499 fprintf(stderr, gettext("but got:\n")); 6500 error_list_unresolved_vdevs(argc - 1, argv + 1, 6501 argv[0], &cb.cb_vdevs); 6502 fprintf(stderr, "\n"); 6503 usage(B_FALSE); 6504 return (1); 6505 } 6506 } else { 6507 /* 6508 * The args don't make sense. The first arg isn't a pool name, 6509 * nor are all the args vdevs. 6510 */ 6511 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); 6512 fprintf(stderr, "\n"); 6513 return (1); 6514 } 6515 6516 if (cb.cb_vdevs.cb_names_count != 0) { 6517 /* 6518 * If user specified vdevs, it implies verbose. 6519 */ 6520 cb.cb_verbose = B_TRUE; 6521 } 6522 6523 /* 6524 * Construct the list of all interesting pools. 6525 */ 6526 ret = 0; 6527 if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable, 6528 &ret)) == NULL) 6529 return (1); 6530 6531 if (pool_list_count(list) == 0 && argc != 0) { 6532 pool_list_free(list); 6533 return (1); 6534 } 6535 6536 if (pool_list_count(list) == 0 && interval == 0) { 6537 pool_list_free(list); 6538 (void) fprintf(stderr, gettext("no pools available\n")); 6539 return (1); 6540 } 6541 6542 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { 6543 pool_list_free(list); 6544 (void) fprintf(stderr, 6545 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); 6546 usage(B_FALSE); 6547 return (1); 6548 } 6549 6550 if (l_histo && rq_histo) { 6551 pool_list_free(list); 6552 (void) fprintf(stderr, 6553 gettext("Only one of [-r|-w] can be passed at a time\n")); 6554 usage(B_FALSE); 6555 return (1); 6556 } 6557 6558 /* 6559 * Enter the main iostat loop. 6560 */ 6561 cb.cb_list = list; 6562 6563 if (l_histo) { 6564 /* 6565 * Histograms tables look out of place when you try to display 6566 * them with the other stats, so make a rule that you can only 6567 * print histograms by themselves. 6568 */ 6569 cb.cb_flags = IOS_L_HISTO_M; 6570 } else if (rq_histo) { 6571 cb.cb_flags = IOS_RQ_HISTO_M; 6572 } else { 6573 cb.cb_flags = IOS_DEFAULT_M; 6574 if (latency) 6575 cb.cb_flags |= IOS_LATENCY_M; 6576 if (queues) 6577 cb.cb_flags |= IOS_QUEUES_M; 6578 } 6579 6580 /* 6581 * See if the module supports all the stats we want to display. 6582 */ 6583 unsupported_flags = cb.cb_flags & ~get_stat_flags(list); 6584 if (unsupported_flags) { 6585 uint64_t f; 6586 int idx; 6587 fprintf(stderr, 6588 gettext("The loaded zfs module doesn't support:")); 6589 6590 /* for each bit set in unsupported_flags */ 6591 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { 6592 idx = lowbit64(f) - 1; 6593 fprintf(stderr, " -%c", flag_to_arg[idx]); 6594 } 6595 6596 fprintf(stderr, ". Try running a newer module.\n"); 6597 pool_list_free(list); 6598 6599 return (1); 6600 } 6601 6602 int last_npools = 0; 6603 for (;;) { 6604 /* 6605 * Refresh all pools in list, adding or removing pools as 6606 * necessary. 6607 */ 6608 int npools = pool_list_refresh(list); 6609 if (npools == 0) { 6610 (void) fprintf(stderr, gettext("no pools available\n")); 6611 } else { 6612 /* 6613 * If the list of pools has changed since last time 6614 * around, reset the iteration count to force the 6615 * header to be redisplayed. 6616 */ 6617 if (last_npools != npools) 6618 cb.cb_iteration = 0; 6619 6620 /* 6621 * If this is the first iteration and -y was supplied 6622 * we skip any printing. 6623 */ 6624 boolean_t skip = (omit_since_boot && 6625 cb.cb_iteration == 0); 6626 6627 /* 6628 * Iterate over all pools to determine the maximum width 6629 * for the pool / device name column across all pools. 6630 */ 6631 cb.cb_namewidth = 0; 6632 (void) pool_list_iter(list, B_FALSE, 6633 get_namewidth_iostat, &cb); 6634 6635 if (timestamp_fmt != NODATE) 6636 print_timestamp(timestamp_fmt); 6637 6638 if (cmd != NULL && cb.cb_verbose && 6639 !(cb.cb_flags & IOS_ANYHISTO_M)) { 6640 cb.vcdl = all_pools_for_each_vdev_run(argc, 6641 argv, cmd, g_zfs, cb.cb_vdevs.cb_names, 6642 cb.cb_vdevs.cb_names_count, 6643 cb.cb_vdevs.cb_name_flags); 6644 } else { 6645 cb.vcdl = NULL; 6646 } 6647 6648 6649 /* 6650 * Check terminal size so we can print headers 6651 * even when terminal window has its height 6652 * changed. 6653 */ 6654 int winheight = terminal_height(); 6655 /* 6656 * Are we connected to TTY? If not, headers_once 6657 * should be true, to avoid breaking scripts. 6658 */ 6659 if (winheight < 0) 6660 headers_once = B_TRUE; 6661 6662 /* 6663 * If it's the first time and we're not skipping it, 6664 * or either skip or verbose mode, print the header. 6665 * 6666 * The histogram code explicitly prints its header on 6667 * every vdev, so skip this for histograms. 6668 */ 6669 if (((++cb.cb_iteration == 1 && !skip) || 6670 (skip != verbose) || 6671 (!headers_once && 6672 (cb.cb_iteration % winheight) == 0)) && 6673 (!(cb.cb_flags & IOS_ANYHISTO_M)) && 6674 !cb.cb_scripted) 6675 print_iostat_header(&cb); 6676 6677 if (skip) { 6678 (void) fflush(stdout); 6679 (void) fsleep(interval); 6680 last_npools = npools; 6681 continue; 6682 } 6683 6684 pool_list_iter(list, B_FALSE, print_iostat, &cb); 6685 6686 /* 6687 * If there's more than one pool, and we're not in 6688 * verbose mode (which prints a separator for us), 6689 * then print a separator. 6690 * 6691 * In addition, if we're printing specific vdevs then 6692 * we also want an ending separator. 6693 */ 6694 if (((npools > 1 && !verbose && 6695 !(cb.cb_flags & IOS_ANYHISTO_M)) || 6696 (!(cb.cb_flags & IOS_ANYHISTO_M) && 6697 cb.cb_vdevs.cb_names_count)) && 6698 !cb.cb_scripted) { 6699 print_iostat_separator(&cb); 6700 if (cb.vcdl != NULL) 6701 print_cmd_columns(cb.vcdl, 1); 6702 printf("\n"); 6703 } 6704 6705 if (cb.vcdl != NULL) 6706 free_vdev_cmd_data_list(cb.vcdl); 6707 6708 } 6709 6710 if (interval == 0) 6711 break; 6712 6713 if (count != 0 && --count == 0) 6714 break; 6715 6716 (void) fflush(stdout); 6717 (void) fsleep(interval); 6718 6719 last_npools = npools; 6720 } 6721 6722 pool_list_free(list); 6723 6724 return (ret); 6725 } 6726 6727 typedef struct list_cbdata { 6728 boolean_t cb_verbose; 6729 int cb_name_flags; 6730 int cb_namewidth; 6731 boolean_t cb_json; 6732 boolean_t cb_scripted; 6733 zprop_list_t *cb_proplist; 6734 boolean_t cb_literal; 6735 nvlist_t *cb_jsobj; 6736 boolean_t cb_json_as_int; 6737 boolean_t cb_json_pool_key_guid; 6738 } list_cbdata_t; 6739 6740 6741 /* 6742 * Given a list of columns to display, output appropriate headers for each one. 6743 */ 6744 static void 6745 print_header(list_cbdata_t *cb) 6746 { 6747 zprop_list_t *pl = cb->cb_proplist; 6748 char headerbuf[ZPOOL_MAXPROPLEN]; 6749 const char *header; 6750 boolean_t first = B_TRUE; 6751 boolean_t right_justify; 6752 size_t width = 0; 6753 6754 for (; pl != NULL; pl = pl->pl_next) { 6755 width = pl->pl_width; 6756 if (first && cb->cb_verbose) { 6757 /* 6758 * Reset the width to accommodate the verbose listing 6759 * of devices. 6760 */ 6761 width = cb->cb_namewidth; 6762 } 6763 6764 if (!first) 6765 (void) fputs(" ", stdout); 6766 else 6767 first = B_FALSE; 6768 6769 right_justify = B_FALSE; 6770 if (pl->pl_prop != ZPROP_USERPROP) { 6771 header = zpool_prop_column_name(pl->pl_prop); 6772 right_justify = zpool_prop_align_right(pl->pl_prop); 6773 } else { 6774 int i; 6775 6776 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 6777 headerbuf[i] = toupper(pl->pl_user_prop[i]); 6778 headerbuf[i] = '\0'; 6779 header = headerbuf; 6780 } 6781 6782 if (pl->pl_next == NULL && !right_justify) 6783 (void) fputs(header, stdout); 6784 else if (right_justify) 6785 (void) printf("%*s", (int)width, header); 6786 else 6787 (void) printf("%-*s", (int)width, header); 6788 } 6789 6790 (void) fputc('\n', stdout); 6791 } 6792 6793 /* 6794 * Given a pool and a list of properties, print out all the properties according 6795 * to the described layout. Used by zpool_do_list(). 6796 */ 6797 static void 6798 collect_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 6799 { 6800 zprop_list_t *pl = cb->cb_proplist; 6801 boolean_t first = B_TRUE; 6802 char property[ZPOOL_MAXPROPLEN]; 6803 const char *propstr; 6804 boolean_t right_justify; 6805 size_t width; 6806 zprop_source_t sourcetype = ZPROP_SRC_NONE; 6807 nvlist_t *item, *d, *props; 6808 item = d = props = NULL; 6809 6810 if (cb->cb_json) { 6811 item = fnvlist_alloc(); 6812 props = fnvlist_alloc(); 6813 d = fnvlist_lookup_nvlist(cb->cb_jsobj, "pools"); 6814 if (d == NULL) { 6815 fprintf(stderr, "pools obj not found.\n"); 6816 exit(1); 6817 } 6818 fill_pool_info(item, zhp, B_TRUE, cb->cb_json_as_int); 6819 } 6820 6821 for (; pl != NULL; pl = pl->pl_next) { 6822 6823 width = pl->pl_width; 6824 if (first && cb->cb_verbose) { 6825 /* 6826 * Reset the width to accommodate the verbose listing 6827 * of devices. 6828 */ 6829 width = cb->cb_namewidth; 6830 } 6831 6832 if (!cb->cb_json && !first) { 6833 if (cb->cb_scripted) 6834 (void) fputc('\t', stdout); 6835 else 6836 (void) fputs(" ", stdout); 6837 } else { 6838 first = B_FALSE; 6839 } 6840 6841 right_justify = B_FALSE; 6842 if (pl->pl_prop != ZPROP_USERPROP) { 6843 if (zpool_get_prop(zhp, pl->pl_prop, property, 6844 sizeof (property), &sourcetype, 6845 cb->cb_literal) != 0) 6846 propstr = "-"; 6847 else 6848 propstr = property; 6849 6850 right_justify = zpool_prop_align_right(pl->pl_prop); 6851 } else if ((zpool_prop_feature(pl->pl_user_prop) || 6852 zpool_prop_unsupported(pl->pl_user_prop)) && 6853 zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 6854 sizeof (property)) == 0) { 6855 propstr = property; 6856 sourcetype = ZPROP_SRC_LOCAL; 6857 } else if (zfs_prop_user(pl->pl_user_prop) && 6858 zpool_get_userprop(zhp, pl->pl_user_prop, property, 6859 sizeof (property), &sourcetype) == 0) { 6860 propstr = property; 6861 } else { 6862 propstr = "-"; 6863 } 6864 6865 if (cb->cb_json) { 6866 if (pl->pl_prop == ZPOOL_PROP_NAME) 6867 continue; 6868 const char *prop_name; 6869 if (pl->pl_prop != ZPROP_USERPROP) 6870 prop_name = zpool_prop_to_name(pl->pl_prop); 6871 else 6872 prop_name = pl->pl_user_prop; 6873 (void) zprop_nvlist_one_property( 6874 prop_name, propstr, 6875 sourcetype, NULL, NULL, props, cb->cb_json_as_int); 6876 } else { 6877 /* 6878 * If this is being called in scripted mode, or if this 6879 * is the last column and it is left-justified, don't 6880 * include a width format specifier. 6881 */ 6882 if (cb->cb_scripted || (pl->pl_next == NULL && 6883 !right_justify)) 6884 (void) fputs(propstr, stdout); 6885 else if (right_justify) 6886 (void) printf("%*s", (int)width, propstr); 6887 else 6888 (void) printf("%-*s", (int)width, propstr); 6889 } 6890 } 6891 6892 if (cb->cb_json) { 6893 fnvlist_add_nvlist(item, "properties", props); 6894 if (cb->cb_json_pool_key_guid) { 6895 char pool_guid[256]; 6896 uint64_t guid = fnvlist_lookup_uint64( 6897 zpool_get_config(zhp, NULL), 6898 ZPOOL_CONFIG_POOL_GUID); 6899 snprintf(pool_guid, 256, "%llu", 6900 (u_longlong_t)guid); 6901 fnvlist_add_nvlist(d, pool_guid, item); 6902 } else { 6903 fnvlist_add_nvlist(d, zpool_get_name(zhp), 6904 item); 6905 } 6906 fnvlist_free(props); 6907 fnvlist_free(item); 6908 } else 6909 (void) fputc('\n', stdout); 6910 } 6911 6912 static void 6913 collect_vdev_prop(zpool_prop_t prop, uint64_t value, const char *str, 6914 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format, 6915 boolean_t json, nvlist_t *nvl, boolean_t as_int) 6916 { 6917 char propval[64]; 6918 boolean_t fixed; 6919 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 6920 6921 switch (prop) { 6922 case ZPOOL_PROP_SIZE: 6923 case ZPOOL_PROP_EXPANDSZ: 6924 case ZPOOL_PROP_CHECKPOINT: 6925 case ZPOOL_PROP_DEDUPRATIO: 6926 case ZPOOL_PROP_DEDUPCACHED: 6927 if (value == 0) 6928 (void) strlcpy(propval, "-", sizeof (propval)); 6929 else 6930 zfs_nicenum_format(value, propval, sizeof (propval), 6931 format); 6932 break; 6933 case ZPOOL_PROP_FRAGMENTATION: 6934 if (value == ZFS_FRAG_INVALID) { 6935 (void) strlcpy(propval, "-", sizeof (propval)); 6936 } else if (format == ZFS_NICENUM_RAW) { 6937 (void) snprintf(propval, sizeof (propval), "%llu", 6938 (unsigned long long)value); 6939 } else { 6940 (void) snprintf(propval, sizeof (propval), "%llu%%", 6941 (unsigned long long)value); 6942 } 6943 break; 6944 case ZPOOL_PROP_CAPACITY: 6945 /* capacity value is in parts-per-10,000 (aka permyriad) */ 6946 if (format == ZFS_NICENUM_RAW) 6947 (void) snprintf(propval, sizeof (propval), "%llu", 6948 (unsigned long long)value / 100); 6949 else 6950 (void) snprintf(propval, sizeof (propval), 6951 value < 1000 ? "%1.2f%%" : value < 10000 ? 6952 "%2.1f%%" : "%3.0f%%", value / 100.0); 6953 break; 6954 case ZPOOL_PROP_HEALTH: 6955 width = 8; 6956 (void) strlcpy(propval, str, sizeof (propval)); 6957 break; 6958 default: 6959 zfs_nicenum_format(value, propval, sizeof (propval), format); 6960 } 6961 6962 if (!valid) 6963 (void) strlcpy(propval, "-", sizeof (propval)); 6964 6965 if (json) { 6966 zprop_nvlist_one_property(zpool_prop_to_name(prop), propval, 6967 ZPROP_SRC_NONE, NULL, NULL, nvl, as_int); 6968 } else { 6969 if (scripted) 6970 (void) printf("\t%s", propval); 6971 else 6972 (void) printf(" %*s", (int)width, propval); 6973 } 6974 } 6975 6976 /* 6977 * print static default line per vdev 6978 * not compatible with '-o' <proplist> option 6979 */ 6980 static void 6981 collect_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 6982 list_cbdata_t *cb, int depth, boolean_t isspare, nvlist_t *item) 6983 { 6984 nvlist_t **child; 6985 vdev_stat_t *vs; 6986 uint_t c, children = 0; 6987 char *vname; 6988 boolean_t scripted = cb->cb_scripted; 6989 uint64_t islog = B_FALSE; 6990 nvlist_t *props, *ent, *ch, *obj, *l2c, *sp; 6991 props = ent = ch = obj = sp = l2c = NULL; 6992 const char *dashes = "%-*s - - - - " 6993 "- - - - -\n"; 6994 6995 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 6996 (uint64_t **)&vs, &c) == 0); 6997 6998 if (name != NULL) { 6999 boolean_t toplevel = (vs->vs_space != 0); 7000 uint64_t cap; 7001 enum zfs_nicenum_format format; 7002 const char *state; 7003 7004 if (cb->cb_literal) 7005 format = ZFS_NICENUM_RAW; 7006 else 7007 format = ZFS_NICENUM_1024; 7008 7009 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) 7010 return; 7011 7012 if (cb->cb_json) { 7013 props = fnvlist_alloc(); 7014 ent = fnvlist_alloc(); 7015 fill_vdev_info(ent, zhp, (char *)name, B_FALSE, 7016 cb->cb_json_as_int); 7017 } else { 7018 if (scripted) 7019 (void) printf("\t%s", name); 7020 else if (strlen(name) + depth > cb->cb_namewidth) 7021 (void) printf("%*s%s", depth, "", name); 7022 else 7023 (void) printf("%*s%s%*s", depth, "", name, 7024 (int)(cb->cb_namewidth - strlen(name) - 7025 depth), ""); 7026 } 7027 7028 /* 7029 * Print the properties for the individual vdevs. Some 7030 * properties are only applicable to toplevel vdevs. The 7031 * 'toplevel' boolean value is passed to the print_one_column() 7032 * to indicate that the value is valid. 7033 */ 7034 if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace) { 7035 collect_vdev_prop(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL, 7036 scripted, B_TRUE, format, cb->cb_json, props, 7037 cb->cb_json_as_int); 7038 } else { 7039 collect_vdev_prop(ZPOOL_PROP_SIZE, vs->vs_space, NULL, 7040 scripted, toplevel, format, cb->cb_json, props, 7041 cb->cb_json_as_int); 7042 } 7043 collect_vdev_prop(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, 7044 scripted, toplevel, format, cb->cb_json, props, 7045 cb->cb_json_as_int); 7046 collect_vdev_prop(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, 7047 NULL, scripted, toplevel, format, cb->cb_json, props, 7048 cb->cb_json_as_int); 7049 collect_vdev_prop(ZPOOL_PROP_CHECKPOINT, 7050 vs->vs_checkpoint_space, NULL, scripted, toplevel, format, 7051 cb->cb_json, props, cb->cb_json_as_int); 7052 collect_vdev_prop(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, 7053 scripted, B_TRUE, format, cb->cb_json, props, 7054 cb->cb_json_as_int); 7055 collect_vdev_prop(ZPOOL_PROP_FRAGMENTATION, 7056 vs->vs_fragmentation, NULL, scripted, 7057 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), 7058 format, cb->cb_json, props, cb->cb_json_as_int); 7059 cap = (vs->vs_space == 0) ? 0 : 7060 (vs->vs_alloc * 10000 / vs->vs_space); 7061 collect_vdev_prop(ZPOOL_PROP_CAPACITY, cap, NULL, 7062 scripted, toplevel, format, cb->cb_json, props, 7063 cb->cb_json_as_int); 7064 collect_vdev_prop(ZPOOL_PROP_DEDUPRATIO, 0, NULL, 7065 scripted, toplevel, format, cb->cb_json, props, 7066 cb->cb_json_as_int); 7067 state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 7068 if (isspare) { 7069 if (vs->vs_aux == VDEV_AUX_SPARED) 7070 state = "INUSE"; 7071 else if (vs->vs_state == VDEV_STATE_HEALTHY) 7072 state = "AVAIL"; 7073 } 7074 collect_vdev_prop(ZPOOL_PROP_HEALTH, 0, state, scripted, 7075 B_TRUE, format, cb->cb_json, props, cb->cb_json_as_int); 7076 7077 if (cb->cb_json) { 7078 fnvlist_add_nvlist(ent, "properties", props); 7079 fnvlist_free(props); 7080 } else 7081 (void) fputc('\n', stdout); 7082 } 7083 7084 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 7085 &child, &children) != 0) { 7086 if (cb->cb_json) { 7087 fnvlist_add_nvlist(item, name, ent); 7088 fnvlist_free(ent); 7089 } 7090 return; 7091 } 7092 7093 if (cb->cb_json) { 7094 ch = fnvlist_alloc(); 7095 } 7096 7097 /* list the normal vdevs first */ 7098 for (c = 0; c < children; c++) { 7099 uint64_t ishole = B_FALSE; 7100 7101 if (nvlist_lookup_uint64(child[c], 7102 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 7103 continue; 7104 7105 if (nvlist_lookup_uint64(child[c], 7106 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) 7107 continue; 7108 7109 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 7110 continue; 7111 7112 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7113 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 7114 7115 if (name == NULL || cb->cb_json != B_TRUE) 7116 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7117 B_FALSE, item); 7118 else if (cb->cb_json) { 7119 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7120 B_FALSE, ch); 7121 } 7122 free(vname); 7123 } 7124 7125 if (cb->cb_json) { 7126 if (!nvlist_empty(ch)) 7127 fnvlist_add_nvlist(ent, "vdevs", ch); 7128 fnvlist_free(ch); 7129 } 7130 7131 /* list the classes: 'logs', 'dedup', and 'special' */ 7132 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { 7133 boolean_t printed = B_FALSE; 7134 if (cb->cb_json) 7135 obj = fnvlist_alloc(); 7136 for (c = 0; c < children; c++) { 7137 const char *bias = NULL; 7138 const char *type = NULL; 7139 7140 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 7141 &islog) == 0 && islog) { 7142 bias = VDEV_ALLOC_CLASS_LOGS; 7143 } else { 7144 (void) nvlist_lookup_string(child[c], 7145 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 7146 (void) nvlist_lookup_string(child[c], 7147 ZPOOL_CONFIG_TYPE, &type); 7148 } 7149 if (bias == NULL || strcmp(bias, class_name[n]) != 0) 7150 continue; 7151 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 7152 continue; 7153 7154 if (!printed && !cb->cb_json) { 7155 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7156 (void) printf(dashes, cb->cb_namewidth, 7157 class_name[n]); 7158 printed = B_TRUE; 7159 } 7160 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7161 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 7162 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7163 B_FALSE, obj); 7164 free(vname); 7165 } 7166 if (cb->cb_json) { 7167 if (!nvlist_empty(obj)) 7168 fnvlist_add_nvlist(item, class_name[n], obj); 7169 fnvlist_free(obj); 7170 } 7171 } 7172 7173 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 7174 &child, &children) == 0 && children > 0) { 7175 if (cb->cb_json) { 7176 l2c = fnvlist_alloc(); 7177 } else { 7178 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7179 (void) printf(dashes, cb->cb_namewidth, "cache"); 7180 } 7181 for (c = 0; c < children; c++) { 7182 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7183 cb->cb_name_flags); 7184 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7185 B_FALSE, l2c); 7186 free(vname); 7187 } 7188 if (cb->cb_json) { 7189 if (!nvlist_empty(l2c)) 7190 fnvlist_add_nvlist(item, "l2cache", l2c); 7191 fnvlist_free(l2c); 7192 } 7193 } 7194 7195 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, 7196 &children) == 0 && children > 0) { 7197 if (cb->cb_json) { 7198 sp = fnvlist_alloc(); 7199 } else { 7200 /* LINTED E_SEC_PRINTF_VAR_FMT */ 7201 (void) printf(dashes, cb->cb_namewidth, "spare"); 7202 } 7203 for (c = 0; c < children; c++) { 7204 vname = zpool_vdev_name(g_zfs, zhp, child[c], 7205 cb->cb_name_flags); 7206 collect_list_stats(zhp, vname, child[c], cb, depth + 2, 7207 B_TRUE, sp); 7208 free(vname); 7209 } 7210 if (cb->cb_json) { 7211 if (!nvlist_empty(sp)) 7212 fnvlist_add_nvlist(item, "spares", sp); 7213 fnvlist_free(sp); 7214 } 7215 } 7216 7217 if (name != NULL && cb->cb_json) { 7218 fnvlist_add_nvlist(item, name, ent); 7219 fnvlist_free(ent); 7220 } 7221 } 7222 7223 /* 7224 * Generic callback function to list a pool. 7225 */ 7226 static int 7227 list_callback(zpool_handle_t *zhp, void *data) 7228 { 7229 nvlist_t *p, *d, *nvdevs; 7230 uint64_t guid; 7231 char pool_guid[256]; 7232 const char *pool_name = zpool_get_name(zhp); 7233 list_cbdata_t *cbp = data; 7234 p = d = nvdevs = NULL; 7235 7236 collect_pool(zhp, cbp); 7237 7238 if (cbp->cb_verbose) { 7239 nvlist_t *config, *nvroot; 7240 config = zpool_get_config(zhp, NULL); 7241 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 7242 &nvroot) == 0); 7243 if (cbp->cb_json) { 7244 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, 7245 "pools"); 7246 if (cbp->cb_json_pool_key_guid) { 7247 guid = fnvlist_lookup_uint64(config, 7248 ZPOOL_CONFIG_POOL_GUID); 7249 snprintf(pool_guid, 256, "%llu", 7250 (u_longlong_t)guid); 7251 p = fnvlist_lookup_nvlist(d, pool_guid); 7252 } else { 7253 p = fnvlist_lookup_nvlist(d, pool_name); 7254 } 7255 nvdevs = fnvlist_alloc(); 7256 } 7257 collect_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE, nvdevs); 7258 if (cbp->cb_json) { 7259 fnvlist_add_nvlist(p, "vdevs", nvdevs); 7260 if (cbp->cb_json_pool_key_guid) 7261 fnvlist_add_nvlist(d, pool_guid, p); 7262 else 7263 fnvlist_add_nvlist(d, pool_name, p); 7264 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d); 7265 fnvlist_free(nvdevs); 7266 } 7267 } 7268 7269 return (0); 7270 } 7271 7272 /* 7273 * Set the minimum pool/vdev name column width. The width must be at least 9, 7274 * but may be as large as needed. 7275 */ 7276 static int 7277 get_namewidth_list(zpool_handle_t *zhp, void *data) 7278 { 7279 list_cbdata_t *cb = data; 7280 int width; 7281 7282 width = get_namewidth(zhp, cb->cb_namewidth, 7283 cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); 7284 7285 if (width < 9) 7286 width = 9; 7287 7288 cb->cb_namewidth = width; 7289 7290 return (0); 7291 } 7292 7293 /* 7294 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 7295 * 7296 * -g Display guid for individual vdev name. 7297 * -H Scripted mode. Don't display headers, and separate properties 7298 * by a single tab. 7299 * -L Follow links when resolving vdev path name. 7300 * -o List of properties to display. Defaults to 7301 * "name,size,allocated,free,expandsize,fragmentation,capacity," 7302 * "dedupratio,health,altroot" 7303 * -p Display values in parsable (exact) format. 7304 * -P Display full path for vdev name. 7305 * -T Display a timestamp in date(1) or Unix format 7306 * -j Display the output in JSON format 7307 * --json-int Display the numbers as integer instead of strings. 7308 * --json-pool-key-guid Set pool GUID as key for pool objects. 7309 * 7310 * List all pools in the system, whether or not they're healthy. Output space 7311 * statistics for each one, as well as health status summary. 7312 */ 7313 int 7314 zpool_do_list(int argc, char **argv) 7315 { 7316 int c; 7317 int ret = 0; 7318 list_cbdata_t cb = { 0 }; 7319 static char default_props[] = 7320 "name,size,allocated,free,checkpoint,expandsize,fragmentation," 7321 "capacity,dedupratio,health,altroot"; 7322 char *props = default_props; 7323 float interval = 0; 7324 unsigned long count = 0; 7325 zpool_list_t *list; 7326 boolean_t first = B_TRUE; 7327 nvlist_t *data = NULL; 7328 current_prop_type = ZFS_TYPE_POOL; 7329 7330 struct option long_options[] = { 7331 {"json", no_argument, NULL, 'j'}, 7332 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 7333 {"json-pool-key-guid", no_argument, NULL, 7334 ZPOOL_OPTION_POOL_KEY_GUID}, 7335 {0, 0, 0, 0} 7336 }; 7337 7338 /* check options */ 7339 while ((c = getopt_long(argc, argv, ":gjHLo:pPT:v", long_options, 7340 NULL)) != -1) { 7341 switch (c) { 7342 case 'g': 7343 cb.cb_name_flags |= VDEV_NAME_GUID; 7344 break; 7345 case 'H': 7346 cb.cb_scripted = B_TRUE; 7347 break; 7348 case 'L': 7349 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 7350 break; 7351 case 'o': 7352 props = optarg; 7353 break; 7354 case 'P': 7355 cb.cb_name_flags |= VDEV_NAME_PATH; 7356 break; 7357 case 'p': 7358 cb.cb_literal = B_TRUE; 7359 break; 7360 case 'j': 7361 cb.cb_json = B_TRUE; 7362 break; 7363 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 7364 cb.cb_json_as_int = B_TRUE; 7365 cb.cb_literal = B_TRUE; 7366 break; 7367 case ZPOOL_OPTION_POOL_KEY_GUID: 7368 cb.cb_json_pool_key_guid = B_TRUE; 7369 break; 7370 case 'T': 7371 get_timestamp_arg(*optarg); 7372 break; 7373 case 'v': 7374 cb.cb_verbose = B_TRUE; 7375 cb.cb_namewidth = 8; /* 8 until precalc is avail */ 7376 break; 7377 case ':': 7378 (void) fprintf(stderr, gettext("missing argument for " 7379 "'%c' option\n"), optopt); 7380 usage(B_FALSE); 7381 break; 7382 case '?': 7383 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7384 optopt); 7385 usage(B_FALSE); 7386 } 7387 } 7388 7389 argc -= optind; 7390 argv += optind; 7391 7392 if (!cb.cb_json && cb.cb_json_as_int) { 7393 (void) fprintf(stderr, gettext("'--json-int' only works with" 7394 " '-j' option\n")); 7395 usage(B_FALSE); 7396 } 7397 7398 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 7399 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 7400 " works with '-j' option\n")); 7401 usage(B_FALSE); 7402 } 7403 7404 get_interval_count(&argc, argv, &interval, &count); 7405 7406 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 7407 usage(B_FALSE); 7408 7409 for (;;) { 7410 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 7411 ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL) 7412 return (1); 7413 7414 if (pool_list_count(list) == 0) 7415 break; 7416 7417 if (cb.cb_json) { 7418 cb.cb_jsobj = zpool_json_schema(0, 1); 7419 data = fnvlist_alloc(); 7420 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 7421 fnvlist_free(data); 7422 } 7423 7424 cb.cb_namewidth = 0; 7425 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); 7426 7427 if (timestamp_fmt != NODATE) { 7428 if (cb.cb_json) { 7429 if (cb.cb_json_as_int) { 7430 fnvlist_add_uint64(cb.cb_jsobj, "time", 7431 time(NULL)); 7432 } else { 7433 char ts[128]; 7434 get_timestamp(timestamp_fmt, ts, 128); 7435 fnvlist_add_string(cb.cb_jsobj, "time", 7436 ts); 7437 } 7438 } else 7439 print_timestamp(timestamp_fmt); 7440 } 7441 7442 if (!cb.cb_scripted && (first || cb.cb_verbose) && 7443 !cb.cb_json) { 7444 print_header(&cb); 7445 first = B_FALSE; 7446 } 7447 ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 7448 7449 if (ret == 0 && cb.cb_json) 7450 zcmd_print_json(cb.cb_jsobj); 7451 else if (ret != 0 && cb.cb_json) 7452 nvlist_free(cb.cb_jsobj); 7453 7454 if (interval == 0) 7455 break; 7456 7457 if (count != 0 && --count == 0) 7458 break; 7459 7460 pool_list_free(list); 7461 7462 (void) fflush(stdout); 7463 (void) fsleep(interval); 7464 } 7465 7466 if (argc == 0 && !cb.cb_scripted && !cb.cb_json && 7467 pool_list_count(list) == 0) { 7468 (void) printf(gettext("no pools available\n")); 7469 ret = 0; 7470 } 7471 7472 pool_list_free(list); 7473 zprop_free_list(cb.cb_proplist); 7474 return (ret); 7475 } 7476 7477 static int 7478 zpool_do_attach_or_replace(int argc, char **argv, int replacing) 7479 { 7480 boolean_t force = B_FALSE; 7481 boolean_t rebuild = B_FALSE; 7482 boolean_t wait = B_FALSE; 7483 int c; 7484 nvlist_t *nvroot; 7485 char *poolname, *old_disk, *new_disk; 7486 zpool_handle_t *zhp; 7487 nvlist_t *props = NULL; 7488 char *propval; 7489 int ret; 7490 7491 /* check options */ 7492 while ((c = getopt(argc, argv, "fo:sw")) != -1) { 7493 switch (c) { 7494 case 'f': 7495 force = B_TRUE; 7496 break; 7497 case 'o': 7498 if ((propval = strchr(optarg, '=')) == NULL) { 7499 (void) fprintf(stderr, gettext("missing " 7500 "'=' for -o option\n")); 7501 usage(B_FALSE); 7502 } 7503 *propval = '\0'; 7504 propval++; 7505 7506 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || 7507 (add_prop_list(optarg, propval, &props, B_TRUE))) 7508 usage(B_FALSE); 7509 break; 7510 case 's': 7511 rebuild = B_TRUE; 7512 break; 7513 case 'w': 7514 wait = B_TRUE; 7515 break; 7516 case '?': 7517 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7518 optopt); 7519 usage(B_FALSE); 7520 } 7521 } 7522 7523 argc -= optind; 7524 argv += optind; 7525 7526 /* get pool name and check number of arguments */ 7527 if (argc < 1) { 7528 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7529 usage(B_FALSE); 7530 } 7531 7532 poolname = argv[0]; 7533 7534 if (argc < 2) { 7535 (void) fprintf(stderr, 7536 gettext("missing <device> specification\n")); 7537 usage(B_FALSE); 7538 } 7539 7540 old_disk = argv[1]; 7541 7542 if (argc < 3) { 7543 if (!replacing) { 7544 (void) fprintf(stderr, 7545 gettext("missing <new_device> specification\n")); 7546 usage(B_FALSE); 7547 } 7548 new_disk = old_disk; 7549 argc -= 1; 7550 argv += 1; 7551 } else { 7552 new_disk = argv[2]; 7553 argc -= 2; 7554 argv += 2; 7555 } 7556 7557 if (argc > 1) { 7558 (void) fprintf(stderr, gettext("too many arguments\n")); 7559 usage(B_FALSE); 7560 } 7561 7562 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7563 nvlist_free(props); 7564 return (1); 7565 } 7566 7567 if (zpool_get_config(zhp, NULL) == NULL) { 7568 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 7569 poolname); 7570 zpool_close(zhp); 7571 nvlist_free(props); 7572 return (1); 7573 } 7574 7575 /* unless manually specified use "ashift" pool property (if set) */ 7576 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { 7577 int intval; 7578 zprop_source_t src; 7579 char strval[ZPOOL_MAXPROPLEN]; 7580 7581 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); 7582 if (src != ZPROP_SRC_DEFAULT) { 7583 (void) sprintf(strval, "%" PRId32, intval); 7584 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, 7585 &props, B_TRUE) == 0); 7586 } 7587 } 7588 7589 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, 7590 argc, argv); 7591 if (nvroot == NULL) { 7592 zpool_close(zhp); 7593 nvlist_free(props); 7594 return (1); 7595 } 7596 7597 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, 7598 rebuild); 7599 7600 if (ret == 0 && wait) { 7601 zpool_wait_activity_t activity = ZPOOL_WAIT_RESILVER; 7602 char raidz_prefix[] = "raidz"; 7603 if (replacing) { 7604 activity = ZPOOL_WAIT_REPLACE; 7605 } else if (strncmp(old_disk, 7606 raidz_prefix, strlen(raidz_prefix)) == 0) { 7607 activity = ZPOOL_WAIT_RAIDZ_EXPAND; 7608 } 7609 ret = zpool_wait(zhp, activity); 7610 } 7611 7612 nvlist_free(props); 7613 nvlist_free(nvroot); 7614 zpool_close(zhp); 7615 7616 return (ret); 7617 } 7618 7619 /* 7620 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> 7621 * 7622 * -f Force attach, even if <new_device> appears to be in use. 7623 * -s Use sequential instead of healing reconstruction for resilver. 7624 * -o Set property=value. 7625 * -w Wait for replacing to complete before returning 7626 * 7627 * Replace <device> with <new_device>. 7628 */ 7629 int 7630 zpool_do_replace(int argc, char **argv) 7631 { 7632 return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 7633 } 7634 7635 /* 7636 * zpool attach [-fsw] [-o property=value] <pool> <vdev> <new_device> 7637 * 7638 * -f Force attach, even if <new_device> appears to be in use. 7639 * -s Use sequential instead of healing reconstruction for resilver. 7640 * -o Set property=value. 7641 * -w Wait for resilvering (mirror) or expansion (raidz) to complete 7642 * before returning. 7643 * 7644 * Attach <new_device> to a <vdev>, where the vdev can be of type 7645 * device, mirror or raidz. If <vdev> is not part of a mirror, then <vdev> will 7646 * be transformed into a mirror of <vdev> and <new_device>. When a mirror 7647 * is involved, <new_device> will begin life with a DTL of [0, now], and will 7648 * immediately begin to resilver itself. For the raidz case, a expansion will 7649 * commence and reflow the raidz data across all the disks including the 7650 * <new_device>. 7651 */ 7652 int 7653 zpool_do_attach(int argc, char **argv) 7654 { 7655 return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 7656 } 7657 7658 /* 7659 * zpool detach [-f] <pool> <device> 7660 * 7661 * -f Force detach of <device>, even if DTLs argue against it 7662 * (not supported yet) 7663 * 7664 * Detach a device from a mirror. The operation will be refused if <device> 7665 * is the last device in the mirror, or if the DTLs indicate that this device 7666 * has the only valid copy of some data. 7667 */ 7668 int 7669 zpool_do_detach(int argc, char **argv) 7670 { 7671 int c; 7672 char *poolname, *path; 7673 zpool_handle_t *zhp; 7674 int ret; 7675 7676 /* check options */ 7677 while ((c = getopt(argc, argv, "")) != -1) { 7678 switch (c) { 7679 case '?': 7680 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7681 optopt); 7682 usage(B_FALSE); 7683 } 7684 } 7685 7686 argc -= optind; 7687 argv += optind; 7688 7689 /* get pool name and check number of arguments */ 7690 if (argc < 1) { 7691 (void) fprintf(stderr, gettext("missing pool name argument\n")); 7692 usage(B_FALSE); 7693 } 7694 7695 if (argc < 2) { 7696 (void) fprintf(stderr, 7697 gettext("missing <device> specification\n")); 7698 usage(B_FALSE); 7699 } 7700 7701 poolname = argv[0]; 7702 path = argv[1]; 7703 7704 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 7705 return (1); 7706 7707 ret = zpool_vdev_detach(zhp, path); 7708 7709 zpool_close(zhp); 7710 7711 return (ret); 7712 } 7713 7714 /* 7715 * zpool split [-gLnP] [-o prop=val] ... 7716 * [-o mntopt] ... 7717 * [-R altroot] <pool> <newpool> [<device> ...] 7718 * 7719 * -g Display guid for individual vdev name. 7720 * -L Follow links when resolving vdev path name. 7721 * -n Do not split the pool, but display the resulting layout if 7722 * it were to be split. 7723 * -o Set property=value, or set mount options. 7724 * -P Display full path for vdev name. 7725 * -R Mount the split-off pool under an alternate root. 7726 * -l Load encryption keys while importing. 7727 * 7728 * Splits the named pool and gives it the new pool name. Devices to be split 7729 * off may be listed, provided that no more than one device is specified 7730 * per top-level vdev mirror. The newly split pool is left in an exported 7731 * state unless -R is specified. 7732 * 7733 * Restrictions: the top-level of the pool pool must only be made up of 7734 * mirrors; all devices in the pool must be healthy; no device may be 7735 * undergoing a resilvering operation. 7736 */ 7737 int 7738 zpool_do_split(int argc, char **argv) 7739 { 7740 char *srcpool, *newpool, *propval; 7741 char *mntopts = NULL; 7742 splitflags_t flags; 7743 int c, ret = 0; 7744 int ms_status = 0; 7745 boolean_t loadkeys = B_FALSE; 7746 zpool_handle_t *zhp; 7747 nvlist_t *config, *props = NULL; 7748 7749 flags.dryrun = B_FALSE; 7750 flags.import = B_FALSE; 7751 flags.name_flags = 0; 7752 7753 /* check options */ 7754 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { 7755 switch (c) { 7756 case 'g': 7757 flags.name_flags |= VDEV_NAME_GUID; 7758 break; 7759 case 'L': 7760 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; 7761 break; 7762 case 'R': 7763 flags.import = B_TRUE; 7764 if (add_prop_list( 7765 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 7766 &props, B_TRUE) != 0) { 7767 nvlist_free(props); 7768 usage(B_FALSE); 7769 } 7770 break; 7771 case 'l': 7772 loadkeys = B_TRUE; 7773 break; 7774 case 'n': 7775 flags.dryrun = B_TRUE; 7776 break; 7777 case 'o': 7778 if ((propval = strchr(optarg, '=')) != NULL) { 7779 *propval = '\0'; 7780 propval++; 7781 if (add_prop_list(optarg, propval, 7782 &props, B_TRUE) != 0) { 7783 nvlist_free(props); 7784 usage(B_FALSE); 7785 } 7786 } else { 7787 mntopts = optarg; 7788 } 7789 break; 7790 case 'P': 7791 flags.name_flags |= VDEV_NAME_PATH; 7792 break; 7793 case ':': 7794 (void) fprintf(stderr, gettext("missing argument for " 7795 "'%c' option\n"), optopt); 7796 usage(B_FALSE); 7797 break; 7798 case '?': 7799 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7800 optopt); 7801 usage(B_FALSE); 7802 break; 7803 } 7804 } 7805 7806 if (!flags.import && mntopts != NULL) { 7807 (void) fprintf(stderr, gettext("setting mntopts is only " 7808 "valid when importing the pool\n")); 7809 usage(B_FALSE); 7810 } 7811 7812 if (!flags.import && loadkeys) { 7813 (void) fprintf(stderr, gettext("loading keys is only " 7814 "valid when importing the pool\n")); 7815 usage(B_FALSE); 7816 } 7817 7818 argc -= optind; 7819 argv += optind; 7820 7821 if (argc < 1) { 7822 (void) fprintf(stderr, gettext("Missing pool name\n")); 7823 usage(B_FALSE); 7824 } 7825 if (argc < 2) { 7826 (void) fprintf(stderr, gettext("Missing new pool name\n")); 7827 usage(B_FALSE); 7828 } 7829 7830 srcpool = argv[0]; 7831 newpool = argv[1]; 7832 7833 argc -= 2; 7834 argv += 2; 7835 7836 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { 7837 nvlist_free(props); 7838 return (1); 7839 } 7840 7841 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 7842 if (config == NULL) { 7843 ret = 1; 7844 } else { 7845 if (flags.dryrun) { 7846 (void) printf(gettext("would create '%s' with the " 7847 "following layout:\n\n"), newpool); 7848 print_vdev_tree(NULL, newpool, config, 0, "", 7849 flags.name_flags); 7850 print_vdev_tree(NULL, "dedup", config, 0, 7851 VDEV_ALLOC_BIAS_DEDUP, 0); 7852 print_vdev_tree(NULL, "special", config, 0, 7853 VDEV_ALLOC_BIAS_SPECIAL, 0); 7854 } 7855 } 7856 7857 zpool_close(zhp); 7858 7859 if (ret != 0 || flags.dryrun || !flags.import) { 7860 nvlist_free(config); 7861 nvlist_free(props); 7862 return (ret); 7863 } 7864 7865 /* 7866 * The split was successful. Now we need to open the new 7867 * pool and import it. 7868 */ 7869 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { 7870 nvlist_free(config); 7871 nvlist_free(props); 7872 return (1); 7873 } 7874 7875 if (loadkeys) { 7876 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); 7877 if (ret != 0) 7878 ret = 1; 7879 } 7880 7881 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) { 7882 ms_status = zpool_enable_datasets(zhp, mntopts, 0, 7883 mount_tp_nthr); 7884 if (ms_status == EZFS_SHAREFAILED) { 7885 (void) fprintf(stderr, gettext("Split was successful, " 7886 "datasets are mounted but sharing of some datasets " 7887 "has failed\n")); 7888 } else if (ms_status == EZFS_MOUNTFAILED) { 7889 (void) fprintf(stderr, gettext("Split was successful" 7890 ", but some datasets could not be mounted\n")); 7891 (void) fprintf(stderr, gettext("Try doing '%s' with a " 7892 "different altroot\n"), "zpool import"); 7893 } 7894 } 7895 zpool_close(zhp); 7896 nvlist_free(config); 7897 nvlist_free(props); 7898 7899 return (ret); 7900 } 7901 7902 7903 /* 7904 * zpool online [--power] <pool> <device> ... 7905 * 7906 * --power: Power on the enclosure slot to the drive (if possible) 7907 */ 7908 int 7909 zpool_do_online(int argc, char **argv) 7910 { 7911 int c, i; 7912 char *poolname; 7913 zpool_handle_t *zhp; 7914 int ret = 0; 7915 vdev_state_t newstate; 7916 int flags = 0; 7917 boolean_t is_power_on = B_FALSE; 7918 struct option long_options[] = { 7919 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 7920 {0, 0, 0, 0} 7921 }; 7922 7923 /* check options */ 7924 while ((c = getopt_long(argc, argv, "e", long_options, NULL)) != -1) { 7925 switch (c) { 7926 case 'e': 7927 flags |= ZFS_ONLINE_EXPAND; 7928 break; 7929 case ZPOOL_OPTION_POWER: 7930 is_power_on = B_TRUE; 7931 break; 7932 case '?': 7933 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7934 optopt); 7935 usage(B_FALSE); 7936 } 7937 } 7938 7939 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) 7940 is_power_on = B_TRUE; 7941 7942 argc -= optind; 7943 argv += optind; 7944 7945 /* get pool name and check number of arguments */ 7946 if (argc < 1) { 7947 (void) fprintf(stderr, gettext("missing pool name\n")); 7948 usage(B_FALSE); 7949 } 7950 if (argc < 2) { 7951 (void) fprintf(stderr, gettext("missing device name\n")); 7952 usage(B_FALSE); 7953 } 7954 7955 poolname = argv[0]; 7956 7957 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7958 (void) fprintf(stderr, gettext("failed to open pool " 7959 "\"%s\""), poolname); 7960 return (1); 7961 } 7962 7963 for (i = 1; i < argc; i++) { 7964 vdev_state_t oldstate; 7965 boolean_t avail_spare, l2cache; 7966 int rc; 7967 7968 if (is_power_on) { 7969 rc = zpool_power_on_and_disk_wait(zhp, argv[i]); 7970 if (rc == ENOTSUP) { 7971 (void) fprintf(stderr, 7972 gettext("Power control not supported\n")); 7973 } 7974 if (rc != 0) 7975 return (rc); 7976 } 7977 7978 nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare, 7979 &l2cache, NULL); 7980 if (tgt == NULL) { 7981 ret = 1; 7982 (void) fprintf(stderr, gettext("couldn't find device " 7983 "\"%s\" in pool \"%s\"\n"), argv[i], poolname); 7984 continue; 7985 } 7986 uint_t vsc; 7987 oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt, 7988 ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state; 7989 if ((rc = zpool_vdev_online(zhp, argv[i], flags, 7990 &newstate)) == 0) { 7991 if (newstate != VDEV_STATE_HEALTHY) { 7992 (void) printf(gettext("warning: device '%s' " 7993 "onlined, but remains in faulted state\n"), 7994 argv[i]); 7995 if (newstate == VDEV_STATE_FAULTED) 7996 (void) printf(gettext("use 'zpool " 7997 "clear' to restore a faulted " 7998 "device\n")); 7999 else 8000 (void) printf(gettext("use 'zpool " 8001 "replace' to replace devices " 8002 "that are no longer present\n")); 8003 if ((flags & ZFS_ONLINE_EXPAND)) { 8004 (void) printf(gettext("%s: failed " 8005 "to expand usable space on " 8006 "unhealthy device '%s'\n"), 8007 (oldstate >= VDEV_STATE_DEGRADED ? 8008 "error" : "warning"), argv[i]); 8009 if (oldstate >= VDEV_STATE_DEGRADED) { 8010 ret = 1; 8011 break; 8012 } 8013 } 8014 } 8015 } else { 8016 (void) fprintf(stderr, gettext("Failed to online " 8017 "\"%s\" in pool \"%s\": %d\n"), 8018 argv[i], poolname, rc); 8019 ret = 1; 8020 } 8021 } 8022 8023 zpool_close(zhp); 8024 8025 return (ret); 8026 } 8027 8028 /* 8029 * zpool offline [-ft]|[--power] <pool> <device> ... 8030 * 8031 * 8032 * -f Force the device into a faulted state. 8033 * 8034 * -t Only take the device off-line temporarily. The offline/faulted 8035 * state will not be persistent across reboots. 8036 * 8037 * --power Power off the enclosure slot to the drive (if possible) 8038 */ 8039 int 8040 zpool_do_offline(int argc, char **argv) 8041 { 8042 int c, i; 8043 char *poolname; 8044 zpool_handle_t *zhp; 8045 int ret = 0; 8046 boolean_t istmp = B_FALSE; 8047 boolean_t fault = B_FALSE; 8048 boolean_t is_power_off = B_FALSE; 8049 8050 struct option long_options[] = { 8051 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 8052 {0, 0, 0, 0} 8053 }; 8054 8055 /* check options */ 8056 while ((c = getopt_long(argc, argv, "ft", long_options, NULL)) != -1) { 8057 switch (c) { 8058 case 'f': 8059 fault = B_TRUE; 8060 break; 8061 case 't': 8062 istmp = B_TRUE; 8063 break; 8064 case ZPOOL_OPTION_POWER: 8065 is_power_off = B_TRUE; 8066 break; 8067 case '?': 8068 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8069 optopt); 8070 usage(B_FALSE); 8071 } 8072 } 8073 8074 if (is_power_off && fault) { 8075 (void) fprintf(stderr, 8076 gettext("-0 and -f cannot be used together\n")); 8077 usage(B_FALSE); 8078 return (1); 8079 } 8080 8081 if (is_power_off && istmp) { 8082 (void) fprintf(stderr, 8083 gettext("-0 and -t cannot be used together\n")); 8084 usage(B_FALSE); 8085 return (1); 8086 } 8087 8088 argc -= optind; 8089 argv += optind; 8090 8091 /* get pool name and check number of arguments */ 8092 if (argc < 1) { 8093 (void) fprintf(stderr, gettext("missing pool name\n")); 8094 usage(B_FALSE); 8095 } 8096 if (argc < 2) { 8097 (void) fprintf(stderr, gettext("missing device name\n")); 8098 usage(B_FALSE); 8099 } 8100 8101 poolname = argv[0]; 8102 8103 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 8104 (void) fprintf(stderr, gettext("failed to open pool " 8105 "\"%s\""), poolname); 8106 return (1); 8107 } 8108 8109 for (i = 1; i < argc; i++) { 8110 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); 8111 if (is_power_off) { 8112 /* 8113 * Note: we have to power off first, then set REMOVED, 8114 * or else zpool_vdev_set_removed_state() returns 8115 * EAGAIN. 8116 */ 8117 ret = zpool_power_off(zhp, argv[i]); 8118 if (ret != 0) { 8119 (void) fprintf(stderr, "%s %s %d\n", 8120 gettext("unable to power off slot for"), 8121 argv[i], ret); 8122 } 8123 zpool_vdev_set_removed_state(zhp, guid, VDEV_AUX_NONE); 8124 8125 } else if (fault) { 8126 vdev_aux_t aux; 8127 if (istmp == B_FALSE) { 8128 /* Force the fault to persist across imports */ 8129 aux = VDEV_AUX_EXTERNAL_PERSIST; 8130 } else { 8131 aux = VDEV_AUX_EXTERNAL; 8132 } 8133 8134 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) 8135 ret = 1; 8136 } else { 8137 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 8138 ret = 1; 8139 } 8140 } 8141 8142 zpool_close(zhp); 8143 8144 return (ret); 8145 } 8146 8147 /* 8148 * zpool clear [-nF]|[--power] <pool> [device] 8149 * 8150 * Clear all errors associated with a pool or a particular device. 8151 */ 8152 int 8153 zpool_do_clear(int argc, char **argv) 8154 { 8155 int c; 8156 int ret = 0; 8157 boolean_t dryrun = B_FALSE; 8158 boolean_t do_rewind = B_FALSE; 8159 boolean_t xtreme_rewind = B_FALSE; 8160 boolean_t is_power_on = B_FALSE; 8161 uint32_t rewind_policy = ZPOOL_NO_REWIND; 8162 nvlist_t *policy = NULL; 8163 zpool_handle_t *zhp; 8164 char *pool, *device; 8165 8166 struct option long_options[] = { 8167 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 8168 {0, 0, 0, 0} 8169 }; 8170 8171 /* check options */ 8172 while ((c = getopt_long(argc, argv, "FnX", long_options, 8173 NULL)) != -1) { 8174 switch (c) { 8175 case 'F': 8176 do_rewind = B_TRUE; 8177 break; 8178 case 'n': 8179 dryrun = B_TRUE; 8180 break; 8181 case 'X': 8182 xtreme_rewind = B_TRUE; 8183 break; 8184 case ZPOOL_OPTION_POWER: 8185 is_power_on = B_TRUE; 8186 break; 8187 case '?': 8188 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8189 optopt); 8190 usage(B_FALSE); 8191 } 8192 } 8193 8194 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) 8195 is_power_on = B_TRUE; 8196 8197 argc -= optind; 8198 argv += optind; 8199 8200 if (argc < 1) { 8201 (void) fprintf(stderr, gettext("missing pool name\n")); 8202 usage(B_FALSE); 8203 } 8204 8205 if (argc > 2) { 8206 (void) fprintf(stderr, gettext("too many arguments\n")); 8207 usage(B_FALSE); 8208 } 8209 8210 if ((dryrun || xtreme_rewind) && !do_rewind) { 8211 (void) fprintf(stderr, 8212 gettext("-n or -X only meaningful with -F\n")); 8213 usage(B_FALSE); 8214 } 8215 if (dryrun) 8216 rewind_policy = ZPOOL_TRY_REWIND; 8217 else if (do_rewind) 8218 rewind_policy = ZPOOL_DO_REWIND; 8219 if (xtreme_rewind) 8220 rewind_policy |= ZPOOL_EXTREME_REWIND; 8221 8222 /* In future, further rewind policy choices can be passed along here */ 8223 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 8224 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, 8225 rewind_policy) != 0) { 8226 return (1); 8227 } 8228 8229 pool = argv[0]; 8230 device = argc == 2 ? argv[1] : NULL; 8231 8232 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 8233 nvlist_free(policy); 8234 return (1); 8235 } 8236 8237 if (is_power_on) { 8238 if (device == NULL) { 8239 zpool_power_on_pool_and_wait_for_devices(zhp); 8240 } else { 8241 zpool_power_on_and_disk_wait(zhp, device); 8242 } 8243 } 8244 8245 if (zpool_clear(zhp, device, policy) != 0) 8246 ret = 1; 8247 8248 zpool_close(zhp); 8249 8250 nvlist_free(policy); 8251 8252 return (ret); 8253 } 8254 8255 /* 8256 * zpool reguid [-g <guid>] <pool> 8257 */ 8258 int 8259 zpool_do_reguid(int argc, char **argv) 8260 { 8261 uint64_t guid; 8262 uint64_t *guidp = NULL; 8263 int c; 8264 char *endptr; 8265 char *poolname; 8266 zpool_handle_t *zhp; 8267 int ret = 0; 8268 8269 /* check options */ 8270 while ((c = getopt(argc, argv, "g:")) != -1) { 8271 switch (c) { 8272 case 'g': 8273 errno = 0; 8274 guid = strtoull(optarg, &endptr, 10); 8275 if (errno != 0 || *endptr != '\0') { 8276 (void) fprintf(stderr, 8277 gettext("invalid GUID: %s\n"), optarg); 8278 usage(B_FALSE); 8279 } 8280 guidp = &guid; 8281 break; 8282 case '?': 8283 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8284 optopt); 8285 usage(B_FALSE); 8286 } 8287 } 8288 8289 argc -= optind; 8290 argv += optind; 8291 8292 /* get pool name and check number of arguments */ 8293 if (argc < 1) { 8294 (void) fprintf(stderr, gettext("missing pool name\n")); 8295 usage(B_FALSE); 8296 } 8297 8298 if (argc > 1) { 8299 (void) fprintf(stderr, gettext("too many arguments\n")); 8300 usage(B_FALSE); 8301 } 8302 8303 poolname = argv[0]; 8304 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 8305 return (1); 8306 8307 ret = zpool_set_guid(zhp, guidp); 8308 8309 zpool_close(zhp); 8310 return (ret); 8311 } 8312 8313 8314 /* 8315 * zpool reopen <pool> 8316 * 8317 * Reopen the pool so that the kernel can update the sizes of all vdevs. 8318 */ 8319 int 8320 zpool_do_reopen(int argc, char **argv) 8321 { 8322 int c; 8323 int ret = 0; 8324 boolean_t scrub_restart = B_TRUE; 8325 8326 /* check options */ 8327 while ((c = getopt(argc, argv, "n")) != -1) { 8328 switch (c) { 8329 case 'n': 8330 scrub_restart = B_FALSE; 8331 break; 8332 case '?': 8333 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8334 optopt); 8335 usage(B_FALSE); 8336 } 8337 } 8338 8339 argc -= optind; 8340 argv += optind; 8341 8342 /* if argc == 0 we will execute zpool_reopen_one on all pools */ 8343 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8344 B_FALSE, zpool_reopen_one, &scrub_restart); 8345 8346 return (ret); 8347 } 8348 8349 typedef struct scrub_cbdata { 8350 int cb_type; 8351 pool_scrub_cmd_t cb_scrub_cmd; 8352 time_t cb_date_start; 8353 time_t cb_date_end; 8354 } scrub_cbdata_t; 8355 8356 static boolean_t 8357 zpool_has_checkpoint(zpool_handle_t *zhp) 8358 { 8359 nvlist_t *config, *nvroot; 8360 8361 config = zpool_get_config(zhp, NULL); 8362 8363 if (config != NULL) { 8364 pool_checkpoint_stat_t *pcs = NULL; 8365 uint_t c; 8366 8367 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 8368 (void) nvlist_lookup_uint64_array(nvroot, 8369 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 8370 8371 if (pcs == NULL || pcs->pcs_state == CS_NONE) 8372 return (B_FALSE); 8373 8374 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || 8375 pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 8376 return (B_TRUE); 8377 } 8378 8379 return (B_FALSE); 8380 } 8381 8382 static int 8383 scrub_callback(zpool_handle_t *zhp, void *data) 8384 { 8385 scrub_cbdata_t *cb = data; 8386 int err; 8387 8388 /* 8389 * Ignore faulted pools. 8390 */ 8391 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 8392 (void) fprintf(stderr, gettext("cannot scan '%s': pool is " 8393 "currently unavailable\n"), zpool_get_name(zhp)); 8394 return (1); 8395 } 8396 8397 err = zpool_scan_range(zhp, cb->cb_type, cb->cb_scrub_cmd, 8398 cb->cb_date_start, cb->cb_date_end); 8399 if (err == 0 && zpool_has_checkpoint(zhp) && 8400 cb->cb_type == POOL_SCAN_SCRUB) { 8401 (void) printf(gettext("warning: will not scrub state that " 8402 "belongs to the checkpoint of pool '%s'\n"), 8403 zpool_get_name(zhp)); 8404 } 8405 8406 return (err != 0); 8407 } 8408 8409 static int 8410 wait_callback(zpool_handle_t *zhp, void *data) 8411 { 8412 zpool_wait_activity_t *act = data; 8413 return (zpool_wait(zhp, *act)); 8414 } 8415 8416 static time_t 8417 date_string_to_sec(const char *timestr, boolean_t rounding) 8418 { 8419 struct tm tm = {0}; 8420 int adjustment = rounding ? 1 : 0; 8421 8422 /* Allow mktime to determine timezone. */ 8423 tm.tm_isdst = -1; 8424 8425 if (strptime(timestr, "%Y-%m-%d %H:%M", &tm) == NULL) { 8426 if (strptime(timestr, "%Y-%m-%d", &tm) == NULL) { 8427 fprintf(stderr, gettext("Failed to parse the date.\n")); 8428 usage(B_FALSE); 8429 } 8430 adjustment *= 24 * 60 * 60; 8431 } else { 8432 adjustment *= 60; 8433 } 8434 8435 return (mktime(&tm) + adjustment); 8436 } 8437 8438 /* 8439 * zpool scrub [-e | -s | -p | -C | -E | -S] [-w] [-a | <pool> ...] 8440 * 8441 * -a Scrub all pools. 8442 * -e Only scrub blocks in the error log. 8443 * -E End date of scrub. 8444 * -S Start date of scrub. 8445 * -s Stop. Stops any in-progress scrub. 8446 * -p Pause. Pause in-progress scrub. 8447 * -w Wait. Blocks until scrub has completed. 8448 * -C Scrub from last saved txg. 8449 */ 8450 int 8451 zpool_do_scrub(int argc, char **argv) 8452 { 8453 int c; 8454 scrub_cbdata_t cb; 8455 boolean_t wait = B_FALSE; 8456 int error; 8457 8458 cb.cb_type = POOL_SCAN_SCRUB; 8459 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8460 cb.cb_date_start = cb.cb_date_end = 0; 8461 8462 boolean_t is_error_scrub = B_FALSE; 8463 boolean_t is_pause = B_FALSE; 8464 boolean_t is_stop = B_FALSE; 8465 boolean_t is_txg_continue = B_FALSE; 8466 boolean_t scrub_all = B_FALSE; 8467 8468 /* check options */ 8469 while ((c = getopt(argc, argv, "aspweCE:S:")) != -1) { 8470 switch (c) { 8471 case 'a': 8472 scrub_all = B_TRUE; 8473 break; 8474 case 'e': 8475 is_error_scrub = B_TRUE; 8476 break; 8477 case 'E': 8478 /* 8479 * Round the date. It's better to scrub more data than 8480 * less. This also makes the date inclusive. 8481 */ 8482 cb.cb_date_end = date_string_to_sec(optarg, B_TRUE); 8483 break; 8484 case 's': 8485 is_stop = B_TRUE; 8486 break; 8487 case 'S': 8488 cb.cb_date_start = date_string_to_sec(optarg, B_FALSE); 8489 break; 8490 case 'p': 8491 is_pause = B_TRUE; 8492 break; 8493 case 'w': 8494 wait = B_TRUE; 8495 break; 8496 case 'C': 8497 is_txg_continue = B_TRUE; 8498 break; 8499 case '?': 8500 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8501 optopt); 8502 usage(B_FALSE); 8503 } 8504 } 8505 8506 if (is_pause && is_stop) { 8507 (void) fprintf(stderr, gettext("invalid option " 8508 "combination: -s and -p are mutually exclusive\n")); 8509 usage(B_FALSE); 8510 } else if (is_pause && is_txg_continue) { 8511 (void) fprintf(stderr, gettext("invalid option " 8512 "combination: -p and -C are mutually exclusive\n")); 8513 usage(B_FALSE); 8514 } else if (is_stop && is_txg_continue) { 8515 (void) fprintf(stderr, gettext("invalid option " 8516 "combination: -s and -C are mutually exclusive\n")); 8517 usage(B_FALSE); 8518 } else if (is_error_scrub && is_txg_continue) { 8519 (void) fprintf(stderr, gettext("invalid option " 8520 "combination: -e and -C are mutually exclusive\n")); 8521 usage(B_FALSE); 8522 } else { 8523 if (is_error_scrub) 8524 cb.cb_type = POOL_SCAN_ERRORSCRUB; 8525 8526 if (is_pause) { 8527 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; 8528 } else if (is_stop) { 8529 cb.cb_type = POOL_SCAN_NONE; 8530 } else if (is_txg_continue) { 8531 cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG; 8532 } else { 8533 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8534 } 8535 } 8536 8537 if ((cb.cb_date_start != 0 || cb.cb_date_end != 0) && 8538 cb.cb_scrub_cmd != POOL_SCRUB_NORMAL) { 8539 (void) fprintf(stderr, gettext("invalid option combination: " 8540 "start/end date is available only with normal scrub\n")); 8541 usage(B_FALSE); 8542 } 8543 if (cb.cb_date_start != 0 && cb.cb_date_end != 0 && 8544 cb.cb_date_start > cb.cb_date_end) { 8545 (void) fprintf(stderr, gettext("invalid arguments: " 8546 "end date has to be later than start date\n")); 8547 usage(B_FALSE); 8548 } 8549 8550 if (wait && (cb.cb_type == POOL_SCAN_NONE || 8551 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { 8552 (void) fprintf(stderr, gettext("invalid option combination: " 8553 "-w cannot be used with -p or -s\n")); 8554 usage(B_FALSE); 8555 } 8556 8557 argc -= optind; 8558 argv += optind; 8559 8560 if (argc < 1 && !scrub_all) { 8561 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8562 usage(B_FALSE); 8563 } 8564 8565 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8566 B_FALSE, scrub_callback, &cb); 8567 8568 if (wait && !error) { 8569 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; 8570 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8571 B_FALSE, wait_callback, &act); 8572 } 8573 8574 return (error); 8575 } 8576 8577 /* 8578 * zpool resilver <pool> ... 8579 * 8580 * Restarts any in-progress resilver 8581 */ 8582 int 8583 zpool_do_resilver(int argc, char **argv) 8584 { 8585 int c; 8586 scrub_cbdata_t cb; 8587 8588 cb.cb_type = POOL_SCAN_RESILVER; 8589 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; 8590 cb.cb_date_start = cb.cb_date_end = 0; 8591 8592 /* check options */ 8593 while ((c = getopt(argc, argv, "")) != -1) { 8594 switch (c) { 8595 case '?': 8596 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8597 optopt); 8598 usage(B_FALSE); 8599 } 8600 } 8601 8602 argc -= optind; 8603 argv += optind; 8604 8605 if (argc < 1) { 8606 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8607 usage(B_FALSE); 8608 } 8609 8610 return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8611 B_FALSE, scrub_callback, &cb)); 8612 } 8613 8614 /* 8615 * zpool trim [-d] [-r <rate>] [-c | -s] <-a | pool> [<device> ...] 8616 * 8617 * -a Trim all pools. 8618 * -c Cancel. Ends any in-progress trim. 8619 * -d Secure trim. Requires kernel and device support. 8620 * -r <rate> Sets the TRIM rate in bytes (per second). Supports 8621 * adding a multiplier suffix such as 'k' or 'm'. 8622 * -s Suspend. TRIM can then be restarted with no flags. 8623 * -w Wait. Blocks until trimming has completed. 8624 */ 8625 int 8626 zpool_do_trim(int argc, char **argv) 8627 { 8628 struct option long_options[] = { 8629 {"cancel", no_argument, NULL, 'c'}, 8630 {"secure", no_argument, NULL, 'd'}, 8631 {"rate", required_argument, NULL, 'r'}, 8632 {"suspend", no_argument, NULL, 's'}, 8633 {"wait", no_argument, NULL, 'w'}, 8634 {"all", no_argument, NULL, 'a'}, 8635 {0, 0, 0, 0} 8636 }; 8637 8638 pool_trim_func_t cmd_type = POOL_TRIM_START; 8639 uint64_t rate = 0; 8640 boolean_t secure = B_FALSE; 8641 boolean_t wait = B_FALSE; 8642 boolean_t trimall = B_FALSE; 8643 int error; 8644 8645 int c; 8646 while ((c = getopt_long(argc, argv, "acdr:sw", long_options, NULL)) 8647 != -1) { 8648 switch (c) { 8649 case 'a': 8650 trimall = B_TRUE; 8651 break; 8652 case 'c': 8653 if (cmd_type != POOL_TRIM_START && 8654 cmd_type != POOL_TRIM_CANCEL) { 8655 (void) fprintf(stderr, gettext("-c cannot be " 8656 "combined with other options\n")); 8657 usage(B_FALSE); 8658 } 8659 cmd_type = POOL_TRIM_CANCEL; 8660 break; 8661 case 'd': 8662 if (cmd_type != POOL_TRIM_START) { 8663 (void) fprintf(stderr, gettext("-d cannot be " 8664 "combined with the -c or -s options\n")); 8665 usage(B_FALSE); 8666 } 8667 secure = B_TRUE; 8668 break; 8669 case 'r': 8670 if (cmd_type != POOL_TRIM_START) { 8671 (void) fprintf(stderr, gettext("-r cannot be " 8672 "combined with the -c or -s options\n")); 8673 usage(B_FALSE); 8674 } 8675 if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) { 8676 (void) fprintf(stderr, "%s: %s\n", 8677 gettext("invalid value for rate"), 8678 libzfs_error_description(g_zfs)); 8679 usage(B_FALSE); 8680 } 8681 break; 8682 case 's': 8683 if (cmd_type != POOL_TRIM_START && 8684 cmd_type != POOL_TRIM_SUSPEND) { 8685 (void) fprintf(stderr, gettext("-s cannot be " 8686 "combined with other options\n")); 8687 usage(B_FALSE); 8688 } 8689 cmd_type = POOL_TRIM_SUSPEND; 8690 break; 8691 case 'w': 8692 wait = B_TRUE; 8693 break; 8694 case '?': 8695 if (optopt != 0) { 8696 (void) fprintf(stderr, 8697 gettext("invalid option '%c'\n"), optopt); 8698 } else { 8699 (void) fprintf(stderr, 8700 gettext("invalid option '%s'\n"), 8701 argv[optind - 1]); 8702 } 8703 usage(B_FALSE); 8704 } 8705 } 8706 8707 argc -= optind; 8708 argv += optind; 8709 8710 trimflags_t trim_flags = { 8711 .secure = secure, 8712 .rate = rate, 8713 .wait = wait, 8714 }; 8715 8716 trim_cbdata_t cbdata = { 8717 .trim_flags = trim_flags, 8718 .cmd_type = cmd_type 8719 }; 8720 8721 if (argc < 1 && !trimall) { 8722 (void) fprintf(stderr, gettext("missing pool name argument\n")); 8723 usage(B_FALSE); 8724 return (-1); 8725 } 8726 8727 if (wait && (cmd_type != POOL_TRIM_START)) { 8728 (void) fprintf(stderr, gettext("-w cannot be used with -c or " 8729 "-s options\n")); 8730 usage(B_FALSE); 8731 } 8732 8733 if (trimall && argc > 0) { 8734 (void) fprintf(stderr, gettext("-a cannot be combined with " 8735 "individual zpools or vdevs\n")); 8736 usage(B_FALSE); 8737 } 8738 8739 if (argc == 0 && trimall) { 8740 cbdata.trim_flags.fullpool = B_TRUE; 8741 /* Trim each pool recursively */ 8742 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 8743 B_FALSE, zpool_trim_one, &cbdata); 8744 } else if (argc == 1) { 8745 char *poolname = argv[0]; 8746 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 8747 if (zhp == NULL) 8748 return (-1); 8749 /* no individual leaf vdevs specified, so add them all */ 8750 error = zpool_trim_one(zhp, &cbdata); 8751 zpool_close(zhp); 8752 } else { 8753 char *poolname = argv[0]; 8754 zpool_handle_t *zhp = zpool_open(g_zfs, poolname); 8755 if (zhp == NULL) 8756 return (-1); 8757 /* leaf vdevs specified, trim only those */ 8758 cbdata.trim_flags.fullpool = B_FALSE; 8759 nvlist_t *vdevs = fnvlist_alloc(); 8760 for (int i = 1; i < argc; i++) { 8761 fnvlist_add_boolean(vdevs, argv[i]); 8762 } 8763 error = zpool_trim(zhp, cbdata.cmd_type, vdevs, 8764 &cbdata.trim_flags); 8765 fnvlist_free(vdevs); 8766 zpool_close(zhp); 8767 } 8768 8769 return (error); 8770 } 8771 8772 /* 8773 * Converts a total number of seconds to a human readable string broken 8774 * down in to days/hours/minutes/seconds. 8775 */ 8776 static void 8777 secs_to_dhms(uint64_t total, char *buf) 8778 { 8779 uint64_t days = total / 60 / 60 / 24; 8780 uint64_t hours = (total / 60 / 60) % 24; 8781 uint64_t mins = (total / 60) % 60; 8782 uint64_t secs = (total % 60); 8783 8784 if (days > 0) { 8785 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", 8786 (u_longlong_t)days, (u_longlong_t)hours, 8787 (u_longlong_t)mins, (u_longlong_t)secs); 8788 } else { 8789 (void) sprintf(buf, "%02llu:%02llu:%02llu", 8790 (u_longlong_t)hours, (u_longlong_t)mins, 8791 (u_longlong_t)secs); 8792 } 8793 } 8794 8795 /* 8796 * Print out detailed error scrub status. 8797 */ 8798 static void 8799 print_err_scrub_status(pool_scan_stat_t *ps) 8800 { 8801 time_t start, end, pause; 8802 uint64_t total_secs_left; 8803 uint64_t secs_left, mins_left, hours_left, days_left; 8804 uint64_t examined, to_be_examined; 8805 8806 if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) { 8807 return; 8808 } 8809 8810 (void) printf(gettext(" scrub: ")); 8811 8812 start = ps->pss_error_scrub_start; 8813 end = ps->pss_error_scrub_end; 8814 pause = ps->pss_pass_error_scrub_pause; 8815 examined = ps->pss_error_scrub_examined; 8816 to_be_examined = ps->pss_error_scrub_to_be_examined; 8817 8818 assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB); 8819 8820 if (ps->pss_error_scrub_state == DSS_FINISHED) { 8821 total_secs_left = end - start; 8822 days_left = total_secs_left / 60 / 60 / 24; 8823 hours_left = (total_secs_left / 60 / 60) % 24; 8824 mins_left = (total_secs_left / 60) % 60; 8825 secs_left = (total_secs_left % 60); 8826 8827 (void) printf(gettext("scrubbed %llu error blocks in %llu days " 8828 "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined, 8829 (u_longlong_t)days_left, (u_longlong_t)hours_left, 8830 (u_longlong_t)mins_left, (u_longlong_t)secs_left, 8831 ctime(&end)); 8832 8833 return; 8834 } else if (ps->pss_error_scrub_state == DSS_CANCELED) { 8835 (void) printf(gettext("error scrub canceled on %s"), 8836 ctime(&end)); 8837 return; 8838 } 8839 assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING); 8840 8841 /* Error scrub is in progress. */ 8842 if (pause == 0) { 8843 (void) printf(gettext("error scrub in progress since %s"), 8844 ctime(&start)); 8845 } else { 8846 (void) printf(gettext("error scrub paused since %s"), 8847 ctime(&pause)); 8848 (void) printf(gettext("\terror scrub started on %s"), 8849 ctime(&start)); 8850 } 8851 8852 double fraction_done = (double)examined / (to_be_examined + examined); 8853 (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error" 8854 " blocks"), 100 * fraction_done, (u_longlong_t)examined); 8855 8856 (void) printf("\n"); 8857 } 8858 8859 /* 8860 * Print out detailed scrub status. 8861 */ 8862 static void 8863 print_scan_scrub_resilver_status(pool_scan_stat_t *ps) 8864 { 8865 time_t start, end, pause; 8866 uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i; 8867 uint64_t elapsed, scan_rate, issue_rate; 8868 double fraction_done; 8869 char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7]; 8870 char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32]; 8871 8872 printf(" "); 8873 printf_color(ANSI_BOLD, gettext("scan:")); 8874 printf(" "); 8875 8876 /* If there's never been a scan, there's not much to say. */ 8877 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 8878 ps->pss_func >= POOL_SCAN_FUNCS) { 8879 (void) printf(gettext("none requested\n")); 8880 return; 8881 } 8882 8883 start = ps->pss_start_time; 8884 end = ps->pss_end_time; 8885 pause = ps->pss_pass_scrub_pause; 8886 8887 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); 8888 8889 int is_resilver = ps->pss_func == POOL_SCAN_RESILVER; 8890 int is_scrub = ps->pss_func == POOL_SCAN_SCRUB; 8891 assert(is_resilver || is_scrub); 8892 8893 /* Scan is finished or canceled. */ 8894 if (ps->pss_state == DSS_FINISHED) { 8895 secs_to_dhms(end - start, time_buf); 8896 8897 if (is_scrub) { 8898 (void) printf(gettext("scrub repaired %s " 8899 "in %s with %llu errors on %s"), processed_buf, 8900 time_buf, (u_longlong_t)ps->pss_errors, 8901 ctime(&end)); 8902 } else if (is_resilver) { 8903 (void) printf(gettext("resilvered %s " 8904 "in %s with %llu errors on %s"), processed_buf, 8905 time_buf, (u_longlong_t)ps->pss_errors, 8906 ctime(&end)); 8907 } 8908 return; 8909 } else if (ps->pss_state == DSS_CANCELED) { 8910 if (is_scrub) { 8911 (void) printf(gettext("scrub canceled on %s"), 8912 ctime(&end)); 8913 } else if (is_resilver) { 8914 (void) printf(gettext("resilver canceled on %s"), 8915 ctime(&end)); 8916 } 8917 return; 8918 } 8919 8920 assert(ps->pss_state == DSS_SCANNING); 8921 8922 /* Scan is in progress. Resilvers can't be paused. */ 8923 if (is_scrub) { 8924 if (pause == 0) { 8925 (void) printf(gettext("scrub in progress since %s"), 8926 ctime(&start)); 8927 } else { 8928 (void) printf(gettext("scrub paused since %s"), 8929 ctime(&pause)); 8930 (void) printf(gettext("\tscrub started on %s"), 8931 ctime(&start)); 8932 } 8933 } else if (is_resilver) { 8934 (void) printf(gettext("resilver in progress since %s"), 8935 ctime(&start)); 8936 } 8937 8938 scanned = ps->pss_examined; 8939 pass_scanned = ps->pss_pass_exam; 8940 issued = ps->pss_issued; 8941 pass_issued = ps->pss_pass_issued; 8942 total_s = ps->pss_to_examine; 8943 total_i = ps->pss_to_examine - ps->pss_skipped; 8944 8945 /* we are only done with a block once we have issued the IO for it */ 8946 fraction_done = (double)issued / total_i; 8947 8948 /* elapsed time for this pass, rounding up to 1 if it's 0 */ 8949 elapsed = time(NULL) - ps->pss_pass_start; 8950 elapsed -= ps->pss_pass_scrub_spent_paused; 8951 elapsed = (elapsed != 0) ? elapsed : 1; 8952 8953 scan_rate = pass_scanned / elapsed; 8954 issue_rate = pass_issued / elapsed; 8955 8956 /* format all of the numbers we will be reporting */ 8957 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); 8958 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); 8959 zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf)); 8960 zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf)); 8961 8962 /* do not print estimated time if we have a paused scrub */ 8963 (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf); 8964 if (pause == 0 && scan_rate > 0) { 8965 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); 8966 (void) printf(gettext(" at %s/s"), srate_buf); 8967 } 8968 (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf); 8969 if (pause == 0 && issue_rate > 0) { 8970 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); 8971 (void) printf(gettext(" at %s/s"), irate_buf); 8972 } 8973 (void) printf(gettext("\n")); 8974 8975 if (is_resilver) { 8976 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 8977 processed_buf, 100 * fraction_done); 8978 } else if (is_scrub) { 8979 (void) printf(gettext("\t%s repaired, %.2f%% done"), 8980 processed_buf, 100 * fraction_done); 8981 } 8982 8983 if (pause == 0) { 8984 /* 8985 * Only provide an estimate iff: 8986 * 1) we haven't yet issued all we expected, and 8987 * 2) the issue rate exceeds 10 MB/s, and 8988 * 3) it's either: 8989 * a) a resilver which has started repairs, or 8990 * b) a scrub which has entered the issue phase. 8991 */ 8992 if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 && 8993 ((is_resilver && ps->pss_processed > 0) || 8994 (is_scrub && issued > 0))) { 8995 secs_to_dhms((total_i - issued) / issue_rate, time_buf); 8996 (void) printf(gettext(", %s to go\n"), time_buf); 8997 } else { 8998 (void) printf(gettext(", no estimated " 8999 "completion time\n")); 9000 } 9001 } else { 9002 (void) printf(gettext("\n")); 9003 } 9004 } 9005 9006 static void 9007 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name) 9008 { 9009 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) 9010 return; 9011 9012 printf(" "); 9013 printf_color(ANSI_BOLD, gettext("scan:")); 9014 printf(" "); 9015 9016 uint64_t bytes_scanned = vrs->vrs_bytes_scanned; 9017 uint64_t bytes_issued = vrs->vrs_bytes_issued; 9018 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; 9019 uint64_t bytes_est_s = vrs->vrs_bytes_est; 9020 uint64_t bytes_est_i = vrs->vrs_bytes_est; 9021 if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8) 9022 bytes_est_i -= vrs->vrs_pass_bytes_skipped; 9023 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / 9024 (vrs->vrs_pass_time_ms + 1)) * 1000; 9025 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / 9026 (vrs->vrs_pass_time_ms + 1)) * 1000; 9027 double scan_pct = MIN((double)bytes_scanned * 100 / 9028 (bytes_est_s + 1), 100); 9029 9030 /* Format all of the numbers we will be reporting */ 9031 char bytes_scanned_buf[7], bytes_issued_buf[7]; 9032 char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7]; 9033 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; 9034 zfs_nicebytes(bytes_scanned, bytes_scanned_buf, 9035 sizeof (bytes_scanned_buf)); 9036 zfs_nicebytes(bytes_issued, bytes_issued_buf, 9037 sizeof (bytes_issued_buf)); 9038 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, 9039 sizeof (bytes_rebuilt_buf)); 9040 zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf)); 9041 zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf)); 9042 9043 time_t start = vrs->vrs_start_time; 9044 time_t end = vrs->vrs_end_time; 9045 9046 /* Rebuild is finished or canceled. */ 9047 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { 9048 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); 9049 (void) printf(gettext("resilvered (%s) %s in %s " 9050 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, 9051 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); 9052 return; 9053 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { 9054 (void) printf(gettext("resilver (%s) canceled on %s"), 9055 vdev_name, ctime(&end)); 9056 return; 9057 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9058 (void) printf(gettext("resilver (%s) in progress since %s"), 9059 vdev_name, ctime(&start)); 9060 } 9061 9062 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); 9063 9064 (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf, 9065 bytes_est_s_buf); 9066 if (scan_rate > 0) { 9067 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); 9068 (void) printf(gettext(" at %s/s"), scan_rate_buf); 9069 } 9070 (void) printf(gettext(", %s / %s issued"), bytes_issued_buf, 9071 bytes_est_i_buf); 9072 if (issue_rate > 0) { 9073 zfs_nicebytes(issue_rate, issue_rate_buf, 9074 sizeof (issue_rate_buf)); 9075 (void) printf(gettext(" at %s/s"), issue_rate_buf); 9076 } 9077 (void) printf(gettext("\n")); 9078 9079 (void) printf(gettext("\t%s resilvered, %.2f%% done"), 9080 bytes_rebuilt_buf, scan_pct); 9081 9082 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9083 if (bytes_est_s >= bytes_scanned && 9084 scan_rate >= 10 * 1024 * 1024) { 9085 secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate, 9086 time_buf); 9087 (void) printf(gettext(", %s to go\n"), time_buf); 9088 } else { 9089 (void) printf(gettext(", no estimated " 9090 "completion time\n")); 9091 } 9092 } else { 9093 (void) printf(gettext("\n")); 9094 } 9095 } 9096 9097 /* 9098 * Print rebuild status for top-level vdevs. 9099 */ 9100 static void 9101 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) 9102 { 9103 nvlist_t **child; 9104 uint_t children; 9105 9106 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9107 &child, &children) != 0) 9108 children = 0; 9109 9110 for (uint_t c = 0; c < children; c++) { 9111 vdev_rebuild_stat_t *vrs; 9112 uint_t i; 9113 9114 if (nvlist_lookup_uint64_array(child[c], 9115 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 9116 char *name = zpool_vdev_name(g_zfs, zhp, 9117 child[c], VDEV_NAME_TYPE_ID); 9118 print_rebuild_status_impl(vrs, i, name); 9119 free(name); 9120 } 9121 } 9122 } 9123 9124 /* 9125 * As we don't scrub checkpointed blocks, we want to warn the user that we 9126 * skipped scanning some blocks if a checkpoint exists or existed at any 9127 * time during the scan. If a sequential instead of healing reconstruction 9128 * was performed then the blocks were reconstructed. However, their checksums 9129 * have not been verified so we still print the warning. 9130 */ 9131 static void 9132 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) 9133 { 9134 if (ps == NULL || pcs == NULL) 9135 return; 9136 9137 if (pcs->pcs_state == CS_NONE || 9138 pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 9139 return; 9140 9141 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); 9142 9143 if (ps->pss_state == DSS_NONE) 9144 return; 9145 9146 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && 9147 ps->pss_end_time < pcs->pcs_start_time) 9148 return; 9149 9150 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { 9151 (void) printf(gettext(" scan warning: skipped blocks " 9152 "that are only referenced by the checkpoint.\n")); 9153 } else { 9154 assert(ps->pss_state == DSS_SCANNING); 9155 (void) printf(gettext(" scan warning: skipping blocks " 9156 "that are only referenced by the checkpoint.\n")); 9157 } 9158 } 9159 9160 /* 9161 * Returns B_TRUE if there is an active rebuild in progress. Otherwise, 9162 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for 9163 * the last completed (or cancelled) rebuild. 9164 */ 9165 static boolean_t 9166 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) 9167 { 9168 nvlist_t **child; 9169 uint_t children; 9170 boolean_t rebuilding = B_FALSE; 9171 uint64_t end_time = 0; 9172 9173 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9174 &child, &children) != 0) 9175 children = 0; 9176 9177 for (uint_t c = 0; c < children; c++) { 9178 vdev_rebuild_stat_t *vrs; 9179 uint_t i; 9180 9181 if (nvlist_lookup_uint64_array(child[c], 9182 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { 9183 9184 if (vrs->vrs_end_time > end_time) 9185 end_time = vrs->vrs_end_time; 9186 9187 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 9188 rebuilding = B_TRUE; 9189 end_time = 0; 9190 break; 9191 } 9192 } 9193 } 9194 9195 if (rebuild_end_time != NULL) 9196 *rebuild_end_time = end_time; 9197 9198 return (rebuilding); 9199 } 9200 9201 static void 9202 vdev_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9203 int depth, boolean_t isspare, char *parent, nvlist_t *item) 9204 { 9205 nvlist_t *vds, **child, *ch = NULL; 9206 uint_t vsc, children; 9207 vdev_stat_t *vs; 9208 char *vname; 9209 uint64_t notpresent; 9210 const char *type, *path; 9211 9212 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 9213 &child, &children) != 0) 9214 children = 0; 9215 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 9216 (uint64_t **)&vs, &vsc) == 0); 9217 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 9218 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) 9219 return; 9220 9221 if (cb->cb_print_unhealthy && depth > 0 && 9222 for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) { 9223 return; 9224 } 9225 vname = zpool_vdev_name(g_zfs, zhp, nv, 9226 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 9227 vds = fnvlist_alloc(); 9228 fill_vdev_info(vds, zhp, vname, B_FALSE, cb->cb_json_as_int); 9229 if (cb->cb_flat_vdevs && parent != NULL) { 9230 fnvlist_add_string(vds, "parent", parent); 9231 } 9232 9233 if (isspare) { 9234 if (vs->vs_aux == VDEV_AUX_SPARED) { 9235 fnvlist_add_string(vds, "state", "INUSE"); 9236 used_by_other(zhp, nv, vds); 9237 } else if (vs->vs_state == VDEV_STATE_HEALTHY) 9238 fnvlist_add_string(vds, "state", "AVAIL"); 9239 } else { 9240 if (vs->vs_alloc) { 9241 nice_num_str_nvlist(vds, "alloc_space", vs->vs_alloc, 9242 cb->cb_literal, cb->cb_json_as_int, 9243 ZFS_NICENUM_BYTES); 9244 } 9245 if (vs->vs_space) { 9246 nice_num_str_nvlist(vds, "total_space", vs->vs_space, 9247 cb->cb_literal, cb->cb_json_as_int, 9248 ZFS_NICENUM_BYTES); 9249 } 9250 if (vs->vs_dspace) { 9251 nice_num_str_nvlist(vds, "def_space", vs->vs_dspace, 9252 cb->cb_literal, cb->cb_json_as_int, 9253 ZFS_NICENUM_BYTES); 9254 } 9255 if (vs->vs_rsize) { 9256 nice_num_str_nvlist(vds, "rep_dev_size", vs->vs_rsize, 9257 cb->cb_literal, cb->cb_json_as_int, 9258 ZFS_NICENUM_BYTES); 9259 } 9260 if (vs->vs_esize) { 9261 nice_num_str_nvlist(vds, "ex_dev_size", vs->vs_esize, 9262 cb->cb_literal, cb->cb_json_as_int, 9263 ZFS_NICENUM_BYTES); 9264 } 9265 if (vs->vs_self_healed) { 9266 nice_num_str_nvlist(vds, "self_healed", 9267 vs->vs_self_healed, cb->cb_literal, 9268 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9269 } 9270 if (vs->vs_pspace) { 9271 nice_num_str_nvlist(vds, "phys_space", vs->vs_pspace, 9272 cb->cb_literal, cb->cb_json_as_int, 9273 ZFS_NICENUM_BYTES); 9274 } 9275 nice_num_str_nvlist(vds, "read_errors", vs->vs_read_errors, 9276 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9277 nice_num_str_nvlist(vds, "write_errors", vs->vs_write_errors, 9278 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9279 nice_num_str_nvlist(vds, "checksum_errors", 9280 vs->vs_checksum_errors, cb->cb_literal, 9281 cb->cb_json_as_int, ZFS_NICENUM_1024); 9282 if (vs->vs_scan_processed) { 9283 nice_num_str_nvlist(vds, "scan_processed", 9284 vs->vs_scan_processed, cb->cb_literal, 9285 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9286 } 9287 if (vs->vs_checkpoint_space) { 9288 nice_num_str_nvlist(vds, "checkpoint_space", 9289 vs->vs_checkpoint_space, cb->cb_literal, 9290 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9291 } 9292 if (vs->vs_resilver_deferred) { 9293 nice_num_str_nvlist(vds, "resilver_deferred", 9294 vs->vs_resilver_deferred, B_TRUE, 9295 cb->cb_json_as_int, ZFS_NICENUM_1024); 9296 } 9297 if (children == 0) { 9298 nice_num_str_nvlist(vds, "slow_ios", vs->vs_slow_ios, 9299 cb->cb_literal, cb->cb_json_as_int, 9300 ZFS_NICENUM_1024); 9301 } 9302 if (cb->cb_print_power) { 9303 if (children == 0) { 9304 /* Only leaf vdevs have physical slots */ 9305 switch (zpool_power_current_state(zhp, (char *) 9306 fnvlist_lookup_string(nv, 9307 ZPOOL_CONFIG_PATH))) { 9308 case 0: 9309 fnvlist_add_string(vds, "power_state", 9310 "off"); 9311 break; 9312 case 1: 9313 fnvlist_add_string(vds, "power_state", 9314 "on"); 9315 break; 9316 default: 9317 fnvlist_add_string(vds, "power_state", 9318 "-"); 9319 } 9320 } else { 9321 fnvlist_add_string(vds, "power_state", "-"); 9322 } 9323 } 9324 } 9325 9326 if (cb->cb_print_dio_verify) { 9327 nice_num_str_nvlist(vds, "dio_verify_errors", 9328 vs->vs_dio_verify_errors, cb->cb_literal, 9329 cb->cb_json_as_int, ZFS_NICENUM_1024); 9330 } 9331 9332 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 9333 ¬present) == 0) { 9334 nice_num_str_nvlist(vds, ZPOOL_CONFIG_NOT_PRESENT, 9335 1, B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9336 fnvlist_add_string(vds, "was", 9337 fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH)); 9338 } else if (vs->vs_aux != VDEV_AUX_NONE) { 9339 fnvlist_add_string(vds, "aux", vdev_aux_str[vs->vs_aux]); 9340 } else if (children == 0 && !isspare && 9341 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && 9342 VDEV_STAT_VALID(vs_physical_ashift, vsc) && 9343 vs->vs_configured_ashift < vs->vs_physical_ashift) { 9344 nice_num_str_nvlist(vds, "configured_ashift", 9345 vs->vs_configured_ashift, B_TRUE, cb->cb_json_as_int, 9346 ZFS_NICENUM_1024); 9347 nice_num_str_nvlist(vds, "physical_ashift", 9348 vs->vs_physical_ashift, B_TRUE, cb->cb_json_as_int, 9349 ZFS_NICENUM_1024); 9350 } 9351 if (vs->vs_scan_removing != 0) { 9352 nice_num_str_nvlist(vds, "removing", vs->vs_scan_removing, 9353 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9354 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) { 9355 nice_num_str_nvlist(vds, "noalloc", vs->vs_noalloc, 9356 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9357 } 9358 9359 if (cb->vcdl != NULL) { 9360 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 9361 zpool_nvlist_cmd(cb->vcdl, zpool_get_name(zhp), 9362 path, vds); 9363 } 9364 } 9365 9366 if (children == 0) { 9367 if (cb->cb_print_vdev_init) { 9368 if (vs->vs_initialize_state != 0) { 9369 uint64_t st = vs->vs_initialize_state; 9370 fnvlist_add_string(vds, "init_state", 9371 vdev_init_state_str[st]); 9372 nice_num_str_nvlist(vds, "initialized", 9373 vs->vs_initialize_bytes_done, 9374 cb->cb_literal, cb->cb_json_as_int, 9375 ZFS_NICENUM_BYTES); 9376 nice_num_str_nvlist(vds, "to_initialize", 9377 vs->vs_initialize_bytes_est, 9378 cb->cb_literal, cb->cb_json_as_int, 9379 ZFS_NICENUM_BYTES); 9380 nice_num_str_nvlist(vds, "init_time", 9381 vs->vs_initialize_action_time, 9382 cb->cb_literal, cb->cb_json_as_int, 9383 ZFS_NICE_TIMESTAMP); 9384 nice_num_str_nvlist(vds, "init_errors", 9385 vs->vs_initialize_errors, 9386 cb->cb_literal, cb->cb_json_as_int, 9387 ZFS_NICENUM_1024); 9388 } else { 9389 fnvlist_add_string(vds, "init_state", 9390 "UNINITIALIZED"); 9391 } 9392 } 9393 if (cb->cb_print_vdev_trim) { 9394 if (vs->vs_trim_notsup == 0) { 9395 if (vs->vs_trim_state != 0) { 9396 uint64_t st = vs->vs_trim_state; 9397 fnvlist_add_string(vds, "trim_state", 9398 vdev_trim_state_str[st]); 9399 nice_num_str_nvlist(vds, "trimmed", 9400 vs->vs_trim_bytes_done, 9401 cb->cb_literal, cb->cb_json_as_int, 9402 ZFS_NICENUM_BYTES); 9403 nice_num_str_nvlist(vds, "to_trim", 9404 vs->vs_trim_bytes_est, 9405 cb->cb_literal, cb->cb_json_as_int, 9406 ZFS_NICENUM_BYTES); 9407 nice_num_str_nvlist(vds, "trim_time", 9408 vs->vs_trim_action_time, 9409 cb->cb_literal, cb->cb_json_as_int, 9410 ZFS_NICE_TIMESTAMP); 9411 nice_num_str_nvlist(vds, "trim_errors", 9412 vs->vs_trim_errors, 9413 cb->cb_literal, cb->cb_json_as_int, 9414 ZFS_NICENUM_1024); 9415 } else 9416 fnvlist_add_string(vds, "trim_state", 9417 "UNTRIMMED"); 9418 } 9419 nice_num_str_nvlist(vds, "trim_notsup", 9420 vs->vs_trim_notsup, B_TRUE, 9421 cb->cb_json_as_int, ZFS_NICENUM_1024); 9422 } 9423 } else { 9424 ch = fnvlist_alloc(); 9425 } 9426 9427 if (cb->cb_flat_vdevs && children == 0) { 9428 fnvlist_add_nvlist(item, vname, vds); 9429 } 9430 9431 for (int c = 0; c < children; c++) { 9432 uint64_t islog = B_FALSE, ishole = B_FALSE; 9433 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 9434 &islog); 9435 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 9436 &ishole); 9437 if (islog || ishole) 9438 continue; 9439 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) 9440 continue; 9441 if (cb->cb_flat_vdevs) { 9442 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare, 9443 vname, item); 9444 } 9445 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare, 9446 vname, ch); 9447 } 9448 9449 if (ch != NULL) { 9450 if (!nvlist_empty(ch)) 9451 fnvlist_add_nvlist(vds, "vdevs", ch); 9452 fnvlist_free(ch); 9453 } 9454 fnvlist_add_nvlist(item, vname, vds); 9455 fnvlist_free(vds); 9456 free(vname); 9457 } 9458 9459 static void 9460 class_vdevs_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9461 const char *class, nvlist_t *item) 9462 { 9463 uint_t c, children; 9464 nvlist_t **child; 9465 nvlist_t *class_obj = NULL; 9466 9467 if (!cb->cb_flat_vdevs) 9468 class_obj = fnvlist_alloc(); 9469 9470 assert(zhp != NULL || !cb->cb_verbose); 9471 9472 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 9473 &children) != 0) 9474 return; 9475 9476 for (c = 0; c < children; c++) { 9477 uint64_t is_log = B_FALSE; 9478 const char *bias = NULL; 9479 const char *type = NULL; 9480 char *name = zpool_vdev_name(g_zfs, zhp, child[c], 9481 cb->cb_name_flags | VDEV_NAME_TYPE_ID); 9482 9483 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 9484 &is_log); 9485 9486 if (is_log) { 9487 bias = (char *)VDEV_ALLOC_CLASS_LOGS; 9488 } else { 9489 (void) nvlist_lookup_string(child[c], 9490 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); 9491 (void) nvlist_lookup_string(child[c], 9492 ZPOOL_CONFIG_TYPE, &type); 9493 } 9494 9495 if (bias == NULL || strcmp(bias, class) != 0) 9496 continue; 9497 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) 9498 continue; 9499 9500 if (cb->cb_flat_vdevs) { 9501 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE, 9502 NULL, item); 9503 } else { 9504 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE, 9505 NULL, class_obj); 9506 } 9507 free(name); 9508 } 9509 if (!cb->cb_flat_vdevs) { 9510 if (!nvlist_empty(class_obj)) 9511 fnvlist_add_nvlist(item, class, class_obj); 9512 fnvlist_free(class_obj); 9513 } 9514 } 9515 9516 static void 9517 l2cache_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9518 nvlist_t *item) 9519 { 9520 nvlist_t *l2c = NULL, **l2cache; 9521 uint_t nl2cache; 9522 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 9523 &l2cache, &nl2cache) == 0) { 9524 if (nl2cache == 0) 9525 return; 9526 if (!cb->cb_flat_vdevs) 9527 l2c = fnvlist_alloc(); 9528 for (int i = 0; i < nl2cache; i++) { 9529 if (cb->cb_flat_vdevs) { 9530 vdev_stats_nvlist(zhp, cb, l2cache[i], 2, 9531 B_FALSE, NULL, item); 9532 } else { 9533 vdev_stats_nvlist(zhp, cb, l2cache[i], 2, 9534 B_FALSE, NULL, l2c); 9535 } 9536 } 9537 } 9538 if (!cb->cb_flat_vdevs) { 9539 if (!nvlist_empty(l2c)) 9540 fnvlist_add_nvlist(item, "l2cache", l2c); 9541 fnvlist_free(l2c); 9542 } 9543 } 9544 9545 static void 9546 spares_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, 9547 nvlist_t *item) 9548 { 9549 nvlist_t *sp = NULL, **spares; 9550 uint_t nspares; 9551 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 9552 &spares, &nspares) == 0) { 9553 if (nspares == 0) 9554 return; 9555 if (!cb->cb_flat_vdevs) 9556 sp = fnvlist_alloc(); 9557 for (int i = 0; i < nspares; i++) { 9558 if (cb->cb_flat_vdevs) { 9559 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE, 9560 NULL, item); 9561 } else { 9562 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE, 9563 NULL, sp); 9564 } 9565 } 9566 } 9567 if (!cb->cb_flat_vdevs) { 9568 if (!nvlist_empty(sp)) 9569 fnvlist_add_nvlist(item, "spares", sp); 9570 fnvlist_free(sp); 9571 } 9572 } 9573 9574 static void 9575 errors_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item) 9576 { 9577 uint64_t nerr; 9578 nvlist_t *config = zpool_get_config(zhp, NULL); 9579 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 9580 &nerr) == 0) { 9581 nice_num_str_nvlist(item, ZPOOL_CONFIG_ERRCOUNT, nerr, 9582 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9583 if (nerr != 0 && cb->cb_verbose) { 9584 nvlist_t *nverrlist = NULL; 9585 if (zpool_get_errlog(zhp, &nverrlist) == 0) { 9586 int i = 0; 9587 int count = 0; 9588 size_t len = MAXPATHLEN * 2; 9589 nvpair_t *elem = NULL; 9590 9591 for (nvpair_t *pair = 9592 nvlist_next_nvpair(nverrlist, NULL); 9593 pair != NULL; 9594 pair = nvlist_next_nvpair(nverrlist, pair)) 9595 count++; 9596 char **errl = (char **)malloc( 9597 count * sizeof (char *)); 9598 9599 while ((elem = nvlist_next_nvpair(nverrlist, 9600 elem)) != NULL) { 9601 nvlist_t *nv; 9602 uint64_t dsobj, obj; 9603 9604 verify(nvpair_value_nvlist(elem, 9605 &nv) == 0); 9606 verify(nvlist_lookup_uint64(nv, 9607 ZPOOL_ERR_DATASET, &dsobj) == 0); 9608 verify(nvlist_lookup_uint64(nv, 9609 ZPOOL_ERR_OBJECT, &obj) == 0); 9610 errl[i] = safe_malloc(len); 9611 zpool_obj_to_path(zhp, dsobj, obj, 9612 errl[i++], len); 9613 } 9614 nvlist_free(nverrlist); 9615 fnvlist_add_string_array(item, "errlist", 9616 (const char **)errl, count); 9617 for (int i = 0; i < count; ++i) 9618 free(errl[i]); 9619 free(errl); 9620 } else 9621 fnvlist_add_string(item, "errlist", 9622 strerror(errno)); 9623 } 9624 } 9625 } 9626 9627 static void 9628 ddt_stats_nvlist(ddt_stat_t *dds, status_cbdata_t *cb, nvlist_t *item) 9629 { 9630 nice_num_str_nvlist(item, "blocks", dds->dds_blocks, 9631 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9632 nice_num_str_nvlist(item, "logical_size", dds->dds_lsize, 9633 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9634 nice_num_str_nvlist(item, "physical_size", dds->dds_psize, 9635 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9636 nice_num_str_nvlist(item, "deflated_size", dds->dds_dsize, 9637 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9638 nice_num_str_nvlist(item, "ref_blocks", dds->dds_ref_blocks, 9639 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9640 nice_num_str_nvlist(item, "ref_lsize", dds->dds_ref_lsize, 9641 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9642 nice_num_str_nvlist(item, "ref_psize", dds->dds_ref_psize, 9643 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9644 nice_num_str_nvlist(item, "ref_dsize", dds->dds_ref_dsize, 9645 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9646 } 9647 9648 static void 9649 dedup_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item) 9650 { 9651 nvlist_t *config; 9652 if (cb->cb_dedup_stats) { 9653 ddt_histogram_t *ddh; 9654 ddt_stat_t *dds; 9655 ddt_object_t *ddo; 9656 nvlist_t *ddt_stat, *ddt_obj, *dedup; 9657 uint_t c; 9658 uint64_t cspace_prop; 9659 9660 config = zpool_get_config(zhp, NULL); 9661 if (nvlist_lookup_uint64_array(config, 9662 ZPOOL_CONFIG_DDT_OBJ_STATS, (uint64_t **)&ddo, &c) != 0) 9663 return; 9664 9665 dedup = fnvlist_alloc(); 9666 ddt_obj = fnvlist_alloc(); 9667 nice_num_str_nvlist(dedup, "obj_count", ddo->ddo_count, 9668 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9669 if (ddo->ddo_count == 0) { 9670 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS, 9671 ddt_obj); 9672 fnvlist_add_nvlist(item, "dedup_stats", dedup); 9673 fnvlist_free(ddt_obj); 9674 fnvlist_free(dedup); 9675 return; 9676 } else { 9677 nice_num_str_nvlist(dedup, "dspace", ddo->ddo_dspace, 9678 cb->cb_literal, cb->cb_json_as_int, 9679 ZFS_NICENUM_1024); 9680 nice_num_str_nvlist(dedup, "mspace", ddo->ddo_mspace, 9681 cb->cb_literal, cb->cb_json_as_int, 9682 ZFS_NICENUM_1024); 9683 /* 9684 * Squash cached size into in-core size to handle race. 9685 * Only include cached size if it is available. 9686 */ 9687 cspace_prop = zpool_get_prop_int(zhp, 9688 ZPOOL_PROP_DEDUPCACHED, NULL); 9689 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace); 9690 nice_num_str_nvlist(dedup, "cspace", cspace_prop, 9691 cb->cb_literal, cb->cb_json_as_int, 9692 ZFS_NICENUM_1024); 9693 } 9694 9695 ddt_stat = fnvlist_alloc(); 9696 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 9697 (uint64_t **)&dds, &c) == 0) { 9698 nvlist_t *total = fnvlist_alloc(); 9699 if (dds->dds_blocks == 0) 9700 fnvlist_add_string(total, "blocks", "0"); 9701 else 9702 ddt_stats_nvlist(dds, cb, total); 9703 fnvlist_add_nvlist(ddt_stat, "total", total); 9704 fnvlist_free(total); 9705 } 9706 if (nvlist_lookup_uint64_array(config, 9707 ZPOOL_CONFIG_DDT_HISTOGRAM, (uint64_t **)&ddh, &c) == 0) { 9708 nvlist_t *hist = fnvlist_alloc(); 9709 nvlist_t *entry = NULL; 9710 char buf[16]; 9711 for (int h = 0; h < 64; h++) { 9712 if (ddh->ddh_stat[h].dds_blocks != 0) { 9713 entry = fnvlist_alloc(); 9714 ddt_stats_nvlist(&ddh->ddh_stat[h], cb, 9715 entry); 9716 snprintf(buf, 16, "%d", h); 9717 fnvlist_add_nvlist(hist, buf, entry); 9718 fnvlist_free(entry); 9719 } 9720 } 9721 if (!nvlist_empty(hist)) 9722 fnvlist_add_nvlist(ddt_stat, "histogram", hist); 9723 fnvlist_free(hist); 9724 } 9725 9726 if (!nvlist_empty(ddt_obj)) { 9727 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS, 9728 ddt_obj); 9729 } 9730 fnvlist_free(ddt_obj); 9731 if (!nvlist_empty(ddt_stat)) { 9732 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_STATS, 9733 ddt_stat); 9734 } 9735 fnvlist_free(ddt_stat); 9736 if (!nvlist_empty(dedup)) 9737 fnvlist_add_nvlist(item, "dedup_stats", dedup); 9738 fnvlist_free(dedup); 9739 } 9740 } 9741 9742 static void 9743 raidz_expand_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9744 nvlist_t *nvroot, nvlist_t *item) 9745 { 9746 uint_t c; 9747 pool_raidz_expand_stat_t *pres = NULL; 9748 if (nvlist_lookup_uint64_array(nvroot, 9749 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c) == 0) { 9750 nvlist_t **child; 9751 uint_t children; 9752 nvlist_t *nv = fnvlist_alloc(); 9753 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9754 &child, &children) == 0); 9755 assert(pres->pres_expanding_vdev < children); 9756 char *name = 9757 zpool_vdev_name(g_zfs, zhp, 9758 child[pres->pres_expanding_vdev], 0); 9759 fill_vdev_info(nv, zhp, name, B_FALSE, cb->cb_json_as_int); 9760 fnvlist_add_string(nv, "state", 9761 pool_scan_state_str[pres->pres_state]); 9762 nice_num_str_nvlist(nv, "expanding_vdev", 9763 pres->pres_expanding_vdev, B_TRUE, cb->cb_json_as_int, 9764 ZFS_NICENUM_1024); 9765 nice_num_str_nvlist(nv, "start_time", pres->pres_start_time, 9766 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9767 nice_num_str_nvlist(nv, "end_time", pres->pres_end_time, 9768 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9769 nice_num_str_nvlist(nv, "to_reflow", pres->pres_to_reflow, 9770 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9771 nice_num_str_nvlist(nv, "reflowed", pres->pres_reflowed, 9772 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9773 nice_num_str_nvlist(nv, "waiting_for_resilver", 9774 pres->pres_waiting_for_resilver, B_TRUE, 9775 cb->cb_json_as_int, ZFS_NICENUM_1024); 9776 fnvlist_add_nvlist(item, ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, nv); 9777 fnvlist_free(nv); 9778 free(name); 9779 } 9780 } 9781 9782 static void 9783 checkpoint_status_nvlist(nvlist_t *nvroot, status_cbdata_t *cb, 9784 nvlist_t *item) 9785 { 9786 uint_t c; 9787 pool_checkpoint_stat_t *pcs = NULL; 9788 if (nvlist_lookup_uint64_array(nvroot, 9789 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c) == 0) { 9790 nvlist_t *nv = fnvlist_alloc(); 9791 fnvlist_add_string(nv, "state", 9792 checkpoint_state_str[pcs->pcs_state]); 9793 nice_num_str_nvlist(nv, "start_time", 9794 pcs->pcs_start_time, cb->cb_literal, cb->cb_json_as_int, 9795 ZFS_NICE_TIMESTAMP); 9796 nice_num_str_nvlist(nv, "space", 9797 pcs->pcs_space, cb->cb_literal, cb->cb_json_as_int, 9798 ZFS_NICENUM_BYTES); 9799 fnvlist_add_nvlist(item, ZPOOL_CONFIG_CHECKPOINT_STATS, nv); 9800 fnvlist_free(nv); 9801 } 9802 } 9803 9804 static void 9805 removal_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9806 nvlist_t *nvroot, nvlist_t *item) 9807 { 9808 uint_t c; 9809 pool_removal_stat_t *prs = NULL; 9810 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_REMOVAL_STATS, 9811 (uint64_t **)&prs, &c) == 0) { 9812 if (prs->prs_state != DSS_NONE) { 9813 nvlist_t **child; 9814 uint_t children; 9815 verify(nvlist_lookup_nvlist_array(nvroot, 9816 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0); 9817 assert(prs->prs_removing_vdev < children); 9818 char *vdev_name = zpool_vdev_name(g_zfs, zhp, 9819 child[prs->prs_removing_vdev], B_TRUE); 9820 nvlist_t *nv = fnvlist_alloc(); 9821 fill_vdev_info(nv, zhp, vdev_name, B_FALSE, 9822 cb->cb_json_as_int); 9823 fnvlist_add_string(nv, "state", 9824 pool_scan_state_str[prs->prs_state]); 9825 nice_num_str_nvlist(nv, "removing_vdev", 9826 prs->prs_removing_vdev, B_TRUE, cb->cb_json_as_int, 9827 ZFS_NICENUM_1024); 9828 nice_num_str_nvlist(nv, "start_time", 9829 prs->prs_start_time, cb->cb_literal, 9830 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9831 nice_num_str_nvlist(nv, "end_time", prs->prs_end_time, 9832 cb->cb_literal, cb->cb_json_as_int, 9833 ZFS_NICE_TIMESTAMP); 9834 nice_num_str_nvlist(nv, "to_copy", prs->prs_to_copy, 9835 cb->cb_literal, cb->cb_json_as_int, 9836 ZFS_NICENUM_BYTES); 9837 nice_num_str_nvlist(nv, "copied", prs->prs_copied, 9838 cb->cb_literal, cb->cb_json_as_int, 9839 ZFS_NICENUM_BYTES); 9840 nice_num_str_nvlist(nv, "mapping_memory", 9841 prs->prs_mapping_memory, cb->cb_literal, 9842 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9843 fnvlist_add_nvlist(item, 9844 ZPOOL_CONFIG_REMOVAL_STATS, nv); 9845 fnvlist_free(nv); 9846 free(vdev_name); 9847 } 9848 } 9849 } 9850 9851 static void 9852 scan_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, 9853 nvlist_t *nvroot, nvlist_t *item) 9854 { 9855 pool_scan_stat_t *ps = NULL; 9856 uint_t c; 9857 nvlist_t *scan = fnvlist_alloc(); 9858 nvlist_t **child; 9859 uint_t children; 9860 9861 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 9862 (uint64_t **)&ps, &c) == 0) { 9863 fnvlist_add_string(scan, "function", 9864 pool_scan_func_str[ps->pss_func]); 9865 fnvlist_add_string(scan, "state", 9866 pool_scan_state_str[ps->pss_state]); 9867 nice_num_str_nvlist(scan, "start_time", ps->pss_start_time, 9868 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9869 nice_num_str_nvlist(scan, "end_time", ps->pss_end_time, 9870 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9871 nice_num_str_nvlist(scan, "to_examine", ps->pss_to_examine, 9872 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9873 nice_num_str_nvlist(scan, "examined", ps->pss_examined, 9874 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9875 nice_num_str_nvlist(scan, "skipped", ps->pss_skipped, 9876 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9877 nice_num_str_nvlist(scan, "processed", ps->pss_processed, 9878 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9879 nice_num_str_nvlist(scan, "errors", ps->pss_errors, 9880 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024); 9881 nice_num_str_nvlist(scan, "bytes_per_scan", ps->pss_pass_exam, 9882 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9883 nice_num_str_nvlist(scan, "pass_start", ps->pss_pass_start, 9884 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9885 nice_num_str_nvlist(scan, "scrub_pause", 9886 ps->pss_pass_scrub_pause, cb->cb_literal, 9887 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP); 9888 nice_num_str_nvlist(scan, "scrub_spent_paused", 9889 ps->pss_pass_scrub_spent_paused, 9890 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9891 nice_num_str_nvlist(scan, "issued_bytes_per_scan", 9892 ps->pss_pass_issued, cb->cb_literal, 9893 cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9894 nice_num_str_nvlist(scan, "issued", ps->pss_issued, 9895 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES); 9896 if (ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB && 9897 ps->pss_error_scrub_start > ps->pss_start_time) { 9898 fnvlist_add_string(scan, "err_scrub_func", 9899 pool_scan_func_str[ps->pss_error_scrub_func]); 9900 fnvlist_add_string(scan, "err_scrub_state", 9901 pool_scan_state_str[ps->pss_error_scrub_state]); 9902 nice_num_str_nvlist(scan, "err_scrub_start_time", 9903 ps->pss_error_scrub_start, 9904 cb->cb_literal, cb->cb_json_as_int, 9905 ZFS_NICE_TIMESTAMP); 9906 nice_num_str_nvlist(scan, "err_scrub_end_time", 9907 ps->pss_error_scrub_end, 9908 cb->cb_literal, cb->cb_json_as_int, 9909 ZFS_NICE_TIMESTAMP); 9910 nice_num_str_nvlist(scan, "err_scrub_examined", 9911 ps->pss_error_scrub_examined, 9912 cb->cb_literal, cb->cb_json_as_int, 9913 ZFS_NICENUM_1024); 9914 nice_num_str_nvlist(scan, "err_scrub_to_examine", 9915 ps->pss_error_scrub_to_be_examined, 9916 cb->cb_literal, cb->cb_json_as_int, 9917 ZFS_NICENUM_1024); 9918 nice_num_str_nvlist(scan, "err_scrub_pause", 9919 ps->pss_pass_error_scrub_pause, 9920 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024); 9921 } 9922 } 9923 9924 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 9925 &child, &children) == 0) { 9926 vdev_rebuild_stat_t *vrs; 9927 uint_t i; 9928 char *name; 9929 nvlist_t *nv; 9930 nvlist_t *rebuild = fnvlist_alloc(); 9931 uint64_t st; 9932 for (uint_t c = 0; c < children; c++) { 9933 if (nvlist_lookup_uint64_array(child[c], 9934 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, 9935 &i) == 0) { 9936 if (vrs->vrs_state != VDEV_REBUILD_NONE) { 9937 nv = fnvlist_alloc(); 9938 name = zpool_vdev_name(g_zfs, zhp, 9939 child[c], VDEV_NAME_TYPE_ID); 9940 fill_vdev_info(nv, zhp, name, B_FALSE, 9941 cb->cb_json_as_int); 9942 st = vrs->vrs_state; 9943 fnvlist_add_string(nv, "state", 9944 vdev_rebuild_state_str[st]); 9945 nice_num_str_nvlist(nv, "start_time", 9946 vrs->vrs_start_time, cb->cb_literal, 9947 cb->cb_json_as_int, 9948 ZFS_NICE_TIMESTAMP); 9949 nice_num_str_nvlist(nv, "end_time", 9950 vrs->vrs_end_time, cb->cb_literal, 9951 cb->cb_json_as_int, 9952 ZFS_NICE_TIMESTAMP); 9953 nice_num_str_nvlist(nv, "scan_time", 9954 vrs->vrs_scan_time_ms * 1000000, 9955 cb->cb_literal, cb->cb_json_as_int, 9956 ZFS_NICENUM_TIME); 9957 nice_num_str_nvlist(nv, "scanned", 9958 vrs->vrs_bytes_scanned, 9959 cb->cb_literal, cb->cb_json_as_int, 9960 ZFS_NICENUM_BYTES); 9961 nice_num_str_nvlist(nv, "issued", 9962 vrs->vrs_bytes_issued, 9963 cb->cb_literal, cb->cb_json_as_int, 9964 ZFS_NICENUM_BYTES); 9965 nice_num_str_nvlist(nv, "rebuilt", 9966 vrs->vrs_bytes_rebuilt, 9967 cb->cb_literal, cb->cb_json_as_int, 9968 ZFS_NICENUM_BYTES); 9969 nice_num_str_nvlist(nv, "to_scan", 9970 vrs->vrs_bytes_est, cb->cb_literal, 9971 cb->cb_json_as_int, 9972 ZFS_NICENUM_BYTES); 9973 nice_num_str_nvlist(nv, "errors", 9974 vrs->vrs_errors, cb->cb_literal, 9975 cb->cb_json_as_int, 9976 ZFS_NICENUM_1024); 9977 nice_num_str_nvlist(nv, "pass_time", 9978 vrs->vrs_pass_time_ms * 1000000, 9979 cb->cb_literal, cb->cb_json_as_int, 9980 ZFS_NICENUM_TIME); 9981 nice_num_str_nvlist(nv, "pass_scanned", 9982 vrs->vrs_pass_bytes_scanned, 9983 cb->cb_literal, cb->cb_json_as_int, 9984 ZFS_NICENUM_BYTES); 9985 nice_num_str_nvlist(nv, "pass_issued", 9986 vrs->vrs_pass_bytes_issued, 9987 cb->cb_literal, cb->cb_json_as_int, 9988 ZFS_NICENUM_BYTES); 9989 nice_num_str_nvlist(nv, "pass_skipped", 9990 vrs->vrs_pass_bytes_skipped, 9991 cb->cb_literal, cb->cb_json_as_int, 9992 ZFS_NICENUM_BYTES); 9993 fnvlist_add_nvlist(rebuild, name, nv); 9994 free(name); 9995 } 9996 } 9997 } 9998 if (!nvlist_empty(rebuild)) 9999 fnvlist_add_nvlist(scan, "rebuild_stats", rebuild); 10000 fnvlist_free(rebuild); 10001 } 10002 10003 if (!nvlist_empty(scan)) 10004 fnvlist_add_nvlist(item, ZPOOL_CONFIG_SCAN_STATS, scan); 10005 fnvlist_free(scan); 10006 } 10007 10008 /* 10009 * Print the scan status. 10010 */ 10011 static void 10012 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) 10013 { 10014 uint64_t rebuild_end_time = 0, resilver_end_time = 0; 10015 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; 10016 boolean_t have_errorscrub = B_FALSE; 10017 boolean_t active_resilver = B_FALSE; 10018 pool_checkpoint_stat_t *pcs = NULL; 10019 pool_scan_stat_t *ps = NULL; 10020 uint_t c; 10021 time_t scrub_start = 0, errorscrub_start = 0; 10022 10023 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, 10024 (uint64_t **)&ps, &c) == 0) { 10025 if (ps->pss_func == POOL_SCAN_RESILVER) { 10026 resilver_end_time = ps->pss_end_time; 10027 active_resilver = (ps->pss_state == DSS_SCANNING); 10028 } 10029 10030 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); 10031 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); 10032 scrub_start = ps->pss_start_time; 10033 if (c > offsetof(pool_scan_stat_t, 10034 pss_pass_error_scrub_pause) / 8) { 10035 have_errorscrub = (ps->pss_error_scrub_func == 10036 POOL_SCAN_ERRORSCRUB); 10037 errorscrub_start = ps->pss_error_scrub_start; 10038 } 10039 } 10040 10041 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); 10042 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); 10043 10044 /* Always print the scrub status when available. */ 10045 if (have_scrub && scrub_start > errorscrub_start) 10046 print_scan_scrub_resilver_status(ps); 10047 else if (have_errorscrub && errorscrub_start >= scrub_start) 10048 print_err_scrub_status(ps); 10049 10050 /* 10051 * When there is an active resilver or rebuild print its status. 10052 * Otherwise print the status of the last resilver or rebuild. 10053 */ 10054 if (active_resilver || (!active_rebuild && have_resilver && 10055 resilver_end_time && resilver_end_time > rebuild_end_time)) { 10056 print_scan_scrub_resilver_status(ps); 10057 } else if (active_rebuild || (!active_resilver && have_rebuild && 10058 rebuild_end_time && rebuild_end_time > resilver_end_time)) { 10059 print_rebuild_status(zhp, nvroot); 10060 } 10061 10062 (void) nvlist_lookup_uint64_array(nvroot, 10063 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10064 print_checkpoint_scan_warning(ps, pcs); 10065 } 10066 10067 /* 10068 * Print out detailed removal status. 10069 */ 10070 static void 10071 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) 10072 { 10073 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 10074 time_t start, end; 10075 nvlist_t *config, *nvroot; 10076 nvlist_t **child; 10077 uint_t children; 10078 char *vdev_name; 10079 10080 if (prs == NULL || prs->prs_state == DSS_NONE) 10081 return; 10082 10083 /* 10084 * Determine name of vdev. 10085 */ 10086 config = zpool_get_config(zhp, NULL); 10087 nvroot = fnvlist_lookup_nvlist(config, 10088 ZPOOL_CONFIG_VDEV_TREE); 10089 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 10090 &child, &children) == 0); 10091 assert(prs->prs_removing_vdev < children); 10092 vdev_name = zpool_vdev_name(g_zfs, zhp, 10093 child[prs->prs_removing_vdev], B_TRUE); 10094 10095 printf_color(ANSI_BOLD, gettext("remove: ")); 10096 10097 start = prs->prs_start_time; 10098 end = prs->prs_end_time; 10099 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); 10100 10101 /* 10102 * Removal is finished or canceled. 10103 */ 10104 if (prs->prs_state == DSS_FINISHED) { 10105 uint64_t minutes_taken = (end - start) / 60; 10106 10107 (void) printf(gettext("Removal of vdev %llu copied %s " 10108 "in %lluh%um, completed on %s"), 10109 (longlong_t)prs->prs_removing_vdev, 10110 copied_buf, 10111 (u_longlong_t)(minutes_taken / 60), 10112 (uint_t)(minutes_taken % 60), 10113 ctime((time_t *)&end)); 10114 } else if (prs->prs_state == DSS_CANCELED) { 10115 (void) printf(gettext("Removal of %s canceled on %s"), 10116 vdev_name, ctime(&end)); 10117 } else { 10118 uint64_t copied, total, elapsed, rate, mins_left, hours_left; 10119 double fraction_done; 10120 10121 assert(prs->prs_state == DSS_SCANNING); 10122 10123 /* 10124 * Removal is in progress. 10125 */ 10126 (void) printf(gettext( 10127 "Evacuation of %s in progress since %s"), 10128 vdev_name, ctime(&start)); 10129 10130 copied = prs->prs_copied > 0 ? prs->prs_copied : 1; 10131 total = prs->prs_to_copy; 10132 fraction_done = (double)copied / total; 10133 10134 /* elapsed time for this pass */ 10135 elapsed = time(NULL) - prs->prs_start_time; 10136 elapsed = elapsed > 0 ? elapsed : 1; 10137 rate = copied / elapsed; 10138 rate = rate > 0 ? rate : 1; 10139 mins_left = ((total - copied) / rate) / 60; 10140 hours_left = mins_left / 60; 10141 10142 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 10143 zfs_nicenum(total, total_buf, sizeof (total_buf)); 10144 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 10145 10146 /* 10147 * do not print estimated time if hours_left is more than 10148 * 30 days 10149 */ 10150 (void) printf(gettext( 10151 "\t%s copied out of %s at %s/s, %.2f%% done"), 10152 examined_buf, total_buf, rate_buf, 100 * fraction_done); 10153 if (hours_left < (30 * 24)) { 10154 (void) printf(gettext(", %lluh%um to go\n"), 10155 (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 10156 } else { 10157 (void) printf(gettext( 10158 ", (copy is slow, no estimated time)\n")); 10159 } 10160 } 10161 free(vdev_name); 10162 10163 if (prs->prs_mapping_memory > 0) { 10164 char mem_buf[7]; 10165 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); 10166 (void) printf(gettext( 10167 "\t%s memory used for removed device mappings\n"), 10168 mem_buf); 10169 } 10170 } 10171 10172 /* 10173 * Print out detailed raidz expansion status. 10174 */ 10175 static void 10176 print_raidz_expand_status(zpool_handle_t *zhp, pool_raidz_expand_stat_t *pres) 10177 { 10178 char copied_buf[7]; 10179 10180 if (pres == NULL || pres->pres_state == DSS_NONE) 10181 return; 10182 10183 /* 10184 * Determine name of vdev. 10185 */ 10186 nvlist_t *config = zpool_get_config(zhp, NULL); 10187 nvlist_t *nvroot = fnvlist_lookup_nvlist(config, 10188 ZPOOL_CONFIG_VDEV_TREE); 10189 nvlist_t **child; 10190 uint_t children; 10191 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 10192 &child, &children) == 0); 10193 assert(pres->pres_expanding_vdev < children); 10194 10195 printf_color(ANSI_BOLD, gettext("expand: ")); 10196 10197 time_t start = pres->pres_start_time; 10198 time_t end = pres->pres_end_time; 10199 char *vname = 10200 zpool_vdev_name(g_zfs, zhp, child[pres->pres_expanding_vdev], 0); 10201 zfs_nicenum(pres->pres_reflowed, copied_buf, sizeof (copied_buf)); 10202 10203 /* 10204 * Expansion is finished or canceled. 10205 */ 10206 if (pres->pres_state == DSS_FINISHED) { 10207 char time_buf[32]; 10208 secs_to_dhms(end - start, time_buf); 10209 10210 (void) printf(gettext("expanded %s-%u copied %s in %s, " 10211 "on %s"), vname, (int)pres->pres_expanding_vdev, 10212 copied_buf, time_buf, ctime((time_t *)&end)); 10213 } else { 10214 char examined_buf[7], total_buf[7], rate_buf[7]; 10215 uint64_t copied, total, elapsed, rate, secs_left; 10216 double fraction_done; 10217 10218 assert(pres->pres_state == DSS_SCANNING); 10219 10220 /* 10221 * Expansion is in progress. 10222 */ 10223 (void) printf(gettext( 10224 "expansion of %s-%u in progress since %s"), 10225 vname, (int)pres->pres_expanding_vdev, ctime(&start)); 10226 10227 copied = pres->pres_reflowed > 0 ? pres->pres_reflowed : 1; 10228 total = pres->pres_to_reflow; 10229 fraction_done = (double)copied / total; 10230 10231 /* elapsed time for this pass */ 10232 elapsed = time(NULL) - pres->pres_start_time; 10233 elapsed = elapsed > 0 ? elapsed : 1; 10234 rate = copied / elapsed; 10235 rate = rate > 0 ? rate : 1; 10236 secs_left = (total - copied) / rate; 10237 10238 zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); 10239 zfs_nicenum(total, total_buf, sizeof (total_buf)); 10240 zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 10241 10242 /* 10243 * do not print estimated time if hours_left is more than 10244 * 30 days 10245 */ 10246 (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"), 10247 examined_buf, total_buf, rate_buf, 100 * fraction_done); 10248 if (pres->pres_waiting_for_resilver) { 10249 (void) printf(gettext(", paused for resilver or " 10250 "clear\n")); 10251 } else if (secs_left < (30 * 24 * 3600)) { 10252 char time_buf[32]; 10253 secs_to_dhms(secs_left, time_buf); 10254 (void) printf(gettext(", %s to go\n"), time_buf); 10255 } else { 10256 (void) printf(gettext( 10257 ", (copy is slow, no estimated time)\n")); 10258 } 10259 } 10260 free(vname); 10261 } 10262 static void 10263 print_checkpoint_status(pool_checkpoint_stat_t *pcs) 10264 { 10265 time_t start; 10266 char space_buf[7]; 10267 10268 if (pcs == NULL || pcs->pcs_state == CS_NONE) 10269 return; 10270 10271 (void) printf(gettext("checkpoint: ")); 10272 10273 start = pcs->pcs_start_time; 10274 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); 10275 10276 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { 10277 char *date = ctime(&start); 10278 10279 /* 10280 * ctime() adds a newline at the end of the generated 10281 * string, thus the weird format specifier and the 10282 * strlen() call used to chop it off from the output. 10283 */ 10284 (void) printf(gettext("created %.*s, consumes %s\n"), 10285 (int)(strlen(date) - 1), date, space_buf); 10286 return; 10287 } 10288 10289 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); 10290 10291 (void) printf(gettext("discarding, %s remaining.\n"), 10292 space_buf); 10293 } 10294 10295 static void 10296 print_error_log(zpool_handle_t *zhp) 10297 { 10298 nvlist_t *nverrlist = NULL; 10299 nvpair_t *elem; 10300 char *pathname; 10301 size_t len = MAXPATHLEN * 2; 10302 10303 if (zpool_get_errlog(zhp, &nverrlist) != 0) 10304 return; 10305 10306 (void) printf("errors: Permanent errors have been " 10307 "detected in the following files:\n\n"); 10308 10309 pathname = safe_malloc(len); 10310 elem = NULL; 10311 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 10312 nvlist_t *nv; 10313 uint64_t dsobj, obj; 10314 10315 verify(nvpair_value_nvlist(elem, &nv) == 0); 10316 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 10317 &dsobj) == 0); 10318 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 10319 &obj) == 0); 10320 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 10321 (void) printf("%7s %s\n", "", pathname); 10322 } 10323 free(pathname); 10324 nvlist_free(nverrlist); 10325 } 10326 10327 static void 10328 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, 10329 uint_t nspares) 10330 { 10331 uint_t i; 10332 char *name; 10333 10334 if (nspares == 0) 10335 return; 10336 10337 (void) printf(gettext("\tspares\n")); 10338 10339 for (i = 0; i < nspares; i++) { 10340 name = zpool_vdev_name(g_zfs, zhp, spares[i], 10341 cb->cb_name_flags); 10342 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); 10343 free(name); 10344 } 10345 } 10346 10347 static void 10348 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, 10349 uint_t nl2cache) 10350 { 10351 uint_t i; 10352 char *name; 10353 10354 if (nl2cache == 0) 10355 return; 10356 10357 (void) printf(gettext("\tcache\n")); 10358 10359 for (i = 0; i < nl2cache; i++) { 10360 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], 10361 cb->cb_name_flags); 10362 print_status_config(zhp, cb, name, l2cache[i], 2, 10363 B_FALSE, NULL); 10364 free(name); 10365 } 10366 } 10367 10368 static void 10369 print_dedup_stats(zpool_handle_t *zhp, nvlist_t *config, boolean_t literal) 10370 { 10371 ddt_histogram_t *ddh; 10372 ddt_stat_t *dds; 10373 ddt_object_t *ddo; 10374 uint_t c; 10375 /* Extra space provided for literal display */ 10376 char dspace[32], mspace[32], cspace[32]; 10377 uint64_t cspace_prop; 10378 enum zfs_nicenum_format format; 10379 zprop_source_t src; 10380 10381 /* 10382 * If the pool was faulted then we may not have been able to 10383 * obtain the config. Otherwise, if we have anything in the dedup 10384 * table continue processing the stats. 10385 */ 10386 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 10387 (uint64_t **)&ddo, &c) != 0) 10388 return; 10389 10390 (void) printf("\n"); 10391 (void) printf(gettext(" dedup: ")); 10392 if (ddo->ddo_count == 0) { 10393 (void) printf(gettext("no DDT entries\n")); 10394 return; 10395 } 10396 10397 /* 10398 * Squash cached size into in-core size to handle race. 10399 * Only include cached size if it is available. 10400 */ 10401 cspace_prop = zpool_get_prop_int(zhp, ZPOOL_PROP_DEDUPCACHED, &src); 10402 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace); 10403 format = literal ? ZFS_NICENUM_RAW : ZFS_NICENUM_1024; 10404 zfs_nicenum_format(cspace_prop, cspace, sizeof (cspace), format); 10405 zfs_nicenum_format(ddo->ddo_dspace, dspace, sizeof (dspace), format); 10406 zfs_nicenum_format(ddo->ddo_mspace, mspace, sizeof (mspace), format); 10407 (void) printf("DDT entries %llu, size %s on disk, %s in core", 10408 (u_longlong_t)ddo->ddo_count, 10409 dspace, 10410 mspace); 10411 if (src != ZPROP_SRC_DEFAULT) { 10412 (void) printf(", %s cached (%.02f%%)", 10413 cspace, 10414 (double)cspace_prop / (double)ddo->ddo_mspace * 100.0); 10415 } 10416 (void) printf("\n"); 10417 10418 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 10419 (uint64_t **)&dds, &c) == 0); 10420 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 10421 (uint64_t **)&ddh, &c) == 0); 10422 zpool_dump_ddt(dds, ddh); 10423 } 10424 10425 #define ST_SIZE 4096 10426 #define AC_SIZE 2048 10427 10428 static void 10429 print_status_reason(zpool_handle_t *zhp, status_cbdata_t *cbp, 10430 zpool_status_t reason, zpool_errata_t errata, nvlist_t *item) 10431 { 10432 char status[ST_SIZE]; 10433 char action[AC_SIZE]; 10434 memset(status, 0, ST_SIZE); 10435 memset(action, 0, AC_SIZE); 10436 10437 switch (reason) { 10438 case ZPOOL_STATUS_MISSING_DEV_R: 10439 snprintf(status, ST_SIZE, gettext("One or more devices could " 10440 "not be opened. Sufficient replicas exist for\n\tthe pool " 10441 "to continue functioning in a degraded state.\n")); 10442 snprintf(action, AC_SIZE, gettext("Attach the missing device " 10443 "and online it using 'zpool online'.\n")); 10444 break; 10445 10446 case ZPOOL_STATUS_MISSING_DEV_NR: 10447 snprintf(status, ST_SIZE, gettext("One or more devices could " 10448 "not be opened. There are insufficient\n\treplicas for the" 10449 " pool to continue functioning.\n")); 10450 snprintf(action, AC_SIZE, gettext("Attach the missing device " 10451 "and online it using 'zpool online'.\n")); 10452 break; 10453 10454 case ZPOOL_STATUS_CORRUPT_LABEL_R: 10455 snprintf(status, ST_SIZE, gettext("One or more devices could " 10456 "not be used because the label is missing or\n\tinvalid. " 10457 "Sufficient replicas exist for the pool to continue\n\t" 10458 "functioning in a degraded state.\n")); 10459 snprintf(action, AC_SIZE, gettext("Replace the device using " 10460 "'zpool replace'.\n")); 10461 break; 10462 10463 case ZPOOL_STATUS_CORRUPT_LABEL_NR: 10464 snprintf(status, ST_SIZE, gettext("One or more devices could " 10465 "not be used because the label is missing \n\tor invalid. " 10466 "There are insufficient replicas for the pool to " 10467 "continue\n\tfunctioning.\n")); 10468 zpool_explain_recover(zpool_get_handle(zhp), 10469 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL), 10470 action, AC_SIZE); 10471 break; 10472 10473 case ZPOOL_STATUS_FAILING_DEV: 10474 snprintf(status, ST_SIZE, gettext("One or more devices has " 10475 "experienced an unrecoverable error. An\n\tattempt was " 10476 "made to correct the error. Applications are " 10477 "unaffected.\n")); 10478 snprintf(action, AC_SIZE, gettext("Determine if the " 10479 "device needs to be replaced, and clear the errors\n\tusing" 10480 " 'zpool clear' or replace the device with 'zpool " 10481 "replace'.\n")); 10482 break; 10483 10484 case ZPOOL_STATUS_OFFLINE_DEV: 10485 snprintf(status, ST_SIZE, gettext("One or more devices has " 10486 "been taken offline by the administrator.\n\tSufficient " 10487 "replicas exist for the pool to continue functioning in " 10488 "a\n\tdegraded state.\n")); 10489 snprintf(action, AC_SIZE, gettext("Online the device " 10490 "using 'zpool online' or replace the device with\n\t'zpool " 10491 "replace'.\n")); 10492 break; 10493 10494 case ZPOOL_STATUS_REMOVED_DEV: 10495 snprintf(status, ST_SIZE, gettext("One or more devices have " 10496 "been removed.\n\tSufficient replicas exist for the pool " 10497 "to continue functioning in a\n\tdegraded state.\n")); 10498 snprintf(action, AC_SIZE, gettext("Online the device " 10499 "using zpool online' or replace the device with\n\t'zpool " 10500 "replace'.\n")); 10501 break; 10502 10503 case ZPOOL_STATUS_RESILVERING: 10504 case ZPOOL_STATUS_REBUILDING: 10505 snprintf(status, ST_SIZE, gettext("One or more devices is " 10506 "currently being resilvered. The pool will\n\tcontinue " 10507 "to function, possibly in a degraded state.\n")); 10508 snprintf(action, AC_SIZE, gettext("Wait for the resilver to " 10509 "complete.\n")); 10510 break; 10511 10512 case ZPOOL_STATUS_REBUILD_SCRUB: 10513 snprintf(status, ST_SIZE, gettext("One or more devices have " 10514 "been sequentially resilvered, scrubbing\n\tthe pool " 10515 "is recommended.\n")); 10516 snprintf(action, AC_SIZE, gettext("Use 'zpool scrub' to " 10517 "verify all data checksums.\n")); 10518 break; 10519 10520 case ZPOOL_STATUS_CORRUPT_DATA: 10521 snprintf(status, ST_SIZE, gettext("One or more devices has " 10522 "experienced an error resulting in data\n\tcorruption. " 10523 "Applications may be affected.\n")); 10524 snprintf(action, AC_SIZE, gettext("Restore the file in question" 10525 " if possible. Otherwise restore the\n\tentire pool from " 10526 "backup.\n")); 10527 break; 10528 10529 case ZPOOL_STATUS_CORRUPT_POOL: 10530 snprintf(status, ST_SIZE, gettext("The pool metadata is " 10531 "corrupted and the pool cannot be opened.\n")); 10532 zpool_explain_recover(zpool_get_handle(zhp), 10533 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL), 10534 action, AC_SIZE); 10535 break; 10536 10537 case ZPOOL_STATUS_VERSION_OLDER: 10538 snprintf(status, ST_SIZE, gettext("The pool is formatted using " 10539 "a legacy on-disk format. The pool can\n\tstill be used, " 10540 "but some features are unavailable.\n")); 10541 snprintf(action, AC_SIZE, gettext("Upgrade the pool using " 10542 "'zpool upgrade'. Once this is done, the\n\tpool will no " 10543 "longer be accessible on software that does not support\n\t" 10544 "feature flags.\n")); 10545 break; 10546 10547 case ZPOOL_STATUS_VERSION_NEWER: 10548 snprintf(status, ST_SIZE, gettext("The pool has been upgraded " 10549 "to a newer, incompatible on-disk version.\n\tThe pool " 10550 "cannot be accessed on this system.\n")); 10551 snprintf(action, AC_SIZE, gettext("Access the pool from a " 10552 "system running more recent software, or\n\trestore the " 10553 "pool from backup.\n")); 10554 break; 10555 10556 case ZPOOL_STATUS_FEAT_DISABLED: 10557 snprintf(status, ST_SIZE, gettext("Some supported and " 10558 "requested features are not enabled on the pool.\n\t" 10559 "The pool can still be used, but some features are " 10560 "unavailable.\n")); 10561 snprintf(action, AC_SIZE, gettext("Enable all features using " 10562 "'zpool upgrade'. Once this is done,\n\tthe pool may no " 10563 "longer be accessible by software that does not support\n\t" 10564 "the features. See zpool-features(7) for details.\n")); 10565 break; 10566 10567 case ZPOOL_STATUS_COMPATIBILITY_ERR: 10568 snprintf(status, ST_SIZE, gettext("This pool has a " 10569 "compatibility list specified, but it could not be\n\t" 10570 "read/parsed at this time. The pool can still be used, " 10571 "but this\n\tshould be investigated.\n")); 10572 snprintf(action, AC_SIZE, gettext("Check the value of the " 10573 "'compatibility' property against the\n\t" 10574 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " 10575 ZPOOL_DATA_COMPAT_D ".\n")); 10576 break; 10577 10578 case ZPOOL_STATUS_INCOMPATIBLE_FEAT: 10579 snprintf(status, ST_SIZE, gettext("One or more features " 10580 "are enabled on the pool despite not being\n\t" 10581 "requested by the 'compatibility' property.\n")); 10582 snprintf(action, AC_SIZE, gettext("Consider setting " 10583 "'compatibility' to an appropriate value, or\n\t" 10584 "adding needed features to the relevant file in\n\t" 10585 ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n")); 10586 break; 10587 10588 case ZPOOL_STATUS_UNSUP_FEAT_READ: 10589 snprintf(status, ST_SIZE, gettext("The pool cannot be accessed " 10590 "on this system because it uses the\n\tfollowing feature(s)" 10591 " not supported on this system:\n")); 10592 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status, 10593 1024); 10594 snprintf(action, AC_SIZE, gettext("Access the pool from a " 10595 "system that supports the required feature(s),\n\tor " 10596 "restore the pool from backup.\n")); 10597 break; 10598 10599 case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 10600 snprintf(status, ST_SIZE, gettext("The pool can only be " 10601 "accessed in read-only mode on this system. It\n\tcannot be" 10602 " accessed in read-write mode because it uses the " 10603 "following\n\tfeature(s) not supported on this system:\n")); 10604 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status, 10605 1024); 10606 snprintf(action, AC_SIZE, gettext("The pool cannot be accessed " 10607 "in read-write mode. Import the pool with\n" 10608 "\t\"-o readonly=on\", access the pool from a system that " 10609 "supports the\n\trequired feature(s), or restore the " 10610 "pool from backup.\n")); 10611 break; 10612 10613 case ZPOOL_STATUS_FAULTED_DEV_R: 10614 snprintf(status, ST_SIZE, gettext("One or more devices are " 10615 "faulted in response to persistent errors.\n\tSufficient " 10616 "replicas exist for the pool to continue functioning " 10617 "in a\n\tdegraded state.\n")); 10618 snprintf(action, AC_SIZE, gettext("Replace the faulted device, " 10619 "or use 'zpool clear' to mark the device\n\trepaired.\n")); 10620 break; 10621 10622 case ZPOOL_STATUS_FAULTED_DEV_NR: 10623 snprintf(status, ST_SIZE, gettext("One or more devices are " 10624 "faulted in response to persistent errors. There are " 10625 "insufficient replicas for the pool to\n\tcontinue " 10626 "functioning.\n")); 10627 snprintf(action, AC_SIZE, gettext("Destroy and re-create the " 10628 "pool from a backup source. Manually marking the device\n" 10629 "\trepaired using 'zpool clear' may allow some data " 10630 "to be recovered.\n")); 10631 break; 10632 10633 case ZPOOL_STATUS_IO_FAILURE_MMP: 10634 snprintf(status, ST_SIZE, gettext("The pool is suspended " 10635 "because multihost writes failed or were delayed;\n\t" 10636 "another system could import the pool undetected.\n")); 10637 snprintf(action, AC_SIZE, gettext("Make sure the pool's devices" 10638 " are connected, then reboot your system and\n\timport the " 10639 "pool or run 'zpool clear' to resume the pool.\n")); 10640 break; 10641 10642 case ZPOOL_STATUS_IO_FAILURE_WAIT: 10643 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 10644 snprintf(status, ST_SIZE, gettext("One or more devices are " 10645 "faulted in response to IO failures.\n")); 10646 snprintf(action, AC_SIZE, gettext("Make sure the affected " 10647 "devices are connected, then run 'zpool clear'.\n")); 10648 break; 10649 10650 case ZPOOL_STATUS_BAD_LOG: 10651 snprintf(status, ST_SIZE, gettext("An intent log record " 10652 "could not be read.\n" 10653 "\tWaiting for administrator intervention to fix the " 10654 "faulted pool.\n")); 10655 snprintf(action, AC_SIZE, gettext("Either restore the affected " 10656 "device(s) and run 'zpool online',\n" 10657 "\tor ignore the intent log records by running " 10658 "'zpool clear'.\n")); 10659 break; 10660 10661 case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 10662 snprintf(status, ST_SIZE, gettext("One or more devices are " 10663 "configured to use a non-native block size.\n" 10664 "\tExpect reduced performance.\n")); 10665 snprintf(action, AC_SIZE, gettext("Replace affected devices " 10666 "with devices that support the\n\tconfigured block size, " 10667 "or migrate data to a properly configured\n\tpool.\n")); 10668 break; 10669 10670 case ZPOOL_STATUS_HOSTID_MISMATCH: 10671 snprintf(status, ST_SIZE, gettext("Mismatch between pool hostid" 10672 " and system hostid on imported pool.\n\tThis pool was " 10673 "previously imported into a system with a different " 10674 "hostid,\n\tand then was verbatim imported into this " 10675 "system.\n")); 10676 snprintf(action, AC_SIZE, gettext("Export this pool on all " 10677 "systems on which it is imported.\n" 10678 "\tThen import it to correct the mismatch.\n")); 10679 break; 10680 10681 case ZPOOL_STATUS_ERRATA: 10682 snprintf(status, ST_SIZE, gettext("Errata #%d detected.\n"), 10683 errata); 10684 switch (errata) { 10685 case ZPOOL_ERRATA_NONE: 10686 break; 10687 10688 case ZPOOL_ERRATA_ZOL_2094_SCRUB: 10689 snprintf(action, AC_SIZE, gettext("To correct the issue" 10690 " run 'zpool scrub'.\n")); 10691 break; 10692 10693 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: 10694 (void) strlcat(status, gettext("\tExisting encrypted " 10695 "datasets contain an on-disk incompatibility\n\t " 10696 "which needs to be corrected.\n"), ST_SIZE); 10697 snprintf(action, AC_SIZE, gettext("To correct the issue" 10698 " backup existing encrypted datasets to new\n\t" 10699 "encrypted datasets and destroy the old ones. " 10700 "'zfs mount -o ro' can\n\tbe used to temporarily " 10701 "mount existing encrypted datasets readonly.\n")); 10702 break; 10703 10704 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: 10705 (void) strlcat(status, gettext("\tExisting encrypted " 10706 "snapshots and bookmarks contain an on-disk\n\t" 10707 "incompatibility. This may cause on-disk " 10708 "corruption if they are used\n\twith " 10709 "'zfs recv'.\n"), ST_SIZE); 10710 snprintf(action, AC_SIZE, gettext("To correct the" 10711 "issue, enable the bookmark_v2 feature. No " 10712 "additional\n\taction is needed if there are no " 10713 "encrypted snapshots or bookmarks.\n\tIf preserving" 10714 "the encrypted snapshots and bookmarks is required," 10715 " use\n\ta non-raw send to backup and restore them." 10716 " Alternately, they may be\n\tremoved to resolve " 10717 "the incompatibility.\n")); 10718 break; 10719 10720 default: 10721 /* 10722 * All errata which allow the pool to be imported 10723 * must contain an action message. 10724 */ 10725 assert(0); 10726 } 10727 break; 10728 10729 default: 10730 /* 10731 * The remaining errors can't actually be generated, yet. 10732 */ 10733 assert(reason == ZPOOL_STATUS_OK); 10734 } 10735 10736 if (status[0] != 0) { 10737 if (cbp->cb_json) 10738 fnvlist_add_string(item, "status", status); 10739 else { 10740 printf_color(ANSI_BOLD, gettext("status: ")); 10741 printf_color(ANSI_YELLOW, status); 10742 } 10743 } 10744 10745 if (action[0] != 0) { 10746 if (cbp->cb_json) 10747 fnvlist_add_string(item, "action", action); 10748 else { 10749 printf_color(ANSI_BOLD, gettext("action: ")); 10750 printf_color(ANSI_YELLOW, action); 10751 } 10752 } 10753 } 10754 10755 static int 10756 status_callback_json(zpool_handle_t *zhp, void *data) 10757 { 10758 status_cbdata_t *cbp = data; 10759 nvlist_t *config, *nvroot; 10760 const char *msgid; 10761 char pool_guid[256]; 10762 char msgbuf[256]; 10763 uint64_t guid; 10764 zpool_status_t reason; 10765 zpool_errata_t errata; 10766 uint_t c; 10767 vdev_stat_t *vs; 10768 nvlist_t *item, *d, *load_info, *vds; 10769 10770 /* If dedup stats were requested, also fetch dedupcached. */ 10771 if (cbp->cb_dedup_stats > 1) 10772 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME); 10773 reason = zpool_get_status(zhp, &msgid, &errata); 10774 /* 10775 * If we were given 'zpool status -x', only report those pools with 10776 * problems. 10777 */ 10778 if (cbp->cb_explain && 10779 (reason == ZPOOL_STATUS_OK || 10780 reason == ZPOOL_STATUS_VERSION_OLDER || 10781 reason == ZPOOL_STATUS_FEAT_DISABLED || 10782 reason == ZPOOL_STATUS_COMPATIBILITY_ERR || 10783 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { 10784 return (0); 10785 } 10786 10787 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools"); 10788 item = fnvlist_alloc(); 10789 vds = fnvlist_alloc(); 10790 fill_pool_info(item, zhp, B_FALSE, cbp->cb_json_as_int); 10791 config = zpool_get_config(zhp, NULL); 10792 10793 if (config != NULL) { 10794 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10795 verify(nvlist_lookup_uint64_array(nvroot, 10796 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &c) == 0); 10797 if (cbp->cb_json_pool_key_guid) { 10798 guid = fnvlist_lookup_uint64(config, 10799 ZPOOL_CONFIG_POOL_GUID); 10800 snprintf(pool_guid, 256, "%llu", (u_longlong_t)guid); 10801 } 10802 cbp->cb_count++; 10803 10804 print_status_reason(zhp, cbp, reason, errata, item); 10805 if (msgid != NULL) { 10806 snprintf(msgbuf, 256, 10807 "https://openzfs.github.io/openzfs-docs/msg/%s", 10808 msgid); 10809 fnvlist_add_string(item, "msgid", msgid); 10810 fnvlist_add_string(item, "moreinfo", msgbuf); 10811 } 10812 10813 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 10814 &load_info) == 0) { 10815 fnvlist_add_nvlist(item, ZPOOL_CONFIG_LOAD_INFO, 10816 load_info); 10817 } 10818 10819 scan_status_nvlist(zhp, cbp, nvroot, item); 10820 removal_status_nvlist(zhp, cbp, nvroot, item); 10821 checkpoint_status_nvlist(nvroot, cbp, item); 10822 raidz_expand_status_nvlist(zhp, cbp, nvroot, item); 10823 vdev_stats_nvlist(zhp, cbp, nvroot, 0, B_FALSE, NULL, vds); 10824 if (cbp->cb_flat_vdevs) { 10825 class_vdevs_nvlist(zhp, cbp, nvroot, 10826 VDEV_ALLOC_BIAS_DEDUP, vds); 10827 class_vdevs_nvlist(zhp, cbp, nvroot, 10828 VDEV_ALLOC_BIAS_SPECIAL, vds); 10829 class_vdevs_nvlist(zhp, cbp, nvroot, 10830 VDEV_ALLOC_CLASS_LOGS, vds); 10831 l2cache_nvlist(zhp, cbp, nvroot, vds); 10832 spares_nvlist(zhp, cbp, nvroot, vds); 10833 10834 fnvlist_add_nvlist(item, "vdevs", vds); 10835 fnvlist_free(vds); 10836 } else { 10837 fnvlist_add_nvlist(item, "vdevs", vds); 10838 fnvlist_free(vds); 10839 10840 class_vdevs_nvlist(zhp, cbp, nvroot, 10841 VDEV_ALLOC_BIAS_DEDUP, item); 10842 class_vdevs_nvlist(zhp, cbp, nvroot, 10843 VDEV_ALLOC_BIAS_SPECIAL, item); 10844 class_vdevs_nvlist(zhp, cbp, nvroot, 10845 VDEV_ALLOC_CLASS_LOGS, item); 10846 l2cache_nvlist(zhp, cbp, nvroot, item); 10847 spares_nvlist(zhp, cbp, nvroot, item); 10848 } 10849 dedup_stats_nvlist(zhp, cbp, item); 10850 errors_nvlist(zhp, cbp, item); 10851 } 10852 if (cbp->cb_json_pool_key_guid) { 10853 fnvlist_add_nvlist(d, pool_guid, item); 10854 } else { 10855 fnvlist_add_nvlist(d, zpool_get_name(zhp), 10856 item); 10857 } 10858 fnvlist_free(item); 10859 return (0); 10860 } 10861 10862 /* 10863 * Display a summary of pool status. Displays a summary such as: 10864 * 10865 * pool: tank 10866 * status: DEGRADED 10867 * reason: One or more devices ... 10868 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 10869 * config: 10870 * mirror DEGRADED 10871 * c1t0d0 OK 10872 * c2t0d0 UNAVAIL 10873 * 10874 * When given the '-v' option, we print out the complete config. If the '-e' 10875 * option is specified, then we print out error rate information as well. 10876 */ 10877 static int 10878 status_callback(zpool_handle_t *zhp, void *data) 10879 { 10880 status_cbdata_t *cbp = data; 10881 nvlist_t *config, *nvroot; 10882 const char *msgid; 10883 zpool_status_t reason; 10884 zpool_errata_t errata; 10885 const char *health; 10886 uint_t c; 10887 vdev_stat_t *vs; 10888 10889 /* If dedup stats were requested, also fetch dedupcached. */ 10890 if (cbp->cb_dedup_stats > 1) 10891 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME); 10892 10893 config = zpool_get_config(zhp, NULL); 10894 reason = zpool_get_status(zhp, &msgid, &errata); 10895 10896 cbp->cb_count++; 10897 10898 /* 10899 * If we were given 'zpool status -x', only report those pools with 10900 * problems. 10901 */ 10902 if (cbp->cb_explain && 10903 (reason == ZPOOL_STATUS_OK || 10904 reason == ZPOOL_STATUS_VERSION_OLDER || 10905 reason == ZPOOL_STATUS_FEAT_DISABLED || 10906 reason == ZPOOL_STATUS_COMPATIBILITY_ERR || 10907 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { 10908 if (!cbp->cb_allpools) { 10909 (void) printf(gettext("pool '%s' is healthy\n"), 10910 zpool_get_name(zhp)); 10911 if (cbp->cb_first) 10912 cbp->cb_first = B_FALSE; 10913 } 10914 return (0); 10915 } 10916 10917 if (cbp->cb_first) 10918 cbp->cb_first = B_FALSE; 10919 else 10920 (void) printf("\n"); 10921 10922 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 10923 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 10924 (uint64_t **)&vs, &c) == 0); 10925 10926 health = zpool_get_state_str(zhp); 10927 10928 printf(" "); 10929 printf_color(ANSI_BOLD, gettext("pool:")); 10930 printf(" %s\n", zpool_get_name(zhp)); 10931 fputc(' ', stdout); 10932 printf_color(ANSI_BOLD, gettext("state: ")); 10933 10934 printf_color(health_str_to_color(health), "%s", health); 10935 10936 fputc('\n', stdout); 10937 print_status_reason(zhp, cbp, reason, errata, NULL); 10938 10939 if (msgid != NULL) { 10940 printf(" "); 10941 printf_color(ANSI_BOLD, gettext("see:")); 10942 printf(gettext( 10943 " https://openzfs.github.io/openzfs-docs/msg/%s\n"), 10944 msgid); 10945 } 10946 10947 if (config != NULL) { 10948 uint64_t nerr; 10949 nvlist_t **spares, **l2cache; 10950 uint_t nspares, nl2cache; 10951 10952 print_scan_status(zhp, nvroot); 10953 10954 pool_removal_stat_t *prs = NULL; 10955 (void) nvlist_lookup_uint64_array(nvroot, 10956 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 10957 print_removal_status(zhp, prs); 10958 10959 pool_checkpoint_stat_t *pcs = NULL; 10960 (void) nvlist_lookup_uint64_array(nvroot, 10961 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 10962 print_checkpoint_status(pcs); 10963 10964 pool_raidz_expand_stat_t *pres = NULL; 10965 (void) nvlist_lookup_uint64_array(nvroot, 10966 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); 10967 print_raidz_expand_status(zhp, pres); 10968 10969 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, 10970 cbp->cb_name_flags | VDEV_NAME_TYPE_ID); 10971 if (cbp->cb_namewidth < 10) 10972 cbp->cb_namewidth = 10; 10973 10974 color_start(ANSI_BOLD); 10975 (void) printf(gettext("config:\n\n")); 10976 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), 10977 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", 10978 "CKSUM"); 10979 color_end(); 10980 10981 if (cbp->cb_print_slow_ios) { 10982 printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); 10983 } 10984 10985 if (cbp->cb_print_power) { 10986 printf_color(ANSI_BOLD, " %5s", gettext("POWER")); 10987 } 10988 10989 if (cbp->cb_print_dio_verify) { 10990 printf_color(ANSI_BOLD, " %5s", gettext("DIO")); 10991 } 10992 10993 if (cbp->vcdl != NULL) 10994 print_cmd_columns(cbp->vcdl, 0); 10995 10996 printf("\n"); 10997 10998 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, 10999 B_FALSE, NULL); 11000 11001 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); 11002 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); 11003 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); 11004 11005 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 11006 &l2cache, &nl2cache) == 0) 11007 print_l2cache(zhp, cbp, l2cache, nl2cache); 11008 11009 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 11010 &spares, &nspares) == 0) 11011 print_spares(zhp, cbp, spares, nspares); 11012 11013 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 11014 &nerr) == 0) { 11015 (void) printf("\n"); 11016 if (nerr == 0) { 11017 (void) printf(gettext( 11018 "errors: No known data errors\n")); 11019 } else if (!cbp->cb_verbose) { 11020 color_start(ANSI_RED); 11021 (void) printf(gettext("errors: %llu data " 11022 "errors, use '-v' for a list\n"), 11023 (u_longlong_t)nerr); 11024 color_end(); 11025 } else { 11026 print_error_log(zhp); 11027 } 11028 } 11029 11030 if (cbp->cb_dedup_stats) 11031 print_dedup_stats(zhp, config, cbp->cb_literal); 11032 } else { 11033 (void) printf(gettext("config: The configuration cannot be " 11034 "determined.\n")); 11035 } 11036 11037 return (0); 11038 } 11039 11040 /* 11041 * zpool status [-dDegiLpPstvx] [-c [script1,script2,...]] ... 11042 * [-j|--json [--json-flat-vdevs] [--json-int] ... 11043 * [--json-pool-key-guid]] [--power] [-T d|u] ... 11044 * [pool] [interval [count]] 11045 * 11046 * -c CMD For each vdev, run command CMD 11047 * -D Display dedup status (undocumented) 11048 * -d Display Direct I/O write verify errors 11049 * -e Display only unhealthy vdevs 11050 * -g Display guid for individual vdev name. 11051 * -i Display vdev initialization status. 11052 * -j [...] Display output in JSON format 11053 * --json-flat-vdevs Display vdevs in flat hierarchy 11054 * --json-int Display numbers in integer format instead of string 11055 * --json-pool-key-guid Use pool GUID as key for pool objects 11056 * -L Follow links when resolving vdev path name. 11057 * -P Display full path for vdev name. 11058 * -p Display values in parsable (exact) format. 11059 * --power Display vdev enclosure slot power status 11060 * -s Display slow IOs column. 11061 * -T Display a timestamp in date(1) or Unix format 11062 * -t Display vdev TRIM status. 11063 * -v Display complete error logs 11064 * -x Display only pools with potential problems 11065 * 11066 * Describes the health status of all pools or some subset. 11067 */ 11068 int 11069 zpool_do_status(int argc, char **argv) 11070 { 11071 int c; 11072 int ret; 11073 float interval = 0; 11074 unsigned long count = 0; 11075 status_cbdata_t cb = { 0 }; 11076 nvlist_t *data; 11077 char *cmd = NULL; 11078 11079 struct option long_options[] = { 11080 {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, 11081 {"json", no_argument, NULL, 'j'}, 11082 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 11083 {"json-flat-vdevs", no_argument, NULL, 11084 ZPOOL_OPTION_JSON_FLAT_VDEVS}, 11085 {"json-pool-key-guid", no_argument, NULL, 11086 ZPOOL_OPTION_POOL_KEY_GUID}, 11087 {0, 0, 0, 0} 11088 }; 11089 11090 /* check options */ 11091 while ((c = getopt_long(argc, argv, "c:jdDegiLpPstT:vx", long_options, 11092 NULL)) != -1) { 11093 switch (c) { 11094 case 'c': 11095 if (cmd != NULL) { 11096 fprintf(stderr, 11097 gettext("Can't set -c flag twice\n")); 11098 exit(1); 11099 } 11100 11101 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && 11102 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { 11103 fprintf(stderr, gettext( 11104 "Can't run -c, disabled by " 11105 "ZPOOL_SCRIPTS_ENABLED.\n")); 11106 exit(1); 11107 } 11108 11109 if ((getuid() <= 0 || geteuid() <= 0) && 11110 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { 11111 fprintf(stderr, gettext( 11112 "Can't run -c with root privileges " 11113 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); 11114 exit(1); 11115 } 11116 cmd = optarg; 11117 break; 11118 case 'd': 11119 cb.cb_print_dio_verify = B_TRUE; 11120 break; 11121 case 'D': 11122 if (++cb.cb_dedup_stats > 2) 11123 cb.cb_dedup_stats = 2; 11124 break; 11125 case 'e': 11126 cb.cb_print_unhealthy = B_TRUE; 11127 break; 11128 case 'g': 11129 cb.cb_name_flags |= VDEV_NAME_GUID; 11130 break; 11131 case 'i': 11132 cb.cb_print_vdev_init = B_TRUE; 11133 break; 11134 case 'L': 11135 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; 11136 break; 11137 case 'p': 11138 cb.cb_literal = B_TRUE; 11139 break; 11140 case 'P': 11141 cb.cb_name_flags |= VDEV_NAME_PATH; 11142 break; 11143 case 's': 11144 cb.cb_print_slow_ios = B_TRUE; 11145 break; 11146 case 't': 11147 cb.cb_print_vdev_trim = B_TRUE; 11148 break; 11149 case 'T': 11150 get_timestamp_arg(*optarg); 11151 break; 11152 case 'v': 11153 cb.cb_verbose = B_TRUE; 11154 break; 11155 case 'j': 11156 cb.cb_json = B_TRUE; 11157 break; 11158 case 'x': 11159 cb.cb_explain = B_TRUE; 11160 break; 11161 case ZPOOL_OPTION_POWER: 11162 cb.cb_print_power = B_TRUE; 11163 break; 11164 case ZPOOL_OPTION_JSON_FLAT_VDEVS: 11165 cb.cb_flat_vdevs = B_TRUE; 11166 break; 11167 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 11168 cb.cb_json_as_int = B_TRUE; 11169 cb.cb_literal = B_TRUE; 11170 break; 11171 case ZPOOL_OPTION_POOL_KEY_GUID: 11172 cb.cb_json_pool_key_guid = B_TRUE; 11173 break; 11174 case '?': 11175 if (optopt == 'c') { 11176 print_zpool_script_list("status"); 11177 exit(0); 11178 } else { 11179 fprintf(stderr, 11180 gettext("invalid option '%c'\n"), optopt); 11181 } 11182 usage(B_FALSE); 11183 } 11184 } 11185 11186 argc -= optind; 11187 argv += optind; 11188 11189 get_interval_count(&argc, argv, &interval, &count); 11190 11191 if (argc == 0) 11192 cb.cb_allpools = B_TRUE; 11193 11194 cb.cb_first = B_TRUE; 11195 cb.cb_print_status = B_TRUE; 11196 11197 if (cb.cb_flat_vdevs && !cb.cb_json) { 11198 fprintf(stderr, gettext("'--json-flat-vdevs' only works with" 11199 " '-j' option\n")); 11200 usage(B_FALSE); 11201 } 11202 11203 if (cb.cb_json_as_int && !cb.cb_json) { 11204 (void) fprintf(stderr, gettext("'--json-int' only works with" 11205 " '-j' option\n")); 11206 usage(B_FALSE); 11207 } 11208 11209 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 11210 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 11211 " works with '-j' option\n")); 11212 usage(B_FALSE); 11213 } 11214 11215 for (;;) { 11216 if (cb.cb_json) { 11217 cb.cb_jsobj = zpool_json_schema(0, 1); 11218 data = fnvlist_alloc(); 11219 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 11220 fnvlist_free(data); 11221 } 11222 11223 if (timestamp_fmt != NODATE) { 11224 if (cb.cb_json) { 11225 if (cb.cb_json_as_int) { 11226 fnvlist_add_uint64(cb.cb_jsobj, "time", 11227 time(NULL)); 11228 } else { 11229 char ts[128]; 11230 get_timestamp(timestamp_fmt, ts, 128); 11231 fnvlist_add_string(cb.cb_jsobj, "time", 11232 ts); 11233 } 11234 } else 11235 print_timestamp(timestamp_fmt); 11236 } 11237 11238 if (cmd != NULL) 11239 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, 11240 NULL, NULL, 0, 0); 11241 11242 if (cb.cb_json) { 11243 ret = for_each_pool(argc, argv, B_TRUE, NULL, 11244 ZFS_TYPE_POOL, cb.cb_literal, 11245 status_callback_json, &cb); 11246 } else { 11247 ret = for_each_pool(argc, argv, B_TRUE, NULL, 11248 ZFS_TYPE_POOL, cb.cb_literal, 11249 status_callback, &cb); 11250 } 11251 11252 if (cb.vcdl != NULL) 11253 free_vdev_cmd_data_list(cb.vcdl); 11254 11255 if (cb.cb_json) { 11256 if (ret == 0) 11257 zcmd_print_json(cb.cb_jsobj); 11258 else 11259 nvlist_free(cb.cb_jsobj); 11260 } else { 11261 if (argc == 0 && cb.cb_count == 0) { 11262 (void) fprintf(stderr, "%s", 11263 gettext("no pools available\n")); 11264 } else if (cb.cb_explain && cb.cb_first && 11265 cb.cb_allpools) { 11266 (void) printf("%s", 11267 gettext("all pools are healthy\n")); 11268 } 11269 } 11270 11271 if (ret != 0) 11272 return (ret); 11273 11274 if (interval == 0) 11275 break; 11276 11277 if (count != 0 && --count == 0) 11278 break; 11279 11280 (void) fflush(stdout); 11281 (void) fsleep(interval); 11282 } 11283 11284 return (0); 11285 } 11286 11287 typedef struct upgrade_cbdata { 11288 int cb_first; 11289 int cb_argc; 11290 uint64_t cb_version; 11291 char **cb_argv; 11292 } upgrade_cbdata_t; 11293 11294 static int 11295 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) 11296 { 11297 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 11298 int *count = (int *)unsupp_fs; 11299 11300 if (zfs_version > ZPL_VERSION) { 11301 (void) printf(gettext("%s (v%d) is not supported by this " 11302 "implementation of ZFS.\n"), 11303 zfs_get_name(zhp), zfs_version); 11304 (*count)++; 11305 } 11306 11307 zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs); 11308 11309 zfs_close(zhp); 11310 11311 return (0); 11312 } 11313 11314 static int 11315 upgrade_version(zpool_handle_t *zhp, uint64_t version) 11316 { 11317 int ret; 11318 nvlist_t *config; 11319 uint64_t oldversion; 11320 int unsupp_fs = 0; 11321 11322 config = zpool_get_config(zhp, NULL); 11323 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11324 &oldversion) == 0); 11325 11326 char compat[ZFS_MAXPROPLEN]; 11327 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 11328 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 11329 compat[0] = '\0'; 11330 11331 assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 11332 assert(oldversion < version); 11333 11334 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); 11335 if (ret != 0) 11336 return (ret); 11337 11338 if (unsupp_fs) { 11339 (void) fprintf(stderr, gettext("Upgrade not performed due " 11340 "to %d unsupported filesystems (max v%d).\n"), 11341 unsupp_fs, (int)ZPL_VERSION); 11342 return (1); 11343 } 11344 11345 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) { 11346 (void) fprintf(stderr, gettext("Upgrade not performed because " 11347 "'compatibility' property set to '" 11348 ZPOOL_COMPAT_LEGACY "'.\n")); 11349 return (1); 11350 } 11351 11352 ret = zpool_upgrade(zhp, version); 11353 if (ret != 0) 11354 return (ret); 11355 11356 if (version >= SPA_VERSION_FEATURES) { 11357 (void) printf(gettext("Successfully upgraded " 11358 "'%s' from version %llu to feature flags.\n"), 11359 zpool_get_name(zhp), (u_longlong_t)oldversion); 11360 } else { 11361 (void) printf(gettext("Successfully upgraded " 11362 "'%s' from version %llu to version %llu.\n"), 11363 zpool_get_name(zhp), (u_longlong_t)oldversion, 11364 (u_longlong_t)version); 11365 } 11366 11367 return (0); 11368 } 11369 11370 static int 11371 upgrade_enable_all(zpool_handle_t *zhp, int *countp) 11372 { 11373 int i, ret, count; 11374 boolean_t firstff = B_TRUE; 11375 nvlist_t *enabled = zpool_get_features(zhp); 11376 11377 char compat[ZFS_MAXPROPLEN]; 11378 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, 11379 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 11380 compat[0] = '\0'; 11381 11382 boolean_t requested_features[SPA_FEATURES]; 11383 if (zpool_do_load_compat(compat, requested_features) != 11384 ZPOOL_COMPATIBILITY_OK) 11385 return (-1); 11386 11387 count = 0; 11388 for (i = 0; i < SPA_FEATURES; i++) { 11389 const char *fname = spa_feature_table[i].fi_uname; 11390 const char *fguid = spa_feature_table[i].fi_guid; 11391 11392 if (!spa_feature_table[i].fi_zfs_mod_supported || 11393 (spa_feature_table[i].fi_flags & ZFEATURE_FLAG_NO_UPGRADE)) 11394 continue; 11395 11396 if (!nvlist_exists(enabled, fguid) && requested_features[i]) { 11397 char *propname; 11398 verify(-1 != asprintf(&propname, "feature@%s", fname)); 11399 ret = zpool_set_prop(zhp, propname, 11400 ZFS_FEATURE_ENABLED); 11401 if (ret != 0) { 11402 free(propname); 11403 return (ret); 11404 } 11405 count++; 11406 11407 if (firstff) { 11408 (void) printf(gettext("Enabled the " 11409 "following features on '%s':\n"), 11410 zpool_get_name(zhp)); 11411 firstff = B_FALSE; 11412 } 11413 (void) printf(gettext(" %s\n"), fname); 11414 free(propname); 11415 } 11416 } 11417 11418 if (countp != NULL) 11419 *countp = count; 11420 return (0); 11421 } 11422 11423 static int 11424 upgrade_cb(zpool_handle_t *zhp, void *arg) 11425 { 11426 upgrade_cbdata_t *cbp = arg; 11427 nvlist_t *config; 11428 uint64_t version; 11429 boolean_t modified_pool = B_FALSE; 11430 int ret; 11431 11432 config = zpool_get_config(zhp, NULL); 11433 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11434 &version) == 0); 11435 11436 assert(SPA_VERSION_IS_SUPPORTED(version)); 11437 11438 if (version < cbp->cb_version) { 11439 cbp->cb_first = B_FALSE; 11440 ret = upgrade_version(zhp, cbp->cb_version); 11441 if (ret != 0) 11442 return (ret); 11443 modified_pool = B_TRUE; 11444 11445 /* 11446 * If they did "zpool upgrade -a", then we could 11447 * be doing ioctls to different pools. We need 11448 * to log this history once to each pool, and bypass 11449 * the normal history logging that happens in main(). 11450 */ 11451 (void) zpool_log_history(g_zfs, history_str); 11452 log_history = B_FALSE; 11453 } 11454 11455 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 11456 int count; 11457 ret = upgrade_enable_all(zhp, &count); 11458 if (ret != 0) 11459 return (ret); 11460 11461 if (count > 0) { 11462 cbp->cb_first = B_FALSE; 11463 modified_pool = B_TRUE; 11464 } 11465 } 11466 11467 if (modified_pool) { 11468 (void) printf("\n"); 11469 (void) after_zpool_upgrade(zhp); 11470 } 11471 11472 return (0); 11473 } 11474 11475 static int 11476 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 11477 { 11478 upgrade_cbdata_t *cbp = arg; 11479 nvlist_t *config; 11480 uint64_t version; 11481 11482 config = zpool_get_config(zhp, NULL); 11483 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11484 &version) == 0); 11485 11486 assert(SPA_VERSION_IS_SUPPORTED(version)); 11487 11488 if (version < SPA_VERSION_FEATURES) { 11489 if (cbp->cb_first) { 11490 (void) printf(gettext("The following pools are " 11491 "formatted with legacy version numbers and can\n" 11492 "be upgraded to use feature flags. After " 11493 "being upgraded, these pools\nwill no " 11494 "longer be accessible by software that does not " 11495 "support feature\nflags.\n\n" 11496 "Note that setting a pool's 'compatibility' " 11497 "feature to '" ZPOOL_COMPAT_LEGACY "' will\n" 11498 "inhibit upgrades.\n\n")); 11499 (void) printf(gettext("VER POOL\n")); 11500 (void) printf(gettext("--- ------------\n")); 11501 cbp->cb_first = B_FALSE; 11502 } 11503 11504 (void) printf("%2llu %s\n", (u_longlong_t)version, 11505 zpool_get_name(zhp)); 11506 } 11507 11508 return (0); 11509 } 11510 11511 static int 11512 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 11513 { 11514 upgrade_cbdata_t *cbp = arg; 11515 nvlist_t *config; 11516 uint64_t version; 11517 11518 config = zpool_get_config(zhp, NULL); 11519 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11520 &version) == 0); 11521 11522 if (version >= SPA_VERSION_FEATURES) { 11523 int i; 11524 boolean_t poolfirst = B_TRUE; 11525 nvlist_t *enabled = zpool_get_features(zhp); 11526 11527 for (i = 0; i < SPA_FEATURES; i++) { 11528 const char *fguid = spa_feature_table[i].fi_guid; 11529 const char *fname = spa_feature_table[i].fi_uname; 11530 11531 if (!spa_feature_table[i].fi_zfs_mod_supported) 11532 continue; 11533 11534 if (!nvlist_exists(enabled, fguid)) { 11535 if (cbp->cb_first) { 11536 (void) printf(gettext("\nSome " 11537 "supported features are not " 11538 "enabled on the following pools. " 11539 "Once a\nfeature is enabled the " 11540 "pool may become incompatible with " 11541 "software\nthat does not support " 11542 "the feature. See " 11543 "zpool-features(7) for " 11544 "details.\n\n" 11545 "Note that the pool " 11546 "'compatibility' feature can be " 11547 "used to inhibit\nfeature " 11548 "upgrades.\n\n" 11549 "Features marked with (*) are not " 11550 "applied automatically on upgrade, " 11551 "and\nmust be applied explicitly " 11552 "with zpool-set(7).\n\n")); 11553 (void) printf(gettext("POOL " 11554 "FEATURE\n")); 11555 (void) printf(gettext("------" 11556 "---------\n")); 11557 cbp->cb_first = B_FALSE; 11558 } 11559 11560 if (poolfirst) { 11561 (void) printf(gettext("%s\n"), 11562 zpool_get_name(zhp)); 11563 poolfirst = B_FALSE; 11564 } 11565 11566 (void) printf(gettext(" %s%s\n"), fname, 11567 spa_feature_table[i].fi_flags & 11568 ZFEATURE_FLAG_NO_UPGRADE ? "(*)" : ""); 11569 } 11570 /* 11571 * If they did "zpool upgrade -a", then we could 11572 * be doing ioctls to different pools. We need 11573 * to log this history once to each pool, and bypass 11574 * the normal history logging that happens in main(). 11575 */ 11576 (void) zpool_log_history(g_zfs, history_str); 11577 log_history = B_FALSE; 11578 } 11579 } 11580 11581 return (0); 11582 } 11583 11584 static int 11585 upgrade_one(zpool_handle_t *zhp, void *data) 11586 { 11587 boolean_t modified_pool = B_FALSE; 11588 upgrade_cbdata_t *cbp = data; 11589 uint64_t cur_version; 11590 int ret; 11591 11592 if (strcmp("log", zpool_get_name(zhp)) == 0) { 11593 (void) fprintf(stderr, gettext("'log' is now a reserved word\n" 11594 "Pool 'log' must be renamed using export and import" 11595 " to upgrade.\n")); 11596 return (1); 11597 } 11598 11599 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 11600 if (cur_version > cbp->cb_version) { 11601 (void) printf(gettext("Pool '%s' is already formatted " 11602 "using more current version '%llu'.\n\n"), 11603 zpool_get_name(zhp), (u_longlong_t)cur_version); 11604 return (0); 11605 } 11606 11607 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 11608 (void) printf(gettext("Pool '%s' is already formatted " 11609 "using version %llu.\n\n"), zpool_get_name(zhp), 11610 (u_longlong_t)cbp->cb_version); 11611 return (0); 11612 } 11613 11614 if (cur_version != cbp->cb_version) { 11615 modified_pool = B_TRUE; 11616 ret = upgrade_version(zhp, cbp->cb_version); 11617 if (ret != 0) 11618 return (ret); 11619 } 11620 11621 if (cbp->cb_version >= SPA_VERSION_FEATURES) { 11622 int count = 0; 11623 ret = upgrade_enable_all(zhp, &count); 11624 if (ret != 0) 11625 return (ret); 11626 11627 if (count != 0) { 11628 modified_pool = B_TRUE; 11629 } else if (cur_version == SPA_VERSION) { 11630 (void) printf(gettext("Pool '%s' already has all " 11631 "supported and requested features enabled.\n"), 11632 zpool_get_name(zhp)); 11633 } 11634 } 11635 11636 if (modified_pool) { 11637 (void) printf("\n"); 11638 (void) after_zpool_upgrade(zhp); 11639 } 11640 11641 return (0); 11642 } 11643 11644 /* 11645 * zpool upgrade 11646 * zpool upgrade -v 11647 * zpool upgrade [-V version] <-a | pool ...> 11648 * 11649 * With no arguments, display downrev'd ZFS pool available for upgrade. 11650 * Individual pools can be upgraded by specifying the pool, and '-a' will 11651 * upgrade all pools. 11652 */ 11653 int 11654 zpool_do_upgrade(int argc, char **argv) 11655 { 11656 int c; 11657 upgrade_cbdata_t cb = { 0 }; 11658 int ret = 0; 11659 boolean_t showversions = B_FALSE; 11660 boolean_t upgradeall = B_FALSE; 11661 char *end; 11662 11663 11664 /* check options */ 11665 while ((c = getopt(argc, argv, ":avV:")) != -1) { 11666 switch (c) { 11667 case 'a': 11668 upgradeall = B_TRUE; 11669 break; 11670 case 'v': 11671 showversions = B_TRUE; 11672 break; 11673 case 'V': 11674 cb.cb_version = strtoll(optarg, &end, 10); 11675 if (*end != '\0' || 11676 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 11677 (void) fprintf(stderr, 11678 gettext("invalid version '%s'\n"), optarg); 11679 usage(B_FALSE); 11680 } 11681 break; 11682 case ':': 11683 (void) fprintf(stderr, gettext("missing argument for " 11684 "'%c' option\n"), optopt); 11685 usage(B_FALSE); 11686 break; 11687 case '?': 11688 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 11689 optopt); 11690 usage(B_FALSE); 11691 } 11692 } 11693 11694 cb.cb_argc = argc; 11695 cb.cb_argv = argv; 11696 argc -= optind; 11697 argv += optind; 11698 11699 if (cb.cb_version == 0) { 11700 cb.cb_version = SPA_VERSION; 11701 } else if (!upgradeall && argc == 0) { 11702 (void) fprintf(stderr, gettext("-V option is " 11703 "incompatible with other arguments\n")); 11704 usage(B_FALSE); 11705 } 11706 11707 if (showversions) { 11708 if (upgradeall || argc != 0) { 11709 (void) fprintf(stderr, gettext("-v option is " 11710 "incompatible with other arguments\n")); 11711 usage(B_FALSE); 11712 } 11713 } else if (upgradeall) { 11714 if (argc != 0) { 11715 (void) fprintf(stderr, gettext("-a option should not " 11716 "be used along with a pool name\n")); 11717 usage(B_FALSE); 11718 } 11719 } 11720 11721 (void) printf("%s", gettext("This system supports ZFS pool feature " 11722 "flags.\n\n")); 11723 if (showversions) { 11724 int i; 11725 11726 (void) printf(gettext("The following features are " 11727 "supported:\n\n")); 11728 (void) printf(gettext("FEAT DESCRIPTION\n")); 11729 (void) printf("----------------------------------------------" 11730 "---------------\n"); 11731 for (i = 0; i < SPA_FEATURES; i++) { 11732 zfeature_info_t *fi = &spa_feature_table[i]; 11733 if (!fi->fi_zfs_mod_supported) 11734 continue; 11735 const char *ro = 11736 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 11737 " (read-only compatible)" : ""; 11738 11739 (void) printf("%-37s%s\n", fi->fi_uname, ro); 11740 (void) printf(" %s\n", fi->fi_desc); 11741 } 11742 (void) printf("\n"); 11743 11744 (void) printf(gettext("The following legacy versions are also " 11745 "supported:\n\n")); 11746 (void) printf(gettext("VER DESCRIPTION\n")); 11747 (void) printf("--- -----------------------------------------" 11748 "---------------\n"); 11749 (void) printf(gettext(" 1 Initial ZFS version\n")); 11750 (void) printf(gettext(" 2 Ditto blocks " 11751 "(replicated metadata)\n")); 11752 (void) printf(gettext(" 3 Hot spares and double parity " 11753 "RAID-Z\n")); 11754 (void) printf(gettext(" 4 zpool history\n")); 11755 (void) printf(gettext(" 5 Compression using the gzip " 11756 "algorithm\n")); 11757 (void) printf(gettext(" 6 bootfs pool property\n")); 11758 (void) printf(gettext(" 7 Separate intent log devices\n")); 11759 (void) printf(gettext(" 8 Delegated administration\n")); 11760 (void) printf(gettext(" 9 refquota and refreservation " 11761 "properties\n")); 11762 (void) printf(gettext(" 10 Cache devices\n")); 11763 (void) printf(gettext(" 11 Improved scrub performance\n")); 11764 (void) printf(gettext(" 12 Snapshot properties\n")); 11765 (void) printf(gettext(" 13 snapused property\n")); 11766 (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 11767 (void) printf(gettext(" 15 user/group space accounting\n")); 11768 (void) printf(gettext(" 16 stmf property support\n")); 11769 (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 11770 (void) printf(gettext(" 18 Snapshot user holds\n")); 11771 (void) printf(gettext(" 19 Log device removal\n")); 11772 (void) printf(gettext(" 20 Compression using zle " 11773 "(zero-length encoding)\n")); 11774 (void) printf(gettext(" 21 Deduplication\n")); 11775 (void) printf(gettext(" 22 Received properties\n")); 11776 (void) printf(gettext(" 23 Slim ZIL\n")); 11777 (void) printf(gettext(" 24 System attributes\n")); 11778 (void) printf(gettext(" 25 Improved scrub stats\n")); 11779 (void) printf(gettext(" 26 Improved snapshot deletion " 11780 "performance\n")); 11781 (void) printf(gettext(" 27 Improved snapshot creation " 11782 "performance\n")); 11783 (void) printf(gettext(" 28 Multiple vdev replacements\n")); 11784 (void) printf(gettext("\nFor more information on a particular " 11785 "version, including supported releases,\n")); 11786 (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 11787 } else if (argc == 0 && upgradeall) { 11788 cb.cb_first = B_TRUE; 11789 ret = zpool_iter(g_zfs, upgrade_cb, &cb); 11790 if (ret == 0 && cb.cb_first) { 11791 if (cb.cb_version == SPA_VERSION) { 11792 (void) printf(gettext("All pools are already " 11793 "formatted using feature flags.\n\n")); 11794 (void) printf(gettext("Every feature flags " 11795 "pool already has all supported and " 11796 "requested features enabled.\n")); 11797 } else { 11798 (void) printf(gettext("All pools are already " 11799 "formatted with version %llu or higher.\n"), 11800 (u_longlong_t)cb.cb_version); 11801 } 11802 } 11803 } else if (argc == 0) { 11804 cb.cb_first = B_TRUE; 11805 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 11806 assert(ret == 0); 11807 11808 if (cb.cb_first) { 11809 (void) printf(gettext("All pools are formatted " 11810 "using feature flags.\n\n")); 11811 } else { 11812 (void) printf(gettext("\nUse 'zpool upgrade -v' " 11813 "for a list of available legacy versions.\n")); 11814 } 11815 11816 cb.cb_first = B_TRUE; 11817 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 11818 assert(ret == 0); 11819 11820 if (cb.cb_first) { 11821 (void) printf(gettext("Every feature flags pool has " 11822 "all supported and requested features enabled.\n")); 11823 } else { 11824 (void) printf(gettext("\n")); 11825 } 11826 } else { 11827 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, 11828 B_FALSE, upgrade_one, &cb); 11829 } 11830 11831 return (ret); 11832 } 11833 11834 typedef struct hist_cbdata { 11835 boolean_t first; 11836 boolean_t longfmt; 11837 boolean_t internal; 11838 } hist_cbdata_t; 11839 11840 static void 11841 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) 11842 { 11843 nvlist_t **records; 11844 uint_t numrecords; 11845 int i; 11846 11847 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 11848 &records, &numrecords) == 0); 11849 for (i = 0; i < numrecords; i++) { 11850 nvlist_t *rec = records[i]; 11851 char tbuf[64] = ""; 11852 11853 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 11854 time_t tsec; 11855 struct tm t; 11856 11857 tsec = fnvlist_lookup_uint64(records[i], 11858 ZPOOL_HIST_TIME); 11859 (void) localtime_r(&tsec, &t); 11860 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 11861 } 11862 11863 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { 11864 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], 11865 ZPOOL_HIST_ELAPSED_NS); 11866 (void) snprintf(tbuf + strlen(tbuf), 11867 sizeof (tbuf) - strlen(tbuf), 11868 " (%lldms)", (long long)elapsed_ns / 1000 / 1000); 11869 } 11870 11871 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 11872 (void) printf("%s %s", tbuf, 11873 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 11874 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 11875 int ievent = 11876 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 11877 if (!cb->internal) 11878 continue; 11879 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 11880 (void) printf("%s unrecognized record:\n", 11881 tbuf); 11882 dump_nvlist(rec, 4); 11883 continue; 11884 } 11885 (void) printf("%s [internal %s txg:%lld] %s", tbuf, 11886 zfs_history_event_names[ievent], 11887 (longlong_t)fnvlist_lookup_uint64( 11888 rec, ZPOOL_HIST_TXG), 11889 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 11890 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 11891 if (!cb->internal) 11892 continue; 11893 (void) printf("%s [txg:%lld] %s", tbuf, 11894 (longlong_t)fnvlist_lookup_uint64( 11895 rec, ZPOOL_HIST_TXG), 11896 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 11897 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 11898 (void) printf(" %s (%llu)", 11899 fnvlist_lookup_string(rec, 11900 ZPOOL_HIST_DSNAME), 11901 (u_longlong_t)fnvlist_lookup_uint64(rec, 11902 ZPOOL_HIST_DSID)); 11903 } 11904 (void) printf(" %s", fnvlist_lookup_string(rec, 11905 ZPOOL_HIST_INT_STR)); 11906 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 11907 if (!cb->internal) 11908 continue; 11909 (void) printf("%s ioctl %s\n", tbuf, 11910 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 11911 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 11912 (void) printf(" input:\n"); 11913 dump_nvlist(fnvlist_lookup_nvlist(rec, 11914 ZPOOL_HIST_INPUT_NVL), 8); 11915 } 11916 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 11917 (void) printf(" output:\n"); 11918 dump_nvlist(fnvlist_lookup_nvlist(rec, 11919 ZPOOL_HIST_OUTPUT_NVL), 8); 11920 } 11921 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { 11922 (void) printf(" output nvlist omitted; " 11923 "original size: %lldKB\n", 11924 (longlong_t)fnvlist_lookup_int64(rec, 11925 ZPOOL_HIST_OUTPUT_SIZE) / 1024); 11926 } 11927 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { 11928 (void) printf(" errno: %lld\n", 11929 (longlong_t)fnvlist_lookup_int64(rec, 11930 ZPOOL_HIST_ERRNO)); 11931 } 11932 } else { 11933 if (!cb->internal) 11934 continue; 11935 (void) printf("%s unrecognized record:\n", tbuf); 11936 dump_nvlist(rec, 4); 11937 } 11938 11939 if (!cb->longfmt) { 11940 (void) printf("\n"); 11941 continue; 11942 } 11943 (void) printf(" ["); 11944 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 11945 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 11946 struct passwd *pwd = getpwuid(who); 11947 (void) printf("user %d ", (int)who); 11948 if (pwd != NULL) 11949 (void) printf("(%s) ", pwd->pw_name); 11950 } 11951 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 11952 (void) printf("on %s", 11953 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 11954 } 11955 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 11956 (void) printf(":%s", 11957 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 11958 } 11959 11960 (void) printf("]"); 11961 (void) printf("\n"); 11962 } 11963 } 11964 11965 /* 11966 * Print out the command history for a specific pool. 11967 */ 11968 static int 11969 get_history_one(zpool_handle_t *zhp, void *data) 11970 { 11971 nvlist_t *nvhis; 11972 int ret; 11973 hist_cbdata_t *cb = (hist_cbdata_t *)data; 11974 uint64_t off = 0; 11975 boolean_t eof = B_FALSE; 11976 11977 cb->first = B_FALSE; 11978 11979 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 11980 11981 while (!eof) { 11982 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) 11983 return (ret); 11984 11985 print_history_records(nvhis, cb); 11986 nvlist_free(nvhis); 11987 } 11988 (void) printf("\n"); 11989 11990 return (ret); 11991 } 11992 11993 /* 11994 * zpool history <pool> 11995 * 11996 * Displays the history of commands that modified pools. 11997 */ 11998 int 11999 zpool_do_history(int argc, char **argv) 12000 { 12001 hist_cbdata_t cbdata = { 0 }; 12002 int ret; 12003 int c; 12004 12005 cbdata.first = B_TRUE; 12006 /* check options */ 12007 while ((c = getopt(argc, argv, "li")) != -1) { 12008 switch (c) { 12009 case 'l': 12010 cbdata.longfmt = B_TRUE; 12011 break; 12012 case 'i': 12013 cbdata.internal = B_TRUE; 12014 break; 12015 case '?': 12016 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12017 optopt); 12018 usage(B_FALSE); 12019 } 12020 } 12021 argc -= optind; 12022 argv += optind; 12023 12024 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, 12025 B_FALSE, get_history_one, &cbdata); 12026 12027 if (argc == 0 && cbdata.first == B_TRUE) { 12028 (void) fprintf(stderr, gettext("no pools available\n")); 12029 return (0); 12030 } 12031 12032 return (ret); 12033 } 12034 12035 typedef struct ev_opts { 12036 int verbose; 12037 int scripted; 12038 int follow; 12039 int clear; 12040 char poolname[ZFS_MAX_DATASET_NAME_LEN]; 12041 } ev_opts_t; 12042 12043 static void 12044 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) 12045 { 12046 char ctime_str[26], str[32]; 12047 const char *ptr; 12048 int64_t *tv; 12049 uint_t n; 12050 12051 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); 12052 memset(str, ' ', 32); 12053 (void) ctime_r((const time_t *)&tv[0], ctime_str); 12054 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ 12055 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ 12056 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ 12057 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ 12058 if (opts->scripted) 12059 (void) printf(gettext("%s\t"), str); 12060 else 12061 (void) printf(gettext("%s "), str); 12062 12063 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); 12064 (void) printf(gettext("%s\n"), ptr); 12065 } 12066 12067 static void 12068 zpool_do_events_nvprint(nvlist_t *nvl, int depth) 12069 { 12070 nvpair_t *nvp; 12071 static char flagstr[256]; 12072 12073 for (nvp = nvlist_next_nvpair(nvl, NULL); 12074 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { 12075 12076 data_type_t type = nvpair_type(nvp); 12077 const char *name = nvpair_name(nvp); 12078 12079 boolean_t b; 12080 uint8_t i8; 12081 uint16_t i16; 12082 uint32_t i32; 12083 uint64_t i64; 12084 const char *str; 12085 nvlist_t *cnv; 12086 12087 printf(gettext("%*s%s = "), depth, "", name); 12088 12089 switch (type) { 12090 case DATA_TYPE_BOOLEAN: 12091 printf(gettext("%s"), "1"); 12092 break; 12093 12094 case DATA_TYPE_BOOLEAN_VALUE: 12095 (void) nvpair_value_boolean_value(nvp, &b); 12096 printf(gettext("%s"), b ? "1" : "0"); 12097 break; 12098 12099 case DATA_TYPE_BYTE: 12100 (void) nvpair_value_byte(nvp, &i8); 12101 printf(gettext("0x%x"), i8); 12102 break; 12103 12104 case DATA_TYPE_INT8: 12105 (void) nvpair_value_int8(nvp, (void *)&i8); 12106 printf(gettext("0x%x"), i8); 12107 break; 12108 12109 case DATA_TYPE_UINT8: 12110 (void) nvpair_value_uint8(nvp, &i8); 12111 printf(gettext("0x%x"), i8); 12112 break; 12113 12114 case DATA_TYPE_INT16: 12115 (void) nvpair_value_int16(nvp, (void *)&i16); 12116 printf(gettext("0x%x"), i16); 12117 break; 12118 12119 case DATA_TYPE_UINT16: 12120 (void) nvpair_value_uint16(nvp, &i16); 12121 printf(gettext("0x%x"), i16); 12122 break; 12123 12124 case DATA_TYPE_INT32: 12125 (void) nvpair_value_int32(nvp, (void *)&i32); 12126 printf(gettext("0x%x"), i32); 12127 break; 12128 12129 case DATA_TYPE_UINT32: 12130 (void) nvpair_value_uint32(nvp, &i32); 12131 if (strcmp(name, 12132 FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE) == 0 || 12133 strcmp(name, 12134 FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE) == 0) { 12135 zfs_valstr_zio_stage(i32, flagstr, 12136 sizeof (flagstr)); 12137 printf(gettext("0x%x [%s]"), i32, flagstr); 12138 } else if (strcmp(name, 12139 FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE) == 0) { 12140 zfs_valstr_zio_type(i32, flagstr, 12141 sizeof (flagstr)); 12142 printf(gettext("0x%x [%s]"), i32, flagstr); 12143 } else if (strcmp(name, 12144 FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY) == 0) { 12145 zfs_valstr_zio_priority(i32, flagstr, 12146 sizeof (flagstr)); 12147 printf(gettext("0x%x [%s]"), i32, flagstr); 12148 } else { 12149 printf(gettext("0x%x"), i32); 12150 } 12151 break; 12152 12153 case DATA_TYPE_INT64: 12154 (void) nvpair_value_int64(nvp, (void *)&i64); 12155 printf(gettext("0x%llx"), (u_longlong_t)i64); 12156 break; 12157 12158 case DATA_TYPE_UINT64: 12159 (void) nvpair_value_uint64(nvp, &i64); 12160 /* 12161 * translate vdev state values to readable 12162 * strings to aide zpool events consumers 12163 */ 12164 if (strcmp(name, 12165 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || 12166 strcmp(name, 12167 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { 12168 printf(gettext("\"%s\" (0x%llx)"), 12169 zpool_state_to_name(i64, VDEV_AUX_NONE), 12170 (u_longlong_t)i64); 12171 } else if (strcmp(name, 12172 FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS) == 0) { 12173 zfs_valstr_zio_flag(i64, flagstr, 12174 sizeof (flagstr)); 12175 printf(gettext("0x%llx [%s]"), 12176 (u_longlong_t)i64, flagstr); 12177 } else { 12178 printf(gettext("0x%llx"), (u_longlong_t)i64); 12179 } 12180 break; 12181 12182 case DATA_TYPE_HRTIME: 12183 (void) nvpair_value_hrtime(nvp, (void *)&i64); 12184 printf(gettext("0x%llx"), (u_longlong_t)i64); 12185 break; 12186 12187 case DATA_TYPE_STRING: 12188 (void) nvpair_value_string(nvp, &str); 12189 printf(gettext("\"%s\""), str ? str : "<NULL>"); 12190 break; 12191 12192 case DATA_TYPE_NVLIST: 12193 printf(gettext("(embedded nvlist)\n")); 12194 (void) nvpair_value_nvlist(nvp, &cnv); 12195 zpool_do_events_nvprint(cnv, depth + 8); 12196 printf(gettext("%*s(end %s)"), depth, "", name); 12197 break; 12198 12199 case DATA_TYPE_NVLIST_ARRAY: { 12200 nvlist_t **val; 12201 uint_t i, nelem; 12202 12203 (void) nvpair_value_nvlist_array(nvp, &val, &nelem); 12204 printf(gettext("(%d embedded nvlists)\n"), nelem); 12205 for (i = 0; i < nelem; i++) { 12206 printf(gettext("%*s%s[%d] = %s\n"), 12207 depth, "", name, i, "(embedded nvlist)"); 12208 zpool_do_events_nvprint(val[i], depth + 8); 12209 printf(gettext("%*s(end %s[%i])\n"), 12210 depth, "", name, i); 12211 } 12212 printf(gettext("%*s(end %s)\n"), depth, "", name); 12213 } 12214 break; 12215 12216 case DATA_TYPE_INT8_ARRAY: { 12217 int8_t *val; 12218 uint_t i, nelem; 12219 12220 (void) nvpair_value_int8_array(nvp, &val, &nelem); 12221 for (i = 0; i < nelem; i++) 12222 printf(gettext("0x%x "), val[i]); 12223 12224 break; 12225 } 12226 12227 case DATA_TYPE_UINT8_ARRAY: { 12228 uint8_t *val; 12229 uint_t i, nelem; 12230 12231 (void) nvpair_value_uint8_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_INT16_ARRAY: { 12239 int16_t *val; 12240 uint_t i, nelem; 12241 12242 (void) nvpair_value_int16_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_UINT16_ARRAY: { 12250 uint16_t *val; 12251 uint_t i, nelem; 12252 12253 (void) nvpair_value_uint16_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_INT32_ARRAY: { 12261 int32_t *val; 12262 uint_t i, nelem; 12263 12264 (void) nvpair_value_int32_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_UINT32_ARRAY: { 12272 uint32_t *val; 12273 uint_t i, nelem; 12274 12275 (void) nvpair_value_uint32_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_INT64_ARRAY: { 12283 int64_t *val; 12284 uint_t i, nelem; 12285 12286 (void) nvpair_value_int64_array(nvp, &val, &nelem); 12287 for (i = 0; i < nelem; i++) 12288 printf(gettext("0x%llx "), 12289 (u_longlong_t)val[i]); 12290 12291 break; 12292 } 12293 12294 case DATA_TYPE_UINT64_ARRAY: { 12295 uint64_t *val; 12296 uint_t i, nelem; 12297 12298 (void) nvpair_value_uint64_array(nvp, &val, &nelem); 12299 for (i = 0; i < nelem; i++) 12300 printf(gettext("0x%llx "), 12301 (u_longlong_t)val[i]); 12302 12303 break; 12304 } 12305 12306 case DATA_TYPE_STRING_ARRAY: { 12307 const char **str; 12308 uint_t i, nelem; 12309 12310 (void) nvpair_value_string_array(nvp, &str, &nelem); 12311 for (i = 0; i < nelem; i++) 12312 printf(gettext("\"%s\" "), 12313 str[i] ? str[i] : "<NULL>"); 12314 12315 break; 12316 } 12317 12318 case DATA_TYPE_BOOLEAN_ARRAY: 12319 case DATA_TYPE_BYTE_ARRAY: 12320 case DATA_TYPE_DOUBLE: 12321 case DATA_TYPE_DONTCARE: 12322 case DATA_TYPE_UNKNOWN: 12323 printf(gettext("<unknown>")); 12324 break; 12325 } 12326 12327 printf(gettext("\n")); 12328 } 12329 } 12330 12331 static int 12332 zpool_do_events_next(ev_opts_t *opts) 12333 { 12334 nvlist_t *nvl; 12335 int zevent_fd, ret, dropped; 12336 const char *pool; 12337 12338 zevent_fd = open(ZFS_DEV, O_RDWR); 12339 VERIFY(zevent_fd >= 0); 12340 12341 if (!opts->scripted) 12342 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); 12343 12344 while (1) { 12345 ret = zpool_events_next(g_zfs, &nvl, &dropped, 12346 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); 12347 if (ret || nvl == NULL) 12348 break; 12349 12350 if (dropped > 0) 12351 (void) printf(gettext("dropped %d events\n"), dropped); 12352 12353 if (strlen(opts->poolname) > 0 && 12354 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && 12355 strcmp(opts->poolname, pool) != 0) 12356 continue; 12357 12358 zpool_do_events_short(nvl, opts); 12359 12360 if (opts->verbose) { 12361 zpool_do_events_nvprint(nvl, 8); 12362 printf(gettext("\n")); 12363 } 12364 (void) fflush(stdout); 12365 12366 nvlist_free(nvl); 12367 } 12368 12369 VERIFY0(close(zevent_fd)); 12370 12371 return (ret); 12372 } 12373 12374 static int 12375 zpool_do_events_clear(void) 12376 { 12377 int count, ret; 12378 12379 ret = zpool_events_clear(g_zfs, &count); 12380 if (!ret) 12381 (void) printf(gettext("cleared %d events\n"), count); 12382 12383 return (ret); 12384 } 12385 12386 /* 12387 * zpool events [-vHf [pool] | -c] 12388 * 12389 * Displays events logs by ZFS. 12390 */ 12391 int 12392 zpool_do_events(int argc, char **argv) 12393 { 12394 ev_opts_t opts = { 0 }; 12395 int ret; 12396 int c; 12397 12398 /* check options */ 12399 while ((c = getopt(argc, argv, "vHfc")) != -1) { 12400 switch (c) { 12401 case 'v': 12402 opts.verbose = 1; 12403 break; 12404 case 'H': 12405 opts.scripted = 1; 12406 break; 12407 case 'f': 12408 opts.follow = 1; 12409 break; 12410 case 'c': 12411 opts.clear = 1; 12412 break; 12413 case '?': 12414 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12415 optopt); 12416 usage(B_FALSE); 12417 } 12418 } 12419 argc -= optind; 12420 argv += optind; 12421 12422 if (argc > 1) { 12423 (void) fprintf(stderr, gettext("too many arguments\n")); 12424 usage(B_FALSE); 12425 } else if (argc == 1) { 12426 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); 12427 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { 12428 (void) fprintf(stderr, 12429 gettext("invalid pool name '%s'\n"), opts.poolname); 12430 usage(B_FALSE); 12431 } 12432 } 12433 12434 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && 12435 opts.clear) { 12436 (void) fprintf(stderr, 12437 gettext("invalid options combined with -c\n")); 12438 usage(B_FALSE); 12439 } 12440 12441 if (opts.clear) 12442 ret = zpool_do_events_clear(); 12443 else 12444 ret = zpool_do_events_next(&opts); 12445 12446 return (ret); 12447 } 12448 12449 static int 12450 get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data) 12451 { 12452 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12453 char value[ZFS_MAXPROPLEN]; 12454 zprop_source_t srctype; 12455 nvlist_t *props, *item, *d; 12456 props = item = d = NULL; 12457 12458 if (cbp->cb_json) { 12459 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "vdevs"); 12460 if (d == NULL) { 12461 fprintf(stderr, "vdevs obj not found.\n"); 12462 exit(1); 12463 } 12464 props = fnvlist_alloc(); 12465 } 12466 12467 for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL; 12468 pl = pl->pl_next) { 12469 char *prop_name; 12470 /* 12471 * If the first property is pool name, it is a special 12472 * placeholder that we can skip. This will also skip 12473 * over the name property when 'all' is specified. 12474 */ 12475 if (pl->pl_prop == ZPOOL_PROP_NAME && 12476 pl == cbp->cb_proplist) 12477 continue; 12478 12479 if (pl->pl_prop == ZPROP_INVAL) { 12480 prop_name = pl->pl_user_prop; 12481 } else { 12482 prop_name = (char *)vdev_prop_to_name(pl->pl_prop); 12483 } 12484 if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop, 12485 prop_name, value, sizeof (value), &srctype, 12486 cbp->cb_literal) == 0) { 12487 zprop_collect_property(vdevname, cbp, prop_name, 12488 value, srctype, NULL, NULL, props); 12489 } 12490 } 12491 12492 if (cbp->cb_json) { 12493 if (!nvlist_empty(props)) { 12494 item = fnvlist_alloc(); 12495 fill_vdev_info(item, zhp, vdevname, B_TRUE, 12496 cbp->cb_json_as_int); 12497 fnvlist_add_nvlist(item, "properties", props); 12498 fnvlist_add_nvlist(d, vdevname, item); 12499 fnvlist_add_nvlist(cbp->cb_jsobj, "vdevs", d); 12500 fnvlist_free(item); 12501 } 12502 fnvlist_free(props); 12503 } 12504 12505 return (0); 12506 } 12507 12508 static int 12509 get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data) 12510 { 12511 zpool_handle_t *zhp = zhp_data; 12512 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12513 char *vdevname; 12514 const char *type; 12515 int ret; 12516 12517 /* 12518 * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the 12519 * pool name for display purposes, which is not desired. Fallback to 12520 * zpool_vdev_name() when not dealing with the root vdev. 12521 */ 12522 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE); 12523 if (zhp != NULL && strcmp(type, "root") == 0) 12524 vdevname = strdup("root-0"); 12525 else 12526 vdevname = zpool_vdev_name(g_zfs, zhp, nv, 12527 cbp->cb_vdevs.cb_name_flags); 12528 12529 (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist); 12530 12531 ret = get_callback_vdev(zhp, vdevname, data); 12532 12533 free(vdevname); 12534 12535 return (ret); 12536 } 12537 12538 static int 12539 get_callback(zpool_handle_t *zhp, void *data) 12540 { 12541 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 12542 char value[ZFS_MAXPROPLEN]; 12543 zprop_source_t srctype; 12544 zprop_list_t *pl; 12545 int vid; 12546 int err = 0; 12547 nvlist_t *props, *item, *d; 12548 props = item = d = NULL; 12549 12550 if (cbp->cb_type == ZFS_TYPE_VDEV) { 12551 if (cbp->cb_json) { 12552 nvlist_t *pool = fnvlist_alloc(); 12553 fill_pool_info(pool, zhp, B_FALSE, cbp->cb_json_as_int); 12554 fnvlist_add_nvlist(cbp->cb_jsobj, "pool", pool); 12555 fnvlist_free(pool); 12556 } 12557 12558 if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) { 12559 for_each_vdev(zhp, get_callback_vdev_cb, data); 12560 } else { 12561 /* Adjust column widths for vdev properties */ 12562 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; 12563 vid++) { 12564 vdev_expand_proplist(zhp, 12565 cbp->cb_vdevs.cb_names[vid], 12566 &cbp->cb_proplist); 12567 } 12568 /* Display the properties */ 12569 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; 12570 vid++) { 12571 get_callback_vdev(zhp, 12572 cbp->cb_vdevs.cb_names[vid], data); 12573 } 12574 } 12575 } else { 12576 assert(cbp->cb_type == ZFS_TYPE_POOL); 12577 if (cbp->cb_json) { 12578 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools"); 12579 if (d == NULL) { 12580 fprintf(stderr, "pools obj not found.\n"); 12581 exit(1); 12582 } 12583 props = fnvlist_alloc(); 12584 } 12585 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 12586 /* 12587 * Skip the special fake placeholder. This will also 12588 * skip over the name property when 'all' is specified. 12589 */ 12590 if (pl->pl_prop == ZPOOL_PROP_NAME && 12591 pl == cbp->cb_proplist) 12592 continue; 12593 12594 if (pl->pl_prop == ZPROP_INVAL && 12595 zfs_prop_user(pl->pl_user_prop)) { 12596 srctype = ZPROP_SRC_LOCAL; 12597 12598 if (zpool_get_userprop(zhp, pl->pl_user_prop, 12599 value, sizeof (value), &srctype) != 0) 12600 continue; 12601 12602 err = zprop_collect_property( 12603 zpool_get_name(zhp), cbp, pl->pl_user_prop, 12604 value, srctype, NULL, NULL, props); 12605 } else if (pl->pl_prop == ZPROP_INVAL && 12606 (zpool_prop_feature(pl->pl_user_prop) || 12607 zpool_prop_unsupported(pl->pl_user_prop))) { 12608 srctype = ZPROP_SRC_LOCAL; 12609 12610 if (zpool_prop_get_feature(zhp, 12611 pl->pl_user_prop, value, 12612 sizeof (value)) == 0) { 12613 err = zprop_collect_property( 12614 zpool_get_name(zhp), cbp, 12615 pl->pl_user_prop, value, srctype, 12616 NULL, NULL, props); 12617 } 12618 } else { 12619 if (zpool_get_prop(zhp, pl->pl_prop, value, 12620 sizeof (value), &srctype, 12621 cbp->cb_literal) != 0) 12622 continue; 12623 12624 err = zprop_collect_property( 12625 zpool_get_name(zhp), cbp, 12626 zpool_prop_to_name(pl->pl_prop), 12627 value, srctype, NULL, NULL, props); 12628 } 12629 if (err != 0) 12630 return (err); 12631 } 12632 12633 if (cbp->cb_json) { 12634 if (!nvlist_empty(props)) { 12635 item = fnvlist_alloc(); 12636 fill_pool_info(item, zhp, B_TRUE, 12637 cbp->cb_json_as_int); 12638 fnvlist_add_nvlist(item, "properties", props); 12639 if (cbp->cb_json_pool_key_guid) { 12640 char buf[256]; 12641 uint64_t guid = fnvlist_lookup_uint64( 12642 zpool_get_config(zhp, NULL), 12643 ZPOOL_CONFIG_POOL_GUID); 12644 snprintf(buf, 256, "%llu", 12645 (u_longlong_t)guid); 12646 fnvlist_add_nvlist(d, buf, item); 12647 } else { 12648 const char *name = zpool_get_name(zhp); 12649 fnvlist_add_nvlist(d, name, item); 12650 } 12651 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d); 12652 fnvlist_free(item); 12653 } 12654 fnvlist_free(props); 12655 } 12656 } 12657 12658 return (0); 12659 } 12660 12661 /* 12662 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 12663 * 12664 * -H Scripted mode. Don't display headers, and separate properties 12665 * by a single tab. 12666 * -o List of columns to display. Defaults to 12667 * "name,property,value,source". 12668 * -p Display values in parsable (exact) format. 12669 * -j Display output in JSON format. 12670 * --json-int Display numbers as integers instead of strings. 12671 * --json-pool-key-guid Set pool GUID as key for pool objects. 12672 * 12673 * Get properties of pools in the system. Output space statistics 12674 * for each one as well as other attributes. 12675 */ 12676 int 12677 zpool_do_get(int argc, char **argv) 12678 { 12679 zprop_get_cbdata_t cb = { 0 }; 12680 zprop_list_t fake_name = { 0 }; 12681 int ret; 12682 int c, i; 12683 char *propstr = NULL; 12684 char *vdev = NULL; 12685 nvlist_t *data = NULL; 12686 12687 cb.cb_first = B_TRUE; 12688 12689 /* 12690 * Set up default columns and sources. 12691 */ 12692 cb.cb_sources = ZPROP_SRC_ALL; 12693 cb.cb_columns[0] = GET_COL_NAME; 12694 cb.cb_columns[1] = GET_COL_PROPERTY; 12695 cb.cb_columns[2] = GET_COL_VALUE; 12696 cb.cb_columns[3] = GET_COL_SOURCE; 12697 cb.cb_type = ZFS_TYPE_POOL; 12698 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; 12699 current_prop_type = cb.cb_type; 12700 12701 struct option long_options[] = { 12702 {"json", no_argument, NULL, 'j'}, 12703 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT}, 12704 {"json-pool-key-guid", no_argument, NULL, 12705 ZPOOL_OPTION_POOL_KEY_GUID}, 12706 {0, 0, 0, 0} 12707 }; 12708 12709 /* check options */ 12710 while ((c = getopt_long(argc, argv, ":jHpo:", long_options, 12711 NULL)) != -1) { 12712 switch (c) { 12713 case 'p': 12714 cb.cb_literal = B_TRUE; 12715 break; 12716 case 'H': 12717 cb.cb_scripted = B_TRUE; 12718 break; 12719 case 'j': 12720 cb.cb_json = B_TRUE; 12721 cb.cb_jsobj = zpool_json_schema(0, 1); 12722 data = fnvlist_alloc(); 12723 break; 12724 case ZPOOL_OPTION_POOL_KEY_GUID: 12725 cb.cb_json_pool_key_guid = B_TRUE; 12726 break; 12727 case ZPOOL_OPTION_JSON_NUMS_AS_INT: 12728 cb.cb_json_as_int = B_TRUE; 12729 cb.cb_literal = B_TRUE; 12730 break; 12731 case 'o': 12732 memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); 12733 i = 0; 12734 12735 for (char *tok; (tok = strsep(&optarg, ",")); ) { 12736 static const char *const col_opts[] = 12737 { "name", "property", "value", "source", 12738 "all" }; 12739 static const zfs_get_column_t col_cols[] = 12740 { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE, 12741 GET_COL_SOURCE }; 12742 12743 if (i == ZFS_GET_NCOLS - 1) { 12744 (void) fprintf(stderr, gettext("too " 12745 "many fields given to -o " 12746 "option\n")); 12747 usage(B_FALSE); 12748 } 12749 12750 for (c = 0; c < ARRAY_SIZE(col_opts); ++c) 12751 if (strcmp(tok, col_opts[c]) == 0) 12752 goto found; 12753 12754 (void) fprintf(stderr, 12755 gettext("invalid column name '%s'\n"), tok); 12756 usage(B_FALSE); 12757 12758 found: 12759 if (c >= 4) { 12760 if (i > 0) { 12761 (void) fprintf(stderr, 12762 gettext("\"all\" conflicts " 12763 "with specific fields " 12764 "given to -o option\n")); 12765 usage(B_FALSE); 12766 } 12767 12768 memcpy(cb.cb_columns, col_cols, 12769 sizeof (col_cols)); 12770 i = ZFS_GET_NCOLS - 1; 12771 } else 12772 cb.cb_columns[i++] = col_cols[c]; 12773 } 12774 break; 12775 case '?': 12776 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 12777 optopt); 12778 usage(B_FALSE); 12779 } 12780 } 12781 12782 argc -= optind; 12783 argv += optind; 12784 12785 if (!cb.cb_json && cb.cb_json_as_int) { 12786 (void) fprintf(stderr, gettext("'--json-int' only works with" 12787 " '-j' option\n")); 12788 usage(B_FALSE); 12789 } 12790 12791 if (!cb.cb_json && cb.cb_json_pool_key_guid) { 12792 (void) fprintf(stderr, gettext("'json-pool-key-guid' only" 12793 " works with '-j' option\n")); 12794 usage(B_FALSE); 12795 } 12796 12797 if (argc < 1) { 12798 (void) fprintf(stderr, gettext("missing property " 12799 "argument\n")); 12800 usage(B_FALSE); 12801 } 12802 12803 /* Properties list is needed later by zprop_get_list() */ 12804 propstr = argv[0]; 12805 12806 argc--; 12807 argv++; 12808 12809 if (argc == 0) { 12810 /* No args, so just print the defaults. */ 12811 } else if (are_all_pools(argc, argv)) { 12812 /* All the args are pool names */ 12813 } else if (are_all_pools(1, argv)) { 12814 /* The first arg is a pool name */ 12815 if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) || 12816 (argc == 2 && strcmp(argv[1], "root") == 0) || 12817 are_vdevs_in_pool(argc - 1, argv + 1, argv[0], 12818 &cb.cb_vdevs)) { 12819 12820 if (strcmp(argv[1], "root") == 0) 12821 vdev = strdup("root-0"); 12822 12823 /* ... and the rest are vdev names */ 12824 if (vdev == NULL) 12825 cb.cb_vdevs.cb_names = argv + 1; 12826 else 12827 cb.cb_vdevs.cb_names = &vdev; 12828 12829 cb.cb_vdevs.cb_names_count = argc - 1; 12830 cb.cb_type = ZFS_TYPE_VDEV; 12831 argc = 1; /* One pool to process */ 12832 } else { 12833 if (cb.cb_json) { 12834 nvlist_free(cb.cb_jsobj); 12835 nvlist_free(data); 12836 } 12837 fprintf(stderr, gettext("Expected a list of vdevs in" 12838 " \"%s\", but got:\n"), argv[0]); 12839 error_list_unresolved_vdevs(argc - 1, argv + 1, 12840 argv[0], &cb.cb_vdevs); 12841 fprintf(stderr, "\n"); 12842 usage(B_FALSE); 12843 return (1); 12844 } 12845 } else { 12846 if (cb.cb_json) { 12847 nvlist_free(cb.cb_jsobj); 12848 nvlist_free(data); 12849 } 12850 /* 12851 * The first arg isn't the name of a valid pool. 12852 */ 12853 fprintf(stderr, gettext("Cannot get properties of %s: " 12854 "no such pool available.\n"), argv[0]); 12855 return (1); 12856 } 12857 12858 if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist, 12859 cb.cb_type) != 0) { 12860 /* Use correct list of valid properties (pool or vdev) */ 12861 current_prop_type = cb.cb_type; 12862 usage(B_FALSE); 12863 } 12864 12865 if (cb.cb_proplist != NULL) { 12866 fake_name.pl_prop = ZPOOL_PROP_NAME; 12867 fake_name.pl_width = strlen(gettext("NAME")); 12868 fake_name.pl_next = cb.cb_proplist; 12869 cb.cb_proplist = &fake_name; 12870 } 12871 12872 if (cb.cb_json) { 12873 if (cb.cb_type == ZFS_TYPE_VDEV) 12874 fnvlist_add_nvlist(cb.cb_jsobj, "vdevs", data); 12875 else 12876 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data); 12877 fnvlist_free(data); 12878 } 12879 12880 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type, 12881 cb.cb_literal, get_callback, &cb); 12882 12883 if (ret == 0 && cb.cb_json) 12884 zcmd_print_json(cb.cb_jsobj); 12885 else if (ret != 0 && cb.cb_json) 12886 nvlist_free(cb.cb_jsobj); 12887 12888 if (cb.cb_proplist == &fake_name) 12889 zprop_free_list(fake_name.pl_next); 12890 else 12891 zprop_free_list(cb.cb_proplist); 12892 12893 if (vdev != NULL) 12894 free(vdev); 12895 12896 return (ret); 12897 } 12898 12899 typedef struct set_cbdata { 12900 char *cb_propname; 12901 char *cb_value; 12902 zfs_type_t cb_type; 12903 vdev_cbdata_t cb_vdevs; 12904 boolean_t cb_any_successful; 12905 } set_cbdata_t; 12906 12907 static int 12908 set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb) 12909 { 12910 int error; 12911 12912 /* Check if we have out-of-bounds features */ 12913 if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) { 12914 boolean_t features[SPA_FEATURES]; 12915 if (zpool_do_load_compat(cb->cb_value, features) != 12916 ZPOOL_COMPATIBILITY_OK) 12917 return (-1); 12918 12919 nvlist_t *enabled = zpool_get_features(zhp); 12920 spa_feature_t i; 12921 for (i = 0; i < SPA_FEATURES; i++) { 12922 const char *fguid = spa_feature_table[i].fi_guid; 12923 if (nvlist_exists(enabled, fguid) && !features[i]) 12924 break; 12925 } 12926 if (i < SPA_FEATURES) 12927 (void) fprintf(stderr, gettext("Warning: one or " 12928 "more features already enabled on pool '%s'\n" 12929 "are not present in this compatibility set.\n"), 12930 zpool_get_name(zhp)); 12931 } 12932 12933 /* if we're setting a feature, check it's in compatibility set */ 12934 if (zpool_prop_feature(cb->cb_propname) && 12935 strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) { 12936 char *fname = strchr(cb->cb_propname, '@') + 1; 12937 spa_feature_t f; 12938 12939 if (zfeature_lookup_name(fname, &f) == 0) { 12940 char compat[ZFS_MAXPROPLEN]; 12941 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, 12942 compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) 12943 compat[0] = '\0'; 12944 12945 boolean_t features[SPA_FEATURES]; 12946 if (zpool_do_load_compat(compat, features) != 12947 ZPOOL_COMPATIBILITY_OK) { 12948 (void) fprintf(stderr, gettext("Error: " 12949 "cannot enable feature '%s' on pool '%s'\n" 12950 "because the pool's 'compatibility' " 12951 "property cannot be parsed.\n"), 12952 fname, zpool_get_name(zhp)); 12953 return (-1); 12954 } 12955 12956 if (!features[f]) { 12957 (void) fprintf(stderr, gettext("Error: " 12958 "cannot enable feature '%s' on pool '%s'\n" 12959 "as it is not specified in this pool's " 12960 "current compatibility set.\n" 12961 "Consider setting 'compatibility' to a " 12962 "less restrictive set, or to 'off'.\n"), 12963 fname, zpool_get_name(zhp)); 12964 return (-1); 12965 } 12966 } 12967 } 12968 12969 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 12970 12971 return (error); 12972 } 12973 12974 static int 12975 set_callback(zpool_handle_t *zhp, void *data) 12976 { 12977 int error; 12978 set_cbdata_t *cb = (set_cbdata_t *)data; 12979 12980 if (cb->cb_type == ZFS_TYPE_VDEV) { 12981 error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names, 12982 cb->cb_propname, cb->cb_value); 12983 } else { 12984 assert(cb->cb_type == ZFS_TYPE_POOL); 12985 error = set_pool_callback(zhp, cb); 12986 } 12987 12988 cb->cb_any_successful = !error; 12989 return (error); 12990 } 12991 12992 int 12993 zpool_do_set(int argc, char **argv) 12994 { 12995 set_cbdata_t cb = { 0 }; 12996 int error; 12997 char *vdev = NULL; 12998 12999 current_prop_type = ZFS_TYPE_POOL; 13000 if (argc > 1 && argv[1][0] == '-') { 13001 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13002 argv[1][1]); 13003 usage(B_FALSE); 13004 } 13005 13006 if (argc < 2) { 13007 (void) fprintf(stderr, gettext("missing property=value " 13008 "argument\n")); 13009 usage(B_FALSE); 13010 } 13011 13012 if (argc < 3) { 13013 (void) fprintf(stderr, gettext("missing pool name\n")); 13014 usage(B_FALSE); 13015 } 13016 13017 if (argc > 4) { 13018 (void) fprintf(stderr, gettext("too many pool names\n")); 13019 usage(B_FALSE); 13020 } 13021 13022 cb.cb_propname = argv[1]; 13023 cb.cb_type = ZFS_TYPE_POOL; 13024 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; 13025 cb.cb_value = strchr(cb.cb_propname, '='); 13026 if (cb.cb_value == NULL) { 13027 (void) fprintf(stderr, gettext("missing value in " 13028 "property=value argument\n")); 13029 usage(B_FALSE); 13030 } 13031 13032 *(cb.cb_value) = '\0'; 13033 cb.cb_value++; 13034 argc -= 2; 13035 argv += 2; 13036 13037 /* argv[0] is pool name */ 13038 if (!is_pool(argv[0])) { 13039 (void) fprintf(stderr, 13040 gettext("cannot open '%s': is not a pool\n"), argv[0]); 13041 return (EINVAL); 13042 } 13043 13044 /* argv[1], when supplied, is vdev name */ 13045 if (argc == 2) { 13046 13047 if (strcmp(argv[1], "root") == 0) 13048 vdev = strdup("root-0"); 13049 else 13050 vdev = strdup(argv[1]); 13051 13052 if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) { 13053 (void) fprintf(stderr, gettext( 13054 "cannot find '%s' in '%s': device not in pool\n"), 13055 vdev, argv[0]); 13056 free(vdev); 13057 return (EINVAL); 13058 } 13059 cb.cb_vdevs.cb_names = &vdev; 13060 cb.cb_vdevs.cb_names_count = 1; 13061 cb.cb_type = ZFS_TYPE_VDEV; 13062 } 13063 13064 error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL, 13065 B_FALSE, set_callback, &cb); 13066 13067 if (vdev != NULL) 13068 free(vdev); 13069 13070 return (error); 13071 } 13072 13073 /* Add up the total number of bytes left to initialize/trim across all vdevs */ 13074 static uint64_t 13075 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) 13076 { 13077 uint64_t bytes_remaining; 13078 nvlist_t **child; 13079 uint_t c, children; 13080 vdev_stat_t *vs; 13081 13082 assert(activity == ZPOOL_WAIT_INITIALIZE || 13083 activity == ZPOOL_WAIT_TRIM); 13084 13085 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 13086 (uint64_t **)&vs, &c) == 0); 13087 13088 if (activity == ZPOOL_WAIT_INITIALIZE && 13089 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) 13090 bytes_remaining = vs->vs_initialize_bytes_est - 13091 vs->vs_initialize_bytes_done; 13092 else if (activity == ZPOOL_WAIT_TRIM && 13093 vs->vs_trim_state == VDEV_TRIM_ACTIVE) 13094 bytes_remaining = vs->vs_trim_bytes_est - 13095 vs->vs_trim_bytes_done; 13096 else 13097 bytes_remaining = 0; 13098 13099 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13100 &child, &children) != 0) 13101 children = 0; 13102 13103 for (c = 0; c < children; c++) 13104 bytes_remaining += vdev_activity_remaining(child[c], activity); 13105 13106 return (bytes_remaining); 13107 } 13108 13109 /* Add up the total number of bytes left to rebuild across top-level vdevs */ 13110 static uint64_t 13111 vdev_activity_top_remaining(nvlist_t *nv) 13112 { 13113 uint64_t bytes_remaining = 0; 13114 nvlist_t **child; 13115 uint_t children; 13116 int error; 13117 13118 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13119 &child, &children) != 0) 13120 children = 0; 13121 13122 for (uint_t c = 0; c < children; c++) { 13123 vdev_rebuild_stat_t *vrs; 13124 uint_t i; 13125 13126 error = nvlist_lookup_uint64_array(child[c], 13127 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); 13128 if (error == 0) { 13129 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { 13130 bytes_remaining += (vrs->vrs_bytes_est - 13131 vrs->vrs_bytes_rebuilt); 13132 } 13133 } 13134 } 13135 13136 return (bytes_remaining); 13137 } 13138 13139 /* Whether any vdevs are 'spare' or 'replacing' vdevs */ 13140 static boolean_t 13141 vdev_any_spare_replacing(nvlist_t *nv) 13142 { 13143 nvlist_t **child; 13144 uint_t c, children; 13145 const char *vdev_type; 13146 13147 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); 13148 13149 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || 13150 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || 13151 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { 13152 return (B_TRUE); 13153 } 13154 13155 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13156 &child, &children) != 0) 13157 children = 0; 13158 13159 for (c = 0; c < children; c++) { 13160 if (vdev_any_spare_replacing(child[c])) 13161 return (B_TRUE); 13162 } 13163 13164 return (B_FALSE); 13165 } 13166 13167 typedef struct wait_data { 13168 char *wd_poolname; 13169 boolean_t wd_scripted; 13170 boolean_t wd_exact; 13171 boolean_t wd_headers_once; 13172 boolean_t wd_should_exit; 13173 /* Which activities to wait for */ 13174 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; 13175 float wd_interval; 13176 pthread_cond_t wd_cv; 13177 pthread_mutex_t wd_mutex; 13178 } wait_data_t; 13179 13180 /* 13181 * Print to stdout a single line, containing one column for each activity that 13182 * we are waiting for specifying how many bytes of work are left for that 13183 * activity. 13184 */ 13185 static void 13186 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) 13187 { 13188 nvlist_t *config, *nvroot; 13189 uint_t c; 13190 int i; 13191 pool_checkpoint_stat_t *pcs = NULL; 13192 pool_scan_stat_t *pss = NULL; 13193 pool_removal_stat_t *prs = NULL; 13194 pool_raidz_expand_stat_t *pres = NULL; 13195 const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE", 13196 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"}; 13197 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; 13198 13199 /* Calculate the width of each column */ 13200 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13201 /* 13202 * Make sure we have enough space in the col for pretty-printed 13203 * numbers and for the column header, and then leave a couple 13204 * spaces between cols for readability. 13205 */ 13206 col_widths[i] = MAX(strlen(headers[i]), 6) + 2; 13207 } 13208 13209 if (timestamp_fmt != NODATE) 13210 print_timestamp(timestamp_fmt); 13211 13212 /* Print header if appropriate */ 13213 int term_height = terminal_height(); 13214 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && 13215 row % (term_height-1) == 0); 13216 if (!wd->wd_scripted && (row == 0 || reprint_header)) { 13217 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13218 if (wd->wd_enabled[i]) 13219 (void) printf("%*s", col_widths[i], headers[i]); 13220 } 13221 (void) fputc('\n', stdout); 13222 } 13223 13224 /* Bytes of work remaining in each activity */ 13225 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; 13226 13227 bytes_rem[ZPOOL_WAIT_FREE] = 13228 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); 13229 13230 config = zpool_get_config(zhp, NULL); 13231 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 13232 13233 (void) nvlist_lookup_uint64_array(nvroot, 13234 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); 13235 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) 13236 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; 13237 13238 (void) nvlist_lookup_uint64_array(nvroot, 13239 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); 13240 if (prs != NULL && prs->prs_state == DSS_SCANNING) 13241 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - 13242 prs->prs_copied; 13243 13244 (void) nvlist_lookup_uint64_array(nvroot, 13245 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); 13246 if (pss != NULL && pss->pss_state == DSS_SCANNING && 13247 pss->pss_pass_scrub_pause == 0) { 13248 int64_t rem = pss->pss_to_examine - pss->pss_issued; 13249 if (pss->pss_func == POOL_SCAN_SCRUB) 13250 bytes_rem[ZPOOL_WAIT_SCRUB] = rem; 13251 else 13252 bytes_rem[ZPOOL_WAIT_RESILVER] = rem; 13253 } else if (check_rebuilding(nvroot, NULL)) { 13254 bytes_rem[ZPOOL_WAIT_RESILVER] = 13255 vdev_activity_top_remaining(nvroot); 13256 } 13257 13258 (void) nvlist_lookup_uint64_array(nvroot, 13259 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); 13260 if (pres != NULL && pres->pres_state == DSS_SCANNING) { 13261 int64_t rem = pres->pres_to_reflow - pres->pres_reflowed; 13262 bytes_rem[ZPOOL_WAIT_RAIDZ_EXPAND] = rem; 13263 } 13264 13265 bytes_rem[ZPOOL_WAIT_INITIALIZE] = 13266 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); 13267 bytes_rem[ZPOOL_WAIT_TRIM] = 13268 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); 13269 13270 /* 13271 * A replace finishes after resilvering finishes, so the amount of work 13272 * left for a replace is the same as for resilvering. 13273 * 13274 * It isn't quite correct to say that if we have any 'spare' or 13275 * 'replacing' vdevs and a resilver is happening, then a replace is in 13276 * progress, like we do here. When a hot spare is used, the faulted vdev 13277 * is not removed after the hot spare is resilvered, so parent 'spare' 13278 * vdev is not removed either. So we could have a 'spare' vdev, but be 13279 * resilvering for a different reason. However, we use it as a heuristic 13280 * because we don't have access to the DTLs, which could tell us whether 13281 * or not we have really finished resilvering a hot spare. 13282 */ 13283 if (vdev_any_spare_replacing(nvroot)) 13284 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; 13285 13286 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13287 char buf[64]; 13288 if (!wd->wd_enabled[i]) 13289 continue; 13290 13291 if (wd->wd_exact) { 13292 (void) snprintf(buf, sizeof (buf), "%" PRIi64, 13293 bytes_rem[i]); 13294 } else { 13295 zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); 13296 } 13297 13298 if (wd->wd_scripted) 13299 (void) printf(i == 0 ? "%s" : "\t%s", buf); 13300 else 13301 (void) printf(" %*s", col_widths[i] - 1, buf); 13302 } 13303 (void) printf("\n"); 13304 (void) fflush(stdout); 13305 } 13306 13307 static void * 13308 wait_status_thread(void *arg) 13309 { 13310 wait_data_t *wd = (wait_data_t *)arg; 13311 zpool_handle_t *zhp; 13312 13313 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) 13314 return (void *)(1); 13315 13316 for (int row = 0; ; row++) { 13317 boolean_t missing; 13318 struct timespec timeout; 13319 int ret = 0; 13320 (void) clock_gettime(CLOCK_REALTIME, &timeout); 13321 13322 if (zpool_refresh_stats(zhp, &missing) != 0 || missing || 13323 zpool_props_refresh(zhp) != 0) { 13324 zpool_close(zhp); 13325 return (void *)(uintptr_t)(missing ? 0 : 1); 13326 } 13327 13328 print_wait_status_row(wd, zhp, row); 13329 13330 timeout.tv_sec += floor(wd->wd_interval); 13331 long nanos = timeout.tv_nsec + 13332 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; 13333 if (nanos >= NANOSEC) { 13334 timeout.tv_sec++; 13335 timeout.tv_nsec = nanos - NANOSEC; 13336 } else { 13337 timeout.tv_nsec = nanos; 13338 } 13339 pthread_mutex_lock(&wd->wd_mutex); 13340 if (!wd->wd_should_exit) 13341 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, 13342 &timeout); 13343 pthread_mutex_unlock(&wd->wd_mutex); 13344 if (ret == 0) { 13345 break; /* signaled by main thread */ 13346 } else if (ret != ETIMEDOUT) { 13347 (void) fprintf(stderr, gettext("pthread_cond_timedwait " 13348 "failed: %s\n"), strerror(ret)); 13349 zpool_close(zhp); 13350 return (void *)(uintptr_t)(1); 13351 } 13352 } 13353 13354 zpool_close(zhp); 13355 return (void *)(0); 13356 } 13357 13358 int 13359 zpool_do_wait(int argc, char **argv) 13360 { 13361 boolean_t verbose = B_FALSE; 13362 int c, i; 13363 unsigned long count; 13364 pthread_t status_thr; 13365 int error = 0; 13366 zpool_handle_t *zhp; 13367 13368 wait_data_t wd; 13369 wd.wd_scripted = B_FALSE; 13370 wd.wd_exact = B_FALSE; 13371 wd.wd_headers_once = B_FALSE; 13372 wd.wd_should_exit = B_FALSE; 13373 13374 pthread_mutex_init(&wd.wd_mutex, NULL); 13375 pthread_cond_init(&wd.wd_cv, NULL); 13376 13377 /* By default, wait for all types of activity. */ 13378 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) 13379 wd.wd_enabled[i] = B_TRUE; 13380 13381 while ((c = getopt(argc, argv, "HpT:t:")) != -1) { 13382 switch (c) { 13383 case 'H': 13384 wd.wd_scripted = B_TRUE; 13385 break; 13386 case 'n': 13387 wd.wd_headers_once = B_TRUE; 13388 break; 13389 case 'p': 13390 wd.wd_exact = B_TRUE; 13391 break; 13392 case 'T': 13393 get_timestamp_arg(*optarg); 13394 break; 13395 case 't': 13396 /* Reset activities array */ 13397 memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled)); 13398 13399 for (char *tok; (tok = strsep(&optarg, ",")); ) { 13400 static const char *const col_opts[] = { 13401 "discard", "free", "initialize", "replace", 13402 "remove", "resilver", "scrub", "trim", 13403 "raidz_expand" }; 13404 13405 for (i = 0; i < ARRAY_SIZE(col_opts); ++i) 13406 if (strcmp(tok, col_opts[i]) == 0) { 13407 wd.wd_enabled[i] = B_TRUE; 13408 goto found; 13409 } 13410 13411 (void) fprintf(stderr, 13412 gettext("invalid activity '%s'\n"), tok); 13413 usage(B_FALSE); 13414 found:; 13415 } 13416 break; 13417 case '?': 13418 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13419 optopt); 13420 usage(B_FALSE); 13421 } 13422 } 13423 13424 argc -= optind; 13425 argv += optind; 13426 13427 get_interval_count(&argc, argv, &wd.wd_interval, &count); 13428 if (count != 0) { 13429 /* This subcmd only accepts an interval, not a count */ 13430 (void) fprintf(stderr, gettext("too many arguments\n")); 13431 usage(B_FALSE); 13432 } 13433 13434 if (wd.wd_interval != 0) 13435 verbose = B_TRUE; 13436 13437 if (argc < 1) { 13438 (void) fprintf(stderr, gettext("missing 'pool' argument\n")); 13439 usage(B_FALSE); 13440 } 13441 if (argc > 1) { 13442 (void) fprintf(stderr, gettext("too many arguments\n")); 13443 usage(B_FALSE); 13444 } 13445 13446 wd.wd_poolname = argv[0]; 13447 13448 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) 13449 return (1); 13450 13451 if (verbose) { 13452 /* 13453 * We use a separate thread for printing status updates because 13454 * the main thread will call lzc_wait(), which blocks as long 13455 * as an activity is in progress, which can be a long time. 13456 */ 13457 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) 13458 != 0) { 13459 (void) fprintf(stderr, gettext("failed to create status" 13460 "thread: %s\n"), strerror(errno)); 13461 zpool_close(zhp); 13462 return (1); 13463 } 13464 } 13465 13466 /* 13467 * Loop over all activities that we are supposed to wait for until none 13468 * of them are in progress. Note that this means we can end up waiting 13469 * for more activities to complete than just those that were in progress 13470 * when we began waiting; if an activity we are interested in begins 13471 * while we are waiting for another activity, we will wait for both to 13472 * complete before exiting. 13473 */ 13474 for (;;) { 13475 boolean_t missing = B_FALSE; 13476 boolean_t any_waited = B_FALSE; 13477 13478 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { 13479 boolean_t waited; 13480 13481 if (!wd.wd_enabled[i]) 13482 continue; 13483 13484 error = zpool_wait_status(zhp, i, &missing, &waited); 13485 if (error != 0 || missing) 13486 break; 13487 13488 any_waited = (any_waited || waited); 13489 } 13490 13491 if (error != 0 || missing || !any_waited) 13492 break; 13493 } 13494 13495 zpool_close(zhp); 13496 13497 if (verbose) { 13498 uintptr_t status; 13499 pthread_mutex_lock(&wd.wd_mutex); 13500 wd.wd_should_exit = B_TRUE; 13501 pthread_cond_signal(&wd.wd_cv); 13502 pthread_mutex_unlock(&wd.wd_mutex); 13503 (void) pthread_join(status_thr, (void *)&status); 13504 if (status != 0) 13505 error = status; 13506 } 13507 13508 pthread_mutex_destroy(&wd.wd_mutex); 13509 pthread_cond_destroy(&wd.wd_cv); 13510 return (error); 13511 } 13512 13513 /* 13514 * zpool ddtprune -d|-p <amount> <pool> 13515 * 13516 * -d <days> Prune entries <days> old and older 13517 * -p <percent> Prune <percent> amount of entries 13518 * 13519 * Prune single reference entries from DDT to satisfy the amount specified. 13520 */ 13521 int 13522 zpool_do_ddt_prune(int argc, char **argv) 13523 { 13524 zpool_ddt_prune_unit_t unit = ZPOOL_DDT_PRUNE_NONE; 13525 uint64_t amount = 0; 13526 zpool_handle_t *zhp; 13527 char *endptr; 13528 int c; 13529 13530 while ((c = getopt(argc, argv, "d:p:")) != -1) { 13531 switch (c) { 13532 case 'd': 13533 if (unit == ZPOOL_DDT_PRUNE_PERCENTAGE) { 13534 (void) fprintf(stderr, gettext("-d cannot be " 13535 "combined with -p option\n")); 13536 usage(B_FALSE); 13537 } 13538 errno = 0; 13539 amount = strtoull(optarg, &endptr, 0); 13540 if (errno != 0 || *endptr != '\0' || amount == 0) { 13541 (void) fprintf(stderr, 13542 gettext("invalid days value\n")); 13543 usage(B_FALSE); 13544 } 13545 amount *= 86400; /* convert days to seconds */ 13546 unit = ZPOOL_DDT_PRUNE_AGE; 13547 break; 13548 case 'p': 13549 if (unit == ZPOOL_DDT_PRUNE_AGE) { 13550 (void) fprintf(stderr, gettext("-p cannot be " 13551 "combined with -d option\n")); 13552 usage(B_FALSE); 13553 } 13554 errno = 0; 13555 amount = strtoull(optarg, &endptr, 0); 13556 if (errno != 0 || *endptr != '\0' || 13557 amount == 0 || amount > 100) { 13558 (void) fprintf(stderr, 13559 gettext("invalid percentage value\n")); 13560 usage(B_FALSE); 13561 } 13562 unit = ZPOOL_DDT_PRUNE_PERCENTAGE; 13563 break; 13564 case '?': 13565 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13566 optopt); 13567 usage(B_FALSE); 13568 } 13569 } 13570 argc -= optind; 13571 argv += optind; 13572 13573 if (unit == ZPOOL_DDT_PRUNE_NONE) { 13574 (void) fprintf(stderr, 13575 gettext("missing amount option (-d|-p <value>)\n")); 13576 usage(B_FALSE); 13577 } else if (argc < 1) { 13578 (void) fprintf(stderr, gettext("missing pool argument\n")); 13579 usage(B_FALSE); 13580 } else if (argc > 1) { 13581 (void) fprintf(stderr, gettext("too many arguments\n")); 13582 usage(B_FALSE); 13583 } 13584 zhp = zpool_open(g_zfs, argv[0]); 13585 if (zhp == NULL) 13586 return (-1); 13587 13588 int error = zpool_ddt_prune(zhp, unit, amount); 13589 13590 zpool_close(zhp); 13591 13592 return (error); 13593 } 13594 13595 static int 13596 find_command_idx(const char *command, int *idx) 13597 { 13598 for (int i = 0; i < NCOMMAND; ++i) { 13599 if (command_table[i].name == NULL) 13600 continue; 13601 13602 if (strcmp(command, command_table[i].name) == 0) { 13603 *idx = i; 13604 return (0); 13605 } 13606 } 13607 return (1); 13608 } 13609 13610 /* 13611 * Display version message 13612 */ 13613 static int 13614 zpool_do_version(int argc, char **argv) 13615 { 13616 int c; 13617 nvlist_t *jsobj = NULL, *zfs_ver = NULL; 13618 boolean_t json = B_FALSE; 13619 13620 struct option long_options[] = { 13621 {"json", no_argument, NULL, 'j'}, 13622 }; 13623 13624 while ((c = getopt_long(argc, argv, "j", long_options, NULL)) != -1) { 13625 switch (c) { 13626 case 'j': 13627 json = B_TRUE; 13628 jsobj = zpool_json_schema(0, 1); 13629 break; 13630 case '?': 13631 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 13632 optopt); 13633 usage(B_FALSE); 13634 } 13635 } 13636 13637 argc -= optind; 13638 if (argc != 0) { 13639 (void) fprintf(stderr, "too many arguments\n"); 13640 usage(B_FALSE); 13641 } 13642 13643 if (json) { 13644 zfs_ver = zfs_version_nvlist(); 13645 if (zfs_ver) { 13646 fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver); 13647 zcmd_print_json(jsobj); 13648 fnvlist_free(zfs_ver); 13649 return (0); 13650 } else 13651 return (-1); 13652 } else 13653 return (zfs_version_print() != 0); 13654 } 13655 13656 /* Display documentation */ 13657 static int 13658 zpool_do_help(int argc, char **argv) 13659 { 13660 char page[MAXNAMELEN]; 13661 if (argc < 3 || strcmp(argv[2], "zpool") == 0) 13662 strcpy(page, "zpool"); 13663 else if (strcmp(argv[2], "concepts") == 0 || 13664 strcmp(argv[2], "props") == 0) 13665 snprintf(page, sizeof (page), "zpool%s", argv[2]); 13666 else 13667 snprintf(page, sizeof (page), "zpool-%s", argv[2]); 13668 13669 execlp("man", "man", page, NULL); 13670 13671 fprintf(stderr, "couldn't run man program: %s", strerror(errno)); 13672 return (-1); 13673 } 13674 13675 /* 13676 * Do zpool_load_compat() and print error message on failure 13677 */ 13678 static zpool_compat_status_t 13679 zpool_do_load_compat(const char *compat, boolean_t *list) 13680 { 13681 char report[1024]; 13682 13683 zpool_compat_status_t ret; 13684 13685 ret = zpool_load_compat(compat, list, report, 1024); 13686 switch (ret) { 13687 13688 case ZPOOL_COMPATIBILITY_OK: 13689 break; 13690 13691 case ZPOOL_COMPATIBILITY_NOFILES: 13692 case ZPOOL_COMPATIBILITY_BADFILE: 13693 case ZPOOL_COMPATIBILITY_BADTOKEN: 13694 (void) fprintf(stderr, "Error: %s\n", report); 13695 break; 13696 13697 case ZPOOL_COMPATIBILITY_WARNTOKEN: 13698 (void) fprintf(stderr, "Warning: %s\n", report); 13699 ret = ZPOOL_COMPATIBILITY_OK; 13700 break; 13701 } 13702 return (ret); 13703 } 13704 13705 int 13706 main(int argc, char **argv) 13707 { 13708 int ret = 0; 13709 int i = 0; 13710 char *cmdname; 13711 char **newargv; 13712 13713 (void) setlocale(LC_ALL, ""); 13714 (void) setlocale(LC_NUMERIC, "C"); 13715 (void) textdomain(TEXT_DOMAIN); 13716 srand(time(NULL)); 13717 13718 opterr = 0; 13719 13720 /* 13721 * Make sure the user has specified some command. 13722 */ 13723 if (argc < 2) { 13724 (void) fprintf(stderr, gettext("missing command\n")); 13725 usage(B_FALSE); 13726 } 13727 13728 cmdname = argv[1]; 13729 13730 /* 13731 * Special case '-?' 13732 */ 13733 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) 13734 usage(B_TRUE); 13735 13736 /* 13737 * Special case '-V|--version' 13738 */ 13739 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) 13740 return (zfs_version_print() != 0); 13741 13742 /* 13743 * Special case 'help' 13744 */ 13745 if (strcmp(cmdname, "help") == 0) 13746 return (zpool_do_help(argc, argv)); 13747 13748 if ((g_zfs = libzfs_init()) == NULL) { 13749 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); 13750 return (1); 13751 } 13752 13753 libzfs_print_on_error(g_zfs, B_TRUE); 13754 13755 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 13756 13757 /* 13758 * Many commands modify input strings for string parsing reasons. 13759 * We create a copy to protect the original argv. 13760 */ 13761 newargv = safe_malloc((argc + 1) * sizeof (newargv[0])); 13762 for (i = 0; i < argc; i++) 13763 newargv[i] = strdup(argv[i]); 13764 newargv[argc] = NULL; 13765 13766 /* 13767 * Run the appropriate command. 13768 */ 13769 if (find_command_idx(cmdname, &i) == 0) { 13770 current_command = &command_table[i]; 13771 ret = command_table[i].func(argc - 1, newargv + 1); 13772 } else if (strchr(cmdname, '=')) { 13773 verify(find_command_idx("set", &i) == 0); 13774 current_command = &command_table[i]; 13775 ret = command_table[i].func(argc, newargv); 13776 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 13777 /* 13778 * 'freeze' is a vile debugging abomination, so we treat 13779 * it as such. 13780 */ 13781 zfs_cmd_t zc = {"\0"}; 13782 13783 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 13784 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); 13785 if (ret != 0) { 13786 (void) fprintf(stderr, 13787 gettext("failed to freeze pool: %d\n"), errno); 13788 ret = 1; 13789 } 13790 13791 log_history = 0; 13792 } else { 13793 (void) fprintf(stderr, gettext("unrecognized " 13794 "command '%s'\n"), cmdname); 13795 usage(B_FALSE); 13796 ret = 1; 13797 } 13798 13799 for (i = 0; i < argc; i++) 13800 free(newargv[i]); 13801 free(newargv); 13802 13803 if (ret == 0 && log_history) 13804 (void) zpool_log_history(g_zfs, history_str); 13805 13806 libzfs_fini(g_zfs); 13807 13808 /* 13809 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 13810 * for the purposes of running ::findleaks. 13811 */ 13812 if (getenv("ZFS_ABORT") != NULL) { 13813 (void) printf("dumping core by request\n"); 13814 abort(); 13815 } 13816 13817 return (ret); 13818 } 13819