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