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