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