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 **);
101 static int zpool_do_import(int, char **);
102 static int zpool_do_export(int, char **);
104 static int zpool_do_upgrade(int, char **);
106 static int zpool_do_history(int, char **);
107 static int zpool_do_events(int, char **);
109 static int zpool_do_get(int, char **);
110 static int zpool_do_set(int, char **);
112 static int zpool_do_sync(int, char **);
115 * These libumem hooks provide a reasonable set of defaults for the allocator's
116 * debugging facilities.
121 _umem_debug_init(void)
123 return ("default,verbose"); /* $UMEM_DEBUG setting */
127 _umem_logging_init(void)
129 return ("fail,contents"); /* $UMEM_LOGGING setting */
165 * Flags for stats to display with "zpool iostats"
173 IOS_COUNT, /* always last element */
176 /* iostat_type entries as bitmasks */
177 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
178 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
179 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
180 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
181 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
183 /* Mask of all the histo bits */
184 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
187 * Lookup table for iostat flags to nvlist names. Basically a list
188 * of all the nvlists a flag requires. Also specifies the order in
189 * which data gets printed in zpool iostat.
191 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
193 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
194 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
195 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
196 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
197 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
198 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
200 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
201 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
204 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
205 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
206 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
207 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
210 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
211 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
212 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
213 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
214 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
217 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
218 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
219 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
220 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
221 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
222 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
223 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
224 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
225 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
226 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
232 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
233 * Right now, only one histo bit is ever set at one time, so we can
234 * just do a highbit64(a)
236 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
238 typedef struct zpool_command {
240 int (*func)(int, char **);
245 * Master command table. Each ZFS command has a name, associated function, and
246 * usage message. The usage messages need to be internationalized, so we have
247 * to have a function to return the usage message based on a command index.
249 * These commands are organized according to how they are displayed in the usage
250 * message. An empty command (one with a NULL name) indicates an empty line in
251 * the generic usage message.
253 static zpool_command_t command_table[] = {
254 { "create", zpool_do_create, HELP_CREATE },
255 { "destroy", zpool_do_destroy, HELP_DESTROY },
257 { "add", zpool_do_add, HELP_ADD },
258 { "remove", zpool_do_remove, HELP_REMOVE },
260 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
262 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
264 { "list", zpool_do_list, HELP_LIST },
265 { "iostat", zpool_do_iostat, HELP_IOSTAT },
266 { "status", zpool_do_status, HELP_STATUS },
268 { "online", zpool_do_online, HELP_ONLINE },
269 { "offline", zpool_do_offline, HELP_OFFLINE },
270 { "clear", zpool_do_clear, HELP_CLEAR },
271 { "reopen", zpool_do_reopen, HELP_REOPEN },
273 { "attach", zpool_do_attach, HELP_ATTACH },
274 { "detach", zpool_do_detach, HELP_DETACH },
275 { "replace", zpool_do_replace, HELP_REPLACE },
276 { "split", zpool_do_split, HELP_SPLIT },
278 { "scrub", zpool_do_scrub, HELP_SCRUB },
280 { "import", zpool_do_import, HELP_IMPORT },
281 { "export", zpool_do_export, HELP_EXPORT },
282 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
283 { "reguid", zpool_do_reguid, HELP_REGUID },
285 { "history", zpool_do_history, HELP_HISTORY },
286 { "events", zpool_do_events, HELP_EVENTS },
288 { "get", zpool_do_get, HELP_GET },
289 { "set", zpool_do_set, HELP_SET },
290 { "sync", zpool_do_sync, HELP_SYNC },
293 #define NCOMMAND (ARRAY_SIZE(command_table))
295 #define VDEV_ALLOC_CLASS_LOGS "logs"
297 static zpool_command_t *current_command;
298 static char history_str[HIS_MAX_RECORD_LEN];
299 static boolean_t log_history = B_TRUE;
300 static uint_t timestamp_fmt = NODATE;
303 get_usage(zpool_help_t idx)
307 return (gettext("\tadd [-fgLnP] [-o property=value] "
308 "<pool> <vdev> ...\n"));
310 return (gettext("\tattach [-f] [-o property=value] "
311 "<pool> <device> <new-device>\n"));
313 return (gettext("\tclear [-nF] <pool> [device]\n"));
315 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
316 "\t [-O file-system-property=value] ... \n"
317 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
318 case HELP_CHECKPOINT:
319 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
321 return (gettext("\tdestroy [-f] <pool>\n"));
323 return (gettext("\tdetach <pool> <device>\n"));
325 return (gettext("\texport [-af] <pool> ...\n"));
327 return (gettext("\thistory [-il] [<pool>] ...\n"));
329 return (gettext("\timport [-d dir] [-D]\n"
330 "\timport [-o mntopts] [-o property=value] ... \n"
331 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
332 "[-R root] [-F [-n]] -a\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]]\n"
336 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
338 return (gettext("\tiostat [[[-c [script1,script2,...]"
339 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
340 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
341 " [interval [count]]\n"));
342 case HELP_LABELCLEAR:
343 return (gettext("\tlabelclear [-f] <vdev>\n"));
345 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
346 "[-T d|u] [pool] ... \n"
347 "\t [interval [count]]\n"));
349 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
351 return (gettext("\tonline [-e] <pool> <device> ...\n"));
353 return (gettext("\treplace [-f] [-o property=value] "
354 "<pool> <device> [new-device]\n"));
356 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
358 return (gettext("\treopen [-n] <pool>\n"));
360 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
362 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
363 "[-T d|u] [pool] ... \n"
364 "\t [interval [count]]\n"));
366 return (gettext("\tupgrade\n"
368 "\tupgrade [-V version] <-a | pool ...>\n"));
370 return (gettext("\tevents [-vHf [pool] | -c]\n"));
372 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
373 "<\"all\" | property[,...]> <pool> ...\n"));
375 return (gettext("\tset <property=value> <pool> \n"));
377 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
378 "\t [-o property=value] <pool> <newpool> "
379 "[<device> ...]\n"));
381 return (gettext("\treguid <pool>\n"));
383 return (gettext("\tsync [pool] ...\n"));
392 * Callback routine that will print out a pool property value.
395 print_prop_cb(int prop, void *cb)
399 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
401 if (zpool_prop_readonly(prop))
402 (void) fprintf(fp, " NO ");
404 (void) fprintf(fp, " YES ");
406 if (zpool_prop_values(prop) == NULL)
407 (void) fprintf(fp, "-\n");
409 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
415 * Display usage message. If we're inside a command, display only the usage for
416 * that command. Otherwise, iterate over the entire command table and display
417 * a complete usage message.
420 usage(boolean_t requested)
422 FILE *fp = requested ? stdout : stderr;
424 if (current_command == NULL) {
427 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
429 gettext("where 'command' is one of the following:\n\n"));
431 for (i = 0; i < NCOMMAND; i++) {
432 if (command_table[i].name == NULL)
433 (void) fprintf(fp, "\n");
435 (void) fprintf(fp, "%s",
436 get_usage(command_table[i].usage));
439 (void) fprintf(fp, gettext("usage:\n"));
440 (void) fprintf(fp, "%s", get_usage(current_command->usage));
443 if (current_command != NULL &&
444 ((strcmp(current_command->name, "set") == 0) ||
445 (strcmp(current_command->name, "get") == 0) ||
446 (strcmp(current_command->name, "list") == 0))) {
449 gettext("\nthe following properties are supported:\n"));
451 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
452 "PROPERTY", "EDIT", "VALUES");
454 /* Iterate over all properties */
455 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
458 (void) fprintf(fp, "\t%-19s ", "feature@...");
459 (void) fprintf(fp, "YES disabled | enabled | active\n");
461 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
462 "appended with a feature name.\nSee zpool-features(5).\n"));
466 * See comments at end of main().
468 if (getenv("ZFS_ABORT") != NULL) {
469 (void) printf("dumping core by request\n");
473 exit(requested ? 0 : 2);
477 * print a pool vdev config for dry runs
480 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
481 const char *match, int name_flags)
486 boolean_t printed = B_FALSE;
488 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
489 &child, &children) != 0) {
491 (void) printf("\t%*s%s\n", indent, "", name);
495 for (c = 0; c < children; c++) {
496 uint64_t is_log = B_FALSE;
499 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
502 class = VDEV_ALLOC_BIAS_LOG;
503 (void) nvlist_lookup_string(child[c],
504 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
505 if (strcmp(match, class) != 0)
508 if (!printed && name != NULL) {
509 (void) printf("\t%*s%s\n", indent, "", name);
512 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
513 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
520 prop_list_contains_feature(nvlist_t *proplist)
523 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
524 nvp = nvlist_next_nvpair(proplist, nvp)) {
525 if (zpool_prop_feature(nvpair_name(nvp)))
532 * Add a property pair (name, string-value) into a property nvlist.
535 add_prop_list(const char *propname, char *propval, nvlist_t **props,
538 zpool_prop_t prop = ZPOOL_PROP_INVAL;
543 if (*props == NULL &&
544 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
545 (void) fprintf(stderr,
546 gettext("internal error: out of memory\n"));
553 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
555 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
556 !zpool_prop_feature(propname)) {
557 (void) fprintf(stderr, gettext("property '%s' is "
558 "not a valid pool property\n"), propname);
563 * feature@ properties and version should not be specified
566 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
567 nvlist_exists(proplist, vname)) ||
568 (prop == ZPOOL_PROP_VERSION &&
569 prop_list_contains_feature(proplist))) {
570 (void) fprintf(stderr, gettext("'feature@' and "
571 "'version' properties cannot be specified "
577 if (zpool_prop_feature(propname))
580 normnm = zpool_prop_to_name(prop);
582 zfs_prop_t fsprop = zfs_name_to_prop(propname);
584 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
586 normnm = zfs_prop_to_name(fsprop);
587 } else if (zfs_prop_user(propname) ||
588 zfs_prop_userquota(propname)) {
591 (void) fprintf(stderr, gettext("property '%s' is "
592 "not a valid filesystem property\n"), propname);
597 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
598 prop != ZPOOL_PROP_CACHEFILE) {
599 (void) fprintf(stderr, gettext("property '%s' "
600 "specified multiple times\n"), propname);
604 if (nvlist_add_string(proplist, normnm, propval) != 0) {
605 (void) fprintf(stderr, gettext("internal "
606 "error: out of memory\n"));
614 * Set a default property pair (name, string-value) in a property nvlist
617 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
622 if (nvlist_lookup_string(*props, propname, &pval) == 0)
625 return (add_prop_list(propname, propval, props, B_TRUE));
629 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
631 * -f Force addition of devices, even if they appear in use
632 * -g Display guid for individual vdev name.
633 * -L Follow links when resolving vdev path name.
634 * -n Do not add the devices, but display the resulting layout if
635 * they were to be added.
636 * -o Set property=value.
637 * -P Display full path for vdev name.
639 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
640 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
644 zpool_do_add(int argc, char **argv)
646 boolean_t force = B_FALSE;
647 boolean_t dryrun = B_FALSE;
655 nvlist_t *props = NULL;
659 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
665 name_flags |= VDEV_NAME_GUID;
668 name_flags |= VDEV_NAME_FOLLOW_LINKS;
674 if ((propval = strchr(optarg, '=')) == NULL) {
675 (void) fprintf(stderr, gettext("missing "
676 "'=' for -o option\n"));
682 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
683 (add_prop_list(optarg, propval, &props, B_TRUE)))
687 name_flags |= VDEV_NAME_PATH;
690 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
699 /* get pool name and check number of arguments */
701 (void) fprintf(stderr, gettext("missing pool name argument\n"));
705 (void) fprintf(stderr, gettext("missing vdev specification\n"));
714 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
717 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
718 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
724 /* unless manually specified use "ashift" pool property (if set) */
725 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
728 char strval[ZPOOL_MAXPROPLEN];
730 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
731 if (src != ZPROP_SRC_DEFAULT) {
732 (void) sprintf(strval, "%" PRId32, intval);
733 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
734 &props, B_TRUE) == 0);
738 /* pass off to get_vdev_spec for processing */
739 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
741 if (nvroot == NULL) {
747 nvlist_t *poolnvroot;
749 uint_t l2children, c;
751 boolean_t hadcache = B_FALSE;
753 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
756 (void) printf(gettext("would update '%s' to the following "
757 "configuration:\n"), zpool_get_name(zhp));
759 /* print original main pool and new tree */
760 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
761 name_flags | VDEV_NAME_TYPE_ID);
762 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
764 /* print other classes: 'dedup', 'special', and 'log' */
765 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
766 VDEV_ALLOC_BIAS_DEDUP, name_flags);
767 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP,
770 print_vdev_tree(zhp, "special", poolnvroot, 0,
771 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
772 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL,
775 print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG,
777 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG,
780 /* Do the same for the caches */
781 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
782 &l2child, &l2children) == 0 && l2children) {
784 (void) printf(gettext("\tcache\n"));
785 for (c = 0; c < l2children; c++) {
786 vname = zpool_vdev_name(g_zfs, NULL,
787 l2child[c], name_flags);
788 (void) printf("\t %s\n", vname);
792 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
793 &l2child, &l2children) == 0 && l2children) {
795 (void) printf(gettext("\tcache\n"));
796 for (c = 0; c < l2children; c++) {
797 vname = zpool_vdev_name(g_zfs, NULL,
798 l2child[c], name_flags);
799 (void) printf("\t %s\n", vname);
806 ret = (zpool_add(zhp, nvroot) != 0);
817 * zpool remove <pool> <vdev> ...
819 * Removes the given vdev from the pool.
822 zpool_do_remove(int argc, char **argv)
826 zpool_handle_t *zhp = NULL;
827 boolean_t stop = B_FALSE;
829 boolean_t noop = B_FALSE;
830 boolean_t parsable = B_FALSE;
833 while ((c = getopt(argc, argv, "nps")) != -1) {
845 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
854 /* get pool name and check number of arguments */
856 (void) fprintf(stderr, gettext("missing pool name argument\n"));
862 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
866 (void) fprintf(stderr, gettext("stop request ignored\n"));
872 (void) fprintf(stderr, gettext("too many arguments\n"));
875 if (zpool_vdev_remove_cancel(zhp) != 0)
879 (void) fprintf(stderr, gettext("missing device\n"));
883 for (i = 1; i < argc; i++) {
887 if (zpool_vdev_indirect_size(zhp, argv[i],
893 (void) printf("%s %llu\n",
894 argv[i], (unsigned long long)size);
897 zfs_nicenum(size, valstr,
899 (void) printf("Memory that will be "
900 "used after removing %s: %s\n",
904 if (zpool_vdev_remove(zhp, argv[i]) != 0)
915 * zpool labelclear [-f] <vdev>
917 * -f Force clearing the label for the vdevs which are members of
918 * the exported or foreign pools.
920 * Verifies that the vdev is not active and zeros out the label information
924 zpool_do_labelclear(int argc, char **argv)
926 char vdev[MAXPATHLEN];
929 int c, fd = -1, ret = 0;
932 boolean_t inuse = B_FALSE;
933 boolean_t force = B_FALSE;
936 while ((c = getopt(argc, argv, "f")) != -1) {
942 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
953 (void) fprintf(stderr, gettext("missing vdev name\n"));
957 (void) fprintf(stderr, gettext("too many arguments\n"));
962 * Check if we were given absolute path and use it as is.
963 * Otherwise if the provided vdev name doesn't point to a file,
964 * try prepending expected disk paths and partition numbers.
966 (void) strlcpy(vdev, argv[0], sizeof (vdev));
967 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
970 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
971 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
972 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
976 if (error || (stat(vdev, &st) != 0)) {
977 (void) fprintf(stderr, gettext(
978 "failed to find device %s, try specifying absolute "
979 "path instead\n"), argv[0]);
984 if ((fd = open(vdev, O_RDWR)) < 0) {
985 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
986 vdev, strerror(errno));
990 if (ioctl(fd, BLKFLSBUF) != 0)
991 (void) fprintf(stderr, gettext("failed to invalidate "
992 "cache for %s: %s\n"), vdev, strerror(errno));
994 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
995 (void) fprintf(stderr,
996 gettext("failed to check state for %s\n"), vdev);
1000 nvlist_free(config);
1002 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1004 (void) fprintf(stderr,
1005 gettext("failed to check state for %s\n"), vdev);
1015 case POOL_STATE_ACTIVE:
1016 case POOL_STATE_SPARE:
1017 case POOL_STATE_L2CACHE:
1018 (void) fprintf(stderr, gettext(
1019 "%s is a member (%s) of pool \"%s\"\n"),
1020 vdev, zpool_pool_state_to_name(state), name);
1024 case POOL_STATE_EXPORTED:
1027 (void) fprintf(stderr, gettext(
1028 "use '-f' to override the following error:\n"
1029 "%s is a member of exported pool \"%s\"\n"),
1034 case POOL_STATE_POTENTIALLY_ACTIVE:
1037 (void) fprintf(stderr, gettext(
1038 "use '-f' to override the following error:\n"
1039 "%s is a member of potentially active pool \"%s\"\n"),
1044 case POOL_STATE_DESTROYED:
1045 /* inuse should never be set for a destroyed pool */
1051 ret = zpool_clear_label(fd);
1053 (void) fprintf(stderr,
1054 gettext("failed to clear label for %s\n"), vdev);
1065 * zpool create [-fnd] [-o property=value] ...
1066 * [-O file-system-property=value] ...
1067 * [-R root] [-m mountpoint] <pool> <dev> ...
1069 * -f Force creation, even if devices appear in use
1070 * -n Do not create the pool, but display the resulting layout if it
1071 * were to be created.
1072 * -R Create a pool under an alternate root
1073 * -m Set default mountpoint for the root dataset. By default it's
1075 * -o Set property=value.
1076 * -o Set feature@feature=enabled|disabled.
1077 * -d Don't automatically enable all supported pool features
1078 * (individual features can be enabled with -o).
1079 * -O Set fsproperty=value in the pool's root file system
1081 * Creates the named pool according to the given vdev specification. The
1082 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1083 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1084 * (if '-n' was specified), or pass it to libzfs to do the creation.
1087 zpool_do_create(int argc, char **argv)
1089 boolean_t force = B_FALSE;
1090 boolean_t dryrun = B_FALSE;
1091 boolean_t enable_all_pool_feat = B_TRUE;
1093 nvlist_t *nvroot = NULL;
1097 char *altroot = NULL;
1098 char *mountpoint = NULL;
1099 nvlist_t *fsprops = NULL;
1100 nvlist_t *props = NULL;
1104 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1113 enable_all_pool_feat = B_FALSE;
1117 if (add_prop_list(zpool_prop_to_name(
1118 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1120 if (add_prop_list_default(zpool_prop_to_name(
1121 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1125 /* Equivalent to -O mountpoint=optarg */
1126 mountpoint = optarg;
1129 if ((propval = strchr(optarg, '=')) == NULL) {
1130 (void) fprintf(stderr, gettext("missing "
1131 "'=' for -o option\n"));
1137 if (add_prop_list(optarg, propval, &props, B_TRUE))
1141 * If the user is creating a pool that doesn't support
1142 * feature flags, don't enable any features.
1144 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1148 ver = strtoull(propval, &end, 10);
1150 ver < SPA_VERSION_FEATURES) {
1151 enable_all_pool_feat = B_FALSE;
1154 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1158 if ((propval = strchr(optarg, '=')) == NULL) {
1159 (void) fprintf(stderr, gettext("missing "
1160 "'=' for -O option\n"));
1167 * Mountpoints are checked and then added later.
1168 * Uniquely among properties, they can be specified
1169 * more than once, to avoid conflict with -m.
1171 if (0 == strcmp(optarg,
1172 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1173 mountpoint = propval;
1174 } else if (add_prop_list(optarg, propval, &fsprops,
1181 * Sanity check temporary pool name.
1183 if (strchr(optarg, '/') != NULL) {
1184 (void) fprintf(stderr, gettext("cannot create "
1185 "'%s': invalid character '/' in temporary "
1187 (void) fprintf(stderr, gettext("use 'zfs "
1188 "create' to create a dataset\n"));
1192 if (add_prop_list(zpool_prop_to_name(
1193 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1195 if (add_prop_list_default(zpool_prop_to_name(
1196 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1201 (void) fprintf(stderr, gettext("missing argument for "
1202 "'%c' option\n"), optopt);
1205 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1214 /* get pool name and check number of arguments */
1216 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1220 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1227 * As a special case, check for use of '/' in the name, and direct the
1228 * user to use 'zfs create' instead.
1230 if (strchr(poolname, '/') != NULL) {
1231 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1232 "character '/' in pool name\n"), poolname);
1233 (void) fprintf(stderr, gettext("use 'zfs create' to "
1234 "create a dataset\n"));
1238 /* pass off to get_vdev_spec for bulk processing */
1239 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1240 argc - 1, argv + 1);
1244 /* make_root_vdev() allows 0 toplevel children if there are spares */
1245 if (!zfs_allocatable_devs(nvroot)) {
1246 (void) fprintf(stderr, gettext("invalid vdev "
1247 "specification: at least one toplevel vdev must be "
1252 if (altroot != NULL && altroot[0] != '/') {
1253 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1254 "must be an absolute path\n"), altroot);
1259 * Check the validity of the mountpoint and direct the user to use the
1260 * '-m' mountpoint option if it looks like its in use.
1262 if (mountpoint == NULL ||
1263 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1264 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1265 char buf[MAXPATHLEN];
1268 if (mountpoint && mountpoint[0] != '/') {
1269 (void) fprintf(stderr, gettext("invalid mountpoint "
1270 "'%s': must be an absolute path, 'legacy', or "
1271 "'none'\n"), mountpoint);
1275 if (mountpoint == NULL) {
1276 if (altroot != NULL)
1277 (void) snprintf(buf, sizeof (buf), "%s/%s",
1280 (void) snprintf(buf, sizeof (buf), "/%s",
1283 if (altroot != NULL)
1284 (void) snprintf(buf, sizeof (buf), "%s%s",
1285 altroot, mountpoint);
1287 (void) snprintf(buf, sizeof (buf), "%s",
1291 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1292 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1293 "%s\n"), buf, strerror(errno));
1294 (void) fprintf(stderr, gettext("use '-m' "
1295 "option to provide a different default\n"));
1300 while (count < 3 && readdir(dirp) != NULL)
1302 (void) closedir(dirp);
1305 (void) fprintf(stderr, gettext("mountpoint "
1306 "'%s' exists and is not empty\n"), buf);
1307 (void) fprintf(stderr, gettext("use '-m' "
1308 "option to provide a "
1309 "different default\n"));
1316 * Now that the mountpoint's validity has been checked, ensure that
1317 * the property is set appropriately prior to creating the pool.
1319 if (mountpoint != NULL) {
1320 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1321 mountpoint, &fsprops, B_FALSE);
1329 * For a dry run invocation, print out a basic message and run
1330 * through all the vdevs in the list and print out in an
1331 * appropriate hierarchy.
1333 (void) printf(gettext("would create '%s' with the "
1334 "following layout:\n\n"), poolname);
1336 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1337 print_vdev_tree(NULL, "dedup", nvroot, 0,
1338 VDEV_ALLOC_BIAS_DEDUP, 0);
1339 print_vdev_tree(NULL, "special", nvroot, 0,
1340 VDEV_ALLOC_BIAS_SPECIAL, 0);
1341 print_vdev_tree(NULL, "logs", nvroot, 0,
1342 VDEV_ALLOC_BIAS_LOG, 0);
1347 * Hand off to libzfs.
1350 for (i = 0; i < SPA_FEATURES; i++) {
1351 char propname[MAXPATHLEN];
1353 zfeature_info_t *feat = &spa_feature_table[i];
1355 (void) snprintf(propname, sizeof (propname),
1356 "feature@%s", feat->fi_uname);
1359 * Only features contained in props will be enabled:
1360 * remove from the nvlist every ZFS_FEATURE_DISABLED
1361 * value and add every missing ZFS_FEATURE_ENABLED if
1362 * enable_all_pool_feat is set.
1364 if (!nvlist_lookup_string(props, propname, &propval)) {
1365 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1366 (void) nvlist_remove_all(props,
1368 } else if (enable_all_pool_feat) {
1369 ret = add_prop_list(propname,
1370 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1377 if (zpool_create(g_zfs, poolname,
1378 nvroot, props, fsprops) == 0) {
1379 zfs_handle_t *pool = zfs_open(g_zfs,
1380 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1382 if (zfs_mount(pool, NULL, 0) == 0)
1383 ret = zfs_shareall(pool);
1386 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1387 (void) fprintf(stderr, gettext("pool name may have "
1393 nvlist_free(nvroot);
1394 nvlist_free(fsprops);
1398 nvlist_free(fsprops);
1405 * zpool destroy <pool>
1407 * -f Forcefully unmount any datasets
1409 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1412 zpool_do_destroy(int argc, char **argv)
1414 boolean_t force = B_FALSE;
1417 zpool_handle_t *zhp;
1421 while ((c = getopt(argc, argv, "f")) != -1) {
1427 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1436 /* check arguments */
1438 (void) fprintf(stderr, gettext("missing pool argument\n"));
1442 (void) fprintf(stderr, gettext("too many arguments\n"));
1448 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1450 * As a special case, check for use of '/' in the name, and
1451 * direct the user to use 'zfs destroy' instead.
1453 if (strchr(pool, '/') != NULL)
1454 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1455 "destroy a dataset\n"));
1459 if (zpool_disable_datasets(zhp, force) != 0) {
1460 (void) fprintf(stderr, gettext("could not destroy '%s': "
1461 "could not unmount datasets\n"), zpool_get_name(zhp));
1466 /* The history must be logged as part of the export */
1467 log_history = B_FALSE;
1469 ret = (zpool_destroy(zhp, history_str) != 0);
1476 typedef struct export_cbdata {
1478 boolean_t hardforce;
1485 zpool_export_one(zpool_handle_t *zhp, void *data)
1487 export_cbdata_t *cb = data;
1489 if (zpool_disable_datasets(zhp, cb->force) != 0)
1492 /* The history must be logged as part of the export */
1493 log_history = B_FALSE;
1495 if (cb->hardforce) {
1496 if (zpool_export_force(zhp, history_str) != 0)
1498 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1506 * zpool export [-f] <pool> ...
1508 * -a Export all pools
1509 * -f Forcefully unmount datasets
1511 * Export the given pools. By default, the command will attempt to cleanly
1512 * unmount any active datasets within the pool. If the '-f' flag is specified,
1513 * then the datasets will be forcefully unmounted.
1516 zpool_do_export(int argc, char **argv)
1519 boolean_t do_all = B_FALSE;
1520 boolean_t force = B_FALSE;
1521 boolean_t hardforce = B_FALSE;
1525 while ((c = getopt(argc, argv, "afF")) != -1) {
1537 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1544 cb.hardforce = hardforce;
1550 (void) fprintf(stderr, gettext("too many arguments\n"));
1554 return (for_each_pool(argc, argv, B_TRUE, NULL,
1555 zpool_export_one, &cb));
1558 /* check arguments */
1560 (void) fprintf(stderr, gettext("missing pool argument\n"));
1564 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1570 * Given a vdev configuration, determine the maximum width needed for the device
1574 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1582 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1583 if (strlen(name) + depth > max)
1584 max = strlen(name) + depth;
1588 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1589 &child, &children) == 0) {
1590 for (c = 0; c < children; c++)
1591 if ((ret = max_width(zhp, child[c], depth + 2,
1592 max, name_flags)) > max)
1596 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1597 &child, &children) == 0) {
1598 for (c = 0; c < children; c++)
1599 if ((ret = max_width(zhp, child[c], depth + 2,
1600 max, name_flags)) > max)
1604 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1605 &child, &children) == 0) {
1606 for (c = 0; c < children; c++)
1607 if ((ret = max_width(zhp, child[c], depth + 2,
1608 max, name_flags)) > max)
1615 typedef struct spare_cbdata {
1617 zpool_handle_t *cb_zhp;
1621 find_vdev(nvlist_t *nv, uint64_t search)
1627 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1631 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1632 &child, &children) == 0) {
1633 for (c = 0; c < children; c++)
1634 if (find_vdev(child[c], search))
1642 find_spare(zpool_handle_t *zhp, void *data)
1644 spare_cbdata_t *cbp = data;
1645 nvlist_t *config, *nvroot;
1647 config = zpool_get_config(zhp, NULL);
1648 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1651 if (find_vdev(nvroot, cbp->cb_guid)) {
1660 typedef struct status_cbdata {
1664 boolean_t cb_allpools;
1665 boolean_t cb_verbose;
1666 boolean_t cb_explain;
1668 boolean_t cb_dedup_stats;
1669 boolean_t cb_print_status;
1670 vdev_cmd_data_list_t *vcdl;
1673 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1675 is_blank_str(char *str)
1677 while (str != NULL && *str != '\0') {
1685 /* Print command output lines for specific vdev in a specific pool */
1687 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1689 vdev_cmd_data_t *data;
1693 for (i = 0; i < vcdl->count; i++) {
1694 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1695 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1696 /* Not the vdev we're looking for */
1700 data = &vcdl->data[i];
1701 /* Print out all the output values for this vdev */
1702 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1704 /* Does this vdev have values for this column? */
1705 for (int k = 0; k < data->cols_cnt; k++) {
1706 if (strcmp(data->cols[k],
1707 vcdl->uniq_cols[j]) == 0) {
1708 /* yes it does, record the value */
1709 val = data->lines[k];
1714 * Mark empty values with dashes to make output
1717 if (is_blank_str(val))
1720 printf("%*s", vcdl->uniq_cols_width[j], val);
1721 if (j < vcdl->uniq_cols_cnt - 1)
1725 /* Print out any values that aren't in a column at the end */
1726 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1727 /* Did we have any columns? If so print a spacer. */
1728 if (vcdl->uniq_cols_cnt > 0)
1731 val = data->lines[j];
1732 printf("%s", val ? val : "");
1739 * Print out configuration state as requested by status_callback.
1742 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1743 nvlist_t *nv, int depth, boolean_t isspare)
1747 pool_scan_stat_t *ps = NULL;
1749 char rbuf[6], wbuf[6], cbuf[6];
1751 uint64_t notpresent;
1752 spare_cbdata_t spare_cb;
1757 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1758 &child, &children) != 0)
1761 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1762 (uint64_t **)&vs, &c) == 0);
1764 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1766 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1769 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1772 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1775 if (vs->vs_aux == VDEV_AUX_SPARED)
1777 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1781 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1785 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1786 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1787 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1788 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1791 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1792 ¬present) == 0) {
1793 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1794 (void) printf(" was %s", path);
1795 } else if (vs->vs_aux != 0) {
1798 switch (vs->vs_aux) {
1799 case VDEV_AUX_OPEN_FAILED:
1800 (void) printf(gettext("cannot open"));
1803 case VDEV_AUX_BAD_GUID_SUM:
1804 (void) printf(gettext("missing device"));
1807 case VDEV_AUX_NO_REPLICAS:
1808 (void) printf(gettext("insufficient replicas"));
1811 case VDEV_AUX_VERSION_NEWER:
1812 (void) printf(gettext("newer version"));
1815 case VDEV_AUX_UNSUP_FEAT:
1816 (void) printf(gettext("unsupported feature(s)"));
1819 case VDEV_AUX_SPARED:
1820 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1821 &spare_cb.cb_guid) == 0);
1822 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1823 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1824 zpool_get_name(zhp)) == 0)
1825 (void) printf(gettext("currently in "
1828 (void) printf(gettext("in use by "
1830 zpool_get_name(spare_cb.cb_zhp));
1831 zpool_close(spare_cb.cb_zhp);
1833 (void) printf(gettext("currently in use"));
1837 case VDEV_AUX_ERR_EXCEEDED:
1838 (void) printf(gettext("too many errors"));
1841 case VDEV_AUX_IO_FAILURE:
1842 (void) printf(gettext("experienced I/O failures"));
1845 case VDEV_AUX_BAD_LOG:
1846 (void) printf(gettext("bad intent log"));
1849 case VDEV_AUX_EXTERNAL:
1850 (void) printf(gettext("external device fault"));
1853 case VDEV_AUX_SPLIT_POOL:
1854 (void) printf(gettext("split into new pool"));
1857 case VDEV_AUX_ACTIVE:
1858 (void) printf(gettext("currently in use"));
1861 case VDEV_AUX_CHILDREN_OFFLINE:
1862 (void) printf(gettext("all children offline"));
1866 (void) printf(gettext("corrupted data"));
1871 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1872 (uint64_t **)&ps, &c);
1874 if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1875 vs->vs_scan_processed != 0 && children == 0) {
1876 (void) printf(gettext(" (%s)"),
1877 (ps->pss_func == POOL_SCAN_RESILVER) ?
1878 "resilvering" : "repairing");
1881 if (cb->vcdl != NULL) {
1882 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1884 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1888 (void) printf("\n");
1890 for (c = 0; c < children; c++) {
1891 uint64_t islog = B_FALSE, ishole = B_FALSE;
1893 /* Don't print logs or holes here */
1894 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1896 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1898 if (islog || ishole)
1900 /* Only print normal classes here */
1901 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
1904 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1905 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1906 print_status_config(zhp, cb, vname, child[c], depth + 2,
1913 * Print the configuration of an exported pool. Iterate over all vdevs in the
1914 * pool, printing out the name and status for each one.
1917 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1925 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1926 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1927 strcmp(type, VDEV_TYPE_HOLE) == 0)
1930 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1931 (uint64_t **)&vs, &c) == 0);
1933 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1934 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1936 if (vs->vs_aux != 0) {
1939 switch (vs->vs_aux) {
1940 case VDEV_AUX_OPEN_FAILED:
1941 (void) printf(gettext("cannot open"));
1944 case VDEV_AUX_BAD_GUID_SUM:
1945 (void) printf(gettext("missing device"));
1948 case VDEV_AUX_NO_REPLICAS:
1949 (void) printf(gettext("insufficient replicas"));
1952 case VDEV_AUX_VERSION_NEWER:
1953 (void) printf(gettext("newer version"));
1956 case VDEV_AUX_UNSUP_FEAT:
1957 (void) printf(gettext("unsupported feature(s)"));
1960 case VDEV_AUX_ERR_EXCEEDED:
1961 (void) printf(gettext("too many errors"));
1964 case VDEV_AUX_ACTIVE:
1965 (void) printf(gettext("currently in use"));
1968 case VDEV_AUX_CHILDREN_OFFLINE:
1969 (void) printf(gettext("all children offline"));
1973 (void) printf(gettext("corrupted data"));
1977 (void) printf("\n");
1979 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1980 &child, &children) != 0)
1983 for (c = 0; c < children; c++) {
1984 uint64_t is_log = B_FALSE;
1986 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1990 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
1993 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1994 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1995 print_import_config(cb, vname, child[c], depth + 2);
1999 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2000 &child, &children) == 0) {
2001 (void) printf(gettext("\tcache\n"));
2002 for (c = 0; c < children; c++) {
2003 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2005 (void) printf("\t %s\n", vname);
2010 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2011 &child, &children) == 0) {
2012 (void) printf(gettext("\tspares\n"));
2013 for (c = 0; c < children; c++) {
2014 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2016 (void) printf("\t %s\n", vname);
2023 * Print specialized class vdevs.
2025 * These are recorded as top level vdevs in the main pool child array
2026 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2027 * print_status_config() or print_import_config() to print the top level
2028 * class vdevs then any of their children (eg mirrored slogs) are printed
2029 * recursively - which works because only the top level vdev is marked.
2032 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2037 boolean_t printed = B_FALSE;
2039 assert(zhp != NULL || !cb->cb_verbose);
2041 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2045 for (c = 0; c < children; c++) {
2046 uint64_t is_log = B_FALSE;
2050 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2054 bias = VDEV_ALLOC_CLASS_LOGS;
2056 (void) nvlist_lookup_string(child[c],
2057 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2058 (void) nvlist_lookup_string(child[c],
2059 ZPOOL_CONFIG_TYPE, &type);
2062 if (bias == NULL || strcmp(bias, class) != 0)
2064 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2068 (void) printf("\t%s\t\n", gettext(class));
2072 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
2073 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2074 if (cb->cb_print_status)
2075 print_status_config(zhp, cb, name, child[c], 2,
2078 print_import_config(cb, name, child[c], 2);
2084 * Display the status for the given pool.
2087 show_import(nvlist_t *config)
2089 uint64_t pool_state;
2093 uint64_t hostid = 0;
2095 char *hostname = "unknown";
2096 nvlist_t *nvroot, *nvinfo;
2097 zpool_status_t reason;
2098 zpool_errata_t errata;
2102 status_cbdata_t cb = { 0 };
2104 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2106 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2108 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2110 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2113 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2114 (uint64_t **)&vs, &vsc) == 0);
2115 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2117 reason = zpool_import_status(config, &msgid, &errata);
2119 (void) printf(gettext(" pool: %s\n"), name);
2120 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2121 (void) printf(gettext(" state: %s"), health);
2122 if (pool_state == POOL_STATE_DESTROYED)
2123 (void) printf(gettext(" (DESTROYED)"));
2124 (void) printf("\n");
2127 case ZPOOL_STATUS_MISSING_DEV_R:
2128 case ZPOOL_STATUS_MISSING_DEV_NR:
2129 case ZPOOL_STATUS_BAD_GUID_SUM:
2130 (void) printf(gettext(" status: One or more devices are "
2131 "missing from the system.\n"));
2134 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2135 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2136 (void) printf(gettext(" status: One or more devices contains "
2137 "corrupted data.\n"));
2140 case ZPOOL_STATUS_CORRUPT_DATA:
2142 gettext(" status: The pool data is corrupted.\n"));
2145 case ZPOOL_STATUS_OFFLINE_DEV:
2146 (void) printf(gettext(" status: One or more devices "
2147 "are offlined.\n"));
2150 case ZPOOL_STATUS_CORRUPT_POOL:
2151 (void) printf(gettext(" status: The pool metadata is "
2155 case ZPOOL_STATUS_VERSION_OLDER:
2156 (void) printf(gettext(" status: The pool is formatted using a "
2157 "legacy on-disk version.\n"));
2160 case ZPOOL_STATUS_VERSION_NEWER:
2161 (void) printf(gettext(" status: The pool is formatted using an "
2162 "incompatible version.\n"));
2165 case ZPOOL_STATUS_FEAT_DISABLED:
2166 (void) printf(gettext(" status: Some supported features are "
2167 "not enabled on the pool.\n"));
2170 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2171 (void) printf(gettext("status: The pool uses the following "
2172 "feature(s) not supported on this system:\n"));
2173 zpool_print_unsup_feat(config);
2176 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2177 (void) printf(gettext("status: The pool can only be accessed "
2178 "in read-only mode on this system. It\n\tcannot be "
2179 "accessed in read-write mode because it uses the "
2180 "following\n\tfeature(s) not supported on this system:\n"));
2181 zpool_print_unsup_feat(config);
2184 case ZPOOL_STATUS_HOSTID_ACTIVE:
2185 (void) printf(gettext(" status: The pool is currently "
2186 "imported by another system.\n"));
2189 case ZPOOL_STATUS_HOSTID_REQUIRED:
2190 (void) printf(gettext(" status: The pool has the "
2191 "multihost property on. It cannot\n\tbe safely imported "
2192 "when the system hostid is not set.\n"));
2195 case ZPOOL_STATUS_HOSTID_MISMATCH:
2196 (void) printf(gettext(" status: The pool was last accessed by "
2197 "another system.\n"));
2200 case ZPOOL_STATUS_FAULTED_DEV_R:
2201 case ZPOOL_STATUS_FAULTED_DEV_NR:
2202 (void) printf(gettext(" status: One or more devices are "
2206 case ZPOOL_STATUS_BAD_LOG:
2207 (void) printf(gettext(" status: An intent log record cannot be "
2211 case ZPOOL_STATUS_RESILVERING:
2212 (void) printf(gettext(" status: One or more devices were being "
2216 case ZPOOL_STATUS_ERRATA:
2217 (void) printf(gettext(" status: Errata #%d detected.\n"),
2223 * No other status can be seen when importing pools.
2225 assert(reason == ZPOOL_STATUS_OK);
2229 * Print out an action according to the overall state of the pool.
2231 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2232 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2233 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2234 (void) printf(gettext(" action: The pool can be "
2235 "imported using its name or numeric identifier, "
2236 "though\n\tsome features will not be available "
2237 "without an explicit 'zpool upgrade'.\n"));
2238 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2239 (void) printf(gettext(" action: The pool can be "
2240 "imported using its name or numeric "
2241 "identifier and\n\tthe '-f' flag.\n"));
2242 } else if (reason == ZPOOL_STATUS_ERRATA) {
2244 case ZPOOL_ERRATA_NONE:
2247 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2248 (void) printf(gettext(" action: The pool can "
2249 "be imported using its name or numeric "
2250 "identifier,\n\thowever there is a compat"
2251 "ibility issue which should be corrected"
2252 "\n\tby running 'zpool scrub'\n"));
2255 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2256 (void) printf(gettext(" action: The pool can"
2257 "not be imported with this version of ZFS "
2258 "due to\n\tan active asynchronous destroy. "
2259 "Revert to an earlier version\n\tand "
2260 "allow the destroy to complete before "
2264 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2265 (void) printf(gettext(" action: Existing "
2266 "encrypted datasets contain an on-disk "
2267 "incompatibility, which\n\tneeds to be "
2268 "corrected. Backup these datasets to new "
2269 "encrypted datasets\n\tand destroy the "
2275 * All errata must contain an action message.
2280 (void) printf(gettext(" action: The pool can be "
2281 "imported using its name or numeric "
2284 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2285 (void) printf(gettext(" action: The pool can be imported "
2286 "despite missing or damaged devices. The\n\tfault "
2287 "tolerance of the pool may be compromised if imported.\n"));
2290 case ZPOOL_STATUS_VERSION_NEWER:
2291 (void) printf(gettext(" action: The pool cannot be "
2292 "imported. Access the pool on a system running "
2293 "newer\n\tsoftware, or recreate the pool from "
2296 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2297 (void) printf(gettext("action: The pool cannot be "
2298 "imported. Access the pool on a system that "
2299 "supports\n\tthe required feature(s), or recreate "
2300 "the pool from backup.\n"));
2302 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2303 (void) printf(gettext("action: The pool cannot be "
2304 "imported in read-write mode. Import the pool "
2306 "\t\"-o readonly=on\", access the pool on a system "
2307 "that supports the\n\trequired feature(s), or "
2308 "recreate the pool from backup.\n"));
2310 case ZPOOL_STATUS_MISSING_DEV_R:
2311 case ZPOOL_STATUS_MISSING_DEV_NR:
2312 case ZPOOL_STATUS_BAD_GUID_SUM:
2313 (void) printf(gettext(" action: The pool cannot be "
2314 "imported. Attach the missing\n\tdevices and try "
2317 case ZPOOL_STATUS_HOSTID_ACTIVE:
2318 VERIFY0(nvlist_lookup_nvlist(config,
2319 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2321 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2322 hostname = fnvlist_lookup_string(nvinfo,
2323 ZPOOL_CONFIG_MMP_HOSTNAME);
2325 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2326 hostid = fnvlist_lookup_uint64(nvinfo,
2327 ZPOOL_CONFIG_MMP_HOSTID);
2329 (void) printf(gettext(" action: The pool must be "
2330 "exported from %s (hostid=%lx)\n\tbefore it "
2331 "can be safely imported.\n"), hostname,
2332 (unsigned long) hostid);
2334 case ZPOOL_STATUS_HOSTID_REQUIRED:
2335 (void) printf(gettext(" action: Set a unique system "
2336 "hostid with the zgenhostid(8) command.\n"));
2339 (void) printf(gettext(" action: The pool cannot be "
2340 "imported due to damaged devices or data.\n"));
2344 /* Print the comment attached to the pool. */
2345 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2346 (void) printf(gettext("comment: %s\n"), comment);
2349 * If the state is "closed" or "can't open", and the aux state
2350 * is "corrupt data":
2352 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2353 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2354 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2355 if (pool_state == POOL_STATE_DESTROYED)
2356 (void) printf(gettext("\tThe pool was destroyed, "
2357 "but can be imported using the '-Df' flags.\n"));
2358 else if (pool_state != POOL_STATE_EXPORTED)
2359 (void) printf(gettext("\tThe pool may be active on "
2360 "another system, but can be imported using\n\t"
2361 "the '-f' flag.\n"));
2365 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2368 (void) printf(gettext(" config:\n\n"));
2370 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2372 if (cb.cb_namewidth < 10)
2373 cb.cb_namewidth = 10;
2375 print_import_config(&cb, name, nvroot, 0);
2377 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
2378 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
2379 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
2381 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2382 (void) printf(gettext("\n\tAdditional devices are known to "
2383 "be part of this pool, though their\n\texact "
2384 "configuration cannot be determined.\n"));
2389 zfs_force_import_required(nvlist_t *config)
2392 uint64_t hostid = 0;
2395 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2396 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2398 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2401 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2402 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2403 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2404 ZPOOL_CONFIG_MMP_STATE);
2406 if (mmp_state != MMP_STATE_INACTIVE)
2414 * Perform the import for the given configuration. This passes the heavy
2415 * lifting off to zpool_import_props(), and then mounts the datasets contained
2419 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2420 nvlist_t *props, int flags)
2423 zpool_handle_t *zhp;
2427 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2428 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2430 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2431 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2432 "is formatted using an unsupported ZFS version\n"), name);
2434 } else if (zfs_force_import_required(config) &&
2435 !(flags & ZFS_IMPORT_ANY_HOST)) {
2436 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2439 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2440 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2441 mmp_state = fnvlist_lookup_uint64(nvinfo,
2442 ZPOOL_CONFIG_MMP_STATE);
2444 if (mmp_state == MMP_STATE_ACTIVE) {
2445 char *hostname = "<unknown>";
2446 uint64_t hostid = 0;
2448 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2449 hostname = fnvlist_lookup_string(nvinfo,
2450 ZPOOL_CONFIG_MMP_HOSTNAME);
2452 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2453 hostid = fnvlist_lookup_uint64(nvinfo,
2454 ZPOOL_CONFIG_MMP_HOSTID);
2456 (void) fprintf(stderr, gettext("cannot import '%s': "
2457 "pool is imported on %s (hostid: "
2458 "0x%lx)\nExport the pool on the other system, "
2459 "then run 'zpool import'.\n"),
2460 name, hostname, (unsigned long) hostid);
2461 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2462 (void) fprintf(stderr, gettext("Cannot import '%s': "
2463 "pool has the multihost property on and the\n"
2464 "system's hostid is not set. Set a unique hostid "
2465 "with the zgenhostid(8) command.\n"), name);
2467 char *hostname = "<unknown>";
2468 uint64_t timestamp = 0;
2469 uint64_t hostid = 0;
2471 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2472 hostname = fnvlist_lookup_string(config,
2473 ZPOOL_CONFIG_HOSTNAME);
2475 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2476 timestamp = fnvlist_lookup_uint64(config,
2477 ZPOOL_CONFIG_TIMESTAMP);
2479 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2480 hostid = fnvlist_lookup_uint64(config,
2481 ZPOOL_CONFIG_HOSTID);
2483 (void) fprintf(stderr, gettext("cannot import '%s': "
2484 "pool was previously in use from another system.\n"
2485 "Last accessed by %s (hostid=%lx) at %s"
2486 "The pool can be imported, use 'zpool import -f' "
2487 "to import the pool.\n"), name, hostname,
2488 (unsigned long)hostid, ctime((time_t *)×tamp));
2494 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2497 if (newname != NULL)
2498 name = (char *)newname;
2500 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2504 * Loading keys is best effort. We don't want to return immediately
2505 * if it fails but we do want to give the error to the caller.
2507 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2508 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2513 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2514 !(flags & ZFS_IMPORT_ONLY) &&
2515 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2525 * zpool checkpoint <pool>
2526 * checkpoint --discard <pool>
2528 * -d Discard the checkpoint from a checkpointed
2531 * Checkpoints the specified pool, by taking a "snapshot" of its
2532 * current state. A pool can only have one checkpoint at a time.
2535 zpool_do_checkpoint(int argc, char **argv)
2539 zpool_handle_t *zhp;
2542 struct option long_options[] = {
2543 {"discard", no_argument, NULL, 'd'},
2548 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2554 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2564 (void) fprintf(stderr, gettext("missing pool argument\n"));
2569 (void) fprintf(stderr, gettext("too many arguments\n"));
2575 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2576 /* As a special case, check for use of '/' in the name */
2577 if (strchr(pool, '/') != NULL)
2578 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2579 "doesn't work on datasets. To save the state "
2580 "of a dataset from a specific point in time "
2581 "please use 'zfs snapshot'\n"));
2586 err = (zpool_discard_checkpoint(zhp) != 0);
2588 err = (zpool_checkpoint(zhp) != 0);
2595 #define CHECKPOINT_OPT 1024
2598 * zpool import [-d dir] [-D]
2599 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2600 * [-d dir | -c cachefile] [-f] -a
2601 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2602 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2604 * -c Read pool information from a cachefile instead of searching
2607 * -d Scan in a specific directory, other than /dev/. More than
2608 * one directory can be specified using multiple '-d' options.
2610 * -D Scan for previously destroyed pools or import all or only
2611 * specified destroyed pools.
2613 * -R Temporarily import the pool, with all mountpoints relative to
2614 * the given root. The pool will remain exported when the machine
2617 * -V Import even in the presence of faulted vdevs. This is an
2618 * intentionally undocumented option for testing purposes, and
2619 * treats the pool configuration as complete, leaving any bad
2620 * vdevs in the FAULTED state. In other words, it does verbatim
2623 * -f Force import, even if it appears that the pool is active.
2625 * -F Attempt rewind if necessary.
2627 * -n See if rewind would work, but don't actually rewind.
2629 * -N Import the pool but don't mount datasets.
2631 * -T Specify a starting txg to use for import. This option is
2632 * intentionally undocumented option for testing purposes.
2634 * -a Import all pools found.
2636 * -l Load encryption keys while importing.
2638 * -o Set property=value and/or temporary mount options (without '=').
2640 * -s Scan using the default search path, the libblkid cache will
2643 * --rewind-to-checkpoint
2644 * Import the pool and revert back to the checkpoint.
2646 * The import command scans for pools to import, and import pools based on pool
2647 * name and GUID. The pool can also be renamed as part of the import process.
2650 zpool_do_import(int argc, char **argv)
2652 char **searchdirs = NULL;
2653 char *env, *envdup = NULL;
2657 nvlist_t *pools = NULL;
2658 boolean_t do_all = B_FALSE;
2659 boolean_t do_destroyed = B_FALSE;
2660 char *mntopts = NULL;
2663 uint64_t searchguid = 0;
2664 char *searchname = NULL;
2666 nvlist_t *found_config;
2667 nvlist_t *policy = NULL;
2668 nvlist_t *props = NULL;
2670 int flags = ZFS_IMPORT_NORMAL;
2671 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2672 boolean_t dryrun = B_FALSE;
2673 boolean_t do_rewind = B_FALSE;
2674 boolean_t xtreme_rewind = B_FALSE;
2675 boolean_t do_scan = B_FALSE;
2676 uint64_t pool_state, txg = -1ULL;
2677 char *cachefile = NULL;
2678 importargs_t idata = { 0 };
2681 struct option long_options[] = {
2682 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2687 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
2688 long_options, NULL)) != -1) {
2697 if (searchdirs == NULL) {
2698 searchdirs = safe_malloc(sizeof (char *));
2700 char **tmp = safe_malloc((nsearch + 1) *
2702 bcopy(searchdirs, tmp, nsearch *
2707 searchdirs[nsearch++] = optarg;
2710 do_destroyed = B_TRUE;
2713 flags |= ZFS_IMPORT_ANY_HOST;
2719 flags |= ZFS_IMPORT_LOAD_KEYS;
2722 flags |= ZFS_IMPORT_MISSING_LOG;
2728 flags |= ZFS_IMPORT_ONLY;
2731 if ((propval = strchr(optarg, '=')) != NULL) {
2734 if (add_prop_list(optarg, propval,
2742 if (add_prop_list(zpool_prop_to_name(
2743 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2745 if (add_prop_list_default(zpool_prop_to_name(
2746 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2753 flags |= ZFS_IMPORT_TEMP_NAME;
2754 if (add_prop_list_default(zpool_prop_to_name(
2755 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2761 txg = strtoull(optarg, &endptr, 0);
2762 if (errno != 0 || *endptr != '\0') {
2763 (void) fprintf(stderr,
2764 gettext("invalid txg value\n"));
2767 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2770 flags |= ZFS_IMPORT_VERBATIM;
2773 xtreme_rewind = B_TRUE;
2775 case CHECKPOINT_OPT:
2776 flags |= ZFS_IMPORT_CHECKPOINT;
2779 (void) fprintf(stderr, gettext("missing argument for "
2780 "'%c' option\n"), optopt);
2784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2793 if (cachefile && nsearch != 0) {
2794 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2798 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2799 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2803 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2804 (void) fprintf(stderr, gettext("-l is only meaningful during "
2809 if ((dryrun || xtreme_rewind) && !do_rewind) {
2810 (void) fprintf(stderr,
2811 gettext("-n or -X only meaningful with -F\n"));
2815 rewind_policy = ZPOOL_TRY_REWIND;
2817 rewind_policy = ZPOOL_DO_REWIND;
2819 rewind_policy |= ZPOOL_EXTREME_REWIND;
2821 /* In the future, we can capture further policy and include it here */
2822 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2823 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
2824 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
2825 rewind_policy) != 0)
2828 /* check argument count */
2831 (void) fprintf(stderr, gettext("too many arguments\n"));
2836 (void) fprintf(stderr, gettext("too many arguments\n"));
2842 * Check for the effective uid. We do this explicitly here because
2843 * otherwise any attempt to discover pools will silently fail.
2845 if (argc == 0 && geteuid() != 0) {
2846 (void) fprintf(stderr, gettext("cannot "
2847 "discover pools: permission denied\n"));
2848 if (searchdirs != NULL)
2852 nvlist_free(policy);
2857 * Depending on the arguments given, we do one of the following:
2859 * <none> Iterate through all pools and display information about
2862 * -a Iterate through all pools and try to import each one.
2864 * <id> Find the pool that corresponds to the given GUID/pool
2865 * name and import that one.
2867 * -D Above options applies only to destroyed pools.
2873 searchguid = strtoull(argv[0], &endptr, 10);
2874 if (errno != 0 || *endptr != '\0') {
2875 searchname = argv[0];
2878 found_config = NULL;
2881 * User specified a name or guid. Ensure it's unique.
2883 idata.unique = B_TRUE;
2887 * Check the environment for the preferred search path.
2889 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2892 envdup = strdup(env);
2894 dir = strtok(envdup, ":");
2895 while (dir != NULL) {
2896 if (searchdirs == NULL) {
2897 searchdirs = safe_malloc(sizeof (char *));
2899 char **tmp = safe_malloc((nsearch + 1) *
2901 bcopy(searchdirs, tmp, nsearch *
2906 searchdirs[nsearch++] = dir;
2907 dir = strtok(NULL, ":");
2911 idata.path = searchdirs;
2912 idata.paths = nsearch;
2913 idata.poolname = searchname;
2914 idata.guid = searchguid;
2915 idata.cachefile = cachefile;
2916 idata.scan = do_scan;
2917 idata.policy = policy;
2919 pools = zpool_search_import(g_zfs, &idata);
2921 if (pools != NULL && idata.exists &&
2922 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2923 (void) fprintf(stderr, gettext("cannot import '%s': "
2924 "a pool with that name already exists\n"),
2926 (void) fprintf(stderr, gettext("use the form '%s "
2927 "<pool | id> <newpool>' to give it a new name\n"),
2930 } else if (pools == NULL && idata.exists) {
2931 (void) fprintf(stderr, gettext("cannot import '%s': "
2932 "a pool with that name is already created/imported,\n"),
2934 (void) fprintf(stderr, gettext("and no additional pools "
2935 "with that name were found\n"));
2937 } else if (pools == NULL) {
2939 (void) fprintf(stderr, gettext("cannot import '%s': "
2940 "no such pool available\n"), argv[0]);
2946 if (searchdirs != NULL)
2950 nvlist_free(policy);
2957 * At this point we have a list of import candidate configs. Even if
2958 * we were searching by pool name or guid, we still need to
2959 * post-process the list to deal with pool state and possible
2965 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2967 verify(nvpair_value_nvlist(elem, &config) == 0);
2969 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2971 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2973 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2976 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2983 (void) printf("\n");
2986 err |= do_import(config, NULL, mntopts,
2989 show_import(config);
2991 } else if (searchname != NULL) {
2995 * We are searching for a pool based on name.
2997 verify(nvlist_lookup_string(config,
2998 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3000 if (strcmp(name, searchname) == 0) {
3001 if (found_config != NULL) {
3002 (void) fprintf(stderr, gettext(
3003 "cannot import '%s': more than "
3004 "one matching pool\n"), searchname);
3005 (void) fprintf(stderr, gettext(
3006 "import by numeric ID instead\n"));
3009 found_config = config;
3015 * Search for a pool by guid.
3017 verify(nvlist_lookup_uint64(config,
3018 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3020 if (guid == searchguid)
3021 found_config = config;
3026 * If we were searching for a specific pool, verify that we found a
3027 * pool, and then do the import.
3029 if (argc != 0 && err == 0) {
3030 if (found_config == NULL) {
3031 (void) fprintf(stderr, gettext("cannot import '%s': "
3032 "no such pool available\n"), argv[0]);
3035 err |= do_import(found_config, argc == 1 ? NULL :
3036 argv[1], mntopts, props, flags);
3041 * If we were just looking for pools, report an error if none were
3044 if (argc == 0 && first)
3045 (void) fprintf(stderr,
3046 gettext("no pools available to import\n"));
3051 nvlist_free(policy);
3052 if (searchdirs != NULL)
3057 return (err ? 1 : 0);
3061 * zpool sync [-f] [pool] ...
3063 * -f (undocumented) force uberblock (and config including zpool cache file)
3066 * Sync the specified pool(s).
3067 * Without arguments "zpool sync" will sync all pools.
3068 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3072 zpool_do_sync(int argc, char **argv)
3075 boolean_t force = B_FALSE;
3078 while ((ret = getopt(argc, argv, "f")) != -1) {
3084 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3093 /* if argc == 0 we will execute zpool_sync_one on all pools */
3094 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
3099 typedef struct iostat_cbdata {
3104 char **cb_vdev_names; /* Only show these vdevs */
3105 unsigned int cb_vdev_names_count;
3106 boolean_t cb_verbose;
3107 boolean_t cb_literal;
3108 boolean_t cb_scripted;
3109 zpool_list_t *cb_list;
3110 vdev_cmd_data_list_t *vcdl;
3114 typedef struct name_and_columns {
3115 const char *name; /* Column name */
3116 unsigned int columns; /* Center name to this number of columns */
3117 } name_and_columns_t;
3119 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
3121 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3123 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3125 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3126 {"asyncq_wait", 2}, {"scrub"}},
3127 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3128 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3130 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
3131 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
3132 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
3133 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
3137 /* Shorthand - if "columns" field not set, default to 1 column */
3138 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3140 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3142 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3143 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
3144 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3145 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
3146 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3147 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
3148 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3149 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
3152 static const char *histo_to_title[] = {
3153 [IOS_L_HISTO] = "latency",
3154 [IOS_RQ_HISTO] = "req_size",
3158 * Return the number of labels in a null-terminated name_and_columns_t
3163 label_array_len(const name_and_columns_t *labels)
3167 while (labels[i].name)
3174 * Return the number of strings in a null-terminated string array.
3177 * const char foo[] = {"bar", "baz", NULL}
3182 str_array_len(const char *array[])
3193 * Return a default column width for default/latency/queue columns. This does
3194 * not include histograms, which have their columns autosized.
3197 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
3199 unsigned long column_width = 5; /* Normal niceprint */
3200 static unsigned long widths[] = {
3202 * Choose some sane default column sizes for printing the
3205 [IOS_DEFAULT] = 15, /* 1PB capacity */
3206 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
3207 [IOS_QUEUES] = 6, /* 1M queue entries */
3211 column_width = widths[type];
3213 return (column_width);
3217 * Print the column labels, i.e:
3219 * capacity operations bandwidth
3220 * alloc free read write read write ...
3222 * If force_column_width is set, use it for the column width. If not set, use
3223 * the default column width.
3226 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
3227 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
3230 unsigned int text_start, rw_column_width, spaces_to_end;
3231 uint64_t flags = cb->cb_flags;
3233 unsigned int column_width = force_column_width;
3235 /* For each bit set in flags */
3236 for (f = flags; f; f &= ~(1ULL << idx)) {
3237 idx = lowbit64(f) - 1;
3238 if (!force_column_width)
3239 column_width = default_column_width(cb, idx);
3240 /* Print our top labels centered over "read write" label. */
3241 for (i = 0; i < label_array_len(labels[idx]); i++) {
3242 const char *name = labels[idx][i].name;
3244 * We treat labels[][].columns == 0 as shorthand
3245 * for one column. It makes writing out the label
3246 * tables more concise.
3248 unsigned int columns = MAX(1, labels[idx][i].columns);
3249 unsigned int slen = strlen(name);
3251 rw_column_width = (column_width * columns) +
3252 (2 * (columns - 1));
3254 text_start = (int)((rw_column_width)/columns -
3257 printf(" "); /* Two spaces between columns */
3259 /* Space from beginning of column to label */
3260 for (s = 0; s < text_start; s++)
3265 /* Print space after label to end of column */
3266 spaces_to_end = rw_column_width - text_start - slen;
3267 for (s = 0; s < spaces_to_end; s++)
3276 * print_cmd_columns - Print custom column titles from -c
3278 * If the user specified the "zpool status|iostat -c" then print their custom
3279 * column titles in the header. For example, print_cmd_columns() would print
3280 * the " col1 col2" part of this:
3282 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3284 * capacity operations bandwidth
3285 * pool alloc free read write read write col1 col2
3286 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3287 * mypool 269K 1008M 0 0 107 946
3288 * mirror 269K 1008M 0 0 107 946
3289 * sdb - - 0 0 102 473 val1 val2
3290 * sdc - - 0 0 5 473 val1 val2
3291 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3294 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3297 vdev_cmd_data_t *data = &vcdl->data[0];
3299 if (vcdl->count == 0 || data == NULL)
3303 * Each vdev cmd should have the same column names unless the user did
3304 * something weird with their cmd. Just take the column names from the
3305 * first vdev and assume it works for all of them.
3307 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3310 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3313 printf("%*s", vcdl->uniq_cols_width[i],
3314 vcdl->uniq_cols[i]);
3321 * Utility function to print out a line of dashes like:
3323 * -------------------------------- ----- ----- ----- ----- -----
3325 * ...or a dashed named-row line like:
3331 * @force_column_width If non-zero, use the value as the column width.
3332 * Otherwise use the default column widths.
3334 * @name: Print a dashed named-row line starting
3335 * with @name. Otherwise, print a regular
3339 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3343 unsigned int namewidth;
3344 uint64_t flags = cb->cb_flags;
3347 const name_and_columns_t *labels;
3351 if (cb->cb_flags & IOS_ANYHISTO_M) {
3352 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3353 } else if (cb->cb_vdev_names_count) {
3359 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3360 name ? strlen(name) : 0);
3364 printf("%-*s", namewidth, name);
3366 for (i = 0; i < namewidth; i++)
3370 /* For each bit in flags */
3371 for (f = flags; f; f &= ~(1ULL << idx)) {
3372 unsigned int column_width;
3373 idx = lowbit64(f) - 1;
3374 if (force_column_width)
3375 column_width = force_column_width;
3377 column_width = default_column_width(cb, idx);
3379 labels = iostat_bottom_labels[idx];
3380 for (i = 0; i < label_array_len(labels); i++) {
3382 printf(" %*s-", column_width - 1, " ");
3384 printf(" %.*s", column_width,
3385 "--------------------");
3392 print_iostat_separator_impl(iostat_cbdata_t *cb,
3393 unsigned int force_column_width)
3395 print_iostat_dashes(cb, force_column_width, NULL);
3399 print_iostat_separator(iostat_cbdata_t *cb)
3401 print_iostat_separator_impl(cb, 0);
3405 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3406 const char *histo_vdev_name)
3408 unsigned int namewidth;
3411 if (cb->cb_flags & IOS_ANYHISTO_M) {
3412 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3413 } else if (cb->cb_vdev_names_count) {
3419 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3420 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3422 if (histo_vdev_name)
3423 printf("%-*s", namewidth, histo_vdev_name);
3425 printf("%*s", namewidth, "");
3428 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3431 printf("%-*s", namewidth, title);
3433 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3434 if (cb->vcdl != NULL)
3435 print_cmd_columns(cb->vcdl, 0);
3439 print_iostat_separator_impl(cb, force_column_width);
3441 if (cb->vcdl != NULL)
3442 print_cmd_columns(cb->vcdl, 1);
3448 print_iostat_header(iostat_cbdata_t *cb)
3450 print_iostat_header_impl(cb, 0, NULL);
3455 * Display a single statistic.
3458 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3459 unsigned int column_size, boolean_t scripted)
3463 zfs_nicenum_format(value, buf, sizeof (buf), format);
3466 printf("\t%s", buf);
3468 printf(" %*s", column_size, buf);
3472 * Calculate the default vdev stats
3474 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3475 * stats into calcvs.
3478 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3479 vdev_stat_t *calcvs)
3483 memcpy(calcvs, newvs, sizeof (*calcvs));
3484 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3485 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3487 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3488 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3492 * Internal representation of the extended iostats data.
3494 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3495 * or single uint64_t's. We make both look like arrays to make them easier
3496 * to process. In order to make single uint64_t's look like arrays, we set
3497 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3498 * we can just use *data and count.
3502 uint_t count; /* Number of entries in data[] */
3503 uint64_t __data; /* Only used when data is a single uint64_t */
3507 stat_histo_max(struct stat_array *nva, unsigned int len)
3511 for (i = 0; i < len; i++)
3512 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3518 * Helper function to lookup a uint64_t array or uint64_t value and store its
3519 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3520 * it look like a one element array to make it easier to process.
3523 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3524 struct stat_array *nva)
3529 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3530 switch (nvpair_type(tmp)) {
3531 case DATA_TYPE_UINT64_ARRAY:
3532 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3534 case DATA_TYPE_UINT64:
3535 ret = nvpair_value_uint64(tmp, &nva->__data);
3536 nva->data = &nva->__data;
3540 /* Not a uint64_t */
3549 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3550 * subtract them, and return the results in a newly allocated stat_array.
3551 * You must free the returned array after you are done with it with
3552 * free_calc_stats().
3554 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3557 static struct stat_array *
3558 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3561 nvlist_t *oldnvx = NULL, *newnvx;
3562 struct stat_array *oldnva, *newnva, *calcnva;
3564 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3566 /* Extract our extended stats nvlist from the main list */
3567 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3570 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3574 newnva = safe_malloc(alloc_size);
3575 oldnva = safe_malloc(alloc_size);
3576 calcnva = safe_malloc(alloc_size);
3578 for (j = 0; j < len; j++) {
3579 verify(nvpair64_to_stat_array(newnvx, names[j],
3581 calcnva[j].count = newnva[j].count;
3582 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3583 calcnva[j].data = safe_malloc(alloc_size);
3584 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3587 verify(nvpair64_to_stat_array(oldnvx, names[j],
3589 for (i = 0; i < oldnva[j].count; i++)
3590 calcnva[j].data[i] -= oldnva[j].data[i];
3599 free_calc_stats(struct stat_array *nva, unsigned int len)
3602 for (i = 0; i < len; i++)
3609 print_iostat_histo(struct stat_array *nva, unsigned int len,
3610 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3616 enum zfs_nicenum_format format;
3617 unsigned int buckets;
3618 unsigned int start_bucket;
3621 format = ZFS_NICENUM_RAW;
3623 format = ZFS_NICENUM_1024;
3625 /* All these histos are the same size, so just use nva[0].count */
3626 buckets = nva[0].count;
3628 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3629 /* Start at 512 - req size should never be lower than this */
3635 for (j = start_bucket; j < buckets; j++) {
3636 /* Print histogram bucket label */
3637 if (cb->cb_flags & IOS_L_HISTO_M) {
3638 /* Ending range of this bucket */
3639 val = (1UL << (j + 1)) - 1;
3640 zfs_nicetime(val, buf, sizeof (buf));
3642 /* Request size (starting range of bucket) */
3644 zfs_nicenum(val, buf, sizeof (buf));
3647 if (cb->cb_scripted)
3648 printf("%llu", (u_longlong_t)val);
3650 printf("%-*s", namewidth, buf);
3652 /* Print the values on the line */
3653 for (i = 0; i < len; i++) {
3654 print_one_stat(nva[i].data[j] * scale, format,
3655 column_width, cb->cb_scripted);
3662 print_solid_separator(unsigned int length)
3670 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3671 nvlist_t *newnv, double scale, const char *name)
3673 unsigned int column_width;
3674 unsigned int namewidth;
3675 unsigned int entire_width;
3676 enum iostat_type type;
3677 struct stat_array *nva;
3679 unsigned int names_len;
3681 /* What type of histo are we? */
3682 type = IOS_HISTO_IDX(cb->cb_flags);
3684 /* Get NULL-terminated array of nvlist names for our histo */
3685 names = vsx_type_to_nvlist[type];
3686 names_len = str_array_len(names); /* num of names */
3688 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3690 if (cb->cb_literal) {
3691 column_width = MAX(5,
3692 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3697 namewidth = MAX(cb->cb_namewidth,
3698 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3701 * Calculate the entire line width of what we're printing. The
3702 * +2 is for the two spaces between columns:
3706 /* |___| <---------- column_width */
3708 /* |__________| <--- entire_width */
3710 entire_width = namewidth + (column_width + 2) *
3711 label_array_len(iostat_bottom_labels[type]);
3713 if (cb->cb_scripted)
3714 printf("%s\n", name);
3716 print_iostat_header_impl(cb, column_width, name);
3718 print_iostat_histo(nva, names_len, cb, column_width,
3721 free_calc_stats(nva, names_len);
3722 if (!cb->cb_scripted)
3723 print_solid_separator(entire_width);
3727 * Calculate the average latency of a power-of-two latency histogram
3730 single_histo_average(uint64_t *histo, unsigned int buckets)
3733 uint64_t count = 0, total = 0;
3735 for (i = 0; i < buckets; i++) {
3737 * Our buckets are power-of-two latency ranges. Use the
3738 * midpoint latency of each bucket to calculate the average.
3746 if (histo[i] != 0) {
3747 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3752 /* Prevent divide by zero */
3753 return (count == 0 ? 0 : total / count);
3757 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3758 nvlist_t *newnv, double scale)
3762 const char *names[] = {
3763 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3764 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3765 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3766 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3767 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3768 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3769 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3770 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3771 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3772 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3775 struct stat_array *nva;
3777 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3778 enum zfs_nicenum_format format;
3780 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3783 format = ZFS_NICENUM_RAW;
3785 format = ZFS_NICENUM_1024;
3787 for (i = 0; i < ARRAY_SIZE(names); i++) {
3788 val = nva[i].data[0] * scale;
3789 print_one_stat(val, format, column_width, cb->cb_scripted);
3792 free_calc_stats(nva, ARRAY_SIZE(names));
3796 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3797 nvlist_t *newnv, double scale)
3801 const char *names[] = {
3802 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3803 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3804 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3805 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3806 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3807 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3808 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3809 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3810 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3812 struct stat_array *nva;
3814 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3815 enum zfs_nicenum_format format;
3817 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3820 format = ZFS_NICENUM_RAWTIME;
3822 format = ZFS_NICENUM_TIME;
3824 /* Print our avg latencies on the line */
3825 for (i = 0; i < ARRAY_SIZE(names); i++) {
3826 /* Compute average latency for a latency histo */
3827 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3828 print_one_stat(val, format, column_width, cb->cb_scripted);
3830 free_calc_stats(nva, ARRAY_SIZE(names));
3834 * Print default statistics (capacity/operations/bandwidth)
3837 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3839 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3840 enum zfs_nicenum_format format;
3841 char na; /* char to print for "not applicable" values */
3843 if (cb->cb_literal) {
3844 format = ZFS_NICENUM_RAW;
3847 format = ZFS_NICENUM_1024;
3851 /* only toplevel vdevs have capacity stats */
3852 if (vs->vs_space == 0) {
3853 if (cb->cb_scripted)
3854 printf("\t%c\t%c", na, na);
3856 printf(" %*c %*c", column_width, na, column_width,
3859 print_one_stat(vs->vs_alloc, format, column_width,
3861 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3862 column_width, cb->cb_scripted);
3865 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3866 format, column_width, cb->cb_scripted);
3867 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3868 format, column_width, cb->cb_scripted);
3869 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3870 format, column_width, cb->cb_scripted);
3871 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3872 format, column_width, cb->cb_scripted);
3875 static const char *class_name[] = {
3876 VDEV_ALLOC_BIAS_DEDUP,
3877 VDEV_ALLOC_BIAS_SPECIAL,
3878 VDEV_ALLOC_CLASS_LOGS
3882 * Print out all the statistics for the given vdev. This can either be the
3883 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3884 * is a verbose output, and we don't want to display the toplevel pool stats.
3886 * Returns the number of stat lines printed.
3889 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3890 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3892 nvlist_t **oldchild, **newchild;
3893 uint_t c, children, oldchildren;
3894 vdev_stat_t *oldvs, *newvs, *calcvs;
3895 vdev_stat_t zerovs = { 0 };
3902 calcvs = safe_malloc(sizeof (*calcvs));
3904 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3907 if (oldnv != NULL) {
3908 verify(nvlist_lookup_uint64_array(oldnv,
3909 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3914 /* Do we only want to see a specific vdev? */
3915 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3916 /* Yes we do. Is this the vdev? */
3917 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3919 * This is our vdev. Since it is the only vdev we
3920 * will be displaying, make depth = 0 so that it
3921 * doesn't get indented.
3928 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3929 /* Couldn't match the name */
3934 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3935 (uint64_t **)&newvs, &c) == 0);
3938 * Print the vdev name unless it's is a histogram. Histograms
3939 * display the vdev name in the header itself.
3941 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3942 if (cb->cb_scripted) {
3945 if (strlen(name) + depth > cb->cb_namewidth)
3946 (void) printf("%*s%s", depth, "", name);
3948 (void) printf("%*s%s%*s", depth, "", name,
3949 (int)(cb->cb_namewidth - strlen(name) -
3954 /* Calculate our scaling factor */
3955 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3956 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3958 * If we specify printing histograms with no time interval, then
3959 * print the histogram numbers over the entire lifetime of the
3967 scale = (double)NANOSEC / tdelta;
3970 if (cb->cb_flags & IOS_DEFAULT_M) {
3971 calc_default_iostats(oldvs, newvs, calcvs);
3972 print_iostat_default(calcvs, cb, scale);
3974 if (cb->cb_flags & IOS_LATENCY_M)
3975 print_iostat_latency(cb, oldnv, newnv, scale);
3976 if (cb->cb_flags & IOS_QUEUES_M)
3977 print_iostat_queues(cb, oldnv, newnv, scale);
3978 if (cb->cb_flags & IOS_ANYHISTO_M) {
3980 print_iostat_histos(cb, oldnv, newnv, scale, name);
3983 if (cb->vcdl != NULL) {
3985 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3988 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3992 if (!(cb->cb_flags & IOS_ANYHISTO_M))
4001 if (!cb->cb_verbose)
4004 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4005 &newchild, &children) != 0)
4009 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4010 &oldchild, &oldchildren) != 0)
4013 children = MIN(oldchildren, children);
4017 * print normal top-level devices
4019 for (c = 0; c < children; c++) {
4020 uint64_t ishole = B_FALSE, islog = B_FALSE;
4022 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4025 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4028 if (ishole || islog)
4031 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4034 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4036 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
4037 newchild[c], cb, depth + 2);
4042 * print all other top-level devices
4044 for (uint_t n = 0; n < 3; n++) {
4045 boolean_t printed = B_FALSE;
4047 for (c = 0; c < children; c++) {
4048 uint64_t islog = B_FALSE;
4052 (void) nvlist_lookup_uint64(newchild[c],
4053 ZPOOL_CONFIG_IS_LOG, &islog);
4055 bias = VDEV_ALLOC_CLASS_LOGS;
4057 (void) nvlist_lookup_string(newchild[c],
4058 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4059 (void) nvlist_lookup_string(newchild[c],
4060 ZPOOL_CONFIG_TYPE, &type);
4062 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4064 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4068 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
4069 !cb->cb_scripted && !cb->cb_vdev_names) {
4070 print_iostat_dashes(cb, 0,
4077 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4079 ret += print_vdev_stats(zhp, vname, oldnv ?
4080 oldchild[c] : NULL, newchild[c], cb, depth + 2);
4086 * Include level 2 ARC devices in iostat output
4088 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4089 &newchild, &children) != 0)
4093 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4094 &oldchild, &oldchildren) != 0)
4097 children = MIN(oldchildren, children);
4101 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
4102 !cb->cb_vdev_names) {
4103 print_iostat_dashes(cb, 0, "cache");
4107 for (c = 0; c < children; c++) {
4108 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4110 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4111 : NULL, newchild[c], cb, depth + 2);
4120 refresh_iostat(zpool_handle_t *zhp, void *data)
4122 iostat_cbdata_t *cb = data;
4126 * If the pool has disappeared, remove it from the list and continue.
4128 if (zpool_refresh_stats(zhp, &missing) != 0)
4132 pool_list_remove(cb->cb_list, zhp);
4138 * Callback to print out the iostats for the given pool.
4141 print_iostat(zpool_handle_t *zhp, void *data)
4143 iostat_cbdata_t *cb = data;
4144 nvlist_t *oldconfig, *newconfig;
4145 nvlist_t *oldnvroot, *newnvroot;
4148 newconfig = zpool_get_config(zhp, &oldconfig);
4150 if (cb->cb_iteration == 1)
4153 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
4156 if (oldconfig == NULL)
4159 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
4162 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
4164 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
4165 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
4166 print_iostat_separator(cb);
4167 if (cb->vcdl != NULL) {
4168 print_cmd_columns(cb->vcdl, 1);
4183 if (isatty(STDOUT_FILENO)) {
4184 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
4186 columns = ws.ws_col;
4195 get_namewidth(zpool_handle_t *zhp, void *data)
4197 iostat_cbdata_t *cb = data;
4198 nvlist_t *config, *nvroot;
4201 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
4202 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4204 unsigned int poolname_len = strlen(zpool_get_name(zhp));
4205 if (!cb->cb_verbose)
4206 cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
4208 cb->cb_namewidth = MAX(poolname_len,
4209 max_width(zhp, nvroot, 0, cb->cb_namewidth,
4210 cb->cb_name_flags));
4213 * The width must be at least 10, but may be as large as the
4214 * column width - 42 so that we can still fit in one line.
4216 columns = get_columns();
4218 if (cb->cb_namewidth < 10)
4219 cb->cb_namewidth = 10;
4220 if (cb->cb_namewidth > columns - 42)
4221 cb->cb_namewidth = columns - 42;
4227 * Parse the input string, get the 'interval' and 'count' value if there is one.
4230 get_interval_count(int *argcp, char **argv, float *iv,
4234 unsigned long count = 0;
4238 * Determine if the last argument is an integer or a pool name
4240 if (argc > 0 && isnumber(argv[argc - 1])) {
4244 interval = strtof(argv[argc - 1], &end);
4246 if (*end == '\0' && errno == 0) {
4247 if (interval == 0) {
4248 (void) fprintf(stderr, gettext("interval "
4249 "cannot be zero\n"));
4253 * Ignore the last parameter
4258 * If this is not a valid number, just plow on. The
4259 * user will get a more informative error message later
4267 * If the last argument is also an integer, then we have both a count
4270 if (argc > 0 && isnumber(argv[argc - 1])) {
4275 interval = strtof(argv[argc - 1], &end);
4277 if (*end == '\0' && errno == 0) {
4278 if (interval == 0) {
4279 (void) fprintf(stderr, gettext("interval "
4280 "cannot be zero\n"));
4285 * Ignore the last parameter
4299 get_timestamp_arg(char c)
4302 timestamp_fmt = UDATE;
4304 timestamp_fmt = DDATE;
4310 * Return stat flags that are supported by all pools by both the module and
4311 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4312 * It will get ANDed down until only the flags that are supported on all pools
4316 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4318 uint64_t *mask = data;
4319 nvlist_t *config, *nvroot, *nvx;
4323 config = zpool_get_config(zhp, NULL);
4324 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4327 /* Default stats are always supported, but for completeness.. */
4328 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4329 flags |= IOS_DEFAULT_M;
4331 /* Get our extended stats nvlist from the main list */
4332 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4335 * No extended stats; they're probably running an older
4336 * module. No big deal, we support that too.
4341 /* For each extended stat, make sure all its nvpairs are supported */
4342 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4343 if (!vsx_type_to_nvlist[j][0])
4346 /* Start off by assuming the flag is supported, then check */
4347 flags |= (1ULL << j);
4348 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4349 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4350 /* flag isn't supported */
4351 flags = flags & ~(1ULL << j);
4357 *mask = *mask & flags;
4362 * Return a bitmask of stats that are supported on all pools by both the module
4366 get_stat_flags(zpool_list_t *list)
4371 * get_stat_flags_cb() will lop off bits from "mask" until only the
4372 * flags that are supported on all pools remain.
4374 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4379 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4382 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4384 iostat_cbdata_t *cb = cb_data;
4388 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4390 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4391 ret = 1; /* match */
4398 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4401 is_vdev(zpool_handle_t *zhp, void *cb_data)
4403 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4407 * Check if vdevs are in a pool
4409 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4410 * return 0. If pool_name is NULL, then search all pools.
4413 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4414 iostat_cbdata_t *cb)
4421 if ((argc == 0) || !*argv)
4427 /* Temporarily hijack cb_vdev_names for a second... */
4428 tmp_name = cb->cb_vdev_names;
4430 /* Go though our list of prospective vdev names */
4431 for (i = 0; i < argc; i++) {
4432 cb->cb_vdev_names = argv + i;
4434 /* Is this name a vdev in our pools? */
4435 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4443 cb->cb_vdev_names = tmp_name;
4449 is_pool_cb(zpool_handle_t *zhp, void *data)
4452 if (strcmp(name, zpool_get_name(zhp)) == 0)
4459 * Do we have a pool named *name? If so, return 1, otherwise 0.
4464 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4467 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4469 are_all_pools(int argc, char **argv)
4471 if ((argc == 0) || !*argv)
4475 if (!is_pool(argv[argc]))
4482 * Helper function to print out vdev/pool names we can't resolve. Used for an
4486 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4487 iostat_cbdata_t *cb)
4492 for (i = 0; i < argc; i++) {
4496 str = gettext("pool");
4497 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4498 str = gettext("vdev in this pool");
4499 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4500 str = gettext("vdev in another pool");
4502 str = gettext("unknown");
4504 fprintf(stderr, "\t%s (%s)\n", name, str);
4509 * Same as get_interval_count(), but with additional checks to not misinterpret
4510 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4514 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4515 unsigned long *count, iostat_cbdata_t *cb)
4517 char **tmpargv = argv;
4518 int argc_for_interval = 0;
4520 /* Is the last arg an interval value? Or a guid? */
4521 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4523 * The last arg is not a guid, so it's probably an
4526 argc_for_interval++;
4529 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4531 * The 2nd to last arg is not a guid, so it's probably
4532 * an interval value.
4534 argc_for_interval++;
4538 /* Point to our list of possible intervals */
4539 tmpargv = &argv[*argc - argc_for_interval];
4541 *argc = *argc - argc_for_interval;
4542 get_interval_count(&argc_for_interval, tmpargv,
4547 * Floating point sleep(). Allows you to pass in a floating point value for
4553 struct timespec req;
4554 req.tv_sec = floor(sec);
4555 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4556 nanosleep(&req, NULL);
4560 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4563 * name: Short name of the script ('iostat').
4564 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4567 print_zpool_script_help(char *name, char *path)
4569 char *argv[] = {path, "-h", NULL};
4570 char **lines = NULL;
4574 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4576 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4578 libzfs_free_str_array(lines, lines_cnt);
4582 for (int i = 0; i < lines_cnt; i++)
4583 if (!is_blank_str(lines[i]))
4584 printf(" %-14s %s\n", name, lines[i]);
4586 libzfs_free_str_array(lines, lines_cnt);
4590 * Go though the zpool status/iostat -c scripts in the user's path, run their
4591 * help option (-h), and print out the results.
4594 print_zpool_dir_scripts(char *dirpath)
4598 char fullpath[MAXPATHLEN];
4599 struct stat dir_stat;
4601 if ((dir = opendir(dirpath)) != NULL) {
4602 /* print all the files and directories within directory */
4603 while ((ent = readdir(dir)) != NULL) {
4604 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4606 /* Print the scripts */
4607 if (stat(fullpath, &dir_stat) == 0)
4608 if (dir_stat.st_mode & S_IXUSR &&
4609 S_ISREG(dir_stat.st_mode))
4610 print_zpool_script_help(ent->d_name,
4618 * Print out help text for all zpool status/iostat -c scripts.
4621 print_zpool_script_list(char *subcommand)
4625 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4627 sp = zpool_get_cmd_search_path();
4631 dir = strtok(sp, ":");
4632 while (dir != NULL) {
4633 print_zpool_dir_scripts(dir);
4634 dir = strtok(NULL, ":");
4641 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4642 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4643 * [interval [count]]
4645 * -c CMD For each vdev, run command CMD
4646 * -g Display guid for individual vdev name.
4647 * -L Follow links when resolving vdev path name.
4648 * -P Display full path for vdev name.
4649 * -v Display statistics for individual vdevs
4651 * -p Display values in parsable (exact) format.
4652 * -H Scripted mode. Don't display headers, and separate properties
4654 * -l Display average latency
4655 * -q Display queue depths
4656 * -w Display latency histograms
4657 * -r Display request size histogram
4658 * -T Display a timestamp in date(1) or Unix format
4660 * This command can be tricky because we want to be able to deal with pool
4661 * creation/destruction as well as vdev configuration changes. The bulk of this
4662 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4663 * on pool_list_update() to detect the addition of new pools. Configuration
4664 * changes are all handled within libzfs.
4667 zpool_do_iostat(int argc, char **argv)
4673 unsigned long count = 0;
4675 boolean_t verbose = B_FALSE;
4676 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4677 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4678 boolean_t omit_since_boot = B_FALSE;
4679 boolean_t guid = B_FALSE;
4680 boolean_t follow_links = B_FALSE;
4681 boolean_t full_name = B_FALSE;
4682 iostat_cbdata_t cb = { 0 };
4685 /* Used for printing error message */
4686 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4687 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4689 uint64_t unsupported_flags;
4692 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4697 gettext("Can't set -c flag twice\n"));
4701 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4702 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4703 fprintf(stderr, gettext(
4704 "Can't run -c, disabled by "
4705 "ZPOOL_SCRIPTS_ENABLED.\n"));
4709 if ((getuid() <= 0 || geteuid() <= 0) &&
4710 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4711 fprintf(stderr, gettext(
4712 "Can't run -c with root privileges "
4713 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4723 follow_links = B_TRUE;
4729 get_timestamp_arg(*optarg);
4753 omit_since_boot = B_TRUE;
4759 if (optopt == 'c') {
4760 print_zpool_script_list("iostat");
4764 gettext("invalid option '%c'\n"), optopt);
4773 cb.cb_literal = parsable;
4774 cb.cb_scripted = scripted;
4777 cb.cb_name_flags |= VDEV_NAME_GUID;
4779 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4781 cb.cb_name_flags |= VDEV_NAME_PATH;
4782 cb.cb_iteration = 0;
4783 cb.cb_namewidth = 0;
4784 cb.cb_verbose = verbose;
4786 /* Get our interval and count values (if any) */
4788 get_interval_count_filter_guids(&argc, argv, &interval,
4791 get_interval_count(&argc, argv, &interval, &count);
4795 /* No args, so just print the defaults. */
4796 } else if (are_all_pools(argc, argv)) {
4797 /* All the args are pool names */
4798 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4799 /* All the args are vdevs */
4800 cb.cb_vdev_names = argv;
4801 cb.cb_vdev_names_count = argc;
4802 argc = 0; /* No pools to process */
4803 } else if (are_all_pools(1, argv)) {
4804 /* The first arg is a pool name */
4805 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4806 /* ...and the rest are vdev names */
4807 cb.cb_vdev_names = argv + 1;
4808 cb.cb_vdev_names_count = argc - 1;
4809 argc = 1; /* One pool to process */
4811 fprintf(stderr, gettext("Expected either a list of "));
4812 fprintf(stderr, gettext("pools, or list of vdevs in"));
4813 fprintf(stderr, " \"%s\", ", argv[0]);
4814 fprintf(stderr, gettext("but got:\n"));
4815 error_list_unresolved_vdevs(argc - 1, argv + 1,
4817 fprintf(stderr, "\n");
4823 * The args don't make sense. The first arg isn't a pool name,
4824 * nor are all the args vdevs.
4826 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4827 fprintf(stderr, "\n");
4831 if (cb.cb_vdev_names_count != 0) {
4833 * If user specified vdevs, it implies verbose.
4835 cb.cb_verbose = B_TRUE;
4839 * Construct the list of all interesting pools.
4842 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4845 if (pool_list_count(list) == 0 && argc != 0) {
4846 pool_list_free(list);
4850 if (pool_list_count(list) == 0 && interval == 0) {
4851 pool_list_free(list);
4852 (void) fprintf(stderr, gettext("no pools available\n"));
4856 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4857 pool_list_free(list);
4858 (void) fprintf(stderr,
4859 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4864 if (l_histo && rq_histo) {
4865 pool_list_free(list);
4866 (void) fprintf(stderr,
4867 gettext("Only one of [-r|-w] can be passed at a time\n"));
4873 * Enter the main iostat loop.
4879 * Histograms tables look out of place when you try to display
4880 * them with the other stats, so make a rule that you can only
4881 * print histograms by themselves.
4883 cb.cb_flags = IOS_L_HISTO_M;
4884 } else if (rq_histo) {
4885 cb.cb_flags = IOS_RQ_HISTO_M;
4887 cb.cb_flags = IOS_DEFAULT_M;
4889 cb.cb_flags |= IOS_LATENCY_M;
4891 cb.cb_flags |= IOS_QUEUES_M;
4895 * See if the module supports all the stats we want to display.
4897 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4898 if (unsupported_flags) {
4902 gettext("The loaded zfs module doesn't support:"));
4904 /* for each bit set in unsupported_flags */
4905 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4906 idx = lowbit64(f) - 1;
4907 fprintf(stderr, " -%c", flag_to_arg[idx]);
4910 fprintf(stderr, ". Try running a newer module.\n");
4911 pool_list_free(list);
4917 if ((npools = pool_list_count(list)) == 0)
4918 (void) fprintf(stderr, gettext("no pools available\n"));
4921 * If this is the first iteration and -y was supplied
4922 * we skip any printing.
4924 boolean_t skip = (omit_since_boot &&
4925 cb.cb_iteration == 0);
4928 * Refresh all statistics. This is done as an
4929 * explicit step before calculating the maximum name
4930 * width, so that any * configuration changes are
4931 * properly accounted for.
4933 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4937 * Iterate over all pools to determine the maximum width
4938 * for the pool / device name column across all pools.
4940 cb.cb_namewidth = 0;
4941 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4944 if (timestamp_fmt != NODATE)
4945 print_timestamp(timestamp_fmt);
4947 if (cmd != NULL && cb.cb_verbose &&
4948 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4949 cb.vcdl = all_pools_for_each_vdev_run(argc,
4950 argv, cmd, g_zfs, cb.cb_vdev_names,
4951 cb.cb_vdev_names_count, cb.cb_name_flags);
4957 * If it's the first time and we're not skipping it,
4958 * or either skip or verbose mode, print the header.
4960 * The histogram code explicitly prints its header on
4961 * every vdev, so skip this for histograms.
4963 if (((++cb.cb_iteration == 1 && !skip) ||
4964 (skip != verbose)) &&
4965 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4967 print_iostat_header(&cb);
4970 (void) fsleep(interval);
4975 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4978 * If there's more than one pool, and we're not in
4979 * verbose mode (which prints a separator for us),
4980 * then print a separator.
4982 * In addition, if we're printing specific vdevs then
4983 * we also want an ending separator.
4985 if (((npools > 1 && !verbose &&
4986 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4987 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4988 cb.cb_vdev_names_count)) &&
4990 print_iostat_separator(&cb);
4991 if (cb.vcdl != NULL)
4992 print_cmd_columns(cb.vcdl, 1);
4996 if (cb.vcdl != NULL)
4997 free_vdev_cmd_data_list(cb.vcdl);
5002 * Flush the output so that redirection to a file isn't buffered
5005 (void) fflush(stdout);
5010 if (count != 0 && --count == 0)
5013 (void) fsleep(interval);
5016 pool_list_free(list);
5021 typedef struct list_cbdata {
5022 boolean_t cb_verbose;
5025 boolean_t cb_scripted;
5026 zprop_list_t *cb_proplist;
5027 boolean_t cb_literal;
5032 * Given a list of columns to display, output appropriate headers for each one.
5035 print_header(list_cbdata_t *cb)
5037 zprop_list_t *pl = cb->cb_proplist;
5038 char headerbuf[ZPOOL_MAXPROPLEN];
5040 boolean_t first = B_TRUE;
5041 boolean_t right_justify;
5044 for (; pl != NULL; pl = pl->pl_next) {
5045 width = pl->pl_width;
5046 if (first && cb->cb_verbose) {
5048 * Reset the width to accommodate the verbose listing
5051 width = cb->cb_namewidth;
5059 right_justify = B_FALSE;
5060 if (pl->pl_prop != ZPROP_INVAL) {
5061 header = zpool_prop_column_name(pl->pl_prop);
5062 right_justify = zpool_prop_align_right(pl->pl_prop);
5066 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5067 headerbuf[i] = toupper(pl->pl_user_prop[i]);
5068 headerbuf[i] = '\0';
5072 if (pl->pl_next == NULL && !right_justify)
5073 (void) printf("%s", header);
5074 else if (right_justify)
5075 (void) printf("%*s", (int)width, header);
5077 (void) printf("%-*s", (int)width, header);
5080 (void) printf("\n");
5084 * Given a pool and a list of properties, print out all the properties according
5085 * to the described layout. Used by zpool_do_list().
5088 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
5090 zprop_list_t *pl = cb->cb_proplist;
5091 boolean_t first = B_TRUE;
5092 char property[ZPOOL_MAXPROPLEN];
5094 boolean_t right_justify;
5097 for (; pl != NULL; pl = pl->pl_next) {
5099 width = pl->pl_width;
5100 if (first && cb->cb_verbose) {
5102 * Reset the width to accommodate the verbose listing
5105 width = cb->cb_namewidth;
5109 if (cb->cb_scripted)
5110 (void) printf("\t");
5117 right_justify = B_FALSE;
5118 if (pl->pl_prop != ZPROP_INVAL) {
5119 if (zpool_get_prop(zhp, pl->pl_prop, property,
5120 sizeof (property), NULL, cb->cb_literal) != 0)
5125 right_justify = zpool_prop_align_right(pl->pl_prop);
5126 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
5127 zpool_prop_unsupported(pl->pl_user_prop)) &&
5128 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
5129 sizeof (property)) == 0) {
5137 * If this is being called in scripted mode, or if this is the
5138 * last column and it is left-justified, don't include a width
5141 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
5142 (void) printf("%s", propstr);
5143 else if (right_justify)
5144 (void) printf("%*s", (int)width, propstr);
5146 (void) printf("%-*s", (int)width, propstr);
5149 (void) printf("\n");
5153 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
5154 boolean_t valid, enum zfs_nicenum_format format)
5158 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
5161 case ZPOOL_PROP_EXPANDSZ:
5162 case ZPOOL_PROP_CHECKPOINT:
5164 (void) strlcpy(propval, "-", sizeof (propval));
5166 zfs_nicenum_format(value, propval, sizeof (propval),
5169 case ZPOOL_PROP_FRAGMENTATION:
5170 if (value == ZFS_FRAG_INVALID) {
5171 (void) strlcpy(propval, "-", sizeof (propval));
5172 } else if (format == ZFS_NICENUM_RAW) {
5173 (void) snprintf(propval, sizeof (propval), "%llu",
5174 (unsigned long long)value);
5176 (void) snprintf(propval, sizeof (propval), "%llu%%",
5177 (unsigned long long)value);
5180 case ZPOOL_PROP_CAPACITY:
5181 /* capacity value is in parts-per-10,000 (aka permyriad) */
5182 if (format == ZFS_NICENUM_RAW)
5183 (void) snprintf(propval, sizeof (propval), "%llu",
5184 (unsigned long long)value / 100);
5186 (void) snprintf(propval, sizeof (propval),
5187 value < 1000 ? "%1.2f%%" : value < 10000 ?
5188 "%2.1f%%" : "%3.0f%%", value / 100.0);
5191 zfs_nicenum_format(value, propval, sizeof (propval), format);
5195 (void) strlcpy(propval, "-", sizeof (propval));
5198 (void) printf("\t%s", propval);
5200 (void) printf(" %*s", (int)width, propval);
5204 * print static default line per vdev
5205 * not compatible with '-o' <proplist> option
5208 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
5209 list_cbdata_t *cb, int depth)
5215 boolean_t scripted = cb->cb_scripted;
5216 uint64_t islog = B_FALSE;
5217 char *dashes = "%-*s - - - - - -\n";
5219 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
5220 (uint64_t **)&vs, &c) == 0);
5223 boolean_t toplevel = (vs->vs_space != 0);
5225 enum zfs_nicenum_format format;
5228 format = ZFS_NICENUM_RAW;
5230 format = ZFS_NICENUM_1024;
5232 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
5236 (void) printf("\t%s", name);
5237 else if (strlen(name) + depth > cb->cb_namewidth)
5238 (void) printf("%*s%s", depth, "", name);
5240 (void) printf("%*s%s%*s", depth, "", name,
5241 (int)(cb->cb_namewidth - strlen(name) - depth), "");
5244 * Print the properties for the individual vdevs. Some
5245 * properties are only applicable to toplevel vdevs. The
5246 * 'toplevel' boolean value is passed to the print_one_column()
5247 * to indicate that the value is valid.
5249 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
5251 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
5253 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
5254 scripted, toplevel, format);
5255 print_one_column(ZPOOL_PROP_CHECKPOINT,
5256 vs->vs_checkpoint_space, scripted, toplevel, format);
5257 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
5259 print_one_column(ZPOOL_PROP_FRAGMENTATION,
5260 vs->vs_fragmentation, scripted,
5261 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
5263 cap = (vs->vs_space == 0) ? 0 :
5264 (vs->vs_alloc * 10000 / vs->vs_space);
5265 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
5267 (void) printf("\n");
5270 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
5271 &child, &children) != 0)
5274 /* list the normal vdevs first */
5275 for (c = 0; c < children; c++) {
5276 uint64_t ishole = B_FALSE;
5278 if (nvlist_lookup_uint64(child[c],
5279 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
5282 if (nvlist_lookup_uint64(child[c],
5283 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
5286 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
5289 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5291 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5295 /* list the classes: 'logs', 'dedup', and 'special' */
5296 for (uint_t n = 0; n < 3; n++) {
5297 boolean_t printed = B_FALSE;
5299 for (c = 0; c < children; c++) {
5303 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5304 &islog) == 0 && islog) {
5305 bias = VDEV_ALLOC_CLASS_LOGS;
5307 (void) nvlist_lookup_string(child[c],
5308 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
5309 (void) nvlist_lookup_string(child[c],
5310 ZPOOL_CONFIG_TYPE, &type);
5312 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
5314 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
5318 /* LINTED E_SEC_PRINTF_VAR_FMT */
5319 (void) printf(dashes, cb->cb_namewidth,
5323 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5325 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5330 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5331 &child, &children) == 0 && children > 0) {
5332 /* LINTED E_SEC_PRINTF_VAR_FMT */
5333 (void) printf(dashes, cb->cb_namewidth, "cache");
5334 for (c = 0; c < children; c++) {
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_SPARES, &child,
5343 &children) == 0 && children > 0) {
5344 /* LINTED E_SEC_PRINTF_VAR_FMT */
5345 (void) printf(dashes, cb->cb_namewidth, "spare");
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);
5356 * Generic callback function to list a pool.
5359 list_callback(zpool_handle_t *zhp, void *data)
5361 list_cbdata_t *cbp = data;
5365 config = zpool_get_config(zhp, NULL);
5367 if (cbp->cb_verbose) {
5368 config = zpool_get_config(zhp, NULL);
5370 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5374 if (cbp->cb_verbose)
5375 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
5376 cbp->cb_name_flags);
5378 print_pool(zhp, cbp);
5380 if (cbp->cb_verbose)
5381 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5387 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5389 * -g Display guid for individual vdev name.
5390 * -H Scripted mode. Don't display headers, and separate properties
5392 * -L Follow links when resolving vdev path name.
5393 * -o List of properties to display. Defaults to
5394 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5395 * "dedupratio,health,altroot"
5396 * -p Display values in parsable (exact) format.
5397 * -P Display full path for vdev name.
5398 * -T Display a timestamp in date(1) or Unix format
5400 * List all pools in the system, whether or not they're healthy. Output space
5401 * statistics for each one, as well as health status summary.
5404 zpool_do_list(int argc, char **argv)
5408 list_cbdata_t cb = { 0 };
5409 static char default_props[] =
5410 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
5411 "capacity,dedupratio,health,altroot";
5412 char *props = default_props;
5414 unsigned long count = 0;
5416 boolean_t first = B_TRUE;
5419 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5422 cb.cb_name_flags |= VDEV_NAME_GUID;
5425 cb.cb_scripted = B_TRUE;
5428 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5434 cb.cb_name_flags |= VDEV_NAME_PATH;
5437 cb.cb_literal = B_TRUE;
5440 get_timestamp_arg(*optarg);
5443 cb.cb_verbose = B_TRUE;
5444 cb.cb_namewidth = 8; /* 8 until precalc is avail */
5447 (void) fprintf(stderr, gettext("missing argument for "
5448 "'%c' option\n"), optopt);
5452 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5461 get_interval_count(&argc, argv, &interval, &count);
5463 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5467 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5471 if (pool_list_count(list) == 0)
5474 if (timestamp_fmt != NODATE)
5475 print_timestamp(timestamp_fmt);
5477 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5481 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5486 if (count != 0 && --count == 0)
5489 pool_list_free(list);
5490 (void) fsleep(interval);
5493 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5494 (void) printf(gettext("no pools available\n"));
5498 pool_list_free(list);
5499 zprop_free_list(cb.cb_proplist);
5504 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5506 boolean_t force = B_FALSE;
5509 char *poolname, *old_disk, *new_disk;
5510 zpool_handle_t *zhp;
5511 nvlist_t *props = NULL;
5516 while ((c = getopt(argc, argv, "fo:")) != -1) {
5522 if ((propval = strchr(optarg, '=')) == NULL) {
5523 (void) fprintf(stderr, gettext("missing "
5524 "'=' for -o option\n"));
5530 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5531 (add_prop_list(optarg, propval, &props, B_TRUE)))
5535 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5544 /* get pool name and check number of arguments */
5546 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5553 (void) fprintf(stderr,
5554 gettext("missing <device> specification\n"));
5562 (void) fprintf(stderr,
5563 gettext("missing <new_device> specification\n"));
5566 new_disk = old_disk;
5576 (void) fprintf(stderr, gettext("too many arguments\n"));
5580 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5585 if (zpool_get_config(zhp, NULL) == NULL) {
5586 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5593 /* unless manually specified use "ashift" pool property (if set) */
5594 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5597 char strval[ZPOOL_MAXPROPLEN];
5599 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5600 if (src != ZPROP_SRC_DEFAULT) {
5601 (void) sprintf(strval, "%" PRId32, intval);
5602 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5603 &props, B_TRUE) == 0);
5607 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5609 if (nvroot == NULL) {
5615 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5618 nvlist_free(nvroot);
5625 * zpool replace [-f] <pool> <device> <new_device>
5627 * -f Force attach, even if <new_device> appears to be in use.
5629 * Replace <device> with <new_device>.
5633 zpool_do_replace(int argc, char **argv)
5635 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5639 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5641 * -f Force attach, even if <new_device> appears to be in use.
5642 * -o Set property=value.
5644 * Attach <new_device> to the mirror containing <device>. If <device> is not
5645 * part of a mirror, then <device> will be transformed into a mirror of
5646 * <device> and <new_device>. In either case, <new_device> will begin life
5647 * with a DTL of [0, now], and will immediately begin to resilver itself.
5650 zpool_do_attach(int argc, char **argv)
5652 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5656 * zpool detach [-f] <pool> <device>
5658 * -f Force detach of <device>, even if DTLs argue against it
5659 * (not supported yet)
5661 * Detach a device from a mirror. The operation will be refused if <device>
5662 * is the last device in the mirror, or if the DTLs indicate that this device
5663 * has the only valid copy of some data.
5667 zpool_do_detach(int argc, char **argv)
5670 char *poolname, *path;
5671 zpool_handle_t *zhp;
5675 while ((c = getopt(argc, argv, "f")) != -1) {
5679 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5688 /* get pool name and check number of arguments */
5690 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5695 (void) fprintf(stderr,
5696 gettext("missing <device> specification\n"));
5703 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5706 ret = zpool_vdev_detach(zhp, path);
5714 * zpool split [-gLnP] [-o prop=val] ...
5716 * [-R altroot] <pool> <newpool> [<device> ...]
5718 * -g Display guid for individual vdev name.
5719 * -L Follow links when resolving vdev path name.
5720 * -n Do not split the pool, but display the resulting layout if
5721 * it were to be split.
5722 * -o Set property=value, or set mount options.
5723 * -P Display full path for vdev name.
5724 * -R Mount the split-off pool under an alternate root.
5725 * -l Load encryption keys while importing.
5727 * Splits the named pool and gives it the new pool name. Devices to be split
5728 * off may be listed, provided that no more than one device is specified
5729 * per top-level vdev mirror. The newly split pool is left in an exported
5730 * state unless -R is specified.
5732 * Restrictions: the top-level of the pool pool must only be made up of
5733 * mirrors; all devices in the pool must be healthy; no device may be
5734 * undergoing a resilvering operation.
5737 zpool_do_split(int argc, char **argv)
5739 char *srcpool, *newpool, *propval;
5740 char *mntopts = NULL;
5743 boolean_t loadkeys = B_FALSE;
5744 zpool_handle_t *zhp;
5745 nvlist_t *config, *props = NULL;
5747 flags.dryrun = B_FALSE;
5748 flags.import = B_FALSE;
5749 flags.name_flags = 0;
5752 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5755 flags.name_flags |= VDEV_NAME_GUID;
5758 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5761 flags.import = B_TRUE;
5763 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5764 &props, B_TRUE) != 0) {
5773 flags.dryrun = B_TRUE;
5776 if ((propval = strchr(optarg, '=')) != NULL) {
5779 if (add_prop_list(optarg, propval,
5780 &props, B_TRUE) != 0) {
5789 flags.name_flags |= VDEV_NAME_PATH;
5792 (void) fprintf(stderr, gettext("missing argument for "
5793 "'%c' option\n"), optopt);
5797 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5804 if (!flags.import && mntopts != NULL) {
5805 (void) fprintf(stderr, gettext("setting mntopts is only "
5806 "valid when importing the pool\n"));
5810 if (!flags.import && loadkeys) {
5811 (void) fprintf(stderr, gettext("loading keys is only "
5812 "valid when importing the pool\n"));
5820 (void) fprintf(stderr, gettext("Missing pool name\n"));
5824 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5834 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5839 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5840 if (config == NULL) {
5844 (void) printf(gettext("would create '%s' with the "
5845 "following layout:\n\n"), newpool);
5846 print_vdev_tree(NULL, newpool, config, 0, "",
5853 if (ret != 0 || flags.dryrun || !flags.import) {
5854 nvlist_free(config);
5860 * The split was successful. Now we need to open the new
5861 * pool and import it.
5863 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5864 nvlist_free(config);
5870 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5875 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5876 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5878 (void) fprintf(stderr, gettext("Split was successful, but "
5879 "the datasets could not all be mounted\n"));
5880 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5881 "different altroot\n"), "zpool import");
5884 nvlist_free(config);
5893 * zpool online <pool> <device> ...
5896 zpool_do_online(int argc, char **argv)
5900 zpool_handle_t *zhp;
5902 vdev_state_t newstate;
5906 while ((c = getopt(argc, argv, "et")) != -1) {
5909 flags |= ZFS_ONLINE_EXPAND;
5913 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5922 /* get pool name and check number of arguments */
5924 (void) fprintf(stderr, gettext("missing pool name\n"));
5928 (void) fprintf(stderr, gettext("missing device name\n"));
5934 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5937 for (i = 1; i < argc; i++) {
5938 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5939 if (newstate != VDEV_STATE_HEALTHY) {
5940 (void) printf(gettext("warning: device '%s' "
5941 "onlined, but remains in faulted state\n"),
5943 if (newstate == VDEV_STATE_FAULTED)
5944 (void) printf(gettext("use 'zpool "
5945 "clear' to restore a faulted "
5948 (void) printf(gettext("use 'zpool "
5949 "replace' to replace devices "
5950 "that are no longer present\n"));
5963 * zpool offline [-ft] <pool> <device> ...
5965 * -f Force the device into a faulted state.
5967 * -t Only take the device off-line temporarily. The offline/faulted
5968 * state will not be persistent across reboots.
5972 zpool_do_offline(int argc, char **argv)
5976 zpool_handle_t *zhp;
5978 boolean_t istmp = B_FALSE;
5979 boolean_t fault = B_FALSE;
5982 while ((c = getopt(argc, argv, "ft")) != -1) {
5991 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6000 /* get pool name and check number of arguments */
6002 (void) fprintf(stderr, gettext("missing pool name\n"));
6006 (void) fprintf(stderr, gettext("missing device name\n"));
6012 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6015 for (i = 1; i < argc; i++) {
6017 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
6019 if (istmp == B_FALSE) {
6020 /* Force the fault to persist across imports */
6021 aux = VDEV_AUX_EXTERNAL_PERSIST;
6023 aux = VDEV_AUX_EXTERNAL;
6026 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
6029 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
6040 * zpool clear <pool> [device]
6042 * Clear all errors associated with a pool or a particular device.
6045 zpool_do_clear(int argc, char **argv)
6049 boolean_t dryrun = B_FALSE;
6050 boolean_t do_rewind = B_FALSE;
6051 boolean_t xtreme_rewind = B_FALSE;
6052 uint32_t rewind_policy = ZPOOL_NO_REWIND;
6053 nvlist_t *policy = NULL;
6054 zpool_handle_t *zhp;
6055 char *pool, *device;
6058 while ((c = getopt(argc, argv, "FnX")) != -1) {
6067 xtreme_rewind = B_TRUE;
6070 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6080 (void) fprintf(stderr, gettext("missing pool name\n"));
6085 (void) fprintf(stderr, gettext("too many arguments\n"));
6089 if ((dryrun || xtreme_rewind) && !do_rewind) {
6090 (void) fprintf(stderr,
6091 gettext("-n or -X only meaningful with -F\n"));
6095 rewind_policy = ZPOOL_TRY_REWIND;
6097 rewind_policy = ZPOOL_DO_REWIND;
6099 rewind_policy |= ZPOOL_EXTREME_REWIND;
6101 /* In future, further rewind policy choices can be passed along here */
6102 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
6103 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
6104 rewind_policy) != 0) {
6109 device = argc == 2 ? argv[1] : NULL;
6111 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
6112 nvlist_free(policy);
6116 if (zpool_clear(zhp, device, policy) != 0)
6121 nvlist_free(policy);
6127 * zpool reguid <pool>
6130 zpool_do_reguid(int argc, char **argv)
6134 zpool_handle_t *zhp;
6138 while ((c = getopt(argc, argv, "")) != -1) {
6141 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6150 /* get pool name and check number of arguments */
6152 (void) fprintf(stderr, gettext("missing pool name\n"));
6157 (void) fprintf(stderr, gettext("too many arguments\n"));
6162 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6165 ret = zpool_reguid(zhp);
6173 * zpool reopen <pool>
6175 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6178 zpool_do_reopen(int argc, char **argv)
6182 boolean_t scrub_restart = B_TRUE;
6185 while ((c = getopt(argc, argv, "n")) != -1) {
6188 scrub_restart = B_FALSE;
6191 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6200 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6201 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
6207 typedef struct scrub_cbdata {
6211 pool_scrub_cmd_t cb_scrub_cmd;
6215 zpool_has_checkpoint(zpool_handle_t *zhp)
6217 nvlist_t *config, *nvroot;
6219 config = zpool_get_config(zhp, NULL);
6221 if (config != NULL) {
6222 pool_checkpoint_stat_t *pcs = NULL;
6225 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6226 (void) nvlist_lookup_uint64_array(nvroot,
6227 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
6229 if (pcs == NULL || pcs->pcs_state == CS_NONE)
6232 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
6233 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
6241 scrub_callback(zpool_handle_t *zhp, void *data)
6243 scrub_cbdata_t *cb = data;
6247 * Ignore faulted pools.
6249 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
6250 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
6251 "currently unavailable\n"), zpool_get_name(zhp));
6255 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
6257 if (err == 0 && zpool_has_checkpoint(zhp) &&
6258 cb->cb_type == POOL_SCAN_SCRUB) {
6259 (void) printf(gettext("warning: will not scrub state that "
6260 "belongs to the checkpoint of pool '%s'\n"),
6261 zpool_get_name(zhp));
6268 * zpool scrub [-s | -p] <pool> ...
6270 * -s Stop. Stops any in-progress scrub.
6271 * -p Pause. Pause in-progress scrub.
6274 zpool_do_scrub(int argc, char **argv)
6279 cb.cb_type = POOL_SCAN_SCRUB;
6280 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6283 while ((c = getopt(argc, argv, "sp")) != -1) {
6286 cb.cb_type = POOL_SCAN_NONE;
6289 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
6292 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6298 if (cb.cb_type == POOL_SCAN_NONE &&
6299 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
6300 (void) fprintf(stderr, gettext("invalid option combination: "
6301 "-s and -p are mutually exclusive\n"));
6311 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6315 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6319 * Print out detailed scrub status.
6322 print_scan_status(pool_scan_stat_t *ps)
6324 time_t start, end, pause;
6325 uint64_t total_secs_left;
6326 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
6327 uint64_t pass_scanned, scanned, pass_issued, issued, total;
6328 uint64_t scan_rate, issue_rate;
6329 double fraction_done;
6330 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
6331 char srate_buf[7], irate_buf[7];
6333 (void) printf(gettext(" scan: "));
6335 /* If there's never been a scan, there's not much to say. */
6336 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
6337 ps->pss_func >= POOL_SCAN_FUNCS) {
6338 (void) printf(gettext("none requested\n"));
6342 start = ps->pss_start_time;
6343 end = ps->pss_end_time;
6344 pause = ps->pss_pass_scrub_pause;
6346 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6348 assert(ps->pss_func == POOL_SCAN_SCRUB ||
6349 ps->pss_func == POOL_SCAN_RESILVER);
6351 /* Scan is finished or canceled. */
6352 if (ps->pss_state == DSS_FINISHED) {
6353 total_secs_left = end - start;
6354 days_left = total_secs_left / 60 / 60 / 24;
6355 hours_left = (total_secs_left / 60 / 60) % 24;
6356 mins_left = (total_secs_left / 60) % 60;
6357 secs_left = (total_secs_left % 60);
6359 if (ps->pss_func == POOL_SCAN_SCRUB) {
6360 (void) printf(gettext("scrub repaired %s "
6361 "in %llu days %02llu:%02llu:%02llu "
6362 "with %llu errors on %s"), processed_buf,
6363 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6364 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6365 (u_longlong_t)ps->pss_errors, ctime(&end));
6366 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6367 (void) printf(gettext("resilvered %s "
6368 "in %llu days %02llu:%02llu:%02llu "
6369 "with %llu errors on %s"), processed_buf,
6370 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6371 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6372 (u_longlong_t)ps->pss_errors, ctime(&end));
6375 } else if (ps->pss_state == DSS_CANCELED) {
6376 if (ps->pss_func == POOL_SCAN_SCRUB) {
6377 (void) printf(gettext("scrub canceled on %s"),
6379 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6380 (void) printf(gettext("resilver canceled on %s"),
6386 assert(ps->pss_state == DSS_SCANNING);
6388 /* Scan is in progress. Resilvers can't be paused. */
6389 if (ps->pss_func == POOL_SCAN_SCRUB) {
6391 (void) printf(gettext("scrub in progress since %s"),
6394 (void) printf(gettext("scrub paused since %s"),
6396 (void) printf(gettext("\tscrub started on %s"),
6399 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6400 (void) printf(gettext("resilver in progress since %s"),
6404 scanned = ps->pss_examined;
6405 pass_scanned = ps->pss_pass_exam;
6406 issued = ps->pss_issued;
6407 pass_issued = ps->pss_pass_issued;
6408 total = ps->pss_to_examine;
6410 /* we are only done with a block once we have issued the IO for it */
6411 fraction_done = (double)issued / total;
6413 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6414 elapsed = time(NULL) - ps->pss_pass_start;
6415 elapsed -= ps->pss_pass_scrub_spent_paused;
6416 elapsed = (elapsed != 0) ? elapsed : 1;
6418 scan_rate = pass_scanned / elapsed;
6419 issue_rate = pass_issued / elapsed;
6420 total_secs_left = (issue_rate != 0) ?
6421 ((total - issued) / issue_rate) : UINT64_MAX;
6423 days_left = total_secs_left / 60 / 60 / 24;
6424 hours_left = (total_secs_left / 60 / 60) % 24;
6425 mins_left = (total_secs_left / 60) % 60;
6426 secs_left = (total_secs_left % 60);
6428 /* format all of the numbers we will be reporting */
6429 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
6430 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
6431 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6432 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
6433 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
6435 /* do not print estimated time if we have a paused scrub */
6437 (void) printf(gettext("\t%s scanned at %s/s, "
6438 "%s issued at %s/s, %s total\n"),
6439 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
6441 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6442 scanned_buf, issued_buf, total_buf);
6445 if (ps->pss_func == POOL_SCAN_RESILVER) {
6446 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6447 processed_buf, 100 * fraction_done);
6448 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6449 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6450 processed_buf, 100 * fraction_done);
6454 if (issue_rate >= 10 * 1024 * 1024) {
6455 (void) printf(gettext(", %llu days "
6456 "%02llu:%02llu:%02llu to go\n"),
6457 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6458 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
6460 (void) printf(gettext(", no estimated "
6461 "completion time\n"));
6464 (void) printf(gettext("\n"));
6469 * As we don't scrub checkpointed blocks, we want to warn the
6470 * user that we skipped scanning some blocks if a checkpoint exists
6471 * or existed at any time during the scan.
6474 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
6476 if (ps == NULL || pcs == NULL)
6479 if (pcs->pcs_state == CS_NONE ||
6480 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
6483 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
6485 if (ps->pss_state == DSS_NONE)
6488 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
6489 ps->pss_end_time < pcs->pcs_start_time)
6492 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
6493 (void) printf(gettext(" scan warning: skipped blocks "
6494 "that are only referenced by the checkpoint.\n"));
6496 assert(ps->pss_state == DSS_SCANNING);
6497 (void) printf(gettext(" scan warning: skipping blocks "
6498 "that are only referenced by the checkpoint.\n"));
6503 * Print out detailed removal status.
6506 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
6508 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
6510 nvlist_t *config, *nvroot;
6515 if (prs == NULL || prs->prs_state == DSS_NONE)
6519 * Determine name of vdev.
6521 config = zpool_get_config(zhp, NULL);
6522 nvroot = fnvlist_lookup_nvlist(config,
6523 ZPOOL_CONFIG_VDEV_TREE);
6524 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
6525 &child, &children) == 0);
6526 assert(prs->prs_removing_vdev < children);
6527 vdev_name = zpool_vdev_name(g_zfs, zhp,
6528 child[prs->prs_removing_vdev], B_TRUE);
6530 (void) printf(gettext("remove: "));
6532 start = prs->prs_start_time;
6533 end = prs->prs_end_time;
6534 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
6537 * Removal is finished or canceled.
6539 if (prs->prs_state == DSS_FINISHED) {
6540 uint64_t minutes_taken = (end - start) / 60;
6542 (void) printf(gettext("Removal of vdev %llu copied %s "
6543 "in %lluh%um, completed on %s"),
6544 (longlong_t)prs->prs_removing_vdev,
6546 (u_longlong_t)(minutes_taken / 60),
6547 (uint_t)(minutes_taken % 60),
6548 ctime((time_t *)&end));
6549 } else if (prs->prs_state == DSS_CANCELED) {
6550 (void) printf(gettext("Removal of %s canceled on %s"),
6551 vdev_name, ctime(&end));
6553 uint64_t copied, total, elapsed, mins_left, hours_left;
6554 double fraction_done;
6557 assert(prs->prs_state == DSS_SCANNING);
6560 * Removal is in progress.
6562 (void) printf(gettext(
6563 "Evacuation of %s in progress since %s"),
6564 vdev_name, ctime(&start));
6566 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
6567 total = prs->prs_to_copy;
6568 fraction_done = (double)copied / total;
6570 /* elapsed time for this pass */
6571 elapsed = time(NULL) - prs->prs_start_time;
6572 elapsed = elapsed > 0 ? elapsed : 1;
6573 rate = copied / elapsed;
6574 rate = rate > 0 ? rate : 1;
6575 mins_left = ((total - copied) / rate) / 60;
6576 hours_left = mins_left / 60;
6578 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
6579 zfs_nicenum(total, total_buf, sizeof (total_buf));
6580 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
6583 * do not print estimated time if hours_left is more than
6586 (void) printf(gettext(" %s copied out of %s at %s/s, "
6588 examined_buf, total_buf, rate_buf, 100 * fraction_done);
6589 if (hours_left < (30 * 24)) {
6590 (void) printf(gettext(", %lluh%um to go\n"),
6591 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
6593 (void) printf(gettext(
6594 ", (copy is slow, no estimated time)\n"));
6598 if (prs->prs_mapping_memory > 0) {
6600 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
6601 (void) printf(gettext(" %s memory used for "
6602 "removed device mappings\n"),
6608 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
6613 if (pcs == NULL || pcs->pcs_state == CS_NONE)
6616 (void) printf(gettext("checkpoint: "));
6618 start = pcs->pcs_start_time;
6619 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
6621 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
6622 char *date = ctime(&start);
6625 * ctime() adds a newline at the end of the generated
6626 * string, thus the weird format specifier and the
6627 * strlen() call used to chop it off from the output.
6629 (void) printf(gettext("created %.*s, consumes %s\n"),
6630 (int)(strlen(date) - 1), date, space_buf);
6634 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
6636 (void) printf(gettext("discarding, %s remaining.\n"),
6641 print_error_log(zpool_handle_t *zhp)
6643 nvlist_t *nverrlist = NULL;
6646 size_t len = MAXPATHLEN * 2;
6648 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6651 (void) printf("errors: Permanent errors have been "
6652 "detected in the following files:\n\n");
6654 pathname = safe_malloc(len);
6656 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6658 uint64_t dsobj, obj;
6660 verify(nvpair_value_nvlist(elem, &nv) == 0);
6661 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6663 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6665 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6666 (void) printf("%7s %s\n", "", pathname);
6669 nvlist_free(nverrlist);
6673 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6682 (void) printf(gettext("\tspares\n"));
6684 for (i = 0; i < nspares; i++) {
6685 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6687 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6693 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6702 (void) printf(gettext("\tcache\n"));
6704 for (i = 0; i < nl2cache; i++) {
6705 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6707 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6713 print_dedup_stats(nvlist_t *config)
6715 ddt_histogram_t *ddh;
6719 char dspace[6], mspace[6];
6722 * If the pool was faulted then we may not have been able to
6723 * obtain the config. Otherwise, if we have anything in the dedup
6724 * table continue processing the stats.
6726 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6727 (uint64_t **)&ddo, &c) != 0)
6730 (void) printf("\n");
6731 (void) printf(gettext(" dedup: "));
6732 if (ddo->ddo_count == 0) {
6733 (void) printf(gettext("no DDT entries\n"));
6737 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6738 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6739 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6740 (u_longlong_t)ddo->ddo_count,
6744 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6745 (uint64_t **)&dds, &c) == 0);
6746 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6747 (uint64_t **)&ddh, &c) == 0);
6748 zpool_dump_ddt(dds, ddh);
6752 * Display a summary of pool status. Displays a summary such as:
6756 * reason: One or more devices ...
6757 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6763 * When given the '-v' option, we print out the complete config. If the '-e'
6764 * option is specified, then we print out error rate information as well.
6767 status_callback(zpool_handle_t *zhp, void *data)
6769 status_cbdata_t *cbp = data;
6770 nvlist_t *config, *nvroot;
6772 zpool_status_t reason;
6773 zpool_errata_t errata;
6778 config = zpool_get_config(zhp, NULL);
6779 reason = zpool_get_status(zhp, &msgid, &errata);
6784 * If we were given 'zpool status -x', only report those pools with
6787 if (cbp->cb_explain &&
6788 (reason == ZPOOL_STATUS_OK ||
6789 reason == ZPOOL_STATUS_VERSION_OLDER ||
6790 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6791 if (!cbp->cb_allpools) {
6792 (void) printf(gettext("pool '%s' is healthy\n"),
6793 zpool_get_name(zhp));
6795 cbp->cb_first = B_FALSE;
6801 cbp->cb_first = B_FALSE;
6803 (void) printf("\n");
6805 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6806 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6807 (uint64_t **)&vs, &c) == 0);
6809 health = zpool_get_state_str(zhp);
6811 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6812 (void) printf(gettext(" state: %s\n"), health);
6815 case ZPOOL_STATUS_MISSING_DEV_R:
6816 (void) printf(gettext("status: One or more devices could not "
6817 "be opened. Sufficient replicas exist for\n\tthe pool to "
6818 "continue functioning in a degraded state.\n"));
6819 (void) printf(gettext("action: Attach the missing device and "
6820 "online it using 'zpool online'.\n"));
6823 case ZPOOL_STATUS_MISSING_DEV_NR:
6824 (void) printf(gettext("status: One or more devices could not "
6825 "be opened. There are insufficient\n\treplicas for the "
6826 "pool to continue functioning.\n"));
6827 (void) printf(gettext("action: Attach the missing device and "
6828 "online it using 'zpool online'.\n"));
6831 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6832 (void) printf(gettext("status: One or more devices could not "
6833 "be used because the label is missing or\n\tinvalid. "
6834 "Sufficient replicas exist for the pool to continue\n\t"
6835 "functioning in a degraded state.\n"));
6836 (void) printf(gettext("action: Replace the device using "
6837 "'zpool replace'.\n"));
6840 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6841 (void) printf(gettext("status: One or more devices could not "
6842 "be used because the label is missing \n\tor invalid. "
6843 "There are insufficient replicas for the pool to "
6844 "continue\n\tfunctioning.\n"));
6845 zpool_explain_recover(zpool_get_handle(zhp),
6846 zpool_get_name(zhp), reason, config);
6849 case ZPOOL_STATUS_FAILING_DEV:
6850 (void) printf(gettext("status: One or more devices has "
6851 "experienced an unrecoverable error. An\n\tattempt was "
6852 "made to correct the error. Applications are "
6854 (void) printf(gettext("action: Determine if the device needs "
6855 "to be replaced, and clear the errors\n\tusing "
6856 "'zpool clear' or replace the device with 'zpool "
6860 case ZPOOL_STATUS_OFFLINE_DEV:
6861 (void) printf(gettext("status: One or more devices has "
6862 "been taken offline by the administrator.\n\tSufficient "
6863 "replicas exist for the pool to continue functioning in "
6864 "a\n\tdegraded state.\n"));
6865 (void) printf(gettext("action: Online the device using "
6866 "'zpool online' or replace the device with\n\t'zpool "
6870 case ZPOOL_STATUS_REMOVED_DEV:
6871 (void) printf(gettext("status: One or more devices has "
6872 "been removed by the administrator.\n\tSufficient "
6873 "replicas exist for the pool to continue functioning in "
6874 "a\n\tdegraded state.\n"));
6875 (void) printf(gettext("action: Online the device using "
6876 "'zpool online' or replace the device with\n\t'zpool "
6880 case ZPOOL_STATUS_RESILVERING:
6881 (void) printf(gettext("status: One or more devices is "
6882 "currently being resilvered. The pool will\n\tcontinue "
6883 "to function, possibly in a degraded state.\n"));
6884 (void) printf(gettext("action: Wait for the resilver to "
6888 case ZPOOL_STATUS_CORRUPT_DATA:
6889 (void) printf(gettext("status: One or more devices has "
6890 "experienced an error resulting in data\n\tcorruption. "
6891 "Applications may be affected.\n"));
6892 (void) printf(gettext("action: Restore the file in question "
6893 "if possible. Otherwise restore the\n\tentire pool from "
6897 case ZPOOL_STATUS_CORRUPT_POOL:
6898 (void) printf(gettext("status: The pool metadata is corrupted "
6899 "and the pool cannot be opened.\n"));
6900 zpool_explain_recover(zpool_get_handle(zhp),
6901 zpool_get_name(zhp), reason, config);
6904 case ZPOOL_STATUS_VERSION_OLDER:
6905 (void) printf(gettext("status: The pool is formatted using a "
6906 "legacy on-disk format. The pool can\n\tstill be used, "
6907 "but some features are unavailable.\n"));
6908 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6909 "upgrade'. Once this is done, the\n\tpool will no longer "
6910 "be accessible on software that does not support\n\t"
6911 "feature flags.\n"));
6914 case ZPOOL_STATUS_VERSION_NEWER:
6915 (void) printf(gettext("status: The pool has been upgraded to a "
6916 "newer, incompatible on-disk version.\n\tThe pool cannot "
6917 "be accessed on this system.\n"));
6918 (void) printf(gettext("action: Access the pool from a system "
6919 "running more recent software, or\n\trestore the pool from "
6923 case ZPOOL_STATUS_FEAT_DISABLED:
6924 (void) printf(gettext("status: Some supported features are not "
6925 "enabled on the pool. The pool can\n\tstill be used, but "
6926 "some features are unavailable.\n"));
6927 (void) printf(gettext("action: Enable all features using "
6928 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6929 "longer be accessible by software that does not support\n\t"
6930 "the features. See zpool-features(5) for details.\n"));
6933 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6934 (void) printf(gettext("status: The pool cannot be accessed on "
6935 "this system because it uses the\n\tfollowing feature(s) "
6936 "not supported on this system:\n"));
6937 zpool_print_unsup_feat(config);
6938 (void) printf("\n");
6939 (void) printf(gettext("action: Access the pool from a system "
6940 "that supports the required feature(s),\n\tor restore the "
6941 "pool from backup.\n"));
6944 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6945 (void) printf(gettext("status: The pool can only be accessed "
6946 "in read-only mode on this system. It\n\tcannot be "
6947 "accessed in read-write mode because it uses the "
6948 "following\n\tfeature(s) not supported on this system:\n"));
6949 zpool_print_unsup_feat(config);
6950 (void) printf("\n");
6951 (void) printf(gettext("action: The pool cannot be accessed in "
6952 "read-write mode. Import the pool with\n"
6953 "\t\"-o readonly=on\", access the pool from a system that "
6954 "supports the\n\trequired feature(s), or restore the "
6955 "pool from backup.\n"));
6958 case ZPOOL_STATUS_FAULTED_DEV_R:
6959 (void) printf(gettext("status: One or more devices are "
6960 "faulted in response to persistent errors.\n\tSufficient "
6961 "replicas exist for the pool to continue functioning "
6962 "in a\n\tdegraded state.\n"));
6963 (void) printf(gettext("action: Replace the faulted device, "
6964 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6967 case ZPOOL_STATUS_FAULTED_DEV_NR:
6968 (void) printf(gettext("status: One or more devices are "
6969 "faulted in response to persistent errors. There are "
6970 "insufficient replicas for the pool to\n\tcontinue "
6972 (void) printf(gettext("action: Destroy and re-create the pool "
6973 "from a backup source. Manually marking the device\n"
6974 "\trepaired using 'zpool clear' may allow some data "
6975 "to be recovered.\n"));
6978 case ZPOOL_STATUS_IO_FAILURE_MMP:
6979 (void) printf(gettext("status: The pool is suspended because "
6980 "multihost writes failed or were delayed;\n\tanother "
6981 "system could import the pool undetected.\n"));
6982 (void) printf(gettext("action: Make sure the pool's devices "
6983 "are connected, then reboot your system and\n\timport the "
6987 case ZPOOL_STATUS_IO_FAILURE_WAIT:
6988 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6989 (void) printf(gettext("status: One or more devices are "
6990 "faulted in response to IO failures.\n"));
6991 (void) printf(gettext("action: Make sure the affected devices "
6992 "are connected, then run 'zpool clear'.\n"));
6995 case ZPOOL_STATUS_BAD_LOG:
6996 (void) printf(gettext("status: An intent log record "
6997 "could not be read.\n"
6998 "\tWaiting for administrator intervention to fix the "
6999 "faulted pool.\n"));
7000 (void) printf(gettext("action: Either restore the affected "
7001 "device(s) and run 'zpool online',\n"
7002 "\tor ignore the intent log records by running "
7003 "'zpool clear'.\n"));
7006 case ZPOOL_STATUS_HOSTID_MISMATCH:
7007 (void) printf(gettext("status: Mismatch between pool hostid "
7008 "and system hostid on imported pool.\n\tThis pool was "
7009 "previously imported into a system with a different "
7010 "hostid,\n\tand then was verbatim imported into this "
7012 (void) printf(gettext("action: Export this pool on all systems "
7013 "on which it is imported.\n"
7014 "\tThen import it to correct the mismatch.\n"));
7017 case ZPOOL_STATUS_ERRATA:
7018 (void) printf(gettext("status: Errata #%d detected.\n"),
7022 case ZPOOL_ERRATA_NONE:
7025 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
7026 (void) printf(gettext("action: To correct the issue "
7027 "run 'zpool scrub'.\n"));
7030 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
7031 (void) printf(gettext("\tExisting encrypted datasets "
7032 "contain an on-disk incompatibility\n\twhich "
7033 "needs to be corrected.\n"));
7034 (void) printf(gettext("action: To correct the issue "
7035 "backup existing encrypted datasets to new\n\t"
7036 "encrypted datasets and destroy the old ones. "
7037 "'zfs mount -o ro' can\n\tbe used to temporarily "
7038 "mount existing encrypted datasets readonly.\n"));
7043 * All errata which allow the pool to be imported
7044 * must contain an action message.
7052 * The remaining errors can't actually be generated, yet.
7054 assert(reason == ZPOOL_STATUS_OK);
7058 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
7061 if (config != NULL) {
7063 nvlist_t **spares, **l2cache;
7064 uint_t nspares, nl2cache;
7065 pool_checkpoint_stat_t *pcs = NULL;
7066 pool_scan_stat_t *ps = NULL;
7067 pool_removal_stat_t *prs = NULL;
7069 (void) nvlist_lookup_uint64_array(nvroot,
7070 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7071 (void) nvlist_lookup_uint64_array(nvroot,
7072 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
7073 (void) nvlist_lookup_uint64_array(nvroot,
7074 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
7076 print_scan_status(ps);
7077 print_checkpoint_scan_warning(ps, pcs);
7078 print_removal_status(zhp, prs);
7079 print_checkpoint_status(pcs);
7081 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
7082 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
7083 if (cbp->cb_namewidth < 10)
7084 cbp->cb_namewidth = 10;
7086 (void) printf(gettext("config:\n\n"));
7087 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
7088 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
7091 if (cbp->vcdl != NULL)
7092 print_cmd_columns(cbp->vcdl, 0);
7096 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
7099 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
7100 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
7101 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
7103 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
7104 &l2cache, &nl2cache) == 0)
7105 print_l2cache(zhp, cbp, l2cache, nl2cache);
7107 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
7108 &spares, &nspares) == 0)
7109 print_spares(zhp, cbp, spares, nspares);
7111 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
7113 nvlist_t *nverrlist = NULL;
7116 * If the approximate error count is small, get a
7117 * precise count by fetching the entire log and
7118 * uniquifying the results.
7120 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
7121 zpool_get_errlog(zhp, &nverrlist) == 0) {
7126 while ((elem = nvlist_next_nvpair(nverrlist,
7131 nvlist_free(nverrlist);
7133 (void) printf("\n");
7136 (void) printf(gettext("errors: No known data "
7138 else if (!cbp->cb_verbose)
7139 (void) printf(gettext("errors: %llu data "
7140 "errors, use '-v' for a list\n"),
7141 (u_longlong_t)nerr);
7143 print_error_log(zhp);
7146 if (cbp->cb_dedup_stats)
7147 print_dedup_stats(config);
7149 (void) printf(gettext("config: The configuration cannot be "
7157 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
7158 * [interval [count]]
7160 * -c CMD For each vdev, run command CMD
7161 * -g Display guid for individual vdev name.
7162 * -L Follow links when resolving vdev path name.
7163 * -P Display full path for vdev name.
7164 * -v Display complete error logs
7165 * -x Display only pools with potential problems
7166 * -D Display dedup status (undocumented)
7167 * -T Display a timestamp in date(1) or Unix format
7169 * Describes the health status of all pools or some subset.
7172 zpool_do_status(int argc, char **argv)
7177 unsigned long count = 0;
7178 status_cbdata_t cb = { 0 };
7182 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
7187 gettext("Can't set -c flag twice\n"));
7191 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
7192 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
7193 fprintf(stderr, gettext(
7194 "Can't run -c, disabled by "
7195 "ZPOOL_SCRIPTS_ENABLED.\n"));
7199 if ((getuid() <= 0 || geteuid() <= 0) &&
7200 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
7201 fprintf(stderr, gettext(
7202 "Can't run -c with root privileges "
7203 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
7209 cb.cb_name_flags |= VDEV_NAME_GUID;
7212 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
7215 cb.cb_name_flags |= VDEV_NAME_PATH;
7218 cb.cb_verbose = B_TRUE;
7221 cb.cb_explain = B_TRUE;
7224 cb.cb_dedup_stats = B_TRUE;
7227 get_timestamp_arg(*optarg);
7230 if (optopt == 'c') {
7231 print_zpool_script_list("status");
7235 gettext("invalid option '%c'\n"), optopt);
7244 get_interval_count(&argc, argv, &interval, &count);
7247 cb.cb_allpools = B_TRUE;
7249 cb.cb_first = B_TRUE;
7250 cb.cb_print_status = B_TRUE;
7253 if (timestamp_fmt != NODATE)
7254 print_timestamp(timestamp_fmt);
7257 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
7260 ret = for_each_pool(argc, argv, B_TRUE, NULL,
7261 status_callback, &cb);
7263 if (cb.vcdl != NULL)
7264 free_vdev_cmd_data_list(cb.vcdl);
7266 if (argc == 0 && cb.cb_count == 0)
7267 (void) fprintf(stderr, gettext("no pools available\n"));
7268 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
7269 (void) printf(gettext("all pools are healthy\n"));
7277 if (count != 0 && --count == 0)
7280 (void) fsleep(interval);
7286 typedef struct upgrade_cbdata {
7289 uint64_t cb_version;
7294 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
7296 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
7297 int *count = (int *)unsupp_fs;
7299 if (zfs_version > ZPL_VERSION) {
7300 (void) printf(gettext("%s (v%d) is not supported by this "
7301 "implementation of ZFS.\n"),
7302 zfs_get_name(zhp), zfs_version);
7306 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
7314 upgrade_version(zpool_handle_t *zhp, uint64_t version)
7318 uint64_t oldversion;
7321 config = zpool_get_config(zhp, NULL);
7322 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7325 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
7326 assert(oldversion < version);
7328 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
7333 (void) fprintf(stderr, gettext("Upgrade not performed due "
7334 "to %d unsupported filesystems (max v%d).\n"),
7335 unsupp_fs, (int)ZPL_VERSION);
7339 ret = zpool_upgrade(zhp, version);
7343 if (version >= SPA_VERSION_FEATURES) {
7344 (void) printf(gettext("Successfully upgraded "
7345 "'%s' from version %llu to feature flags.\n"),
7346 zpool_get_name(zhp), (u_longlong_t)oldversion);
7348 (void) printf(gettext("Successfully upgraded "
7349 "'%s' from version %llu to version %llu.\n"),
7350 zpool_get_name(zhp), (u_longlong_t)oldversion,
7351 (u_longlong_t)version);
7358 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
7361 boolean_t firstff = B_TRUE;
7362 nvlist_t *enabled = zpool_get_features(zhp);
7365 for (i = 0; i < SPA_FEATURES; i++) {
7366 const char *fname = spa_feature_table[i].fi_uname;
7367 const char *fguid = spa_feature_table[i].fi_guid;
7368 if (!nvlist_exists(enabled, fguid)) {
7370 verify(-1 != asprintf(&propname, "feature@%s", fname));
7371 ret = zpool_set_prop(zhp, propname,
7372 ZFS_FEATURE_ENABLED);
7380 (void) printf(gettext("Enabled the "
7381 "following features on '%s':\n"),
7382 zpool_get_name(zhp));
7385 (void) printf(gettext(" %s\n"), fname);
7396 upgrade_cb(zpool_handle_t *zhp, void *arg)
7398 upgrade_cbdata_t *cbp = arg;
7401 boolean_t printnl = B_FALSE;
7404 config = zpool_get_config(zhp, NULL);
7405 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7408 assert(SPA_VERSION_IS_SUPPORTED(version));
7410 if (version < cbp->cb_version) {
7411 cbp->cb_first = B_FALSE;
7412 ret = upgrade_version(zhp, cbp->cb_version);
7418 * If they did "zpool upgrade -a", then we could
7419 * be doing ioctls to different pools. We need
7420 * to log this history once to each pool, and bypass
7421 * the normal history logging that happens in main().
7423 (void) zpool_log_history(g_zfs, history_str);
7424 log_history = B_FALSE;
7427 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7429 ret = upgrade_enable_all(zhp, &count);
7434 cbp->cb_first = B_FALSE;
7440 (void) printf(gettext("\n"));
7447 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
7449 upgrade_cbdata_t *cbp = arg;
7453 config = zpool_get_config(zhp, NULL);
7454 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7457 assert(SPA_VERSION_IS_SUPPORTED(version));
7459 if (version < SPA_VERSION_FEATURES) {
7460 if (cbp->cb_first) {
7461 (void) printf(gettext("The following pools are "
7462 "formatted with legacy version numbers and can\n"
7463 "be upgraded to use feature flags. After "
7464 "being upgraded, these pools\nwill no "
7465 "longer be accessible by software that does not "
7466 "support feature\nflags.\n\n"));
7467 (void) printf(gettext("VER POOL\n"));
7468 (void) printf(gettext("--- ------------\n"));
7469 cbp->cb_first = B_FALSE;
7472 (void) printf("%2llu %s\n", (u_longlong_t)version,
7473 zpool_get_name(zhp));
7480 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
7482 upgrade_cbdata_t *cbp = arg;
7486 config = zpool_get_config(zhp, NULL);
7487 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7490 if (version >= SPA_VERSION_FEATURES) {
7492 boolean_t poolfirst = B_TRUE;
7493 nvlist_t *enabled = zpool_get_features(zhp);
7495 for (i = 0; i < SPA_FEATURES; i++) {
7496 const char *fguid = spa_feature_table[i].fi_guid;
7497 const char *fname = spa_feature_table[i].fi_uname;
7498 if (!nvlist_exists(enabled, fguid)) {
7499 if (cbp->cb_first) {
7500 (void) printf(gettext("\nSome "
7501 "supported features are not "
7502 "enabled on the following pools. "
7503 "Once a\nfeature is enabled the "
7504 "pool may become incompatible with "
7505 "software\nthat does not support "
7507 "zpool-features(5) for "
7509 (void) printf(gettext("POOL "
7511 (void) printf(gettext("------"
7513 cbp->cb_first = B_FALSE;
7517 (void) printf(gettext("%s\n"),
7518 zpool_get_name(zhp));
7519 poolfirst = B_FALSE;
7522 (void) printf(gettext(" %s\n"), fname);
7525 * If they did "zpool upgrade -a", then we could
7526 * be doing ioctls to different pools. We need
7527 * to log this history once to each pool, and bypass
7528 * the normal history logging that happens in main().
7530 (void) zpool_log_history(g_zfs, history_str);
7531 log_history = B_FALSE;
7540 upgrade_one(zpool_handle_t *zhp, void *data)
7542 boolean_t printnl = B_FALSE;
7543 upgrade_cbdata_t *cbp = data;
7544 uint64_t cur_version;
7547 if (strcmp("log", zpool_get_name(zhp)) == 0) {
7548 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
7549 "Pool 'log' must be renamed using export and import"
7554 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7555 if (cur_version > cbp->cb_version) {
7556 (void) printf(gettext("Pool '%s' is already formatted "
7557 "using more current version '%llu'.\n\n"),
7558 zpool_get_name(zhp), (u_longlong_t)cur_version);
7562 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
7563 (void) printf(gettext("Pool '%s' is already formatted "
7564 "using version %llu.\n\n"), zpool_get_name(zhp),
7565 (u_longlong_t)cbp->cb_version);
7569 if (cur_version != cbp->cb_version) {
7571 ret = upgrade_version(zhp, cbp->cb_version);
7576 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7578 ret = upgrade_enable_all(zhp, &count);
7584 } else if (cur_version == SPA_VERSION) {
7585 (void) printf(gettext("Pool '%s' already has all "
7586 "supported features enabled.\n"),
7587 zpool_get_name(zhp));
7592 (void) printf(gettext("\n"));
7601 * zpool upgrade [-V version] <-a | pool ...>
7603 * With no arguments, display downrev'd ZFS pool available for upgrade.
7604 * Individual pools can be upgraded by specifying the pool, and '-a' will
7605 * upgrade all pools.
7608 zpool_do_upgrade(int argc, char **argv)
7611 upgrade_cbdata_t cb = { 0 };
7613 boolean_t showversions = B_FALSE;
7614 boolean_t upgradeall = B_FALSE;
7619 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7622 upgradeall = B_TRUE;
7625 showversions = B_TRUE;
7628 cb.cb_version = strtoll(optarg, &end, 10);
7630 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7631 (void) fprintf(stderr,
7632 gettext("invalid version '%s'\n"), optarg);
7637 (void) fprintf(stderr, gettext("missing argument for "
7638 "'%c' option\n"), optopt);
7642 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7653 if (cb.cb_version == 0) {
7654 cb.cb_version = SPA_VERSION;
7655 } else if (!upgradeall && argc == 0) {
7656 (void) fprintf(stderr, gettext("-V option is "
7657 "incompatible with other arguments\n"));
7662 if (upgradeall || argc != 0) {
7663 (void) fprintf(stderr, gettext("-v option is "
7664 "incompatible with other arguments\n"));
7667 } else if (upgradeall) {
7669 (void) fprintf(stderr, gettext("-a option should not "
7670 "be used along with a pool name\n"));
7675 (void) printf(gettext("This system supports ZFS pool feature "
7680 (void) printf(gettext("The following features are "
7682 (void) printf(gettext("FEAT DESCRIPTION\n"));
7683 (void) printf("----------------------------------------------"
7684 "---------------\n");
7685 for (i = 0; i < SPA_FEATURES; i++) {
7686 zfeature_info_t *fi = &spa_feature_table[i];
7688 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7689 " (read-only compatible)" : "";
7691 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7692 (void) printf(" %s\n", fi->fi_desc);
7694 (void) printf("\n");
7696 (void) printf(gettext("The following legacy versions are also "
7698 (void) printf(gettext("VER DESCRIPTION\n"));
7699 (void) printf("--- -----------------------------------------"
7700 "---------------\n");
7701 (void) printf(gettext(" 1 Initial ZFS version\n"));
7702 (void) printf(gettext(" 2 Ditto blocks "
7703 "(replicated metadata)\n"));
7704 (void) printf(gettext(" 3 Hot spares and double parity "
7706 (void) printf(gettext(" 4 zpool history\n"));
7707 (void) printf(gettext(" 5 Compression using the gzip "
7709 (void) printf(gettext(" 6 bootfs pool property\n"));
7710 (void) printf(gettext(" 7 Separate intent log devices\n"));
7711 (void) printf(gettext(" 8 Delegated administration\n"));
7712 (void) printf(gettext(" 9 refquota and refreservation "
7714 (void) printf(gettext(" 10 Cache devices\n"));
7715 (void) printf(gettext(" 11 Improved scrub performance\n"));
7716 (void) printf(gettext(" 12 Snapshot properties\n"));
7717 (void) printf(gettext(" 13 snapused property\n"));
7718 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7719 (void) printf(gettext(" 15 user/group space accounting\n"));
7720 (void) printf(gettext(" 16 stmf property support\n"));
7721 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7722 (void) printf(gettext(" 18 Snapshot user holds\n"));
7723 (void) printf(gettext(" 19 Log device removal\n"));
7724 (void) printf(gettext(" 20 Compression using zle "
7725 "(zero-length encoding)\n"));
7726 (void) printf(gettext(" 21 Deduplication\n"));
7727 (void) printf(gettext(" 22 Received properties\n"));
7728 (void) printf(gettext(" 23 Slim ZIL\n"));
7729 (void) printf(gettext(" 24 System attributes\n"));
7730 (void) printf(gettext(" 25 Improved scrub stats\n"));
7731 (void) printf(gettext(" 26 Improved snapshot deletion "
7733 (void) printf(gettext(" 27 Improved snapshot creation "
7735 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7736 (void) printf(gettext("\nFor more information on a particular "
7737 "version, including supported releases,\n"));
7738 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7739 } else if (argc == 0 && upgradeall) {
7740 cb.cb_first = B_TRUE;
7741 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7742 if (ret == 0 && cb.cb_first) {
7743 if (cb.cb_version == SPA_VERSION) {
7744 (void) printf(gettext("All pools are already "
7745 "formatted using feature flags.\n\n"));
7746 (void) printf(gettext("Every feature flags "
7747 "pool already has all supported features "
7750 (void) printf(gettext("All pools are already "
7751 "formatted with version %llu or higher.\n"),
7752 (u_longlong_t)cb.cb_version);
7755 } else if (argc == 0) {
7756 cb.cb_first = B_TRUE;
7757 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7761 (void) printf(gettext("All pools are formatted "
7762 "using feature flags.\n\n"));
7764 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7765 "for a list of available legacy versions.\n"));
7768 cb.cb_first = B_TRUE;
7769 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7773 (void) printf(gettext("Every feature flags pool has "
7774 "all supported features enabled.\n"));
7776 (void) printf(gettext("\n"));
7779 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7786 typedef struct hist_cbdata {
7793 * Print out the command history for a specific pool.
7796 get_history_one(zpool_handle_t *zhp, void *data)
7802 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7804 cb->first = B_FALSE;
7806 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7808 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7811 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7812 &records, &numrecords) == 0);
7813 for (i = 0; i < numrecords; i++) {
7814 nvlist_t *rec = records[i];
7817 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7821 tsec = fnvlist_lookup_uint64(records[i],
7823 (void) localtime_r(&tsec, &t);
7824 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7827 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7828 (void) printf("%s %s", tbuf,
7829 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7830 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7832 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7835 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7836 (void) printf("%s unrecognized record:\n",
7838 dump_nvlist(rec, 4);
7841 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7842 zfs_history_event_names[ievent],
7843 (longlong_t)fnvlist_lookup_uint64(
7844 rec, ZPOOL_HIST_TXG),
7845 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7846 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7849 (void) printf("%s [txg:%lld] %s", tbuf,
7850 (longlong_t)fnvlist_lookup_uint64(
7851 rec, ZPOOL_HIST_TXG),
7852 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7853 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7854 (void) printf(" %s (%llu)",
7855 fnvlist_lookup_string(rec,
7857 (u_longlong_t)fnvlist_lookup_uint64(rec,
7860 (void) printf(" %s", fnvlist_lookup_string(rec,
7861 ZPOOL_HIST_INT_STR));
7862 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7865 (void) printf("%s ioctl %s\n", tbuf,
7866 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7867 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7868 (void) printf(" input:\n");
7869 dump_nvlist(fnvlist_lookup_nvlist(rec,
7870 ZPOOL_HIST_INPUT_NVL), 8);
7872 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7873 (void) printf(" output:\n");
7874 dump_nvlist(fnvlist_lookup_nvlist(rec,
7875 ZPOOL_HIST_OUTPUT_NVL), 8);
7877 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
7878 (void) printf(" errno: %lld\n",
7879 (longlong_t)fnvlist_lookup_int64(rec,
7885 (void) printf("%s unrecognized record:\n", tbuf);
7886 dump_nvlist(rec, 4);
7890 (void) printf("\n");
7893 (void) printf(" [");
7894 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7895 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7896 struct passwd *pwd = getpwuid(who);
7897 (void) printf("user %d ", (int)who);
7899 (void) printf("(%s) ", pwd->pw_name);
7901 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7902 (void) printf("on %s",
7903 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7905 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7906 (void) printf(":%s",
7907 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7911 (void) printf("\n");
7913 (void) printf("\n");
7920 * zpool history <pool>
7922 * Displays the history of commands that modified pools.
7925 zpool_do_history(int argc, char **argv)
7927 hist_cbdata_t cbdata = { 0 };
7931 cbdata.first = B_TRUE;
7933 while ((c = getopt(argc, argv, "li")) != -1) {
7936 cbdata.longfmt = B_TRUE;
7939 cbdata.internal = B_TRUE;
7942 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7950 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
7953 if (argc == 0 && cbdata.first == B_TRUE) {
7954 (void) fprintf(stderr, gettext("no pools available\n"));
7961 typedef struct ev_opts {
7966 char poolname[ZFS_MAX_DATASET_NAME_LEN];
7970 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7972 char ctime_str[26], str[32], *ptr;
7976 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7977 memset(str, ' ', 32);
7978 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7979 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
7980 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
7981 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
7982 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7984 (void) printf(gettext("%s\t"), str);
7986 (void) printf(gettext("%s "), str);
7988 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7989 (void) printf(gettext("%s\n"), ptr);
7993 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7997 for (nvp = nvlist_next_nvpair(nvl, NULL);
7998 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
8000 data_type_t type = nvpair_type(nvp);
8001 const char *name = nvpair_name(nvp);
8011 printf(gettext("%*s%s = "), depth, "", name);
8014 case DATA_TYPE_BOOLEAN:
8015 printf(gettext("%s"), "1");
8018 case DATA_TYPE_BOOLEAN_VALUE:
8019 (void) nvpair_value_boolean_value(nvp, &b);
8020 printf(gettext("%s"), b ? "1" : "0");
8023 case DATA_TYPE_BYTE:
8024 (void) nvpair_value_byte(nvp, &i8);
8025 printf(gettext("0x%x"), i8);
8028 case DATA_TYPE_INT8:
8029 (void) nvpair_value_int8(nvp, (void *)&i8);
8030 printf(gettext("0x%x"), i8);
8033 case DATA_TYPE_UINT8:
8034 (void) nvpair_value_uint8(nvp, &i8);
8035 printf(gettext("0x%x"), i8);
8038 case DATA_TYPE_INT16:
8039 (void) nvpair_value_int16(nvp, (void *)&i16);
8040 printf(gettext("0x%x"), i16);
8043 case DATA_TYPE_UINT16:
8044 (void) nvpair_value_uint16(nvp, &i16);
8045 printf(gettext("0x%x"), i16);
8048 case DATA_TYPE_INT32:
8049 (void) nvpair_value_int32(nvp, (void *)&i32);
8050 printf(gettext("0x%x"), i32);
8053 case DATA_TYPE_UINT32:
8054 (void) nvpair_value_uint32(nvp, &i32);
8055 printf(gettext("0x%x"), i32);
8058 case DATA_TYPE_INT64:
8059 (void) nvpair_value_int64(nvp, (void *)&i64);
8060 printf(gettext("0x%llx"), (u_longlong_t)i64);
8063 case DATA_TYPE_UINT64:
8064 (void) nvpair_value_uint64(nvp, &i64);
8066 * translate vdev state values to readable
8067 * strings to aide zpool events consumers
8070 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
8072 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
8073 printf(gettext("\"%s\" (0x%llx)"),
8074 zpool_state_to_name(i64, VDEV_AUX_NONE),
8077 printf(gettext("0x%llx"), (u_longlong_t)i64);
8081 case DATA_TYPE_HRTIME:
8082 (void) nvpair_value_hrtime(nvp, (void *)&i64);
8083 printf(gettext("0x%llx"), (u_longlong_t)i64);
8086 case DATA_TYPE_STRING:
8087 (void) nvpair_value_string(nvp, &str);
8088 printf(gettext("\"%s\""), str ? str : "<NULL>");
8091 case DATA_TYPE_NVLIST:
8092 printf(gettext("(embedded nvlist)\n"));
8093 (void) nvpair_value_nvlist(nvp, &cnv);
8094 zpool_do_events_nvprint(cnv, depth + 8);
8095 printf(gettext("%*s(end %s)"), depth, "", name);
8098 case DATA_TYPE_NVLIST_ARRAY: {
8102 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
8103 printf(gettext("(%d embedded nvlists)\n"), nelem);
8104 for (i = 0; i < nelem; i++) {
8105 printf(gettext("%*s%s[%d] = %s\n"),
8106 depth, "", name, i, "(embedded nvlist)");
8107 zpool_do_events_nvprint(val[i], depth + 8);
8108 printf(gettext("%*s(end %s[%i])\n"),
8109 depth, "", name, i);
8111 printf(gettext("%*s(end %s)\n"), depth, "", name);
8115 case DATA_TYPE_INT8_ARRAY: {
8119 (void) nvpair_value_int8_array(nvp, &val, &nelem);
8120 for (i = 0; i < nelem; i++)
8121 printf(gettext("0x%x "), val[i]);
8126 case DATA_TYPE_UINT8_ARRAY: {
8130 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
8131 for (i = 0; i < nelem; i++)
8132 printf(gettext("0x%x "), val[i]);
8137 case DATA_TYPE_INT16_ARRAY: {
8141 (void) nvpair_value_int16_array(nvp, &val, &nelem);
8142 for (i = 0; i < nelem; i++)
8143 printf(gettext("0x%x "), val[i]);
8148 case DATA_TYPE_UINT16_ARRAY: {
8152 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
8153 for (i = 0; i < nelem; i++)
8154 printf(gettext("0x%x "), val[i]);
8159 case DATA_TYPE_INT32_ARRAY: {
8163 (void) nvpair_value_int32_array(nvp, &val, &nelem);
8164 for (i = 0; i < nelem; i++)
8165 printf(gettext("0x%x "), val[i]);
8170 case DATA_TYPE_UINT32_ARRAY: {
8174 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
8175 for (i = 0; i < nelem; i++)
8176 printf(gettext("0x%x "), val[i]);
8181 case DATA_TYPE_INT64_ARRAY: {
8185 (void) nvpair_value_int64_array(nvp, &val, &nelem);
8186 for (i = 0; i < nelem; i++)
8187 printf(gettext("0x%llx "),
8188 (u_longlong_t)val[i]);
8193 case DATA_TYPE_UINT64_ARRAY: {
8197 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
8198 for (i = 0; i < nelem; i++)
8199 printf(gettext("0x%llx "),
8200 (u_longlong_t)val[i]);
8205 case DATA_TYPE_STRING_ARRAY: {
8209 (void) nvpair_value_string_array(nvp, &str, &nelem);
8210 for (i = 0; i < nelem; i++)
8211 printf(gettext("\"%s\" "),
8212 str[i] ? str[i] : "<NULL>");
8217 case DATA_TYPE_BOOLEAN_ARRAY:
8218 case DATA_TYPE_BYTE_ARRAY:
8219 case DATA_TYPE_DOUBLE:
8220 case DATA_TYPE_DONTCARE:
8221 case DATA_TYPE_UNKNOWN:
8222 printf(gettext("<unknown>"));
8226 printf(gettext("\n"));
8231 zpool_do_events_next(ev_opts_t *opts)
8234 int zevent_fd, ret, dropped;
8237 zevent_fd = open(ZFS_DEV, O_RDWR);
8238 VERIFY(zevent_fd >= 0);
8240 if (!opts->scripted)
8241 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
8244 ret = zpool_events_next(g_zfs, &nvl, &dropped,
8245 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
8246 if (ret || nvl == NULL)
8250 (void) printf(gettext("dropped %d events\n"), dropped);
8252 if (strlen(opts->poolname) > 0 &&
8253 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
8254 strcmp(opts->poolname, pool) != 0)
8257 zpool_do_events_short(nvl, opts);
8259 if (opts->verbose) {
8260 zpool_do_events_nvprint(nvl, 8);
8261 printf(gettext("\n"));
8263 (void) fflush(stdout);
8268 VERIFY(0 == close(zevent_fd));
8274 zpool_do_events_clear(ev_opts_t *opts)
8278 ret = zpool_events_clear(g_zfs, &count);
8280 (void) printf(gettext("cleared %d events\n"), count);
8286 * zpool events [-vHf [pool] | -c]
8288 * Displays events logs by ZFS.
8291 zpool_do_events(int argc, char **argv)
8293 ev_opts_t opts = { 0 };
8298 while ((c = getopt(argc, argv, "vHfc")) != -1) {
8313 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8322 (void) fprintf(stderr, gettext("too many arguments\n"));
8324 } else if (argc == 1) {
8325 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
8326 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
8327 (void) fprintf(stderr,
8328 gettext("invalid pool name '%s'\n"), opts.poolname);
8333 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
8335 (void) fprintf(stderr,
8336 gettext("invalid options combined with -c\n"));
8341 ret = zpool_do_events_clear(&opts);
8343 ret = zpool_do_events_next(&opts);
8349 get_callback(zpool_handle_t *zhp, void *data)
8351 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
8352 char value[MAXNAMELEN];
8353 zprop_source_t srctype;
8356 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8359 * Skip the special fake placeholder. This will also skip
8360 * over the name property when 'all' is specified.
8362 if (pl->pl_prop == ZPOOL_PROP_NAME &&
8363 pl == cbp->cb_proplist)
8366 if (pl->pl_prop == ZPROP_INVAL &&
8367 (zpool_prop_feature(pl->pl_user_prop) ||
8368 zpool_prop_unsupported(pl->pl_user_prop))) {
8369 srctype = ZPROP_SRC_LOCAL;
8371 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
8372 value, sizeof (value)) == 0) {
8373 zprop_print_one_property(zpool_get_name(zhp),
8374 cbp, pl->pl_user_prop, value, srctype,
8378 if (zpool_get_prop(zhp, pl->pl_prop, value,
8379 sizeof (value), &srctype, cbp->cb_literal) != 0)
8382 zprop_print_one_property(zpool_get_name(zhp), cbp,
8383 zpool_prop_to_name(pl->pl_prop), value, srctype,
8391 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8393 * -H Scripted mode. Don't display headers, and separate properties
8395 * -o List of columns to display. Defaults to
8396 * "name,property,value,source".
8397 * -p Display values in parsable (exact) format.
8399 * Get properties of pools in the system. Output space statistics
8400 * for each one as well as other attributes.
8403 zpool_do_get(int argc, char **argv)
8405 zprop_get_cbdata_t cb = { 0 };
8406 zprop_list_t fake_name = { 0 };
8411 cb.cb_first = B_TRUE;
8414 * Set up default columns and sources.
8416 cb.cb_sources = ZPROP_SRC_ALL;
8417 cb.cb_columns[0] = GET_COL_NAME;
8418 cb.cb_columns[1] = GET_COL_PROPERTY;
8419 cb.cb_columns[2] = GET_COL_VALUE;
8420 cb.cb_columns[3] = GET_COL_SOURCE;
8421 cb.cb_type = ZFS_TYPE_POOL;
8424 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
8427 cb.cb_literal = B_TRUE;
8430 cb.cb_scripted = B_TRUE;
8433 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
8435 while (*optarg != '\0') {
8436 static char *col_subopts[] =
8437 { "name", "property", "value", "source",
8440 if (i == ZFS_GET_NCOLS) {
8441 (void) fprintf(stderr, gettext("too "
8442 "many fields given to -o "
8447 switch (getsubopt(&optarg, col_subopts,
8450 cb.cb_columns[i++] = GET_COL_NAME;
8453 cb.cb_columns[i++] = GET_COL_PROPERTY;
8456 cb.cb_columns[i++] = GET_COL_VALUE;
8459 cb.cb_columns[i++] = GET_COL_SOURCE;
8463 (void) fprintf(stderr,
8464 gettext("\"all\" conflicts "
8465 "with specific fields "
8466 "given to -o option\n"));
8469 cb.cb_columns[0] = GET_COL_NAME;
8470 cb.cb_columns[1] = GET_COL_PROPERTY;
8471 cb.cb_columns[2] = GET_COL_VALUE;
8472 cb.cb_columns[3] = GET_COL_SOURCE;
8476 (void) fprintf(stderr,
8477 gettext("invalid column name "
8484 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8494 (void) fprintf(stderr, gettext("missing property "
8499 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
8500 ZFS_TYPE_POOL) != 0)
8506 if (cb.cb_proplist != NULL) {
8507 fake_name.pl_prop = ZPOOL_PROP_NAME;
8508 fake_name.pl_width = strlen(gettext("NAME"));
8509 fake_name.pl_next = cb.cb_proplist;
8510 cb.cb_proplist = &fake_name;
8513 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
8516 if (cb.cb_proplist == &fake_name)
8517 zprop_free_list(fake_name.pl_next);
8519 zprop_free_list(cb.cb_proplist);
8524 typedef struct set_cbdata {
8527 boolean_t cb_any_successful;
8531 set_callback(zpool_handle_t *zhp, void *data)
8534 set_cbdata_t *cb = (set_cbdata_t *)data;
8536 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
8539 cb->cb_any_successful = B_TRUE;
8545 zpool_do_set(int argc, char **argv)
8547 set_cbdata_t cb = { 0 };
8550 if (argc > 1 && argv[1][0] == '-') {
8551 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8557 (void) fprintf(stderr, gettext("missing property=value "
8563 (void) fprintf(stderr, gettext("missing pool name\n"));
8568 (void) fprintf(stderr, gettext("too many pool names\n"));
8572 cb.cb_propname = argv[1];
8573 cb.cb_value = strchr(cb.cb_propname, '=');
8574 if (cb.cb_value == NULL) {
8575 (void) fprintf(stderr, gettext("missing value in "
8576 "property=value argument\n"));
8580 *(cb.cb_value) = '\0';
8583 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8590 find_command_idx(char *command, int *idx)
8594 for (i = 0; i < NCOMMAND; i++) {
8595 if (command_table[i].name == NULL)
8598 if (strcmp(command, command_table[i].name) == 0) {
8607 main(int argc, char **argv)
8614 (void) setlocale(LC_ALL, "");
8615 (void) textdomain(TEXT_DOMAIN);
8621 * Make sure the user has specified some command.
8624 (void) fprintf(stderr, gettext("missing command\n"));
8633 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8636 if ((g_zfs = libzfs_init()) == NULL) {
8637 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8641 libzfs_print_on_error(g_zfs, B_TRUE);
8643 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8646 * Many commands modify input strings for string parsing reasons.
8647 * We create a copy to protect the original argv.
8649 newargv = malloc((argc + 1) * sizeof (newargv[0]));
8650 for (i = 0; i < argc; i++)
8651 newargv[i] = strdup(argv[i]);
8652 newargv[argc] = NULL;
8655 * Run the appropriate command.
8657 if (find_command_idx(cmdname, &i) == 0) {
8658 current_command = &command_table[i];
8659 ret = command_table[i].func(argc - 1, newargv + 1);
8660 } else if (strchr(cmdname, '=')) {
8661 verify(find_command_idx("set", &i) == 0);
8662 current_command = &command_table[i];
8663 ret = command_table[i].func(argc, newargv);
8664 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8666 * 'freeze' is a vile debugging abomination, so we treat
8669 zfs_cmd_t zc = {"\0"};
8671 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8672 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8674 (void) fprintf(stderr,
8675 gettext("failed to freeze pool: %d\n"), errno);
8681 (void) fprintf(stderr, gettext("unrecognized "
8682 "command '%s'\n"), cmdname);
8687 for (i = 0; i < argc; i++)
8691 if (ret == 0 && log_history)
8692 (void) zpool_log_history(g_zfs, history_str);
8697 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8698 * for the purposes of running ::findleaks.
8700 if (getenv("ZFS_ABORT") != NULL) {
8701 (void) printf("dumping core by request\n");