4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
54 #include <sys/fs/zfs.h>
56 #include <sys/systeminfo.h>
57 #include <sys/fm/fs/zfs.h>
58 #include <sys/fm/util.h>
59 #include <sys/fm/protocol.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/mount.h>
62 #include <sys/sysmacros.h>
68 #include "zpool_util.h"
69 #include "zfs_comutil.h"
70 #include "zfeature_common.h"
72 #include "statcommon.h"
74 static int zpool_do_create(int, char **);
75 static int zpool_do_destroy(int, char **);
77 static int zpool_do_add(int, char **);
78 static int zpool_do_remove(int, char **);
79 static int zpool_do_labelclear(int, char **);
81 static int zpool_do_checkpoint(int, char **);
83 static int zpool_do_list(int, char **);
84 static int zpool_do_iostat(int, char **);
85 static int zpool_do_status(int, char **);
87 static int zpool_do_online(int, char **);
88 static int zpool_do_offline(int, char **);
89 static int zpool_do_clear(int, char **);
90 static int zpool_do_reopen(int, char **);
92 static int zpool_do_reguid(int, char **);
94 static int zpool_do_attach(int, char **);
95 static int zpool_do_detach(int, char **);
96 static int zpool_do_replace(int, char **);
97 static int zpool_do_split(int, char **);
99 static int zpool_do_scrub(int, char **);
100 static int zpool_do_resilver(int, char **);
102 static int zpool_do_import(int, char **);
103 static int zpool_do_export(int, char **);
105 static int zpool_do_upgrade(int, char **);
107 static int zpool_do_history(int, char **);
108 static int zpool_do_events(int, char **);
110 static int zpool_do_get(int, char **);
111 static int zpool_do_set(int, char **);
113 static int zpool_do_sync(int, char **);
116 * These libumem hooks provide a reasonable set of defaults for the allocator's
117 * debugging facilities.
122 _umem_debug_init(void)
124 return ("default,verbose"); /* $UMEM_DEBUG setting */
128 _umem_logging_init(void)
130 return ("fail,contents"); /* $UMEM_LOGGING setting */
167 * Flags for stats to display with "zpool iostats"
175 IOS_COUNT, /* always last element */
178 /* iostat_type entries as bitmasks */
179 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
180 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
181 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
182 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
183 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
185 /* Mask of all the histo bits */
186 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
189 * Lookup table for iostat flags to nvlist names. Basically a list
190 * of all the nvlists a flag requires. Also specifies the order in
191 * which data gets printed in zpool iostat.
193 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
195 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
196 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
197 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
198 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
200 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
201 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
202 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
203 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
206 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
207 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
208 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
209 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
212 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
213 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
214 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
215 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
216 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
219 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
220 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
221 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
222 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
223 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
224 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
225 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
226 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
227 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
228 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
234 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
235 * Right now, only one histo bit is ever set at one time, so we can
236 * just do a highbit64(a)
238 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
240 typedef struct zpool_command {
242 int (*func)(int, char **);
247 * Master command table. Each ZFS command has a name, associated function, and
248 * usage message. The usage messages need to be internationalized, so we have
249 * to have a function to return the usage message based on a command index.
251 * These commands are organized according to how they are displayed in the usage
252 * message. An empty command (one with a NULL name) indicates an empty line in
253 * the generic usage message.
255 static zpool_command_t command_table[] = {
256 { "create", zpool_do_create, HELP_CREATE },
257 { "destroy", zpool_do_destroy, HELP_DESTROY },
259 { "add", zpool_do_add, HELP_ADD },
260 { "remove", zpool_do_remove, HELP_REMOVE },
262 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
264 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
266 { "list", zpool_do_list, HELP_LIST },
267 { "iostat", zpool_do_iostat, HELP_IOSTAT },
268 { "status", zpool_do_status, HELP_STATUS },
270 { "online", zpool_do_online, HELP_ONLINE },
271 { "offline", zpool_do_offline, HELP_OFFLINE },
272 { "clear", zpool_do_clear, HELP_CLEAR },
273 { "reopen", zpool_do_reopen, HELP_REOPEN },
275 { "attach", zpool_do_attach, HELP_ATTACH },
276 { "detach", zpool_do_detach, HELP_DETACH },
277 { "replace", zpool_do_replace, HELP_REPLACE },
278 { "split", zpool_do_split, HELP_SPLIT },
280 { "scrub", zpool_do_scrub, HELP_SCRUB },
281 { "resilver", zpool_do_resilver, HELP_RESILVER },
283 { "import", zpool_do_import, HELP_IMPORT },
284 { "export", zpool_do_export, HELP_EXPORT },
285 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
286 { "reguid", zpool_do_reguid, HELP_REGUID },
288 { "history", zpool_do_history, HELP_HISTORY },
289 { "events", zpool_do_events, HELP_EVENTS },
291 { "get", zpool_do_get, HELP_GET },
292 { "set", zpool_do_set, HELP_SET },
293 { "sync", zpool_do_sync, HELP_SYNC },
296 #define NCOMMAND (ARRAY_SIZE(command_table))
298 #define VDEV_ALLOC_CLASS_LOGS "logs"
300 static zpool_command_t *current_command;
301 static char history_str[HIS_MAX_RECORD_LEN];
302 static boolean_t log_history = B_TRUE;
303 static uint_t timestamp_fmt = NODATE;
306 get_usage(zpool_help_t idx)
310 return (gettext("\tadd [-fgLnP] [-o property=value] "
311 "<pool> <vdev> ...\n"));
313 return (gettext("\tattach [-f] [-o property=value] "
314 "<pool> <device> <new-device>\n"));
316 return (gettext("\tclear [-nF] <pool> [device]\n"));
318 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
319 "\t [-O file-system-property=value] ... \n"
320 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
321 case HELP_CHECKPOINT:
322 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
324 return (gettext("\tdestroy [-f] <pool>\n"));
326 return (gettext("\tdetach <pool> <device>\n"));
328 return (gettext("\texport [-af] <pool> ...\n"));
330 return (gettext("\thistory [-il] [<pool>] ...\n"));
332 return (gettext("\timport [-d dir] [-D]\n"
333 "\timport [-o mntopts] [-o property=value] ... \n"
334 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
335 "[-R root] [-F [-n]] -a\n"
336 "\timport [-o mntopts] [-o property=value] ... \n"
337 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
338 "[-R root] [-F [-n]]\n"
339 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
341 return (gettext("\tiostat [[[-c [script1,script2,...]"
342 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
343 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
344 " [interval [count]]\n"));
345 case HELP_LABELCLEAR:
346 return (gettext("\tlabelclear [-f] <vdev>\n"));
348 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
349 "[-T d|u] [pool] ... \n"
350 "\t [interval [count]]\n"));
352 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
354 return (gettext("\tonline [-e] <pool> <device> ...\n"));
356 return (gettext("\treplace [-f] [-o property=value] "
357 "<pool> <device> [new-device]\n"));
359 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
361 return (gettext("\treopen [-n] <pool>\n"));
363 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
365 return (gettext("\tresilver <pool> ...\n"));
367 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
368 "[-T d|u] [pool] ... \n"
369 "\t [interval [count]]\n"));
371 return (gettext("\tupgrade\n"
373 "\tupgrade [-V version] <-a | pool ...>\n"));
375 return (gettext("\tevents [-vHf [pool] | -c]\n"));
377 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
378 "<\"all\" | property[,...]> <pool> ...\n"));
380 return (gettext("\tset <property=value> <pool> \n"));
382 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
383 "\t [-o property=value] <pool> <newpool> "
384 "[<device> ...]\n"));
386 return (gettext("\treguid <pool>\n"));
388 return (gettext("\tsync [pool] ...\n"));
397 * Callback routine that will print out a pool property value.
400 print_prop_cb(int prop, void *cb)
404 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
406 if (zpool_prop_readonly(prop))
407 (void) fprintf(fp, " NO ");
409 (void) fprintf(fp, " YES ");
411 if (zpool_prop_values(prop) == NULL)
412 (void) fprintf(fp, "-\n");
414 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
420 * Display usage message. If we're inside a command, display only the usage for
421 * that command. Otherwise, iterate over the entire command table and display
422 * a complete usage message.
425 usage(boolean_t requested)
427 FILE *fp = requested ? stdout : stderr;
429 if (current_command == NULL) {
432 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
434 gettext("where 'command' is one of the following:\n\n"));
436 for (i = 0; i < NCOMMAND; i++) {
437 if (command_table[i].name == NULL)
438 (void) fprintf(fp, "\n");
440 (void) fprintf(fp, "%s",
441 get_usage(command_table[i].usage));
444 (void) fprintf(fp, gettext("usage:\n"));
445 (void) fprintf(fp, "%s", get_usage(current_command->usage));
448 if (current_command != NULL &&
449 ((strcmp(current_command->name, "set") == 0) ||
450 (strcmp(current_command->name, "get") == 0) ||
451 (strcmp(current_command->name, "list") == 0))) {
454 gettext("\nthe following properties are supported:\n"));
456 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
457 "PROPERTY", "EDIT", "VALUES");
459 /* Iterate over all properties */
460 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
463 (void) fprintf(fp, "\t%-19s ", "feature@...");
464 (void) fprintf(fp, "YES disabled | enabled | active\n");
466 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
467 "appended with a feature name.\nSee zpool-features(5).\n"));
471 * See comments at end of main().
473 if (getenv("ZFS_ABORT") != NULL) {
474 (void) printf("dumping core by request\n");
478 exit(requested ? 0 : 2);
482 * print a pool vdev config for dry runs
485 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
486 const char *match, int name_flags)
491 boolean_t printed = B_FALSE;
493 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
494 &child, &children) != 0) {
496 (void) printf("\t%*s%s\n", indent, "", name);
500 for (c = 0; c < children; c++) {
501 uint64_t is_log = B_FALSE;
504 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
507 class = VDEV_ALLOC_BIAS_LOG;
508 (void) nvlist_lookup_string(child[c],
509 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
510 if (strcmp(match, class) != 0)
513 if (!printed && name != NULL) {
514 (void) printf("\t%*s%s\n", indent, "", name);
517 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
518 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
525 prop_list_contains_feature(nvlist_t *proplist)
528 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
529 nvp = nvlist_next_nvpair(proplist, nvp)) {
530 if (zpool_prop_feature(nvpair_name(nvp)))
537 * Add a property pair (name, string-value) into a property nvlist.
540 add_prop_list(const char *propname, char *propval, nvlist_t **props,
543 zpool_prop_t prop = ZPOOL_PROP_INVAL;
548 if (*props == NULL &&
549 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
550 (void) fprintf(stderr,
551 gettext("internal error: out of memory\n"));
558 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
560 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
561 !zpool_prop_feature(propname)) {
562 (void) fprintf(stderr, gettext("property '%s' is "
563 "not a valid pool property\n"), propname);
568 * feature@ properties and version should not be specified
571 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
572 nvlist_exists(proplist, vname)) ||
573 (prop == ZPOOL_PROP_VERSION &&
574 prop_list_contains_feature(proplist))) {
575 (void) fprintf(stderr, gettext("'feature@' and "
576 "'version' properties cannot be specified "
582 if (zpool_prop_feature(propname))
585 normnm = zpool_prop_to_name(prop);
587 zfs_prop_t fsprop = zfs_name_to_prop(propname);
589 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
591 normnm = zfs_prop_to_name(fsprop);
592 } else if (zfs_prop_user(propname) ||
593 zfs_prop_userquota(propname)) {
596 (void) fprintf(stderr, gettext("property '%s' is "
597 "not a valid filesystem property\n"), propname);
602 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
603 prop != ZPOOL_PROP_CACHEFILE) {
604 (void) fprintf(stderr, gettext("property '%s' "
605 "specified multiple times\n"), propname);
609 if (nvlist_add_string(proplist, normnm, propval) != 0) {
610 (void) fprintf(stderr, gettext("internal "
611 "error: out of memory\n"));
619 * Set a default property pair (name, string-value) in a property nvlist
622 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
627 if (nvlist_lookup_string(*props, propname, &pval) == 0)
630 return (add_prop_list(propname, propval, props, B_TRUE));
634 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
636 * -f Force addition of devices, even if they appear in use
637 * -g Display guid for individual vdev name.
638 * -L Follow links when resolving vdev path name.
639 * -n Do not add the devices, but display the resulting layout if
640 * they were to be added.
641 * -o Set property=value.
642 * -P Display full path for vdev name.
644 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
645 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
649 zpool_do_add(int argc, char **argv)
651 boolean_t force = B_FALSE;
652 boolean_t dryrun = B_FALSE;
660 nvlist_t *props = NULL;
664 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
670 name_flags |= VDEV_NAME_GUID;
673 name_flags |= VDEV_NAME_FOLLOW_LINKS;
679 if ((propval = strchr(optarg, '=')) == NULL) {
680 (void) fprintf(stderr, gettext("missing "
681 "'=' for -o option\n"));
687 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
688 (add_prop_list(optarg, propval, &props, B_TRUE)))
692 name_flags |= VDEV_NAME_PATH;
695 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
704 /* get pool name and check number of arguments */
706 (void) fprintf(stderr, gettext("missing pool name argument\n"));
710 (void) fprintf(stderr, gettext("missing vdev specification\n"));
719 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
722 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
723 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
729 /* unless manually specified use "ashift" pool property (if set) */
730 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
733 char strval[ZPOOL_MAXPROPLEN];
735 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
736 if (src != ZPROP_SRC_DEFAULT) {
737 (void) sprintf(strval, "%" PRId32, intval);
738 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
739 &props, B_TRUE) == 0);
743 /* pass off to get_vdev_spec for processing */
744 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
746 if (nvroot == NULL) {
752 nvlist_t *poolnvroot;
754 uint_t l2children, c;
756 boolean_t hadcache = B_FALSE;
758 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
761 (void) printf(gettext("would update '%s' to the following "
762 "configuration:\n"), zpool_get_name(zhp));
764 /* print original main pool and new tree */
765 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
766 name_flags | VDEV_NAME_TYPE_ID);
767 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
769 /* print other classes: 'dedup', 'special', and 'log' */
770 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
771 VDEV_ALLOC_BIAS_DEDUP, name_flags);
772 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP,
775 print_vdev_tree(zhp, "special", poolnvroot, 0,
776 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
777 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL,
780 print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG,
782 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG,
785 /* Do the same for the caches */
786 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
787 &l2child, &l2children) == 0 && l2children) {
789 (void) printf(gettext("\tcache\n"));
790 for (c = 0; c < l2children; c++) {
791 vname = zpool_vdev_name(g_zfs, NULL,
792 l2child[c], name_flags);
793 (void) printf("\t %s\n", vname);
797 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
798 &l2child, &l2children) == 0 && l2children) {
800 (void) printf(gettext("\tcache\n"));
801 for (c = 0; c < l2children; c++) {
802 vname = zpool_vdev_name(g_zfs, NULL,
803 l2child[c], name_flags);
804 (void) printf("\t %s\n", vname);
811 ret = (zpool_add(zhp, nvroot) != 0);
822 * zpool remove <pool> <vdev> ...
824 * Removes the given vdev from the pool.
827 zpool_do_remove(int argc, char **argv)
831 zpool_handle_t *zhp = NULL;
832 boolean_t stop = B_FALSE;
834 boolean_t noop = B_FALSE;
835 boolean_t parsable = B_FALSE;
838 while ((c = getopt(argc, argv, "nps")) != -1) {
850 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
859 /* get pool name and check number of arguments */
861 (void) fprintf(stderr, gettext("missing pool name argument\n"));
867 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
871 (void) fprintf(stderr, gettext("stop request ignored\n"));
877 (void) fprintf(stderr, gettext("too many arguments\n"));
880 if (zpool_vdev_remove_cancel(zhp) != 0)
884 (void) fprintf(stderr, gettext("missing device\n"));
888 for (i = 1; i < argc; i++) {
892 if (zpool_vdev_indirect_size(zhp, argv[i],
898 (void) printf("%s %llu\n",
899 argv[i], (unsigned long long)size);
902 zfs_nicenum(size, valstr,
904 (void) printf("Memory that will be "
905 "used after removing %s: %s\n",
909 if (zpool_vdev_remove(zhp, argv[i]) != 0)
920 * zpool labelclear [-f] <vdev>
922 * -f Force clearing the label for the vdevs which are members of
923 * the exported or foreign pools.
925 * Verifies that the vdev is not active and zeros out the label information
929 zpool_do_labelclear(int argc, char **argv)
931 char vdev[MAXPATHLEN];
934 int c, fd = -1, ret = 0;
937 boolean_t inuse = B_FALSE;
938 boolean_t force = B_FALSE;
941 while ((c = getopt(argc, argv, "f")) != -1) {
947 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
958 (void) fprintf(stderr, gettext("missing vdev name\n"));
962 (void) fprintf(stderr, gettext("too many arguments\n"));
967 * Check if we were given absolute path and use it as is.
968 * Otherwise if the provided vdev name doesn't point to a file,
969 * try prepending expected disk paths and partition numbers.
971 (void) strlcpy(vdev, argv[0], sizeof (vdev));
972 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
975 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
976 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
977 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
981 if (error || (stat(vdev, &st) != 0)) {
982 (void) fprintf(stderr, gettext(
983 "failed to find device %s, try specifying absolute "
984 "path instead\n"), argv[0]);
989 if ((fd = open(vdev, O_RDWR)) < 0) {
990 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
991 vdev, strerror(errno));
995 if (ioctl(fd, BLKFLSBUF) != 0)
996 (void) fprintf(stderr, gettext("failed to invalidate "
997 "cache for %s: %s\n"), vdev, strerror(errno));
999 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
1000 (void) fprintf(stderr,
1001 gettext("failed to check state for %s\n"), vdev);
1005 nvlist_free(config);
1007 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1009 (void) fprintf(stderr,
1010 gettext("failed to check state for %s\n"), vdev);
1020 case POOL_STATE_ACTIVE:
1021 case POOL_STATE_SPARE:
1022 case POOL_STATE_L2CACHE:
1023 (void) fprintf(stderr, gettext(
1024 "%s is a member (%s) of pool \"%s\"\n"),
1025 vdev, zpool_pool_state_to_name(state), name);
1029 case POOL_STATE_EXPORTED:
1032 (void) fprintf(stderr, gettext(
1033 "use '-f' to override the following error:\n"
1034 "%s is a member of exported pool \"%s\"\n"),
1039 case POOL_STATE_POTENTIALLY_ACTIVE:
1042 (void) fprintf(stderr, gettext(
1043 "use '-f' to override the following error:\n"
1044 "%s is a member of potentially active pool \"%s\"\n"),
1049 case POOL_STATE_DESTROYED:
1050 /* inuse should never be set for a destroyed pool */
1056 ret = zpool_clear_label(fd);
1058 (void) fprintf(stderr,
1059 gettext("failed to clear label for %s\n"), vdev);
1070 * zpool create [-fnd] [-o property=value] ...
1071 * [-O file-system-property=value] ...
1072 * [-R root] [-m mountpoint] <pool> <dev> ...
1074 * -f Force creation, even if devices appear in use
1075 * -n Do not create the pool, but display the resulting layout if it
1076 * were to be created.
1077 * -R Create a pool under an alternate root
1078 * -m Set default mountpoint for the root dataset. By default it's
1080 * -o Set property=value.
1081 * -o Set feature@feature=enabled|disabled.
1082 * -d Don't automatically enable all supported pool features
1083 * (individual features can be enabled with -o).
1084 * -O Set fsproperty=value in the pool's root file system
1086 * Creates the named pool according to the given vdev specification. The
1087 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1088 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1089 * (if '-n' was specified), or pass it to libzfs to do the creation.
1092 zpool_do_create(int argc, char **argv)
1094 boolean_t force = B_FALSE;
1095 boolean_t dryrun = B_FALSE;
1096 boolean_t enable_all_pool_feat = B_TRUE;
1098 nvlist_t *nvroot = NULL;
1102 char *altroot = NULL;
1103 char *mountpoint = NULL;
1104 nvlist_t *fsprops = NULL;
1105 nvlist_t *props = NULL;
1109 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1118 enable_all_pool_feat = B_FALSE;
1122 if (add_prop_list(zpool_prop_to_name(
1123 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1125 if (add_prop_list_default(zpool_prop_to_name(
1126 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1130 /* Equivalent to -O mountpoint=optarg */
1131 mountpoint = optarg;
1134 if ((propval = strchr(optarg, '=')) == NULL) {
1135 (void) fprintf(stderr, gettext("missing "
1136 "'=' for -o option\n"));
1142 if (add_prop_list(optarg, propval, &props, B_TRUE))
1146 * If the user is creating a pool that doesn't support
1147 * feature flags, don't enable any features.
1149 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1153 ver = strtoull(propval, &end, 10);
1155 ver < SPA_VERSION_FEATURES) {
1156 enable_all_pool_feat = B_FALSE;
1159 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1163 if ((propval = strchr(optarg, '=')) == NULL) {
1164 (void) fprintf(stderr, gettext("missing "
1165 "'=' for -O option\n"));
1172 * Mountpoints are checked and then added later.
1173 * Uniquely among properties, they can be specified
1174 * more than once, to avoid conflict with -m.
1176 if (0 == strcmp(optarg,
1177 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1178 mountpoint = propval;
1179 } else if (add_prop_list(optarg, propval, &fsprops,
1186 * Sanity check temporary pool name.
1188 if (strchr(optarg, '/') != NULL) {
1189 (void) fprintf(stderr, gettext("cannot create "
1190 "'%s': invalid character '/' in temporary "
1192 (void) fprintf(stderr, gettext("use 'zfs "
1193 "create' to create a dataset\n"));
1197 if (add_prop_list(zpool_prop_to_name(
1198 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1200 if (add_prop_list_default(zpool_prop_to_name(
1201 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1206 (void) fprintf(stderr, gettext("missing argument for "
1207 "'%c' option\n"), optopt);
1210 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1219 /* get pool name and check number of arguments */
1221 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1225 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1232 * As a special case, check for use of '/' in the name, and direct the
1233 * user to use 'zfs create' instead.
1235 if (strchr(poolname, '/') != NULL) {
1236 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1237 "character '/' in pool name\n"), poolname);
1238 (void) fprintf(stderr, gettext("use 'zfs create' to "
1239 "create a dataset\n"));
1243 /* pass off to get_vdev_spec for bulk processing */
1244 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1245 argc - 1, argv + 1);
1249 /* make_root_vdev() allows 0 toplevel children if there are spares */
1250 if (!zfs_allocatable_devs(nvroot)) {
1251 (void) fprintf(stderr, gettext("invalid vdev "
1252 "specification: at least one toplevel vdev must be "
1257 if (altroot != NULL && altroot[0] != '/') {
1258 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1259 "must be an absolute path\n"), altroot);
1264 * Check the validity of the mountpoint and direct the user to use the
1265 * '-m' mountpoint option if it looks like its in use.
1267 if (mountpoint == NULL ||
1268 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1269 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1270 char buf[MAXPATHLEN];
1273 if (mountpoint && mountpoint[0] != '/') {
1274 (void) fprintf(stderr, gettext("invalid mountpoint "
1275 "'%s': must be an absolute path, 'legacy', or "
1276 "'none'\n"), mountpoint);
1280 if (mountpoint == NULL) {
1281 if (altroot != NULL)
1282 (void) snprintf(buf, sizeof (buf), "%s/%s",
1285 (void) snprintf(buf, sizeof (buf), "/%s",
1288 if (altroot != NULL)
1289 (void) snprintf(buf, sizeof (buf), "%s%s",
1290 altroot, mountpoint);
1292 (void) snprintf(buf, sizeof (buf), "%s",
1296 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1297 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1298 "%s\n"), buf, strerror(errno));
1299 (void) fprintf(stderr, gettext("use '-m' "
1300 "option to provide a different default\n"));
1305 while (count < 3 && readdir(dirp) != NULL)
1307 (void) closedir(dirp);
1310 (void) fprintf(stderr, gettext("mountpoint "
1311 "'%s' exists and is not empty\n"), buf);
1312 (void) fprintf(stderr, gettext("use '-m' "
1313 "option to provide a "
1314 "different default\n"));
1321 * Now that the mountpoint's validity has been checked, ensure that
1322 * the property is set appropriately prior to creating the pool.
1324 if (mountpoint != NULL) {
1325 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1326 mountpoint, &fsprops, B_FALSE);
1334 * For a dry run invocation, print out a basic message and run
1335 * through all the vdevs in the list and print out in an
1336 * appropriate hierarchy.
1338 (void) printf(gettext("would create '%s' with the "
1339 "following layout:\n\n"), poolname);
1341 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1342 print_vdev_tree(NULL, "dedup", nvroot, 0,
1343 VDEV_ALLOC_BIAS_DEDUP, 0);
1344 print_vdev_tree(NULL, "special", nvroot, 0,
1345 VDEV_ALLOC_BIAS_SPECIAL, 0);
1346 print_vdev_tree(NULL, "logs", nvroot, 0,
1347 VDEV_ALLOC_BIAS_LOG, 0);
1352 * Hand off to libzfs.
1355 for (i = 0; i < SPA_FEATURES; i++) {
1356 char propname[MAXPATHLEN];
1358 zfeature_info_t *feat = &spa_feature_table[i];
1360 (void) snprintf(propname, sizeof (propname),
1361 "feature@%s", feat->fi_uname);
1364 * Only features contained in props will be enabled:
1365 * remove from the nvlist every ZFS_FEATURE_DISABLED
1366 * value and add every missing ZFS_FEATURE_ENABLED if
1367 * enable_all_pool_feat is set.
1369 if (!nvlist_lookup_string(props, propname, &propval)) {
1370 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1371 (void) nvlist_remove_all(props,
1373 } else if (enable_all_pool_feat) {
1374 ret = add_prop_list(propname,
1375 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1382 if (zpool_create(g_zfs, poolname,
1383 nvroot, props, fsprops) == 0) {
1384 zfs_handle_t *pool = zfs_open(g_zfs,
1385 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1387 if (zfs_mount(pool, NULL, 0) == 0)
1388 ret = zfs_shareall(pool);
1391 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1392 (void) fprintf(stderr, gettext("pool name may have "
1398 nvlist_free(nvroot);
1399 nvlist_free(fsprops);
1403 nvlist_free(fsprops);
1410 * zpool destroy <pool>
1412 * -f Forcefully unmount any datasets
1414 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1417 zpool_do_destroy(int argc, char **argv)
1419 boolean_t force = B_FALSE;
1422 zpool_handle_t *zhp;
1426 while ((c = getopt(argc, argv, "f")) != -1) {
1432 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1441 /* check arguments */
1443 (void) fprintf(stderr, gettext("missing pool argument\n"));
1447 (void) fprintf(stderr, gettext("too many arguments\n"));
1453 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1455 * As a special case, check for use of '/' in the name, and
1456 * direct the user to use 'zfs destroy' instead.
1458 if (strchr(pool, '/') != NULL)
1459 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1460 "destroy a dataset\n"));
1464 if (zpool_disable_datasets(zhp, force) != 0) {
1465 (void) fprintf(stderr, gettext("could not destroy '%s': "
1466 "could not unmount datasets\n"), zpool_get_name(zhp));
1471 /* The history must be logged as part of the export */
1472 log_history = B_FALSE;
1474 ret = (zpool_destroy(zhp, history_str) != 0);
1481 typedef struct export_cbdata {
1483 boolean_t hardforce;
1490 zpool_export_one(zpool_handle_t *zhp, void *data)
1492 export_cbdata_t *cb = data;
1494 if (zpool_disable_datasets(zhp, cb->force) != 0)
1497 /* The history must be logged as part of the export */
1498 log_history = B_FALSE;
1500 if (cb->hardforce) {
1501 if (zpool_export_force(zhp, history_str) != 0)
1503 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1511 * zpool export [-f] <pool> ...
1513 * -a Export all pools
1514 * -f Forcefully unmount datasets
1516 * Export the given pools. By default, the command will attempt to cleanly
1517 * unmount any active datasets within the pool. If the '-f' flag is specified,
1518 * then the datasets will be forcefully unmounted.
1521 zpool_do_export(int argc, char **argv)
1524 boolean_t do_all = B_FALSE;
1525 boolean_t force = B_FALSE;
1526 boolean_t hardforce = B_FALSE;
1530 while ((c = getopt(argc, argv, "afF")) != -1) {
1542 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1549 cb.hardforce = hardforce;
1555 (void) fprintf(stderr, gettext("too many arguments\n"));
1559 return (for_each_pool(argc, argv, B_TRUE, NULL,
1560 zpool_export_one, &cb));
1563 /* check arguments */
1565 (void) fprintf(stderr, gettext("missing pool argument\n"));
1569 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1575 * Given a vdev configuration, determine the maximum width needed for the device
1579 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1587 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1588 if (strlen(name) + depth > max)
1589 max = strlen(name) + depth;
1593 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1594 &child, &children) == 0) {
1595 for (c = 0; c < children; c++)
1596 if ((ret = max_width(zhp, child[c], depth + 2,
1597 max, name_flags)) > max)
1601 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1602 &child, &children) == 0) {
1603 for (c = 0; c < children; c++)
1604 if ((ret = max_width(zhp, child[c], depth + 2,
1605 max, name_flags)) > max)
1609 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1610 &child, &children) == 0) {
1611 for (c = 0; c < children; c++)
1612 if ((ret = max_width(zhp, child[c], depth + 2,
1613 max, name_flags)) > max)
1620 typedef struct spare_cbdata {
1622 zpool_handle_t *cb_zhp;
1626 find_vdev(nvlist_t *nv, uint64_t search)
1632 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1636 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1637 &child, &children) == 0) {
1638 for (c = 0; c < children; c++)
1639 if (find_vdev(child[c], search))
1647 find_spare(zpool_handle_t *zhp, void *data)
1649 spare_cbdata_t *cbp = data;
1650 nvlist_t *config, *nvroot;
1652 config = zpool_get_config(zhp, NULL);
1653 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1656 if (find_vdev(nvroot, cbp->cb_guid)) {
1665 typedef struct status_cbdata {
1669 boolean_t cb_allpools;
1670 boolean_t cb_verbose;
1671 boolean_t cb_explain;
1673 boolean_t cb_dedup_stats;
1674 boolean_t cb_print_status;
1675 vdev_cmd_data_list_t *vcdl;
1678 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1680 is_blank_str(char *str)
1682 while (str != NULL && *str != '\0') {
1690 /* Print command output lines for specific vdev in a specific pool */
1692 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1694 vdev_cmd_data_t *data;
1698 for (i = 0; i < vcdl->count; i++) {
1699 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1700 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1701 /* Not the vdev we're looking for */
1705 data = &vcdl->data[i];
1706 /* Print out all the output values for this vdev */
1707 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1709 /* Does this vdev have values for this column? */
1710 for (int k = 0; k < data->cols_cnt; k++) {
1711 if (strcmp(data->cols[k],
1712 vcdl->uniq_cols[j]) == 0) {
1713 /* yes it does, record the value */
1714 val = data->lines[k];
1719 * Mark empty values with dashes to make output
1722 if (is_blank_str(val))
1725 printf("%*s", vcdl->uniq_cols_width[j], val);
1726 if (j < vcdl->uniq_cols_cnt - 1)
1730 /* Print out any values that aren't in a column at the end */
1731 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1732 /* Did we have any columns? If so print a spacer. */
1733 if (vcdl->uniq_cols_cnt > 0)
1736 val = data->lines[j];
1737 printf("%s", val ? val : "");
1744 * Print out configuration state as requested by status_callback.
1747 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1748 nvlist_t *nv, int depth, boolean_t isspare)
1750 nvlist_t **child, *root;
1752 pool_scan_stat_t *ps = NULL;
1754 char rbuf[6], wbuf[6], cbuf[6];
1756 uint64_t notpresent;
1757 spare_cbdata_t spare_cb;
1762 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1763 &child, &children) != 0)
1766 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1767 (uint64_t **)&vs, &c) == 0);
1769 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1771 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1774 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1777 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1780 if (vs->vs_aux == VDEV_AUX_SPARED)
1782 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1786 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1790 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1791 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1792 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1793 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1796 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1797 ¬present) == 0) {
1798 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1799 (void) printf(" was %s", path);
1800 } else if (vs->vs_aux != 0) {
1803 switch (vs->vs_aux) {
1804 case VDEV_AUX_OPEN_FAILED:
1805 (void) printf(gettext("cannot open"));
1808 case VDEV_AUX_BAD_GUID_SUM:
1809 (void) printf(gettext("missing device"));
1812 case VDEV_AUX_NO_REPLICAS:
1813 (void) printf(gettext("insufficient replicas"));
1816 case VDEV_AUX_VERSION_NEWER:
1817 (void) printf(gettext("newer version"));
1820 case VDEV_AUX_UNSUP_FEAT:
1821 (void) printf(gettext("unsupported feature(s)"));
1824 case VDEV_AUX_SPARED:
1825 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1826 &spare_cb.cb_guid) == 0);
1827 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1828 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1829 zpool_get_name(zhp)) == 0)
1830 (void) printf(gettext("currently in "
1833 (void) printf(gettext("in use by "
1835 zpool_get_name(spare_cb.cb_zhp));
1836 zpool_close(spare_cb.cb_zhp);
1838 (void) printf(gettext("currently in use"));
1842 case VDEV_AUX_ERR_EXCEEDED:
1843 (void) printf(gettext("too many errors"));
1846 case VDEV_AUX_IO_FAILURE:
1847 (void) printf(gettext("experienced I/O failures"));
1850 case VDEV_AUX_BAD_LOG:
1851 (void) printf(gettext("bad intent log"));
1854 case VDEV_AUX_EXTERNAL:
1855 (void) printf(gettext("external device fault"));
1858 case VDEV_AUX_SPLIT_POOL:
1859 (void) printf(gettext("split into new pool"));
1862 case VDEV_AUX_ACTIVE:
1863 (void) printf(gettext("currently in use"));
1866 case VDEV_AUX_CHILDREN_OFFLINE:
1867 (void) printf(gettext("all children offline"));
1871 (void) printf(gettext("corrupted data"));
1876 /* The root vdev has the scrub/resilver stats */
1877 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1878 ZPOOL_CONFIG_VDEV_TREE);
1879 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
1880 (uint64_t **)&ps, &c);
1882 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) {
1883 if (vs->vs_scan_processed != 0) {
1884 (void) printf(gettext(" (%s)"),
1885 (ps->pss_func == POOL_SCAN_RESILVER) ?
1886 "resilvering" : "repairing");
1887 } else if (vs->vs_resilver_deferred) {
1888 (void) printf(gettext(" (awaiting resilver)"));
1892 if (cb->vcdl != NULL) {
1893 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1895 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1899 (void) printf("\n");
1901 for (c = 0; c < children; c++) {
1902 uint64_t islog = B_FALSE, ishole = B_FALSE;
1904 /* Don't print logs or holes here */
1905 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1907 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1909 if (islog || ishole)
1911 /* Only print normal classes here */
1912 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
1915 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1916 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1918 print_status_config(zhp, cb, vname, child[c], depth + 2,
1925 * Print the configuration of an exported pool. Iterate over all vdevs in the
1926 * pool, printing out the name and status for each one.
1929 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1937 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1938 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1939 strcmp(type, VDEV_TYPE_HOLE) == 0)
1942 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1943 (uint64_t **)&vs, &c) == 0);
1945 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1946 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1948 if (vs->vs_aux != 0) {
1951 switch (vs->vs_aux) {
1952 case VDEV_AUX_OPEN_FAILED:
1953 (void) printf(gettext("cannot open"));
1956 case VDEV_AUX_BAD_GUID_SUM:
1957 (void) printf(gettext("missing device"));
1960 case VDEV_AUX_NO_REPLICAS:
1961 (void) printf(gettext("insufficient replicas"));
1964 case VDEV_AUX_VERSION_NEWER:
1965 (void) printf(gettext("newer version"));
1968 case VDEV_AUX_UNSUP_FEAT:
1969 (void) printf(gettext("unsupported feature(s)"));
1972 case VDEV_AUX_ERR_EXCEEDED:
1973 (void) printf(gettext("too many errors"));
1976 case VDEV_AUX_ACTIVE:
1977 (void) printf(gettext("currently in use"));
1980 case VDEV_AUX_CHILDREN_OFFLINE:
1981 (void) printf(gettext("all children offline"));
1985 (void) printf(gettext("corrupted data"));
1989 (void) printf("\n");
1991 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1992 &child, &children) != 0)
1995 for (c = 0; c < children; c++) {
1996 uint64_t is_log = B_FALSE;
1998 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2002 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2005 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2006 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2007 print_import_config(cb, vname, child[c], depth + 2);
2011 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2012 &child, &children) == 0) {
2013 (void) printf(gettext("\tcache\n"));
2014 for (c = 0; c < children; c++) {
2015 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2017 (void) printf("\t %s\n", vname);
2022 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2023 &child, &children) == 0) {
2024 (void) printf(gettext("\tspares\n"));
2025 for (c = 0; c < children; c++) {
2026 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2028 (void) printf("\t %s\n", vname);
2035 * Print specialized class vdevs.
2037 * These are recorded as top level vdevs in the main pool child array
2038 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2039 * print_status_config() or print_import_config() to print the top level
2040 * class vdevs then any of their children (eg mirrored slogs) are printed
2041 * recursively - which works because only the top level vdev is marked.
2044 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2049 boolean_t printed = B_FALSE;
2051 assert(zhp != NULL || !cb->cb_verbose);
2053 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2057 for (c = 0; c < children; c++) {
2058 uint64_t is_log = B_FALSE;
2062 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2066 bias = VDEV_ALLOC_CLASS_LOGS;
2068 (void) nvlist_lookup_string(child[c],
2069 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2070 (void) nvlist_lookup_string(child[c],
2071 ZPOOL_CONFIG_TYPE, &type);
2074 if (bias == NULL || strcmp(bias, class) != 0)
2076 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2080 (void) printf("\t%s\t\n", gettext(class));
2084 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
2085 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2086 if (cb->cb_print_status)
2087 print_status_config(zhp, cb, name, child[c], 2,
2090 print_import_config(cb, name, child[c], 2);
2096 * Display the status for the given pool.
2099 show_import(nvlist_t *config)
2101 uint64_t pool_state;
2105 uint64_t hostid = 0;
2107 char *hostname = "unknown";
2108 nvlist_t *nvroot, *nvinfo;
2109 zpool_status_t reason;
2110 zpool_errata_t errata;
2114 status_cbdata_t cb = { 0 };
2116 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2118 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2120 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2122 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2125 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2126 (uint64_t **)&vs, &vsc) == 0);
2127 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2129 reason = zpool_import_status(config, &msgid, &errata);
2131 (void) printf(gettext(" pool: %s\n"), name);
2132 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2133 (void) printf(gettext(" state: %s"), health);
2134 if (pool_state == POOL_STATE_DESTROYED)
2135 (void) printf(gettext(" (DESTROYED)"));
2136 (void) printf("\n");
2139 case ZPOOL_STATUS_MISSING_DEV_R:
2140 case ZPOOL_STATUS_MISSING_DEV_NR:
2141 case ZPOOL_STATUS_BAD_GUID_SUM:
2142 (void) printf(gettext(" status: One or more devices are "
2143 "missing from the system.\n"));
2146 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2147 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2148 (void) printf(gettext(" status: One or more devices contains "
2149 "corrupted data.\n"));
2152 case ZPOOL_STATUS_CORRUPT_DATA:
2154 gettext(" status: The pool data is corrupted.\n"));
2157 case ZPOOL_STATUS_OFFLINE_DEV:
2158 (void) printf(gettext(" status: One or more devices "
2159 "are offlined.\n"));
2162 case ZPOOL_STATUS_CORRUPT_POOL:
2163 (void) printf(gettext(" status: The pool metadata is "
2167 case ZPOOL_STATUS_VERSION_OLDER:
2168 (void) printf(gettext(" status: The pool is formatted using a "
2169 "legacy on-disk version.\n"));
2172 case ZPOOL_STATUS_VERSION_NEWER:
2173 (void) printf(gettext(" status: The pool is formatted using an "
2174 "incompatible version.\n"));
2177 case ZPOOL_STATUS_FEAT_DISABLED:
2178 (void) printf(gettext(" status: Some supported features are "
2179 "not enabled on the pool.\n"));
2182 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2183 (void) printf(gettext("status: The pool uses the following "
2184 "feature(s) not supported on this system:\n"));
2185 zpool_print_unsup_feat(config);
2188 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2189 (void) printf(gettext("status: The pool can only be accessed "
2190 "in read-only mode on this system. It\n\tcannot be "
2191 "accessed in read-write mode because it uses the "
2192 "following\n\tfeature(s) not supported on this system:\n"));
2193 zpool_print_unsup_feat(config);
2196 case ZPOOL_STATUS_HOSTID_ACTIVE:
2197 (void) printf(gettext(" status: The pool is currently "
2198 "imported by another system.\n"));
2201 case ZPOOL_STATUS_HOSTID_REQUIRED:
2202 (void) printf(gettext(" status: The pool has the "
2203 "multihost property on. It cannot\n\tbe safely imported "
2204 "when the system hostid is not set.\n"));
2207 case ZPOOL_STATUS_HOSTID_MISMATCH:
2208 (void) printf(gettext(" status: The pool was last accessed by "
2209 "another system.\n"));
2212 case ZPOOL_STATUS_FAULTED_DEV_R:
2213 case ZPOOL_STATUS_FAULTED_DEV_NR:
2214 (void) printf(gettext(" status: One or more devices are "
2218 case ZPOOL_STATUS_BAD_LOG:
2219 (void) printf(gettext(" status: An intent log record cannot be "
2223 case ZPOOL_STATUS_RESILVERING:
2224 (void) printf(gettext(" status: One or more devices were being "
2228 case ZPOOL_STATUS_ERRATA:
2229 (void) printf(gettext(" status: Errata #%d detected.\n"),
2235 * No other status can be seen when importing pools.
2237 assert(reason == ZPOOL_STATUS_OK);
2241 * Print out an action according to the overall state of the pool.
2243 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2244 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2245 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2246 (void) printf(gettext(" action: The pool can be "
2247 "imported using its name or numeric identifier, "
2248 "though\n\tsome features will not be available "
2249 "without an explicit 'zpool upgrade'.\n"));
2250 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2251 (void) printf(gettext(" action: The pool can be "
2252 "imported using its name or numeric "
2253 "identifier and\n\tthe '-f' flag.\n"));
2254 } else if (reason == ZPOOL_STATUS_ERRATA) {
2256 case ZPOOL_ERRATA_NONE:
2259 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2260 (void) printf(gettext(" action: The pool can "
2261 "be imported using its name or numeric "
2262 "identifier,\n\thowever there is a compat"
2263 "ibility issue which should be corrected"
2264 "\n\tby running 'zpool scrub'\n"));
2267 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2268 (void) printf(gettext(" action: The pool can"
2269 "not be imported with this version of ZFS "
2270 "due to\n\tan active asynchronous destroy. "
2271 "Revert to an earlier version\n\tand "
2272 "allow the destroy to complete before "
2276 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2277 (void) printf(gettext(" action: Existing "
2278 "encrypted datasets contain an on-disk "
2279 "incompatibility, which\n\tneeds to be "
2280 "corrected. Backup these datasets to new "
2281 "encrypted datasets\n\tand destroy the "
2287 * All errata must contain an action message.
2292 (void) printf(gettext(" action: The pool can be "
2293 "imported using its name or numeric "
2296 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2297 (void) printf(gettext(" action: The pool can be imported "
2298 "despite missing or damaged devices. The\n\tfault "
2299 "tolerance of the pool may be compromised if imported.\n"));
2302 case ZPOOL_STATUS_VERSION_NEWER:
2303 (void) printf(gettext(" action: The pool cannot be "
2304 "imported. Access the pool on a system running "
2305 "newer\n\tsoftware, or recreate the pool from "
2308 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2309 (void) printf(gettext("action: The pool cannot be "
2310 "imported. Access the pool on a system that "
2311 "supports\n\tthe required feature(s), or recreate "
2312 "the pool from backup.\n"));
2314 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2315 (void) printf(gettext("action: The pool cannot be "
2316 "imported in read-write mode. Import the pool "
2318 "\t\"-o readonly=on\", access the pool on a system "
2319 "that supports the\n\trequired feature(s), or "
2320 "recreate the pool from backup.\n"));
2322 case ZPOOL_STATUS_MISSING_DEV_R:
2323 case ZPOOL_STATUS_MISSING_DEV_NR:
2324 case ZPOOL_STATUS_BAD_GUID_SUM:
2325 (void) printf(gettext(" action: The pool cannot be "
2326 "imported. Attach the missing\n\tdevices and try "
2329 case ZPOOL_STATUS_HOSTID_ACTIVE:
2330 VERIFY0(nvlist_lookup_nvlist(config,
2331 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2333 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2334 hostname = fnvlist_lookup_string(nvinfo,
2335 ZPOOL_CONFIG_MMP_HOSTNAME);
2337 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2338 hostid = fnvlist_lookup_uint64(nvinfo,
2339 ZPOOL_CONFIG_MMP_HOSTID);
2341 (void) printf(gettext(" action: The pool must be "
2342 "exported from %s (hostid=%lx)\n\tbefore it "
2343 "can be safely imported.\n"), hostname,
2344 (unsigned long) hostid);
2346 case ZPOOL_STATUS_HOSTID_REQUIRED:
2347 (void) printf(gettext(" action: Set a unique system "
2348 "hostid with the zgenhostid(8) command.\n"));
2351 (void) printf(gettext(" action: The pool cannot be "
2352 "imported due to damaged devices or data.\n"));
2356 /* Print the comment attached to the pool. */
2357 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2358 (void) printf(gettext("comment: %s\n"), comment);
2361 * If the state is "closed" or "can't open", and the aux state
2362 * is "corrupt data":
2364 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2365 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2366 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2367 if (pool_state == POOL_STATE_DESTROYED)
2368 (void) printf(gettext("\tThe pool was destroyed, "
2369 "but can be imported using the '-Df' flags.\n"));
2370 else if (pool_state != POOL_STATE_EXPORTED)
2371 (void) printf(gettext("\tThe pool may be active on "
2372 "another system, but can be imported using\n\t"
2373 "the '-f' flag.\n"));
2377 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2380 (void) printf(gettext(" config:\n\n"));
2382 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2384 if (cb.cb_namewidth < 10)
2385 cb.cb_namewidth = 10;
2387 print_import_config(&cb, name, nvroot, 0);
2389 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
2390 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
2391 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
2393 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2394 (void) printf(gettext("\n\tAdditional devices are known to "
2395 "be part of this pool, though their\n\texact "
2396 "configuration cannot be determined.\n"));
2401 zfs_force_import_required(nvlist_t *config)
2404 uint64_t hostid = 0;
2407 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2408 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2410 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2413 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2414 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2415 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2416 ZPOOL_CONFIG_MMP_STATE);
2418 if (mmp_state != MMP_STATE_INACTIVE)
2426 * Perform the import for the given configuration. This passes the heavy
2427 * lifting off to zpool_import_props(), and then mounts the datasets contained
2431 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2432 nvlist_t *props, int flags)
2435 zpool_handle_t *zhp;
2439 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2440 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2442 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2443 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2444 "is formatted using an unsupported ZFS version\n"), name);
2446 } else if (zfs_force_import_required(config) &&
2447 !(flags & ZFS_IMPORT_ANY_HOST)) {
2448 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2451 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2452 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2453 mmp_state = fnvlist_lookup_uint64(nvinfo,
2454 ZPOOL_CONFIG_MMP_STATE);
2456 if (mmp_state == MMP_STATE_ACTIVE) {
2457 char *hostname = "<unknown>";
2458 uint64_t hostid = 0;
2460 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2461 hostname = fnvlist_lookup_string(nvinfo,
2462 ZPOOL_CONFIG_MMP_HOSTNAME);
2464 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2465 hostid = fnvlist_lookup_uint64(nvinfo,
2466 ZPOOL_CONFIG_MMP_HOSTID);
2468 (void) fprintf(stderr, gettext("cannot import '%s': "
2469 "pool is imported on %s (hostid: "
2470 "0x%lx)\nExport the pool on the other system, "
2471 "then run 'zpool import'.\n"),
2472 name, hostname, (unsigned long) hostid);
2473 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2474 (void) fprintf(stderr, gettext("Cannot import '%s': "
2475 "pool has the multihost property on and the\n"
2476 "system's hostid is not set. Set a unique hostid "
2477 "with the zgenhostid(8) command.\n"), name);
2479 char *hostname = "<unknown>";
2480 uint64_t timestamp = 0;
2481 uint64_t hostid = 0;
2483 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2484 hostname = fnvlist_lookup_string(config,
2485 ZPOOL_CONFIG_HOSTNAME);
2487 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2488 timestamp = fnvlist_lookup_uint64(config,
2489 ZPOOL_CONFIG_TIMESTAMP);
2491 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2492 hostid = fnvlist_lookup_uint64(config,
2493 ZPOOL_CONFIG_HOSTID);
2495 (void) fprintf(stderr, gettext("cannot import '%s': "
2496 "pool was previously in use from another system.\n"
2497 "Last accessed by %s (hostid=%lx) at %s"
2498 "The pool can be imported, use 'zpool import -f' "
2499 "to import the pool.\n"), name, hostname,
2500 (unsigned long)hostid, ctime((time_t *)×tamp));
2506 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2509 if (newname != NULL)
2510 name = (char *)newname;
2512 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2516 * Loading keys is best effort. We don't want to return immediately
2517 * if it fails but we do want to give the error to the caller.
2519 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2520 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2525 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2526 !(flags & ZFS_IMPORT_ONLY) &&
2527 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2537 * zpool checkpoint <pool>
2538 * checkpoint --discard <pool>
2540 * -d Discard the checkpoint from a checkpointed
2543 * Checkpoints the specified pool, by taking a "snapshot" of its
2544 * current state. A pool can only have one checkpoint at a time.
2547 zpool_do_checkpoint(int argc, char **argv)
2551 zpool_handle_t *zhp;
2554 struct option long_options[] = {
2555 {"discard", no_argument, NULL, 'd'},
2560 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2566 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2576 (void) fprintf(stderr, gettext("missing pool argument\n"));
2581 (void) fprintf(stderr, gettext("too many arguments\n"));
2587 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2588 /* As a special case, check for use of '/' in the name */
2589 if (strchr(pool, '/') != NULL)
2590 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2591 "doesn't work on datasets. To save the state "
2592 "of a dataset from a specific point in time "
2593 "please use 'zfs snapshot'\n"));
2598 err = (zpool_discard_checkpoint(zhp) != 0);
2600 err = (zpool_checkpoint(zhp) != 0);
2607 #define CHECKPOINT_OPT 1024
2610 * zpool import [-d dir] [-D]
2611 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2612 * [-d dir | -c cachefile] [-f] -a
2613 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2614 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2616 * -c Read pool information from a cachefile instead of searching
2619 * -d Scan in a specific directory, other than /dev/. More than
2620 * one directory can be specified using multiple '-d' options.
2622 * -D Scan for previously destroyed pools or import all or only
2623 * specified destroyed pools.
2625 * -R Temporarily import the pool, with all mountpoints relative to
2626 * the given root. The pool will remain exported when the machine
2629 * -V Import even in the presence of faulted vdevs. This is an
2630 * intentionally undocumented option for testing purposes, and
2631 * treats the pool configuration as complete, leaving any bad
2632 * vdevs in the FAULTED state. In other words, it does verbatim
2635 * -f Force import, even if it appears that the pool is active.
2637 * -F Attempt rewind if necessary.
2639 * -n See if rewind would work, but don't actually rewind.
2641 * -N Import the pool but don't mount datasets.
2643 * -T Specify a starting txg to use for import. This option is
2644 * intentionally undocumented option for testing purposes.
2646 * -a Import all pools found.
2648 * -l Load encryption keys while importing.
2650 * -o Set property=value and/or temporary mount options (without '=').
2652 * -s Scan using the default search path, the libblkid cache will
2655 * --rewind-to-checkpoint
2656 * Import the pool and revert back to the checkpoint.
2658 * The import command scans for pools to import, and import pools based on pool
2659 * name and GUID. The pool can also be renamed as part of the import process.
2662 zpool_do_import(int argc, char **argv)
2664 char **searchdirs = NULL;
2665 char *env, *envdup = NULL;
2669 nvlist_t *pools = NULL;
2670 boolean_t do_all = B_FALSE;
2671 boolean_t do_destroyed = B_FALSE;
2672 char *mntopts = NULL;
2675 uint64_t searchguid = 0;
2676 char *searchname = NULL;
2678 nvlist_t *found_config;
2679 nvlist_t *policy = NULL;
2680 nvlist_t *props = NULL;
2682 int flags = ZFS_IMPORT_NORMAL;
2683 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2684 boolean_t dryrun = B_FALSE;
2685 boolean_t do_rewind = B_FALSE;
2686 boolean_t xtreme_rewind = B_FALSE;
2687 boolean_t do_scan = B_FALSE;
2688 uint64_t pool_state, txg = -1ULL;
2689 char *cachefile = NULL;
2690 importargs_t idata = { 0 };
2693 struct option long_options[] = {
2694 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2699 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
2700 long_options, NULL)) != -1) {
2709 if (searchdirs == NULL) {
2710 searchdirs = safe_malloc(sizeof (char *));
2712 char **tmp = safe_malloc((nsearch + 1) *
2714 bcopy(searchdirs, tmp, nsearch *
2719 searchdirs[nsearch++] = optarg;
2722 do_destroyed = B_TRUE;
2725 flags |= ZFS_IMPORT_ANY_HOST;
2731 flags |= ZFS_IMPORT_LOAD_KEYS;
2734 flags |= ZFS_IMPORT_MISSING_LOG;
2740 flags |= ZFS_IMPORT_ONLY;
2743 if ((propval = strchr(optarg, '=')) != NULL) {
2746 if (add_prop_list(optarg, propval,
2754 if (add_prop_list(zpool_prop_to_name(
2755 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2757 if (add_prop_list_default(zpool_prop_to_name(
2758 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2765 flags |= ZFS_IMPORT_TEMP_NAME;
2766 if (add_prop_list_default(zpool_prop_to_name(
2767 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2773 txg = strtoull(optarg, &endptr, 0);
2774 if (errno != 0 || *endptr != '\0') {
2775 (void) fprintf(stderr,
2776 gettext("invalid txg value\n"));
2779 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2782 flags |= ZFS_IMPORT_VERBATIM;
2785 xtreme_rewind = B_TRUE;
2787 case CHECKPOINT_OPT:
2788 flags |= ZFS_IMPORT_CHECKPOINT;
2791 (void) fprintf(stderr, gettext("missing argument for "
2792 "'%c' option\n"), optopt);
2796 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2805 if (cachefile && nsearch != 0) {
2806 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2810 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2811 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2815 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2816 (void) fprintf(stderr, gettext("-l is only meaningful during "
2821 if ((dryrun || xtreme_rewind) && !do_rewind) {
2822 (void) fprintf(stderr,
2823 gettext("-n or -X only meaningful with -F\n"));
2827 rewind_policy = ZPOOL_TRY_REWIND;
2829 rewind_policy = ZPOOL_DO_REWIND;
2831 rewind_policy |= ZPOOL_EXTREME_REWIND;
2833 /* In the future, we can capture further policy and include it here */
2834 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2835 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
2836 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
2837 rewind_policy) != 0)
2840 /* check argument count */
2843 (void) fprintf(stderr, gettext("too many arguments\n"));
2848 (void) fprintf(stderr, gettext("too many arguments\n"));
2854 * Check for the effective uid. We do this explicitly here because
2855 * otherwise any attempt to discover pools will silently fail.
2857 if (argc == 0 && geteuid() != 0) {
2858 (void) fprintf(stderr, gettext("cannot "
2859 "discover pools: permission denied\n"));
2860 if (searchdirs != NULL)
2864 nvlist_free(policy);
2869 * Depending on the arguments given, we do one of the following:
2871 * <none> Iterate through all pools and display information about
2874 * -a Iterate through all pools and try to import each one.
2876 * <id> Find the pool that corresponds to the given GUID/pool
2877 * name and import that one.
2879 * -D Above options applies only to destroyed pools.
2885 searchguid = strtoull(argv[0], &endptr, 10);
2886 if (errno != 0 || *endptr != '\0') {
2887 searchname = argv[0];
2890 found_config = NULL;
2893 * User specified a name or guid. Ensure it's unique.
2895 idata.unique = B_TRUE;
2899 * Check the environment for the preferred search path.
2901 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2904 envdup = strdup(env);
2906 dir = strtok(envdup, ":");
2907 while (dir != NULL) {
2908 if (searchdirs == NULL) {
2909 searchdirs = safe_malloc(sizeof (char *));
2911 char **tmp = safe_malloc((nsearch + 1) *
2913 bcopy(searchdirs, tmp, nsearch *
2918 searchdirs[nsearch++] = dir;
2919 dir = strtok(NULL, ":");
2923 idata.path = searchdirs;
2924 idata.paths = nsearch;
2925 idata.poolname = searchname;
2926 idata.guid = searchguid;
2927 idata.cachefile = cachefile;
2928 idata.scan = do_scan;
2929 idata.policy = policy;
2931 pools = zpool_search_import(g_zfs, &idata);
2933 if (pools != NULL && idata.exists &&
2934 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2935 (void) fprintf(stderr, gettext("cannot import '%s': "
2936 "a pool with that name already exists\n"),
2938 (void) fprintf(stderr, gettext("use the form '%s "
2939 "<pool | id> <newpool>' to give it a new name\n"),
2942 } else if (pools == NULL && idata.exists) {
2943 (void) fprintf(stderr, gettext("cannot import '%s': "
2944 "a pool with that name is already created/imported,\n"),
2946 (void) fprintf(stderr, gettext("and no additional pools "
2947 "with that name were found\n"));
2949 } else if (pools == NULL) {
2951 (void) fprintf(stderr, gettext("cannot import '%s': "
2952 "no such pool available\n"), argv[0]);
2958 if (searchdirs != NULL)
2962 nvlist_free(policy);
2969 * At this point we have a list of import candidate configs. Even if
2970 * we were searching by pool name or guid, we still need to
2971 * post-process the list to deal with pool state and possible
2977 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2979 verify(nvpair_value_nvlist(elem, &config) == 0);
2981 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2983 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2985 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2988 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2995 (void) printf("\n");
2998 err |= do_import(config, NULL, mntopts,
3001 show_import(config);
3003 } else if (searchname != NULL) {
3007 * We are searching for a pool based on name.
3009 verify(nvlist_lookup_string(config,
3010 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3012 if (strcmp(name, searchname) == 0) {
3013 if (found_config != NULL) {
3014 (void) fprintf(stderr, gettext(
3015 "cannot import '%s': more than "
3016 "one matching pool\n"), searchname);
3017 (void) fprintf(stderr, gettext(
3018 "import by numeric ID instead\n"));
3021 found_config = config;
3027 * Search for a pool by guid.
3029 verify(nvlist_lookup_uint64(config,
3030 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3032 if (guid == searchguid)
3033 found_config = config;
3038 * If we were searching for a specific pool, verify that we found a
3039 * pool, and then do the import.
3041 if (argc != 0 && err == 0) {
3042 if (found_config == NULL) {
3043 (void) fprintf(stderr, gettext("cannot import '%s': "
3044 "no such pool available\n"), argv[0]);
3047 err |= do_import(found_config, argc == 1 ? NULL :
3048 argv[1], mntopts, props, flags);
3053 * If we were just looking for pools, report an error if none were
3056 if (argc == 0 && first)
3057 (void) fprintf(stderr,
3058 gettext("no pools available to import\n"));
3063 nvlist_free(policy);
3064 if (searchdirs != NULL)
3069 return (err ? 1 : 0);
3073 * zpool sync [-f] [pool] ...
3075 * -f (undocumented) force uberblock (and config including zpool cache file)
3078 * Sync the specified pool(s).
3079 * Without arguments "zpool sync" will sync all pools.
3080 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3084 zpool_do_sync(int argc, char **argv)
3087 boolean_t force = B_FALSE;
3090 while ((ret = getopt(argc, argv, "f")) != -1) {
3096 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3105 /* if argc == 0 we will execute zpool_sync_one on all pools */
3106 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
3111 typedef struct iostat_cbdata {
3116 char **cb_vdev_names; /* Only show these vdevs */
3117 unsigned int cb_vdev_names_count;
3118 boolean_t cb_verbose;
3119 boolean_t cb_literal;
3120 boolean_t cb_scripted;
3121 zpool_list_t *cb_list;
3122 vdev_cmd_data_list_t *vcdl;
3126 typedef struct name_and_columns {
3127 const char *name; /* Column name */
3128 unsigned int columns; /* Center name to this number of columns */
3129 } name_and_columns_t;
3131 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
3133 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3135 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3137 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3138 {"asyncq_wait", 2}, {"scrub"}},
3139 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3140 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3142 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
3143 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
3144 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
3145 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
3149 /* Shorthand - if "columns" field not set, default to 1 column */
3150 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3152 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3154 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3155 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
3156 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3157 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
3158 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3159 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
3160 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3161 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
3164 static const char *histo_to_title[] = {
3165 [IOS_L_HISTO] = "latency",
3166 [IOS_RQ_HISTO] = "req_size",
3170 * Return the number of labels in a null-terminated name_and_columns_t
3175 label_array_len(const name_and_columns_t *labels)
3179 while (labels[i].name)
3186 * Return the number of strings in a null-terminated string array.
3189 * const char foo[] = {"bar", "baz", NULL}
3194 str_array_len(const char *array[])
3205 * Return a default column width for default/latency/queue columns. This does
3206 * not include histograms, which have their columns autosized.
3209 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
3211 unsigned long column_width = 5; /* Normal niceprint */
3212 static unsigned long widths[] = {
3214 * Choose some sane default column sizes for printing the
3217 [IOS_DEFAULT] = 15, /* 1PB capacity */
3218 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
3219 [IOS_QUEUES] = 6, /* 1M queue entries */
3223 column_width = widths[type];
3225 return (column_width);
3229 * Print the column labels, i.e:
3231 * capacity operations bandwidth
3232 * alloc free read write read write ...
3234 * If force_column_width is set, use it for the column width. If not set, use
3235 * the default column width.
3238 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
3239 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
3242 unsigned int text_start, rw_column_width, spaces_to_end;
3243 uint64_t flags = cb->cb_flags;
3245 unsigned int column_width = force_column_width;
3247 /* For each bit set in flags */
3248 for (f = flags; f; f &= ~(1ULL << idx)) {
3249 idx = lowbit64(f) - 1;
3250 if (!force_column_width)
3251 column_width = default_column_width(cb, idx);
3252 /* Print our top labels centered over "read write" label. */
3253 for (i = 0; i < label_array_len(labels[idx]); i++) {
3254 const char *name = labels[idx][i].name;
3256 * We treat labels[][].columns == 0 as shorthand
3257 * for one column. It makes writing out the label
3258 * tables more concise.
3260 unsigned int columns = MAX(1, labels[idx][i].columns);
3261 unsigned int slen = strlen(name);
3263 rw_column_width = (column_width * columns) +
3264 (2 * (columns - 1));
3266 text_start = (int)((rw_column_width)/columns -
3269 printf(" "); /* Two spaces between columns */
3271 /* Space from beginning of column to label */
3272 for (s = 0; s < text_start; s++)
3277 /* Print space after label to end of column */
3278 spaces_to_end = rw_column_width - text_start - slen;
3279 for (s = 0; s < spaces_to_end; s++)
3288 * print_cmd_columns - Print custom column titles from -c
3290 * If the user specified the "zpool status|iostat -c" then print their custom
3291 * column titles in the header. For example, print_cmd_columns() would print
3292 * the " col1 col2" part of this:
3294 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3296 * capacity operations bandwidth
3297 * pool alloc free read write read write col1 col2
3298 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3299 * mypool 269K 1008M 0 0 107 946
3300 * mirror 269K 1008M 0 0 107 946
3301 * sdb - - 0 0 102 473 val1 val2
3302 * sdc - - 0 0 5 473 val1 val2
3303 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3306 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3309 vdev_cmd_data_t *data = &vcdl->data[0];
3311 if (vcdl->count == 0 || data == NULL)
3315 * Each vdev cmd should have the same column names unless the user did
3316 * something weird with their cmd. Just take the column names from the
3317 * first vdev and assume it works for all of them.
3319 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3322 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3325 printf("%*s", vcdl->uniq_cols_width[i],
3326 vcdl->uniq_cols[i]);
3333 * Utility function to print out a line of dashes like:
3335 * -------------------------------- ----- ----- ----- ----- -----
3337 * ...or a dashed named-row line like:
3343 * @force_column_width If non-zero, use the value as the column width.
3344 * Otherwise use the default column widths.
3346 * @name: Print a dashed named-row line starting
3347 * with @name. Otherwise, print a regular
3351 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3355 unsigned int namewidth;
3356 uint64_t flags = cb->cb_flags;
3359 const name_and_columns_t *labels;
3363 if (cb->cb_flags & IOS_ANYHISTO_M) {
3364 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3365 } else if (cb->cb_vdev_names_count) {
3371 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3372 name ? strlen(name) : 0);
3376 printf("%-*s", namewidth, name);
3378 for (i = 0; i < namewidth; i++)
3382 /* For each bit in flags */
3383 for (f = flags; f; f &= ~(1ULL << idx)) {
3384 unsigned int column_width;
3385 idx = lowbit64(f) - 1;
3386 if (force_column_width)
3387 column_width = force_column_width;
3389 column_width = default_column_width(cb, idx);
3391 labels = iostat_bottom_labels[idx];
3392 for (i = 0; i < label_array_len(labels); i++) {
3394 printf(" %*s-", column_width - 1, " ");
3396 printf(" %.*s", column_width,
3397 "--------------------");
3404 print_iostat_separator_impl(iostat_cbdata_t *cb,
3405 unsigned int force_column_width)
3407 print_iostat_dashes(cb, force_column_width, NULL);
3411 print_iostat_separator(iostat_cbdata_t *cb)
3413 print_iostat_separator_impl(cb, 0);
3417 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3418 const char *histo_vdev_name)
3420 unsigned int namewidth;
3423 if (cb->cb_flags & IOS_ANYHISTO_M) {
3424 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3425 } else if (cb->cb_vdev_names_count) {
3431 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3432 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3434 if (histo_vdev_name)
3435 printf("%-*s", namewidth, histo_vdev_name);
3437 printf("%*s", namewidth, "");
3440 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3443 printf("%-*s", namewidth, title);
3445 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3446 if (cb->vcdl != NULL)
3447 print_cmd_columns(cb->vcdl, 0);
3451 print_iostat_separator_impl(cb, force_column_width);
3453 if (cb->vcdl != NULL)
3454 print_cmd_columns(cb->vcdl, 1);
3460 print_iostat_header(iostat_cbdata_t *cb)
3462 print_iostat_header_impl(cb, 0, NULL);
3467 * Display a single statistic.
3470 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3471 unsigned int column_size, boolean_t scripted)
3475 zfs_nicenum_format(value, buf, sizeof (buf), format);
3478 printf("\t%s", buf);
3480 printf(" %*s", column_size, buf);
3484 * Calculate the default vdev stats
3486 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3487 * stats into calcvs.
3490 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3491 vdev_stat_t *calcvs)
3495 memcpy(calcvs, newvs, sizeof (*calcvs));
3496 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3497 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3499 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3500 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3504 * Internal representation of the extended iostats data.
3506 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3507 * or single uint64_t's. We make both look like arrays to make them easier
3508 * to process. In order to make single uint64_t's look like arrays, we set
3509 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3510 * we can just use *data and count.
3514 uint_t count; /* Number of entries in data[] */
3515 uint64_t __data; /* Only used when data is a single uint64_t */
3519 stat_histo_max(struct stat_array *nva, unsigned int len)
3523 for (i = 0; i < len; i++)
3524 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3530 * Helper function to lookup a uint64_t array or uint64_t value and store its
3531 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3532 * it look like a one element array to make it easier to process.
3535 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3536 struct stat_array *nva)
3541 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3542 switch (nvpair_type(tmp)) {
3543 case DATA_TYPE_UINT64_ARRAY:
3544 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3546 case DATA_TYPE_UINT64:
3547 ret = nvpair_value_uint64(tmp, &nva->__data);
3548 nva->data = &nva->__data;
3552 /* Not a uint64_t */
3561 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3562 * subtract them, and return the results in a newly allocated stat_array.
3563 * You must free the returned array after you are done with it with
3564 * free_calc_stats().
3566 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3569 static struct stat_array *
3570 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3573 nvlist_t *oldnvx = NULL, *newnvx;
3574 struct stat_array *oldnva, *newnva, *calcnva;
3576 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3578 /* Extract our extended stats nvlist from the main list */
3579 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3582 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3586 newnva = safe_malloc(alloc_size);
3587 oldnva = safe_malloc(alloc_size);
3588 calcnva = safe_malloc(alloc_size);
3590 for (j = 0; j < len; j++) {
3591 verify(nvpair64_to_stat_array(newnvx, names[j],
3593 calcnva[j].count = newnva[j].count;
3594 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3595 calcnva[j].data = safe_malloc(alloc_size);
3596 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3599 verify(nvpair64_to_stat_array(oldnvx, names[j],
3601 for (i = 0; i < oldnva[j].count; i++)
3602 calcnva[j].data[i] -= oldnva[j].data[i];
3611 free_calc_stats(struct stat_array *nva, unsigned int len)
3614 for (i = 0; i < len; i++)
3621 print_iostat_histo(struct stat_array *nva, unsigned int len,
3622 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3628 enum zfs_nicenum_format format;
3629 unsigned int buckets;
3630 unsigned int start_bucket;
3633 format = ZFS_NICENUM_RAW;
3635 format = ZFS_NICENUM_1024;
3637 /* All these histos are the same size, so just use nva[0].count */
3638 buckets = nva[0].count;
3640 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3641 /* Start at 512 - req size should never be lower than this */
3647 for (j = start_bucket; j < buckets; j++) {
3648 /* Print histogram bucket label */
3649 if (cb->cb_flags & IOS_L_HISTO_M) {
3650 /* Ending range of this bucket */
3651 val = (1UL << (j + 1)) - 1;
3652 zfs_nicetime(val, buf, sizeof (buf));
3654 /* Request size (starting range of bucket) */
3656 zfs_nicenum(val, buf, sizeof (buf));
3659 if (cb->cb_scripted)
3660 printf("%llu", (u_longlong_t)val);
3662 printf("%-*s", namewidth, buf);
3664 /* Print the values on the line */
3665 for (i = 0; i < len; i++) {
3666 print_one_stat(nva[i].data[j] * scale, format,
3667 column_width, cb->cb_scripted);
3674 print_solid_separator(unsigned int length)
3682 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3683 nvlist_t *newnv, double scale, const char *name)
3685 unsigned int column_width;
3686 unsigned int namewidth;
3687 unsigned int entire_width;
3688 enum iostat_type type;
3689 struct stat_array *nva;
3691 unsigned int names_len;
3693 /* What type of histo are we? */
3694 type = IOS_HISTO_IDX(cb->cb_flags);
3696 /* Get NULL-terminated array of nvlist names for our histo */
3697 names = vsx_type_to_nvlist[type];
3698 names_len = str_array_len(names); /* num of names */
3700 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3702 if (cb->cb_literal) {
3703 column_width = MAX(5,
3704 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3709 namewidth = MAX(cb->cb_namewidth,
3710 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3713 * Calculate the entire line width of what we're printing. The
3714 * +2 is for the two spaces between columns:
3718 /* |___| <---------- column_width */
3720 /* |__________| <--- entire_width */
3722 entire_width = namewidth + (column_width + 2) *
3723 label_array_len(iostat_bottom_labels[type]);
3725 if (cb->cb_scripted)
3726 printf("%s\n", name);
3728 print_iostat_header_impl(cb, column_width, name);
3730 print_iostat_histo(nva, names_len, cb, column_width,
3733 free_calc_stats(nva, names_len);
3734 if (!cb->cb_scripted)
3735 print_solid_separator(entire_width);
3739 * Calculate the average latency of a power-of-two latency histogram
3742 single_histo_average(uint64_t *histo, unsigned int buckets)
3745 uint64_t count = 0, total = 0;
3747 for (i = 0; i < buckets; i++) {
3749 * Our buckets are power-of-two latency ranges. Use the
3750 * midpoint latency of each bucket to calculate the average.
3758 if (histo[i] != 0) {
3759 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3764 /* Prevent divide by zero */
3765 return (count == 0 ? 0 : total / count);
3769 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3774 const char *names[] = {
3775 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3776 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3777 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3778 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3779 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3780 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3781 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3782 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3783 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3784 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3787 struct stat_array *nva;
3789 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3790 enum zfs_nicenum_format format;
3792 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3795 format = ZFS_NICENUM_RAW;
3797 format = ZFS_NICENUM_1024;
3799 for (i = 0; i < ARRAY_SIZE(names); i++) {
3800 val = nva[i].data[0];
3801 print_one_stat(val, format, column_width, cb->cb_scripted);
3804 free_calc_stats(nva, ARRAY_SIZE(names));
3808 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3813 const char *names[] = {
3814 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3815 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3816 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3817 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3818 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3819 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3820 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3821 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3822 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3824 struct stat_array *nva;
3826 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3827 enum zfs_nicenum_format format;
3829 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3832 format = ZFS_NICENUM_RAWTIME;
3834 format = ZFS_NICENUM_TIME;
3836 /* Print our avg latencies on the line */
3837 for (i = 0; i < ARRAY_SIZE(names); i++) {
3838 /* Compute average latency for a latency histo */
3839 val = single_histo_average(nva[i].data, nva[i].count);
3840 print_one_stat(val, format, column_width, cb->cb_scripted);
3842 free_calc_stats(nva, ARRAY_SIZE(names));
3846 * Print default statistics (capacity/operations/bandwidth)
3849 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3851 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3852 enum zfs_nicenum_format format;
3853 char na; /* char to print for "not applicable" values */
3855 if (cb->cb_literal) {
3856 format = ZFS_NICENUM_RAW;
3859 format = ZFS_NICENUM_1024;
3863 /* only toplevel vdevs have capacity stats */
3864 if (vs->vs_space == 0) {
3865 if (cb->cb_scripted)
3866 printf("\t%c\t%c", na, na);
3868 printf(" %*c %*c", column_width, na, column_width,
3871 print_one_stat(vs->vs_alloc, format, column_width,
3873 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3874 column_width, cb->cb_scripted);
3877 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3878 format, column_width, cb->cb_scripted);
3879 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3880 format, column_width, cb->cb_scripted);
3881 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3882 format, column_width, cb->cb_scripted);
3883 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3884 format, column_width, cb->cb_scripted);
3887 static const char *class_name[] = {
3888 VDEV_ALLOC_BIAS_DEDUP,
3889 VDEV_ALLOC_BIAS_SPECIAL,
3890 VDEV_ALLOC_CLASS_LOGS
3894 * Print out all the statistics for the given vdev. This can either be the
3895 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3896 * is a verbose output, and we don't want to display the toplevel pool stats.
3898 * Returns the number of stat lines printed.
3901 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3902 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3904 nvlist_t **oldchild, **newchild;
3905 uint_t c, children, oldchildren;
3906 vdev_stat_t *oldvs, *newvs, *calcvs;
3907 vdev_stat_t zerovs = { 0 };
3914 calcvs = safe_malloc(sizeof (*calcvs));
3916 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3919 if (oldnv != NULL) {
3920 verify(nvlist_lookup_uint64_array(oldnv,
3921 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3926 /* Do we only want to see a specific vdev? */
3927 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3928 /* Yes we do. Is this the vdev? */
3929 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3931 * This is our vdev. Since it is the only vdev we
3932 * will be displaying, make depth = 0 so that it
3933 * doesn't get indented.
3940 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3941 /* Couldn't match the name */
3946 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3947 (uint64_t **)&newvs, &c) == 0);
3950 * Print the vdev name unless it's is a histogram. Histograms
3951 * display the vdev name in the header itself.
3953 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3954 if (cb->cb_scripted) {
3957 if (strlen(name) + depth > cb->cb_namewidth)
3958 (void) printf("%*s%s", depth, "", name);
3960 (void) printf("%*s%s%*s", depth, "", name,
3961 (int)(cb->cb_namewidth - strlen(name) -
3966 /* Calculate our scaling factor */
3967 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3968 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3970 * If we specify printing histograms with no time interval, then
3971 * print the histogram numbers over the entire lifetime of the
3979 scale = (double)NANOSEC / tdelta;
3982 if (cb->cb_flags & IOS_DEFAULT_M) {
3983 calc_default_iostats(oldvs, newvs, calcvs);
3984 print_iostat_default(calcvs, cb, scale);
3986 if (cb->cb_flags & IOS_LATENCY_M)
3987 print_iostat_latency(cb, oldnv, newnv);
3988 if (cb->cb_flags & IOS_QUEUES_M)
3989 print_iostat_queues(cb, oldnv, newnv);
3990 if (cb->cb_flags & IOS_ANYHISTO_M) {
3992 print_iostat_histos(cb, oldnv, newnv, scale, name);
3995 if (cb->vcdl != NULL) {
3997 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
4000 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
4004 if (!(cb->cb_flags & IOS_ANYHISTO_M))
4013 if (!cb->cb_verbose)
4016 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4017 &newchild, &children) != 0)
4021 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4022 &oldchild, &oldchildren) != 0)
4025 children = MIN(oldchildren, children);
4029 * print normal top-level devices
4031 for (c = 0; c < children; c++) {
4032 uint64_t ishole = B_FALSE, islog = B_FALSE;
4034 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4037 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4040 if (ishole || islog)
4043 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4046 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4048 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
4049 newchild[c], cb, depth + 2);
4054 * print all other top-level devices
4056 for (uint_t n = 0; n < 3; n++) {
4057 boolean_t printed = B_FALSE;
4059 for (c = 0; c < children; c++) {
4060 uint64_t islog = B_FALSE;
4064 (void) nvlist_lookup_uint64(newchild[c],
4065 ZPOOL_CONFIG_IS_LOG, &islog);
4067 bias = VDEV_ALLOC_CLASS_LOGS;
4069 (void) nvlist_lookup_string(newchild[c],
4070 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4071 (void) nvlist_lookup_string(newchild[c],
4072 ZPOOL_CONFIG_TYPE, &type);
4074 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4076 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4080 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
4081 !cb->cb_scripted && !cb->cb_vdev_names) {
4082 print_iostat_dashes(cb, 0,
4089 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4091 ret += print_vdev_stats(zhp, vname, oldnv ?
4092 oldchild[c] : NULL, newchild[c], cb, depth + 2);
4098 * Include level 2 ARC devices in iostat output
4100 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4101 &newchild, &children) != 0)
4105 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4106 &oldchild, &oldchildren) != 0)
4109 children = MIN(oldchildren, children);
4113 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
4114 !cb->cb_vdev_names) {
4115 print_iostat_dashes(cb, 0, "cache");
4119 for (c = 0; c < children; c++) {
4120 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4122 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4123 : NULL, newchild[c], cb, depth + 2);
4132 refresh_iostat(zpool_handle_t *zhp, void *data)
4134 iostat_cbdata_t *cb = data;
4138 * If the pool has disappeared, remove it from the list and continue.
4140 if (zpool_refresh_stats(zhp, &missing) != 0)
4144 pool_list_remove(cb->cb_list, zhp);
4150 * Callback to print out the iostats for the given pool.
4153 print_iostat(zpool_handle_t *zhp, void *data)
4155 iostat_cbdata_t *cb = data;
4156 nvlist_t *oldconfig, *newconfig;
4157 nvlist_t *oldnvroot, *newnvroot;
4160 newconfig = zpool_get_config(zhp, &oldconfig);
4162 if (cb->cb_iteration == 1)
4165 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
4168 if (oldconfig == NULL)
4171 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
4174 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
4176 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
4177 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
4178 print_iostat_separator(cb);
4179 if (cb->vcdl != NULL) {
4180 print_cmd_columns(cb->vcdl, 1);
4195 if (isatty(STDOUT_FILENO)) {
4196 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
4198 columns = ws.ws_col;
4207 get_namewidth(zpool_handle_t *zhp, void *data)
4209 iostat_cbdata_t *cb = data;
4210 nvlist_t *config, *nvroot;
4213 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
4214 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4216 unsigned int poolname_len = strlen(zpool_get_name(zhp));
4217 if (!cb->cb_verbose)
4218 cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
4220 cb->cb_namewidth = MAX(poolname_len,
4221 max_width(zhp, nvroot, 0, cb->cb_namewidth,
4222 cb->cb_name_flags));
4225 * The width must be at least 10, but may be as large as the
4226 * column width - 42 so that we can still fit in one line.
4228 columns = get_columns();
4230 if (cb->cb_namewidth < 10)
4231 cb->cb_namewidth = 10;
4232 if (cb->cb_namewidth > columns - 42)
4233 cb->cb_namewidth = columns - 42;
4239 * Parse the input string, get the 'interval' and 'count' value if there is one.
4242 get_interval_count(int *argcp, char **argv, float *iv,
4246 unsigned long count = 0;
4250 * Determine if the last argument is an integer or a pool name
4252 if (argc > 0 && isnumber(argv[argc - 1])) {
4256 interval = strtof(argv[argc - 1], &end);
4258 if (*end == '\0' && errno == 0) {
4259 if (interval == 0) {
4260 (void) fprintf(stderr, gettext("interval "
4261 "cannot be zero\n"));
4265 * Ignore the last parameter
4270 * If this is not a valid number, just plow on. The
4271 * user will get a more informative error message later
4279 * If the last argument is also an integer, then we have both a count
4282 if (argc > 0 && isnumber(argv[argc - 1])) {
4287 interval = strtof(argv[argc - 1], &end);
4289 if (*end == '\0' && errno == 0) {
4290 if (interval == 0) {
4291 (void) fprintf(stderr, gettext("interval "
4292 "cannot be zero\n"));
4297 * Ignore the last parameter
4311 get_timestamp_arg(char c)
4314 timestamp_fmt = UDATE;
4316 timestamp_fmt = DDATE;
4322 * Return stat flags that are supported by all pools by both the module and
4323 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4324 * It will get ANDed down until only the flags that are supported on all pools
4328 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4330 uint64_t *mask = data;
4331 nvlist_t *config, *nvroot, *nvx;
4335 config = zpool_get_config(zhp, NULL);
4336 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4339 /* Default stats are always supported, but for completeness.. */
4340 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4341 flags |= IOS_DEFAULT_M;
4343 /* Get our extended stats nvlist from the main list */
4344 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4347 * No extended stats; they're probably running an older
4348 * module. No big deal, we support that too.
4353 /* For each extended stat, make sure all its nvpairs are supported */
4354 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4355 if (!vsx_type_to_nvlist[j][0])
4358 /* Start off by assuming the flag is supported, then check */
4359 flags |= (1ULL << j);
4360 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4361 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4362 /* flag isn't supported */
4363 flags = flags & ~(1ULL << j);
4369 *mask = *mask & flags;
4374 * Return a bitmask of stats that are supported on all pools by both the module
4378 get_stat_flags(zpool_list_t *list)
4383 * get_stat_flags_cb() will lop off bits from "mask" until only the
4384 * flags that are supported on all pools remain.
4386 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4391 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4394 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4396 iostat_cbdata_t *cb = cb_data;
4400 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4402 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4403 ret = 1; /* match */
4410 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4413 is_vdev(zpool_handle_t *zhp, void *cb_data)
4415 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4419 * Check if vdevs are in a pool
4421 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4422 * return 0. If pool_name is NULL, then search all pools.
4425 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4426 iostat_cbdata_t *cb)
4433 if ((argc == 0) || !*argv)
4439 /* Temporarily hijack cb_vdev_names for a second... */
4440 tmp_name = cb->cb_vdev_names;
4442 /* Go though our list of prospective vdev names */
4443 for (i = 0; i < argc; i++) {
4444 cb->cb_vdev_names = argv + i;
4446 /* Is this name a vdev in our pools? */
4447 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4455 cb->cb_vdev_names = tmp_name;
4461 is_pool_cb(zpool_handle_t *zhp, void *data)
4464 if (strcmp(name, zpool_get_name(zhp)) == 0)
4471 * Do we have a pool named *name? If so, return 1, otherwise 0.
4476 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4479 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4481 are_all_pools(int argc, char **argv)
4483 if ((argc == 0) || !*argv)
4487 if (!is_pool(argv[argc]))
4494 * Helper function to print out vdev/pool names we can't resolve. Used for an
4498 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4499 iostat_cbdata_t *cb)
4504 for (i = 0; i < argc; i++) {
4508 str = gettext("pool");
4509 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4510 str = gettext("vdev in this pool");
4511 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4512 str = gettext("vdev in another pool");
4514 str = gettext("unknown");
4516 fprintf(stderr, "\t%s (%s)\n", name, str);
4521 * Same as get_interval_count(), but with additional checks to not misinterpret
4522 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4526 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4527 unsigned long *count, iostat_cbdata_t *cb)
4529 char **tmpargv = argv;
4530 int argc_for_interval = 0;
4532 /* Is the last arg an interval value? Or a guid? */
4533 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4535 * The last arg is not a guid, so it's probably an
4538 argc_for_interval++;
4541 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4543 * The 2nd to last arg is not a guid, so it's probably
4544 * an interval value.
4546 argc_for_interval++;
4550 /* Point to our list of possible intervals */
4551 tmpargv = &argv[*argc - argc_for_interval];
4553 *argc = *argc - argc_for_interval;
4554 get_interval_count(&argc_for_interval, tmpargv,
4559 * Floating point sleep(). Allows you to pass in a floating point value for
4565 struct timespec req;
4566 req.tv_sec = floor(sec);
4567 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4568 nanosleep(&req, NULL);
4572 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4575 * name: Short name of the script ('iostat').
4576 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4579 print_zpool_script_help(char *name, char *path)
4581 char *argv[] = {path, "-h", NULL};
4582 char **lines = NULL;
4586 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4588 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4590 libzfs_free_str_array(lines, lines_cnt);
4594 for (int i = 0; i < lines_cnt; i++)
4595 if (!is_blank_str(lines[i]))
4596 printf(" %-14s %s\n", name, lines[i]);
4598 libzfs_free_str_array(lines, lines_cnt);
4602 * Go though the zpool status/iostat -c scripts in the user's path, run their
4603 * help option (-h), and print out the results.
4606 print_zpool_dir_scripts(char *dirpath)
4610 char fullpath[MAXPATHLEN];
4611 struct stat dir_stat;
4613 if ((dir = opendir(dirpath)) != NULL) {
4614 /* print all the files and directories within directory */
4615 while ((ent = readdir(dir)) != NULL) {
4616 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4618 /* Print the scripts */
4619 if (stat(fullpath, &dir_stat) == 0)
4620 if (dir_stat.st_mode & S_IXUSR &&
4621 S_ISREG(dir_stat.st_mode))
4622 print_zpool_script_help(ent->d_name,
4630 * Print out help text for all zpool status/iostat -c scripts.
4633 print_zpool_script_list(char *subcommand)
4637 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4639 sp = zpool_get_cmd_search_path();
4643 dir = strtok(sp, ":");
4644 while (dir != NULL) {
4645 print_zpool_dir_scripts(dir);
4646 dir = strtok(NULL, ":");
4653 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4654 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4655 * [interval [count]]
4657 * -c CMD For each vdev, run command CMD
4658 * -g Display guid for individual vdev name.
4659 * -L Follow links when resolving vdev path name.
4660 * -P Display full path for vdev name.
4661 * -v Display statistics for individual vdevs
4663 * -p Display values in parsable (exact) format.
4664 * -H Scripted mode. Don't display headers, and separate properties
4666 * -l Display average latency
4667 * -q Display queue depths
4668 * -w Display latency histograms
4669 * -r Display request size histogram
4670 * -T Display a timestamp in date(1) or Unix format
4672 * This command can be tricky because we want to be able to deal with pool
4673 * creation/destruction as well as vdev configuration changes. The bulk of this
4674 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4675 * on pool_list_update() to detect the addition of new pools. Configuration
4676 * changes are all handled within libzfs.
4679 zpool_do_iostat(int argc, char **argv)
4685 unsigned long count = 0;
4687 boolean_t verbose = B_FALSE;
4688 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4689 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4690 boolean_t omit_since_boot = B_FALSE;
4691 boolean_t guid = B_FALSE;
4692 boolean_t follow_links = B_FALSE;
4693 boolean_t full_name = B_FALSE;
4694 iostat_cbdata_t cb = { 0 };
4697 /* Used for printing error message */
4698 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4699 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4701 uint64_t unsupported_flags;
4704 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4709 gettext("Can't set -c flag twice\n"));
4713 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4714 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4715 fprintf(stderr, gettext(
4716 "Can't run -c, disabled by "
4717 "ZPOOL_SCRIPTS_ENABLED.\n"));
4721 if ((getuid() <= 0 || geteuid() <= 0) &&
4722 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4723 fprintf(stderr, gettext(
4724 "Can't run -c with root privileges "
4725 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4735 follow_links = B_TRUE;
4741 get_timestamp_arg(*optarg);
4765 omit_since_boot = B_TRUE;
4771 if (optopt == 'c') {
4772 print_zpool_script_list("iostat");
4776 gettext("invalid option '%c'\n"), optopt);
4785 cb.cb_literal = parsable;
4786 cb.cb_scripted = scripted;
4789 cb.cb_name_flags |= VDEV_NAME_GUID;
4791 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4793 cb.cb_name_flags |= VDEV_NAME_PATH;
4794 cb.cb_iteration = 0;
4795 cb.cb_namewidth = 0;
4796 cb.cb_verbose = verbose;
4798 /* Get our interval and count values (if any) */
4800 get_interval_count_filter_guids(&argc, argv, &interval,
4803 get_interval_count(&argc, argv, &interval, &count);
4807 /* No args, so just print the defaults. */
4808 } else if (are_all_pools(argc, argv)) {
4809 /* All the args are pool names */
4810 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4811 /* All the args are vdevs */
4812 cb.cb_vdev_names = argv;
4813 cb.cb_vdev_names_count = argc;
4814 argc = 0; /* No pools to process */
4815 } else if (are_all_pools(1, argv)) {
4816 /* The first arg is a pool name */
4817 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4818 /* ...and the rest are vdev names */
4819 cb.cb_vdev_names = argv + 1;
4820 cb.cb_vdev_names_count = argc - 1;
4821 argc = 1; /* One pool to process */
4823 fprintf(stderr, gettext("Expected either a list of "));
4824 fprintf(stderr, gettext("pools, or list of vdevs in"));
4825 fprintf(stderr, " \"%s\", ", argv[0]);
4826 fprintf(stderr, gettext("but got:\n"));
4827 error_list_unresolved_vdevs(argc - 1, argv + 1,
4829 fprintf(stderr, "\n");
4835 * The args don't make sense. The first arg isn't a pool name,
4836 * nor are all the args vdevs.
4838 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4839 fprintf(stderr, "\n");
4843 if (cb.cb_vdev_names_count != 0) {
4845 * If user specified vdevs, it implies verbose.
4847 cb.cb_verbose = B_TRUE;
4851 * Construct the list of all interesting pools.
4854 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4857 if (pool_list_count(list) == 0 && argc != 0) {
4858 pool_list_free(list);
4862 if (pool_list_count(list) == 0 && interval == 0) {
4863 pool_list_free(list);
4864 (void) fprintf(stderr, gettext("no pools available\n"));
4868 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4869 pool_list_free(list);
4870 (void) fprintf(stderr,
4871 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4876 if (l_histo && rq_histo) {
4877 pool_list_free(list);
4878 (void) fprintf(stderr,
4879 gettext("Only one of [-r|-w] can be passed at a time\n"));
4885 * Enter the main iostat loop.
4891 * Histograms tables look out of place when you try to display
4892 * them with the other stats, so make a rule that you can only
4893 * print histograms by themselves.
4895 cb.cb_flags = IOS_L_HISTO_M;
4896 } else if (rq_histo) {
4897 cb.cb_flags = IOS_RQ_HISTO_M;
4899 cb.cb_flags = IOS_DEFAULT_M;
4901 cb.cb_flags |= IOS_LATENCY_M;
4903 cb.cb_flags |= IOS_QUEUES_M;
4907 * See if the module supports all the stats we want to display.
4909 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4910 if (unsupported_flags) {
4914 gettext("The loaded zfs module doesn't support:"));
4916 /* for each bit set in unsupported_flags */
4917 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4918 idx = lowbit64(f) - 1;
4919 fprintf(stderr, " -%c", flag_to_arg[idx]);
4922 fprintf(stderr, ". Try running a newer module.\n");
4923 pool_list_free(list);
4929 if ((npools = pool_list_count(list)) == 0)
4930 (void) fprintf(stderr, gettext("no pools available\n"));
4933 * If this is the first iteration and -y was supplied
4934 * we skip any printing.
4936 boolean_t skip = (omit_since_boot &&
4937 cb.cb_iteration == 0);
4940 * Refresh all statistics. This is done as an
4941 * explicit step before calculating the maximum name
4942 * width, so that any * configuration changes are
4943 * properly accounted for.
4945 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4949 * Iterate over all pools to determine the maximum width
4950 * for the pool / device name column across all pools.
4952 cb.cb_namewidth = 0;
4953 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4956 if (timestamp_fmt != NODATE)
4957 print_timestamp(timestamp_fmt);
4959 if (cmd != NULL && cb.cb_verbose &&
4960 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4961 cb.vcdl = all_pools_for_each_vdev_run(argc,
4962 argv, cmd, g_zfs, cb.cb_vdev_names,
4963 cb.cb_vdev_names_count, cb.cb_name_flags);
4969 * If it's the first time and we're not skipping it,
4970 * or either skip or verbose mode, print the header.
4972 * The histogram code explicitly prints its header on
4973 * every vdev, so skip this for histograms.
4975 if (((++cb.cb_iteration == 1 && !skip) ||
4976 (skip != verbose)) &&
4977 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4979 print_iostat_header(&cb);
4982 (void) fsleep(interval);
4987 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4990 * If there's more than one pool, and we're not in
4991 * verbose mode (which prints a separator for us),
4992 * then print a separator.
4994 * In addition, if we're printing specific vdevs then
4995 * we also want an ending separator.
4997 if (((npools > 1 && !verbose &&
4998 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4999 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
5000 cb.cb_vdev_names_count)) &&
5002 print_iostat_separator(&cb);
5003 if (cb.vcdl != NULL)
5004 print_cmd_columns(cb.vcdl, 1);
5008 if (cb.vcdl != NULL)
5009 free_vdev_cmd_data_list(cb.vcdl);
5014 * Flush the output so that redirection to a file isn't buffered
5017 (void) fflush(stdout);
5022 if (count != 0 && --count == 0)
5025 (void) fsleep(interval);
5028 pool_list_free(list);
5033 typedef struct list_cbdata {
5034 boolean_t cb_verbose;
5037 boolean_t cb_scripted;
5038 zprop_list_t *cb_proplist;
5039 boolean_t cb_literal;
5044 * Given a list of columns to display, output appropriate headers for each one.
5047 print_header(list_cbdata_t *cb)
5049 zprop_list_t *pl = cb->cb_proplist;
5050 char headerbuf[ZPOOL_MAXPROPLEN];
5052 boolean_t first = B_TRUE;
5053 boolean_t right_justify;
5056 for (; pl != NULL; pl = pl->pl_next) {
5057 width = pl->pl_width;
5058 if (first && cb->cb_verbose) {
5060 * Reset the width to accommodate the verbose listing
5063 width = cb->cb_namewidth;
5071 right_justify = B_FALSE;
5072 if (pl->pl_prop != ZPROP_INVAL) {
5073 header = zpool_prop_column_name(pl->pl_prop);
5074 right_justify = zpool_prop_align_right(pl->pl_prop);
5078 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5079 headerbuf[i] = toupper(pl->pl_user_prop[i]);
5080 headerbuf[i] = '\0';
5084 if (pl->pl_next == NULL && !right_justify)
5085 (void) printf("%s", header);
5086 else if (right_justify)
5087 (void) printf("%*s", (int)width, header);
5089 (void) printf("%-*s", (int)width, header);
5092 (void) printf("\n");
5096 * Given a pool and a list of properties, print out all the properties according
5097 * to the described layout. Used by zpool_do_list().
5100 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
5102 zprop_list_t *pl = cb->cb_proplist;
5103 boolean_t first = B_TRUE;
5104 char property[ZPOOL_MAXPROPLEN];
5106 boolean_t right_justify;
5109 for (; pl != NULL; pl = pl->pl_next) {
5111 width = pl->pl_width;
5112 if (first && cb->cb_verbose) {
5114 * Reset the width to accommodate the verbose listing
5117 width = cb->cb_namewidth;
5121 if (cb->cb_scripted)
5122 (void) printf("\t");
5129 right_justify = B_FALSE;
5130 if (pl->pl_prop != ZPROP_INVAL) {
5131 if (zpool_get_prop(zhp, pl->pl_prop, property,
5132 sizeof (property), NULL, cb->cb_literal) != 0)
5137 right_justify = zpool_prop_align_right(pl->pl_prop);
5138 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
5139 zpool_prop_unsupported(pl->pl_user_prop)) &&
5140 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
5141 sizeof (property)) == 0) {
5149 * If this is being called in scripted mode, or if this is the
5150 * last column and it is left-justified, don't include a width
5153 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
5154 (void) printf("%s", propstr);
5155 else if (right_justify)
5156 (void) printf("%*s", (int)width, propstr);
5158 (void) printf("%-*s", (int)width, propstr);
5161 (void) printf("\n");
5165 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
5166 boolean_t valid, enum zfs_nicenum_format format)
5170 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
5173 case ZPOOL_PROP_EXPANDSZ:
5174 case ZPOOL_PROP_CHECKPOINT:
5176 (void) strlcpy(propval, "-", sizeof (propval));
5178 zfs_nicenum_format(value, propval, sizeof (propval),
5181 case ZPOOL_PROP_FRAGMENTATION:
5182 if (value == ZFS_FRAG_INVALID) {
5183 (void) strlcpy(propval, "-", sizeof (propval));
5184 } else if (format == ZFS_NICENUM_RAW) {
5185 (void) snprintf(propval, sizeof (propval), "%llu",
5186 (unsigned long long)value);
5188 (void) snprintf(propval, sizeof (propval), "%llu%%",
5189 (unsigned long long)value);
5192 case ZPOOL_PROP_CAPACITY:
5193 /* capacity value is in parts-per-10,000 (aka permyriad) */
5194 if (format == ZFS_NICENUM_RAW)
5195 (void) snprintf(propval, sizeof (propval), "%llu",
5196 (unsigned long long)value / 100);
5198 (void) snprintf(propval, sizeof (propval),
5199 value < 1000 ? "%1.2f%%" : value < 10000 ?
5200 "%2.1f%%" : "%3.0f%%", value / 100.0);
5203 zfs_nicenum_format(value, propval, sizeof (propval), format);
5207 (void) strlcpy(propval, "-", sizeof (propval));
5210 (void) printf("\t%s", propval);
5212 (void) printf(" %*s", (int)width, propval);
5216 * print static default line per vdev
5217 * not compatible with '-o' <proplist> option
5220 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
5221 list_cbdata_t *cb, int depth)
5227 boolean_t scripted = cb->cb_scripted;
5228 uint64_t islog = B_FALSE;
5229 char *dashes = "%-*s - - - - - -\n";
5231 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
5232 (uint64_t **)&vs, &c) == 0);
5235 boolean_t toplevel = (vs->vs_space != 0);
5237 enum zfs_nicenum_format format;
5240 format = ZFS_NICENUM_RAW;
5242 format = ZFS_NICENUM_1024;
5244 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
5248 (void) printf("\t%s", name);
5249 else if (strlen(name) + depth > cb->cb_namewidth)
5250 (void) printf("%*s%s", depth, "", name);
5252 (void) printf("%*s%s%*s", depth, "", name,
5253 (int)(cb->cb_namewidth - strlen(name) - depth), "");
5256 * Print the properties for the individual vdevs. Some
5257 * properties are only applicable to toplevel vdevs. The
5258 * 'toplevel' boolean value is passed to the print_one_column()
5259 * to indicate that the value is valid.
5261 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
5263 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
5265 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
5266 scripted, toplevel, format);
5267 print_one_column(ZPOOL_PROP_CHECKPOINT,
5268 vs->vs_checkpoint_space, scripted, toplevel, format);
5269 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
5271 print_one_column(ZPOOL_PROP_FRAGMENTATION,
5272 vs->vs_fragmentation, scripted,
5273 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
5275 cap = (vs->vs_space == 0) ? 0 :
5276 (vs->vs_alloc * 10000 / vs->vs_space);
5277 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
5279 (void) printf("\n");
5282 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
5283 &child, &children) != 0)
5286 /* list the normal vdevs first */
5287 for (c = 0; c < children; c++) {
5288 uint64_t ishole = B_FALSE;
5290 if (nvlist_lookup_uint64(child[c],
5291 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
5294 if (nvlist_lookup_uint64(child[c],
5295 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
5298 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
5301 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5303 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5307 /* list the classes: 'logs', 'dedup', and 'special' */
5308 for (uint_t n = 0; n < 3; n++) {
5309 boolean_t printed = B_FALSE;
5311 for (c = 0; c < children; c++) {
5315 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5316 &islog) == 0 && islog) {
5317 bias = VDEV_ALLOC_CLASS_LOGS;
5319 (void) nvlist_lookup_string(child[c],
5320 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
5321 (void) nvlist_lookup_string(child[c],
5322 ZPOOL_CONFIG_TYPE, &type);
5324 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
5326 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
5330 /* LINTED E_SEC_PRINTF_VAR_FMT */
5331 (void) printf(dashes, cb->cb_namewidth,
5335 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5337 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5342 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5343 &child, &children) == 0 && children > 0) {
5344 /* LINTED E_SEC_PRINTF_VAR_FMT */
5345 (void) printf(dashes, cb->cb_namewidth, "cache");
5346 for (c = 0; c < children; c++) {
5347 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5349 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5354 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5355 &children) == 0 && children > 0) {
5356 /* LINTED E_SEC_PRINTF_VAR_FMT */
5357 (void) printf(dashes, cb->cb_namewidth, "spare");
5358 for (c = 0; c < children; c++) {
5359 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5361 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5368 * Generic callback function to list a pool.
5371 list_callback(zpool_handle_t *zhp, void *data)
5373 list_cbdata_t *cbp = data;
5377 config = zpool_get_config(zhp, NULL);
5379 if (cbp->cb_verbose) {
5380 config = zpool_get_config(zhp, NULL);
5382 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5386 if (cbp->cb_verbose)
5387 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
5388 cbp->cb_name_flags);
5390 print_pool(zhp, cbp);
5392 if (cbp->cb_verbose)
5393 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5399 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5401 * -g Display guid for individual vdev name.
5402 * -H Scripted mode. Don't display headers, and separate properties
5404 * -L Follow links when resolving vdev path name.
5405 * -o List of properties to display. Defaults to
5406 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5407 * "dedupratio,health,altroot"
5408 * -p Display values in parsable (exact) format.
5409 * -P Display full path for vdev name.
5410 * -T Display a timestamp in date(1) or Unix format
5412 * List all pools in the system, whether or not they're healthy. Output space
5413 * statistics for each one, as well as health status summary.
5416 zpool_do_list(int argc, char **argv)
5420 list_cbdata_t cb = { 0 };
5421 static char default_props[] =
5422 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
5423 "capacity,dedupratio,health,altroot";
5424 char *props = default_props;
5426 unsigned long count = 0;
5428 boolean_t first = B_TRUE;
5431 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5434 cb.cb_name_flags |= VDEV_NAME_GUID;
5437 cb.cb_scripted = B_TRUE;
5440 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5446 cb.cb_name_flags |= VDEV_NAME_PATH;
5449 cb.cb_literal = B_TRUE;
5452 get_timestamp_arg(*optarg);
5455 cb.cb_verbose = B_TRUE;
5456 cb.cb_namewidth = 8; /* 8 until precalc is avail */
5459 (void) fprintf(stderr, gettext("missing argument for "
5460 "'%c' option\n"), optopt);
5464 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5473 get_interval_count(&argc, argv, &interval, &count);
5475 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5479 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5483 if (pool_list_count(list) == 0)
5486 if (timestamp_fmt != NODATE)
5487 print_timestamp(timestamp_fmt);
5489 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5493 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5498 if (count != 0 && --count == 0)
5501 pool_list_free(list);
5502 (void) fsleep(interval);
5505 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5506 (void) printf(gettext("no pools available\n"));
5510 pool_list_free(list);
5511 zprop_free_list(cb.cb_proplist);
5516 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5518 boolean_t force = B_FALSE;
5521 char *poolname, *old_disk, *new_disk;
5522 zpool_handle_t *zhp;
5523 nvlist_t *props = NULL;
5528 while ((c = getopt(argc, argv, "fo:")) != -1) {
5534 if ((propval = strchr(optarg, '=')) == NULL) {
5535 (void) fprintf(stderr, gettext("missing "
5536 "'=' for -o option\n"));
5542 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5543 (add_prop_list(optarg, propval, &props, B_TRUE)))
5547 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5556 /* get pool name and check number of arguments */
5558 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5565 (void) fprintf(stderr,
5566 gettext("missing <device> specification\n"));
5574 (void) fprintf(stderr,
5575 gettext("missing <new_device> specification\n"));
5578 new_disk = old_disk;
5588 (void) fprintf(stderr, gettext("too many arguments\n"));
5592 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5597 if (zpool_get_config(zhp, NULL) == NULL) {
5598 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5605 /* unless manually specified use "ashift" pool property (if set) */
5606 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5609 char strval[ZPOOL_MAXPROPLEN];
5611 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5612 if (src != ZPROP_SRC_DEFAULT) {
5613 (void) sprintf(strval, "%" PRId32, intval);
5614 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5615 &props, B_TRUE) == 0);
5619 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5621 if (nvroot == NULL) {
5627 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5630 nvlist_free(nvroot);
5637 * zpool replace [-f] <pool> <device> <new_device>
5639 * -f Force attach, even if <new_device> appears to be in use.
5641 * Replace <device> with <new_device>.
5645 zpool_do_replace(int argc, char **argv)
5647 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5651 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5653 * -f Force attach, even if <new_device> appears to be in use.
5654 * -o Set property=value.
5656 * Attach <new_device> to the mirror containing <device>. If <device> is not
5657 * part of a mirror, then <device> will be transformed into a mirror of
5658 * <device> and <new_device>. In either case, <new_device> will begin life
5659 * with a DTL of [0, now], and will immediately begin to resilver itself.
5662 zpool_do_attach(int argc, char **argv)
5664 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5668 * zpool detach [-f] <pool> <device>
5670 * -f Force detach of <device>, even if DTLs argue against it
5671 * (not supported yet)
5673 * Detach a device from a mirror. The operation will be refused if <device>
5674 * is the last device in the mirror, or if the DTLs indicate that this device
5675 * has the only valid copy of some data.
5679 zpool_do_detach(int argc, char **argv)
5682 char *poolname, *path;
5683 zpool_handle_t *zhp;
5687 while ((c = getopt(argc, argv, "f")) != -1) {
5691 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5700 /* get pool name and check number of arguments */
5702 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5707 (void) fprintf(stderr,
5708 gettext("missing <device> specification\n"));
5715 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5718 ret = zpool_vdev_detach(zhp, path);
5726 * zpool split [-gLnP] [-o prop=val] ...
5728 * [-R altroot] <pool> <newpool> [<device> ...]
5730 * -g Display guid for individual vdev name.
5731 * -L Follow links when resolving vdev path name.
5732 * -n Do not split the pool, but display the resulting layout if
5733 * it were to be split.
5734 * -o Set property=value, or set mount options.
5735 * -P Display full path for vdev name.
5736 * -R Mount the split-off pool under an alternate root.
5737 * -l Load encryption keys while importing.
5739 * Splits the named pool and gives it the new pool name. Devices to be split
5740 * off may be listed, provided that no more than one device is specified
5741 * per top-level vdev mirror. The newly split pool is left in an exported
5742 * state unless -R is specified.
5744 * Restrictions: the top-level of the pool pool must only be made up of
5745 * mirrors; all devices in the pool must be healthy; no device may be
5746 * undergoing a resilvering operation.
5749 zpool_do_split(int argc, char **argv)
5751 char *srcpool, *newpool, *propval;
5752 char *mntopts = NULL;
5755 boolean_t loadkeys = B_FALSE;
5756 zpool_handle_t *zhp;
5757 nvlist_t *config, *props = NULL;
5759 flags.dryrun = B_FALSE;
5760 flags.import = B_FALSE;
5761 flags.name_flags = 0;
5764 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5767 flags.name_flags |= VDEV_NAME_GUID;
5770 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5773 flags.import = B_TRUE;
5775 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5776 &props, B_TRUE) != 0) {
5785 flags.dryrun = B_TRUE;
5788 if ((propval = strchr(optarg, '=')) != NULL) {
5791 if (add_prop_list(optarg, propval,
5792 &props, B_TRUE) != 0) {
5801 flags.name_flags |= VDEV_NAME_PATH;
5804 (void) fprintf(stderr, gettext("missing argument for "
5805 "'%c' option\n"), optopt);
5809 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5816 if (!flags.import && mntopts != NULL) {
5817 (void) fprintf(stderr, gettext("setting mntopts is only "
5818 "valid when importing the pool\n"));
5822 if (!flags.import && loadkeys) {
5823 (void) fprintf(stderr, gettext("loading keys is only "
5824 "valid when importing the pool\n"));
5832 (void) fprintf(stderr, gettext("Missing pool name\n"));
5836 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5846 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5851 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5852 if (config == NULL) {
5856 (void) printf(gettext("would create '%s' with the "
5857 "following layout:\n\n"), newpool);
5858 print_vdev_tree(NULL, newpool, config, 0, "",
5865 if (ret != 0 || flags.dryrun || !flags.import) {
5866 nvlist_free(config);
5872 * The split was successful. Now we need to open the new
5873 * pool and import it.
5875 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5876 nvlist_free(config);
5882 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5887 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5888 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5890 (void) fprintf(stderr, gettext("Split was successful, but "
5891 "the datasets could not all be mounted\n"));
5892 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5893 "different altroot\n"), "zpool import");
5896 nvlist_free(config);
5905 * zpool online <pool> <device> ...
5908 zpool_do_online(int argc, char **argv)
5912 zpool_handle_t *zhp;
5914 vdev_state_t newstate;
5918 while ((c = getopt(argc, argv, "et")) != -1) {
5921 flags |= ZFS_ONLINE_EXPAND;
5925 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5934 /* get pool name and check number of arguments */
5936 (void) fprintf(stderr, gettext("missing pool name\n"));
5940 (void) fprintf(stderr, gettext("missing device name\n"));
5946 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5949 for (i = 1; i < argc; i++) {
5950 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5951 if (newstate != VDEV_STATE_HEALTHY) {
5952 (void) printf(gettext("warning: device '%s' "
5953 "onlined, but remains in faulted state\n"),
5955 if (newstate == VDEV_STATE_FAULTED)
5956 (void) printf(gettext("use 'zpool "
5957 "clear' to restore a faulted "
5960 (void) printf(gettext("use 'zpool "
5961 "replace' to replace devices "
5962 "that are no longer present\n"));
5975 * zpool offline [-ft] <pool> <device> ...
5977 * -f Force the device into a faulted state.
5979 * -t Only take the device off-line temporarily. The offline/faulted
5980 * state will not be persistent across reboots.
5984 zpool_do_offline(int argc, char **argv)
5988 zpool_handle_t *zhp;
5990 boolean_t istmp = B_FALSE;
5991 boolean_t fault = B_FALSE;
5994 while ((c = getopt(argc, argv, "ft")) != -1) {
6003 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6012 /* get pool name and check number of arguments */
6014 (void) fprintf(stderr, gettext("missing pool name\n"));
6018 (void) fprintf(stderr, gettext("missing device name\n"));
6024 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6027 for (i = 1; i < argc; i++) {
6029 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
6031 if (istmp == B_FALSE) {
6032 /* Force the fault to persist across imports */
6033 aux = VDEV_AUX_EXTERNAL_PERSIST;
6035 aux = VDEV_AUX_EXTERNAL;
6038 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
6041 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
6052 * zpool clear <pool> [device]
6054 * Clear all errors associated with a pool or a particular device.
6057 zpool_do_clear(int argc, char **argv)
6061 boolean_t dryrun = B_FALSE;
6062 boolean_t do_rewind = B_FALSE;
6063 boolean_t xtreme_rewind = B_FALSE;
6064 uint32_t rewind_policy = ZPOOL_NO_REWIND;
6065 nvlist_t *policy = NULL;
6066 zpool_handle_t *zhp;
6067 char *pool, *device;
6070 while ((c = getopt(argc, argv, "FnX")) != -1) {
6079 xtreme_rewind = B_TRUE;
6082 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6092 (void) fprintf(stderr, gettext("missing pool name\n"));
6097 (void) fprintf(stderr, gettext("too many arguments\n"));
6101 if ((dryrun || xtreme_rewind) && !do_rewind) {
6102 (void) fprintf(stderr,
6103 gettext("-n or -X only meaningful with -F\n"));
6107 rewind_policy = ZPOOL_TRY_REWIND;
6109 rewind_policy = ZPOOL_DO_REWIND;
6111 rewind_policy |= ZPOOL_EXTREME_REWIND;
6113 /* In future, further rewind policy choices can be passed along here */
6114 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
6115 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
6116 rewind_policy) != 0) {
6121 device = argc == 2 ? argv[1] : NULL;
6123 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
6124 nvlist_free(policy);
6128 if (zpool_clear(zhp, device, policy) != 0)
6133 nvlist_free(policy);
6139 * zpool reguid <pool>
6142 zpool_do_reguid(int argc, char **argv)
6146 zpool_handle_t *zhp;
6150 while ((c = getopt(argc, argv, "")) != -1) {
6153 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6162 /* get pool name and check number of arguments */
6164 (void) fprintf(stderr, gettext("missing pool name\n"));
6169 (void) fprintf(stderr, gettext("too many arguments\n"));
6174 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6177 ret = zpool_reguid(zhp);
6185 * zpool reopen <pool>
6187 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6190 zpool_do_reopen(int argc, char **argv)
6194 boolean_t scrub_restart = B_TRUE;
6197 while ((c = getopt(argc, argv, "n")) != -1) {
6200 scrub_restart = B_FALSE;
6203 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6212 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6213 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
6219 typedef struct scrub_cbdata {
6223 pool_scrub_cmd_t cb_scrub_cmd;
6227 zpool_has_checkpoint(zpool_handle_t *zhp)
6229 nvlist_t *config, *nvroot;
6231 config = zpool_get_config(zhp, NULL);
6233 if (config != NULL) {
6234 pool_checkpoint_stat_t *pcs = NULL;
6237 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6238 (void) nvlist_lookup_uint64_array(nvroot,
6239 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
6241 if (pcs == NULL || pcs->pcs_state == CS_NONE)
6244 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
6245 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
6253 scrub_callback(zpool_handle_t *zhp, void *data)
6255 scrub_cbdata_t *cb = data;
6259 * Ignore faulted pools.
6261 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
6262 (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
6263 "currently unavailable\n"), zpool_get_name(zhp));
6267 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
6269 if (err == 0 && zpool_has_checkpoint(zhp) &&
6270 cb->cb_type == POOL_SCAN_SCRUB) {
6271 (void) printf(gettext("warning: will not scrub state that "
6272 "belongs to the checkpoint of pool '%s'\n"),
6273 zpool_get_name(zhp));
6280 * zpool scrub [-s | -p] <pool> ...
6282 * -s Stop. Stops any in-progress scrub.
6283 * -p Pause. Pause in-progress scrub.
6286 zpool_do_scrub(int argc, char **argv)
6291 cb.cb_type = POOL_SCAN_SCRUB;
6292 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6295 while ((c = getopt(argc, argv, "sp")) != -1) {
6298 cb.cb_type = POOL_SCAN_NONE;
6301 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
6304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6310 if (cb.cb_type == POOL_SCAN_NONE &&
6311 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
6312 (void) fprintf(stderr, gettext("invalid option combination: "
6313 "-s and -p are mutually exclusive\n"));
6323 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6327 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6331 * zpool resilver <pool> ...
6333 * Restarts any in-progress resilver
6336 zpool_do_resilver(int argc, char **argv)
6341 cb.cb_type = POOL_SCAN_RESILVER;
6342 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6347 while ((c = getopt(argc, argv, "")) != -1) {
6350 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6360 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6364 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6369 * Print out detailed scrub status.
6372 print_scan_status(pool_scan_stat_t *ps)
6374 time_t start, end, pause;
6375 uint64_t total_secs_left;
6376 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
6377 uint64_t pass_scanned, scanned, pass_issued, issued, total;
6378 uint64_t scan_rate, issue_rate;
6379 double fraction_done;
6380 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
6381 char srate_buf[7], irate_buf[7];
6383 (void) printf(gettext(" scan: "));
6385 /* If there's never been a scan, there's not much to say. */
6386 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
6387 ps->pss_func >= POOL_SCAN_FUNCS) {
6388 (void) printf(gettext("none requested\n"));
6392 start = ps->pss_start_time;
6393 end = ps->pss_end_time;
6394 pause = ps->pss_pass_scrub_pause;
6396 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6398 assert(ps->pss_func == POOL_SCAN_SCRUB ||
6399 ps->pss_func == POOL_SCAN_RESILVER);
6401 /* Scan is finished or canceled. */
6402 if (ps->pss_state == DSS_FINISHED) {
6403 total_secs_left = end - start;
6404 days_left = total_secs_left / 60 / 60 / 24;
6405 hours_left = (total_secs_left / 60 / 60) % 24;
6406 mins_left = (total_secs_left / 60) % 60;
6407 secs_left = (total_secs_left % 60);
6409 if (ps->pss_func == POOL_SCAN_SCRUB) {
6410 (void) printf(gettext("scrub repaired %s "
6411 "in %llu days %02llu:%02llu:%02llu "
6412 "with %llu errors on %s"), processed_buf,
6413 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6414 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6415 (u_longlong_t)ps->pss_errors, ctime(&end));
6416 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6417 (void) printf(gettext("resilvered %s "
6418 "in %llu days %02llu:%02llu:%02llu "
6419 "with %llu errors on %s"), processed_buf,
6420 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6421 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6422 (u_longlong_t)ps->pss_errors, ctime(&end));
6425 } else if (ps->pss_state == DSS_CANCELED) {
6426 if (ps->pss_func == POOL_SCAN_SCRUB) {
6427 (void) printf(gettext("scrub canceled on %s"),
6429 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6430 (void) printf(gettext("resilver canceled on %s"),
6436 assert(ps->pss_state == DSS_SCANNING);
6438 /* Scan is in progress. Resilvers can't be paused. */
6439 if (ps->pss_func == POOL_SCAN_SCRUB) {
6441 (void) printf(gettext("scrub in progress since %s"),
6444 (void) printf(gettext("scrub paused since %s"),
6446 (void) printf(gettext("\tscrub started on %s"),
6449 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6450 (void) printf(gettext("resilver in progress since %s"),
6454 scanned = ps->pss_examined;
6455 pass_scanned = ps->pss_pass_exam;
6456 issued = ps->pss_issued;
6457 pass_issued = ps->pss_pass_issued;
6458 total = ps->pss_to_examine;
6460 /* we are only done with a block once we have issued the IO for it */
6461 fraction_done = (double)issued / total;
6463 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6464 elapsed = time(NULL) - ps->pss_pass_start;
6465 elapsed -= ps->pss_pass_scrub_spent_paused;
6466 elapsed = (elapsed != 0) ? elapsed : 1;
6468 scan_rate = pass_scanned / elapsed;
6469 issue_rate = pass_issued / elapsed;
6470 total_secs_left = (issue_rate != 0) ?
6471 ((total - issued) / issue_rate) : UINT64_MAX;
6473 days_left = total_secs_left / 60 / 60 / 24;
6474 hours_left = (total_secs_left / 60 / 60) % 24;
6475 mins_left = (total_secs_left / 60) % 60;
6476 secs_left = (total_secs_left % 60);
6478 /* format all of the numbers we will be reporting */
6479 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
6480 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
6481 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6482 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
6483 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
6485 /* do not print estimated time if we have a paused scrub */
6487 (void) printf(gettext("\t%s scanned at %s/s, "
6488 "%s issued at %s/s, %s total\n"),
6489 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
6491 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6492 scanned_buf, issued_buf, total_buf);
6495 if (ps->pss_func == POOL_SCAN_RESILVER) {
6496 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6497 processed_buf, 100 * fraction_done);
6498 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6499 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6500 processed_buf, 100 * fraction_done);
6504 if (issue_rate >= 10 * 1024 * 1024) {
6505 (void) printf(gettext(", %llu days "
6506 "%02llu:%02llu:%02llu to go\n"),
6507 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6508 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
6510 (void) printf(gettext(", no estimated "
6511 "completion time\n"));
6514 (void) printf(gettext("\n"));
6519 * As we don't scrub checkpointed blocks, we want to warn the
6520 * user that we skipped scanning some blocks if a checkpoint exists
6521 * or existed at any time during the scan.
6524 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
6526 if (ps == NULL || pcs == NULL)
6529 if (pcs->pcs_state == CS_NONE ||
6530 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
6533 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
6535 if (ps->pss_state == DSS_NONE)
6538 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
6539 ps->pss_end_time < pcs->pcs_start_time)
6542 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
6543 (void) printf(gettext(" scan warning: skipped blocks "
6544 "that are only referenced by the checkpoint.\n"));
6546 assert(ps->pss_state == DSS_SCANNING);
6547 (void) printf(gettext(" scan warning: skipping blocks "
6548 "that are only referenced by the checkpoint.\n"));
6553 * Print out detailed removal status.
6556 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
6558 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
6560 nvlist_t *config, *nvroot;
6565 if (prs == NULL || prs->prs_state == DSS_NONE)
6569 * Determine name of vdev.
6571 config = zpool_get_config(zhp, NULL);
6572 nvroot = fnvlist_lookup_nvlist(config,
6573 ZPOOL_CONFIG_VDEV_TREE);
6574 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
6575 &child, &children) == 0);
6576 assert(prs->prs_removing_vdev < children);
6577 vdev_name = zpool_vdev_name(g_zfs, zhp,
6578 child[prs->prs_removing_vdev], B_TRUE);
6580 (void) printf(gettext("remove: "));
6582 start = prs->prs_start_time;
6583 end = prs->prs_end_time;
6584 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
6587 * Removal is finished or canceled.
6589 if (prs->prs_state == DSS_FINISHED) {
6590 uint64_t minutes_taken = (end - start) / 60;
6592 (void) printf(gettext("Removal of vdev %llu copied %s "
6593 "in %lluh%um, completed on %s"),
6594 (longlong_t)prs->prs_removing_vdev,
6596 (u_longlong_t)(minutes_taken / 60),
6597 (uint_t)(minutes_taken % 60),
6598 ctime((time_t *)&end));
6599 } else if (prs->prs_state == DSS_CANCELED) {
6600 (void) printf(gettext("Removal of %s canceled on %s"),
6601 vdev_name, ctime(&end));
6603 uint64_t copied, total, elapsed, mins_left, hours_left;
6604 double fraction_done;
6607 assert(prs->prs_state == DSS_SCANNING);
6610 * Removal is in progress.
6612 (void) printf(gettext(
6613 "Evacuation of %s in progress since %s"),
6614 vdev_name, ctime(&start));
6616 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
6617 total = prs->prs_to_copy;
6618 fraction_done = (double)copied / total;
6620 /* elapsed time for this pass */
6621 elapsed = time(NULL) - prs->prs_start_time;
6622 elapsed = elapsed > 0 ? elapsed : 1;
6623 rate = copied / elapsed;
6624 rate = rate > 0 ? rate : 1;
6625 mins_left = ((total - copied) / rate) / 60;
6626 hours_left = mins_left / 60;
6628 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
6629 zfs_nicenum(total, total_buf, sizeof (total_buf));
6630 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
6633 * do not print estimated time if hours_left is more than
6636 (void) printf(gettext(" %s copied out of %s at %s/s, "
6638 examined_buf, total_buf, rate_buf, 100 * fraction_done);
6639 if (hours_left < (30 * 24)) {
6640 (void) printf(gettext(", %lluh%um to go\n"),
6641 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
6643 (void) printf(gettext(
6644 ", (copy is slow, no estimated time)\n"));
6648 if (prs->prs_mapping_memory > 0) {
6650 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
6651 (void) printf(gettext(" %s memory used for "
6652 "removed device mappings\n"),
6658 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
6663 if (pcs == NULL || pcs->pcs_state == CS_NONE)
6666 (void) printf(gettext("checkpoint: "));
6668 start = pcs->pcs_start_time;
6669 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
6671 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
6672 char *date = ctime(&start);
6675 * ctime() adds a newline at the end of the generated
6676 * string, thus the weird format specifier and the
6677 * strlen() call used to chop it off from the output.
6679 (void) printf(gettext("created %.*s, consumes %s\n"),
6680 (int)(strlen(date) - 1), date, space_buf);
6684 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
6686 (void) printf(gettext("discarding, %s remaining.\n"),
6691 print_error_log(zpool_handle_t *zhp)
6693 nvlist_t *nverrlist = NULL;
6696 size_t len = MAXPATHLEN * 2;
6698 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6701 (void) printf("errors: Permanent errors have been "
6702 "detected in the following files:\n\n");
6704 pathname = safe_malloc(len);
6706 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6708 uint64_t dsobj, obj;
6710 verify(nvpair_value_nvlist(elem, &nv) == 0);
6711 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6713 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6715 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6716 (void) printf("%7s %s\n", "", pathname);
6719 nvlist_free(nverrlist);
6723 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6732 (void) printf(gettext("\tspares\n"));
6734 for (i = 0; i < nspares; i++) {
6735 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6737 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6743 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6752 (void) printf(gettext("\tcache\n"));
6754 for (i = 0; i < nl2cache; i++) {
6755 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6757 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6763 print_dedup_stats(nvlist_t *config)
6765 ddt_histogram_t *ddh;
6769 char dspace[6], mspace[6];
6772 * If the pool was faulted then we may not have been able to
6773 * obtain the config. Otherwise, if we have anything in the dedup
6774 * table continue processing the stats.
6776 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6777 (uint64_t **)&ddo, &c) != 0)
6780 (void) printf("\n");
6781 (void) printf(gettext(" dedup: "));
6782 if (ddo->ddo_count == 0) {
6783 (void) printf(gettext("no DDT entries\n"));
6787 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6788 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6789 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6790 (u_longlong_t)ddo->ddo_count,
6794 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6795 (uint64_t **)&dds, &c) == 0);
6796 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6797 (uint64_t **)&ddh, &c) == 0);
6798 zpool_dump_ddt(dds, ddh);
6802 * Display a summary of pool status. Displays a summary such as:
6806 * reason: One or more devices ...
6807 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6813 * When given the '-v' option, we print out the complete config. If the '-e'
6814 * option is specified, then we print out error rate information as well.
6817 status_callback(zpool_handle_t *zhp, void *data)
6819 status_cbdata_t *cbp = data;
6820 nvlist_t *config, *nvroot;
6822 zpool_status_t reason;
6823 zpool_errata_t errata;
6828 config = zpool_get_config(zhp, NULL);
6829 reason = zpool_get_status(zhp, &msgid, &errata);
6834 * If we were given 'zpool status -x', only report those pools with
6837 if (cbp->cb_explain &&
6838 (reason == ZPOOL_STATUS_OK ||
6839 reason == ZPOOL_STATUS_VERSION_OLDER ||
6840 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6841 if (!cbp->cb_allpools) {
6842 (void) printf(gettext("pool '%s' is healthy\n"),
6843 zpool_get_name(zhp));
6845 cbp->cb_first = B_FALSE;
6851 cbp->cb_first = B_FALSE;
6853 (void) printf("\n");
6855 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6856 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6857 (uint64_t **)&vs, &c) == 0);
6859 health = zpool_get_state_str(zhp);
6861 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6862 (void) printf(gettext(" state: %s\n"), health);
6865 case ZPOOL_STATUS_MISSING_DEV_R:
6866 (void) printf(gettext("status: One or more devices could not "
6867 "be opened. Sufficient replicas exist for\n\tthe pool to "
6868 "continue functioning in a degraded state.\n"));
6869 (void) printf(gettext("action: Attach the missing device and "
6870 "online it using 'zpool online'.\n"));
6873 case ZPOOL_STATUS_MISSING_DEV_NR:
6874 (void) printf(gettext("status: One or more devices could not "
6875 "be opened. There are insufficient\n\treplicas for the "
6876 "pool to continue functioning.\n"));
6877 (void) printf(gettext("action: Attach the missing device and "
6878 "online it using 'zpool online'.\n"));
6881 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6882 (void) printf(gettext("status: One or more devices could not "
6883 "be used because the label is missing or\n\tinvalid. "
6884 "Sufficient replicas exist for the pool to continue\n\t"
6885 "functioning in a degraded state.\n"));
6886 (void) printf(gettext("action: Replace the device using "
6887 "'zpool replace'.\n"));
6890 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6891 (void) printf(gettext("status: One or more devices could not "
6892 "be used because the label is missing \n\tor invalid. "
6893 "There are insufficient replicas for the pool to "
6894 "continue\n\tfunctioning.\n"));
6895 zpool_explain_recover(zpool_get_handle(zhp),
6896 zpool_get_name(zhp), reason, config);
6899 case ZPOOL_STATUS_FAILING_DEV:
6900 (void) printf(gettext("status: One or more devices has "
6901 "experienced an unrecoverable error. An\n\tattempt was "
6902 "made to correct the error. Applications are "
6904 (void) printf(gettext("action: Determine if the device needs "
6905 "to be replaced, and clear the errors\n\tusing "
6906 "'zpool clear' or replace the device with 'zpool "
6910 case ZPOOL_STATUS_OFFLINE_DEV:
6911 (void) printf(gettext("status: One or more devices has "
6912 "been taken offline by the administrator.\n\tSufficient "
6913 "replicas exist for the pool to continue functioning in "
6914 "a\n\tdegraded state.\n"));
6915 (void) printf(gettext("action: Online the device using "
6916 "'zpool online' or replace the device with\n\t'zpool "
6920 case ZPOOL_STATUS_REMOVED_DEV:
6921 (void) printf(gettext("status: One or more devices has "
6922 "been removed by the administrator.\n\tSufficient "
6923 "replicas exist for the pool to continue functioning in "
6924 "a\n\tdegraded state.\n"));
6925 (void) printf(gettext("action: Online the device using "
6926 "'zpool online' or replace the device with\n\t'zpool "
6930 case ZPOOL_STATUS_RESILVERING:
6931 (void) printf(gettext("status: One or more devices is "
6932 "currently being resilvered. The pool will\n\tcontinue "
6933 "to function, possibly in a degraded state.\n"));
6934 (void) printf(gettext("action: Wait for the resilver to "
6938 case ZPOOL_STATUS_CORRUPT_DATA:
6939 (void) printf(gettext("status: One or more devices has "
6940 "experienced an error resulting in data\n\tcorruption. "
6941 "Applications may be affected.\n"));
6942 (void) printf(gettext("action: Restore the file in question "
6943 "if possible. Otherwise restore the\n\tentire pool from "
6947 case ZPOOL_STATUS_CORRUPT_POOL:
6948 (void) printf(gettext("status: The pool metadata is corrupted "
6949 "and the pool cannot be opened.\n"));
6950 zpool_explain_recover(zpool_get_handle(zhp),
6951 zpool_get_name(zhp), reason, config);
6954 case ZPOOL_STATUS_VERSION_OLDER:
6955 (void) printf(gettext("status: The pool is formatted using a "
6956 "legacy on-disk format. The pool can\n\tstill be used, "
6957 "but some features are unavailable.\n"));
6958 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6959 "upgrade'. Once this is done, the\n\tpool will no longer "
6960 "be accessible on software that does not support\n\t"
6961 "feature flags.\n"));
6964 case ZPOOL_STATUS_VERSION_NEWER:
6965 (void) printf(gettext("status: The pool has been upgraded to a "
6966 "newer, incompatible on-disk version.\n\tThe pool cannot "
6967 "be accessed on this system.\n"));
6968 (void) printf(gettext("action: Access the pool from a system "
6969 "running more recent software, or\n\trestore the pool from "
6973 case ZPOOL_STATUS_FEAT_DISABLED:
6974 (void) printf(gettext("status: Some supported features are not "
6975 "enabled on the pool. The pool can\n\tstill be used, but "
6976 "some features are unavailable.\n"));
6977 (void) printf(gettext("action: Enable all features using "
6978 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6979 "longer be accessible by software that does not support\n\t"
6980 "the features. See zpool-features(5) for details.\n"));
6983 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6984 (void) printf(gettext("status: The pool cannot be accessed on "
6985 "this system because it uses the\n\tfollowing feature(s) "
6986 "not supported on this system:\n"));
6987 zpool_print_unsup_feat(config);
6988 (void) printf("\n");
6989 (void) printf(gettext("action: Access the pool from a system "
6990 "that supports the required feature(s),\n\tor restore the "
6991 "pool from backup.\n"));
6994 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6995 (void) printf(gettext("status: The pool can only be accessed "
6996 "in read-only mode on this system. It\n\tcannot be "
6997 "accessed in read-write mode because it uses the "
6998 "following\n\tfeature(s) not supported on this system:\n"));
6999 zpool_print_unsup_feat(config);
7000 (void) printf("\n");
7001 (void) printf(gettext("action: The pool cannot be accessed in "
7002 "read-write mode. Import the pool with\n"
7003 "\t\"-o readonly=on\", access the pool from a system that "
7004 "supports the\n\trequired feature(s), or restore the "
7005 "pool from backup.\n"));
7008 case ZPOOL_STATUS_FAULTED_DEV_R:
7009 (void) printf(gettext("status: One or more devices are "
7010 "faulted in response to persistent errors.\n\tSufficient "
7011 "replicas exist for the pool to continue functioning "
7012 "in a\n\tdegraded state.\n"));
7013 (void) printf(gettext("action: Replace the faulted device, "
7014 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
7017 case ZPOOL_STATUS_FAULTED_DEV_NR:
7018 (void) printf(gettext("status: One or more devices are "
7019 "faulted in response to persistent errors. There are "
7020 "insufficient replicas for the pool to\n\tcontinue "
7022 (void) printf(gettext("action: Destroy and re-create the pool "
7023 "from a backup source. Manually marking the device\n"
7024 "\trepaired using 'zpool clear' may allow some data "
7025 "to be recovered.\n"));
7028 case ZPOOL_STATUS_IO_FAILURE_MMP:
7029 (void) printf(gettext("status: The pool is suspended because "
7030 "multihost writes failed or were delayed;\n\tanother "
7031 "system could import the pool undetected.\n"));
7032 (void) printf(gettext("action: Make sure the pool's devices "
7033 "are connected, then reboot your system and\n\timport the "
7037 case ZPOOL_STATUS_IO_FAILURE_WAIT:
7038 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
7039 (void) printf(gettext("status: One or more devices are "
7040 "faulted in response to IO failures.\n"));
7041 (void) printf(gettext("action: Make sure the affected devices "
7042 "are connected, then run 'zpool clear'.\n"));
7045 case ZPOOL_STATUS_BAD_LOG:
7046 (void) printf(gettext("status: An intent log record "
7047 "could not be read.\n"
7048 "\tWaiting for administrator intervention to fix the "
7049 "faulted pool.\n"));
7050 (void) printf(gettext("action: Either restore the affected "
7051 "device(s) and run 'zpool online',\n"
7052 "\tor ignore the intent log records by running "
7053 "'zpool clear'.\n"));
7056 case ZPOOL_STATUS_HOSTID_MISMATCH:
7057 (void) printf(gettext("status: Mismatch between pool hostid "
7058 "and system hostid on imported pool.\n\tThis pool was "
7059 "previously imported into a system with a different "
7060 "hostid,\n\tand then was verbatim imported into this "
7062 (void) printf(gettext("action: Export this pool on all systems "
7063 "on which it is imported.\n"
7064 "\tThen import it to correct the mismatch.\n"));
7067 case ZPOOL_STATUS_ERRATA:
7068 (void) printf(gettext("status: Errata #%d detected.\n"),
7072 case ZPOOL_ERRATA_NONE:
7075 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
7076 (void) printf(gettext("action: To correct the issue "
7077 "run 'zpool scrub'.\n"));
7080 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
7081 (void) printf(gettext("\tExisting encrypted datasets "
7082 "contain an on-disk incompatibility\n\twhich "
7083 "needs to be corrected.\n"));
7084 (void) printf(gettext("action: To correct the issue "
7085 "backup existing encrypted datasets to new\n\t"
7086 "encrypted datasets and destroy the old ones. "
7087 "'zfs mount -o ro' can\n\tbe used to temporarily "
7088 "mount existing encrypted datasets readonly.\n"));
7093 * All errata which allow the pool to be imported
7094 * must contain an action message.
7102 * The remaining errors can't actually be generated, yet.
7104 assert(reason == ZPOOL_STATUS_OK);
7108 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
7111 if (config != NULL) {
7113 nvlist_t **spares, **l2cache;
7114 uint_t nspares, nl2cache;
7115 pool_checkpoint_stat_t *pcs = NULL;
7116 pool_scan_stat_t *ps = NULL;
7117 pool_removal_stat_t *prs = NULL;
7119 (void) nvlist_lookup_uint64_array(nvroot,
7120 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7121 (void) nvlist_lookup_uint64_array(nvroot,
7122 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
7123 (void) nvlist_lookup_uint64_array(nvroot,
7124 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
7126 print_scan_status(ps);
7127 print_checkpoint_scan_warning(ps, pcs);
7128 print_removal_status(zhp, prs);
7129 print_checkpoint_status(pcs);
7131 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
7132 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
7133 if (cbp->cb_namewidth < 10)
7134 cbp->cb_namewidth = 10;
7136 (void) printf(gettext("config:\n\n"));
7137 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
7138 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
7141 if (cbp->vcdl != NULL)
7142 print_cmd_columns(cbp->vcdl, 0);
7146 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
7149 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
7150 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
7151 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
7153 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
7154 &l2cache, &nl2cache) == 0)
7155 print_l2cache(zhp, cbp, l2cache, nl2cache);
7157 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
7158 &spares, &nspares) == 0)
7159 print_spares(zhp, cbp, spares, nspares);
7161 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
7163 nvlist_t *nverrlist = NULL;
7166 * If the approximate error count is small, get a
7167 * precise count by fetching the entire log and
7168 * uniquifying the results.
7170 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
7171 zpool_get_errlog(zhp, &nverrlist) == 0) {
7176 while ((elem = nvlist_next_nvpair(nverrlist,
7181 nvlist_free(nverrlist);
7183 (void) printf("\n");
7186 (void) printf(gettext("errors: No known data "
7188 else if (!cbp->cb_verbose)
7189 (void) printf(gettext("errors: %llu data "
7190 "errors, use '-v' for a list\n"),
7191 (u_longlong_t)nerr);
7193 print_error_log(zhp);
7196 if (cbp->cb_dedup_stats)
7197 print_dedup_stats(config);
7199 (void) printf(gettext("config: The configuration cannot be "
7207 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
7208 * [interval [count]]
7210 * -c CMD For each vdev, run command CMD
7211 * -g Display guid for individual vdev name.
7212 * -L Follow links when resolving vdev path name.
7213 * -P Display full path for vdev name.
7214 * -v Display complete error logs
7215 * -x Display only pools with potential problems
7216 * -D Display dedup status (undocumented)
7217 * -T Display a timestamp in date(1) or Unix format
7219 * Describes the health status of all pools or some subset.
7222 zpool_do_status(int argc, char **argv)
7227 unsigned long count = 0;
7228 status_cbdata_t cb = { 0 };
7232 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
7237 gettext("Can't set -c flag twice\n"));
7241 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
7242 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
7243 fprintf(stderr, gettext(
7244 "Can't run -c, disabled by "
7245 "ZPOOL_SCRIPTS_ENABLED.\n"));
7249 if ((getuid() <= 0 || geteuid() <= 0) &&
7250 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
7251 fprintf(stderr, gettext(
7252 "Can't run -c with root privileges "
7253 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
7259 cb.cb_name_flags |= VDEV_NAME_GUID;
7262 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
7265 cb.cb_name_flags |= VDEV_NAME_PATH;
7268 cb.cb_verbose = B_TRUE;
7271 cb.cb_explain = B_TRUE;
7274 cb.cb_dedup_stats = B_TRUE;
7277 get_timestamp_arg(*optarg);
7280 if (optopt == 'c') {
7281 print_zpool_script_list("status");
7285 gettext("invalid option '%c'\n"), optopt);
7294 get_interval_count(&argc, argv, &interval, &count);
7297 cb.cb_allpools = B_TRUE;
7299 cb.cb_first = B_TRUE;
7300 cb.cb_print_status = B_TRUE;
7303 if (timestamp_fmt != NODATE)
7304 print_timestamp(timestamp_fmt);
7307 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
7310 ret = for_each_pool(argc, argv, B_TRUE, NULL,
7311 status_callback, &cb);
7313 if (cb.vcdl != NULL)
7314 free_vdev_cmd_data_list(cb.vcdl);
7316 if (argc == 0 && cb.cb_count == 0)
7317 (void) fprintf(stderr, gettext("no pools available\n"));
7318 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
7319 (void) printf(gettext("all pools are healthy\n"));
7327 if (count != 0 && --count == 0)
7330 (void) fsleep(interval);
7336 typedef struct upgrade_cbdata {
7339 uint64_t cb_version;
7344 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
7346 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
7347 int *count = (int *)unsupp_fs;
7349 if (zfs_version > ZPL_VERSION) {
7350 (void) printf(gettext("%s (v%d) is not supported by this "
7351 "implementation of ZFS.\n"),
7352 zfs_get_name(zhp), zfs_version);
7356 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
7364 upgrade_version(zpool_handle_t *zhp, uint64_t version)
7368 uint64_t oldversion;
7371 config = zpool_get_config(zhp, NULL);
7372 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7375 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
7376 assert(oldversion < version);
7378 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
7383 (void) fprintf(stderr, gettext("Upgrade not performed due "
7384 "to %d unsupported filesystems (max v%d).\n"),
7385 unsupp_fs, (int)ZPL_VERSION);
7389 ret = zpool_upgrade(zhp, version);
7393 if (version >= SPA_VERSION_FEATURES) {
7394 (void) printf(gettext("Successfully upgraded "
7395 "'%s' from version %llu to feature flags.\n"),
7396 zpool_get_name(zhp), (u_longlong_t)oldversion);
7398 (void) printf(gettext("Successfully upgraded "
7399 "'%s' from version %llu to version %llu.\n"),
7400 zpool_get_name(zhp), (u_longlong_t)oldversion,
7401 (u_longlong_t)version);
7408 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
7411 boolean_t firstff = B_TRUE;
7412 nvlist_t *enabled = zpool_get_features(zhp);
7415 for (i = 0; i < SPA_FEATURES; i++) {
7416 const char *fname = spa_feature_table[i].fi_uname;
7417 const char *fguid = spa_feature_table[i].fi_guid;
7418 if (!nvlist_exists(enabled, fguid)) {
7420 verify(-1 != asprintf(&propname, "feature@%s", fname));
7421 ret = zpool_set_prop(zhp, propname,
7422 ZFS_FEATURE_ENABLED);
7430 (void) printf(gettext("Enabled the "
7431 "following features on '%s':\n"),
7432 zpool_get_name(zhp));
7435 (void) printf(gettext(" %s\n"), fname);
7446 upgrade_cb(zpool_handle_t *zhp, void *arg)
7448 upgrade_cbdata_t *cbp = arg;
7451 boolean_t printnl = B_FALSE;
7454 config = zpool_get_config(zhp, NULL);
7455 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7458 assert(SPA_VERSION_IS_SUPPORTED(version));
7460 if (version < cbp->cb_version) {
7461 cbp->cb_first = B_FALSE;
7462 ret = upgrade_version(zhp, cbp->cb_version);
7468 * If they did "zpool upgrade -a", then we could
7469 * be doing ioctls to different pools. We need
7470 * to log this history once to each pool, and bypass
7471 * the normal history logging that happens in main().
7473 (void) zpool_log_history(g_zfs, history_str);
7474 log_history = B_FALSE;
7477 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7479 ret = upgrade_enable_all(zhp, &count);
7484 cbp->cb_first = B_FALSE;
7490 (void) printf(gettext("\n"));
7497 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
7499 upgrade_cbdata_t *cbp = arg;
7503 config = zpool_get_config(zhp, NULL);
7504 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7507 assert(SPA_VERSION_IS_SUPPORTED(version));
7509 if (version < SPA_VERSION_FEATURES) {
7510 if (cbp->cb_first) {
7511 (void) printf(gettext("The following pools are "
7512 "formatted with legacy version numbers and can\n"
7513 "be upgraded to use feature flags. After "
7514 "being upgraded, these pools\nwill no "
7515 "longer be accessible by software that does not "
7516 "support feature\nflags.\n\n"));
7517 (void) printf(gettext("VER POOL\n"));
7518 (void) printf(gettext("--- ------------\n"));
7519 cbp->cb_first = B_FALSE;
7522 (void) printf("%2llu %s\n", (u_longlong_t)version,
7523 zpool_get_name(zhp));
7530 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
7532 upgrade_cbdata_t *cbp = arg;
7536 config = zpool_get_config(zhp, NULL);
7537 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7540 if (version >= SPA_VERSION_FEATURES) {
7542 boolean_t poolfirst = B_TRUE;
7543 nvlist_t *enabled = zpool_get_features(zhp);
7545 for (i = 0; i < SPA_FEATURES; i++) {
7546 const char *fguid = spa_feature_table[i].fi_guid;
7547 const char *fname = spa_feature_table[i].fi_uname;
7548 if (!nvlist_exists(enabled, fguid)) {
7549 if (cbp->cb_first) {
7550 (void) printf(gettext("\nSome "
7551 "supported features are not "
7552 "enabled on the following pools. "
7553 "Once a\nfeature is enabled the "
7554 "pool may become incompatible with "
7555 "software\nthat does not support "
7557 "zpool-features(5) for "
7559 (void) printf(gettext("POOL "
7561 (void) printf(gettext("------"
7563 cbp->cb_first = B_FALSE;
7567 (void) printf(gettext("%s\n"),
7568 zpool_get_name(zhp));
7569 poolfirst = B_FALSE;
7572 (void) printf(gettext(" %s\n"), fname);
7575 * If they did "zpool upgrade -a", then we could
7576 * be doing ioctls to different pools. We need
7577 * to log this history once to each pool, and bypass
7578 * the normal history logging that happens in main().
7580 (void) zpool_log_history(g_zfs, history_str);
7581 log_history = B_FALSE;
7590 upgrade_one(zpool_handle_t *zhp, void *data)
7592 boolean_t printnl = B_FALSE;
7593 upgrade_cbdata_t *cbp = data;
7594 uint64_t cur_version;
7597 if (strcmp("log", zpool_get_name(zhp)) == 0) {
7598 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
7599 "Pool 'log' must be renamed using export and import"
7604 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7605 if (cur_version > cbp->cb_version) {
7606 (void) printf(gettext("Pool '%s' is already formatted "
7607 "using more current version '%llu'.\n\n"),
7608 zpool_get_name(zhp), (u_longlong_t)cur_version);
7612 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
7613 (void) printf(gettext("Pool '%s' is already formatted "
7614 "using version %llu.\n\n"), zpool_get_name(zhp),
7615 (u_longlong_t)cbp->cb_version);
7619 if (cur_version != cbp->cb_version) {
7621 ret = upgrade_version(zhp, cbp->cb_version);
7626 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7628 ret = upgrade_enable_all(zhp, &count);
7634 } else if (cur_version == SPA_VERSION) {
7635 (void) printf(gettext("Pool '%s' already has all "
7636 "supported features enabled.\n"),
7637 zpool_get_name(zhp));
7642 (void) printf(gettext("\n"));
7651 * zpool upgrade [-V version] <-a | pool ...>
7653 * With no arguments, display downrev'd ZFS pool available for upgrade.
7654 * Individual pools can be upgraded by specifying the pool, and '-a' will
7655 * upgrade all pools.
7658 zpool_do_upgrade(int argc, char **argv)
7661 upgrade_cbdata_t cb = { 0 };
7663 boolean_t showversions = B_FALSE;
7664 boolean_t upgradeall = B_FALSE;
7669 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7672 upgradeall = B_TRUE;
7675 showversions = B_TRUE;
7678 cb.cb_version = strtoll(optarg, &end, 10);
7680 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7681 (void) fprintf(stderr,
7682 gettext("invalid version '%s'\n"), optarg);
7687 (void) fprintf(stderr, gettext("missing argument for "
7688 "'%c' option\n"), optopt);
7692 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7703 if (cb.cb_version == 0) {
7704 cb.cb_version = SPA_VERSION;
7705 } else if (!upgradeall && argc == 0) {
7706 (void) fprintf(stderr, gettext("-V option is "
7707 "incompatible with other arguments\n"));
7712 if (upgradeall || argc != 0) {
7713 (void) fprintf(stderr, gettext("-v option is "
7714 "incompatible with other arguments\n"));
7717 } else if (upgradeall) {
7719 (void) fprintf(stderr, gettext("-a option should not "
7720 "be used along with a pool name\n"));
7725 (void) printf(gettext("This system supports ZFS pool feature "
7730 (void) printf(gettext("The following features are "
7732 (void) printf(gettext("FEAT DESCRIPTION\n"));
7733 (void) printf("----------------------------------------------"
7734 "---------------\n");
7735 for (i = 0; i < SPA_FEATURES; i++) {
7736 zfeature_info_t *fi = &spa_feature_table[i];
7738 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7739 " (read-only compatible)" : "";
7741 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7742 (void) printf(" %s\n", fi->fi_desc);
7744 (void) printf("\n");
7746 (void) printf(gettext("The following legacy versions are also "
7748 (void) printf(gettext("VER DESCRIPTION\n"));
7749 (void) printf("--- -----------------------------------------"
7750 "---------------\n");
7751 (void) printf(gettext(" 1 Initial ZFS version\n"));
7752 (void) printf(gettext(" 2 Ditto blocks "
7753 "(replicated metadata)\n"));
7754 (void) printf(gettext(" 3 Hot spares and double parity "
7756 (void) printf(gettext(" 4 zpool history\n"));
7757 (void) printf(gettext(" 5 Compression using the gzip "
7759 (void) printf(gettext(" 6 bootfs pool property\n"));
7760 (void) printf(gettext(" 7 Separate intent log devices\n"));
7761 (void) printf(gettext(" 8 Delegated administration\n"));
7762 (void) printf(gettext(" 9 refquota and refreservation "
7764 (void) printf(gettext(" 10 Cache devices\n"));
7765 (void) printf(gettext(" 11 Improved scrub performance\n"));
7766 (void) printf(gettext(" 12 Snapshot properties\n"));
7767 (void) printf(gettext(" 13 snapused property\n"));
7768 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7769 (void) printf(gettext(" 15 user/group space accounting\n"));
7770 (void) printf(gettext(" 16 stmf property support\n"));
7771 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7772 (void) printf(gettext(" 18 Snapshot user holds\n"));
7773 (void) printf(gettext(" 19 Log device removal\n"));
7774 (void) printf(gettext(" 20 Compression using zle "
7775 "(zero-length encoding)\n"));
7776 (void) printf(gettext(" 21 Deduplication\n"));
7777 (void) printf(gettext(" 22 Received properties\n"));
7778 (void) printf(gettext(" 23 Slim ZIL\n"));
7779 (void) printf(gettext(" 24 System attributes\n"));
7780 (void) printf(gettext(" 25 Improved scrub stats\n"));
7781 (void) printf(gettext(" 26 Improved snapshot deletion "
7783 (void) printf(gettext(" 27 Improved snapshot creation "
7785 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7786 (void) printf(gettext("\nFor more information on a particular "
7787 "version, including supported releases,\n"));
7788 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7789 } else if (argc == 0 && upgradeall) {
7790 cb.cb_first = B_TRUE;
7791 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7792 if (ret == 0 && cb.cb_first) {
7793 if (cb.cb_version == SPA_VERSION) {
7794 (void) printf(gettext("All pools are already "
7795 "formatted using feature flags.\n\n"));
7796 (void) printf(gettext("Every feature flags "
7797 "pool already has all supported features "
7800 (void) printf(gettext("All pools are already "
7801 "formatted with version %llu or higher.\n"),
7802 (u_longlong_t)cb.cb_version);
7805 } else if (argc == 0) {
7806 cb.cb_first = B_TRUE;
7807 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7811 (void) printf(gettext("All pools are formatted "
7812 "using feature flags.\n\n"));
7814 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7815 "for a list of available legacy versions.\n"));
7818 cb.cb_first = B_TRUE;
7819 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7823 (void) printf(gettext("Every feature flags pool has "
7824 "all supported features enabled.\n"));
7826 (void) printf(gettext("\n"));
7829 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7836 typedef struct hist_cbdata {
7843 * Print out the command history for a specific pool.
7846 get_history_one(zpool_handle_t *zhp, void *data)
7852 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7854 cb->first = B_FALSE;
7856 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7858 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7861 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7862 &records, &numrecords) == 0);
7863 for (i = 0; i < numrecords; i++) {
7864 nvlist_t *rec = records[i];
7867 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7871 tsec = fnvlist_lookup_uint64(records[i],
7873 (void) localtime_r(&tsec, &t);
7874 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7877 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7878 (void) printf("%s %s", tbuf,
7879 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7880 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7882 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7885 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7886 (void) printf("%s unrecognized record:\n",
7888 dump_nvlist(rec, 4);
7891 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7892 zfs_history_event_names[ievent],
7893 (longlong_t)fnvlist_lookup_uint64(
7894 rec, ZPOOL_HIST_TXG),
7895 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7896 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7899 (void) printf("%s [txg:%lld] %s", tbuf,
7900 (longlong_t)fnvlist_lookup_uint64(
7901 rec, ZPOOL_HIST_TXG),
7902 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7903 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7904 (void) printf(" %s (%llu)",
7905 fnvlist_lookup_string(rec,
7907 (u_longlong_t)fnvlist_lookup_uint64(rec,
7910 (void) printf(" %s", fnvlist_lookup_string(rec,
7911 ZPOOL_HIST_INT_STR));
7912 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7915 (void) printf("%s ioctl %s\n", tbuf,
7916 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7917 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7918 (void) printf(" input:\n");
7919 dump_nvlist(fnvlist_lookup_nvlist(rec,
7920 ZPOOL_HIST_INPUT_NVL), 8);
7922 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7923 (void) printf(" output:\n");
7924 dump_nvlist(fnvlist_lookup_nvlist(rec,
7925 ZPOOL_HIST_OUTPUT_NVL), 8);
7927 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
7928 (void) printf(" errno: %lld\n",
7929 (longlong_t)fnvlist_lookup_int64(rec,
7935 (void) printf("%s unrecognized record:\n", tbuf);
7936 dump_nvlist(rec, 4);
7940 (void) printf("\n");
7943 (void) printf(" [");
7944 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7945 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7946 struct passwd *pwd = getpwuid(who);
7947 (void) printf("user %d ", (int)who);
7949 (void) printf("(%s) ", pwd->pw_name);
7951 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7952 (void) printf("on %s",
7953 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7955 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7956 (void) printf(":%s",
7957 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7961 (void) printf("\n");
7963 (void) printf("\n");
7970 * zpool history <pool>
7972 * Displays the history of commands that modified pools.
7975 zpool_do_history(int argc, char **argv)
7977 hist_cbdata_t cbdata = { 0 };
7981 cbdata.first = B_TRUE;
7983 while ((c = getopt(argc, argv, "li")) != -1) {
7986 cbdata.longfmt = B_TRUE;
7989 cbdata.internal = B_TRUE;
7992 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8000 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
8003 if (argc == 0 && cbdata.first == B_TRUE) {
8004 (void) fprintf(stderr, gettext("no pools available\n"));
8011 typedef struct ev_opts {
8016 char poolname[ZFS_MAX_DATASET_NAME_LEN];
8020 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
8022 char ctime_str[26], str[32], *ptr;
8026 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
8027 memset(str, ' ', 32);
8028 (void) ctime_r((const time_t *)&tv[0], ctime_str);
8029 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
8030 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
8031 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
8032 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
8034 (void) printf(gettext("%s\t"), str);
8036 (void) printf(gettext("%s "), str);
8038 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
8039 (void) printf(gettext("%s\n"), ptr);
8043 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
8047 for (nvp = nvlist_next_nvpair(nvl, NULL);
8048 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
8050 data_type_t type = nvpair_type(nvp);
8051 const char *name = nvpair_name(nvp);
8061 printf(gettext("%*s%s = "), depth, "", name);
8064 case DATA_TYPE_BOOLEAN:
8065 printf(gettext("%s"), "1");
8068 case DATA_TYPE_BOOLEAN_VALUE:
8069 (void) nvpair_value_boolean_value(nvp, &b);
8070 printf(gettext("%s"), b ? "1" : "0");
8073 case DATA_TYPE_BYTE:
8074 (void) nvpair_value_byte(nvp, &i8);
8075 printf(gettext("0x%x"), i8);
8078 case DATA_TYPE_INT8:
8079 (void) nvpair_value_int8(nvp, (void *)&i8);
8080 printf(gettext("0x%x"), i8);
8083 case DATA_TYPE_UINT8:
8084 (void) nvpair_value_uint8(nvp, &i8);
8085 printf(gettext("0x%x"), i8);
8088 case DATA_TYPE_INT16:
8089 (void) nvpair_value_int16(nvp, (void *)&i16);
8090 printf(gettext("0x%x"), i16);
8093 case DATA_TYPE_UINT16:
8094 (void) nvpair_value_uint16(nvp, &i16);
8095 printf(gettext("0x%x"), i16);
8098 case DATA_TYPE_INT32:
8099 (void) nvpair_value_int32(nvp, (void *)&i32);
8100 printf(gettext("0x%x"), i32);
8103 case DATA_TYPE_UINT32:
8104 (void) nvpair_value_uint32(nvp, &i32);
8105 printf(gettext("0x%x"), i32);
8108 case DATA_TYPE_INT64:
8109 (void) nvpair_value_int64(nvp, (void *)&i64);
8110 printf(gettext("0x%llx"), (u_longlong_t)i64);
8113 case DATA_TYPE_UINT64:
8114 (void) nvpair_value_uint64(nvp, &i64);
8116 * translate vdev state values to readable
8117 * strings to aide zpool events consumers
8120 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
8122 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
8123 printf(gettext("\"%s\" (0x%llx)"),
8124 zpool_state_to_name(i64, VDEV_AUX_NONE),
8127 printf(gettext("0x%llx"), (u_longlong_t)i64);
8131 case DATA_TYPE_HRTIME:
8132 (void) nvpair_value_hrtime(nvp, (void *)&i64);
8133 printf(gettext("0x%llx"), (u_longlong_t)i64);
8136 case DATA_TYPE_STRING:
8137 (void) nvpair_value_string(nvp, &str);
8138 printf(gettext("\"%s\""), str ? str : "<NULL>");
8141 case DATA_TYPE_NVLIST:
8142 printf(gettext("(embedded nvlist)\n"));
8143 (void) nvpair_value_nvlist(nvp, &cnv);
8144 zpool_do_events_nvprint(cnv, depth + 8);
8145 printf(gettext("%*s(end %s)"), depth, "", name);
8148 case DATA_TYPE_NVLIST_ARRAY: {
8152 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
8153 printf(gettext("(%d embedded nvlists)\n"), nelem);
8154 for (i = 0; i < nelem; i++) {
8155 printf(gettext("%*s%s[%d] = %s\n"),
8156 depth, "", name, i, "(embedded nvlist)");
8157 zpool_do_events_nvprint(val[i], depth + 8);
8158 printf(gettext("%*s(end %s[%i])\n"),
8159 depth, "", name, i);
8161 printf(gettext("%*s(end %s)\n"), depth, "", name);
8165 case DATA_TYPE_INT8_ARRAY: {
8169 (void) nvpair_value_int8_array(nvp, &val, &nelem);
8170 for (i = 0; i < nelem; i++)
8171 printf(gettext("0x%x "), val[i]);
8176 case DATA_TYPE_UINT8_ARRAY: {
8180 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
8181 for (i = 0; i < nelem; i++)
8182 printf(gettext("0x%x "), val[i]);
8187 case DATA_TYPE_INT16_ARRAY: {
8191 (void) nvpair_value_int16_array(nvp, &val, &nelem);
8192 for (i = 0; i < nelem; i++)
8193 printf(gettext("0x%x "), val[i]);
8198 case DATA_TYPE_UINT16_ARRAY: {
8202 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
8203 for (i = 0; i < nelem; i++)
8204 printf(gettext("0x%x "), val[i]);
8209 case DATA_TYPE_INT32_ARRAY: {
8213 (void) nvpair_value_int32_array(nvp, &val, &nelem);
8214 for (i = 0; i < nelem; i++)
8215 printf(gettext("0x%x "), val[i]);
8220 case DATA_TYPE_UINT32_ARRAY: {
8224 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
8225 for (i = 0; i < nelem; i++)
8226 printf(gettext("0x%x "), val[i]);
8231 case DATA_TYPE_INT64_ARRAY: {
8235 (void) nvpair_value_int64_array(nvp, &val, &nelem);
8236 for (i = 0; i < nelem; i++)
8237 printf(gettext("0x%llx "),
8238 (u_longlong_t)val[i]);
8243 case DATA_TYPE_UINT64_ARRAY: {
8247 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
8248 for (i = 0; i < nelem; i++)
8249 printf(gettext("0x%llx "),
8250 (u_longlong_t)val[i]);
8255 case DATA_TYPE_STRING_ARRAY: {
8259 (void) nvpair_value_string_array(nvp, &str, &nelem);
8260 for (i = 0; i < nelem; i++)
8261 printf(gettext("\"%s\" "),
8262 str[i] ? str[i] : "<NULL>");
8267 case DATA_TYPE_BOOLEAN_ARRAY:
8268 case DATA_TYPE_BYTE_ARRAY:
8269 case DATA_TYPE_DOUBLE:
8270 case DATA_TYPE_DONTCARE:
8271 case DATA_TYPE_UNKNOWN:
8272 printf(gettext("<unknown>"));
8276 printf(gettext("\n"));
8281 zpool_do_events_next(ev_opts_t *opts)
8284 int zevent_fd, ret, dropped;
8287 zevent_fd = open(ZFS_DEV, O_RDWR);
8288 VERIFY(zevent_fd >= 0);
8290 if (!opts->scripted)
8291 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
8294 ret = zpool_events_next(g_zfs, &nvl, &dropped,
8295 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
8296 if (ret || nvl == NULL)
8300 (void) printf(gettext("dropped %d events\n"), dropped);
8302 if (strlen(opts->poolname) > 0 &&
8303 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
8304 strcmp(opts->poolname, pool) != 0)
8307 zpool_do_events_short(nvl, opts);
8309 if (opts->verbose) {
8310 zpool_do_events_nvprint(nvl, 8);
8311 printf(gettext("\n"));
8313 (void) fflush(stdout);
8318 VERIFY(0 == close(zevent_fd));
8324 zpool_do_events_clear(ev_opts_t *opts)
8328 ret = zpool_events_clear(g_zfs, &count);
8330 (void) printf(gettext("cleared %d events\n"), count);
8336 * zpool events [-vHf [pool] | -c]
8338 * Displays events logs by ZFS.
8341 zpool_do_events(int argc, char **argv)
8343 ev_opts_t opts = { 0 };
8348 while ((c = getopt(argc, argv, "vHfc")) != -1) {
8363 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8372 (void) fprintf(stderr, gettext("too many arguments\n"));
8374 } else if (argc == 1) {
8375 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
8376 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
8377 (void) fprintf(stderr,
8378 gettext("invalid pool name '%s'\n"), opts.poolname);
8383 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
8385 (void) fprintf(stderr,
8386 gettext("invalid options combined with -c\n"));
8391 ret = zpool_do_events_clear(&opts);
8393 ret = zpool_do_events_next(&opts);
8399 get_callback(zpool_handle_t *zhp, void *data)
8401 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
8402 char value[MAXNAMELEN];
8403 zprop_source_t srctype;
8406 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8409 * Skip the special fake placeholder. This will also skip
8410 * over the name property when 'all' is specified.
8412 if (pl->pl_prop == ZPOOL_PROP_NAME &&
8413 pl == cbp->cb_proplist)
8416 if (pl->pl_prop == ZPROP_INVAL &&
8417 (zpool_prop_feature(pl->pl_user_prop) ||
8418 zpool_prop_unsupported(pl->pl_user_prop))) {
8419 srctype = ZPROP_SRC_LOCAL;
8421 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
8422 value, sizeof (value)) == 0) {
8423 zprop_print_one_property(zpool_get_name(zhp),
8424 cbp, pl->pl_user_prop, value, srctype,
8428 if (zpool_get_prop(zhp, pl->pl_prop, value,
8429 sizeof (value), &srctype, cbp->cb_literal) != 0)
8432 zprop_print_one_property(zpool_get_name(zhp), cbp,
8433 zpool_prop_to_name(pl->pl_prop), value, srctype,
8441 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8443 * -H Scripted mode. Don't display headers, and separate properties
8445 * -o List of columns to display. Defaults to
8446 * "name,property,value,source".
8447 * -p Display values in parsable (exact) format.
8449 * Get properties of pools in the system. Output space statistics
8450 * for each one as well as other attributes.
8453 zpool_do_get(int argc, char **argv)
8455 zprop_get_cbdata_t cb = { 0 };
8456 zprop_list_t fake_name = { 0 };
8461 cb.cb_first = B_TRUE;
8464 * Set up default columns and sources.
8466 cb.cb_sources = ZPROP_SRC_ALL;
8467 cb.cb_columns[0] = GET_COL_NAME;
8468 cb.cb_columns[1] = GET_COL_PROPERTY;
8469 cb.cb_columns[2] = GET_COL_VALUE;
8470 cb.cb_columns[3] = GET_COL_SOURCE;
8471 cb.cb_type = ZFS_TYPE_POOL;
8474 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
8477 cb.cb_literal = B_TRUE;
8480 cb.cb_scripted = B_TRUE;
8483 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
8485 while (*optarg != '\0') {
8486 static char *col_subopts[] =
8487 { "name", "property", "value", "source",
8490 if (i == ZFS_GET_NCOLS) {
8491 (void) fprintf(stderr, gettext("too "
8492 "many fields given to -o "
8497 switch (getsubopt(&optarg, col_subopts,
8500 cb.cb_columns[i++] = GET_COL_NAME;
8503 cb.cb_columns[i++] = GET_COL_PROPERTY;
8506 cb.cb_columns[i++] = GET_COL_VALUE;
8509 cb.cb_columns[i++] = GET_COL_SOURCE;
8513 (void) fprintf(stderr,
8514 gettext("\"all\" conflicts "
8515 "with specific fields "
8516 "given to -o option\n"));
8519 cb.cb_columns[0] = GET_COL_NAME;
8520 cb.cb_columns[1] = GET_COL_PROPERTY;
8521 cb.cb_columns[2] = GET_COL_VALUE;
8522 cb.cb_columns[3] = GET_COL_SOURCE;
8526 (void) fprintf(stderr,
8527 gettext("invalid column name "
8534 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8544 (void) fprintf(stderr, gettext("missing property "
8549 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
8550 ZFS_TYPE_POOL) != 0)
8556 if (cb.cb_proplist != NULL) {
8557 fake_name.pl_prop = ZPOOL_PROP_NAME;
8558 fake_name.pl_width = strlen(gettext("NAME"));
8559 fake_name.pl_next = cb.cb_proplist;
8560 cb.cb_proplist = &fake_name;
8563 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
8566 if (cb.cb_proplist == &fake_name)
8567 zprop_free_list(fake_name.pl_next);
8569 zprop_free_list(cb.cb_proplist);
8574 typedef struct set_cbdata {
8577 boolean_t cb_any_successful;
8581 set_callback(zpool_handle_t *zhp, void *data)
8584 set_cbdata_t *cb = (set_cbdata_t *)data;
8586 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
8589 cb->cb_any_successful = B_TRUE;
8595 zpool_do_set(int argc, char **argv)
8597 set_cbdata_t cb = { 0 };
8600 if (argc > 1 && argv[1][0] == '-') {
8601 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8607 (void) fprintf(stderr, gettext("missing property=value "
8613 (void) fprintf(stderr, gettext("missing pool name\n"));
8618 (void) fprintf(stderr, gettext("too many pool names\n"));
8622 cb.cb_propname = argv[1];
8623 cb.cb_value = strchr(cb.cb_propname, '=');
8624 if (cb.cb_value == NULL) {
8625 (void) fprintf(stderr, gettext("missing value in "
8626 "property=value argument\n"));
8630 *(cb.cb_value) = '\0';
8633 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8640 find_command_idx(char *command, int *idx)
8644 for (i = 0; i < NCOMMAND; i++) {
8645 if (command_table[i].name == NULL)
8648 if (strcmp(command, command_table[i].name) == 0) {
8657 main(int argc, char **argv)
8664 (void) setlocale(LC_ALL, "");
8665 (void) textdomain(TEXT_DOMAIN);
8671 * Make sure the user has specified some command.
8674 (void) fprintf(stderr, gettext("missing command\n"));
8683 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8686 if ((g_zfs = libzfs_init()) == NULL) {
8687 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8691 libzfs_print_on_error(g_zfs, B_TRUE);
8693 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8696 * Many commands modify input strings for string parsing reasons.
8697 * We create a copy to protect the original argv.
8699 newargv = malloc((argc + 1) * sizeof (newargv[0]));
8700 for (i = 0; i < argc; i++)
8701 newargv[i] = strdup(argv[i]);
8702 newargv[argc] = NULL;
8705 * Run the appropriate command.
8707 if (find_command_idx(cmdname, &i) == 0) {
8708 current_command = &command_table[i];
8709 ret = command_table[i].func(argc - 1, newargv + 1);
8710 } else if (strchr(cmdname, '=')) {
8711 verify(find_command_idx("set", &i) == 0);
8712 current_command = &command_table[i];
8713 ret = command_table[i].func(argc, newargv);
8714 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8716 * 'freeze' is a vile debugging abomination, so we treat
8719 zfs_cmd_t zc = {"\0"};
8721 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8722 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8724 (void) fprintf(stderr,
8725 gettext("failed to freeze pool: %d\n"), errno);
8731 (void) fprintf(stderr, gettext("unrecognized "
8732 "command '%s'\n"), cmdname);
8737 for (i = 0; i < argc; i++)
8741 if (ret == 0 && log_history)
8742 (void) zpool_log_history(g_zfs, history_str);
8747 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8748 * for the purposes of running ::findleaks.
8750 if (getenv("ZFS_ABORT") != NULL) {
8751 (void) printf("dumping core by request\n");