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 http://www.opensolaris.org/os/licensing.
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, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
57 #include <sys/fs/zfs.h>
59 #include <sys/systeminfo.h>
60 #include <sys/fm/fs/zfs.h>
61 #include <sys/fm/util.h>
62 #include <sys/fm/protocol.h>
63 #include <sys/zfs_ioctl.h>
64 #include <sys/mount.h>
65 #include <sys/sysmacros.h>
72 #include "zpool_util.h"
73 #include "zfs_comutil.h"
74 #include "zfeature_common.h"
76 #include "statcommon.h"
78 libzfs_handle_t
*g_zfs
;
80 static int zpool_do_create(int, char **);
81 static int zpool_do_destroy(int, char **);
83 static int zpool_do_add(int, char **);
84 static int zpool_do_remove(int, char **);
85 static int zpool_do_labelclear(int, char **);
87 static int zpool_do_checkpoint(int, char **);
89 static int zpool_do_list(int, char **);
90 static int zpool_do_iostat(int, char **);
91 static int zpool_do_status(int, char **);
93 static int zpool_do_online(int, char **);
94 static int zpool_do_offline(int, char **);
95 static int zpool_do_clear(int, char **);
96 static int zpool_do_reopen(int, char **);
98 static int zpool_do_reguid(int, char **);
100 static int zpool_do_attach(int, char **);
101 static int zpool_do_detach(int, char **);
102 static int zpool_do_replace(int, char **);
103 static int zpool_do_split(int, char **);
105 static int zpool_do_initialize(int, char **);
106 static int zpool_do_scrub(int, char **);
107 static int zpool_do_resilver(int, char **);
108 static int zpool_do_trim(int, char **);
110 static int zpool_do_import(int, char **);
111 static int zpool_do_export(int, char **);
113 static int zpool_do_upgrade(int, char **);
115 static int zpool_do_history(int, char **);
116 static int zpool_do_events(int, char **);
118 static int zpool_do_get(int, char **);
119 static int zpool_do_set(int, char **);
121 static int zpool_do_sync(int, char **);
123 static int zpool_do_version(int, char **);
125 static int zpool_do_wait(int, char **);
128 * These libumem hooks provide a reasonable set of defaults for the allocator's
129 * debugging facilities.
134 _umem_debug_init(void)
136 return ("default,verbose"); /* $UMEM_DEBUG setting */
140 _umem_logging_init(void)
142 return ("fail,contents"); /* $UMEM_LOGGING setting */
183 * Flags for stats to display with "zpool iostats"
191 IOS_COUNT
, /* always last element */
194 /* iostat_type entries as bitmasks */
195 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
196 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
197 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
198 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
199 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
201 /* Mask of all the histo bits */
202 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
205 * Lookup table for iostat flags to nvlist names. Basically a list
206 * of all the nvlists a flag requires. Also specifies the order in
207 * which data gets printed in zpool iostat.
209 static const char *vsx_type_to_nvlist
[IOS_COUNT
][13] = {
211 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
212 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
213 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
214 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
215 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
216 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
217 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
226 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
227 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
231 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
232 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
233 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
234 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
235 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
238 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
239 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
240 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
241 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
242 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
243 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
244 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
245 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
246 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
247 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
248 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
249 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
255 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
256 * Right now, only one histo bit is ever set at one time, so we can
257 * just do a highbit64(a)
259 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
261 typedef struct zpool_command
{
263 int (*func
)(int, char **);
268 * Master command table. Each ZFS command has a name, associated function, and
269 * usage message. The usage messages need to be internationalized, so we have
270 * to have a function to return the usage message based on a command index.
272 * These commands are organized according to how they are displayed in the usage
273 * message. An empty command (one with a NULL name) indicates an empty line in
274 * the generic usage message.
276 static zpool_command_t command_table
[] = {
277 { "version", zpool_do_version
, HELP_VERSION
},
279 { "create", zpool_do_create
, HELP_CREATE
},
280 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
282 { "add", zpool_do_add
, HELP_ADD
},
283 { "remove", zpool_do_remove
, HELP_REMOVE
},
285 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
287 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
289 { "list", zpool_do_list
, HELP_LIST
},
290 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
291 { "status", zpool_do_status
, HELP_STATUS
},
293 { "online", zpool_do_online
, HELP_ONLINE
},
294 { "offline", zpool_do_offline
, HELP_OFFLINE
},
295 { "clear", zpool_do_clear
, HELP_CLEAR
},
296 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
298 { "attach", zpool_do_attach
, HELP_ATTACH
},
299 { "detach", zpool_do_detach
, HELP_DETACH
},
300 { "replace", zpool_do_replace
, HELP_REPLACE
},
301 { "split", zpool_do_split
, HELP_SPLIT
},
303 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
304 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
305 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
306 { "trim", zpool_do_trim
, HELP_TRIM
},
308 { "import", zpool_do_import
, HELP_IMPORT
},
309 { "export", zpool_do_export
, HELP_EXPORT
},
310 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
311 { "reguid", zpool_do_reguid
, HELP_REGUID
},
313 { "history", zpool_do_history
, HELP_HISTORY
},
314 { "events", zpool_do_events
, HELP_EVENTS
},
316 { "get", zpool_do_get
, HELP_GET
},
317 { "set", zpool_do_set
, HELP_SET
},
318 { "sync", zpool_do_sync
, HELP_SYNC
},
320 { "wait", zpool_do_wait
, HELP_WAIT
},
323 #define NCOMMAND (ARRAY_SIZE(command_table))
325 #define VDEV_ALLOC_CLASS_LOGS "logs"
327 static zpool_command_t
*current_command
;
328 static char history_str
[HIS_MAX_RECORD_LEN
];
329 static boolean_t log_history
= B_TRUE
;
330 static uint_t timestamp_fmt
= NODATE
;
333 get_usage(zpool_help_t idx
)
337 return (gettext("\tadd [-fgLnP] [-o property=value] "
338 "<pool> <vdev> ...\n"));
340 return (gettext("\tattach [-fsw] [-o property=value] "
341 "<pool> <device> <new-device>\n"));
343 return (gettext("\tclear [-nF] <pool> [device]\n"));
345 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
346 "\t [-O file-system-property=value] ... \n"
347 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
348 case HELP_CHECKPOINT
:
349 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
351 return (gettext("\tdestroy [-f] <pool>\n"));
353 return (gettext("\tdetach <pool> <device>\n"));
355 return (gettext("\texport [-af] <pool> ...\n"));
357 return (gettext("\thistory [-il] [<pool>] ...\n"));
359 return (gettext("\timport [-d dir] [-D]\n"
360 "\timport [-o mntopts] [-o property=value] ... \n"
361 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
362 "[-R root] [-F [-n]] -a\n"
363 "\timport [-o mntopts] [-o property=value] ... \n"
364 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
365 "[-R root] [-F [-n]]\n"
366 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
368 return (gettext("\tiostat [[[-c [script1,script2,...]"
369 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
370 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
371 " [[-n] interval [count]]\n"));
372 case HELP_LABELCLEAR
:
373 return (gettext("\tlabelclear [-f] <vdev>\n"));
375 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
376 "[-T d|u] [pool] ... \n"
377 "\t [interval [count]]\n"));
379 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
381 return (gettext("\tonline [-e] <pool> <device> ...\n"));
383 return (gettext("\treplace [-fsw] [-o property=value] "
384 "<pool> <device> [new-device]\n"));
386 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
388 return (gettext("\treopen [-n] <pool>\n"));
389 case HELP_INITIALIZE
:
390 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
391 "[<device> ...]\n"));
393 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
395 return (gettext("\tresilver <pool> ...\n"));
397 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
398 "[<device> ...]\n"));
400 return (gettext("\tstatus [-c [script1,script2,...]] "
401 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
402 "\t [interval [count]]\n"));
404 return (gettext("\tupgrade\n"
406 "\tupgrade [-V version] <-a | pool ...>\n"));
408 return (gettext("\tevents [-vHf [pool] | -c]\n"));
410 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
411 "<\"all\" | property[,...]> <pool> ...\n"));
413 return (gettext("\tset <property=value> <pool> \n"));
415 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
416 "\t [-o property=value] <pool> <newpool> "
417 "[<device> ...]\n"));
419 return (gettext("\treguid <pool>\n"));
421 return (gettext("\tsync [pool] ...\n"));
423 return (gettext("\tversion\n"));
425 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
426 "<pool> [interval]\n"));
434 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
440 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
444 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
447 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
448 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
449 fnvlist_add_boolean(res
, path
);
455 for (i
= 0; i
< children
; i
++) {
456 zpool_collect_leaves(zhp
, child
[i
], res
);
461 * Callback routine that will print out a pool property value.
464 print_prop_cb(int prop
, void *cb
)
468 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
470 if (zpool_prop_readonly(prop
))
471 (void) fprintf(fp
, " NO ");
473 (void) fprintf(fp
, " YES ");
475 if (zpool_prop_values(prop
) == NULL
)
476 (void) fprintf(fp
, "-\n");
478 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
484 * Display usage message. If we're inside a command, display only the usage for
485 * that command. Otherwise, iterate over the entire command table and display
486 * a complete usage message.
489 usage(boolean_t requested
)
491 FILE *fp
= requested
? stdout
: stderr
;
493 if (current_command
== NULL
) {
496 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
498 gettext("where 'command' is one of the following:\n\n"));
500 for (i
= 0; i
< NCOMMAND
; i
++) {
501 if (command_table
[i
].name
== NULL
)
502 (void) fprintf(fp
, "\n");
504 (void) fprintf(fp
, "%s",
505 get_usage(command_table
[i
].usage
));
508 (void) fprintf(fp
, gettext("usage:\n"));
509 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
512 if (current_command
!= NULL
&&
513 ((strcmp(current_command
->name
, "set") == 0) ||
514 (strcmp(current_command
->name
, "get") == 0) ||
515 (strcmp(current_command
->name
, "list") == 0))) {
518 gettext("\nthe following properties are supported:\n"));
520 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
521 "PROPERTY", "EDIT", "VALUES");
523 /* Iterate over all properties */
524 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
527 (void) fprintf(fp
, "\t%-19s ", "feature@...");
528 (void) fprintf(fp
, "YES disabled | enabled | active\n");
530 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
531 "appended with a feature name.\nSee zpool-features(5).\n"));
535 * See comments at end of main().
537 if (getenv("ZFS_ABORT") != NULL
) {
538 (void) printf("dumping core by request\n");
542 exit(requested
? 0 : 2);
546 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
547 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
550 * -c Cancel. Ends active initializing.
551 * -s Suspend. Initializing can then be restarted with no flags.
552 * -w Wait. Blocks until initializing has completed.
555 zpool_do_initialize(int argc
, char **argv
)
562 boolean_t wait
= B_FALSE
;
564 struct option long_options
[] = {
565 {"cancel", no_argument
, NULL
, 'c'},
566 {"suspend", no_argument
, NULL
, 's'},
567 {"wait", no_argument
, NULL
, 'w'},
571 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
572 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
575 if (cmd_type
!= POOL_INITIALIZE_START
&&
576 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
577 (void) fprintf(stderr
, gettext("-c cannot be "
578 "combined with other options\n"));
581 cmd_type
= POOL_INITIALIZE_CANCEL
;
584 if (cmd_type
!= POOL_INITIALIZE_START
&&
585 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
586 (void) fprintf(stderr
, gettext("-s cannot be "
587 "combined with other options\n"));
590 cmd_type
= POOL_INITIALIZE_SUSPEND
;
597 (void) fprintf(stderr
,
598 gettext("invalid option '%c'\n"), optopt
);
600 (void) fprintf(stderr
,
601 gettext("invalid option '%s'\n"),
612 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
617 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
618 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
624 zhp
= zpool_open(g_zfs
, poolname
);
628 vdevs
= fnvlist_alloc();
630 /* no individual leaf vdevs specified, so add them all */
631 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
632 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
633 ZPOOL_CONFIG_VDEV_TREE
);
634 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
636 for (int i
= 1; i
< argc
; i
++) {
637 fnvlist_add_boolean(vdevs
, argv
[i
]);
642 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
644 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
653 * print a pool vdev config for dry runs
656 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
657 const char *match
, int name_flags
)
662 boolean_t printed
= B_FALSE
;
664 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
665 &child
, &children
) != 0) {
667 (void) printf("\t%*s%s\n", indent
, "", name
);
671 for (c
= 0; c
< children
; c
++) {
672 uint64_t is_log
= B_FALSE
;
675 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
678 class = VDEV_ALLOC_BIAS_LOG
;
679 (void) nvlist_lookup_string(child
[c
],
680 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
681 if (strcmp(match
, class) != 0)
684 if (!printed
&& name
!= NULL
) {
685 (void) printf("\t%*s%s\n", indent
, "", name
);
688 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
689 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
696 prop_list_contains_feature(nvlist_t
*proplist
)
699 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
700 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
701 if (zpool_prop_feature(nvpair_name(nvp
)))
708 * Add a property pair (name, string-value) into a property nvlist.
711 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
714 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
719 if (*props
== NULL
&&
720 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
721 (void) fprintf(stderr
,
722 gettext("internal error: out of memory\n"));
729 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
731 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
732 !zpool_prop_feature(propname
)) {
733 (void) fprintf(stderr
, gettext("property '%s' is "
734 "not a valid pool property\n"), propname
);
739 * feature@ properties and version should not be specified
742 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
743 nvlist_exists(proplist
, vname
)) ||
744 (prop
== ZPOOL_PROP_VERSION
&&
745 prop_list_contains_feature(proplist
))) {
746 (void) fprintf(stderr
, gettext("'feature@' and "
747 "'version' properties cannot be specified "
753 if (zpool_prop_feature(propname
))
756 normnm
= zpool_prop_to_name(prop
);
758 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
760 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
762 normnm
= zfs_prop_to_name(fsprop
);
763 } else if (zfs_prop_user(propname
) ||
764 zfs_prop_userquota(propname
)) {
767 (void) fprintf(stderr
, gettext("property '%s' is "
768 "not a valid filesystem property\n"), propname
);
773 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
774 prop
!= ZPOOL_PROP_CACHEFILE
) {
775 (void) fprintf(stderr
, gettext("property '%s' "
776 "specified multiple times\n"), propname
);
780 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
781 (void) fprintf(stderr
, gettext("internal "
782 "error: out of memory\n"));
790 * Set a default property pair (name, string-value) in a property nvlist
793 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
798 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
801 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
805 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
807 * -f Force addition of devices, even if they appear in use
808 * -g Display guid for individual vdev name.
809 * -L Follow links when resolving vdev path name.
810 * -n Do not add the devices, but display the resulting layout if
811 * they were to be added.
812 * -o Set property=value.
813 * -P Display full path for vdev name.
815 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
816 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
820 zpool_do_add(int argc
, char **argv
)
822 boolean_t force
= B_FALSE
;
823 boolean_t dryrun
= B_FALSE
;
831 nvlist_t
*props
= NULL
;
835 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
841 name_flags
|= VDEV_NAME_GUID
;
844 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
850 if ((propval
= strchr(optarg
, '=')) == NULL
) {
851 (void) fprintf(stderr
, gettext("missing "
852 "'=' for -o option\n"));
858 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
859 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
863 name_flags
|= VDEV_NAME_PATH
;
866 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
875 /* get pool name and check number of arguments */
877 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
881 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
890 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
893 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
894 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
900 /* unless manually specified use "ashift" pool property (if set) */
901 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
904 char strval
[ZPOOL_MAXPROPLEN
];
906 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
907 if (src
!= ZPROP_SRC_DEFAULT
) {
908 (void) sprintf(strval
, "%" PRId32
, intval
);
909 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
910 &props
, B_TRUE
) == 0);
914 /* pass off to make_root_vdev for processing */
915 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
917 if (nvroot
== NULL
) {
923 nvlist_t
*poolnvroot
;
925 uint_t l2children
, c
;
927 boolean_t hadcache
= B_FALSE
;
929 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
932 (void) printf(gettext("would update '%s' to the following "
933 "configuration:\n"), zpool_get_name(zhp
));
935 /* print original main pool and new tree */
936 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
937 name_flags
| VDEV_NAME_TYPE_ID
);
938 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
940 /* print other classes: 'dedup', 'special', and 'log' */
941 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
942 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
943 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
944 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
945 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
946 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
947 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
948 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
951 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
952 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
953 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
954 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
955 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
956 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
957 print_vdev_tree(zhp
, "special", nvroot
, 0,
958 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
961 if (num_logs(poolnvroot
) > 0) {
962 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
963 VDEV_ALLOC_BIAS_LOG
, name_flags
);
964 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
965 VDEV_ALLOC_BIAS_LOG
, name_flags
);
966 } else if (num_logs(nvroot
) > 0) {
967 print_vdev_tree(zhp
, "logs", nvroot
, 0,
968 VDEV_ALLOC_BIAS_LOG
, name_flags
);
971 /* Do the same for the caches */
972 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
973 &l2child
, &l2children
) == 0 && l2children
) {
975 (void) printf(gettext("\tcache\n"));
976 for (c
= 0; c
< l2children
; c
++) {
977 vname
= zpool_vdev_name(g_zfs
, NULL
,
978 l2child
[c
], name_flags
);
979 (void) printf("\t %s\n", vname
);
983 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
984 &l2child
, &l2children
) == 0 && l2children
) {
986 (void) printf(gettext("\tcache\n"));
987 for (c
= 0; c
< l2children
; c
++) {
988 vname
= zpool_vdev_name(g_zfs
, NULL
,
989 l2child
[c
], name_flags
);
990 (void) printf("\t %s\n", vname
);
997 ret
= (zpool_add(zhp
, nvroot
) != 0);
1001 nvlist_free(nvroot
);
1008 * zpool remove [-npsw] <pool> <vdev> ...
1010 * Removes the given vdev from the pool.
1013 zpool_do_remove(int argc
, char **argv
)
1017 zpool_handle_t
*zhp
= NULL
;
1018 boolean_t stop
= B_FALSE
;
1020 boolean_t noop
= B_FALSE
;
1021 boolean_t parsable
= B_FALSE
;
1022 boolean_t wait
= B_FALSE
;
1025 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1040 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1049 /* get pool name and check number of arguments */
1051 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1057 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1061 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1067 (void) fprintf(stderr
, gettext("too many arguments\n"));
1070 if (zpool_vdev_remove_cancel(zhp
) != 0)
1073 (void) fprintf(stderr
, gettext("invalid option "
1074 "combination: -w cannot be used with -s\n"));
1079 (void) fprintf(stderr
, gettext("missing device\n"));
1083 for (i
= 1; i
< argc
; i
++) {
1087 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1093 (void) printf("%s %llu\n",
1094 argv
[i
], (unsigned long long)size
);
1097 zfs_nicenum(size
, valstr
,
1099 (void) printf("Memory that will be "
1100 "used after removing %s: %s\n",
1104 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1109 if (ret
== 0 && wait
)
1110 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1118 * zpool labelclear [-f] <vdev>
1120 * -f Force clearing the label for the vdevs which are members of
1121 * the exported or foreign pools.
1123 * Verifies that the vdev is not active and zeros out the label information
1127 zpool_do_labelclear(int argc
, char **argv
)
1129 char vdev
[MAXPATHLEN
];
1132 int c
, fd
= -1, ret
= 0;
1135 boolean_t inuse
= B_FALSE
;
1136 boolean_t force
= B_FALSE
;
1139 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1145 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1156 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1160 (void) fprintf(stderr
, gettext("too many arguments\n"));
1165 * Check if we were given absolute path and use it as is.
1166 * Otherwise if the provided vdev name doesn't point to a file,
1167 * try prepending expected disk paths and partition numbers.
1169 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1170 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1173 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1174 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1175 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1179 if (error
|| (stat(vdev
, &st
) != 0)) {
1180 (void) fprintf(stderr
, gettext(
1181 "failed to find device %s, try specifying absolute "
1182 "path instead\n"), argv
[0]);
1187 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1188 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1189 vdev
, strerror(errno
));
1194 * Flush all dirty pages for the block device. This should not be
1195 * fatal when the device does not support BLKFLSBUF as would be the
1196 * case for a file vdev.
1198 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1199 (void) fprintf(stderr
, gettext("failed to invalidate "
1200 "cache for %s: %s\n"), vdev
, strerror(errno
));
1202 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1203 (void) fprintf(stderr
,
1204 gettext("failed to read label from %s\n"), vdev
);
1208 nvlist_free(config
);
1210 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1212 (void) fprintf(stderr
,
1213 gettext("failed to check state for %s\n"), vdev
);
1223 case POOL_STATE_ACTIVE
:
1224 case POOL_STATE_SPARE
:
1225 case POOL_STATE_L2CACHE
:
1226 (void) fprintf(stderr
, gettext(
1227 "%s is a member (%s) of pool \"%s\"\n"),
1228 vdev
, zpool_pool_state_to_name(state
), name
);
1232 case POOL_STATE_EXPORTED
:
1235 (void) fprintf(stderr
, gettext(
1236 "use '-f' to override the following error:\n"
1237 "%s is a member of exported pool \"%s\"\n"),
1242 case POOL_STATE_POTENTIALLY_ACTIVE
:
1245 (void) fprintf(stderr
, gettext(
1246 "use '-f' to override the following error:\n"
1247 "%s is a member of potentially active pool \"%s\"\n"),
1252 case POOL_STATE_DESTROYED
:
1253 /* inuse should never be set for a destroyed pool */
1259 ret
= zpool_clear_label(fd
);
1261 (void) fprintf(stderr
,
1262 gettext("failed to clear label for %s\n"), vdev
);
1273 * zpool create [-fnd] [-o property=value] ...
1274 * [-O file-system-property=value] ...
1275 * [-R root] [-m mountpoint] <pool> <dev> ...
1277 * -f Force creation, even if devices appear in use
1278 * -n Do not create the pool, but display the resulting layout if it
1279 * were to be created.
1280 * -R Create a pool under an alternate root
1281 * -m Set default mountpoint for the root dataset. By default it's
1283 * -o Set property=value.
1284 * -o Set feature@feature=enabled|disabled.
1285 * -d Don't automatically enable all supported pool features
1286 * (individual features can be enabled with -o).
1287 * -O Set fsproperty=value in the pool's root file system
1289 * Creates the named pool according to the given vdev specification. The
1290 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1291 * Once we get the nvlist back from make_root_vdev(), we either print out the
1292 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1295 zpool_do_create(int argc
, char **argv
)
1297 boolean_t force
= B_FALSE
;
1298 boolean_t dryrun
= B_FALSE
;
1299 boolean_t enable_all_pool_feat
= B_TRUE
;
1301 nvlist_t
*nvroot
= NULL
;
1305 char *altroot
= NULL
;
1306 char *mountpoint
= NULL
;
1307 nvlist_t
*fsprops
= NULL
;
1308 nvlist_t
*props
= NULL
;
1312 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1321 enable_all_pool_feat
= B_FALSE
;
1325 if (add_prop_list(zpool_prop_to_name(
1326 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1328 if (add_prop_list_default(zpool_prop_to_name(
1329 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1333 /* Equivalent to -O mountpoint=optarg */
1334 mountpoint
= optarg
;
1337 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1338 (void) fprintf(stderr
, gettext("missing "
1339 "'=' for -o option\n"));
1345 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1349 * If the user is creating a pool that doesn't support
1350 * feature flags, don't enable any features.
1352 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1356 ver
= strtoull(propval
, &end
, 10);
1358 ver
< SPA_VERSION_FEATURES
) {
1359 enable_all_pool_feat
= B_FALSE
;
1362 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1366 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1367 (void) fprintf(stderr
, gettext("missing "
1368 "'=' for -O option\n"));
1375 * Mountpoints are checked and then added later.
1376 * Uniquely among properties, they can be specified
1377 * more than once, to avoid conflict with -m.
1379 if (0 == strcmp(optarg
,
1380 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1381 mountpoint
= propval
;
1382 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1389 * Sanity check temporary pool name.
1391 if (strchr(optarg
, '/') != NULL
) {
1392 (void) fprintf(stderr
, gettext("cannot create "
1393 "'%s': invalid character '/' in temporary "
1395 (void) fprintf(stderr
, gettext("use 'zfs "
1396 "create' to create a dataset\n"));
1400 if (add_prop_list(zpool_prop_to_name(
1401 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1403 if (add_prop_list_default(zpool_prop_to_name(
1404 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1409 (void) fprintf(stderr
, gettext("missing argument for "
1410 "'%c' option\n"), optopt
);
1413 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1422 /* get pool name and check number of arguments */
1424 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1428 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1435 * As a special case, check for use of '/' in the name, and direct the
1436 * user to use 'zfs create' instead.
1438 if (strchr(poolname
, '/') != NULL
) {
1439 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1440 "character '/' in pool name\n"), poolname
);
1441 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1442 "create a dataset\n"));
1446 /* pass off to make_root_vdev for bulk processing */
1447 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1448 argc
- 1, argv
+ 1);
1452 /* make_root_vdev() allows 0 toplevel children if there are spares */
1453 if (!zfs_allocatable_devs(nvroot
)) {
1454 (void) fprintf(stderr
, gettext("invalid vdev "
1455 "specification: at least one toplevel vdev must be "
1460 if (altroot
!= NULL
&& altroot
[0] != '/') {
1461 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1462 "must be an absolute path\n"), altroot
);
1467 * Check the validity of the mountpoint and direct the user to use the
1468 * '-m' mountpoint option if it looks like its in use.
1470 if (mountpoint
== NULL
||
1471 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1472 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1473 char buf
[MAXPATHLEN
];
1476 if (mountpoint
&& mountpoint
[0] != '/') {
1477 (void) fprintf(stderr
, gettext("invalid mountpoint "
1478 "'%s': must be an absolute path, 'legacy', or "
1479 "'none'\n"), mountpoint
);
1483 if (mountpoint
== NULL
) {
1484 if (altroot
!= NULL
)
1485 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1488 (void) snprintf(buf
, sizeof (buf
), "/%s",
1491 if (altroot
!= NULL
)
1492 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1493 altroot
, mountpoint
);
1495 (void) snprintf(buf
, sizeof (buf
), "%s",
1499 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1500 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1501 "%s\n"), buf
, strerror(errno
));
1502 (void) fprintf(stderr
, gettext("use '-m' "
1503 "option to provide a different default\n"));
1508 while (count
< 3 && readdir(dirp
) != NULL
)
1510 (void) closedir(dirp
);
1513 (void) fprintf(stderr
, gettext("mountpoint "
1514 "'%s' exists and is not empty\n"), buf
);
1515 (void) fprintf(stderr
, gettext("use '-m' "
1516 "option to provide a "
1517 "different default\n"));
1524 * Now that the mountpoint's validity has been checked, ensure that
1525 * the property is set appropriately prior to creating the pool.
1527 if (mountpoint
!= NULL
) {
1528 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1529 mountpoint
, &fsprops
, B_FALSE
);
1537 * For a dry run invocation, print out a basic message and run
1538 * through all the vdevs in the list and print out in an
1539 * appropriate hierarchy.
1541 (void) printf(gettext("would create '%s' with the "
1542 "following layout:\n\n"), poolname
);
1544 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1545 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1546 VDEV_ALLOC_BIAS_DEDUP
, 0);
1547 print_vdev_tree(NULL
, "special", nvroot
, 0,
1548 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1549 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1550 VDEV_ALLOC_BIAS_LOG
, 0);
1555 * Hand off to libzfs.
1558 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1559 char propname
[MAXPATHLEN
];
1561 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1563 (void) snprintf(propname
, sizeof (propname
),
1564 "feature@%s", feat
->fi_uname
);
1567 * Only features contained in props will be enabled:
1568 * remove from the nvlist every ZFS_FEATURE_DISABLED
1569 * value and add every missing ZFS_FEATURE_ENABLED if
1570 * enable_all_pool_feat is set.
1572 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1573 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1574 (void) nvlist_remove_all(props
,
1576 } else if (enable_all_pool_feat
) {
1577 ret
= add_prop_list(propname
,
1578 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1585 if (zpool_create(g_zfs
, poolname
,
1586 nvroot
, props
, fsprops
) == 0) {
1587 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1588 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1590 if (zfs_mount(pool
, NULL
, 0) == 0) {
1591 ret
= zfs_shareall(pool
);
1592 zfs_commit_all_shares();
1596 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1597 (void) fprintf(stderr
, gettext("pool name may have "
1603 nvlist_free(nvroot
);
1604 nvlist_free(fsprops
);
1608 nvlist_free(fsprops
);
1615 * zpool destroy <pool>
1617 * -f Forcefully unmount any datasets
1619 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1622 zpool_do_destroy(int argc
, char **argv
)
1624 boolean_t force
= B_FALSE
;
1627 zpool_handle_t
*zhp
;
1631 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1646 /* check arguments */
1648 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1652 (void) fprintf(stderr
, gettext("too many arguments\n"));
1658 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1660 * As a special case, check for use of '/' in the name, and
1661 * direct the user to use 'zfs destroy' instead.
1663 if (strchr(pool
, '/') != NULL
)
1664 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1665 "destroy a dataset\n"));
1669 if (zpool_disable_datasets(zhp
, force
) != 0) {
1670 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1671 "could not unmount datasets\n"), zpool_get_name(zhp
));
1676 /* The history must be logged as part of the export */
1677 log_history
= B_FALSE
;
1679 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1686 typedef struct export_cbdata
{
1688 boolean_t hardforce
;
1695 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1697 export_cbdata_t
*cb
= data
;
1699 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1702 /* The history must be logged as part of the export */
1703 log_history
= B_FALSE
;
1705 if (cb
->hardforce
) {
1706 if (zpool_export_force(zhp
, history_str
) != 0)
1708 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1716 * zpool export [-f] <pool> ...
1718 * -a Export all pools
1719 * -f Forcefully unmount datasets
1721 * Export the given pools. By default, the command will attempt to cleanly
1722 * unmount any active datasets within the pool. If the '-f' flag is specified,
1723 * then the datasets will be forcefully unmounted.
1726 zpool_do_export(int argc
, char **argv
)
1729 boolean_t do_all
= B_FALSE
;
1730 boolean_t force
= B_FALSE
;
1731 boolean_t hardforce
= B_FALSE
;
1735 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1747 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1754 cb
.hardforce
= hardforce
;
1760 (void) fprintf(stderr
, gettext("too many arguments\n"));
1764 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1765 zpool_export_one
, &cb
));
1768 /* check arguments */
1770 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1774 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1780 * Given a vdev configuration, determine the maximum width needed for the device
1784 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1792 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1793 if (strlen(name
) + depth
> max
)
1794 max
= strlen(name
) + depth
;
1798 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1799 &child
, &children
) == 0) {
1800 for (c
= 0; c
< children
; c
++)
1801 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1802 max
, name_flags
)) > max
)
1806 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1807 &child
, &children
) == 0) {
1808 for (c
= 0; c
< children
; c
++)
1809 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1810 max
, name_flags
)) > max
)
1814 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1815 &child
, &children
) == 0) {
1816 for (c
= 0; c
< children
; c
++)
1817 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1818 max
, name_flags
)) > max
)
1825 typedef struct spare_cbdata
{
1827 zpool_handle_t
*cb_zhp
;
1831 find_vdev(nvlist_t
*nv
, uint64_t search
)
1837 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1841 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1842 &child
, &children
) == 0) {
1843 for (c
= 0; c
< children
; c
++)
1844 if (find_vdev(child
[c
], search
))
1852 find_spare(zpool_handle_t
*zhp
, void *data
)
1854 spare_cbdata_t
*cbp
= data
;
1855 nvlist_t
*config
, *nvroot
;
1857 config
= zpool_get_config(zhp
, NULL
);
1858 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1861 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1870 typedef struct status_cbdata
{
1874 boolean_t cb_allpools
;
1875 boolean_t cb_verbose
;
1876 boolean_t cb_literal
;
1877 boolean_t cb_explain
;
1879 boolean_t cb_dedup_stats
;
1880 boolean_t cb_print_status
;
1881 boolean_t cb_print_slow_ios
;
1882 boolean_t cb_print_vdev_init
;
1883 boolean_t cb_print_vdev_trim
;
1884 vdev_cmd_data_list_t
*vcdl
;
1887 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1889 is_blank_str(char *str
)
1891 while (str
!= NULL
&& *str
!= '\0') {
1899 /* Print command output lines for specific vdev in a specific pool */
1901 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1903 vdev_cmd_data_t
*data
;
1907 for (i
= 0; i
< vcdl
->count
; i
++) {
1908 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1909 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1910 /* Not the vdev we're looking for */
1914 data
= &vcdl
->data
[i
];
1915 /* Print out all the output values for this vdev */
1916 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1918 /* Does this vdev have values for this column? */
1919 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1920 if (strcmp(data
->cols
[k
],
1921 vcdl
->uniq_cols
[j
]) == 0) {
1922 /* yes it does, record the value */
1923 val
= data
->lines
[k
];
1928 * Mark empty values with dashes to make output
1931 if (is_blank_str(val
))
1934 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1935 if (j
< vcdl
->uniq_cols_cnt
- 1)
1939 /* Print out any values that aren't in a column at the end */
1940 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1941 /* Did we have any columns? If so print a spacer. */
1942 if (vcdl
->uniq_cols_cnt
> 0)
1945 val
= data
->lines
[j
];
1946 printf("%s", val
? val
: "");
1953 * Print vdev initialization status for leaves
1956 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
1959 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
1960 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
1961 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
1962 !vs
->vs_scan_removing
) {
1965 struct tm zaction_ts
;
1967 time_t t
= vs
->vs_initialize_action_time
;
1968 int initialize_pct
= 100;
1969 if (vs
->vs_initialize_state
!=
1970 VDEV_INITIALIZE_COMPLETE
) {
1971 initialize_pct
= (vs
->vs_initialize_bytes_done
*
1972 100 / (vs
->vs_initialize_bytes_est
+ 1));
1975 (void) localtime_r(&t
, &zaction_ts
);
1976 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
1978 switch (vs
->vs_initialize_state
) {
1979 case VDEV_INITIALIZE_SUSPENDED
:
1980 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1981 gettext("suspended, started at"), tbuf
);
1983 case VDEV_INITIALIZE_ACTIVE
:
1984 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1985 gettext("started at"), tbuf
);
1987 case VDEV_INITIALIZE_COMPLETE
:
1988 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1989 gettext("completed at"), tbuf
);
1993 (void) printf(gettext(" (%d%% initialized%s)"),
1994 initialize_pct
, zbuf
);
1996 (void) printf(gettext(" (uninitialized)"));
1998 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
1999 (void) printf(gettext(" (initializing)"));
2004 * Print vdev TRIM status for leaves
2007 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2010 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2011 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2012 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2013 !vs
->vs_scan_removing
) {
2016 struct tm zaction_ts
;
2018 time_t t
= vs
->vs_trim_action_time
;
2020 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2021 trim_pct
= (vs
->vs_trim_bytes_done
*
2022 100 / (vs
->vs_trim_bytes_est
+ 1));
2025 (void) localtime_r(&t
, &zaction_ts
);
2026 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2028 switch (vs
->vs_trim_state
) {
2029 case VDEV_TRIM_SUSPENDED
:
2030 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2031 gettext("suspended, started at"), tbuf
);
2033 case VDEV_TRIM_ACTIVE
:
2034 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2035 gettext("started at"), tbuf
);
2037 case VDEV_TRIM_COMPLETE
:
2038 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2039 gettext("completed at"), tbuf
);
2043 (void) printf(gettext(" (%d%% trimmed%s)"),
2045 } else if (vs
->vs_trim_notsup
) {
2046 (void) printf(gettext(" (trim unsupported)"));
2048 (void) printf(gettext(" (untrimmed)"));
2050 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2051 (void) printf(gettext(" (trimming)"));
2056 * Return the color associated with a health string. This includes returning
2057 * NULL for no color change.
2060 health_str_to_color(const char *health
)
2062 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2063 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2064 strcmp(health
, gettext("UNAVAIL")) == 0) {
2068 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2069 strcmp(health
, gettext("DEGRADED")) == 0 ||
2070 strcmp(health
, gettext("REMOVED")) == 0) {
2071 return (ANSI_YELLOW
);
2078 * Print out configuration state as requested by status_callback.
2081 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2082 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2084 nvlist_t
**child
, *root
;
2085 uint_t c
, i
, vsc
, children
;
2086 pool_scan_stat_t
*ps
= NULL
;
2088 char rbuf
[6], wbuf
[6], cbuf
[6];
2090 uint64_t notpresent
;
2091 spare_cbdata_t spare_cb
;
2095 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2097 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2098 &child
, &children
) != 0)
2101 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2102 (uint64_t **)&vs
, &vsc
) == 0);
2104 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2106 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2109 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2113 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2116 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2117 state
= gettext("INUSE");
2118 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2119 state
= gettext("AVAIL");
2122 printf_color(health_str_to_color(state
),
2123 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2127 if (vs
->vs_read_errors
)
2130 if (vs
->vs_write_errors
)
2133 if (vs
->vs_checksum_errors
)
2136 if (cb
->cb_literal
) {
2138 printf_color(rcolor
, "%5llu",
2139 (u_longlong_t
)vs
->vs_read_errors
);
2141 printf_color(wcolor
, "%5llu",
2142 (u_longlong_t
)vs
->vs_write_errors
);
2144 printf_color(ccolor
, "%5llu",
2145 (u_longlong_t
)vs
->vs_checksum_errors
);
2147 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2148 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2149 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2152 printf_color(rcolor
, "%5s", rbuf
);
2154 printf_color(wcolor
, "%5s", wbuf
);
2156 printf_color(ccolor
, "%5s", cbuf
);
2158 if (cb
->cb_print_slow_ios
) {
2159 if (children
== 0) {
2160 /* Only leafs vdevs have slow IOs */
2161 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2164 snprintf(rbuf
, sizeof (rbuf
), "-");
2168 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2170 printf(" %5s", rbuf
);
2174 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2175 ¬present
) == 0) {
2176 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2177 (void) printf(" %s %s", gettext("was"), path
);
2178 } else if (vs
->vs_aux
!= 0) {
2180 color_start(ANSI_RED
);
2181 switch (vs
->vs_aux
) {
2182 case VDEV_AUX_OPEN_FAILED
:
2183 (void) printf(gettext("cannot open"));
2186 case VDEV_AUX_BAD_GUID_SUM
:
2187 (void) printf(gettext("missing device"));
2190 case VDEV_AUX_NO_REPLICAS
:
2191 (void) printf(gettext("insufficient replicas"));
2194 case VDEV_AUX_VERSION_NEWER
:
2195 (void) printf(gettext("newer version"));
2198 case VDEV_AUX_UNSUP_FEAT
:
2199 (void) printf(gettext("unsupported feature(s)"));
2202 case VDEV_AUX_ASHIFT_TOO_BIG
:
2203 (void) printf(gettext("unsupported minimum blocksize"));
2206 case VDEV_AUX_SPARED
:
2207 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2208 &spare_cb
.cb_guid
) == 0);
2209 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2210 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2211 zpool_get_name(zhp
)) == 0)
2212 (void) printf(gettext("currently in "
2215 (void) printf(gettext("in use by "
2217 zpool_get_name(spare_cb
.cb_zhp
));
2218 zpool_close(spare_cb
.cb_zhp
);
2220 (void) printf(gettext("currently in use"));
2224 case VDEV_AUX_ERR_EXCEEDED
:
2225 (void) printf(gettext("too many errors"));
2228 case VDEV_AUX_IO_FAILURE
:
2229 (void) printf(gettext("experienced I/O failures"));
2232 case VDEV_AUX_BAD_LOG
:
2233 (void) printf(gettext("bad intent log"));
2236 case VDEV_AUX_EXTERNAL
:
2237 (void) printf(gettext("external device fault"));
2240 case VDEV_AUX_SPLIT_POOL
:
2241 (void) printf(gettext("split into new pool"));
2244 case VDEV_AUX_ACTIVE
:
2245 (void) printf(gettext("currently in use"));
2248 case VDEV_AUX_CHILDREN_OFFLINE
:
2249 (void) printf(gettext("all children offline"));
2253 (void) printf(gettext("corrupted data"));
2257 } else if (children
== 0 && !isspare
&&
2258 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2259 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2260 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2262 gettext(" block size: %dB configured, %dB native"),
2263 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2266 /* The root vdev has the scrub/resilver stats */
2267 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2268 ZPOOL_CONFIG_VDEV_TREE
);
2269 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2270 (uint64_t **)&ps
, &c
);
2272 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2273 if (vs
->vs_scan_processed
!= 0) {
2274 (void) printf(gettext(" (%s)"),
2275 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2276 "resilvering" : "repairing");
2277 } else if (vs
->vs_resilver_deferred
) {
2278 (void) printf(gettext(" (awaiting resilver)"));
2282 /* The top-level vdevs have the rebuild stats */
2283 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2285 if (vs
->vs_rebuild_processed
!= 0) {
2286 (void) printf(gettext(" (resilvering)"));
2290 if (cb
->vcdl
!= NULL
) {
2291 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2293 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2297 /* Display vdev initialization and trim status for leaves. */
2298 if (children
== 0) {
2299 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2300 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2303 (void) printf("\n");
2305 for (c
= 0; c
< children
; c
++) {
2306 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2308 /* Don't print logs or holes here */
2309 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2311 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2313 if (islog
|| ishole
)
2315 /* Only print normal classes here */
2316 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2319 /* Provide vdev_rebuild_stats to children if available */
2321 (void) nvlist_lookup_uint64_array(nv
,
2322 ZPOOL_CONFIG_REBUILD_STATS
,
2323 (uint64_t **)&vrs
, &i
);
2326 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2327 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2328 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2335 * Print the configuration of an exported pool. Iterate over all vdevs in the
2336 * pool, printing out the name and status for each one.
2339 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2347 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2348 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2349 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2352 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2353 (uint64_t **)&vs
, &c
) == 0);
2355 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2356 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2358 if (vs
->vs_aux
!= 0) {
2361 switch (vs
->vs_aux
) {
2362 case VDEV_AUX_OPEN_FAILED
:
2363 (void) printf(gettext("cannot open"));
2366 case VDEV_AUX_BAD_GUID_SUM
:
2367 (void) printf(gettext("missing device"));
2370 case VDEV_AUX_NO_REPLICAS
:
2371 (void) printf(gettext("insufficient replicas"));
2374 case VDEV_AUX_VERSION_NEWER
:
2375 (void) printf(gettext("newer version"));
2378 case VDEV_AUX_UNSUP_FEAT
:
2379 (void) printf(gettext("unsupported feature(s)"));
2382 case VDEV_AUX_ERR_EXCEEDED
:
2383 (void) printf(gettext("too many errors"));
2386 case VDEV_AUX_ACTIVE
:
2387 (void) printf(gettext("currently in use"));
2390 case VDEV_AUX_CHILDREN_OFFLINE
:
2391 (void) printf(gettext("all children offline"));
2395 (void) printf(gettext("corrupted data"));
2399 (void) printf("\n");
2401 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2402 &child
, &children
) != 0)
2405 for (c
= 0; c
< children
; c
++) {
2406 uint64_t is_log
= B_FALSE
;
2408 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2412 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2415 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2416 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2417 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2421 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2422 &child
, &children
) == 0) {
2423 (void) printf(gettext("\tcache\n"));
2424 for (c
= 0; c
< children
; c
++) {
2425 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2427 (void) printf("\t %s\n", vname
);
2432 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2433 &child
, &children
) == 0) {
2434 (void) printf(gettext("\tspares\n"));
2435 for (c
= 0; c
< children
; c
++) {
2436 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2438 (void) printf("\t %s\n", vname
);
2445 * Print specialized class vdevs.
2447 * These are recorded as top level vdevs in the main pool child array
2448 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2449 * print_status_config() or print_import_config() to print the top level
2450 * class vdevs then any of their children (eg mirrored slogs) are printed
2451 * recursively - which works because only the top level vdev is marked.
2454 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2459 boolean_t printed
= B_FALSE
;
2461 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2463 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2467 for (c
= 0; c
< children
; c
++) {
2468 uint64_t is_log
= B_FALSE
;
2472 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2476 bias
= VDEV_ALLOC_CLASS_LOGS
;
2478 (void) nvlist_lookup_string(child
[c
],
2479 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2480 (void) nvlist_lookup_string(child
[c
],
2481 ZPOOL_CONFIG_TYPE
, &type
);
2484 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2486 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2490 (void) printf("\t%s\t\n", gettext(class));
2494 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2495 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2496 if (cb
->cb_print_status
)
2497 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2500 print_import_config(cb
, name
, child
[c
], 2);
2506 * Display the status for the given pool.
2509 show_import(nvlist_t
*config
)
2511 uint64_t pool_state
;
2515 uint64_t hostid
= 0;
2517 char *hostname
= "unknown";
2518 nvlist_t
*nvroot
, *nvinfo
;
2519 zpool_status_t reason
;
2520 zpool_errata_t errata
;
2524 status_cbdata_t cb
= { 0 };
2526 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2528 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2530 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2532 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2535 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2536 (uint64_t **)&vs
, &vsc
) == 0);
2537 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2539 reason
= zpool_import_status(config
, &msgid
, &errata
);
2541 (void) printf(gettext(" pool: %s\n"), name
);
2542 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2543 (void) printf(gettext(" state: %s"), health
);
2544 if (pool_state
== POOL_STATE_DESTROYED
)
2545 (void) printf(gettext(" (DESTROYED)"));
2546 (void) printf("\n");
2549 case ZPOOL_STATUS_MISSING_DEV_R
:
2550 case ZPOOL_STATUS_MISSING_DEV_NR
:
2551 case ZPOOL_STATUS_BAD_GUID_SUM
:
2552 printf_color(ANSI_BOLD
, gettext("status: "));
2553 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2554 "missing from the system.\n"));
2557 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2558 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2559 printf_color(ANSI_BOLD
, gettext("status: "));
2560 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2561 " corrupted data.\n"));
2564 case ZPOOL_STATUS_CORRUPT_DATA
:
2566 gettext(" status: The pool data is corrupted.\n"));
2569 case ZPOOL_STATUS_OFFLINE_DEV
:
2570 printf_color(ANSI_BOLD
, gettext("status: "));
2571 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2572 "are offlined.\n"));
2575 case ZPOOL_STATUS_CORRUPT_POOL
:
2576 printf_color(ANSI_BOLD
, gettext("status: "));
2577 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2581 case ZPOOL_STATUS_VERSION_OLDER
:
2582 printf_color(ANSI_BOLD
, gettext("status: "));
2583 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2584 "a legacy on-disk version.\n"));
2587 case ZPOOL_STATUS_VERSION_NEWER
:
2588 printf_color(ANSI_BOLD
, gettext("status: "));
2589 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2590 "an incompatible version.\n"));
2593 case ZPOOL_STATUS_FEAT_DISABLED
:
2594 printf_color(ANSI_BOLD
, gettext("status: "));
2595 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
2596 "not enabled on the pool.\n"));
2599 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2600 printf_color(ANSI_BOLD
, gettext("status: "));
2601 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2602 "feature(s) not supported on this system:\n"));
2603 color_start(ANSI_YELLOW
);
2604 zpool_print_unsup_feat(config
);
2608 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2609 printf_color(ANSI_BOLD
, gettext("status: "));
2610 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2611 "accessed in read-only mode on this system. It\n\tcannot be"
2612 " accessed in read-write mode because it uses the "
2613 "following\n\tfeature(s) not supported on this system:\n"));
2614 color_start(ANSI_YELLOW
);
2615 zpool_print_unsup_feat(config
);
2619 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2620 printf_color(ANSI_BOLD
, gettext("status: "));
2621 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2622 "imported by another system.\n"));
2625 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2626 printf_color(ANSI_BOLD
, gettext("status: "));
2627 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2628 "multihost property on. It cannot\n\tbe safely imported "
2629 "when the system hostid is not set.\n"));
2632 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2633 printf_color(ANSI_BOLD
, gettext("status: "));
2634 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2635 "by another system.\n"));
2638 case ZPOOL_STATUS_FAULTED_DEV_R
:
2639 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2640 printf_color(ANSI_BOLD
, gettext("status: "));
2641 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2645 case ZPOOL_STATUS_BAD_LOG
:
2646 printf_color(ANSI_BOLD
, gettext("status: "));
2647 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2651 case ZPOOL_STATUS_RESILVERING
:
2652 case ZPOOL_STATUS_REBUILDING
:
2653 printf_color(ANSI_BOLD
, gettext("status: "));
2654 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2655 "being resilvered.\n"));
2658 case ZPOOL_STATUS_ERRATA
:
2659 printf_color(ANSI_BOLD
, gettext("status: "));
2660 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2664 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2665 printf_color(ANSI_BOLD
, gettext("status: "));
2666 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2667 "configured to use a non-native block size.\n"
2668 "\tExpect reduced performance.\n"));
2673 * No other status can be seen when importing pools.
2675 assert(reason
== ZPOOL_STATUS_OK
);
2679 * Print out an action according to the overall state of the pool.
2681 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2682 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2683 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2684 (void) printf(gettext(" action: The pool can be "
2685 "imported using its name or numeric identifier, "
2686 "though\n\tsome features will not be available "
2687 "without an explicit 'zpool upgrade'.\n"));
2688 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2689 (void) printf(gettext(" action: The pool can be "
2690 "imported using its name or numeric "
2691 "identifier and\n\tthe '-f' flag.\n"));
2692 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2694 case ZPOOL_ERRATA_NONE
:
2697 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2698 (void) printf(gettext(" action: The pool can "
2699 "be imported using its name or numeric "
2700 "identifier,\n\thowever there is a compat"
2701 "ibility issue which should be corrected"
2702 "\n\tby running 'zpool scrub'\n"));
2705 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2706 (void) printf(gettext(" action: The pool can"
2707 "not be imported with this version of ZFS "
2708 "due to\n\tan active asynchronous destroy. "
2709 "Revert to an earlier version\n\tand "
2710 "allow the destroy to complete before "
2714 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2715 (void) printf(gettext(" action: Existing "
2716 "encrypted datasets contain an on-disk "
2717 "incompatibility, which\n\tneeds to be "
2718 "corrected. Backup these datasets to new "
2719 "encrypted datasets\n\tand destroy the "
2723 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2724 (void) printf(gettext(" action: Existing "
2725 "encrypted snapshots and bookmarks contain "
2726 "an on-disk\n\tincompatibility. This may "
2727 "cause on-disk corruption if they are used"
2728 "\n\twith 'zfs recv'. To correct the "
2729 "issue, enable the bookmark_v2 feature.\n\t"
2730 "No additional action is needed if there "
2731 "are no encrypted snapshots or\n\t"
2732 "bookmarks. If preserving the encrypted "
2733 "snapshots and bookmarks is\n\trequired, "
2734 "use a non-raw send to backup and restore "
2735 "them. Alternately,\n\tthey may be removed"
2736 " to resolve the incompatibility.\n"));
2740 * All errata must contain an action message.
2745 (void) printf(gettext(" action: The pool can be "
2746 "imported using its name or numeric "
2749 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2750 (void) printf(gettext(" action: The pool can be imported "
2751 "despite missing or damaged devices. The\n\tfault "
2752 "tolerance of the pool may be compromised if imported.\n"));
2755 case ZPOOL_STATUS_VERSION_NEWER
:
2756 (void) printf(gettext(" action: The pool cannot be "
2757 "imported. Access the pool on a system running "
2758 "newer\n\tsoftware, or recreate the pool from "
2761 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2762 printf_color(ANSI_BOLD
, gettext("action: "));
2763 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2764 "imported. Access the pool on a system that "
2765 "supports\n\tthe required feature(s), or recreate "
2766 "the pool from backup.\n"));
2768 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2769 printf_color(ANSI_BOLD
, gettext("action: "));
2770 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2771 "imported in read-write mode. Import the pool "
2773 "\t\"-o readonly=on\", access the pool on a system "
2774 "that supports the\n\trequired feature(s), or "
2775 "recreate the pool from backup.\n"));
2777 case ZPOOL_STATUS_MISSING_DEV_R
:
2778 case ZPOOL_STATUS_MISSING_DEV_NR
:
2779 case ZPOOL_STATUS_BAD_GUID_SUM
:
2780 (void) printf(gettext(" action: The pool cannot be "
2781 "imported. Attach the missing\n\tdevices and try "
2784 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2785 VERIFY0(nvlist_lookup_nvlist(config
,
2786 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2788 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2789 hostname
= fnvlist_lookup_string(nvinfo
,
2790 ZPOOL_CONFIG_MMP_HOSTNAME
);
2792 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2793 hostid
= fnvlist_lookup_uint64(nvinfo
,
2794 ZPOOL_CONFIG_MMP_HOSTID
);
2796 (void) printf(gettext(" action: The pool must be "
2797 "exported from %s (hostid=%lx)\n\tbefore it "
2798 "can be safely imported.\n"), hostname
,
2799 (unsigned long) hostid
);
2801 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2802 (void) printf(gettext(" action: Set a unique system "
2803 "hostid with the zgenhostid(8) command.\n"));
2806 (void) printf(gettext(" action: The pool cannot be "
2807 "imported due to damaged devices or data.\n"));
2811 /* Print the comment attached to the pool. */
2812 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2813 (void) printf(gettext("comment: %s\n"), comment
);
2816 * If the state is "closed" or "can't open", and the aux state
2817 * is "corrupt data":
2819 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2820 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2821 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2822 if (pool_state
== POOL_STATE_DESTROYED
)
2823 (void) printf(gettext("\tThe pool was destroyed, "
2824 "but can be imported using the '-Df' flags.\n"));
2825 else if (pool_state
!= POOL_STATE_EXPORTED
)
2826 (void) printf(gettext("\tThe pool may be active on "
2827 "another system, but can be imported using\n\t"
2828 "the '-f' flag.\n"));
2831 if (msgid
!= NULL
) {
2832 (void) printf(gettext(
2833 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
2837 (void) printf(gettext(" config:\n\n"));
2839 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2841 if (cb
.cb_namewidth
< 10)
2842 cb
.cb_namewidth
= 10;
2844 print_import_config(&cb
, name
, nvroot
, 0);
2846 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
2847 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
2848 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
2850 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2851 (void) printf(gettext("\n\tAdditional devices are known to "
2852 "be part of this pool, though their\n\texact "
2853 "configuration cannot be determined.\n"));
2858 zfs_force_import_required(nvlist_t
*config
)
2861 uint64_t hostid
= 0;
2864 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2865 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2867 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2870 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2871 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2872 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2873 ZPOOL_CONFIG_MMP_STATE
);
2875 if (mmp_state
!= MMP_STATE_INACTIVE
)
2883 * Perform the import for the given configuration. This passes the heavy
2884 * lifting off to zpool_import_props(), and then mounts the datasets contained
2888 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2889 nvlist_t
*props
, int flags
)
2892 zpool_handle_t
*zhp
;
2896 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2897 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2899 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2900 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2901 "is formatted using an unsupported ZFS version\n"), name
);
2903 } else if (zfs_force_import_required(config
) &&
2904 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2905 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2908 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2909 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2910 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2911 ZPOOL_CONFIG_MMP_STATE
);
2913 if (mmp_state
== MMP_STATE_ACTIVE
) {
2914 char *hostname
= "<unknown>";
2915 uint64_t hostid
= 0;
2917 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2918 hostname
= fnvlist_lookup_string(nvinfo
,
2919 ZPOOL_CONFIG_MMP_HOSTNAME
);
2921 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2922 hostid
= fnvlist_lookup_uint64(nvinfo
,
2923 ZPOOL_CONFIG_MMP_HOSTID
);
2925 (void) fprintf(stderr
, gettext("cannot import '%s': "
2926 "pool is imported on %s (hostid: "
2927 "0x%lx)\nExport the pool on the other system, "
2928 "then run 'zpool import'.\n"),
2929 name
, hostname
, (unsigned long) hostid
);
2930 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2931 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2932 "pool has the multihost property on and the\n"
2933 "system's hostid is not set. Set a unique hostid "
2934 "with the zgenhostid(8) command.\n"), name
);
2936 char *hostname
= "<unknown>";
2937 uint64_t timestamp
= 0;
2938 uint64_t hostid
= 0;
2940 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2941 hostname
= fnvlist_lookup_string(config
,
2942 ZPOOL_CONFIG_HOSTNAME
);
2944 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2945 timestamp
= fnvlist_lookup_uint64(config
,
2946 ZPOOL_CONFIG_TIMESTAMP
);
2948 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2949 hostid
= fnvlist_lookup_uint64(config
,
2950 ZPOOL_CONFIG_HOSTID
);
2952 (void) fprintf(stderr
, gettext("cannot import '%s': "
2953 "pool was previously in use from another system.\n"
2954 "Last accessed by %s (hostid=%lx) at %s"
2955 "The pool can be imported, use 'zpool import -f' "
2956 "to import the pool.\n"), name
, hostname
,
2957 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2963 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2966 if (newname
!= NULL
)
2967 name
= (char *)newname
;
2969 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2973 * Loading keys is best effort. We don't want to return immediately
2974 * if it fails but we do want to give the error to the caller.
2976 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2977 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2982 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2983 !(flags
& ZFS_IMPORT_ONLY
) &&
2984 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2993 typedef struct target_exists_args
{
2994 const char *poolname
;
2996 } target_exists_args_t
;
2999 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3001 target_exists_args_t
*args
= data
;
3002 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3008 if (args
->poolname
!= NULL
) {
3011 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3013 if (strcmp(pool_name
, args
->poolname
) == 0)
3018 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3020 if (pool_guid
== args
->poolguid
)
3028 * zpool checkpoint <pool>
3029 * checkpoint --discard <pool>
3031 * -d Discard the checkpoint from a checkpointed
3034 * -w Wait for discarding a checkpoint to complete.
3037 * Checkpoints the specified pool, by taking a "snapshot" of its
3038 * current state. A pool can only have one checkpoint at a time.
3041 zpool_do_checkpoint(int argc
, char **argv
)
3043 boolean_t discard
, wait
;
3045 zpool_handle_t
*zhp
;
3048 struct option long_options
[] = {
3049 {"discard", no_argument
, NULL
, 'd'},
3050 {"wait", no_argument
, NULL
, 'w'},
3056 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3065 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3071 if (wait
&& !discard
) {
3072 (void) fprintf(stderr
, gettext("--wait only valid when "
3073 "--discard also specified\n"));
3081 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3086 (void) fprintf(stderr
, gettext("too many arguments\n"));
3092 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3093 /* As a special case, check for use of '/' in the name */
3094 if (strchr(pool
, '/') != NULL
)
3095 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3096 "doesn't work on datasets. To save the state "
3097 "of a dataset from a specific point in time "
3098 "please use 'zfs snapshot'\n"));
3103 err
= (zpool_discard_checkpoint(zhp
) != 0);
3104 if (err
== 0 && wait
)
3105 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3107 err
= (zpool_checkpoint(zhp
) != 0);
3115 #define CHECKPOINT_OPT 1024
3118 * zpool import [-d dir] [-D]
3119 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3120 * [-d dir | -c cachefile] [-f] -a
3121 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3122 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
3124 * -c Read pool information from a cachefile instead of searching
3127 * -d Scan in a specific directory, other than /dev/. More than
3128 * one directory can be specified using multiple '-d' options.
3130 * -D Scan for previously destroyed pools or import all or only
3131 * specified destroyed pools.
3133 * -R Temporarily import the pool, with all mountpoints relative to
3134 * the given root. The pool will remain exported when the machine
3137 * -V Import even in the presence of faulted vdevs. This is an
3138 * intentionally undocumented option for testing purposes, and
3139 * treats the pool configuration as complete, leaving any bad
3140 * vdevs in the FAULTED state. In other words, it does verbatim
3143 * -f Force import, even if it appears that the pool is active.
3145 * -F Attempt rewind if necessary.
3147 * -n See if rewind would work, but don't actually rewind.
3149 * -N Import the pool but don't mount datasets.
3151 * -T Specify a starting txg to use for import. This option is
3152 * intentionally undocumented option for testing purposes.
3154 * -a Import all pools found.
3156 * -l Load encryption keys while importing.
3158 * -o Set property=value and/or temporary mount options (without '=').
3160 * -s Scan using the default search path, the libblkid cache will
3163 * --rewind-to-checkpoint
3164 * Import the pool and revert back to the checkpoint.
3166 * The import command scans for pools to import, and import pools based on pool
3167 * name and GUID. The pool can also be renamed as part of the import process.
3170 zpool_do_import(int argc
, char **argv
)
3172 char **searchdirs
= NULL
;
3173 char *env
, *envdup
= NULL
;
3177 nvlist_t
*pools
= NULL
;
3178 boolean_t do_all
= B_FALSE
;
3179 boolean_t do_destroyed
= B_FALSE
;
3180 char *mntopts
= NULL
;
3183 uint64_t searchguid
= 0;
3184 char *searchname
= NULL
;
3186 nvlist_t
*found_config
;
3187 nvlist_t
*policy
= NULL
;
3188 nvlist_t
*props
= NULL
;
3190 int flags
= ZFS_IMPORT_NORMAL
;
3191 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3192 boolean_t dryrun
= B_FALSE
;
3193 boolean_t do_rewind
= B_FALSE
;
3194 boolean_t xtreme_rewind
= B_FALSE
;
3195 boolean_t do_scan
= B_FALSE
;
3196 boolean_t pool_exists
= B_FALSE
;
3197 uint64_t pool_state
, txg
= -1ULL;
3198 char *cachefile
= NULL
;
3199 importargs_t idata
= { 0 };
3202 struct option long_options
[] = {
3203 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3208 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3209 long_options
, NULL
)) != -1) {
3218 if (searchdirs
== NULL
) {
3219 searchdirs
= safe_malloc(sizeof (char *));
3221 char **tmp
= safe_malloc((nsearch
+ 1) *
3223 bcopy(searchdirs
, tmp
, nsearch
*
3228 searchdirs
[nsearch
++] = optarg
;
3231 do_destroyed
= B_TRUE
;
3234 flags
|= ZFS_IMPORT_ANY_HOST
;
3240 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3243 flags
|= ZFS_IMPORT_MISSING_LOG
;
3249 flags
|= ZFS_IMPORT_ONLY
;
3252 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3255 if (add_prop_list(optarg
, propval
,
3263 if (add_prop_list(zpool_prop_to_name(
3264 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3266 if (add_prop_list_default(zpool_prop_to_name(
3267 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3274 flags
|= ZFS_IMPORT_TEMP_NAME
;
3275 if (add_prop_list_default(zpool_prop_to_name(
3276 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3282 txg
= strtoull(optarg
, &endptr
, 0);
3283 if (errno
!= 0 || *endptr
!= '\0') {
3284 (void) fprintf(stderr
,
3285 gettext("invalid txg value\n"));
3288 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3291 flags
|= ZFS_IMPORT_VERBATIM
;
3294 xtreme_rewind
= B_TRUE
;
3296 case CHECKPOINT_OPT
:
3297 flags
|= ZFS_IMPORT_CHECKPOINT
;
3300 (void) fprintf(stderr
, gettext("missing argument for "
3301 "'%c' option\n"), optopt
);
3305 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3314 if (cachefile
&& nsearch
!= 0) {
3315 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3319 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3320 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3324 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3325 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3330 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3331 (void) fprintf(stderr
,
3332 gettext("-n or -X only meaningful with -F\n"));
3336 rewind_policy
= ZPOOL_TRY_REWIND
;
3338 rewind_policy
= ZPOOL_DO_REWIND
;
3340 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3342 /* In the future, we can capture further policy and include it here */
3343 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3344 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3345 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3346 rewind_policy
) != 0)
3349 /* check argument count */
3352 (void) fprintf(stderr
, gettext("too many arguments\n"));
3357 (void) fprintf(stderr
, gettext("too many arguments\n"));
3363 * Check for the effective uid. We do this explicitly here because
3364 * otherwise any attempt to discover pools will silently fail.
3366 if (argc
== 0 && geteuid() != 0) {
3367 (void) fprintf(stderr
, gettext("cannot "
3368 "discover pools: permission denied\n"));
3369 if (searchdirs
!= NULL
)
3373 nvlist_free(policy
);
3378 * Depending on the arguments given, we do one of the following:
3380 * <none> Iterate through all pools and display information about
3383 * -a Iterate through all pools and try to import each one.
3385 * <id> Find the pool that corresponds to the given GUID/pool
3386 * name and import that one.
3388 * -D Above options applies only to destroyed pools.
3394 searchguid
= strtoull(argv
[0], &endptr
, 10);
3395 if (errno
!= 0 || *endptr
!= '\0') {
3396 searchname
= argv
[0];
3399 found_config
= NULL
;
3402 * User specified a name or guid. Ensure it's unique.
3404 target_exists_args_t search
= {searchname
, searchguid
};
3405 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3409 * Check the environment for the preferred search path.
3411 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3414 envdup
= strdup(env
);
3416 dir
= strtok(envdup
, ":");
3417 while (dir
!= NULL
) {
3418 if (searchdirs
== NULL
) {
3419 searchdirs
= safe_malloc(sizeof (char *));
3421 char **tmp
= safe_malloc((nsearch
+ 1) *
3423 bcopy(searchdirs
, tmp
, nsearch
*
3428 searchdirs
[nsearch
++] = dir
;
3429 dir
= strtok(NULL
, ":");
3433 idata
.path
= searchdirs
;
3434 idata
.paths
= nsearch
;
3435 idata
.poolname
= searchname
;
3436 idata
.guid
= searchguid
;
3437 idata
.cachefile
= cachefile
;
3438 idata
.scan
= do_scan
;
3439 idata
.policy
= policy
;
3441 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3443 if (pools
!= NULL
&& pool_exists
&&
3444 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3445 (void) fprintf(stderr
, gettext("cannot import '%s': "
3446 "a pool with that name already exists\n"),
3448 (void) fprintf(stderr
, gettext("use the form '%s "
3449 "<pool | id> <newpool>' to give it a new name\n"),
3452 } else if (pools
== NULL
&& pool_exists
) {
3453 (void) fprintf(stderr
, gettext("cannot import '%s': "
3454 "a pool with that name is already created/imported,\n"),
3456 (void) fprintf(stderr
, gettext("and no additional pools "
3457 "with that name were found\n"));
3459 } else if (pools
== NULL
) {
3461 (void) fprintf(stderr
, gettext("cannot import '%s': "
3462 "no such pool available\n"), argv
[0]);
3468 if (searchdirs
!= NULL
)
3472 nvlist_free(policy
);
3479 * At this point we have a list of import candidate configs. Even if
3480 * we were searching by pool name or guid, we still need to
3481 * post-process the list to deal with pool state and possible
3487 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3489 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3491 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3493 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3495 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3498 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3505 (void) printf("\n");
3508 err
|= do_import(config
, NULL
, mntopts
,
3511 show_import(config
);
3513 } else if (searchname
!= NULL
) {
3517 * We are searching for a pool based on name.
3519 verify(nvlist_lookup_string(config
,
3520 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3522 if (strcmp(name
, searchname
) == 0) {
3523 if (found_config
!= NULL
) {
3524 (void) fprintf(stderr
, gettext(
3525 "cannot import '%s': more than "
3526 "one matching pool\n"), searchname
);
3527 (void) fprintf(stderr
, gettext(
3528 "import by numeric ID instead\n"));
3531 found_config
= config
;
3537 * Search for a pool by guid.
3539 verify(nvlist_lookup_uint64(config
,
3540 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3542 if (guid
== searchguid
)
3543 found_config
= config
;
3548 * If we were searching for a specific pool, verify that we found a
3549 * pool, and then do the import.
3551 if (argc
!= 0 && err
== 0) {
3552 if (found_config
== NULL
) {
3553 (void) fprintf(stderr
, gettext("cannot import '%s': "
3554 "no such pool available\n"), argv
[0]);
3557 err
|= do_import(found_config
, argc
== 1 ? NULL
:
3558 argv
[1], mntopts
, props
, flags
);
3563 * If we were just looking for pools, report an error if none were
3566 if (argc
== 0 && first
)
3567 (void) fprintf(stderr
,
3568 gettext("no pools available to import\n"));
3573 nvlist_free(policy
);
3574 if (searchdirs
!= NULL
)
3579 return (err
? 1 : 0);
3583 * zpool sync [-f] [pool] ...
3585 * -f (undocumented) force uberblock (and config including zpool cache file)
3588 * Sync the specified pool(s).
3589 * Without arguments "zpool sync" will sync all pools.
3590 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3594 zpool_do_sync(int argc
, char **argv
)
3597 boolean_t force
= B_FALSE
;
3600 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3606 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3615 /* if argc == 0 we will execute zpool_sync_one on all pools */
3616 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
3621 typedef struct iostat_cbdata
{
3626 char **cb_vdev_names
; /* Only show these vdevs */
3627 unsigned int cb_vdev_names_count
;
3628 boolean_t cb_verbose
;
3629 boolean_t cb_literal
;
3630 boolean_t cb_scripted
;
3631 zpool_list_t
*cb_list
;
3632 vdev_cmd_data_list_t
*vcdl
;
3636 typedef struct name_and_columns
{
3637 const char *name
; /* Column name */
3638 unsigned int columns
; /* Center name to this number of columns */
3639 } name_and_columns_t
;
3641 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3643 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3645 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3647 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3648 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL
}},
3649 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3650 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3651 {"trimq_write", 2}, {NULL
}},
3652 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3653 {"asyncq_wait", 2}, {NULL
}},
3654 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3655 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3656 {"trim", 2}, {NULL
}},
3659 /* Shorthand - if "columns" field not set, default to 1 column */
3660 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3662 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3664 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3665 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL
}},
3666 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3667 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3668 {"pend"}, {"activ"}, {NULL
}},
3669 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3670 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL
}},
3671 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3672 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3675 static const char *histo_to_title
[] = {
3676 [IOS_L_HISTO
] = "latency",
3677 [IOS_RQ_HISTO
] = "req_size",
3681 * Return the number of labels in a null-terminated name_and_columns_t
3686 label_array_len(const name_and_columns_t
*labels
)
3690 while (labels
[i
].name
)
3697 * Return the number of strings in a null-terminated string array.
3700 * const char foo[] = {"bar", "baz", NULL}
3705 str_array_len(const char *array
[])
3716 * Return a default column width for default/latency/queue columns. This does
3717 * not include histograms, which have their columns autosized.
3720 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3722 unsigned long column_width
= 5; /* Normal niceprint */
3723 static unsigned long widths
[] = {
3725 * Choose some sane default column sizes for printing the
3728 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3729 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3730 [IOS_QUEUES
] = 6, /* 1M queue entries */
3731 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
3732 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
3736 column_width
= widths
[type
];
3738 return (column_width
);
3742 * Print the column labels, i.e:
3744 * capacity operations bandwidth
3745 * alloc free read write read write ...
3747 * If force_column_width is set, use it for the column width. If not set, use
3748 * the default column width.
3751 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3752 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3755 int text_start
, rw_column_width
, spaces_to_end
;
3756 uint64_t flags
= cb
->cb_flags
;
3758 unsigned int column_width
= force_column_width
;
3760 /* For each bit set in flags */
3761 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3762 idx
= lowbit64(f
) - 1;
3763 if (!force_column_width
)
3764 column_width
= default_column_width(cb
, idx
);
3765 /* Print our top labels centered over "read write" label. */
3766 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3767 const char *name
= labels
[idx
][i
].name
;
3769 * We treat labels[][].columns == 0 as shorthand
3770 * for one column. It makes writing out the label
3771 * tables more concise.
3773 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3774 unsigned int slen
= strlen(name
);
3776 rw_column_width
= (column_width
* columns
) +
3777 (2 * (columns
- 1));
3779 text_start
= (int)((rw_column_width
) / columns
-
3784 printf(" "); /* Two spaces between columns */
3786 /* Space from beginning of column to label */
3787 for (s
= 0; s
< text_start
; s
++)
3792 /* Print space after label to end of column */
3793 spaces_to_end
= rw_column_width
- text_start
- slen
;
3794 if (spaces_to_end
< 0)
3797 for (s
= 0; s
< spaces_to_end
; s
++)
3805 * print_cmd_columns - Print custom column titles from -c
3807 * If the user specified the "zpool status|iostat -c" then print their custom
3808 * column titles in the header. For example, print_cmd_columns() would print
3809 * the " col1 col2" part of this:
3811 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3813 * capacity operations bandwidth
3814 * pool alloc free read write read write col1 col2
3815 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3816 * mypool 269K 1008M 0 0 107 946
3817 * mirror 269K 1008M 0 0 107 946
3818 * sdb - - 0 0 102 473 val1 val2
3819 * sdc - - 0 0 5 473 val1 val2
3820 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3823 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3826 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3828 if (vcdl
->count
== 0 || data
== NULL
)
3832 * Each vdev cmd should have the same column names unless the user did
3833 * something weird with their cmd. Just take the column names from the
3834 * first vdev and assume it works for all of them.
3836 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3839 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3842 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
3843 vcdl
->uniq_cols
[i
]);
3850 * Utility function to print out a line of dashes like:
3852 * -------------------------------- ----- ----- ----- ----- -----
3854 * ...or a dashed named-row line like:
3860 * @force_column_width If non-zero, use the value as the column width.
3861 * Otherwise use the default column widths.
3863 * @name: Print a dashed named-row line starting
3864 * with @name. Otherwise, print a regular
3868 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3872 unsigned int namewidth
;
3873 uint64_t flags
= cb
->cb_flags
;
3876 const name_and_columns_t
*labels
;
3880 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3881 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3882 } else if (cb
->cb_vdev_names_count
) {
3888 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3889 name
? strlen(name
) : 0);
3893 printf("%-*s", namewidth
, name
);
3895 for (i
= 0; i
< namewidth
; i
++)
3899 /* For each bit in flags */
3900 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3901 unsigned int column_width
;
3902 idx
= lowbit64(f
) - 1;
3903 if (force_column_width
)
3904 column_width
= force_column_width
;
3906 column_width
= default_column_width(cb
, idx
);
3908 labels
= iostat_bottom_labels
[idx
];
3909 for (i
= 0; i
< label_array_len(labels
); i
++) {
3911 printf(" %*s-", column_width
- 1, " ");
3913 printf(" %.*s", column_width
,
3914 "--------------------");
3921 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3922 unsigned int force_column_width
)
3924 print_iostat_dashes(cb
, force_column_width
, NULL
);
3928 print_iostat_separator(iostat_cbdata_t
*cb
)
3930 print_iostat_separator_impl(cb
, 0);
3934 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3935 const char *histo_vdev_name
)
3937 unsigned int namewidth
;
3940 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3941 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3942 } else if (cb
->cb_vdev_names_count
) {
3948 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3949 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3951 if (histo_vdev_name
)
3952 printf("%-*s", namewidth
, histo_vdev_name
);
3954 printf("%*s", namewidth
, "");
3957 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3960 printf("%-*s", namewidth
, title
);
3962 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3963 if (cb
->vcdl
!= NULL
)
3964 print_cmd_columns(cb
->vcdl
, 0);
3968 print_iostat_separator_impl(cb
, force_column_width
);
3970 if (cb
->vcdl
!= NULL
)
3971 print_cmd_columns(cb
->vcdl
, 1);
3977 print_iostat_header(iostat_cbdata_t
*cb
)
3979 print_iostat_header_impl(cb
, 0, NULL
);
3984 * Display a single statistic.
3987 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3988 unsigned int column_size
, boolean_t scripted
)
3992 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3995 printf("\t%s", buf
);
3997 printf(" %*s", column_size
, buf
);
4001 * Calculate the default vdev stats
4003 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4004 * stats into calcvs.
4007 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4008 vdev_stat_t
*calcvs
)
4012 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4013 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4014 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4016 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4017 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4021 * Internal representation of the extended iostats data.
4023 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4024 * or single uint64_t's. We make both look like arrays to make them easier
4025 * to process. In order to make single uint64_t's look like arrays, we set
4026 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4027 * we can just use *data and count.
4031 uint_t count
; /* Number of entries in data[] */
4032 uint64_t __data
; /* Only used when data is a single uint64_t */
4036 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4040 for (i
= 0; i
< len
; i
++)
4041 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4047 * Helper function to lookup a uint64_t array or uint64_t value and store its
4048 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4049 * it look like a one element array to make it easier to process.
4052 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4053 struct stat_array
*nva
)
4058 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4059 switch (nvpair_type(tmp
)) {
4060 case DATA_TYPE_UINT64_ARRAY
:
4061 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4063 case DATA_TYPE_UINT64
:
4064 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4065 nva
->data
= &nva
->__data
;
4069 /* Not a uint64_t */
4078 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4079 * subtract them, and return the results in a newly allocated stat_array.
4080 * You must free the returned array after you are done with it with
4081 * free_calc_stats().
4083 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4086 static struct stat_array
*
4087 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4090 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4091 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4093 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4095 /* Extract our extended stats nvlist from the main list */
4096 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4099 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4103 newnva
= safe_malloc(alloc_size
);
4104 oldnva
= safe_malloc(alloc_size
);
4105 calcnva
= safe_malloc(alloc_size
);
4107 for (j
= 0; j
< len
; j
++) {
4108 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4110 calcnva
[j
].count
= newnva
[j
].count
;
4111 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4112 calcnva
[j
].data
= safe_malloc(alloc_size
);
4113 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4116 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4118 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4119 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4128 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4131 for (i
= 0; i
< len
; i
++)
4138 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4139 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4145 enum zfs_nicenum_format format
;
4146 unsigned int buckets
;
4147 unsigned int start_bucket
;
4150 format
= ZFS_NICENUM_RAW
;
4152 format
= ZFS_NICENUM_1024
;
4154 /* All these histos are the same size, so just use nva[0].count */
4155 buckets
= nva
[0].count
;
4157 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4158 /* Start at 512 - req size should never be lower than this */
4164 for (j
= start_bucket
; j
< buckets
; j
++) {
4165 /* Print histogram bucket label */
4166 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4167 /* Ending range of this bucket */
4168 val
= (1UL << (j
+ 1)) - 1;
4169 zfs_nicetime(val
, buf
, sizeof (buf
));
4171 /* Request size (starting range of bucket) */
4173 zfs_nicenum(val
, buf
, sizeof (buf
));
4176 if (cb
->cb_scripted
)
4177 printf("%llu", (u_longlong_t
)val
);
4179 printf("%-*s", namewidth
, buf
);
4181 /* Print the values on the line */
4182 for (i
= 0; i
< len
; i
++) {
4183 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4184 column_width
, cb
->cb_scripted
);
4191 print_solid_separator(unsigned int length
)
4199 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4200 nvlist_t
*newnv
, double scale
, const char *name
)
4202 unsigned int column_width
;
4203 unsigned int namewidth
;
4204 unsigned int entire_width
;
4205 enum iostat_type type
;
4206 struct stat_array
*nva
;
4208 unsigned int names_len
;
4210 /* What type of histo are we? */
4211 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4213 /* Get NULL-terminated array of nvlist names for our histo */
4214 names
= vsx_type_to_nvlist
[type
];
4215 names_len
= str_array_len(names
); /* num of names */
4217 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4219 if (cb
->cb_literal
) {
4220 column_width
= MAX(5,
4221 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4226 namewidth
= MAX(cb
->cb_namewidth
,
4227 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4230 * Calculate the entire line width of what we're printing. The
4231 * +2 is for the two spaces between columns:
4235 /* |___| <---------- column_width */
4237 /* |__________| <--- entire_width */
4239 entire_width
= namewidth
+ (column_width
+ 2) *
4240 label_array_len(iostat_bottom_labels
[type
]);
4242 if (cb
->cb_scripted
)
4243 printf("%s\n", name
);
4245 print_iostat_header_impl(cb
, column_width
, name
);
4247 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4250 free_calc_stats(nva
, names_len
);
4251 if (!cb
->cb_scripted
)
4252 print_solid_separator(entire_width
);
4256 * Calculate the average latency of a power-of-two latency histogram
4259 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4262 uint64_t count
= 0, total
= 0;
4264 for (i
= 0; i
< buckets
; i
++) {
4266 * Our buckets are power-of-two latency ranges. Use the
4267 * midpoint latency of each bucket to calculate the average.
4275 if (histo
[i
] != 0) {
4276 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4281 /* Prevent divide by zero */
4282 return (count
== 0 ? 0 : total
/ count
);
4286 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4291 const char *names
[] = {
4292 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4293 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4294 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4295 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4296 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4297 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4298 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4299 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4300 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4301 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4302 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4303 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4306 struct stat_array
*nva
;
4308 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4309 enum zfs_nicenum_format format
;
4311 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4314 format
= ZFS_NICENUM_RAW
;
4316 format
= ZFS_NICENUM_1024
;
4318 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4319 val
= nva
[i
].data
[0];
4320 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4323 free_calc_stats(nva
, ARRAY_SIZE(names
));
4327 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4332 const char *names
[] = {
4333 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4334 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4335 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4336 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4337 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4338 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4339 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4340 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4341 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4342 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4344 struct stat_array
*nva
;
4346 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4347 enum zfs_nicenum_format format
;
4349 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4352 format
= ZFS_NICENUM_RAWTIME
;
4354 format
= ZFS_NICENUM_TIME
;
4356 /* Print our avg latencies on the line */
4357 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4358 /* Compute average latency for a latency histo */
4359 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4360 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4362 free_calc_stats(nva
, ARRAY_SIZE(names
));
4366 * Print default statistics (capacity/operations/bandwidth)
4369 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4371 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4372 enum zfs_nicenum_format format
;
4373 char na
; /* char to print for "not applicable" values */
4375 if (cb
->cb_literal
) {
4376 format
= ZFS_NICENUM_RAW
;
4379 format
= ZFS_NICENUM_1024
;
4383 /* only toplevel vdevs have capacity stats */
4384 if (vs
->vs_space
== 0) {
4385 if (cb
->cb_scripted
)
4386 printf("\t%c\t%c", na
, na
);
4388 printf(" %*c %*c", column_width
, na
, column_width
,
4391 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4393 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4394 column_width
, cb
->cb_scripted
);
4397 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4398 format
, column_width
, cb
->cb_scripted
);
4399 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4400 format
, column_width
, cb
->cb_scripted
);
4401 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4402 format
, column_width
, cb
->cb_scripted
);
4403 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4404 format
, column_width
, cb
->cb_scripted
);
4407 static const char *class_name
[] = {
4408 VDEV_ALLOC_BIAS_DEDUP
,
4409 VDEV_ALLOC_BIAS_SPECIAL
,
4410 VDEV_ALLOC_CLASS_LOGS
4414 * Print out all the statistics for the given vdev. This can either be the
4415 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4416 * is a verbose output, and we don't want to display the toplevel pool stats.
4418 * Returns the number of stat lines printed.
4421 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4422 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4424 nvlist_t
**oldchild
, **newchild
;
4425 uint_t c
, children
, oldchildren
;
4426 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4427 vdev_stat_t zerovs
= { 0 };
4434 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4437 calcvs
= safe_malloc(sizeof (*calcvs
));
4439 if (oldnv
!= NULL
) {
4440 verify(nvlist_lookup_uint64_array(oldnv
,
4441 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4446 /* Do we only want to see a specific vdev? */
4447 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
4448 /* Yes we do. Is this the vdev? */
4449 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
4451 * This is our vdev. Since it is the only vdev we
4452 * will be displaying, make depth = 0 so that it
4453 * doesn't get indented.
4460 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4461 /* Couldn't match the name */
4466 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4467 (uint64_t **)&newvs
, &c
) == 0);
4470 * Print the vdev name unless it's is a histogram. Histograms
4471 * display the vdev name in the header itself.
4473 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4474 if (cb
->cb_scripted
) {
4477 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4478 (void) printf("%*s%s", depth
, "", name
);
4480 (void) printf("%*s%s%*s", depth
, "", name
,
4481 (int)(cb
->cb_namewidth
- strlen(name
) -
4486 /* Calculate our scaling factor */
4487 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4488 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4490 * If we specify printing histograms with no time interval, then
4491 * print the histogram numbers over the entire lifetime of the
4499 scale
= (double)NANOSEC
/ tdelta
;
4502 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4503 calc_default_iostats(oldvs
, newvs
, calcvs
);
4504 print_iostat_default(calcvs
, cb
, scale
);
4506 if (cb
->cb_flags
& IOS_LATENCY_M
)
4507 print_iostat_latency(cb
, oldnv
, newnv
);
4508 if (cb
->cb_flags
& IOS_QUEUES_M
)
4509 print_iostat_queues(cb
, oldnv
, newnv
);
4510 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4512 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4515 if (cb
->vcdl
!= NULL
) {
4517 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4520 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4524 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4533 if (!cb
->cb_verbose
)
4536 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4537 &newchild
, &children
) != 0)
4541 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4542 &oldchild
, &oldchildren
) != 0)
4545 children
= MIN(oldchildren
, children
);
4549 * print normal top-level devices
4551 for (c
= 0; c
< children
; c
++) {
4552 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4554 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4557 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4560 if (ishole
|| islog
)
4563 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4566 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4568 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4569 newchild
[c
], cb
, depth
+ 2);
4574 * print all other top-level devices
4576 for (uint_t n
= 0; n
< 3; n
++) {
4577 boolean_t printed
= B_FALSE
;
4579 for (c
= 0; c
< children
; c
++) {
4580 uint64_t islog
= B_FALSE
;
4584 (void) nvlist_lookup_uint64(newchild
[c
],
4585 ZPOOL_CONFIG_IS_LOG
, &islog
);
4587 bias
= VDEV_ALLOC_CLASS_LOGS
;
4589 (void) nvlist_lookup_string(newchild
[c
],
4590 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4591 (void) nvlist_lookup_string(newchild
[c
],
4592 ZPOOL_CONFIG_TYPE
, &type
);
4594 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4596 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4600 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4601 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4602 print_iostat_dashes(cb
, 0,
4609 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4611 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4612 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4618 * Include level 2 ARC devices in iostat output
4620 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4621 &newchild
, &children
) != 0)
4625 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4626 &oldchild
, &oldchildren
) != 0)
4629 children
= MIN(oldchildren
, children
);
4633 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4634 !cb
->cb_vdev_names
) {
4635 print_iostat_dashes(cb
, 0, "cache");
4639 for (c
= 0; c
< children
; c
++) {
4640 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4642 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4643 : NULL
, newchild
[c
], cb
, depth
+ 2);
4652 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4654 iostat_cbdata_t
*cb
= data
;
4658 * If the pool has disappeared, remove it from the list and continue.
4660 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4664 pool_list_remove(cb
->cb_list
, zhp
);
4670 * Callback to print out the iostats for the given pool.
4673 print_iostat(zpool_handle_t
*zhp
, void *data
)
4675 iostat_cbdata_t
*cb
= data
;
4676 nvlist_t
*oldconfig
, *newconfig
;
4677 nvlist_t
*oldnvroot
, *newnvroot
;
4680 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4682 if (cb
->cb_iteration
== 1)
4685 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4688 if (oldconfig
== NULL
)
4691 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4694 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4696 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4697 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4698 print_iostat_separator(cb
);
4699 if (cb
->vcdl
!= NULL
) {
4700 print_cmd_columns(cb
->vcdl
, 1);
4715 if (isatty(STDOUT_FILENO
)) {
4716 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4718 columns
= ws
.ws_col
;
4727 * Return the required length of the pool/vdev name column. The minimum
4728 * allowed width and output formatting flags must be provided.
4731 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4733 nvlist_t
*config
, *nvroot
;
4734 int width
= min_width
;
4736 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4737 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4739 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4740 if (verbose
== B_FALSE
) {
4741 width
= MAX(poolname_len
, min_width
);
4743 width
= MAX(poolname_len
,
4744 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4752 * Parse the input string, get the 'interval' and 'count' value if there is one.
4755 get_interval_count(int *argcp
, char **argv
, float *iv
,
4759 unsigned long count
= 0;
4763 * Determine if the last argument is an integer or a pool name
4765 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4769 interval
= strtof(argv
[argc
- 1], &end
);
4771 if (*end
== '\0' && errno
== 0) {
4772 if (interval
== 0) {
4773 (void) fprintf(stderr
, gettext("interval "
4774 "cannot be zero\n"));
4778 * Ignore the last parameter
4783 * If this is not a valid number, just plow on. The
4784 * user will get a more informative error message later
4792 * If the last argument is also an integer, then we have both a count
4795 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4800 interval
= strtof(argv
[argc
- 1], &end
);
4802 if (*end
== '\0' && errno
== 0) {
4803 if (interval
== 0) {
4804 (void) fprintf(stderr
, gettext("interval "
4805 "cannot be zero\n"));
4810 * Ignore the last parameter
4824 get_timestamp_arg(char c
)
4827 timestamp_fmt
= UDATE
;
4829 timestamp_fmt
= DDATE
;
4835 * Return stat flags that are supported by all pools by both the module and
4836 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4837 * It will get ANDed down until only the flags that are supported on all pools
4841 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4843 uint64_t *mask
= data
;
4844 nvlist_t
*config
, *nvroot
, *nvx
;
4848 config
= zpool_get_config(zhp
, NULL
);
4849 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4852 /* Default stats are always supported, but for completeness.. */
4853 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4854 flags
|= IOS_DEFAULT_M
;
4856 /* Get our extended stats nvlist from the main list */
4857 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4860 * No extended stats; they're probably running an older
4861 * module. No big deal, we support that too.
4866 /* For each extended stat, make sure all its nvpairs are supported */
4867 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4868 if (!vsx_type_to_nvlist
[j
][0])
4871 /* Start off by assuming the flag is supported, then check */
4872 flags
|= (1ULL << j
);
4873 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4874 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4875 /* flag isn't supported */
4876 flags
= flags
& ~(1ULL << j
);
4882 *mask
= *mask
& flags
;
4887 * Return a bitmask of stats that are supported on all pools by both the module
4891 get_stat_flags(zpool_list_t
*list
)
4896 * get_stat_flags_cb() will lop off bits from "mask" until only the
4897 * flags that are supported on all pools remain.
4899 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4904 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4907 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4909 iostat_cbdata_t
*cb
= cb_data
;
4913 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4915 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4916 ret
= 1; /* match */
4923 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4926 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4928 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4932 * Check if vdevs are in a pool
4934 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4935 * return 0. If pool_name is NULL, then search all pools.
4938 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4939 iostat_cbdata_t
*cb
)
4946 if ((argc
== 0) || !*argv
)
4952 /* Temporarily hijack cb_vdev_names for a second... */
4953 tmp_name
= cb
->cb_vdev_names
;
4955 /* Go though our list of prospective vdev names */
4956 for (i
= 0; i
< argc
; i
++) {
4957 cb
->cb_vdev_names
= argv
+ i
;
4959 /* Is this name a vdev in our pools? */
4960 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4968 cb
->cb_vdev_names
= tmp_name
;
4974 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4977 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4984 * Do we have a pool named *name? If so, return 1, otherwise 0.
4989 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4992 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4994 are_all_pools(int argc
, char **argv
)
4996 if ((argc
== 0) || !*argv
)
5000 if (!is_pool(argv
[argc
]))
5007 * Helper function to print out vdev/pool names we can't resolve. Used for an
5011 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5012 iostat_cbdata_t
*cb
)
5017 for (i
= 0; i
< argc
; i
++) {
5021 str
= gettext("pool");
5022 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5023 str
= gettext("vdev in this pool");
5024 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5025 str
= gettext("vdev in another pool");
5027 str
= gettext("unknown");
5029 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5034 * Same as get_interval_count(), but with additional checks to not misinterpret
5035 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5039 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5040 unsigned long *count
, iostat_cbdata_t
*cb
)
5042 char **tmpargv
= argv
;
5043 int argc_for_interval
= 0;
5045 /* Is the last arg an interval value? Or a guid? */
5046 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
5048 * The last arg is not a guid, so it's probably an
5051 argc_for_interval
++;
5054 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
5056 * The 2nd to last arg is not a guid, so it's probably
5057 * an interval value.
5059 argc_for_interval
++;
5063 /* Point to our list of possible intervals */
5064 tmpargv
= &argv
[*argc
- argc_for_interval
];
5066 *argc
= *argc
- argc_for_interval
;
5067 get_interval_count(&argc_for_interval
, tmpargv
,
5072 * Floating point sleep(). Allows you to pass in a floating point value for
5078 struct timespec req
;
5079 req
.tv_sec
= floor(sec
);
5080 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5081 nanosleep(&req
, NULL
);
5085 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5086 * if we were unable to determine its size.
5089 terminal_height(void)
5093 if (isatty(STDOUT_FILENO
) == 0)
5096 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5097 return (win
.ws_row
);
5103 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5106 * name: Short name of the script ('iostat').
5107 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5110 print_zpool_script_help(char *name
, char *path
)
5112 char *argv
[] = {path
, "-h", NULL
};
5113 char **lines
= NULL
;
5117 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5119 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5121 libzfs_free_str_array(lines
, lines_cnt
);
5125 for (int i
= 0; i
< lines_cnt
; i
++)
5126 if (!is_blank_str(lines
[i
]))
5127 printf(" %-14s %s\n", name
, lines
[i
]);
5129 libzfs_free_str_array(lines
, lines_cnt
);
5133 * Go though the zpool status/iostat -c scripts in the user's path, run their
5134 * help option (-h), and print out the results.
5137 print_zpool_dir_scripts(char *dirpath
)
5141 char fullpath
[MAXPATHLEN
];
5142 struct stat dir_stat
;
5144 if ((dir
= opendir(dirpath
)) != NULL
) {
5145 /* print all the files and directories within directory */
5146 while ((ent
= readdir(dir
)) != NULL
) {
5147 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5149 /* Print the scripts */
5150 if (stat(fullpath
, &dir_stat
) == 0)
5151 if (dir_stat
.st_mode
& S_IXUSR
&&
5152 S_ISREG(dir_stat
.st_mode
))
5153 print_zpool_script_help(ent
->d_name
,
5161 * Print out help text for all zpool status/iostat -c scripts.
5164 print_zpool_script_list(char *subcommand
)
5168 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5170 sp
= zpool_get_cmd_search_path();
5174 dir
= strtok(sp
, ":");
5175 while (dir
!= NULL
) {
5176 print_zpool_dir_scripts(dir
);
5177 dir
= strtok(NULL
, ":");
5184 * Set the minimum pool/vdev name column width. The width must be at least 10,
5185 * but may be as large as the column width - 42 so it still fits on one line.
5186 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5189 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5191 iostat_cbdata_t
*cb
= data
;
5192 int width
, available_width
;
5195 * get_namewidth() returns the maximum width of any name in that column
5196 * for any pool/vdev/device line that will be output.
5198 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5202 * The width we are calculating is the width of the header and also the
5203 * padding width for names that are less than maximum width. The stats
5204 * take up 42 characters, so the width available for names is:
5206 available_width
= get_columns() - 42;
5209 * If the maximum width fits on a screen, then great! Make everything
5210 * line up by justifying all lines to the same width. If that max
5211 * width is larger than what's available, the name plus stats won't fit
5212 * on one line, and justifying to that width would cause every line to
5213 * wrap on the screen. We only want lines with long names to wrap.
5214 * Limit the padding to what won't wrap.
5216 if (width
> available_width
)
5217 width
= available_width
;
5220 * And regardless of whatever the screen width is (get_columns can
5221 * return 0 if the width is not known or less than 42 for a narrow
5222 * terminal) have the width be a minimum of 10.
5227 /* Save the calculated width */
5228 cb
->cb_namewidth
= width
;
5234 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5235 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5236 * [interval [count]]
5238 * -c CMD For each vdev, run command CMD
5239 * -g Display guid for individual vdev name.
5240 * -L Follow links when resolving vdev path name.
5241 * -P Display full path for vdev name.
5242 * -v Display statistics for individual vdevs
5244 * -p Display values in parsable (exact) format.
5245 * -H Scripted mode. Don't display headers, and separate properties
5247 * -l Display average latency
5248 * -q Display queue depths
5249 * -w Display latency histograms
5250 * -r Display request size histogram
5251 * -T Display a timestamp in date(1) or Unix format
5252 * -n Only print headers once
5254 * This command can be tricky because we want to be able to deal with pool
5255 * creation/destruction as well as vdev configuration changes. The bulk of this
5256 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5257 * on pool_list_update() to detect the addition of new pools. Configuration
5258 * changes are all handled within libzfs.
5261 zpool_do_iostat(int argc
, char **argv
)
5267 unsigned long count
= 0;
5270 boolean_t verbose
= B_FALSE
;
5271 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5272 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5273 boolean_t omit_since_boot
= B_FALSE
;
5274 boolean_t guid
= B_FALSE
;
5275 boolean_t follow_links
= B_FALSE
;
5276 boolean_t full_name
= B_FALSE
;
5277 boolean_t headers_once
= B_FALSE
;
5278 iostat_cbdata_t cb
= { 0 };
5281 /* Used for printing error message */
5282 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5283 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5285 uint64_t unsupported_flags
;
5288 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5293 gettext("Can't set -c flag twice\n"));
5297 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5298 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5299 fprintf(stderr
, gettext(
5300 "Can't run -c, disabled by "
5301 "ZPOOL_SCRIPTS_ENABLED.\n"));
5305 if ((getuid() <= 0 || geteuid() <= 0) &&
5306 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5307 fprintf(stderr
, gettext(
5308 "Can't run -c with root privileges "
5309 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5319 follow_links
= B_TRUE
;
5325 get_timestamp_arg(*optarg
);
5349 omit_since_boot
= B_TRUE
;
5352 headers_once
= B_TRUE
;
5358 if (optopt
== 'c') {
5359 print_zpool_script_list("iostat");
5363 gettext("invalid option '%c'\n"), optopt
);
5372 cb
.cb_literal
= parsable
;
5373 cb
.cb_scripted
= scripted
;
5376 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5378 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5380 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5381 cb
.cb_iteration
= 0;
5382 cb
.cb_namewidth
= 0;
5383 cb
.cb_verbose
= verbose
;
5385 /* Get our interval and count values (if any) */
5387 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5390 get_interval_count(&argc
, argv
, &interval
, &count
);
5394 /* No args, so just print the defaults. */
5395 } else if (are_all_pools(argc
, argv
)) {
5396 /* All the args are pool names */
5397 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
5398 /* All the args are vdevs */
5399 cb
.cb_vdev_names
= argv
;
5400 cb
.cb_vdev_names_count
= argc
;
5401 argc
= 0; /* No pools to process */
5402 } else if (are_all_pools(1, argv
)) {
5403 /* The first arg is a pool name */
5404 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
5405 /* ...and the rest are vdev names */
5406 cb
.cb_vdev_names
= argv
+ 1;
5407 cb
.cb_vdev_names_count
= argc
- 1;
5408 argc
= 1; /* One pool to process */
5410 fprintf(stderr
, gettext("Expected either a list of "));
5411 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5412 fprintf(stderr
, " \"%s\", ", argv
[0]);
5413 fprintf(stderr
, gettext("but got:\n"));
5414 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5416 fprintf(stderr
, "\n");
5422 * The args don't make sense. The first arg isn't a pool name,
5423 * nor are all the args vdevs.
5425 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5426 fprintf(stderr
, "\n");
5430 if (cb
.cb_vdev_names_count
!= 0) {
5432 * If user specified vdevs, it implies verbose.
5434 cb
.cb_verbose
= B_TRUE
;
5438 * Construct the list of all interesting pools.
5441 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
5444 if (pool_list_count(list
) == 0 && argc
!= 0) {
5445 pool_list_free(list
);
5449 if (pool_list_count(list
) == 0 && interval
== 0) {
5450 pool_list_free(list
);
5451 (void) fprintf(stderr
, gettext("no pools available\n"));
5455 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5456 pool_list_free(list
);
5457 (void) fprintf(stderr
,
5458 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5463 if (l_histo
&& rq_histo
) {
5464 pool_list_free(list
);
5465 (void) fprintf(stderr
,
5466 gettext("Only one of [-r|-w] can be passed at a time\n"));
5472 * Enter the main iostat loop.
5478 * Histograms tables look out of place when you try to display
5479 * them with the other stats, so make a rule that you can only
5480 * print histograms by themselves.
5482 cb
.cb_flags
= IOS_L_HISTO_M
;
5483 } else if (rq_histo
) {
5484 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5486 cb
.cb_flags
= IOS_DEFAULT_M
;
5488 cb
.cb_flags
|= IOS_LATENCY_M
;
5490 cb
.cb_flags
|= IOS_QUEUES_M
;
5494 * See if the module supports all the stats we want to display.
5496 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5497 if (unsupported_flags
) {
5501 gettext("The loaded zfs module doesn't support:"));
5503 /* for each bit set in unsupported_flags */
5504 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5505 idx
= lowbit64(f
) - 1;
5506 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5509 fprintf(stderr
, ". Try running a newer module.\n");
5510 pool_list_free(list
);
5516 if ((npools
= pool_list_count(list
)) == 0)
5517 (void) fprintf(stderr
, gettext("no pools available\n"));
5520 * If this is the first iteration and -y was supplied
5521 * we skip any printing.
5523 boolean_t skip
= (omit_since_boot
&&
5524 cb
.cb_iteration
== 0);
5527 * Refresh all statistics. This is done as an
5528 * explicit step before calculating the maximum name
5529 * width, so that any * configuration changes are
5530 * properly accounted for.
5532 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5536 * Iterate over all pools to determine the maximum width
5537 * for the pool / device name column across all pools.
5539 cb
.cb_namewidth
= 0;
5540 (void) pool_list_iter(list
, B_FALSE
,
5541 get_namewidth_iostat
, &cb
);
5543 if (timestamp_fmt
!= NODATE
)
5544 print_timestamp(timestamp_fmt
);
5546 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5547 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5548 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5549 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5550 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5557 * Check terminal size so we can print headers
5558 * even when terminal window has its height
5561 winheight
= terminal_height();
5563 * Are we connected to TTY? If not, headers_once
5564 * should be true, to avoid breaking scripts.
5567 headers_once
= B_TRUE
;
5570 * If it's the first time and we're not skipping it,
5571 * or either skip or verbose mode, print the header.
5573 * The histogram code explicitly prints its header on
5574 * every vdev, so skip this for histograms.
5576 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5577 (skip
!= verbose
) ||
5579 (cb
.cb_iteration
% winheight
) == 0)) &&
5580 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5582 print_iostat_header(&cb
);
5585 (void) fsleep(interval
);
5589 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5592 * If there's more than one pool, and we're not in
5593 * verbose mode (which prints a separator for us),
5594 * then print a separator.
5596 * In addition, if we're printing specific vdevs then
5597 * we also want an ending separator.
5599 if (((npools
> 1 && !verbose
&&
5600 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5601 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5602 cb
.cb_vdev_names_count
)) &&
5604 print_iostat_separator(&cb
);
5605 if (cb
.vcdl
!= NULL
)
5606 print_cmd_columns(cb
.vcdl
, 1);
5610 if (cb
.vcdl
!= NULL
)
5611 free_vdev_cmd_data_list(cb
.vcdl
);
5616 * Flush the output so that redirection to a file isn't buffered
5619 (void) fflush(stdout
);
5624 if (count
!= 0 && --count
== 0)
5627 (void) fsleep(interval
);
5630 pool_list_free(list
);
5635 typedef struct list_cbdata
{
5636 boolean_t cb_verbose
;
5639 boolean_t cb_scripted
;
5640 zprop_list_t
*cb_proplist
;
5641 boolean_t cb_literal
;
5646 * Given a list of columns to display, output appropriate headers for each one.
5649 print_header(list_cbdata_t
*cb
)
5651 zprop_list_t
*pl
= cb
->cb_proplist
;
5652 char headerbuf
[ZPOOL_MAXPROPLEN
];
5654 boolean_t first
= B_TRUE
;
5655 boolean_t right_justify
;
5658 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5659 width
= pl
->pl_width
;
5660 if (first
&& cb
->cb_verbose
) {
5662 * Reset the width to accommodate the verbose listing
5665 width
= cb
->cb_namewidth
;
5673 right_justify
= B_FALSE
;
5674 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5675 header
= zpool_prop_column_name(pl
->pl_prop
);
5676 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5680 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5681 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5682 headerbuf
[i
] = '\0';
5686 if (pl
->pl_next
== NULL
&& !right_justify
)
5687 (void) printf("%s", header
);
5688 else if (right_justify
)
5689 (void) printf("%*s", (int)width
, header
);
5691 (void) printf("%-*s", (int)width
, header
);
5694 (void) printf("\n");
5698 * Given a pool and a list of properties, print out all the properties according
5699 * to the described layout. Used by zpool_do_list().
5702 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5704 zprop_list_t
*pl
= cb
->cb_proplist
;
5705 boolean_t first
= B_TRUE
;
5706 char property
[ZPOOL_MAXPROPLEN
];
5708 boolean_t right_justify
;
5711 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5713 width
= pl
->pl_width
;
5714 if (first
&& cb
->cb_verbose
) {
5716 * Reset the width to accommodate the verbose listing
5719 width
= cb
->cb_namewidth
;
5723 if (cb
->cb_scripted
)
5724 (void) printf("\t");
5731 right_justify
= B_FALSE
;
5732 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5733 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5734 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5739 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5740 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5741 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5742 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5743 sizeof (property
)) == 0) {
5751 * If this is being called in scripted mode, or if this is the
5752 * last column and it is left-justified, don't include a width
5755 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5756 (void) printf("%s", propstr
);
5757 else if (right_justify
)
5758 (void) printf("%*s", (int)width
, propstr
);
5760 (void) printf("%-*s", (int)width
, propstr
);
5763 (void) printf("\n");
5767 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5768 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5772 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5775 case ZPOOL_PROP_EXPANDSZ
:
5776 case ZPOOL_PROP_CHECKPOINT
:
5777 case ZPOOL_PROP_DEDUPRATIO
:
5779 (void) strlcpy(propval
, "-", sizeof (propval
));
5781 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5784 case ZPOOL_PROP_FRAGMENTATION
:
5785 if (value
== ZFS_FRAG_INVALID
) {
5786 (void) strlcpy(propval
, "-", sizeof (propval
));
5787 } else if (format
== ZFS_NICENUM_RAW
) {
5788 (void) snprintf(propval
, sizeof (propval
), "%llu",
5789 (unsigned long long)value
);
5791 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5792 (unsigned long long)value
);
5795 case ZPOOL_PROP_CAPACITY
:
5796 /* capacity value is in parts-per-10,000 (aka permyriad) */
5797 if (format
== ZFS_NICENUM_RAW
)
5798 (void) snprintf(propval
, sizeof (propval
), "%llu",
5799 (unsigned long long)value
/ 100);
5801 (void) snprintf(propval
, sizeof (propval
),
5802 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
5803 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
5805 case ZPOOL_PROP_HEALTH
:
5807 snprintf(propval
, sizeof (propval
), "%-*s", (int)width
, str
);
5810 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5814 (void) strlcpy(propval
, "-", sizeof (propval
));
5817 (void) printf("\t%s", propval
);
5819 (void) printf(" %*s", (int)width
, propval
);
5823 * print static default line per vdev
5824 * not compatible with '-o' <proplist> option
5827 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5828 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
5834 boolean_t scripted
= cb
->cb_scripted
;
5835 uint64_t islog
= B_FALSE
;
5836 char *dashes
= "%-*s - - - - "
5839 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5840 (uint64_t **)&vs
, &c
) == 0);
5843 boolean_t toplevel
= (vs
->vs_space
!= 0);
5845 enum zfs_nicenum_format format
;
5849 format
= ZFS_NICENUM_RAW
;
5851 format
= ZFS_NICENUM_1024
;
5853 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5857 (void) printf("\t%s", name
);
5858 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5859 (void) printf("%*s%s", depth
, "", name
);
5861 (void) printf("%*s%s%*s", depth
, "", name
,
5862 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5865 * Print the properties for the individual vdevs. Some
5866 * properties are only applicable to toplevel vdevs. The
5867 * 'toplevel' boolean value is passed to the print_one_column()
5868 * to indicate that the value is valid.
5870 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
5872 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
5873 scripted
, toplevel
, format
);
5874 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5875 NULL
, scripted
, toplevel
, format
);
5876 print_one_column(ZPOOL_PROP_CHECKPOINT
,
5877 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
5878 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
5879 scripted
, B_TRUE
, format
);
5880 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5881 vs
->vs_fragmentation
, NULL
, scripted
,
5882 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5884 cap
= (vs
->vs_space
== 0) ? 0 :
5885 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
5886 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
5887 scripted
, toplevel
, format
);
5888 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
5889 scripted
, toplevel
, format
);
5890 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5892 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
5894 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
5897 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
5899 (void) printf("\n");
5902 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5903 &child
, &children
) != 0)
5906 /* list the normal vdevs first */
5907 for (c
= 0; c
< children
; c
++) {
5908 uint64_t ishole
= B_FALSE
;
5910 if (nvlist_lookup_uint64(child
[c
],
5911 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5914 if (nvlist_lookup_uint64(child
[c
],
5915 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
5918 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
5921 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5923 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
5927 /* list the classes: 'logs', 'dedup', and 'special' */
5928 for (uint_t n
= 0; n
< 3; n
++) {
5929 boolean_t printed
= B_FALSE
;
5931 for (c
= 0; c
< children
; c
++) {
5935 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5936 &islog
) == 0 && islog
) {
5937 bias
= VDEV_ALLOC_CLASS_LOGS
;
5939 (void) nvlist_lookup_string(child
[c
],
5940 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
5941 (void) nvlist_lookup_string(child
[c
],
5942 ZPOOL_CONFIG_TYPE
, &type
);
5944 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
5946 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
5950 /* LINTED E_SEC_PRINTF_VAR_FMT */
5951 (void) printf(dashes
, cb
->cb_namewidth
,
5955 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5957 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5963 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5964 &child
, &children
) == 0 && children
> 0) {
5965 /* LINTED E_SEC_PRINTF_VAR_FMT */
5966 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5967 for (c
= 0; c
< children
; c
++) {
5968 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5970 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5976 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5977 &children
) == 0 && children
> 0) {
5978 /* LINTED E_SEC_PRINTF_VAR_FMT */
5979 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5980 for (c
= 0; c
< children
; c
++) {
5981 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5983 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5991 * Generic callback function to list a pool.
5994 list_callback(zpool_handle_t
*zhp
, void *data
)
5996 list_cbdata_t
*cbp
= data
;
5998 print_pool(zhp
, cbp
);
6000 if (cbp
->cb_verbose
) {
6001 nvlist_t
*config
, *nvroot
;
6003 config
= zpool_get_config(zhp
, NULL
);
6004 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6006 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6013 * Set the minimum pool/vdev name column width. The width must be at least 9,
6014 * but may be as large as needed.
6017 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6019 list_cbdata_t
*cb
= data
;
6022 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6028 cb
->cb_namewidth
= width
;
6034 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6036 * -g Display guid for individual vdev name.
6037 * -H Scripted mode. Don't display headers, and separate properties
6039 * -L Follow links when resolving vdev path name.
6040 * -o List of properties to display. Defaults to
6041 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6042 * "dedupratio,health,altroot"
6043 * -p Display values in parsable (exact) format.
6044 * -P Display full path for vdev name.
6045 * -T Display a timestamp in date(1) or Unix format
6047 * List all pools in the system, whether or not they're healthy. Output space
6048 * statistics for each one, as well as health status summary.
6051 zpool_do_list(int argc
, char **argv
)
6055 list_cbdata_t cb
= { 0 };
6056 static char default_props
[] =
6057 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6058 "capacity,dedupratio,health,altroot";
6059 char *props
= default_props
;
6061 unsigned long count
= 0;
6063 boolean_t first
= B_TRUE
;
6066 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6069 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6072 cb
.cb_scripted
= B_TRUE
;
6075 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6081 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6084 cb
.cb_literal
= B_TRUE
;
6087 get_timestamp_arg(*optarg
);
6090 cb
.cb_verbose
= B_TRUE
;
6091 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6094 (void) fprintf(stderr
, gettext("missing argument for "
6095 "'%c' option\n"), optopt
);
6099 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6108 get_interval_count(&argc
, argv
, &interval
, &count
);
6110 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6114 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6118 if (pool_list_count(list
) == 0)
6121 cb
.cb_namewidth
= 0;
6122 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6124 if (timestamp_fmt
!= NODATE
)
6125 print_timestamp(timestamp_fmt
);
6127 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6131 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6136 if (count
!= 0 && --count
== 0)
6139 pool_list_free(list
);
6140 (void) fsleep(interval
);
6143 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6144 (void) printf(gettext("no pools available\n"));
6148 pool_list_free(list
);
6149 zprop_free_list(cb
.cb_proplist
);
6154 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6156 boolean_t force
= B_FALSE
;
6157 boolean_t rebuild
= B_FALSE
;
6158 boolean_t wait
= B_FALSE
;
6161 char *poolname
, *old_disk
, *new_disk
;
6162 zpool_handle_t
*zhp
;
6163 nvlist_t
*props
= NULL
;
6168 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6174 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6175 (void) fprintf(stderr
, gettext("missing "
6176 "'=' for -o option\n"));
6182 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6183 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6193 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6202 /* get pool name and check number of arguments */
6204 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6211 (void) fprintf(stderr
,
6212 gettext("missing <device> specification\n"));
6220 (void) fprintf(stderr
,
6221 gettext("missing <new_device> specification\n"));
6224 new_disk
= old_disk
;
6234 (void) fprintf(stderr
, gettext("too many arguments\n"));
6238 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6243 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6244 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6251 /* unless manually specified use "ashift" pool property (if set) */
6252 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6255 char strval
[ZPOOL_MAXPROPLEN
];
6257 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6258 if (src
!= ZPROP_SRC_DEFAULT
) {
6259 (void) sprintf(strval
, "%" PRId32
, intval
);
6260 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6261 &props
, B_TRUE
) == 0);
6265 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6267 if (nvroot
== NULL
) {
6273 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6276 if (ret
== 0 && wait
)
6277 ret
= zpool_wait(zhp
,
6278 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6281 nvlist_free(nvroot
);
6288 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6290 * -f Force attach, even if <new_device> appears to be in use.
6291 * -s Use sequential instead of healing reconstruction for resilver.
6292 * -o Set property=value.
6293 * -w Wait for replacing to complete before returning
6295 * Replace <device> with <new_device>.
6299 zpool_do_replace(int argc
, char **argv
)
6301 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6305 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6307 * -f Force attach, even if <new_device> appears to be in use.
6308 * -s Use sequential instead of healing reconstruction for resilver.
6309 * -o Set property=value.
6310 * -w Wait for resilvering to complete before returning
6312 * Attach <new_device> to the mirror containing <device>. If <device> is not
6313 * part of a mirror, then <device> will be transformed into a mirror of
6314 * <device> and <new_device>. In either case, <new_device> will begin life
6315 * with a DTL of [0, now], and will immediately begin to resilver itself.
6318 zpool_do_attach(int argc
, char **argv
)
6320 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6324 * zpool detach [-f] <pool> <device>
6326 * -f Force detach of <device>, even if DTLs argue against it
6327 * (not supported yet)
6329 * Detach a device from a mirror. The operation will be refused if <device>
6330 * is the last device in the mirror, or if the DTLs indicate that this device
6331 * has the only valid copy of some data.
6335 zpool_do_detach(int argc
, char **argv
)
6338 char *poolname
, *path
;
6339 zpool_handle_t
*zhp
;
6343 while ((c
= getopt(argc
, argv
, "")) != -1) {
6346 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6355 /* get pool name and check number of arguments */
6357 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6362 (void) fprintf(stderr
,
6363 gettext("missing <device> specification\n"));
6370 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6373 ret
= zpool_vdev_detach(zhp
, path
);
6381 * zpool split [-gLnP] [-o prop=val] ...
6383 * [-R altroot] <pool> <newpool> [<device> ...]
6385 * -g Display guid for individual vdev name.
6386 * -L Follow links when resolving vdev path name.
6387 * -n Do not split the pool, but display the resulting layout if
6388 * it were to be split.
6389 * -o Set property=value, or set mount options.
6390 * -P Display full path for vdev name.
6391 * -R Mount the split-off pool under an alternate root.
6392 * -l Load encryption keys while importing.
6394 * Splits the named pool and gives it the new pool name. Devices to be split
6395 * off may be listed, provided that no more than one device is specified
6396 * per top-level vdev mirror. The newly split pool is left in an exported
6397 * state unless -R is specified.
6399 * Restrictions: the top-level of the pool pool must only be made up of
6400 * mirrors; all devices in the pool must be healthy; no device may be
6401 * undergoing a resilvering operation.
6404 zpool_do_split(int argc
, char **argv
)
6406 char *srcpool
, *newpool
, *propval
;
6407 char *mntopts
= NULL
;
6410 boolean_t loadkeys
= B_FALSE
;
6411 zpool_handle_t
*zhp
;
6412 nvlist_t
*config
, *props
= NULL
;
6414 flags
.dryrun
= B_FALSE
;
6415 flags
.import
= B_FALSE
;
6416 flags
.name_flags
= 0;
6419 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6422 flags
.name_flags
|= VDEV_NAME_GUID
;
6425 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6428 flags
.import
= B_TRUE
;
6430 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6431 &props
, B_TRUE
) != 0) {
6440 flags
.dryrun
= B_TRUE
;
6443 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6446 if (add_prop_list(optarg
, propval
,
6447 &props
, B_TRUE
) != 0) {
6456 flags
.name_flags
|= VDEV_NAME_PATH
;
6459 (void) fprintf(stderr
, gettext("missing argument for "
6460 "'%c' option\n"), optopt
);
6464 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6471 if (!flags
.import
&& mntopts
!= NULL
) {
6472 (void) fprintf(stderr
, gettext("setting mntopts is only "
6473 "valid when importing the pool\n"));
6477 if (!flags
.import
&& loadkeys
) {
6478 (void) fprintf(stderr
, gettext("loading keys is only "
6479 "valid when importing the pool\n"));
6487 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6491 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6501 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6506 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6507 if (config
== NULL
) {
6511 (void) printf(gettext("would create '%s' with the "
6512 "following layout:\n\n"), newpool
);
6513 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6520 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6521 nvlist_free(config
);
6527 * The split was successful. Now we need to open the new
6528 * pool and import it.
6530 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6531 nvlist_free(config
);
6537 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6542 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6543 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6545 (void) fprintf(stderr
, gettext("Split was successful, but "
6546 "the datasets could not all be mounted\n"));
6547 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6548 "different altroot\n"), "zpool import");
6551 nvlist_free(config
);
6560 * zpool online <pool> <device> ...
6563 zpool_do_online(int argc
, char **argv
)
6567 zpool_handle_t
*zhp
;
6569 vdev_state_t newstate
;
6573 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6576 flags
|= ZFS_ONLINE_EXPAND
;
6579 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6588 /* get pool name and check number of arguments */
6590 (void) fprintf(stderr
, gettext("missing pool name\n"));
6594 (void) fprintf(stderr
, gettext("missing device name\n"));
6600 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6603 for (i
= 1; i
< argc
; i
++) {
6604 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6605 if (newstate
!= VDEV_STATE_HEALTHY
) {
6606 (void) printf(gettext("warning: device '%s' "
6607 "onlined, but remains in faulted state\n"),
6609 if (newstate
== VDEV_STATE_FAULTED
)
6610 (void) printf(gettext("use 'zpool "
6611 "clear' to restore a faulted "
6614 (void) printf(gettext("use 'zpool "
6615 "replace' to replace devices "
6616 "that are no longer present\n"));
6629 * zpool offline [-ft] <pool> <device> ...
6631 * -f Force the device into a faulted state.
6633 * -t Only take the device off-line temporarily. The offline/faulted
6634 * state will not be persistent across reboots.
6638 zpool_do_offline(int argc
, char **argv
)
6642 zpool_handle_t
*zhp
;
6644 boolean_t istmp
= B_FALSE
;
6645 boolean_t fault
= B_FALSE
;
6648 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6657 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6666 /* get pool name and check number of arguments */
6668 (void) fprintf(stderr
, gettext("missing pool name\n"));
6672 (void) fprintf(stderr
, gettext("missing device name\n"));
6678 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6681 for (i
= 1; i
< argc
; i
++) {
6683 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6685 if (istmp
== B_FALSE
) {
6686 /* Force the fault to persist across imports */
6687 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6689 aux
= VDEV_AUX_EXTERNAL
;
6692 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6695 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6706 * zpool clear <pool> [device]
6708 * Clear all errors associated with a pool or a particular device.
6711 zpool_do_clear(int argc
, char **argv
)
6715 boolean_t dryrun
= B_FALSE
;
6716 boolean_t do_rewind
= B_FALSE
;
6717 boolean_t xtreme_rewind
= B_FALSE
;
6718 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6719 nvlist_t
*policy
= NULL
;
6720 zpool_handle_t
*zhp
;
6721 char *pool
, *device
;
6724 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6733 xtreme_rewind
= B_TRUE
;
6736 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6746 (void) fprintf(stderr
, gettext("missing pool name\n"));
6751 (void) fprintf(stderr
, gettext("too many arguments\n"));
6755 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6756 (void) fprintf(stderr
,
6757 gettext("-n or -X only meaningful with -F\n"));
6761 rewind_policy
= ZPOOL_TRY_REWIND
;
6763 rewind_policy
= ZPOOL_DO_REWIND
;
6765 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6767 /* In future, further rewind policy choices can be passed along here */
6768 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6769 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
6770 rewind_policy
) != 0) {
6775 device
= argc
== 2 ? argv
[1] : NULL
;
6777 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
6778 nvlist_free(policy
);
6782 if (zpool_clear(zhp
, device
, policy
) != 0)
6787 nvlist_free(policy
);
6793 * zpool reguid <pool>
6796 zpool_do_reguid(int argc
, char **argv
)
6800 zpool_handle_t
*zhp
;
6804 while ((c
= getopt(argc
, argv
, "")) != -1) {
6807 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6816 /* get pool name and check number of arguments */
6818 (void) fprintf(stderr
, gettext("missing pool name\n"));
6823 (void) fprintf(stderr
, gettext("too many arguments\n"));
6828 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6831 ret
= zpool_reguid(zhp
);
6839 * zpool reopen <pool>
6841 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6844 zpool_do_reopen(int argc
, char **argv
)
6848 boolean_t scrub_restart
= B_TRUE
;
6851 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6854 scrub_restart
= B_FALSE
;
6857 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6866 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6867 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
6873 typedef struct scrub_cbdata
{
6875 pool_scrub_cmd_t cb_scrub_cmd
;
6879 zpool_has_checkpoint(zpool_handle_t
*zhp
)
6881 nvlist_t
*config
, *nvroot
;
6883 config
= zpool_get_config(zhp
, NULL
);
6885 if (config
!= NULL
) {
6886 pool_checkpoint_stat_t
*pcs
= NULL
;
6889 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6890 (void) nvlist_lookup_uint64_array(nvroot
,
6891 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6893 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6896 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
6897 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6905 scrub_callback(zpool_handle_t
*zhp
, void *data
)
6907 scrub_cbdata_t
*cb
= data
;
6911 * Ignore faulted pools.
6913 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
6914 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
6915 "currently unavailable\n"), zpool_get_name(zhp
));
6919 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
6921 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
6922 cb
->cb_type
== POOL_SCAN_SCRUB
) {
6923 (void) printf(gettext("warning: will not scrub state that "
6924 "belongs to the checkpoint of pool '%s'\n"),
6925 zpool_get_name(zhp
));
6932 wait_callback(zpool_handle_t
*zhp
, void *data
)
6934 zpool_wait_activity_t
*act
= data
;
6935 return (zpool_wait(zhp
, *act
));
6939 * zpool scrub [-s | -p] [-w] <pool> ...
6941 * -s Stop. Stops any in-progress scrub.
6942 * -p Pause. Pause in-progress scrub.
6943 * -w Wait. Blocks until scrub has completed.
6946 zpool_do_scrub(int argc
, char **argv
)
6950 boolean_t wait
= B_FALSE
;
6953 cb
.cb_type
= POOL_SCAN_SCRUB
;
6954 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6957 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
6960 cb
.cb_type
= POOL_SCAN_NONE
;
6963 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
6969 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6975 if (cb
.cb_type
== POOL_SCAN_NONE
&&
6976 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
6977 (void) fprintf(stderr
, gettext("invalid option combination: "
6978 "-s and -p are mutually exclusive\n"));
6982 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
6983 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
6984 (void) fprintf(stderr
, gettext("invalid option combination: "
6985 "-w cannot be used with -p or -s\n"));
6993 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6997 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
);
6999 if (wait
&& !error
) {
7000 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7001 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, wait_callback
,
7009 * zpool resilver <pool> ...
7011 * Restarts any in-progress resilver
7014 zpool_do_resilver(int argc
, char **argv
)
7019 cb
.cb_type
= POOL_SCAN_RESILVER
;
7020 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7023 while ((c
= getopt(argc
, argv
, "")) != -1) {
7026 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7036 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7040 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
7044 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7046 * -c Cancel. Ends any in-progress trim.
7047 * -d Secure trim. Requires kernel and device support.
7048 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7049 * adding a multiplier suffix such as 'k' or 'm'.
7050 * -s Suspend. TRIM can then be restarted with no flags.
7051 * -w Wait. Blocks until trimming has completed.
7054 zpool_do_trim(int argc
, char **argv
)
7056 struct option long_options
[] = {
7057 {"cancel", no_argument
, NULL
, 'c'},
7058 {"secure", no_argument
, NULL
, 'd'},
7059 {"rate", required_argument
, NULL
, 'r'},
7060 {"suspend", no_argument
, NULL
, 's'},
7061 {"wait", no_argument
, NULL
, 'w'},
7065 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7067 boolean_t secure
= B_FALSE
;
7068 boolean_t wait
= B_FALSE
;
7071 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7075 if (cmd_type
!= POOL_TRIM_START
&&
7076 cmd_type
!= POOL_TRIM_CANCEL
) {
7077 (void) fprintf(stderr
, gettext("-c cannot be "
7078 "combined with other options\n"));
7081 cmd_type
= POOL_TRIM_CANCEL
;
7084 if (cmd_type
!= POOL_TRIM_START
) {
7085 (void) fprintf(stderr
, gettext("-d cannot be "
7086 "combined with the -c or -s options\n"));
7092 if (cmd_type
!= POOL_TRIM_START
) {
7093 (void) fprintf(stderr
, gettext("-r cannot be "
7094 "combined with the -c or -s options\n"));
7097 if (zfs_nicestrtonum(NULL
, optarg
, &rate
) == -1) {
7098 (void) fprintf(stderr
,
7099 gettext("invalid value for rate\n"));
7104 if (cmd_type
!= POOL_TRIM_START
&&
7105 cmd_type
!= POOL_TRIM_SUSPEND
) {
7106 (void) fprintf(stderr
, gettext("-s cannot be "
7107 "combined with other options\n"));
7110 cmd_type
= POOL_TRIM_SUSPEND
;
7117 (void) fprintf(stderr
,
7118 gettext("invalid option '%c'\n"), optopt
);
7120 (void) fprintf(stderr
,
7121 gettext("invalid option '%s'\n"),
7132 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7137 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7138 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7143 char *poolname
= argv
[0];
7144 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7148 trimflags_t trim_flags
= {
7154 nvlist_t
*vdevs
= fnvlist_alloc();
7156 /* no individual leaf vdevs specified, so add them all */
7157 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7158 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7159 ZPOOL_CONFIG_VDEV_TREE
);
7160 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7161 trim_flags
.fullpool
= B_TRUE
;
7163 trim_flags
.fullpool
= B_FALSE
;
7164 for (int i
= 1; i
< argc
; i
++) {
7165 fnvlist_add_boolean(vdevs
, argv
[i
]);
7169 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7171 fnvlist_free(vdevs
);
7178 * Converts a total number of seconds to a human readable string broken
7179 * down in to days/hours/minutes/seconds.
7182 secs_to_dhms(uint64_t total
, char *buf
)
7184 uint64_t days
= total
/ 60 / 60 / 24;
7185 uint64_t hours
= (total
/ 60 / 60) % 24;
7186 uint64_t mins
= (total
/ 60) % 60;
7187 uint64_t secs
= (total
% 60);
7190 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7191 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7192 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7194 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7195 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7196 (u_longlong_t
)secs
);
7201 * Print out detailed scrub status.
7204 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7206 time_t start
, end
, pause
;
7207 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7208 uint64_t elapsed
, scan_rate
, issue_rate
;
7209 double fraction_done
;
7210 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7211 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7214 printf_color(ANSI_BOLD
, gettext("scan:"));
7217 /* If there's never been a scan, there's not much to say. */
7218 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7219 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7220 (void) printf(gettext("none requested\n"));
7224 start
= ps
->pss_start_time
;
7225 end
= ps
->pss_end_time
;
7226 pause
= ps
->pss_pass_scrub_pause
;
7228 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7230 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7231 ps
->pss_func
== POOL_SCAN_RESILVER
);
7233 /* Scan is finished or canceled. */
7234 if (ps
->pss_state
== DSS_FINISHED
) {
7235 secs_to_dhms(end
- start
, time_buf
);
7237 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7238 (void) printf(gettext("scrub repaired %s "
7239 "in %s with %llu errors on %s"), processed_buf
,
7240 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7242 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7243 (void) printf(gettext("resilvered %s "
7244 "in %s with %llu errors on %s"), processed_buf
,
7245 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7249 } else if (ps
->pss_state
== DSS_CANCELED
) {
7250 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7251 (void) printf(gettext("scrub canceled on %s"),
7253 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7254 (void) printf(gettext("resilver canceled on %s"),
7260 assert(ps
->pss_state
== DSS_SCANNING
);
7262 /* Scan is in progress. Resilvers can't be paused. */
7263 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7265 (void) printf(gettext("scrub in progress since %s"),
7268 (void) printf(gettext("scrub paused since %s"),
7270 (void) printf(gettext("\tscrub started on %s"),
7273 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7274 (void) printf(gettext("resilver in progress since %s"),
7278 scanned
= ps
->pss_examined
;
7279 pass_scanned
= ps
->pss_pass_exam
;
7280 issued
= ps
->pss_issued
;
7281 pass_issued
= ps
->pss_pass_issued
;
7282 total
= ps
->pss_to_examine
;
7284 /* we are only done with a block once we have issued the IO for it */
7285 fraction_done
= (double)issued
/ total
;
7287 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7288 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7289 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7290 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7292 scan_rate
= pass_scanned
/ elapsed
;
7293 issue_rate
= pass_issued
/ elapsed
;
7294 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7295 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7296 secs_to_dhms(total_secs_left
, time_buf
);
7298 /* format all of the numbers we will be reporting */
7299 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7300 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7301 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7302 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7303 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7305 /* do not print estimated time if we have a paused scrub */
7307 (void) printf(gettext("\t%s scanned at %s/s, "
7308 "%s issued at %s/s, %s total\n"),
7309 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7311 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7312 scanned_buf
, issued_buf
, total_buf
);
7315 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7316 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7317 processed_buf
, 100 * fraction_done
);
7318 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7319 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7320 processed_buf
, 100 * fraction_done
);
7324 if (total_secs_left
!= UINT64_MAX
&&
7325 issue_rate
>= 10 * 1024 * 1024) {
7326 (void) printf(gettext(", %s to go\n"), time_buf
);
7328 (void) printf(gettext(", no estimated "
7329 "completion time\n"));
7332 (void) printf(gettext("\n"));
7337 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7339 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7343 printf_color(ANSI_BOLD
, gettext("scan:"));
7346 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7347 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7348 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7349 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7350 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7351 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7352 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7353 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7354 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7355 (bytes_est
+ 1), 100);
7357 /* Format all of the numbers we will be reporting */
7358 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7359 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7360 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7361 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7362 sizeof (bytes_scanned_buf
));
7363 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7364 sizeof (bytes_issued_buf
));
7365 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7366 sizeof (bytes_rebuilt_buf
));
7367 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7368 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7369 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7371 time_t start
= vrs
->vrs_start_time
;
7372 time_t end
= vrs
->vrs_end_time
;
7374 /* Rebuild is finished or canceled. */
7375 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7376 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7377 (void) printf(gettext("resilvered (%s) %s in %s "
7378 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7379 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7381 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7382 (void) printf(gettext("resilver (%s) canceled on %s"),
7383 vdev_name
, ctime(&end
));
7385 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7386 (void) printf(gettext("resilver (%s) in progress since %s"),
7387 vdev_name
, ctime(&start
));
7390 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7392 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7393 MAX(scan_rate
, 1), time_buf
);
7395 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7396 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7397 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7398 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7399 bytes_rebuilt_buf
, scan_pct
);
7401 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7402 if (scan_rate
>= 10 * 1024 * 1024) {
7403 (void) printf(gettext(", %s to go\n"), time_buf
);
7405 (void) printf(gettext(", no estimated "
7406 "completion time\n"));
7409 (void) printf(gettext("\n"));
7414 * Print rebuild status for top-level vdevs.
7417 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7422 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7423 &child
, &children
) != 0)
7426 for (uint_t c
= 0; c
< children
; c
++) {
7427 vdev_rebuild_stat_t
*vrs
;
7430 if (nvlist_lookup_uint64_array(child
[c
],
7431 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7432 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7433 child
[c
], VDEV_NAME_TYPE_ID
);
7434 print_rebuild_status_impl(vrs
, name
);
7441 * As we don't scrub checkpointed blocks, we want to warn the user that we
7442 * skipped scanning some blocks if a checkpoint exists or existed at any
7443 * time during the scan. If a sequential instead of healing reconstruction
7444 * was performed then the blocks were reconstructed. However, their checksums
7445 * have not been verified so we still print the warning.
7448 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7450 if (ps
== NULL
|| pcs
== NULL
)
7453 if (pcs
->pcs_state
== CS_NONE
||
7454 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7457 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7459 if (ps
->pss_state
== DSS_NONE
)
7462 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7463 ps
->pss_end_time
< pcs
->pcs_start_time
)
7466 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7467 (void) printf(gettext(" scan warning: skipped blocks "
7468 "that are only referenced by the checkpoint.\n"));
7470 assert(ps
->pss_state
== DSS_SCANNING
);
7471 (void) printf(gettext(" scan warning: skipping blocks "
7472 "that are only referenced by the checkpoint.\n"));
7477 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7478 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7479 * the last completed (or cancelled) rebuild.
7482 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7486 boolean_t rebuilding
= B_FALSE
;
7487 uint64_t end_time
= 0;
7489 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7490 &child
, &children
) != 0)
7493 for (uint_t c
= 0; c
< children
; c
++) {
7494 vdev_rebuild_stat_t
*vrs
;
7497 if (nvlist_lookup_uint64_array(child
[c
],
7498 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7500 if (vrs
->vrs_end_time
> end_time
)
7501 end_time
= vrs
->vrs_end_time
;
7503 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7504 rebuilding
= B_TRUE
;
7511 if (rebuild_end_time
!= NULL
)
7512 *rebuild_end_time
= end_time
;
7514 return (rebuilding
);
7518 * Print the scan status.
7521 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7523 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7524 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7525 boolean_t active_resilver
= B_FALSE
;
7526 pool_checkpoint_stat_t
*pcs
= NULL
;
7527 pool_scan_stat_t
*ps
= NULL
;
7530 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7531 (uint64_t **)&ps
, &c
) == 0) {
7532 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7533 resilver_end_time
= ps
->pss_end_time
;
7534 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7537 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7538 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7541 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7542 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7544 /* Always print the scrub status when available. */
7546 print_scan_scrub_resilver_status(ps
);
7549 * When there is an active resilver or rebuild print its status.
7550 * Otherwise print the status of the last resilver or rebuild.
7552 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7553 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7554 print_scan_scrub_resilver_status(ps
);
7555 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7556 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7557 print_rebuild_status(zhp
, nvroot
);
7560 (void) nvlist_lookup_uint64_array(nvroot
,
7561 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7562 print_checkpoint_scan_warning(ps
, pcs
);
7566 * Print out detailed removal status.
7569 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7571 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7573 nvlist_t
*config
, *nvroot
;
7578 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7582 * Determine name of vdev.
7584 config
= zpool_get_config(zhp
, NULL
);
7585 nvroot
= fnvlist_lookup_nvlist(config
,
7586 ZPOOL_CONFIG_VDEV_TREE
);
7587 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7588 &child
, &children
) == 0);
7589 assert(prs
->prs_removing_vdev
< children
);
7590 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7591 child
[prs
->prs_removing_vdev
], B_TRUE
);
7593 (void) printf(gettext("remove: "));
7595 start
= prs
->prs_start_time
;
7596 end
= prs
->prs_end_time
;
7597 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7600 * Removal is finished or canceled.
7602 if (prs
->prs_state
== DSS_FINISHED
) {
7603 uint64_t minutes_taken
= (end
- start
) / 60;
7605 (void) printf(gettext("Removal of vdev %llu copied %s "
7606 "in %lluh%um, completed on %s"),
7607 (longlong_t
)prs
->prs_removing_vdev
,
7609 (u_longlong_t
)(minutes_taken
/ 60),
7610 (uint_t
)(minutes_taken
% 60),
7611 ctime((time_t *)&end
));
7612 } else if (prs
->prs_state
== DSS_CANCELED
) {
7613 (void) printf(gettext("Removal of %s canceled on %s"),
7614 vdev_name
, ctime(&end
));
7616 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7617 double fraction_done
;
7620 assert(prs
->prs_state
== DSS_SCANNING
);
7623 * Removal is in progress.
7625 (void) printf(gettext(
7626 "Evacuation of %s in progress since %s"),
7627 vdev_name
, ctime(&start
));
7629 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7630 total
= prs
->prs_to_copy
;
7631 fraction_done
= (double)copied
/ total
;
7633 /* elapsed time for this pass */
7634 elapsed
= time(NULL
) - prs
->prs_start_time
;
7635 elapsed
= elapsed
> 0 ? elapsed
: 1;
7636 rate
= copied
/ elapsed
;
7637 rate
= rate
> 0 ? rate
: 1;
7638 mins_left
= ((total
- copied
) / rate
) / 60;
7639 hours_left
= mins_left
/ 60;
7641 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7642 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7643 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7646 * do not print estimated time if hours_left is more than
7649 (void) printf(gettext(" %s copied out of %s at %s/s, "
7651 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7652 if (hours_left
< (30 * 24)) {
7653 (void) printf(gettext(", %lluh%um to go\n"),
7654 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7656 (void) printf(gettext(
7657 ", (copy is slow, no estimated time)\n"));
7662 if (prs
->prs_mapping_memory
> 0) {
7664 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7665 (void) printf(gettext(" %s memory used for "
7666 "removed device mappings\n"),
7672 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7677 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7680 (void) printf(gettext("checkpoint: "));
7682 start
= pcs
->pcs_start_time
;
7683 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7685 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7686 char *date
= ctime(&start
);
7689 * ctime() adds a newline at the end of the generated
7690 * string, thus the weird format specifier and the
7691 * strlen() call used to chop it off from the output.
7693 (void) printf(gettext("created %.*s, consumes %s\n"),
7694 (int)(strlen(date
) - 1), date
, space_buf
);
7698 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7700 (void) printf(gettext("discarding, %s remaining.\n"),
7705 print_error_log(zpool_handle_t
*zhp
)
7707 nvlist_t
*nverrlist
= NULL
;
7710 size_t len
= MAXPATHLEN
* 2;
7712 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
7715 (void) printf("errors: Permanent errors have been "
7716 "detected in the following files:\n\n");
7718 pathname
= safe_malloc(len
);
7720 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
7722 uint64_t dsobj
, obj
;
7724 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
7725 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
7727 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
7729 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
7730 (void) printf("%7s %s\n", "", pathname
);
7733 nvlist_free(nverrlist
);
7737 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
7746 (void) printf(gettext("\tspares\n"));
7748 for (i
= 0; i
< nspares
; i
++) {
7749 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
7751 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
7757 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
7766 (void) printf(gettext("\tcache\n"));
7768 for (i
= 0; i
< nl2cache
; i
++) {
7769 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
7771 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
7778 print_dedup_stats(nvlist_t
*config
)
7780 ddt_histogram_t
*ddh
;
7784 char dspace
[6], mspace
[6];
7787 * If the pool was faulted then we may not have been able to
7788 * obtain the config. Otherwise, if we have anything in the dedup
7789 * table continue processing the stats.
7791 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
7792 (uint64_t **)&ddo
, &c
) != 0)
7795 (void) printf("\n");
7796 (void) printf(gettext(" dedup: "));
7797 if (ddo
->ddo_count
== 0) {
7798 (void) printf(gettext("no DDT entries\n"));
7802 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
7803 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
7804 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
7805 (u_longlong_t
)ddo
->ddo_count
,
7809 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
7810 (uint64_t **)&dds
, &c
) == 0);
7811 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
7812 (uint64_t **)&ddh
, &c
) == 0);
7813 zpool_dump_ddt(dds
, ddh
);
7817 * Display a summary of pool status. Displays a summary such as:
7821 * reason: One or more devices ...
7822 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
7828 * When given the '-v' option, we print out the complete config. If the '-e'
7829 * option is specified, then we print out error rate information as well.
7832 status_callback(zpool_handle_t
*zhp
, void *data
)
7834 status_cbdata_t
*cbp
= data
;
7835 nvlist_t
*config
, *nvroot
;
7837 zpool_status_t reason
;
7838 zpool_errata_t errata
;
7843 config
= zpool_get_config(zhp
, NULL
);
7844 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
7849 * If we were given 'zpool status -x', only report those pools with
7852 if (cbp
->cb_explain
&&
7853 (reason
== ZPOOL_STATUS_OK
||
7854 reason
== ZPOOL_STATUS_VERSION_OLDER
||
7855 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
7856 if (!cbp
->cb_allpools
) {
7857 (void) printf(gettext("pool '%s' is healthy\n"),
7858 zpool_get_name(zhp
));
7860 cbp
->cb_first
= B_FALSE
;
7866 cbp
->cb_first
= B_FALSE
;
7868 (void) printf("\n");
7870 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7871 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
7872 (uint64_t **)&vs
, &c
) == 0);
7874 health
= zpool_get_state_str(zhp
);
7877 printf_color(ANSI_BOLD
, gettext("pool:"));
7878 printf(" %s\n", zpool_get_name(zhp
));
7880 printf_color(ANSI_BOLD
, gettext("state: "));
7882 printf_color(health_str_to_color(health
), "%s", health
);
7887 case ZPOOL_STATUS_MISSING_DEV_R
:
7888 printf_color(ANSI_BOLD
, gettext("status: "));
7889 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7890 "not be opened. Sufficient replicas exist for\n\tthe pool "
7891 "to continue functioning in a degraded state.\n"));
7892 printf_color(ANSI_BOLD
, gettext("action: "));
7893 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7894 "and online it using 'zpool online'.\n"));
7897 case ZPOOL_STATUS_MISSING_DEV_NR
:
7898 printf_color(ANSI_BOLD
, gettext("status: "));
7899 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7900 "not be opened. There are insufficient\n\treplicas for the"
7901 " pool to continue functioning.\n"));
7902 printf_color(ANSI_BOLD
, gettext("action: "));
7903 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7904 "and online it using 'zpool online'.\n"));
7907 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
7908 printf_color(ANSI_BOLD
, gettext("status: "));
7909 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7910 "not be used because the label is missing or\n\tinvalid. "
7911 "Sufficient replicas exist for the pool to continue\n\t"
7912 "functioning in a degraded state.\n"));
7913 printf_color(ANSI_BOLD
, gettext("action: "));
7914 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
7915 "'zpool replace'.\n"));
7918 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
7919 printf_color(ANSI_BOLD
, gettext("status: "));
7920 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7921 "not be used because the label is missing \n\tor invalid. "
7922 "There are insufficient replicas for the pool to "
7923 "continue\n\tfunctioning.\n"));
7924 zpool_explain_recover(zpool_get_handle(zhp
),
7925 zpool_get_name(zhp
), reason
, config
);
7928 case ZPOOL_STATUS_FAILING_DEV
:
7929 printf_color(ANSI_BOLD
, gettext("status: "));
7930 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7931 "experienced an unrecoverable error. An\n\tattempt was "
7932 "made to correct the error. Applications are "
7934 printf_color(ANSI_BOLD
, gettext("action: "));
7935 printf_color(ANSI_YELLOW
, gettext("Determine if the "
7936 "device needs to be replaced, and clear the errors\n\tusing"
7937 " 'zpool clear' or replace the device with 'zpool "
7941 case ZPOOL_STATUS_OFFLINE_DEV
:
7942 printf_color(ANSI_BOLD
, gettext("status: "));
7943 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7944 "been taken offline by the administrator.\n\tSufficient "
7945 "replicas exist for the pool to continue functioning in "
7946 "a\n\tdegraded state.\n"));
7947 printf_color(ANSI_BOLD
, gettext("action: "));
7948 printf_color(ANSI_YELLOW
, gettext("Online the device "
7949 "using 'zpool online' or replace the device with\n\t'zpool "
7953 case ZPOOL_STATUS_REMOVED_DEV
:
7954 printf_color(ANSI_BOLD
, gettext("status: "));
7955 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7956 "been removed by the administrator.\n\tSufficient "
7957 "replicas exist for the pool to continue functioning in "
7958 "a\n\tdegraded state.\n"));
7959 printf_color(ANSI_BOLD
, gettext("action: "));
7960 printf_color(ANSI_YELLOW
, gettext("Online the device "
7961 "using zpool online' or replace the device with\n\t'zpool "
7965 case ZPOOL_STATUS_RESILVERING
:
7966 case ZPOOL_STATUS_REBUILDING
:
7967 printf_color(ANSI_BOLD
, gettext("status: "));
7968 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
7969 "currently being resilvered. The pool will\n\tcontinue "
7970 "to function, possibly in a degraded state.\n"));
7971 printf_color(ANSI_BOLD
, gettext("action: "));
7972 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
7976 case ZPOOL_STATUS_REBUILD_SCRUB
:
7977 printf_color(ANSI_BOLD
, gettext("status: "));
7978 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
7979 "been sequentially resilvered, scrubbing\n\tthe pool "
7980 "is recommended.\n"));
7981 printf_color(ANSI_BOLD
, gettext("action: "));
7982 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
7983 "verify all data checksums.\n"));
7986 case ZPOOL_STATUS_CORRUPT_DATA
:
7987 printf_color(ANSI_BOLD
, gettext("status: "));
7988 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7989 "experienced an error resulting in data\n\tcorruption. "
7990 "Applications may be affected.\n"));
7991 printf_color(ANSI_BOLD
, gettext("action: "));
7992 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
7993 " if possible. Otherwise restore the\n\tentire pool from "
7997 case ZPOOL_STATUS_CORRUPT_POOL
:
7998 printf_color(ANSI_BOLD
, gettext("status: "));
7999 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8000 "corrupted and the pool cannot be opened.\n"));
8001 zpool_explain_recover(zpool_get_handle(zhp
),
8002 zpool_get_name(zhp
), reason
, config
);
8005 case ZPOOL_STATUS_VERSION_OLDER
:
8006 printf_color(ANSI_BOLD
, gettext("status: "));
8007 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8008 "a legacy on-disk format. The pool can\n\tstill be used, "
8009 "but some features are unavailable.\n"));
8010 printf_color(ANSI_BOLD
, gettext("action: "));
8011 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8012 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8013 "longer be accessible on software that does not support\n\t"
8014 "feature flags.\n"));
8017 case ZPOOL_STATUS_VERSION_NEWER
:
8018 printf_color(ANSI_BOLD
, gettext("status: "));
8019 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8020 "to a newer, incompatible on-disk version.\n\tThe pool "
8021 "cannot be accessed on this system.\n"));
8022 printf_color(ANSI_BOLD
, gettext("action: "));
8023 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8024 "system running more recent software, or\n\trestore the "
8025 "pool from backup.\n"));
8028 case ZPOOL_STATUS_FEAT_DISABLED
:
8029 printf_color(ANSI_BOLD
, gettext("status: "));
8030 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
8031 "not enabled on the pool. The pool can\n\tstill be used, "
8032 "but some features are unavailable.\n"));
8033 printf_color(ANSI_BOLD
, gettext("action: "));
8034 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8035 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8036 "longer be accessible by software that does not support\n\t"
8037 "the features. See zpool-features(5) for details.\n"));
8040 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8041 printf_color(ANSI_BOLD
, gettext("status: "));
8042 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8043 "on this system because it uses the\n\tfollowing feature(s)"
8044 " not supported on this system:\n"));
8045 zpool_print_unsup_feat(config
);
8046 (void) printf("\n");
8047 printf_color(ANSI_BOLD
, gettext("action: "));
8048 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8049 "system that supports the required feature(s),\n\tor "
8050 "restore the pool from backup.\n"));
8053 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8054 printf_color(ANSI_BOLD
, gettext("status: "));
8055 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8056 "accessed in read-only mode on this system. It\n\tcannot be"
8057 " accessed in read-write mode because it uses the "
8058 "following\n\tfeature(s) not supported on this system:\n"));
8059 zpool_print_unsup_feat(config
);
8060 (void) printf("\n");
8061 printf_color(ANSI_BOLD
, gettext("action: "));
8062 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8063 "in read-write mode. Import the pool with\n"
8064 "\t\"-o readonly=on\", access the pool from a system that "
8065 "supports the\n\trequired feature(s), or restore the "
8066 "pool from backup.\n"));
8069 case ZPOOL_STATUS_FAULTED_DEV_R
:
8070 printf_color(ANSI_BOLD
, gettext("status: "));
8071 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8072 "faulted in response to persistent errors.\n\tSufficient "
8073 "replicas exist for the pool to continue functioning "
8074 "in a\n\tdegraded state.\n"));
8075 printf_color(ANSI_BOLD
, gettext("action: "));
8076 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8077 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8080 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8081 printf_color(ANSI_BOLD
, gettext("status: "));
8082 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8083 "faulted in response to persistent errors. There are "
8084 "insufficient replicas for the pool to\n\tcontinue "
8086 printf_color(ANSI_BOLD
, gettext("action: "));
8087 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8088 "pool from a backup source. Manually marking the device\n"
8089 "\trepaired using 'zpool clear' may allow some data "
8090 "to be recovered.\n"));
8093 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8094 printf_color(ANSI_BOLD
, gettext("status: "));
8095 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8096 "because multihost writes failed or were delayed;\n\t"
8097 "another system could import the pool undetected.\n"));
8098 printf_color(ANSI_BOLD
, gettext("action: "));
8099 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8100 " are connected, then reboot your system and\n\timport the "
8104 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8105 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8106 printf_color(ANSI_BOLD
, gettext("status: "));
8107 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8108 "faulted in response to IO failures.\n"));
8109 printf_color(ANSI_BOLD
, gettext("action: "));
8110 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8111 "devices are connected, then run 'zpool clear'.\n"));
8114 case ZPOOL_STATUS_BAD_LOG
:
8115 printf_color(ANSI_BOLD
, gettext("status: "));
8116 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8117 "could not be read.\n"
8118 "\tWaiting for administrator intervention to fix the "
8119 "faulted pool.\n"));
8120 printf_color(ANSI_BOLD
, gettext("action: "));
8121 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8122 "device(s) and run 'zpool online',\n"
8123 "\tor ignore the intent log records by running "
8124 "'zpool clear'.\n"));
8127 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8128 (void) printf(gettext("status: One or more devices are "
8129 "configured to use a non-native block size.\n"
8130 "\tExpect reduced performance.\n"));
8131 (void) printf(gettext("action: Replace affected devices with "
8132 "devices that support the\n\tconfigured block size, or "
8133 "migrate data to a properly configured\n\tpool.\n"));
8136 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8137 printf_color(ANSI_BOLD
, gettext("status: "));
8138 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8139 " and system hostid on imported pool.\n\tThis pool was "
8140 "previously imported into a system with a different "
8141 "hostid,\n\tand then was verbatim imported into this "
8143 printf_color(ANSI_BOLD
, gettext("action: "));
8144 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8145 "systems on which it is imported.\n"
8146 "\tThen import it to correct the mismatch.\n"));
8149 case ZPOOL_STATUS_ERRATA
:
8150 printf_color(ANSI_BOLD
, gettext("status: "));
8151 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8155 case ZPOOL_ERRATA_NONE
:
8158 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8159 printf_color(ANSI_BOLD
, gettext("action: "));
8160 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8161 " run 'zpool scrub'.\n"));
8164 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8165 (void) printf(gettext("\tExisting encrypted datasets "
8166 "contain an on-disk incompatibility\n\twhich "
8167 "needs to be corrected.\n"));
8168 printf_color(ANSI_BOLD
, gettext("action: "));
8169 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8170 " backup existing encrypted datasets to new\n\t"
8171 "encrypted datasets and destroy the old ones. "
8172 "'zfs mount -o ro' can\n\tbe used to temporarily "
8173 "mount existing encrypted datasets readonly.\n"));
8176 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8177 (void) printf(gettext("\tExisting encrypted snapshots "
8178 "and bookmarks contain an on-disk\n\tincompat"
8179 "ibility. This may cause on-disk corruption if "
8180 "they are used\n\twith 'zfs recv'.\n"));
8181 printf_color(ANSI_BOLD
, gettext("action: "));
8182 printf_color(ANSI_YELLOW
, gettext("To correct the"
8183 "issue, enable the bookmark_v2 feature. No "
8184 "additional\n\taction is needed if there are no "
8185 "encrypted snapshots or bookmarks.\n\tIf preserving"
8186 "the encrypted snapshots and bookmarks is required,"
8187 " use\n\ta non-raw send to backup and restore them."
8188 " Alternately, they may be\n\tremoved to resolve "
8189 "the incompatibility.\n"));
8194 * All errata which allow the pool to be imported
8195 * must contain an action message.
8203 * The remaining errors can't actually be generated, yet.
8205 assert(reason
== ZPOOL_STATUS_OK
);
8208 if (msgid
!= NULL
) {
8210 printf_color(ANSI_BOLD
, gettext("see:"));
8212 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8216 if (config
!= NULL
) {
8218 nvlist_t
**spares
, **l2cache
;
8219 uint_t nspares
, nl2cache
;
8220 pool_checkpoint_stat_t
*pcs
= NULL
;
8221 pool_removal_stat_t
*prs
= NULL
;
8223 print_scan_status(zhp
, nvroot
);
8225 (void) nvlist_lookup_uint64_array(nvroot
,
8226 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8227 print_removal_status(zhp
, prs
);
8229 (void) nvlist_lookup_uint64_array(nvroot
,
8230 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8231 print_checkpoint_status(pcs
);
8233 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8234 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8235 if (cbp
->cb_namewidth
< 10)
8236 cbp
->cb_namewidth
= 10;
8238 color_start(ANSI_BOLD
);
8239 (void) printf(gettext("config:\n\n"));
8240 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8241 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8245 if (cbp
->cb_print_slow_ios
) {
8246 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8249 if (cbp
->vcdl
!= NULL
)
8250 print_cmd_columns(cbp
->vcdl
, 0);
8254 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8257 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8258 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8259 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8261 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8262 &l2cache
, &nl2cache
) == 0)
8263 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8265 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8266 &spares
, &nspares
) == 0)
8267 print_spares(zhp
, cbp
, spares
, nspares
);
8269 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8271 nvlist_t
*nverrlist
= NULL
;
8274 * If the approximate error count is small, get a
8275 * precise count by fetching the entire log and
8276 * uniquifying the results.
8278 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8279 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8284 while ((elem
= nvlist_next_nvpair(nverrlist
,
8289 nvlist_free(nverrlist
);
8291 (void) printf("\n");
8294 (void) printf(gettext("errors: No known data "
8296 else if (!cbp
->cb_verbose
)
8297 (void) printf(gettext("errors: %llu data "
8298 "errors, use '-v' for a list\n"),
8299 (u_longlong_t
)nerr
);
8301 print_error_log(zhp
);
8304 if (cbp
->cb_dedup_stats
)
8305 print_dedup_stats(config
);
8307 (void) printf(gettext("config: The configuration cannot be "
8315 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8316 * [interval [count]]
8318 * -c CMD For each vdev, run command CMD
8319 * -i Display vdev initialization status.
8320 * -g Display guid for individual vdev name.
8321 * -L Follow links when resolving vdev path name.
8322 * -p Display values in parsable (exact) format.
8323 * -P Display full path for vdev name.
8324 * -s Display slow IOs column.
8325 * -v Display complete error logs
8326 * -x Display only pools with potential problems
8327 * -D Display dedup status (undocumented)
8328 * -t Display vdev TRIM status.
8329 * -T Display a timestamp in date(1) or Unix format
8331 * Describes the health status of all pools or some subset.
8334 zpool_do_status(int argc
, char **argv
)
8339 unsigned long count
= 0;
8340 status_cbdata_t cb
= { 0 };
8344 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8349 gettext("Can't set -c flag twice\n"));
8353 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8354 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8355 fprintf(stderr
, gettext(
8356 "Can't run -c, disabled by "
8357 "ZPOOL_SCRIPTS_ENABLED.\n"));
8361 if ((getuid() <= 0 || geteuid() <= 0) &&
8362 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8363 fprintf(stderr
, gettext(
8364 "Can't run -c with root privileges "
8365 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8371 cb
.cb_print_vdev_init
= B_TRUE
;
8374 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8377 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8380 cb
.cb_literal
= B_TRUE
;
8383 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8386 cb
.cb_print_slow_ios
= B_TRUE
;
8389 cb
.cb_verbose
= B_TRUE
;
8392 cb
.cb_explain
= B_TRUE
;
8395 cb
.cb_dedup_stats
= B_TRUE
;
8398 cb
.cb_print_vdev_trim
= B_TRUE
;
8401 get_timestamp_arg(*optarg
);
8404 if (optopt
== 'c') {
8405 print_zpool_script_list("status");
8409 gettext("invalid option '%c'\n"), optopt
);
8418 get_interval_count(&argc
, argv
, &interval
, &count
);
8421 cb
.cb_allpools
= B_TRUE
;
8423 cb
.cb_first
= B_TRUE
;
8424 cb
.cb_print_status
= B_TRUE
;
8427 if (timestamp_fmt
!= NODATE
)
8428 print_timestamp(timestamp_fmt
);
8431 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8434 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
8435 status_callback
, &cb
);
8437 if (cb
.vcdl
!= NULL
)
8438 free_vdev_cmd_data_list(cb
.vcdl
);
8440 if (argc
== 0 && cb
.cb_count
== 0)
8441 (void) fprintf(stderr
, gettext("no pools available\n"));
8442 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8443 (void) printf(gettext("all pools are healthy\n"));
8451 if (count
!= 0 && --count
== 0)
8454 (void) fsleep(interval
);
8460 typedef struct upgrade_cbdata
{
8463 uint64_t cb_version
;
8468 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8470 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8471 int *count
= (int *)unsupp_fs
;
8473 if (zfs_version
> ZPL_VERSION
) {
8474 (void) printf(gettext("%s (v%d) is not supported by this "
8475 "implementation of ZFS.\n"),
8476 zfs_get_name(zhp
), zfs_version
);
8480 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8488 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8492 uint64_t oldversion
;
8495 config
= zpool_get_config(zhp
, NULL
);
8496 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8499 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8500 assert(oldversion
< version
);
8502 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8507 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8508 "to %d unsupported filesystems (max v%d).\n"),
8509 unsupp_fs
, (int)ZPL_VERSION
);
8513 ret
= zpool_upgrade(zhp
, version
);
8517 if (version
>= SPA_VERSION_FEATURES
) {
8518 (void) printf(gettext("Successfully upgraded "
8519 "'%s' from version %llu to feature flags.\n"),
8520 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8522 (void) printf(gettext("Successfully upgraded "
8523 "'%s' from version %llu to version %llu.\n"),
8524 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8525 (u_longlong_t
)version
);
8532 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8535 boolean_t firstff
= B_TRUE
;
8536 nvlist_t
*enabled
= zpool_get_features(zhp
);
8539 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8540 const char *fname
= spa_feature_table
[i
].fi_uname
;
8541 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8542 if (!nvlist_exists(enabled
, fguid
)) {
8544 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8545 ret
= zpool_set_prop(zhp
, propname
,
8546 ZFS_FEATURE_ENABLED
);
8554 (void) printf(gettext("Enabled the "
8555 "following features on '%s':\n"),
8556 zpool_get_name(zhp
));
8559 (void) printf(gettext(" %s\n"), fname
);
8570 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8572 upgrade_cbdata_t
*cbp
= arg
;
8575 boolean_t printnl
= B_FALSE
;
8578 config
= zpool_get_config(zhp
, NULL
);
8579 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8582 assert(SPA_VERSION_IS_SUPPORTED(version
));
8584 if (version
< cbp
->cb_version
) {
8585 cbp
->cb_first
= B_FALSE
;
8586 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8592 * If they did "zpool upgrade -a", then we could
8593 * be doing ioctls to different pools. We need
8594 * to log this history once to each pool, and bypass
8595 * the normal history logging that happens in main().
8597 (void) zpool_log_history(g_zfs
, history_str
);
8598 log_history
= B_FALSE
;
8601 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8603 ret
= upgrade_enable_all(zhp
, &count
);
8608 cbp
->cb_first
= B_FALSE
;
8614 (void) printf(gettext("\n"));
8621 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8623 upgrade_cbdata_t
*cbp
= arg
;
8627 config
= zpool_get_config(zhp
, NULL
);
8628 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8631 assert(SPA_VERSION_IS_SUPPORTED(version
));
8633 if (version
< SPA_VERSION_FEATURES
) {
8634 if (cbp
->cb_first
) {
8635 (void) printf(gettext("The following pools are "
8636 "formatted with legacy version numbers and can\n"
8637 "be upgraded to use feature flags. After "
8638 "being upgraded, these pools\nwill no "
8639 "longer be accessible by software that does not "
8640 "support feature\nflags.\n\n"));
8641 (void) printf(gettext("VER POOL\n"));
8642 (void) printf(gettext("--- ------------\n"));
8643 cbp
->cb_first
= B_FALSE
;
8646 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
8647 zpool_get_name(zhp
));
8654 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
8656 upgrade_cbdata_t
*cbp
= arg
;
8660 config
= zpool_get_config(zhp
, NULL
);
8661 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8664 if (version
>= SPA_VERSION_FEATURES
) {
8666 boolean_t poolfirst
= B_TRUE
;
8667 nvlist_t
*enabled
= zpool_get_features(zhp
);
8669 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8670 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8671 const char *fname
= spa_feature_table
[i
].fi_uname
;
8672 if (!nvlist_exists(enabled
, fguid
)) {
8673 if (cbp
->cb_first
) {
8674 (void) printf(gettext("\nSome "
8675 "supported features are not "
8676 "enabled on the following pools. "
8677 "Once a\nfeature is enabled the "
8678 "pool may become incompatible with "
8679 "software\nthat does not support "
8681 "zpool-features(5) for "
8683 (void) printf(gettext("POOL "
8685 (void) printf(gettext("------"
8687 cbp
->cb_first
= B_FALSE
;
8691 (void) printf(gettext("%s\n"),
8692 zpool_get_name(zhp
));
8693 poolfirst
= B_FALSE
;
8696 (void) printf(gettext(" %s\n"), fname
);
8699 * If they did "zpool upgrade -a", then we could
8700 * be doing ioctls to different pools. We need
8701 * to log this history once to each pool, and bypass
8702 * the normal history logging that happens in main().
8704 (void) zpool_log_history(g_zfs
, history_str
);
8705 log_history
= B_FALSE
;
8714 upgrade_one(zpool_handle_t
*zhp
, void *data
)
8716 boolean_t printnl
= B_FALSE
;
8717 upgrade_cbdata_t
*cbp
= data
;
8718 uint64_t cur_version
;
8721 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
8722 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
8723 "Pool 'log' must be renamed using export and import"
8728 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
8729 if (cur_version
> cbp
->cb_version
) {
8730 (void) printf(gettext("Pool '%s' is already formatted "
8731 "using more current version '%llu'.\n\n"),
8732 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
8736 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
8737 (void) printf(gettext("Pool '%s' is already formatted "
8738 "using version %llu.\n\n"), zpool_get_name(zhp
),
8739 (u_longlong_t
)cbp
->cb_version
);
8743 if (cur_version
!= cbp
->cb_version
) {
8745 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8750 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8752 ret
= upgrade_enable_all(zhp
, &count
);
8758 } else if (cur_version
== SPA_VERSION
) {
8759 (void) printf(gettext("Pool '%s' already has all "
8760 "supported features enabled.\n"),
8761 zpool_get_name(zhp
));
8766 (void) printf(gettext("\n"));
8775 * zpool upgrade [-V version] <-a | pool ...>
8777 * With no arguments, display downrev'd ZFS pool available for upgrade.
8778 * Individual pools can be upgraded by specifying the pool, and '-a' will
8779 * upgrade all pools.
8782 zpool_do_upgrade(int argc
, char **argv
)
8785 upgrade_cbdata_t cb
= { 0 };
8787 boolean_t showversions
= B_FALSE
;
8788 boolean_t upgradeall
= B_FALSE
;
8793 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
8796 upgradeall
= B_TRUE
;
8799 showversions
= B_TRUE
;
8802 cb
.cb_version
= strtoll(optarg
, &end
, 10);
8804 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
8805 (void) fprintf(stderr
,
8806 gettext("invalid version '%s'\n"), optarg
);
8811 (void) fprintf(stderr
, gettext("missing argument for "
8812 "'%c' option\n"), optopt
);
8816 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8827 if (cb
.cb_version
== 0) {
8828 cb
.cb_version
= SPA_VERSION
;
8829 } else if (!upgradeall
&& argc
== 0) {
8830 (void) fprintf(stderr
, gettext("-V option is "
8831 "incompatible with other arguments\n"));
8836 if (upgradeall
|| argc
!= 0) {
8837 (void) fprintf(stderr
, gettext("-v option is "
8838 "incompatible with other arguments\n"));
8841 } else if (upgradeall
) {
8843 (void) fprintf(stderr
, gettext("-a option should not "
8844 "be used along with a pool name\n"));
8849 (void) printf(gettext("This system supports ZFS pool feature "
8854 (void) printf(gettext("The following features are "
8856 (void) printf(gettext("FEAT DESCRIPTION\n"));
8857 (void) printf("----------------------------------------------"
8858 "---------------\n");
8859 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8860 zfeature_info_t
*fi
= &spa_feature_table
[i
];
8862 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
8863 " (read-only compatible)" : "";
8865 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
8866 (void) printf(" %s\n", fi
->fi_desc
);
8868 (void) printf("\n");
8870 (void) printf(gettext("The following legacy versions are also "
8872 (void) printf(gettext("VER DESCRIPTION\n"));
8873 (void) printf("--- -----------------------------------------"
8874 "---------------\n");
8875 (void) printf(gettext(" 1 Initial ZFS version\n"));
8876 (void) printf(gettext(" 2 Ditto blocks "
8877 "(replicated metadata)\n"));
8878 (void) printf(gettext(" 3 Hot spares and double parity "
8880 (void) printf(gettext(" 4 zpool history\n"));
8881 (void) printf(gettext(" 5 Compression using the gzip "
8883 (void) printf(gettext(" 6 bootfs pool property\n"));
8884 (void) printf(gettext(" 7 Separate intent log devices\n"));
8885 (void) printf(gettext(" 8 Delegated administration\n"));
8886 (void) printf(gettext(" 9 refquota and refreservation "
8888 (void) printf(gettext(" 10 Cache devices\n"));
8889 (void) printf(gettext(" 11 Improved scrub performance\n"));
8890 (void) printf(gettext(" 12 Snapshot properties\n"));
8891 (void) printf(gettext(" 13 snapused property\n"));
8892 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
8893 (void) printf(gettext(" 15 user/group space accounting\n"));
8894 (void) printf(gettext(" 16 stmf property support\n"));
8895 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
8896 (void) printf(gettext(" 18 Snapshot user holds\n"));
8897 (void) printf(gettext(" 19 Log device removal\n"));
8898 (void) printf(gettext(" 20 Compression using zle "
8899 "(zero-length encoding)\n"));
8900 (void) printf(gettext(" 21 Deduplication\n"));
8901 (void) printf(gettext(" 22 Received properties\n"));
8902 (void) printf(gettext(" 23 Slim ZIL\n"));
8903 (void) printf(gettext(" 24 System attributes\n"));
8904 (void) printf(gettext(" 25 Improved scrub stats\n"));
8905 (void) printf(gettext(" 26 Improved snapshot deletion "
8907 (void) printf(gettext(" 27 Improved snapshot creation "
8909 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
8910 (void) printf(gettext("\nFor more information on a particular "
8911 "version, including supported releases,\n"));
8912 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
8913 } else if (argc
== 0 && upgradeall
) {
8914 cb
.cb_first
= B_TRUE
;
8915 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
8916 if (ret
== 0 && cb
.cb_first
) {
8917 if (cb
.cb_version
== SPA_VERSION
) {
8918 (void) printf(gettext("All pools are already "
8919 "formatted using feature flags.\n\n"));
8920 (void) printf(gettext("Every feature flags "
8921 "pool already has all supported features "
8924 (void) printf(gettext("All pools are already "
8925 "formatted with version %llu or higher.\n"),
8926 (u_longlong_t
)cb
.cb_version
);
8929 } else if (argc
== 0) {
8930 cb
.cb_first
= B_TRUE
;
8931 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
8935 (void) printf(gettext("All pools are formatted "
8936 "using feature flags.\n\n"));
8938 (void) printf(gettext("\nUse 'zpool upgrade -v' "
8939 "for a list of available legacy versions.\n"));
8942 cb
.cb_first
= B_TRUE
;
8943 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
8947 (void) printf(gettext("Every feature flags pool has "
8948 "all supported features enabled.\n"));
8950 (void) printf(gettext("\n"));
8953 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
8960 typedef struct hist_cbdata
{
8967 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
8973 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
8974 &records
, &numrecords
) == 0);
8975 for (i
= 0; i
< numrecords
; i
++) {
8976 nvlist_t
*rec
= records
[i
];
8979 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
8983 tsec
= fnvlist_lookup_uint64(records
[i
],
8985 (void) localtime_r(&tsec
, &t
);
8986 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
8989 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
8990 (void) printf("%s %s", tbuf
,
8991 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
8992 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
8994 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
8997 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
8998 (void) printf("%s unrecognized record:\n",
9000 dump_nvlist(rec
, 4);
9003 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9004 zfs_history_event_names
[ievent
],
9005 (longlong_t
)fnvlist_lookup_uint64(
9006 rec
, ZPOOL_HIST_TXG
),
9007 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9008 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9011 (void) printf("%s [txg:%lld] %s", tbuf
,
9012 (longlong_t
)fnvlist_lookup_uint64(
9013 rec
, ZPOOL_HIST_TXG
),
9014 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9015 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9016 (void) printf(" %s (%llu)",
9017 fnvlist_lookup_string(rec
,
9019 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9022 (void) printf(" %s", fnvlist_lookup_string(rec
,
9023 ZPOOL_HIST_INT_STR
));
9024 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9027 (void) printf("%s ioctl %s\n", tbuf
,
9028 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9029 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9030 (void) printf(" input:\n");
9031 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9032 ZPOOL_HIST_INPUT_NVL
), 8);
9034 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9035 (void) printf(" output:\n");
9036 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9037 ZPOOL_HIST_OUTPUT_NVL
), 8);
9039 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9040 (void) printf(" errno: %lld\n",
9041 (longlong_t
)fnvlist_lookup_int64(rec
,
9047 (void) printf("%s unrecognized record:\n", tbuf
);
9048 dump_nvlist(rec
, 4);
9052 (void) printf("\n");
9055 (void) printf(" [");
9056 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9057 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9058 struct passwd
*pwd
= getpwuid(who
);
9059 (void) printf("user %d ", (int)who
);
9061 (void) printf("(%s) ", pwd
->pw_name
);
9063 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9064 (void) printf("on %s",
9065 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9067 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9068 (void) printf(":%s",
9069 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9073 (void) printf("\n");
9078 * Print out the command history for a specific pool.
9081 get_history_one(zpool_handle_t
*zhp
, void *data
)
9085 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9087 boolean_t eof
= B_FALSE
;
9089 cb
->first
= B_FALSE
;
9091 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9094 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9097 print_history_records(nvhis
, cb
);
9100 (void) printf("\n");
9106 * zpool history <pool>
9108 * Displays the history of commands that modified pools.
9111 zpool_do_history(int argc
, char **argv
)
9113 hist_cbdata_t cbdata
= { 0 };
9117 cbdata
.first
= B_TRUE
;
9119 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9122 cbdata
.longfmt
= B_TRUE
;
9125 cbdata
.internal
= B_TRUE
;
9128 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9136 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
9139 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9140 (void) fprintf(stderr
, gettext("no pools available\n"));
9147 typedef struct ev_opts
{
9152 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9156 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9158 char ctime_str
[26], str
[32], *ptr
;
9162 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9163 memset(str
, ' ', 32);
9164 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9165 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9166 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9167 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9168 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9170 (void) printf(gettext("%s\t"), str
);
9172 (void) printf(gettext("%s "), str
);
9174 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9175 (void) printf(gettext("%s\n"), ptr
);
9179 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9183 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9184 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9186 data_type_t type
= nvpair_type(nvp
);
9187 const char *name
= nvpair_name(nvp
);
9197 printf(gettext("%*s%s = "), depth
, "", name
);
9200 case DATA_TYPE_BOOLEAN
:
9201 printf(gettext("%s"), "1");
9204 case DATA_TYPE_BOOLEAN_VALUE
:
9205 (void) nvpair_value_boolean_value(nvp
, &b
);
9206 printf(gettext("%s"), b
? "1" : "0");
9209 case DATA_TYPE_BYTE
:
9210 (void) nvpair_value_byte(nvp
, &i8
);
9211 printf(gettext("0x%x"), i8
);
9214 case DATA_TYPE_INT8
:
9215 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9216 printf(gettext("0x%x"), i8
);
9219 case DATA_TYPE_UINT8
:
9220 (void) nvpair_value_uint8(nvp
, &i8
);
9221 printf(gettext("0x%x"), i8
);
9224 case DATA_TYPE_INT16
:
9225 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9226 printf(gettext("0x%x"), i16
);
9229 case DATA_TYPE_UINT16
:
9230 (void) nvpair_value_uint16(nvp
, &i16
);
9231 printf(gettext("0x%x"), i16
);
9234 case DATA_TYPE_INT32
:
9235 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9236 printf(gettext("0x%x"), i32
);
9239 case DATA_TYPE_UINT32
:
9240 (void) nvpair_value_uint32(nvp
, &i32
);
9241 printf(gettext("0x%x"), i32
);
9244 case DATA_TYPE_INT64
:
9245 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9246 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9249 case DATA_TYPE_UINT64
:
9250 (void) nvpair_value_uint64(nvp
, &i64
);
9252 * translate vdev state values to readable
9253 * strings to aide zpool events consumers
9256 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9258 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9259 printf(gettext("\"%s\" (0x%llx)"),
9260 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9263 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9267 case DATA_TYPE_HRTIME
:
9268 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9269 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9272 case DATA_TYPE_STRING
:
9273 (void) nvpair_value_string(nvp
, &str
);
9274 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9277 case DATA_TYPE_NVLIST
:
9278 printf(gettext("(embedded nvlist)\n"));
9279 (void) nvpair_value_nvlist(nvp
, &cnv
);
9280 zpool_do_events_nvprint(cnv
, depth
+ 8);
9281 printf(gettext("%*s(end %s)"), depth
, "", name
);
9284 case DATA_TYPE_NVLIST_ARRAY
: {
9288 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9289 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9290 for (i
= 0; i
< nelem
; i
++) {
9291 printf(gettext("%*s%s[%d] = %s\n"),
9292 depth
, "", name
, i
, "(embedded nvlist)");
9293 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9294 printf(gettext("%*s(end %s[%i])\n"),
9295 depth
, "", name
, i
);
9297 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9301 case DATA_TYPE_INT8_ARRAY
: {
9305 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9306 for (i
= 0; i
< nelem
; i
++)
9307 printf(gettext("0x%x "), val
[i
]);
9312 case DATA_TYPE_UINT8_ARRAY
: {
9316 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9317 for (i
= 0; i
< nelem
; i
++)
9318 printf(gettext("0x%x "), val
[i
]);
9323 case DATA_TYPE_INT16_ARRAY
: {
9327 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9328 for (i
= 0; i
< nelem
; i
++)
9329 printf(gettext("0x%x "), val
[i
]);
9334 case DATA_TYPE_UINT16_ARRAY
: {
9338 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9339 for (i
= 0; i
< nelem
; i
++)
9340 printf(gettext("0x%x "), val
[i
]);
9345 case DATA_TYPE_INT32_ARRAY
: {
9349 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9350 for (i
= 0; i
< nelem
; i
++)
9351 printf(gettext("0x%x "), val
[i
]);
9356 case DATA_TYPE_UINT32_ARRAY
: {
9360 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9361 for (i
= 0; i
< nelem
; i
++)
9362 printf(gettext("0x%x "), val
[i
]);
9367 case DATA_TYPE_INT64_ARRAY
: {
9371 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9372 for (i
= 0; i
< nelem
; i
++)
9373 printf(gettext("0x%llx "),
9374 (u_longlong_t
)val
[i
]);
9379 case DATA_TYPE_UINT64_ARRAY
: {
9383 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9384 for (i
= 0; i
< nelem
; i
++)
9385 printf(gettext("0x%llx "),
9386 (u_longlong_t
)val
[i
]);
9391 case DATA_TYPE_STRING_ARRAY
: {
9395 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9396 for (i
= 0; i
< nelem
; i
++)
9397 printf(gettext("\"%s\" "),
9398 str
[i
] ? str
[i
] : "<NULL>");
9403 case DATA_TYPE_BOOLEAN_ARRAY
:
9404 case DATA_TYPE_BYTE_ARRAY
:
9405 case DATA_TYPE_DOUBLE
:
9406 case DATA_TYPE_DONTCARE
:
9407 case DATA_TYPE_UNKNOWN
:
9408 printf(gettext("<unknown>"));
9412 printf(gettext("\n"));
9417 zpool_do_events_next(ev_opts_t
*opts
)
9420 int zevent_fd
, ret
, dropped
;
9423 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9424 VERIFY(zevent_fd
>= 0);
9426 if (!opts
->scripted
)
9427 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9430 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9431 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9432 if (ret
|| nvl
== NULL
)
9436 (void) printf(gettext("dropped %d events\n"), dropped
);
9438 if (strlen(opts
->poolname
) > 0 &&
9439 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9440 strcmp(opts
->poolname
, pool
) != 0)
9443 zpool_do_events_short(nvl
, opts
);
9445 if (opts
->verbose
) {
9446 zpool_do_events_nvprint(nvl
, 8);
9447 printf(gettext("\n"));
9449 (void) fflush(stdout
);
9454 VERIFY(0 == close(zevent_fd
));
9460 zpool_do_events_clear(ev_opts_t
*opts
)
9464 ret
= zpool_events_clear(g_zfs
, &count
);
9466 (void) printf(gettext("cleared %d events\n"), count
);
9472 * zpool events [-vHf [pool] | -c]
9474 * Displays events logs by ZFS.
9477 zpool_do_events(int argc
, char **argv
)
9479 ev_opts_t opts
= { 0 };
9484 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9499 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9508 (void) fprintf(stderr
, gettext("too many arguments\n"));
9510 } else if (argc
== 1) {
9511 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9512 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9513 (void) fprintf(stderr
,
9514 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9519 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9521 (void) fprintf(stderr
,
9522 gettext("invalid options combined with -c\n"));
9527 ret
= zpool_do_events_clear(&opts
);
9529 ret
= zpool_do_events_next(&opts
);
9535 get_callback(zpool_handle_t
*zhp
, void *data
)
9537 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9538 char value
[MAXNAMELEN
];
9539 zprop_source_t srctype
;
9542 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
9545 * Skip the special fake placeholder. This will also skip
9546 * over the name property when 'all' is specified.
9548 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9549 pl
== cbp
->cb_proplist
)
9552 if (pl
->pl_prop
== ZPROP_INVAL
&&
9553 (zpool_prop_feature(pl
->pl_user_prop
) ||
9554 zpool_prop_unsupported(pl
->pl_user_prop
))) {
9555 srctype
= ZPROP_SRC_LOCAL
;
9557 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
9558 value
, sizeof (value
)) == 0) {
9559 zprop_print_one_property(zpool_get_name(zhp
),
9560 cbp
, pl
->pl_user_prop
, value
, srctype
,
9564 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
9565 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
9568 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
9569 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
9577 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9579 * -H Scripted mode. Don't display headers, and separate properties
9581 * -o List of columns to display. Defaults to
9582 * "name,property,value,source".
9583 * -p Display values in parsable (exact) format.
9585 * Get properties of pools in the system. Output space statistics
9586 * for each one as well as other attributes.
9589 zpool_do_get(int argc
, char **argv
)
9591 zprop_get_cbdata_t cb
= { 0 };
9592 zprop_list_t fake_name
= { 0 };
9597 cb
.cb_first
= B_TRUE
;
9600 * Set up default columns and sources.
9602 cb
.cb_sources
= ZPROP_SRC_ALL
;
9603 cb
.cb_columns
[0] = GET_COL_NAME
;
9604 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9605 cb
.cb_columns
[2] = GET_COL_VALUE
;
9606 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9607 cb
.cb_type
= ZFS_TYPE_POOL
;
9610 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
9613 cb
.cb_literal
= B_TRUE
;
9616 cb
.cb_scripted
= B_TRUE
;
9619 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
9621 while (*optarg
!= '\0') {
9622 static char *col_subopts
[] =
9623 { "name", "property", "value", "source",
9626 if (i
== ZFS_GET_NCOLS
) {
9627 (void) fprintf(stderr
, gettext("too "
9628 "many fields given to -o "
9633 switch (getsubopt(&optarg
, col_subopts
,
9636 cb
.cb_columns
[i
++] = GET_COL_NAME
;
9639 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
9642 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
9645 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
9649 (void) fprintf(stderr
,
9650 gettext("\"all\" conflicts "
9651 "with specific fields "
9652 "given to -o option\n"));
9655 cb
.cb_columns
[0] = GET_COL_NAME
;
9656 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9657 cb
.cb_columns
[2] = GET_COL_VALUE
;
9658 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9662 (void) fprintf(stderr
,
9663 gettext("invalid column name "
9670 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9680 (void) fprintf(stderr
, gettext("missing property "
9685 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
9686 ZFS_TYPE_POOL
) != 0)
9692 if (cb
.cb_proplist
!= NULL
) {
9693 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
9694 fake_name
.pl_width
= strlen(gettext("NAME"));
9695 fake_name
.pl_next
= cb
.cb_proplist
;
9696 cb
.cb_proplist
= &fake_name
;
9699 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
9702 if (cb
.cb_proplist
== &fake_name
)
9703 zprop_free_list(fake_name
.pl_next
);
9705 zprop_free_list(cb
.cb_proplist
);
9710 typedef struct set_cbdata
{
9713 boolean_t cb_any_successful
;
9717 set_callback(zpool_handle_t
*zhp
, void *data
)
9720 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
9722 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
9725 cb
->cb_any_successful
= B_TRUE
;
9731 zpool_do_set(int argc
, char **argv
)
9733 set_cbdata_t cb
= { 0 };
9736 if (argc
> 1 && argv
[1][0] == '-') {
9737 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9743 (void) fprintf(stderr
, gettext("missing property=value "
9749 (void) fprintf(stderr
, gettext("missing pool name\n"));
9754 (void) fprintf(stderr
, gettext("too many pool names\n"));
9758 cb
.cb_propname
= argv
[1];
9759 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
9760 if (cb
.cb_value
== NULL
) {
9761 (void) fprintf(stderr
, gettext("missing value in "
9762 "property=value argument\n"));
9766 *(cb
.cb_value
) = '\0';
9769 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
9775 /* Add up the total number of bytes left to initialize/trim across all vdevs */
9777 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
9779 uint64_t bytes_remaining
;
9784 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
9785 activity
== ZPOOL_WAIT_TRIM
);
9787 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
9788 (uint64_t **)&vs
, &c
) == 0);
9790 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
9791 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
9792 bytes_remaining
= vs
->vs_initialize_bytes_est
-
9793 vs
->vs_initialize_bytes_done
;
9794 else if (activity
== ZPOOL_WAIT_TRIM
&&
9795 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
9796 bytes_remaining
= vs
->vs_trim_bytes_est
-
9797 vs
->vs_trim_bytes_done
;
9799 bytes_remaining
= 0;
9801 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9802 &child
, &children
) != 0)
9805 for (c
= 0; c
< children
; c
++)
9806 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
9808 return (bytes_remaining
);
9811 /* Add up the total number of bytes left to rebuild across top-level vdevs */
9813 vdev_activity_top_remaining(nvlist_t
*nv
)
9815 uint64_t bytes_remaining
= 0;
9820 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9821 &child
, &children
) != 0)
9824 for (uint_t c
= 0; c
< children
; c
++) {
9825 vdev_rebuild_stat_t
*vrs
;
9828 error
= nvlist_lookup_uint64_array(child
[c
],
9829 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
9831 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
9832 bytes_remaining
+= (vrs
->vrs_bytes_est
-
9833 vrs
->vrs_bytes_rebuilt
);
9838 return (bytes_remaining
);
9841 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
9843 vdev_any_spare_replacing(nvlist_t
*nv
)
9849 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
9851 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
9852 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
9853 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
9857 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9858 &child
, &children
) != 0)
9861 for (c
= 0; c
< children
; c
++) {
9862 if (vdev_any_spare_replacing(child
[c
]))
9869 typedef struct wait_data
{
9871 boolean_t wd_scripted
;
9873 boolean_t wd_headers_once
;
9874 boolean_t wd_should_exit
;
9875 /* Which activities to wait for */
9876 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
9878 pthread_cond_t wd_cv
;
9879 pthread_mutex_t wd_mutex
;
9883 * Print to stdout a single line, containing one column for each activity that
9884 * we are waiting for specifying how many bytes of work are left for that
9888 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
9890 nvlist_t
*config
, *nvroot
;
9893 pool_checkpoint_stat_t
*pcs
= NULL
;
9894 pool_scan_stat_t
*pss
= NULL
;
9895 pool_removal_stat_t
*prs
= NULL
;
9896 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
9897 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
9898 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
9900 /* Calculate the width of each column */
9901 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9903 * Make sure we have enough space in the col for pretty-printed
9904 * numbers and for the column header, and then leave a couple
9905 * spaces between cols for readability.
9907 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
9910 /* Print header if appropriate */
9911 int term_height
= terminal_height();
9912 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
9913 row
% (term_height
-1) == 0);
9914 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
9915 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9916 if (wd
->wd_enabled
[i
])
9917 (void) printf("%*s", col_widths
[i
], headers
[i
]);
9919 (void) printf("\n");
9922 /* Bytes of work remaining in each activity */
9923 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
9925 bytes_rem
[ZPOOL_WAIT_FREE
] =
9926 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
9928 config
= zpool_get_config(zhp
, NULL
);
9929 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
9931 (void) nvlist_lookup_uint64_array(nvroot
,
9932 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
9933 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
9934 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
9936 (void) nvlist_lookup_uint64_array(nvroot
,
9937 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
9938 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
9939 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
9942 (void) nvlist_lookup_uint64_array(nvroot
,
9943 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
9944 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
9945 pss
->pss_pass_scrub_pause
== 0) {
9946 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
9947 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
9948 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
9950 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
9951 } else if (check_rebuilding(nvroot
, NULL
)) {
9952 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
9953 vdev_activity_top_remaining(nvroot
);
9956 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
9957 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
9958 bytes_rem
[ZPOOL_WAIT_TRIM
] =
9959 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
9962 * A replace finishes after resilvering finishes, so the amount of work
9963 * left for a replace is the same as for resilvering.
9965 * It isn't quite correct to say that if we have any 'spare' or
9966 * 'replacing' vdevs and a resilver is happening, then a replace is in
9967 * progress, like we do here. When a hot spare is used, the faulted vdev
9968 * is not removed after the hot spare is resilvered, so parent 'spare'
9969 * vdev is not removed either. So we could have a 'spare' vdev, but be
9970 * resilvering for a different reason. However, we use it as a heuristic
9971 * because we don't have access to the DTLs, which could tell us whether
9972 * or not we have really finished resilvering a hot spare.
9974 if (vdev_any_spare_replacing(nvroot
))
9975 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
9977 if (timestamp_fmt
!= NODATE
)
9978 print_timestamp(timestamp_fmt
);
9980 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9982 if (!wd
->wd_enabled
[i
])
9986 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
9989 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
9991 if (wd
->wd_scripted
)
9992 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
9994 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
9996 (void) printf("\n");
9997 (void) fflush(stdout
);
10001 wait_status_thread(void *arg
)
10003 wait_data_t
*wd
= (wait_data_t
*)arg
;
10004 zpool_handle_t
*zhp
;
10006 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10007 return (void *)(1);
10009 for (int row
= 0; ; row
++) {
10011 struct timespec timeout
;
10013 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10015 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10016 zpool_props_refresh(zhp
) != 0) {
10018 return (void *)(uintptr_t)(missing
? 0 : 1);
10021 print_wait_status_row(wd
, zhp
, row
);
10023 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10024 long nanos
= timeout
.tv_nsec
+
10025 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10026 if (nanos
>= NANOSEC
) {
10028 timeout
.tv_nsec
= nanos
- NANOSEC
;
10030 timeout
.tv_nsec
= nanos
;
10032 pthread_mutex_lock(&wd
->wd_mutex
);
10033 if (!wd
->wd_should_exit
)
10034 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10036 pthread_mutex_unlock(&wd
->wd_mutex
);
10038 break; /* signaled by main thread */
10039 } else if (ret
!= ETIMEDOUT
) {
10040 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10041 "failed: %s\n"), strerror(ret
));
10043 return (void *)(uintptr_t)(1);
10048 return (void *)(0);
10052 zpool_do_wait(int argc
, char **argv
)
10054 boolean_t verbose
= B_FALSE
;
10058 unsigned long count
;
10059 pthread_t status_thr
;
10061 zpool_handle_t
*zhp
;
10064 wd
.wd_scripted
= B_FALSE
;
10065 wd
.wd_exact
= B_FALSE
;
10066 wd
.wd_headers_once
= B_FALSE
;
10067 wd
.wd_should_exit
= B_FALSE
;
10069 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10070 pthread_cond_init(&wd
.wd_cv
, NULL
);
10072 /* By default, wait for all types of activity. */
10073 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10074 wd
.wd_enabled
[i
] = B_TRUE
;
10076 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10079 wd
.wd_scripted
= B_TRUE
;
10082 wd
.wd_headers_once
= B_TRUE
;
10085 wd
.wd_exact
= B_TRUE
;
10088 get_timestamp_arg(*optarg
);
10092 static char *col_subopts
[] = { "discard", "free",
10093 "initialize", "replace", "remove", "resilver",
10094 "scrub", "trim", NULL
};
10096 /* Reset activities array */
10097 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10098 while (*optarg
!= '\0') {
10099 int activity
= getsubopt(&optarg
, col_subopts
,
10102 if (activity
< 0) {
10103 (void) fprintf(stderr
,
10104 gettext("invalid activity '%s'\n"),
10109 wd
.wd_enabled
[activity
] = B_TRUE
;
10114 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10123 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10125 /* This subcmd only accepts an interval, not a count */
10126 (void) fprintf(stderr
, gettext("too many arguments\n"));
10130 if (wd
.wd_interval
!= 0)
10134 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10138 (void) fprintf(stderr
, gettext("too many arguments\n"));
10142 wd
.wd_poolname
= argv
[0];
10144 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10149 * We use a separate thread for printing status updates because
10150 * the main thread will call lzc_wait(), which blocks as long
10151 * as an activity is in progress, which can be a long time.
10153 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10155 (void) fprintf(stderr
, gettext("failed to create status"
10156 "thread: %s\n"), strerror(errno
));
10163 * Loop over all activities that we are supposed to wait for until none
10164 * of them are in progress. Note that this means we can end up waiting
10165 * for more activities to complete than just those that were in progress
10166 * when we began waiting; if an activity we are interested in begins
10167 * while we are waiting for another activity, we will wait for both to
10168 * complete before exiting.
10171 boolean_t missing
= B_FALSE
;
10172 boolean_t any_waited
= B_FALSE
;
10174 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10177 if (!wd
.wd_enabled
[i
])
10180 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10181 if (error
!= 0 || missing
)
10184 any_waited
= (any_waited
|| waited
);
10187 if (error
!= 0 || missing
|| !any_waited
)
10195 pthread_mutex_lock(&wd
.wd_mutex
);
10196 wd
.wd_should_exit
= B_TRUE
;
10197 pthread_cond_signal(&wd
.wd_cv
);
10198 pthread_mutex_unlock(&wd
.wd_mutex
);
10199 (void) pthread_join(status_thr
, (void *)&status
);
10204 pthread_mutex_destroy(&wd
.wd_mutex
);
10205 pthread_cond_destroy(&wd
.wd_cv
);
10210 find_command_idx(char *command
, int *idx
)
10214 for (i
= 0; i
< NCOMMAND
; i
++) {
10215 if (command_table
[i
].name
== NULL
)
10218 if (strcmp(command
, command_table
[i
].name
) == 0) {
10227 * Display version message
10230 zpool_do_version(int argc
, char **argv
)
10232 if (zfs_version_print() == -1)
10239 main(int argc
, char **argv
)
10246 (void) setlocale(LC_ALL
, "");
10247 (void) setlocale(LC_NUMERIC
, "C");
10248 (void) textdomain(TEXT_DOMAIN
);
10254 * Make sure the user has specified some command.
10257 (void) fprintf(stderr
, gettext("missing command\n"));
10264 * Special case '-?'
10266 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10270 * Special case '-V|--version'
10272 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10273 return (zpool_do_version(argc
, argv
));
10275 if ((g_zfs
= libzfs_init()) == NULL
) {
10276 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10280 libzfs_print_on_error(g_zfs
, B_TRUE
);
10282 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10285 * Many commands modify input strings for string parsing reasons.
10286 * We create a copy to protect the original argv.
10288 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10289 for (i
= 0; i
< argc
; i
++)
10290 newargv
[i
] = strdup(argv
[i
]);
10291 newargv
[argc
] = NULL
;
10294 * Run the appropriate command.
10296 if (find_command_idx(cmdname
, &i
) == 0) {
10297 current_command
= &command_table
[i
];
10298 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10299 } else if (strchr(cmdname
, '=')) {
10300 verify(find_command_idx("set", &i
) == 0);
10301 current_command
= &command_table
[i
];
10302 ret
= command_table
[i
].func(argc
, newargv
);
10303 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10305 * 'freeze' is a vile debugging abomination, so we treat
10308 zfs_cmd_t zc
= {"\0"};
10310 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10311 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10313 (void) fprintf(stderr
,
10314 gettext("failed to freeze pool: %d\n"), errno
);
10320 (void) fprintf(stderr
, gettext("unrecognized "
10321 "command '%s'\n"), cmdname
);
10326 for (i
= 0; i
< argc
; i
++)
10330 if (ret
== 0 && log_history
)
10331 (void) zpool_log_history(g_zfs
, history_str
);
10333 libzfs_fini(g_zfs
);
10336 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10337 * for the purposes of running ::findleaks.
10339 if (getenv("ZFS_ABORT") != NULL
) {
10340 (void) printf("dumping core by request\n");