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, 2016 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.
53 #include <sys/fs/zfs.h>
55 #include <sys/systeminfo.h>
56 #include <sys/fm/fs/zfs.h>
57 #include <sys/fm/util.h>
58 #include <sys/fm/protocol.h>
59 #include <sys/zfs_ioctl.h>
60 #include <sys/mount.h>
61 #include <sys/sysmacros.h>
67 #include "zpool_util.h"
68 #include "zfs_comutil.h"
69 #include "zfeature_common.h"
71 #include "statcommon.h"
73 static int zpool_do_create(int, char **);
74 static int zpool_do_destroy(int, char **);
76 static int zpool_do_add(int, char **);
77 static int zpool_do_remove(int, char **);
78 static int zpool_do_labelclear(int, char **);
80 static int zpool_do_checkpoint(int, char **);
82 static int zpool_do_list(int, char **);
83 static int zpool_do_iostat(int, char **);
84 static int zpool_do_status(int, char **);
86 static int zpool_do_online(int, char **);
87 static int zpool_do_offline(int, char **);
88 static int zpool_do_clear(int, char **);
89 static int zpool_do_reopen(int, char **);
91 static int zpool_do_reguid(int, char **);
93 static int zpool_do_attach(int, char **);
94 static int zpool_do_detach(int, char **);
95 static int zpool_do_replace(int, char **);
96 static int zpool_do_split(int, char **);
98 static int zpool_do_scrub(int, char **);
100 static int zpool_do_import(int, char **);
101 static int zpool_do_export(int, char **);
103 static int zpool_do_upgrade(int, char **);
105 static int zpool_do_history(int, char **);
106 static int zpool_do_events(int, char **);
108 static int zpool_do_get(int, char **);
109 static int zpool_do_set(int, char **);
111 static int zpool_do_sync(int, char **);
114 * These libumem hooks provide a reasonable set of defaults for the allocator's
115 * debugging facilities.
120 _umem_debug_init(void)
122 return ("default,verbose"); /* $UMEM_DEBUG setting */
126 _umem_logging_init(void)
128 return ("fail,contents"); /* $UMEM_LOGGING setting */
164 * Flags for stats to display with "zpool iostats"
172 IOS_COUNT
, /* always last element */
175 /* iostat_type entries as bitmasks */
176 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
177 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
178 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
179 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
180 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
182 /* Mask of all the histo bits */
183 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
186 * Lookup table for iostat flags to nvlist names. Basically a list
187 * of all the nvlists a flag requires. Also specifies the order in
188 * which data gets printed in zpool iostat.
190 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
192 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
193 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
194 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
195 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
196 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
197 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
198 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
199 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
200 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
203 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
204 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
205 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
206 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
209 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
210 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
211 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
212 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
213 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
216 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
217 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
218 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
219 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
220 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
221 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
222 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
223 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
224 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
225 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
231 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
232 * Right now, only one histo bit is ever set at one time, so we can
233 * just do a highbit64(a)
235 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
237 typedef struct zpool_command
{
239 int (*func
)(int, char **);
244 * Master command table. Each ZFS command has a name, associated function, and
245 * usage message. The usage messages need to be internationalized, so we have
246 * to have a function to return the usage message based on a command index.
248 * These commands are organized according to how they are displayed in the usage
249 * message. An empty command (one with a NULL name) indicates an empty line in
250 * the generic usage message.
252 static zpool_command_t command_table
[] = {
253 { "create", zpool_do_create
, HELP_CREATE
},
254 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
256 { "add", zpool_do_add
, HELP_ADD
},
257 { "remove", zpool_do_remove
, HELP_REMOVE
},
259 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
261 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
263 { "list", zpool_do_list
, HELP_LIST
},
264 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
265 { "status", zpool_do_status
, HELP_STATUS
},
267 { "online", zpool_do_online
, HELP_ONLINE
},
268 { "offline", zpool_do_offline
, HELP_OFFLINE
},
269 { "clear", zpool_do_clear
, HELP_CLEAR
},
270 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
272 { "attach", zpool_do_attach
, HELP_ATTACH
},
273 { "detach", zpool_do_detach
, HELP_DETACH
},
274 { "replace", zpool_do_replace
, HELP_REPLACE
},
275 { "split", zpool_do_split
, HELP_SPLIT
},
277 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
279 { "import", zpool_do_import
, HELP_IMPORT
},
280 { "export", zpool_do_export
, HELP_EXPORT
},
281 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
282 { "reguid", zpool_do_reguid
, HELP_REGUID
},
284 { "history", zpool_do_history
, HELP_HISTORY
},
285 { "events", zpool_do_events
, HELP_EVENTS
},
287 { "get", zpool_do_get
, HELP_GET
},
288 { "set", zpool_do_set
, HELP_SET
},
289 { "sync", zpool_do_sync
, HELP_SYNC
},
292 #define NCOMMAND (ARRAY_SIZE(command_table))
294 static zpool_command_t
*current_command
;
295 static char history_str
[HIS_MAX_RECORD_LEN
];
296 static boolean_t log_history
= B_TRUE
;
297 static uint_t timestamp_fmt
= NODATE
;
300 get_usage(zpool_help_t idx
)
304 return (gettext("\tadd [-fgLnP] [-o property=value] "
305 "<pool> <vdev> ...\n"));
307 return (gettext("\tattach [-f] [-o property=value] "
308 "<pool> <device> <new-device>\n"));
310 return (gettext("\tclear [-nF] <pool> [device]\n"));
312 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
313 "\t [-O file-system-property=value] ... \n"
314 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
315 case HELP_CHECKPOINT
:
316 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
318 return (gettext("\tdestroy [-f] <pool>\n"));
320 return (gettext("\tdetach <pool> <device>\n"));
322 return (gettext("\texport [-af] <pool> ...\n"));
324 return (gettext("\thistory [-il] [<pool>] ...\n"));
326 return (gettext("\timport [-d dir] [-D]\n"
327 "\timport [-o mntopts] [-o property=value] ... \n"
328 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
329 "[-R root] [-F [-n]] -a\n"
330 "\timport [-o mntopts] [-o property=value] ... \n"
331 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
332 "[-R root] [-F [-n]]\n"
333 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
335 return (gettext("\tiostat [[[-c [script1,script2,...]"
336 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
337 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
338 " [interval [count]]\n"));
339 case HELP_LABELCLEAR
:
340 return (gettext("\tlabelclear [-f] <vdev>\n"));
342 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
343 "[-T d|u] [pool] ... \n"
344 "\t [interval [count]]\n"));
346 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
348 return (gettext("\tonline [-e] <pool> <device> ...\n"));
350 return (gettext("\treplace [-f] [-o property=value] "
351 "<pool> <device> [new-device]\n"));
353 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
355 return (gettext("\treopen [-n] <pool>\n"));
357 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
359 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
360 "[-T d|u] [pool] ... \n"
361 "\t [interval [count]]\n"));
363 return (gettext("\tupgrade\n"
365 "\tupgrade [-V version] <-a | pool ...>\n"));
367 return (gettext("\tevents [-vHf [pool] | -c]\n"));
369 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
370 "<\"all\" | property[,...]> <pool> ...\n"));
372 return (gettext("\tset <property=value> <pool> \n"));
374 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
375 "\t [-o property=value] <pool> <newpool> "
376 "[<device> ...]\n"));
378 return (gettext("\treguid <pool>\n"));
380 return (gettext("\tsync [pool] ...\n"));
389 * Callback routine that will print out a pool property value.
392 print_prop_cb(int prop
, void *cb
)
396 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
398 if (zpool_prop_readonly(prop
))
399 (void) fprintf(fp
, " NO ");
401 (void) fprintf(fp
, " YES ");
403 if (zpool_prop_values(prop
) == NULL
)
404 (void) fprintf(fp
, "-\n");
406 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
412 * Display usage message. If we're inside a command, display only the usage for
413 * that command. Otherwise, iterate over the entire command table and display
414 * a complete usage message.
417 usage(boolean_t requested
)
419 FILE *fp
= requested
? stdout
: stderr
;
421 if (current_command
== NULL
) {
424 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
426 gettext("where 'command' is one of the following:\n\n"));
428 for (i
= 0; i
< NCOMMAND
; i
++) {
429 if (command_table
[i
].name
== NULL
)
430 (void) fprintf(fp
, "\n");
432 (void) fprintf(fp
, "%s",
433 get_usage(command_table
[i
].usage
));
436 (void) fprintf(fp
, gettext("usage:\n"));
437 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
440 if (current_command
!= NULL
&&
441 ((strcmp(current_command
->name
, "set") == 0) ||
442 (strcmp(current_command
->name
, "get") == 0) ||
443 (strcmp(current_command
->name
, "list") == 0))) {
446 gettext("\nthe following properties are supported:\n"));
448 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
449 "PROPERTY", "EDIT", "VALUES");
451 /* Iterate over all properties */
452 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
455 (void) fprintf(fp
, "\t%-15s ", "feature@...");
456 (void) fprintf(fp
, "YES disabled | enabled | active\n");
458 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
459 "appended with a feature name.\nSee zpool-features(5).\n"));
463 * See comments at end of main().
465 if (getenv("ZFS_ABORT") != NULL
) {
466 (void) printf("dumping core by request\n");
470 exit(requested
? 0 : 2);
474 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
475 boolean_t print_logs
, int name_flags
)
482 (void) printf("\t%*s%s\n", indent
, "", name
);
484 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
485 &child
, &children
) != 0)
488 for (c
= 0; c
< children
; c
++) {
489 uint64_t is_log
= B_FALSE
;
491 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
493 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
496 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
497 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
498 B_FALSE
, name_flags
);
504 prop_list_contains_feature(nvlist_t
*proplist
)
507 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
508 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
509 if (zpool_prop_feature(nvpair_name(nvp
)))
516 * Add a property pair (name, string-value) into a property nvlist.
519 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
522 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
528 if (*props
== NULL
&&
529 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
530 (void) fprintf(stderr
,
531 gettext("internal error: out of memory\n"));
538 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
540 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
541 !zpool_prop_feature(propname
)) {
542 (void) fprintf(stderr
, gettext("property '%s' is "
543 "not a valid pool property\n"), propname
);
548 * feature@ properties and version should not be specified
551 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
552 nvlist_exists(proplist
, vname
)) ||
553 (prop
== ZPOOL_PROP_VERSION
&&
554 prop_list_contains_feature(proplist
))) {
555 (void) fprintf(stderr
, gettext("'feature@' and "
556 "'version' properties cannot be specified "
562 if (zpool_prop_feature(propname
))
565 normnm
= zpool_prop_to_name(prop
);
567 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
568 normnm
= zfs_prop_to_name(fprop
);
574 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
575 prop
!= ZPOOL_PROP_CACHEFILE
) {
576 (void) fprintf(stderr
, gettext("property '%s' "
577 "specified multiple times\n"), propname
);
581 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
582 (void) fprintf(stderr
, gettext("internal "
583 "error: out of memory\n"));
591 * Set a default property pair (name, string-value) in a property nvlist
594 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
599 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
602 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
606 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
608 * -f Force addition of devices, even if they appear in use
609 * -g Display guid for individual vdev name.
610 * -L Follow links when resolving vdev path name.
611 * -n Do not add the devices, but display the resulting layout if
612 * they were to be added.
613 * -o Set property=value.
614 * -P Display full path for vdev name.
616 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
617 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
621 zpool_do_add(int argc
, char **argv
)
623 boolean_t force
= B_FALSE
;
624 boolean_t dryrun
= B_FALSE
;
632 nvlist_t
*props
= NULL
;
636 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
642 name_flags
|= VDEV_NAME_GUID
;
645 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
651 if ((propval
= strchr(optarg
, '=')) == NULL
) {
652 (void) fprintf(stderr
, gettext("missing "
653 "'=' for -o option\n"));
659 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
660 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
664 name_flags
|= VDEV_NAME_PATH
;
667 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
676 /* get pool name and check number of arguments */
678 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
682 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
691 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
694 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
695 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
701 /* unless manually specified use "ashift" pool property (if set) */
702 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
705 char strval
[ZPOOL_MAXPROPLEN
];
707 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
708 if (src
!= ZPROP_SRC_DEFAULT
) {
709 (void) sprintf(strval
, "%" PRId32
, intval
);
710 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
711 &props
, B_TRUE
) == 0);
715 /* pass off to get_vdev_spec for processing */
716 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
718 if (nvroot
== NULL
) {
724 nvlist_t
*poolnvroot
;
726 uint_t l2children
, c
;
728 boolean_t hadcache
= B_FALSE
;
730 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
733 (void) printf(gettext("would update '%s' to the following "
734 "configuration:\n"), zpool_get_name(zhp
));
736 /* print original main pool and new tree */
737 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
739 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
741 /* Do the same for the logs */
742 if (num_logs(poolnvroot
) > 0) {
743 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
745 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
747 } else if (num_logs(nvroot
) > 0) {
748 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
752 /* Do the same for the caches */
753 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
754 &l2child
, &l2children
) == 0 && l2children
) {
756 (void) printf(gettext("\tcache\n"));
757 for (c
= 0; c
< l2children
; c
++) {
758 vname
= zpool_vdev_name(g_zfs
, NULL
,
759 l2child
[c
], name_flags
);
760 (void) printf("\t %s\n", vname
);
764 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
765 &l2child
, &l2children
) == 0 && l2children
) {
767 (void) printf(gettext("\tcache\n"));
768 for (c
= 0; c
< l2children
; c
++) {
769 vname
= zpool_vdev_name(g_zfs
, NULL
,
770 l2child
[c
], name_flags
);
771 (void) printf("\t %s\n", vname
);
778 ret
= (zpool_add(zhp
, nvroot
) != 0);
789 * zpool remove <pool> <vdev> ...
791 * Removes the given vdev from the pool.
794 zpool_do_remove(int argc
, char **argv
)
798 zpool_handle_t
*zhp
= NULL
;
799 boolean_t stop
= B_FALSE
;
801 boolean_t noop
= B_FALSE
;
802 boolean_t parsable
= B_FALSE
;
805 while ((c
= getopt(argc
, argv
, "nps")) != -1) {
817 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
826 /* get pool name and check number of arguments */
828 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
834 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
838 (void) fprintf(stderr
, gettext("stop request ignored\n"));
844 (void) fprintf(stderr
, gettext("too many arguments\n"));
847 if (zpool_vdev_remove_cancel(zhp
) != 0)
851 (void) fprintf(stderr
, gettext("missing device\n"));
855 for (i
= 1; i
< argc
; i
++) {
859 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
865 (void) printf("%s %llu\n",
866 argv
[i
], (unsigned long long)size
);
869 zfs_nicenum(size
, valstr
,
871 (void) printf("Memory that will be "
872 "used after removing %s: %s\n",
876 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
887 * zpool labelclear [-f] <vdev>
889 * -f Force clearing the label for the vdevs which are members of
890 * the exported or foreign pools.
892 * Verifies that the vdev is not active and zeros out the label information
896 zpool_do_labelclear(int argc
, char **argv
)
898 char vdev
[MAXPATHLEN
];
901 int c
, fd
= -1, ret
= 0;
904 boolean_t inuse
= B_FALSE
;
905 boolean_t force
= B_FALSE
;
908 while ((c
= getopt(argc
, argv
, "f")) != -1) {
914 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
925 (void) fprintf(stderr
, gettext("missing vdev name\n"));
929 (void) fprintf(stderr
, gettext("too many arguments\n"));
934 * Check if we were given absolute path and use it as is.
935 * Otherwise if the provided vdev name doesn't point to a file,
936 * try prepending expected disk paths and partition numbers.
938 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
939 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
942 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
943 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
944 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
948 if (error
|| (stat(vdev
, &st
) != 0)) {
949 (void) fprintf(stderr
, gettext(
950 "failed to find device %s, try specifying absolute "
951 "path instead\n"), argv
[0]);
956 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
957 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
958 vdev
, strerror(errno
));
962 if (ioctl(fd
, BLKFLSBUF
) != 0)
963 (void) fprintf(stderr
, gettext("failed to invalidate "
964 "cache for %s: %s\n"), vdev
, strerror(errno
));
966 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
967 (void) fprintf(stderr
,
968 gettext("failed to check state for %s\n"), vdev
);
974 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
976 (void) fprintf(stderr
,
977 gettext("failed to check state for %s\n"), vdev
);
987 case POOL_STATE_ACTIVE
:
988 case POOL_STATE_SPARE
:
989 case POOL_STATE_L2CACHE
:
990 (void) fprintf(stderr
, gettext(
991 "%s is a member (%s) of pool \"%s\"\n"),
992 vdev
, zpool_pool_state_to_name(state
), name
);
996 case POOL_STATE_EXPORTED
:
999 (void) fprintf(stderr
, gettext(
1000 "use '-f' to override the following error:\n"
1001 "%s is a member of exported pool \"%s\"\n"),
1006 case POOL_STATE_POTENTIALLY_ACTIVE
:
1009 (void) fprintf(stderr
, gettext(
1010 "use '-f' to override the following error:\n"
1011 "%s is a member of potentially active pool \"%s\"\n"),
1016 case POOL_STATE_DESTROYED
:
1017 /* inuse should never be set for a destroyed pool */
1023 ret
= zpool_clear_label(fd
);
1025 (void) fprintf(stderr
,
1026 gettext("failed to clear label for %s\n"), vdev
);
1037 * zpool create [-fnd] [-o property=value] ...
1038 * [-O file-system-property=value] ...
1039 * [-R root] [-m mountpoint] <pool> <dev> ...
1041 * -f Force creation, even if devices appear in use
1042 * -n Do not create the pool, but display the resulting layout if it
1043 * were to be created.
1044 * -R Create a pool under an alternate root
1045 * -m Set default mountpoint for the root dataset. By default it's
1047 * -o Set property=value.
1048 * -o Set feature@feature=enabled|disabled.
1049 * -d Don't automatically enable all supported pool features
1050 * (individual features can be enabled with -o).
1051 * -O Set fsproperty=value in the pool's root file system
1053 * Creates the named pool according to the given vdev specification. The
1054 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1055 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1056 * (if '-n' was specified), or pass it to libzfs to do the creation.
1059 zpool_do_create(int argc
, char **argv
)
1061 boolean_t force
= B_FALSE
;
1062 boolean_t dryrun
= B_FALSE
;
1063 boolean_t enable_all_pool_feat
= B_TRUE
;
1065 nvlist_t
*nvroot
= NULL
;
1069 char *altroot
= NULL
;
1070 char *mountpoint
= NULL
;
1071 nvlist_t
*fsprops
= NULL
;
1072 nvlist_t
*props
= NULL
;
1076 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1085 enable_all_pool_feat
= B_FALSE
;
1089 if (add_prop_list(zpool_prop_to_name(
1090 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1092 if (add_prop_list_default(zpool_prop_to_name(
1093 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1097 /* Equivalent to -O mountpoint=optarg */
1098 mountpoint
= optarg
;
1101 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1102 (void) fprintf(stderr
, gettext("missing "
1103 "'=' for -o option\n"));
1109 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1113 * If the user is creating a pool that doesn't support
1114 * feature flags, don't enable any features.
1116 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1120 ver
= strtoull(propval
, &end
, 10);
1122 ver
< SPA_VERSION_FEATURES
) {
1123 enable_all_pool_feat
= B_FALSE
;
1126 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1130 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1131 (void) fprintf(stderr
, gettext("missing "
1132 "'=' for -O option\n"));
1139 * Mountpoints are checked and then added later.
1140 * Uniquely among properties, they can be specified
1141 * more than once, to avoid conflict with -m.
1143 if (0 == strcmp(optarg
,
1144 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1145 mountpoint
= propval
;
1146 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1153 * Sanity check temporary pool name.
1155 if (strchr(optarg
, '/') != NULL
) {
1156 (void) fprintf(stderr
, gettext("cannot create "
1157 "'%s': invalid character '/' in temporary "
1159 (void) fprintf(stderr
, gettext("use 'zfs "
1160 "create' to create a dataset\n"));
1164 if (add_prop_list(zpool_prop_to_name(
1165 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1167 if (add_prop_list_default(zpool_prop_to_name(
1168 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1173 (void) fprintf(stderr
, gettext("missing argument for "
1174 "'%c' option\n"), optopt
);
1177 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1186 /* get pool name and check number of arguments */
1188 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1192 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1199 * As a special case, check for use of '/' in the name, and direct the
1200 * user to use 'zfs create' instead.
1202 if (strchr(poolname
, '/') != NULL
) {
1203 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1204 "character '/' in pool name\n"), poolname
);
1205 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1206 "create a dataset\n"));
1210 /* pass off to get_vdev_spec for bulk processing */
1211 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1212 argc
- 1, argv
+ 1);
1216 /* make_root_vdev() allows 0 toplevel children if there are spares */
1217 if (!zfs_allocatable_devs(nvroot
)) {
1218 (void) fprintf(stderr
, gettext("invalid vdev "
1219 "specification: at least one toplevel vdev must be "
1224 if (altroot
!= NULL
&& altroot
[0] != '/') {
1225 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1226 "must be an absolute path\n"), altroot
);
1231 * Check the validity of the mountpoint and direct the user to use the
1232 * '-m' mountpoint option if it looks like its in use.
1234 if (mountpoint
== NULL
||
1235 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1236 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1237 char buf
[MAXPATHLEN
];
1240 if (mountpoint
&& mountpoint
[0] != '/') {
1241 (void) fprintf(stderr
, gettext("invalid mountpoint "
1242 "'%s': must be an absolute path, 'legacy', or "
1243 "'none'\n"), mountpoint
);
1247 if (mountpoint
== NULL
) {
1248 if (altroot
!= NULL
)
1249 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1252 (void) snprintf(buf
, sizeof (buf
), "/%s",
1255 if (altroot
!= NULL
)
1256 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1257 altroot
, mountpoint
);
1259 (void) snprintf(buf
, sizeof (buf
), "%s",
1263 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1264 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1265 "%s\n"), buf
, strerror(errno
));
1266 (void) fprintf(stderr
, gettext("use '-m' "
1267 "option to provide a different default\n"));
1272 while (count
< 3 && readdir(dirp
) != NULL
)
1274 (void) closedir(dirp
);
1277 (void) fprintf(stderr
, gettext("mountpoint "
1278 "'%s' exists and is not empty\n"), buf
);
1279 (void) fprintf(stderr
, gettext("use '-m' "
1280 "option to provide a "
1281 "different default\n"));
1288 * Now that the mountpoint's validity has been checked, ensure that
1289 * the property is set appropriately prior to creating the pool.
1291 if (mountpoint
!= NULL
) {
1292 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1293 mountpoint
, &fsprops
, B_FALSE
);
1301 * For a dry run invocation, print out a basic message and run
1302 * through all the vdevs in the list and print out in an
1303 * appropriate hierarchy.
1305 (void) printf(gettext("would create '%s' with the "
1306 "following layout:\n\n"), poolname
);
1308 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1309 if (num_logs(nvroot
) > 0)
1310 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1315 * Hand off to libzfs.
1318 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1319 char propname
[MAXPATHLEN
];
1321 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1323 (void) snprintf(propname
, sizeof (propname
),
1324 "feature@%s", feat
->fi_uname
);
1327 * Only features contained in props will be enabled:
1328 * remove from the nvlist every ZFS_FEATURE_DISABLED
1329 * value and add every missing ZFS_FEATURE_ENABLED if
1330 * enable_all_pool_feat is set.
1332 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1333 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1334 (void) nvlist_remove_all(props
,
1336 } else if (enable_all_pool_feat
) {
1337 ret
= add_prop_list(propname
,
1338 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1345 if (zpool_create(g_zfs
, poolname
,
1346 nvroot
, props
, fsprops
) == 0) {
1347 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1348 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1350 if (zfs_mount(pool
, NULL
, 0) == 0)
1351 ret
= zfs_shareall(pool
);
1354 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1355 (void) fprintf(stderr
, gettext("pool name may have "
1361 nvlist_free(nvroot
);
1362 nvlist_free(fsprops
);
1366 nvlist_free(fsprops
);
1373 * zpool destroy <pool>
1375 * -f Forcefully unmount any datasets
1377 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1380 zpool_do_destroy(int argc
, char **argv
)
1382 boolean_t force
= B_FALSE
;
1385 zpool_handle_t
*zhp
;
1389 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1395 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1404 /* check arguments */
1406 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1410 (void) fprintf(stderr
, gettext("too many arguments\n"));
1416 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1418 * As a special case, check for use of '/' in the name, and
1419 * direct the user to use 'zfs destroy' instead.
1421 if (strchr(pool
, '/') != NULL
)
1422 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1423 "destroy a dataset\n"));
1427 if (zpool_disable_datasets(zhp
, force
) != 0) {
1428 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1429 "could not unmount datasets\n"), zpool_get_name(zhp
));
1434 /* The history must be logged as part of the export */
1435 log_history
= B_FALSE
;
1437 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1444 typedef struct export_cbdata
{
1446 boolean_t hardforce
;
1453 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1455 export_cbdata_t
*cb
= data
;
1457 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1460 /* The history must be logged as part of the export */
1461 log_history
= B_FALSE
;
1463 if (cb
->hardforce
) {
1464 if (zpool_export_force(zhp
, history_str
) != 0)
1466 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1474 * zpool export [-f] <pool> ...
1476 * -a Export all pools
1477 * -f Forcefully unmount datasets
1479 * Export the given pools. By default, the command will attempt to cleanly
1480 * unmount any active datasets within the pool. If the '-f' flag is specified,
1481 * then the datasets will be forcefully unmounted.
1484 zpool_do_export(int argc
, char **argv
)
1487 boolean_t do_all
= B_FALSE
;
1488 boolean_t force
= B_FALSE
;
1489 boolean_t hardforce
= B_FALSE
;
1493 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1505 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1512 cb
.hardforce
= hardforce
;
1518 (void) fprintf(stderr
, gettext("too many arguments\n"));
1522 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1523 zpool_export_one
, &cb
));
1526 /* check arguments */
1528 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1532 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1538 * Given a vdev configuration, determine the maximum width needed for the device
1542 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1550 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1551 if (strlen(name
) + depth
> max
)
1552 max
= strlen(name
) + depth
;
1556 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1557 &child
, &children
) == 0) {
1558 for (c
= 0; c
< children
; c
++)
1559 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1560 max
, name_flags
)) > max
)
1564 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1565 &child
, &children
) == 0) {
1566 for (c
= 0; c
< children
; c
++)
1567 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1568 max
, name_flags
)) > max
)
1572 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1573 &child
, &children
) == 0) {
1574 for (c
= 0; c
< children
; c
++)
1575 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1576 max
, name_flags
)) > max
)
1583 typedef struct spare_cbdata
{
1585 zpool_handle_t
*cb_zhp
;
1589 find_vdev(nvlist_t
*nv
, uint64_t search
)
1595 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1599 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1600 &child
, &children
) == 0) {
1601 for (c
= 0; c
< children
; c
++)
1602 if (find_vdev(child
[c
], search
))
1610 find_spare(zpool_handle_t
*zhp
, void *data
)
1612 spare_cbdata_t
*cbp
= data
;
1613 nvlist_t
*config
, *nvroot
;
1615 config
= zpool_get_config(zhp
, NULL
);
1616 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1619 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1628 typedef struct status_cbdata
{
1632 boolean_t cb_allpools
;
1633 boolean_t cb_verbose
;
1634 boolean_t cb_explain
;
1636 boolean_t cb_dedup_stats
;
1637 boolean_t cb_print_status
;
1638 vdev_cmd_data_list_t
*vcdl
;
1641 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1643 is_blank_str(char *str
)
1645 while (str
!= NULL
&& *str
!= '\0') {
1653 /* Print command output lines for specific vdev in a specific pool */
1655 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1657 vdev_cmd_data_t
*data
;
1661 for (i
= 0; i
< vcdl
->count
; i
++) {
1662 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1663 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1664 /* Not the vdev we're looking for */
1668 data
= &vcdl
->data
[i
];
1669 /* Print out all the output values for this vdev */
1670 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1672 /* Does this vdev have values for this column? */
1673 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1674 if (strcmp(data
->cols
[k
],
1675 vcdl
->uniq_cols
[j
]) == 0) {
1676 /* yes it does, record the value */
1677 val
= data
->lines
[k
];
1682 * Mark empty values with dashes to make output
1685 if (is_blank_str(val
))
1688 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1689 if (j
< vcdl
->uniq_cols_cnt
- 1)
1693 /* Print out any values that aren't in a column at the end */
1694 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1695 /* Did we have any columns? If so print a spacer. */
1696 if (vcdl
->uniq_cols_cnt
> 0)
1699 val
= data
->lines
[j
];
1700 printf("%s", val
? val
: "");
1707 * Print out configuration state as requested by status_callback.
1710 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1711 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1715 pool_scan_stat_t
*ps
= NULL
;
1717 char rbuf
[6], wbuf
[6], cbuf
[6];
1719 uint64_t notpresent
;
1720 spare_cbdata_t spare_cb
;
1725 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1726 &child
, &children
) != 0)
1729 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1730 (uint64_t **)&vs
, &c
) == 0);
1732 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1734 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
1737 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1740 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1743 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1745 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1749 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1753 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1754 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1755 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1756 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1759 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1760 ¬present
) == 0) {
1761 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1762 (void) printf(" was %s", path
);
1763 } else if (vs
->vs_aux
!= 0) {
1766 switch (vs
->vs_aux
) {
1767 case VDEV_AUX_OPEN_FAILED
:
1768 (void) printf(gettext("cannot open"));
1771 case VDEV_AUX_BAD_GUID_SUM
:
1772 (void) printf(gettext("missing device"));
1775 case VDEV_AUX_NO_REPLICAS
:
1776 (void) printf(gettext("insufficient replicas"));
1779 case VDEV_AUX_VERSION_NEWER
:
1780 (void) printf(gettext("newer version"));
1783 case VDEV_AUX_UNSUP_FEAT
:
1784 (void) printf(gettext("unsupported feature(s)"));
1787 case VDEV_AUX_SPARED
:
1788 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1789 &spare_cb
.cb_guid
) == 0);
1790 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1791 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1792 zpool_get_name(zhp
)) == 0)
1793 (void) printf(gettext("currently in "
1796 (void) printf(gettext("in use by "
1798 zpool_get_name(spare_cb
.cb_zhp
));
1799 zpool_close(spare_cb
.cb_zhp
);
1801 (void) printf(gettext("currently in use"));
1805 case VDEV_AUX_ERR_EXCEEDED
:
1806 (void) printf(gettext("too many errors"));
1809 case VDEV_AUX_IO_FAILURE
:
1810 (void) printf(gettext("experienced I/O failures"));
1813 case VDEV_AUX_BAD_LOG
:
1814 (void) printf(gettext("bad intent log"));
1817 case VDEV_AUX_EXTERNAL
:
1818 (void) printf(gettext("external device fault"));
1821 case VDEV_AUX_SPLIT_POOL
:
1822 (void) printf(gettext("split into new pool"));
1825 case VDEV_AUX_ACTIVE
:
1826 (void) printf(gettext("currently in use"));
1829 case VDEV_AUX_CHILDREN_OFFLINE
:
1830 (void) printf(gettext("all children offline"));
1834 (void) printf(gettext("corrupted data"));
1839 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1840 (uint64_t **)&ps
, &c
);
1842 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&&
1843 vs
->vs_scan_processed
!= 0 && children
== 0) {
1844 (void) printf(gettext(" (%s)"),
1845 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1846 "resilvering" : "repairing");
1849 if (cb
->vcdl
!= NULL
) {
1850 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1852 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1856 (void) printf("\n");
1858 for (c
= 0; c
< children
; c
++) {
1859 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1861 /* Don't print logs or holes here */
1862 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1864 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1866 if (islog
|| ishole
)
1868 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1869 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1870 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1877 * Print the configuration of an exported pool. Iterate over all vdevs in the
1878 * pool, printing out the name and status for each one.
1881 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1889 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1890 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1891 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1894 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1895 (uint64_t **)&vs
, &c
) == 0);
1897 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1898 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1900 if (vs
->vs_aux
!= 0) {
1903 switch (vs
->vs_aux
) {
1904 case VDEV_AUX_OPEN_FAILED
:
1905 (void) printf(gettext("cannot open"));
1908 case VDEV_AUX_BAD_GUID_SUM
:
1909 (void) printf(gettext("missing device"));
1912 case VDEV_AUX_NO_REPLICAS
:
1913 (void) printf(gettext("insufficient replicas"));
1916 case VDEV_AUX_VERSION_NEWER
:
1917 (void) printf(gettext("newer version"));
1920 case VDEV_AUX_UNSUP_FEAT
:
1921 (void) printf(gettext("unsupported feature(s)"));
1924 case VDEV_AUX_ERR_EXCEEDED
:
1925 (void) printf(gettext("too many errors"));
1928 case VDEV_AUX_ACTIVE
:
1929 (void) printf(gettext("currently in use"));
1932 case VDEV_AUX_CHILDREN_OFFLINE
:
1933 (void) printf(gettext("all children offline"));
1937 (void) printf(gettext("corrupted data"));
1941 (void) printf("\n");
1943 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1944 &child
, &children
) != 0)
1947 for (c
= 0; c
< children
; c
++) {
1948 uint64_t is_log
= B_FALSE
;
1950 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1955 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1956 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1957 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1961 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1962 &child
, &children
) == 0) {
1963 (void) printf(gettext("\tcache\n"));
1964 for (c
= 0; c
< children
; c
++) {
1965 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1967 (void) printf("\t %s\n", vname
);
1972 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1973 &child
, &children
) == 0) {
1974 (void) printf(gettext("\tspares\n"));
1975 for (c
= 0; c
< children
; c
++) {
1976 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1978 (void) printf("\t %s\n", vname
);
1986 * Logs are recorded as top level vdevs in the main pool child array
1987 * but with "is_log" set to 1. We use either print_status_config() or
1988 * print_import_config() to print the top level logs then any log
1989 * children (eg mirrored slogs) are printed recursively - which
1990 * works because only the top level vdev is marked "is_log"
1993 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1998 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2002 (void) printf(gettext("\tlogs\n"));
2004 for (c
= 0; c
< children
; c
++) {
2005 uint64_t is_log
= B_FALSE
;
2008 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2012 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2013 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2014 if (cb
->cb_print_status
)
2015 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2018 print_import_config(cb
, name
, child
[c
], 2);
2024 * Display the status for the given pool.
2027 show_import(nvlist_t
*config
)
2029 uint64_t pool_state
;
2033 uint64_t hostid
= 0;
2035 char *hostname
= "unknown";
2036 nvlist_t
*nvroot
, *nvinfo
;
2037 zpool_status_t reason
;
2038 zpool_errata_t errata
;
2042 status_cbdata_t cb
= { 0 };
2044 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2046 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2048 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2050 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2053 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2054 (uint64_t **)&vs
, &vsc
) == 0);
2055 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2057 reason
= zpool_import_status(config
, &msgid
, &errata
);
2059 (void) printf(gettext(" pool: %s\n"), name
);
2060 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2061 (void) printf(gettext(" state: %s"), health
);
2062 if (pool_state
== POOL_STATE_DESTROYED
)
2063 (void) printf(gettext(" (DESTROYED)"));
2064 (void) printf("\n");
2067 case ZPOOL_STATUS_MISSING_DEV_R
:
2068 case ZPOOL_STATUS_MISSING_DEV_NR
:
2069 case ZPOOL_STATUS_BAD_GUID_SUM
:
2070 (void) printf(gettext(" status: One or more devices are "
2071 "missing from the system.\n"));
2074 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2075 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2076 (void) printf(gettext(" status: One or more devices contains "
2077 "corrupted data.\n"));
2080 case ZPOOL_STATUS_CORRUPT_DATA
:
2082 gettext(" status: The pool data is corrupted.\n"));
2085 case ZPOOL_STATUS_OFFLINE_DEV
:
2086 (void) printf(gettext(" status: One or more devices "
2087 "are offlined.\n"));
2090 case ZPOOL_STATUS_CORRUPT_POOL
:
2091 (void) printf(gettext(" status: The pool metadata is "
2095 case ZPOOL_STATUS_VERSION_OLDER
:
2096 (void) printf(gettext(" status: The pool is formatted using a "
2097 "legacy on-disk version.\n"));
2100 case ZPOOL_STATUS_VERSION_NEWER
:
2101 (void) printf(gettext(" status: The pool is formatted using an "
2102 "incompatible version.\n"));
2105 case ZPOOL_STATUS_FEAT_DISABLED
:
2106 (void) printf(gettext(" status: Some supported features are "
2107 "not enabled on the pool.\n"));
2110 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2111 (void) printf(gettext("status: The pool uses the following "
2112 "feature(s) not supported on this system:\n"));
2113 zpool_print_unsup_feat(config
);
2116 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2117 (void) printf(gettext("status: The pool can only be accessed "
2118 "in read-only mode on this system. It\n\tcannot be "
2119 "accessed in read-write mode because it uses the "
2120 "following\n\tfeature(s) not supported on this system:\n"));
2121 zpool_print_unsup_feat(config
);
2124 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2125 (void) printf(gettext(" status: The pool is currently "
2126 "imported by another system.\n"));
2129 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2130 (void) printf(gettext(" status: The pool has the "
2131 "multihost property on. It cannot\n\tbe safely imported "
2132 "when the system hostid is not set.\n"));
2135 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2136 (void) printf(gettext(" status: The pool was last accessed by "
2137 "another system.\n"));
2140 case ZPOOL_STATUS_FAULTED_DEV_R
:
2141 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2142 (void) printf(gettext(" status: One or more devices are "
2146 case ZPOOL_STATUS_BAD_LOG
:
2147 (void) printf(gettext(" status: An intent log record cannot be "
2151 case ZPOOL_STATUS_RESILVERING
:
2152 (void) printf(gettext(" status: One or more devices were being "
2156 case ZPOOL_STATUS_ERRATA
:
2157 (void) printf(gettext(" status: Errata #%d detected.\n"),
2163 * No other status can be seen when importing pools.
2165 assert(reason
== ZPOOL_STATUS_OK
);
2169 * Print out an action according to the overall state of the pool.
2171 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2172 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2173 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2174 (void) printf(gettext(" action: The pool can be "
2175 "imported using its name or numeric identifier, "
2176 "though\n\tsome features will not be available "
2177 "without an explicit 'zpool upgrade'.\n"));
2178 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2179 (void) printf(gettext(" action: The pool can be "
2180 "imported using its name or numeric "
2181 "identifier and\n\tthe '-f' flag.\n"));
2182 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2184 case ZPOOL_ERRATA_NONE
:
2187 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2188 (void) printf(gettext(" action: The pool can "
2189 "be imported using its name or numeric "
2190 "identifier,\n\thowever there is a compat"
2191 "ibility issue which should be corrected"
2192 "\n\tby running 'zpool scrub'\n"));
2195 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2196 (void) printf(gettext(" action: The pool can"
2197 "not be imported with this version of ZFS "
2198 "due to\n\tan active asynchronous destroy. "
2199 "Revert to an earlier version\n\tand "
2200 "allow the destroy to complete before "
2204 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2205 (void) printf(gettext(" action: Existing "
2206 "encrypted datasets contain an on-disk "
2207 "incompatibility, which\n\tneeds to be "
2208 "corrected. Backup these datasets to new "
2209 "encrypted datasets\n\tand destroy the "
2215 * All errata must contain an action message.
2220 (void) printf(gettext(" action: The pool can be "
2221 "imported using its name or numeric "
2224 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2225 (void) printf(gettext(" action: The pool can be imported "
2226 "despite missing or damaged devices. The\n\tfault "
2227 "tolerance of the pool may be compromised if imported.\n"));
2230 case ZPOOL_STATUS_VERSION_NEWER
:
2231 (void) printf(gettext(" action: The pool cannot be "
2232 "imported. Access the pool on a system running "
2233 "newer\n\tsoftware, or recreate the pool from "
2236 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2237 (void) printf(gettext("action: The pool cannot be "
2238 "imported. Access the pool on a system that "
2239 "supports\n\tthe required feature(s), or recreate "
2240 "the pool from backup.\n"));
2242 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2243 (void) printf(gettext("action: The pool cannot be "
2244 "imported in read-write mode. Import the pool "
2246 "\t\"-o readonly=on\", access the pool on a system "
2247 "that supports the\n\trequired feature(s), or "
2248 "recreate the pool from backup.\n"));
2250 case ZPOOL_STATUS_MISSING_DEV_R
:
2251 case ZPOOL_STATUS_MISSING_DEV_NR
:
2252 case ZPOOL_STATUS_BAD_GUID_SUM
:
2253 (void) printf(gettext(" action: The pool cannot be "
2254 "imported. Attach the missing\n\tdevices and try "
2257 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2258 VERIFY0(nvlist_lookup_nvlist(config
,
2259 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2261 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2262 hostname
= fnvlist_lookup_string(nvinfo
,
2263 ZPOOL_CONFIG_MMP_HOSTNAME
);
2265 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2266 hostid
= fnvlist_lookup_uint64(nvinfo
,
2267 ZPOOL_CONFIG_MMP_HOSTID
);
2269 (void) printf(gettext(" action: The pool must be "
2270 "exported from %s (hostid=%lx)\n\tbefore it "
2271 "can be safely imported.\n"), hostname
,
2272 (unsigned long) hostid
);
2274 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2275 (void) printf(gettext(" action: Set a unique system "
2276 "hostid with the zgenhostid(8) command.\n"));
2279 (void) printf(gettext(" action: The pool cannot be "
2280 "imported due to damaged devices or data.\n"));
2284 /* Print the comment attached to the pool. */
2285 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2286 (void) printf(gettext("comment: %s\n"), comment
);
2289 * If the state is "closed" or "can't open", and the aux state
2290 * is "corrupt data":
2292 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2293 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2294 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2295 if (pool_state
== POOL_STATE_DESTROYED
)
2296 (void) printf(gettext("\tThe pool was destroyed, "
2297 "but can be imported using the '-Df' flags.\n"));
2298 else if (pool_state
!= POOL_STATE_EXPORTED
)
2299 (void) printf(gettext("\tThe pool may be active on "
2300 "another system, but can be imported using\n\t"
2301 "the '-f' flag.\n"));
2305 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2308 (void) printf(gettext(" config:\n\n"));
2310 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2312 if (cb
.cb_namewidth
< 10)
2313 cb
.cb_namewidth
= 10;
2315 print_import_config(&cb
, name
, nvroot
, 0);
2316 if (num_logs(nvroot
) > 0)
2317 print_logs(NULL
, &cb
, nvroot
);
2319 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2320 (void) printf(gettext("\n\tAdditional devices are known to "
2321 "be part of this pool, though their\n\texact "
2322 "configuration cannot be determined.\n"));
2327 zfs_force_import_required(nvlist_t
*config
)
2330 uint64_t hostid
= 0;
2333 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2334 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2336 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2339 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2340 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2341 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2342 ZPOOL_CONFIG_MMP_STATE
);
2344 if (mmp_state
!= MMP_STATE_INACTIVE
)
2352 * Perform the import for the given configuration. This passes the heavy
2353 * lifting off to zpool_import_props(), and then mounts the datasets contained
2357 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2358 nvlist_t
*props
, int flags
)
2361 zpool_handle_t
*zhp
;
2365 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2366 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2368 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2369 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2370 "is formatted using an unsupported ZFS version\n"), name
);
2372 } else if (zfs_force_import_required(config
) &&
2373 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2374 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2377 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2378 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2379 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2380 ZPOOL_CONFIG_MMP_STATE
);
2382 if (mmp_state
== MMP_STATE_ACTIVE
) {
2383 char *hostname
= "<unknown>";
2384 uint64_t hostid
= 0;
2386 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2387 hostname
= fnvlist_lookup_string(nvinfo
,
2388 ZPOOL_CONFIG_MMP_HOSTNAME
);
2390 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2391 hostid
= fnvlist_lookup_uint64(nvinfo
,
2392 ZPOOL_CONFIG_MMP_HOSTID
);
2394 (void) fprintf(stderr
, gettext("cannot import '%s': "
2395 "pool is imported on %s (hostid: "
2396 "0x%lx)\nExport the pool on the other system, "
2397 "then run 'zpool import'.\n"),
2398 name
, hostname
, (unsigned long) hostid
);
2399 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2400 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2401 "pool has the multihost property on and the\n"
2402 "system's hostid is not set. Set a unique hostid "
2403 "with the zgenhostid(8) command.\n"), name
);
2405 char *hostname
= "<unknown>";
2406 uint64_t timestamp
= 0;
2407 uint64_t hostid
= 0;
2409 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2410 hostname
= fnvlist_lookup_string(config
,
2411 ZPOOL_CONFIG_HOSTNAME
);
2413 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2414 timestamp
= fnvlist_lookup_uint64(config
,
2415 ZPOOL_CONFIG_TIMESTAMP
);
2417 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2418 hostid
= fnvlist_lookup_uint64(config
,
2419 ZPOOL_CONFIG_HOSTID
);
2421 (void) fprintf(stderr
, gettext("cannot import '%s': "
2422 "pool was previously in use from another system.\n"
2423 "Last accessed by %s (hostid=%lx) at %s"
2424 "The pool can be imported, use 'zpool import -f' "
2425 "to import the pool.\n"), name
, hostname
,
2426 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2432 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2435 if (newname
!= NULL
)
2436 name
= (char *)newname
;
2438 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2442 * Loading keys is best effort. We don't want to return immediately
2443 * if it fails but we do want to give the error to the caller.
2445 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2446 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2451 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2452 !(flags
& ZFS_IMPORT_ONLY
) &&
2453 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2463 * zpool checkpoint <pool>
2464 * checkpoint --discard <pool>
2466 * -d Discard the checkpoint from a checkpointed
2469 * Checkpoints the specified pool, by taking a "snapshot" of its
2470 * current state. A pool can only have one checkpoint at a time.
2473 zpool_do_checkpoint(int argc
, char **argv
)
2477 zpool_handle_t
*zhp
;
2480 struct option long_options
[] = {
2481 {"discard", no_argument
, NULL
, 'd'},
2486 while ((c
= getopt_long(argc
, argv
, ":d", long_options
, NULL
)) != -1) {
2492 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2502 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2507 (void) fprintf(stderr
, gettext("too many arguments\n"));
2513 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
2514 /* As a special case, check for use of '/' in the name */
2515 if (strchr(pool
, '/') != NULL
)
2516 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
2517 "doesn't work on datasets. To save the state "
2518 "of a dataset from a specific point in time "
2519 "please use 'zfs snapshot'\n"));
2524 err
= (zpool_discard_checkpoint(zhp
) != 0);
2526 err
= (zpool_checkpoint(zhp
) != 0);
2533 #define CHECKPOINT_OPT 1024
2536 * zpool import [-d dir] [-D]
2537 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2538 * [-d dir | -c cachefile] [-f] -a
2539 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2540 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2542 * -c Read pool information from a cachefile instead of searching
2545 * -d Scan in a specific directory, other than /dev/. More than
2546 * one directory can be specified using multiple '-d' options.
2548 * -D Scan for previously destroyed pools or import all or only
2549 * specified destroyed pools.
2551 * -R Temporarily import the pool, with all mountpoints relative to
2552 * the given root. The pool will remain exported when the machine
2555 * -V Import even in the presence of faulted vdevs. This is an
2556 * intentionally undocumented option for testing purposes, and
2557 * treats the pool configuration as complete, leaving any bad
2558 * vdevs in the FAULTED state. In other words, it does verbatim
2561 * -f Force import, even if it appears that the pool is active.
2563 * -F Attempt rewind if necessary.
2565 * -n See if rewind would work, but don't actually rewind.
2567 * -N Import the pool but don't mount datasets.
2569 * -T Specify a starting txg to use for import. This option is
2570 * intentionally undocumented option for testing purposes.
2572 * -a Import all pools found.
2574 * -l Load encryption keys while importing.
2576 * -o Set property=value and/or temporary mount options (without '=').
2578 * -s Scan using the default search path, the libblkid cache will
2581 * --rewind-to-checkpoint
2582 * Import the pool and revert back to the checkpoint.
2584 * The import command scans for pools to import, and import pools based on pool
2585 * name and GUID. The pool can also be renamed as part of the import process.
2588 zpool_do_import(int argc
, char **argv
)
2590 char **searchdirs
= NULL
;
2591 char *env
, *envdup
= NULL
;
2595 nvlist_t
*pools
= NULL
;
2596 boolean_t do_all
= B_FALSE
;
2597 boolean_t do_destroyed
= B_FALSE
;
2598 char *mntopts
= NULL
;
2601 uint64_t searchguid
= 0;
2602 char *searchname
= NULL
;
2604 nvlist_t
*found_config
;
2605 nvlist_t
*policy
= NULL
;
2606 nvlist_t
*props
= NULL
;
2608 int flags
= ZFS_IMPORT_NORMAL
;
2609 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2610 boolean_t dryrun
= B_FALSE
;
2611 boolean_t do_rewind
= B_FALSE
;
2612 boolean_t xtreme_rewind
= B_FALSE
;
2613 boolean_t do_scan
= B_FALSE
;
2614 uint64_t pool_state
, txg
= -1ULL;
2615 char *cachefile
= NULL
;
2616 importargs_t idata
= { 0 };
2619 struct option long_options
[] = {
2620 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
2625 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
2626 long_options
, NULL
)) != -1) {
2635 if (searchdirs
== NULL
) {
2636 searchdirs
= safe_malloc(sizeof (char *));
2638 char **tmp
= safe_malloc((nsearch
+ 1) *
2640 bcopy(searchdirs
, tmp
, nsearch
*
2645 searchdirs
[nsearch
++] = optarg
;
2648 do_destroyed
= B_TRUE
;
2651 flags
|= ZFS_IMPORT_ANY_HOST
;
2657 flags
|= ZFS_IMPORT_LOAD_KEYS
;
2660 flags
|= ZFS_IMPORT_MISSING_LOG
;
2666 flags
|= ZFS_IMPORT_ONLY
;
2669 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2672 if (add_prop_list(optarg
, propval
,
2680 if (add_prop_list(zpool_prop_to_name(
2681 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2683 if (add_prop_list_default(zpool_prop_to_name(
2684 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2691 flags
|= ZFS_IMPORT_TEMP_NAME
;
2692 if (add_prop_list_default(zpool_prop_to_name(
2693 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2699 txg
= strtoull(optarg
, &endptr
, 0);
2700 if (errno
!= 0 || *endptr
!= '\0') {
2701 (void) fprintf(stderr
,
2702 gettext("invalid txg value\n"));
2705 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2708 flags
|= ZFS_IMPORT_VERBATIM
;
2711 xtreme_rewind
= B_TRUE
;
2713 case CHECKPOINT_OPT
:
2714 flags
|= ZFS_IMPORT_CHECKPOINT
;
2717 (void) fprintf(stderr
, gettext("missing argument for "
2718 "'%c' option\n"), optopt
);
2722 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2731 if (cachefile
&& nsearch
!= 0) {
2732 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2736 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
2737 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
2741 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
2742 (void) fprintf(stderr
, gettext("-l is only meaningful during "
2747 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2748 (void) fprintf(stderr
,
2749 gettext("-n or -X only meaningful with -F\n"));
2753 rewind_policy
= ZPOOL_TRY_REWIND
;
2755 rewind_policy
= ZPOOL_DO_REWIND
;
2757 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2759 /* In the future, we can capture further policy and include it here */
2760 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2761 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
2762 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
2763 rewind_policy
) != 0)
2766 /* check argument count */
2769 (void) fprintf(stderr
, gettext("too many arguments\n"));
2774 (void) fprintf(stderr
, gettext("too many arguments\n"));
2780 * Check for the effective uid. We do this explicitly here because
2781 * otherwise any attempt to discover pools will silently fail.
2783 if (argc
== 0 && geteuid() != 0) {
2784 (void) fprintf(stderr
, gettext("cannot "
2785 "discover pools: permission denied\n"));
2786 if (searchdirs
!= NULL
)
2790 nvlist_free(policy
);
2795 * Depending on the arguments given, we do one of the following:
2797 * <none> Iterate through all pools and display information about
2800 * -a Iterate through all pools and try to import each one.
2802 * <id> Find the pool that corresponds to the given GUID/pool
2803 * name and import that one.
2805 * -D Above options applies only to destroyed pools.
2811 searchguid
= strtoull(argv
[0], &endptr
, 10);
2812 if (errno
!= 0 || *endptr
!= '\0') {
2813 searchname
= argv
[0];
2816 found_config
= NULL
;
2819 * User specified a name or guid. Ensure it's unique.
2821 idata
.unique
= B_TRUE
;
2825 * Check the environment for the preferred search path.
2827 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2830 envdup
= strdup(env
);
2832 dir
= strtok(envdup
, ":");
2833 while (dir
!= NULL
) {
2834 if (searchdirs
== NULL
) {
2835 searchdirs
= safe_malloc(sizeof (char *));
2837 char **tmp
= safe_malloc((nsearch
+ 1) *
2839 bcopy(searchdirs
, tmp
, nsearch
*
2844 searchdirs
[nsearch
++] = dir
;
2845 dir
= strtok(NULL
, ":");
2849 idata
.path
= searchdirs
;
2850 idata
.paths
= nsearch
;
2851 idata
.poolname
= searchname
;
2852 idata
.guid
= searchguid
;
2853 idata
.cachefile
= cachefile
;
2854 idata
.scan
= do_scan
;
2855 idata
.policy
= policy
;
2857 pools
= zpool_search_import(g_zfs
, &idata
);
2859 if (pools
!= NULL
&& idata
.exists
&&
2860 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2861 (void) fprintf(stderr
, gettext("cannot import '%s': "
2862 "a pool with that name already exists\n"),
2864 (void) fprintf(stderr
, gettext("use the form '%s "
2865 "<pool | id> <newpool>' to give it a new name\n"),
2868 } else if (pools
== NULL
&& idata
.exists
) {
2869 (void) fprintf(stderr
, gettext("cannot import '%s': "
2870 "a pool with that name is already created/imported,\n"),
2872 (void) fprintf(stderr
, gettext("and no additional pools "
2873 "with that name were found\n"));
2875 } else if (pools
== NULL
) {
2877 (void) fprintf(stderr
, gettext("cannot import '%s': "
2878 "no such pool available\n"), argv
[0]);
2884 if (searchdirs
!= NULL
)
2888 nvlist_free(policy
);
2895 * At this point we have a list of import candidate configs. Even if
2896 * we were searching by pool name or guid, we still need to
2897 * post-process the list to deal with pool state and possible
2903 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2905 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2907 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2909 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2911 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2914 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
2921 (void) printf("\n");
2924 err
|= do_import(config
, NULL
, mntopts
,
2927 show_import(config
);
2929 } else if (searchname
!= NULL
) {
2933 * We are searching for a pool based on name.
2935 verify(nvlist_lookup_string(config
,
2936 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2938 if (strcmp(name
, searchname
) == 0) {
2939 if (found_config
!= NULL
) {
2940 (void) fprintf(stderr
, gettext(
2941 "cannot import '%s': more than "
2942 "one matching pool\n"), searchname
);
2943 (void) fprintf(stderr
, gettext(
2944 "import by numeric ID instead\n"));
2947 found_config
= config
;
2953 * Search for a pool by guid.
2955 verify(nvlist_lookup_uint64(config
,
2956 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2958 if (guid
== searchguid
)
2959 found_config
= config
;
2964 * If we were searching for a specific pool, verify that we found a
2965 * pool, and then do the import.
2967 if (argc
!= 0 && err
== 0) {
2968 if (found_config
== NULL
) {
2969 (void) fprintf(stderr
, gettext("cannot import '%s': "
2970 "no such pool available\n"), argv
[0]);
2973 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2974 argv
[1], mntopts
, props
, flags
);
2979 * If we were just looking for pools, report an error if none were
2982 if (argc
== 0 && first
)
2983 (void) fprintf(stderr
,
2984 gettext("no pools available to import\n"));
2989 nvlist_free(policy
);
2990 if (searchdirs
!= NULL
)
2995 return (err
? 1 : 0);
2999 * zpool sync [-f] [pool] ...
3001 * -f (undocumented) force uberblock (and config including zpool cache file)
3004 * Sync the specified pool(s).
3005 * Without arguments "zpool sync" will sync all pools.
3006 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3010 zpool_do_sync(int argc
, char **argv
)
3013 boolean_t force
= B_FALSE
;
3016 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3022 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3031 /* if argc == 0 we will execute zpool_sync_one on all pools */
3032 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
3037 typedef struct iostat_cbdata
{
3042 char **cb_vdev_names
; /* Only show these vdevs */
3043 unsigned int cb_vdev_names_count
;
3044 boolean_t cb_verbose
;
3045 boolean_t cb_literal
;
3046 boolean_t cb_scripted
;
3047 zpool_list_t
*cb_list
;
3048 vdev_cmd_data_list_t
*vcdl
;
3052 typedef struct name_and_columns
{
3053 const char *name
; /* Column name */
3054 unsigned int columns
; /* Center name to this number of columns */
3055 } name_and_columns_t
;
3057 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
3059 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3061 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3063 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3064 {"asyncq_wait", 2}, {"scrub"}},
3065 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3066 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3068 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
3069 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
3070 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3071 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
3075 /* Shorthand - if "columns" field not set, default to 1 column */
3076 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3078 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3080 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3081 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
3082 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3083 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3084 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3085 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
3086 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3087 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3090 static const char *histo_to_title
[] = {
3091 [IOS_L_HISTO
] = "latency",
3092 [IOS_RQ_HISTO
] = "req_size",
3096 * Return the number of labels in a null-terminated name_and_columns_t
3101 label_array_len(const name_and_columns_t
*labels
)
3105 while (labels
[i
].name
)
3112 * Return the number of strings in a null-terminated string array.
3115 * const char foo[] = {"bar", "baz", NULL}
3120 str_array_len(const char *array
[])
3131 * Return a default column width for default/latency/queue columns. This does
3132 * not include histograms, which have their columns autosized.
3135 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3137 unsigned long column_width
= 5; /* Normal niceprint */
3138 static unsigned long widths
[] = {
3140 * Choose some sane default column sizes for printing the
3143 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3144 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3145 [IOS_QUEUES
] = 6, /* 1M queue entries */
3149 column_width
= widths
[type
];
3151 return (column_width
);
3155 * Print the column labels, i.e:
3157 * capacity operations bandwidth
3158 * alloc free read write read write ...
3160 * If force_column_width is set, use it for the column width. If not set, use
3161 * the default column width.
3164 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3165 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3168 unsigned int text_start
, rw_column_width
, spaces_to_end
;
3169 uint64_t flags
= cb
->cb_flags
;
3171 unsigned int column_width
= force_column_width
;
3173 /* For each bit set in flags */
3174 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3175 idx
= lowbit64(f
) - 1;
3176 if (!force_column_width
)
3177 column_width
= default_column_width(cb
, idx
);
3178 /* Print our top labels centered over "read write" label. */
3179 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3180 const char *name
= labels
[idx
][i
].name
;
3182 * We treat labels[][].columns == 0 as shorthand
3183 * for one column. It makes writing out the label
3184 * tables more concise.
3186 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3187 unsigned int slen
= strlen(name
);
3189 rw_column_width
= (column_width
* columns
) +
3190 (2 * (columns
- 1));
3192 text_start
= (int)((rw_column_width
)/columns
-
3195 printf(" "); /* Two spaces between columns */
3197 /* Space from beginning of column to label */
3198 for (s
= 0; s
< text_start
; s
++)
3203 /* Print space after label to end of column */
3204 spaces_to_end
= rw_column_width
- text_start
- slen
;
3205 for (s
= 0; s
< spaces_to_end
; s
++)
3214 * print_cmd_columns - Print custom column titles from -c
3216 * If the user specified the "zpool status|iostat -c" then print their custom
3217 * column titles in the header. For example, print_cmd_columns() would print
3218 * the " col1 col2" part of this:
3220 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3222 * capacity operations bandwidth
3223 * pool alloc free read write read write col1 col2
3224 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3225 * mypool 269K 1008M 0 0 107 946
3226 * mirror 269K 1008M 0 0 107 946
3227 * sdb - - 0 0 102 473 val1 val2
3228 * sdc - - 0 0 5 473 val1 val2
3229 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3232 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3235 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3237 if (vcdl
->count
== 0 || data
== NULL
)
3241 * Each vdev cmd should have the same column names unless the user did
3242 * something weird with their cmd. Just take the column names from the
3243 * first vdev and assume it works for all of them.
3245 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3248 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3251 printf("%*s", vcdl
->uniq_cols_width
[i
],
3252 vcdl
->uniq_cols
[i
]);
3259 * Utility function to print out a line of dashes like:
3261 * -------------------------------- ----- ----- ----- ----- -----
3263 * ...or a dashed named-row line like:
3269 * @force_column_width If non-zero, use the value as the column width.
3270 * Otherwise use the default column widths.
3272 * @name: Print a dashed named-row line starting
3273 * with @name. Otherwise, print a regular
3277 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3281 unsigned int namewidth
;
3282 uint64_t flags
= cb
->cb_flags
;
3285 const name_and_columns_t
*labels
;
3289 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3290 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3291 } else if (cb
->cb_vdev_names_count
) {
3297 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3298 name
? strlen(name
) : 0);
3302 printf("%-*s", namewidth
, name
);
3304 for (i
= 0; i
< namewidth
; i
++)
3308 /* For each bit in flags */
3309 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3310 unsigned int column_width
;
3311 idx
= lowbit64(f
) - 1;
3312 if (force_column_width
)
3313 column_width
= force_column_width
;
3315 column_width
= default_column_width(cb
, idx
);
3317 labels
= iostat_bottom_labels
[idx
];
3318 for (i
= 0; i
< label_array_len(labels
); i
++) {
3320 printf(" %*s-", column_width
- 1, " ");
3322 printf(" %.*s", column_width
,
3323 "--------------------");
3330 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3331 unsigned int force_column_width
)
3333 print_iostat_dashes(cb
, force_column_width
, NULL
);
3337 print_iostat_separator(iostat_cbdata_t
*cb
)
3339 print_iostat_separator_impl(cb
, 0);
3343 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3344 const char *histo_vdev_name
)
3346 unsigned int namewidth
;
3349 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3350 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3351 } else if (cb
->cb_vdev_names_count
) {
3357 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3358 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3360 if (histo_vdev_name
)
3361 printf("%-*s", namewidth
, histo_vdev_name
);
3363 printf("%*s", namewidth
, "");
3366 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3369 printf("%-*s", namewidth
, title
);
3371 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3372 if (cb
->vcdl
!= NULL
)
3373 print_cmd_columns(cb
->vcdl
, 0);
3377 print_iostat_separator_impl(cb
, force_column_width
);
3379 if (cb
->vcdl
!= NULL
)
3380 print_cmd_columns(cb
->vcdl
, 1);
3386 print_iostat_header(iostat_cbdata_t
*cb
)
3388 print_iostat_header_impl(cb
, 0, NULL
);
3393 * Display a single statistic.
3396 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3397 unsigned int column_size
, boolean_t scripted
)
3401 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3404 printf("\t%s", buf
);
3406 printf(" %*s", column_size
, buf
);
3410 * Calculate the default vdev stats
3412 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3413 * stats into calcvs.
3416 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3417 vdev_stat_t
*calcvs
)
3421 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3422 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3423 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3425 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3426 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3430 * Internal representation of the extended iostats data.
3432 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3433 * or single uint64_t's. We make both look like arrays to make them easier
3434 * to process. In order to make single uint64_t's look like arrays, we set
3435 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3436 * we can just use *data and count.
3440 uint_t count
; /* Number of entries in data[] */
3441 uint64_t __data
; /* Only used when data is a single uint64_t */
3445 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3449 for (i
= 0; i
< len
; i
++)
3450 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3456 * Helper function to lookup a uint64_t array or uint64_t value and store its
3457 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3458 * it look like a one element array to make it easier to process.
3461 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3462 struct stat_array
*nva
)
3467 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3468 switch (nvpair_type(tmp
)) {
3469 case DATA_TYPE_UINT64_ARRAY
:
3470 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3472 case DATA_TYPE_UINT64
:
3473 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3474 nva
->data
= &nva
->__data
;
3478 /* Not a uint64_t */
3487 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3488 * subtract them, and return the results in a newly allocated stat_array.
3489 * You must free the returned array after you are done with it with
3490 * free_calc_stats().
3492 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3495 static struct stat_array
*
3496 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3499 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3500 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3502 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3504 /* Extract our extended stats nvlist from the main list */
3505 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3508 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3512 newnva
= safe_malloc(alloc_size
);
3513 oldnva
= safe_malloc(alloc_size
);
3514 calcnva
= safe_malloc(alloc_size
);
3516 for (j
= 0; j
< len
; j
++) {
3517 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3519 calcnva
[j
].count
= newnva
[j
].count
;
3520 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3521 calcnva
[j
].data
= safe_malloc(alloc_size
);
3522 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3525 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3527 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3528 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3537 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3540 for (i
= 0; i
< len
; i
++)
3547 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3548 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3554 enum zfs_nicenum_format format
;
3555 unsigned int buckets
;
3556 unsigned int start_bucket
;
3559 format
= ZFS_NICENUM_RAW
;
3561 format
= ZFS_NICENUM_1024
;
3563 /* All these histos are the same size, so just use nva[0].count */
3564 buckets
= nva
[0].count
;
3566 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3567 /* Start at 512 - req size should never be lower than this */
3573 for (j
= start_bucket
; j
< buckets
; j
++) {
3574 /* Print histogram bucket label */
3575 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3576 /* Ending range of this bucket */
3577 val
= (1UL << (j
+ 1)) - 1;
3578 zfs_nicetime(val
, buf
, sizeof (buf
));
3580 /* Request size (starting range of bucket) */
3582 zfs_nicenum(val
, buf
, sizeof (buf
));
3585 if (cb
->cb_scripted
)
3586 printf("%llu", (u_longlong_t
)val
);
3588 printf("%-*s", namewidth
, buf
);
3590 /* Print the values on the line */
3591 for (i
= 0; i
< len
; i
++) {
3592 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3593 column_width
, cb
->cb_scripted
);
3600 print_solid_separator(unsigned int length
)
3608 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3609 nvlist_t
*newnv
, double scale
, const char *name
)
3611 unsigned int column_width
;
3612 unsigned int namewidth
;
3613 unsigned int entire_width
;
3614 enum iostat_type type
;
3615 struct stat_array
*nva
;
3617 unsigned int names_len
;
3619 /* What type of histo are we? */
3620 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3622 /* Get NULL-terminated array of nvlist names for our histo */
3623 names
= vsx_type_to_nvlist
[type
];
3624 names_len
= str_array_len(names
); /* num of names */
3626 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3628 if (cb
->cb_literal
) {
3629 column_width
= MAX(5,
3630 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3635 namewidth
= MAX(cb
->cb_namewidth
,
3636 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3639 * Calculate the entire line width of what we're printing. The
3640 * +2 is for the two spaces between columns:
3644 /* |___| <---------- column_width */
3646 /* |__________| <--- entire_width */
3648 entire_width
= namewidth
+ (column_width
+ 2) *
3649 label_array_len(iostat_bottom_labels
[type
]);
3651 if (cb
->cb_scripted
)
3652 printf("%s\n", name
);
3654 print_iostat_header_impl(cb
, column_width
, name
);
3656 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3659 free_calc_stats(nva
, names_len
);
3660 if (!cb
->cb_scripted
)
3661 print_solid_separator(entire_width
);
3665 * Calculate the average latency of a power-of-two latency histogram
3668 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3671 uint64_t count
= 0, total
= 0;
3673 for (i
= 0; i
< buckets
; i
++) {
3675 * Our buckets are power-of-two latency ranges. Use the
3676 * midpoint latency of each bucket to calculate the average.
3684 if (histo
[i
] != 0) {
3685 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3690 /* Prevent divide by zero */
3691 return (count
== 0 ? 0 : total
/ count
);
3695 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3696 nvlist_t
*newnv
, double scale
)
3700 const char *names
[] = {
3701 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3702 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3703 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3704 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3705 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3706 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3707 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3708 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3709 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3710 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3713 struct stat_array
*nva
;
3715 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3716 enum zfs_nicenum_format format
;
3718 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3721 format
= ZFS_NICENUM_RAW
;
3723 format
= ZFS_NICENUM_1024
;
3725 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3726 val
= nva
[i
].data
[0] * scale
;
3727 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3730 free_calc_stats(nva
, ARRAY_SIZE(names
));
3734 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3735 nvlist_t
*newnv
, double scale
)
3739 const char *names
[] = {
3740 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3741 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3742 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3743 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3744 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3745 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3746 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3747 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3748 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3750 struct stat_array
*nva
;
3752 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3753 enum zfs_nicenum_format format
;
3755 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3758 format
= ZFS_NICENUM_RAWTIME
;
3760 format
= ZFS_NICENUM_TIME
;
3762 /* Print our avg latencies on the line */
3763 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3764 /* Compute average latency for a latency histo */
3765 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3766 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3768 free_calc_stats(nva
, ARRAY_SIZE(names
));
3772 * Print default statistics (capacity/operations/bandwidth)
3775 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3777 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3778 enum zfs_nicenum_format format
;
3779 char na
; /* char to print for "not applicable" values */
3781 if (cb
->cb_literal
) {
3782 format
= ZFS_NICENUM_RAW
;
3785 format
= ZFS_NICENUM_1024
;
3789 /* only toplevel vdevs have capacity stats */
3790 if (vs
->vs_space
== 0) {
3791 if (cb
->cb_scripted
)
3792 printf("\t%c\t%c", na
, na
);
3794 printf(" %*c %*c", column_width
, na
, column_width
,
3797 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3799 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3800 column_width
, cb
->cb_scripted
);
3803 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3804 format
, column_width
, cb
->cb_scripted
);
3805 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3806 format
, column_width
, cb
->cb_scripted
);
3807 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3808 format
, column_width
, cb
->cb_scripted
);
3809 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3810 format
, column_width
, cb
->cb_scripted
);
3814 * Print out all the statistics for the given vdev. This can either be the
3815 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3816 * is a verbose output, and we don't want to display the toplevel pool stats.
3818 * Returns the number of stat lines printed.
3821 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3822 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3824 nvlist_t
**oldchild
, **newchild
;
3825 uint_t c
, children
, oldchildren
;
3826 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3827 vdev_stat_t zerovs
= { 0 };
3834 calcvs
= safe_malloc(sizeof (*calcvs
));
3836 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
3839 if (oldnv
!= NULL
) {
3840 verify(nvlist_lookup_uint64_array(oldnv
,
3841 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3846 /* Do we only want to see a specific vdev? */
3847 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3848 /* Yes we do. Is this the vdev? */
3849 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3851 * This is our vdev. Since it is the only vdev we
3852 * will be displaying, make depth = 0 so that it
3853 * doesn't get indented.
3860 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3861 /* Couldn't match the name */
3866 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3867 (uint64_t **)&newvs
, &c
) == 0);
3870 * Print the vdev name unless it's is a histogram. Histograms
3871 * display the vdev name in the header itself.
3873 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3874 if (cb
->cb_scripted
) {
3877 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3878 (void) printf("%*s%s", depth
, "", name
);
3880 (void) printf("%*s%s%*s", depth
, "", name
,
3881 (int)(cb
->cb_namewidth
- strlen(name
) -
3886 /* Calculate our scaling factor */
3887 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3888 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3890 * If we specify printing histograms with no time interval, then
3891 * print the histogram numbers over the entire lifetime of the
3899 scale
= (double)NANOSEC
/ tdelta
;
3902 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3903 calc_default_iostats(oldvs
, newvs
, calcvs
);
3904 print_iostat_default(calcvs
, cb
, scale
);
3906 if (cb
->cb_flags
& IOS_LATENCY_M
)
3907 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3908 if (cb
->cb_flags
& IOS_QUEUES_M
)
3909 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3910 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3912 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3915 if (cb
->vcdl
!= NULL
) {
3917 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3920 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3924 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3933 if (!cb
->cb_verbose
)
3936 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3937 &newchild
, &children
) != 0)
3941 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3942 &oldchild
, &oldchildren
) != 0)
3945 children
= MIN(oldchildren
, children
);
3948 for (c
= 0; c
< children
; c
++) {
3949 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3951 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3954 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3957 if (ishole
|| islog
)
3960 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3962 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3963 newchild
[c
], cb
, depth
+ 2);
3968 * Log device section
3971 if (num_logs(newnv
) > 0) {
3972 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3973 !cb
->cb_vdev_names
) {
3974 print_iostat_dashes(cb
, 0, "logs");
3978 for (c
= 0; c
< children
; c
++) {
3979 uint64_t islog
= B_FALSE
;
3980 (void) nvlist_lookup_uint64(newchild
[c
],
3981 ZPOOL_CONFIG_IS_LOG
, &islog
);
3984 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3986 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3987 oldchild
[c
] : NULL
, newchild
[c
],
3996 * Include level 2 ARC devices in iostat output
3998 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3999 &newchild
, &children
) != 0)
4003 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4004 &oldchild
, &oldchildren
) != 0)
4007 children
= MIN(oldchildren
, children
);
4011 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4012 !cb
->cb_vdev_names
) {
4013 print_iostat_dashes(cb
, 0, "cache");
4017 for (c
= 0; c
< children
; c
++) {
4018 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4020 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4021 : NULL
, newchild
[c
], cb
, depth
+ 2);
4030 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4032 iostat_cbdata_t
*cb
= data
;
4036 * If the pool has disappeared, remove it from the list and continue.
4038 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4042 pool_list_remove(cb
->cb_list
, zhp
);
4048 * Callback to print out the iostats for the given pool.
4051 print_iostat(zpool_handle_t
*zhp
, void *data
)
4053 iostat_cbdata_t
*cb
= data
;
4054 nvlist_t
*oldconfig
, *newconfig
;
4055 nvlist_t
*oldnvroot
, *newnvroot
;
4058 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4060 if (cb
->cb_iteration
== 1)
4063 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4066 if (oldconfig
== NULL
)
4069 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4072 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4074 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4075 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4076 print_iostat_separator(cb
);
4077 if (cb
->vcdl
!= NULL
) {
4078 print_cmd_columns(cb
->vcdl
, 1);
4093 if (isatty(STDOUT_FILENO
)) {
4094 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4096 columns
= ws
.ws_col
;
4105 get_namewidth(zpool_handle_t
*zhp
, void *data
)
4107 iostat_cbdata_t
*cb
= data
;
4108 nvlist_t
*config
, *nvroot
;
4111 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4112 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4114 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4115 if (!cb
->cb_verbose
)
4116 cb
->cb_namewidth
= MAX(poolname_len
, cb
->cb_namewidth
);
4118 cb
->cb_namewidth
= MAX(poolname_len
,
4119 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
4120 cb
->cb_name_flags
));
4123 * The width must be at least 10, but may be as large as the
4124 * column width - 42 so that we can still fit in one line.
4126 columns
= get_columns();
4128 if (cb
->cb_namewidth
< 10)
4129 cb
->cb_namewidth
= 10;
4130 if (cb
->cb_namewidth
> columns
- 42)
4131 cb
->cb_namewidth
= columns
- 42;
4137 * Parse the input string, get the 'interval' and 'count' value if there is one.
4140 get_interval_count(int *argcp
, char **argv
, float *iv
,
4144 unsigned long count
= 0;
4148 * Determine if the last argument is an integer or a pool name
4150 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4154 interval
= strtof(argv
[argc
- 1], &end
);
4156 if (*end
== '\0' && errno
== 0) {
4157 if (interval
== 0) {
4158 (void) fprintf(stderr
, gettext("interval "
4159 "cannot be zero\n"));
4163 * Ignore the last parameter
4168 * If this is not a valid number, just plow on. The
4169 * user will get a more informative error message later
4177 * If the last argument is also an integer, then we have both a count
4180 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4185 interval
= strtof(argv
[argc
- 1], &end
);
4187 if (*end
== '\0' && errno
== 0) {
4188 if (interval
== 0) {
4189 (void) fprintf(stderr
, gettext("interval "
4190 "cannot be zero\n"));
4195 * Ignore the last parameter
4209 get_timestamp_arg(char c
)
4212 timestamp_fmt
= UDATE
;
4214 timestamp_fmt
= DDATE
;
4220 * Return stat flags that are supported by all pools by both the module and
4221 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4222 * It will get ANDed down until only the flags that are supported on all pools
4226 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4228 uint64_t *mask
= data
;
4229 nvlist_t
*config
, *nvroot
, *nvx
;
4233 config
= zpool_get_config(zhp
, NULL
);
4234 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4237 /* Default stats are always supported, but for completeness.. */
4238 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4239 flags
|= IOS_DEFAULT_M
;
4241 /* Get our extended stats nvlist from the main list */
4242 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4245 * No extended stats; they're probably running an older
4246 * module. No big deal, we support that too.
4251 /* For each extended stat, make sure all its nvpairs are supported */
4252 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4253 if (!vsx_type_to_nvlist
[j
][0])
4256 /* Start off by assuming the flag is supported, then check */
4257 flags
|= (1ULL << j
);
4258 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4259 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4260 /* flag isn't supported */
4261 flags
= flags
& ~(1ULL << j
);
4267 *mask
= *mask
& flags
;
4272 * Return a bitmask of stats that are supported on all pools by both the module
4276 get_stat_flags(zpool_list_t
*list
)
4281 * get_stat_flags_cb() will lop off bits from "mask" until only the
4282 * flags that are supported on all pools remain.
4284 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4289 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4292 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4294 iostat_cbdata_t
*cb
= cb_data
;
4298 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4300 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4301 ret
= 1; /* match */
4308 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4311 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4313 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4317 * Check if vdevs are in a pool
4319 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4320 * return 0. If pool_name is NULL, then search all pools.
4323 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4324 iostat_cbdata_t
*cb
)
4331 if ((argc
== 0) || !*argv
)
4337 /* Temporarily hijack cb_vdev_names for a second... */
4338 tmp_name
= cb
->cb_vdev_names
;
4340 /* Go though our list of prospective vdev names */
4341 for (i
= 0; i
< argc
; i
++) {
4342 cb
->cb_vdev_names
= argv
+ i
;
4344 /* Is this name a vdev in our pools? */
4345 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4353 cb
->cb_vdev_names
= tmp_name
;
4359 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4362 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4369 * Do we have a pool named *name? If so, return 1, otherwise 0.
4374 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4377 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4379 are_all_pools(int argc
, char **argv
)
4381 if ((argc
== 0) || !*argv
)
4385 if (!is_pool(argv
[argc
]))
4392 * Helper function to print out vdev/pool names we can't resolve. Used for an
4396 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4397 iostat_cbdata_t
*cb
)
4402 for (i
= 0; i
< argc
; i
++) {
4406 str
= gettext("pool");
4407 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4408 str
= gettext("vdev in this pool");
4409 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4410 str
= gettext("vdev in another pool");
4412 str
= gettext("unknown");
4414 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4419 * Same as get_interval_count(), but with additional checks to not misinterpret
4420 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4424 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4425 unsigned long *count
, iostat_cbdata_t
*cb
)
4427 char **tmpargv
= argv
;
4428 int argc_for_interval
= 0;
4430 /* Is the last arg an interval value? Or a guid? */
4431 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4433 * The last arg is not a guid, so it's probably an
4436 argc_for_interval
++;
4439 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4441 * The 2nd to last arg is not a guid, so it's probably
4442 * an interval value.
4444 argc_for_interval
++;
4448 /* Point to our list of possible intervals */
4449 tmpargv
= &argv
[*argc
- argc_for_interval
];
4451 *argc
= *argc
- argc_for_interval
;
4452 get_interval_count(&argc_for_interval
, tmpargv
,
4457 * Floating point sleep(). Allows you to pass in a floating point value for
4463 struct timespec req
;
4464 req
.tv_sec
= floor(sec
);
4465 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4466 nanosleep(&req
, NULL
);
4470 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4473 * name: Short name of the script ('iostat').
4474 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4477 print_zpool_script_help(char *name
, char *path
)
4479 char *argv
[] = {path
, "-h", NULL
};
4480 char **lines
= NULL
;
4484 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4486 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
4488 libzfs_free_str_array(lines
, lines_cnt
);
4492 for (int i
= 0; i
< lines_cnt
; i
++)
4493 if (!is_blank_str(lines
[i
]))
4494 printf(" %-14s %s\n", name
, lines
[i
]);
4496 libzfs_free_str_array(lines
, lines_cnt
);
4500 * Go though the zpool status/iostat -c scripts in the user's path, run their
4501 * help option (-h), and print out the results.
4504 print_zpool_dir_scripts(char *dirpath
)
4508 char fullpath
[MAXPATHLEN
];
4509 struct stat dir_stat
;
4511 if ((dir
= opendir(dirpath
)) != NULL
) {
4512 /* print all the files and directories within directory */
4513 while ((ent
= readdir(dir
)) != NULL
) {
4514 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
4516 /* Print the scripts */
4517 if (stat(fullpath
, &dir_stat
) == 0)
4518 if (dir_stat
.st_mode
& S_IXUSR
&&
4519 S_ISREG(dir_stat
.st_mode
))
4520 print_zpool_script_help(ent
->d_name
,
4528 * Print out help text for all zpool status/iostat -c scripts.
4531 print_zpool_script_list(char *subcommand
)
4535 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
4537 sp
= zpool_get_cmd_search_path();
4541 dir
= strtok(sp
, ":");
4542 while (dir
!= NULL
) {
4543 print_zpool_dir_scripts(dir
);
4544 dir
= strtok(NULL
, ":");
4551 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4552 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4553 * [interval [count]]
4555 * -c CMD For each vdev, run command CMD
4556 * -g Display guid for individual vdev name.
4557 * -L Follow links when resolving vdev path name.
4558 * -P Display full path for vdev name.
4559 * -v Display statistics for individual vdevs
4561 * -p Display values in parsable (exact) format.
4562 * -H Scripted mode. Don't display headers, and separate properties
4564 * -l Display average latency
4565 * -q Display queue depths
4566 * -w Display latency histograms
4567 * -r Display request size histogram
4568 * -T Display a timestamp in date(1) or Unix format
4570 * This command can be tricky because we want to be able to deal with pool
4571 * creation/destruction as well as vdev configuration changes. The bulk of this
4572 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4573 * on pool_list_update() to detect the addition of new pools. Configuration
4574 * changes are all handled within libzfs.
4577 zpool_do_iostat(int argc
, char **argv
)
4583 unsigned long count
= 0;
4585 boolean_t verbose
= B_FALSE
;
4586 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4587 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4588 boolean_t omit_since_boot
= B_FALSE
;
4589 boolean_t guid
= B_FALSE
;
4590 boolean_t follow_links
= B_FALSE
;
4591 boolean_t full_name
= B_FALSE
;
4592 iostat_cbdata_t cb
= { 0 };
4595 /* Used for printing error message */
4596 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4597 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4599 uint64_t unsupported_flags
;
4602 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4607 gettext("Can't set -c flag twice\n"));
4611 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
4612 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4613 fprintf(stderr
, gettext(
4614 "Can't run -c, disabled by "
4615 "ZPOOL_SCRIPTS_ENABLED.\n"));
4619 if ((getuid() <= 0 || geteuid() <= 0) &&
4620 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4621 fprintf(stderr
, gettext(
4622 "Can't run -c with root privileges "
4623 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4633 follow_links
= B_TRUE
;
4639 get_timestamp_arg(*optarg
);
4663 omit_since_boot
= B_TRUE
;
4669 if (optopt
== 'c') {
4670 print_zpool_script_list("iostat");
4674 gettext("invalid option '%c'\n"), optopt
);
4683 cb
.cb_literal
= parsable
;
4684 cb
.cb_scripted
= scripted
;
4687 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4689 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4691 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4692 cb
.cb_iteration
= 0;
4693 cb
.cb_namewidth
= 0;
4694 cb
.cb_verbose
= verbose
;
4696 /* Get our interval and count values (if any) */
4698 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4701 get_interval_count(&argc
, argv
, &interval
, &count
);
4705 /* No args, so just print the defaults. */
4706 } else if (are_all_pools(argc
, argv
)) {
4707 /* All the args are pool names */
4708 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4709 /* All the args are vdevs */
4710 cb
.cb_vdev_names
= argv
;
4711 cb
.cb_vdev_names_count
= argc
;
4712 argc
= 0; /* No pools to process */
4713 } else if (are_all_pools(1, argv
)) {
4714 /* The first arg is a pool name */
4715 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4716 /* ...and the rest are vdev names */
4717 cb
.cb_vdev_names
= argv
+ 1;
4718 cb
.cb_vdev_names_count
= argc
- 1;
4719 argc
= 1; /* One pool to process */
4721 fprintf(stderr
, gettext("Expected either a list of "));
4722 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4723 fprintf(stderr
, " \"%s\", ", argv
[0]);
4724 fprintf(stderr
, gettext("but got:\n"));
4725 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4727 fprintf(stderr
, "\n");
4733 * The args don't make sense. The first arg isn't a pool name,
4734 * nor are all the args vdevs.
4736 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4737 fprintf(stderr
, "\n");
4741 if (cb
.cb_vdev_names_count
!= 0) {
4743 * If user specified vdevs, it implies verbose.
4745 cb
.cb_verbose
= B_TRUE
;
4749 * Construct the list of all interesting pools.
4752 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4755 if (pool_list_count(list
) == 0 && argc
!= 0) {
4756 pool_list_free(list
);
4760 if (pool_list_count(list
) == 0 && interval
== 0) {
4761 pool_list_free(list
);
4762 (void) fprintf(stderr
, gettext("no pools available\n"));
4766 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4767 pool_list_free(list
);
4768 (void) fprintf(stderr
,
4769 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4774 if (l_histo
&& rq_histo
) {
4775 pool_list_free(list
);
4776 (void) fprintf(stderr
,
4777 gettext("Only one of [-r|-w] can be passed at a time\n"));
4783 * Enter the main iostat loop.
4789 * Histograms tables look out of place when you try to display
4790 * them with the other stats, so make a rule that you can only
4791 * print histograms by themselves.
4793 cb
.cb_flags
= IOS_L_HISTO_M
;
4794 } else if (rq_histo
) {
4795 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4797 cb
.cb_flags
= IOS_DEFAULT_M
;
4799 cb
.cb_flags
|= IOS_LATENCY_M
;
4801 cb
.cb_flags
|= IOS_QUEUES_M
;
4805 * See if the module supports all the stats we want to display.
4807 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4808 if (unsupported_flags
) {
4812 gettext("The loaded zfs module doesn't support:"));
4814 /* for each bit set in unsupported_flags */
4815 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4816 idx
= lowbit64(f
) - 1;
4817 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4820 fprintf(stderr
, ". Try running a newer module.\n");
4821 pool_list_free(list
);
4827 if ((npools
= pool_list_count(list
)) == 0)
4828 (void) fprintf(stderr
, gettext("no pools available\n"));
4831 * If this is the first iteration and -y was supplied
4832 * we skip any printing.
4834 boolean_t skip
= (omit_since_boot
&&
4835 cb
.cb_iteration
== 0);
4838 * Refresh all statistics. This is done as an
4839 * explicit step before calculating the maximum name
4840 * width, so that any * configuration changes are
4841 * properly accounted for.
4843 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4847 * Iterate over all pools to determine the maximum width
4848 * for the pool / device name column across all pools.
4850 cb
.cb_namewidth
= 0;
4851 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4854 if (timestamp_fmt
!= NODATE
)
4855 print_timestamp(timestamp_fmt
);
4857 if (cmd
!= NULL
&& cb
.cb_verbose
&&
4858 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
4859 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4860 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4861 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4867 * If it's the first time and we're not skipping it,
4868 * or either skip or verbose mode, print the header.
4870 * The histogram code explicitly prints its header on
4871 * every vdev, so skip this for histograms.
4873 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4874 (skip
!= verbose
)) &&
4875 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4877 print_iostat_header(&cb
);
4880 (void) fsleep(interval
);
4885 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4888 * If there's more than one pool, and we're not in
4889 * verbose mode (which prints a separator for us),
4890 * then print a separator.
4892 * In addition, if we're printing specific vdevs then
4893 * we also want an ending separator.
4895 if (((npools
> 1 && !verbose
&&
4896 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4897 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4898 cb
.cb_vdev_names_count
)) &&
4900 print_iostat_separator(&cb
);
4901 if (cb
.vcdl
!= NULL
)
4902 print_cmd_columns(cb
.vcdl
, 1);
4906 if (cb
.vcdl
!= NULL
)
4907 free_vdev_cmd_data_list(cb
.vcdl
);
4912 * Flush the output so that redirection to a file isn't buffered
4915 (void) fflush(stdout
);
4920 if (count
!= 0 && --count
== 0)
4923 (void) fsleep(interval
);
4926 pool_list_free(list
);
4931 typedef struct list_cbdata
{
4932 boolean_t cb_verbose
;
4935 boolean_t cb_scripted
;
4936 zprop_list_t
*cb_proplist
;
4937 boolean_t cb_literal
;
4941 * Given a list of columns to display, output appropriate headers for each one.
4944 print_header(list_cbdata_t
*cb
)
4946 zprop_list_t
*pl
= cb
->cb_proplist
;
4947 char headerbuf
[ZPOOL_MAXPROPLEN
];
4949 boolean_t first
= B_TRUE
;
4950 boolean_t right_justify
;
4953 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4954 width
= pl
->pl_width
;
4955 if (first
&& cb
->cb_verbose
) {
4957 * Reset the width to accommodate the verbose listing
4960 width
= cb
->cb_namewidth
;
4968 right_justify
= B_FALSE
;
4969 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4970 header
= zpool_prop_column_name(pl
->pl_prop
);
4971 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4975 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4976 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4977 headerbuf
[i
] = '\0';
4981 if (pl
->pl_next
== NULL
&& !right_justify
)
4982 (void) printf("%s", header
);
4983 else if (right_justify
)
4984 (void) printf("%*s", (int)width
, header
);
4986 (void) printf("%-*s", (int)width
, header
);
4989 (void) printf("\n");
4993 * Given a pool and a list of properties, print out all the properties according
4994 * to the described layout.
4997 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4999 zprop_list_t
*pl
= cb
->cb_proplist
;
5000 boolean_t first
= B_TRUE
;
5001 char property
[ZPOOL_MAXPROPLEN
];
5003 boolean_t right_justify
;
5006 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5008 width
= pl
->pl_width
;
5009 if (first
&& cb
->cb_verbose
) {
5011 * Reset the width to accommodate the verbose listing
5014 width
= cb
->cb_namewidth
;
5018 if (cb
->cb_scripted
)
5019 (void) printf("\t");
5026 right_justify
= B_FALSE
;
5027 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5028 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5029 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5034 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5035 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5036 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5037 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5038 sizeof (property
)) == 0) {
5046 * If this is being called in scripted mode, or if this is the
5047 * last column and it is left-justified, don't include a width
5050 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5051 (void) printf("%s", propstr
);
5052 else if (right_justify
)
5053 (void) printf("%*s", (int)width
, propstr
);
5055 (void) printf("%-*s", (int)width
, propstr
);
5058 (void) printf("\n");
5062 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
5063 boolean_t valid
, enum zfs_nicenum_format format
)
5067 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5070 case ZPOOL_PROP_EXPANDSZ
:
5071 case ZPOOL_PROP_CHECKPOINT
:
5073 (void) strlcpy(propval
, "-", sizeof (propval
));
5075 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5078 case ZPOOL_PROP_FRAGMENTATION
:
5079 if (value
== ZFS_FRAG_INVALID
) {
5080 (void) strlcpy(propval
, "-", sizeof (propval
));
5081 } else if (format
== ZFS_NICENUM_RAW
) {
5082 (void) snprintf(propval
, sizeof (propval
), "%llu",
5083 (unsigned long long)value
);
5085 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5086 (unsigned long long)value
);
5089 case ZPOOL_PROP_CAPACITY
:
5090 if (format
== ZFS_NICENUM_RAW
)
5091 (void) snprintf(propval
, sizeof (propval
), "%llu",
5092 (unsigned long long)value
);
5094 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5095 (unsigned long long)value
);
5098 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5102 (void) strlcpy(propval
, "-", sizeof (propval
));
5105 (void) printf("\t%s", propval
);
5107 (void) printf(" %*s", (int)width
, propval
);
5111 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5112 list_cbdata_t
*cb
, int depth
)
5118 boolean_t scripted
= cb
->cb_scripted
;
5119 uint64_t islog
= B_FALSE
;
5120 boolean_t haslog
= B_FALSE
;
5121 char *dashes
= "%-*s - - - - - -\n";
5123 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5124 (uint64_t **)&vs
, &c
) == 0);
5127 boolean_t toplevel
= (vs
->vs_space
!= 0);
5129 enum zfs_nicenum_format format
;
5132 format
= ZFS_NICENUM_RAW
;
5134 format
= ZFS_NICENUM_1024
;
5136 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5140 (void) printf("\t%s", name
);
5141 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5142 (void) printf("%*s%s", depth
, "", name
);
5144 (void) printf("%*s%s%*s", depth
, "", name
,
5145 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5148 * Print the properties for the individual vdevs. Some
5149 * properties are only applicable to toplevel vdevs. The
5150 * 'toplevel' boolean value is passed to the print_one_column()
5151 * to indicate that the value is valid.
5153 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
5155 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
5157 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5158 scripted
, toplevel
, format
);
5159 print_one_column(ZPOOL_PROP_CHECKPOINT
,
5160 vs
->vs_checkpoint_space
, scripted
, toplevel
, format
);
5161 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
5163 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5164 vs
->vs_fragmentation
, scripted
,
5165 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5167 cap
= (vs
->vs_space
== 0) ? 0 :
5168 (vs
->vs_alloc
* 100 / vs
->vs_space
);
5169 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
5171 (void) printf("\n");
5174 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5175 &child
, &children
) != 0)
5178 for (c
= 0; c
< children
; c
++) {
5179 uint64_t ishole
= B_FALSE
;
5181 if (nvlist_lookup_uint64(child
[c
],
5182 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5185 if (nvlist_lookup_uint64(child
[c
],
5186 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
5191 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5193 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5197 if (haslog
== B_TRUE
) {
5198 /* LINTED E_SEC_PRINTF_VAR_FMT */
5199 (void) printf(dashes
, cb
->cb_namewidth
, "log");
5200 for (c
= 0; c
< children
; c
++) {
5201 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5202 &islog
) != 0 || !islog
)
5204 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5206 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5211 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5212 &child
, &children
) == 0 && children
> 0) {
5213 /* LINTED E_SEC_PRINTF_VAR_FMT */
5214 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5215 for (c
= 0; c
< children
; c
++) {
5216 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5218 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5223 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5224 &children
) == 0 && children
> 0) {
5225 /* LINTED E_SEC_PRINTF_VAR_FMT */
5226 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5227 for (c
= 0; c
< children
; c
++) {
5228 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5230 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5238 * Generic callback function to list a pool.
5241 list_callback(zpool_handle_t
*zhp
, void *data
)
5243 list_cbdata_t
*cbp
= data
;
5247 config
= zpool_get_config(zhp
, NULL
);
5249 print_pool(zhp
, cbp
);
5250 if (!cbp
->cb_verbose
)
5253 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5255 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
5261 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5263 * -g Display guid for individual vdev name.
5264 * -H Scripted mode. Don't display headers, and separate properties
5266 * -L Follow links when resolving vdev path name.
5267 * -o List of properties to display. Defaults to
5268 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5269 * "dedupratio,health,altroot"
5270 * -p Display values in parsable (exact) format.
5271 * -P Display full path for vdev name.
5272 * -T Display a timestamp in date(1) or Unix format
5274 * List all pools in the system, whether or not they're healthy. Output space
5275 * statistics for each one, as well as health status summary.
5278 zpool_do_list(int argc
, char **argv
)
5282 list_cbdata_t cb
= { 0 };
5283 static char default_props
[] =
5284 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
5285 "capacity,dedupratio,health,altroot";
5286 char *props
= default_props
;
5288 unsigned long count
= 0;
5290 boolean_t first
= B_TRUE
;
5293 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
5296 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5299 cb
.cb_scripted
= B_TRUE
;
5302 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5308 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5311 cb
.cb_literal
= B_TRUE
;
5314 get_timestamp_arg(*optarg
);
5317 cb
.cb_verbose
= B_TRUE
;
5320 (void) fprintf(stderr
, gettext("missing argument for "
5321 "'%c' option\n"), optopt
);
5325 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5334 get_interval_count(&argc
, argv
, &interval
, &count
);
5336 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5340 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
5344 if (pool_list_count(list
) == 0)
5347 if (timestamp_fmt
!= NODATE
)
5348 print_timestamp(timestamp_fmt
);
5350 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
5354 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
5359 if (count
!= 0 && --count
== 0)
5362 pool_list_free(list
);
5363 (void) fsleep(interval
);
5366 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
5367 (void) printf(gettext("no pools available\n"));
5371 pool_list_free(list
);
5372 zprop_free_list(cb
.cb_proplist
);
5377 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
5379 boolean_t force
= B_FALSE
;
5382 char *poolname
, *old_disk
, *new_disk
;
5383 zpool_handle_t
*zhp
;
5384 nvlist_t
*props
= NULL
;
5389 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5395 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5396 (void) fprintf(stderr
, gettext("missing "
5397 "'=' for -o option\n"));
5403 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5404 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5408 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5417 /* get pool name and check number of arguments */
5419 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5426 (void) fprintf(stderr
,
5427 gettext("missing <device> specification\n"));
5435 (void) fprintf(stderr
,
5436 gettext("missing <new_device> specification\n"));
5439 new_disk
= old_disk
;
5449 (void) fprintf(stderr
, gettext("too many arguments\n"));
5453 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5458 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5459 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5466 /* unless manually specified use "ashift" pool property (if set) */
5467 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5470 char strval
[ZPOOL_MAXPROPLEN
];
5472 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5473 if (src
!= ZPROP_SRC_DEFAULT
) {
5474 (void) sprintf(strval
, "%" PRId32
, intval
);
5475 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5476 &props
, B_TRUE
) == 0);
5480 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5482 if (nvroot
== NULL
) {
5488 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5491 nvlist_free(nvroot
);
5498 * zpool replace [-f] <pool> <device> <new_device>
5500 * -f Force attach, even if <new_device> appears to be in use.
5502 * Replace <device> with <new_device>.
5506 zpool_do_replace(int argc
, char **argv
)
5508 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5512 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5514 * -f Force attach, even if <new_device> appears to be in use.
5515 * -o Set property=value.
5517 * Attach <new_device> to the mirror containing <device>. If <device> is not
5518 * part of a mirror, then <device> will be transformed into a mirror of
5519 * <device> and <new_device>. In either case, <new_device> will begin life
5520 * with a DTL of [0, now], and will immediately begin to resilver itself.
5523 zpool_do_attach(int argc
, char **argv
)
5525 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5529 * zpool detach [-f] <pool> <device>
5531 * -f Force detach of <device>, even if DTLs argue against it
5532 * (not supported yet)
5534 * Detach a device from a mirror. The operation will be refused if <device>
5535 * is the last device in the mirror, or if the DTLs indicate that this device
5536 * has the only valid copy of some data.
5540 zpool_do_detach(int argc
, char **argv
)
5543 char *poolname
, *path
;
5544 zpool_handle_t
*zhp
;
5548 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5552 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5561 /* get pool name and check number of arguments */
5563 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5568 (void) fprintf(stderr
,
5569 gettext("missing <device> specification\n"));
5576 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5579 ret
= zpool_vdev_detach(zhp
, path
);
5587 * zpool split [-gLnP] [-o prop=val] ...
5589 * [-R altroot] <pool> <newpool> [<device> ...]
5591 * -g Display guid for individual vdev name.
5592 * -L Follow links when resolving vdev path name.
5593 * -n Do not split the pool, but display the resulting layout if
5594 * it were to be split.
5595 * -o Set property=value, or set mount options.
5596 * -P Display full path for vdev name.
5597 * -R Mount the split-off pool under an alternate root.
5598 * -l Load encryption keys while importing.
5600 * Splits the named pool and gives it the new pool name. Devices to be split
5601 * off may be listed, provided that no more than one device is specified
5602 * per top-level vdev mirror. The newly split pool is left in an exported
5603 * state unless -R is specified.
5605 * Restrictions: the top-level of the pool pool must only be made up of
5606 * mirrors; all devices in the pool must be healthy; no device may be
5607 * undergoing a resilvering operation.
5610 zpool_do_split(int argc
, char **argv
)
5612 char *srcpool
, *newpool
, *propval
;
5613 char *mntopts
= NULL
;
5616 boolean_t loadkeys
= B_FALSE
;
5617 zpool_handle_t
*zhp
;
5618 nvlist_t
*config
, *props
= NULL
;
5620 flags
.dryrun
= B_FALSE
;
5621 flags
.import
= B_FALSE
;
5622 flags
.name_flags
= 0;
5625 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
5628 flags
.name_flags
|= VDEV_NAME_GUID
;
5631 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5634 flags
.import
= B_TRUE
;
5636 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5637 &props
, B_TRUE
) != 0) {
5646 flags
.dryrun
= B_TRUE
;
5649 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5652 if (add_prop_list(optarg
, propval
,
5653 &props
, B_TRUE
) != 0) {
5662 flags
.name_flags
|= VDEV_NAME_PATH
;
5665 (void) fprintf(stderr
, gettext("missing argument for "
5666 "'%c' option\n"), optopt
);
5670 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5677 if (!flags
.import
&& mntopts
!= NULL
) {
5678 (void) fprintf(stderr
, gettext("setting mntopts is only "
5679 "valid when importing the pool\n"));
5683 if (!flags
.import
&& loadkeys
) {
5684 (void) fprintf(stderr
, gettext("loading keys is only "
5685 "valid when importing the pool\n"));
5693 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5697 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5707 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5712 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5713 if (config
== NULL
) {
5717 (void) printf(gettext("would create '%s' with the "
5718 "following layout:\n\n"), newpool
);
5719 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5726 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5727 nvlist_free(config
);
5733 * The split was successful. Now we need to open the new
5734 * pool and import it.
5736 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5737 nvlist_free(config
);
5743 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
5748 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5749 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5751 (void) fprintf(stderr
, gettext("Split was successful, but "
5752 "the datasets could not all be mounted\n"));
5753 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5754 "different altroot\n"), "zpool import");
5757 nvlist_free(config
);
5766 * zpool online <pool> <device> ...
5769 zpool_do_online(int argc
, char **argv
)
5773 zpool_handle_t
*zhp
;
5775 vdev_state_t newstate
;
5779 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5782 flags
|= ZFS_ONLINE_EXPAND
;
5786 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5795 /* get pool name and check number of arguments */
5797 (void) fprintf(stderr
, gettext("missing pool name\n"));
5801 (void) fprintf(stderr
, gettext("missing device name\n"));
5807 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5810 for (i
= 1; i
< argc
; i
++) {
5811 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5812 if (newstate
!= VDEV_STATE_HEALTHY
) {
5813 (void) printf(gettext("warning: device '%s' "
5814 "onlined, but remains in faulted state\n"),
5816 if (newstate
== VDEV_STATE_FAULTED
)
5817 (void) printf(gettext("use 'zpool "
5818 "clear' to restore a faulted "
5821 (void) printf(gettext("use 'zpool "
5822 "replace' to replace devices "
5823 "that are no longer present\n"));
5836 * zpool offline [-ft] <pool> <device> ...
5838 * -f Force the device into a faulted state.
5840 * -t Only take the device off-line temporarily. The offline/faulted
5841 * state will not be persistent across reboots.
5845 zpool_do_offline(int argc
, char **argv
)
5849 zpool_handle_t
*zhp
;
5851 boolean_t istmp
= B_FALSE
;
5852 boolean_t fault
= B_FALSE
;
5855 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5864 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5873 /* get pool name and check number of arguments */
5875 (void) fprintf(stderr
, gettext("missing pool name\n"));
5879 (void) fprintf(stderr
, gettext("missing device name\n"));
5885 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5888 for (i
= 1; i
< argc
; i
++) {
5890 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
5892 if (istmp
== B_FALSE
) {
5893 /* Force the fault to persist across imports */
5894 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
5896 aux
= VDEV_AUX_EXTERNAL
;
5899 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
5902 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5913 * zpool clear <pool> [device]
5915 * Clear all errors associated with a pool or a particular device.
5918 zpool_do_clear(int argc
, char **argv
)
5922 boolean_t dryrun
= B_FALSE
;
5923 boolean_t do_rewind
= B_FALSE
;
5924 boolean_t xtreme_rewind
= B_FALSE
;
5925 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5926 nvlist_t
*policy
= NULL
;
5927 zpool_handle_t
*zhp
;
5928 char *pool
, *device
;
5931 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5940 xtreme_rewind
= B_TRUE
;
5943 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5953 (void) fprintf(stderr
, gettext("missing pool name\n"));
5958 (void) fprintf(stderr
, gettext("too many arguments\n"));
5962 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5963 (void) fprintf(stderr
,
5964 gettext("-n or -X only meaningful with -F\n"));
5968 rewind_policy
= ZPOOL_TRY_REWIND
;
5970 rewind_policy
= ZPOOL_DO_REWIND
;
5972 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5974 /* In future, further rewind policy choices can be passed along here */
5975 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5976 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
5977 rewind_policy
) != 0) {
5982 device
= argc
== 2 ? argv
[1] : NULL
;
5984 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5985 nvlist_free(policy
);
5989 if (zpool_clear(zhp
, device
, policy
) != 0)
5994 nvlist_free(policy
);
6000 * zpool reguid <pool>
6003 zpool_do_reguid(int argc
, char **argv
)
6007 zpool_handle_t
*zhp
;
6011 while ((c
= getopt(argc
, argv
, "")) != -1) {
6014 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6023 /* get pool name and check number of arguments */
6025 (void) fprintf(stderr
, gettext("missing pool name\n"));
6030 (void) fprintf(stderr
, gettext("too many arguments\n"));
6035 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6038 ret
= zpool_reguid(zhp
);
6046 * zpool reopen <pool>
6048 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6051 zpool_do_reopen(int argc
, char **argv
)
6055 boolean_t scrub_restart
= B_TRUE
;
6058 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6061 scrub_restart
= B_FALSE
;
6064 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6073 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6074 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
6080 typedef struct scrub_cbdata
{
6084 pool_scrub_cmd_t cb_scrub_cmd
;
6088 zpool_has_checkpoint(zpool_handle_t
*zhp
)
6090 nvlist_t
*config
, *nvroot
;
6092 config
= zpool_get_config(zhp
, NULL
);
6094 if (config
!= NULL
) {
6095 pool_checkpoint_stat_t
*pcs
= NULL
;
6098 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6099 (void) nvlist_lookup_uint64_array(nvroot
,
6100 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6102 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6105 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
6106 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6114 scrub_callback(zpool_handle_t
*zhp
, void *data
)
6116 scrub_cbdata_t
*cb
= data
;
6120 * Ignore faulted pools.
6122 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
6123 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
6124 "currently unavailable\n"), zpool_get_name(zhp
));
6128 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
6130 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
6131 cb
->cb_type
== POOL_SCAN_SCRUB
) {
6132 (void) printf(gettext("warning: will not scrub state that "
6133 "belongs to the checkpoint of pool '%s'\n"),
6134 zpool_get_name(zhp
));
6141 * zpool scrub [-s | -p] <pool> ...
6143 * -s Stop. Stops any in-progress scrub.
6144 * -p Pause. Pause in-progress scrub.
6147 zpool_do_scrub(int argc
, char **argv
)
6152 cb
.cb_type
= POOL_SCAN_SCRUB
;
6153 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6156 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
6159 cb
.cb_type
= POOL_SCAN_NONE
;
6162 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
6165 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6171 if (cb
.cb_type
== POOL_SCAN_NONE
&&
6172 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
6173 (void) fprintf(stderr
, gettext("invalid option combination: "
6174 "-s and -p are mutually exclusive\n"));
6184 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6188 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
6192 * Print out detailed scrub status.
6195 print_scan_status(pool_scan_stat_t
*ps
)
6197 time_t start
, end
, pause
;
6198 uint64_t total_secs_left
;
6199 uint64_t elapsed
, secs_left
, mins_left
, hours_left
, days_left
;
6200 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
6201 uint64_t scan_rate
, issue_rate
;
6202 double fraction_done
;
6203 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
6204 char srate_buf
[7], irate_buf
[7];
6206 (void) printf(gettext(" scan: "));
6208 /* If there's never been a scan, there's not much to say. */
6209 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
6210 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
6211 (void) printf(gettext("none requested\n"));
6215 start
= ps
->pss_start_time
;
6216 end
= ps
->pss_end_time
;
6217 pause
= ps
->pss_pass_scrub_pause
;
6219 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
6221 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
6222 ps
->pss_func
== POOL_SCAN_RESILVER
);
6224 /* Scan is finished or canceled. */
6225 if (ps
->pss_state
== DSS_FINISHED
) {
6226 total_secs_left
= end
- start
;
6227 days_left
= total_secs_left
/ 60 / 60 / 24;
6228 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6229 mins_left
= (total_secs_left
/ 60) % 60;
6230 secs_left
= (total_secs_left
% 60);
6232 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6233 (void) printf(gettext("scrub repaired %s "
6234 "in %llu days %02llu:%02llu:%02llu "
6235 "with %llu errors on %s"), processed_buf
,
6236 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6237 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6238 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6239 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6240 (void) printf(gettext("resilvered %s "
6241 "in %llu days %02llu:%02llu:%02llu "
6242 "with %llu errors on %s"), processed_buf
,
6243 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6244 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6245 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6248 } else if (ps
->pss_state
== DSS_CANCELED
) {
6249 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6250 (void) printf(gettext("scrub canceled on %s"),
6252 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6253 (void) printf(gettext("resilver canceled on %s"),
6259 assert(ps
->pss_state
== DSS_SCANNING
);
6261 /* Scan is in progress. Resilvers can't be paused. */
6262 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6264 (void) printf(gettext("scrub in progress since %s"),
6267 (void) printf(gettext("scrub paused since %s"),
6269 (void) printf(gettext("\tscrub started on %s"),
6272 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6273 (void) printf(gettext("resilver in progress since %s"),
6277 scanned
= ps
->pss_examined
;
6278 pass_scanned
= ps
->pss_pass_exam
;
6279 issued
= ps
->pss_issued
;
6280 pass_issued
= ps
->pss_pass_issued
;
6281 total
= ps
->pss_to_examine
;
6283 /* we are only done with a block once we have issued the IO for it */
6284 fraction_done
= (double)issued
/ total
;
6286 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6287 elapsed
= time(NULL
) - ps
->pss_pass_start
;
6288 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
6289 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
6291 scan_rate
= pass_scanned
/ elapsed
;
6292 issue_rate
= pass_issued
/ elapsed
;
6293 total_secs_left
= (issue_rate
!= 0) ?
6294 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
6296 days_left
= total_secs_left
/ 60 / 60 / 24;
6297 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6298 mins_left
= (total_secs_left
/ 60) % 60;
6299 secs_left
= (total_secs_left
% 60);
6301 /* format all of the numbers we will be reporting */
6302 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
6303 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
6304 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
6305 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
6306 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
6308 /* do not print estimated time if we have a paused scrub */
6310 (void) printf(gettext("\t%s scanned at %s/s, "
6311 "%s issued at %s/s, %s total\n"),
6312 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
6314 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6315 scanned_buf
, issued_buf
, total_buf
);
6318 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6319 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6320 processed_buf
, 100 * fraction_done
);
6321 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6322 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6323 processed_buf
, 100 * fraction_done
);
6327 if (issue_rate
>= 10 * 1024 * 1024) {
6328 (void) printf(gettext(", %llu days "
6329 "%02llu:%02llu:%02llu to go\n"),
6330 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6331 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
);
6333 (void) printf(gettext(", no estimated "
6334 "completion time\n"));
6337 (void) printf(gettext("\n"));
6342 * As we don't scrub checkpointed blocks, we want to warn the
6343 * user that we skipped scanning some blocks if a checkpoint exists
6344 * or existed at any time during the scan.
6347 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
6349 if (ps
== NULL
|| pcs
== NULL
)
6352 if (pcs
->pcs_state
== CS_NONE
||
6353 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
6356 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
6358 if (ps
->pss_state
== DSS_NONE
)
6361 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
6362 ps
->pss_end_time
< pcs
->pcs_start_time
)
6365 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
6366 (void) printf(gettext(" scan warning: skipped blocks "
6367 "that are only referenced by the checkpoint.\n"));
6369 assert(ps
->pss_state
== DSS_SCANNING
);
6370 (void) printf(gettext(" scan warning: skipping blocks "
6371 "that are only referenced by the checkpoint.\n"));
6376 * Print out detailed removal status.
6379 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
6381 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
6383 nvlist_t
*config
, *nvroot
;
6388 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
6392 * Determine name of vdev.
6394 config
= zpool_get_config(zhp
, NULL
);
6395 nvroot
= fnvlist_lookup_nvlist(config
,
6396 ZPOOL_CONFIG_VDEV_TREE
);
6397 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
6398 &child
, &children
) == 0);
6399 assert(prs
->prs_removing_vdev
< children
);
6400 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
6401 child
[prs
->prs_removing_vdev
], B_TRUE
);
6403 (void) printf(gettext("remove: "));
6405 start
= prs
->prs_start_time
;
6406 end
= prs
->prs_end_time
;
6407 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
6410 * Removal is finished or canceled.
6412 if (prs
->prs_state
== DSS_FINISHED
) {
6413 uint64_t minutes_taken
= (end
- start
) / 60;
6415 (void) printf(gettext("Removal of vdev %llu copied %s "
6416 "in %lluh%um, completed on %s"),
6417 (longlong_t
)prs
->prs_removing_vdev
,
6419 (u_longlong_t
)(minutes_taken
/ 60),
6420 (uint_t
)(minutes_taken
% 60),
6421 ctime((time_t *)&end
));
6422 } else if (prs
->prs_state
== DSS_CANCELED
) {
6423 (void) printf(gettext("Removal of %s canceled on %s"),
6424 vdev_name
, ctime(&end
));
6426 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
6427 double fraction_done
;
6430 assert(prs
->prs_state
== DSS_SCANNING
);
6433 * Removal is in progress.
6435 (void) printf(gettext(
6436 "Evacuation of %s in progress since %s"),
6437 vdev_name
, ctime(&start
));
6439 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
6440 total
= prs
->prs_to_copy
;
6441 fraction_done
= (double)copied
/ total
;
6443 /* elapsed time for this pass */
6444 elapsed
= time(NULL
) - prs
->prs_start_time
;
6445 elapsed
= elapsed
> 0 ? elapsed
: 1;
6446 rate
= copied
/ elapsed
;
6447 rate
= rate
> 0 ? rate
: 1;
6448 mins_left
= ((total
- copied
) / rate
) / 60;
6449 hours_left
= mins_left
/ 60;
6451 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
6452 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
6453 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
6456 * do not print estimated time if hours_left is more than
6459 (void) printf(gettext(" %s copied out of %s at %s/s, "
6461 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
6462 if (hours_left
< (30 * 24)) {
6463 (void) printf(gettext(", %lluh%um to go\n"),
6464 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
6466 (void) printf(gettext(
6467 ", (copy is slow, no estimated time)\n"));
6471 if (prs
->prs_mapping_memory
> 0) {
6473 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
6474 (void) printf(gettext(" %s memory used for "
6475 "removed device mappings\n"),
6481 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
6486 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6489 (void) printf(gettext("checkpoint: "));
6491 start
= pcs
->pcs_start_time
;
6492 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
6494 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
6495 char *date
= ctime(&start
);
6498 * ctime() adds a newline at the end of the generated
6499 * string, thus the weird format specifier and the
6500 * strlen() call used to chop it off from the output.
6502 (void) printf(gettext("created %.*s, consumes %s\n"),
6503 (int)(strlen(date
) - 1), date
, space_buf
);
6507 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6509 (void) printf(gettext("discarding, %s remaining.\n"),
6514 print_error_log(zpool_handle_t
*zhp
)
6516 nvlist_t
*nverrlist
= NULL
;
6519 size_t len
= MAXPATHLEN
* 2;
6521 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
6524 (void) printf("errors: Permanent errors have been "
6525 "detected in the following files:\n\n");
6527 pathname
= safe_malloc(len
);
6529 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
6531 uint64_t dsobj
, obj
;
6533 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
6534 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
6536 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
6538 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
6539 (void) printf("%7s %s\n", "", pathname
);
6542 nvlist_free(nverrlist
);
6546 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
6555 (void) printf(gettext("\tspares\n"));
6557 for (i
= 0; i
< nspares
; i
++) {
6558 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
6560 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
6566 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
6575 (void) printf(gettext("\tcache\n"));
6577 for (i
= 0; i
< nl2cache
; i
++) {
6578 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
6580 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
6586 print_dedup_stats(nvlist_t
*config
)
6588 ddt_histogram_t
*ddh
;
6592 char dspace
[6], mspace
[6];
6595 * If the pool was faulted then we may not have been able to
6596 * obtain the config. Otherwise, if we have anything in the dedup
6597 * table continue processing the stats.
6599 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
6600 (uint64_t **)&ddo
, &c
) != 0)
6603 (void) printf("\n");
6604 (void) printf(gettext(" dedup: "));
6605 if (ddo
->ddo_count
== 0) {
6606 (void) printf(gettext("no DDT entries\n"));
6610 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
6611 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
6612 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6613 (u_longlong_t
)ddo
->ddo_count
,
6617 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
6618 (uint64_t **)&dds
, &c
) == 0);
6619 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
6620 (uint64_t **)&ddh
, &c
) == 0);
6621 zpool_dump_ddt(dds
, ddh
);
6625 * Display a summary of pool status. Displays a summary such as:
6629 * reason: One or more devices ...
6630 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6636 * When given the '-v' option, we print out the complete config. If the '-e'
6637 * option is specified, then we print out error rate information as well.
6640 status_callback(zpool_handle_t
*zhp
, void *data
)
6642 status_cbdata_t
*cbp
= data
;
6643 nvlist_t
*config
, *nvroot
;
6645 zpool_status_t reason
;
6646 zpool_errata_t errata
;
6651 config
= zpool_get_config(zhp
, NULL
);
6652 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
6657 * If we were given 'zpool status -x', only report those pools with
6660 if (cbp
->cb_explain
&&
6661 (reason
== ZPOOL_STATUS_OK
||
6662 reason
== ZPOOL_STATUS_VERSION_OLDER
||
6663 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6664 if (!cbp
->cb_allpools
) {
6665 (void) printf(gettext("pool '%s' is healthy\n"),
6666 zpool_get_name(zhp
));
6668 cbp
->cb_first
= B_FALSE
;
6674 cbp
->cb_first
= B_FALSE
;
6676 (void) printf("\n");
6678 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6679 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6680 (uint64_t **)&vs
, &c
) == 0);
6682 health
= zpool_get_state_str(zhp
);
6684 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6685 (void) printf(gettext(" state: %s\n"), health
);
6688 case ZPOOL_STATUS_MISSING_DEV_R
:
6689 (void) printf(gettext("status: One or more devices could not "
6690 "be opened. Sufficient replicas exist for\n\tthe pool to "
6691 "continue functioning in a degraded state.\n"));
6692 (void) printf(gettext("action: Attach the missing device and "
6693 "online it using 'zpool online'.\n"));
6696 case ZPOOL_STATUS_MISSING_DEV_NR
:
6697 (void) printf(gettext("status: One or more devices could not "
6698 "be opened. There are insufficient\n\treplicas for the "
6699 "pool to continue functioning.\n"));
6700 (void) printf(gettext("action: Attach the missing device and "
6701 "online it using 'zpool online'.\n"));
6704 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6705 (void) printf(gettext("status: One or more devices could not "
6706 "be used because the label is missing or\n\tinvalid. "
6707 "Sufficient replicas exist for the pool to continue\n\t"
6708 "functioning in a degraded state.\n"));
6709 (void) printf(gettext("action: Replace the device using "
6710 "'zpool replace'.\n"));
6713 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
6714 (void) printf(gettext("status: One or more devices could not "
6715 "be used because the label is missing \n\tor invalid. "
6716 "There are insufficient replicas for the pool to "
6717 "continue\n\tfunctioning.\n"));
6718 zpool_explain_recover(zpool_get_handle(zhp
),
6719 zpool_get_name(zhp
), reason
, config
);
6722 case ZPOOL_STATUS_FAILING_DEV
:
6723 (void) printf(gettext("status: One or more devices has "
6724 "experienced an unrecoverable error. An\n\tattempt was "
6725 "made to correct the error. Applications are "
6727 (void) printf(gettext("action: Determine if the device needs "
6728 "to be replaced, and clear the errors\n\tusing "
6729 "'zpool clear' or replace the device with 'zpool "
6733 case ZPOOL_STATUS_OFFLINE_DEV
:
6734 (void) printf(gettext("status: One or more devices has "
6735 "been taken offline by the administrator.\n\tSufficient "
6736 "replicas exist for the pool to continue functioning in "
6737 "a\n\tdegraded state.\n"));
6738 (void) printf(gettext("action: Online the device using "
6739 "'zpool online' or replace the device with\n\t'zpool "
6743 case ZPOOL_STATUS_REMOVED_DEV
:
6744 (void) printf(gettext("status: One or more devices has "
6745 "been removed by the administrator.\n\tSufficient "
6746 "replicas exist for the pool to continue functioning in "
6747 "a\n\tdegraded state.\n"));
6748 (void) printf(gettext("action: Online the device using "
6749 "'zpool online' or replace the device with\n\t'zpool "
6753 case ZPOOL_STATUS_RESILVERING
:
6754 (void) printf(gettext("status: One or more devices is "
6755 "currently being resilvered. The pool will\n\tcontinue "
6756 "to function, possibly in a degraded state.\n"));
6757 (void) printf(gettext("action: Wait for the resilver to "
6761 case ZPOOL_STATUS_CORRUPT_DATA
:
6762 (void) printf(gettext("status: One or more devices has "
6763 "experienced an error resulting in data\n\tcorruption. "
6764 "Applications may be affected.\n"));
6765 (void) printf(gettext("action: Restore the file in question "
6766 "if possible. Otherwise restore the\n\tentire pool from "
6770 case ZPOOL_STATUS_CORRUPT_POOL
:
6771 (void) printf(gettext("status: The pool metadata is corrupted "
6772 "and the pool cannot be opened.\n"));
6773 zpool_explain_recover(zpool_get_handle(zhp
),
6774 zpool_get_name(zhp
), reason
, config
);
6777 case ZPOOL_STATUS_VERSION_OLDER
:
6778 (void) printf(gettext("status: The pool is formatted using a "
6779 "legacy on-disk format. The pool can\n\tstill be used, "
6780 "but some features are unavailable.\n"));
6781 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6782 "upgrade'. Once this is done, the\n\tpool will no longer "
6783 "be accessible on software that does not support\n\t"
6784 "feature flags.\n"));
6787 case ZPOOL_STATUS_VERSION_NEWER
:
6788 (void) printf(gettext("status: The pool has been upgraded to a "
6789 "newer, incompatible on-disk version.\n\tThe pool cannot "
6790 "be accessed on this system.\n"));
6791 (void) printf(gettext("action: Access the pool from a system "
6792 "running more recent software, or\n\trestore the pool from "
6796 case ZPOOL_STATUS_FEAT_DISABLED
:
6797 (void) printf(gettext("status: Some supported features are not "
6798 "enabled on the pool. The pool can\n\tstill be used, but "
6799 "some features are unavailable.\n"));
6800 (void) printf(gettext("action: Enable all features using "
6801 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6802 "longer be accessible by software that does not support\n\t"
6803 "the features. See zpool-features(5) for details.\n"));
6806 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
6807 (void) printf(gettext("status: The pool cannot be accessed on "
6808 "this system because it uses the\n\tfollowing feature(s) "
6809 "not supported on this system:\n"));
6810 zpool_print_unsup_feat(config
);
6811 (void) printf("\n");
6812 (void) printf(gettext("action: Access the pool from a system "
6813 "that supports the required feature(s),\n\tor restore the "
6814 "pool from backup.\n"));
6817 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
6818 (void) printf(gettext("status: The pool can only be accessed "
6819 "in read-only mode on this system. It\n\tcannot be "
6820 "accessed in read-write mode because it uses the "
6821 "following\n\tfeature(s) not supported on this system:\n"));
6822 zpool_print_unsup_feat(config
);
6823 (void) printf("\n");
6824 (void) printf(gettext("action: The pool cannot be accessed in "
6825 "read-write mode. Import the pool with\n"
6826 "\t\"-o readonly=on\", access the pool from a system that "
6827 "supports the\n\trequired feature(s), or restore the "
6828 "pool from backup.\n"));
6831 case ZPOOL_STATUS_FAULTED_DEV_R
:
6832 (void) printf(gettext("status: One or more devices are "
6833 "faulted in response to persistent errors.\n\tSufficient "
6834 "replicas exist for the pool to continue functioning "
6835 "in a\n\tdegraded state.\n"));
6836 (void) printf(gettext("action: Replace the faulted device, "
6837 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6840 case ZPOOL_STATUS_FAULTED_DEV_NR
:
6841 (void) printf(gettext("status: One or more devices are "
6842 "faulted in response to persistent errors. There are "
6843 "insufficient replicas for the pool to\n\tcontinue "
6845 (void) printf(gettext("action: Destroy and re-create the pool "
6846 "from a backup source. Manually marking the device\n"
6847 "\trepaired using 'zpool clear' may allow some data "
6848 "to be recovered.\n"));
6851 case ZPOOL_STATUS_IO_FAILURE_MMP
:
6852 (void) printf(gettext("status: The pool is suspended because "
6853 "multihost writes failed or were delayed;\n\tanother "
6854 "system could import the pool undetected.\n"));
6855 (void) printf(gettext("action: Make sure the pool's devices "
6856 "are connected, then reboot your system and\n\timport the "
6860 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
6861 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
6862 (void) printf(gettext("status: One or more devices are "
6863 "faulted in response to IO failures.\n"));
6864 (void) printf(gettext("action: Make sure the affected devices "
6865 "are connected, then run 'zpool clear'.\n"));
6868 case ZPOOL_STATUS_BAD_LOG
:
6869 (void) printf(gettext("status: An intent log record "
6870 "could not be read.\n"
6871 "\tWaiting for administrator intervention to fix the "
6872 "faulted pool.\n"));
6873 (void) printf(gettext("action: Either restore the affected "
6874 "device(s) and run 'zpool online',\n"
6875 "\tor ignore the intent log records by running "
6876 "'zpool clear'.\n"));
6879 case ZPOOL_STATUS_HOSTID_MISMATCH
:
6880 (void) printf(gettext("status: Mismatch between pool hostid "
6881 "and system hostid on imported pool.\n\tThis pool was "
6882 "previously imported into a system with a different "
6883 "hostid,\n\tand then was verbatim imported into this "
6885 (void) printf(gettext("action: Export this pool on all systems "
6886 "on which it is imported.\n"
6887 "\tThen import it to correct the mismatch.\n"));
6890 case ZPOOL_STATUS_ERRATA
:
6891 (void) printf(gettext("status: Errata #%d detected.\n"),
6895 case ZPOOL_ERRATA_NONE
:
6898 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
6899 (void) printf(gettext("action: To correct the issue "
6900 "run 'zpool scrub'.\n"));
6903 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
6904 (void) printf(gettext("\tExisting encrypted datasets "
6905 "contain an on-disk incompatibility\n\twhich "
6906 "needs to be corrected.\n"));
6907 (void) printf(gettext("action: To correct the issue "
6908 "backup existing encrypted datasets to new\n\t"
6909 "encrypted datasets and destroy the old ones. "
6910 "'zfs mount -o ro' can\n\tbe used to temporarily "
6911 "mount existing encrypted datasets readonly.\n"));
6916 * All errata which allow the pool to be imported
6917 * must contain an action message.
6925 * The remaining errors can't actually be generated, yet.
6927 assert(reason
== ZPOOL_STATUS_OK
);
6931 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6934 if (config
!= NULL
) {
6936 nvlist_t
**spares
, **l2cache
;
6937 uint_t nspares
, nl2cache
;
6938 pool_checkpoint_stat_t
*pcs
= NULL
;
6939 pool_scan_stat_t
*ps
= NULL
;
6940 pool_removal_stat_t
*prs
= NULL
;
6942 (void) nvlist_lookup_uint64_array(nvroot
,
6943 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6944 (void) nvlist_lookup_uint64_array(nvroot
,
6945 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6946 (void) nvlist_lookup_uint64_array(nvroot
,
6947 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
6949 print_scan_status(ps
);
6950 print_checkpoint_scan_warning(ps
, pcs
);
6951 print_removal_status(zhp
, prs
);
6952 print_checkpoint_status(pcs
);
6954 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6955 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6956 if (cbp
->cb_namewidth
< 10)
6957 cbp
->cb_namewidth
= 10;
6959 (void) printf(gettext("config:\n\n"));
6960 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6961 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6964 if (cbp
->vcdl
!= NULL
)
6965 print_cmd_columns(cbp
->vcdl
, 0);
6968 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6971 if (num_logs(nvroot
) > 0)
6972 print_logs(zhp
, cbp
, nvroot
);
6973 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6974 &l2cache
, &nl2cache
) == 0)
6975 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6977 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6978 &spares
, &nspares
) == 0)
6979 print_spares(zhp
, cbp
, spares
, nspares
);
6981 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6983 nvlist_t
*nverrlist
= NULL
;
6986 * If the approximate error count is small, get a
6987 * precise count by fetching the entire log and
6988 * uniquifying the results.
6990 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6991 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6996 while ((elem
= nvlist_next_nvpair(nverrlist
,
7001 nvlist_free(nverrlist
);
7003 (void) printf("\n");
7006 (void) printf(gettext("errors: No known data "
7008 else if (!cbp
->cb_verbose
)
7009 (void) printf(gettext("errors: %llu data "
7010 "errors, use '-v' for a list\n"),
7011 (u_longlong_t
)nerr
);
7013 print_error_log(zhp
);
7016 if (cbp
->cb_dedup_stats
)
7017 print_dedup_stats(config
);
7019 (void) printf(gettext("config: The configuration cannot be "
7027 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
7028 * [interval [count]]
7030 * -c CMD For each vdev, run command CMD
7031 * -g Display guid for individual vdev name.
7032 * -L Follow links when resolving vdev path name.
7033 * -P Display full path for vdev name.
7034 * -v Display complete error logs
7035 * -x Display only pools with potential problems
7036 * -D Display dedup status (undocumented)
7037 * -T Display a timestamp in date(1) or Unix format
7039 * Describes the health status of all pools or some subset.
7042 zpool_do_status(int argc
, char **argv
)
7047 unsigned long count
= 0;
7048 status_cbdata_t cb
= { 0 };
7052 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
7057 gettext("Can't set -c flag twice\n"));
7061 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
7062 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
7063 fprintf(stderr
, gettext(
7064 "Can't run -c, disabled by "
7065 "ZPOOL_SCRIPTS_ENABLED.\n"));
7069 if ((getuid() <= 0 || geteuid() <= 0) &&
7070 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
7071 fprintf(stderr
, gettext(
7072 "Can't run -c with root privileges "
7073 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
7079 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
7082 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
7085 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
7088 cb
.cb_verbose
= B_TRUE
;
7091 cb
.cb_explain
= B_TRUE
;
7094 cb
.cb_dedup_stats
= B_TRUE
;
7097 get_timestamp_arg(*optarg
);
7100 if (optopt
== 'c') {
7101 print_zpool_script_list("status");
7105 gettext("invalid option '%c'\n"), optopt
);
7114 get_interval_count(&argc
, argv
, &interval
, &count
);
7117 cb
.cb_allpools
= B_TRUE
;
7119 cb
.cb_first
= B_TRUE
;
7120 cb
.cb_print_status
= B_TRUE
;
7123 if (timestamp_fmt
!= NODATE
)
7124 print_timestamp(timestamp_fmt
);
7127 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
7130 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
7131 status_callback
, &cb
);
7133 if (cb
.vcdl
!= NULL
)
7134 free_vdev_cmd_data_list(cb
.vcdl
);
7136 if (argc
== 0 && cb
.cb_count
== 0)
7137 (void) fprintf(stderr
, gettext("no pools available\n"));
7138 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
7139 (void) printf(gettext("all pools are healthy\n"));
7147 if (count
!= 0 && --count
== 0)
7150 (void) fsleep(interval
);
7156 typedef struct upgrade_cbdata
{
7159 uint64_t cb_version
;
7164 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
7166 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
7167 int *count
= (int *)unsupp_fs
;
7169 if (zfs_version
> ZPL_VERSION
) {
7170 (void) printf(gettext("%s (v%d) is not supported by this "
7171 "implementation of ZFS.\n"),
7172 zfs_get_name(zhp
), zfs_version
);
7176 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
7184 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
7188 uint64_t oldversion
;
7191 config
= zpool_get_config(zhp
, NULL
);
7192 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7195 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
7196 assert(oldversion
< version
);
7198 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
7203 (void) fprintf(stderr
, gettext("Upgrade not performed due "
7204 "to %d unsupported filesystems (max v%d).\n"),
7205 unsupp_fs
, (int)ZPL_VERSION
);
7209 ret
= zpool_upgrade(zhp
, version
);
7213 if (version
>= SPA_VERSION_FEATURES
) {
7214 (void) printf(gettext("Successfully upgraded "
7215 "'%s' from version %llu to feature flags.\n"),
7216 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
7218 (void) printf(gettext("Successfully upgraded "
7219 "'%s' from version %llu to version %llu.\n"),
7220 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
7221 (u_longlong_t
)version
);
7228 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
7231 boolean_t firstff
= B_TRUE
;
7232 nvlist_t
*enabled
= zpool_get_features(zhp
);
7235 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7236 const char *fname
= spa_feature_table
[i
].fi_uname
;
7237 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7238 if (!nvlist_exists(enabled
, fguid
)) {
7240 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
7241 ret
= zpool_set_prop(zhp
, propname
,
7242 ZFS_FEATURE_ENABLED
);
7250 (void) printf(gettext("Enabled the "
7251 "following features on '%s':\n"),
7252 zpool_get_name(zhp
));
7255 (void) printf(gettext(" %s\n"), fname
);
7266 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
7268 upgrade_cbdata_t
*cbp
= arg
;
7271 boolean_t printnl
= B_FALSE
;
7274 config
= zpool_get_config(zhp
, NULL
);
7275 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7278 assert(SPA_VERSION_IS_SUPPORTED(version
));
7280 if (version
< cbp
->cb_version
) {
7281 cbp
->cb_first
= B_FALSE
;
7282 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7288 * If they did "zpool upgrade -a", then we could
7289 * be doing ioctls to different pools. We need
7290 * to log this history once to each pool, and bypass
7291 * the normal history logging that happens in main().
7293 (void) zpool_log_history(g_zfs
, history_str
);
7294 log_history
= B_FALSE
;
7297 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7299 ret
= upgrade_enable_all(zhp
, &count
);
7304 cbp
->cb_first
= B_FALSE
;
7310 (void) printf(gettext("\n"));
7317 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
7319 upgrade_cbdata_t
*cbp
= arg
;
7323 config
= zpool_get_config(zhp
, NULL
);
7324 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7327 assert(SPA_VERSION_IS_SUPPORTED(version
));
7329 if (version
< SPA_VERSION_FEATURES
) {
7330 if (cbp
->cb_first
) {
7331 (void) printf(gettext("The following pools are "
7332 "formatted with legacy version numbers and can\n"
7333 "be upgraded to use feature flags. After "
7334 "being upgraded, these pools\nwill no "
7335 "longer be accessible by software that does not "
7336 "support feature\nflags.\n\n"));
7337 (void) printf(gettext("VER POOL\n"));
7338 (void) printf(gettext("--- ------------\n"));
7339 cbp
->cb_first
= B_FALSE
;
7342 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
7343 zpool_get_name(zhp
));
7350 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
7352 upgrade_cbdata_t
*cbp
= arg
;
7356 config
= zpool_get_config(zhp
, NULL
);
7357 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7360 if (version
>= SPA_VERSION_FEATURES
) {
7362 boolean_t poolfirst
= B_TRUE
;
7363 nvlist_t
*enabled
= zpool_get_features(zhp
);
7365 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7366 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7367 const char *fname
= spa_feature_table
[i
].fi_uname
;
7368 if (!nvlist_exists(enabled
, fguid
)) {
7369 if (cbp
->cb_first
) {
7370 (void) printf(gettext("\nSome "
7371 "supported features are not "
7372 "enabled on the following pools. "
7373 "Once a\nfeature is enabled the "
7374 "pool may become incompatible with "
7375 "software\nthat does not support "
7377 "zpool-features(5) for "
7379 (void) printf(gettext("POOL "
7381 (void) printf(gettext("------"
7383 cbp
->cb_first
= B_FALSE
;
7387 (void) printf(gettext("%s\n"),
7388 zpool_get_name(zhp
));
7389 poolfirst
= B_FALSE
;
7392 (void) printf(gettext(" %s\n"), fname
);
7395 * If they did "zpool upgrade -a", then we could
7396 * be doing ioctls to different pools. We need
7397 * to log this history once to each pool, and bypass
7398 * the normal history logging that happens in main().
7400 (void) zpool_log_history(g_zfs
, history_str
);
7401 log_history
= B_FALSE
;
7410 upgrade_one(zpool_handle_t
*zhp
, void *data
)
7412 boolean_t printnl
= B_FALSE
;
7413 upgrade_cbdata_t
*cbp
= data
;
7414 uint64_t cur_version
;
7417 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
7418 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
7419 "Pool 'log' must be renamed using export and import"
7424 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
7425 if (cur_version
> cbp
->cb_version
) {
7426 (void) printf(gettext("Pool '%s' is already formatted "
7427 "using more current version '%llu'.\n\n"),
7428 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
7432 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
7433 (void) printf(gettext("Pool '%s' is already formatted "
7434 "using version %llu.\n\n"), zpool_get_name(zhp
),
7435 (u_longlong_t
)cbp
->cb_version
);
7439 if (cur_version
!= cbp
->cb_version
) {
7441 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7446 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7448 ret
= upgrade_enable_all(zhp
, &count
);
7454 } else if (cur_version
== SPA_VERSION
) {
7455 (void) printf(gettext("Pool '%s' already has all "
7456 "supported features enabled.\n"),
7457 zpool_get_name(zhp
));
7462 (void) printf(gettext("\n"));
7471 * zpool upgrade [-V version] <-a | pool ...>
7473 * With no arguments, display downrev'd ZFS pool available for upgrade.
7474 * Individual pools can be upgraded by specifying the pool, and '-a' will
7475 * upgrade all pools.
7478 zpool_do_upgrade(int argc
, char **argv
)
7481 upgrade_cbdata_t cb
= { 0 };
7483 boolean_t showversions
= B_FALSE
;
7484 boolean_t upgradeall
= B_FALSE
;
7489 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
7492 upgradeall
= B_TRUE
;
7495 showversions
= B_TRUE
;
7498 cb
.cb_version
= strtoll(optarg
, &end
, 10);
7500 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
7501 (void) fprintf(stderr
,
7502 gettext("invalid version '%s'\n"), optarg
);
7507 (void) fprintf(stderr
, gettext("missing argument for "
7508 "'%c' option\n"), optopt
);
7512 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7523 if (cb
.cb_version
== 0) {
7524 cb
.cb_version
= SPA_VERSION
;
7525 } else if (!upgradeall
&& argc
== 0) {
7526 (void) fprintf(stderr
, gettext("-V option is "
7527 "incompatible with other arguments\n"));
7532 if (upgradeall
|| argc
!= 0) {
7533 (void) fprintf(stderr
, gettext("-v option is "
7534 "incompatible with other arguments\n"));
7537 } else if (upgradeall
) {
7539 (void) fprintf(stderr
, gettext("-a option should not "
7540 "be used along with a pool name\n"));
7545 (void) printf(gettext("This system supports ZFS pool feature "
7550 (void) printf(gettext("The following features are "
7552 (void) printf(gettext("FEAT DESCRIPTION\n"));
7553 (void) printf("----------------------------------------------"
7554 "---------------\n");
7555 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7556 zfeature_info_t
*fi
= &spa_feature_table
[i
];
7558 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
7559 " (read-only compatible)" : "";
7561 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
7562 (void) printf(" %s\n", fi
->fi_desc
);
7564 (void) printf("\n");
7566 (void) printf(gettext("The following legacy versions are also "
7568 (void) printf(gettext("VER DESCRIPTION\n"));
7569 (void) printf("--- -----------------------------------------"
7570 "---------------\n");
7571 (void) printf(gettext(" 1 Initial ZFS version\n"));
7572 (void) printf(gettext(" 2 Ditto blocks "
7573 "(replicated metadata)\n"));
7574 (void) printf(gettext(" 3 Hot spares and double parity "
7576 (void) printf(gettext(" 4 zpool history\n"));
7577 (void) printf(gettext(" 5 Compression using the gzip "
7579 (void) printf(gettext(" 6 bootfs pool property\n"));
7580 (void) printf(gettext(" 7 Separate intent log devices\n"));
7581 (void) printf(gettext(" 8 Delegated administration\n"));
7582 (void) printf(gettext(" 9 refquota and refreservation "
7584 (void) printf(gettext(" 10 Cache devices\n"));
7585 (void) printf(gettext(" 11 Improved scrub performance\n"));
7586 (void) printf(gettext(" 12 Snapshot properties\n"));
7587 (void) printf(gettext(" 13 snapused property\n"));
7588 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7589 (void) printf(gettext(" 15 user/group space accounting\n"));
7590 (void) printf(gettext(" 16 stmf property support\n"));
7591 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7592 (void) printf(gettext(" 18 Snapshot user holds\n"));
7593 (void) printf(gettext(" 19 Log device removal\n"));
7594 (void) printf(gettext(" 20 Compression using zle "
7595 "(zero-length encoding)\n"));
7596 (void) printf(gettext(" 21 Deduplication\n"));
7597 (void) printf(gettext(" 22 Received properties\n"));
7598 (void) printf(gettext(" 23 Slim ZIL\n"));
7599 (void) printf(gettext(" 24 System attributes\n"));
7600 (void) printf(gettext(" 25 Improved scrub stats\n"));
7601 (void) printf(gettext(" 26 Improved snapshot deletion "
7603 (void) printf(gettext(" 27 Improved snapshot creation "
7605 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7606 (void) printf(gettext("\nFor more information on a particular "
7607 "version, including supported releases,\n"));
7608 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7609 } else if (argc
== 0 && upgradeall
) {
7610 cb
.cb_first
= B_TRUE
;
7611 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
7612 if (ret
== 0 && cb
.cb_first
) {
7613 if (cb
.cb_version
== SPA_VERSION
) {
7614 (void) printf(gettext("All pools are already "
7615 "formatted using feature flags.\n\n"));
7616 (void) printf(gettext("Every feature flags "
7617 "pool already has all supported features "
7620 (void) printf(gettext("All pools are already "
7621 "formatted with version %llu or higher.\n"),
7622 (u_longlong_t
)cb
.cb_version
);
7625 } else if (argc
== 0) {
7626 cb
.cb_first
= B_TRUE
;
7627 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
7631 (void) printf(gettext("All pools are formatted "
7632 "using feature flags.\n\n"));
7634 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7635 "for a list of available legacy versions.\n"));
7638 cb
.cb_first
= B_TRUE
;
7639 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
7643 (void) printf(gettext("Every feature flags pool has "
7644 "all supported features enabled.\n"));
7646 (void) printf(gettext("\n"));
7649 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
7656 typedef struct hist_cbdata
{
7663 * Print out the command history for a specific pool.
7666 get_history_one(zpool_handle_t
*zhp
, void *data
)
7672 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
7674 cb
->first
= B_FALSE
;
7676 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
7678 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
7681 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
7682 &records
, &numrecords
) == 0);
7683 for (i
= 0; i
< numrecords
; i
++) {
7684 nvlist_t
*rec
= records
[i
];
7687 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
7691 tsec
= fnvlist_lookup_uint64(records
[i
],
7693 (void) localtime_r(&tsec
, &t
);
7694 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
7697 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
7698 (void) printf("%s %s", tbuf
,
7699 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
7700 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
7702 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
7705 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
7706 (void) printf("%s unrecognized record:\n",
7708 dump_nvlist(rec
, 4);
7711 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
7712 zfs_history_event_names
[ievent
],
7713 (longlong_t
)fnvlist_lookup_uint64(
7714 rec
, ZPOOL_HIST_TXG
),
7715 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
7716 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
7719 (void) printf("%s [txg:%lld] %s", tbuf
,
7720 (longlong_t
)fnvlist_lookup_uint64(
7721 rec
, ZPOOL_HIST_TXG
),
7722 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
7723 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
7724 (void) printf(" %s (%llu)",
7725 fnvlist_lookup_string(rec
,
7727 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
7730 (void) printf(" %s", fnvlist_lookup_string(rec
,
7731 ZPOOL_HIST_INT_STR
));
7732 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
7735 (void) printf("%s ioctl %s\n", tbuf
,
7736 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
7737 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
7738 (void) printf(" input:\n");
7739 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7740 ZPOOL_HIST_INPUT_NVL
), 8);
7742 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
7743 (void) printf(" output:\n");
7744 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7745 ZPOOL_HIST_OUTPUT_NVL
), 8);
7747 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
7748 (void) printf(" errno: %lld\n",
7749 (longlong_t
)fnvlist_lookup_int64(rec
,
7755 (void) printf("%s unrecognized record:\n", tbuf
);
7756 dump_nvlist(rec
, 4);
7760 (void) printf("\n");
7763 (void) printf(" [");
7764 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
7765 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
7766 struct passwd
*pwd
= getpwuid(who
);
7767 (void) printf("user %d ", (int)who
);
7769 (void) printf("(%s) ", pwd
->pw_name
);
7771 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
7772 (void) printf("on %s",
7773 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
7775 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
7776 (void) printf(":%s",
7777 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
7781 (void) printf("\n");
7783 (void) printf("\n");
7790 * zpool history <pool>
7792 * Displays the history of commands that modified pools.
7795 zpool_do_history(int argc
, char **argv
)
7797 hist_cbdata_t cbdata
= { 0 };
7801 cbdata
.first
= B_TRUE
;
7803 while ((c
= getopt(argc
, argv
, "li")) != -1) {
7806 cbdata
.longfmt
= B_TRUE
;
7809 cbdata
.internal
= B_TRUE
;
7812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7820 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
7823 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
7824 (void) fprintf(stderr
, gettext("no pools available\n"));
7831 typedef struct ev_opts
{
7836 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
7840 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
7842 char ctime_str
[26], str
[32], *ptr
;
7846 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
7847 memset(str
, ' ', 32);
7848 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
7849 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
7850 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
7851 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
7852 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
7854 (void) printf(gettext("%s\t"), str
);
7856 (void) printf(gettext("%s "), str
);
7858 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
7859 (void) printf(gettext("%s\n"), ptr
);
7863 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
7867 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
7868 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
7870 data_type_t type
= nvpair_type(nvp
);
7871 const char *name
= nvpair_name(nvp
);
7881 printf(gettext("%*s%s = "), depth
, "", name
);
7884 case DATA_TYPE_BOOLEAN
:
7885 printf(gettext("%s"), "1");
7888 case DATA_TYPE_BOOLEAN_VALUE
:
7889 (void) nvpair_value_boolean_value(nvp
, &b
);
7890 printf(gettext("%s"), b
? "1" : "0");
7893 case DATA_TYPE_BYTE
:
7894 (void) nvpair_value_byte(nvp
, &i8
);
7895 printf(gettext("0x%x"), i8
);
7898 case DATA_TYPE_INT8
:
7899 (void) nvpair_value_int8(nvp
, (void *)&i8
);
7900 printf(gettext("0x%x"), i8
);
7903 case DATA_TYPE_UINT8
:
7904 (void) nvpair_value_uint8(nvp
, &i8
);
7905 printf(gettext("0x%x"), i8
);
7908 case DATA_TYPE_INT16
:
7909 (void) nvpair_value_int16(nvp
, (void *)&i16
);
7910 printf(gettext("0x%x"), i16
);
7913 case DATA_TYPE_UINT16
:
7914 (void) nvpair_value_uint16(nvp
, &i16
);
7915 printf(gettext("0x%x"), i16
);
7918 case DATA_TYPE_INT32
:
7919 (void) nvpair_value_int32(nvp
, (void *)&i32
);
7920 printf(gettext("0x%x"), i32
);
7923 case DATA_TYPE_UINT32
:
7924 (void) nvpair_value_uint32(nvp
, &i32
);
7925 printf(gettext("0x%x"), i32
);
7928 case DATA_TYPE_INT64
:
7929 (void) nvpair_value_int64(nvp
, (void *)&i64
);
7930 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7933 case DATA_TYPE_UINT64
:
7934 (void) nvpair_value_uint64(nvp
, &i64
);
7936 * translate vdev state values to readable
7937 * strings to aide zpool events consumers
7940 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
7942 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
7943 printf(gettext("\"%s\" (0x%llx)"),
7944 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
7947 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7951 case DATA_TYPE_HRTIME
:
7952 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
7953 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7956 case DATA_TYPE_STRING
:
7957 (void) nvpair_value_string(nvp
, &str
);
7958 printf(gettext("\"%s\""), str
? str
: "<NULL>");
7961 case DATA_TYPE_NVLIST
:
7962 printf(gettext("(embedded nvlist)\n"));
7963 (void) nvpair_value_nvlist(nvp
, &cnv
);
7964 zpool_do_events_nvprint(cnv
, depth
+ 8);
7965 printf(gettext("%*s(end %s)"), depth
, "", name
);
7968 case DATA_TYPE_NVLIST_ARRAY
: {
7972 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
7973 printf(gettext("(%d embedded nvlists)\n"), nelem
);
7974 for (i
= 0; i
< nelem
; i
++) {
7975 printf(gettext("%*s%s[%d] = %s\n"),
7976 depth
, "", name
, i
, "(embedded nvlist)");
7977 zpool_do_events_nvprint(val
[i
], depth
+ 8);
7978 printf(gettext("%*s(end %s[%i])\n"),
7979 depth
, "", name
, i
);
7981 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7985 case DATA_TYPE_INT8_ARRAY
: {
7989 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7990 for (i
= 0; i
< nelem
; i
++)
7991 printf(gettext("0x%x "), val
[i
]);
7996 case DATA_TYPE_UINT8_ARRAY
: {
8000 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
8001 for (i
= 0; i
< nelem
; i
++)
8002 printf(gettext("0x%x "), val
[i
]);
8007 case DATA_TYPE_INT16_ARRAY
: {
8011 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
8012 for (i
= 0; i
< nelem
; i
++)
8013 printf(gettext("0x%x "), val
[i
]);
8018 case DATA_TYPE_UINT16_ARRAY
: {
8022 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
8023 for (i
= 0; i
< nelem
; i
++)
8024 printf(gettext("0x%x "), val
[i
]);
8029 case DATA_TYPE_INT32_ARRAY
: {
8033 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
8034 for (i
= 0; i
< nelem
; i
++)
8035 printf(gettext("0x%x "), val
[i
]);
8040 case DATA_TYPE_UINT32_ARRAY
: {
8044 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
8045 for (i
= 0; i
< nelem
; i
++)
8046 printf(gettext("0x%x "), val
[i
]);
8051 case DATA_TYPE_INT64_ARRAY
: {
8055 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
8056 for (i
= 0; i
< nelem
; i
++)
8057 printf(gettext("0x%llx "),
8058 (u_longlong_t
)val
[i
]);
8063 case DATA_TYPE_UINT64_ARRAY
: {
8067 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
8068 for (i
= 0; i
< nelem
; i
++)
8069 printf(gettext("0x%llx "),
8070 (u_longlong_t
)val
[i
]);
8075 case DATA_TYPE_STRING_ARRAY
: {
8079 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
8080 for (i
= 0; i
< nelem
; i
++)
8081 printf(gettext("\"%s\" "),
8082 str
[i
] ? str
[i
] : "<NULL>");
8087 case DATA_TYPE_BOOLEAN_ARRAY
:
8088 case DATA_TYPE_BYTE_ARRAY
:
8089 case DATA_TYPE_DOUBLE
:
8090 case DATA_TYPE_DONTCARE
:
8091 case DATA_TYPE_UNKNOWN
:
8092 printf(gettext("<unknown>"));
8096 printf(gettext("\n"));
8101 zpool_do_events_next(ev_opts_t
*opts
)
8104 int zevent_fd
, ret
, dropped
;
8107 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
8108 VERIFY(zevent_fd
>= 0);
8110 if (!opts
->scripted
)
8111 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
8114 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
8115 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
8116 if (ret
|| nvl
== NULL
)
8120 (void) printf(gettext("dropped %d events\n"), dropped
);
8122 if (strlen(opts
->poolname
) > 0 &&
8123 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
8124 strcmp(opts
->poolname
, pool
) != 0)
8127 zpool_do_events_short(nvl
, opts
);
8129 if (opts
->verbose
) {
8130 zpool_do_events_nvprint(nvl
, 8);
8131 printf(gettext("\n"));
8133 (void) fflush(stdout
);
8138 VERIFY(0 == close(zevent_fd
));
8144 zpool_do_events_clear(ev_opts_t
*opts
)
8148 ret
= zpool_events_clear(g_zfs
, &count
);
8150 (void) printf(gettext("cleared %d events\n"), count
);
8156 * zpool events [-vHf [pool] | -c]
8158 * Displays events logs by ZFS.
8161 zpool_do_events(int argc
, char **argv
)
8163 ev_opts_t opts
= { 0 };
8168 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
8183 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8192 (void) fprintf(stderr
, gettext("too many arguments\n"));
8194 } else if (argc
== 1) {
8195 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
8196 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
8197 (void) fprintf(stderr
,
8198 gettext("invalid pool name '%s'\n"), opts
.poolname
);
8203 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
8205 (void) fprintf(stderr
,
8206 gettext("invalid options combined with -c\n"));
8211 ret
= zpool_do_events_clear(&opts
);
8213 ret
= zpool_do_events_next(&opts
);
8219 get_callback(zpool_handle_t
*zhp
, void *data
)
8221 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
8222 char value
[MAXNAMELEN
];
8223 zprop_source_t srctype
;
8226 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
8229 * Skip the special fake placeholder. This will also skip
8230 * over the name property when 'all' is specified.
8232 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
8233 pl
== cbp
->cb_proplist
)
8236 if (pl
->pl_prop
== ZPROP_INVAL
&&
8237 (zpool_prop_feature(pl
->pl_user_prop
) ||
8238 zpool_prop_unsupported(pl
->pl_user_prop
))) {
8239 srctype
= ZPROP_SRC_LOCAL
;
8241 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
8242 value
, sizeof (value
)) == 0) {
8243 zprop_print_one_property(zpool_get_name(zhp
),
8244 cbp
, pl
->pl_user_prop
, value
, srctype
,
8248 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
8249 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
8252 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
8253 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
8261 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8263 * -H Scripted mode. Don't display headers, and separate properties
8265 * -o List of columns to display. Defaults to
8266 * "name,property,value,source".
8267 * -p Display values in parsable (exact) format.
8269 * Get properties of pools in the system. Output space statistics
8270 * for each one as well as other attributes.
8273 zpool_do_get(int argc
, char **argv
)
8275 zprop_get_cbdata_t cb
= { 0 };
8276 zprop_list_t fake_name
= { 0 };
8281 cb
.cb_first
= B_TRUE
;
8284 * Set up default columns and sources.
8286 cb
.cb_sources
= ZPROP_SRC_ALL
;
8287 cb
.cb_columns
[0] = GET_COL_NAME
;
8288 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8289 cb
.cb_columns
[2] = GET_COL_VALUE
;
8290 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8291 cb
.cb_type
= ZFS_TYPE_POOL
;
8294 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
8297 cb
.cb_literal
= B_TRUE
;
8300 cb
.cb_scripted
= B_TRUE
;
8303 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
8305 while (*optarg
!= '\0') {
8306 static char *col_subopts
[] =
8307 { "name", "property", "value", "source",
8310 if (i
== ZFS_GET_NCOLS
) {
8311 (void) fprintf(stderr
, gettext("too "
8312 "many fields given to -o "
8317 switch (getsubopt(&optarg
, col_subopts
,
8320 cb
.cb_columns
[i
++] = GET_COL_NAME
;
8323 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
8326 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
8329 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
8333 (void) fprintf(stderr
,
8334 gettext("\"all\" conflicts "
8335 "with specific fields "
8336 "given to -o option\n"));
8339 cb
.cb_columns
[0] = GET_COL_NAME
;
8340 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8341 cb
.cb_columns
[2] = GET_COL_VALUE
;
8342 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8346 (void) fprintf(stderr
,
8347 gettext("invalid column name "
8354 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8364 (void) fprintf(stderr
, gettext("missing property "
8369 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
8370 ZFS_TYPE_POOL
) != 0)
8376 if (cb
.cb_proplist
!= NULL
) {
8377 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
8378 fake_name
.pl_width
= strlen(gettext("NAME"));
8379 fake_name
.pl_next
= cb
.cb_proplist
;
8380 cb
.cb_proplist
= &fake_name
;
8383 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
8386 if (cb
.cb_proplist
== &fake_name
)
8387 zprop_free_list(fake_name
.pl_next
);
8389 zprop_free_list(cb
.cb_proplist
);
8394 typedef struct set_cbdata
{
8397 boolean_t cb_any_successful
;
8401 set_callback(zpool_handle_t
*zhp
, void *data
)
8404 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
8406 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
8409 cb
->cb_any_successful
= B_TRUE
;
8415 zpool_do_set(int argc
, char **argv
)
8417 set_cbdata_t cb
= { 0 };
8420 if (argc
> 1 && argv
[1][0] == '-') {
8421 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8427 (void) fprintf(stderr
, gettext("missing property=value "
8433 (void) fprintf(stderr
, gettext("missing pool name\n"));
8438 (void) fprintf(stderr
, gettext("too many pool names\n"));
8442 cb
.cb_propname
= argv
[1];
8443 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
8444 if (cb
.cb_value
== NULL
) {
8445 (void) fprintf(stderr
, gettext("missing value in "
8446 "property=value argument\n"));
8450 *(cb
.cb_value
) = '\0';
8453 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
8460 find_command_idx(char *command
, int *idx
)
8464 for (i
= 0; i
< NCOMMAND
; i
++) {
8465 if (command_table
[i
].name
== NULL
)
8468 if (strcmp(command
, command_table
[i
].name
) == 0) {
8477 main(int argc
, char **argv
)
8483 (void) setlocale(LC_ALL
, "");
8484 (void) textdomain(TEXT_DOMAIN
);
8490 * Make sure the user has specified some command.
8493 (void) fprintf(stderr
, gettext("missing command\n"));
8502 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
8505 if ((g_zfs
= libzfs_init()) == NULL
) {
8506 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
8510 libzfs_print_on_error(g_zfs
, B_TRUE
);
8512 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8515 * Run the appropriate command.
8517 if (find_command_idx(cmdname
, &i
) == 0) {
8518 current_command
= &command_table
[i
];
8519 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
8520 } else if (strchr(cmdname
, '=')) {
8521 verify(find_command_idx("set", &i
) == 0);
8522 current_command
= &command_table
[i
];
8523 ret
= command_table
[i
].func(argc
, argv
);
8524 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
8526 * 'freeze' is a vile debugging abomination, so we treat
8529 zfs_cmd_t zc
= {"\0"};
8531 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
8532 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
8534 (void) fprintf(stderr
,
8535 gettext("failed to freeze pool: %d\n"), errno
);
8541 (void) fprintf(stderr
, gettext("unrecognized "
8542 "command '%s'\n"), cmdname
);
8547 if (ret
== 0 && log_history
)
8548 (void) zpool_log_history(g_zfs
, history_str
);
8553 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8554 * for the purposes of running ::findleaks.
8556 if (getenv("ZFS_ABORT") != NULL
) {
8557 (void) printf("dumping core by request\n");