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.
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.
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]
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, 2024 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, 2023, Klara Inc.
36 * Copyright [2021] Hewlett Packard Enterprise Development LP
53 #include <thread_pool.h>
60 #include <sys/fs/zfs.h>
62 #include <sys/systeminfo.h>
63 #include <sys/fm/fs/zfs.h>
64 #include <sys/fm/util.h>
65 #include <sys/fm/protocol.h>
66 #include <sys/zfs_ioctl.h>
67 #include <sys/mount.h>
68 #include <sys/sysmacros.h>
75 #include "zpool_util.h"
76 #include "zfs_comutil.h"
77 #include "zfeature_common.h"
78 #include "zfs_valstr.h"
80 #include "statcommon.h"
82 libzfs_handle_t
*g_zfs
;
84 static int mount_tp_nthr
= 512; /* tpool threads for multi-threaded mounting */
86 static int zpool_do_create(int, char **);
87 static int zpool_do_destroy(int, char **);
89 static int zpool_do_add(int, char **);
90 static int zpool_do_remove(int, char **);
91 static int zpool_do_labelclear(int, char **);
93 static int zpool_do_checkpoint(int, char **);
94 static int zpool_do_prefetch(int, char **);
96 static int zpool_do_list(int, char **);
97 static int zpool_do_iostat(int, char **);
98 static int zpool_do_status(int, char **);
100 static int zpool_do_online(int, char **);
101 static int zpool_do_offline(int, char **);
102 static int zpool_do_clear(int, char **);
103 static int zpool_do_reopen(int, char **);
105 static int zpool_do_reguid(int, char **);
107 static int zpool_do_attach(int, char **);
108 static int zpool_do_detach(int, char **);
109 static int zpool_do_replace(int, char **);
110 static int zpool_do_split(int, char **);
112 static int zpool_do_initialize(int, char **);
113 static int zpool_do_scrub(int, char **);
114 static int zpool_do_resilver(int, char **);
115 static int zpool_do_trim(int, char **);
117 static int zpool_do_import(int, char **);
118 static int zpool_do_export(int, char **);
120 static int zpool_do_upgrade(int, char **);
122 static int zpool_do_history(int, char **);
123 static int zpool_do_events(int, char **);
125 static int zpool_do_get(int, char **);
126 static int zpool_do_set(int, char **);
128 static int zpool_do_sync(int, char **);
130 static int zpool_do_version(int, char **);
132 static int zpool_do_wait(int, char **);
134 static int zpool_do_ddt_prune(int, char **);
136 static int zpool_do_help(int argc
, char **argv
);
138 static zpool_compat_status_t
zpool_do_load_compat(
139 const char *, boolean_t
*);
142 ZPOOL_OPTION_POWER
= 1024,
143 ZPOOL_OPTION_ALLOW_INUSE
,
144 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH
,
145 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH
,
146 ZPOOL_OPTION_POOL_KEY_GUID
,
147 ZPOOL_OPTION_JSON_NUMS_AS_INT
,
148 ZPOOL_OPTION_JSON_FLAT_VDEVS
152 * These libumem hooks provide a reasonable set of defaults for the allocator's
153 * debugging facilities.
158 _umem_debug_init(void)
160 return ("default,verbose"); /* $UMEM_DEBUG setting */
164 _umem_logging_init(void)
166 return ("fail,contents"); /* $UMEM_LOGGING setting */
209 * Flags for stats to display with "zpool iostats"
217 IOS_COUNT
, /* always last element */
220 /* iostat_type entries as bitmasks */
221 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
222 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
223 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
224 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
225 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
227 /* Mask of all the histo bits */
228 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
231 * Lookup table for iostat flags to nvlist names. Basically a list
232 * of all the nvlists a flag requires. Also specifies the order in
233 * which data gets printed in zpool iostat.
235 static const char *vsx_type_to_nvlist
[IOS_COUNT
][15] = {
237 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
238 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
239 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
240 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
241 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
242 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
243 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
244 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
245 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
246 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
247 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
250 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
251 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
252 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
253 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
254 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
255 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
258 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
259 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
260 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
261 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
262 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
263 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
264 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
267 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
268 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
269 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
270 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
271 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
272 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
273 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
274 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
275 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
276 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
277 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
278 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
279 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO
,
280 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO
,
284 static const char *pool_scan_func_str
[] = {
291 static const char *pool_scan_state_str
[] = {
299 static const char *vdev_rebuild_state_str
[] = {
306 static const char *checkpoint_state_str
[] = {
312 static const char *vdev_state_str
[] = {
323 static const char *vdev_aux_str
[] = {
347 static const char *vdev_init_state_str
[] = {
355 static const char *vdev_trim_state_str
[] = {
363 #define ZFS_NICE_TIMESTAMP 100
366 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
367 * Right now, only one histo bit is ever set at one time, so we can
368 * just do a highbit64(a)
370 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
372 typedef struct zpool_command
{
374 int (*func
)(int, char **);
379 * Master command table. Each ZFS command has a name, associated function, and
380 * usage message. The usage messages need to be internationalized, so we have
381 * to have a function to return the usage message based on a command index.
383 * These commands are organized according to how they are displayed in the usage
384 * message. An empty command (one with a NULL name) indicates an empty line in
385 * the generic usage message.
387 static zpool_command_t command_table
[] = {
388 { "version", zpool_do_version
, HELP_VERSION
},
390 { "create", zpool_do_create
, HELP_CREATE
},
391 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
393 { "add", zpool_do_add
, HELP_ADD
},
394 { "remove", zpool_do_remove
, HELP_REMOVE
},
396 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
398 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
399 { "prefetch", zpool_do_prefetch
, HELP_PREFETCH
},
401 { "list", zpool_do_list
, HELP_LIST
},
402 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
403 { "status", zpool_do_status
, HELP_STATUS
},
405 { "online", zpool_do_online
, HELP_ONLINE
},
406 { "offline", zpool_do_offline
, HELP_OFFLINE
},
407 { "clear", zpool_do_clear
, HELP_CLEAR
},
408 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
410 { "attach", zpool_do_attach
, HELP_ATTACH
},
411 { "detach", zpool_do_detach
, HELP_DETACH
},
412 { "replace", zpool_do_replace
, HELP_REPLACE
},
413 { "split", zpool_do_split
, HELP_SPLIT
},
415 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
416 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
417 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
418 { "trim", zpool_do_trim
, HELP_TRIM
},
420 { "import", zpool_do_import
, HELP_IMPORT
},
421 { "export", zpool_do_export
, HELP_EXPORT
},
422 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
423 { "reguid", zpool_do_reguid
, HELP_REGUID
},
425 { "history", zpool_do_history
, HELP_HISTORY
},
426 { "events", zpool_do_events
, HELP_EVENTS
},
428 { "get", zpool_do_get
, HELP_GET
},
429 { "set", zpool_do_set
, HELP_SET
},
430 { "sync", zpool_do_sync
, HELP_SYNC
},
432 { "wait", zpool_do_wait
, HELP_WAIT
},
434 { "ddtprune", zpool_do_ddt_prune
, HELP_DDT_PRUNE
},
437 #define NCOMMAND (ARRAY_SIZE(command_table))
439 #define VDEV_ALLOC_CLASS_LOGS "logs"
441 #define MAX_CMD_LEN 256
443 static zpool_command_t
*current_command
;
444 static zfs_type_t current_prop_type
= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
);
445 static char history_str
[HIS_MAX_RECORD_LEN
];
446 static boolean_t log_history
= B_TRUE
;
447 static uint_t timestamp_fmt
= NODATE
;
450 get_usage(zpool_help_t idx
)
454 return (gettext("\tadd [-afgLnP] [-o property=value] "
455 "<pool> <vdev> ...\n"));
457 return (gettext("\tattach [-fsw] [-o property=value] "
458 "<pool> <device> <new-device>\n"));
460 return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n"));
462 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
463 "\t [-O file-system-property=value] ... \n"
464 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
465 case HELP_CHECKPOINT
:
466 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
468 return (gettext("\tdestroy [-f] <pool>\n"));
470 return (gettext("\tdetach <pool> <device>\n"));
472 return (gettext("\texport [-af] <pool> ...\n"));
474 return (gettext("\thistory [-il] [<pool>] ...\n"));
476 return (gettext("\timport [-d dir] [-D]\n"
477 "\timport [-o mntopts] [-o property=value] ... \n"
478 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
479 "[-R root] [-F [-n]] -a\n"
480 "\timport [-o mntopts] [-o property=value] ... \n"
481 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
482 "[-R root] [-F [-n]]\n"
483 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
485 return (gettext("\tiostat [[[-c [script1,script2,...]"
486 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
487 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
488 " [[-n] interval [count]]\n"));
489 case HELP_LABELCLEAR
:
490 return (gettext("\tlabelclear [-f] <vdev>\n"));
492 return (gettext("\tlist [-gHLpPv] [-o property[,...]] [-j "
493 "[--json-int, --json-pool-key-guid]] ...\n"
494 "\t [-T d|u] [pool] [interval [count]]\n"));
496 return (gettext("\tprefetch -t <type> [<type opts>] <pool>\n"
497 "\t -t ddt <pool>\n"));
499 return (gettext("\toffline [--power]|[[-f][-t]] <pool> "
502 return (gettext("\tonline [--power][-e] <pool> <device> "
505 return (gettext("\treplace [-fsw] [-o property=value] "
506 "<pool> <device> [new-device]\n"));
508 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
510 return (gettext("\treopen [-n] <pool>\n"));
511 case HELP_INITIALIZE
:
512 return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> "
513 "[<device> ...]\n"));
515 return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n"));
517 return (gettext("\tresilver <pool> ...\n"));
519 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
520 "[<device> ...]\n"));
522 return (gettext("\tstatus [--power] [-j [--json-int, "
523 "--json-flat-vdevs, ...\n"
524 "\t --json-pool-key-guid]] [-c [script1,script2,...]] "
525 "[-dDegiLpPstvx] ...\n"
526 "\t [-T d|u] [pool] [interval [count]]\n"));
528 return (gettext("\tupgrade\n"
530 "\tupgrade [-V version] <-a | pool ...>\n"));
532 return (gettext("\tevents [-vHf [pool] | -c]\n"));
534 return (gettext("\tget [-Hp] [-j [--json-int, "
535 "--json-pool-key-guid]] ...\n"
536 "\t [-o \"all\" | field[,...]] "
537 "<\"all\" | property[,...]> <pool> ...\n"));
539 return (gettext("\tset <property=value> <pool>\n"
540 "\tset <vdev_property=value> <pool> <vdev>\n"));
542 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
543 "\t [-o property=value] <pool> <newpool> "
544 "[<device> ...]\n"));
546 return (gettext("\treguid [-g guid] <pool>\n"));
548 return (gettext("\tsync [pool] ...\n"));
550 return (gettext("\tversion [-j]\n"));
552 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
553 "<pool> [interval]\n"));
555 return (gettext("\tddtprune -d|-p <amount> <pool>\n"));
557 __builtin_unreachable();
562 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
568 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
572 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
575 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
576 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
577 fnvlist_add_boolean(res
, path
);
583 for (i
= 0; i
< children
; i
++) {
584 zpool_collect_leaves(zhp
, child
[i
], res
);
589 * Callback routine that will print out a pool property value.
592 print_pool_prop_cb(int prop
, void *cb
)
596 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
598 if (zpool_prop_readonly(prop
))
599 (void) fprintf(fp
, " NO ");
601 (void) fprintf(fp
, " YES ");
603 if (zpool_prop_values(prop
) == NULL
)
604 (void) fprintf(fp
, "-\n");
606 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
612 * Callback routine that will print out a vdev property value.
615 print_vdev_prop_cb(int prop
, void *cb
)
619 (void) fprintf(fp
, "\t%-19s ", vdev_prop_to_name(prop
));
621 if (vdev_prop_readonly(prop
))
622 (void) fprintf(fp
, " NO ");
624 (void) fprintf(fp
, " YES ");
626 if (vdev_prop_values(prop
) == NULL
)
627 (void) fprintf(fp
, "-\n");
629 (void) fprintf(fp
, "%s\n", vdev_prop_values(prop
));
635 * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like
636 * '/dev/disk/by-vdev/L5'.
639 vdev_name_to_path(zpool_handle_t
*zhp
, char *vdev
)
641 nvlist_t
*vdev_nv
= zpool_find_vdev(zhp
, vdev
, NULL
, NULL
, NULL
);
642 if (vdev_nv
== NULL
) {
645 return (fnvlist_lookup_string(vdev_nv
, ZPOOL_CONFIG_PATH
));
649 zpool_power_on(zpool_handle_t
*zhp
, char *vdev
)
651 return (zpool_power(zhp
, vdev
, B_TRUE
));
655 zpool_power_on_and_disk_wait(zpool_handle_t
*zhp
, char *vdev
)
659 rc
= zpool_power_on(zhp
, vdev
);
663 zpool_disk_wait(vdev_name_to_path(zhp
, vdev
));
669 zpool_power_on_pool_and_wait_for_devices(zpool_handle_t
*zhp
)
672 const char *path
= NULL
;
675 /* Power up all the devices first */
676 FOR_EACH_REAL_LEAF_VDEV(zhp
, nv
) {
677 path
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
);
679 rc
= zpool_power_on(zhp
, (char *)path
);
687 * Wait for their devices to show up. Since we powered them on
688 * at roughly the same time, they should all come online around
691 FOR_EACH_REAL_LEAF_VDEV(zhp
, nv
) {
692 path
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
);
693 zpool_disk_wait(path
);
700 zpool_power_off(zpool_handle_t
*zhp
, char *vdev
)
702 return (zpool_power(zhp
, vdev
, B_FALSE
));
706 * Display usage message. If we're inside a command, display only the usage for
707 * that command. Otherwise, iterate over the entire command table and display
708 * a complete usage message.
710 static __attribute__((noreturn
)) void
711 usage(boolean_t requested
)
713 FILE *fp
= requested
? stdout
: stderr
;
715 if (current_command
== NULL
) {
718 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
720 gettext("where 'command' is one of the following:\n\n"));
722 for (i
= 0; i
< NCOMMAND
; i
++) {
723 if (command_table
[i
].name
== NULL
)
724 (void) fprintf(fp
, "\n");
726 (void) fprintf(fp
, "%s",
727 get_usage(command_table
[i
].usage
));
731 gettext("\nFor further help on a command or topic, "
732 "run: %s\n"), "zpool help [<topic>]");
734 (void) fprintf(fp
, gettext("usage:\n"));
735 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
738 if (current_command
!= NULL
&&
739 current_prop_type
!= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
) &&
740 ((strcmp(current_command
->name
, "set") == 0) ||
741 (strcmp(current_command
->name
, "get") == 0) ||
742 (strcmp(current_command
->name
, "list") == 0))) {
744 (void) fprintf(fp
, "%s",
745 gettext("\nthe following properties are supported:\n"));
747 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
748 "PROPERTY", "EDIT", "VALUES");
750 /* Iterate over all properties */
751 if (current_prop_type
== ZFS_TYPE_POOL
) {
752 (void) zprop_iter(print_pool_prop_cb
, fp
, B_FALSE
,
753 B_TRUE
, current_prop_type
);
755 (void) fprintf(fp
, "\t%-19s ", "feature@...");
756 (void) fprintf(fp
, "YES "
757 "disabled | enabled | active\n");
759 (void) fprintf(fp
, gettext("\nThe feature@ properties "
760 "must be appended with a feature name.\n"
761 "See zpool-features(7).\n"));
762 } else if (current_prop_type
== ZFS_TYPE_VDEV
) {
763 (void) zprop_iter(print_vdev_prop_cb
, fp
, B_FALSE
,
764 B_TRUE
, current_prop_type
);
769 * See comments at end of main().
771 if (getenv("ZFS_ABORT") != NULL
) {
772 (void) printf("dumping core by request\n");
776 exit(requested
? 0 : 2);
780 * zpool initialize [-c | -s | -u] [-w] <pool> [<vdev> ...]
781 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
784 * -c Cancel. Ends active initializing.
785 * -s Suspend. Initializing can then be restarted with no flags.
786 * -u Uninitialize. Clears initialization state.
787 * -w Wait. Blocks until initializing has completed.
790 zpool_do_initialize(int argc
, char **argv
)
797 boolean_t wait
= B_FALSE
;
799 struct option long_options
[] = {
800 {"cancel", no_argument
, NULL
, 'c'},
801 {"suspend", no_argument
, NULL
, 's'},
802 {"uninit", no_argument
, NULL
, 'u'},
803 {"wait", no_argument
, NULL
, 'w'},
807 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
808 while ((c
= getopt_long(argc
, argv
, "csuw", long_options
,
812 if (cmd_type
!= POOL_INITIALIZE_START
&&
813 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
814 (void) fprintf(stderr
, gettext("-c cannot be "
815 "combined with other options\n"));
818 cmd_type
= POOL_INITIALIZE_CANCEL
;
821 if (cmd_type
!= POOL_INITIALIZE_START
&&
822 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
823 (void) fprintf(stderr
, gettext("-s cannot be "
824 "combined with other options\n"));
827 cmd_type
= POOL_INITIALIZE_SUSPEND
;
830 if (cmd_type
!= POOL_INITIALIZE_START
&&
831 cmd_type
!= POOL_INITIALIZE_UNINIT
) {
832 (void) fprintf(stderr
, gettext("-u cannot be "
833 "combined with other options\n"));
836 cmd_type
= POOL_INITIALIZE_UNINIT
;
843 (void) fprintf(stderr
,
844 gettext("invalid option '%c'\n"), optopt
);
846 (void) fprintf(stderr
,
847 gettext("invalid option '%s'\n"),
858 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
863 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
864 (void) fprintf(stderr
, gettext("-w cannot be used with -c, -s"
870 zhp
= zpool_open(g_zfs
, poolname
);
874 vdevs
= fnvlist_alloc();
876 /* no individual leaf vdevs specified, so add them all */
877 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
878 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
879 ZPOOL_CONFIG_VDEV_TREE
);
880 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
882 for (int i
= 1; i
< argc
; i
++) {
883 fnvlist_add_boolean(vdevs
, argv
[i
]);
888 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
890 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
899 * print a pool vdev config for dry runs
902 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
903 const char *match
, int name_flags
)
908 boolean_t printed
= B_FALSE
;
910 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
911 &child
, &children
) != 0) {
913 (void) printf("\t%*s%s\n", indent
, "", name
);
917 for (c
= 0; c
< children
; c
++) {
918 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
919 const char *class = "";
921 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
924 if (is_hole
== B_TRUE
) {
928 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
931 class = VDEV_ALLOC_BIAS_LOG
;
932 (void) nvlist_lookup_string(child
[c
],
933 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
934 if (strcmp(match
, class) != 0)
937 if (!printed
&& name
!= NULL
) {
938 (void) printf("\t%*s%s\n", indent
, "", name
);
941 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
942 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
949 * Print the list of l2cache devices for dry runs.
952 print_cache_list(nvlist_t
*nv
, int indent
)
957 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
958 &child
, &children
) == 0 && children
> 0) {
959 (void) printf("\t%*s%s\n", indent
, "", "cache");
963 for (c
= 0; c
< children
; c
++) {
966 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
967 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
973 * Print the list of spares for dry runs.
976 print_spare_list(nvlist_t
*nv
, int indent
)
981 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
982 &child
, &children
) == 0 && children
> 0) {
983 (void) printf("\t%*s%s\n", indent
, "", "spares");
987 for (c
= 0; c
< children
; c
++) {
990 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
991 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
996 typedef struct spare_cbdata
{
998 zpool_handle_t
*cb_zhp
;
1002 find_vdev(nvlist_t
*nv
, uint64_t search
)
1008 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1012 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1013 &child
, &children
) == 0) {
1014 for (c
= 0; c
< children
; c
++)
1015 if (find_vdev(child
[c
], search
))
1023 find_spare(zpool_handle_t
*zhp
, void *data
)
1025 spare_cbdata_t
*cbp
= data
;
1026 nvlist_t
*config
, *nvroot
;
1028 config
= zpool_get_config(zhp
, NULL
);
1029 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1032 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1042 nice_num_str_nvlist(nvlist_t
*item
, const char *key
, uint64_t value
,
1043 boolean_t literal
, boolean_t as_int
, int format
)
1048 snprintf(buf
, 256, "%llu", (u_longlong_t
)value
);
1051 case ZFS_NICENUM_1024
:
1052 zfs_nicenum_format(value
, buf
, 256, ZFS_NICENUM_1024
);
1054 case ZFS_NICENUM_BYTES
:
1055 zfs_nicenum_format(value
, buf
, 256, ZFS_NICENUM_BYTES
);
1057 case ZFS_NICENUM_TIME
:
1058 zfs_nicenum_format(value
, buf
, 256, ZFS_NICENUM_TIME
);
1060 case ZFS_NICE_TIMESTAMP
:
1061 format_timestamp(value
, buf
, 256);
1064 fprintf(stderr
, "Invalid number format");
1069 fnvlist_add_uint64(item
, key
, value
);
1071 fnvlist_add_string(item
, key
, buf
);
1075 * Generates an nvlist with output version for every command based on params.
1076 * Purpose of this is to add a version of JSON output, considering the schema
1077 * format might be updated for each command in future.
1081 * "output_version": {
1082 * "command": string,
1083 * "vers_major": integer,
1084 * "vers_minor": integer,
1088 zpool_json_schema(int maj_v
, int min_v
)
1090 char cmd
[MAX_CMD_LEN
];
1091 nvlist_t
*sch
= fnvlist_alloc();
1092 nvlist_t
*ov
= fnvlist_alloc();
1094 snprintf(cmd
, MAX_CMD_LEN
, "zpool %s", current_command
->name
);
1095 fnvlist_add_string(ov
, "command", cmd
);
1096 fnvlist_add_uint32(ov
, "vers_major", maj_v
);
1097 fnvlist_add_uint32(ov
, "vers_minor", min_v
);
1098 fnvlist_add_nvlist(sch
, "output_version", ov
);
1104 fill_pool_info(nvlist_t
*list
, zpool_handle_t
*zhp
, boolean_t addtype
,
1107 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
1108 uint64_t guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
1109 uint64_t txg
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
);
1111 fnvlist_add_string(list
, "name", zpool_get_name(zhp
));
1113 fnvlist_add_string(list
, "type", "POOL");
1114 fnvlist_add_string(list
, "state", zpool_get_state_str(zhp
));
1117 fnvlist_add_uint64(list
, ZPOOL_CONFIG_POOL_GUID
, guid
);
1119 fnvlist_add_uint64(list
, ZPOOL_CONFIG_POOL_TXG
, txg
);
1120 fnvlist_add_uint64(list
, "spa_version", SPA_VERSION
);
1121 fnvlist_add_uint64(list
, "zpl_version", ZPL_VERSION
);
1123 char value
[ZFS_MAXPROPLEN
];
1125 snprintf(value
, ZFS_MAXPROPLEN
, "%llu",
1126 (u_longlong_t
)guid
);
1127 fnvlist_add_string(list
, ZPOOL_CONFIG_POOL_GUID
, value
);
1130 snprintf(value
, ZFS_MAXPROPLEN
, "%llu",
1132 fnvlist_add_string(list
, ZPOOL_CONFIG_POOL_TXG
, value
);
1134 fnvlist_add_string(list
, "spa_version", SPA_VERSION_STRING
);
1135 fnvlist_add_string(list
, "zpl_version", ZPL_VERSION_STRING
);
1140 used_by_other(zpool_handle_t
*zhp
, nvlist_t
*nvdev
, nvlist_t
*list
)
1142 spare_cbdata_t spare_cb
;
1143 verify(nvlist_lookup_uint64(nvdev
, ZPOOL_CONFIG_GUID
,
1144 &spare_cb
.cb_guid
) == 0);
1145 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1146 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1147 zpool_get_name(zhp
)) != 0) {
1148 fnvlist_add_string(list
, "used_by",
1149 zpool_get_name(spare_cb
.cb_zhp
));
1151 zpool_close(spare_cb
.cb_zhp
);
1156 fill_vdev_info(nvlist_t
*list
, zpool_handle_t
*zhp
, char *name
,
1157 boolean_t addtype
, boolean_t as_int
)
1159 boolean_t l2c
= B_FALSE
;
1160 const char *path
, *phys
, *devid
, *bias
= NULL
;
1161 uint64_t hole
= 0, log
= 0, spare
= 0;
1165 nvlist_t
*nvdev_parent
= NULL
;
1168 if (strcmp(name
, zpool_get_name(zhp
)) != 0)
1171 _name
= (char *)"root-0";
1173 nvdev
= zpool_find_vdev(zhp
, _name
, NULL
, &l2c
, NULL
);
1175 fnvlist_add_string(list
, "name", name
);
1177 fnvlist_add_string(list
, "type", "VDEV");
1179 const char *type
= fnvlist_lookup_string(nvdev
,
1182 fnvlist_add_string(list
, "vdev_type", type
);
1183 uint64_t guid
= fnvlist_lookup_uint64(nvdev
, ZPOOL_CONFIG_GUID
);
1186 fnvlist_add_uint64(list
, "guid", guid
);
1188 char buf
[ZFS_MAXPROPLEN
];
1189 snprintf(buf
, ZFS_MAXPROPLEN
, "%llu",
1190 (u_longlong_t
)guid
);
1191 fnvlist_add_string(list
, "guid", buf
);
1194 if (nvlist_lookup_string(nvdev
, ZPOOL_CONFIG_PATH
, &path
) == 0)
1195 fnvlist_add_string(list
, "path", path
);
1196 if (nvlist_lookup_string(nvdev
, ZPOOL_CONFIG_PHYS_PATH
,
1198 fnvlist_add_string(list
, "phys_path", phys
);
1199 if (nvlist_lookup_string(nvdev
, ZPOOL_CONFIG_DEVID
,
1201 fnvlist_add_string(list
, "devid", devid
);
1202 (void) nvlist_lookup_uint64(nvdev
, ZPOOL_CONFIG_IS_LOG
, &log
);
1203 (void) nvlist_lookup_uint64(nvdev
, ZPOOL_CONFIG_IS_SPARE
,
1205 (void) nvlist_lookup_uint64(nvdev
, ZPOOL_CONFIG_IS_HOLE
, &hole
);
1207 fnvlist_add_string(list
, "class", VDEV_TYPE_HOLE
);
1209 fnvlist_add_string(list
, "class", VDEV_TYPE_L2CACHE
);
1211 fnvlist_add_string(list
, "class", VDEV_TYPE_SPARE
);
1213 fnvlist_add_string(list
, "class", VDEV_TYPE_LOG
);
1215 (void) nvlist_lookup_string(nvdev
,
1216 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
1218 fnvlist_add_string(list
, "class", bias
);
1220 nvdev_parent
= NULL
;
1221 nvdev_parent
= zpool_find_parent_vdev(zhp
,
1222 _name
, NULL
, NULL
, NULL
);
1225 * With a mirrored special device, the parent
1226 * "mirror" vdev will have
1227 * ZPOOL_CONFIG_ALLOCATION_BIAS set to "special"
1228 * not the leaf vdevs. If we're a leaf vdev
1229 * in that case we need to look at our parent
1230 * to see if they're "special" to know if we
1231 * are "special" too.
1234 (void) nvlist_lookup_string(
1236 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1240 fnvlist_add_string(list
, "class", bias
);
1242 fnvlist_add_string(list
, "class",
1246 if (nvlist_lookup_uint64_array(nvdev
, ZPOOL_CONFIG_VDEV_STATS
,
1247 (uint64_t **)&vs
, &c
) == 0) {
1248 fnvlist_add_string(list
, "state",
1249 vdev_state_str
[vs
->vs_state
]);
1255 prop_list_contains_feature(nvlist_t
*proplist
)
1258 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
1259 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
1260 if (zpool_prop_feature(nvpair_name(nvp
)))
1267 * Add a property pair (name, string-value) into a property nvlist.
1270 add_prop_list(const char *propname
, const char *propval
, nvlist_t
**props
,
1273 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
1278 if (*props
== NULL
&&
1279 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
1280 (void) fprintf(stderr
,
1281 gettext("internal error: out of memory\n"));
1288 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
1290 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
1292 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
1293 (!zpool_prop_feature(propname
) &&
1294 !zpool_prop_vdev(propname
))) {
1295 (void) fprintf(stderr
, gettext("property '%s' is "
1296 "not a valid pool or vdev property\n"), propname
);
1301 * feature@ properties and version should not be specified
1304 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
1305 nvlist_exists(proplist
, vname
)) ||
1306 (prop
== ZPOOL_PROP_VERSION
&&
1307 prop_list_contains_feature(proplist
))) {
1308 (void) fprintf(stderr
, gettext("'feature@' and "
1309 "'version' properties cannot be specified "
1315 * if version is specified, only "legacy" compatibility
1318 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
1319 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
1320 nvlist_exists(proplist
, vname
)) ||
1321 (prop
== ZPOOL_PROP_VERSION
&&
1322 nvlist_exists(proplist
, cname
) &&
1323 strcmp(fnvlist_lookup_string(proplist
, cname
),
1324 ZPOOL_COMPAT_LEGACY
) != 0)) {
1325 (void) fprintf(stderr
, gettext("when 'version' is "
1326 "specified, the 'compatibility' feature may only "
1327 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
1331 if (zpool_prop_feature(propname
) || zpool_prop_vdev(propname
))
1334 normnm
= zpool_prop_to_name(prop
);
1336 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
1338 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
1340 normnm
= zfs_prop_to_name(fsprop
);
1341 } else if (zfs_prop_user(propname
) ||
1342 zfs_prop_userquota(propname
)) {
1345 (void) fprintf(stderr
, gettext("property '%s' is "
1346 "not a valid filesystem property\n"), propname
);
1351 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
1352 prop
!= ZPOOL_PROP_CACHEFILE
) {
1353 (void) fprintf(stderr
, gettext("property '%s' "
1354 "specified multiple times\n"), propname
);
1358 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
1359 (void) fprintf(stderr
, gettext("internal "
1360 "error: out of memory\n"));
1368 * Set a default property pair (name, string-value) in a property nvlist
1371 add_prop_list_default(const char *propname
, const char *propval
,
1376 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
1379 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
1383 * zpool add [-afgLnP] [-o property=value] <pool> <vdev> ...
1385 * -a Disable the ashift validation checks
1386 * -f Force addition of devices, even if they appear in use
1387 * -g Display guid for individual vdev name.
1388 * -L Follow links when resolving vdev path name.
1389 * -n Do not add the devices, but display the resulting layout if
1390 * they were to be added.
1391 * -o Set property=value.
1392 * -P Display full path for vdev name.
1394 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
1395 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
1399 zpool_do_add(int argc
, char **argv
)
1401 boolean_t check_replication
= B_TRUE
;
1402 boolean_t check_inuse
= B_TRUE
;
1403 boolean_t dryrun
= B_FALSE
;
1404 boolean_t check_ashift
= B_TRUE
;
1405 boolean_t force
= B_FALSE
;
1411 zpool_handle_t
*zhp
;
1413 nvlist_t
*props
= NULL
;
1416 struct option long_options
[] = {
1417 {"allow-in-use", no_argument
, NULL
, ZPOOL_OPTION_ALLOW_INUSE
},
1418 {"allow-replication-mismatch", no_argument
, NULL
,
1419 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH
},
1420 {"allow-ashift-mismatch", no_argument
, NULL
,
1421 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH
},
1426 while ((c
= getopt_long(argc
, argv
, "fgLno:P", long_options
, NULL
))
1433 name_flags
|= VDEV_NAME_GUID
;
1436 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
1442 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1443 (void) fprintf(stderr
, gettext("missing "
1444 "'=' for -o option\n"));
1450 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
1451 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
1455 name_flags
|= VDEV_NAME_PATH
;
1457 case ZPOOL_OPTION_ALLOW_INUSE
:
1458 check_inuse
= B_FALSE
;
1460 case ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH
:
1461 check_replication
= B_FALSE
;
1463 case ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH
:
1464 check_ashift
= B_FALSE
;
1467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1476 /* get pool name and check number of arguments */
1478 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1482 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1487 if (!check_inuse
|| !check_replication
|| !check_ashift
) {
1488 (void) fprintf(stderr
, gettext("'-f' option is not "
1489 "allowed with '--allow-replication-mismatch', "
1490 "'--allow-ashift-mismatch', or "
1491 "'--allow-in-use'\n"));
1494 check_inuse
= B_FALSE
;
1495 check_replication
= B_FALSE
;
1496 check_ashift
= B_FALSE
;
1504 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1507 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1508 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1514 /* unless manually specified use "ashift" pool property (if set) */
1515 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1518 char strval
[ZPOOL_MAXPROPLEN
];
1520 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1521 if (src
!= ZPROP_SRC_DEFAULT
) {
1522 (void) sprintf(strval
, "%" PRId32
, intval
);
1523 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1524 &props
, B_TRUE
) == 0);
1528 /* pass off to make_root_vdev for processing */
1529 nvroot
= make_root_vdev(zhp
, props
, !check_inuse
,
1530 check_replication
, B_FALSE
, dryrun
, argc
, argv
);
1531 if (nvroot
== NULL
) {
1537 nvlist_t
*poolnvroot
;
1538 nvlist_t
**l2child
, **sparechild
;
1539 uint_t l2children
, sparechildren
, c
;
1541 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1543 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1546 (void) printf(gettext("would update '%s' to the following "
1547 "configuration:\n\n"), zpool_get_name(zhp
));
1549 /* print original main pool and new tree */
1550 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1551 name_flags
| VDEV_NAME_TYPE_ID
);
1552 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1554 /* print other classes: 'dedup', 'special', and 'log' */
1555 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1556 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1557 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1558 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1559 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1560 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1561 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1562 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1565 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1566 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1567 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1568 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1569 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1570 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1571 print_vdev_tree(zhp
, "special", nvroot
, 0,
1572 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1575 if (num_logs(poolnvroot
) > 0) {
1576 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1577 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1578 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1579 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1580 } else if (num_logs(nvroot
) > 0) {
1581 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1582 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1585 /* Do the same for the caches */
1586 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1587 &l2child
, &l2children
) == 0 && l2children
) {
1589 (void) printf(gettext("\tcache\n"));
1590 for (c
= 0; c
< l2children
; c
++) {
1591 vname
= zpool_vdev_name(g_zfs
, NULL
,
1592 l2child
[c
], name_flags
);
1593 (void) printf("\t %s\n", vname
);
1597 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1598 &l2child
, &l2children
) == 0 && l2children
) {
1600 (void) printf(gettext("\tcache\n"));
1601 for (c
= 0; c
< l2children
; c
++) {
1602 vname
= zpool_vdev_name(g_zfs
, NULL
,
1603 l2child
[c
], name_flags
);
1604 (void) printf("\t %s\n", vname
);
1608 /* And finally the spares */
1609 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1610 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1612 (void) printf(gettext("\tspares\n"));
1613 for (c
= 0; c
< sparechildren
; c
++) {
1614 vname
= zpool_vdev_name(g_zfs
, NULL
,
1615 sparechild
[c
], name_flags
);
1616 (void) printf("\t %s\n", vname
);
1620 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1621 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1623 (void) printf(gettext("\tspares\n"));
1624 for (c
= 0; c
< sparechildren
; c
++) {
1625 vname
= zpool_vdev_name(g_zfs
, NULL
,
1626 sparechild
[c
], name_flags
);
1627 (void) printf("\t %s\n", vname
);
1634 ret
= (zpool_add(zhp
, nvroot
, check_ashift
) != 0);
1638 nvlist_free(nvroot
);
1645 * zpool remove [-npsw] <pool> <vdev> ...
1647 * Removes the given vdev from the pool.
1650 zpool_do_remove(int argc
, char **argv
)
1654 zpool_handle_t
*zhp
= NULL
;
1655 boolean_t stop
= B_FALSE
;
1657 boolean_t noop
= B_FALSE
;
1658 boolean_t parsable
= B_FALSE
;
1659 boolean_t wait
= B_FALSE
;
1662 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1677 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1686 /* get pool name and check number of arguments */
1688 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1694 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1699 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1705 (void) fprintf(stderr
, gettext("too many arguments\n"));
1708 if (zpool_vdev_remove_cancel(zhp
) != 0)
1711 (void) fprintf(stderr
, gettext("invalid option "
1712 "combination: -w cannot be used with -s\n"));
1717 (void) fprintf(stderr
, gettext("missing device\n"));
1721 for (i
= 1; i
< argc
; i
++) {
1725 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1731 (void) printf("%s %llu\n",
1732 argv
[i
], (unsigned long long)size
);
1735 zfs_nicenum(size
, valstr
,
1737 (void) printf("Memory that will be "
1738 "used after removing %s: %s\n",
1742 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1747 if (ret
== 0 && wait
)
1748 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1756 * Return 1 if a vdev is active (being used in a pool)
1757 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1759 * This is useful for checking if a disk in an active pool is offlined or
1763 vdev_is_active(char *vdev_path
)
1766 fd
= open(vdev_path
, O_EXCL
);
1768 return (1); /* cant open O_EXCL - disk is active */
1772 return (0); /* disk is inactive in the pool */
1776 * zpool labelclear [-f] <vdev>
1778 * -f Force clearing the label for the vdevs which are members of
1779 * the exported or foreign pools.
1781 * Verifies that the vdev is not active and zeros out the label information
1785 zpool_do_labelclear(int argc
, char **argv
)
1787 char vdev
[MAXPATHLEN
];
1789 int c
, fd
= -1, ret
= 0;
1792 boolean_t inuse
= B_FALSE
;
1793 boolean_t force
= B_FALSE
;
1796 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1802 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1813 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1817 (void) fprintf(stderr
, gettext("too many arguments\n"));
1821 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1824 * If we cannot open an absolute path, we quit.
1825 * Otherwise if the provided vdev name doesn't point to a file,
1826 * try prepending expected disk paths and partition numbers.
1828 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1830 if (vdev
[0] == '/') {
1831 (void) fprintf(stderr
, gettext("failed to open "
1832 "%s: %s\n"), vdev
, strerror(errno
));
1836 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1837 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1838 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1842 if (error
|| ((fd
= open(vdev
, O_RDWR
)) < 0)) {
1843 if (errno
== ENOENT
) {
1844 (void) fprintf(stderr
, gettext(
1845 "failed to find device %s, try "
1846 "specifying absolute path instead\n"),
1851 (void) fprintf(stderr
, gettext("failed to open %s:"
1852 " %s\n"), vdev
, strerror(errno
));
1858 * Flush all dirty pages for the block device. This should not be
1859 * fatal when the device does not support BLKFLSBUF as would be the
1860 * case for a file vdev.
1862 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1863 (void) fprintf(stderr
, gettext("failed to invalidate "
1864 "cache for %s: %s\n"), vdev
, strerror(errno
));
1866 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1867 (void) fprintf(stderr
,
1868 gettext("failed to read label from %s\n"), vdev
);
1872 nvlist_free(config
);
1874 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1876 (void) fprintf(stderr
,
1877 gettext("failed to check state for %s\n"), vdev
);
1887 case POOL_STATE_ACTIVE
:
1888 case POOL_STATE_SPARE
:
1889 case POOL_STATE_L2CACHE
:
1891 * We allow the user to call 'zpool offline -f'
1892 * on an offlined disk in an active pool. We can check if
1893 * the disk is online by calling vdev_is_active().
1895 if (force
&& !vdev_is_active(vdev
))
1898 (void) fprintf(stderr
, gettext(
1899 "%s is a member (%s) of pool \"%s\""),
1900 vdev
, zpool_pool_state_to_name(state
), name
);
1903 (void) fprintf(stderr
, gettext(
1904 ". Offline the disk first to clear its label."));
1910 case POOL_STATE_EXPORTED
:
1913 (void) fprintf(stderr
, gettext(
1914 "use '-f' to override the following error:\n"
1915 "%s is a member of exported pool \"%s\"\n"),
1920 case POOL_STATE_POTENTIALLY_ACTIVE
:
1923 (void) fprintf(stderr
, gettext(
1924 "use '-f' to override the following error:\n"
1925 "%s is a member of potentially active pool \"%s\"\n"),
1930 case POOL_STATE_DESTROYED
:
1931 /* inuse should never be set for a destroyed pool */
1937 ret
= zpool_clear_label(fd
);
1939 (void) fprintf(stderr
,
1940 gettext("failed to clear label for %s\n"), vdev
);
1951 * zpool create [-fnd] [-o property=value] ...
1952 * [-O file-system-property=value] ...
1953 * [-R root] [-m mountpoint] <pool> <dev> ...
1955 * -f Force creation, even if devices appear in use
1956 * -n Do not create the pool, but display the resulting layout if it
1957 * were to be created.
1958 * -R Create a pool under an alternate root
1959 * -m Set default mountpoint for the root dataset. By default it's
1961 * -o Set property=value.
1962 * -o Set feature@feature=enabled|disabled.
1963 * -d Don't automatically enable all supported pool features
1964 * (individual features can be enabled with -o).
1965 * -O Set fsproperty=value in the pool's root file system
1967 * Creates the named pool according to the given vdev specification. The
1968 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1969 * Once we get the nvlist back from make_root_vdev(), we either print out the
1970 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1973 zpool_do_create(int argc
, char **argv
)
1975 boolean_t force
= B_FALSE
;
1976 boolean_t dryrun
= B_FALSE
;
1977 boolean_t enable_pool_features
= B_TRUE
;
1980 nvlist_t
*nvroot
= NULL
;
1984 char *altroot
= NULL
;
1985 char *compat
= NULL
;
1986 char *mountpoint
= NULL
;
1987 nvlist_t
*fsprops
= NULL
;
1988 nvlist_t
*props
= NULL
;
1992 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
2001 enable_pool_features
= B_FALSE
;
2005 if (add_prop_list(zpool_prop_to_name(
2006 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2008 if (add_prop_list_default(zpool_prop_to_name(
2009 ZPOOL_PROP_CACHEFILE
), "none", &props
))
2013 /* Equivalent to -O mountpoint=optarg */
2014 mountpoint
= optarg
;
2017 if ((propval
= strchr(optarg
, '=')) == NULL
) {
2018 (void) fprintf(stderr
, gettext("missing "
2019 "'=' for -o option\n"));
2025 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
2029 * If the user is creating a pool that doesn't support
2030 * feature flags, don't enable any features.
2032 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
2036 ver
= strtoull(propval
, &end
, 0);
2038 ver
< SPA_VERSION_FEATURES
) {
2039 enable_pool_features
= B_FALSE
;
2042 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
2044 if (zpool_name_to_prop(optarg
) ==
2045 ZPOOL_PROP_COMPATIBILITY
)
2049 if ((propval
= strchr(optarg
, '=')) == NULL
) {
2050 (void) fprintf(stderr
, gettext("missing "
2051 "'=' for -O option\n"));
2058 * Mountpoints are checked and then added later.
2059 * Uniquely among properties, they can be specified
2060 * more than once, to avoid conflict with -m.
2062 if (0 == strcmp(optarg
,
2063 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
2064 mountpoint
= propval
;
2065 } else if (add_prop_list(optarg
, propval
, &fsprops
,
2072 * Sanity check temporary pool name.
2074 if (strchr(optarg
, '/') != NULL
) {
2075 (void) fprintf(stderr
, gettext("cannot create "
2076 "'%s': invalid character '/' in temporary "
2078 (void) fprintf(stderr
, gettext("use 'zfs "
2079 "create' to create a dataset\n"));
2083 if (add_prop_list(zpool_prop_to_name(
2084 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
2086 if (add_prop_list_default(zpool_prop_to_name(
2087 ZPOOL_PROP_CACHEFILE
), "none", &props
))
2092 (void) fprintf(stderr
, gettext("missing argument for "
2093 "'%c' option\n"), optopt
);
2096 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2105 /* get pool name and check number of arguments */
2107 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
2111 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
2118 * As a special case, check for use of '/' in the name, and direct the
2119 * user to use 'zfs create' instead.
2121 if (strchr(poolname
, '/') != NULL
) {
2122 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
2123 "character '/' in pool name\n"), poolname
);
2124 (void) fprintf(stderr
, gettext("use 'zfs create' to "
2125 "create a dataset\n"));
2129 /* pass off to make_root_vdev for bulk processing */
2130 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
2131 argc
- 1, argv
+ 1);
2135 /* make_root_vdev() allows 0 toplevel children if there are spares */
2136 if (!zfs_allocatable_devs(nvroot
)) {
2137 (void) fprintf(stderr
, gettext("invalid vdev "
2138 "specification: at least one toplevel vdev must be "
2143 if (altroot
!= NULL
&& altroot
[0] != '/') {
2144 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
2145 "must be an absolute path\n"), altroot
);
2150 * Check the validity of the mountpoint and direct the user to use the
2151 * '-m' mountpoint option if it looks like its in use.
2153 if (mountpoint
== NULL
||
2154 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
2155 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
2156 char buf
[MAXPATHLEN
];
2159 if (mountpoint
&& mountpoint
[0] != '/') {
2160 (void) fprintf(stderr
, gettext("invalid mountpoint "
2161 "'%s': must be an absolute path, 'legacy', or "
2162 "'none'\n"), mountpoint
);
2166 if (mountpoint
== NULL
) {
2167 if (altroot
!= NULL
)
2168 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
2171 (void) snprintf(buf
, sizeof (buf
), "/%s",
2174 if (altroot
!= NULL
)
2175 (void) snprintf(buf
, sizeof (buf
), "%s%s",
2176 altroot
, mountpoint
);
2178 (void) snprintf(buf
, sizeof (buf
), "%s",
2182 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
2183 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
2184 "%s\n"), buf
, strerror(errno
));
2185 (void) fprintf(stderr
, gettext("use '-m' "
2186 "option to provide a different default\n"));
2191 while (count
< 3 && readdir(dirp
) != NULL
)
2193 (void) closedir(dirp
);
2196 (void) fprintf(stderr
, gettext("mountpoint "
2197 "'%s' exists and is not empty\n"), buf
);
2198 (void) fprintf(stderr
, gettext("use '-m' "
2199 "option to provide a "
2200 "different default\n"));
2207 * Now that the mountpoint's validity has been checked, ensure that
2208 * the property is set appropriately prior to creating the pool.
2210 if (mountpoint
!= NULL
) {
2211 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
2212 mountpoint
, &fsprops
, B_FALSE
);
2220 * For a dry run invocation, print out a basic message and run
2221 * through all the vdevs in the list and print out in an
2222 * appropriate hierarchy.
2224 (void) printf(gettext("would create '%s' with the "
2225 "following layout:\n\n"), poolname
);
2227 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
2228 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
2229 VDEV_ALLOC_BIAS_DEDUP
, 0);
2230 print_vdev_tree(NULL
, "special", nvroot
, 0,
2231 VDEV_ALLOC_BIAS_SPECIAL
, 0);
2232 print_vdev_tree(NULL
, "logs", nvroot
, 0,
2233 VDEV_ALLOC_BIAS_LOG
, 0);
2234 print_cache_list(nvroot
, 0);
2235 print_spare_list(nvroot
, 0);
2240 * Load in feature set.
2241 * Note: if compatibility property not given, we'll have
2242 * NULL, which means 'all features'.
2244 boolean_t requested_features
[SPA_FEATURES
];
2245 if (zpool_do_load_compat(compat
, requested_features
) !=
2246 ZPOOL_COMPATIBILITY_OK
)
2250 * props contains list of features to enable.
2252 * - remove it if feature@name=disabled
2253 * - leave it there if feature@name=enabled
2255 * - enable_pool_features (ie: no '-d' or '-o version')
2256 * - it's supported by the kernel module
2257 * - it's in the requested feature set
2258 * - warn if it's enabled but not in compat
2260 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
2261 char propname
[MAXPATHLEN
];
2262 const char *propval
;
2263 zfeature_info_t
*feat
= &spa_feature_table
[i
];
2265 (void) snprintf(propname
, sizeof (propname
),
2266 "feature@%s", feat
->fi_uname
);
2268 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
2270 ZFS_FEATURE_DISABLED
) == 0) {
2271 (void) nvlist_remove_all(props
,
2273 } else if (strcmp(propval
,
2274 ZFS_FEATURE_ENABLED
) == 0 &&
2275 !requested_features
[i
]) {
2276 (void) fprintf(stderr
, gettext(
2277 "Warning: feature \"%s\" enabled "
2278 "but is not in specified "
2279 "'compatibility' feature set.\n"),
2283 enable_pool_features
&&
2284 feat
->fi_zfs_mod_supported
&&
2285 requested_features
[i
]) {
2286 ret
= add_prop_list(propname
,
2287 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
2294 if (zpool_create(g_zfs
, poolname
,
2295 nvroot
, props
, fsprops
) == 0) {
2296 zfs_handle_t
*pool
= zfs_open(g_zfs
,
2297 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
2299 if (zfs_mount(pool
, NULL
, 0) == 0) {
2300 ret
= zfs_share(pool
, NULL
);
2301 zfs_commit_shares(NULL
);
2305 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
2306 (void) fprintf(stderr
, gettext("pool name may have "
2312 nvlist_free(nvroot
);
2313 nvlist_free(fsprops
);
2317 nvlist_free(fsprops
);
2324 * zpool destroy <pool>
2326 * -f Forcefully unmount any datasets
2328 * Destroy the given pool. Automatically unmounts any datasets in the pool.
2331 zpool_do_destroy(int argc
, char **argv
)
2333 boolean_t force
= B_FALSE
;
2336 zpool_handle_t
*zhp
;
2340 while ((c
= getopt(argc
, argv
, "f")) != -1) {
2346 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2355 /* check arguments */
2357 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2361 (void) fprintf(stderr
, gettext("too many arguments\n"));
2367 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
2369 * As a special case, check for use of '/' in the name, and
2370 * direct the user to use 'zfs destroy' instead.
2372 if (strchr(pool
, '/') != NULL
)
2373 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
2374 "destroy a dataset\n"));
2378 if (zpool_disable_datasets(zhp
, force
) != 0) {
2379 (void) fprintf(stderr
, gettext("could not destroy '%s': "
2380 "could not unmount datasets\n"), zpool_get_name(zhp
));
2385 /* The history must be logged as part of the export */
2386 log_history
= B_FALSE
;
2388 ret
= (zpool_destroy(zhp
, history_str
) != 0);
2395 typedef struct export_cbdata
{
2397 pthread_mutex_t mnttab_lock
;
2399 boolean_t hardforce
;
2406 export_cbdata_t
*aea_cbdata
;
2407 } async_export_args_t
;
2413 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
2415 export_cbdata_t
*cb
= data
;
2418 * zpool_disable_datasets() is not thread-safe for mnttab access.
2419 * So we serialize access here for 'zpool export -a' parallel case.
2421 if (cb
->tpool
!= NULL
)
2422 pthread_mutex_lock(&cb
->mnttab_lock
);
2424 int retval
= zpool_disable_datasets(zhp
, cb
->force
);
2426 if (cb
->tpool
!= NULL
)
2427 pthread_mutex_unlock(&cb
->mnttab_lock
);
2432 if (cb
->hardforce
) {
2433 if (zpool_export_force(zhp
, history_str
) != 0)
2435 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
2443 * Asynchronous export request
2446 zpool_export_task(void *arg
)
2448 async_export_args_t
*aea
= arg
;
2450 zpool_handle_t
*zhp
= zpool_open(g_zfs
, aea
->aea_poolname
);
2452 int ret
= zpool_export_one(zhp
, aea
->aea_cbdata
);
2454 aea
->aea_cbdata
->retval
= ret
;
2457 aea
->aea_cbdata
->retval
= 1;
2460 free(aea
->aea_poolname
);
2465 * Process an export request in parallel
2468 zpool_export_one_async(zpool_handle_t
*zhp
, void *data
)
2470 tpool_t
*tpool
= ((export_cbdata_t
*)data
)->tpool
;
2471 async_export_args_t
*aea
= safe_malloc(sizeof (async_export_args_t
));
2473 /* save pool name since zhp will go out of scope */
2474 aea
->aea_poolname
= strdup(zpool_get_name(zhp
));
2475 aea
->aea_cbdata
= data
;
2477 /* ship off actual export to another thread */
2478 if (tpool_dispatch(tpool
, zpool_export_task
, (void *)aea
) != 0)
2479 return (errno
); /* unlikely */
2485 * zpool export [-f] <pool> ...
2487 * -a Export all pools
2488 * -f Forcefully unmount datasets
2490 * Export the given pools. By default, the command will attempt to cleanly
2491 * unmount any active datasets within the pool. If the '-f' flag is specified,
2492 * then the datasets will be forcefully unmounted.
2495 zpool_do_export(int argc
, char **argv
)
2498 boolean_t do_all
= B_FALSE
;
2499 boolean_t force
= B_FALSE
;
2500 boolean_t hardforce
= B_FALSE
;
2504 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
2516 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2523 cb
.hardforce
= hardforce
;
2529 /* The history will be logged as part of the export itself */
2530 log_history
= B_FALSE
;
2534 (void) fprintf(stderr
, gettext("too many arguments\n"));
2538 cb
.tpool
= tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN
),
2540 pthread_mutex_init(&cb
.mnttab_lock
, NULL
);
2542 /* Asynchronously call zpool_export_one using thread pool */
2543 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
2544 B_FALSE
, zpool_export_one_async
, &cb
);
2546 tpool_wait(cb
.tpool
);
2547 tpool_destroy(cb
.tpool
);
2548 (void) pthread_mutex_destroy(&cb
.mnttab_lock
);
2550 return (ret
| cb
.retval
);
2553 /* check arguments */
2555 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2559 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
2560 B_FALSE
, zpool_export_one
, &cb
);
2566 * Given a vdev configuration, determine the maximum width needed for the device
2570 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
2573 static const char *const subtypes
[] =
2574 {ZPOOL_CONFIG_SPARES
, ZPOOL_CONFIG_L2CACHE
, ZPOOL_CONFIG_CHILDREN
};
2576 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
2577 max
= MAX(strlen(name
) + depth
, max
);
2582 for (size_t i
= 0; i
< ARRAY_SIZE(subtypes
); ++i
)
2583 if (nvlist_lookup_nvlist_array(nv
, subtypes
[i
],
2584 &child
, &children
) == 0)
2585 for (uint_t c
= 0; c
< children
; ++c
)
2586 max
= MAX(max_width(zhp
, child
[c
], depth
+ 2,
2587 max
, name_flags
), max
);
2592 typedef struct status_cbdata
{
2596 boolean_t cb_allpools
;
2597 boolean_t cb_verbose
;
2598 boolean_t cb_literal
;
2599 boolean_t cb_explain
;
2601 boolean_t cb_dedup_stats
;
2602 boolean_t cb_print_unhealthy
;
2603 boolean_t cb_print_status
;
2604 boolean_t cb_print_slow_ios
;
2605 boolean_t cb_print_dio_verify
;
2606 boolean_t cb_print_vdev_init
;
2607 boolean_t cb_print_vdev_trim
;
2608 vdev_cmd_data_list_t
*vcdl
;
2609 boolean_t cb_print_power
;
2611 boolean_t cb_flat_vdevs
;
2613 boolean_t cb_json_as_int
;
2614 boolean_t cb_json_pool_key_guid
;
2617 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2619 is_blank_str(const char *str
)
2621 for (; str
!= NULL
&& *str
!= '\0'; ++str
)
2628 zpool_nvlist_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, const char *path
,
2631 vdev_cmd_data_t
*data
;
2636 for (i
= 0; i
< vcdl
->count
; i
++) {
2637 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2638 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0))
2641 data
= &vcdl
->data
[i
];
2642 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2644 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2645 if (strcmp(data
->cols
[k
],
2646 vcdl
->uniq_cols
[j
]) == 0) {
2647 val
= data
->lines
[k
];
2651 if (val
== NULL
|| is_blank_str(val
))
2653 fnvlist_add_string(item
, vcdl
->uniq_cols
[j
], val
);
2656 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2657 if (data
->lines
[j
]) {
2658 snprintf(tmp
, 256, "extra_%d", k
++);
2659 fnvlist_add_string(item
, tmp
,
2667 /* Print command output lines for specific vdev in a specific pool */
2669 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, const char *path
)
2671 vdev_cmd_data_t
*data
;
2675 for (i
= 0; i
< vcdl
->count
; i
++) {
2676 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2677 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2678 /* Not the vdev we're looking for */
2682 data
= &vcdl
->data
[i
];
2683 /* Print out all the output values for this vdev */
2684 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2686 /* Does this vdev have values for this column? */
2687 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2688 if (strcmp(data
->cols
[k
],
2689 vcdl
->uniq_cols
[j
]) == 0) {
2690 /* yes it does, record the value */
2691 val
= data
->lines
[k
];
2696 * Mark empty values with dashes to make output
2699 if (val
== NULL
|| is_blank_str(val
))
2702 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2703 if (j
< vcdl
->uniq_cols_cnt
- 1)
2707 /* Print out any values that aren't in a column at the end */
2708 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2709 /* Did we have any columns? If so print a spacer. */
2710 if (vcdl
->uniq_cols_cnt
> 0)
2713 val
= data
->lines
[j
];
2714 fputs(val
?: "", stdout
);
2721 * Print vdev initialization status for leaves
2724 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2727 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2728 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2729 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2730 !vs
->vs_scan_removing
) {
2734 time_t t
= vs
->vs_initialize_action_time
;
2735 int initialize_pct
= 100;
2736 if (vs
->vs_initialize_state
!=
2737 VDEV_INITIALIZE_COMPLETE
) {
2738 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2739 100 / (vs
->vs_initialize_bytes_est
+ 1));
2742 (void) ctime_r(&t
, tbuf
);
2745 switch (vs
->vs_initialize_state
) {
2746 case VDEV_INITIALIZE_SUSPENDED
:
2747 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2748 gettext("suspended, started at"), tbuf
);
2750 case VDEV_INITIALIZE_ACTIVE
:
2751 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2752 gettext("started at"), tbuf
);
2754 case VDEV_INITIALIZE_COMPLETE
:
2755 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2756 gettext("completed at"), tbuf
);
2760 (void) printf(gettext(" (%d%% initialized%s)"),
2761 initialize_pct
, zbuf
);
2763 (void) printf(gettext(" (uninitialized)"));
2765 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2766 (void) printf(gettext(" (initializing)"));
2771 * Print vdev TRIM status for leaves
2774 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2777 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2778 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2779 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2780 !vs
->vs_scan_removing
) {
2784 time_t t
= vs
->vs_trim_action_time
;
2786 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2787 trim_pct
= (vs
->vs_trim_bytes_done
*
2788 100 / (vs
->vs_trim_bytes_est
+ 1));
2791 (void) ctime_r(&t
, tbuf
);
2794 switch (vs
->vs_trim_state
) {
2795 case VDEV_TRIM_SUSPENDED
:
2796 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2797 gettext("suspended, started at"), tbuf
);
2799 case VDEV_TRIM_ACTIVE
:
2800 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2801 gettext("started at"), tbuf
);
2803 case VDEV_TRIM_COMPLETE
:
2804 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2805 gettext("completed at"), tbuf
);
2809 (void) printf(gettext(" (%d%% trimmed%s)"),
2811 } else if (vs
->vs_trim_notsup
) {
2812 (void) printf(gettext(" (trim unsupported)"));
2814 (void) printf(gettext(" (untrimmed)"));
2816 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2817 (void) printf(gettext(" (trimming)"));
2822 * Return the color associated with a health string. This includes returning
2823 * NULL for no color change.
2826 health_str_to_color(const char *health
)
2828 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2829 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2830 strcmp(health
, gettext("UNAVAIL")) == 0) {
2834 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2835 strcmp(health
, gettext("DEGRADED")) == 0 ||
2836 strcmp(health
, gettext("REMOVED")) == 0) {
2837 return (ANSI_YELLOW
);
2844 * Called for each leaf vdev. Returns 0 if the vdev is healthy.
2845 * A vdev is unhealthy if any of the following are true:
2846 * 1) there are read, write, or checksum errors,
2847 * 2) its state is not ONLINE, or
2848 * 3) slow IO reporting was requested (-s) and there are slow IOs.
2851 vdev_health_check_cb(void *hdl_data
, nvlist_t
*nv
, void *data
)
2853 status_cbdata_t
*cb
= data
;
2858 if (nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2859 (uint64_t **)&vs
, &vsc
) != 0)
2862 if (vs
->vs_checksum_errors
|| vs
->vs_read_errors
||
2863 vs
->vs_write_errors
|| vs
->vs_state
!= VDEV_STATE_HEALTHY
)
2866 if (cb
->cb_print_slow_ios
&& vs
->vs_slow_ios
)
2873 * Print out configuration state as requested by status_callback.
2876 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2877 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2879 nvlist_t
**child
, *root
;
2880 uint_t c
, i
, vsc
, children
;
2881 pool_scan_stat_t
*ps
= NULL
;
2883 char rbuf
[6], wbuf
[6], cbuf
[6], dbuf
[6];
2885 uint64_t notpresent
;
2886 spare_cbdata_t spare_cb
;
2889 const char *path
= NULL
;
2890 const char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
,
2893 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2894 &child
, &children
) != 0)
2897 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2898 (uint64_t **)&vs
, &vsc
) == 0);
2900 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2902 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2905 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2909 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2912 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2913 state
= gettext("INUSE");
2914 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2915 state
= gettext("AVAIL");
2919 * If '-e' is specified then top-level vdevs and their children
2920 * can be pruned if all of their leaves are healthy.
2922 if (cb
->cb_print_unhealthy
&& depth
> 0 &&
2923 for_each_vdev_in_nvlist(nv
, vdev_health_check_cb
, cb
) == 0) {
2927 printf_color(health_str_to_color(state
),
2928 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2932 if (vs
->vs_read_errors
)
2935 if (vs
->vs_write_errors
)
2938 if (vs
->vs_checksum_errors
)
2941 if (vs
->vs_slow_ios
)
2944 if (cb
->cb_literal
) {
2946 printf_color(rcolor
, "%5llu",
2947 (u_longlong_t
)vs
->vs_read_errors
);
2949 printf_color(wcolor
, "%5llu",
2950 (u_longlong_t
)vs
->vs_write_errors
);
2952 printf_color(ccolor
, "%5llu",
2953 (u_longlong_t
)vs
->vs_checksum_errors
);
2955 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2956 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2957 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2960 printf_color(rcolor
, "%5s", rbuf
);
2962 printf_color(wcolor
, "%5s", wbuf
);
2964 printf_color(ccolor
, "%5s", cbuf
);
2966 if (cb
->cb_print_slow_ios
) {
2967 if (children
== 0) {
2968 /* Only leafs vdevs have slow IOs */
2969 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2972 snprintf(rbuf
, sizeof (rbuf
), "-");
2976 printf_color(scolor
, " %5llu",
2977 (u_longlong_t
)vs
->vs_slow_ios
);
2979 printf_color(scolor
, " %5s", rbuf
);
2981 if (cb
->cb_print_power
) {
2982 if (children
== 0) {
2983 /* Only leaf vdevs have physical slots */
2984 switch (zpool_power_current_state(zhp
, (char *)
2985 fnvlist_lookup_string(nv
,
2986 ZPOOL_CONFIG_PATH
))) {
2988 printf_color(ANSI_RED
, " %5s",
2992 printf(" %5s", gettext("on"));
2995 printf(" %5s", "-");
2998 printf(" %5s", "-");
3001 if (VDEV_STAT_VALID(vs_dio_verify_errors
, vsc
) &&
3002 cb
->cb_print_dio_verify
) {
3003 zfs_nicenum(vs
->vs_dio_verify_errors
, dbuf
,
3008 (u_longlong_t
)vs
->vs_dio_verify_errors
);
3010 printf(" %5s", dbuf
);
3014 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
3015 ¬present
) == 0) {
3016 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
3017 (void) printf(" %s %s", gettext("was"), path
);
3018 } else if (vs
->vs_aux
!= 0) {
3020 color_start(ANSI_RED
);
3021 switch (vs
->vs_aux
) {
3022 case VDEV_AUX_OPEN_FAILED
:
3023 (void) printf(gettext("cannot open"));
3026 case VDEV_AUX_BAD_GUID_SUM
:
3027 (void) printf(gettext("missing device"));
3030 case VDEV_AUX_NO_REPLICAS
:
3031 (void) printf(gettext("insufficient replicas"));
3034 case VDEV_AUX_VERSION_NEWER
:
3035 (void) printf(gettext("newer version"));
3038 case VDEV_AUX_UNSUP_FEAT
:
3039 (void) printf(gettext("unsupported feature(s)"));
3042 case VDEV_AUX_ASHIFT_TOO_BIG
:
3043 (void) printf(gettext("unsupported minimum blocksize"));
3046 case VDEV_AUX_SPARED
:
3047 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3048 &spare_cb
.cb_guid
) == 0);
3049 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
3050 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
3051 zpool_get_name(zhp
)) == 0)
3052 (void) printf(gettext("currently in "
3055 (void) printf(gettext("in use by "
3057 zpool_get_name(spare_cb
.cb_zhp
));
3058 zpool_close(spare_cb
.cb_zhp
);
3060 (void) printf(gettext("currently in use"));
3064 case VDEV_AUX_ERR_EXCEEDED
:
3065 if (vs
->vs_read_errors
+ vs
->vs_write_errors
+
3066 vs
->vs_checksum_errors
== 0 && children
== 0 &&
3067 vs
->vs_slow_ios
> 0) {
3068 (void) printf(gettext("too many slow I/Os"));
3070 (void) printf(gettext("too many errors"));
3074 case VDEV_AUX_IO_FAILURE
:
3075 (void) printf(gettext("experienced I/O failures"));
3078 case VDEV_AUX_BAD_LOG
:
3079 (void) printf(gettext("bad intent log"));
3082 case VDEV_AUX_EXTERNAL
:
3083 (void) printf(gettext("external device fault"));
3086 case VDEV_AUX_SPLIT_POOL
:
3087 (void) printf(gettext("split into new pool"));
3090 case VDEV_AUX_ACTIVE
:
3091 (void) printf(gettext("currently in use"));
3094 case VDEV_AUX_CHILDREN_OFFLINE
:
3095 (void) printf(gettext("all children offline"));
3098 case VDEV_AUX_BAD_LABEL
:
3099 (void) printf(gettext("invalid label"));
3103 (void) printf(gettext("corrupted data"));
3107 } else if (children
== 0 && !isspare
&&
3108 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
3109 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
3110 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
3112 gettext(" block size: %dB configured, %dB native"),
3113 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
3116 if (vs
->vs_scan_removing
!= 0) {
3117 (void) printf(gettext(" (removing)"));
3118 } else if (VDEV_STAT_VALID(vs_noalloc
, vsc
) && vs
->vs_noalloc
!= 0) {
3119 (void) printf(gettext(" (non-allocating)"));
3122 /* The root vdev has the scrub/resilver stats */
3123 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3124 ZPOOL_CONFIG_VDEV_TREE
);
3125 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
3126 (uint64_t **)&ps
, &c
);
3129 * If you force fault a drive that's resilvering, its scan stats can
3130 * get frozen in time, giving the false impression that it's
3131 * being resilvered. That's why we check the state to see if the vdev
3132 * is healthy before reporting "resilvering" or "repairing".
3134 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0 &&
3135 vs
->vs_state
== VDEV_STATE_HEALTHY
) {
3136 if (vs
->vs_scan_processed
!= 0) {
3137 (void) printf(gettext(" (%s)"),
3138 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
3139 "resilvering" : "repairing");
3140 } else if (vs
->vs_resilver_deferred
) {
3141 (void) printf(gettext(" (awaiting resilver)"));
3145 /* The top-level vdevs have the rebuild stats */
3146 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
3147 children
== 0 && vs
->vs_state
== VDEV_STATE_HEALTHY
) {
3148 if (vs
->vs_rebuild_processed
!= 0) {
3149 (void) printf(gettext(" (resilvering)"));
3153 if (cb
->vcdl
!= NULL
) {
3154 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3156 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3160 /* Display vdev initialization and trim status for leaves. */
3161 if (children
== 0) {
3162 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
3163 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
3166 (void) printf("\n");
3168 for (c
= 0; c
< children
; c
++) {
3169 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
3171 /* Don't print logs or holes here */
3172 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3174 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3176 if (islog
|| ishole
)
3178 /* Only print normal classes here */
3179 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
3182 /* Provide vdev_rebuild_stats to children if available */
3184 (void) nvlist_lookup_uint64_array(nv
,
3185 ZPOOL_CONFIG_REBUILD_STATS
,
3186 (uint64_t **)&vrs
, &i
);
3189 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3190 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
3191 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
3198 * Print the configuration of an exported pool. Iterate over all vdevs in the
3199 * pool, printing out the name and status for each one.
3202 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
3211 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3212 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
3213 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
3216 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3217 (uint64_t **)&vs
, &c
) == 0);
3219 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
3220 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
3222 if (vs
->vs_aux
!= 0) {
3225 switch (vs
->vs_aux
) {
3226 case VDEV_AUX_OPEN_FAILED
:
3227 (void) printf(gettext("cannot open"));
3230 case VDEV_AUX_BAD_GUID_SUM
:
3231 (void) printf(gettext("missing device"));
3234 case VDEV_AUX_NO_REPLICAS
:
3235 (void) printf(gettext("insufficient replicas"));
3238 case VDEV_AUX_VERSION_NEWER
:
3239 (void) printf(gettext("newer version"));
3242 case VDEV_AUX_UNSUP_FEAT
:
3243 (void) printf(gettext("unsupported feature(s)"));
3246 case VDEV_AUX_ERR_EXCEEDED
:
3247 (void) printf(gettext("too many errors"));
3250 case VDEV_AUX_ACTIVE
:
3251 (void) printf(gettext("currently in use"));
3254 case VDEV_AUX_CHILDREN_OFFLINE
:
3255 (void) printf(gettext("all children offline"));
3258 case VDEV_AUX_BAD_LABEL
:
3259 (void) printf(gettext("invalid label"));
3263 (void) printf(gettext("corrupted data"));
3267 (void) printf("\n");
3269 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3270 &child
, &children
) != 0)
3273 for (c
= 0; c
< children
; c
++) {
3274 uint64_t is_log
= B_FALSE
;
3276 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3280 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
3283 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
3284 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
3285 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
3289 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3290 &child
, &children
) == 0) {
3291 (void) printf(gettext("\tcache\n"));
3292 for (c
= 0; c
< children
; c
++) {
3293 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
3295 (void) printf("\t %s\n", vname
);
3300 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
3301 &child
, &children
) == 0) {
3302 (void) printf(gettext("\tspares\n"));
3303 for (c
= 0; c
< children
; c
++) {
3304 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
3306 (void) printf("\t %s\n", vname
);
3313 * Print specialized class vdevs.
3315 * These are recorded as top level vdevs in the main pool child array
3316 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
3317 * print_status_config() or print_import_config() to print the top level
3318 * class vdevs then any of their children (eg mirrored slogs) are printed
3319 * recursively - which works because only the top level vdev is marked.
3322 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
3327 boolean_t printed
= B_FALSE
;
3329 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
3331 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
3335 for (c
= 0; c
< children
; c
++) {
3336 uint64_t is_log
= B_FALSE
;
3337 const char *bias
= NULL
;
3338 const char *type
= NULL
;
3340 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3344 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
3346 (void) nvlist_lookup_string(child
[c
],
3347 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
3348 (void) nvlist_lookup_string(child
[c
],
3349 ZPOOL_CONFIG_TYPE
, &type
);
3352 if (bias
== NULL
|| strcmp(bias
, class) != 0)
3354 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
3358 (void) printf("\t%s\t\n", gettext(class));
3362 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3363 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
3364 if (cb
->cb_print_status
)
3365 print_status_config(zhp
, cb
, name
, child
[c
], 2,
3368 print_import_config(cb
, name
, child
[c
], 2);
3374 * Display the status for the given pool.
3377 show_import(nvlist_t
*config
, boolean_t report_error
)
3379 uint64_t pool_state
;
3383 uint64_t hostid
= 0;
3385 const char *hostname
= "unknown";
3386 nvlist_t
*nvroot
, *nvinfo
;
3387 zpool_status_t reason
;
3388 zpool_errata_t errata
;
3391 const char *comment
;
3394 status_cbdata_t cb
= { 0 };
3396 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3398 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3400 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3402 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3405 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
3406 (uint64_t **)&vs
, &vsc
) == 0);
3407 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
3409 reason
= zpool_import_status(config
, &msgid
, &errata
);
3412 * If we're importing using a cachefile, then we won't report any
3413 * errors unless we are in the scan phase of the import.
3415 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
3418 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0) {
3425 (void) printf(gettext("%s pool: %s\n"), indent
, name
);
3426 (void) printf(gettext("%s id: %llu\n"), indent
, (u_longlong_t
)guid
);
3427 (void) printf(gettext("%s state: %s"), indent
, health
);
3428 if (pool_state
== POOL_STATE_DESTROYED
)
3429 (void) printf(gettext(" (DESTROYED)"));
3430 (void) printf("\n");
3432 if (reason
!= ZPOOL_STATUS_OK
) {
3433 (void) printf("%s", indent
);
3434 printf_color(ANSI_BOLD
, gettext("status: "));
3437 case ZPOOL_STATUS_MISSING_DEV_R
:
3438 case ZPOOL_STATUS_MISSING_DEV_NR
:
3439 case ZPOOL_STATUS_BAD_GUID_SUM
:
3440 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
3441 "missing from the system.\n"));
3444 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
3445 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
3446 printf_color(ANSI_YELLOW
, gettext("One or more devices "
3447 "contains corrupted data.\n"));
3450 case ZPOOL_STATUS_CORRUPT_DATA
:
3451 printf_color(ANSI_YELLOW
, gettext("The pool data is "
3455 case ZPOOL_STATUS_OFFLINE_DEV
:
3456 printf_color(ANSI_YELLOW
, gettext("One or more devices "
3457 "are offlined.\n"));
3460 case ZPOOL_STATUS_CORRUPT_POOL
:
3461 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
3465 case ZPOOL_STATUS_VERSION_OLDER
:
3466 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
3467 "a legacy on-disk version.\n"));
3470 case ZPOOL_STATUS_VERSION_NEWER
:
3471 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
3472 "an incompatible version.\n"));
3475 case ZPOOL_STATUS_FEAT_DISABLED
:
3476 printf_color(ANSI_YELLOW
, gettext("Some supported "
3477 "features are not enabled on the pool.\n"
3478 "\t%s(Note that they may be intentionally disabled if the\n"
3479 "\t%s'compatibility' property is set.)\n"), indent
, indent
);
3482 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
3483 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
3484 "the file(s) indicated by the 'compatibility'\n"
3485 "\t%sproperty.\n"), indent
);
3488 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
3489 printf_color(ANSI_YELLOW
, gettext("One or more features "
3490 "are enabled on the pool despite not being\n"
3491 "\t%srequested by the 'compatibility' property.\n"),
3495 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3496 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
3497 "feature(s) not supported on this system:\n"));
3498 color_start(ANSI_YELLOW
);
3499 zpool_collect_unsup_feat(config
, buf
, 2048);
3500 (void) printf("%s", buf
);
3504 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3505 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
3506 "accessed in read-only mode on this system. It\n"
3507 "\t%scannot be accessed in read-write mode because it uses "
3509 "\t%sfeature(s) not supported on this system:\n"),
3511 color_start(ANSI_YELLOW
);
3512 zpool_collect_unsup_feat(config
, buf
, 2048);
3513 (void) printf("%s", buf
);
3517 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3518 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
3519 "imported by another system.\n"));
3522 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3523 printf_color(ANSI_YELLOW
, gettext("The pool has the "
3524 "multihost property on. It cannot\n"
3525 "\t%sbe safely imported when the system hostid is not "
3529 case ZPOOL_STATUS_HOSTID_MISMATCH
:
3530 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
3531 "by another system.\n"));
3534 case ZPOOL_STATUS_FAULTED_DEV_R
:
3535 case ZPOOL_STATUS_FAULTED_DEV_NR
:
3536 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
3540 case ZPOOL_STATUS_BAD_LOG
:
3541 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
3545 case ZPOOL_STATUS_RESILVERING
:
3546 case ZPOOL_STATUS_REBUILDING
:
3547 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
3548 "being resilvered.\n"));
3551 case ZPOOL_STATUS_ERRATA
:
3552 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
3556 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
3557 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
3558 "configured to use a non-native block size.\n"
3559 "\t%sExpect reduced performance.\n"), indent
);
3564 * No other status can be seen when importing pools.
3566 assert(reason
== ZPOOL_STATUS_OK
);
3570 * Print out an action according to the overall state of the pool.
3572 if (vs
->vs_state
!= VDEV_STATE_HEALTHY
||
3573 reason
!= ZPOOL_STATUS_ERRATA
|| errata
!= ZPOOL_ERRATA_NONE
) {
3574 (void) printf("%s", indent
);
3575 (void) printf(gettext("action: "));
3577 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
3578 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
3579 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
3580 (void) printf(gettext("The pool can be imported using "
3581 "its name or numeric identifier, though\n"
3582 "\t%ssome features will not be available without "
3583 "an explicit 'zpool upgrade'.\n"), indent
);
3584 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
3585 (void) printf(gettext("The pool can be imported using "
3586 "its name or numeric\n"
3587 "\t%sidentifier, though the file(s) indicated by "
3588 "its 'compatibility'\n"
3589 "\t%sproperty cannot be parsed at this time.\n"),
3591 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
3592 (void) printf(gettext("The pool can be imported using "
3593 "its name or numeric identifier and\n"
3594 "\t%sthe '-f' flag.\n"), indent
);
3595 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
3597 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
3598 (void) printf(gettext("The pool can be "
3599 "imported using its name or numeric "
3601 "\t%showever there is a compatibility "
3602 "issue which should be corrected\n"
3603 "\t%sby running 'zpool scrub'\n"),
3607 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
3608 (void) printf(gettext("The pool cannot be "
3609 "imported with this version of ZFS due to\n"
3610 "\t%san active asynchronous destroy. "
3611 "Revert to an earlier version\n"
3612 "\t%sand allow the destroy to complete "
3613 "before updating.\n"), indent
, indent
);
3616 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
3617 (void) printf(gettext("Existing encrypted "
3618 "datasets contain an on-disk "
3619 "incompatibility, which\n"
3620 "\t%sneeds to be corrected. Backup these "
3621 "datasets to new encrypted datasets\n"
3622 "\t%sand destroy the old ones.\n"),
3626 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
3627 (void) printf(gettext("Existing encrypted "
3628 "snapshots and bookmarks contain an "
3630 "\t%sincompatibility. This may cause "
3631 "on-disk corruption if they are used\n"
3632 "\t%swith 'zfs recv'. To correct the "
3633 "issue, enable the bookmark_v2 feature.\n"
3634 "\t%sNo additional action is needed if "
3635 "there are no encrypted snapshots or\n"
3636 "\t%sbookmarks. If preserving the "
3637 "encrypted snapshots and bookmarks is\n"
3638 "\t%srequired, use a non-raw send to "
3639 "backup and restore them. Alternately,\n"
3640 "\t%sthey may be removed to resolve the "
3641 "incompatibility.\n"), indent
, indent
,
3642 indent
, indent
, indent
, indent
);
3646 * All errata must contain an action message.
3648 assert(errata
== ZPOOL_ERRATA_NONE
);
3651 (void) printf(gettext("The pool can be imported using "
3652 "its name or numeric identifier.\n"));
3654 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
3655 (void) printf(gettext("The pool can be imported despite "
3656 "missing or damaged devices. The\n"
3657 "\t%sfault tolerance of the pool may be compromised if "
3658 "imported.\n"), indent
);
3661 case ZPOOL_STATUS_VERSION_NEWER
:
3662 (void) printf(gettext("The pool cannot be imported. "
3663 "Access the pool on a system running newer\n"
3664 "\t%ssoftware, or recreate the pool from "
3665 "backup.\n"), indent
);
3667 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3668 (void) printf(gettext("The pool cannot be imported. "
3669 "Access the pool on a system that supports\n"
3670 "\t%sthe required feature(s), or recreate the pool "
3671 "from backup.\n"), indent
);
3673 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3674 (void) printf(gettext("The pool cannot be imported in "
3675 "read-write mode. Import the pool with\n"
3676 "\t%s'-o readonly=on', access the pool on a system "
3677 "that supports the\n"
3678 "\t%srequired feature(s), or recreate the pool "
3679 "from backup.\n"), indent
, indent
);
3681 case ZPOOL_STATUS_MISSING_DEV_R
:
3682 case ZPOOL_STATUS_MISSING_DEV_NR
:
3683 case ZPOOL_STATUS_BAD_GUID_SUM
:
3684 (void) printf(gettext("The pool cannot be imported. "
3685 "Attach the missing\n"
3686 "\t%sdevices and try again.\n"), indent
);
3688 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3689 VERIFY0(nvlist_lookup_nvlist(config
,
3690 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3692 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3693 hostname
= fnvlist_lookup_string(nvinfo
,
3694 ZPOOL_CONFIG_MMP_HOSTNAME
);
3696 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3697 hostid
= fnvlist_lookup_uint64(nvinfo
,
3698 ZPOOL_CONFIG_MMP_HOSTID
);
3700 (void) printf(gettext("The pool must be exported from "
3701 "%s (hostid=%"PRIx64
")\n"
3702 "\t%sbefore it can be safely imported.\n"),
3703 hostname
, hostid
, indent
);
3705 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3706 (void) printf(gettext("Set a unique system hostid with "
3707 "the zgenhostid(8) command.\n"));
3710 (void) printf(gettext("The pool cannot be imported due "
3711 "to damaged devices or data.\n"));
3715 /* Print the comment attached to the pool. */
3716 if (comment
!= NULL
)
3717 (void) printf(gettext("comment: %s\n"), comment
);
3720 * If the state is "closed" or "can't open", and the aux state
3721 * is "corrupt data":
3723 if ((vs
->vs_state
== VDEV_STATE_CLOSED
||
3724 vs
->vs_state
== VDEV_STATE_CANT_OPEN
) &&
3725 vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
) {
3726 if (pool_state
== POOL_STATE_DESTROYED
)
3727 (void) printf(gettext("\t%sThe pool was destroyed, "
3728 "but can be imported using the '-Df' flags.\n"),
3730 else if (pool_state
!= POOL_STATE_EXPORTED
)
3731 (void) printf(gettext("\t%sThe pool may be active on "
3732 "another system, but can be imported using\n"
3733 "\t%sthe '-f' flag.\n"), indent
, indent
);
3736 if (msgid
!= NULL
) {
3737 (void) printf(gettext("%s see: "
3738 "https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3742 (void) printf(gettext("%sconfig:\n\n"), indent
);
3744 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3746 if (cb
.cb_namewidth
< 10)
3747 cb
.cb_namewidth
= 10;
3749 print_import_config(&cb
, name
, nvroot
, 0);
3751 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3752 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3753 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3755 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3756 (void) printf(gettext("\n\t%sAdditional devices are known to "
3757 "be part of this pool, though their\n"
3758 "\t%sexact configuration cannot be determined.\n"),
3765 zfs_force_import_required(nvlist_t
*config
)
3768 uint64_t hostid
= 0;
3771 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3772 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3775 * The hostid on LOAD_INFO comes from the MOS label via
3776 * spa_tryimport(). If its not there then we're likely talking to an
3777 * older kernel, so use the top one, which will be from the label
3778 * discovered in zpool_find_import(), or if a cachefile is in use, the
3781 if (nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_HOSTID
, &hostid
) != 0)
3782 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
3785 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3788 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3789 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3790 ZPOOL_CONFIG_MMP_STATE
);
3792 if (mmp_state
!= MMP_STATE_INACTIVE
)
3800 * Perform the import for the given configuration. This passes the heavy
3801 * lifting off to zpool_import_props(), and then mounts the datasets contained
3805 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3806 nvlist_t
*props
, int flags
, uint_t mntthreads
)
3810 zpool_handle_t
*zhp
;
3814 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3815 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3817 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3818 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3819 "is formatted using an unsupported ZFS version\n"), name
);
3821 } else if (zfs_force_import_required(config
) &&
3822 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3823 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3826 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3827 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3828 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3829 ZPOOL_CONFIG_MMP_STATE
);
3831 if (mmp_state
== MMP_STATE_ACTIVE
) {
3832 const char *hostname
= "<unknown>";
3833 uint64_t hostid
= 0;
3835 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3836 hostname
= fnvlist_lookup_string(nvinfo
,
3837 ZPOOL_CONFIG_MMP_HOSTNAME
);
3839 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3840 hostid
= fnvlist_lookup_uint64(nvinfo
,
3841 ZPOOL_CONFIG_MMP_HOSTID
);
3843 (void) fprintf(stderr
, gettext("cannot import '%s': "
3844 "pool is imported on %s (hostid: "
3845 "0x%"PRIx64
")\nExport the pool on the other "
3846 "system, then run 'zpool import'.\n"),
3847 name
, hostname
, hostid
);
3848 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3849 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3850 "pool has the multihost property on and the\n"
3851 "system's hostid is not set. Set a unique hostid "
3852 "with the zgenhostid(8) command.\n"), name
);
3854 const char *hostname
= "<unknown>";
3855 time_t timestamp
= 0;
3856 uint64_t hostid
= 0;
3858 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_HOSTNAME
))
3859 hostname
= fnvlist_lookup_string(nvinfo
,
3860 ZPOOL_CONFIG_HOSTNAME
);
3861 else if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3862 hostname
= fnvlist_lookup_string(config
,
3863 ZPOOL_CONFIG_HOSTNAME
);
3865 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3866 timestamp
= fnvlist_lookup_uint64(config
,
3867 ZPOOL_CONFIG_TIMESTAMP
);
3869 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_HOSTID
))
3870 hostid
= fnvlist_lookup_uint64(nvinfo
,
3871 ZPOOL_CONFIG_HOSTID
);
3872 else if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3873 hostid
= fnvlist_lookup_uint64(config
,
3874 ZPOOL_CONFIG_HOSTID
);
3876 (void) fprintf(stderr
, gettext("cannot import '%s': "
3877 "pool was previously in use from another system.\n"
3878 "Last accessed by %s (hostid=%"PRIx64
") at %s"
3879 "The pool can be imported, use 'zpool import -f' "
3880 "to import the pool.\n"), name
, hostname
,
3881 hostid
, ctime(×tamp
));
3887 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3890 if (newname
!= NULL
)
3893 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3897 * Loading keys is best effort. We don't want to return immediately
3898 * if it fails but we do want to give the error to the caller.
3900 if (flags
& ZFS_IMPORT_LOAD_KEYS
&&
3901 zfs_crypto_attempt_load_keys(g_zfs
, name
) != 0)
3904 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3905 !(flags
& ZFS_IMPORT_ONLY
)) {
3906 ms_status
= zpool_enable_datasets(zhp
, mntopts
, 0, mntthreads
);
3907 if (ms_status
== EZFS_SHAREFAILED
) {
3908 (void) fprintf(stderr
, gettext("Import was "
3909 "successful, but unable to share some datasets\n"));
3910 } else if (ms_status
== EZFS_MOUNTFAILED
) {
3911 (void) fprintf(stderr
, gettext("Import was "
3912 "successful, but unable to mount some datasets\n"));
3920 typedef struct import_parameters
{
3921 nvlist_t
*ip_config
;
3922 const char *ip_mntopts
;
3925 uint_t ip_mntthreads
;
3927 } import_parameters_t
;
3930 do_import_task(void *arg
)
3932 import_parameters_t
*ip
= arg
;
3933 *ip
->ip_err
|= do_import(ip
->ip_config
, NULL
, ip
->ip_mntopts
,
3934 ip
->ip_props
, ip
->ip_flags
, ip
->ip_mntthreads
);
3940 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3941 char *orig_name
, char *new_name
, importargs_t
*import
)
3943 nvlist_t
*config
= NULL
;
3944 nvlist_t
*found_config
= NULL
;
3945 uint64_t pool_state
;
3946 boolean_t pool_specified
= (import
->poolname
!= NULL
||
3952 if (import
->do_all
) {
3953 tp
= tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN
),
3958 * At this point we have a list of import candidate configs. Even if
3959 * we were searching by pool name or guid, we still need to
3960 * post-process the list to deal with pool state and possible
3964 nvpair_t
*elem
= NULL
;
3965 boolean_t first
= B_TRUE
;
3966 if (!pool_specified
&& import
->do_all
) {
3967 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
)
3970 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3972 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3974 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3976 if (!import
->do_destroyed
&&
3977 pool_state
== POOL_STATE_DESTROYED
)
3979 if (import
->do_destroyed
&&
3980 pool_state
!= POOL_STATE_DESTROYED
)
3983 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3984 import
->policy
) == 0);
3986 if (!pool_specified
) {
3989 else if (!import
->do_all
)
3990 (void) fputc('\n', stdout
);
3992 if (import
->do_all
) {
3993 import_parameters_t
*ip
= safe_malloc(
3994 sizeof (import_parameters_t
));
3996 ip
->ip_config
= config
;
3997 ip
->ip_mntopts
= mntopts
;
3998 ip
->ip_props
= props
;
3999 ip
->ip_flags
= flags
;
4000 ip
->ip_mntthreads
= mount_tp_nthr
/ npools
;
4003 (void) tpool_dispatch(tp
, do_import_task
,
4007 * If we're importing from cachefile, then
4008 * we don't want to report errors until we
4009 * are in the scan phase of the import. If
4010 * we get an error, then we return that error
4011 * to invoke the scan phase.
4013 if (import
->cachefile
&& !import
->scan
)
4014 err
= show_import(config
, B_FALSE
);
4016 (void) show_import(config
, B_TRUE
);
4018 } else if (import
->poolname
!= NULL
) {
4022 * We are searching for a pool based on name.
4024 verify(nvlist_lookup_string(config
,
4025 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
4027 if (strcmp(name
, import
->poolname
) == 0) {
4028 if (found_config
!= NULL
) {
4029 (void) fprintf(stderr
, gettext(
4030 "cannot import '%s': more than "
4031 "one matching pool\n"),
4033 (void) fprintf(stderr
, gettext(
4034 "import by numeric ID instead\n"));
4037 found_config
= config
;
4043 * Search for a pool by guid.
4045 verify(nvlist_lookup_uint64(config
,
4046 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
4048 if (guid
== import
->guid
)
4049 found_config
= config
;
4052 if (import
->do_all
) {
4058 * If we were searching for a specific pool, verify that we found a
4059 * pool, and then do the import.
4061 if (pool_specified
&& err
== 0) {
4062 if (found_config
== NULL
) {
4063 (void) fprintf(stderr
, gettext("cannot import '%s': "
4064 "no such pool available\n"), orig_name
);
4067 err
|= do_import(found_config
, new_name
,
4068 mntopts
, props
, flags
, mount_tp_nthr
);
4073 * If we were just looking for pools, report an error if none were
4076 if (!pool_specified
&& first
)
4077 (void) fprintf(stderr
,
4078 gettext("no pools available to import\n"));
4082 typedef struct target_exists_args
{
4083 const char *poolname
;
4085 } target_exists_args_t
;
4088 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
4090 target_exists_args_t
*args
= data
;
4091 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
4097 if (args
->poolname
!= NULL
) {
4098 const char *pool_name
;
4100 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4102 if (strcmp(pool_name
, args
->poolname
) == 0)
4107 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
4109 if (pool_guid
== args
->poolguid
)
4117 * zpool checkpoint <pool>
4118 * checkpoint --discard <pool>
4120 * -d Discard the checkpoint from a checkpointed
4123 * -w Wait for discarding a checkpoint to complete.
4126 * Checkpoints the specified pool, by taking a "snapshot" of its
4127 * current state. A pool can only have one checkpoint at a time.
4130 zpool_do_checkpoint(int argc
, char **argv
)
4132 boolean_t discard
, wait
;
4134 zpool_handle_t
*zhp
;
4137 struct option long_options
[] = {
4138 {"discard", no_argument
, NULL
, 'd'},
4139 {"wait", no_argument
, NULL
, 'w'},
4145 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
4154 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4160 if (wait
&& !discard
) {
4161 (void) fprintf(stderr
, gettext("--wait only valid when "
4162 "--discard also specified\n"));
4170 (void) fprintf(stderr
, gettext("missing pool argument\n"));
4175 (void) fprintf(stderr
, gettext("too many arguments\n"));
4181 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
4182 /* As a special case, check for use of '/' in the name */
4183 if (strchr(pool
, '/') != NULL
)
4184 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
4185 "doesn't work on datasets. To save the state "
4186 "of a dataset from a specific point in time "
4187 "please use 'zfs snapshot'\n"));
4192 err
= (zpool_discard_checkpoint(zhp
) != 0);
4193 if (err
== 0 && wait
)
4194 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
4196 err
= (zpool_checkpoint(zhp
) != 0);
4204 #define CHECKPOINT_OPT 1024
4207 * zpool prefetch <type> [<type opts>] <pool>
4209 * Prefetchs a particular type of data in the specified pool.
4212 zpool_do_prefetch(int argc
, char **argv
)
4216 char *typestr
= NULL
;
4217 zpool_prefetch_type_t type
;
4218 zpool_handle_t
*zhp
;
4221 while ((c
= getopt(argc
, argv
, "t:")) != -1) {
4227 (void) fprintf(stderr
, gettext("missing argument for "
4228 "'%c' option\n"), optopt
);
4232 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4241 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4246 (void) fprintf(stderr
, gettext("too many arguments\n"));
4255 if (strcmp(typestr
, "ddt") == 0) {
4256 type
= ZPOOL_PREFETCH_DDT
;
4258 (void) fprintf(stderr
, gettext("unsupported prefetch type\n"));
4262 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
4265 err
= zpool_prefetch(zhp
, type
);
4273 * zpool import [-d dir] [-D]
4274 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
4275 * [-d dir | -c cachefile | -s] [-f] -a
4276 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
4277 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
4280 * -c Read pool information from a cachefile instead of searching
4281 * devices. If importing from a cachefile config fails, then
4282 * fallback to searching for devices only in the directories that
4283 * exist in the cachefile.
4285 * -d Scan in a specific directory, other than /dev/. More than
4286 * one directory can be specified using multiple '-d' options.
4288 * -D Scan for previously destroyed pools or import all or only
4289 * specified destroyed pools.
4291 * -R Temporarily import the pool, with all mountpoints relative to
4292 * the given root. The pool will remain exported when the machine
4295 * -V Import even in the presence of faulted vdevs. This is an
4296 * intentionally undocumented option for testing purposes, and
4297 * treats the pool configuration as complete, leaving any bad
4298 * vdevs in the FAULTED state. In other words, it does verbatim
4301 * -f Force import, even if it appears that the pool is active.
4303 * -F Attempt rewind if necessary.
4305 * -n See if rewind would work, but don't actually rewind.
4307 * -N Import the pool but don't mount datasets.
4309 * -T Specify a starting txg to use for import. This option is
4310 * intentionally undocumented option for testing purposes.
4312 * -a Import all pools found.
4314 * -l Load encryption keys while importing.
4316 * -o Set property=value and/or temporary mount options (without '=').
4318 * -s Scan using the default search path, the libblkid cache will
4321 * --rewind-to-checkpoint
4322 * Import the pool and revert back to the checkpoint.
4324 * The import command scans for pools to import, and import pools based on pool
4325 * name and GUID. The pool can also be renamed as part of the import process.
4328 zpool_do_import(int argc
, char **argv
)
4330 char **searchdirs
= NULL
;
4331 char *env
, *envdup
= NULL
;
4335 nvlist_t
*pools
= NULL
;
4336 boolean_t do_all
= B_FALSE
;
4337 boolean_t do_destroyed
= B_FALSE
;
4338 char *mntopts
= NULL
;
4339 uint64_t searchguid
= 0;
4340 char *searchname
= NULL
;
4342 nvlist_t
*policy
= NULL
;
4343 nvlist_t
*props
= NULL
;
4344 int flags
= ZFS_IMPORT_NORMAL
;
4345 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
4346 boolean_t dryrun
= B_FALSE
;
4347 boolean_t do_rewind
= B_FALSE
;
4348 boolean_t xtreme_rewind
= B_FALSE
;
4349 boolean_t do_scan
= B_FALSE
;
4350 boolean_t pool_exists
= B_FALSE
;
4351 uint64_t txg
= -1ULL;
4352 char *cachefile
= NULL
;
4353 importargs_t idata
= { 0 };
4356 struct option long_options
[] = {
4357 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
4362 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
4363 long_options
, NULL
)) != -1) {
4372 searchdirs
= safe_realloc(searchdirs
,
4373 (nsearch
+ 1) * sizeof (char *));
4374 searchdirs
[nsearch
++] = optarg
;
4377 do_destroyed
= B_TRUE
;
4380 flags
|= ZFS_IMPORT_ANY_HOST
;
4386 flags
|= ZFS_IMPORT_LOAD_KEYS
;
4389 flags
|= ZFS_IMPORT_MISSING_LOG
;
4395 flags
|= ZFS_IMPORT_ONLY
;
4398 if ((propval
= strchr(optarg
, '=')) != NULL
) {
4401 if (add_prop_list(optarg
, propval
,
4409 if (add_prop_list(zpool_prop_to_name(
4410 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
4412 if (add_prop_list_default(zpool_prop_to_name(
4413 ZPOOL_PROP_CACHEFILE
), "none", &props
))
4420 flags
|= ZFS_IMPORT_TEMP_NAME
;
4421 if (add_prop_list_default(zpool_prop_to_name(
4422 ZPOOL_PROP_CACHEFILE
), "none", &props
))
4428 txg
= strtoull(optarg
, &endptr
, 0);
4429 if (errno
!= 0 || *endptr
!= '\0') {
4430 (void) fprintf(stderr
,
4431 gettext("invalid txg value\n"));
4434 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
4437 flags
|= ZFS_IMPORT_VERBATIM
;
4440 xtreme_rewind
= B_TRUE
;
4442 case CHECKPOINT_OPT
:
4443 flags
|= ZFS_IMPORT_CHECKPOINT
;
4446 (void) fprintf(stderr
, gettext("missing argument for "
4447 "'%c' option\n"), optopt
);
4451 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4460 if (cachefile
&& nsearch
!= 0) {
4461 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
4465 if (cachefile
&& do_scan
) {
4466 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
4470 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
4471 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
4475 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
4476 (void) fprintf(stderr
, gettext("-l is only meaningful during "
4481 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
4482 (void) fprintf(stderr
,
4483 gettext("-n or -X only meaningful with -F\n"));
4487 rewind_policy
= ZPOOL_TRY_REWIND
;
4489 rewind_policy
= ZPOOL_DO_REWIND
;
4491 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
4493 /* In the future, we can capture further policy and include it here */
4494 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
4495 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
4496 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
4497 rewind_policy
) != 0)
4500 /* check argument count */
4503 (void) fprintf(stderr
, gettext("too many arguments\n"));
4508 (void) fprintf(stderr
, gettext("too many arguments\n"));
4514 * Check for the effective uid. We do this explicitly here because
4515 * otherwise any attempt to discover pools will silently fail.
4517 if (argc
== 0 && geteuid() != 0) {
4518 (void) fprintf(stderr
, gettext("cannot "
4519 "discover pools: permission denied\n"));
4523 nvlist_free(policy
);
4528 * Depending on the arguments given, we do one of the following:
4530 * <none> Iterate through all pools and display information about
4533 * -a Iterate through all pools and try to import each one.
4535 * <id> Find the pool that corresponds to the given GUID/pool
4536 * name and import that one.
4538 * -D Above options applies only to destroyed pools.
4544 searchguid
= strtoull(argv
[0], &endptr
, 10);
4545 if (errno
!= 0 || *endptr
!= '\0') {
4546 searchname
= argv
[0];
4551 * User specified a name or guid. Ensure it's unique.
4553 target_exists_args_t search
= {searchname
, searchguid
};
4554 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
4558 * Check the environment for the preferred search path.
4560 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
4561 char *dir
, *tmp
= NULL
;
4563 envdup
= strdup(env
);
4565 for (dir
= strtok_r(envdup
, ":", &tmp
);
4567 dir
= strtok_r(NULL
, ":", &tmp
)) {
4568 searchdirs
= safe_realloc(searchdirs
,
4569 (nsearch
+ 1) * sizeof (char *));
4570 searchdirs
[nsearch
++] = dir
;
4574 idata
.path
= searchdirs
;
4575 idata
.paths
= nsearch
;
4576 idata
.poolname
= searchname
;
4577 idata
.guid
= searchguid
;
4578 idata
.cachefile
= cachefile
;
4579 idata
.scan
= do_scan
;
4580 idata
.policy
= policy
;
4581 idata
.do_destroyed
= do_destroyed
;
4582 idata
.do_all
= do_all
;
4584 libpc_handle_t lpch
= {
4585 .lpc_lib_handle
= g_zfs
,
4586 .lpc_ops
= &libzfs_config_ops
,
4587 .lpc_printerr
= B_TRUE
4589 pools
= zpool_search_import(&lpch
, &idata
);
4591 if (pools
!= NULL
&& pool_exists
&&
4592 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
4593 (void) fprintf(stderr
, gettext("cannot import '%s': "
4594 "a pool with that name already exists\n"),
4596 (void) fprintf(stderr
, gettext("use the form '%s "
4597 "<pool | id> <newpool>' to give it a new name\n"),
4600 } else if (pools
== NULL
&& pool_exists
) {
4601 (void) fprintf(stderr
, gettext("cannot import '%s': "
4602 "a pool with that name is already created/imported,\n"),
4604 (void) fprintf(stderr
, gettext("and no additional pools "
4605 "with that name were found\n"));
4607 } else if (pools
== NULL
) {
4609 (void) fprintf(stderr
, gettext("cannot import '%s': "
4610 "no such pool available\n"), argv
[0]);
4618 nvlist_free(policy
);
4624 err
= import_pools(pools
, props
, mntopts
, flags
,
4625 argc
>= 1 ? argv
[0] : NULL
, argc
>= 2 ? argv
[1] : NULL
, &idata
);
4628 * If we're using the cachefile and we failed to import, then
4629 * fallback to scanning the directory for pools that match
4630 * those in the cachefile.
4632 if (err
!= 0 && cachefile
!= NULL
) {
4633 (void) printf(gettext("cachefile import failed, retrying\n"));
4636 * We use the scan flag to gather the directories that exist
4637 * in the cachefile. If we need to fallback to searching for
4638 * the pool config, we will only search devices in these
4641 idata
.scan
= B_TRUE
;
4643 pools
= zpool_search_import(&lpch
, &idata
);
4645 err
= import_pools(pools
, props
, mntopts
, flags
,
4646 argc
>= 1 ? argv
[0] : NULL
, argc
>= 2 ? argv
[1] : NULL
,
4653 nvlist_free(policy
);
4657 return (err
? 1 : 0);
4661 * zpool sync [-f] [pool] ...
4663 * -f (undocumented) force uberblock (and config including zpool cache file)
4666 * Sync the specified pool(s).
4667 * Without arguments "zpool sync" will sync all pools.
4668 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
4672 zpool_do_sync(int argc
, char **argv
)
4675 boolean_t force
= B_FALSE
;
4678 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
4684 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4693 /* if argc == 0 we will execute zpool_sync_one on all pools */
4694 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
4695 B_FALSE
, zpool_sync_one
, &force
);
4700 typedef struct iostat_cbdata
{
4704 boolean_t cb_verbose
;
4705 boolean_t cb_literal
;
4706 boolean_t cb_scripted
;
4707 zpool_list_t
*cb_list
;
4708 vdev_cmd_data_list_t
*vcdl
;
4709 vdev_cbdata_t cb_vdevs
;
4713 typedef struct name_and_columns
{
4714 const char *name
; /* Column name */
4715 unsigned int columns
; /* Center name to this number of columns */
4716 } name_and_columns_t
;
4718 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
4720 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
4722 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
4724 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4725 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
4727 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
4728 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
4729 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
4730 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4731 {"asyncq_wait", 2}, {NULL
}},
4732 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
4733 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
4734 {"trim", 2}, {"rebuild", 2}, {NULL
}},
4737 /* Shorthand - if "columns" field not set, default to 1 column */
4738 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
4740 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
4742 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4743 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
4745 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
4746 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
4747 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
4748 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4749 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
4751 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4752 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4753 {"ind"}, {"agg"}, {NULL
}},
4756 static const char *histo_to_title
[] = {
4757 [IOS_L_HISTO
] = "latency",
4758 [IOS_RQ_HISTO
] = "req_size",
4762 * Return the number of labels in a null-terminated name_and_columns_t
4767 label_array_len(const name_and_columns_t
*labels
)
4771 while (labels
[i
].name
)
4778 * Return the number of strings in a null-terminated string array.
4781 * const char foo[] = {"bar", "baz", NULL}
4786 str_array_len(const char *array
[])
4797 * Return a default column width for default/latency/queue columns. This does
4798 * not include histograms, which have their columns autosized.
4801 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
4803 unsigned long column_width
= 5; /* Normal niceprint */
4804 static unsigned long widths
[] = {
4806 * Choose some sane default column sizes for printing the
4809 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4810 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4811 [IOS_QUEUES
] = 6, /* 1M queue entries */
4812 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4813 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4817 column_width
= widths
[type
];
4819 return (column_width
);
4823 * Print the column labels, i.e:
4825 * capacity operations bandwidth
4826 * alloc free read write read write ...
4828 * If force_column_width is set, use it for the column width. If not set, use
4829 * the default column width.
4832 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4833 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4836 int text_start
, rw_column_width
, spaces_to_end
;
4837 uint64_t flags
= cb
->cb_flags
;
4839 unsigned int column_width
= force_column_width
;
4841 /* For each bit set in flags */
4842 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4843 idx
= lowbit64(f
) - 1;
4844 if (!force_column_width
)
4845 column_width
= default_column_width(cb
, idx
);
4846 /* Print our top labels centered over "read write" label. */
4847 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4848 const char *name
= labels
[idx
][i
].name
;
4850 * We treat labels[][].columns == 0 as shorthand
4851 * for one column. It makes writing out the label
4852 * tables more concise.
4854 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4855 unsigned int slen
= strlen(name
);
4857 rw_column_width
= (column_width
* columns
) +
4858 (2 * (columns
- 1));
4860 text_start
= (int)((rw_column_width
) / columns
-
4865 printf(" "); /* Two spaces between columns */
4867 /* Space from beginning of column to label */
4868 for (s
= 0; s
< text_start
; s
++)
4873 /* Print space after label to end of column */
4874 spaces_to_end
= rw_column_width
- text_start
- slen
;
4875 if (spaces_to_end
< 0)
4878 for (s
= 0; s
< spaces_to_end
; s
++)
4886 * print_cmd_columns - Print custom column titles from -c
4888 * If the user specified the "zpool status|iostat -c" then print their custom
4889 * column titles in the header. For example, print_cmd_columns() would print
4890 * the " col1 col2" part of this:
4892 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4894 * capacity operations bandwidth
4895 * pool alloc free read write read write col1 col2
4896 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4897 * mypool 269K 1008M 0 0 107 946
4898 * mirror 269K 1008M 0 0 107 946
4899 * sdb - - 0 0 102 473 val1 val2
4900 * sdc - - 0 0 5 473 val1 val2
4901 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4904 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4907 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4909 if (vcdl
->count
== 0 || data
== NULL
)
4913 * Each vdev cmd should have the same column names unless the user did
4914 * something weird with their cmd. Just take the column names from the
4915 * first vdev and assume it works for all of them.
4917 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4920 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4923 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4924 vcdl
->uniq_cols
[i
]);
4931 * Utility function to print out a line of dashes like:
4933 * -------------------------------- ----- ----- ----- ----- -----
4935 * ...or a dashed named-row line like:
4941 * @force_column_width If non-zero, use the value as the column width.
4942 * Otherwise use the default column widths.
4944 * @name: Print a dashed named-row line starting
4945 * with @name. Otherwise, print a regular
4949 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4953 unsigned int namewidth
;
4954 uint64_t flags
= cb
->cb_flags
;
4957 const name_and_columns_t
*labels
;
4961 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4962 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4963 } else if (cb
->cb_vdevs
.cb_names_count
) {
4969 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4970 name
? strlen(name
) : 0);
4974 printf("%-*s", namewidth
, name
);
4976 for (i
= 0; i
< namewidth
; i
++)
4980 /* For each bit in flags */
4981 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4982 unsigned int column_width
;
4983 idx
= lowbit64(f
) - 1;
4984 if (force_column_width
)
4985 column_width
= force_column_width
;
4987 column_width
= default_column_width(cb
, idx
);
4989 labels
= iostat_bottom_labels
[idx
];
4990 for (i
= 0; i
< label_array_len(labels
); i
++) {
4992 printf(" %*s-", column_width
- 1, " ");
4994 printf(" %.*s", column_width
,
4995 "--------------------");
5002 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
5003 unsigned int force_column_width
)
5005 print_iostat_dashes(cb
, force_column_width
, NULL
);
5009 print_iostat_separator(iostat_cbdata_t
*cb
)
5011 print_iostat_separator_impl(cb
, 0);
5015 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
5016 const char *histo_vdev_name
)
5018 unsigned int namewidth
;
5021 color_start(ANSI_BOLD
);
5023 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
5024 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
5025 } else if (cb
->cb_vdevs
.cb_names_count
) {
5031 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
5032 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
5034 if (histo_vdev_name
)
5035 printf("%-*s", namewidth
, histo_vdev_name
);
5037 printf("%*s", namewidth
, "");
5040 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
5043 printf("%-*s", namewidth
, title
);
5045 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
5046 if (cb
->vcdl
!= NULL
)
5047 print_cmd_columns(cb
->vcdl
, 0);
5051 print_iostat_separator_impl(cb
, force_column_width
);
5053 if (cb
->vcdl
!= NULL
)
5054 print_cmd_columns(cb
->vcdl
, 1);
5062 print_iostat_header(iostat_cbdata_t
*cb
)
5064 print_iostat_header_impl(cb
, 0, NULL
);
5068 * Prints a size string (i.e. 120M) with the suffix ("M") colored
5069 * by order of magnitude. Uses column_size to add padding.
5072 print_stat_color(const char *statbuf
, unsigned int column_size
)
5075 size_t len
= strlen(statbuf
);
5076 while (len
< column_size
) {
5080 if (*statbuf
== '0') {
5081 color_start(ANSI_GRAY
);
5084 for (; *statbuf
; statbuf
++) {
5085 if (*statbuf
== 'K') color_start(ANSI_GREEN
);
5086 else if (*statbuf
== 'M') color_start(ANSI_YELLOW
);
5087 else if (*statbuf
== 'G') color_start(ANSI_RED
);
5088 else if (*statbuf
== 'T') color_start(ANSI_BOLD_BLUE
);
5089 else if (*statbuf
== 'P') color_start(ANSI_MAGENTA
);
5090 else if (*statbuf
== 'E') color_start(ANSI_CYAN
);
5091 fputc(*statbuf
, stdout
);
5092 if (--column_size
<= 0)
5100 * Display a single statistic.
5103 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
5104 unsigned int column_size
, boolean_t scripted
)
5108 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
5111 printf("\t%s", buf
);
5113 print_stat_color(buf
, column_size
);
5117 * Calculate the default vdev stats
5119 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
5120 * stats into calcvs.
5123 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
5124 vdev_stat_t
*calcvs
)
5128 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
5129 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
5130 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
5132 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
5133 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
5137 * Internal representation of the extended iostats data.
5139 * The extended iostat stats are exported in nvlists as either uint64_t arrays
5140 * or single uint64_t's. We make both look like arrays to make them easier
5141 * to process. In order to make single uint64_t's look like arrays, we set
5142 * __data to the stat data, and then set *data = &__data with count = 1. Then,
5143 * we can just use *data and count.
5147 uint_t count
; /* Number of entries in data[] */
5148 uint64_t __data
; /* Only used when data is a single uint64_t */
5152 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
5156 for (i
= 0; i
< len
; i
++)
5157 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
5163 * Helper function to lookup a uint64_t array or uint64_t value and store its
5164 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
5165 * it look like a one element array to make it easier to process.
5168 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
5169 struct stat_array
*nva
)
5174 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
5175 switch (nvpair_type(tmp
)) {
5176 case DATA_TYPE_UINT64_ARRAY
:
5177 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
5179 case DATA_TYPE_UINT64
:
5180 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
5181 nva
->data
= &nva
->__data
;
5185 /* Not a uint64_t */
5194 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
5195 * subtract them, and return the results in a newly allocated stat_array.
5196 * You must free the returned array after you are done with it with
5197 * free_calc_stats().
5199 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
5202 static struct stat_array
*
5203 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
5206 nvlist_t
*oldnvx
= NULL
, *newnvx
;
5207 struct stat_array
*oldnva
, *newnva
, *calcnva
;
5209 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
5211 /* Extract our extended stats nvlist from the main list */
5212 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5215 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5219 newnva
= safe_malloc(alloc_size
);
5220 oldnva
= safe_malloc(alloc_size
);
5221 calcnva
= safe_malloc(alloc_size
);
5223 for (j
= 0; j
< len
; j
++) {
5224 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
5226 calcnva
[j
].count
= newnva
[j
].count
;
5227 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
5228 calcnva
[j
].data
= safe_malloc(alloc_size
);
5229 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
5232 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
5234 for (i
= 0; i
< oldnva
[j
].count
; i
++)
5235 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
5244 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
5247 for (i
= 0; i
< len
; i
++)
5254 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
5255 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
5261 enum zfs_nicenum_format format
;
5262 unsigned int buckets
;
5263 unsigned int start_bucket
;
5266 format
= ZFS_NICENUM_RAW
;
5268 format
= ZFS_NICENUM_1024
;
5270 /* All these histos are the same size, so just use nva[0].count */
5271 buckets
= nva
[0].count
;
5273 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
5274 /* Start at 512 - req size should never be lower than this */
5280 for (j
= start_bucket
; j
< buckets
; j
++) {
5281 /* Print histogram bucket label */
5282 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
5283 /* Ending range of this bucket */
5284 val
= (1UL << (j
+ 1)) - 1;
5285 zfs_nicetime(val
, buf
, sizeof (buf
));
5287 /* Request size (starting range of bucket) */
5289 zfs_nicenum(val
, buf
, sizeof (buf
));
5292 if (cb
->cb_scripted
)
5293 printf("%llu", (u_longlong_t
)val
);
5295 printf("%-*s", namewidth
, buf
);
5297 /* Print the values on the line */
5298 for (i
= 0; i
< len
; i
++) {
5299 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
5300 column_width
, cb
->cb_scripted
);
5307 print_solid_separator(unsigned int length
)
5315 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
5316 nvlist_t
*newnv
, double scale
, const char *name
)
5318 unsigned int column_width
;
5319 unsigned int namewidth
;
5320 unsigned int entire_width
;
5321 enum iostat_type type
;
5322 struct stat_array
*nva
;
5324 unsigned int names_len
;
5326 /* What type of histo are we? */
5327 type
= IOS_HISTO_IDX(cb
->cb_flags
);
5329 /* Get NULL-terminated array of nvlist names for our histo */
5330 names
= vsx_type_to_nvlist
[type
];
5331 names_len
= str_array_len(names
); /* num of names */
5333 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
5335 if (cb
->cb_literal
) {
5336 column_width
= MAX(5,
5337 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
5342 namewidth
= MAX(cb
->cb_namewidth
,
5343 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
5346 * Calculate the entire line width of what we're printing. The
5347 * +2 is for the two spaces between columns:
5351 /* |___| <---------- column_width */
5353 /* |__________| <--- entire_width */
5355 entire_width
= namewidth
+ (column_width
+ 2) *
5356 label_array_len(iostat_bottom_labels
[type
]);
5358 if (cb
->cb_scripted
)
5359 printf("%s\n", name
);
5361 print_iostat_header_impl(cb
, column_width
, name
);
5363 print_iostat_histo(nva
, names_len
, cb
, column_width
,
5366 free_calc_stats(nva
, names_len
);
5367 if (!cb
->cb_scripted
)
5368 print_solid_separator(entire_width
);
5372 * Calculate the average latency of a power-of-two latency histogram
5375 single_histo_average(uint64_t *histo
, unsigned int buckets
)
5378 uint64_t count
= 0, total
= 0;
5380 for (i
= 0; i
< buckets
; i
++) {
5382 * Our buckets are power-of-two latency ranges. Use the
5383 * midpoint latency of each bucket to calculate the average.
5391 if (histo
[i
] != 0) {
5392 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
5397 /* Prevent divide by zero */
5398 return (count
== 0 ? 0 : total
/ count
);
5402 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
5404 const char *names
[] = {
5405 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
5406 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
5407 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
5408 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
5409 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
5410 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
5411 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
5412 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
5413 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
5414 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
5415 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
5416 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
5417 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
5418 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
5421 struct stat_array
*nva
;
5423 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
5424 enum zfs_nicenum_format format
;
5426 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
5429 format
= ZFS_NICENUM_RAW
;
5431 format
= ZFS_NICENUM_1024
;
5433 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
5434 uint64_t val
= nva
[i
].data
[0];
5435 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
5438 free_calc_stats(nva
, ARRAY_SIZE(names
));
5442 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
5447 const char *names
[] = {
5448 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
5449 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
5450 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
5451 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
5452 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
5453 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
5454 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
5455 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
5456 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
5457 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
5458 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
5460 struct stat_array
*nva
;
5462 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
5463 enum zfs_nicenum_format format
;
5465 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
5468 format
= ZFS_NICENUM_RAWTIME
;
5470 format
= ZFS_NICENUM_TIME
;
5472 /* Print our avg latencies on the line */
5473 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
5474 /* Compute average latency for a latency histo */
5475 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
5476 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
5478 free_calc_stats(nva
, ARRAY_SIZE(names
));
5482 * Print default statistics (capacity/operations/bandwidth)
5485 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
5487 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
5488 enum zfs_nicenum_format format
;
5489 char na
; /* char to print for "not applicable" values */
5491 if (cb
->cb_literal
) {
5492 format
= ZFS_NICENUM_RAW
;
5495 format
= ZFS_NICENUM_1024
;
5499 /* only toplevel vdevs have capacity stats */
5500 if (vs
->vs_space
== 0) {
5501 if (cb
->cb_scripted
)
5502 printf("\t%c\t%c", na
, na
);
5504 printf(" %*c %*c", column_width
, na
, column_width
,
5507 print_one_stat(vs
->vs_alloc
, format
, column_width
,
5509 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
5510 column_width
, cb
->cb_scripted
);
5513 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
5514 format
, column_width
, cb
->cb_scripted
);
5515 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
5516 format
, column_width
, cb
->cb_scripted
);
5517 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
5518 format
, column_width
, cb
->cb_scripted
);
5519 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
5520 format
, column_width
, cb
->cb_scripted
);
5523 static const char *const class_name
[] = {
5524 VDEV_ALLOC_BIAS_DEDUP
,
5525 VDEV_ALLOC_BIAS_SPECIAL
,
5526 VDEV_ALLOC_CLASS_LOGS
5530 * Print out all the statistics for the given vdev. This can either be the
5531 * toplevel configuration, or called recursively. If 'name' is NULL, then this
5532 * is a verbose output, and we don't want to display the toplevel pool stats.
5534 * Returns the number of stat lines printed.
5537 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
5538 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
5540 nvlist_t
**oldchild
, **newchild
;
5541 uint_t c
, children
, oldchildren
;
5542 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
5543 vdev_stat_t zerovs
= { 0 };
5550 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5553 calcvs
= safe_malloc(sizeof (*calcvs
));
5555 if (oldnv
!= NULL
) {
5556 verify(nvlist_lookup_uint64_array(oldnv
,
5557 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
5562 /* Do we only want to see a specific vdev? */
5563 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
5564 /* Yes we do. Is this the vdev? */
5565 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
5567 * This is our vdev. Since it is the only vdev we
5568 * will be displaying, make depth = 0 so that it
5569 * doesn't get indented.
5576 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
5577 /* Couldn't match the name */
5582 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
5583 (uint64_t **)&newvs
, &c
) == 0);
5586 * Print the vdev name unless it's is a histogram. Histograms
5587 * display the vdev name in the header itself.
5589 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
5590 if (cb
->cb_scripted
) {
5593 if (strlen(name
) + depth
> cb
->cb_namewidth
)
5594 (void) printf("%*s%s", depth
, "", name
);
5596 (void) printf("%*s%s%*s", depth
, "", name
,
5597 (int)(cb
->cb_namewidth
- strlen(name
) -
5602 /* Calculate our scaling factor */
5603 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
5604 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
5606 * If we specify printing histograms with no time interval, then
5607 * print the histogram numbers over the entire lifetime of the
5615 scale
= (double)NANOSEC
/ tdelta
;
5618 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
5619 calc_default_iostats(oldvs
, newvs
, calcvs
);
5620 print_iostat_default(calcvs
, cb
, scale
);
5622 if (cb
->cb_flags
& IOS_LATENCY_M
)
5623 print_iostat_latency(cb
, oldnv
, newnv
);
5624 if (cb
->cb_flags
& IOS_QUEUES_M
)
5625 print_iostat_queues(cb
, newnv
);
5626 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
5628 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
5631 if (cb
->vcdl
!= NULL
) {
5633 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
5636 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
5640 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
5649 if (!cb
->cb_verbose
)
5652 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
5653 &newchild
, &children
) != 0)
5657 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
5658 &oldchild
, &oldchildren
) != 0)
5661 children
= MIN(oldchildren
, children
);
5665 * print normal top-level devices
5667 for (c
= 0; c
< children
; c
++) {
5668 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
5670 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
5673 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
5676 if (ishole
|| islog
)
5679 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
5682 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5683 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
5684 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
5685 newchild
[c
], cb
, depth
+ 2);
5690 * print all other top-level devices
5692 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
5693 boolean_t printed
= B_FALSE
;
5695 for (c
= 0; c
< children
; c
++) {
5696 uint64_t islog
= B_FALSE
;
5697 const char *bias
= NULL
;
5698 const char *type
= NULL
;
5700 (void) nvlist_lookup_uint64(newchild
[c
],
5701 ZPOOL_CONFIG_IS_LOG
, &islog
);
5703 bias
= VDEV_ALLOC_CLASS_LOGS
;
5705 (void) nvlist_lookup_string(newchild
[c
],
5706 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
5707 (void) nvlist_lookup_string(newchild
[c
],
5708 ZPOOL_CONFIG_TYPE
, &type
);
5710 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
5712 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
5716 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
5718 !cb
->cb_vdevs
.cb_names
) {
5719 print_iostat_dashes(cb
, 0,
5726 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5727 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
5728 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
5729 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
5735 * Include level 2 ARC devices in iostat output
5737 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
5738 &newchild
, &children
) != 0)
5742 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
5743 &oldchild
, &oldchildren
) != 0)
5746 children
= MIN(oldchildren
, children
);
5750 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
5751 !cb
->cb_vdevs
.cb_names
) {
5752 print_iostat_dashes(cb
, 0, "cache");
5756 for (c
= 0; c
< children
; c
++) {
5757 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5758 cb
->cb_vdevs
.cb_name_flags
);
5759 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
5760 : NULL
, newchild
[c
], cb
, depth
+ 2);
5769 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
5771 iostat_cbdata_t
*cb
= data
;
5775 * If the pool has disappeared, remove it from the list and continue.
5777 if (zpool_refresh_stats(zhp
, &missing
) != 0)
5781 pool_list_remove(cb
->cb_list
, zhp
);
5787 * Callback to print out the iostats for the given pool.
5790 print_iostat(zpool_handle_t
*zhp
, void *data
)
5792 iostat_cbdata_t
*cb
= data
;
5793 nvlist_t
*oldconfig
, *newconfig
;
5794 nvlist_t
*oldnvroot
, *newnvroot
;
5797 newconfig
= zpool_get_config(zhp
, &oldconfig
);
5799 if (cb
->cb_iteration
== 1)
5802 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5805 if (oldconfig
== NULL
)
5808 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5811 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
5813 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
5814 !cb
->cb_scripted
&& cb
->cb_verbose
&&
5815 !cb
->cb_vdevs
.cb_names_count
) {
5816 print_iostat_separator(cb
);
5817 if (cb
->vcdl
!= NULL
) {
5818 print_cmd_columns(cb
->vcdl
, 1);
5833 if (isatty(STDOUT_FILENO
)) {
5834 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
5836 columns
= ws
.ws_col
;
5845 * Return the required length of the pool/vdev name column. The minimum
5846 * allowed width and output formatting flags must be provided.
5849 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5851 nvlist_t
*config
, *nvroot
;
5852 int width
= min_width
;
5854 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5855 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5857 size_t poolname_len
= strlen(zpool_get_name(zhp
));
5858 if (verbose
== B_FALSE
) {
5859 width
= MAX(poolname_len
, min_width
);
5861 width
= MAX(poolname_len
,
5862 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5870 * Parse the input string, get the 'interval' and 'count' value if there is one.
5873 get_interval_count(int *argcp
, char **argv
, float *iv
,
5877 unsigned long count
= 0;
5881 * Determine if the last argument is an integer or a pool name
5883 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5887 interval
= strtof(argv
[argc
- 1], &end
);
5889 if (*end
== '\0' && errno
== 0) {
5890 if (interval
== 0) {
5891 (void) fprintf(stderr
, gettext(
5892 "interval cannot be zero\n"));
5896 * Ignore the last parameter
5901 * If this is not a valid number, just plow on. The
5902 * user will get a more informative error message later
5910 * If the last argument is also an integer, then we have both a count
5913 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5918 interval
= strtof(argv
[argc
- 1], &end
);
5920 if (*end
== '\0' && errno
== 0) {
5921 if (interval
== 0) {
5922 (void) fprintf(stderr
, gettext(
5923 "interval cannot be zero\n"));
5928 * Ignore the last parameter
5942 get_timestamp_arg(char c
)
5945 timestamp_fmt
= UDATE
;
5947 timestamp_fmt
= DDATE
;
5953 * Return stat flags that are supported by all pools by both the module and
5954 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5955 * It will get ANDed down until only the flags that are supported on all pools
5959 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5961 uint64_t *mask
= data
;
5962 nvlist_t
*config
, *nvroot
, *nvx
;
5966 config
= zpool_get_config(zhp
, NULL
);
5967 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5970 /* Default stats are always supported, but for completeness.. */
5971 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5972 flags
|= IOS_DEFAULT_M
;
5974 /* Get our extended stats nvlist from the main list */
5975 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5978 * No extended stats; they're probably running an older
5979 * module. No big deal, we support that too.
5984 /* For each extended stat, make sure all its nvpairs are supported */
5985 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5986 if (!vsx_type_to_nvlist
[j
][0])
5989 /* Start off by assuming the flag is supported, then check */
5990 flags
|= (1ULL << j
);
5991 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5992 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5993 /* flag isn't supported */
5994 flags
= flags
& ~(1ULL << j
);
6000 *mask
= *mask
& flags
;
6005 * Return a bitmask of stats that are supported on all pools by both the module
6009 get_stat_flags(zpool_list_t
*list
)
6014 * get_stat_flags_cb() will lop off bits from "mask" until only the
6015 * flags that are supported on all pools remain.
6017 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
6022 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
6025 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
6028 vdev_cbdata_t
*cb
= cb_data
;
6029 zpool_handle_t
*zhp
= zhp_data
;
6031 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
6034 return (guid
== zpool_vdev_path_to_guid(zhp
, cb
->cb_names
[0]));
6038 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
6041 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
6043 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
6047 * Check if vdevs are in a pool
6049 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
6050 * return 0. If pool_name is NULL, then search all pools.
6053 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
6061 if ((argc
== 0) || !*argv
)
6067 /* Temporarily hijack cb_names for a second... */
6068 tmp_name
= cb
->cb_names
;
6070 /* Go though our list of prospective vdev names */
6071 for (i
= 0; i
< argc
; i
++) {
6072 cb
->cb_names
= argv
+ i
;
6074 /* Is this name a vdev in our pools? */
6075 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
6076 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
6083 cb
->cb_names
= tmp_name
;
6089 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
6092 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
6099 * Do we have a pool named *name? If so, return 1, otherwise 0.
6104 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
6108 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
6110 are_all_pools(int argc
, char **argv
)
6112 if ((argc
== 0) || !*argv
)
6116 if (!is_pool(argv
[argc
]))
6123 * Helper function to print out vdev/pool names we can't resolve. Used for an
6127 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
6133 for (i
= 0; i
< argc
; i
++) {
6137 str
= gettext("pool");
6138 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
6139 str
= gettext("vdev in this pool");
6140 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
6141 str
= gettext("vdev in another pool");
6143 str
= gettext("unknown");
6145 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
6150 * Same as get_interval_count(), but with additional checks to not misinterpret
6151 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
6152 * cb.cb_vdevs.cb_name_flags.
6155 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
6156 unsigned long *count
, iostat_cbdata_t
*cb
)
6158 char **tmpargv
= argv
;
6159 int argc_for_interval
= 0;
6161 /* Is the last arg an interval value? Or a guid? */
6162 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
6165 * The last arg is not a guid, so it's probably an
6168 argc_for_interval
++;
6171 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
6174 * The 2nd to last arg is not a guid, so it's probably
6175 * an interval value.
6177 argc_for_interval
++;
6181 /* Point to our list of possible intervals */
6182 tmpargv
= &argv
[*argc
- argc_for_interval
];
6184 *argc
= *argc
- argc_for_interval
;
6185 get_interval_count(&argc_for_interval
, tmpargv
,
6190 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
6191 * if we were unable to determine its size.
6194 terminal_height(void)
6198 if (isatty(STDOUT_FILENO
) == 0)
6201 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
6202 return (win
.ws_row
);
6208 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
6211 * name: Short name of the script ('iostat').
6212 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
6215 print_zpool_script_help(char *name
, char *path
)
6217 char *argv
[] = {path
, (char *)"-h", NULL
};
6218 char **lines
= NULL
;
6222 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
6224 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
6226 libzfs_free_str_array(lines
, lines_cnt
);
6230 for (int i
= 0; i
< lines_cnt
; i
++)
6231 if (!is_blank_str(lines
[i
]))
6232 printf(" %-14s %s\n", name
, lines
[i
]);
6234 libzfs_free_str_array(lines
, lines_cnt
);
6238 * Go though the zpool status/iostat -c scripts in the user's path, run their
6239 * help option (-h), and print out the results.
6242 print_zpool_dir_scripts(char *dirpath
)
6246 char fullpath
[MAXPATHLEN
];
6247 struct stat dir_stat
;
6249 if ((dir
= opendir(dirpath
)) != NULL
) {
6250 /* print all the files and directories within directory */
6251 while ((ent
= readdir(dir
)) != NULL
) {
6252 if (snprintf(fullpath
, sizeof (fullpath
), "%s/%s",
6253 dirpath
, ent
->d_name
) >= sizeof (fullpath
)) {
6254 (void) fprintf(stderr
,
6255 gettext("internal error: "
6256 "ZPOOL_SCRIPTS_PATH too large.\n"));
6260 /* Print the scripts */
6261 if (stat(fullpath
, &dir_stat
) == 0)
6262 if (dir_stat
.st_mode
& S_IXUSR
&&
6263 S_ISREG(dir_stat
.st_mode
))
6264 print_zpool_script_help(ent
->d_name
,
6272 * Print out help text for all zpool status/iostat -c scripts.
6275 print_zpool_script_list(const char *subcommand
)
6277 char *dir
, *sp
, *tmp
;
6279 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
6281 sp
= zpool_get_cmd_search_path();
6285 for (dir
= strtok_r(sp
, ":", &tmp
);
6287 dir
= strtok_r(NULL
, ":", &tmp
))
6288 print_zpool_dir_scripts(dir
);
6294 * Set the minimum pool/vdev name column width. The width must be at least 10,
6295 * but may be as large as the column width - 42 so it still fits on one line.
6296 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
6299 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
6301 iostat_cbdata_t
*cb
= data
;
6302 int width
, available_width
;
6305 * get_namewidth() returns the maximum width of any name in that column
6306 * for any pool/vdev/device line that will be output.
6308 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
6309 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
6312 * The width we are calculating is the width of the header and also the
6313 * padding width for names that are less than maximum width. The stats
6314 * take up 42 characters, so the width available for names is:
6316 available_width
= get_columns() - 42;
6319 * If the maximum width fits on a screen, then great! Make everything
6320 * line up by justifying all lines to the same width. If that max
6321 * width is larger than what's available, the name plus stats won't fit
6322 * on one line, and justifying to that width would cause every line to
6323 * wrap on the screen. We only want lines with long names to wrap.
6324 * Limit the padding to what won't wrap.
6326 if (width
> available_width
)
6327 width
= available_width
;
6330 * And regardless of whatever the screen width is (get_columns can
6331 * return 0 if the width is not known or less than 42 for a narrow
6332 * terminal) have the width be a minimum of 10.
6337 /* Save the calculated width */
6338 cb
->cb_namewidth
= width
;
6344 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
6345 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
6346 * [interval [count]]
6348 * -c CMD For each vdev, run command CMD
6349 * -g Display guid for individual vdev name.
6350 * -L Follow links when resolving vdev path name.
6351 * -P Display full path for vdev name.
6352 * -v Display statistics for individual vdevs
6354 * -p Display values in parsable (exact) format.
6355 * -H Scripted mode. Don't display headers, and separate properties
6357 * -l Display average latency
6358 * -q Display queue depths
6359 * -w Display latency histograms
6360 * -r Display request size histogram
6361 * -T Display a timestamp in date(1) or Unix format
6362 * -n Only print headers once
6364 * This command can be tricky because we want to be able to deal with pool
6365 * creation/destruction as well as vdev configuration changes. The bulk of this
6366 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
6367 * on pool_list_update() to detect the addition of new pools. Configuration
6368 * changes are all handled within libzfs.
6371 zpool_do_iostat(int argc
, char **argv
)
6377 unsigned long count
= 0;
6380 boolean_t verbose
= B_FALSE
;
6381 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
6382 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
6383 boolean_t omit_since_boot
= B_FALSE
;
6384 boolean_t guid
= B_FALSE
;
6385 boolean_t follow_links
= B_FALSE
;
6386 boolean_t full_name
= B_FALSE
;
6387 boolean_t headers_once
= B_FALSE
;
6388 iostat_cbdata_t cb
= { 0 };
6391 /* Used for printing error message */
6392 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
6393 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
6395 uint64_t unsupported_flags
;
6398 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
6403 gettext("Can't set -c flag twice\n"));
6407 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
6408 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6409 fprintf(stderr
, gettext(
6410 "Can't run -c, disabled by "
6411 "ZPOOL_SCRIPTS_ENABLED.\n"));
6415 if ((getuid() <= 0 || geteuid() <= 0) &&
6416 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6417 fprintf(stderr
, gettext(
6418 "Can't run -c with root privileges "
6419 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6429 follow_links
= B_TRUE
;
6435 get_timestamp_arg(*optarg
);
6459 omit_since_boot
= B_TRUE
;
6462 headers_once
= B_TRUE
;
6468 if (optopt
== 'c') {
6469 print_zpool_script_list("iostat");
6473 gettext("invalid option '%c'\n"), optopt
);
6482 cb
.cb_literal
= parsable
;
6483 cb
.cb_scripted
= scripted
;
6486 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
6488 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6490 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
6491 cb
.cb_iteration
= 0;
6492 cb
.cb_namewidth
= 0;
6493 cb
.cb_verbose
= verbose
;
6495 /* Get our interval and count values (if any) */
6497 get_interval_count_filter_guids(&argc
, argv
, &interval
,
6500 get_interval_count(&argc
, argv
, &interval
, &count
);
6504 /* No args, so just print the defaults. */
6505 } else if (are_all_pools(argc
, argv
)) {
6506 /* All the args are pool names */
6507 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
6508 /* All the args are vdevs */
6509 cb
.cb_vdevs
.cb_names
= argv
;
6510 cb
.cb_vdevs
.cb_names_count
= argc
;
6511 argc
= 0; /* No pools to process */
6512 } else if (are_all_pools(1, argv
)) {
6513 /* The first arg is a pool name */
6514 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
6516 /* ...and the rest are vdev names */
6517 cb
.cb_vdevs
.cb_names
= argv
+ 1;
6518 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
6519 argc
= 1; /* One pool to process */
6521 fprintf(stderr
, gettext("Expected either a list of "));
6522 fprintf(stderr
, gettext("pools, or list of vdevs in"));
6523 fprintf(stderr
, " \"%s\", ", argv
[0]);
6524 fprintf(stderr
, gettext("but got:\n"));
6525 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
6526 argv
[0], &cb
.cb_vdevs
);
6527 fprintf(stderr
, "\n");
6533 * The args don't make sense. The first arg isn't a pool name,
6534 * nor are all the args vdevs.
6536 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
6537 fprintf(stderr
, "\n");
6541 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
6543 * If user specified vdevs, it implies verbose.
6545 cb
.cb_verbose
= B_TRUE
;
6549 * Construct the list of all interesting pools.
6552 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
6556 if (pool_list_count(list
) == 0 && argc
!= 0) {
6557 pool_list_free(list
);
6561 if (pool_list_count(list
) == 0 && interval
== 0) {
6562 pool_list_free(list
);
6563 (void) fprintf(stderr
, gettext("no pools available\n"));
6567 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
6568 pool_list_free(list
);
6569 (void) fprintf(stderr
,
6570 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
6575 if (l_histo
&& rq_histo
) {
6576 pool_list_free(list
);
6577 (void) fprintf(stderr
,
6578 gettext("Only one of [-r|-w] can be passed at a time\n"));
6584 * Enter the main iostat loop.
6590 * Histograms tables look out of place when you try to display
6591 * them with the other stats, so make a rule that you can only
6592 * print histograms by themselves.
6594 cb
.cb_flags
= IOS_L_HISTO_M
;
6595 } else if (rq_histo
) {
6596 cb
.cb_flags
= IOS_RQ_HISTO_M
;
6598 cb
.cb_flags
= IOS_DEFAULT_M
;
6600 cb
.cb_flags
|= IOS_LATENCY_M
;
6602 cb
.cb_flags
|= IOS_QUEUES_M
;
6606 * See if the module supports all the stats we want to display.
6608 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
6609 if (unsupported_flags
) {
6613 gettext("The loaded zfs module doesn't support:"));
6615 /* for each bit set in unsupported_flags */
6616 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
6617 idx
= lowbit64(f
) - 1;
6618 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
6621 fprintf(stderr
, ". Try running a newer module.\n");
6622 pool_list_free(list
);
6628 if ((npools
= pool_list_count(list
)) == 0)
6629 (void) fprintf(stderr
, gettext("no pools available\n"));
6632 * If this is the first iteration and -y was supplied
6633 * we skip any printing.
6635 boolean_t skip
= (omit_since_boot
&&
6636 cb
.cb_iteration
== 0);
6639 * Refresh all statistics. This is done as an
6640 * explicit step before calculating the maximum name
6641 * width, so that any * configuration changes are
6642 * properly accounted for.
6644 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
6648 * Iterate over all pools to determine the maximum width
6649 * for the pool / device name column across all pools.
6651 cb
.cb_namewidth
= 0;
6652 (void) pool_list_iter(list
, B_FALSE
,
6653 get_namewidth_iostat
, &cb
);
6655 if (timestamp_fmt
!= NODATE
)
6656 print_timestamp(timestamp_fmt
);
6658 if (cmd
!= NULL
&& cb
.cb_verbose
&&
6659 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
6660 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
6661 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
6662 cb
.cb_vdevs
.cb_names_count
,
6663 cb
.cb_vdevs
.cb_name_flags
);
6670 * Check terminal size so we can print headers
6671 * even when terminal window has its height
6674 winheight
= terminal_height();
6676 * Are we connected to TTY? If not, headers_once
6677 * should be true, to avoid breaking scripts.
6680 headers_once
= B_TRUE
;
6683 * If it's the first time and we're not skipping it,
6684 * or either skip or verbose mode, print the header.
6686 * The histogram code explicitly prints its header on
6687 * every vdev, so skip this for histograms.
6689 if (((++cb
.cb_iteration
== 1 && !skip
) ||
6690 (skip
!= verbose
) ||
6692 (cb
.cb_iteration
% winheight
) == 0)) &&
6693 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
6695 print_iostat_header(&cb
);
6698 (void) fflush(stdout
);
6699 (void) fsleep(interval
);
6703 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
6706 * If there's more than one pool, and we're not in
6707 * verbose mode (which prints a separator for us),
6708 * then print a separator.
6710 * In addition, if we're printing specific vdevs then
6711 * we also want an ending separator.
6713 if (((npools
> 1 && !verbose
&&
6714 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
6715 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
6716 cb
.cb_vdevs
.cb_names_count
)) &&
6718 print_iostat_separator(&cb
);
6719 if (cb
.vcdl
!= NULL
)
6720 print_cmd_columns(cb
.vcdl
, 1);
6724 if (cb
.vcdl
!= NULL
)
6725 free_vdev_cmd_data_list(cb
.vcdl
);
6732 if (count
!= 0 && --count
== 0)
6735 (void) fflush(stdout
);
6736 (void) fsleep(interval
);
6739 pool_list_free(list
);
6744 typedef struct list_cbdata
{
6745 boolean_t cb_verbose
;
6749 boolean_t cb_scripted
;
6750 zprop_list_t
*cb_proplist
;
6751 boolean_t cb_literal
;
6753 boolean_t cb_json_as_int
;
6754 boolean_t cb_json_pool_key_guid
;
6759 * Given a list of columns to display, output appropriate headers for each one.
6762 print_header(list_cbdata_t
*cb
)
6764 zprop_list_t
*pl
= cb
->cb_proplist
;
6765 char headerbuf
[ZPOOL_MAXPROPLEN
];
6767 boolean_t first
= B_TRUE
;
6768 boolean_t right_justify
;
6771 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6772 width
= pl
->pl_width
;
6773 if (first
&& cb
->cb_verbose
) {
6775 * Reset the width to accommodate the verbose listing
6778 width
= cb
->cb_namewidth
;
6782 (void) fputs(" ", stdout
);
6786 right_justify
= B_FALSE
;
6787 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6788 header
= zpool_prop_column_name(pl
->pl_prop
);
6789 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6793 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
6794 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
6795 headerbuf
[i
] = '\0';
6799 if (pl
->pl_next
== NULL
&& !right_justify
)
6800 (void) fputs(header
, stdout
);
6801 else if (right_justify
)
6802 (void) printf("%*s", (int)width
, header
);
6804 (void) printf("%-*s", (int)width
, header
);
6807 (void) fputc('\n', stdout
);
6811 * Given a pool and a list of properties, print out all the properties according
6812 * to the described layout. Used by zpool_do_list().
6815 collect_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
6817 zprop_list_t
*pl
= cb
->cb_proplist
;
6818 boolean_t first
= B_TRUE
;
6819 char property
[ZPOOL_MAXPROPLEN
];
6820 const char *propstr
;
6821 boolean_t right_justify
;
6823 zprop_source_t sourcetype
= ZPROP_SRC_NONE
;
6824 nvlist_t
*item
, *d
, *props
;
6825 item
= d
= props
= NULL
;
6828 item
= fnvlist_alloc();
6829 props
= fnvlist_alloc();
6830 d
= fnvlist_lookup_nvlist(cb
->cb_jsobj
, "pools");
6832 fprintf(stderr
, "pools obj not found.\n");
6835 fill_pool_info(item
, zhp
, B_TRUE
, cb
->cb_json_as_int
);
6838 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6840 width
= pl
->pl_width
;
6841 if (first
&& cb
->cb_verbose
) {
6843 * Reset the width to accommodate the verbose listing
6846 width
= cb
->cb_namewidth
;
6849 if (!cb
->cb_json
&& !first
) {
6850 if (cb
->cb_scripted
)
6851 (void) fputc('\t', stdout
);
6853 (void) fputs(" ", stdout
);
6858 right_justify
= B_FALSE
;
6859 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6860 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6861 sizeof (property
), &sourcetype
,
6862 cb
->cb_literal
) != 0)
6867 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6868 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6869 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6870 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6871 sizeof (property
)) == 0) {
6873 sourcetype
= ZPROP_SRC_LOCAL
;
6874 } else if (zfs_prop_user(pl
->pl_user_prop
) &&
6875 zpool_get_userprop(zhp
, pl
->pl_user_prop
, property
,
6876 sizeof (property
), &sourcetype
) == 0) {
6883 if (pl
->pl_prop
== ZPOOL_PROP_NAME
)
6885 (void) zprop_nvlist_one_property(
6886 zpool_prop_to_name(pl
->pl_prop
), propstr
,
6887 sourcetype
, NULL
, NULL
, props
, cb
->cb_json_as_int
);
6890 * If this is being called in scripted mode, or if this
6891 * is the last column and it is left-justified, don't
6892 * include a width format specifier.
6894 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&&
6896 (void) fputs(propstr
, stdout
);
6897 else if (right_justify
)
6898 (void) printf("%*s", (int)width
, propstr
);
6900 (void) printf("%-*s", (int)width
, propstr
);
6905 fnvlist_add_nvlist(item
, "properties", props
);
6906 if (cb
->cb_json_pool_key_guid
) {
6907 char pool_guid
[256];
6908 uint64_t guid
= fnvlist_lookup_uint64(
6909 zpool_get_config(zhp
, NULL
),
6910 ZPOOL_CONFIG_POOL_GUID
);
6911 snprintf(pool_guid
, 256, "%llu",
6912 (u_longlong_t
)guid
);
6913 fnvlist_add_nvlist(d
, pool_guid
, item
);
6915 fnvlist_add_nvlist(d
, zpool_get_name(zhp
),
6918 fnvlist_free(props
);
6921 (void) fputc('\n', stdout
);
6925 collect_vdev_prop(zpool_prop_t prop
, uint64_t value
, const char *str
,
6926 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
,
6927 boolean_t json
, nvlist_t
*nvl
, boolean_t as_int
)
6931 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6934 case ZPOOL_PROP_SIZE
:
6935 case ZPOOL_PROP_EXPANDSZ
:
6936 case ZPOOL_PROP_CHECKPOINT
:
6937 case ZPOOL_PROP_DEDUPRATIO
:
6938 case ZPOOL_PROP_DEDUPCACHED
:
6940 (void) strlcpy(propval
, "-", sizeof (propval
));
6942 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6945 case ZPOOL_PROP_FRAGMENTATION
:
6946 if (value
== ZFS_FRAG_INVALID
) {
6947 (void) strlcpy(propval
, "-", sizeof (propval
));
6948 } else if (format
== ZFS_NICENUM_RAW
) {
6949 (void) snprintf(propval
, sizeof (propval
), "%llu",
6950 (unsigned long long)value
);
6952 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6953 (unsigned long long)value
);
6956 case ZPOOL_PROP_CAPACITY
:
6957 /* capacity value is in parts-per-10,000 (aka permyriad) */
6958 if (format
== ZFS_NICENUM_RAW
)
6959 (void) snprintf(propval
, sizeof (propval
), "%llu",
6960 (unsigned long long)value
/ 100);
6962 (void) snprintf(propval
, sizeof (propval
),
6963 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6964 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6966 case ZPOOL_PROP_HEALTH
:
6968 (void) strlcpy(propval
, str
, sizeof (propval
));
6971 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6975 (void) strlcpy(propval
, "-", sizeof (propval
));
6978 zprop_nvlist_one_property(zpool_prop_to_name(prop
), propval
,
6979 ZPROP_SRC_NONE
, NULL
, NULL
, nvl
, as_int
);
6982 (void) printf("\t%s", propval
);
6984 (void) printf(" %*s", (int)width
, propval
);
6989 * print static default line per vdev
6990 * not compatible with '-o' <proplist> option
6993 collect_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6994 list_cbdata_t
*cb
, int depth
, boolean_t isspare
, nvlist_t
*item
)
6998 uint_t c
, children
= 0;
7000 boolean_t scripted
= cb
->cb_scripted
;
7001 uint64_t islog
= B_FALSE
;
7002 nvlist_t
*props
, *ent
, *ch
, *obj
, *l2c
, *sp
;
7003 props
= ent
= ch
= obj
= sp
= l2c
= NULL
;
7004 const char *dashes
= "%-*s - - - - "
7007 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
7008 (uint64_t **)&vs
, &c
) == 0);
7011 boolean_t toplevel
= (vs
->vs_space
!= 0);
7013 enum zfs_nicenum_format format
;
7017 format
= ZFS_NICENUM_RAW
;
7019 format
= ZFS_NICENUM_1024
;
7021 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
7025 props
= fnvlist_alloc();
7026 ent
= fnvlist_alloc();
7027 fill_vdev_info(ent
, zhp
, (char *)name
, B_FALSE
,
7028 cb
->cb_json_as_int
);
7031 (void) printf("\t%s", name
);
7032 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
7033 (void) printf("%*s%s", depth
, "", name
);
7035 (void) printf("%*s%s%*s", depth
, "", name
,
7036 (int)(cb
->cb_namewidth
- strlen(name
) -
7041 * Print the properties for the individual vdevs. Some
7042 * properties are only applicable to toplevel vdevs. The
7043 * 'toplevel' boolean value is passed to the print_one_column()
7044 * to indicate that the value is valid.
7046 if (VDEV_STAT_VALID(vs_pspace
, c
) && vs
->vs_pspace
) {
7047 collect_vdev_prop(ZPOOL_PROP_SIZE
, vs
->vs_pspace
, NULL
,
7048 scripted
, B_TRUE
, format
, cb
->cb_json
, props
,
7049 cb
->cb_json_as_int
);
7051 collect_vdev_prop(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
,
7052 scripted
, toplevel
, format
, cb
->cb_json
, props
,
7053 cb
->cb_json_as_int
);
7055 collect_vdev_prop(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
7056 scripted
, toplevel
, format
, cb
->cb_json
, props
,
7057 cb
->cb_json_as_int
);
7058 collect_vdev_prop(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
7059 NULL
, scripted
, toplevel
, format
, cb
->cb_json
, props
,
7060 cb
->cb_json_as_int
);
7061 collect_vdev_prop(ZPOOL_PROP_CHECKPOINT
,
7062 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
,
7063 cb
->cb_json
, props
, cb
->cb_json_as_int
);
7064 collect_vdev_prop(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
7065 scripted
, B_TRUE
, format
, cb
->cb_json
, props
,
7066 cb
->cb_json_as_int
);
7067 collect_vdev_prop(ZPOOL_PROP_FRAGMENTATION
,
7068 vs
->vs_fragmentation
, NULL
, scripted
,
7069 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
7070 format
, cb
->cb_json
, props
, cb
->cb_json_as_int
);
7071 cap
= (vs
->vs_space
== 0) ? 0 :
7072 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
7073 collect_vdev_prop(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
7074 scripted
, toplevel
, format
, cb
->cb_json
, props
,
7075 cb
->cb_json_as_int
);
7076 collect_vdev_prop(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
7077 scripted
, toplevel
, format
, cb
->cb_json
, props
,
7078 cb
->cb_json_as_int
);
7079 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
7081 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
7083 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
7086 collect_vdev_prop(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
7087 B_TRUE
, format
, cb
->cb_json
, props
, cb
->cb_json_as_int
);
7090 fnvlist_add_nvlist(ent
, "properties", props
);
7091 fnvlist_free(props
);
7093 (void) fputc('\n', stdout
);
7096 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
7097 &child
, &children
) != 0) {
7099 fnvlist_add_nvlist(item
, name
, ent
);
7106 ch
= fnvlist_alloc();
7109 /* list the normal vdevs first */
7110 for (c
= 0; c
< children
; c
++) {
7111 uint64_t ishole
= B_FALSE
;
7113 if (nvlist_lookup_uint64(child
[c
],
7114 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
7117 if (nvlist_lookup_uint64(child
[c
],
7118 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
7121 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
7124 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
7125 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
7127 if (name
== NULL
|| cb
->cb_json
!= B_TRUE
)
7128 collect_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
7130 else if (cb
->cb_json
) {
7131 collect_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
7138 if (!nvlist_empty(ch
))
7139 fnvlist_add_nvlist(ent
, "vdevs", ch
);
7143 /* list the classes: 'logs', 'dedup', and 'special' */
7144 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
7145 boolean_t printed
= B_FALSE
;
7147 obj
= fnvlist_alloc();
7148 for (c
= 0; c
< children
; c
++) {
7149 const char *bias
= NULL
;
7150 const char *type
= NULL
;
7152 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
7153 &islog
) == 0 && islog
) {
7154 bias
= VDEV_ALLOC_CLASS_LOGS
;
7156 (void) nvlist_lookup_string(child
[c
],
7157 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
7158 (void) nvlist_lookup_string(child
[c
],
7159 ZPOOL_CONFIG_TYPE
, &type
);
7161 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
7163 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
7166 if (!printed
&& !cb
->cb_json
) {
7167 /* LINTED E_SEC_PRINTF_VAR_FMT */
7168 (void) printf(dashes
, cb
->cb_namewidth
,
7172 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
7173 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
7174 collect_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
7179 if (!nvlist_empty(obj
))
7180 fnvlist_add_nvlist(item
, class_name
[n
], obj
);
7185 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
7186 &child
, &children
) == 0 && children
> 0) {
7188 l2c
= fnvlist_alloc();
7190 /* LINTED E_SEC_PRINTF_VAR_FMT */
7191 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
7193 for (c
= 0; c
< children
; c
++) {
7194 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
7196 collect_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
7201 if (!nvlist_empty(l2c
))
7202 fnvlist_add_nvlist(item
, "l2cache", l2c
);
7207 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
7208 &children
) == 0 && children
> 0) {
7210 sp
= fnvlist_alloc();
7212 /* LINTED E_SEC_PRINTF_VAR_FMT */
7213 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
7215 for (c
= 0; c
< children
; c
++) {
7216 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
7218 collect_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
7223 if (!nvlist_empty(sp
))
7224 fnvlist_add_nvlist(item
, "spares", sp
);
7229 if (name
!= NULL
&& cb
->cb_json
) {
7230 fnvlist_add_nvlist(item
, name
, ent
);
7236 * Generic callback function to list a pool.
7239 list_callback(zpool_handle_t
*zhp
, void *data
)
7241 nvlist_t
*p
, *d
, *nvdevs
;
7243 char pool_guid
[256];
7244 const char *pool_name
= zpool_get_name(zhp
);
7245 list_cbdata_t
*cbp
= data
;
7246 p
= d
= nvdevs
= NULL
;
7248 collect_pool(zhp
, cbp
);
7250 if (cbp
->cb_verbose
) {
7251 nvlist_t
*config
, *nvroot
;
7252 config
= zpool_get_config(zhp
, NULL
);
7253 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
7256 d
= fnvlist_lookup_nvlist(cbp
->cb_jsobj
,
7258 if (cbp
->cb_json_pool_key_guid
) {
7259 guid
= fnvlist_lookup_uint64(config
,
7260 ZPOOL_CONFIG_POOL_GUID
);
7261 snprintf(pool_guid
, 256, "%llu",
7262 (u_longlong_t
)guid
);
7263 p
= fnvlist_lookup_nvlist(d
, pool_guid
);
7265 p
= fnvlist_lookup_nvlist(d
, pool_name
);
7267 nvdevs
= fnvlist_alloc();
7269 collect_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
, nvdevs
);
7271 fnvlist_add_nvlist(p
, "vdevs", nvdevs
);
7272 if (cbp
->cb_json_pool_key_guid
)
7273 fnvlist_add_nvlist(d
, pool_guid
, p
);
7275 fnvlist_add_nvlist(d
, pool_name
, p
);
7276 fnvlist_add_nvlist(cbp
->cb_jsobj
, "pools", d
);
7277 fnvlist_free(nvdevs
);
7285 * Set the minimum pool/vdev name column width. The width must be at least 9,
7286 * but may be as large as needed.
7289 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
7291 list_cbdata_t
*cb
= data
;
7294 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
7295 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
7300 cb
->cb_namewidth
= width
;
7306 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
7308 * -g Display guid for individual vdev name.
7309 * -H Scripted mode. Don't display headers, and separate properties
7311 * -L Follow links when resolving vdev path name.
7312 * -o List of properties to display. Defaults to
7313 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
7314 * "dedupratio,health,altroot"
7315 * -p Display values in parsable (exact) format.
7316 * -P Display full path for vdev name.
7317 * -T Display a timestamp in date(1) or Unix format
7318 * -j Display the output in JSON format
7319 * --json-int Display the numbers as integer instead of strings.
7320 * --json-pool-key-guid Set pool GUID as key for pool objects.
7322 * List all pools in the system, whether or not they're healthy. Output space
7323 * statistics for each one, as well as health status summary.
7326 zpool_do_list(int argc
, char **argv
)
7330 list_cbdata_t cb
= { 0 };
7331 static char default_props
[] =
7332 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
7333 "capacity,dedupratio,health,altroot";
7334 char *props
= default_props
;
7336 unsigned long count
= 0;
7338 boolean_t first
= B_TRUE
;
7339 nvlist_t
*data
= NULL
;
7340 current_prop_type
= ZFS_TYPE_POOL
;
7342 struct option long_options
[] = {
7343 {"json-int", no_argument
, NULL
, ZPOOL_OPTION_JSON_NUMS_AS_INT
},
7344 {"json-pool-key-guid", no_argument
, NULL
,
7345 ZPOOL_OPTION_POOL_KEY_GUID
},
7350 while ((c
= getopt_long(argc
, argv
, ":gjHLo:pPT:v", long_options
,
7354 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
7357 cb
.cb_scripted
= B_TRUE
;
7360 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
7366 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
7369 cb
.cb_literal
= B_TRUE
;
7372 cb
.cb_json
= B_TRUE
;
7374 case ZPOOL_OPTION_JSON_NUMS_AS_INT
:
7375 cb
.cb_json_as_int
= B_TRUE
;
7376 cb
.cb_literal
= B_TRUE
;
7378 case ZPOOL_OPTION_POOL_KEY_GUID
:
7379 cb
.cb_json_pool_key_guid
= B_TRUE
;
7382 get_timestamp_arg(*optarg
);
7385 cb
.cb_verbose
= B_TRUE
;
7386 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
7389 (void) fprintf(stderr
, gettext("missing argument for "
7390 "'%c' option\n"), optopt
);
7394 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7403 if (!cb
.cb_json
&& cb
.cb_json_as_int
) {
7404 (void) fprintf(stderr
, gettext("'--json-int' only works with"
7409 if (!cb
.cb_json
&& cb
.cb_json_pool_key_guid
) {
7410 (void) fprintf(stderr
, gettext("'json-pool-key-guid' only"
7411 " works with '-j' option\n"));
7415 get_interval_count(&argc
, argv
, &interval
, &count
);
7417 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
7421 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
7422 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
7425 if (pool_list_count(list
) == 0)
7429 cb
.cb_jsobj
= zpool_json_schema(0, 1);
7430 data
= fnvlist_alloc();
7431 fnvlist_add_nvlist(cb
.cb_jsobj
, "pools", data
);
7435 cb
.cb_namewidth
= 0;
7436 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
7438 if (timestamp_fmt
!= NODATE
) {
7440 if (cb
.cb_json_as_int
) {
7441 fnvlist_add_uint64(cb
.cb_jsobj
, "time",
7445 get_timestamp(timestamp_fmt
, ts
, 128);
7446 fnvlist_add_string(cb
.cb_jsobj
, "time",
7450 print_timestamp(timestamp_fmt
);
7453 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
) &&
7458 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
7460 if (ret
== 0 && cb
.cb_json
)
7461 zcmd_print_json(cb
.cb_jsobj
);
7462 else if (ret
!= 0 && cb
.cb_json
)
7463 nvlist_free(cb
.cb_jsobj
);
7468 if (count
!= 0 && --count
== 0)
7471 pool_list_free(list
);
7473 (void) fflush(stdout
);
7474 (void) fsleep(interval
);
7477 if (argc
== 0 && !cb
.cb_scripted
&& !cb
.cb_json
&&
7478 pool_list_count(list
) == 0) {
7479 (void) printf(gettext("no pools available\n"));
7483 pool_list_free(list
);
7484 zprop_free_list(cb
.cb_proplist
);
7489 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
7491 boolean_t force
= B_FALSE
;
7492 boolean_t rebuild
= B_FALSE
;
7493 boolean_t wait
= B_FALSE
;
7496 char *poolname
, *old_disk
, *new_disk
;
7497 zpool_handle_t
*zhp
;
7498 nvlist_t
*props
= NULL
;
7503 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
7509 if ((propval
= strchr(optarg
, '=')) == NULL
) {
7510 (void) fprintf(stderr
, gettext("missing "
7511 "'=' for -o option\n"));
7517 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
7518 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
7528 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7537 /* get pool name and check number of arguments */
7539 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7546 (void) fprintf(stderr
,
7547 gettext("missing <device> specification\n"));
7555 (void) fprintf(stderr
,
7556 gettext("missing <new_device> specification\n"));
7559 new_disk
= old_disk
;
7569 (void) fprintf(stderr
, gettext("too many arguments\n"));
7573 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7578 if (zpool_get_config(zhp
, NULL
) == NULL
) {
7579 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
7586 /* unless manually specified use "ashift" pool property (if set) */
7587 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
7590 char strval
[ZPOOL_MAXPROPLEN
];
7592 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
7593 if (src
!= ZPROP_SRC_DEFAULT
) {
7594 (void) sprintf(strval
, "%" PRId32
, intval
);
7595 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
7596 &props
, B_TRUE
) == 0);
7600 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
7602 if (nvroot
== NULL
) {
7608 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
7611 if (ret
== 0 && wait
) {
7612 zpool_wait_activity_t activity
= ZPOOL_WAIT_RESILVER
;
7613 char raidz_prefix
[] = "raidz";
7615 activity
= ZPOOL_WAIT_REPLACE
;
7616 } else if (strncmp(old_disk
,
7617 raidz_prefix
, strlen(raidz_prefix
)) == 0) {
7618 activity
= ZPOOL_WAIT_RAIDZ_EXPAND
;
7620 ret
= zpool_wait(zhp
, activity
);
7624 nvlist_free(nvroot
);
7631 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
7633 * -f Force attach, even if <new_device> appears to be in use.
7634 * -s Use sequential instead of healing reconstruction for resilver.
7635 * -o Set property=value.
7636 * -w Wait for replacing to complete before returning
7638 * Replace <device> with <new_device>.
7641 zpool_do_replace(int argc
, char **argv
)
7643 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
7647 * zpool attach [-fsw] [-o property=value] <pool> <device>|<vdev> <new_device>
7649 * -f Force attach, even if <new_device> appears to be in use.
7650 * -s Use sequential instead of healing reconstruction for resilver.
7651 * -o Set property=value.
7652 * -w Wait for resilvering (mirror) or expansion (raidz) to complete
7655 * Attach <new_device> to a <device> or <vdev>, where the vdev can be of type
7656 * mirror or raidz. If <device> is not part of a mirror, then <device> will
7657 * be transformed into a mirror of <device> and <new_device>. When a mirror
7658 * is involved, <new_device> will begin life with a DTL of [0, now], and will
7659 * immediately begin to resilver itself. For the raidz case, a expansion will
7660 * commence and reflow the raidz data across all the disks including the
7664 zpool_do_attach(int argc
, char **argv
)
7666 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
7670 * zpool detach [-f] <pool> <device>
7672 * -f Force detach of <device>, even if DTLs argue against it
7673 * (not supported yet)
7675 * Detach a device from a mirror. The operation will be refused if <device>
7676 * is the last device in the mirror, or if the DTLs indicate that this device
7677 * has the only valid copy of some data.
7680 zpool_do_detach(int argc
, char **argv
)
7683 char *poolname
, *path
;
7684 zpool_handle_t
*zhp
;
7688 while ((c
= getopt(argc
, argv
, "")) != -1) {
7691 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7700 /* get pool name and check number of arguments */
7702 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7707 (void) fprintf(stderr
,
7708 gettext("missing <device> specification\n"));
7715 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7718 ret
= zpool_vdev_detach(zhp
, path
);
7726 * zpool split [-gLnP] [-o prop=val] ...
7728 * [-R altroot] <pool> <newpool> [<device> ...]
7730 * -g Display guid for individual vdev name.
7731 * -L Follow links when resolving vdev path name.
7732 * -n Do not split the pool, but display the resulting layout if
7733 * it were to be split.
7734 * -o Set property=value, or set mount options.
7735 * -P Display full path for vdev name.
7736 * -R Mount the split-off pool under an alternate root.
7737 * -l Load encryption keys while importing.
7739 * Splits the named pool and gives it the new pool name. Devices to be split
7740 * off may be listed, provided that no more than one device is specified
7741 * per top-level vdev mirror. The newly split pool is left in an exported
7742 * state unless -R is specified.
7744 * Restrictions: the top-level of the pool pool must only be made up of
7745 * mirrors; all devices in the pool must be healthy; no device may be
7746 * undergoing a resilvering operation.
7749 zpool_do_split(int argc
, char **argv
)
7751 char *srcpool
, *newpool
, *propval
;
7752 char *mntopts
= NULL
;
7756 boolean_t loadkeys
= B_FALSE
;
7757 zpool_handle_t
*zhp
;
7758 nvlist_t
*config
, *props
= NULL
;
7760 flags
.dryrun
= B_FALSE
;
7761 flags
.import
= B_FALSE
;
7762 flags
.name_flags
= 0;
7765 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
7768 flags
.name_flags
|= VDEV_NAME_GUID
;
7771 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
7774 flags
.import
= B_TRUE
;
7776 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
7777 &props
, B_TRUE
) != 0) {
7786 flags
.dryrun
= B_TRUE
;
7789 if ((propval
= strchr(optarg
, '=')) != NULL
) {
7792 if (add_prop_list(optarg
, propval
,
7793 &props
, B_TRUE
) != 0) {
7802 flags
.name_flags
|= VDEV_NAME_PATH
;
7805 (void) fprintf(stderr
, gettext("missing argument for "
7806 "'%c' option\n"), optopt
);
7810 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7817 if (!flags
.import
&& mntopts
!= NULL
) {
7818 (void) fprintf(stderr
, gettext("setting mntopts is only "
7819 "valid when importing the pool\n"));
7823 if (!flags
.import
&& loadkeys
) {
7824 (void) fprintf(stderr
, gettext("loading keys is only "
7825 "valid when importing the pool\n"));
7833 (void) fprintf(stderr
, gettext("Missing pool name\n"));
7837 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
7847 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
7852 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
7853 if (config
== NULL
) {
7857 (void) printf(gettext("would create '%s' with the "
7858 "following layout:\n\n"), newpool
);
7859 print_vdev_tree(NULL
, newpool
, config
, 0, "",
7861 print_vdev_tree(NULL
, "dedup", config
, 0,
7862 VDEV_ALLOC_BIAS_DEDUP
, 0);
7863 print_vdev_tree(NULL
, "special", config
, 0,
7864 VDEV_ALLOC_BIAS_SPECIAL
, 0);
7870 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
7871 nvlist_free(config
);
7877 * The split was successful. Now we need to open the new
7878 * pool and import it.
7880 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
7881 nvlist_free(config
);
7887 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
7892 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
) {
7893 ms_status
= zpool_enable_datasets(zhp
, mntopts
, 0,
7895 if (ms_status
== EZFS_SHAREFAILED
) {
7896 (void) fprintf(stderr
, gettext("Split was successful, "
7897 "datasets are mounted but sharing of some datasets "
7899 } else if (ms_status
== EZFS_MOUNTFAILED
) {
7900 (void) fprintf(stderr
, gettext("Split was successful"
7901 ", but some datasets could not be mounted\n"));
7902 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
7903 "different altroot\n"), "zpool import");
7907 nvlist_free(config
);
7915 * zpool online [--power] <pool> <device> ...
7917 * --power: Power on the enclosure slot to the drive (if possible)
7920 zpool_do_online(int argc
, char **argv
)
7924 zpool_handle_t
*zhp
;
7926 vdev_state_t newstate
;
7928 boolean_t is_power_on
= B_FALSE
;
7929 struct option long_options
[] = {
7930 {"power", no_argument
, NULL
, ZPOOL_OPTION_POWER
},
7935 while ((c
= getopt_long(argc
, argv
, "e", long_options
, NULL
)) != -1) {
7938 flags
|= ZFS_ONLINE_EXPAND
;
7940 case ZPOOL_OPTION_POWER
:
7941 is_power_on
= B_TRUE
;
7944 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7950 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
7951 is_power_on
= B_TRUE
;
7956 /* get pool name and check number of arguments */
7958 (void) fprintf(stderr
, gettext("missing pool name\n"));
7962 (void) fprintf(stderr
, gettext("missing device name\n"));
7968 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7971 for (i
= 1; i
< argc
; i
++) {
7972 vdev_state_t oldstate
;
7973 boolean_t avail_spare
, l2cache
;
7977 rc
= zpool_power_on_and_disk_wait(zhp
, argv
[i
]);
7978 if (rc
== ENOTSUP
) {
7979 (void) fprintf(stderr
,
7980 gettext("Power control not supported\n"));
7986 nvlist_t
*tgt
= zpool_find_vdev(zhp
, argv
[i
], &avail_spare
,
7993 oldstate
= ((vdev_stat_t
*)fnvlist_lookup_uint64_array(tgt
,
7994 ZPOOL_CONFIG_VDEV_STATS
, &vsc
))->vs_state
;
7995 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
7996 if (newstate
!= VDEV_STATE_HEALTHY
) {
7997 (void) printf(gettext("warning: device '%s' "
7998 "onlined, but remains in faulted state\n"),
8000 if (newstate
== VDEV_STATE_FAULTED
)
8001 (void) printf(gettext("use 'zpool "
8002 "clear' to restore a faulted "
8005 (void) printf(gettext("use 'zpool "
8006 "replace' to replace devices "
8007 "that are no longer present\n"));
8008 if ((flags
& ZFS_ONLINE_EXPAND
)) {
8009 (void) printf(gettext("%s: failed "
8010 "to expand usable space on "
8011 "unhealthy device '%s'\n"),
8012 (oldstate
>= VDEV_STATE_DEGRADED
?
8013 "error" : "warning"), argv
[i
]);
8014 if (oldstate
>= VDEV_STATE_DEGRADED
) {
8031 * zpool offline [-ft]|[--power] <pool> <device> ...
8034 * -f Force the device into a faulted state.
8036 * -t Only take the device off-line temporarily. The offline/faulted
8037 * state will not be persistent across reboots.
8039 * --power Power off the enclosure slot to the drive (if possible)
8042 zpool_do_offline(int argc
, char **argv
)
8046 zpool_handle_t
*zhp
;
8048 boolean_t istmp
= B_FALSE
;
8049 boolean_t fault
= B_FALSE
;
8050 boolean_t is_power_off
= B_FALSE
;
8052 struct option long_options
[] = {
8053 {"power", no_argument
, NULL
, ZPOOL_OPTION_POWER
},
8058 while ((c
= getopt_long(argc
, argv
, "ft", long_options
, NULL
)) != -1) {
8066 case ZPOOL_OPTION_POWER
:
8067 is_power_off
= B_TRUE
;
8070 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8076 if (is_power_off
&& fault
) {
8077 (void) fprintf(stderr
,
8078 gettext("-0 and -f cannot be used together\n"));
8083 if (is_power_off
&& istmp
) {
8084 (void) fprintf(stderr
,
8085 gettext("-0 and -t cannot be used together\n"));
8093 /* get pool name and check number of arguments */
8095 (void) fprintf(stderr
, gettext("missing pool name\n"));
8099 (void) fprintf(stderr
, gettext("missing device name\n"));
8105 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
8108 for (i
= 1; i
< argc
; i
++) {
8109 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
8112 * Note: we have to power off first, then set REMOVED,
8113 * or else zpool_vdev_set_removed_state() returns
8116 ret
= zpool_power_off(zhp
, argv
[i
]);
8118 (void) fprintf(stderr
, "%s %s %d\n",
8119 gettext("unable to power off slot for"),
8122 zpool_vdev_set_removed_state(zhp
, guid
, VDEV_AUX_NONE
);
8126 if (istmp
== B_FALSE
) {
8127 /* Force the fault to persist across imports */
8128 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
8130 aux
= VDEV_AUX_EXTERNAL
;
8133 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
8136 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
8147 * zpool clear [-nF]|[--power] <pool> [device]
8149 * Clear all errors associated with a pool or a particular device.
8152 zpool_do_clear(int argc
, char **argv
)
8156 boolean_t dryrun
= B_FALSE
;
8157 boolean_t do_rewind
= B_FALSE
;
8158 boolean_t xtreme_rewind
= B_FALSE
;
8159 boolean_t is_power_on
= B_FALSE
;
8160 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
8161 nvlist_t
*policy
= NULL
;
8162 zpool_handle_t
*zhp
;
8163 char *pool
, *device
;
8165 struct option long_options
[] = {
8166 {"power", no_argument
, NULL
, ZPOOL_OPTION_POWER
},
8171 while ((c
= getopt_long(argc
, argv
, "FnX", long_options
,
8181 xtreme_rewind
= B_TRUE
;
8183 case ZPOOL_OPTION_POWER
:
8184 is_power_on
= B_TRUE
;
8187 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8193 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
8194 is_power_on
= B_TRUE
;
8200 (void) fprintf(stderr
, gettext("missing pool name\n"));
8205 (void) fprintf(stderr
, gettext("too many arguments\n"));
8209 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
8210 (void) fprintf(stderr
,
8211 gettext("-n or -X only meaningful with -F\n"));
8215 rewind_policy
= ZPOOL_TRY_REWIND
;
8217 rewind_policy
= ZPOOL_DO_REWIND
;
8219 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
8221 /* In future, further rewind policy choices can be passed along here */
8222 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
8223 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
8224 rewind_policy
) != 0) {
8229 device
= argc
== 2 ? argv
[1] : NULL
;
8231 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
8232 nvlist_free(policy
);
8237 if (device
== NULL
) {
8238 zpool_power_on_pool_and_wait_for_devices(zhp
);
8240 zpool_power_on_and_disk_wait(zhp
, device
);
8244 if (zpool_clear(zhp
, device
, policy
) != 0)
8249 nvlist_free(policy
);
8255 * zpool reguid [-g <guid>] <pool>
8258 zpool_do_reguid(int argc
, char **argv
)
8261 uint64_t *guidp
= NULL
;
8265 zpool_handle_t
*zhp
;
8269 while ((c
= getopt(argc
, argv
, "g:")) != -1) {
8273 guid
= strtoull(optarg
, &endptr
, 10);
8274 if (errno
!= 0 || *endptr
!= '\0') {
8275 (void) fprintf(stderr
,
8276 gettext("invalid GUID: %s\n"), optarg
);
8282 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8291 /* get pool name and check number of arguments */
8293 (void) fprintf(stderr
, gettext("missing pool name\n"));
8298 (void) fprintf(stderr
, gettext("too many arguments\n"));
8303 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
8306 ret
= zpool_set_guid(zhp
, guidp
);
8314 * zpool reopen <pool>
8316 * Reopen the pool so that the kernel can update the sizes of all vdevs.
8319 zpool_do_reopen(int argc
, char **argv
)
8323 boolean_t scrub_restart
= B_TRUE
;
8326 while ((c
= getopt(argc
, argv
, "n")) != -1) {
8329 scrub_restart
= B_FALSE
;
8332 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8341 /* if argc == 0 we will execute zpool_reopen_one on all pools */
8342 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8343 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
8348 typedef struct scrub_cbdata
{
8350 pool_scrub_cmd_t cb_scrub_cmd
;
8354 zpool_has_checkpoint(zpool_handle_t
*zhp
)
8356 nvlist_t
*config
, *nvroot
;
8358 config
= zpool_get_config(zhp
, NULL
);
8360 if (config
!= NULL
) {
8361 pool_checkpoint_stat_t
*pcs
= NULL
;
8364 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8365 (void) nvlist_lookup_uint64_array(nvroot
,
8366 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8368 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
8371 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
8372 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
8380 scrub_callback(zpool_handle_t
*zhp
, void *data
)
8382 scrub_cbdata_t
*cb
= data
;
8386 * Ignore faulted pools.
8388 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
8389 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
8390 "currently unavailable\n"), zpool_get_name(zhp
));
8394 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
8396 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
8397 cb
->cb_type
== POOL_SCAN_SCRUB
) {
8398 (void) printf(gettext("warning: will not scrub state that "
8399 "belongs to the checkpoint of pool '%s'\n"),
8400 zpool_get_name(zhp
));
8407 wait_callback(zpool_handle_t
*zhp
, void *data
)
8409 zpool_wait_activity_t
*act
= data
;
8410 return (zpool_wait(zhp
, *act
));
8414 * zpool scrub [-s | -p] [-w] [-e] <pool> ...
8416 * -e Only scrub blocks in the error log.
8417 * -s Stop. Stops any in-progress scrub.
8418 * -p Pause. Pause in-progress scrub.
8419 * -w Wait. Blocks until scrub has completed.
8422 zpool_do_scrub(int argc
, char **argv
)
8426 boolean_t wait
= B_FALSE
;
8429 cb
.cb_type
= POOL_SCAN_SCRUB
;
8430 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
8432 boolean_t is_error_scrub
= B_FALSE
;
8433 boolean_t is_pause
= B_FALSE
;
8434 boolean_t is_stop
= B_FALSE
;
8437 while ((c
= getopt(argc
, argv
, "spwe")) != -1) {
8440 is_error_scrub
= B_TRUE
;
8452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8458 if (is_pause
&& is_stop
) {
8459 (void) fprintf(stderr
, gettext("invalid option "
8460 "combination :-s and -p are mutually exclusive\n"));
8464 cb
.cb_type
= POOL_SCAN_ERRORSCRUB
;
8467 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
8468 } else if (is_stop
) {
8469 cb
.cb_type
= POOL_SCAN_NONE
;
8471 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
8475 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
8476 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
8477 (void) fprintf(stderr
, gettext("invalid option combination: "
8478 "-w cannot be used with -p or -s\n"));
8486 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
8490 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8491 B_FALSE
, scrub_callback
, &cb
);
8493 if (wait
&& !error
) {
8494 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
8495 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8496 B_FALSE
, wait_callback
, &act
);
8503 * zpool resilver <pool> ...
8505 * Restarts any in-progress resilver
8508 zpool_do_resilver(int argc
, char **argv
)
8513 cb
.cb_type
= POOL_SCAN_RESILVER
;
8514 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
8517 while ((c
= getopt(argc
, argv
, "")) != -1) {
8520 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8530 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
8534 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8535 B_FALSE
, scrub_callback
, &cb
));
8539 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
8541 * -c Cancel. Ends any in-progress trim.
8542 * -d Secure trim. Requires kernel and device support.
8543 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
8544 * adding a multiplier suffix such as 'k' or 'm'.
8545 * -s Suspend. TRIM can then be restarted with no flags.
8546 * -w Wait. Blocks until trimming has completed.
8549 zpool_do_trim(int argc
, char **argv
)
8551 struct option long_options
[] = {
8552 {"cancel", no_argument
, NULL
, 'c'},
8553 {"secure", no_argument
, NULL
, 'd'},
8554 {"rate", required_argument
, NULL
, 'r'},
8555 {"suspend", no_argument
, NULL
, 's'},
8556 {"wait", no_argument
, NULL
, 'w'},
8560 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
8562 boolean_t secure
= B_FALSE
;
8563 boolean_t wait
= B_FALSE
;
8566 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
8570 if (cmd_type
!= POOL_TRIM_START
&&
8571 cmd_type
!= POOL_TRIM_CANCEL
) {
8572 (void) fprintf(stderr
, gettext("-c cannot be "
8573 "combined with other options\n"));
8576 cmd_type
= POOL_TRIM_CANCEL
;
8579 if (cmd_type
!= POOL_TRIM_START
) {
8580 (void) fprintf(stderr
, gettext("-d cannot be "
8581 "combined with the -c or -s options\n"));
8587 if (cmd_type
!= POOL_TRIM_START
) {
8588 (void) fprintf(stderr
, gettext("-r cannot be "
8589 "combined with the -c or -s options\n"));
8592 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
8593 (void) fprintf(stderr
, "%s: %s\n",
8594 gettext("invalid value for rate"),
8595 libzfs_error_description(g_zfs
));
8600 if (cmd_type
!= POOL_TRIM_START
&&
8601 cmd_type
!= POOL_TRIM_SUSPEND
) {
8602 (void) fprintf(stderr
, gettext("-s cannot be "
8603 "combined with other options\n"));
8606 cmd_type
= POOL_TRIM_SUSPEND
;
8613 (void) fprintf(stderr
,
8614 gettext("invalid option '%c'\n"), optopt
);
8616 (void) fprintf(stderr
,
8617 gettext("invalid option '%s'\n"),
8628 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
8633 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
8634 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
8639 char *poolname
= argv
[0];
8640 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
8644 trimflags_t trim_flags
= {
8650 nvlist_t
*vdevs
= fnvlist_alloc();
8652 /* no individual leaf vdevs specified, so add them all */
8653 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
8654 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
8655 ZPOOL_CONFIG_VDEV_TREE
);
8656 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
8657 trim_flags
.fullpool
= B_TRUE
;
8659 trim_flags
.fullpool
= B_FALSE
;
8660 for (int i
= 1; i
< argc
; i
++) {
8661 fnvlist_add_boolean(vdevs
, argv
[i
]);
8665 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
8667 fnvlist_free(vdevs
);
8674 * Converts a total number of seconds to a human readable string broken
8675 * down in to days/hours/minutes/seconds.
8678 secs_to_dhms(uint64_t total
, char *buf
)
8680 uint64_t days
= total
/ 60 / 60 / 24;
8681 uint64_t hours
= (total
/ 60 / 60) % 24;
8682 uint64_t mins
= (total
/ 60) % 60;
8683 uint64_t secs
= (total
% 60);
8686 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
8687 (u_longlong_t
)days
, (u_longlong_t
)hours
,
8688 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
8690 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
8691 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
8692 (u_longlong_t
)secs
);
8697 * Print out detailed error scrub status.
8700 print_err_scrub_status(pool_scan_stat_t
*ps
)
8702 time_t start
, end
, pause
;
8703 uint64_t total_secs_left
;
8704 uint64_t secs_left
, mins_left
, hours_left
, days_left
;
8705 uint64_t examined
, to_be_examined
;
8707 if (ps
== NULL
|| ps
->pss_error_scrub_func
!= POOL_SCAN_ERRORSCRUB
) {
8711 (void) printf(gettext(" scrub: "));
8713 start
= ps
->pss_error_scrub_start
;
8714 end
= ps
->pss_error_scrub_end
;
8715 pause
= ps
->pss_pass_error_scrub_pause
;
8716 examined
= ps
->pss_error_scrub_examined
;
8717 to_be_examined
= ps
->pss_error_scrub_to_be_examined
;
8719 assert(ps
->pss_error_scrub_func
== POOL_SCAN_ERRORSCRUB
);
8721 if (ps
->pss_error_scrub_state
== DSS_FINISHED
) {
8722 total_secs_left
= end
- start
;
8723 days_left
= total_secs_left
/ 60 / 60 / 24;
8724 hours_left
= (total_secs_left
/ 60 / 60) % 24;
8725 mins_left
= (total_secs_left
/ 60) % 60;
8726 secs_left
= (total_secs_left
% 60);
8728 (void) printf(gettext("scrubbed %llu error blocks in %llu days "
8729 "%02llu:%02llu:%02llu on %s"), (u_longlong_t
)examined
,
8730 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
8731 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
8735 } else if (ps
->pss_error_scrub_state
== DSS_CANCELED
) {
8736 (void) printf(gettext("error scrub canceled on %s"),
8740 assert(ps
->pss_error_scrub_state
== DSS_ERRORSCRUBBING
);
8742 /* Error scrub is in progress. */
8744 (void) printf(gettext("error scrub in progress since %s"),
8747 (void) printf(gettext("error scrub paused since %s"),
8749 (void) printf(gettext("\terror scrub started on %s"),
8753 double fraction_done
= (double)examined
/ (to_be_examined
+ examined
);
8754 (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error"
8755 " blocks"), 100 * fraction_done
, (u_longlong_t
)examined
);
8757 (void) printf("\n");
8761 * Print out detailed scrub status.
8764 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
8766 time_t start
, end
, pause
;
8767 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total_s
, total_i
;
8768 uint64_t elapsed
, scan_rate
, issue_rate
;
8769 double fraction_done
;
8770 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_s_buf
[7];
8771 char total_i_buf
[7], srate_buf
[7], irate_buf
[7], time_buf
[32];
8774 printf_color(ANSI_BOLD
, gettext("scan:"));
8777 /* If there's never been a scan, there's not much to say. */
8778 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
8779 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
8780 (void) printf(gettext("none requested\n"));
8784 start
= ps
->pss_start_time
;
8785 end
= ps
->pss_end_time
;
8786 pause
= ps
->pss_pass_scrub_pause
;
8788 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
8790 int is_resilver
= ps
->pss_func
== POOL_SCAN_RESILVER
;
8791 int is_scrub
= ps
->pss_func
== POOL_SCAN_SCRUB
;
8792 assert(is_resilver
|| is_scrub
);
8794 /* Scan is finished or canceled. */
8795 if (ps
->pss_state
== DSS_FINISHED
) {
8796 secs_to_dhms(end
- start
, time_buf
);
8799 (void) printf(gettext("scrub repaired %s "
8800 "in %s with %llu errors on %s"), processed_buf
,
8801 time_buf
, (u_longlong_t
)ps
->pss_errors
,
8803 } else if (is_resilver
) {
8804 (void) printf(gettext("resilvered %s "
8805 "in %s with %llu errors on %s"), processed_buf
,
8806 time_buf
, (u_longlong_t
)ps
->pss_errors
,
8810 } else if (ps
->pss_state
== DSS_CANCELED
) {
8812 (void) printf(gettext("scrub canceled on %s"),
8814 } else if (is_resilver
) {
8815 (void) printf(gettext("resilver canceled on %s"),
8821 assert(ps
->pss_state
== DSS_SCANNING
);
8823 /* Scan is in progress. Resilvers can't be paused. */
8826 (void) printf(gettext("scrub in progress since %s"),
8829 (void) printf(gettext("scrub paused since %s"),
8831 (void) printf(gettext("\tscrub started on %s"),
8834 } else if (is_resilver
) {
8835 (void) printf(gettext("resilver in progress since %s"),
8839 scanned
= ps
->pss_examined
;
8840 pass_scanned
= ps
->pss_pass_exam
;
8841 issued
= ps
->pss_issued
;
8842 pass_issued
= ps
->pss_pass_issued
;
8843 total_s
= ps
->pss_to_examine
;
8844 total_i
= ps
->pss_to_examine
- ps
->pss_skipped
;
8846 /* we are only done with a block once we have issued the IO for it */
8847 fraction_done
= (double)issued
/ total_i
;
8849 /* elapsed time for this pass, rounding up to 1 if it's 0 */
8850 elapsed
= time(NULL
) - ps
->pss_pass_start
;
8851 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
8852 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
8854 scan_rate
= pass_scanned
/ elapsed
;
8855 issue_rate
= pass_issued
/ elapsed
;
8857 /* format all of the numbers we will be reporting */
8858 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
8859 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
8860 zfs_nicebytes(total_s
, total_s_buf
, sizeof (total_s_buf
));
8861 zfs_nicebytes(total_i
, total_i_buf
, sizeof (total_i_buf
));
8863 /* do not print estimated time if we have a paused scrub */
8864 (void) printf(gettext("\t%s / %s scanned"), scanned_buf
, total_s_buf
);
8865 if (pause
== 0 && scan_rate
> 0) {
8866 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
8867 (void) printf(gettext(" at %s/s"), srate_buf
);
8869 (void) printf(gettext(", %s / %s issued"), issued_buf
, total_i_buf
);
8870 if (pause
== 0 && issue_rate
> 0) {
8871 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
8872 (void) printf(gettext(" at %s/s"), irate_buf
);
8874 (void) printf(gettext("\n"));
8877 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
8878 processed_buf
, 100 * fraction_done
);
8879 } else if (is_scrub
) {
8880 (void) printf(gettext("\t%s repaired, %.2f%% done"),
8881 processed_buf
, 100 * fraction_done
);
8886 * Only provide an estimate iff:
8887 * 1) we haven't yet issued all we expected, and
8888 * 2) the issue rate exceeds 10 MB/s, and
8890 * a) a resilver which has started repairs, or
8891 * b) a scrub which has entered the issue phase.
8893 if (total_i
>= issued
&& issue_rate
>= 10 * 1024 * 1024 &&
8894 ((is_resilver
&& ps
->pss_processed
> 0) ||
8895 (is_scrub
&& issued
> 0))) {
8896 secs_to_dhms((total_i
- issued
) / issue_rate
, time_buf
);
8897 (void) printf(gettext(", %s to go\n"), time_buf
);
8899 (void) printf(gettext(", no estimated "
8900 "completion time\n"));
8903 (void) printf(gettext("\n"));
8908 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, uint_t c
, char *vdev_name
)
8910 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
8914 printf_color(ANSI_BOLD
, gettext("scan:"));
8917 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
8918 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
8919 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
8920 uint64_t bytes_est_s
= vrs
->vrs_bytes_est
;
8921 uint64_t bytes_est_i
= vrs
->vrs_bytes_est
;
8922 if (c
> offsetof(vdev_rebuild_stat_t
, vrs_pass_bytes_skipped
) / 8)
8923 bytes_est_i
-= vrs
->vrs_pass_bytes_skipped
;
8924 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
8925 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
8926 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
8927 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
8928 double scan_pct
= MIN((double)bytes_scanned
* 100 /
8929 (bytes_est_s
+ 1), 100);
8931 /* Format all of the numbers we will be reporting */
8932 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
8933 char bytes_rebuilt_buf
[7], bytes_est_s_buf
[7], bytes_est_i_buf
[7];
8934 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
8935 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
8936 sizeof (bytes_scanned_buf
));
8937 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
8938 sizeof (bytes_issued_buf
));
8939 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
8940 sizeof (bytes_rebuilt_buf
));
8941 zfs_nicebytes(bytes_est_s
, bytes_est_s_buf
, sizeof (bytes_est_s_buf
));
8942 zfs_nicebytes(bytes_est_i
, bytes_est_i_buf
, sizeof (bytes_est_i_buf
));
8944 time_t start
= vrs
->vrs_start_time
;
8945 time_t end
= vrs
->vrs_end_time
;
8947 /* Rebuild is finished or canceled. */
8948 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
8949 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
8950 (void) printf(gettext("resilvered (%s) %s in %s "
8951 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
8952 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
8954 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
8955 (void) printf(gettext("resilver (%s) canceled on %s"),
8956 vdev_name
, ctime(&end
));
8958 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
8959 (void) printf(gettext("resilver (%s) in progress since %s"),
8960 vdev_name
, ctime(&start
));
8963 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
8965 (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf
,
8967 if (scan_rate
> 0) {
8968 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
8969 (void) printf(gettext(" at %s/s"), scan_rate_buf
);
8971 (void) printf(gettext(", %s / %s issued"), bytes_issued_buf
,
8973 if (issue_rate
> 0) {
8974 zfs_nicebytes(issue_rate
, issue_rate_buf
,
8975 sizeof (issue_rate_buf
));
8976 (void) printf(gettext(" at %s/s"), issue_rate_buf
);
8978 (void) printf(gettext("\n"));
8980 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
8981 bytes_rebuilt_buf
, scan_pct
);
8983 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
8984 if (bytes_est_s
>= bytes_scanned
&&
8985 scan_rate
>= 10 * 1024 * 1024) {
8986 secs_to_dhms((bytes_est_s
- bytes_scanned
) / scan_rate
,
8988 (void) printf(gettext(", %s to go\n"), time_buf
);
8990 (void) printf(gettext(", no estimated "
8991 "completion time\n"));
8994 (void) printf(gettext("\n"));
8999 * Print rebuild status for top-level vdevs.
9002 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
9007 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
9008 &child
, &children
) != 0)
9011 for (uint_t c
= 0; c
< children
; c
++) {
9012 vdev_rebuild_stat_t
*vrs
;
9015 if (nvlist_lookup_uint64_array(child
[c
],
9016 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
9017 char *name
= zpool_vdev_name(g_zfs
, zhp
,
9018 child
[c
], VDEV_NAME_TYPE_ID
);
9019 print_rebuild_status_impl(vrs
, i
, name
);
9026 * As we don't scrub checkpointed blocks, we want to warn the user that we
9027 * skipped scanning some blocks if a checkpoint exists or existed at any
9028 * time during the scan. If a sequential instead of healing reconstruction
9029 * was performed then the blocks were reconstructed. However, their checksums
9030 * have not been verified so we still print the warning.
9033 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
9035 if (ps
== NULL
|| pcs
== NULL
)
9038 if (pcs
->pcs_state
== CS_NONE
||
9039 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
9042 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
9044 if (ps
->pss_state
== DSS_NONE
)
9047 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
9048 ps
->pss_end_time
< pcs
->pcs_start_time
)
9051 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
9052 (void) printf(gettext(" scan warning: skipped blocks "
9053 "that are only referenced by the checkpoint.\n"));
9055 assert(ps
->pss_state
== DSS_SCANNING
);
9056 (void) printf(gettext(" scan warning: skipping blocks "
9057 "that are only referenced by the checkpoint.\n"));
9062 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
9063 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
9064 * the last completed (or cancelled) rebuild.
9067 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
9071 boolean_t rebuilding
= B_FALSE
;
9072 uint64_t end_time
= 0;
9074 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
9075 &child
, &children
) != 0)
9078 for (uint_t c
= 0; c
< children
; c
++) {
9079 vdev_rebuild_stat_t
*vrs
;
9082 if (nvlist_lookup_uint64_array(child
[c
],
9083 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
9085 if (vrs
->vrs_end_time
> end_time
)
9086 end_time
= vrs
->vrs_end_time
;
9088 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
9089 rebuilding
= B_TRUE
;
9096 if (rebuild_end_time
!= NULL
)
9097 *rebuild_end_time
= end_time
;
9099 return (rebuilding
);
9103 vdev_stats_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
9104 int depth
, boolean_t isspare
, char *parent
, nvlist_t
*item
)
9106 nvlist_t
*vds
, **child
, *ch
= NULL
;
9107 uint_t vsc
, children
;
9110 uint64_t notpresent
;
9111 const char *type
, *path
;
9113 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9114 &child
, &children
) != 0)
9116 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
9117 (uint64_t **)&vs
, &vsc
) == 0);
9118 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
9119 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
9122 if (cb
->cb_print_unhealthy
&& depth
> 0 &&
9123 for_each_vdev_in_nvlist(nv
, vdev_health_check_cb
, cb
) == 0) {
9126 vname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9127 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
9128 vds
= fnvlist_alloc();
9129 fill_vdev_info(vds
, zhp
, vname
, B_FALSE
, cb
->cb_json_as_int
);
9130 if (cb
->cb_flat_vdevs
&& parent
!= NULL
) {
9131 fnvlist_add_string(vds
, "parent", parent
);
9135 if (vs
->vs_aux
== VDEV_AUX_SPARED
) {
9136 fnvlist_add_string(vds
, "state", "INUSE");
9137 used_by_other(zhp
, nv
, vds
);
9138 } else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
9139 fnvlist_add_string(vds
, "state", "AVAIL");
9142 nice_num_str_nvlist(vds
, "alloc_space", vs
->vs_alloc
,
9143 cb
->cb_literal
, cb
->cb_json_as_int
,
9147 nice_num_str_nvlist(vds
, "total_space", vs
->vs_space
,
9148 cb
->cb_literal
, cb
->cb_json_as_int
,
9151 if (vs
->vs_dspace
) {
9152 nice_num_str_nvlist(vds
, "def_space", vs
->vs_dspace
,
9153 cb
->cb_literal
, cb
->cb_json_as_int
,
9157 nice_num_str_nvlist(vds
, "rep_dev_size", vs
->vs_rsize
,
9158 cb
->cb_literal
, cb
->cb_json_as_int
,
9162 nice_num_str_nvlist(vds
, "ex_dev_size", vs
->vs_esize
,
9163 cb
->cb_literal
, cb
->cb_json_as_int
,
9166 if (vs
->vs_self_healed
) {
9167 nice_num_str_nvlist(vds
, "self_healed",
9168 vs
->vs_self_healed
, cb
->cb_literal
,
9169 cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9171 if (vs
->vs_pspace
) {
9172 nice_num_str_nvlist(vds
, "phys_space", vs
->vs_pspace
,
9173 cb
->cb_literal
, cb
->cb_json_as_int
,
9176 nice_num_str_nvlist(vds
, "read_errors", vs
->vs_read_errors
,
9177 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9178 nice_num_str_nvlist(vds
, "write_errors", vs
->vs_write_errors
,
9179 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9180 nice_num_str_nvlist(vds
, "checksum_errors",
9181 vs
->vs_checksum_errors
, cb
->cb_literal
,
9182 cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9183 if (vs
->vs_scan_processed
) {
9184 nice_num_str_nvlist(vds
, "scan_processed",
9185 vs
->vs_scan_processed
, cb
->cb_literal
,
9186 cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9188 if (vs
->vs_checkpoint_space
) {
9189 nice_num_str_nvlist(vds
, "checkpoint_space",
9190 vs
->vs_checkpoint_space
, cb
->cb_literal
,
9191 cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9193 if (vs
->vs_resilver_deferred
) {
9194 nice_num_str_nvlist(vds
, "resilver_deferred",
9195 vs
->vs_resilver_deferred
, B_TRUE
,
9196 cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9198 if (children
== 0) {
9199 nice_num_str_nvlist(vds
, "slow_ios", vs
->vs_slow_ios
,
9200 cb
->cb_literal
, cb
->cb_json_as_int
,
9203 if (cb
->cb_print_power
) {
9204 if (children
== 0) {
9205 /* Only leaf vdevs have physical slots */
9206 switch (zpool_power_current_state(zhp
, (char *)
9207 fnvlist_lookup_string(nv
,
9208 ZPOOL_CONFIG_PATH
))) {
9210 fnvlist_add_string(vds
, "power_state",
9214 fnvlist_add_string(vds
, "power_state",
9218 fnvlist_add_string(vds
, "power_state",
9222 fnvlist_add_string(vds
, "power_state", "-");
9227 if (cb
->cb_print_dio_verify
) {
9228 nice_num_str_nvlist(vds
, "dio_verify_errors",
9229 vs
->vs_dio_verify_errors
, cb
->cb_literal
,
9230 cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9233 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
9234 ¬present
) == 0) {
9235 nice_num_str_nvlist(vds
, ZPOOL_CONFIG_NOT_PRESENT
,
9236 1, B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9237 fnvlist_add_string(vds
, "was",
9238 fnvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
));
9239 } else if (vs
->vs_aux
!= VDEV_AUX_NONE
) {
9240 fnvlist_add_string(vds
, "aux", vdev_aux_str
[vs
->vs_aux
]);
9241 } else if (children
== 0 && !isspare
&&
9242 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
9243 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
9244 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
9245 nice_num_str_nvlist(vds
, "configured_ashift",
9246 vs
->vs_configured_ashift
, B_TRUE
, cb
->cb_json_as_int
,
9248 nice_num_str_nvlist(vds
, "physical_ashift",
9249 vs
->vs_physical_ashift
, B_TRUE
, cb
->cb_json_as_int
,
9252 if (vs
->vs_scan_removing
!= 0) {
9253 nice_num_str_nvlist(vds
, "removing", vs
->vs_scan_removing
,
9254 B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9255 } else if (VDEV_STAT_VALID(vs_noalloc
, vsc
) && vs
->vs_noalloc
!= 0) {
9256 nice_num_str_nvlist(vds
, "noalloc", vs
->vs_noalloc
,
9257 B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9260 if (cb
->vcdl
!= NULL
) {
9261 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
9262 zpool_nvlist_cmd(cb
->vcdl
, zpool_get_name(zhp
),
9267 if (children
== 0) {
9268 if (cb
->cb_print_vdev_init
) {
9269 if (vs
->vs_initialize_state
!= 0) {
9270 uint64_t st
= vs
->vs_initialize_state
;
9271 fnvlist_add_string(vds
, "init_state",
9272 vdev_init_state_str
[st
]);
9273 nice_num_str_nvlist(vds
, "initialized",
9274 vs
->vs_initialize_bytes_done
,
9275 cb
->cb_literal
, cb
->cb_json_as_int
,
9277 nice_num_str_nvlist(vds
, "to_initialize",
9278 vs
->vs_initialize_bytes_est
,
9279 cb
->cb_literal
, cb
->cb_json_as_int
,
9281 nice_num_str_nvlist(vds
, "init_time",
9282 vs
->vs_initialize_action_time
,
9283 cb
->cb_literal
, cb
->cb_json_as_int
,
9284 ZFS_NICE_TIMESTAMP
);
9285 nice_num_str_nvlist(vds
, "init_errors",
9286 vs
->vs_initialize_errors
,
9287 cb
->cb_literal
, cb
->cb_json_as_int
,
9290 fnvlist_add_string(vds
, "init_state",
9294 if (cb
->cb_print_vdev_trim
) {
9295 if (vs
->vs_trim_notsup
== 0) {
9296 if (vs
->vs_trim_state
!= 0) {
9297 uint64_t st
= vs
->vs_trim_state
;
9298 fnvlist_add_string(vds
, "trim_state",
9299 vdev_trim_state_str
[st
]);
9300 nice_num_str_nvlist(vds
, "trimmed",
9301 vs
->vs_trim_bytes_done
,
9302 cb
->cb_literal
, cb
->cb_json_as_int
,
9304 nice_num_str_nvlist(vds
, "to_trim",
9305 vs
->vs_trim_bytes_est
,
9306 cb
->cb_literal
, cb
->cb_json_as_int
,
9308 nice_num_str_nvlist(vds
, "trim_time",
9309 vs
->vs_trim_action_time
,
9310 cb
->cb_literal
, cb
->cb_json_as_int
,
9311 ZFS_NICE_TIMESTAMP
);
9312 nice_num_str_nvlist(vds
, "trim_errors",
9314 cb
->cb_literal
, cb
->cb_json_as_int
,
9317 fnvlist_add_string(vds
, "trim_state",
9320 nice_num_str_nvlist(vds
, "trim_notsup",
9321 vs
->vs_trim_notsup
, B_TRUE
,
9322 cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9325 ch
= fnvlist_alloc();
9328 if (cb
->cb_flat_vdevs
&& children
== 0) {
9329 fnvlist_add_nvlist(item
, vname
, vds
);
9332 for (int c
= 0; c
< children
; c
++) {
9333 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
9334 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
9336 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
9338 if (islog
|| ishole
)
9340 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
9342 if (cb
->cb_flat_vdevs
) {
9343 vdev_stats_nvlist(zhp
, cb
, child
[c
], depth
+ 2, isspare
,
9346 vdev_stats_nvlist(zhp
, cb
, child
[c
], depth
+ 2, isspare
,
9351 if (!nvlist_empty(ch
))
9352 fnvlist_add_nvlist(vds
, "vdevs", ch
);
9355 fnvlist_add_nvlist(item
, vname
, vds
);
9361 class_vdevs_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
9362 const char *class, nvlist_t
*item
)
9366 nvlist_t
*class_obj
= NULL
;
9368 if (!cb
->cb_flat_vdevs
)
9369 class_obj
= fnvlist_alloc();
9371 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
9373 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
9377 for (c
= 0; c
< children
; c
++) {
9378 uint64_t is_log
= B_FALSE
;
9379 const char *bias
= NULL
;
9380 const char *type
= NULL
;
9381 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
9382 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
9384 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
9388 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
9390 (void) nvlist_lookup_string(child
[c
],
9391 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
9392 (void) nvlist_lookup_string(child
[c
],
9393 ZPOOL_CONFIG_TYPE
, &type
);
9396 if (bias
== NULL
|| strcmp(bias
, class) != 0)
9398 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
9401 if (cb
->cb_flat_vdevs
) {
9402 vdev_stats_nvlist(zhp
, cb
, child
[c
], 2, B_FALSE
,
9405 vdev_stats_nvlist(zhp
, cb
, child
[c
], 2, B_FALSE
,
9410 if (!cb
->cb_flat_vdevs
) {
9411 if (!nvlist_empty(class_obj
))
9412 fnvlist_add_nvlist(item
, class, class_obj
);
9413 fnvlist_free(class_obj
);
9418 l2cache_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
9421 nvlist_t
*l2c
= NULL
, **l2cache
;
9423 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
9424 &l2cache
, &nl2cache
) == 0) {
9427 if (!cb
->cb_flat_vdevs
)
9428 l2c
= fnvlist_alloc();
9429 for (int i
= 0; i
< nl2cache
; i
++) {
9430 if (cb
->cb_flat_vdevs
) {
9431 vdev_stats_nvlist(zhp
, cb
, l2cache
[i
], 2,
9432 B_FALSE
, NULL
, item
);
9434 vdev_stats_nvlist(zhp
, cb
, l2cache
[i
], 2,
9435 B_FALSE
, NULL
, l2c
);
9439 if (!cb
->cb_flat_vdevs
) {
9440 if (!nvlist_empty(l2c
))
9441 fnvlist_add_nvlist(item
, "l2cache", l2c
);
9447 spares_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
9450 nvlist_t
*sp
= NULL
, **spares
;
9452 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
9453 &spares
, &nspares
) == 0) {
9456 if (!cb
->cb_flat_vdevs
)
9457 sp
= fnvlist_alloc();
9458 for (int i
= 0; i
< nspares
; i
++) {
9459 if (cb
->cb_flat_vdevs
) {
9460 vdev_stats_nvlist(zhp
, cb
, spares
[i
], 2, B_TRUE
,
9463 vdev_stats_nvlist(zhp
, cb
, spares
[i
], 2, B_TRUE
,
9468 if (!cb
->cb_flat_vdevs
) {
9469 if (!nvlist_empty(sp
))
9470 fnvlist_add_nvlist(item
, "spares", sp
);
9476 errors_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*item
)
9479 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
9480 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
9482 nice_num_str_nvlist(item
, ZPOOL_CONFIG_ERRCOUNT
, nerr
,
9483 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9484 if (nerr
!= 0 && cb
->cb_verbose
) {
9485 nvlist_t
*nverrlist
= NULL
;
9486 if (zpool_get_errlog(zhp
, &nverrlist
) == 0) {
9489 size_t len
= MAXPATHLEN
* 2;
9490 nvpair_t
*elem
= NULL
;
9492 for (nvpair_t
*pair
=
9493 nvlist_next_nvpair(nverrlist
, NULL
);
9495 pair
= nvlist_next_nvpair(nverrlist
, pair
))
9497 char **errl
= (char **)malloc(
9498 count
* sizeof (char *));
9500 while ((elem
= nvlist_next_nvpair(nverrlist
,
9503 uint64_t dsobj
, obj
;
9505 verify(nvpair_value_nvlist(elem
,
9507 verify(nvlist_lookup_uint64(nv
,
9508 ZPOOL_ERR_DATASET
, &dsobj
) == 0);
9509 verify(nvlist_lookup_uint64(nv
,
9510 ZPOOL_ERR_OBJECT
, &obj
) == 0);
9511 errl
[i
] = safe_malloc(len
);
9512 zpool_obj_to_path(zhp
, dsobj
, obj
,
9515 nvlist_free(nverrlist
);
9516 fnvlist_add_string_array(item
, "errlist",
9517 (const char **)errl
, count
);
9518 for (int i
= 0; i
< count
; ++i
)
9522 fnvlist_add_string(item
, "errlist",
9529 ddt_stats_nvlist(ddt_stat_t
*dds
, status_cbdata_t
*cb
, nvlist_t
*item
)
9531 nice_num_str_nvlist(item
, "blocks", dds
->dds_blocks
,
9532 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9533 nice_num_str_nvlist(item
, "logical_size", dds
->dds_lsize
,
9534 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9535 nice_num_str_nvlist(item
, "physical_size", dds
->dds_psize
,
9536 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9537 nice_num_str_nvlist(item
, "deflated_size", dds
->dds_dsize
,
9538 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9539 nice_num_str_nvlist(item
, "ref_blocks", dds
->dds_ref_blocks
,
9540 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9541 nice_num_str_nvlist(item
, "ref_lsize", dds
->dds_ref_lsize
,
9542 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9543 nice_num_str_nvlist(item
, "ref_psize", dds
->dds_ref_psize
,
9544 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9545 nice_num_str_nvlist(item
, "ref_dsize", dds
->dds_ref_dsize
,
9546 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9550 dedup_stats_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*item
)
9553 if (cb
->cb_dedup_stats
) {
9554 ddt_histogram_t
*ddh
;
9557 nvlist_t
*ddt_stat
, *ddt_obj
, *dedup
;
9559 uint64_t cspace_prop
;
9561 config
= zpool_get_config(zhp
, NULL
);
9562 if (nvlist_lookup_uint64_array(config
,
9563 ZPOOL_CONFIG_DDT_OBJ_STATS
, (uint64_t **)&ddo
, &c
) != 0)
9566 dedup
= fnvlist_alloc();
9567 ddt_obj
= fnvlist_alloc();
9568 nice_num_str_nvlist(dedup
, "obj_count", ddo
->ddo_count
,
9569 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9570 if (ddo
->ddo_count
== 0) {
9571 fnvlist_add_nvlist(dedup
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
9573 fnvlist_add_nvlist(item
, "dedup_stats", dedup
);
9574 fnvlist_free(ddt_obj
);
9575 fnvlist_free(dedup
);
9578 nice_num_str_nvlist(dedup
, "dspace", ddo
->ddo_dspace
,
9579 cb
->cb_literal
, cb
->cb_json_as_int
,
9581 nice_num_str_nvlist(dedup
, "mspace", ddo
->ddo_mspace
,
9582 cb
->cb_literal
, cb
->cb_json_as_int
,
9585 * Squash cached size into in-core size to handle race.
9586 * Only include cached size if it is available.
9588 cspace_prop
= zpool_get_prop_int(zhp
,
9589 ZPOOL_PROP_DEDUPCACHED
, NULL
);
9590 cspace_prop
= MIN(cspace_prop
, ddo
->ddo_mspace
);
9591 nice_num_str_nvlist(dedup
, "cspace", cspace_prop
,
9592 cb
->cb_literal
, cb
->cb_json_as_int
,
9596 ddt_stat
= fnvlist_alloc();
9597 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
9598 (uint64_t **)&dds
, &c
) == 0) {
9599 nvlist_t
*total
= fnvlist_alloc();
9600 if (dds
->dds_blocks
== 0)
9601 fnvlist_add_string(total
, "blocks", "0");
9603 ddt_stats_nvlist(dds
, cb
, total
);
9604 fnvlist_add_nvlist(ddt_stat
, "total", total
);
9605 fnvlist_free(total
);
9607 if (nvlist_lookup_uint64_array(config
,
9608 ZPOOL_CONFIG_DDT_HISTOGRAM
, (uint64_t **)&ddh
, &c
) == 0) {
9609 nvlist_t
*hist
= fnvlist_alloc();
9610 nvlist_t
*entry
= NULL
;
9612 for (int h
= 0; h
< 64; h
++) {
9613 if (ddh
->ddh_stat
[h
].dds_blocks
!= 0) {
9614 entry
= fnvlist_alloc();
9615 ddt_stats_nvlist(&ddh
->ddh_stat
[h
], cb
,
9617 snprintf(buf
, 16, "%d", h
);
9618 fnvlist_add_nvlist(hist
, buf
, entry
);
9619 fnvlist_free(entry
);
9622 if (!nvlist_empty(hist
))
9623 fnvlist_add_nvlist(ddt_stat
, "histogram", hist
);
9627 if (!nvlist_empty(ddt_obj
)) {
9628 fnvlist_add_nvlist(dedup
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
9631 fnvlist_free(ddt_obj
);
9632 if (!nvlist_empty(ddt_stat
)) {
9633 fnvlist_add_nvlist(dedup
, ZPOOL_CONFIG_DDT_STATS
,
9636 fnvlist_free(ddt_stat
);
9637 if (!nvlist_empty(dedup
))
9638 fnvlist_add_nvlist(item
, "dedup_stats", dedup
);
9639 fnvlist_free(dedup
);
9644 raidz_expand_status_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
,
9645 nvlist_t
*nvroot
, nvlist_t
*item
)
9648 pool_raidz_expand_stat_t
*pres
= NULL
;
9649 if (nvlist_lookup_uint64_array(nvroot
,
9650 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, (uint64_t **)&pres
, &c
) == 0) {
9653 nvlist_t
*nv
= fnvlist_alloc();
9654 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
9655 &child
, &children
) == 0);
9656 assert(pres
->pres_expanding_vdev
< children
);
9658 zpool_vdev_name(g_zfs
, zhp
,
9659 child
[pres
->pres_expanding_vdev
], 0);
9660 fill_vdev_info(nv
, zhp
, name
, B_FALSE
, cb
->cb_json_as_int
);
9661 fnvlist_add_string(nv
, "state",
9662 pool_scan_state_str
[pres
->pres_state
]);
9663 nice_num_str_nvlist(nv
, "expanding_vdev",
9664 pres
->pres_expanding_vdev
, B_TRUE
, cb
->cb_json_as_int
,
9666 nice_num_str_nvlist(nv
, "start_time", pres
->pres_start_time
,
9667 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9668 nice_num_str_nvlist(nv
, "end_time", pres
->pres_end_time
,
9669 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9670 nice_num_str_nvlist(nv
, "to_reflow", pres
->pres_to_reflow
,
9671 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9672 nice_num_str_nvlist(nv
, "reflowed", pres
->pres_reflowed
,
9673 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9674 nice_num_str_nvlist(nv
, "waiting_for_resilver",
9675 pres
->pres_waiting_for_resilver
, B_TRUE
,
9676 cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9677 fnvlist_add_nvlist(item
, ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, nv
);
9684 checkpoint_status_nvlist(nvlist_t
*nvroot
, status_cbdata_t
*cb
,
9688 pool_checkpoint_stat_t
*pcs
= NULL
;
9689 if (nvlist_lookup_uint64_array(nvroot
,
9690 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
) == 0) {
9691 nvlist_t
*nv
= fnvlist_alloc();
9692 fnvlist_add_string(nv
, "state",
9693 checkpoint_state_str
[pcs
->pcs_state
]);
9694 nice_num_str_nvlist(nv
, "start_time",
9695 pcs
->pcs_start_time
, cb
->cb_literal
, cb
->cb_json_as_int
,
9696 ZFS_NICE_TIMESTAMP
);
9697 nice_num_str_nvlist(nv
, "space",
9698 pcs
->pcs_space
, cb
->cb_literal
, cb
->cb_json_as_int
,
9700 fnvlist_add_nvlist(item
, ZPOOL_CONFIG_CHECKPOINT_STATS
, nv
);
9706 removal_status_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
,
9707 nvlist_t
*nvroot
, nvlist_t
*item
)
9710 pool_removal_stat_t
*prs
= NULL
;
9711 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_REMOVAL_STATS
,
9712 (uint64_t **)&prs
, &c
) == 0) {
9713 if (prs
->prs_state
!= DSS_NONE
) {
9716 verify(nvlist_lookup_nvlist_array(nvroot
,
9717 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
9718 assert(prs
->prs_removing_vdev
< children
);
9719 char *vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
9720 child
[prs
->prs_removing_vdev
], B_TRUE
);
9721 nvlist_t
*nv
= fnvlist_alloc();
9722 fill_vdev_info(nv
, zhp
, vdev_name
, B_FALSE
,
9723 cb
->cb_json_as_int
);
9724 fnvlist_add_string(nv
, "state",
9725 pool_scan_state_str
[prs
->prs_state
]);
9726 nice_num_str_nvlist(nv
, "removing_vdev",
9727 prs
->prs_removing_vdev
, B_TRUE
, cb
->cb_json_as_int
,
9729 nice_num_str_nvlist(nv
, "start_time",
9730 prs
->prs_start_time
, cb
->cb_literal
,
9731 cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9732 nice_num_str_nvlist(nv
, "end_time", prs
->prs_end_time
,
9733 cb
->cb_literal
, cb
->cb_json_as_int
,
9734 ZFS_NICE_TIMESTAMP
);
9735 nice_num_str_nvlist(nv
, "to_copy", prs
->prs_to_copy
,
9736 cb
->cb_literal
, cb
->cb_json_as_int
,
9738 nice_num_str_nvlist(nv
, "copied", prs
->prs_copied
,
9739 cb
->cb_literal
, cb
->cb_json_as_int
,
9741 nice_num_str_nvlist(nv
, "mapping_memory",
9742 prs
->prs_mapping_memory
, cb
->cb_literal
,
9743 cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9744 fnvlist_add_nvlist(item
,
9745 ZPOOL_CONFIG_REMOVAL_STATS
, nv
);
9753 scan_status_nvlist(zpool_handle_t
*zhp
, status_cbdata_t
*cb
,
9754 nvlist_t
*nvroot
, nvlist_t
*item
)
9756 pool_scan_stat_t
*ps
= NULL
;
9758 nvlist_t
*scan
= fnvlist_alloc();
9762 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
9763 (uint64_t **)&ps
, &c
) == 0) {
9764 fnvlist_add_string(scan
, "function",
9765 pool_scan_func_str
[ps
->pss_func
]);
9766 fnvlist_add_string(scan
, "state",
9767 pool_scan_state_str
[ps
->pss_state
]);
9768 nice_num_str_nvlist(scan
, "start_time", ps
->pss_start_time
,
9769 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9770 nice_num_str_nvlist(scan
, "end_time", ps
->pss_end_time
,
9771 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9772 nice_num_str_nvlist(scan
, "to_examine", ps
->pss_to_examine
,
9773 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9774 nice_num_str_nvlist(scan
, "examined", ps
->pss_examined
,
9775 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9776 nice_num_str_nvlist(scan
, "skipped", ps
->pss_skipped
,
9777 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9778 nice_num_str_nvlist(scan
, "processed", ps
->pss_processed
,
9779 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9780 nice_num_str_nvlist(scan
, "errors", ps
->pss_errors
,
9781 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9782 nice_num_str_nvlist(scan
, "bytes_per_scan", ps
->pss_pass_exam
,
9783 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9784 nice_num_str_nvlist(scan
, "pass_start", ps
->pss_pass_start
,
9785 B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9786 nice_num_str_nvlist(scan
, "scrub_pause",
9787 ps
->pss_pass_scrub_pause
, cb
->cb_literal
,
9788 cb
->cb_json_as_int
, ZFS_NICE_TIMESTAMP
);
9789 nice_num_str_nvlist(scan
, "scrub_spent_paused",
9790 ps
->pss_pass_scrub_spent_paused
,
9791 B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9792 nice_num_str_nvlist(scan
, "issued_bytes_per_scan",
9793 ps
->pss_pass_issued
, cb
->cb_literal
,
9794 cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9795 nice_num_str_nvlist(scan
, "issued", ps
->pss_issued
,
9796 cb
->cb_literal
, cb
->cb_json_as_int
, ZFS_NICENUM_BYTES
);
9797 if (ps
->pss_error_scrub_func
== POOL_SCAN_ERRORSCRUB
&&
9798 ps
->pss_error_scrub_start
> ps
->pss_start_time
) {
9799 fnvlist_add_string(scan
, "err_scrub_func",
9800 pool_scan_func_str
[ps
->pss_error_scrub_func
]);
9801 fnvlist_add_string(scan
, "err_scrub_state",
9802 pool_scan_state_str
[ps
->pss_error_scrub_state
]);
9803 nice_num_str_nvlist(scan
, "err_scrub_start_time",
9804 ps
->pss_error_scrub_start
,
9805 cb
->cb_literal
, cb
->cb_json_as_int
,
9806 ZFS_NICE_TIMESTAMP
);
9807 nice_num_str_nvlist(scan
, "err_scrub_end_time",
9808 ps
->pss_error_scrub_end
,
9809 cb
->cb_literal
, cb
->cb_json_as_int
,
9810 ZFS_NICE_TIMESTAMP
);
9811 nice_num_str_nvlist(scan
, "err_scrub_examined",
9812 ps
->pss_error_scrub_examined
,
9813 cb
->cb_literal
, cb
->cb_json_as_int
,
9815 nice_num_str_nvlist(scan
, "err_scrub_to_examine",
9816 ps
->pss_error_scrub_to_be_examined
,
9817 cb
->cb_literal
, cb
->cb_json_as_int
,
9819 nice_num_str_nvlist(scan
, "err_scrub_pause",
9820 ps
->pss_pass_error_scrub_pause
,
9821 B_TRUE
, cb
->cb_json_as_int
, ZFS_NICENUM_1024
);
9825 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
9826 &child
, &children
) == 0) {
9827 vdev_rebuild_stat_t
*vrs
;
9831 nvlist_t
*rebuild
= fnvlist_alloc();
9833 for (uint_t c
= 0; c
< children
; c
++) {
9834 if (nvlist_lookup_uint64_array(child
[c
],
9835 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
,
9837 if (vrs
->vrs_state
!= VDEV_REBUILD_NONE
) {
9838 nv
= fnvlist_alloc();
9839 name
= zpool_vdev_name(g_zfs
, zhp
,
9840 child
[c
], VDEV_NAME_TYPE_ID
);
9841 fill_vdev_info(nv
, zhp
, name
, B_FALSE
,
9842 cb
->cb_json_as_int
);
9843 st
= vrs
->vrs_state
;
9844 fnvlist_add_string(nv
, "state",
9845 vdev_rebuild_state_str
[st
]);
9846 nice_num_str_nvlist(nv
, "start_time",
9847 vrs
->vrs_start_time
, cb
->cb_literal
,
9849 ZFS_NICE_TIMESTAMP
);
9850 nice_num_str_nvlist(nv
, "end_time",
9851 vrs
->vrs_end_time
, cb
->cb_literal
,
9853 ZFS_NICE_TIMESTAMP
);
9854 nice_num_str_nvlist(nv
, "scan_time",
9855 vrs
->vrs_scan_time_ms
* 1000000,
9856 cb
->cb_literal
, cb
->cb_json_as_int
,
9858 nice_num_str_nvlist(nv
, "scanned",
9859 vrs
->vrs_bytes_scanned
,
9860 cb
->cb_literal
, cb
->cb_json_as_int
,
9862 nice_num_str_nvlist(nv
, "issued",
9863 vrs
->vrs_bytes_issued
,
9864 cb
->cb_literal
, cb
->cb_json_as_int
,
9866 nice_num_str_nvlist(nv
, "rebuilt",
9867 vrs
->vrs_bytes_rebuilt
,
9868 cb
->cb_literal
, cb
->cb_json_as_int
,
9870 nice_num_str_nvlist(nv
, "to_scan",
9871 vrs
->vrs_bytes_est
, cb
->cb_literal
,
9874 nice_num_str_nvlist(nv
, "errors",
9875 vrs
->vrs_errors
, cb
->cb_literal
,
9878 nice_num_str_nvlist(nv
, "pass_time",
9879 vrs
->vrs_pass_time_ms
* 1000000,
9880 cb
->cb_literal
, cb
->cb_json_as_int
,
9882 nice_num_str_nvlist(nv
, "pass_scanned",
9883 vrs
->vrs_pass_bytes_scanned
,
9884 cb
->cb_literal
, cb
->cb_json_as_int
,
9886 nice_num_str_nvlist(nv
, "pass_issued",
9887 vrs
->vrs_pass_bytes_issued
,
9888 cb
->cb_literal
, cb
->cb_json_as_int
,
9890 nice_num_str_nvlist(nv
, "pass_skipped",
9891 vrs
->vrs_pass_bytes_skipped
,
9892 cb
->cb_literal
, cb
->cb_json_as_int
,
9894 fnvlist_add_nvlist(rebuild
, name
, nv
);
9899 if (!nvlist_empty(rebuild
))
9900 fnvlist_add_nvlist(scan
, "rebuild_stats", rebuild
);
9901 fnvlist_free(rebuild
);
9904 if (!nvlist_empty(scan
))
9905 fnvlist_add_nvlist(item
, ZPOOL_CONFIG_SCAN_STATS
, scan
);
9910 * Print the scan status.
9913 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
9915 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
9916 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
9917 boolean_t have_errorscrub
= B_FALSE
;
9918 boolean_t active_resilver
= B_FALSE
;
9919 pool_checkpoint_stat_t
*pcs
= NULL
;
9920 pool_scan_stat_t
*ps
= NULL
;
9922 time_t scrub_start
= 0, errorscrub_start
= 0;
9924 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
9925 (uint64_t **)&ps
, &c
) == 0) {
9926 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
9927 resilver_end_time
= ps
->pss_end_time
;
9928 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
9931 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
9932 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
9933 scrub_start
= ps
->pss_start_time
;
9934 if (c
> offsetof(pool_scan_stat_t
,
9935 pss_pass_error_scrub_pause
) / 8) {
9936 have_errorscrub
= (ps
->pss_error_scrub_func
==
9937 POOL_SCAN_ERRORSCRUB
);
9938 errorscrub_start
= ps
->pss_error_scrub_start
;
9942 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
9943 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
9945 /* Always print the scrub status when available. */
9946 if (have_scrub
&& scrub_start
> errorscrub_start
)
9947 print_scan_scrub_resilver_status(ps
);
9948 else if (have_errorscrub
&& errorscrub_start
>= scrub_start
)
9949 print_err_scrub_status(ps
);
9952 * When there is an active resilver or rebuild print its status.
9953 * Otherwise print the status of the last resilver or rebuild.
9955 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
9956 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
9957 print_scan_scrub_resilver_status(ps
);
9958 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
9959 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
9960 print_rebuild_status(zhp
, nvroot
);
9963 (void) nvlist_lookup_uint64_array(nvroot
,
9964 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
9965 print_checkpoint_scan_warning(ps
, pcs
);
9969 * Print out detailed removal status.
9972 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
9974 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
9976 nvlist_t
*config
, *nvroot
;
9981 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
9985 * Determine name of vdev.
9987 config
= zpool_get_config(zhp
, NULL
);
9988 nvroot
= fnvlist_lookup_nvlist(config
,
9989 ZPOOL_CONFIG_VDEV_TREE
);
9990 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
9991 &child
, &children
) == 0);
9992 assert(prs
->prs_removing_vdev
< children
);
9993 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
9994 child
[prs
->prs_removing_vdev
], B_TRUE
);
9996 printf_color(ANSI_BOLD
, gettext("remove: "));
9998 start
= prs
->prs_start_time
;
9999 end
= prs
->prs_end_time
;
10000 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
10003 * Removal is finished or canceled.
10005 if (prs
->prs_state
== DSS_FINISHED
) {
10006 uint64_t minutes_taken
= (end
- start
) / 60;
10008 (void) printf(gettext("Removal of vdev %llu copied %s "
10009 "in %lluh%um, completed on %s"),
10010 (longlong_t
)prs
->prs_removing_vdev
,
10012 (u_longlong_t
)(minutes_taken
/ 60),
10013 (uint_t
)(minutes_taken
% 60),
10014 ctime((time_t *)&end
));
10015 } else if (prs
->prs_state
== DSS_CANCELED
) {
10016 (void) printf(gettext("Removal of %s canceled on %s"),
10017 vdev_name
, ctime(&end
));
10019 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
10020 double fraction_done
;
10023 assert(prs
->prs_state
== DSS_SCANNING
);
10026 * Removal is in progress.
10028 (void) printf(gettext(
10029 "Evacuation of %s in progress since %s"),
10030 vdev_name
, ctime(&start
));
10032 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
10033 total
= prs
->prs_to_copy
;
10034 fraction_done
= (double)copied
/ total
;
10036 /* elapsed time for this pass */
10037 elapsed
= time(NULL
) - prs
->prs_start_time
;
10038 elapsed
= elapsed
> 0 ? elapsed
: 1;
10039 rate
= copied
/ elapsed
;
10040 rate
= rate
> 0 ? rate
: 1;
10041 mins_left
= ((total
- copied
) / rate
) / 60;
10042 hours_left
= mins_left
/ 60;
10044 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
10045 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
10046 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
10049 * do not print estimated time if hours_left is more than
10052 (void) printf(gettext(
10053 "\t%s copied out of %s at %s/s, %.2f%% done"),
10054 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
10055 if (hours_left
< (30 * 24)) {
10056 (void) printf(gettext(", %lluh%um to go\n"),
10057 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
10059 (void) printf(gettext(
10060 ", (copy is slow, no estimated time)\n"));
10065 if (prs
->prs_mapping_memory
> 0) {
10067 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
10068 (void) printf(gettext(
10069 "\t%s memory used for removed device mappings\n"),
10075 * Print out detailed raidz expansion status.
10078 print_raidz_expand_status(zpool_handle_t
*zhp
, pool_raidz_expand_stat_t
*pres
)
10080 char copied_buf
[7];
10082 if (pres
== NULL
|| pres
->pres_state
== DSS_NONE
)
10086 * Determine name of vdev.
10088 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
10089 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
10090 ZPOOL_CONFIG_VDEV_TREE
);
10093 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
10094 &child
, &children
) == 0);
10095 assert(pres
->pres_expanding_vdev
< children
);
10097 printf_color(ANSI_BOLD
, gettext("expand: "));
10099 time_t start
= pres
->pres_start_time
;
10100 time_t end
= pres
->pres_end_time
;
10102 zpool_vdev_name(g_zfs
, zhp
, child
[pres
->pres_expanding_vdev
], 0);
10103 zfs_nicenum(pres
->pres_reflowed
, copied_buf
, sizeof (copied_buf
));
10106 * Expansion is finished or canceled.
10108 if (pres
->pres_state
== DSS_FINISHED
) {
10110 secs_to_dhms(end
- start
, time_buf
);
10112 (void) printf(gettext("expanded %s-%u copied %s in %s, "
10113 "on %s"), vname
, (int)pres
->pres_expanding_vdev
,
10114 copied_buf
, time_buf
, ctime((time_t *)&end
));
10116 char examined_buf
[7], total_buf
[7], rate_buf
[7];
10117 uint64_t copied
, total
, elapsed
, secs_left
;
10118 double fraction_done
;
10121 assert(pres
->pres_state
== DSS_SCANNING
);
10124 * Expansion is in progress.
10126 (void) printf(gettext(
10127 "expansion of %s-%u in progress since %s"),
10128 vname
, (int)pres
->pres_expanding_vdev
, ctime(&start
));
10130 copied
= pres
->pres_reflowed
> 0 ? pres
->pres_reflowed
: 1;
10131 total
= pres
->pres_to_reflow
;
10132 fraction_done
= (double)copied
/ total
;
10134 /* elapsed time for this pass */
10135 elapsed
= time(NULL
) - pres
->pres_start_time
;
10136 elapsed
= elapsed
> 0 ? elapsed
: 1;
10137 rate
= copied
/ elapsed
;
10138 rate
= rate
> 0 ? rate
: 1;
10139 secs_left
= (total
- copied
) / rate
;
10141 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
10142 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
10143 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
10146 * do not print estimated time if hours_left is more than
10149 (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"),
10150 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
10151 if (pres
->pres_waiting_for_resilver
) {
10152 (void) printf(gettext(", paused for resilver or "
10154 } else if (secs_left
< (30 * 24 * 3600)) {
10156 secs_to_dhms(secs_left
, time_buf
);
10157 (void) printf(gettext(", %s to go\n"), time_buf
);
10159 (void) printf(gettext(
10160 ", (copy is slow, no estimated time)\n"));
10166 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
10171 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
10174 (void) printf(gettext("checkpoint: "));
10176 start
= pcs
->pcs_start_time
;
10177 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
10179 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
10180 char *date
= ctime(&start
);
10183 * ctime() adds a newline at the end of the generated
10184 * string, thus the weird format specifier and the
10185 * strlen() call used to chop it off from the output.
10187 (void) printf(gettext("created %.*s, consumes %s\n"),
10188 (int)(strlen(date
) - 1), date
, space_buf
);
10192 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
10194 (void) printf(gettext("discarding, %s remaining.\n"),
10199 print_error_log(zpool_handle_t
*zhp
)
10201 nvlist_t
*nverrlist
= NULL
;
10204 size_t len
= MAXPATHLEN
* 2;
10206 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
10209 (void) printf("errors: Permanent errors have been "
10210 "detected in the following files:\n\n");
10212 pathname
= safe_malloc(len
);
10214 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
10216 uint64_t dsobj
, obj
;
10218 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
10219 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
10221 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
10223 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
10224 (void) printf("%7s %s\n", "", pathname
);
10227 nvlist_free(nverrlist
);
10231 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
10240 (void) printf(gettext("\tspares\n"));
10242 for (i
= 0; i
< nspares
; i
++) {
10243 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
10244 cb
->cb_name_flags
);
10245 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
10251 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
10260 (void) printf(gettext("\tcache\n"));
10262 for (i
= 0; i
< nl2cache
; i
++) {
10263 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
10264 cb
->cb_name_flags
);
10265 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
10272 print_dedup_stats(zpool_handle_t
*zhp
, nvlist_t
*config
, boolean_t literal
)
10274 ddt_histogram_t
*ddh
;
10278 /* Extra space provided for literal display */
10279 char dspace
[32], mspace
[32], cspace
[32];
10280 uint64_t cspace_prop
;
10281 enum zfs_nicenum_format format
;
10282 zprop_source_t src
;
10285 * If the pool was faulted then we may not have been able to
10286 * obtain the config. Otherwise, if we have anything in the dedup
10287 * table continue processing the stats.
10289 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
10290 (uint64_t **)&ddo
, &c
) != 0)
10293 (void) printf("\n");
10294 (void) printf(gettext(" dedup: "));
10295 if (ddo
->ddo_count
== 0) {
10296 (void) printf(gettext("no DDT entries\n"));
10301 * Squash cached size into in-core size to handle race.
10302 * Only include cached size if it is available.
10304 cspace_prop
= zpool_get_prop_int(zhp
, ZPOOL_PROP_DEDUPCACHED
, &src
);
10305 cspace_prop
= MIN(cspace_prop
, ddo
->ddo_mspace
);
10306 format
= literal
? ZFS_NICENUM_RAW
: ZFS_NICENUM_1024
;
10307 zfs_nicenum_format(cspace_prop
, cspace
, sizeof (cspace
), format
);
10308 zfs_nicenum_format(ddo
->ddo_dspace
, dspace
, sizeof (dspace
), format
);
10309 zfs_nicenum_format(ddo
->ddo_mspace
, mspace
, sizeof (mspace
), format
);
10310 (void) printf("DDT entries %llu, size %s on disk, %s in core",
10311 (u_longlong_t
)ddo
->ddo_count
,
10314 if (src
!= ZPROP_SRC_DEFAULT
) {
10315 (void) printf(", %s cached (%.02f%%)",
10317 (double)cspace_prop
/ (double)ddo
->ddo_mspace
* 100.0);
10319 (void) printf("\n");
10321 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
10322 (uint64_t **)&dds
, &c
) == 0);
10323 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
10324 (uint64_t **)&ddh
, &c
) == 0);
10325 zpool_dump_ddt(dds
, ddh
);
10328 #define ST_SIZE 4096
10329 #define AC_SIZE 2048
10332 print_status_reason(zpool_handle_t
*zhp
, status_cbdata_t
*cbp
,
10333 zpool_status_t reason
, zpool_errata_t errata
, nvlist_t
*item
)
10335 char status
[ST_SIZE
];
10336 char action
[AC_SIZE
];
10337 memset(status
, 0, ST_SIZE
);
10338 memset(action
, 0, AC_SIZE
);
10341 case ZPOOL_STATUS_MISSING_DEV_R
:
10342 snprintf(status
, ST_SIZE
, gettext("One or more devices could "
10343 "not be opened. Sufficient replicas exist for\n\tthe pool "
10344 "to continue functioning in a degraded state.\n"));
10345 snprintf(action
, AC_SIZE
, gettext("Attach the missing device "
10346 "and online it using 'zpool online'.\n"));
10349 case ZPOOL_STATUS_MISSING_DEV_NR
:
10350 snprintf(status
, ST_SIZE
, gettext("One or more devices could "
10351 "not be opened. There are insufficient\n\treplicas for the"
10352 " pool to continue functioning.\n"));
10353 snprintf(action
, AC_SIZE
, gettext("Attach the missing device "
10354 "and online it using 'zpool online'.\n"));
10357 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
10358 snprintf(status
, ST_SIZE
, gettext("One or more devices could "
10359 "not be used because the label is missing or\n\tinvalid. "
10360 "Sufficient replicas exist for the pool to continue\n\t"
10361 "functioning in a degraded state.\n"));
10362 snprintf(action
, AC_SIZE
, gettext("Replace the device using "
10363 "'zpool replace'.\n"));
10366 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
10367 snprintf(status
, ST_SIZE
, gettext("One or more devices could "
10368 "not be used because the label is missing \n\tor invalid. "
10369 "There are insufficient replicas for the pool to "
10370 "continue\n\tfunctioning.\n"));
10371 zpool_explain_recover(zpool_get_handle(zhp
),
10372 zpool_get_name(zhp
), reason
, zpool_get_config(zhp
, NULL
),
10376 case ZPOOL_STATUS_FAILING_DEV
:
10377 snprintf(status
, ST_SIZE
, gettext("One or more devices has "
10378 "experienced an unrecoverable error. An\n\tattempt was "
10379 "made to correct the error. Applications are "
10381 snprintf(action
, AC_SIZE
, gettext("Determine if the "
10382 "device needs to be replaced, and clear the errors\n\tusing"
10383 " 'zpool clear' or replace the device with 'zpool "
10387 case ZPOOL_STATUS_OFFLINE_DEV
:
10388 snprintf(status
, ST_SIZE
, gettext("One or more devices has "
10389 "been taken offline by the administrator.\n\tSufficient "
10390 "replicas exist for the pool to continue functioning in "
10391 "a\n\tdegraded state.\n"));
10392 snprintf(action
, AC_SIZE
, gettext("Online the device "
10393 "using 'zpool online' or replace the device with\n\t'zpool "
10397 case ZPOOL_STATUS_REMOVED_DEV
:
10398 snprintf(status
, ST_SIZE
, gettext("One or more devices has "
10399 "been removed by the administrator.\n\tSufficient "
10400 "replicas exist for the pool to continue functioning in "
10401 "a\n\tdegraded state.\n"));
10402 snprintf(action
, AC_SIZE
, gettext("Online the device "
10403 "using zpool online' or replace the device with\n\t'zpool "
10407 case ZPOOL_STATUS_RESILVERING
:
10408 case ZPOOL_STATUS_REBUILDING
:
10409 snprintf(status
, ST_SIZE
, gettext("One or more devices is "
10410 "currently being resilvered. The pool will\n\tcontinue "
10411 "to function, possibly in a degraded state.\n"));
10412 snprintf(action
, AC_SIZE
, gettext("Wait for the resilver to "
10416 case ZPOOL_STATUS_REBUILD_SCRUB
:
10417 snprintf(status
, ST_SIZE
, gettext("One or more devices have "
10418 "been sequentially resilvered, scrubbing\n\tthe pool "
10419 "is recommended.\n"));
10420 snprintf(action
, AC_SIZE
, gettext("Use 'zpool scrub' to "
10421 "verify all data checksums.\n"));
10424 case ZPOOL_STATUS_CORRUPT_DATA
:
10425 snprintf(status
, ST_SIZE
, gettext("One or more devices has "
10426 "experienced an error resulting in data\n\tcorruption. "
10427 "Applications may be affected.\n"));
10428 snprintf(action
, AC_SIZE
, gettext("Restore the file in question"
10429 " if possible. Otherwise restore the\n\tentire pool from "
10433 case ZPOOL_STATUS_CORRUPT_POOL
:
10434 snprintf(status
, ST_SIZE
, gettext("The pool metadata is "
10435 "corrupted and the pool cannot be opened.\n"));
10436 zpool_explain_recover(zpool_get_handle(zhp
),
10437 zpool_get_name(zhp
), reason
, zpool_get_config(zhp
, NULL
),
10441 case ZPOOL_STATUS_VERSION_OLDER
:
10442 snprintf(status
, ST_SIZE
, gettext("The pool is formatted using "
10443 "a legacy on-disk format. The pool can\n\tstill be used, "
10444 "but some features are unavailable.\n"));
10445 snprintf(action
, AC_SIZE
, gettext("Upgrade the pool using "
10446 "'zpool upgrade'. Once this is done, the\n\tpool will no "
10447 "longer be accessible on software that does not support\n\t"
10448 "feature flags.\n"));
10451 case ZPOOL_STATUS_VERSION_NEWER
:
10452 snprintf(status
, ST_SIZE
, gettext("The pool has been upgraded "
10453 "to a newer, incompatible on-disk version.\n\tThe pool "
10454 "cannot be accessed on this system.\n"));
10455 snprintf(action
, AC_SIZE
, gettext("Access the pool from a "
10456 "system running more recent software, or\n\trestore the "
10457 "pool from backup.\n"));
10460 case ZPOOL_STATUS_FEAT_DISABLED
:
10461 snprintf(status
, ST_SIZE
, gettext("Some supported and "
10462 "requested features are not enabled on the pool.\n\t"
10463 "The pool can still be used, but some features are "
10464 "unavailable.\n"));
10465 snprintf(action
, AC_SIZE
, gettext("Enable all features using "
10466 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
10467 "longer be accessible by software that does not support\n\t"
10468 "the features. See zpool-features(7) for details.\n"));
10471 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
10472 snprintf(status
, ST_SIZE
, gettext("This pool has a "
10473 "compatibility list specified, but it could not be\n\t"
10474 "read/parsed at this time. The pool can still be used, "
10475 "but this\n\tshould be investigated.\n"));
10476 snprintf(action
, AC_SIZE
, gettext("Check the value of the "
10477 "'compatibility' property against the\n\t"
10478 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
10479 ZPOOL_DATA_COMPAT_D
".\n"));
10482 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
10483 snprintf(status
, ST_SIZE
, gettext("One or more features "
10484 "are enabled on the pool despite not being\n\t"
10485 "requested by the 'compatibility' property.\n"));
10486 snprintf(action
, AC_SIZE
, gettext("Consider setting "
10487 "'compatibility' to an appropriate value, or\n\t"
10488 "adding needed features to the relevant file in\n\t"
10489 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
10492 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
10493 snprintf(status
, ST_SIZE
, gettext("The pool cannot be accessed "
10494 "on this system because it uses the\n\tfollowing feature(s)"
10495 " not supported on this system:\n"));
10496 zpool_collect_unsup_feat(zpool_get_config(zhp
, NULL
), status
,
10498 snprintf(action
, AC_SIZE
, gettext("Access the pool from a "
10499 "system that supports the required feature(s),\n\tor "
10500 "restore the pool from backup.\n"));
10503 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
10504 snprintf(status
, ST_SIZE
, gettext("The pool can only be "
10505 "accessed in read-only mode on this system. It\n\tcannot be"
10506 " accessed in read-write mode because it uses the "
10507 "following\n\tfeature(s) not supported on this system:\n"));
10508 zpool_collect_unsup_feat(zpool_get_config(zhp
, NULL
), status
,
10510 snprintf(action
, AC_SIZE
, gettext("The pool cannot be accessed "
10511 "in read-write mode. Import the pool with\n"
10512 "\t\"-o readonly=on\", access the pool from a system that "
10513 "supports the\n\trequired feature(s), or restore the "
10514 "pool from backup.\n"));
10517 case ZPOOL_STATUS_FAULTED_DEV_R
:
10518 snprintf(status
, ST_SIZE
, gettext("One or more devices are "
10519 "faulted in response to persistent errors.\n\tSufficient "
10520 "replicas exist for the pool to continue functioning "
10521 "in a\n\tdegraded state.\n"));
10522 snprintf(action
, AC_SIZE
, gettext("Replace the faulted device, "
10523 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
10526 case ZPOOL_STATUS_FAULTED_DEV_NR
:
10527 snprintf(status
, ST_SIZE
, gettext("One or more devices are "
10528 "faulted in response to persistent errors. There are "
10529 "insufficient replicas for the pool to\n\tcontinue "
10530 "functioning.\n"));
10531 snprintf(action
, AC_SIZE
, gettext("Destroy and re-create the "
10532 "pool from a backup source. Manually marking the device\n"
10533 "\trepaired using 'zpool clear' may allow some data "
10534 "to be recovered.\n"));
10537 case ZPOOL_STATUS_IO_FAILURE_MMP
:
10538 snprintf(status
, ST_SIZE
, gettext("The pool is suspended "
10539 "because multihost writes failed or were delayed;\n\t"
10540 "another system could import the pool undetected.\n"));
10541 snprintf(action
, AC_SIZE
, gettext("Make sure the pool's devices"
10542 " are connected, then reboot your system and\n\timport the "
10543 "pool or run 'zpool clear' to resume the pool.\n"));
10546 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
10547 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
10548 snprintf(status
, ST_SIZE
, gettext("One or more devices are "
10549 "faulted in response to IO failures.\n"));
10550 snprintf(action
, AC_SIZE
, gettext("Make sure the affected "
10551 "devices are connected, then run 'zpool clear'.\n"));
10554 case ZPOOL_STATUS_BAD_LOG
:
10555 snprintf(status
, ST_SIZE
, gettext("An intent log record "
10556 "could not be read.\n"
10557 "\tWaiting for administrator intervention to fix the "
10558 "faulted pool.\n"));
10559 snprintf(action
, AC_SIZE
, gettext("Either restore the affected "
10560 "device(s) and run 'zpool online',\n"
10561 "\tor ignore the intent log records by running "
10562 "'zpool clear'.\n"));
10565 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
10566 snprintf(status
, ST_SIZE
, gettext("One or more devices are "
10567 "configured to use a non-native block size.\n"
10568 "\tExpect reduced performance.\n"));
10569 snprintf(action
, AC_SIZE
, gettext("Replace affected devices "
10570 "with devices that support the\n\tconfigured block size, "
10571 "or migrate data to a properly configured\n\tpool.\n"));
10574 case ZPOOL_STATUS_HOSTID_MISMATCH
:
10575 snprintf(status
, ST_SIZE
, gettext("Mismatch between pool hostid"
10576 " and system hostid on imported pool.\n\tThis pool was "
10577 "previously imported into a system with a different "
10578 "hostid,\n\tand then was verbatim imported into this "
10580 snprintf(action
, AC_SIZE
, gettext("Export this pool on all "
10581 "systems on which it is imported.\n"
10582 "\tThen import it to correct the mismatch.\n"));
10585 case ZPOOL_STATUS_ERRATA
:
10586 snprintf(status
, ST_SIZE
, gettext("Errata #%d detected.\n"),
10589 case ZPOOL_ERRATA_NONE
:
10592 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
10593 snprintf(action
, AC_SIZE
, gettext("To correct the issue"
10594 " run 'zpool scrub'.\n"));
10597 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
10598 (void) strlcat(status
, gettext("\tExisting encrypted "
10599 "datasets contain an on-disk incompatibility\n\t "
10600 "which needs to be corrected.\n"), ST_SIZE
);
10601 snprintf(action
, AC_SIZE
, gettext("To correct the issue"
10602 " backup existing encrypted datasets to new\n\t"
10603 "encrypted datasets and destroy the old ones. "
10604 "'zfs mount -o ro' can\n\tbe used to temporarily "
10605 "mount existing encrypted datasets readonly.\n"));
10608 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
10609 (void) strlcat(status
, gettext("\tExisting encrypted "
10610 "snapshots and bookmarks contain an on-disk\n\t"
10611 "incompatibility. This may cause on-disk "
10612 "corruption if they are used\n\twith "
10613 "'zfs recv'.\n"), ST_SIZE
);
10614 snprintf(action
, AC_SIZE
, gettext("To correct the"
10615 "issue, enable the bookmark_v2 feature. No "
10616 "additional\n\taction is needed if there are no "
10617 "encrypted snapshots or bookmarks.\n\tIf preserving"
10618 "the encrypted snapshots and bookmarks is required,"
10619 " use\n\ta non-raw send to backup and restore them."
10620 " Alternately, they may be\n\tremoved to resolve "
10621 "the incompatibility.\n"));
10626 * All errata which allow the pool to be imported
10627 * must contain an action message.
10635 * The remaining errors can't actually be generated, yet.
10637 assert(reason
== ZPOOL_STATUS_OK
);
10640 if (status
[0] != 0) {
10642 fnvlist_add_string(item
, "status", status
);
10644 printf_color(ANSI_BOLD
, gettext("status: "));
10645 printf_color(ANSI_YELLOW
, status
);
10649 if (action
[0] != 0) {
10651 fnvlist_add_string(item
, "action", action
);
10653 printf_color(ANSI_BOLD
, gettext("action: "));
10654 printf_color(ANSI_YELLOW
, action
);
10660 status_callback_json(zpool_handle_t
*zhp
, void *data
)
10662 status_cbdata_t
*cbp
= data
;
10663 nvlist_t
*config
, *nvroot
;
10665 char pool_guid
[256];
10668 zpool_status_t reason
;
10669 zpool_errata_t errata
;
10672 nvlist_t
*item
, *d
, *load_info
, *vds
;
10675 /* If dedup stats were requested, also fetch dedupcached. */
10676 if (cbp
->cb_dedup_stats
> 1)
10677 zpool_add_propname(zhp
, ZPOOL_DEDUPCACHED_PROP_NAME
);
10678 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
10680 * If we were given 'zpool status -x', only report those pools with
10683 if (cbp
->cb_explain
&&
10684 (reason
== ZPOOL_STATUS_OK
||
10685 reason
== ZPOOL_STATUS_VERSION_OLDER
||
10686 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
10687 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
10688 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
10692 d
= fnvlist_lookup_nvlist(cbp
->cb_jsobj
, "pools");
10693 item
= fnvlist_alloc();
10694 vds
= fnvlist_alloc();
10695 fill_pool_info(item
, zhp
, B_FALSE
, cbp
->cb_json_as_int
);
10696 config
= zpool_get_config(zhp
, NULL
);
10698 if (config
!= NULL
) {
10699 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10700 verify(nvlist_lookup_uint64_array(nvroot
,
10701 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &c
) == 0);
10702 if (cbp
->cb_json_pool_key_guid
) {
10703 guid
= fnvlist_lookup_uint64(config
,
10704 ZPOOL_CONFIG_POOL_GUID
);
10705 snprintf(pool_guid
, 256, "%llu", (u_longlong_t
)guid
);
10709 print_status_reason(zhp
, cbp
, reason
, errata
, item
);
10710 if (msgid
!= NULL
) {
10711 snprintf(msgbuf
, 256,
10712 "https://openzfs.github.io/openzfs-docs/msg/%s",
10714 fnvlist_add_string(item
, "msgid", msgid
);
10715 fnvlist_add_string(item
, "moreinfo", msgbuf
);
10718 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
10719 &load_info
) == 0) {
10720 fnvlist_add_nvlist(item
, ZPOOL_CONFIG_LOAD_INFO
,
10724 scan_status_nvlist(zhp
, cbp
, nvroot
, item
);
10725 removal_status_nvlist(zhp
, cbp
, nvroot
, item
);
10726 checkpoint_status_nvlist(nvroot
, cbp
, item
);
10727 raidz_expand_status_nvlist(zhp
, cbp
, nvroot
, item
);
10728 vdev_stats_nvlist(zhp
, cbp
, nvroot
, 0, B_FALSE
, NULL
, vds
);
10729 if (cbp
->cb_flat_vdevs
) {
10730 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10731 VDEV_ALLOC_BIAS_DEDUP
, vds
);
10732 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10733 VDEV_ALLOC_BIAS_SPECIAL
, vds
);
10734 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10735 VDEV_ALLOC_CLASS_LOGS
, vds
);
10736 l2cache_nvlist(zhp
, cbp
, nvroot
, vds
);
10737 spares_nvlist(zhp
, cbp
, nvroot
, vds
);
10739 fnvlist_add_nvlist(item
, "vdevs", vds
);
10742 fnvlist_add_nvlist(item
, "vdevs", vds
);
10745 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10746 VDEV_ALLOC_BIAS_DEDUP
, item
);
10747 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10748 VDEV_ALLOC_BIAS_SPECIAL
, item
);
10749 class_vdevs_nvlist(zhp
, cbp
, nvroot
,
10750 VDEV_ALLOC_CLASS_LOGS
, item
);
10751 l2cache_nvlist(zhp
, cbp
, nvroot
, item
);
10752 spares_nvlist(zhp
, cbp
, nvroot
, item
);
10754 dedup_stats_nvlist(zhp
, cbp
, item
);
10755 errors_nvlist(zhp
, cbp
, item
);
10757 if (cbp
->cb_json_pool_key_guid
) {
10758 fnvlist_add_nvlist(d
, pool_guid
, item
);
10760 fnvlist_add_nvlist(d
, zpool_get_name(zhp
),
10763 fnvlist_free(item
);
10768 * Display a summary of pool status. Displays a summary such as:
10772 * reason: One or more devices ...
10773 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
10779 * When given the '-v' option, we print out the complete config. If the '-e'
10780 * option is specified, then we print out error rate information as well.
10783 status_callback(zpool_handle_t
*zhp
, void *data
)
10785 status_cbdata_t
*cbp
= data
;
10786 nvlist_t
*config
, *nvroot
;
10788 zpool_status_t reason
;
10789 zpool_errata_t errata
;
10790 const char *health
;
10794 /* If dedup stats were requested, also fetch dedupcached. */
10795 if (cbp
->cb_dedup_stats
> 1)
10796 zpool_add_propname(zhp
, ZPOOL_DEDUPCACHED_PROP_NAME
);
10798 config
= zpool_get_config(zhp
, NULL
);
10799 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
10804 * If we were given 'zpool status -x', only report those pools with
10807 if (cbp
->cb_explain
&&
10808 (reason
== ZPOOL_STATUS_OK
||
10809 reason
== ZPOOL_STATUS_VERSION_OLDER
||
10810 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
10811 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
10812 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
10813 if (!cbp
->cb_allpools
) {
10814 (void) printf(gettext("pool '%s' is healthy\n"),
10815 zpool_get_name(zhp
));
10817 cbp
->cb_first
= B_FALSE
;
10823 cbp
->cb_first
= B_FALSE
;
10825 (void) printf("\n");
10827 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10828 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
10829 (uint64_t **)&vs
, &c
) == 0);
10831 health
= zpool_get_state_str(zhp
);
10834 printf_color(ANSI_BOLD
, gettext("pool:"));
10835 printf(" %s\n", zpool_get_name(zhp
));
10836 fputc(' ', stdout
);
10837 printf_color(ANSI_BOLD
, gettext("state: "));
10839 printf_color(health_str_to_color(health
), "%s", health
);
10841 fputc('\n', stdout
);
10842 print_status_reason(zhp
, cbp
, reason
, errata
, NULL
);
10844 if (msgid
!= NULL
) {
10846 printf_color(ANSI_BOLD
, gettext("see:"));
10848 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
10852 if (config
!= NULL
) {
10854 nvlist_t
**spares
, **l2cache
;
10855 uint_t nspares
, nl2cache
;
10857 print_scan_status(zhp
, nvroot
);
10859 pool_removal_stat_t
*prs
= NULL
;
10860 (void) nvlist_lookup_uint64_array(nvroot
,
10861 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10862 print_removal_status(zhp
, prs
);
10864 pool_checkpoint_stat_t
*pcs
= NULL
;
10865 (void) nvlist_lookup_uint64_array(nvroot
,
10866 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10867 print_checkpoint_status(pcs
);
10869 pool_raidz_expand_stat_t
*pres
= NULL
;
10870 (void) nvlist_lookup_uint64_array(nvroot
,
10871 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, (uint64_t **)&pres
, &c
);
10872 print_raidz_expand_status(zhp
, pres
);
10874 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
10875 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
10876 if (cbp
->cb_namewidth
< 10)
10877 cbp
->cb_namewidth
= 10;
10879 color_start(ANSI_BOLD
);
10880 (void) printf(gettext("config:\n\n"));
10881 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
10882 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
10886 if (cbp
->cb_print_slow_ios
) {
10887 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
10890 if (cbp
->cb_print_power
) {
10891 printf_color(ANSI_BOLD
, " %5s", gettext("POWER"));
10894 if (cbp
->cb_print_dio_verify
) {
10895 printf_color(ANSI_BOLD
, " %5s", gettext("DIO"));
10898 if (cbp
->vcdl
!= NULL
)
10899 print_cmd_columns(cbp
->vcdl
, 0);
10903 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
10906 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
10907 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
10908 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
10910 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
10911 &l2cache
, &nl2cache
) == 0)
10912 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
10914 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
10915 &spares
, &nspares
) == 0)
10916 print_spares(zhp
, cbp
, spares
, nspares
);
10918 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
10920 (void) printf("\n");
10922 (void) printf(gettext(
10923 "errors: No known data errors\n"));
10924 } else if (!cbp
->cb_verbose
) {
10925 color_start(ANSI_RED
);
10926 (void) printf(gettext("errors: %llu data "
10927 "errors, use '-v' for a list\n"),
10928 (u_longlong_t
)nerr
);
10931 print_error_log(zhp
);
10935 if (cbp
->cb_dedup_stats
)
10936 print_dedup_stats(zhp
, config
, cbp
->cb_literal
);
10938 (void) printf(gettext("config: The configuration cannot be "
10946 * zpool status [-c [script1,script2,...]] [-dDegiLpPstvx] [--power] ...
10947 * [-T d|u] [pool] [interval [count]]
10949 * -c CMD For each vdev, run command CMD
10950 * -d Display Direct I/O write verify errors
10951 * -D Display dedup status (undocumented)
10952 * -e Display only unhealthy vdevs
10953 * -g Display guid for individual vdev name.
10954 * -i Display vdev initialization status.
10955 * -L Follow links when resolving vdev path name.
10956 * -p Display values in parsable (exact) format.
10957 * -P Display full path for vdev name.
10958 * -s Display slow IOs column.
10959 * -t Display vdev TRIM status.
10960 * -T Display a timestamp in date(1) or Unix format
10961 * -v Display complete error logs
10962 * -x Display only pools with potential problems
10963 * -j Display output in JSON format
10964 * --power Display vdev enclosure slot power status
10965 * --json-int Display numbers in inteeger format instead of string
10966 * --json-flat-vdevs Display vdevs in flat hierarchy
10967 * --json-pool-key-guid Use pool GUID as key for pool objects
10969 * Describes the health status of all pools or some subset.
10972 zpool_do_status(int argc
, char **argv
)
10976 float interval
= 0;
10977 unsigned long count
= 0;
10978 status_cbdata_t cb
= { 0 };
10982 struct option long_options
[] = {
10983 {"power", no_argument
, NULL
, ZPOOL_OPTION_POWER
},
10984 {"json-int", no_argument
, NULL
, ZPOOL_OPTION_JSON_NUMS_AS_INT
},
10985 {"json-flat-vdevs", no_argument
, NULL
,
10986 ZPOOL_OPTION_JSON_FLAT_VDEVS
},
10987 {"json-pool-key-guid", no_argument
, NULL
,
10988 ZPOOL_OPTION_POOL_KEY_GUID
},
10992 /* check options */
10993 while ((c
= getopt_long(argc
, argv
, "c:jdDegiLpPstT:vx", long_options
,
10999 gettext("Can't set -c flag twice\n"));
11003 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
11004 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
11005 fprintf(stderr
, gettext(
11006 "Can't run -c, disabled by "
11007 "ZPOOL_SCRIPTS_ENABLED.\n"));
11011 if ((getuid() <= 0 || geteuid() <= 0) &&
11012 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
11013 fprintf(stderr
, gettext(
11014 "Can't run -c with root privileges "
11015 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
11021 cb
.cb_print_dio_verify
= B_TRUE
;
11024 if (++cb
.cb_dedup_stats
> 2)
11025 cb
.cb_dedup_stats
= 2;
11028 cb
.cb_print_unhealthy
= B_TRUE
;
11031 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
11034 cb
.cb_print_vdev_init
= B_TRUE
;
11037 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
11040 cb
.cb_literal
= B_TRUE
;
11043 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
11046 cb
.cb_print_slow_ios
= B_TRUE
;
11049 cb
.cb_print_vdev_trim
= B_TRUE
;
11052 get_timestamp_arg(*optarg
);
11055 cb
.cb_verbose
= B_TRUE
;
11058 cb
.cb_json
= B_TRUE
;
11061 cb
.cb_explain
= B_TRUE
;
11063 case ZPOOL_OPTION_POWER
:
11064 cb
.cb_print_power
= B_TRUE
;
11066 case ZPOOL_OPTION_JSON_FLAT_VDEVS
:
11067 cb
.cb_flat_vdevs
= B_TRUE
;
11069 case ZPOOL_OPTION_JSON_NUMS_AS_INT
:
11070 cb
.cb_json_as_int
= B_TRUE
;
11071 cb
.cb_literal
= B_TRUE
;
11073 case ZPOOL_OPTION_POOL_KEY_GUID
:
11074 cb
.cb_json_pool_key_guid
= B_TRUE
;
11077 if (optopt
== 'c') {
11078 print_zpool_script_list("status");
11082 gettext("invalid option '%c'\n"), optopt
);
11091 get_interval_count(&argc
, argv
, &interval
, &count
);
11094 cb
.cb_allpools
= B_TRUE
;
11096 cb
.cb_first
= B_TRUE
;
11097 cb
.cb_print_status
= B_TRUE
;
11099 if (cb
.cb_flat_vdevs
&& !cb
.cb_json
) {
11100 fprintf(stderr
, gettext("'--json-flat-vdevs' only works with"
11101 " '-j' option\n"));
11105 if (cb
.cb_json_as_int
&& !cb
.cb_json
) {
11106 (void) fprintf(stderr
, gettext("'--json-int' only works with"
11107 " '-j' option\n"));
11111 if (!cb
.cb_json
&& cb
.cb_json_pool_key_guid
) {
11112 (void) fprintf(stderr
, gettext("'json-pool-key-guid' only"
11113 " works with '-j' option\n"));
11119 cb
.cb_jsobj
= zpool_json_schema(0, 1);
11120 data
= fnvlist_alloc();
11121 fnvlist_add_nvlist(cb
.cb_jsobj
, "pools", data
);
11122 fnvlist_free(data
);
11125 if (timestamp_fmt
!= NODATE
) {
11127 if (cb
.cb_json_as_int
) {
11128 fnvlist_add_uint64(cb
.cb_jsobj
, "time",
11132 get_timestamp(timestamp_fmt
, ts
, 128);
11133 fnvlist_add_string(cb
.cb_jsobj
, "time",
11137 print_timestamp(timestamp_fmt
);
11141 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
11145 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
11146 ZFS_TYPE_POOL
, cb
.cb_literal
,
11147 status_callback_json
, &cb
);
11149 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
11150 ZFS_TYPE_POOL
, cb
.cb_literal
,
11151 status_callback
, &cb
);
11154 if (cb
.vcdl
!= NULL
)
11155 free_vdev_cmd_data_list(cb
.vcdl
);
11159 zcmd_print_json(cb
.cb_jsobj
);
11161 nvlist_free(cb
.cb_jsobj
);
11163 if (argc
== 0 && cb
.cb_count
== 0) {
11164 (void) fprintf(stderr
, "%s",
11165 gettext("no pools available\n"));
11166 } else if (cb
.cb_explain
&& cb
.cb_first
&&
11168 (void) printf("%s",
11169 gettext("all pools are healthy\n"));
11179 if (count
!= 0 && --count
== 0)
11182 (void) fflush(stdout
);
11183 (void) fsleep(interval
);
11189 typedef struct upgrade_cbdata
{
11192 uint64_t cb_version
;
11194 } upgrade_cbdata_t
;
11197 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
11199 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
11200 int *count
= (int *)unsupp_fs
;
11202 if (zfs_version
> ZPL_VERSION
) {
11203 (void) printf(gettext("%s (v%d) is not supported by this "
11204 "implementation of ZFS.\n"),
11205 zfs_get_name(zhp
), zfs_version
);
11209 zfs_iter_filesystems_v2(zhp
, 0, check_unsupp_fs
, unsupp_fs
);
11217 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
11221 uint64_t oldversion
;
11224 config
= zpool_get_config(zhp
, NULL
);
11225 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
11226 &oldversion
) == 0);
11228 char compat
[ZFS_MAXPROPLEN
];
11229 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
11230 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
11233 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
11234 assert(oldversion
< version
);
11236 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
11241 (void) fprintf(stderr
, gettext("Upgrade not performed due "
11242 "to %d unsupported filesystems (max v%d).\n"),
11243 unsupp_fs
, (int)ZPL_VERSION
);
11247 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
11248 (void) fprintf(stderr
, gettext("Upgrade not performed because "
11249 "'compatibility' property set to '"
11250 ZPOOL_COMPAT_LEGACY
"'.\n"));
11254 ret
= zpool_upgrade(zhp
, version
);
11258 if (version
>= SPA_VERSION_FEATURES
) {
11259 (void) printf(gettext("Successfully upgraded "
11260 "'%s' from version %llu to feature flags.\n"),
11261 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
11263 (void) printf(gettext("Successfully upgraded "
11264 "'%s' from version %llu to version %llu.\n"),
11265 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
11266 (u_longlong_t
)version
);
11273 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
11276 boolean_t firstff
= B_TRUE
;
11277 nvlist_t
*enabled
= zpool_get_features(zhp
);
11279 char compat
[ZFS_MAXPROPLEN
];
11280 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
11281 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
11284 boolean_t requested_features
[SPA_FEATURES
];
11285 if (zpool_do_load_compat(compat
, requested_features
) !=
11286 ZPOOL_COMPATIBILITY_OK
)
11290 for (i
= 0; i
< SPA_FEATURES
; i
++) {
11291 const char *fname
= spa_feature_table
[i
].fi_uname
;
11292 const char *fguid
= spa_feature_table
[i
].fi_guid
;
11294 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
11297 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
11299 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
11300 ret
= zpool_set_prop(zhp
, propname
,
11301 ZFS_FEATURE_ENABLED
);
11309 (void) printf(gettext("Enabled the "
11310 "following features on '%s':\n"),
11311 zpool_get_name(zhp
));
11314 (void) printf(gettext(" %s\n"), fname
);
11319 if (countp
!= NULL
)
11325 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
11327 upgrade_cbdata_t
*cbp
= arg
;
11330 boolean_t modified_pool
= B_FALSE
;
11333 config
= zpool_get_config(zhp
, NULL
);
11334 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
11337 assert(SPA_VERSION_IS_SUPPORTED(version
));
11339 if (version
< cbp
->cb_version
) {
11340 cbp
->cb_first
= B_FALSE
;
11341 ret
= upgrade_version(zhp
, cbp
->cb_version
);
11344 modified_pool
= B_TRUE
;
11347 * If they did "zpool upgrade -a", then we could
11348 * be doing ioctls to different pools. We need
11349 * to log this history once to each pool, and bypass
11350 * the normal history logging that happens in main().
11352 (void) zpool_log_history(g_zfs
, history_str
);
11353 log_history
= B_FALSE
;
11356 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
11358 ret
= upgrade_enable_all(zhp
, &count
);
11363 cbp
->cb_first
= B_FALSE
;
11364 modified_pool
= B_TRUE
;
11368 if (modified_pool
) {
11369 (void) printf("\n");
11370 (void) after_zpool_upgrade(zhp
);
11377 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
11379 upgrade_cbdata_t
*cbp
= arg
;
11383 config
= zpool_get_config(zhp
, NULL
);
11384 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
11387 assert(SPA_VERSION_IS_SUPPORTED(version
));
11389 if (version
< SPA_VERSION_FEATURES
) {
11390 if (cbp
->cb_first
) {
11391 (void) printf(gettext("The following pools are "
11392 "formatted with legacy version numbers and can\n"
11393 "be upgraded to use feature flags. After "
11394 "being upgraded, these pools\nwill no "
11395 "longer be accessible by software that does not "
11396 "support feature\nflags.\n\n"
11397 "Note that setting a pool's 'compatibility' "
11398 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
11399 "inhibit upgrades.\n\n"));
11400 (void) printf(gettext("VER POOL\n"));
11401 (void) printf(gettext("--- ------------\n"));
11402 cbp
->cb_first
= B_FALSE
;
11405 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
11406 zpool_get_name(zhp
));
11413 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
11415 upgrade_cbdata_t
*cbp
= arg
;
11419 config
= zpool_get_config(zhp
, NULL
);
11420 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
11423 if (version
>= SPA_VERSION_FEATURES
) {
11425 boolean_t poolfirst
= B_TRUE
;
11426 nvlist_t
*enabled
= zpool_get_features(zhp
);
11428 for (i
= 0; i
< SPA_FEATURES
; i
++) {
11429 const char *fguid
= spa_feature_table
[i
].fi_guid
;
11430 const char *fname
= spa_feature_table
[i
].fi_uname
;
11432 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
11435 if (!nvlist_exists(enabled
, fguid
)) {
11436 if (cbp
->cb_first
) {
11437 (void) printf(gettext("\nSome "
11438 "supported features are not "
11439 "enabled on the following pools. "
11440 "Once a\nfeature is enabled the "
11441 "pool may become incompatible with "
11442 "software\nthat does not support "
11443 "the feature. See "
11444 "zpool-features(7) for "
11446 "Note that the pool "
11447 "'compatibility' feature can be "
11448 "used to inhibit\nfeature "
11450 (void) printf(gettext("POOL "
11452 (void) printf(gettext("------"
11454 cbp
->cb_first
= B_FALSE
;
11458 (void) printf(gettext("%s\n"),
11459 zpool_get_name(zhp
));
11460 poolfirst
= B_FALSE
;
11463 (void) printf(gettext(" %s\n"), fname
);
11466 * If they did "zpool upgrade -a", then we could
11467 * be doing ioctls to different pools. We need
11468 * to log this history once to each pool, and bypass
11469 * the normal history logging that happens in main().
11471 (void) zpool_log_history(g_zfs
, history_str
);
11472 log_history
= B_FALSE
;
11480 upgrade_one(zpool_handle_t
*zhp
, void *data
)
11482 boolean_t modified_pool
= B_FALSE
;
11483 upgrade_cbdata_t
*cbp
= data
;
11484 uint64_t cur_version
;
11487 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
11488 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
11489 "Pool 'log' must be renamed using export and import"
11490 " to upgrade.\n"));
11494 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
11495 if (cur_version
> cbp
->cb_version
) {
11496 (void) printf(gettext("Pool '%s' is already formatted "
11497 "using more current version '%llu'.\n\n"),
11498 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
11502 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
11503 (void) printf(gettext("Pool '%s' is already formatted "
11504 "using version %llu.\n\n"), zpool_get_name(zhp
),
11505 (u_longlong_t
)cbp
->cb_version
);
11509 if (cur_version
!= cbp
->cb_version
) {
11510 modified_pool
= B_TRUE
;
11511 ret
= upgrade_version(zhp
, cbp
->cb_version
);
11516 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
11518 ret
= upgrade_enable_all(zhp
, &count
);
11523 modified_pool
= B_TRUE
;
11524 } else if (cur_version
== SPA_VERSION
) {
11525 (void) printf(gettext("Pool '%s' already has all "
11526 "supported and requested features enabled.\n"),
11527 zpool_get_name(zhp
));
11531 if (modified_pool
) {
11532 (void) printf("\n");
11533 (void) after_zpool_upgrade(zhp
);
11542 * zpool upgrade [-V version] <-a | pool ...>
11544 * With no arguments, display downrev'd ZFS pool available for upgrade.
11545 * Individual pools can be upgraded by specifying the pool, and '-a' will
11546 * upgrade all pools.
11549 zpool_do_upgrade(int argc
, char **argv
)
11552 upgrade_cbdata_t cb
= { 0 };
11554 boolean_t showversions
= B_FALSE
;
11555 boolean_t upgradeall
= B_FALSE
;
11559 /* check options */
11560 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
11563 upgradeall
= B_TRUE
;
11566 showversions
= B_TRUE
;
11569 cb
.cb_version
= strtoll(optarg
, &end
, 10);
11570 if (*end
!= '\0' ||
11571 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
11572 (void) fprintf(stderr
,
11573 gettext("invalid version '%s'\n"), optarg
);
11578 (void) fprintf(stderr
, gettext("missing argument for "
11579 "'%c' option\n"), optopt
);
11583 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
11594 if (cb
.cb_version
== 0) {
11595 cb
.cb_version
= SPA_VERSION
;
11596 } else if (!upgradeall
&& argc
== 0) {
11597 (void) fprintf(stderr
, gettext("-V option is "
11598 "incompatible with other arguments\n"));
11602 if (showversions
) {
11603 if (upgradeall
|| argc
!= 0) {
11604 (void) fprintf(stderr
, gettext("-v option is "
11605 "incompatible with other arguments\n"));
11608 } else if (upgradeall
) {
11610 (void) fprintf(stderr
, gettext("-a option should not "
11611 "be used along with a pool name\n"));
11616 (void) printf("%s", gettext("This system supports ZFS pool feature "
11618 if (showversions
) {
11621 (void) printf(gettext("The following features are "
11622 "supported:\n\n"));
11623 (void) printf(gettext("FEAT DESCRIPTION\n"));
11624 (void) printf("----------------------------------------------"
11625 "---------------\n");
11626 for (i
= 0; i
< SPA_FEATURES
; i
++) {
11627 zfeature_info_t
*fi
= &spa_feature_table
[i
];
11628 if (!fi
->fi_zfs_mod_supported
)
11631 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
11632 " (read-only compatible)" : "";
11634 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
11635 (void) printf(" %s\n", fi
->fi_desc
);
11637 (void) printf("\n");
11639 (void) printf(gettext("The following legacy versions are also "
11640 "supported:\n\n"));
11641 (void) printf(gettext("VER DESCRIPTION\n"));
11642 (void) printf("--- -----------------------------------------"
11643 "---------------\n");
11644 (void) printf(gettext(" 1 Initial ZFS version\n"));
11645 (void) printf(gettext(" 2 Ditto blocks "
11646 "(replicated metadata)\n"));
11647 (void) printf(gettext(" 3 Hot spares and double parity "
11649 (void) printf(gettext(" 4 zpool history\n"));
11650 (void) printf(gettext(" 5 Compression using the gzip "
11652 (void) printf(gettext(" 6 bootfs pool property\n"));
11653 (void) printf(gettext(" 7 Separate intent log devices\n"));
11654 (void) printf(gettext(" 8 Delegated administration\n"));
11655 (void) printf(gettext(" 9 refquota and refreservation "
11657 (void) printf(gettext(" 10 Cache devices\n"));
11658 (void) printf(gettext(" 11 Improved scrub performance\n"));
11659 (void) printf(gettext(" 12 Snapshot properties\n"));
11660 (void) printf(gettext(" 13 snapused property\n"));
11661 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
11662 (void) printf(gettext(" 15 user/group space accounting\n"));
11663 (void) printf(gettext(" 16 stmf property support\n"));
11664 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
11665 (void) printf(gettext(" 18 Snapshot user holds\n"));
11666 (void) printf(gettext(" 19 Log device removal\n"));
11667 (void) printf(gettext(" 20 Compression using zle "
11668 "(zero-length encoding)\n"));
11669 (void) printf(gettext(" 21 Deduplication\n"));
11670 (void) printf(gettext(" 22 Received properties\n"));
11671 (void) printf(gettext(" 23 Slim ZIL\n"));
11672 (void) printf(gettext(" 24 System attributes\n"));
11673 (void) printf(gettext(" 25 Improved scrub stats\n"));
11674 (void) printf(gettext(" 26 Improved snapshot deletion "
11676 (void) printf(gettext(" 27 Improved snapshot creation "
11678 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
11679 (void) printf(gettext("\nFor more information on a particular "
11680 "version, including supported releases,\n"));
11681 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
11682 } else if (argc
== 0 && upgradeall
) {
11683 cb
.cb_first
= B_TRUE
;
11684 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
11685 if (ret
== 0 && cb
.cb_first
) {
11686 if (cb
.cb_version
== SPA_VERSION
) {
11687 (void) printf(gettext("All pools are already "
11688 "formatted using feature flags.\n\n"));
11689 (void) printf(gettext("Every feature flags "
11690 "pool already has all supported and "
11691 "requested features enabled.\n"));
11693 (void) printf(gettext("All pools are already "
11694 "formatted with version %llu or higher.\n"),
11695 (u_longlong_t
)cb
.cb_version
);
11698 } else if (argc
== 0) {
11699 cb
.cb_first
= B_TRUE
;
11700 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
11704 (void) printf(gettext("All pools are formatted "
11705 "using feature flags.\n\n"));
11707 (void) printf(gettext("\nUse 'zpool upgrade -v' "
11708 "for a list of available legacy versions.\n"));
11711 cb
.cb_first
= B_TRUE
;
11712 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
11716 (void) printf(gettext("Every feature flags pool has "
11717 "all supported and requested features enabled.\n"));
11719 (void) printf(gettext("\n"));
11722 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
11723 B_FALSE
, upgrade_one
, &cb
);
11729 typedef struct hist_cbdata
{
11732 boolean_t internal
;
11736 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
11738 nvlist_t
**records
;
11742 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
11743 &records
, &numrecords
) == 0);
11744 for (i
= 0; i
< numrecords
; i
++) {
11745 nvlist_t
*rec
= records
[i
];
11746 char tbuf
[64] = "";
11748 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
11752 tsec
= fnvlist_lookup_uint64(records
[i
],
11754 (void) localtime_r(&tsec
, &t
);
11755 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
11758 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
11759 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
11760 ZPOOL_HIST_ELAPSED_NS
);
11761 (void) snprintf(tbuf
+ strlen(tbuf
),
11762 sizeof (tbuf
) - strlen(tbuf
),
11763 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
11766 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
11767 (void) printf("%s %s", tbuf
,
11768 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
11769 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
11771 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
11774 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
11775 (void) printf("%s unrecognized record:\n",
11777 dump_nvlist(rec
, 4);
11780 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
11781 zfs_history_event_names
[ievent
],
11782 (longlong_t
)fnvlist_lookup_uint64(
11783 rec
, ZPOOL_HIST_TXG
),
11784 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
11785 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
11788 (void) printf("%s [txg:%lld] %s", tbuf
,
11789 (longlong_t
)fnvlist_lookup_uint64(
11790 rec
, ZPOOL_HIST_TXG
),
11791 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
11792 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
11793 (void) printf(" %s (%llu)",
11794 fnvlist_lookup_string(rec
,
11795 ZPOOL_HIST_DSNAME
),
11796 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
11799 (void) printf(" %s", fnvlist_lookup_string(rec
,
11800 ZPOOL_HIST_INT_STR
));
11801 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
11804 (void) printf("%s ioctl %s\n", tbuf
,
11805 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
11806 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
11807 (void) printf(" input:\n");
11808 dump_nvlist(fnvlist_lookup_nvlist(rec
,
11809 ZPOOL_HIST_INPUT_NVL
), 8);
11811 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
11812 (void) printf(" output:\n");
11813 dump_nvlist(fnvlist_lookup_nvlist(rec
,
11814 ZPOOL_HIST_OUTPUT_NVL
), 8);
11816 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
11817 (void) printf(" output nvlist omitted; "
11818 "original size: %lldKB\n",
11819 (longlong_t
)fnvlist_lookup_int64(rec
,
11820 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
11822 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
11823 (void) printf(" errno: %lld\n",
11824 (longlong_t
)fnvlist_lookup_int64(rec
,
11825 ZPOOL_HIST_ERRNO
));
11830 (void) printf("%s unrecognized record:\n", tbuf
);
11831 dump_nvlist(rec
, 4);
11834 if (!cb
->longfmt
) {
11835 (void) printf("\n");
11838 (void) printf(" [");
11839 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
11840 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
11841 struct passwd
*pwd
= getpwuid(who
);
11842 (void) printf("user %d ", (int)who
);
11844 (void) printf("(%s) ", pwd
->pw_name
);
11846 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
11847 (void) printf("on %s",
11848 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
11850 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
11851 (void) printf(":%s",
11852 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
11855 (void) printf("]");
11856 (void) printf("\n");
11861 * Print out the command history for a specific pool.
11864 get_history_one(zpool_handle_t
*zhp
, void *data
)
11868 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
11870 boolean_t eof
= B_FALSE
;
11872 cb
->first
= B_FALSE
;
11874 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
11877 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
11880 print_history_records(nvhis
, cb
);
11881 nvlist_free(nvhis
);
11883 (void) printf("\n");
11889 * zpool history <pool>
11891 * Displays the history of commands that modified pools.
11894 zpool_do_history(int argc
, char **argv
)
11896 hist_cbdata_t cbdata
= { 0 };
11900 cbdata
.first
= B_TRUE
;
11901 /* check options */
11902 while ((c
= getopt(argc
, argv
, "li")) != -1) {
11905 cbdata
.longfmt
= B_TRUE
;
11908 cbdata
.internal
= B_TRUE
;
11911 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
11919 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
11920 B_FALSE
, get_history_one
, &cbdata
);
11922 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
11923 (void) fprintf(stderr
, gettext("no pools available\n"));
11930 typedef struct ev_opts
{
11935 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
11939 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
11941 char ctime_str
[26], str
[32];
11946 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
11947 memset(str
, ' ', 32);
11948 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
11949 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
11950 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
11951 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
11952 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
11953 if (opts
->scripted
)
11954 (void) printf(gettext("%s\t"), str
);
11956 (void) printf(gettext("%s "), str
);
11958 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
11959 (void) printf(gettext("%s\n"), ptr
);
11963 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
11966 static char flagstr
[256];
11968 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
11969 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
11971 data_type_t type
= nvpair_type(nvp
);
11972 const char *name
= nvpair_name(nvp
);
11982 printf(gettext("%*s%s = "), depth
, "", name
);
11985 case DATA_TYPE_BOOLEAN
:
11986 printf(gettext("%s"), "1");
11989 case DATA_TYPE_BOOLEAN_VALUE
:
11990 (void) nvpair_value_boolean_value(nvp
, &b
);
11991 printf(gettext("%s"), b
? "1" : "0");
11994 case DATA_TYPE_BYTE
:
11995 (void) nvpair_value_byte(nvp
, &i8
);
11996 printf(gettext("0x%x"), i8
);
11999 case DATA_TYPE_INT8
:
12000 (void) nvpair_value_int8(nvp
, (void *)&i8
);
12001 printf(gettext("0x%x"), i8
);
12004 case DATA_TYPE_UINT8
:
12005 (void) nvpair_value_uint8(nvp
, &i8
);
12006 printf(gettext("0x%x"), i8
);
12009 case DATA_TYPE_INT16
:
12010 (void) nvpair_value_int16(nvp
, (void *)&i16
);
12011 printf(gettext("0x%x"), i16
);
12014 case DATA_TYPE_UINT16
:
12015 (void) nvpair_value_uint16(nvp
, &i16
);
12016 printf(gettext("0x%x"), i16
);
12019 case DATA_TYPE_INT32
:
12020 (void) nvpair_value_int32(nvp
, (void *)&i32
);
12021 printf(gettext("0x%x"), i32
);
12024 case DATA_TYPE_UINT32
:
12025 (void) nvpair_value_uint32(nvp
, &i32
);
12027 FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE
) == 0 ||
12029 FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE
) == 0) {
12030 zfs_valstr_zio_stage(i32
, flagstr
,
12032 printf(gettext("0x%x [%s]"), i32
, flagstr
);
12033 } else if (strcmp(name
,
12034 FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY
) == 0) {
12035 zfs_valstr_zio_priority(i32
, flagstr
,
12037 printf(gettext("0x%x [%s]"), i32
, flagstr
);
12039 printf(gettext("0x%x"), i32
);
12043 case DATA_TYPE_INT64
:
12044 (void) nvpair_value_int64(nvp
, (void *)&i64
);
12045 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
12048 case DATA_TYPE_UINT64
:
12049 (void) nvpair_value_uint64(nvp
, &i64
);
12051 * translate vdev state values to readable
12052 * strings to aide zpool events consumers
12055 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
12057 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
12058 printf(gettext("\"%s\" (0x%llx)"),
12059 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
12060 (u_longlong_t
)i64
);
12061 } else if (strcmp(name
,
12062 FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS
) == 0) {
12063 zfs_valstr_zio_flag(i64
, flagstr
,
12065 printf(gettext("0x%llx [%s]"),
12066 (u_longlong_t
)i64
, flagstr
);
12068 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
12072 case DATA_TYPE_HRTIME
:
12073 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
12074 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
12077 case DATA_TYPE_STRING
:
12078 (void) nvpair_value_string(nvp
, &str
);
12079 printf(gettext("\"%s\""), str
? str
: "<NULL>");
12082 case DATA_TYPE_NVLIST
:
12083 printf(gettext("(embedded nvlist)\n"));
12084 (void) nvpair_value_nvlist(nvp
, &cnv
);
12085 zpool_do_events_nvprint(cnv
, depth
+ 8);
12086 printf(gettext("%*s(end %s)"), depth
, "", name
);
12089 case DATA_TYPE_NVLIST_ARRAY
: {
12093 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
12094 printf(gettext("(%d embedded nvlists)\n"), nelem
);
12095 for (i
= 0; i
< nelem
; i
++) {
12096 printf(gettext("%*s%s[%d] = %s\n"),
12097 depth
, "", name
, i
, "(embedded nvlist)");
12098 zpool_do_events_nvprint(val
[i
], depth
+ 8);
12099 printf(gettext("%*s(end %s[%i])\n"),
12100 depth
, "", name
, i
);
12102 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
12106 case DATA_TYPE_INT8_ARRAY
: {
12110 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
12111 for (i
= 0; i
< nelem
; i
++)
12112 printf(gettext("0x%x "), val
[i
]);
12117 case DATA_TYPE_UINT8_ARRAY
: {
12121 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
12122 for (i
= 0; i
< nelem
; i
++)
12123 printf(gettext("0x%x "), val
[i
]);
12128 case DATA_TYPE_INT16_ARRAY
: {
12132 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
12133 for (i
= 0; i
< nelem
; i
++)
12134 printf(gettext("0x%x "), val
[i
]);
12139 case DATA_TYPE_UINT16_ARRAY
: {
12143 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
12144 for (i
= 0; i
< nelem
; i
++)
12145 printf(gettext("0x%x "), val
[i
]);
12150 case DATA_TYPE_INT32_ARRAY
: {
12154 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
12155 for (i
= 0; i
< nelem
; i
++)
12156 printf(gettext("0x%x "), val
[i
]);
12161 case DATA_TYPE_UINT32_ARRAY
: {
12165 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
12166 for (i
= 0; i
< nelem
; i
++)
12167 printf(gettext("0x%x "), val
[i
]);
12172 case DATA_TYPE_INT64_ARRAY
: {
12176 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
12177 for (i
= 0; i
< nelem
; i
++)
12178 printf(gettext("0x%llx "),
12179 (u_longlong_t
)val
[i
]);
12184 case DATA_TYPE_UINT64_ARRAY
: {
12188 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
12189 for (i
= 0; i
< nelem
; i
++)
12190 printf(gettext("0x%llx "),
12191 (u_longlong_t
)val
[i
]);
12196 case DATA_TYPE_STRING_ARRAY
: {
12200 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
12201 for (i
= 0; i
< nelem
; i
++)
12202 printf(gettext("\"%s\" "),
12203 str
[i
] ? str
[i
] : "<NULL>");
12208 case DATA_TYPE_BOOLEAN_ARRAY
:
12209 case DATA_TYPE_BYTE_ARRAY
:
12210 case DATA_TYPE_DOUBLE
:
12211 case DATA_TYPE_DONTCARE
:
12212 case DATA_TYPE_UNKNOWN
:
12213 printf(gettext("<unknown>"));
12217 printf(gettext("\n"));
12222 zpool_do_events_next(ev_opts_t
*opts
)
12225 int zevent_fd
, ret
, dropped
;
12228 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
12229 VERIFY(zevent_fd
>= 0);
12231 if (!opts
->scripted
)
12232 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
12235 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
12236 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
12237 if (ret
|| nvl
== NULL
)
12241 (void) printf(gettext("dropped %d events\n"), dropped
);
12243 if (strlen(opts
->poolname
) > 0 &&
12244 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
12245 strcmp(opts
->poolname
, pool
) != 0)
12248 zpool_do_events_short(nvl
, opts
);
12250 if (opts
->verbose
) {
12251 zpool_do_events_nvprint(nvl
, 8);
12252 printf(gettext("\n"));
12254 (void) fflush(stdout
);
12259 VERIFY(0 == close(zevent_fd
));
12265 zpool_do_events_clear(void)
12269 ret
= zpool_events_clear(g_zfs
, &count
);
12271 (void) printf(gettext("cleared %d events\n"), count
);
12277 * zpool events [-vHf [pool] | -c]
12279 * Displays events logs by ZFS.
12282 zpool_do_events(int argc
, char **argv
)
12284 ev_opts_t opts
= { 0 };
12288 /* check options */
12289 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
12304 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
12313 (void) fprintf(stderr
, gettext("too many arguments\n"));
12315 } else if (argc
== 1) {
12316 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
12317 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
12318 (void) fprintf(stderr
,
12319 gettext("invalid pool name '%s'\n"), opts
.poolname
);
12324 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
12326 (void) fprintf(stderr
,
12327 gettext("invalid options combined with -c\n"));
12332 ret
= zpool_do_events_clear();
12334 ret
= zpool_do_events_next(&opts
);
12340 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
12342 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
12343 char value
[ZFS_MAXPROPLEN
];
12344 zprop_source_t srctype
;
12345 nvlist_t
*props
, *item
, *d
;
12346 props
= item
= d
= NULL
;
12348 if (cbp
->cb_json
) {
12349 d
= fnvlist_lookup_nvlist(cbp
->cb_jsobj
, "vdevs");
12351 fprintf(stderr
, "vdevs obj not found.\n");
12354 props
= fnvlist_alloc();
12357 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
12358 pl
= pl
->pl_next
) {
12361 * If the first property is pool name, it is a special
12362 * placeholder that we can skip. This will also skip
12363 * over the name property when 'all' is specified.
12365 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
12366 pl
== cbp
->cb_proplist
)
12369 if (pl
->pl_prop
== ZPROP_INVAL
) {
12370 prop_name
= pl
->pl_user_prop
;
12372 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
12374 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
12375 prop_name
, value
, sizeof (value
), &srctype
,
12376 cbp
->cb_literal
) == 0) {
12377 zprop_collect_property(vdevname
, cbp
, prop_name
,
12378 value
, srctype
, NULL
, NULL
, props
);
12382 if (cbp
->cb_json
) {
12383 if (!nvlist_empty(props
)) {
12384 item
= fnvlist_alloc();
12385 fill_vdev_info(item
, zhp
, vdevname
, B_TRUE
,
12386 cbp
->cb_json_as_int
);
12387 fnvlist_add_nvlist(item
, "properties", props
);
12388 fnvlist_add_nvlist(d
, vdevname
, item
);
12389 fnvlist_add_nvlist(cbp
->cb_jsobj
, "vdevs", d
);
12390 fnvlist_free(item
);
12392 fnvlist_free(props
);
12399 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
12401 zpool_handle_t
*zhp
= zhp_data
;
12402 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
12408 * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the
12409 * pool name for display purposes, which is not desired. Fallback to
12410 * zpool_vdev_name() when not dealing with the root vdev.
12412 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
12413 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
12414 vdevname
= strdup("root-0");
12416 vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
12417 cbp
->cb_vdevs
.cb_name_flags
);
12419 (void) vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
12421 ret
= get_callback_vdev(zhp
, vdevname
, data
);
12429 get_callback(zpool_handle_t
*zhp
, void *data
)
12431 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
12432 char value
[ZFS_MAXPROPLEN
];
12433 zprop_source_t srctype
;
12437 nvlist_t
*props
, *item
, *d
;
12438 props
= item
= d
= NULL
;
12440 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
12441 if (cbp
->cb_json
) {
12442 nvlist_t
*pool
= fnvlist_alloc();
12443 fill_pool_info(pool
, zhp
, B_FALSE
, cbp
->cb_json_as_int
);
12444 fnvlist_add_nvlist(cbp
->cb_jsobj
, "pool", pool
);
12445 fnvlist_free(pool
);
12448 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
12449 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
12451 /* Adjust column widths for vdev properties */
12452 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
12454 vdev_expand_proplist(zhp
,
12455 cbp
->cb_vdevs
.cb_names
[vid
],
12456 &cbp
->cb_proplist
);
12458 /* Display the properties */
12459 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
12461 get_callback_vdev(zhp
,
12462 cbp
->cb_vdevs
.cb_names
[vid
], data
);
12466 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
12467 if (cbp
->cb_json
) {
12468 d
= fnvlist_lookup_nvlist(cbp
->cb_jsobj
, "pools");
12470 fprintf(stderr
, "pools obj not found.\n");
12473 props
= fnvlist_alloc();
12475 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
12477 * Skip the special fake placeholder. This will also
12478 * skip over the name property when 'all' is specified.
12480 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
12481 pl
== cbp
->cb_proplist
)
12484 if (pl
->pl_prop
== ZPROP_INVAL
&&
12485 zfs_prop_user(pl
->pl_user_prop
)) {
12486 srctype
= ZPROP_SRC_LOCAL
;
12488 if (zpool_get_userprop(zhp
, pl
->pl_user_prop
,
12489 value
, sizeof (value
), &srctype
) != 0)
12492 err
= zprop_collect_property(
12493 zpool_get_name(zhp
), cbp
, pl
->pl_user_prop
,
12494 value
, srctype
, NULL
, NULL
, props
);
12495 } else if (pl
->pl_prop
== ZPROP_INVAL
&&
12496 (zpool_prop_feature(pl
->pl_user_prop
) ||
12497 zpool_prop_unsupported(pl
->pl_user_prop
))) {
12498 srctype
= ZPROP_SRC_LOCAL
;
12500 if (zpool_prop_get_feature(zhp
,
12501 pl
->pl_user_prop
, value
,
12502 sizeof (value
)) == 0) {
12503 err
= zprop_collect_property(
12504 zpool_get_name(zhp
), cbp
,
12505 pl
->pl_user_prop
, value
, srctype
,
12506 NULL
, NULL
, props
);
12509 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
12510 sizeof (value
), &srctype
,
12511 cbp
->cb_literal
) != 0)
12514 err
= zprop_collect_property(
12515 zpool_get_name(zhp
), cbp
,
12516 zpool_prop_to_name(pl
->pl_prop
),
12517 value
, srctype
, NULL
, NULL
, props
);
12523 if (cbp
->cb_json
) {
12524 if (!nvlist_empty(props
)) {
12525 item
= fnvlist_alloc();
12526 fill_pool_info(item
, zhp
, B_TRUE
,
12527 cbp
->cb_json_as_int
);
12528 fnvlist_add_nvlist(item
, "properties", props
);
12529 if (cbp
->cb_json_pool_key_guid
) {
12531 uint64_t guid
= fnvlist_lookup_uint64(
12532 zpool_get_config(zhp
, NULL
),
12533 ZPOOL_CONFIG_POOL_GUID
);
12534 snprintf(buf
, 256, "%llu",
12535 (u_longlong_t
)guid
);
12536 fnvlist_add_nvlist(d
, buf
, item
);
12538 const char *name
= zpool_get_name(zhp
);
12539 fnvlist_add_nvlist(d
, name
, item
);
12541 fnvlist_add_nvlist(cbp
->cb_jsobj
, "pools", d
);
12542 fnvlist_free(item
);
12544 fnvlist_free(props
);
12552 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
12554 * -H Scripted mode. Don't display headers, and separate properties
12556 * -o List of columns to display. Defaults to
12557 * "name,property,value,source".
12558 * -p Display values in parsable (exact) format.
12559 * -j Display output in JSON format.
12560 * --json-int Display numbers as integers instead of strings.
12561 * --json-pool-key-guid Set pool GUID as key for pool objects.
12563 * Get properties of pools in the system. Output space statistics
12564 * for each one as well as other attributes.
12567 zpool_do_get(int argc
, char **argv
)
12569 zprop_get_cbdata_t cb
= { 0 };
12570 zprop_list_t fake_name
= { 0 };
12573 char *propstr
= NULL
;
12575 nvlist_t
*data
= NULL
;
12577 cb
.cb_first
= B_TRUE
;
12580 * Set up default columns and sources.
12582 cb
.cb_sources
= ZPROP_SRC_ALL
;
12583 cb
.cb_columns
[0] = GET_COL_NAME
;
12584 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
12585 cb
.cb_columns
[2] = GET_COL_VALUE
;
12586 cb
.cb_columns
[3] = GET_COL_SOURCE
;
12587 cb
.cb_type
= ZFS_TYPE_POOL
;
12588 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
12589 current_prop_type
= cb
.cb_type
;
12591 struct option long_options
[] = {
12592 {"json-int", no_argument
, NULL
, ZPOOL_OPTION_JSON_NUMS_AS_INT
},
12593 {"json-pool-key-guid", no_argument
, NULL
,
12594 ZPOOL_OPTION_POOL_KEY_GUID
},
12598 /* check options */
12599 while ((c
= getopt_long(argc
, argv
, ":jHpo:", long_options
,
12603 cb
.cb_literal
= B_TRUE
;
12606 cb
.cb_scripted
= B_TRUE
;
12609 cb
.cb_json
= B_TRUE
;
12610 cb
.cb_jsobj
= zpool_json_schema(0, 1);
12611 data
= fnvlist_alloc();
12613 case ZPOOL_OPTION_POOL_KEY_GUID
:
12614 cb
.cb_json_pool_key_guid
= B_TRUE
;
12616 case ZPOOL_OPTION_JSON_NUMS_AS_INT
:
12617 cb
.cb_json_as_int
= B_TRUE
;
12618 cb
.cb_literal
= B_TRUE
;
12621 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
12624 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
12625 static const char *const col_opts
[] =
12626 { "name", "property", "value", "source",
12628 static const zfs_get_column_t col_cols
[] =
12629 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
12632 if (i
== ZFS_GET_NCOLS
- 1) {
12633 (void) fprintf(stderr
, gettext("too "
12634 "many fields given to -o "
12639 for (c
= 0; c
< ARRAY_SIZE(col_opts
); ++c
)
12640 if (strcmp(tok
, col_opts
[c
]) == 0)
12643 (void) fprintf(stderr
,
12644 gettext("invalid column name '%s'\n"), tok
);
12650 (void) fprintf(stderr
,
12651 gettext("\"all\" conflicts "
12652 "with specific fields "
12653 "given to -o option\n"));
12657 memcpy(cb
.cb_columns
, col_cols
,
12658 sizeof (col_cols
));
12659 i
= ZFS_GET_NCOLS
- 1;
12661 cb
.cb_columns
[i
++] = col_cols
[c
];
12665 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
12674 if (!cb
.cb_json
&& cb
.cb_json_as_int
) {
12675 (void) fprintf(stderr
, gettext("'--json-int' only works with"
12676 " '-j' option\n"));
12680 if (!cb
.cb_json
&& cb
.cb_json_pool_key_guid
) {
12681 (void) fprintf(stderr
, gettext("'json-pool-key-guid' only"
12682 " works with '-j' option\n"));
12687 (void) fprintf(stderr
, gettext("missing property "
12692 /* Properties list is needed later by zprop_get_list() */
12699 /* No args, so just print the defaults. */
12700 } else if (are_all_pools(argc
, argv
)) {
12701 /* All the args are pool names */
12702 } else if (are_all_pools(1, argv
)) {
12703 /* The first arg is a pool name */
12704 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
12705 (argc
== 2 && strcmp(argv
[1], "root") == 0) ||
12706 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
12709 if (strcmp(argv
[1], "root") == 0)
12710 vdev
= strdup("root-0");
12712 vdev
= strdup(argv
[1]);
12714 /* ... and the rest are vdev names */
12715 cb
.cb_vdevs
.cb_names
= &vdev
;
12716 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
12717 cb
.cb_type
= ZFS_TYPE_VDEV
;
12718 argc
= 1; /* One pool to process */
12721 nvlist_free(cb
.cb_jsobj
);
12724 fprintf(stderr
, gettext("Expected a list of vdevs in"
12725 " \"%s\", but got:\n"), argv
[0]);
12726 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
12727 argv
[0], &cb
.cb_vdevs
);
12728 fprintf(stderr
, "\n");
12734 nvlist_free(cb
.cb_jsobj
);
12738 * The first arg isn't the name of a valid pool.
12740 fprintf(stderr
, gettext("Cannot get properties of %s: "
12741 "no such pool available.\n"), argv
[0]);
12745 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
12746 cb
.cb_type
) != 0) {
12747 /* Use correct list of valid properties (pool or vdev) */
12748 current_prop_type
= cb
.cb_type
;
12752 if (cb
.cb_proplist
!= NULL
) {
12753 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
12754 fake_name
.pl_width
= strlen(gettext("NAME"));
12755 fake_name
.pl_next
= cb
.cb_proplist
;
12756 cb
.cb_proplist
= &fake_name
;
12760 if (cb
.cb_type
== ZFS_TYPE_VDEV
)
12761 fnvlist_add_nvlist(cb
.cb_jsobj
, "vdevs", data
);
12763 fnvlist_add_nvlist(cb
.cb_jsobj
, "pools", data
);
12764 fnvlist_free(data
);
12767 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
12768 cb
.cb_literal
, get_callback
, &cb
);
12770 if (ret
== 0 && cb
.cb_json
)
12771 zcmd_print_json(cb
.cb_jsobj
);
12772 else if (ret
!= 0 && cb
.cb_json
)
12773 nvlist_free(cb
.cb_jsobj
);
12775 if (cb
.cb_proplist
== &fake_name
)
12776 zprop_free_list(fake_name
.pl_next
);
12778 zprop_free_list(cb
.cb_proplist
);
12786 typedef struct set_cbdata
{
12789 zfs_type_t cb_type
;
12790 vdev_cbdata_t cb_vdevs
;
12791 boolean_t cb_any_successful
;
12795 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
12799 /* Check if we have out-of-bounds features */
12800 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
12801 boolean_t features
[SPA_FEATURES
];
12802 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
12803 ZPOOL_COMPATIBILITY_OK
)
12806 nvlist_t
*enabled
= zpool_get_features(zhp
);
12808 for (i
= 0; i
< SPA_FEATURES
; i
++) {
12809 const char *fguid
= spa_feature_table
[i
].fi_guid
;
12810 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
12813 if (i
< SPA_FEATURES
)
12814 (void) fprintf(stderr
, gettext("Warning: one or "
12815 "more features already enabled on pool '%s'\n"
12816 "are not present in this compatibility set.\n"),
12817 zpool_get_name(zhp
));
12820 /* if we're setting a feature, check it's in compatibility set */
12821 if (zpool_prop_feature(cb
->cb_propname
) &&
12822 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
12823 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
12826 if (zfeature_lookup_name(fname
, &f
) == 0) {
12827 char compat
[ZFS_MAXPROPLEN
];
12828 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
12829 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
12832 boolean_t features
[SPA_FEATURES
];
12833 if (zpool_do_load_compat(compat
, features
) !=
12834 ZPOOL_COMPATIBILITY_OK
) {
12835 (void) fprintf(stderr
, gettext("Error: "
12836 "cannot enable feature '%s' on pool '%s'\n"
12837 "because the pool's 'compatibility' "
12838 "property cannot be parsed.\n"),
12839 fname
, zpool_get_name(zhp
));
12843 if (!features
[f
]) {
12844 (void) fprintf(stderr
, gettext("Error: "
12845 "cannot enable feature '%s' on pool '%s'\n"
12846 "as it is not specified in this pool's "
12847 "current compatibility set.\n"
12848 "Consider setting 'compatibility' to a "
12849 "less restrictive set, or to 'off'.\n"),
12850 fname
, zpool_get_name(zhp
));
12856 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
12862 set_callback(zpool_handle_t
*zhp
, void *data
)
12865 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
12867 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
12868 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
12869 cb
->cb_propname
, cb
->cb_value
);
12871 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
12872 error
= set_pool_callback(zhp
, cb
);
12875 cb
->cb_any_successful
= !error
;
12880 zpool_do_set(int argc
, char **argv
)
12882 set_cbdata_t cb
= { 0 };
12886 current_prop_type
= ZFS_TYPE_POOL
;
12887 if (argc
> 1 && argv
[1][0] == '-') {
12888 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
12894 (void) fprintf(stderr
, gettext("missing property=value "
12900 (void) fprintf(stderr
, gettext("missing pool name\n"));
12905 (void) fprintf(stderr
, gettext("too many pool names\n"));
12909 cb
.cb_propname
= argv
[1];
12910 cb
.cb_type
= ZFS_TYPE_POOL
;
12911 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
12912 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
12913 if (cb
.cb_value
== NULL
) {
12914 (void) fprintf(stderr
, gettext("missing value in "
12915 "property=value argument\n"));
12919 *(cb
.cb_value
) = '\0';
12924 /* argv[0] is pool name */
12925 if (!is_pool(argv
[0])) {
12926 (void) fprintf(stderr
,
12927 gettext("cannot open '%s': is not a pool\n"), argv
[0]);
12931 /* argv[1], when supplied, is vdev name */
12934 if (strcmp(argv
[1], "root") == 0)
12935 vdev
= strdup("root-0");
12937 vdev
= strdup(argv
[1]);
12939 if (!are_vdevs_in_pool(1, &vdev
, argv
[0], &cb
.cb_vdevs
)) {
12940 (void) fprintf(stderr
, gettext(
12941 "cannot find '%s' in '%s': device not in pool\n"),
12946 cb
.cb_vdevs
.cb_names
= &vdev
;
12947 cb
.cb_vdevs
.cb_names_count
= 1;
12948 cb
.cb_type
= ZFS_TYPE_VDEV
;
12951 error
= for_each_pool(1, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
12952 B_FALSE
, set_callback
, &cb
);
12960 /* Add up the total number of bytes left to initialize/trim across all vdevs */
12962 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
12964 uint64_t bytes_remaining
;
12966 uint_t c
, children
;
12969 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
12970 activity
== ZPOOL_WAIT_TRIM
);
12972 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
12973 (uint64_t **)&vs
, &c
) == 0);
12975 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
12976 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
12977 bytes_remaining
= vs
->vs_initialize_bytes_est
-
12978 vs
->vs_initialize_bytes_done
;
12979 else if (activity
== ZPOOL_WAIT_TRIM
&&
12980 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
12981 bytes_remaining
= vs
->vs_trim_bytes_est
-
12982 vs
->vs_trim_bytes_done
;
12984 bytes_remaining
= 0;
12986 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
12987 &child
, &children
) != 0)
12990 for (c
= 0; c
< children
; c
++)
12991 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
12993 return (bytes_remaining
);
12996 /* Add up the total number of bytes left to rebuild across top-level vdevs */
12998 vdev_activity_top_remaining(nvlist_t
*nv
)
13000 uint64_t bytes_remaining
= 0;
13005 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
13006 &child
, &children
) != 0)
13009 for (uint_t c
= 0; c
< children
; c
++) {
13010 vdev_rebuild_stat_t
*vrs
;
13013 error
= nvlist_lookup_uint64_array(child
[c
],
13014 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
13016 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
13017 bytes_remaining
+= (vrs
->vrs_bytes_est
-
13018 vrs
->vrs_bytes_rebuilt
);
13023 return (bytes_remaining
);
13026 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
13028 vdev_any_spare_replacing(nvlist_t
*nv
)
13031 uint_t c
, children
;
13032 const char *vdev_type
;
13034 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
13036 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
13037 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
13038 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
13042 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
13043 &child
, &children
) != 0)
13046 for (c
= 0; c
< children
; c
++) {
13047 if (vdev_any_spare_replacing(child
[c
]))
13054 typedef struct wait_data
{
13056 boolean_t wd_scripted
;
13057 boolean_t wd_exact
;
13058 boolean_t wd_headers_once
;
13059 boolean_t wd_should_exit
;
13060 /* Which activities to wait for */
13061 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
13063 pthread_cond_t wd_cv
;
13064 pthread_mutex_t wd_mutex
;
13068 * Print to stdout a single line, containing one column for each activity that
13069 * we are waiting for specifying how many bytes of work are left for that
13073 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
13075 nvlist_t
*config
, *nvroot
;
13078 pool_checkpoint_stat_t
*pcs
= NULL
;
13079 pool_scan_stat_t
*pss
= NULL
;
13080 pool_removal_stat_t
*prs
= NULL
;
13081 pool_raidz_expand_stat_t
*pres
= NULL
;
13082 const char *const headers
[] = {"DISCARD", "FREE", "INITIALIZE",
13083 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"};
13084 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
13086 /* Calculate the width of each column */
13087 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
13089 * Make sure we have enough space in the col for pretty-printed
13090 * numbers and for the column header, and then leave a couple
13091 * spaces between cols for readability.
13093 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
13096 if (timestamp_fmt
!= NODATE
)
13097 print_timestamp(timestamp_fmt
);
13099 /* Print header if appropriate */
13100 int term_height
= terminal_height();
13101 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
13102 row
% (term_height
-1) == 0);
13103 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
13104 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
13105 if (wd
->wd_enabled
[i
])
13106 (void) printf("%*s", col_widths
[i
], headers
[i
]);
13108 (void) fputc('\n', stdout
);
13111 /* Bytes of work remaining in each activity */
13112 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
13114 bytes_rem
[ZPOOL_WAIT_FREE
] =
13115 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
13117 config
= zpool_get_config(zhp
, NULL
);
13118 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
13120 (void) nvlist_lookup_uint64_array(nvroot
,
13121 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
13122 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
13123 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
13125 (void) nvlist_lookup_uint64_array(nvroot
,
13126 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
13127 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
13128 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
13131 (void) nvlist_lookup_uint64_array(nvroot
,
13132 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
13133 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
13134 pss
->pss_pass_scrub_pause
== 0) {
13135 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
13136 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
13137 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
13139 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
13140 } else if (check_rebuilding(nvroot
, NULL
)) {
13141 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
13142 vdev_activity_top_remaining(nvroot
);
13145 (void) nvlist_lookup_uint64_array(nvroot
,
13146 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, (uint64_t **)&pres
, &c
);
13147 if (pres
!= NULL
&& pres
->pres_state
== DSS_SCANNING
) {
13148 int64_t rem
= pres
->pres_to_reflow
- pres
->pres_reflowed
;
13149 bytes_rem
[ZPOOL_WAIT_RAIDZ_EXPAND
] = rem
;
13152 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
13153 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
13154 bytes_rem
[ZPOOL_WAIT_TRIM
] =
13155 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
13158 * A replace finishes after resilvering finishes, so the amount of work
13159 * left for a replace is the same as for resilvering.
13161 * It isn't quite correct to say that if we have any 'spare' or
13162 * 'replacing' vdevs and a resilver is happening, then a replace is in
13163 * progress, like we do here. When a hot spare is used, the faulted vdev
13164 * is not removed after the hot spare is resilvered, so parent 'spare'
13165 * vdev is not removed either. So we could have a 'spare' vdev, but be
13166 * resilvering for a different reason. However, we use it as a heuristic
13167 * because we don't have access to the DTLs, which could tell us whether
13168 * or not we have really finished resilvering a hot spare.
13170 if (vdev_any_spare_replacing(nvroot
))
13171 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
13173 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
13175 if (!wd
->wd_enabled
[i
])
13178 if (wd
->wd_exact
) {
13179 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
13182 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
13185 if (wd
->wd_scripted
)
13186 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
13188 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
13190 (void) printf("\n");
13191 (void) fflush(stdout
);
13195 wait_status_thread(void *arg
)
13197 wait_data_t
*wd
= (wait_data_t
*)arg
;
13198 zpool_handle_t
*zhp
;
13200 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
13201 return (void *)(1);
13203 for (int row
= 0; ; row
++) {
13205 struct timespec timeout
;
13207 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
13209 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
13210 zpool_props_refresh(zhp
) != 0) {
13212 return (void *)(uintptr_t)(missing
? 0 : 1);
13215 print_wait_status_row(wd
, zhp
, row
);
13217 timeout
.tv_sec
+= floor(wd
->wd_interval
);
13218 long nanos
= timeout
.tv_nsec
+
13219 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
13220 if (nanos
>= NANOSEC
) {
13222 timeout
.tv_nsec
= nanos
- NANOSEC
;
13224 timeout
.tv_nsec
= nanos
;
13226 pthread_mutex_lock(&wd
->wd_mutex
);
13227 if (!wd
->wd_should_exit
)
13228 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
13230 pthread_mutex_unlock(&wd
->wd_mutex
);
13232 break; /* signaled by main thread */
13233 } else if (ret
!= ETIMEDOUT
) {
13234 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
13235 "failed: %s\n"), strerror(ret
));
13237 return (void *)(uintptr_t)(1);
13242 return (void *)(0);
13246 zpool_do_wait(int argc
, char **argv
)
13248 boolean_t verbose
= B_FALSE
;
13250 unsigned long count
;
13251 pthread_t status_thr
;
13253 zpool_handle_t
*zhp
;
13256 wd
.wd_scripted
= B_FALSE
;
13257 wd
.wd_exact
= B_FALSE
;
13258 wd
.wd_headers_once
= B_FALSE
;
13259 wd
.wd_should_exit
= B_FALSE
;
13261 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
13262 pthread_cond_init(&wd
.wd_cv
, NULL
);
13264 /* By default, wait for all types of activity. */
13265 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
13266 wd
.wd_enabled
[i
] = B_TRUE
;
13268 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
13271 wd
.wd_scripted
= B_TRUE
;
13274 wd
.wd_headers_once
= B_TRUE
;
13277 wd
.wd_exact
= B_TRUE
;
13280 get_timestamp_arg(*optarg
);
13283 /* Reset activities array */
13284 memset(&wd
.wd_enabled
, 0, sizeof (wd
.wd_enabled
));
13286 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
13287 static const char *const col_opts
[] = {
13288 "discard", "free", "initialize", "replace",
13289 "remove", "resilver", "scrub", "trim",
13292 for (i
= 0; i
< ARRAY_SIZE(col_opts
); ++i
)
13293 if (strcmp(tok
, col_opts
[i
]) == 0) {
13294 wd
.wd_enabled
[i
] = B_TRUE
;
13298 (void) fprintf(stderr
,
13299 gettext("invalid activity '%s'\n"), tok
);
13305 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
13314 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
13316 /* This subcmd only accepts an interval, not a count */
13317 (void) fprintf(stderr
, gettext("too many arguments\n"));
13321 if (wd
.wd_interval
!= 0)
13325 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
13329 (void) fprintf(stderr
, gettext("too many arguments\n"));
13333 wd
.wd_poolname
= argv
[0];
13335 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
13340 * We use a separate thread for printing status updates because
13341 * the main thread will call lzc_wait(), which blocks as long
13342 * as an activity is in progress, which can be a long time.
13344 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
13346 (void) fprintf(stderr
, gettext("failed to create status"
13347 "thread: %s\n"), strerror(errno
));
13354 * Loop over all activities that we are supposed to wait for until none
13355 * of them are in progress. Note that this means we can end up waiting
13356 * for more activities to complete than just those that were in progress
13357 * when we began waiting; if an activity we are interested in begins
13358 * while we are waiting for another activity, we will wait for both to
13359 * complete before exiting.
13362 boolean_t missing
= B_FALSE
;
13363 boolean_t any_waited
= B_FALSE
;
13365 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
13368 if (!wd
.wd_enabled
[i
])
13371 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
13372 if (error
!= 0 || missing
)
13375 any_waited
= (any_waited
|| waited
);
13378 if (error
!= 0 || missing
|| !any_waited
)
13386 pthread_mutex_lock(&wd
.wd_mutex
);
13387 wd
.wd_should_exit
= B_TRUE
;
13388 pthread_cond_signal(&wd
.wd_cv
);
13389 pthread_mutex_unlock(&wd
.wd_mutex
);
13390 (void) pthread_join(status_thr
, (void *)&status
);
13395 pthread_mutex_destroy(&wd
.wd_mutex
);
13396 pthread_cond_destroy(&wd
.wd_cv
);
13401 * zpool ddtprune -d|-p <amount> <pool>
13403 * -d <days> Prune entries <days> old and older
13404 * -p <percent> Prune <percent> amount of entries
13406 * Prune single reference entries from DDT to satisfy the amount specified.
13409 zpool_do_ddt_prune(int argc
, char **argv
)
13411 zpool_ddt_prune_unit_t unit
= ZPOOL_DDT_PRUNE_NONE
;
13412 uint64_t amount
= 0;
13413 zpool_handle_t
*zhp
;
13417 while ((c
= getopt(argc
, argv
, "d:p:")) != -1) {
13420 if (unit
== ZPOOL_DDT_PRUNE_PERCENTAGE
) {
13421 (void) fprintf(stderr
, gettext("-d cannot be "
13422 "combined with -p option\n"));
13426 amount
= strtoull(optarg
, &endptr
, 0);
13427 if (errno
!= 0 || *endptr
!= '\0' || amount
== 0) {
13428 (void) fprintf(stderr
,
13429 gettext("invalid days value\n"));
13432 amount
*= 86400; /* convert days to seconds */
13433 unit
= ZPOOL_DDT_PRUNE_AGE
;
13436 if (unit
== ZPOOL_DDT_PRUNE_AGE
) {
13437 (void) fprintf(stderr
, gettext("-p cannot be "
13438 "combined with -d option\n"));
13442 amount
= strtoull(optarg
, &endptr
, 0);
13443 if (errno
!= 0 || *endptr
!= '\0' ||
13444 amount
== 0 || amount
> 100) {
13445 (void) fprintf(stderr
,
13446 gettext("invalid percentage value\n"));
13449 unit
= ZPOOL_DDT_PRUNE_PERCENTAGE
;
13452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
13460 if (unit
== ZPOOL_DDT_PRUNE_NONE
) {
13461 (void) fprintf(stderr
,
13462 gettext("missing amount option (-d|-p <value>)\n"));
13464 } else if (argc
< 1) {
13465 (void) fprintf(stderr
, gettext("missing pool argument\n"));
13467 } else if (argc
> 1) {
13468 (void) fprintf(stderr
, gettext("too many arguments\n"));
13471 zhp
= zpool_open(g_zfs
, argv
[0]);
13475 int error
= zpool_ddt_prune(zhp
, unit
, amount
);
13483 find_command_idx(const char *command
, int *idx
)
13485 for (int i
= 0; i
< NCOMMAND
; ++i
) {
13486 if (command_table
[i
].name
== NULL
)
13489 if (strcmp(command
, command_table
[i
].name
) == 0) {
13498 * Display version message
13501 zpool_do_version(int argc
, char **argv
)
13504 nvlist_t
*jsobj
= NULL
, *zfs_ver
= NULL
;
13505 boolean_t json
= B_FALSE
;
13506 while ((c
= getopt(argc
, argv
, "j")) != -1) {
13510 jsobj
= zpool_json_schema(0, 1);
13513 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
13521 (void) fprintf(stderr
, "too many arguments\n");
13526 zfs_ver
= zfs_version_nvlist();
13528 fnvlist_add_nvlist(jsobj
, "zfs_version", zfs_ver
);
13529 zcmd_print_json(jsobj
);
13530 fnvlist_free(zfs_ver
);
13535 return (zfs_version_print() != 0);
13538 /* Display documentation */
13540 zpool_do_help(int argc
, char **argv
)
13542 char page
[MAXNAMELEN
];
13543 if (argc
< 3 || strcmp(argv
[2], "zpool") == 0)
13544 strcpy(page
, "zpool");
13545 else if (strcmp(argv
[2], "concepts") == 0 ||
13546 strcmp(argv
[2], "props") == 0)
13547 snprintf(page
, sizeof (page
), "zpool%s", argv
[2]);
13549 snprintf(page
, sizeof (page
), "zpool-%s", argv
[2]);
13551 execlp("man", "man", page
, NULL
);
13553 fprintf(stderr
, "couldn't run man program: %s", strerror(errno
));
13558 * Do zpool_load_compat() and print error message on failure
13560 static zpool_compat_status_t
13561 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
13565 zpool_compat_status_t ret
;
13567 ret
= zpool_load_compat(compat
, list
, report
, 1024);
13570 case ZPOOL_COMPATIBILITY_OK
:
13573 case ZPOOL_COMPATIBILITY_NOFILES
:
13574 case ZPOOL_COMPATIBILITY_BADFILE
:
13575 case ZPOOL_COMPATIBILITY_BADTOKEN
:
13576 (void) fprintf(stderr
, "Error: %s\n", report
);
13579 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
13580 (void) fprintf(stderr
, "Warning: %s\n", report
);
13581 ret
= ZPOOL_COMPATIBILITY_OK
;
13588 main(int argc
, char **argv
)
13595 (void) setlocale(LC_ALL
, "");
13596 (void) setlocale(LC_NUMERIC
, "C");
13597 (void) textdomain(TEXT_DOMAIN
);
13603 * Make sure the user has specified some command.
13606 (void) fprintf(stderr
, gettext("missing command\n"));
13613 * Special case '-?'
13615 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
13619 * Special case '-V|--version'
13621 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
13622 return (zfs_version_print() != 0);
13625 * Special case 'help'
13627 if (strcmp(cmdname
, "help") == 0)
13628 return (zpool_do_help(argc
, argv
));
13630 if ((g_zfs
= libzfs_init()) == NULL
) {
13631 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
13635 libzfs_print_on_error(g_zfs
, B_TRUE
);
13637 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
13640 * Many commands modify input strings for string parsing reasons.
13641 * We create a copy to protect the original argv.
13643 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
13644 for (i
= 0; i
< argc
; i
++)
13645 newargv
[i
] = strdup(argv
[i
]);
13646 newargv
[argc
] = NULL
;
13649 * Run the appropriate command.
13651 if (find_command_idx(cmdname
, &i
) == 0) {
13652 current_command
= &command_table
[i
];
13653 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
13654 } else if (strchr(cmdname
, '=')) {
13655 verify(find_command_idx("set", &i
) == 0);
13656 current_command
= &command_table
[i
];
13657 ret
= command_table
[i
].func(argc
, newargv
);
13658 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
13660 * 'freeze' is a vile debugging abomination, so we treat
13663 zfs_cmd_t zc
= {"\0"};
13665 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
13666 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
13668 (void) fprintf(stderr
,
13669 gettext("failed to freeze pool: %d\n"), errno
);
13675 (void) fprintf(stderr
, gettext("unrecognized "
13676 "command '%s'\n"), cmdname
);
13681 for (i
= 0; i
< argc
; i
++)
13685 if (ret
== 0 && log_history
)
13686 (void) zpool_log_history(g_zfs
, history_str
);
13688 libzfs_fini(g_zfs
);
13691 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
13692 * for the purposes of running ::findleaks.
13694 if (getenv("ZFS_ABORT") != NULL
) {
13695 (void) printf("dumping core by request\n");