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, 2018 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
);
1594 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1595 (void) fprintf(stderr
, gettext("pool name may have "
1601 nvlist_free(nvroot
);
1602 nvlist_free(fsprops
);
1606 nvlist_free(fsprops
);
1613 * zpool destroy <pool>
1615 * -f Forcefully unmount any datasets
1617 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1620 zpool_do_destroy(int argc
, char **argv
)
1622 boolean_t force
= B_FALSE
;
1625 zpool_handle_t
*zhp
;
1629 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1635 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1644 /* check arguments */
1646 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1650 (void) fprintf(stderr
, gettext("too many arguments\n"));
1656 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1658 * As a special case, check for use of '/' in the name, and
1659 * direct the user to use 'zfs destroy' instead.
1661 if (strchr(pool
, '/') != NULL
)
1662 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1663 "destroy a dataset\n"));
1667 if (zpool_disable_datasets(zhp
, force
) != 0) {
1668 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1669 "could not unmount datasets\n"), zpool_get_name(zhp
));
1674 /* The history must be logged as part of the export */
1675 log_history
= B_FALSE
;
1677 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1684 typedef struct export_cbdata
{
1686 boolean_t hardforce
;
1693 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1695 export_cbdata_t
*cb
= data
;
1697 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1700 /* The history must be logged as part of the export */
1701 log_history
= B_FALSE
;
1703 if (cb
->hardforce
) {
1704 if (zpool_export_force(zhp
, history_str
) != 0)
1706 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1714 * zpool export [-f] <pool> ...
1716 * -a Export all pools
1717 * -f Forcefully unmount datasets
1719 * Export the given pools. By default, the command will attempt to cleanly
1720 * unmount any active datasets within the pool. If the '-f' flag is specified,
1721 * then the datasets will be forcefully unmounted.
1724 zpool_do_export(int argc
, char **argv
)
1727 boolean_t do_all
= B_FALSE
;
1728 boolean_t force
= B_FALSE
;
1729 boolean_t hardforce
= B_FALSE
;
1733 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1745 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1752 cb
.hardforce
= hardforce
;
1758 (void) fprintf(stderr
, gettext("too many arguments\n"));
1762 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1763 zpool_export_one
, &cb
));
1766 /* check arguments */
1768 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1772 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1778 * Given a vdev configuration, determine the maximum width needed for the device
1782 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1790 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1791 if (strlen(name
) + depth
> max
)
1792 max
= strlen(name
) + depth
;
1796 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1797 &child
, &children
) == 0) {
1798 for (c
= 0; c
< children
; c
++)
1799 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1800 max
, name_flags
)) > max
)
1804 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1805 &child
, &children
) == 0) {
1806 for (c
= 0; c
< children
; c
++)
1807 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1808 max
, name_flags
)) > max
)
1812 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1813 &child
, &children
) == 0) {
1814 for (c
= 0; c
< children
; c
++)
1815 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1816 max
, name_flags
)) > max
)
1823 typedef struct spare_cbdata
{
1825 zpool_handle_t
*cb_zhp
;
1829 find_vdev(nvlist_t
*nv
, uint64_t search
)
1835 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1839 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1840 &child
, &children
) == 0) {
1841 for (c
= 0; c
< children
; c
++)
1842 if (find_vdev(child
[c
], search
))
1850 find_spare(zpool_handle_t
*zhp
, void *data
)
1852 spare_cbdata_t
*cbp
= data
;
1853 nvlist_t
*config
, *nvroot
;
1855 config
= zpool_get_config(zhp
, NULL
);
1856 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1859 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1868 typedef struct status_cbdata
{
1872 boolean_t cb_allpools
;
1873 boolean_t cb_verbose
;
1874 boolean_t cb_literal
;
1875 boolean_t cb_explain
;
1877 boolean_t cb_dedup_stats
;
1878 boolean_t cb_print_status
;
1879 boolean_t cb_print_slow_ios
;
1880 boolean_t cb_print_vdev_init
;
1881 boolean_t cb_print_vdev_trim
;
1882 vdev_cmd_data_list_t
*vcdl
;
1885 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1887 is_blank_str(char *str
)
1889 while (str
!= NULL
&& *str
!= '\0') {
1897 /* Print command output lines for specific vdev in a specific pool */
1899 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1901 vdev_cmd_data_t
*data
;
1905 for (i
= 0; i
< vcdl
->count
; i
++) {
1906 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1907 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1908 /* Not the vdev we're looking for */
1912 data
= &vcdl
->data
[i
];
1913 /* Print out all the output values for this vdev */
1914 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1916 /* Does this vdev have values for this column? */
1917 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1918 if (strcmp(data
->cols
[k
],
1919 vcdl
->uniq_cols
[j
]) == 0) {
1920 /* yes it does, record the value */
1921 val
= data
->lines
[k
];
1926 * Mark empty values with dashes to make output
1929 if (is_blank_str(val
))
1932 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1933 if (j
< vcdl
->uniq_cols_cnt
- 1)
1937 /* Print out any values that aren't in a column at the end */
1938 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1939 /* Did we have any columns? If so print a spacer. */
1940 if (vcdl
->uniq_cols_cnt
> 0)
1943 val
= data
->lines
[j
];
1944 printf("%s", val
? val
: "");
1951 * Print vdev initialization status for leaves
1954 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
1957 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
1958 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
1959 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
1960 !vs
->vs_scan_removing
) {
1963 struct tm zaction_ts
;
1965 time_t t
= vs
->vs_initialize_action_time
;
1966 int initialize_pct
= 100;
1967 if (vs
->vs_initialize_state
!=
1968 VDEV_INITIALIZE_COMPLETE
) {
1969 initialize_pct
= (vs
->vs_initialize_bytes_done
*
1970 100 / (vs
->vs_initialize_bytes_est
+ 1));
1973 (void) localtime_r(&t
, &zaction_ts
);
1974 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
1976 switch (vs
->vs_initialize_state
) {
1977 case VDEV_INITIALIZE_SUSPENDED
:
1978 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1979 gettext("suspended, started at"), tbuf
);
1981 case VDEV_INITIALIZE_ACTIVE
:
1982 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1983 gettext("started at"), tbuf
);
1985 case VDEV_INITIALIZE_COMPLETE
:
1986 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
1987 gettext("completed at"), tbuf
);
1991 (void) printf(gettext(" (%d%% initialized%s)"),
1992 initialize_pct
, zbuf
);
1994 (void) printf(gettext(" (uninitialized)"));
1996 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
1997 (void) printf(gettext(" (initializing)"));
2002 * Print vdev TRIM status for leaves
2005 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2008 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2009 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2010 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2011 !vs
->vs_scan_removing
) {
2014 struct tm zaction_ts
;
2016 time_t t
= vs
->vs_trim_action_time
;
2018 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2019 trim_pct
= (vs
->vs_trim_bytes_done
*
2020 100 / (vs
->vs_trim_bytes_est
+ 1));
2023 (void) localtime_r(&t
, &zaction_ts
);
2024 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2026 switch (vs
->vs_trim_state
) {
2027 case VDEV_TRIM_SUSPENDED
:
2028 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2029 gettext("suspended, started at"), tbuf
);
2031 case VDEV_TRIM_ACTIVE
:
2032 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2033 gettext("started at"), tbuf
);
2035 case VDEV_TRIM_COMPLETE
:
2036 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2037 gettext("completed at"), tbuf
);
2041 (void) printf(gettext(" (%d%% trimmed%s)"),
2043 } else if (vs
->vs_trim_notsup
) {
2044 (void) printf(gettext(" (trim unsupported)"));
2046 (void) printf(gettext(" (untrimmed)"));
2048 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2049 (void) printf(gettext(" (trimming)"));
2054 * Return the color associated with a health string. This includes returning
2055 * NULL for no color change.
2058 health_str_to_color(const char *health
)
2060 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2061 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2062 strcmp(health
, gettext("UNAVAIL")) == 0) {
2066 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2067 strcmp(health
, gettext("DEGRADED")) == 0 ||
2068 strcmp(health
, gettext("REMOVED")) == 0) {
2069 return (ANSI_YELLOW
);
2076 * Print out configuration state as requested by status_callback.
2079 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2080 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2082 nvlist_t
**child
, *root
;
2083 uint_t c
, i
, children
;
2084 pool_scan_stat_t
*ps
= NULL
;
2086 char rbuf
[6], wbuf
[6], cbuf
[6];
2088 uint64_t notpresent
;
2089 spare_cbdata_t spare_cb
;
2093 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2095 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2096 &child
, &children
) != 0)
2099 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2100 (uint64_t **)&vs
, &c
) == 0);
2102 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2104 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2107 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2111 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2114 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2115 state
= gettext("INUSE");
2116 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2117 state
= gettext("AVAIL");
2120 printf_color(health_str_to_color(state
),
2121 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2125 if (vs
->vs_read_errors
)
2128 if (vs
->vs_write_errors
)
2131 if (vs
->vs_checksum_errors
)
2134 if (cb
->cb_literal
) {
2136 printf_color(rcolor
, "%5llu",
2137 (u_longlong_t
)vs
->vs_read_errors
);
2139 printf_color(wcolor
, "%5llu",
2140 (u_longlong_t
)vs
->vs_write_errors
);
2142 printf_color(ccolor
, "%5llu",
2143 (u_longlong_t
)vs
->vs_checksum_errors
);
2145 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2146 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2147 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2150 printf_color(rcolor
, "%5s", rbuf
);
2152 printf_color(wcolor
, "%5s", wbuf
);
2154 printf_color(ccolor
, "%5s", cbuf
);
2156 if (cb
->cb_print_slow_ios
) {
2157 if (children
== 0) {
2158 /* Only leafs vdevs have slow IOs */
2159 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2162 snprintf(rbuf
, sizeof (rbuf
), "-");
2166 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2168 printf(" %5s", rbuf
);
2172 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2173 ¬present
) == 0) {
2174 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2175 (void) printf(" %s %s", gettext("was"), path
);
2176 } else if (vs
->vs_aux
!= 0) {
2178 color_start(ANSI_RED
);
2179 switch (vs
->vs_aux
) {
2180 case VDEV_AUX_OPEN_FAILED
:
2181 (void) printf(gettext("cannot open"));
2184 case VDEV_AUX_BAD_GUID_SUM
:
2185 (void) printf(gettext("missing device"));
2188 case VDEV_AUX_NO_REPLICAS
:
2189 (void) printf(gettext("insufficient replicas"));
2192 case VDEV_AUX_VERSION_NEWER
:
2193 (void) printf(gettext("newer version"));
2196 case VDEV_AUX_UNSUP_FEAT
:
2197 (void) printf(gettext("unsupported feature(s)"));
2200 case VDEV_AUX_SPARED
:
2201 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2202 &spare_cb
.cb_guid
) == 0);
2203 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2204 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2205 zpool_get_name(zhp
)) == 0)
2206 (void) printf(gettext("currently in "
2209 (void) printf(gettext("in use by "
2211 zpool_get_name(spare_cb
.cb_zhp
));
2212 zpool_close(spare_cb
.cb_zhp
);
2214 (void) printf(gettext("currently in use"));
2218 case VDEV_AUX_ERR_EXCEEDED
:
2219 (void) printf(gettext("too many errors"));
2222 case VDEV_AUX_IO_FAILURE
:
2223 (void) printf(gettext("experienced I/O failures"));
2226 case VDEV_AUX_BAD_LOG
:
2227 (void) printf(gettext("bad intent log"));
2230 case VDEV_AUX_EXTERNAL
:
2231 (void) printf(gettext("external device fault"));
2234 case VDEV_AUX_SPLIT_POOL
:
2235 (void) printf(gettext("split into new pool"));
2238 case VDEV_AUX_ACTIVE
:
2239 (void) printf(gettext("currently in use"));
2242 case VDEV_AUX_CHILDREN_OFFLINE
:
2243 (void) printf(gettext("all children offline"));
2247 (void) printf(gettext("corrupted data"));
2253 /* The root vdev has the scrub/resilver stats */
2254 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2255 ZPOOL_CONFIG_VDEV_TREE
);
2256 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2257 (uint64_t **)&ps
, &c
);
2259 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2260 if (vs
->vs_scan_processed
!= 0) {
2261 (void) printf(gettext(" (%s)"),
2262 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2263 "resilvering" : "repairing");
2264 } else if (vs
->vs_resilver_deferred
) {
2265 (void) printf(gettext(" (awaiting resilver)"));
2269 /* The top-level vdevs have the rebuild stats */
2270 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2272 if (vs
->vs_rebuild_processed
!= 0) {
2273 (void) printf(gettext(" (resilvering)"));
2277 if (cb
->vcdl
!= NULL
) {
2278 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2280 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2284 /* Display vdev initialization and trim status for leaves */
2285 if (children
== 0) {
2286 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2287 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2290 (void) printf("\n");
2292 for (c
= 0; c
< children
; c
++) {
2293 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2295 /* Don't print logs or holes here */
2296 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2298 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2300 if (islog
|| ishole
)
2302 /* Only print normal classes here */
2303 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2306 /* Provide vdev_rebuild_stats to children if available */
2308 (void) nvlist_lookup_uint64_array(nv
,
2309 ZPOOL_CONFIG_REBUILD_STATS
,
2310 (uint64_t **)&vrs
, &i
);
2313 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2314 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2315 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2322 * Print the configuration of an exported pool. Iterate over all vdevs in the
2323 * pool, printing out the name and status for each one.
2326 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2334 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2335 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2336 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2339 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2340 (uint64_t **)&vs
, &c
) == 0);
2342 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2343 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2345 if (vs
->vs_aux
!= 0) {
2348 switch (vs
->vs_aux
) {
2349 case VDEV_AUX_OPEN_FAILED
:
2350 (void) printf(gettext("cannot open"));
2353 case VDEV_AUX_BAD_GUID_SUM
:
2354 (void) printf(gettext("missing device"));
2357 case VDEV_AUX_NO_REPLICAS
:
2358 (void) printf(gettext("insufficient replicas"));
2361 case VDEV_AUX_VERSION_NEWER
:
2362 (void) printf(gettext("newer version"));
2365 case VDEV_AUX_UNSUP_FEAT
:
2366 (void) printf(gettext("unsupported feature(s)"));
2369 case VDEV_AUX_ERR_EXCEEDED
:
2370 (void) printf(gettext("too many errors"));
2373 case VDEV_AUX_ACTIVE
:
2374 (void) printf(gettext("currently in use"));
2377 case VDEV_AUX_CHILDREN_OFFLINE
:
2378 (void) printf(gettext("all children offline"));
2382 (void) printf(gettext("corrupted data"));
2386 (void) printf("\n");
2388 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2389 &child
, &children
) != 0)
2392 for (c
= 0; c
< children
; c
++) {
2393 uint64_t is_log
= B_FALSE
;
2395 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2399 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2402 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2403 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2404 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2408 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2409 &child
, &children
) == 0) {
2410 (void) printf(gettext("\tcache\n"));
2411 for (c
= 0; c
< children
; c
++) {
2412 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2414 (void) printf("\t %s\n", vname
);
2419 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2420 &child
, &children
) == 0) {
2421 (void) printf(gettext("\tspares\n"));
2422 for (c
= 0; c
< children
; c
++) {
2423 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2425 (void) printf("\t %s\n", vname
);
2432 * Print specialized class vdevs.
2434 * These are recorded as top level vdevs in the main pool child array
2435 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2436 * print_status_config() or print_import_config() to print the top level
2437 * class vdevs then any of their children (eg mirrored slogs) are printed
2438 * recursively - which works because only the top level vdev is marked.
2441 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2446 boolean_t printed
= B_FALSE
;
2448 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2450 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2454 for (c
= 0; c
< children
; c
++) {
2455 uint64_t is_log
= B_FALSE
;
2459 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2463 bias
= VDEV_ALLOC_CLASS_LOGS
;
2465 (void) nvlist_lookup_string(child
[c
],
2466 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2467 (void) nvlist_lookup_string(child
[c
],
2468 ZPOOL_CONFIG_TYPE
, &type
);
2471 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2473 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2477 (void) printf("\t%s\t\n", gettext(class));
2481 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2482 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2483 if (cb
->cb_print_status
)
2484 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2487 print_import_config(cb
, name
, child
[c
], 2);
2493 * Display the status for the given pool.
2496 show_import(nvlist_t
*config
)
2498 uint64_t pool_state
;
2502 uint64_t hostid
= 0;
2504 char *hostname
= "unknown";
2505 nvlist_t
*nvroot
, *nvinfo
;
2506 zpool_status_t reason
;
2507 zpool_errata_t errata
;
2511 status_cbdata_t cb
= { 0 };
2513 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2515 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2517 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2519 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2522 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2523 (uint64_t **)&vs
, &vsc
) == 0);
2524 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2526 reason
= zpool_import_status(config
, &msgid
, &errata
);
2528 (void) printf(gettext(" pool: %s\n"), name
);
2529 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2530 (void) printf(gettext(" state: %s"), health
);
2531 if (pool_state
== POOL_STATE_DESTROYED
)
2532 (void) printf(gettext(" (DESTROYED)"));
2533 (void) printf("\n");
2536 case ZPOOL_STATUS_MISSING_DEV_R
:
2537 case ZPOOL_STATUS_MISSING_DEV_NR
:
2538 case ZPOOL_STATUS_BAD_GUID_SUM
:
2539 printf_color(ANSI_BOLD
, gettext("status: "));
2540 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2541 "missing from the system.\n"));
2544 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2545 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2546 printf_color(ANSI_BOLD
, gettext("status: "));
2547 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2548 " corrupted data.\n"));
2551 case ZPOOL_STATUS_CORRUPT_DATA
:
2553 gettext(" status: The pool data is corrupted.\n"));
2556 case ZPOOL_STATUS_OFFLINE_DEV
:
2557 printf_color(ANSI_BOLD
, gettext("status: "));
2558 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2559 "are offlined.\n"));
2562 case ZPOOL_STATUS_CORRUPT_POOL
:
2563 printf_color(ANSI_BOLD
, gettext("status: "));
2564 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2568 case ZPOOL_STATUS_VERSION_OLDER
:
2569 printf_color(ANSI_BOLD
, gettext("status: "));
2570 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2571 "a legacy on-disk version.\n"));
2574 case ZPOOL_STATUS_VERSION_NEWER
:
2575 printf_color(ANSI_BOLD
, gettext("status: "));
2576 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2577 "an incompatible version.\n"));
2580 case ZPOOL_STATUS_FEAT_DISABLED
:
2581 printf_color(ANSI_BOLD
, gettext("status: "));
2582 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
2583 "not enabled on the pool.\n"));
2586 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2587 printf_color(ANSI_BOLD
, gettext("status: "));
2588 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2589 "feature(s) not supported on this system:\n"));
2590 color_start(ANSI_YELLOW
);
2591 zpool_print_unsup_feat(config
);
2595 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2596 printf_color(ANSI_BOLD
, gettext("status: "));
2597 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2598 "accessed in read-only mode on this system. It\n\tcannot be"
2599 " accessed in read-write mode because it uses the "
2600 "following\n\tfeature(s) not supported on this system:\n"));
2601 color_start(ANSI_YELLOW
);
2602 zpool_print_unsup_feat(config
);
2606 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2607 printf_color(ANSI_BOLD
, gettext("status: "));
2608 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2609 "imported by another system.\n"));
2612 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2613 printf_color(ANSI_BOLD
, gettext("status: "));
2614 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2615 "multihost property on. It cannot\n\tbe safely imported "
2616 "when the system hostid is not set.\n"));
2619 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2620 printf_color(ANSI_BOLD
, gettext("status: "));
2621 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2622 "by another system.\n"));
2625 case ZPOOL_STATUS_FAULTED_DEV_R
:
2626 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2627 printf_color(ANSI_BOLD
, gettext("status: "));
2628 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2632 case ZPOOL_STATUS_BAD_LOG
:
2633 printf_color(ANSI_BOLD
, gettext("status: "));
2634 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2638 case ZPOOL_STATUS_RESILVERING
:
2639 case ZPOOL_STATUS_REBUILDING
:
2640 printf_color(ANSI_BOLD
, gettext("status: "));
2641 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2642 "being resilvered.\n"));
2645 case ZPOOL_STATUS_ERRATA
:
2646 printf_color(ANSI_BOLD
, gettext("status: "));
2647 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2653 * No other status can be seen when importing pools.
2655 assert(reason
== ZPOOL_STATUS_OK
);
2659 * Print out an action according to the overall state of the pool.
2661 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2662 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2663 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2664 (void) printf(gettext(" action: The pool can be "
2665 "imported using its name or numeric identifier, "
2666 "though\n\tsome features will not be available "
2667 "without an explicit 'zpool upgrade'.\n"));
2668 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2669 (void) printf(gettext(" action: The pool can be "
2670 "imported using its name or numeric "
2671 "identifier and\n\tthe '-f' flag.\n"));
2672 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2674 case ZPOOL_ERRATA_NONE
:
2677 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2678 (void) printf(gettext(" action: The pool can "
2679 "be imported using its name or numeric "
2680 "identifier,\n\thowever there is a compat"
2681 "ibility issue which should be corrected"
2682 "\n\tby running 'zpool scrub'\n"));
2685 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2686 (void) printf(gettext(" action: The pool can"
2687 "not be imported with this version of ZFS "
2688 "due to\n\tan active asynchronous destroy. "
2689 "Revert to an earlier version\n\tand "
2690 "allow the destroy to complete before "
2694 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2695 (void) printf(gettext(" action: Existing "
2696 "encrypted datasets contain an on-disk "
2697 "incompatibility, which\n\tneeds to be "
2698 "corrected. Backup these datasets to new "
2699 "encrypted datasets\n\tand destroy the "
2703 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2704 (void) printf(gettext(" action: Existing "
2705 "encrypted snapshots and bookmarks contain "
2706 "an on-disk\n\tincompatibility. This may "
2707 "cause on-disk corruption if they are used"
2708 "\n\twith 'zfs recv'. To correct the "
2709 "issue, enable the bookmark_v2 feature.\n\t"
2710 "No additional action is needed if there "
2711 "are no encrypted snapshots or\n\t"
2712 "bookmarks. If preserving the encrypted "
2713 "snapshots and bookmarks is\n\trequired, "
2714 "use a non-raw send to backup and restore "
2715 "them. Alternately,\n\tthey may be removed"
2716 " to resolve the incompatibility.\n"));
2720 * All errata must contain an action message.
2725 (void) printf(gettext(" action: The pool can be "
2726 "imported using its name or numeric "
2729 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2730 (void) printf(gettext(" action: The pool can be imported "
2731 "despite missing or damaged devices. The\n\tfault "
2732 "tolerance of the pool may be compromised if imported.\n"));
2735 case ZPOOL_STATUS_VERSION_NEWER
:
2736 (void) printf(gettext(" action: The pool cannot be "
2737 "imported. Access the pool on a system running "
2738 "newer\n\tsoftware, or recreate the pool from "
2741 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2742 printf_color(ANSI_BOLD
, gettext("action: "));
2743 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2744 "imported. Access the pool on a system that "
2745 "supports\n\tthe required feature(s), or recreate "
2746 "the pool from backup.\n"));
2748 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2749 printf_color(ANSI_BOLD
, gettext("action: "));
2750 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2751 "imported in read-write mode. Import the pool "
2753 "\t\"-o readonly=on\", access the pool on a system "
2754 "that supports the\n\trequired feature(s), or "
2755 "recreate the pool from backup.\n"));
2757 case ZPOOL_STATUS_MISSING_DEV_R
:
2758 case ZPOOL_STATUS_MISSING_DEV_NR
:
2759 case ZPOOL_STATUS_BAD_GUID_SUM
:
2760 (void) printf(gettext(" action: The pool cannot be "
2761 "imported. Attach the missing\n\tdevices and try "
2764 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2765 VERIFY0(nvlist_lookup_nvlist(config
,
2766 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2768 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2769 hostname
= fnvlist_lookup_string(nvinfo
,
2770 ZPOOL_CONFIG_MMP_HOSTNAME
);
2772 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2773 hostid
= fnvlist_lookup_uint64(nvinfo
,
2774 ZPOOL_CONFIG_MMP_HOSTID
);
2776 (void) printf(gettext(" action: The pool must be "
2777 "exported from %s (hostid=%lx)\n\tbefore it "
2778 "can be safely imported.\n"), hostname
,
2779 (unsigned long) hostid
);
2781 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2782 (void) printf(gettext(" action: Set a unique system "
2783 "hostid with the zgenhostid(8) command.\n"));
2786 (void) printf(gettext(" action: The pool cannot be "
2787 "imported due to damaged devices or data.\n"));
2791 /* Print the comment attached to the pool. */
2792 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2793 (void) printf(gettext("comment: %s\n"), comment
);
2796 * If the state is "closed" or "can't open", and the aux state
2797 * is "corrupt data":
2799 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2800 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2801 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2802 if (pool_state
== POOL_STATE_DESTROYED
)
2803 (void) printf(gettext("\tThe pool was destroyed, "
2804 "but can be imported using the '-Df' flags.\n"));
2805 else if (pool_state
!= POOL_STATE_EXPORTED
)
2806 (void) printf(gettext("\tThe pool may be active on "
2807 "another system, but can be imported using\n\t"
2808 "the '-f' flag.\n"));
2811 if (msgid
!= NULL
) {
2812 (void) printf(gettext(
2813 " see: https://zfsonlinux.org/msg/%s\n"), msgid
);
2816 (void) printf(gettext(" config:\n\n"));
2818 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2820 if (cb
.cb_namewidth
< 10)
2821 cb
.cb_namewidth
= 10;
2823 print_import_config(&cb
, name
, nvroot
, 0);
2825 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
2826 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
2827 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
2829 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2830 (void) printf(gettext("\n\tAdditional devices are known to "
2831 "be part of this pool, though their\n\texact "
2832 "configuration cannot be determined.\n"));
2837 zfs_force_import_required(nvlist_t
*config
)
2840 uint64_t hostid
= 0;
2843 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2844 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2846 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2849 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2850 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2851 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2852 ZPOOL_CONFIG_MMP_STATE
);
2854 if (mmp_state
!= MMP_STATE_INACTIVE
)
2862 * Perform the import for the given configuration. This passes the heavy
2863 * lifting off to zpool_import_props(), and then mounts the datasets contained
2867 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2868 nvlist_t
*props
, int flags
)
2871 zpool_handle_t
*zhp
;
2875 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2876 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2878 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2879 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2880 "is formatted using an unsupported ZFS version\n"), name
);
2882 } else if (zfs_force_import_required(config
) &&
2883 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2884 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2887 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2888 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2889 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2890 ZPOOL_CONFIG_MMP_STATE
);
2892 if (mmp_state
== MMP_STATE_ACTIVE
) {
2893 char *hostname
= "<unknown>";
2894 uint64_t hostid
= 0;
2896 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2897 hostname
= fnvlist_lookup_string(nvinfo
,
2898 ZPOOL_CONFIG_MMP_HOSTNAME
);
2900 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2901 hostid
= fnvlist_lookup_uint64(nvinfo
,
2902 ZPOOL_CONFIG_MMP_HOSTID
);
2904 (void) fprintf(stderr
, gettext("cannot import '%s': "
2905 "pool is imported on %s (hostid: "
2906 "0x%lx)\nExport the pool on the other system, "
2907 "then run 'zpool import'.\n"),
2908 name
, hostname
, (unsigned long) hostid
);
2909 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2910 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2911 "pool has the multihost property on and the\n"
2912 "system's hostid is not set. Set a unique hostid "
2913 "with the zgenhostid(8) command.\n"), name
);
2915 char *hostname
= "<unknown>";
2916 uint64_t timestamp
= 0;
2917 uint64_t hostid
= 0;
2919 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2920 hostname
= fnvlist_lookup_string(config
,
2921 ZPOOL_CONFIG_HOSTNAME
);
2923 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2924 timestamp
= fnvlist_lookup_uint64(config
,
2925 ZPOOL_CONFIG_TIMESTAMP
);
2927 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2928 hostid
= fnvlist_lookup_uint64(config
,
2929 ZPOOL_CONFIG_HOSTID
);
2931 (void) fprintf(stderr
, gettext("cannot import '%s': "
2932 "pool was previously in use from another system.\n"
2933 "Last accessed by %s (hostid=%lx) at %s"
2934 "The pool can be imported, use 'zpool import -f' "
2935 "to import the pool.\n"), name
, hostname
,
2936 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2942 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2945 if (newname
!= NULL
)
2946 name
= (char *)newname
;
2948 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2952 * Loading keys is best effort. We don't want to return immediately
2953 * if it fails but we do want to give the error to the caller.
2955 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2956 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2961 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2962 !(flags
& ZFS_IMPORT_ONLY
) &&
2963 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2972 typedef struct target_exists_args
{
2973 const char *poolname
;
2975 } target_exists_args_t
;
2978 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
2980 target_exists_args_t
*args
= data
;
2981 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
2987 if (args
->poolname
!= NULL
) {
2990 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2992 if (strcmp(pool_name
, args
->poolname
) == 0)
2997 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2999 if (pool_guid
== args
->poolguid
)
3007 * zpool checkpoint <pool>
3008 * checkpoint --discard <pool>
3010 * -d Discard the checkpoint from a checkpointed
3013 * -w Wait for discarding a checkpoint to complete.
3016 * Checkpoints the specified pool, by taking a "snapshot" of its
3017 * current state. A pool can only have one checkpoint at a time.
3020 zpool_do_checkpoint(int argc
, char **argv
)
3022 boolean_t discard
, wait
;
3024 zpool_handle_t
*zhp
;
3027 struct option long_options
[] = {
3028 {"discard", no_argument
, NULL
, 'd'},
3029 {"wait", no_argument
, NULL
, 'w'},
3035 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3044 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3050 if (wait
&& !discard
) {
3051 (void) fprintf(stderr
, gettext("--wait only valid when "
3052 "--discard also specified\n"));
3060 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3065 (void) fprintf(stderr
, gettext("too many arguments\n"));
3071 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3072 /* As a special case, check for use of '/' in the name */
3073 if (strchr(pool
, '/') != NULL
)
3074 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3075 "doesn't work on datasets. To save the state "
3076 "of a dataset from a specific point in time "
3077 "please use 'zfs snapshot'\n"));
3082 err
= (zpool_discard_checkpoint(zhp
) != 0);
3083 if (err
== 0 && wait
)
3084 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3086 err
= (zpool_checkpoint(zhp
) != 0);
3094 #define CHECKPOINT_OPT 1024
3097 * zpool import [-d dir] [-D]
3098 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3099 * [-d dir | -c cachefile] [-f] -a
3100 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3101 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
3103 * -c Read pool information from a cachefile instead of searching
3106 * -d Scan in a specific directory, other than /dev/. More than
3107 * one directory can be specified using multiple '-d' options.
3109 * -D Scan for previously destroyed pools or import all or only
3110 * specified destroyed pools.
3112 * -R Temporarily import the pool, with all mountpoints relative to
3113 * the given root. The pool will remain exported when the machine
3116 * -V Import even in the presence of faulted vdevs. This is an
3117 * intentionally undocumented option for testing purposes, and
3118 * treats the pool configuration as complete, leaving any bad
3119 * vdevs in the FAULTED state. In other words, it does verbatim
3122 * -f Force import, even if it appears that the pool is active.
3124 * -F Attempt rewind if necessary.
3126 * -n See if rewind would work, but don't actually rewind.
3128 * -N Import the pool but don't mount datasets.
3130 * -T Specify a starting txg to use for import. This option is
3131 * intentionally undocumented option for testing purposes.
3133 * -a Import all pools found.
3135 * -l Load encryption keys while importing.
3137 * -o Set property=value and/or temporary mount options (without '=').
3139 * -s Scan using the default search path, the libblkid cache will
3142 * --rewind-to-checkpoint
3143 * Import the pool and revert back to the checkpoint.
3145 * The import command scans for pools to import, and import pools based on pool
3146 * name and GUID. The pool can also be renamed as part of the import process.
3149 zpool_do_import(int argc
, char **argv
)
3151 char **searchdirs
= NULL
;
3152 char *env
, *envdup
= NULL
;
3156 nvlist_t
*pools
= NULL
;
3157 boolean_t do_all
= B_FALSE
;
3158 boolean_t do_destroyed
= B_FALSE
;
3159 char *mntopts
= NULL
;
3162 uint64_t searchguid
= 0;
3163 char *searchname
= NULL
;
3165 nvlist_t
*found_config
;
3166 nvlist_t
*policy
= NULL
;
3167 nvlist_t
*props
= NULL
;
3169 int flags
= ZFS_IMPORT_NORMAL
;
3170 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3171 boolean_t dryrun
= B_FALSE
;
3172 boolean_t do_rewind
= B_FALSE
;
3173 boolean_t xtreme_rewind
= B_FALSE
;
3174 boolean_t do_scan
= B_FALSE
;
3175 boolean_t pool_exists
= B_FALSE
;
3176 uint64_t pool_state
, txg
= -1ULL;
3177 char *cachefile
= NULL
;
3178 importargs_t idata
= { 0 };
3181 struct option long_options
[] = {
3182 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3187 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3188 long_options
, NULL
)) != -1) {
3197 if (searchdirs
== NULL
) {
3198 searchdirs
= safe_malloc(sizeof (char *));
3200 char **tmp
= safe_malloc((nsearch
+ 1) *
3202 bcopy(searchdirs
, tmp
, nsearch
*
3207 searchdirs
[nsearch
++] = optarg
;
3210 do_destroyed
= B_TRUE
;
3213 flags
|= ZFS_IMPORT_ANY_HOST
;
3219 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3222 flags
|= ZFS_IMPORT_MISSING_LOG
;
3228 flags
|= ZFS_IMPORT_ONLY
;
3231 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3234 if (add_prop_list(optarg
, propval
,
3242 if (add_prop_list(zpool_prop_to_name(
3243 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3245 if (add_prop_list_default(zpool_prop_to_name(
3246 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3253 flags
|= ZFS_IMPORT_TEMP_NAME
;
3254 if (add_prop_list_default(zpool_prop_to_name(
3255 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3261 txg
= strtoull(optarg
, &endptr
, 0);
3262 if (errno
!= 0 || *endptr
!= '\0') {
3263 (void) fprintf(stderr
,
3264 gettext("invalid txg value\n"));
3267 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3270 flags
|= ZFS_IMPORT_VERBATIM
;
3273 xtreme_rewind
= B_TRUE
;
3275 case CHECKPOINT_OPT
:
3276 flags
|= ZFS_IMPORT_CHECKPOINT
;
3279 (void) fprintf(stderr
, gettext("missing argument for "
3280 "'%c' option\n"), optopt
);
3284 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3293 if (cachefile
&& nsearch
!= 0) {
3294 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3298 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3299 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3303 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3304 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3309 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3310 (void) fprintf(stderr
,
3311 gettext("-n or -X only meaningful with -F\n"));
3315 rewind_policy
= ZPOOL_TRY_REWIND
;
3317 rewind_policy
= ZPOOL_DO_REWIND
;
3319 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3321 /* In the future, we can capture further policy and include it here */
3322 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3323 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3324 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3325 rewind_policy
) != 0)
3328 /* check argument count */
3331 (void) fprintf(stderr
, gettext("too many arguments\n"));
3336 (void) fprintf(stderr
, gettext("too many arguments\n"));
3342 * Check for the effective uid. We do this explicitly here because
3343 * otherwise any attempt to discover pools will silently fail.
3345 if (argc
== 0 && geteuid() != 0) {
3346 (void) fprintf(stderr
, gettext("cannot "
3347 "discover pools: permission denied\n"));
3348 if (searchdirs
!= NULL
)
3352 nvlist_free(policy
);
3357 * Depending on the arguments given, we do one of the following:
3359 * <none> Iterate through all pools and display information about
3362 * -a Iterate through all pools and try to import each one.
3364 * <id> Find the pool that corresponds to the given GUID/pool
3365 * name and import that one.
3367 * -D Above options applies only to destroyed pools.
3373 searchguid
= strtoull(argv
[0], &endptr
, 10);
3374 if (errno
!= 0 || *endptr
!= '\0') {
3375 searchname
= argv
[0];
3378 found_config
= NULL
;
3381 * User specified a name or guid. Ensure it's unique.
3383 target_exists_args_t search
= {searchname
, searchguid
};
3384 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3388 * Check the environment for the preferred search path.
3390 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3393 envdup
= strdup(env
);
3395 dir
= strtok(envdup
, ":");
3396 while (dir
!= NULL
) {
3397 if (searchdirs
== NULL
) {
3398 searchdirs
= safe_malloc(sizeof (char *));
3400 char **tmp
= safe_malloc((nsearch
+ 1) *
3402 bcopy(searchdirs
, tmp
, nsearch
*
3407 searchdirs
[nsearch
++] = dir
;
3408 dir
= strtok(NULL
, ":");
3412 idata
.path
= searchdirs
;
3413 idata
.paths
= nsearch
;
3414 idata
.poolname
= searchname
;
3415 idata
.guid
= searchguid
;
3416 idata
.cachefile
= cachefile
;
3417 idata
.scan
= do_scan
;
3418 idata
.policy
= policy
;
3420 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3422 if (pools
!= NULL
&& pool_exists
&&
3423 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3424 (void) fprintf(stderr
, gettext("cannot import '%s': "
3425 "a pool with that name already exists\n"),
3427 (void) fprintf(stderr
, gettext("use the form '%s "
3428 "<pool | id> <newpool>' to give it a new name\n"),
3431 } else if (pools
== NULL
&& pool_exists
) {
3432 (void) fprintf(stderr
, gettext("cannot import '%s': "
3433 "a pool with that name is already created/imported,\n"),
3435 (void) fprintf(stderr
, gettext("and no additional pools "
3436 "with that name were found\n"));
3438 } else if (pools
== NULL
) {
3440 (void) fprintf(stderr
, gettext("cannot import '%s': "
3441 "no such pool available\n"), argv
[0]);
3447 if (searchdirs
!= NULL
)
3451 nvlist_free(policy
);
3458 * At this point we have a list of import candidate configs. Even if
3459 * we were searching by pool name or guid, we still need to
3460 * post-process the list to deal with pool state and possible
3466 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3468 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3470 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3472 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3474 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3477 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3484 (void) printf("\n");
3487 err
|= do_import(config
, NULL
, mntopts
,
3490 show_import(config
);
3492 } else if (searchname
!= NULL
) {
3496 * We are searching for a pool based on name.
3498 verify(nvlist_lookup_string(config
,
3499 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3501 if (strcmp(name
, searchname
) == 0) {
3502 if (found_config
!= NULL
) {
3503 (void) fprintf(stderr
, gettext(
3504 "cannot import '%s': more than "
3505 "one matching pool\n"), searchname
);
3506 (void) fprintf(stderr
, gettext(
3507 "import by numeric ID instead\n"));
3510 found_config
= config
;
3516 * Search for a pool by guid.
3518 verify(nvlist_lookup_uint64(config
,
3519 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3521 if (guid
== searchguid
)
3522 found_config
= config
;
3527 * If we were searching for a specific pool, verify that we found a
3528 * pool, and then do the import.
3530 if (argc
!= 0 && err
== 0) {
3531 if (found_config
== NULL
) {
3532 (void) fprintf(stderr
, gettext("cannot import '%s': "
3533 "no such pool available\n"), argv
[0]);
3536 err
|= do_import(found_config
, argc
== 1 ? NULL
:
3537 argv
[1], mntopts
, props
, flags
);
3542 * If we were just looking for pools, report an error if none were
3545 if (argc
== 0 && first
)
3546 (void) fprintf(stderr
,
3547 gettext("no pools available to import\n"));
3552 nvlist_free(policy
);
3553 if (searchdirs
!= NULL
)
3558 return (err
? 1 : 0);
3562 * zpool sync [-f] [pool] ...
3564 * -f (undocumented) force uberblock (and config including zpool cache file)
3567 * Sync the specified pool(s).
3568 * Without arguments "zpool sync" will sync all pools.
3569 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3573 zpool_do_sync(int argc
, char **argv
)
3576 boolean_t force
= B_FALSE
;
3579 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3585 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3594 /* if argc == 0 we will execute zpool_sync_one on all pools */
3595 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
3600 typedef struct iostat_cbdata
{
3605 char **cb_vdev_names
; /* Only show these vdevs */
3606 unsigned int cb_vdev_names_count
;
3607 boolean_t cb_verbose
;
3608 boolean_t cb_literal
;
3609 boolean_t cb_scripted
;
3610 zpool_list_t
*cb_list
;
3611 vdev_cmd_data_list_t
*vcdl
;
3615 typedef struct name_and_columns
{
3616 const char *name
; /* Column name */
3617 unsigned int columns
; /* Center name to this number of columns */
3618 } name_and_columns_t
;
3620 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3622 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3624 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3626 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3627 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL
}},
3628 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3629 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3630 {"trimq_write", 2}, {NULL
}},
3631 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3632 {"asyncq_wait", 2}, {NULL
}},
3633 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3634 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3635 {"trim", 2}, {NULL
}},
3638 /* Shorthand - if "columns" field not set, default to 1 column */
3639 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3641 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3643 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3644 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL
}},
3645 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3646 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3647 {"pend"}, {"activ"}, {NULL
}},
3648 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3649 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL
}},
3650 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3651 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3654 static const char *histo_to_title
[] = {
3655 [IOS_L_HISTO
] = "latency",
3656 [IOS_RQ_HISTO
] = "req_size",
3660 * Return the number of labels in a null-terminated name_and_columns_t
3665 label_array_len(const name_and_columns_t
*labels
)
3669 while (labels
[i
].name
)
3676 * Return the number of strings in a null-terminated string array.
3679 * const char foo[] = {"bar", "baz", NULL}
3684 str_array_len(const char *array
[])
3695 * Return a default column width for default/latency/queue columns. This does
3696 * not include histograms, which have their columns autosized.
3699 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3701 unsigned long column_width
= 5; /* Normal niceprint */
3702 static unsigned long widths
[] = {
3704 * Choose some sane default column sizes for printing the
3707 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3708 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3709 [IOS_QUEUES
] = 6, /* 1M queue entries */
3710 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
3711 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
3715 column_width
= widths
[type
];
3717 return (column_width
);
3721 * Print the column labels, i.e:
3723 * capacity operations bandwidth
3724 * alloc free read write read write ...
3726 * If force_column_width is set, use it for the column width. If not set, use
3727 * the default column width.
3730 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3731 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3734 int text_start
, rw_column_width
, spaces_to_end
;
3735 uint64_t flags
= cb
->cb_flags
;
3737 unsigned int column_width
= force_column_width
;
3739 /* For each bit set in flags */
3740 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3741 idx
= lowbit64(f
) - 1;
3742 if (!force_column_width
)
3743 column_width
= default_column_width(cb
, idx
);
3744 /* Print our top labels centered over "read write" label. */
3745 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3746 const char *name
= labels
[idx
][i
].name
;
3748 * We treat labels[][].columns == 0 as shorthand
3749 * for one column. It makes writing out the label
3750 * tables more concise.
3752 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3753 unsigned int slen
= strlen(name
);
3755 rw_column_width
= (column_width
* columns
) +
3756 (2 * (columns
- 1));
3758 text_start
= (int)((rw_column_width
) / columns
-
3763 printf(" "); /* Two spaces between columns */
3765 /* Space from beginning of column to label */
3766 for (s
= 0; s
< text_start
; s
++)
3771 /* Print space after label to end of column */
3772 spaces_to_end
= rw_column_width
- text_start
- slen
;
3773 if (spaces_to_end
< 0)
3776 for (s
= 0; s
< spaces_to_end
; s
++)
3784 * print_cmd_columns - Print custom column titles from -c
3786 * If the user specified the "zpool status|iostat -c" then print their custom
3787 * column titles in the header. For example, print_cmd_columns() would print
3788 * the " col1 col2" part of this:
3790 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3792 * capacity operations bandwidth
3793 * pool alloc free read write read write col1 col2
3794 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3795 * mypool 269K 1008M 0 0 107 946
3796 * mirror 269K 1008M 0 0 107 946
3797 * sdb - - 0 0 102 473 val1 val2
3798 * sdc - - 0 0 5 473 val1 val2
3799 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3802 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3805 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3807 if (vcdl
->count
== 0 || data
== NULL
)
3811 * Each vdev cmd should have the same column names unless the user did
3812 * something weird with their cmd. Just take the column names from the
3813 * first vdev and assume it works for all of them.
3815 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3818 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3821 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
3822 vcdl
->uniq_cols
[i
]);
3829 * Utility function to print out a line of dashes like:
3831 * -------------------------------- ----- ----- ----- ----- -----
3833 * ...or a dashed named-row line like:
3839 * @force_column_width If non-zero, use the value as the column width.
3840 * Otherwise use the default column widths.
3842 * @name: Print a dashed named-row line starting
3843 * with @name. Otherwise, print a regular
3847 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3851 unsigned int namewidth
;
3852 uint64_t flags
= cb
->cb_flags
;
3855 const name_and_columns_t
*labels
;
3859 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3860 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3861 } else if (cb
->cb_vdev_names_count
) {
3867 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3868 name
? strlen(name
) : 0);
3872 printf("%-*s", namewidth
, name
);
3874 for (i
= 0; i
< namewidth
; i
++)
3878 /* For each bit in flags */
3879 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3880 unsigned int column_width
;
3881 idx
= lowbit64(f
) - 1;
3882 if (force_column_width
)
3883 column_width
= force_column_width
;
3885 column_width
= default_column_width(cb
, idx
);
3887 labels
= iostat_bottom_labels
[idx
];
3888 for (i
= 0; i
< label_array_len(labels
); i
++) {
3890 printf(" %*s-", column_width
- 1, " ");
3892 printf(" %.*s", column_width
,
3893 "--------------------");
3900 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3901 unsigned int force_column_width
)
3903 print_iostat_dashes(cb
, force_column_width
, NULL
);
3907 print_iostat_separator(iostat_cbdata_t
*cb
)
3909 print_iostat_separator_impl(cb
, 0);
3913 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3914 const char *histo_vdev_name
)
3916 unsigned int namewidth
;
3919 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3920 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3921 } else if (cb
->cb_vdev_names_count
) {
3927 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3928 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3930 if (histo_vdev_name
)
3931 printf("%-*s", namewidth
, histo_vdev_name
);
3933 printf("%*s", namewidth
, "");
3936 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3939 printf("%-*s", namewidth
, title
);
3941 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3942 if (cb
->vcdl
!= NULL
)
3943 print_cmd_columns(cb
->vcdl
, 0);
3947 print_iostat_separator_impl(cb
, force_column_width
);
3949 if (cb
->vcdl
!= NULL
)
3950 print_cmd_columns(cb
->vcdl
, 1);
3956 print_iostat_header(iostat_cbdata_t
*cb
)
3958 print_iostat_header_impl(cb
, 0, NULL
);
3963 * Display a single statistic.
3966 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3967 unsigned int column_size
, boolean_t scripted
)
3971 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3974 printf("\t%s", buf
);
3976 printf(" %*s", column_size
, buf
);
3980 * Calculate the default vdev stats
3982 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3983 * stats into calcvs.
3986 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3987 vdev_stat_t
*calcvs
)
3991 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3992 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3993 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3995 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3996 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4000 * Internal representation of the extended iostats data.
4002 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4003 * or single uint64_t's. We make both look like arrays to make them easier
4004 * to process. In order to make single uint64_t's look like arrays, we set
4005 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4006 * we can just use *data and count.
4010 uint_t count
; /* Number of entries in data[] */
4011 uint64_t __data
; /* Only used when data is a single uint64_t */
4015 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4019 for (i
= 0; i
< len
; i
++)
4020 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4026 * Helper function to lookup a uint64_t array or uint64_t value and store its
4027 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4028 * it look like a one element array to make it easier to process.
4031 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4032 struct stat_array
*nva
)
4037 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4038 switch (nvpair_type(tmp
)) {
4039 case DATA_TYPE_UINT64_ARRAY
:
4040 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4042 case DATA_TYPE_UINT64
:
4043 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4044 nva
->data
= &nva
->__data
;
4048 /* Not a uint64_t */
4057 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4058 * subtract them, and return the results in a newly allocated stat_array.
4059 * You must free the returned array after you are done with it with
4060 * free_calc_stats().
4062 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4065 static struct stat_array
*
4066 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4069 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4070 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4072 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4074 /* Extract our extended stats nvlist from the main list */
4075 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4078 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4082 newnva
= safe_malloc(alloc_size
);
4083 oldnva
= safe_malloc(alloc_size
);
4084 calcnva
= safe_malloc(alloc_size
);
4086 for (j
= 0; j
< len
; j
++) {
4087 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4089 calcnva
[j
].count
= newnva
[j
].count
;
4090 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4091 calcnva
[j
].data
= safe_malloc(alloc_size
);
4092 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4095 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4097 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4098 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4107 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4110 for (i
= 0; i
< len
; i
++)
4117 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4118 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4124 enum zfs_nicenum_format format
;
4125 unsigned int buckets
;
4126 unsigned int start_bucket
;
4129 format
= ZFS_NICENUM_RAW
;
4131 format
= ZFS_NICENUM_1024
;
4133 /* All these histos are the same size, so just use nva[0].count */
4134 buckets
= nva
[0].count
;
4136 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4137 /* Start at 512 - req size should never be lower than this */
4143 for (j
= start_bucket
; j
< buckets
; j
++) {
4144 /* Print histogram bucket label */
4145 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4146 /* Ending range of this bucket */
4147 val
= (1UL << (j
+ 1)) - 1;
4148 zfs_nicetime(val
, buf
, sizeof (buf
));
4150 /* Request size (starting range of bucket) */
4152 zfs_nicenum(val
, buf
, sizeof (buf
));
4155 if (cb
->cb_scripted
)
4156 printf("%llu", (u_longlong_t
)val
);
4158 printf("%-*s", namewidth
, buf
);
4160 /* Print the values on the line */
4161 for (i
= 0; i
< len
; i
++) {
4162 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4163 column_width
, cb
->cb_scripted
);
4170 print_solid_separator(unsigned int length
)
4178 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4179 nvlist_t
*newnv
, double scale
, const char *name
)
4181 unsigned int column_width
;
4182 unsigned int namewidth
;
4183 unsigned int entire_width
;
4184 enum iostat_type type
;
4185 struct stat_array
*nva
;
4187 unsigned int names_len
;
4189 /* What type of histo are we? */
4190 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4192 /* Get NULL-terminated array of nvlist names for our histo */
4193 names
= vsx_type_to_nvlist
[type
];
4194 names_len
= str_array_len(names
); /* num of names */
4196 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4198 if (cb
->cb_literal
) {
4199 column_width
= MAX(5,
4200 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4205 namewidth
= MAX(cb
->cb_namewidth
,
4206 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4209 * Calculate the entire line width of what we're printing. The
4210 * +2 is for the two spaces between columns:
4214 /* |___| <---------- column_width */
4216 /* |__________| <--- entire_width */
4218 entire_width
= namewidth
+ (column_width
+ 2) *
4219 label_array_len(iostat_bottom_labels
[type
]);
4221 if (cb
->cb_scripted
)
4222 printf("%s\n", name
);
4224 print_iostat_header_impl(cb
, column_width
, name
);
4226 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4229 free_calc_stats(nva
, names_len
);
4230 if (!cb
->cb_scripted
)
4231 print_solid_separator(entire_width
);
4235 * Calculate the average latency of a power-of-two latency histogram
4238 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4241 uint64_t count
= 0, total
= 0;
4243 for (i
= 0; i
< buckets
; i
++) {
4245 * Our buckets are power-of-two latency ranges. Use the
4246 * midpoint latency of each bucket to calculate the average.
4254 if (histo
[i
] != 0) {
4255 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4260 /* Prevent divide by zero */
4261 return (count
== 0 ? 0 : total
/ count
);
4265 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4270 const char *names
[] = {
4271 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4272 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4273 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4274 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4275 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4276 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4277 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4278 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4279 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4280 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4281 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4282 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4285 struct stat_array
*nva
;
4287 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4288 enum zfs_nicenum_format format
;
4290 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4293 format
= ZFS_NICENUM_RAW
;
4295 format
= ZFS_NICENUM_1024
;
4297 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4298 val
= nva
[i
].data
[0];
4299 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4302 free_calc_stats(nva
, ARRAY_SIZE(names
));
4306 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4311 const char *names
[] = {
4312 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4313 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4314 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4315 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4316 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4317 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4318 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4319 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4320 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4321 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4323 struct stat_array
*nva
;
4325 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4326 enum zfs_nicenum_format format
;
4328 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4331 format
= ZFS_NICENUM_RAWTIME
;
4333 format
= ZFS_NICENUM_TIME
;
4335 /* Print our avg latencies on the line */
4336 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4337 /* Compute average latency for a latency histo */
4338 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4339 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4341 free_calc_stats(nva
, ARRAY_SIZE(names
));
4345 * Print default statistics (capacity/operations/bandwidth)
4348 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4350 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4351 enum zfs_nicenum_format format
;
4352 char na
; /* char to print for "not applicable" values */
4354 if (cb
->cb_literal
) {
4355 format
= ZFS_NICENUM_RAW
;
4358 format
= ZFS_NICENUM_1024
;
4362 /* only toplevel vdevs have capacity stats */
4363 if (vs
->vs_space
== 0) {
4364 if (cb
->cb_scripted
)
4365 printf("\t%c\t%c", na
, na
);
4367 printf(" %*c %*c", column_width
, na
, column_width
,
4370 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4372 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4373 column_width
, cb
->cb_scripted
);
4376 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4377 format
, column_width
, cb
->cb_scripted
);
4378 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4379 format
, column_width
, cb
->cb_scripted
);
4380 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4381 format
, column_width
, cb
->cb_scripted
);
4382 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4383 format
, column_width
, cb
->cb_scripted
);
4386 static const char *class_name
[] = {
4387 VDEV_ALLOC_BIAS_DEDUP
,
4388 VDEV_ALLOC_BIAS_SPECIAL
,
4389 VDEV_ALLOC_CLASS_LOGS
4393 * Print out all the statistics for the given vdev. This can either be the
4394 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4395 * is a verbose output, and we don't want to display the toplevel pool stats.
4397 * Returns the number of stat lines printed.
4400 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4401 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4403 nvlist_t
**oldchild
, **newchild
;
4404 uint_t c
, children
, oldchildren
;
4405 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4406 vdev_stat_t zerovs
= { 0 };
4413 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4416 calcvs
= safe_malloc(sizeof (*calcvs
));
4418 if (oldnv
!= NULL
) {
4419 verify(nvlist_lookup_uint64_array(oldnv
,
4420 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4425 /* Do we only want to see a specific vdev? */
4426 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
4427 /* Yes we do. Is this the vdev? */
4428 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
4430 * This is our vdev. Since it is the only vdev we
4431 * will be displaying, make depth = 0 so that it
4432 * doesn't get indented.
4439 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4440 /* Couldn't match the name */
4445 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4446 (uint64_t **)&newvs
, &c
) == 0);
4449 * Print the vdev name unless it's is a histogram. Histograms
4450 * display the vdev name in the header itself.
4452 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4453 if (cb
->cb_scripted
) {
4456 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4457 (void) printf("%*s%s", depth
, "", name
);
4459 (void) printf("%*s%s%*s", depth
, "", name
,
4460 (int)(cb
->cb_namewidth
- strlen(name
) -
4465 /* Calculate our scaling factor */
4466 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4467 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4469 * If we specify printing histograms with no time interval, then
4470 * print the histogram numbers over the entire lifetime of the
4478 scale
= (double)NANOSEC
/ tdelta
;
4481 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4482 calc_default_iostats(oldvs
, newvs
, calcvs
);
4483 print_iostat_default(calcvs
, cb
, scale
);
4485 if (cb
->cb_flags
& IOS_LATENCY_M
)
4486 print_iostat_latency(cb
, oldnv
, newnv
);
4487 if (cb
->cb_flags
& IOS_QUEUES_M
)
4488 print_iostat_queues(cb
, oldnv
, newnv
);
4489 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4491 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4494 if (cb
->vcdl
!= NULL
) {
4496 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4499 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4503 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4512 if (!cb
->cb_verbose
)
4515 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4516 &newchild
, &children
) != 0)
4520 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4521 &oldchild
, &oldchildren
) != 0)
4524 children
= MIN(oldchildren
, children
);
4528 * print normal top-level devices
4530 for (c
= 0; c
< children
; c
++) {
4531 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4533 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4536 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4539 if (ishole
|| islog
)
4542 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4545 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4547 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4548 newchild
[c
], cb
, depth
+ 2);
4553 * print all other top-level devices
4555 for (uint_t n
= 0; n
< 3; n
++) {
4556 boolean_t printed
= B_FALSE
;
4558 for (c
= 0; c
< children
; c
++) {
4559 uint64_t islog
= B_FALSE
;
4563 (void) nvlist_lookup_uint64(newchild
[c
],
4564 ZPOOL_CONFIG_IS_LOG
, &islog
);
4566 bias
= VDEV_ALLOC_CLASS_LOGS
;
4568 (void) nvlist_lookup_string(newchild
[c
],
4569 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4570 (void) nvlist_lookup_string(newchild
[c
],
4571 ZPOOL_CONFIG_TYPE
, &type
);
4573 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4575 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4579 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4580 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4581 print_iostat_dashes(cb
, 0,
4588 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4590 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4591 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4597 * Include level 2 ARC devices in iostat output
4599 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4600 &newchild
, &children
) != 0)
4604 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4605 &oldchild
, &oldchildren
) != 0)
4608 children
= MIN(oldchildren
, children
);
4612 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4613 !cb
->cb_vdev_names
) {
4614 print_iostat_dashes(cb
, 0, "cache");
4618 for (c
= 0; c
< children
; c
++) {
4619 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4621 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4622 : NULL
, newchild
[c
], cb
, depth
+ 2);
4631 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4633 iostat_cbdata_t
*cb
= data
;
4637 * If the pool has disappeared, remove it from the list and continue.
4639 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4643 pool_list_remove(cb
->cb_list
, zhp
);
4649 * Callback to print out the iostats for the given pool.
4652 print_iostat(zpool_handle_t
*zhp
, void *data
)
4654 iostat_cbdata_t
*cb
= data
;
4655 nvlist_t
*oldconfig
, *newconfig
;
4656 nvlist_t
*oldnvroot
, *newnvroot
;
4659 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4661 if (cb
->cb_iteration
== 1)
4664 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4667 if (oldconfig
== NULL
)
4670 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4673 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4675 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4676 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4677 print_iostat_separator(cb
);
4678 if (cb
->vcdl
!= NULL
) {
4679 print_cmd_columns(cb
->vcdl
, 1);
4694 if (isatty(STDOUT_FILENO
)) {
4695 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4697 columns
= ws
.ws_col
;
4706 * Return the required length of the pool/vdev name column. The minimum
4707 * allowed width and output formatting flags must be provided.
4710 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4712 nvlist_t
*config
, *nvroot
;
4713 int width
= min_width
;
4715 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4716 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4718 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4719 if (verbose
== B_FALSE
) {
4720 width
= MAX(poolname_len
, min_width
);
4722 width
= MAX(poolname_len
,
4723 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4731 * Parse the input string, get the 'interval' and 'count' value if there is one.
4734 get_interval_count(int *argcp
, char **argv
, float *iv
,
4738 unsigned long count
= 0;
4742 * Determine if the last argument is an integer or a pool name
4744 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4748 interval
= strtof(argv
[argc
- 1], &end
);
4750 if (*end
== '\0' && errno
== 0) {
4751 if (interval
== 0) {
4752 (void) fprintf(stderr
, gettext("interval "
4753 "cannot be zero\n"));
4757 * Ignore the last parameter
4762 * If this is not a valid number, just plow on. The
4763 * user will get a more informative error message later
4771 * If the last argument is also an integer, then we have both a count
4774 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4779 interval
= strtof(argv
[argc
- 1], &end
);
4781 if (*end
== '\0' && errno
== 0) {
4782 if (interval
== 0) {
4783 (void) fprintf(stderr
, gettext("interval "
4784 "cannot be zero\n"));
4789 * Ignore the last parameter
4803 get_timestamp_arg(char c
)
4806 timestamp_fmt
= UDATE
;
4808 timestamp_fmt
= DDATE
;
4814 * Return stat flags that are supported by all pools by both the module and
4815 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4816 * It will get ANDed down until only the flags that are supported on all pools
4820 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4822 uint64_t *mask
= data
;
4823 nvlist_t
*config
, *nvroot
, *nvx
;
4827 config
= zpool_get_config(zhp
, NULL
);
4828 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4831 /* Default stats are always supported, but for completeness.. */
4832 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4833 flags
|= IOS_DEFAULT_M
;
4835 /* Get our extended stats nvlist from the main list */
4836 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4839 * No extended stats; they're probably running an older
4840 * module. No big deal, we support that too.
4845 /* For each extended stat, make sure all its nvpairs are supported */
4846 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4847 if (!vsx_type_to_nvlist
[j
][0])
4850 /* Start off by assuming the flag is supported, then check */
4851 flags
|= (1ULL << j
);
4852 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4853 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4854 /* flag isn't supported */
4855 flags
= flags
& ~(1ULL << j
);
4861 *mask
= *mask
& flags
;
4866 * Return a bitmask of stats that are supported on all pools by both the module
4870 get_stat_flags(zpool_list_t
*list
)
4875 * get_stat_flags_cb() will lop off bits from "mask" until only the
4876 * flags that are supported on all pools remain.
4878 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4883 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4886 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4888 iostat_cbdata_t
*cb
= cb_data
;
4892 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4894 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4895 ret
= 1; /* match */
4902 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4905 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4907 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4911 * Check if vdevs are in a pool
4913 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4914 * return 0. If pool_name is NULL, then search all pools.
4917 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4918 iostat_cbdata_t
*cb
)
4925 if ((argc
== 0) || !*argv
)
4931 /* Temporarily hijack cb_vdev_names for a second... */
4932 tmp_name
= cb
->cb_vdev_names
;
4934 /* Go though our list of prospective vdev names */
4935 for (i
= 0; i
< argc
; i
++) {
4936 cb
->cb_vdev_names
= argv
+ i
;
4938 /* Is this name a vdev in our pools? */
4939 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4947 cb
->cb_vdev_names
= tmp_name
;
4953 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4956 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4963 * Do we have a pool named *name? If so, return 1, otherwise 0.
4968 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4971 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4973 are_all_pools(int argc
, char **argv
)
4975 if ((argc
== 0) || !*argv
)
4979 if (!is_pool(argv
[argc
]))
4986 * Helper function to print out vdev/pool names we can't resolve. Used for an
4990 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4991 iostat_cbdata_t
*cb
)
4996 for (i
= 0; i
< argc
; i
++) {
5000 str
= gettext("pool");
5001 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5002 str
= gettext("vdev in this pool");
5003 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5004 str
= gettext("vdev in another pool");
5006 str
= gettext("unknown");
5008 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5013 * Same as get_interval_count(), but with additional checks to not misinterpret
5014 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5018 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5019 unsigned long *count
, iostat_cbdata_t
*cb
)
5021 char **tmpargv
= argv
;
5022 int argc_for_interval
= 0;
5024 /* Is the last arg an interval value? Or a guid? */
5025 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
5027 * The last arg is not a guid, so it's probably an
5030 argc_for_interval
++;
5033 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
5035 * The 2nd to last arg is not a guid, so it's probably
5036 * an interval value.
5038 argc_for_interval
++;
5042 /* Point to our list of possible intervals */
5043 tmpargv
= &argv
[*argc
- argc_for_interval
];
5045 *argc
= *argc
- argc_for_interval
;
5046 get_interval_count(&argc_for_interval
, tmpargv
,
5051 * Floating point sleep(). Allows you to pass in a floating point value for
5057 struct timespec req
;
5058 req
.tv_sec
= floor(sec
);
5059 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5060 nanosleep(&req
, NULL
);
5064 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5065 * if we were unable to determine its size.
5068 terminal_height(void)
5072 if (isatty(STDOUT_FILENO
) == 0)
5075 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5076 return (win
.ws_row
);
5082 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5085 * name: Short name of the script ('iostat').
5086 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5089 print_zpool_script_help(char *name
, char *path
)
5091 char *argv
[] = {path
, "-h", NULL
};
5092 char **lines
= NULL
;
5096 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5098 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5100 libzfs_free_str_array(lines
, lines_cnt
);
5104 for (int i
= 0; i
< lines_cnt
; i
++)
5105 if (!is_blank_str(lines
[i
]))
5106 printf(" %-14s %s\n", name
, lines
[i
]);
5108 libzfs_free_str_array(lines
, lines_cnt
);
5112 * Go though the zpool status/iostat -c scripts in the user's path, run their
5113 * help option (-h), and print out the results.
5116 print_zpool_dir_scripts(char *dirpath
)
5120 char fullpath
[MAXPATHLEN
];
5121 struct stat dir_stat
;
5123 if ((dir
= opendir(dirpath
)) != NULL
) {
5124 /* print all the files and directories within directory */
5125 while ((ent
= readdir(dir
)) != NULL
) {
5126 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5128 /* Print the scripts */
5129 if (stat(fullpath
, &dir_stat
) == 0)
5130 if (dir_stat
.st_mode
& S_IXUSR
&&
5131 S_ISREG(dir_stat
.st_mode
))
5132 print_zpool_script_help(ent
->d_name
,
5140 * Print out help text for all zpool status/iostat -c scripts.
5143 print_zpool_script_list(char *subcommand
)
5147 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5149 sp
= zpool_get_cmd_search_path();
5153 dir
= strtok(sp
, ":");
5154 while (dir
!= NULL
) {
5155 print_zpool_dir_scripts(dir
);
5156 dir
= strtok(NULL
, ":");
5163 * Set the minimum pool/vdev name column width. The width must be at least 10,
5164 * but may be as large as the column width - 42 so it still fits on one line.
5165 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5168 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5170 iostat_cbdata_t
*cb
= data
;
5171 int width
, available_width
;
5174 * get_namewidth() returns the maximum width of any name in that column
5175 * for any pool/vdev/device line that will be output.
5177 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5181 * The width we are calculating is the width of the header and also the
5182 * padding width for names that are less than maximum width. The stats
5183 * take up 42 characters, so the width available for names is:
5185 available_width
= get_columns() - 42;
5188 * If the maximum width fits on a screen, then great! Make everything
5189 * line up by justifying all lines to the same width. If that max
5190 * width is larger than what's available, the name plus stats won't fit
5191 * on one line, and justifying to that width would cause every line to
5192 * wrap on the screen. We only want lines with long names to wrap.
5193 * Limit the padding to what won't wrap.
5195 if (width
> available_width
)
5196 width
= available_width
;
5199 * And regardless of whatever the screen width is (get_columns can
5200 * return 0 if the width is not known or less than 42 for a narrow
5201 * terminal) have the width be a minimum of 10.
5206 /* Save the calculated width */
5207 cb
->cb_namewidth
= width
;
5213 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5214 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5215 * [interval [count]]
5217 * -c CMD For each vdev, run command CMD
5218 * -g Display guid for individual vdev name.
5219 * -L Follow links when resolving vdev path name.
5220 * -P Display full path for vdev name.
5221 * -v Display statistics for individual vdevs
5223 * -p Display values in parsable (exact) format.
5224 * -H Scripted mode. Don't display headers, and separate properties
5226 * -l Display average latency
5227 * -q Display queue depths
5228 * -w Display latency histograms
5229 * -r Display request size histogram
5230 * -T Display a timestamp in date(1) or Unix format
5231 * -n Only print headers once
5233 * This command can be tricky because we want to be able to deal with pool
5234 * creation/destruction as well as vdev configuration changes. The bulk of this
5235 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5236 * on pool_list_update() to detect the addition of new pools. Configuration
5237 * changes are all handled within libzfs.
5240 zpool_do_iostat(int argc
, char **argv
)
5246 unsigned long count
= 0;
5249 boolean_t verbose
= B_FALSE
;
5250 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5251 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5252 boolean_t omit_since_boot
= B_FALSE
;
5253 boolean_t guid
= B_FALSE
;
5254 boolean_t follow_links
= B_FALSE
;
5255 boolean_t full_name
= B_FALSE
;
5256 boolean_t headers_once
= B_FALSE
;
5257 iostat_cbdata_t cb
= { 0 };
5260 /* Used for printing error message */
5261 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5262 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5264 uint64_t unsupported_flags
;
5267 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5272 gettext("Can't set -c flag twice\n"));
5276 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5277 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5278 fprintf(stderr
, gettext(
5279 "Can't run -c, disabled by "
5280 "ZPOOL_SCRIPTS_ENABLED.\n"));
5284 if ((getuid() <= 0 || geteuid() <= 0) &&
5285 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5286 fprintf(stderr
, gettext(
5287 "Can't run -c with root privileges "
5288 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5298 follow_links
= B_TRUE
;
5304 get_timestamp_arg(*optarg
);
5328 omit_since_boot
= B_TRUE
;
5331 headers_once
= B_TRUE
;
5337 if (optopt
== 'c') {
5338 print_zpool_script_list("iostat");
5342 gettext("invalid option '%c'\n"), optopt
);
5351 cb
.cb_literal
= parsable
;
5352 cb
.cb_scripted
= scripted
;
5355 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5357 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5359 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5360 cb
.cb_iteration
= 0;
5361 cb
.cb_namewidth
= 0;
5362 cb
.cb_verbose
= verbose
;
5364 /* Get our interval and count values (if any) */
5366 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5369 get_interval_count(&argc
, argv
, &interval
, &count
);
5373 /* No args, so just print the defaults. */
5374 } else if (are_all_pools(argc
, argv
)) {
5375 /* All the args are pool names */
5376 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
5377 /* All the args are vdevs */
5378 cb
.cb_vdev_names
= argv
;
5379 cb
.cb_vdev_names_count
= argc
;
5380 argc
= 0; /* No pools to process */
5381 } else if (are_all_pools(1, argv
)) {
5382 /* The first arg is a pool name */
5383 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
5384 /* ...and the rest are vdev names */
5385 cb
.cb_vdev_names
= argv
+ 1;
5386 cb
.cb_vdev_names_count
= argc
- 1;
5387 argc
= 1; /* One pool to process */
5389 fprintf(stderr
, gettext("Expected either a list of "));
5390 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5391 fprintf(stderr
, " \"%s\", ", argv
[0]);
5392 fprintf(stderr
, gettext("but got:\n"));
5393 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5395 fprintf(stderr
, "\n");
5401 * The args don't make sense. The first arg isn't a pool name,
5402 * nor are all the args vdevs.
5404 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5405 fprintf(stderr
, "\n");
5409 if (cb
.cb_vdev_names_count
!= 0) {
5411 * If user specified vdevs, it implies verbose.
5413 cb
.cb_verbose
= B_TRUE
;
5417 * Construct the list of all interesting pools.
5420 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
5423 if (pool_list_count(list
) == 0 && argc
!= 0) {
5424 pool_list_free(list
);
5428 if (pool_list_count(list
) == 0 && interval
== 0) {
5429 pool_list_free(list
);
5430 (void) fprintf(stderr
, gettext("no pools available\n"));
5434 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5435 pool_list_free(list
);
5436 (void) fprintf(stderr
,
5437 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5442 if (l_histo
&& rq_histo
) {
5443 pool_list_free(list
);
5444 (void) fprintf(stderr
,
5445 gettext("Only one of [-r|-w] can be passed at a time\n"));
5451 * Enter the main iostat loop.
5457 * Histograms tables look out of place when you try to display
5458 * them with the other stats, so make a rule that you can only
5459 * print histograms by themselves.
5461 cb
.cb_flags
= IOS_L_HISTO_M
;
5462 } else if (rq_histo
) {
5463 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5465 cb
.cb_flags
= IOS_DEFAULT_M
;
5467 cb
.cb_flags
|= IOS_LATENCY_M
;
5469 cb
.cb_flags
|= IOS_QUEUES_M
;
5473 * See if the module supports all the stats we want to display.
5475 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5476 if (unsupported_flags
) {
5480 gettext("The loaded zfs module doesn't support:"));
5482 /* for each bit set in unsupported_flags */
5483 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5484 idx
= lowbit64(f
) - 1;
5485 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5488 fprintf(stderr
, ". Try running a newer module.\n");
5489 pool_list_free(list
);
5495 if ((npools
= pool_list_count(list
)) == 0)
5496 (void) fprintf(stderr
, gettext("no pools available\n"));
5499 * If this is the first iteration and -y was supplied
5500 * we skip any printing.
5502 boolean_t skip
= (omit_since_boot
&&
5503 cb
.cb_iteration
== 0);
5506 * Refresh all statistics. This is done as an
5507 * explicit step before calculating the maximum name
5508 * width, so that any * configuration changes are
5509 * properly accounted for.
5511 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5515 * Iterate over all pools to determine the maximum width
5516 * for the pool / device name column across all pools.
5518 cb
.cb_namewidth
= 0;
5519 (void) pool_list_iter(list
, B_FALSE
,
5520 get_namewidth_iostat
, &cb
);
5522 if (timestamp_fmt
!= NODATE
)
5523 print_timestamp(timestamp_fmt
);
5525 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5526 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5527 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5528 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5529 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5536 * Check terminal size so we can print headers
5537 * even when terminal window has its height
5540 winheight
= terminal_height();
5542 * Are we connected to TTY? If not, headers_once
5543 * should be true, to avoid breaking scripts.
5546 headers_once
= B_TRUE
;
5549 * If it's the first time and we're not skipping it,
5550 * or either skip or verbose mode, print the header.
5552 * The histogram code explicitly prints its header on
5553 * every vdev, so skip this for histograms.
5555 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5556 (skip
!= verbose
) ||
5558 (cb
.cb_iteration
% winheight
) == 0)) &&
5559 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5561 print_iostat_header(&cb
);
5564 (void) fsleep(interval
);
5568 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5571 * If there's more than one pool, and we're not in
5572 * verbose mode (which prints a separator for us),
5573 * then print a separator.
5575 * In addition, if we're printing specific vdevs then
5576 * we also want an ending separator.
5578 if (((npools
> 1 && !verbose
&&
5579 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5580 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5581 cb
.cb_vdev_names_count
)) &&
5583 print_iostat_separator(&cb
);
5584 if (cb
.vcdl
!= NULL
)
5585 print_cmd_columns(cb
.vcdl
, 1);
5589 if (cb
.vcdl
!= NULL
)
5590 free_vdev_cmd_data_list(cb
.vcdl
);
5595 * Flush the output so that redirection to a file isn't buffered
5598 (void) fflush(stdout
);
5603 if (count
!= 0 && --count
== 0)
5606 (void) fsleep(interval
);
5609 pool_list_free(list
);
5614 typedef struct list_cbdata
{
5615 boolean_t cb_verbose
;
5618 boolean_t cb_scripted
;
5619 zprop_list_t
*cb_proplist
;
5620 boolean_t cb_literal
;
5625 * Given a list of columns to display, output appropriate headers for each one.
5628 print_header(list_cbdata_t
*cb
)
5630 zprop_list_t
*pl
= cb
->cb_proplist
;
5631 char headerbuf
[ZPOOL_MAXPROPLEN
];
5633 boolean_t first
= B_TRUE
;
5634 boolean_t right_justify
;
5637 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5638 width
= pl
->pl_width
;
5639 if (first
&& cb
->cb_verbose
) {
5641 * Reset the width to accommodate the verbose listing
5644 width
= cb
->cb_namewidth
;
5652 right_justify
= B_FALSE
;
5653 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5654 header
= zpool_prop_column_name(pl
->pl_prop
);
5655 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5659 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5660 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5661 headerbuf
[i
] = '\0';
5665 if (pl
->pl_next
== NULL
&& !right_justify
)
5666 (void) printf("%s", header
);
5667 else if (right_justify
)
5668 (void) printf("%*s", (int)width
, header
);
5670 (void) printf("%-*s", (int)width
, header
);
5673 (void) printf("\n");
5677 * Given a pool and a list of properties, print out all the properties according
5678 * to the described layout. Used by zpool_do_list().
5681 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5683 zprop_list_t
*pl
= cb
->cb_proplist
;
5684 boolean_t first
= B_TRUE
;
5685 char property
[ZPOOL_MAXPROPLEN
];
5687 boolean_t right_justify
;
5690 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5692 width
= pl
->pl_width
;
5693 if (first
&& cb
->cb_verbose
) {
5695 * Reset the width to accommodate the verbose listing
5698 width
= cb
->cb_namewidth
;
5702 if (cb
->cb_scripted
)
5703 (void) printf("\t");
5710 right_justify
= B_FALSE
;
5711 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5712 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5713 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5718 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5719 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5720 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5721 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5722 sizeof (property
)) == 0) {
5730 * If this is being called in scripted mode, or if this is the
5731 * last column and it is left-justified, don't include a width
5734 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5735 (void) printf("%s", propstr
);
5736 else if (right_justify
)
5737 (void) printf("%*s", (int)width
, propstr
);
5739 (void) printf("%-*s", (int)width
, propstr
);
5742 (void) printf("\n");
5746 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5747 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5751 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5754 case ZPOOL_PROP_EXPANDSZ
:
5755 case ZPOOL_PROP_CHECKPOINT
:
5756 case ZPOOL_PROP_DEDUPRATIO
:
5758 (void) strlcpy(propval
, "-", sizeof (propval
));
5760 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5763 case ZPOOL_PROP_FRAGMENTATION
:
5764 if (value
== ZFS_FRAG_INVALID
) {
5765 (void) strlcpy(propval
, "-", sizeof (propval
));
5766 } else if (format
== ZFS_NICENUM_RAW
) {
5767 (void) snprintf(propval
, sizeof (propval
), "%llu",
5768 (unsigned long long)value
);
5770 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5771 (unsigned long long)value
);
5774 case ZPOOL_PROP_CAPACITY
:
5775 /* capacity value is in parts-per-10,000 (aka permyriad) */
5776 if (format
== ZFS_NICENUM_RAW
)
5777 (void) snprintf(propval
, sizeof (propval
), "%llu",
5778 (unsigned long long)value
/ 100);
5780 (void) snprintf(propval
, sizeof (propval
),
5781 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
5782 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
5784 case ZPOOL_PROP_HEALTH
:
5786 snprintf(propval
, sizeof (propval
), "%-*s", (int)width
, str
);
5789 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5793 (void) strlcpy(propval
, "-", sizeof (propval
));
5796 (void) printf("\t%s", propval
);
5798 (void) printf(" %*s", (int)width
, propval
);
5802 * print static default line per vdev
5803 * not compatible with '-o' <proplist> option
5806 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5807 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
5813 boolean_t scripted
= cb
->cb_scripted
;
5814 uint64_t islog
= B_FALSE
;
5815 char *dashes
= "%-*s - - - - "
5818 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5819 (uint64_t **)&vs
, &c
) == 0);
5822 boolean_t toplevel
= (vs
->vs_space
!= 0);
5824 enum zfs_nicenum_format format
;
5828 format
= ZFS_NICENUM_RAW
;
5830 format
= ZFS_NICENUM_1024
;
5832 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5836 (void) printf("\t%s", name
);
5837 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5838 (void) printf("%*s%s", depth
, "", name
);
5840 (void) printf("%*s%s%*s", depth
, "", name
,
5841 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5844 * Print the properties for the individual vdevs. Some
5845 * properties are only applicable to toplevel vdevs. The
5846 * 'toplevel' boolean value is passed to the print_one_column()
5847 * to indicate that the value is valid.
5849 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
5851 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
5852 scripted
, toplevel
, format
);
5853 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5854 NULL
, scripted
, toplevel
, format
);
5855 print_one_column(ZPOOL_PROP_CHECKPOINT
,
5856 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
5857 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
5858 scripted
, B_TRUE
, format
);
5859 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5860 vs
->vs_fragmentation
, NULL
, scripted
,
5861 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5863 cap
= (vs
->vs_space
== 0) ? 0 :
5864 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
5865 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
5866 scripted
, toplevel
, format
);
5867 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
5868 scripted
, toplevel
, format
);
5869 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5871 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
5873 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
5876 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
5878 (void) printf("\n");
5881 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5882 &child
, &children
) != 0)
5885 /* list the normal vdevs first */
5886 for (c
= 0; c
< children
; c
++) {
5887 uint64_t ishole
= B_FALSE
;
5889 if (nvlist_lookup_uint64(child
[c
],
5890 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5893 if (nvlist_lookup_uint64(child
[c
],
5894 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
5897 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
5900 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5902 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
5906 /* list the classes: 'logs', 'dedup', and 'special' */
5907 for (uint_t n
= 0; n
< 3; n
++) {
5908 boolean_t printed
= B_FALSE
;
5910 for (c
= 0; c
< children
; c
++) {
5914 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5915 &islog
) == 0 && islog
) {
5916 bias
= VDEV_ALLOC_CLASS_LOGS
;
5918 (void) nvlist_lookup_string(child
[c
],
5919 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
5920 (void) nvlist_lookup_string(child
[c
],
5921 ZPOOL_CONFIG_TYPE
, &type
);
5923 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
5925 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
5929 /* LINTED E_SEC_PRINTF_VAR_FMT */
5930 (void) printf(dashes
, cb
->cb_namewidth
,
5934 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5936 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5942 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5943 &child
, &children
) == 0 && children
> 0) {
5944 /* LINTED E_SEC_PRINTF_VAR_FMT */
5945 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5946 for (c
= 0; c
< children
; c
++) {
5947 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5949 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5955 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5956 &children
) == 0 && children
> 0) {
5957 /* LINTED E_SEC_PRINTF_VAR_FMT */
5958 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5959 for (c
= 0; c
< children
; c
++) {
5960 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5962 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5970 * Generic callback function to list a pool.
5973 list_callback(zpool_handle_t
*zhp
, void *data
)
5975 list_cbdata_t
*cbp
= data
;
5977 print_pool(zhp
, cbp
);
5979 if (cbp
->cb_verbose
) {
5980 nvlist_t
*config
, *nvroot
;
5982 config
= zpool_get_config(zhp
, NULL
);
5983 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5985 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
5992 * Set the minimum pool/vdev name column width. The width must be at least 9,
5993 * but may be as large as needed.
5996 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
5998 list_cbdata_t
*cb
= data
;
6001 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6007 cb
->cb_namewidth
= width
;
6013 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6015 * -g Display guid for individual vdev name.
6016 * -H Scripted mode. Don't display headers, and separate properties
6018 * -L Follow links when resolving vdev path name.
6019 * -o List of properties to display. Defaults to
6020 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6021 * "dedupratio,health,altroot"
6022 * -p Display values in parsable (exact) format.
6023 * -P Display full path for vdev name.
6024 * -T Display a timestamp in date(1) or Unix format
6026 * List all pools in the system, whether or not they're healthy. Output space
6027 * statistics for each one, as well as health status summary.
6030 zpool_do_list(int argc
, char **argv
)
6034 list_cbdata_t cb
= { 0 };
6035 static char default_props
[] =
6036 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6037 "capacity,dedupratio,health,altroot";
6038 char *props
= default_props
;
6040 unsigned long count
= 0;
6042 boolean_t first
= B_TRUE
;
6045 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6048 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6051 cb
.cb_scripted
= B_TRUE
;
6054 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6060 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6063 cb
.cb_literal
= B_TRUE
;
6066 get_timestamp_arg(*optarg
);
6069 cb
.cb_verbose
= B_TRUE
;
6070 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6073 (void) fprintf(stderr
, gettext("missing argument for "
6074 "'%c' option\n"), optopt
);
6078 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6087 get_interval_count(&argc
, argv
, &interval
, &count
);
6089 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6093 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6097 if (pool_list_count(list
) == 0)
6100 cb
.cb_namewidth
= 0;
6101 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6103 if (timestamp_fmt
!= NODATE
)
6104 print_timestamp(timestamp_fmt
);
6106 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6110 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6115 if (count
!= 0 && --count
== 0)
6118 pool_list_free(list
);
6119 (void) fsleep(interval
);
6122 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6123 (void) printf(gettext("no pools available\n"));
6127 pool_list_free(list
);
6128 zprop_free_list(cb
.cb_proplist
);
6133 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6135 boolean_t force
= B_FALSE
;
6136 boolean_t rebuild
= B_FALSE
;
6137 boolean_t wait
= B_FALSE
;
6140 char *poolname
, *old_disk
, *new_disk
;
6141 zpool_handle_t
*zhp
;
6142 nvlist_t
*props
= NULL
;
6147 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6153 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6154 (void) fprintf(stderr
, gettext("missing "
6155 "'=' for -o option\n"));
6161 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6162 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6172 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6181 /* get pool name and check number of arguments */
6183 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6190 (void) fprintf(stderr
,
6191 gettext("missing <device> specification\n"));
6199 (void) fprintf(stderr
,
6200 gettext("missing <new_device> specification\n"));
6203 new_disk
= old_disk
;
6213 (void) fprintf(stderr
, gettext("too many arguments\n"));
6217 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6222 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6223 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6230 /* unless manually specified use "ashift" pool property (if set) */
6231 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6234 char strval
[ZPOOL_MAXPROPLEN
];
6236 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6237 if (src
!= ZPROP_SRC_DEFAULT
) {
6238 (void) sprintf(strval
, "%" PRId32
, intval
);
6239 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6240 &props
, B_TRUE
) == 0);
6244 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6246 if (nvroot
== NULL
) {
6252 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6255 if (ret
== 0 && wait
)
6256 ret
= zpool_wait(zhp
,
6257 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6260 nvlist_free(nvroot
);
6267 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6269 * -f Force attach, even if <new_device> appears to be in use.
6270 * -s Use sequential instead of healing reconstruction for resilver.
6271 * -o Set property=value.
6272 * -w Wait for replacing to complete before returning
6274 * Replace <device> with <new_device>.
6278 zpool_do_replace(int argc
, char **argv
)
6280 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6284 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6286 * -f Force attach, even if <new_device> appears to be in use.
6287 * -s Use sequential instead of healing reconstruction for resilver.
6288 * -o Set property=value.
6289 * -w Wait for resilvering to complete before returning
6291 * Attach <new_device> to the mirror containing <device>. If <device> is not
6292 * part of a mirror, then <device> will be transformed into a mirror of
6293 * <device> and <new_device>. In either case, <new_device> will begin life
6294 * with a DTL of [0, now], and will immediately begin to resilver itself.
6297 zpool_do_attach(int argc
, char **argv
)
6299 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6303 * zpool detach [-f] <pool> <device>
6305 * -f Force detach of <device>, even if DTLs argue against it
6306 * (not supported yet)
6308 * Detach a device from a mirror. The operation will be refused if <device>
6309 * is the last device in the mirror, or if the DTLs indicate that this device
6310 * has the only valid copy of some data.
6314 zpool_do_detach(int argc
, char **argv
)
6317 char *poolname
, *path
;
6318 zpool_handle_t
*zhp
;
6322 while ((c
= getopt(argc
, argv
, "")) != -1) {
6325 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6334 /* get pool name and check number of arguments */
6336 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6341 (void) fprintf(stderr
,
6342 gettext("missing <device> specification\n"));
6349 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6352 ret
= zpool_vdev_detach(zhp
, path
);
6360 * zpool split [-gLnP] [-o prop=val] ...
6362 * [-R altroot] <pool> <newpool> [<device> ...]
6364 * -g Display guid for individual vdev name.
6365 * -L Follow links when resolving vdev path name.
6366 * -n Do not split the pool, but display the resulting layout if
6367 * it were to be split.
6368 * -o Set property=value, or set mount options.
6369 * -P Display full path for vdev name.
6370 * -R Mount the split-off pool under an alternate root.
6371 * -l Load encryption keys while importing.
6373 * Splits the named pool and gives it the new pool name. Devices to be split
6374 * off may be listed, provided that no more than one device is specified
6375 * per top-level vdev mirror. The newly split pool is left in an exported
6376 * state unless -R is specified.
6378 * Restrictions: the top-level of the pool pool must only be made up of
6379 * mirrors; all devices in the pool must be healthy; no device may be
6380 * undergoing a resilvering operation.
6383 zpool_do_split(int argc
, char **argv
)
6385 char *srcpool
, *newpool
, *propval
;
6386 char *mntopts
= NULL
;
6389 boolean_t loadkeys
= B_FALSE
;
6390 zpool_handle_t
*zhp
;
6391 nvlist_t
*config
, *props
= NULL
;
6393 flags
.dryrun
= B_FALSE
;
6394 flags
.import
= B_FALSE
;
6395 flags
.name_flags
= 0;
6398 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6401 flags
.name_flags
|= VDEV_NAME_GUID
;
6404 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6407 flags
.import
= B_TRUE
;
6409 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6410 &props
, B_TRUE
) != 0) {
6419 flags
.dryrun
= B_TRUE
;
6422 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6425 if (add_prop_list(optarg
, propval
,
6426 &props
, B_TRUE
) != 0) {
6435 flags
.name_flags
|= VDEV_NAME_PATH
;
6438 (void) fprintf(stderr
, gettext("missing argument for "
6439 "'%c' option\n"), optopt
);
6443 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6450 if (!flags
.import
&& mntopts
!= NULL
) {
6451 (void) fprintf(stderr
, gettext("setting mntopts is only "
6452 "valid when importing the pool\n"));
6456 if (!flags
.import
&& loadkeys
) {
6457 (void) fprintf(stderr
, gettext("loading keys is only "
6458 "valid when importing the pool\n"));
6466 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6470 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6480 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6485 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6486 if (config
== NULL
) {
6490 (void) printf(gettext("would create '%s' with the "
6491 "following layout:\n\n"), newpool
);
6492 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6499 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6500 nvlist_free(config
);
6506 * The split was successful. Now we need to open the new
6507 * pool and import it.
6509 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6510 nvlist_free(config
);
6516 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6521 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6522 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6524 (void) fprintf(stderr
, gettext("Split was successful, but "
6525 "the datasets could not all be mounted\n"));
6526 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6527 "different altroot\n"), "zpool import");
6530 nvlist_free(config
);
6539 * zpool online <pool> <device> ...
6542 zpool_do_online(int argc
, char **argv
)
6546 zpool_handle_t
*zhp
;
6548 vdev_state_t newstate
;
6552 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6555 flags
|= ZFS_ONLINE_EXPAND
;
6558 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6567 /* get pool name and check number of arguments */
6569 (void) fprintf(stderr
, gettext("missing pool name\n"));
6573 (void) fprintf(stderr
, gettext("missing device name\n"));
6579 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6582 for (i
= 1; i
< argc
; i
++) {
6583 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6584 if (newstate
!= VDEV_STATE_HEALTHY
) {
6585 (void) printf(gettext("warning: device '%s' "
6586 "onlined, but remains in faulted state\n"),
6588 if (newstate
== VDEV_STATE_FAULTED
)
6589 (void) printf(gettext("use 'zpool "
6590 "clear' to restore a faulted "
6593 (void) printf(gettext("use 'zpool "
6594 "replace' to replace devices "
6595 "that are no longer present\n"));
6608 * zpool offline [-ft] <pool> <device> ...
6610 * -f Force the device into a faulted state.
6612 * -t Only take the device off-line temporarily. The offline/faulted
6613 * state will not be persistent across reboots.
6617 zpool_do_offline(int argc
, char **argv
)
6621 zpool_handle_t
*zhp
;
6623 boolean_t istmp
= B_FALSE
;
6624 boolean_t fault
= B_FALSE
;
6627 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6636 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6645 /* get pool name and check number of arguments */
6647 (void) fprintf(stderr
, gettext("missing pool name\n"));
6651 (void) fprintf(stderr
, gettext("missing device name\n"));
6657 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6660 for (i
= 1; i
< argc
; i
++) {
6662 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6664 if (istmp
== B_FALSE
) {
6665 /* Force the fault to persist across imports */
6666 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6668 aux
= VDEV_AUX_EXTERNAL
;
6671 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6674 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6685 * zpool clear <pool> [device]
6687 * Clear all errors associated with a pool or a particular device.
6690 zpool_do_clear(int argc
, char **argv
)
6694 boolean_t dryrun
= B_FALSE
;
6695 boolean_t do_rewind
= B_FALSE
;
6696 boolean_t xtreme_rewind
= B_FALSE
;
6697 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6698 nvlist_t
*policy
= NULL
;
6699 zpool_handle_t
*zhp
;
6700 char *pool
, *device
;
6703 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6712 xtreme_rewind
= B_TRUE
;
6715 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6725 (void) fprintf(stderr
, gettext("missing pool name\n"));
6730 (void) fprintf(stderr
, gettext("too many arguments\n"));
6734 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6735 (void) fprintf(stderr
,
6736 gettext("-n or -X only meaningful with -F\n"));
6740 rewind_policy
= ZPOOL_TRY_REWIND
;
6742 rewind_policy
= ZPOOL_DO_REWIND
;
6744 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6746 /* In future, further rewind policy choices can be passed along here */
6747 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6748 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
6749 rewind_policy
) != 0) {
6754 device
= argc
== 2 ? argv
[1] : NULL
;
6756 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
6757 nvlist_free(policy
);
6761 if (zpool_clear(zhp
, device
, policy
) != 0)
6766 nvlist_free(policy
);
6772 * zpool reguid <pool>
6775 zpool_do_reguid(int argc
, char **argv
)
6779 zpool_handle_t
*zhp
;
6783 while ((c
= getopt(argc
, argv
, "")) != -1) {
6786 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6795 /* get pool name and check number of arguments */
6797 (void) fprintf(stderr
, gettext("missing pool name\n"));
6802 (void) fprintf(stderr
, gettext("too many arguments\n"));
6807 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6810 ret
= zpool_reguid(zhp
);
6818 * zpool reopen <pool>
6820 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6823 zpool_do_reopen(int argc
, char **argv
)
6827 boolean_t scrub_restart
= B_TRUE
;
6830 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6833 scrub_restart
= B_FALSE
;
6836 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6845 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6846 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
6852 typedef struct scrub_cbdata
{
6854 pool_scrub_cmd_t cb_scrub_cmd
;
6858 zpool_has_checkpoint(zpool_handle_t
*zhp
)
6860 nvlist_t
*config
, *nvroot
;
6862 config
= zpool_get_config(zhp
, NULL
);
6864 if (config
!= NULL
) {
6865 pool_checkpoint_stat_t
*pcs
= NULL
;
6868 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6869 (void) nvlist_lookup_uint64_array(nvroot
,
6870 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6872 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6875 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
6876 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6884 scrub_callback(zpool_handle_t
*zhp
, void *data
)
6886 scrub_cbdata_t
*cb
= data
;
6890 * Ignore faulted pools.
6892 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
6893 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
6894 "currently unavailable\n"), zpool_get_name(zhp
));
6898 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
6900 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
6901 cb
->cb_type
== POOL_SCAN_SCRUB
) {
6902 (void) printf(gettext("warning: will not scrub state that "
6903 "belongs to the checkpoint of pool '%s'\n"),
6904 zpool_get_name(zhp
));
6911 wait_callback(zpool_handle_t
*zhp
, void *data
)
6913 zpool_wait_activity_t
*act
= data
;
6914 return (zpool_wait(zhp
, *act
));
6918 * zpool scrub [-s | -p] [-w] <pool> ...
6920 * -s Stop. Stops any in-progress scrub.
6921 * -p Pause. Pause in-progress scrub.
6922 * -w Wait. Blocks until scrub has completed.
6925 zpool_do_scrub(int argc
, char **argv
)
6929 boolean_t wait
= B_FALSE
;
6932 cb
.cb_type
= POOL_SCAN_SCRUB
;
6933 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6936 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
6939 cb
.cb_type
= POOL_SCAN_NONE
;
6942 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
6948 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6954 if (cb
.cb_type
== POOL_SCAN_NONE
&&
6955 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
6956 (void) fprintf(stderr
, gettext("invalid option combination: "
6957 "-s and -p are mutually exclusive\n"));
6961 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
6962 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
6963 (void) fprintf(stderr
, gettext("invalid option combination: "
6964 "-w cannot be used with -p or -s\n"));
6972 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6976 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
);
6978 if (wait
&& !error
) {
6979 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
6980 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, wait_callback
,
6988 * zpool resilver <pool> ...
6990 * Restarts any in-progress resilver
6993 zpool_do_resilver(int argc
, char **argv
)
6998 cb
.cb_type
= POOL_SCAN_RESILVER
;
6999 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7002 while ((c
= getopt(argc
, argv
, "")) != -1) {
7005 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7015 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7019 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
7023 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7025 * -c Cancel. Ends any in-progress trim.
7026 * -d Secure trim. Requires kernel and device support.
7027 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7028 * adding a multiplier suffix such as 'k' or 'm'.
7029 * -s Suspend. TRIM can then be restarted with no flags.
7030 * -w Wait. Blocks until trimming has completed.
7033 zpool_do_trim(int argc
, char **argv
)
7035 struct option long_options
[] = {
7036 {"cancel", no_argument
, NULL
, 'c'},
7037 {"secure", no_argument
, NULL
, 'd'},
7038 {"rate", required_argument
, NULL
, 'r'},
7039 {"suspend", no_argument
, NULL
, 's'},
7040 {"wait", no_argument
, NULL
, 'w'},
7044 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7046 boolean_t secure
= B_FALSE
;
7047 boolean_t wait
= B_FALSE
;
7050 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7054 if (cmd_type
!= POOL_TRIM_START
&&
7055 cmd_type
!= POOL_TRIM_CANCEL
) {
7056 (void) fprintf(stderr
, gettext("-c cannot be "
7057 "combined with other options\n"));
7060 cmd_type
= POOL_TRIM_CANCEL
;
7063 if (cmd_type
!= POOL_TRIM_START
) {
7064 (void) fprintf(stderr
, gettext("-d cannot be "
7065 "combined with the -c or -s options\n"));
7071 if (cmd_type
!= POOL_TRIM_START
) {
7072 (void) fprintf(stderr
, gettext("-r cannot be "
7073 "combined with the -c or -s options\n"));
7076 if (zfs_nicestrtonum(NULL
, optarg
, &rate
) == -1) {
7077 (void) fprintf(stderr
,
7078 gettext("invalid value for rate\n"));
7083 if (cmd_type
!= POOL_TRIM_START
&&
7084 cmd_type
!= POOL_TRIM_SUSPEND
) {
7085 (void) fprintf(stderr
, gettext("-s cannot be "
7086 "combined with other options\n"));
7089 cmd_type
= POOL_TRIM_SUSPEND
;
7096 (void) fprintf(stderr
,
7097 gettext("invalid option '%c'\n"), optopt
);
7099 (void) fprintf(stderr
,
7100 gettext("invalid option '%s'\n"),
7111 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7116 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7117 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7122 char *poolname
= argv
[0];
7123 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7127 trimflags_t trim_flags
= {
7133 nvlist_t
*vdevs
= fnvlist_alloc();
7135 /* no individual leaf vdevs specified, so add them all */
7136 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7137 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7138 ZPOOL_CONFIG_VDEV_TREE
);
7139 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7140 trim_flags
.fullpool
= B_TRUE
;
7142 trim_flags
.fullpool
= B_FALSE
;
7143 for (int i
= 1; i
< argc
; i
++) {
7144 fnvlist_add_boolean(vdevs
, argv
[i
]);
7148 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7150 fnvlist_free(vdevs
);
7157 * Converts a total number of seconds to a human readable string broken
7158 * down in to days/hours/minutes/seconds.
7161 secs_to_dhms(uint64_t total
, char *buf
)
7163 uint64_t days
= total
/ 60 / 60 / 24;
7164 uint64_t hours
= (total
/ 60 / 60) % 24;
7165 uint64_t mins
= (total
/ 60) % 60;
7166 uint64_t secs
= (total
% 60);
7169 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7170 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7171 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7173 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7174 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7175 (u_longlong_t
)secs
);
7180 * Print out detailed scrub status.
7183 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7185 time_t start
, end
, pause
;
7186 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7187 uint64_t elapsed
, scan_rate
, issue_rate
;
7188 double fraction_done
;
7189 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7190 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7193 printf_color(ANSI_BOLD
, gettext("scan:"));
7196 /* If there's never been a scan, there's not much to say. */
7197 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7198 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7199 (void) printf(gettext("none requested\n"));
7203 start
= ps
->pss_start_time
;
7204 end
= ps
->pss_end_time
;
7205 pause
= ps
->pss_pass_scrub_pause
;
7207 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7209 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7210 ps
->pss_func
== POOL_SCAN_RESILVER
);
7212 /* Scan is finished or canceled. */
7213 if (ps
->pss_state
== DSS_FINISHED
) {
7214 secs_to_dhms(end
- start
, time_buf
);
7216 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7217 (void) printf(gettext("scrub repaired %s "
7218 "in %s with %llu errors on %s"), processed_buf
,
7219 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7221 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7222 (void) printf(gettext("resilvered %s "
7223 "in %s with %llu errors on %s"), processed_buf
,
7224 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7228 } else if (ps
->pss_state
== DSS_CANCELED
) {
7229 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7230 (void) printf(gettext("scrub canceled on %s"),
7232 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7233 (void) printf(gettext("resilver canceled on %s"),
7239 assert(ps
->pss_state
== DSS_SCANNING
);
7241 /* Scan is in progress. Resilvers can't be paused. */
7242 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7244 (void) printf(gettext("scrub in progress since %s"),
7247 (void) printf(gettext("scrub paused since %s"),
7249 (void) printf(gettext("\tscrub started on %s"),
7252 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7253 (void) printf(gettext("resilver in progress since %s"),
7257 scanned
= ps
->pss_examined
;
7258 pass_scanned
= ps
->pss_pass_exam
;
7259 issued
= ps
->pss_issued
;
7260 pass_issued
= ps
->pss_pass_issued
;
7261 total
= ps
->pss_to_examine
;
7263 /* we are only done with a block once we have issued the IO for it */
7264 fraction_done
= (double)issued
/ total
;
7266 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7267 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7268 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7269 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7271 scan_rate
= pass_scanned
/ elapsed
;
7272 issue_rate
= pass_issued
/ elapsed
;
7273 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7274 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7275 secs_to_dhms(total_secs_left
, time_buf
);
7277 /* format all of the numbers we will be reporting */
7278 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7279 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7280 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7281 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7282 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7284 /* do not print estimated time if we have a paused scrub */
7286 (void) printf(gettext("\t%s scanned at %s/s, "
7287 "%s issued at %s/s, %s total\n"),
7288 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7290 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7291 scanned_buf
, issued_buf
, total_buf
);
7294 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7295 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7296 processed_buf
, 100 * fraction_done
);
7297 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7298 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7299 processed_buf
, 100 * fraction_done
);
7303 if (total_secs_left
!= UINT64_MAX
&&
7304 issue_rate
>= 10 * 1024 * 1024) {
7305 (void) printf(gettext(", %s to go\n"), time_buf
);
7307 (void) printf(gettext(", no estimated "
7308 "completion time\n"));
7311 (void) printf(gettext("\n"));
7316 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7318 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7322 printf_color(ANSI_BOLD
, gettext("scan:"));
7325 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7326 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7327 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7328 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7329 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7330 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7331 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7332 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7333 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7334 (bytes_est
+ 1), 100);
7336 /* Format all of the numbers we will be reporting */
7337 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7338 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7339 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7340 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7341 sizeof (bytes_scanned_buf
));
7342 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7343 sizeof (bytes_issued_buf
));
7344 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7345 sizeof (bytes_rebuilt_buf
));
7346 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7347 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7348 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7350 time_t start
= vrs
->vrs_start_time
;
7351 time_t end
= vrs
->vrs_end_time
;
7353 /* Rebuild is finished or canceled. */
7354 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7355 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7356 (void) printf(gettext("resilvered (%s) %s in %s "
7357 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7358 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7360 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7361 (void) printf(gettext("resilver (%s) canceled on %s"),
7362 vdev_name
, ctime(&end
));
7364 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7365 (void) printf(gettext("resilver (%s) in progress since %s"),
7366 vdev_name
, ctime(&start
));
7369 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7371 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7372 MAX(scan_rate
, 1), time_buf
);
7374 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7375 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7376 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7377 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7378 bytes_rebuilt_buf
, scan_pct
);
7380 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7381 if (scan_rate
>= 10 * 1024 * 1024) {
7382 (void) printf(gettext(", %s to go\n"), time_buf
);
7384 (void) printf(gettext(", no estimated "
7385 "completion time\n"));
7388 (void) printf(gettext("\n"));
7393 * Print rebuild status for top-level vdevs.
7396 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7401 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7402 &child
, &children
) != 0)
7405 for (uint_t c
= 0; c
< children
; c
++) {
7406 vdev_rebuild_stat_t
*vrs
;
7409 if (nvlist_lookup_uint64_array(child
[c
],
7410 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7411 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7412 child
[c
], VDEV_NAME_TYPE_ID
);
7413 print_rebuild_status_impl(vrs
, name
);
7420 * As we don't scrub checkpointed blocks, we want to warn the user that we
7421 * skipped scanning some blocks if a checkpoint exists or existed at any
7422 * time during the scan. If a sequential instead of healing reconstruction
7423 * was performed then the blocks were reconstructed. However, their checksums
7424 * have not been verified so we still print the warning.
7427 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7429 if (ps
== NULL
|| pcs
== NULL
)
7432 if (pcs
->pcs_state
== CS_NONE
||
7433 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7436 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7438 if (ps
->pss_state
== DSS_NONE
)
7441 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7442 ps
->pss_end_time
< pcs
->pcs_start_time
)
7445 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7446 (void) printf(gettext(" scan warning: skipped blocks "
7447 "that are only referenced by the checkpoint.\n"));
7449 assert(ps
->pss_state
== DSS_SCANNING
);
7450 (void) printf(gettext(" scan warning: skipping blocks "
7451 "that are only referenced by the checkpoint.\n"));
7456 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7457 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7458 * the last completed (or cancelled) rebuild.
7461 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7465 boolean_t rebuilding
= B_FALSE
;
7466 uint64_t end_time
= 0;
7468 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7469 &child
, &children
) != 0)
7472 for (uint_t c
= 0; c
< children
; c
++) {
7473 vdev_rebuild_stat_t
*vrs
;
7476 if (nvlist_lookup_uint64_array(child
[c
],
7477 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7479 if (vrs
->vrs_end_time
> end_time
)
7480 end_time
= vrs
->vrs_end_time
;
7482 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7483 rebuilding
= B_TRUE
;
7490 if (rebuild_end_time
!= NULL
)
7491 *rebuild_end_time
= end_time
;
7493 return (rebuilding
);
7497 * Print the scan status.
7500 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7502 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7503 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7504 boolean_t active_resilver
= B_FALSE
;
7505 pool_checkpoint_stat_t
*pcs
= NULL
;
7506 pool_scan_stat_t
*ps
= NULL
;
7509 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7510 (uint64_t **)&ps
, &c
) == 0) {
7511 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7512 resilver_end_time
= ps
->pss_end_time
;
7513 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7516 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7517 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7520 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7521 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7523 /* Always print the scrub status when available. */
7525 print_scan_scrub_resilver_status(ps
);
7528 * When there is an active resilver or rebuild print its status.
7529 * Otherwise print the status of the last resilver or rebuild.
7531 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7532 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7533 print_scan_scrub_resilver_status(ps
);
7534 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7535 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7536 print_rebuild_status(zhp
, nvroot
);
7539 (void) nvlist_lookup_uint64_array(nvroot
,
7540 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7541 print_checkpoint_scan_warning(ps
, pcs
);
7545 * Print out detailed removal status.
7548 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7550 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7552 nvlist_t
*config
, *nvroot
;
7557 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7561 * Determine name of vdev.
7563 config
= zpool_get_config(zhp
, NULL
);
7564 nvroot
= fnvlist_lookup_nvlist(config
,
7565 ZPOOL_CONFIG_VDEV_TREE
);
7566 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7567 &child
, &children
) == 0);
7568 assert(prs
->prs_removing_vdev
< children
);
7569 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7570 child
[prs
->prs_removing_vdev
], B_TRUE
);
7572 (void) printf(gettext("remove: "));
7574 start
= prs
->prs_start_time
;
7575 end
= prs
->prs_end_time
;
7576 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7579 * Removal is finished or canceled.
7581 if (prs
->prs_state
== DSS_FINISHED
) {
7582 uint64_t minutes_taken
= (end
- start
) / 60;
7584 (void) printf(gettext("Removal of vdev %llu copied %s "
7585 "in %lluh%um, completed on %s"),
7586 (longlong_t
)prs
->prs_removing_vdev
,
7588 (u_longlong_t
)(minutes_taken
/ 60),
7589 (uint_t
)(minutes_taken
% 60),
7590 ctime((time_t *)&end
));
7591 } else if (prs
->prs_state
== DSS_CANCELED
) {
7592 (void) printf(gettext("Removal of %s canceled on %s"),
7593 vdev_name
, ctime(&end
));
7595 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7596 double fraction_done
;
7599 assert(prs
->prs_state
== DSS_SCANNING
);
7602 * Removal is in progress.
7604 (void) printf(gettext(
7605 "Evacuation of %s in progress since %s"),
7606 vdev_name
, ctime(&start
));
7608 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7609 total
= prs
->prs_to_copy
;
7610 fraction_done
= (double)copied
/ total
;
7612 /* elapsed time for this pass */
7613 elapsed
= time(NULL
) - prs
->prs_start_time
;
7614 elapsed
= elapsed
> 0 ? elapsed
: 1;
7615 rate
= copied
/ elapsed
;
7616 rate
= rate
> 0 ? rate
: 1;
7617 mins_left
= ((total
- copied
) / rate
) / 60;
7618 hours_left
= mins_left
/ 60;
7620 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7621 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7622 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7625 * do not print estimated time if hours_left is more than
7628 (void) printf(gettext(" %s copied out of %s at %s/s, "
7630 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7631 if (hours_left
< (30 * 24)) {
7632 (void) printf(gettext(", %lluh%um to go\n"),
7633 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7635 (void) printf(gettext(
7636 ", (copy is slow, no estimated time)\n"));
7641 if (prs
->prs_mapping_memory
> 0) {
7643 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7644 (void) printf(gettext(" %s memory used for "
7645 "removed device mappings\n"),
7651 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7656 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7659 (void) printf(gettext("checkpoint: "));
7661 start
= pcs
->pcs_start_time
;
7662 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7664 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7665 char *date
= ctime(&start
);
7668 * ctime() adds a newline at the end of the generated
7669 * string, thus the weird format specifier and the
7670 * strlen() call used to chop it off from the output.
7672 (void) printf(gettext("created %.*s, consumes %s\n"),
7673 (int)(strlen(date
) - 1), date
, space_buf
);
7677 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7679 (void) printf(gettext("discarding, %s remaining.\n"),
7684 print_error_log(zpool_handle_t
*zhp
)
7686 nvlist_t
*nverrlist
= NULL
;
7689 size_t len
= MAXPATHLEN
* 2;
7691 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
7694 (void) printf("errors: Permanent errors have been "
7695 "detected in the following files:\n\n");
7697 pathname
= safe_malloc(len
);
7699 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
7701 uint64_t dsobj
, obj
;
7703 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
7704 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
7706 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
7708 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
7709 (void) printf("%7s %s\n", "", pathname
);
7712 nvlist_free(nverrlist
);
7716 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
7725 (void) printf(gettext("\tspares\n"));
7727 for (i
= 0; i
< nspares
; i
++) {
7728 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
7730 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
7736 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
7745 (void) printf(gettext("\tcache\n"));
7747 for (i
= 0; i
< nl2cache
; i
++) {
7748 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
7750 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
7757 print_dedup_stats(nvlist_t
*config
)
7759 ddt_histogram_t
*ddh
;
7763 char dspace
[6], mspace
[6];
7766 * If the pool was faulted then we may not have been able to
7767 * obtain the config. Otherwise, if we have anything in the dedup
7768 * table continue processing the stats.
7770 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
7771 (uint64_t **)&ddo
, &c
) != 0)
7774 (void) printf("\n");
7775 (void) printf(gettext(" dedup: "));
7776 if (ddo
->ddo_count
== 0) {
7777 (void) printf(gettext("no DDT entries\n"));
7781 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
7782 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
7783 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
7784 (u_longlong_t
)ddo
->ddo_count
,
7788 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
7789 (uint64_t **)&dds
, &c
) == 0);
7790 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
7791 (uint64_t **)&ddh
, &c
) == 0);
7792 zpool_dump_ddt(dds
, ddh
);
7796 * Display a summary of pool status. Displays a summary such as:
7800 * reason: One or more devices ...
7801 * see: https://zfsonlinux.org/msg/ZFS-xxxx-01
7807 * When given the '-v' option, we print out the complete config. If the '-e'
7808 * option is specified, then we print out error rate information as well.
7811 status_callback(zpool_handle_t
*zhp
, void *data
)
7813 status_cbdata_t
*cbp
= data
;
7814 nvlist_t
*config
, *nvroot
;
7816 zpool_status_t reason
;
7817 zpool_errata_t errata
;
7822 config
= zpool_get_config(zhp
, NULL
);
7823 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
7828 * If we were given 'zpool status -x', only report those pools with
7831 if (cbp
->cb_explain
&&
7832 (reason
== ZPOOL_STATUS_OK
||
7833 reason
== ZPOOL_STATUS_VERSION_OLDER
||
7834 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
7835 if (!cbp
->cb_allpools
) {
7836 (void) printf(gettext("pool '%s' is healthy\n"),
7837 zpool_get_name(zhp
));
7839 cbp
->cb_first
= B_FALSE
;
7845 cbp
->cb_first
= B_FALSE
;
7847 (void) printf("\n");
7849 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7850 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
7851 (uint64_t **)&vs
, &c
) == 0);
7853 health
= zpool_get_state_str(zhp
);
7856 printf_color(ANSI_BOLD
, gettext("pool:"));
7857 printf(" %s\n", zpool_get_name(zhp
));
7859 printf_color(ANSI_BOLD
, gettext("state: "));
7861 printf_color(health_str_to_color(health
), "%s", health
);
7866 case ZPOOL_STATUS_MISSING_DEV_R
:
7867 printf_color(ANSI_BOLD
, gettext("status: "));
7868 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7869 "not be opened. Sufficient replicas exist for\n\tthe pool "
7870 "to continue functioning in a degraded state.\n"));
7871 printf_color(ANSI_BOLD
, gettext("action: "));
7872 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7873 "and online it using 'zpool online'.\n"));
7876 case ZPOOL_STATUS_MISSING_DEV_NR
:
7877 printf_color(ANSI_BOLD
, gettext("status: "));
7878 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7879 "not be opened. There are insufficient\n\treplicas for the"
7880 " pool to continue functioning.\n"));
7881 printf_color(ANSI_BOLD
, gettext("action: "));
7882 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7883 "and online it using 'zpool online'.\n"));
7886 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
7887 printf_color(ANSI_BOLD
, gettext("status: "));
7888 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7889 "not be used because the label is missing or\n\tinvalid. "
7890 "Sufficient replicas exist for the pool to continue\n\t"
7891 "functioning in a degraded state.\n"));
7892 printf_color(ANSI_BOLD
, gettext("action: "));
7893 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
7894 "'zpool replace'.\n"));
7897 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
7898 printf_color(ANSI_BOLD
, gettext("status: "));
7899 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7900 "not be used because the label is missing \n\tor invalid. "
7901 "There are insufficient replicas for the pool to "
7902 "continue\n\tfunctioning.\n"));
7903 zpool_explain_recover(zpool_get_handle(zhp
),
7904 zpool_get_name(zhp
), reason
, config
);
7907 case ZPOOL_STATUS_FAILING_DEV
:
7908 printf_color(ANSI_BOLD
, gettext("status: "));
7909 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7910 "experienced an unrecoverable error. An\n\tattempt was "
7911 "made to correct the error. Applications are "
7913 printf_color(ANSI_BOLD
, gettext("action: "));
7914 printf_color(ANSI_YELLOW
, gettext("Determine if the "
7915 "device needs to be replaced, and clear the errors\n\tusing"
7916 " 'zpool clear' or replace the device with 'zpool "
7920 case ZPOOL_STATUS_OFFLINE_DEV
:
7921 printf_color(ANSI_BOLD
, gettext("status: "));
7922 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7923 "been taken offline by the administrator.\n\tSufficient "
7924 "replicas exist for the pool to continue functioning in "
7925 "a\n\tdegraded state.\n"));
7926 printf_color(ANSI_BOLD
, gettext("action: "));
7927 printf_color(ANSI_YELLOW
, gettext("Online the device "
7928 "using 'zpool online' or replace the device with\n\t'zpool "
7932 case ZPOOL_STATUS_REMOVED_DEV
:
7933 printf_color(ANSI_BOLD
, gettext("status: "));
7934 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7935 "been removed by the administrator.\n\tSufficient "
7936 "replicas exist for the pool to continue functioning in "
7937 "a\n\tdegraded state.\n"));
7938 printf_color(ANSI_BOLD
, gettext("action: "));
7939 printf_color(ANSI_YELLOW
, gettext("Online the device "
7940 "using zpool online' or replace the device with\n\t'zpool "
7944 case ZPOOL_STATUS_RESILVERING
:
7945 case ZPOOL_STATUS_REBUILDING
:
7946 printf_color(ANSI_BOLD
, gettext("status: "));
7947 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
7948 "currently being resilvered. The pool will\n\tcontinue "
7949 "to function, possibly in a degraded state.\n"));
7950 printf_color(ANSI_BOLD
, gettext("action: "));
7951 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
7955 case ZPOOL_STATUS_REBUILD_SCRUB
:
7956 printf_color(ANSI_BOLD
, gettext("status: "));
7957 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
7958 "been sequentially resilvered, scrubbing\n\tthe pool "
7959 "is recommended.\n"));
7960 printf_color(ANSI_BOLD
, gettext("action: "));
7961 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
7962 "verify all data checksums.\n"));
7965 case ZPOOL_STATUS_CORRUPT_DATA
:
7966 printf_color(ANSI_BOLD
, gettext("status: "));
7967 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
7968 "experienced an error resulting in data\n\tcorruption. "
7969 "Applications may be affected.\n"));
7970 printf_color(ANSI_BOLD
, gettext("action: "));
7971 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
7972 " if possible. Otherwise restore the\n\tentire pool from "
7976 case ZPOOL_STATUS_CORRUPT_POOL
:
7977 printf_color(ANSI_BOLD
, gettext("status: "));
7978 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
7979 "corrupted and the pool cannot be opened.\n"));
7980 zpool_explain_recover(zpool_get_handle(zhp
),
7981 zpool_get_name(zhp
), reason
, config
);
7984 case ZPOOL_STATUS_VERSION_OLDER
:
7985 printf_color(ANSI_BOLD
, gettext("status: "));
7986 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
7987 "a legacy on-disk format. The pool can\n\tstill be used, "
7988 "but some features are unavailable.\n"));
7989 printf_color(ANSI_BOLD
, gettext("action: "));
7990 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
7991 "'zpool upgrade'. Once this is done, the\n\tpool will no "
7992 "longer be accessible on software that does not support\n\t"
7993 "feature flags.\n"));
7996 case ZPOOL_STATUS_VERSION_NEWER
:
7997 printf_color(ANSI_BOLD
, gettext("status: "));
7998 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
7999 "to a newer, incompatible on-disk version.\n\tThe pool "
8000 "cannot be accessed on this system.\n"));
8001 printf_color(ANSI_BOLD
, gettext("action: "));
8002 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8003 "system running more recent software, or\n\trestore the "
8004 "pool from backup.\n"));
8007 case ZPOOL_STATUS_FEAT_DISABLED
:
8008 printf_color(ANSI_BOLD
, gettext("status: "));
8009 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
8010 "not enabled on the pool. The pool can\n\tstill be used, "
8011 "but some features are unavailable.\n"));
8012 printf_color(ANSI_BOLD
, gettext("action: "));
8013 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8014 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8015 "longer be accessible by software that does not support\n\t"
8016 "the features. See zpool-features(5) for details.\n"));
8019 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8020 printf_color(ANSI_BOLD
, gettext("status: "));
8021 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8022 "on this system because it uses the\n\tfollowing feature(s)"
8023 " not supported on this system:\n"));
8024 zpool_print_unsup_feat(config
);
8025 (void) printf("\n");
8026 printf_color(ANSI_BOLD
, gettext("action: "));
8027 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8028 "system that supports the required feature(s),\n\tor "
8029 "restore the pool from backup.\n"));
8032 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8033 printf_color(ANSI_BOLD
, gettext("status: "));
8034 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8035 "accessed in read-only mode on this system. It\n\tcannot be"
8036 " accessed in read-write mode because it uses the "
8037 "following\n\tfeature(s) not supported on this system:\n"));
8038 zpool_print_unsup_feat(config
);
8039 (void) printf("\n");
8040 printf_color(ANSI_BOLD
, gettext("action: "));
8041 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8042 "in read-write mode. Import the pool with\n"
8043 "\t\"-o readonly=on\", access the pool from a system that "
8044 "supports the\n\trequired feature(s), or restore the "
8045 "pool from backup.\n"));
8048 case ZPOOL_STATUS_FAULTED_DEV_R
:
8049 printf_color(ANSI_BOLD
, gettext("status: "));
8050 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8051 "faulted in response to persistent errors.\n\tSufficient "
8052 "replicas exist for the pool to continue functioning "
8053 "in a\n\tdegraded state.\n"));
8054 printf_color(ANSI_BOLD
, gettext("action: "));
8055 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8056 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8059 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8060 printf_color(ANSI_BOLD
, gettext("status: "));
8061 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8062 "faulted in response to persistent errors. There are "
8063 "insufficient replicas for the pool to\n\tcontinue "
8065 printf_color(ANSI_BOLD
, gettext("action: "));
8066 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8067 "pool from a backup source. Manually marking the device\n"
8068 "\trepaired using 'zpool clear' may allow some data "
8069 "to be recovered.\n"));
8072 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8073 printf_color(ANSI_BOLD
, gettext("status: "));
8074 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8075 "because multihost writes failed or were delayed;\n\t"
8076 "another system could import the pool undetected.\n"));
8077 printf_color(ANSI_BOLD
, gettext("action: "));
8078 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8079 " are connected, then reboot your system and\n\timport the "
8083 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8084 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8085 printf_color(ANSI_BOLD
, gettext("status: "));
8086 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8087 "faulted in response to IO failures.\n"));
8088 printf_color(ANSI_BOLD
, gettext("action: "));
8089 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8090 "devices are connected, then run 'zpool clear'.\n"));
8093 case ZPOOL_STATUS_BAD_LOG
:
8094 printf_color(ANSI_BOLD
, gettext("status: "));
8095 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8096 "could not be read.\n"
8097 "\tWaiting for administrator intervention to fix the "
8098 "faulted pool.\n"));
8099 printf_color(ANSI_BOLD
, gettext("action: "));
8100 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8101 "device(s) and run 'zpool online',\n"
8102 "\tor ignore the intent log records by running "
8103 "'zpool clear'.\n"));
8106 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8107 printf_color(ANSI_BOLD
, gettext("status: "));
8108 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8109 " and system hostid on imported pool.\n\tThis pool was "
8110 "previously imported into a system with a different "
8111 "hostid,\n\tand then was verbatim imported into this "
8113 printf_color(ANSI_BOLD
, gettext("action: "));
8114 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8115 "systems on which it is imported.\n"
8116 "\tThen import it to correct the mismatch.\n"));
8119 case ZPOOL_STATUS_ERRATA
:
8120 printf_color(ANSI_BOLD
, gettext("status: "));
8121 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8125 case ZPOOL_ERRATA_NONE
:
8128 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8129 printf_color(ANSI_BOLD
, gettext("action: "));
8130 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8131 " run 'zpool scrub'.\n"));
8134 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8135 (void) printf(gettext("\tExisting encrypted datasets "
8136 "contain an on-disk incompatibility\n\twhich "
8137 "needs to be corrected.\n"));
8138 printf_color(ANSI_BOLD
, gettext("action: "));
8139 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8140 " backup existing encrypted datasets to new\n\t"
8141 "encrypted datasets and destroy the old ones. "
8142 "'zfs mount -o ro' can\n\tbe used to temporarily "
8143 "mount existing encrypted datasets readonly.\n"));
8146 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8147 (void) printf(gettext("\tExisting encrypted snapshots "
8148 "and bookmarks contain an on-disk\n\tincompat"
8149 "ibility. This may cause on-disk corruption if "
8150 "they are used\n\twith 'zfs recv'.\n"));
8151 printf_color(ANSI_BOLD
, gettext("action: "));
8152 printf_color(ANSI_YELLOW
, gettext("To correct the"
8153 "issue, enable the bookmark_v2 feature. No "
8154 "additional\n\taction is needed if there are no "
8155 "encrypted snapshots or bookmarks.\n\tIf preserving"
8156 "the encrypted snapshots and bookmarks is required,"
8157 " use\n\ta non-raw send to backup and restore them."
8158 " Alternately, they may be\n\tremoved to resolve "
8159 "the incompatibility.\n"));
8164 * All errata which allow the pool to be imported
8165 * must contain an action message.
8173 * The remaining errors can't actually be generated, yet.
8175 assert(reason
== ZPOOL_STATUS_OK
);
8178 if (msgid
!= NULL
) {
8180 printf_color(ANSI_BOLD
, gettext("see:"));
8181 printf(gettext(" https://zfsonlinux.org/msg/%s\n"), msgid
);
8184 if (config
!= NULL
) {
8186 nvlist_t
**spares
, **l2cache
;
8187 uint_t nspares
, nl2cache
;
8188 pool_checkpoint_stat_t
*pcs
= NULL
;
8189 pool_removal_stat_t
*prs
= NULL
;
8191 print_scan_status(zhp
, nvroot
);
8193 (void) nvlist_lookup_uint64_array(nvroot
,
8194 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8195 print_removal_status(zhp
, prs
);
8197 (void) nvlist_lookup_uint64_array(nvroot
,
8198 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8199 print_checkpoint_status(pcs
);
8201 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8202 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8203 if (cbp
->cb_namewidth
< 10)
8204 cbp
->cb_namewidth
= 10;
8206 color_start(ANSI_BOLD
);
8207 (void) printf(gettext("config:\n\n"));
8208 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8209 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8213 if (cbp
->cb_print_slow_ios
) {
8214 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8217 if (cbp
->vcdl
!= NULL
)
8218 print_cmd_columns(cbp
->vcdl
, 0);
8222 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8225 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8226 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8227 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8229 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8230 &l2cache
, &nl2cache
) == 0)
8231 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8233 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8234 &spares
, &nspares
) == 0)
8235 print_spares(zhp
, cbp
, spares
, nspares
);
8237 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8239 nvlist_t
*nverrlist
= NULL
;
8242 * If the approximate error count is small, get a
8243 * precise count by fetching the entire log and
8244 * uniquifying the results.
8246 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8247 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8252 while ((elem
= nvlist_next_nvpair(nverrlist
,
8257 nvlist_free(nverrlist
);
8259 (void) printf("\n");
8262 (void) printf(gettext("errors: No known data "
8264 else if (!cbp
->cb_verbose
)
8265 (void) printf(gettext("errors: %llu data "
8266 "errors, use '-v' for a list\n"),
8267 (u_longlong_t
)nerr
);
8269 print_error_log(zhp
);
8272 if (cbp
->cb_dedup_stats
)
8273 print_dedup_stats(config
);
8275 (void) printf(gettext("config: The configuration cannot be "
8283 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8284 * [interval [count]]
8286 * -c CMD For each vdev, run command CMD
8287 * -i Display vdev initialization status.
8288 * -g Display guid for individual vdev name.
8289 * -L Follow links when resolving vdev path name.
8290 * -p Display values in parsable (exact) format.
8291 * -P Display full path for vdev name.
8292 * -s Display slow IOs column.
8293 * -v Display complete error logs
8294 * -x Display only pools with potential problems
8295 * -D Display dedup status (undocumented)
8296 * -t Display vdev TRIM status.
8297 * -T Display a timestamp in date(1) or Unix format
8299 * Describes the health status of all pools or some subset.
8302 zpool_do_status(int argc
, char **argv
)
8307 unsigned long count
= 0;
8308 status_cbdata_t cb
= { 0 };
8312 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8317 gettext("Can't set -c flag twice\n"));
8321 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8322 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8323 fprintf(stderr
, gettext(
8324 "Can't run -c, disabled by "
8325 "ZPOOL_SCRIPTS_ENABLED.\n"));
8329 if ((getuid() <= 0 || geteuid() <= 0) &&
8330 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8331 fprintf(stderr
, gettext(
8332 "Can't run -c with root privileges "
8333 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8339 cb
.cb_print_vdev_init
= B_TRUE
;
8342 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8345 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8348 cb
.cb_literal
= B_TRUE
;
8351 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8354 cb
.cb_print_slow_ios
= B_TRUE
;
8357 cb
.cb_verbose
= B_TRUE
;
8360 cb
.cb_explain
= B_TRUE
;
8363 cb
.cb_dedup_stats
= B_TRUE
;
8366 cb
.cb_print_vdev_trim
= B_TRUE
;
8369 get_timestamp_arg(*optarg
);
8372 if (optopt
== 'c') {
8373 print_zpool_script_list("status");
8377 gettext("invalid option '%c'\n"), optopt
);
8386 get_interval_count(&argc
, argv
, &interval
, &count
);
8389 cb
.cb_allpools
= B_TRUE
;
8391 cb
.cb_first
= B_TRUE
;
8392 cb
.cb_print_status
= B_TRUE
;
8395 if (timestamp_fmt
!= NODATE
)
8396 print_timestamp(timestamp_fmt
);
8399 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8402 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
8403 status_callback
, &cb
);
8405 if (cb
.vcdl
!= NULL
)
8406 free_vdev_cmd_data_list(cb
.vcdl
);
8408 if (argc
== 0 && cb
.cb_count
== 0)
8409 (void) fprintf(stderr
, gettext("no pools available\n"));
8410 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8411 (void) printf(gettext("all pools are healthy\n"));
8419 if (count
!= 0 && --count
== 0)
8422 (void) fsleep(interval
);
8428 typedef struct upgrade_cbdata
{
8431 uint64_t cb_version
;
8436 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8438 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8439 int *count
= (int *)unsupp_fs
;
8441 if (zfs_version
> ZPL_VERSION
) {
8442 (void) printf(gettext("%s (v%d) is not supported by this "
8443 "implementation of ZFS.\n"),
8444 zfs_get_name(zhp
), zfs_version
);
8448 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8456 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8460 uint64_t oldversion
;
8463 config
= zpool_get_config(zhp
, NULL
);
8464 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8467 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8468 assert(oldversion
< version
);
8470 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8475 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8476 "to %d unsupported filesystems (max v%d).\n"),
8477 unsupp_fs
, (int)ZPL_VERSION
);
8481 ret
= zpool_upgrade(zhp
, version
);
8485 if (version
>= SPA_VERSION_FEATURES
) {
8486 (void) printf(gettext("Successfully upgraded "
8487 "'%s' from version %llu to feature flags.\n"),
8488 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8490 (void) printf(gettext("Successfully upgraded "
8491 "'%s' from version %llu to version %llu.\n"),
8492 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8493 (u_longlong_t
)version
);
8500 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8503 boolean_t firstff
= B_TRUE
;
8504 nvlist_t
*enabled
= zpool_get_features(zhp
);
8507 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8508 const char *fname
= spa_feature_table
[i
].fi_uname
;
8509 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8510 if (!nvlist_exists(enabled
, fguid
)) {
8512 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8513 ret
= zpool_set_prop(zhp
, propname
,
8514 ZFS_FEATURE_ENABLED
);
8522 (void) printf(gettext("Enabled the "
8523 "following features on '%s':\n"),
8524 zpool_get_name(zhp
));
8527 (void) printf(gettext(" %s\n"), fname
);
8538 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8540 upgrade_cbdata_t
*cbp
= arg
;
8543 boolean_t printnl
= B_FALSE
;
8546 config
= zpool_get_config(zhp
, NULL
);
8547 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8550 assert(SPA_VERSION_IS_SUPPORTED(version
));
8552 if (version
< cbp
->cb_version
) {
8553 cbp
->cb_first
= B_FALSE
;
8554 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8560 * If they did "zpool upgrade -a", then we could
8561 * be doing ioctls to different pools. We need
8562 * to log this history once to each pool, and bypass
8563 * the normal history logging that happens in main().
8565 (void) zpool_log_history(g_zfs
, history_str
);
8566 log_history
= B_FALSE
;
8569 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8571 ret
= upgrade_enable_all(zhp
, &count
);
8576 cbp
->cb_first
= B_FALSE
;
8582 (void) printf(gettext("\n"));
8589 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8591 upgrade_cbdata_t
*cbp
= arg
;
8595 config
= zpool_get_config(zhp
, NULL
);
8596 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8599 assert(SPA_VERSION_IS_SUPPORTED(version
));
8601 if (version
< SPA_VERSION_FEATURES
) {
8602 if (cbp
->cb_first
) {
8603 (void) printf(gettext("The following pools are "
8604 "formatted with legacy version numbers and can\n"
8605 "be upgraded to use feature flags. After "
8606 "being upgraded, these pools\nwill no "
8607 "longer be accessible by software that does not "
8608 "support feature\nflags.\n\n"));
8609 (void) printf(gettext("VER POOL\n"));
8610 (void) printf(gettext("--- ------------\n"));
8611 cbp
->cb_first
= B_FALSE
;
8614 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
8615 zpool_get_name(zhp
));
8622 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
8624 upgrade_cbdata_t
*cbp
= arg
;
8628 config
= zpool_get_config(zhp
, NULL
);
8629 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8632 if (version
>= SPA_VERSION_FEATURES
) {
8634 boolean_t poolfirst
= B_TRUE
;
8635 nvlist_t
*enabled
= zpool_get_features(zhp
);
8637 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8638 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8639 const char *fname
= spa_feature_table
[i
].fi_uname
;
8640 if (!nvlist_exists(enabled
, fguid
)) {
8641 if (cbp
->cb_first
) {
8642 (void) printf(gettext("\nSome "
8643 "supported features are not "
8644 "enabled on the following pools. "
8645 "Once a\nfeature is enabled the "
8646 "pool may become incompatible with "
8647 "software\nthat does not support "
8649 "zpool-features(5) for "
8651 (void) printf(gettext("POOL "
8653 (void) printf(gettext("------"
8655 cbp
->cb_first
= B_FALSE
;
8659 (void) printf(gettext("%s\n"),
8660 zpool_get_name(zhp
));
8661 poolfirst
= B_FALSE
;
8664 (void) printf(gettext(" %s\n"), fname
);
8667 * If they did "zpool upgrade -a", then we could
8668 * be doing ioctls to different pools. We need
8669 * to log this history once to each pool, and bypass
8670 * the normal history logging that happens in main().
8672 (void) zpool_log_history(g_zfs
, history_str
);
8673 log_history
= B_FALSE
;
8682 upgrade_one(zpool_handle_t
*zhp
, void *data
)
8684 boolean_t printnl
= B_FALSE
;
8685 upgrade_cbdata_t
*cbp
= data
;
8686 uint64_t cur_version
;
8689 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
8690 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
8691 "Pool 'log' must be renamed using export and import"
8696 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
8697 if (cur_version
> cbp
->cb_version
) {
8698 (void) printf(gettext("Pool '%s' is already formatted "
8699 "using more current version '%llu'.\n\n"),
8700 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
8704 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
8705 (void) printf(gettext("Pool '%s' is already formatted "
8706 "using version %llu.\n\n"), zpool_get_name(zhp
),
8707 (u_longlong_t
)cbp
->cb_version
);
8711 if (cur_version
!= cbp
->cb_version
) {
8713 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8718 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8720 ret
= upgrade_enable_all(zhp
, &count
);
8726 } else if (cur_version
== SPA_VERSION
) {
8727 (void) printf(gettext("Pool '%s' already has all "
8728 "supported features enabled.\n"),
8729 zpool_get_name(zhp
));
8734 (void) printf(gettext("\n"));
8743 * zpool upgrade [-V version] <-a | pool ...>
8745 * With no arguments, display downrev'd ZFS pool available for upgrade.
8746 * Individual pools can be upgraded by specifying the pool, and '-a' will
8747 * upgrade all pools.
8750 zpool_do_upgrade(int argc
, char **argv
)
8753 upgrade_cbdata_t cb
= { 0 };
8755 boolean_t showversions
= B_FALSE
;
8756 boolean_t upgradeall
= B_FALSE
;
8761 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
8764 upgradeall
= B_TRUE
;
8767 showversions
= B_TRUE
;
8770 cb
.cb_version
= strtoll(optarg
, &end
, 10);
8772 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
8773 (void) fprintf(stderr
,
8774 gettext("invalid version '%s'\n"), optarg
);
8779 (void) fprintf(stderr
, gettext("missing argument for "
8780 "'%c' option\n"), optopt
);
8784 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8795 if (cb
.cb_version
== 0) {
8796 cb
.cb_version
= SPA_VERSION
;
8797 } else if (!upgradeall
&& argc
== 0) {
8798 (void) fprintf(stderr
, gettext("-V option is "
8799 "incompatible with other arguments\n"));
8804 if (upgradeall
|| argc
!= 0) {
8805 (void) fprintf(stderr
, gettext("-v option is "
8806 "incompatible with other arguments\n"));
8809 } else if (upgradeall
) {
8811 (void) fprintf(stderr
, gettext("-a option should not "
8812 "be used along with a pool name\n"));
8817 (void) printf(gettext("This system supports ZFS pool feature "
8822 (void) printf(gettext("The following features are "
8824 (void) printf(gettext("FEAT DESCRIPTION\n"));
8825 (void) printf("----------------------------------------------"
8826 "---------------\n");
8827 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8828 zfeature_info_t
*fi
= &spa_feature_table
[i
];
8830 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
8831 " (read-only compatible)" : "";
8833 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
8834 (void) printf(" %s\n", fi
->fi_desc
);
8836 (void) printf("\n");
8838 (void) printf(gettext("The following legacy versions are also "
8840 (void) printf(gettext("VER DESCRIPTION\n"));
8841 (void) printf("--- -----------------------------------------"
8842 "---------------\n");
8843 (void) printf(gettext(" 1 Initial ZFS version\n"));
8844 (void) printf(gettext(" 2 Ditto blocks "
8845 "(replicated metadata)\n"));
8846 (void) printf(gettext(" 3 Hot spares and double parity "
8848 (void) printf(gettext(" 4 zpool history\n"));
8849 (void) printf(gettext(" 5 Compression using the gzip "
8851 (void) printf(gettext(" 6 bootfs pool property\n"));
8852 (void) printf(gettext(" 7 Separate intent log devices\n"));
8853 (void) printf(gettext(" 8 Delegated administration\n"));
8854 (void) printf(gettext(" 9 refquota and refreservation "
8856 (void) printf(gettext(" 10 Cache devices\n"));
8857 (void) printf(gettext(" 11 Improved scrub performance\n"));
8858 (void) printf(gettext(" 12 Snapshot properties\n"));
8859 (void) printf(gettext(" 13 snapused property\n"));
8860 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
8861 (void) printf(gettext(" 15 user/group space accounting\n"));
8862 (void) printf(gettext(" 16 stmf property support\n"));
8863 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
8864 (void) printf(gettext(" 18 Snapshot user holds\n"));
8865 (void) printf(gettext(" 19 Log device removal\n"));
8866 (void) printf(gettext(" 20 Compression using zle "
8867 "(zero-length encoding)\n"));
8868 (void) printf(gettext(" 21 Deduplication\n"));
8869 (void) printf(gettext(" 22 Received properties\n"));
8870 (void) printf(gettext(" 23 Slim ZIL\n"));
8871 (void) printf(gettext(" 24 System attributes\n"));
8872 (void) printf(gettext(" 25 Improved scrub stats\n"));
8873 (void) printf(gettext(" 26 Improved snapshot deletion "
8875 (void) printf(gettext(" 27 Improved snapshot creation "
8877 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
8878 (void) printf(gettext("\nFor more information on a particular "
8879 "version, including supported releases,\n"));
8880 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
8881 } else if (argc
== 0 && upgradeall
) {
8882 cb
.cb_first
= B_TRUE
;
8883 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
8884 if (ret
== 0 && cb
.cb_first
) {
8885 if (cb
.cb_version
== SPA_VERSION
) {
8886 (void) printf(gettext("All pools are already "
8887 "formatted using feature flags.\n\n"));
8888 (void) printf(gettext("Every feature flags "
8889 "pool already has all supported features "
8892 (void) printf(gettext("All pools are already "
8893 "formatted with version %llu or higher.\n"),
8894 (u_longlong_t
)cb
.cb_version
);
8897 } else if (argc
== 0) {
8898 cb
.cb_first
= B_TRUE
;
8899 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
8903 (void) printf(gettext("All pools are formatted "
8904 "using feature flags.\n\n"));
8906 (void) printf(gettext("\nUse 'zpool upgrade -v' "
8907 "for a list of available legacy versions.\n"));
8910 cb
.cb_first
= B_TRUE
;
8911 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
8915 (void) printf(gettext("Every feature flags pool has "
8916 "all supported features enabled.\n"));
8918 (void) printf(gettext("\n"));
8921 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
8928 typedef struct hist_cbdata
{
8935 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
8941 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
8942 &records
, &numrecords
) == 0);
8943 for (i
= 0; i
< numrecords
; i
++) {
8944 nvlist_t
*rec
= records
[i
];
8947 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
8951 tsec
= fnvlist_lookup_uint64(records
[i
],
8953 (void) localtime_r(&tsec
, &t
);
8954 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
8957 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
8958 (void) printf("%s %s", tbuf
,
8959 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
8960 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
8962 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
8965 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
8966 (void) printf("%s unrecognized record:\n",
8968 dump_nvlist(rec
, 4);
8971 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
8972 zfs_history_event_names
[ievent
],
8973 (longlong_t
)fnvlist_lookup_uint64(
8974 rec
, ZPOOL_HIST_TXG
),
8975 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
8976 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
8979 (void) printf("%s [txg:%lld] %s", tbuf
,
8980 (longlong_t
)fnvlist_lookup_uint64(
8981 rec
, ZPOOL_HIST_TXG
),
8982 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
8983 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
8984 (void) printf(" %s (%llu)",
8985 fnvlist_lookup_string(rec
,
8987 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
8990 (void) printf(" %s", fnvlist_lookup_string(rec
,
8991 ZPOOL_HIST_INT_STR
));
8992 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
8995 (void) printf("%s ioctl %s\n", tbuf
,
8996 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
8997 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
8998 (void) printf(" input:\n");
8999 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9000 ZPOOL_HIST_INPUT_NVL
), 8);
9002 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9003 (void) printf(" output:\n");
9004 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9005 ZPOOL_HIST_OUTPUT_NVL
), 8);
9007 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9008 (void) printf(" errno: %lld\n",
9009 (longlong_t
)fnvlist_lookup_int64(rec
,
9015 (void) printf("%s unrecognized record:\n", tbuf
);
9016 dump_nvlist(rec
, 4);
9020 (void) printf("\n");
9023 (void) printf(" [");
9024 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9025 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9026 struct passwd
*pwd
= getpwuid(who
);
9027 (void) printf("user %d ", (int)who
);
9029 (void) printf("(%s) ", pwd
->pw_name
);
9031 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9032 (void) printf("on %s",
9033 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9035 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9036 (void) printf(":%s",
9037 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9041 (void) printf("\n");
9046 * Print out the command history for a specific pool.
9049 get_history_one(zpool_handle_t
*zhp
, void *data
)
9053 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9055 boolean_t eof
= B_FALSE
;
9057 cb
->first
= B_FALSE
;
9059 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9062 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9065 print_history_records(nvhis
, cb
);
9068 (void) printf("\n");
9074 * zpool history <pool>
9076 * Displays the history of commands that modified pools.
9079 zpool_do_history(int argc
, char **argv
)
9081 hist_cbdata_t cbdata
= { 0 };
9085 cbdata
.first
= B_TRUE
;
9087 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9090 cbdata
.longfmt
= B_TRUE
;
9093 cbdata
.internal
= B_TRUE
;
9096 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9104 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
9107 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9108 (void) fprintf(stderr
, gettext("no pools available\n"));
9115 typedef struct ev_opts
{
9120 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9124 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9126 char ctime_str
[26], str
[32], *ptr
;
9130 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9131 memset(str
, ' ', 32);
9132 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9133 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9134 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9135 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9136 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9138 (void) printf(gettext("%s\t"), str
);
9140 (void) printf(gettext("%s "), str
);
9142 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9143 (void) printf(gettext("%s\n"), ptr
);
9147 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9151 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9152 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9154 data_type_t type
= nvpair_type(nvp
);
9155 const char *name
= nvpair_name(nvp
);
9165 printf(gettext("%*s%s = "), depth
, "", name
);
9168 case DATA_TYPE_BOOLEAN
:
9169 printf(gettext("%s"), "1");
9172 case DATA_TYPE_BOOLEAN_VALUE
:
9173 (void) nvpair_value_boolean_value(nvp
, &b
);
9174 printf(gettext("%s"), b
? "1" : "0");
9177 case DATA_TYPE_BYTE
:
9178 (void) nvpair_value_byte(nvp
, &i8
);
9179 printf(gettext("0x%x"), i8
);
9182 case DATA_TYPE_INT8
:
9183 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9184 printf(gettext("0x%x"), i8
);
9187 case DATA_TYPE_UINT8
:
9188 (void) nvpair_value_uint8(nvp
, &i8
);
9189 printf(gettext("0x%x"), i8
);
9192 case DATA_TYPE_INT16
:
9193 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9194 printf(gettext("0x%x"), i16
);
9197 case DATA_TYPE_UINT16
:
9198 (void) nvpair_value_uint16(nvp
, &i16
);
9199 printf(gettext("0x%x"), i16
);
9202 case DATA_TYPE_INT32
:
9203 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9204 printf(gettext("0x%x"), i32
);
9207 case DATA_TYPE_UINT32
:
9208 (void) nvpair_value_uint32(nvp
, &i32
);
9209 printf(gettext("0x%x"), i32
);
9212 case DATA_TYPE_INT64
:
9213 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9214 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9217 case DATA_TYPE_UINT64
:
9218 (void) nvpair_value_uint64(nvp
, &i64
);
9220 * translate vdev state values to readable
9221 * strings to aide zpool events consumers
9224 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9226 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9227 printf(gettext("\"%s\" (0x%llx)"),
9228 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9231 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9235 case DATA_TYPE_HRTIME
:
9236 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9237 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9240 case DATA_TYPE_STRING
:
9241 (void) nvpair_value_string(nvp
, &str
);
9242 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9245 case DATA_TYPE_NVLIST
:
9246 printf(gettext("(embedded nvlist)\n"));
9247 (void) nvpair_value_nvlist(nvp
, &cnv
);
9248 zpool_do_events_nvprint(cnv
, depth
+ 8);
9249 printf(gettext("%*s(end %s)"), depth
, "", name
);
9252 case DATA_TYPE_NVLIST_ARRAY
: {
9256 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9257 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9258 for (i
= 0; i
< nelem
; i
++) {
9259 printf(gettext("%*s%s[%d] = %s\n"),
9260 depth
, "", name
, i
, "(embedded nvlist)");
9261 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9262 printf(gettext("%*s(end %s[%i])\n"),
9263 depth
, "", name
, i
);
9265 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9269 case DATA_TYPE_INT8_ARRAY
: {
9273 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9274 for (i
= 0; i
< nelem
; i
++)
9275 printf(gettext("0x%x "), val
[i
]);
9280 case DATA_TYPE_UINT8_ARRAY
: {
9284 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9285 for (i
= 0; i
< nelem
; i
++)
9286 printf(gettext("0x%x "), val
[i
]);
9291 case DATA_TYPE_INT16_ARRAY
: {
9295 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9296 for (i
= 0; i
< nelem
; i
++)
9297 printf(gettext("0x%x "), val
[i
]);
9302 case DATA_TYPE_UINT16_ARRAY
: {
9306 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9307 for (i
= 0; i
< nelem
; i
++)
9308 printf(gettext("0x%x "), val
[i
]);
9313 case DATA_TYPE_INT32_ARRAY
: {
9317 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9318 for (i
= 0; i
< nelem
; i
++)
9319 printf(gettext("0x%x "), val
[i
]);
9324 case DATA_TYPE_UINT32_ARRAY
: {
9328 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9329 for (i
= 0; i
< nelem
; i
++)
9330 printf(gettext("0x%x "), val
[i
]);
9335 case DATA_TYPE_INT64_ARRAY
: {
9339 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9340 for (i
= 0; i
< nelem
; i
++)
9341 printf(gettext("0x%llx "),
9342 (u_longlong_t
)val
[i
]);
9347 case DATA_TYPE_UINT64_ARRAY
: {
9351 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9352 for (i
= 0; i
< nelem
; i
++)
9353 printf(gettext("0x%llx "),
9354 (u_longlong_t
)val
[i
]);
9359 case DATA_TYPE_STRING_ARRAY
: {
9363 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9364 for (i
= 0; i
< nelem
; i
++)
9365 printf(gettext("\"%s\" "),
9366 str
[i
] ? str
[i
] : "<NULL>");
9371 case DATA_TYPE_BOOLEAN_ARRAY
:
9372 case DATA_TYPE_BYTE_ARRAY
:
9373 case DATA_TYPE_DOUBLE
:
9374 case DATA_TYPE_DONTCARE
:
9375 case DATA_TYPE_UNKNOWN
:
9376 printf(gettext("<unknown>"));
9380 printf(gettext("\n"));
9385 zpool_do_events_next(ev_opts_t
*opts
)
9388 int zevent_fd
, ret
, dropped
;
9391 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9392 VERIFY(zevent_fd
>= 0);
9394 if (!opts
->scripted
)
9395 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9398 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9399 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9400 if (ret
|| nvl
== NULL
)
9404 (void) printf(gettext("dropped %d events\n"), dropped
);
9406 if (strlen(opts
->poolname
) > 0 &&
9407 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9408 strcmp(opts
->poolname
, pool
) != 0)
9411 zpool_do_events_short(nvl
, opts
);
9413 if (opts
->verbose
) {
9414 zpool_do_events_nvprint(nvl
, 8);
9415 printf(gettext("\n"));
9417 (void) fflush(stdout
);
9422 VERIFY(0 == close(zevent_fd
));
9428 zpool_do_events_clear(ev_opts_t
*opts
)
9432 ret
= zpool_events_clear(g_zfs
, &count
);
9434 (void) printf(gettext("cleared %d events\n"), count
);
9440 * zpool events [-vHf [pool] | -c]
9442 * Displays events logs by ZFS.
9445 zpool_do_events(int argc
, char **argv
)
9447 ev_opts_t opts
= { 0 };
9452 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9476 (void) fprintf(stderr
, gettext("too many arguments\n"));
9478 } else if (argc
== 1) {
9479 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9480 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9481 (void) fprintf(stderr
,
9482 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9487 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9489 (void) fprintf(stderr
,
9490 gettext("invalid options combined with -c\n"));
9495 ret
= zpool_do_events_clear(&opts
);
9497 ret
= zpool_do_events_next(&opts
);
9503 get_callback(zpool_handle_t
*zhp
, void *data
)
9505 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9506 char value
[MAXNAMELEN
];
9507 zprop_source_t srctype
;
9510 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
9513 * Skip the special fake placeholder. This will also skip
9514 * over the name property when 'all' is specified.
9516 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9517 pl
== cbp
->cb_proplist
)
9520 if (pl
->pl_prop
== ZPROP_INVAL
&&
9521 (zpool_prop_feature(pl
->pl_user_prop
) ||
9522 zpool_prop_unsupported(pl
->pl_user_prop
))) {
9523 srctype
= ZPROP_SRC_LOCAL
;
9525 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
9526 value
, sizeof (value
)) == 0) {
9527 zprop_print_one_property(zpool_get_name(zhp
),
9528 cbp
, pl
->pl_user_prop
, value
, srctype
,
9532 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
9533 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
9536 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
9537 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
9545 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9547 * -H Scripted mode. Don't display headers, and separate properties
9549 * -o List of columns to display. Defaults to
9550 * "name,property,value,source".
9551 * -p Display values in parsable (exact) format.
9553 * Get properties of pools in the system. Output space statistics
9554 * for each one as well as other attributes.
9557 zpool_do_get(int argc
, char **argv
)
9559 zprop_get_cbdata_t cb
= { 0 };
9560 zprop_list_t fake_name
= { 0 };
9565 cb
.cb_first
= B_TRUE
;
9568 * Set up default columns and sources.
9570 cb
.cb_sources
= ZPROP_SRC_ALL
;
9571 cb
.cb_columns
[0] = GET_COL_NAME
;
9572 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9573 cb
.cb_columns
[2] = GET_COL_VALUE
;
9574 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9575 cb
.cb_type
= ZFS_TYPE_POOL
;
9578 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
9581 cb
.cb_literal
= B_TRUE
;
9584 cb
.cb_scripted
= B_TRUE
;
9587 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
9589 while (*optarg
!= '\0') {
9590 static char *col_subopts
[] =
9591 { "name", "property", "value", "source",
9594 if (i
== ZFS_GET_NCOLS
) {
9595 (void) fprintf(stderr
, gettext("too "
9596 "many fields given to -o "
9601 switch (getsubopt(&optarg
, col_subopts
,
9604 cb
.cb_columns
[i
++] = GET_COL_NAME
;
9607 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
9610 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
9613 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
9617 (void) fprintf(stderr
,
9618 gettext("\"all\" conflicts "
9619 "with specific fields "
9620 "given to -o option\n"));
9623 cb
.cb_columns
[0] = GET_COL_NAME
;
9624 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9625 cb
.cb_columns
[2] = GET_COL_VALUE
;
9626 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9630 (void) fprintf(stderr
,
9631 gettext("invalid column name "
9638 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9648 (void) fprintf(stderr
, gettext("missing property "
9653 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
9654 ZFS_TYPE_POOL
) != 0)
9660 if (cb
.cb_proplist
!= NULL
) {
9661 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
9662 fake_name
.pl_width
= strlen(gettext("NAME"));
9663 fake_name
.pl_next
= cb
.cb_proplist
;
9664 cb
.cb_proplist
= &fake_name
;
9667 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
9670 if (cb
.cb_proplist
== &fake_name
)
9671 zprop_free_list(fake_name
.pl_next
);
9673 zprop_free_list(cb
.cb_proplist
);
9678 typedef struct set_cbdata
{
9681 boolean_t cb_any_successful
;
9685 set_callback(zpool_handle_t
*zhp
, void *data
)
9688 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
9690 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
9693 cb
->cb_any_successful
= B_TRUE
;
9699 zpool_do_set(int argc
, char **argv
)
9701 set_cbdata_t cb
= { 0 };
9704 if (argc
> 1 && argv
[1][0] == '-') {
9705 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9711 (void) fprintf(stderr
, gettext("missing property=value "
9717 (void) fprintf(stderr
, gettext("missing pool name\n"));
9722 (void) fprintf(stderr
, gettext("too many pool names\n"));
9726 cb
.cb_propname
= argv
[1];
9727 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
9728 if (cb
.cb_value
== NULL
) {
9729 (void) fprintf(stderr
, gettext("missing value in "
9730 "property=value argument\n"));
9734 *(cb
.cb_value
) = '\0';
9737 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
9743 /* Add up the total number of bytes left to initialize/trim across all vdevs */
9745 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
9747 uint64_t bytes_remaining
;
9752 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
9753 activity
== ZPOOL_WAIT_TRIM
);
9755 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
9756 (uint64_t **)&vs
, &c
) == 0);
9758 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
9759 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
9760 bytes_remaining
= vs
->vs_initialize_bytes_est
-
9761 vs
->vs_initialize_bytes_done
;
9762 else if (activity
== ZPOOL_WAIT_TRIM
&&
9763 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
9764 bytes_remaining
= vs
->vs_trim_bytes_est
-
9765 vs
->vs_trim_bytes_done
;
9767 bytes_remaining
= 0;
9769 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9770 &child
, &children
) != 0)
9773 for (c
= 0; c
< children
; c
++)
9774 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
9776 return (bytes_remaining
);
9779 /* Add up the total number of bytes left to rebuild across top-level vdevs */
9781 vdev_activity_top_remaining(nvlist_t
*nv
)
9783 uint64_t bytes_remaining
= 0;
9788 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9789 &child
, &children
) != 0)
9792 for (uint_t c
= 0; c
< children
; c
++) {
9793 vdev_rebuild_stat_t
*vrs
;
9796 error
= nvlist_lookup_uint64_array(child
[c
],
9797 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
9799 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
9800 bytes_remaining
+= (vrs
->vrs_bytes_est
-
9801 vrs
->vrs_bytes_rebuilt
);
9806 return (bytes_remaining
);
9809 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
9811 vdev_any_spare_replacing(nvlist_t
*nv
)
9817 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
9819 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
9820 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0) {
9824 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9825 &child
, &children
) != 0)
9828 for (c
= 0; c
< children
; c
++) {
9829 if (vdev_any_spare_replacing(child
[c
]))
9836 typedef struct wait_data
{
9838 boolean_t wd_scripted
;
9840 boolean_t wd_headers_once
;
9841 boolean_t wd_should_exit
;
9842 /* Which activities to wait for */
9843 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
9845 pthread_cond_t wd_cv
;
9846 pthread_mutex_t wd_mutex
;
9850 * Print to stdout a single line, containing one column for each activity that
9851 * we are waiting for specifying how many bytes of work are left for that
9855 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
9857 nvlist_t
*config
, *nvroot
;
9860 pool_checkpoint_stat_t
*pcs
= NULL
;
9861 pool_scan_stat_t
*pss
= NULL
;
9862 pool_removal_stat_t
*prs
= NULL
;
9863 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
9864 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
9865 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
9867 /* Calculate the width of each column */
9868 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9870 * Make sure we have enough space in the col for pretty-printed
9871 * numbers and for the column header, and then leave a couple
9872 * spaces between cols for readability.
9874 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
9877 /* Print header if appropriate */
9878 int term_height
= terminal_height();
9879 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
9880 row
% (term_height
-1) == 0);
9881 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
9882 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9883 if (wd
->wd_enabled
[i
])
9884 (void) printf("%*s", col_widths
[i
], headers
[i
]);
9886 (void) printf("\n");
9889 /* Bytes of work remaining in each activity */
9890 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
9892 bytes_rem
[ZPOOL_WAIT_FREE
] =
9893 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
9895 config
= zpool_get_config(zhp
, NULL
);
9896 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
9898 (void) nvlist_lookup_uint64_array(nvroot
,
9899 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
9900 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
9901 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
9903 (void) nvlist_lookup_uint64_array(nvroot
,
9904 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
9905 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
9906 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
9909 (void) nvlist_lookup_uint64_array(nvroot
,
9910 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
9911 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
9912 pss
->pss_pass_scrub_pause
== 0) {
9913 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
9914 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
9915 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
9917 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
9918 } else if (check_rebuilding(nvroot
, NULL
)) {
9919 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
9920 vdev_activity_top_remaining(nvroot
);
9923 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
9924 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
9925 bytes_rem
[ZPOOL_WAIT_TRIM
] =
9926 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
9929 * A replace finishes after resilvering finishes, so the amount of work
9930 * left for a replace is the same as for resilvering.
9932 * It isn't quite correct to say that if we have any 'spare' or
9933 * 'replacing' vdevs and a resilver is happening, then a replace is in
9934 * progress, like we do here. When a hot spare is used, the faulted vdev
9935 * is not removed after the hot spare is resilvered, so parent 'spare'
9936 * vdev is not removed either. So we could have a 'spare' vdev, but be
9937 * resilvering for a different reason. However, we use it as a heuristic
9938 * because we don't have access to the DTLs, which could tell us whether
9939 * or not we have really finished resilvering a hot spare.
9941 if (vdev_any_spare_replacing(nvroot
))
9942 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
9944 if (timestamp_fmt
!= NODATE
)
9945 print_timestamp(timestamp_fmt
);
9947 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
9949 if (!wd
->wd_enabled
[i
])
9953 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
9956 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
9958 if (wd
->wd_scripted
)
9959 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
9961 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
9963 (void) printf("\n");
9964 (void) fflush(stdout
);
9968 wait_status_thread(void *arg
)
9970 wait_data_t
*wd
= (wait_data_t
*)arg
;
9971 zpool_handle_t
*zhp
;
9973 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
9976 for (int row
= 0; ; row
++) {
9978 struct timespec timeout
;
9980 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
9982 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
9983 zpool_props_refresh(zhp
) != 0) {
9985 return (void *)(uintptr_t)(missing
? 0 : 1);
9988 print_wait_status_row(wd
, zhp
, row
);
9990 timeout
.tv_sec
+= floor(wd
->wd_interval
);
9991 long nanos
= timeout
.tv_nsec
+
9992 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
9993 if (nanos
>= NANOSEC
) {
9995 timeout
.tv_nsec
= nanos
- NANOSEC
;
9997 timeout
.tv_nsec
= nanos
;
9999 pthread_mutex_lock(&wd
->wd_mutex
);
10000 if (!wd
->wd_should_exit
)
10001 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10003 pthread_mutex_unlock(&wd
->wd_mutex
);
10005 break; /* signaled by main thread */
10006 } else if (ret
!= ETIMEDOUT
) {
10007 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10008 "failed: %s\n"), strerror(ret
));
10010 return (void *)(uintptr_t)(1);
10015 return (void *)(0);
10019 zpool_do_wait(int argc
, char **argv
)
10021 boolean_t verbose
= B_FALSE
;
10025 unsigned long count
;
10026 pthread_t status_thr
;
10028 zpool_handle_t
*zhp
;
10031 wd
.wd_scripted
= B_FALSE
;
10032 wd
.wd_exact
= B_FALSE
;
10033 wd
.wd_headers_once
= B_FALSE
;
10034 wd
.wd_should_exit
= B_FALSE
;
10036 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10037 pthread_cond_init(&wd
.wd_cv
, NULL
);
10039 /* By default, wait for all types of activity. */
10040 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10041 wd
.wd_enabled
[i
] = B_TRUE
;
10043 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10046 wd
.wd_scripted
= B_TRUE
;
10049 wd
.wd_headers_once
= B_TRUE
;
10052 wd
.wd_exact
= B_TRUE
;
10055 get_timestamp_arg(*optarg
);
10059 static char *col_subopts
[] = { "discard", "free",
10060 "initialize", "replace", "remove", "resilver",
10061 "scrub", "trim", NULL
};
10063 /* Reset activities array */
10064 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10065 while (*optarg
!= '\0') {
10066 int activity
= getsubopt(&optarg
, col_subopts
,
10069 if (activity
< 0) {
10070 (void) fprintf(stderr
,
10071 gettext("invalid activity '%s'\n"),
10076 wd
.wd_enabled
[activity
] = B_TRUE
;
10081 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10090 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10092 /* This subcmd only accepts an interval, not a count */
10093 (void) fprintf(stderr
, gettext("too many arguments\n"));
10097 if (wd
.wd_interval
!= 0)
10101 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10105 (void) fprintf(stderr
, gettext("too many arguments\n"));
10109 wd
.wd_poolname
= argv
[0];
10111 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10116 * We use a separate thread for printing status updates because
10117 * the main thread will call lzc_wait(), which blocks as long
10118 * as an activity is in progress, which can be a long time.
10120 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10122 (void) fprintf(stderr
, gettext("failed to create status"
10123 "thread: %s\n"), strerror(errno
));
10130 * Loop over all activities that we are supposed to wait for until none
10131 * of them are in progress. Note that this means we can end up waiting
10132 * for more activities to complete than just those that were in progress
10133 * when we began waiting; if an activity we are interested in begins
10134 * while we are waiting for another activity, we will wait for both to
10135 * complete before exiting.
10138 boolean_t missing
= B_FALSE
;
10139 boolean_t any_waited
= B_FALSE
;
10141 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10144 if (!wd
.wd_enabled
[i
])
10147 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10148 if (error
!= 0 || missing
)
10151 any_waited
= (any_waited
|| waited
);
10154 if (error
!= 0 || missing
|| !any_waited
)
10162 pthread_mutex_lock(&wd
.wd_mutex
);
10163 wd
.wd_should_exit
= B_TRUE
;
10164 pthread_cond_signal(&wd
.wd_cv
);
10165 pthread_mutex_unlock(&wd
.wd_mutex
);
10166 (void) pthread_join(status_thr
, (void *)&status
);
10171 pthread_mutex_destroy(&wd
.wd_mutex
);
10172 pthread_cond_destroy(&wd
.wd_cv
);
10177 find_command_idx(char *command
, int *idx
)
10181 for (i
= 0; i
< NCOMMAND
; i
++) {
10182 if (command_table
[i
].name
== NULL
)
10185 if (strcmp(command
, command_table
[i
].name
) == 0) {
10194 * Display version message
10197 zpool_do_version(int argc
, char **argv
)
10199 if (zfs_version_print() == -1)
10206 main(int argc
, char **argv
)
10213 (void) setlocale(LC_ALL
, "");
10214 (void) textdomain(TEXT_DOMAIN
);
10220 * Make sure the user has specified some command.
10223 (void) fprintf(stderr
, gettext("missing command\n"));
10230 * Special case '-?'
10232 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10236 * Special case '-V|--version'
10238 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10239 return (zpool_do_version(argc
, argv
));
10241 if ((g_zfs
= libzfs_init()) == NULL
) {
10242 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10246 libzfs_print_on_error(g_zfs
, B_TRUE
);
10248 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10251 * Many commands modify input strings for string parsing reasons.
10252 * We create a copy to protect the original argv.
10254 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10255 for (i
= 0; i
< argc
; i
++)
10256 newargv
[i
] = strdup(argv
[i
]);
10257 newargv
[argc
] = NULL
;
10260 * Run the appropriate command.
10262 if (find_command_idx(cmdname
, &i
) == 0) {
10263 current_command
= &command_table
[i
];
10264 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10265 } else if (strchr(cmdname
, '=')) {
10266 verify(find_command_idx("set", &i
) == 0);
10267 current_command
= &command_table
[i
];
10268 ret
= command_table
[i
].func(argc
, newargv
);
10269 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10271 * 'freeze' is a vile debugging abomination, so we treat
10274 zfs_cmd_t zc
= {"\0"};
10276 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10277 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10279 (void) fprintf(stderr
,
10280 gettext("failed to freeze pool: %d\n"), errno
);
10286 (void) fprintf(stderr
, gettext("unrecognized "
10287 "command '%s'\n"), cmdname
);
10292 for (i
= 0; i
< argc
; i
++)
10296 if (ret
== 0 && log_history
)
10297 (void) zpool_log_history(g_zfs
, history_str
);
10299 libzfs_fini(g_zfs
);
10302 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10303 * for the purposes of running ::findleaks.
10305 if (getenv("ZFS_ABORT") != NULL
) {
10306 (void) printf("dumping core by request\n");