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