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, 2015 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>.
49 #include <sys/fs/zfs.h>
51 #include <sys/fm/fs/zfs.h>
52 #include <sys/fm/util.h>
53 #include <sys/fm/protocol.h>
54 #include <sys/zfs_ioctl.h>
59 #include "zpool_util.h"
60 #include "zfs_comutil.h"
61 #include "zfeature_common.h"
63 #include "statcommon.h"
65 static int zpool_do_create(int, char **);
66 static int zpool_do_destroy(int, char **);
68 static int zpool_do_add(int, char **);
69 static int zpool_do_remove(int, char **);
70 static int zpool_do_labelclear(int, char **);
72 static int zpool_do_list(int, char **);
73 static int zpool_do_iostat(int, char **);
74 static int zpool_do_status(int, char **);
76 static int zpool_do_online(int, char **);
77 static int zpool_do_offline(int, char **);
78 static int zpool_do_clear(int, char **);
79 static int zpool_do_reopen(int, char **);
81 static int zpool_do_reguid(int, char **);
83 static int zpool_do_attach(int, char **);
84 static int zpool_do_detach(int, char **);
85 static int zpool_do_replace(int, char **);
86 static int zpool_do_split(int, char **);
88 static int zpool_do_scrub(int, char **);
90 static int zpool_do_import(int, char **);
91 static int zpool_do_export(int, char **);
93 static int zpool_do_upgrade(int, char **);
95 static int zpool_do_history(int, char **);
96 static int zpool_do_events(int, char **);
98 static int zpool_do_get(int, char **);
99 static int zpool_do_set(int, char **);
102 * These libumem hooks provide a reasonable set of defaults for the allocator's
103 * debugging facilities.
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
150 * Flags for stats to display with "zpool iostats"
158 IOS_COUNT, /* always last element */
161 /* iostat_type entries as bitmasks */
162 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
163 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
164 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
165 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
166 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
168 /* Mask of all the histo bits */
169 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
172 * Lookup table for iostat flags to nvlist names. Basically a list
173 * of all the nvlists a flag requires. Also specifies the order in
174 * which data gets printed in zpool iostat.
176 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
178 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
179 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
180 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
181 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
182 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
183 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
184 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
185 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
186 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
195 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
196 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
197 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
198 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
199 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
202 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
203 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
204 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
205 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
206 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
207 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
208 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
209 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
210 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
211 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
217 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
218 * Right now, only one histo bit is ever set at one time, so we can
219 * just do a highbit64(a)
221 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
223 typedef struct zpool_command {
225 int (*func)(int, char **);
230 * Master command table. Each ZFS command has a name, associated function, and
231 * usage message. The usage messages need to be internationalized, so we have
232 * to have a function to return the usage message based on a command index.
234 * These commands are organized according to how they are displayed in the usage
235 * message. An empty command (one with a NULL name) indicates an empty line in
236 * the generic usage message.
238 static zpool_command_t command_table[] = {
239 { "create", zpool_do_create, HELP_CREATE },
240 { "destroy", zpool_do_destroy, HELP_DESTROY },
242 { "add", zpool_do_add, HELP_ADD },
243 { "remove", zpool_do_remove, HELP_REMOVE },
245 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
247 { "list", zpool_do_list, HELP_LIST },
248 { "iostat", zpool_do_iostat, HELP_IOSTAT },
249 { "status", zpool_do_status, HELP_STATUS },
251 { "online", zpool_do_online, HELP_ONLINE },
252 { "offline", zpool_do_offline, HELP_OFFLINE },
253 { "clear", zpool_do_clear, HELP_CLEAR },
254 { "reopen", zpool_do_reopen, HELP_REOPEN },
256 { "attach", zpool_do_attach, HELP_ATTACH },
257 { "detach", zpool_do_detach, HELP_DETACH },
258 { "replace", zpool_do_replace, HELP_REPLACE },
259 { "split", zpool_do_split, HELP_SPLIT },
261 { "scrub", zpool_do_scrub, HELP_SCRUB },
263 { "import", zpool_do_import, HELP_IMPORT },
264 { "export", zpool_do_export, HELP_EXPORT },
265 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
266 { "reguid", zpool_do_reguid, HELP_REGUID },
268 { "history", zpool_do_history, HELP_HISTORY },
269 { "events", zpool_do_events, HELP_EVENTS },
271 { "get", zpool_do_get, HELP_GET },
272 { "set", zpool_do_set, HELP_SET },
275 #define NCOMMAND (ARRAY_SIZE(command_table))
277 static zpool_command_t *current_command;
278 static char history_str[HIS_MAX_RECORD_LEN];
279 static boolean_t log_history = B_TRUE;
280 static uint_t timestamp_fmt = NODATE;
283 get_usage(zpool_help_t idx)
287 return (gettext("\tadd [-fgLnP] [-o property=value] "
288 "<pool> <vdev> ...\n"));
290 return (gettext("\tattach [-f] [-o property=value] "
291 "<pool> <device> <new-device>\n"));
293 return (gettext("\tclear [-nF] <pool> [device]\n"));
295 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
296 "\t [-O file-system-property=value] ... \n"
297 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
299 return (gettext("\tdestroy [-f] <pool>\n"));
301 return (gettext("\tdetach <pool> <device>\n"));
303 return (gettext("\texport [-af] <pool> ...\n"));
305 return (gettext("\thistory [-il] [<pool>] ...\n"));
307 return (gettext("\timport [-d dir] [-D]\n"
308 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
309 "\timport [-o mntopts] [-o property=value] ... \n"
310 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
311 "[-R root] [-F [-n]] -a\n"
312 "\timport [-o mntopts] [-o property=value] ... \n"
313 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
314 "[-R root] [-F [-n]]\n"
315 "\t <pool | id> [newpool]\n"));
317 return (gettext("\tiostat [-c CMD] [-T d | u] [-ghHLpPvy] "
319 "\t [[pool ...]|[pool vdev ...]|[vdev ...]] "
320 "[interval [count]]\n"));
321 case HELP_LABELCLEAR:
322 return (gettext("\tlabelclear [-f] <vdev>\n"));
324 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
325 "[-T d|u] [pool] ... [interval [count]]\n"));
327 return (gettext("\toffline [-t] <pool> <device> ...\n"));
329 return (gettext("\tonline <pool> <device> ...\n"));
331 return (gettext("\treplace [-f] [-o property=value] "
332 "<pool> <device> [new-device]\n"));
334 return (gettext("\tremove <pool> <device> ...\n"));
336 return (gettext("\treopen <pool>\n"));
338 return (gettext("\tscrub [-s] <pool> ...\n"));
340 return (gettext("\tstatus [-c CMD] [-gLPvxD] [-T d|u] [pool]"
341 " ... [interval [count]]\n"));
343 return (gettext("\tupgrade\n"
345 "\tupgrade [-V version] <-a | pool ...>\n"));
347 return (gettext("\tevents [-vHfc]\n"));
349 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
350 "<\"all\" | property[,...]> <pool> ...\n"));
352 return (gettext("\tset <property=value> <pool> \n"));
354 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
355 "\t [-o property=value] <pool> <newpool> "
356 "[<device> ...]\n"));
358 return (gettext("\treguid <pool>\n"));
367 * Callback routine that will print out a pool property value.
370 print_prop_cb(int prop, void *cb)
374 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
376 if (zpool_prop_readonly(prop))
377 (void) fprintf(fp, " NO ");
379 (void) fprintf(fp, " YES ");
381 if (zpool_prop_values(prop) == NULL)
382 (void) fprintf(fp, "-\n");
384 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
390 * Display usage message. If we're inside a command, display only the usage for
391 * that command. Otherwise, iterate over the entire command table and display
392 * a complete usage message.
395 usage(boolean_t requested)
397 FILE *fp = requested ? stdout : stderr;
399 if (current_command == NULL) {
402 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
404 gettext("where 'command' is one of the following:\n\n"));
406 for (i = 0; i < NCOMMAND; i++) {
407 if (command_table[i].name == NULL)
408 (void) fprintf(fp, "\n");
410 (void) fprintf(fp, "%s",
411 get_usage(command_table[i].usage));
414 (void) fprintf(fp, gettext("usage:\n"));
415 (void) fprintf(fp, "%s", get_usage(current_command->usage));
418 if (current_command != NULL &&
419 ((strcmp(current_command->name, "set") == 0) ||
420 (strcmp(current_command->name, "get") == 0) ||
421 (strcmp(current_command->name, "list") == 0))) {
424 gettext("\nthe following properties are supported:\n"));
426 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
427 "PROPERTY", "EDIT", "VALUES");
429 /* Iterate over all properties */
430 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
433 (void) fprintf(fp, "\t%-15s ", "feature@...");
434 (void) fprintf(fp, "YES disabled | enabled | active\n");
436 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
437 "appended with a feature name.\nSee zpool-features(5).\n"));
441 * See comments at end of main().
443 if (getenv("ZFS_ABORT") != NULL) {
444 (void) printf("dumping core by request\n");
448 exit(requested ? 0 : 2);
452 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
453 boolean_t print_logs, int name_flags)
460 (void) printf("\t%*s%s\n", indent, "", name);
462 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
463 &child, &children) != 0)
466 for (c = 0; c < children; c++) {
467 uint64_t is_log = B_FALSE;
469 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
471 if ((is_log && !print_logs) || (!is_log && print_logs))
474 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
475 print_vdev_tree(zhp, vname, child[c], indent + 2,
476 B_FALSE, name_flags);
482 prop_list_contains_feature(nvlist_t *proplist)
485 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
486 nvp = nvlist_next_nvpair(proplist, nvp)) {
487 if (zpool_prop_feature(nvpair_name(nvp)))
494 * Add a property pair (name, string-value) into a property nvlist.
497 add_prop_list(const char *propname, char *propval, nvlist_t **props,
500 zpool_prop_t prop = ZPROP_INVAL;
506 if (*props == NULL &&
507 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
508 (void) fprintf(stderr,
509 gettext("internal error: out of memory\n"));
516 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
518 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
519 !zpool_prop_feature(propname)) {
520 (void) fprintf(stderr, gettext("property '%s' is "
521 "not a valid pool property\n"), propname);
526 * feature@ properties and version should not be specified
529 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
530 nvlist_exists(proplist, vname)) ||
531 (prop == ZPOOL_PROP_VERSION &&
532 prop_list_contains_feature(proplist))) {
533 (void) fprintf(stderr, gettext("'feature@' and "
534 "'version' properties cannot be specified "
540 if (zpool_prop_feature(propname))
543 normnm = zpool_prop_to_name(prop);
545 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
546 normnm = zfs_prop_to_name(fprop);
552 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
553 prop != ZPOOL_PROP_CACHEFILE) {
554 (void) fprintf(stderr, gettext("property '%s' "
555 "specified multiple times\n"), propname);
559 if (nvlist_add_string(proplist, normnm, propval) != 0) {
560 (void) fprintf(stderr, gettext("internal "
561 "error: out of memory\n"));
569 * Set a default property pair (name, string-value) in a property nvlist
572 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
577 if (nvlist_lookup_string(*props, propname, &pval) == 0)
580 return (add_prop_list(propname, propval, props, B_TRUE));
584 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
586 * -f Force addition of devices, even if they appear in use
587 * -g Display guid for individual vdev name.
588 * -L Follow links when resolving vdev path name.
589 * -n Do not add the devices, but display the resulting layout if
590 * they were to be added.
591 * -o Set property=value.
592 * -P Display full path for vdev name.
594 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
595 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
599 zpool_do_add(int argc, char **argv)
601 boolean_t force = B_FALSE;
602 boolean_t dryrun = B_FALSE;
610 nvlist_t *props = NULL;
614 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
620 name_flags |= VDEV_NAME_GUID;
623 name_flags |= VDEV_NAME_FOLLOW_LINKS;
629 if ((propval = strchr(optarg, '=')) == NULL) {
630 (void) fprintf(stderr, gettext("missing "
631 "'=' for -o option\n"));
637 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
638 (add_prop_list(optarg, propval, &props, B_TRUE)))
642 name_flags |= VDEV_NAME_PATH;
645 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
654 /* get pool name and check number of arguments */
656 (void) fprintf(stderr, gettext("missing pool name argument\n"));
660 (void) fprintf(stderr, gettext("missing vdev specification\n"));
669 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
672 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
673 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
679 /* pass off to get_vdev_spec for processing */
680 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
682 if (nvroot == NULL) {
688 nvlist_t *poolnvroot;
690 uint_t l2children, c;
692 boolean_t hadcache = B_FALSE;
694 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
697 (void) printf(gettext("would update '%s' to the following "
698 "configuration:\n"), zpool_get_name(zhp));
700 /* print original main pool and new tree */
701 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
703 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
705 /* Do the same for the logs */
706 if (num_logs(poolnvroot) > 0) {
707 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
709 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
711 } else if (num_logs(nvroot) > 0) {
712 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
716 /* Do the same for the caches */
717 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
718 &l2child, &l2children) == 0 && l2children) {
720 (void) printf(gettext("\tcache\n"));
721 for (c = 0; c < l2children; c++) {
722 vname = zpool_vdev_name(g_zfs, NULL,
723 l2child[c], name_flags);
724 (void) printf("\t %s\n", vname);
728 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
729 &l2child, &l2children) == 0 && l2children) {
731 (void) printf(gettext("\tcache\n"));
732 for (c = 0; c < l2children; c++) {
733 vname = zpool_vdev_name(g_zfs, NULL,
734 l2child[c], name_flags);
735 (void) printf("\t %s\n", vname);
742 ret = (zpool_add(zhp, nvroot) != 0);
753 * zpool remove <pool> <vdev> ...
755 * Removes the given vdev from the pool. Currently, this supports removing
756 * spares, cache, and log devices from the pool.
759 zpool_do_remove(int argc, char **argv)
763 zpool_handle_t *zhp = NULL;
768 /* get pool name and check number of arguments */
770 (void) fprintf(stderr, gettext("missing pool name argument\n"));
774 (void) fprintf(stderr, gettext("missing device\n"));
780 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
783 for (i = 1; i < argc; i++) {
784 if (zpool_vdev_remove(zhp, argv[i]) != 0)
793 * zpool labelclear <vdev>
795 * Verifies that the vdev is not active and zeros out the label information
799 zpool_do_labelclear(int argc, char **argv)
802 int c, fd = -1, ret = 0;
804 boolean_t inuse = B_FALSE;
805 boolean_t force = B_FALSE;
808 while ((c = getopt(argc, argv, "f")) != -1) {
814 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
825 (void) fprintf(stderr, gettext("missing vdev device name\n"));
830 if ((fd = open(vdev, O_RDWR)) < 0) {
831 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
836 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
840 (void) fprintf(stderr,
841 gettext("Unable to determine pool state for %s\n"
842 "Use -f to force the clearing any label data\n"), vdev);
850 case POOL_STATE_ACTIVE:
851 case POOL_STATE_SPARE:
852 case POOL_STATE_L2CACHE:
853 (void) fprintf(stderr,
854 gettext("labelclear operation failed.\n"
855 "\tVdev %s is a member (%s), of pool \"%s\".\n"
856 "\tTo remove label information from this device, "
857 "export or destroy\n\tthe pool, or remove %s from "
858 "the configuration of this pool\n\tand retry the "
859 "labelclear operation.\n"),
860 vdev, zpool_pool_state_to_name(state), name, vdev);
864 case POOL_STATE_EXPORTED:
868 (void) fprintf(stderr,
869 gettext("labelclear operation failed.\n\tVdev "
870 "%s is a member of the exported pool \"%s\".\n"
871 "\tUse \"zpool labelclear -f %s\" to force the "
872 "removal of label\n\tinformation.\n"),
877 case POOL_STATE_POTENTIALLY_ACTIVE:
881 (void) fprintf(stderr,
882 gettext("labelclear operation failed.\n"
883 "\tVdev %s is a member of the pool \"%s\".\n"
884 "\tThis pool is unknown to this system, but may "
885 "be active on\n\tanother system. Use "
886 "\'zpool labelclear -f %s\' to force the\n"
887 "\tremoval of label information.\n"),
892 case POOL_STATE_DESTROYED:
893 /* inuse should never be set for a destroyed pool... */
899 if (zpool_clear_label(fd) != 0) {
900 (void) fprintf(stderr,
901 gettext("Label clear failed on vdev %s\n"), vdev);
914 * zpool create [-fnd] [-o property=value] ...
915 * [-O file-system-property=value] ...
916 * [-R root] [-m mountpoint] <pool> <dev> ...
918 * -f Force creation, even if devices appear in use
919 * -n Do not create the pool, but display the resulting layout if it
920 * were to be created.
921 * -R Create a pool under an alternate root
922 * -m Set default mountpoint for the root dataset. By default it's
924 * -o Set property=value.
925 * -o Set feature@feature=enabled|disabled.
926 * -d Don't automatically enable all supported pool features
927 * (individual features can be enabled with -o).
928 * -O Set fsproperty=value in the pool's root file system
930 * Creates the named pool according to the given vdev specification. The
931 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
932 * we get the nvlist back from get_vdev_spec(), we either print out the contents
933 * (if '-n' was specified), or pass it to libzfs to do the creation.
936 zpool_do_create(int argc, char **argv)
938 boolean_t force = B_FALSE;
939 boolean_t dryrun = B_FALSE;
940 boolean_t enable_all_pool_feat = B_TRUE;
942 nvlist_t *nvroot = NULL;
946 char *altroot = NULL;
947 char *mountpoint = NULL;
948 nvlist_t *fsprops = NULL;
949 nvlist_t *props = NULL;
953 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
962 enable_all_pool_feat = B_FALSE;
966 if (add_prop_list(zpool_prop_to_name(
967 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
969 if (add_prop_list_default(zpool_prop_to_name(
970 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
974 /* Equivalent to -O mountpoint=optarg */
978 if ((propval = strchr(optarg, '=')) == NULL) {
979 (void) fprintf(stderr, gettext("missing "
980 "'=' for -o option\n"));
986 if (add_prop_list(optarg, propval, &props, B_TRUE))
990 * If the user is creating a pool that doesn't support
991 * feature flags, don't enable any features.
993 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
997 ver = strtoull(propval, &end, 10);
999 ver < SPA_VERSION_FEATURES) {
1000 enable_all_pool_feat = B_FALSE;
1003 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1007 if ((propval = strchr(optarg, '=')) == NULL) {
1008 (void) fprintf(stderr, gettext("missing "
1009 "'=' for -O option\n"));
1016 * Mountpoints are checked and then added later.
1017 * Uniquely among properties, they can be specified
1018 * more than once, to avoid conflict with -m.
1020 if (0 == strcmp(optarg,
1021 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1022 mountpoint = propval;
1023 } else if (add_prop_list(optarg, propval, &fsprops,
1030 * Sanity check temporary pool name.
1032 if (strchr(optarg, '/') != NULL) {
1033 (void) fprintf(stderr, gettext("cannot create "
1034 "'%s': invalid character '/' in temporary "
1036 (void) fprintf(stderr, gettext("use 'zfs "
1037 "create' to create a dataset\n"));
1041 if (add_prop_list(zpool_prop_to_name(
1042 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1044 if (add_prop_list_default(zpool_prop_to_name(
1045 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1050 (void) fprintf(stderr, gettext("missing argument for "
1051 "'%c' option\n"), optopt);
1054 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1063 /* get pool name and check number of arguments */
1065 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1069 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1076 * As a special case, check for use of '/' in the name, and direct the
1077 * user to use 'zfs create' instead.
1079 if (strchr(poolname, '/') != NULL) {
1080 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1081 "character '/' in pool name\n"), poolname);
1082 (void) fprintf(stderr, gettext("use 'zfs create' to "
1083 "create a dataset\n"));
1087 /* pass off to get_vdev_spec for bulk processing */
1088 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1089 argc - 1, argv + 1);
1093 /* make_root_vdev() allows 0 toplevel children if there are spares */
1094 if (!zfs_allocatable_devs(nvroot)) {
1095 (void) fprintf(stderr, gettext("invalid vdev "
1096 "specification: at least one toplevel vdev must be "
1101 if (altroot != NULL && altroot[0] != '/') {
1102 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1103 "must be an absolute path\n"), altroot);
1108 * Check the validity of the mountpoint and direct the user to use the
1109 * '-m' mountpoint option if it looks like its in use.
1111 if (mountpoint == NULL ||
1112 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1113 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1114 char buf[MAXPATHLEN];
1117 if (mountpoint && mountpoint[0] != '/') {
1118 (void) fprintf(stderr, gettext("invalid mountpoint "
1119 "'%s': must be an absolute path, 'legacy', or "
1120 "'none'\n"), mountpoint);
1124 if (mountpoint == NULL) {
1125 if (altroot != NULL)
1126 (void) snprintf(buf, sizeof (buf), "%s/%s",
1129 (void) snprintf(buf, sizeof (buf), "/%s",
1132 if (altroot != NULL)
1133 (void) snprintf(buf, sizeof (buf), "%s%s",
1134 altroot, mountpoint);
1136 (void) snprintf(buf, sizeof (buf), "%s",
1140 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1141 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1142 "%s\n"), buf, strerror(errno));
1143 (void) fprintf(stderr, gettext("use '-m' "
1144 "option to provide a different default\n"));
1149 while (count < 3 && readdir(dirp) != NULL)
1151 (void) closedir(dirp);
1154 (void) fprintf(stderr, gettext("mountpoint "
1155 "'%s' exists and is not empty\n"), buf);
1156 (void) fprintf(stderr, gettext("use '-m' "
1157 "option to provide a "
1158 "different default\n"));
1165 * Now that the mountpoint's validity has been checked, ensure that
1166 * the property is set appropriately prior to creating the pool.
1168 if (mountpoint != NULL) {
1169 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1170 mountpoint, &fsprops, B_FALSE);
1178 * For a dry run invocation, print out a basic message and run
1179 * through all the vdevs in the list and print out in an
1180 * appropriate hierarchy.
1182 (void) printf(gettext("would create '%s' with the "
1183 "following layout:\n\n"), poolname);
1185 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1186 if (num_logs(nvroot) > 0)
1187 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1192 * Hand off to libzfs.
1195 for (i = 0; i < SPA_FEATURES; i++) {
1196 char propname[MAXPATHLEN];
1198 zfeature_info_t *feat = &spa_feature_table[i];
1200 (void) snprintf(propname, sizeof (propname),
1201 "feature@%s", feat->fi_uname);
1204 * Only features contained in props will be enabled:
1205 * remove from the nvlist every ZFS_FEATURE_DISABLED
1206 * value and add every missing ZFS_FEATURE_ENABLED if
1207 * enable_all_pool_feat is set.
1209 if (!nvlist_lookup_string(props, propname, &propval)) {
1210 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1211 (void) nvlist_remove_all(props,
1213 } else if (enable_all_pool_feat) {
1214 ret = add_prop_list(propname,
1215 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1222 if (zpool_create(g_zfs, poolname,
1223 nvroot, props, fsprops) == 0) {
1224 zfs_handle_t *pool = zfs_open(g_zfs,
1225 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1227 if (zfs_mount(pool, NULL, 0) == 0)
1228 ret = zfs_shareall(pool);
1231 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1232 (void) fprintf(stderr, gettext("pool name may have "
1238 nvlist_free(nvroot);
1239 nvlist_free(fsprops);
1243 nvlist_free(fsprops);
1250 * zpool destroy <pool>
1252 * -f Forcefully unmount any datasets
1254 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1257 zpool_do_destroy(int argc, char **argv)
1259 boolean_t force = B_FALSE;
1262 zpool_handle_t *zhp;
1266 while ((c = getopt(argc, argv, "f")) != -1) {
1272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1281 /* check arguments */
1283 (void) fprintf(stderr, gettext("missing pool argument\n"));
1287 (void) fprintf(stderr, gettext("too many arguments\n"));
1293 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1295 * As a special case, check for use of '/' in the name, and
1296 * direct the user to use 'zfs destroy' instead.
1298 if (strchr(pool, '/') != NULL)
1299 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1300 "destroy a dataset\n"));
1304 if (zpool_disable_datasets(zhp, force) != 0) {
1305 (void) fprintf(stderr, gettext("could not destroy '%s': "
1306 "could not unmount datasets\n"), zpool_get_name(zhp));
1311 /* The history must be logged as part of the export */
1312 log_history = B_FALSE;
1314 ret = (zpool_destroy(zhp, history_str) != 0);
1321 typedef struct export_cbdata {
1323 boolean_t hardforce;
1330 zpool_export_one(zpool_handle_t *zhp, void *data)
1332 export_cbdata_t *cb = data;
1334 if (zpool_disable_datasets(zhp, cb->force) != 0)
1337 /* The history must be logged as part of the export */
1338 log_history = B_FALSE;
1340 if (cb->hardforce) {
1341 if (zpool_export_force(zhp, history_str) != 0)
1343 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1351 * zpool export [-f] <pool> ...
1353 * -a Export all pools
1354 * -f Forcefully unmount datasets
1356 * Export the given pools. By default, the command will attempt to cleanly
1357 * unmount any active datasets within the pool. If the '-f' flag is specified,
1358 * then the datasets will be forcefully unmounted.
1361 zpool_do_export(int argc, char **argv)
1364 boolean_t do_all = B_FALSE;
1365 boolean_t force = B_FALSE;
1366 boolean_t hardforce = B_FALSE;
1370 while ((c = getopt(argc, argv, "afF")) != -1) {
1382 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1389 cb.hardforce = hardforce;
1395 (void) fprintf(stderr, gettext("too many arguments\n"));
1399 return (for_each_pool(argc, argv, B_TRUE, NULL,
1400 zpool_export_one, &cb));
1403 /* check arguments */
1405 (void) fprintf(stderr, gettext("missing pool argument\n"));
1409 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1415 * Given a vdev configuration, determine the maximum width needed for the device
1419 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1427 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1428 if (strlen(name) + depth > max)
1429 max = strlen(name) + depth;
1433 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1434 &child, &children) == 0) {
1435 for (c = 0; c < children; c++)
1436 if ((ret = max_width(zhp, child[c], depth + 2,
1437 max, name_flags)) > max)
1441 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1442 &child, &children) == 0) {
1443 for (c = 0; c < children; c++)
1444 if ((ret = max_width(zhp, child[c], depth + 2,
1445 max, name_flags)) > max)
1449 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1450 &child, &children) == 0) {
1451 for (c = 0; c < children; c++)
1452 if ((ret = max_width(zhp, child[c], depth + 2,
1453 max, name_flags)) > max)
1460 typedef struct spare_cbdata {
1462 zpool_handle_t *cb_zhp;
1466 find_vdev(nvlist_t *nv, uint64_t search)
1472 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1476 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1477 &child, &children) == 0) {
1478 for (c = 0; c < children; c++)
1479 if (find_vdev(child[c], search))
1487 find_spare(zpool_handle_t *zhp, void *data)
1489 spare_cbdata_t *cbp = data;
1490 nvlist_t *config, *nvroot;
1492 config = zpool_get_config(zhp, NULL);
1493 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1496 if (find_vdev(nvroot, cbp->cb_guid)) {
1505 typedef struct status_cbdata {
1509 boolean_t cb_allpools;
1510 boolean_t cb_verbose;
1511 boolean_t cb_explain;
1513 boolean_t cb_dedup_stats;
1514 boolean_t cb_print_status;
1515 vdev_cmd_data_list_t *vcdl;
1518 /* Print output line for specific vdev in a specific pool */
1520 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1523 for (i = 0; i < vcdl->count; i++) {
1524 if ((strcmp(vcdl->data[i].path, path) == 0) &&
1525 (strcmp(vcdl->data[i].pool, pool) == 0)) {
1526 printf("%s", vcdl->data[i].line);
1533 * Print out configuration state as requested by status_callback.
1536 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1537 nvlist_t *nv, int depth, boolean_t isspare)
1541 pool_scan_stat_t *ps = NULL;
1543 char rbuf[6], wbuf[6], cbuf[6];
1545 uint64_t notpresent;
1546 spare_cbdata_t spare_cb;
1550 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1551 &child, &children) != 0)
1554 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1555 (uint64_t **)&vs, &c) == 0);
1557 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1560 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1563 if (vs->vs_aux == VDEV_AUX_SPARED)
1565 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1569 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1573 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1574 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1575 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1576 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1579 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1580 ¬present) == 0) {
1581 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1582 (void) printf(" was %s", path);
1583 } else if (vs->vs_aux != 0) {
1586 switch (vs->vs_aux) {
1587 case VDEV_AUX_OPEN_FAILED:
1588 (void) printf(gettext("cannot open"));
1591 case VDEV_AUX_BAD_GUID_SUM:
1592 (void) printf(gettext("missing device"));
1595 case VDEV_AUX_NO_REPLICAS:
1596 (void) printf(gettext("insufficient replicas"));
1599 case VDEV_AUX_VERSION_NEWER:
1600 (void) printf(gettext("newer version"));
1603 case VDEV_AUX_UNSUP_FEAT:
1604 (void) printf(gettext("unsupported feature(s)"));
1607 case VDEV_AUX_SPARED:
1608 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1609 &spare_cb.cb_guid) == 0);
1610 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1611 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1612 zpool_get_name(zhp)) == 0)
1613 (void) printf(gettext("currently in "
1616 (void) printf(gettext("in use by "
1618 zpool_get_name(spare_cb.cb_zhp));
1619 zpool_close(spare_cb.cb_zhp);
1621 (void) printf(gettext("currently in use"));
1625 case VDEV_AUX_ERR_EXCEEDED:
1626 (void) printf(gettext("too many errors"));
1629 case VDEV_AUX_IO_FAILURE:
1630 (void) printf(gettext("experienced I/O failures"));
1633 case VDEV_AUX_BAD_LOG:
1634 (void) printf(gettext("bad intent log"));
1637 case VDEV_AUX_EXTERNAL:
1638 (void) printf(gettext("external device fault"));
1641 case VDEV_AUX_SPLIT_POOL:
1642 (void) printf(gettext("split into new pool"));
1646 (void) printf(gettext("corrupted data"));
1651 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1652 (uint64_t **)&ps, &c);
1654 if (ps && ps->pss_state == DSS_SCANNING &&
1655 vs->vs_scan_processed != 0 && children == 0) {
1656 (void) printf(gettext(" (%s)"),
1657 (ps->pss_func == POOL_SCAN_RESILVER) ?
1658 "resilvering" : "repairing");
1661 if (cb->vcdl != NULL) {
1662 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1664 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1668 (void) printf("\n");
1670 for (c = 0; c < children; c++) {
1671 uint64_t islog = B_FALSE, ishole = B_FALSE;
1673 /* Don't print logs or holes here */
1674 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1676 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1678 if (islog || ishole)
1680 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1681 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1682 print_status_config(zhp, cb, vname, child[c], depth + 2,
1689 * Print the configuration of an exported pool. Iterate over all vdevs in the
1690 * pool, printing out the name and status for each one.
1693 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1701 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1702 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1703 strcmp(type, VDEV_TYPE_HOLE) == 0)
1706 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1707 (uint64_t **)&vs, &c) == 0);
1709 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1710 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1712 if (vs->vs_aux != 0) {
1715 switch (vs->vs_aux) {
1716 case VDEV_AUX_OPEN_FAILED:
1717 (void) printf(gettext("cannot open"));
1720 case VDEV_AUX_BAD_GUID_SUM:
1721 (void) printf(gettext("missing device"));
1724 case VDEV_AUX_NO_REPLICAS:
1725 (void) printf(gettext("insufficient replicas"));
1728 case VDEV_AUX_VERSION_NEWER:
1729 (void) printf(gettext("newer version"));
1732 case VDEV_AUX_UNSUP_FEAT:
1733 (void) printf(gettext("unsupported feature(s)"));
1736 case VDEV_AUX_ERR_EXCEEDED:
1737 (void) printf(gettext("too many errors"));
1741 (void) printf(gettext("corrupted data"));
1745 (void) printf("\n");
1747 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1748 &child, &children) != 0)
1751 for (c = 0; c < children; c++) {
1752 uint64_t is_log = B_FALSE;
1754 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1759 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1760 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1761 print_import_config(cb, vname, child[c], depth + 2);
1765 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1766 &child, &children) == 0) {
1767 (void) printf(gettext("\tcache\n"));
1768 for (c = 0; c < children; c++) {
1769 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1771 (void) printf("\t %s\n", vname);
1776 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1777 &child, &children) == 0) {
1778 (void) printf(gettext("\tspares\n"));
1779 for (c = 0; c < children; c++) {
1780 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1782 (void) printf("\t %s\n", vname);
1790 * Logs are recorded as top level vdevs in the main pool child array
1791 * but with "is_log" set to 1. We use either print_status_config() or
1792 * print_import_config() to print the top level logs then any log
1793 * children (eg mirrored slogs) are printed recursively - which
1794 * works because only the top level vdev is marked "is_log"
1797 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1802 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1806 (void) printf(gettext("\tlogs\n"));
1808 for (c = 0; c < children; c++) {
1809 uint64_t is_log = B_FALSE;
1812 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1816 name = zpool_vdev_name(g_zfs, zhp, child[c],
1817 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1818 if (cb->cb_print_status)
1819 print_status_config(zhp, cb, name, child[c], 2,
1822 print_import_config(cb, name, child[c], 2);
1828 * Display the status for the given pool.
1831 show_import(nvlist_t *config)
1833 uint64_t pool_state;
1839 zpool_status_t reason;
1840 zpool_errata_t errata;
1844 status_cbdata_t cb = { 0 };
1846 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1848 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1850 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1852 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1855 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1856 (uint64_t **)&vs, &vsc) == 0);
1857 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1859 reason = zpool_import_status(config, &msgid, &errata);
1861 (void) printf(gettext(" pool: %s\n"), name);
1862 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1863 (void) printf(gettext(" state: %s"), health);
1864 if (pool_state == POOL_STATE_DESTROYED)
1865 (void) printf(gettext(" (DESTROYED)"));
1866 (void) printf("\n");
1869 case ZPOOL_STATUS_MISSING_DEV_R:
1870 case ZPOOL_STATUS_MISSING_DEV_NR:
1871 case ZPOOL_STATUS_BAD_GUID_SUM:
1872 (void) printf(gettext(" status: One or more devices are "
1873 "missing from the system.\n"));
1876 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1877 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1878 (void) printf(gettext(" status: One or more devices contains "
1879 "corrupted data.\n"));
1882 case ZPOOL_STATUS_CORRUPT_DATA:
1884 gettext(" status: The pool data is corrupted.\n"));
1887 case ZPOOL_STATUS_OFFLINE_DEV:
1888 (void) printf(gettext(" status: One or more devices "
1889 "are offlined.\n"));
1892 case ZPOOL_STATUS_CORRUPT_POOL:
1893 (void) printf(gettext(" status: The pool metadata is "
1897 case ZPOOL_STATUS_VERSION_OLDER:
1898 (void) printf(gettext(" status: The pool is formatted using a "
1899 "legacy on-disk version.\n"));
1902 case ZPOOL_STATUS_VERSION_NEWER:
1903 (void) printf(gettext(" status: The pool is formatted using an "
1904 "incompatible version.\n"));
1907 case ZPOOL_STATUS_FEAT_DISABLED:
1908 (void) printf(gettext(" status: Some supported features are "
1909 "not enabled on the pool.\n"));
1912 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1913 (void) printf(gettext("status: The pool uses the following "
1914 "feature(s) not supported on this system:\n"));
1915 zpool_print_unsup_feat(config);
1918 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1919 (void) printf(gettext("status: The pool can only be accessed "
1920 "in read-only mode on this system. It\n\tcannot be "
1921 "accessed in read-write mode because it uses the "
1922 "following\n\tfeature(s) not supported on this system:\n"));
1923 zpool_print_unsup_feat(config);
1926 case ZPOOL_STATUS_HOSTID_MISMATCH:
1927 (void) printf(gettext(" status: The pool was last accessed by "
1928 "another system.\n"));
1931 case ZPOOL_STATUS_FAULTED_DEV_R:
1932 case ZPOOL_STATUS_FAULTED_DEV_NR:
1933 (void) printf(gettext(" status: One or more devices are "
1937 case ZPOOL_STATUS_BAD_LOG:
1938 (void) printf(gettext(" status: An intent log record cannot be "
1942 case ZPOOL_STATUS_RESILVERING:
1943 (void) printf(gettext(" status: One or more devices were being "
1947 case ZPOOL_STATUS_ERRATA:
1948 (void) printf(gettext(" status: Errata #%d detected.\n"),
1954 * No other status can be seen when importing pools.
1956 assert(reason == ZPOOL_STATUS_OK);
1960 * Print out an action according to the overall state of the pool.
1962 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1963 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1964 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1965 (void) printf(gettext(" action: The pool can be "
1966 "imported using its name or numeric identifier, "
1967 "though\n\tsome features will not be available "
1968 "without an explicit 'zpool upgrade'.\n"));
1969 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1970 (void) printf(gettext(" action: The pool can be "
1971 "imported using its name or numeric "
1972 "identifier and\n\tthe '-f' flag.\n"));
1973 } else if (reason == ZPOOL_STATUS_ERRATA) {
1975 case ZPOOL_ERRATA_NONE:
1978 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1979 (void) printf(gettext(" action: The pool can "
1980 "be imported using its name or numeric "
1981 "identifier,\n\thowever there is a compat"
1982 "ibility issue which should be corrected"
1983 "\n\tby running 'zpool scrub'\n"));
1986 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1987 (void) printf(gettext(" action: The pool can"
1988 "not be imported with this version of ZFS "
1989 "due to\n\tan active asynchronous destroy. "
1990 "Revert to an earlier version\n\tand "
1991 "allow the destroy to complete before "
1997 * All errata must contain an action message.
2002 (void) printf(gettext(" action: The pool can be "
2003 "imported using its name or numeric "
2006 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2007 (void) printf(gettext(" action: The pool can be imported "
2008 "despite missing or damaged devices. The\n\tfault "
2009 "tolerance of the pool may be compromised if imported.\n"));
2012 case ZPOOL_STATUS_VERSION_NEWER:
2013 (void) printf(gettext(" action: The pool cannot be "
2014 "imported. Access the pool on a system running "
2015 "newer\n\tsoftware, or recreate the pool from "
2018 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2019 (void) printf(gettext("action: The pool cannot be "
2020 "imported. Access the pool on a system that "
2021 "supports\n\tthe required feature(s), or recreate "
2022 "the pool from backup.\n"));
2024 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2025 (void) printf(gettext("action: The pool cannot be "
2026 "imported in read-write mode. Import the pool "
2028 "\t\"-o readonly=on\", access the pool on a system "
2029 "that supports the\n\trequired feature(s), or "
2030 "recreate the pool from backup.\n"));
2032 case ZPOOL_STATUS_MISSING_DEV_R:
2033 case ZPOOL_STATUS_MISSING_DEV_NR:
2034 case ZPOOL_STATUS_BAD_GUID_SUM:
2035 (void) printf(gettext(" action: The pool cannot be "
2036 "imported. Attach the missing\n\tdevices and try "
2040 (void) printf(gettext(" action: The pool cannot be "
2041 "imported due to damaged devices or data.\n"));
2045 /* Print the comment attached to the pool. */
2046 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2047 (void) printf(gettext("comment: %s\n"), comment);
2050 * If the state is "closed" or "can't open", and the aux state
2051 * is "corrupt data":
2053 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2054 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2055 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2056 if (pool_state == POOL_STATE_DESTROYED)
2057 (void) printf(gettext("\tThe pool was destroyed, "
2058 "but can be imported using the '-Df' flags.\n"));
2059 else if (pool_state != POOL_STATE_EXPORTED)
2060 (void) printf(gettext("\tThe pool may be active on "
2061 "another system, but can be imported using\n\t"
2062 "the '-f' flag.\n"));
2066 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2069 (void) printf(gettext(" config:\n\n"));
2071 cb.cb_namewidth = max_width(NULL, nvroot, 0, 0, VDEV_NAME_TYPE_ID);
2072 if (cb.cb_namewidth < 10)
2073 cb.cb_namewidth = 10;
2075 print_import_config(&cb, name, nvroot, 0);
2076 if (num_logs(nvroot) > 0)
2077 print_logs(NULL, &cb, nvroot);
2079 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2080 (void) printf(gettext("\n\tAdditional devices are known to "
2081 "be part of this pool, though their\n\texact "
2082 "configuration cannot be determined.\n"));
2087 * Perform the import for the given configuration. This passes the heavy
2088 * lifting off to zpool_import_props(), and then mounts the datasets contained
2092 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2093 nvlist_t *props, int flags)
2095 zpool_handle_t *zhp;
2100 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2103 verify(nvlist_lookup_uint64(config,
2104 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2105 verify(nvlist_lookup_uint64(config,
2106 ZPOOL_CONFIG_VERSION, &version) == 0);
2107 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2108 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2109 "is formatted using an unsupported ZFS version\n"), name);
2111 } else if (state != POOL_STATE_EXPORTED &&
2112 !(flags & ZFS_IMPORT_ANY_HOST)) {
2113 uint64_t hostid = 0;
2114 unsigned long system_hostid = get_system_hostid();
2116 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
2119 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
2124 verify(nvlist_lookup_string(config,
2125 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2126 verify(nvlist_lookup_uint64(config,
2127 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
2129 (void) fprintf(stderr, gettext("cannot import "
2130 "'%s': pool may be in use from other "
2131 "system, it was last accessed by %s "
2132 "(hostid: 0x%lx) on %s"), name, hostname,
2133 (unsigned long)hostid,
2134 asctime(localtime(&t)));
2135 (void) fprintf(stderr, gettext("use '-f' to "
2136 "import anyway\n"));
2141 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2144 if (newname != NULL)
2145 name = (char *)newname;
2147 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2150 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2151 !(flags & ZFS_IMPORT_ONLY) &&
2152 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2162 * zpool import [-d dir] [-D]
2163 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2164 * [-d dir | -c cachefile] [-f] -a
2165 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2166 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2168 * -c Read pool information from a cachefile instead of searching
2171 * -d Scan in a specific directory, other than /dev/. More than
2172 * one directory can be specified using multiple '-d' options.
2174 * -D Scan for previously destroyed pools or import all or only
2175 * specified destroyed pools.
2177 * -R Temporarily import the pool, with all mountpoints relative to
2178 * the given root. The pool will remain exported when the machine
2181 * -V Import even in the presence of faulted vdevs. This is an
2182 * intentionally undocumented option for testing purposes, and
2183 * treats the pool configuration as complete, leaving any bad
2184 * vdevs in the FAULTED state. In other words, it does verbatim
2187 * -f Force import, even if it appears that the pool is active.
2189 * -F Attempt rewind if necessary.
2191 * -n See if rewind would work, but don't actually rewind.
2193 * -N Import the pool but don't mount datasets.
2195 * -T Specify a starting txg to use for import. This option is
2196 * intentionally undocumented option for testing purposes.
2198 * -a Import all pools found.
2200 * -o Set property=value and/or temporary mount options (without '=').
2202 * -s Scan using the default search path, the libblkid cache will
2205 * The import command scans for pools to import, and import pools based on pool
2206 * name and GUID. The pool can also be renamed as part of the import process.
2209 zpool_do_import(int argc, char **argv)
2211 char **searchdirs = NULL;
2212 char *env, *envdup = NULL;
2216 nvlist_t *pools = NULL;
2217 boolean_t do_all = B_FALSE;
2218 boolean_t do_destroyed = B_FALSE;
2219 char *mntopts = NULL;
2222 uint64_t searchguid = 0;
2223 char *searchname = NULL;
2225 nvlist_t *found_config;
2226 nvlist_t *policy = NULL;
2227 nvlist_t *props = NULL;
2229 int flags = ZFS_IMPORT_NORMAL;
2230 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2231 boolean_t dryrun = B_FALSE;
2232 boolean_t do_rewind = B_FALSE;
2233 boolean_t xtreme_rewind = B_FALSE;
2234 boolean_t do_scan = B_FALSE;
2235 uint64_t pool_state, txg = -1ULL;
2236 char *cachefile = NULL;
2237 importargs_t idata = { 0 };
2241 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2250 if (searchdirs == NULL) {
2251 searchdirs = safe_malloc(sizeof (char *));
2253 char **tmp = safe_malloc((nsearch + 1) *
2255 bcopy(searchdirs, tmp, nsearch *
2260 searchdirs[nsearch++] = optarg;
2263 do_destroyed = B_TRUE;
2266 flags |= ZFS_IMPORT_ANY_HOST;
2272 flags |= ZFS_IMPORT_MISSING_LOG;
2278 flags |= ZFS_IMPORT_ONLY;
2281 if ((propval = strchr(optarg, '=')) != NULL) {
2284 if (add_prop_list(optarg, propval,
2292 if (add_prop_list(zpool_prop_to_name(
2293 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2295 if (add_prop_list_default(zpool_prop_to_name(
2296 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2303 flags |= ZFS_IMPORT_TEMP_NAME;
2304 if (add_prop_list_default(zpool_prop_to_name(
2305 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2311 txg = strtoull(optarg, &endptr, 0);
2312 if (errno != 0 || *endptr != '\0') {
2313 (void) fprintf(stderr,
2314 gettext("invalid txg value\n"));
2317 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2320 flags |= ZFS_IMPORT_VERBATIM;
2323 xtreme_rewind = B_TRUE;
2326 (void) fprintf(stderr, gettext("missing argument for "
2327 "'%c' option\n"), optopt);
2331 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2340 if (cachefile && nsearch != 0) {
2341 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2345 if ((dryrun || xtreme_rewind) && !do_rewind) {
2346 (void) fprintf(stderr,
2347 gettext("-n or -X only meaningful with -F\n"));
2351 rewind_policy = ZPOOL_TRY_REWIND;
2353 rewind_policy = ZPOOL_DO_REWIND;
2355 rewind_policy |= ZPOOL_EXTREME_REWIND;
2357 /* In the future, we can capture further policy and include it here */
2358 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2359 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2360 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2363 /* check argument count */
2366 (void) fprintf(stderr, gettext("too many arguments\n"));
2371 (void) fprintf(stderr, gettext("too many arguments\n"));
2377 * Check for the effective uid. We do this explicitly here because
2378 * otherwise any attempt to discover pools will silently fail.
2380 if (argc == 0 && geteuid() != 0) {
2381 (void) fprintf(stderr, gettext("cannot "
2382 "discover pools: permission denied\n"));
2383 if (searchdirs != NULL)
2387 nvlist_free(policy);
2392 * Depending on the arguments given, we do one of the following:
2394 * <none> Iterate through all pools and display information about
2397 * -a Iterate through all pools and try to import each one.
2399 * <id> Find the pool that corresponds to the given GUID/pool
2400 * name and import that one.
2402 * -D Above options applies only to destroyed pools.
2408 searchguid = strtoull(argv[0], &endptr, 10);
2409 if (errno != 0 || *endptr != '\0') {
2410 searchname = argv[0];
2413 found_config = NULL;
2416 * User specified a name or guid. Ensure it's unique.
2418 idata.unique = B_TRUE;
2422 * Check the environment for the preferred search path.
2424 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2427 envdup = strdup(env);
2429 dir = strtok(envdup, ":");
2430 while (dir != NULL) {
2431 if (searchdirs == NULL) {
2432 searchdirs = safe_malloc(sizeof (char *));
2434 char **tmp = safe_malloc((nsearch + 1) *
2436 bcopy(searchdirs, tmp, nsearch *
2441 searchdirs[nsearch++] = dir;
2442 dir = strtok(NULL, ":");
2446 idata.path = searchdirs;
2447 idata.paths = nsearch;
2448 idata.poolname = searchname;
2449 idata.guid = searchguid;
2450 idata.cachefile = cachefile;
2451 idata.scan = do_scan;
2454 * Under Linux the zpool_find_import_impl() function leverages the
2455 * taskq implementation to parallelize device scanning. It is
2456 * therefore necessary to initialize this functionality for the
2457 * duration of the zpool_search_import() function.
2460 pools = zpool_search_import(g_zfs, &idata);
2463 if (pools != NULL && idata.exists &&
2464 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2465 (void) fprintf(stderr, gettext("cannot import '%s': "
2466 "a pool with that name already exists\n"),
2468 (void) fprintf(stderr, gettext("use the form '%s "
2469 "<pool | id> <newpool>' to give it a new name\n"),
2472 } else if (pools == NULL && idata.exists) {
2473 (void) fprintf(stderr, gettext("cannot import '%s': "
2474 "a pool with that name is already created/imported,\n"),
2476 (void) fprintf(stderr, gettext("and no additional pools "
2477 "with that name were found\n"));
2479 } else if (pools == NULL) {
2481 (void) fprintf(stderr, gettext("cannot import '%s': "
2482 "no such pool available\n"), argv[0]);
2488 if (searchdirs != NULL)
2492 nvlist_free(policy);
2499 * At this point we have a list of import candidate configs. Even if
2500 * we were searching by pool name or guid, we still need to
2501 * post-process the list to deal with pool state and possible
2507 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2509 verify(nvpair_value_nvlist(elem, &config) == 0);
2511 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2513 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2515 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2518 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2525 (void) printf("\n");
2528 err |= do_import(config, NULL, mntopts,
2531 show_import(config);
2533 } else if (searchname != NULL) {
2537 * We are searching for a pool based on name.
2539 verify(nvlist_lookup_string(config,
2540 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2542 if (strcmp(name, searchname) == 0) {
2543 if (found_config != NULL) {
2544 (void) fprintf(stderr, gettext(
2545 "cannot import '%s': more than "
2546 "one matching pool\n"), searchname);
2547 (void) fprintf(stderr, gettext(
2548 "import by numeric ID instead\n"));
2551 found_config = config;
2557 * Search for a pool by guid.
2559 verify(nvlist_lookup_uint64(config,
2560 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2562 if (guid == searchguid)
2563 found_config = config;
2568 * If we were searching for a specific pool, verify that we found a
2569 * pool, and then do the import.
2571 if (argc != 0 && err == 0) {
2572 if (found_config == NULL) {
2573 (void) fprintf(stderr, gettext("cannot import '%s': "
2574 "no such pool available\n"), argv[0]);
2577 err |= do_import(found_config, argc == 1 ? NULL :
2578 argv[1], mntopts, props, flags);
2583 * If we were just looking for pools, report an error if none were
2586 if (argc == 0 && first)
2587 (void) fprintf(stderr,
2588 gettext("no pools available to import\n"));
2593 nvlist_free(policy);
2594 if (searchdirs != NULL)
2599 return (err ? 1 : 0);
2602 typedef struct iostat_cbdata {
2607 char **cb_vdev_names; /* Only show these vdevs */
2608 unsigned int cb_vdev_names_count;
2609 boolean_t cb_verbose;
2610 boolean_t cb_literal;
2611 boolean_t cb_scripted;
2612 zpool_list_t *cb_list;
2613 vdev_cmd_data_list_t *vcdl;
2617 typedef struct name_and_columns {
2618 const char *name; /* Column name */
2619 unsigned int columns; /* Center name to this number of columns */
2620 } name_and_columns_t;
2622 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2624 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2626 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2628 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2629 {"asyncq_wait", 2}, {"scrub"}},
2630 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2631 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2633 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2634 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2635 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2636 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2640 /* Shorthand - if "columns" field not set, default to 1 column */
2641 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2643 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2645 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2646 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2647 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2648 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2649 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2650 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2651 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2652 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2655 static const char *histo_to_title[] = {
2656 [IOS_L_HISTO] = "latency",
2657 [IOS_RQ_HISTO] = "req_size",
2661 * Return the number of labels in a null-terminated name_and_columns_t
2666 label_array_len(const name_and_columns_t *labels)
2670 while (labels[i].name)
2677 * Return the number of strings in a null-terminated string array.
2680 * const char foo[] = {"bar", "baz", NULL}
2685 str_array_len(const char *array[])
2696 * Return a default column width for default/latency/queue columns. This does
2697 * not include histograms, which have their columns autosized.
2700 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2702 unsigned long column_width = 5; /* Normal niceprint */
2703 static unsigned long widths[] = {
2705 * Choose some sane default column sizes for printing the
2708 [IOS_DEFAULT] = 15, /* 1PB capacity */
2709 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2710 [IOS_QUEUES] = 6, /* 1M queue entries */
2714 column_width = widths[type];
2716 return (column_width);
2720 * Print the column labels, i.e:
2722 * capacity operations bandwidth
2723 * alloc free read write read write ...
2725 * If force_column_width is set, use it for the column width. If not set, use
2726 * the default column width.
2729 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2730 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2733 unsigned int text_start, rw_column_width, spaces_to_end;
2734 uint64_t flags = cb->cb_flags;
2736 unsigned int column_width = force_column_width;
2738 /* For each bit set in flags */
2739 for (f = flags; f; f &= ~(1ULL << idx)) {
2740 idx = lowbit64(f) - 1;
2741 if (!force_column_width)
2742 column_width = default_column_width(cb, idx);
2743 /* Print our top labels centered over "read write" label. */
2744 for (i = 0; i < label_array_len(labels[idx]); i++) {
2745 const char *name = labels[idx][i].name;
2747 * We treat labels[][].columns == 0 as shorthand
2748 * for one column. It makes writing out the label
2749 * tables more concise.
2751 unsigned int columns = MAX(1, labels[idx][i].columns);
2752 unsigned int slen = strlen(name);
2754 rw_column_width = (column_width * columns) +
2755 (2 * (columns - 1));
2757 text_start = (int)((rw_column_width)/columns -
2760 printf(" "); /* Two spaces between columns */
2762 /* Space from beginning of column to label */
2763 for (s = 0; s < text_start; s++)
2768 /* Print space after label to end of column */
2769 spaces_to_end = rw_column_width - text_start - slen;
2770 for (s = 0; s < spaces_to_end; s++)
2779 * Utility function to print out a line of dashes like:
2781 * -------------------------------- ----- ----- ----- ----- -----
2783 * ...or a dashed named-row line like:
2789 * @force_column_width If non-zero, use the value as the column width.
2790 * Otherwise use the default column widths.
2792 * @name: Print a dashed named-row line starting
2793 * with @name. Otherwise, print a regular
2797 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
2801 unsigned int namewidth;
2802 uint64_t flags = cb->cb_flags;
2805 const name_and_columns_t *labels;
2809 if (cb->cb_flags & IOS_ANYHISTO_M) {
2810 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2811 } else if (cb->cb_vdev_names_count) {
2817 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2818 name ? strlen(name) : 0);
2822 printf("%-*s", namewidth, name);
2824 for (i = 0; i < namewidth; i++)
2828 /* For each bit in flags */
2829 for (f = flags; f; f &= ~(1ULL << idx)) {
2830 unsigned int column_width;
2831 idx = lowbit64(f) - 1;
2832 if (force_column_width)
2833 column_width = force_column_width;
2835 column_width = default_column_width(cb, idx);
2837 labels = iostat_bottom_labels[idx];
2838 for (i = 0; i < label_array_len(labels); i++) {
2840 printf(" %*s-", column_width - 1, " ");
2842 printf(" %.*s", column_width,
2843 "--------------------");
2851 print_iostat_separator_impl(iostat_cbdata_t *cb,
2852 unsigned int force_column_width)
2854 print_iostat_dashes(cb, force_column_width, NULL);
2858 print_iostat_separator(iostat_cbdata_t *cb)
2860 print_iostat_separator_impl(cb, 0);
2864 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
2865 const char *histo_vdev_name)
2867 unsigned int namewidth;
2870 if (cb->cb_flags & IOS_ANYHISTO_M) {
2871 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2872 } else if (cb->cb_vdev_names_count) {
2878 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2879 histo_vdev_name ? strlen(histo_vdev_name) : 0);
2881 if (histo_vdev_name)
2882 printf("%-*s", namewidth, histo_vdev_name);
2884 printf("%*s", namewidth, "");
2887 print_iostat_labels(cb, force_column_width, iostat_top_labels);
2889 printf("%-*s", namewidth, title);
2891 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
2893 print_iostat_separator_impl(cb, force_column_width);
2897 print_iostat_header(iostat_cbdata_t *cb)
2899 print_iostat_header_impl(cb, 0, NULL);
2904 * Display a single statistic.
2907 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
2908 unsigned int column_size, boolean_t scripted)
2912 zfs_nicenum_format(value, buf, sizeof (buf), format);
2915 printf("\t%s", buf);
2917 printf(" %*s", column_size, buf);
2921 * Calculate the default vdev stats
2923 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
2924 * stats into calcvs.
2927 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
2928 vdev_stat_t *calcvs)
2932 memcpy(calcvs, newvs, sizeof (*calcvs));
2933 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
2934 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
2936 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
2937 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
2941 * Internal representation of the extended iostats data.
2943 * The extended iostat stats are exported in nvlists as either uint64_t arrays
2944 * or single uint64_t's. We make both look like arrays to make them easier
2945 * to process. In order to make single uint64_t's look like arrays, we set
2946 * __data to the stat data, and then set *data = &__data with count = 1. Then,
2947 * we can just use *data and count.
2951 uint_t count; /* Number of entries in data[] */
2952 uint64_t __data; /* Only used when data is a single uint64_t */
2956 stat_histo_max(struct stat_array *nva, unsigned int len) {
2959 for (i = 0; i < len; i++)
2960 max = MAX(max, array64_max(nva[i].data, nva[i].count));
2966 * Helper function to lookup a uint64_t array or uint64_t value and store its
2967 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
2968 * it look like a one element array to make it easier to process.
2971 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
2972 struct stat_array *nva) {
2976 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
2977 switch (nvpair_type(tmp)) {
2978 case DATA_TYPE_UINT64_ARRAY:
2979 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
2981 case DATA_TYPE_UINT64:
2982 ret = nvpair_value_uint64(tmp, &nva->__data);
2983 nva->data = &nva->__data;
2987 /* Not a uint64_t */
2996 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
2997 * subtract them, and return the results in a newly allocated stat_array.
2998 * You must free the returned array after you are done with it with
2999 * free_calc_stats().
3001 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3004 static struct stat_array *
3005 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3008 nvlist_t *oldnvx = NULL, *newnvx;
3009 struct stat_array *oldnva, *newnva, *calcnva;
3011 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3013 /* Extract our extended stats nvlist from the main list */
3014 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3017 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3021 newnva = safe_malloc(alloc_size);
3022 oldnva = safe_malloc(alloc_size);
3023 calcnva = safe_malloc(alloc_size);
3025 for (j = 0; j < len; j++) {
3026 verify(nvpair64_to_stat_array(newnvx, names[j],
3028 calcnva[j].count = newnva[j].count;
3029 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3030 calcnva[j].data = safe_malloc(alloc_size);
3031 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3034 verify(nvpair64_to_stat_array(oldnvx, names[j],
3036 for (i = 0; i < oldnva[j].count; i++)
3037 calcnva[j].data[i] -= oldnva[j].data[i];
3046 free_calc_stats(struct stat_array *nva, unsigned int len)
3049 for (i = 0; i < len; i++)
3056 print_iostat_histo(struct stat_array *nva, unsigned int len,
3057 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3063 enum zfs_nicenum_format format;
3064 unsigned int buckets;
3065 unsigned int start_bucket;
3068 format = ZFS_NICENUM_RAW;
3070 format = ZFS_NICENUM_1024;
3072 /* All these histos are the same size, so just use nva[0].count */
3073 buckets = nva[0].count;
3075 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3076 /* Start at 512 - req size should never be lower than this */
3082 for (j = start_bucket; j < buckets; j++) {
3083 /* Print histogram bucket label */
3084 if (cb->cb_flags & IOS_L_HISTO_M) {
3085 /* Ending range of this bucket */
3086 val = (1UL << (j + 1)) - 1;
3087 zfs_nicetime(val, buf, sizeof (buf));
3089 /* Request size (starting range of bucket) */
3091 zfs_nicenum(val, buf, sizeof (buf));
3094 if (cb->cb_scripted)
3095 printf("%llu", (u_longlong_t)val);
3097 printf("%-*s", namewidth, buf);
3099 /* Print the values on the line */
3100 for (i = 0; i < len; i++) {
3101 print_one_stat(nva[i].data[j] * scale, format,
3102 column_width, cb->cb_scripted);
3109 print_solid_separator(unsigned int length)
3117 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3118 nvlist_t *newnv, double scale, const char *name)
3120 unsigned int column_width;
3121 unsigned int namewidth;
3122 unsigned int entire_width;
3123 enum iostat_type type;
3124 struct stat_array *nva;
3126 unsigned int names_len;
3128 /* What type of histo are we? */
3129 type = IOS_HISTO_IDX(cb->cb_flags);
3131 /* Get NULL-terminated array of nvlist names for our histo */
3132 names = vsx_type_to_nvlist[type];
3133 names_len = str_array_len(names); /* num of names */
3135 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3137 if (cb->cb_literal) {
3138 column_width = MAX(5,
3139 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3144 namewidth = MAX(cb->cb_namewidth,
3145 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3148 * Calculate the entire line width of what we're printing. The
3149 * +2 is for the two spaces between columns:
3153 /* |___| <---------- column_width */
3155 /* |__________| <--- entire_width */
3157 entire_width = namewidth + (column_width + 2) *
3158 label_array_len(iostat_bottom_labels[type]);
3160 if (cb->cb_scripted)
3161 printf("%s\n", name);
3163 print_iostat_header_impl(cb, column_width, name);
3165 print_iostat_histo(nva, names_len, cb, column_width,
3168 free_calc_stats(nva, names_len);
3169 if (!cb->cb_scripted)
3170 print_solid_separator(entire_width);
3174 * Calculate the average latency of a power-of-two latency histogram
3177 single_histo_average(uint64_t *histo, unsigned int buckets)
3180 uint64_t count = 0, total = 0;
3182 for (i = 0; i < buckets; i++) {
3184 * Our buckets are power-of-two latency ranges. Use the
3185 * midpoint latency of each bucket to calculate the average.
3193 if (histo[i] != 0) {
3194 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3199 /* Prevent divide by zero */
3200 return (count == 0 ? 0 : total / count);
3204 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3205 nvlist_t *newnv, double scale)
3209 const char *names[] = {
3210 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3211 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3212 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3213 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3214 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3215 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3216 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3217 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3218 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3219 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3222 struct stat_array *nva;
3224 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3225 enum zfs_nicenum_format format;
3227 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3230 format = ZFS_NICENUM_RAW;
3232 format = ZFS_NICENUM_1024;
3234 for (i = 0; i < ARRAY_SIZE(names); i++) {
3235 val = nva[i].data[0] * scale;
3236 print_one_stat(val, format, column_width, cb->cb_scripted);
3239 free_calc_stats(nva, ARRAY_SIZE(names));
3243 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3244 nvlist_t *newnv, double scale)
3248 const char *names[] = {
3249 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3250 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3251 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3252 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3253 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3254 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3255 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3256 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3257 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3259 struct stat_array *nva;
3261 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3262 enum zfs_nicenum_format format;
3264 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3267 format = ZFS_NICENUM_RAW;
3269 format = ZFS_NICENUM_TIME;
3271 /* Print our avg latencies on the line */
3272 for (i = 0; i < ARRAY_SIZE(names); i++) {
3273 /* Compute average latency for a latency histo */
3274 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3275 print_one_stat(val, format, column_width, cb->cb_scripted);
3277 free_calc_stats(nva, ARRAY_SIZE(names));
3281 * Print default statistics (capacity/operations/bandwidth)
3284 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3286 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3287 enum zfs_nicenum_format format;
3288 char na; /* char to print for "not applicable" values */
3290 if (cb->cb_literal) {
3291 format = ZFS_NICENUM_RAW;
3294 format = ZFS_NICENUM_1024;
3298 /* only toplevel vdevs have capacity stats */
3299 if (vs->vs_space == 0) {
3300 if (cb->cb_scripted)
3301 printf("\t%c\t%c", na, na);
3303 printf(" %*c %*c", column_width, na, column_width,
3306 print_one_stat(vs->vs_alloc, format, column_width,
3308 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3309 column_width, cb->cb_scripted);
3312 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3313 format, column_width, cb->cb_scripted);
3314 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3315 format, column_width, cb->cb_scripted);
3316 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3317 format, column_width, cb->cb_scripted);
3318 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3319 format, column_width, cb->cb_scripted);
3323 * Print out all the statistics for the given vdev. This can either be the
3324 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3325 * is a verbose output, and we don't want to display the toplevel pool stats.
3327 * Returns the number of stat lines printed.
3330 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3331 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3333 nvlist_t **oldchild, **newchild;
3335 vdev_stat_t *oldvs, *newvs, *calcvs;
3336 vdev_stat_t zerovs = { 0 };
3343 calcvs = safe_malloc(sizeof (*calcvs));
3345 if (oldnv != NULL) {
3346 verify(nvlist_lookup_uint64_array(oldnv,
3347 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3352 /* Do we only want to see a specific vdev? */
3353 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3354 /* Yes we do. Is this the vdev? */
3355 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3357 * This is our vdev. Since it is the only vdev we
3358 * will be displaying, make depth = 0 so that it
3359 * doesn't get indented.
3366 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3367 /* Couldn't match the name */
3372 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3373 (uint64_t **)&newvs, &c) == 0);
3376 * Print the vdev name unless it's is a histogram. Histograms
3377 * display the vdev name in the header itself.
3379 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3380 if (cb->cb_scripted) {
3383 if (strlen(name) + depth > cb->cb_namewidth)
3384 (void) printf("%*s%s", depth, "", name);
3386 (void) printf("%*s%s%*s", depth, "", name,
3387 (int)(cb->cb_namewidth - strlen(name) -
3392 /* Calculate our scaling factor */
3393 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3394 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3396 * If we specify printing histograms with no time interval, then
3397 * print the histogram numbers over the entire lifetime of the
3405 scale = (double)NANOSEC / tdelta;
3408 if (cb->cb_flags & IOS_DEFAULT_M) {
3409 calc_default_iostats(oldvs, newvs, calcvs);
3410 print_iostat_default(calcvs, cb, scale);
3412 if (cb->cb_flags & IOS_LATENCY_M)
3413 print_iostat_latency(cb, oldnv, newnv, scale);
3414 if (cb->cb_flags & IOS_QUEUES_M)
3415 print_iostat_queues(cb, oldnv, newnv, scale);
3416 if (cb->cb_flags & IOS_ANYHISTO_M) {
3418 print_iostat_histos(cb, oldnv, newnv, scale, name);
3421 if (cb->vcdl != NULL) {
3423 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3425 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3427 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3428 if (cb->cb_flags & IOS_ANYHISTO_M)
3433 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3442 if (!cb->cb_verbose)
3445 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3446 &newchild, &children) != 0)
3449 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3450 &oldchild, &c) != 0)
3453 for (c = 0; c < children; c++) {
3454 uint64_t ishole = B_FALSE, islog = B_FALSE;
3456 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3459 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3462 if (ishole || islog)
3465 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3467 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3468 newchild[c], cb, depth + 2);
3473 * Log device section
3476 if (num_logs(newnv) > 0) {
3477 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3478 !cb->cb_vdev_names) {
3479 print_iostat_dashes(cb, 0, "logs");
3482 for (c = 0; c < children; c++) {
3483 uint64_t islog = B_FALSE;
3484 (void) nvlist_lookup_uint64(newchild[c],
3485 ZPOOL_CONFIG_IS_LOG, &islog);
3488 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3490 ret += print_vdev_stats(zhp, vname, oldnv ?
3491 oldchild[c] : NULL, newchild[c],
3500 * Include level 2 ARC devices in iostat output
3502 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3503 &newchild, &children) != 0)
3506 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3507 &oldchild, &c) != 0)
3511 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3512 !cb->cb_vdev_names) {
3513 print_iostat_dashes(cb, 0, "cache");
3516 for (c = 0; c < children; c++) {
3517 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3519 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3520 : NULL, newchild[c], cb, depth + 2);
3529 refresh_iostat(zpool_handle_t *zhp, void *data)
3531 iostat_cbdata_t *cb = data;
3535 * If the pool has disappeared, remove it from the list and continue.
3537 if (zpool_refresh_stats(zhp, &missing) != 0)
3541 pool_list_remove(cb->cb_list, zhp);
3547 * Callback to print out the iostats for the given pool.
3550 print_iostat(zpool_handle_t *zhp, void *data)
3552 iostat_cbdata_t *cb = data;
3553 nvlist_t *oldconfig, *newconfig;
3554 nvlist_t *oldnvroot, *newnvroot;
3557 newconfig = zpool_get_config(zhp, &oldconfig);
3559 if (cb->cb_iteration == 1)
3562 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3565 if (oldconfig == NULL)
3568 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3571 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3573 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3574 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3575 print_iostat_separator(cb);
3588 if (isatty(STDOUT_FILENO)) {
3589 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3591 columns = ws.ws_col;
3600 get_namewidth(zpool_handle_t *zhp, void *data)
3602 iostat_cbdata_t *cb = data;
3603 nvlist_t *config, *nvroot;
3606 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3607 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3609 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3610 if (!cb->cb_verbose)
3611 cb->cb_namewidth = poolname_len;
3613 cb->cb_namewidth = MAX(poolname_len,
3614 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3615 cb->cb_name_flags));
3618 * The width must be at least 10, but may be as large as the
3619 * column width - 42 so that we can still fit in one line.
3621 columns = get_columns();
3623 if (cb->cb_namewidth < 10)
3624 cb->cb_namewidth = 10;
3625 if (cb->cb_namewidth > columns - 42)
3626 cb->cb_namewidth = columns - 42;
3632 * Parse the input string, get the 'interval' and 'count' value if there is one.
3635 get_interval_count(int *argcp, char **argv, float *iv,
3639 unsigned long count = 0;
3643 * Determine if the last argument is an integer or a pool name
3645 if (argc > 0 && isnumber(argv[argc - 1])) {
3649 interval = strtof(argv[argc - 1], &end);
3651 if (*end == '\0' && errno == 0) {
3652 if (interval == 0) {
3653 (void) fprintf(stderr, gettext("interval "
3654 "cannot be zero\n"));
3658 * Ignore the last parameter
3663 * If this is not a valid number, just plow on. The
3664 * user will get a more informative error message later
3672 * If the last argument is also an integer, then we have both a count
3675 if (argc > 0 && isnumber(argv[argc - 1])) {
3680 interval = strtof(argv[argc - 1], &end);
3682 if (*end == '\0' && errno == 0) {
3683 if (interval == 0) {
3684 (void) fprintf(stderr, gettext("interval "
3685 "cannot be zero\n"));
3690 * Ignore the last parameter
3704 get_timestamp_arg(char c)
3707 timestamp_fmt = UDATE;
3709 timestamp_fmt = DDATE;
3715 * Return stat flags that are supported by all pools by both the module and
3716 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3717 * It will get ANDed down until only the flags that are supported on all pools
3721 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
3723 uint64_t *mask = data;
3724 nvlist_t *config, *nvroot, *nvx;
3728 config = zpool_get_config(zhp, NULL);
3729 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3732 /* Default stats are always supported, but for completeness.. */
3733 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
3734 flags |= IOS_DEFAULT_M;
3736 /* Get our extended stats nvlist from the main list */
3737 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
3740 * No extended stats; they're probably running an older
3741 * module. No big deal, we support that too.
3746 /* For each extended stat, make sure all its nvpairs are supported */
3747 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
3748 if (!vsx_type_to_nvlist[j][0])
3751 /* Start off by assuming the flag is supported, then check */
3752 flags |= (1ULL << j);
3753 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
3754 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
3755 /* flag isn't supported */
3756 flags = flags & ~(1ULL << j);
3762 *mask = *mask & flags;
3767 * Return a bitmask of stats that are supported on all pools by both the module
3771 get_stat_flags(zpool_list_t *list)
3776 * get_stat_flags_cb() will lop off bits from "mask" until only the
3777 * flags that are supported on all pools remain.
3779 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
3784 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3787 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
3789 iostat_cbdata_t *cb = cb_data;
3793 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
3795 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
3796 ret = 1; /* match */
3803 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3806 is_vdev(zpool_handle_t *zhp, void *cb_data)
3808 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
3812 * Check if vdevs are in a pool
3814 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3815 * return 0. If pool_name is NULL, then search all pools.
3818 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
3819 iostat_cbdata_t *cb)
3826 if ((argc == 0) || !*argv)
3832 /* Temporarily hijack cb_vdev_names for a second... */
3833 tmp_name = cb->cb_vdev_names;
3835 /* Go though our list of prospective vdev names */
3836 for (i = 0; i < argc; i++) {
3837 cb->cb_vdev_names = argv + i;
3839 /* Is this name a vdev in our pools? */
3840 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
3848 cb->cb_vdev_names = tmp_name;
3854 is_pool_cb(zpool_handle_t *zhp, void *data)
3857 if (strcmp(name, zpool_get_name(zhp)) == 0)
3864 * Do we have a pool named *name? If so, return 1, otherwise 0.
3869 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
3872 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
3874 are_all_pools(int argc, char **argv) {
3875 if ((argc == 0) || !*argv)
3879 if (!is_pool(argv[argc]))
3886 * Helper function to print out vdev/pool names we can't resolve. Used for an
3890 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
3891 iostat_cbdata_t *cb)
3896 for (i = 0; i < argc; i++) {
3900 str = gettext("pool");
3901 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
3902 str = gettext("vdev in this pool");
3903 else if (are_vdevs_in_pool(1, &name, NULL, cb))
3904 str = gettext("vdev in another pool");
3906 str = gettext("unknown");
3908 fprintf(stderr, "\t%s (%s)\n", name, str);
3913 * Same as get_interval_count(), but with additional checks to not misinterpret
3914 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
3918 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
3919 unsigned long *count, iostat_cbdata_t *cb)
3921 char **tmpargv = argv;
3922 int argc_for_interval = 0;
3924 /* Is the last arg an interval value? Or a guid? */
3925 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
3927 * The last arg is not a guid, so it's probably an
3930 argc_for_interval++;
3933 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
3935 * The 2nd to last arg is not a guid, so it's probably
3936 * an interval value.
3938 argc_for_interval++;
3942 /* Point to our list of possible intervals */
3943 tmpargv = &argv[*argc - argc_for_interval];
3945 *argc = *argc - argc_for_interval;
3946 get_interval_count(&argc_for_interval, tmpargv,
3951 * Floating point sleep(). Allows you to pass in a floating point value for
3956 struct timespec req;
3957 req.tv_sec = floor(sec);
3958 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
3959 nanosleep(&req, NULL);
3964 * zpool iostat [-c CMD] [-ghHLpPvy] [[-lq]|[-r|-w]] [-n name] [-T d|u]
3965 * [[ pool ...]|[pool vdev ...]|[vdev ...]]
3966 * [interval [count]]
3968 * -c CMD For each vdev, run command CMD
3969 * -g Display guid for individual vdev name.
3970 * -L Follow links when resolving vdev path name.
3971 * -P Display full path for vdev name.
3972 * -v Display statistics for individual vdevs
3974 * -p Display values in parsable (exact) format.
3975 * -H Scripted mode. Don't display headers, and separate properties
3977 * -l Display average latency
3978 * -q Display queue depths
3979 * -w Display latency histograms
3980 * -r Display request size histogram
3981 * -T Display a timestamp in date(1) or Unix format
3983 * This command can be tricky because we want to be able to deal with pool
3984 * creation/destruction as well as vdev configuration changes. The bulk of this
3985 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3986 * on pool_list_update() to detect the addition of new pools. Configuration
3987 * changes are all handled within libzfs.
3990 zpool_do_iostat(int argc, char **argv)
3996 unsigned long count = 0;
3998 boolean_t verbose = B_FALSE;
3999 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4000 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4001 boolean_t omit_since_boot = B_FALSE;
4002 boolean_t guid = B_FALSE;
4003 boolean_t follow_links = B_FALSE;
4004 boolean_t full_name = B_FALSE;
4005 iostat_cbdata_t cb = { 0 };
4008 /* Used for printing error message */
4009 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4010 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4012 uint64_t unsupported_flags;
4015 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4024 follow_links = B_TRUE;
4030 get_timestamp_arg(*optarg);
4054 omit_since_boot = B_TRUE;
4060 if (optopt == 'c') {
4062 gettext("Missing CMD for -c\n"));
4065 gettext("invalid option '%c'\n"), optopt);
4074 cb.cb_literal = parsable;
4075 cb.cb_scripted = scripted;
4078 cb.cb_name_flags |= VDEV_NAME_GUID;
4080 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4082 cb.cb_name_flags |= VDEV_NAME_PATH;
4083 cb.cb_iteration = 0;
4084 cb.cb_namewidth = 0;
4085 cb.cb_verbose = verbose;
4087 /* Get our interval and count values (if any) */
4089 get_interval_count_filter_guids(&argc, argv, &interval,
4092 get_interval_count(&argc, argv, &interval, &count);
4096 /* No args, so just print the defaults. */
4097 } else if (are_all_pools(argc, argv)) {
4098 /* All the args are pool names */
4099 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4100 /* All the args are vdevs */
4101 cb.cb_vdev_names = argv;
4102 cb.cb_vdev_names_count = argc;
4103 argc = 0; /* No pools to process */
4104 } else if (are_all_pools(1, argv)) {
4105 /* The first arg is a pool name */
4106 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4107 /* ...and the rest are vdev names */
4108 cb.cb_vdev_names = argv + 1;
4109 cb.cb_vdev_names_count = argc - 1;
4110 argc = 1; /* One pool to process */
4112 fprintf(stderr, gettext("Expected either a list of "));
4113 fprintf(stderr, gettext("pools, or list of vdevs in"));
4114 fprintf(stderr, " \"%s\", ", argv[0]);
4115 fprintf(stderr, gettext("but got:\n"));
4116 error_list_unresolved_vdevs(argc - 1, argv + 1,
4118 fprintf(stderr, "\n");
4124 * The args don't make sense. The first arg isn't a pool name,
4125 * nor are all the args vdevs.
4127 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4128 fprintf(stderr, "\n");
4132 if (cb.cb_vdev_names_count != 0) {
4134 * If user specified vdevs, it implies verbose.
4136 cb.cb_verbose = B_TRUE;
4140 * Construct the list of all interesting pools.
4143 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4146 if (pool_list_count(list) == 0 && argc != 0) {
4147 pool_list_free(list);
4151 if (pool_list_count(list) == 0 && interval == 0) {
4152 pool_list_free(list);
4153 (void) fprintf(stderr, gettext("no pools available\n"));
4157 if ((l_histo || rq_histo) && (queues || latency)) {
4158 pool_list_free(list);
4159 (void) fprintf(stderr,
4160 gettext("[-r|-w] isn't allowed with [-q|-l]\n"));
4165 if (l_histo && rq_histo) {
4166 pool_list_free(list);
4167 (void) fprintf(stderr,
4168 gettext("Only one of [-r|-w] can be passed at a time\n"));
4174 * Enter the main iostat loop.
4180 * Histograms tables look out of place when you try to display
4181 * them with the other stats, so make a rule that you can only
4182 * print histograms by themselves.
4184 cb.cb_flags = IOS_L_HISTO_M;
4185 } else if (rq_histo) {
4186 cb.cb_flags = IOS_RQ_HISTO_M;
4188 cb.cb_flags = IOS_DEFAULT_M;
4190 cb.cb_flags |= IOS_LATENCY_M;
4192 cb.cb_flags |= IOS_QUEUES_M;
4196 * See if the module supports all the stats we want to display.
4198 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4199 if (unsupported_flags) {
4203 gettext("The loaded zfs module doesn't support:"));
4205 /* for each bit set in unsupported_flags */
4206 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4207 idx = lowbit64(f) - 1;
4208 fprintf(stderr, " -%c", flag_to_arg[idx]);
4211 fprintf(stderr, ". Try running a newer module.\n");
4212 pool_list_free(list);
4218 if ((npools = pool_list_count(list)) == 0)
4219 (void) fprintf(stderr, gettext("no pools available\n"));
4222 * If this is the first iteration and -y was supplied
4223 * we skip any printing.
4225 boolean_t skip = (omit_since_boot &&
4226 cb.cb_iteration == 0);
4229 * Refresh all statistics. This is done as an
4230 * explicit step before calculating the maximum name
4231 * width, so that any * configuration changes are
4232 * properly accounted for.
4234 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4238 * Iterate over all pools to determine the maximum width
4239 * for the pool / device name column across all pools.
4241 cb.cb_namewidth = 0;
4242 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4245 if (timestamp_fmt != NODATE)
4246 print_timestamp(timestamp_fmt);
4249 * If it's the first time and we're not skipping it,
4250 * or either skip or verbose mode, print the header.
4252 * The histogram code explicitly prints its header on
4253 * every vdev, so skip this for histograms.
4255 if (((++cb.cb_iteration == 1 && !skip) ||
4256 (skip != verbose)) &&
4257 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4259 print_iostat_header(&cb);
4262 (void) fsleep(interval);
4266 if (cmd != NULL && cb.cb_verbose)
4267 cb.vcdl = all_pools_for_each_vdev_run(argc,
4268 argv, cmd, g_zfs, cb.cb_vdev_names,
4269 cb.cb_vdev_names_count, cb.cb_name_flags);
4271 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4273 if (cb.vcdl != NULL)
4274 free_vdev_cmd_data_list(cb.vcdl);
4277 * If there's more than one pool, and we're not in
4278 * verbose mode (which prints a separator for us),
4279 * then print a separator.
4281 * In addition, if we're printing specific vdevs then
4282 * we also want an ending separator.
4284 if (((npools > 1 && !verbose &&
4285 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4286 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4287 cb.cb_vdev_names_count)) &&
4289 print_iostat_separator(&cb);
4294 * Flush the output so that redirection to a file isn't buffered
4297 (void) fflush(stdout);
4302 if (count != 0 && --count == 0)
4305 (void) fsleep(interval);
4308 pool_list_free(list);
4313 typedef struct list_cbdata {
4314 boolean_t cb_verbose;
4317 boolean_t cb_scripted;
4318 zprop_list_t *cb_proplist;
4319 boolean_t cb_literal;
4323 * Given a list of columns to display, output appropriate headers for each one.
4326 print_header(list_cbdata_t *cb)
4328 zprop_list_t *pl = cb->cb_proplist;
4329 char headerbuf[ZPOOL_MAXPROPLEN];
4331 boolean_t first = B_TRUE;
4332 boolean_t right_justify;
4335 for (; pl != NULL; pl = pl->pl_next) {
4336 width = pl->pl_width;
4337 if (first && cb->cb_verbose) {
4339 * Reset the width to accommodate the verbose listing
4342 width = cb->cb_namewidth;
4350 right_justify = B_FALSE;
4351 if (pl->pl_prop != ZPROP_INVAL) {
4352 header = zpool_prop_column_name(pl->pl_prop);
4353 right_justify = zpool_prop_align_right(pl->pl_prop);
4357 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4358 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4359 headerbuf[i] = '\0';
4363 if (pl->pl_next == NULL && !right_justify)
4364 (void) printf("%s", header);
4365 else if (right_justify)
4366 (void) printf("%*s", (int)width, header);
4368 (void) printf("%-*s", (int)width, header);
4371 (void) printf("\n");
4375 * Given a pool and a list of properties, print out all the properties according
4376 * to the described layout.
4379 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4381 zprop_list_t *pl = cb->cb_proplist;
4382 boolean_t first = B_TRUE;
4383 char property[ZPOOL_MAXPROPLEN];
4385 boolean_t right_justify;
4388 for (; pl != NULL; pl = pl->pl_next) {
4390 width = pl->pl_width;
4391 if (first && cb->cb_verbose) {
4393 * Reset the width to accommodate the verbose listing
4396 width = cb->cb_namewidth;
4400 if (cb->cb_scripted)
4401 (void) printf("\t");
4408 right_justify = B_FALSE;
4409 if (pl->pl_prop != ZPROP_INVAL) {
4410 if (zpool_get_prop(zhp, pl->pl_prop, property,
4411 sizeof (property), NULL, cb->cb_literal) != 0)
4416 right_justify = zpool_prop_align_right(pl->pl_prop);
4417 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4418 zpool_prop_unsupported(pl->pl_user_prop)) &&
4419 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4420 sizeof (property)) == 0) {
4428 * If this is being called in scripted mode, or if this is the
4429 * last column and it is left-justified, don't include a width
4432 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4433 (void) printf("%s", propstr);
4434 else if (right_justify)
4435 (void) printf("%*s", (int)width, propstr);
4437 (void) printf("%-*s", (int)width, propstr);
4440 (void) printf("\n");
4444 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4445 boolean_t valid, enum zfs_nicenum_format format)
4449 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4452 case ZPOOL_PROP_EXPANDSZ:
4454 (void) strlcpy(propval, "-", sizeof (propval));
4456 zfs_nicenum_format(value, propval, sizeof (propval),
4459 case ZPOOL_PROP_FRAGMENTATION:
4460 if (value == ZFS_FRAG_INVALID) {
4461 (void) strlcpy(propval, "-", sizeof (propval));
4462 } else if (format == ZFS_NICENUM_RAW) {
4463 (void) snprintf(propval, sizeof (propval), "%llu",
4464 (unsigned long long)value);
4466 (void) snprintf(propval, sizeof (propval), "%llu%%",
4467 (unsigned long long)value);
4470 case ZPOOL_PROP_CAPACITY:
4471 if (format == ZFS_NICENUM_RAW)
4472 (void) snprintf(propval, sizeof (propval), "%llu",
4473 (unsigned long long)value);
4475 (void) snprintf(propval, sizeof (propval), "%llu%%",
4476 (unsigned long long)value);
4479 zfs_nicenum_format(value, propval, sizeof (propval), format);
4483 (void) strlcpy(propval, "-", sizeof (propval));
4486 (void) printf("\t%s", propval);
4488 (void) printf(" %*s", (int)width, propval);
4492 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4493 list_cbdata_t *cb, int depth)
4499 boolean_t scripted = cb->cb_scripted;
4500 uint64_t islog = B_FALSE;
4501 boolean_t haslog = B_FALSE;
4502 char *dashes = "%-*s - - - - - -\n";
4504 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4505 (uint64_t **)&vs, &c) == 0);
4508 boolean_t toplevel = (vs->vs_space != 0);
4510 enum zfs_nicenum_format format;
4513 format = ZFS_NICENUM_RAW;
4515 format = ZFS_NICENUM_1024;
4518 (void) printf("\t%s", name);
4519 else if (strlen(name) + depth > cb->cb_namewidth)
4520 (void) printf("%*s%s", depth, "", name);
4522 (void) printf("%*s%s%*s", depth, "", name,
4523 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4526 * Print the properties for the individual vdevs. Some
4527 * properties are only applicable to toplevel vdevs. The
4528 * 'toplevel' boolean value is passed to the print_one_column()
4529 * to indicate that the value is valid.
4531 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4533 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4535 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4536 scripted, toplevel, format);
4537 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4539 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4540 vs->vs_fragmentation, scripted,
4541 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4543 cap = (vs->vs_space == 0) ? 0 :
4544 (vs->vs_alloc * 100 / vs->vs_space);
4545 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4547 (void) printf("\n");
4550 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4551 &child, &children) != 0)
4554 for (c = 0; c < children; c++) {
4555 uint64_t ishole = B_FALSE;
4557 if (nvlist_lookup_uint64(child[c],
4558 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4561 if (nvlist_lookup_uint64(child[c],
4562 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
4567 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4569 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4573 if (haslog == B_TRUE) {
4574 /* LINTED E_SEC_PRINTF_VAR_FMT */
4575 (void) printf(dashes, cb->cb_namewidth, "log");
4576 for (c = 0; c < children; c++) {
4577 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4578 &islog) != 0 || !islog)
4580 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4582 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4587 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
4588 &child, &children) == 0 && children > 0) {
4589 /* LINTED E_SEC_PRINTF_VAR_FMT */
4590 (void) printf(dashes, cb->cb_namewidth, "cache");
4591 for (c = 0; c < children; c++) {
4592 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4594 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4599 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
4600 &children) == 0 && children > 0) {
4601 /* LINTED E_SEC_PRINTF_VAR_FMT */
4602 (void) printf(dashes, cb->cb_namewidth, "spare");
4603 for (c = 0; c < children; c++) {
4604 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4606 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4614 * Generic callback function to list a pool.
4617 list_callback(zpool_handle_t *zhp, void *data)
4619 list_cbdata_t *cbp = data;
4623 config = zpool_get_config(zhp, NULL);
4625 print_pool(zhp, cbp);
4626 if (!cbp->cb_verbose)
4629 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4631 print_list_stats(zhp, NULL, nvroot, cbp, 0);
4637 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4639 * -g Display guid for individual vdev name.
4640 * -H Scripted mode. Don't display headers, and separate properties
4642 * -L Follow links when resolving vdev path name.
4643 * -o List of properties to display. Defaults to
4644 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4645 * "dedupratio,health,altroot"
4646 * -p Display values in parsable (exact) format.
4647 * -P Display full path for vdev name.
4648 * -T Display a timestamp in date(1) or Unix format
4650 * List all pools in the system, whether or not they're healthy. Output space
4651 * statistics for each one, as well as health status summary.
4654 zpool_do_list(int argc, char **argv)
4658 list_cbdata_t cb = { 0 };
4659 static char default_props[] =
4660 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4661 "dedupratio,health,altroot";
4662 char *props = default_props;
4664 unsigned long count = 0;
4666 boolean_t first = B_TRUE;
4669 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
4672 cb.cb_name_flags |= VDEV_NAME_GUID;
4675 cb.cb_scripted = B_TRUE;
4678 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4684 cb.cb_name_flags |= VDEV_NAME_PATH;
4687 cb.cb_literal = B_TRUE;
4690 get_timestamp_arg(*optarg);
4693 cb.cb_verbose = B_TRUE;
4696 (void) fprintf(stderr, gettext("missing argument for "
4697 "'%c' option\n"), optopt);
4701 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4710 get_interval_count(&argc, argv, &interval, &count);
4712 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
4716 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
4720 if (pool_list_count(list) == 0)
4723 if (timestamp_fmt != NODATE)
4724 print_timestamp(timestamp_fmt);
4726 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
4730 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
4735 if (count != 0 && --count == 0)
4738 pool_list_free(list);
4739 (void) fsleep(interval);
4742 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
4743 (void) printf(gettext("no pools available\n"));
4747 pool_list_free(list);
4748 zprop_free_list(cb.cb_proplist);
4753 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
4755 boolean_t force = B_FALSE;
4758 char *poolname, *old_disk, *new_disk;
4759 zpool_handle_t *zhp;
4760 nvlist_t *props = NULL;
4765 while ((c = getopt(argc, argv, "fo:")) != -1) {
4771 if ((propval = strchr(optarg, '=')) == NULL) {
4772 (void) fprintf(stderr, gettext("missing "
4773 "'=' for -o option\n"));
4779 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
4780 (add_prop_list(optarg, propval, &props, B_TRUE)))
4784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4793 /* get pool name and check number of arguments */
4795 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4802 (void) fprintf(stderr,
4803 gettext("missing <device> specification\n"));
4811 (void) fprintf(stderr,
4812 gettext("missing <new_device> specification\n"));
4815 new_disk = old_disk;
4825 (void) fprintf(stderr, gettext("too many arguments\n"));
4829 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
4834 if (zpool_get_config(zhp, NULL) == NULL) {
4835 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
4842 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
4844 if (nvroot == NULL) {
4850 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
4853 nvlist_free(nvroot);
4860 * zpool replace [-f] <pool> <device> <new_device>
4862 * -f Force attach, even if <new_device> appears to be in use.
4864 * Replace <device> with <new_device>.
4868 zpool_do_replace(int argc, char **argv)
4870 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
4874 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
4876 * -f Force attach, even if <new_device> appears to be in use.
4877 * -o Set property=value.
4879 * Attach <new_device> to the mirror containing <device>. If <device> is not
4880 * part of a mirror, then <device> will be transformed into a mirror of
4881 * <device> and <new_device>. In either case, <new_device> will begin life
4882 * with a DTL of [0, now], and will immediately begin to resilver itself.
4885 zpool_do_attach(int argc, char **argv)
4887 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
4891 * zpool detach [-f] <pool> <device>
4893 * -f Force detach of <device>, even if DTLs argue against it
4894 * (not supported yet)
4896 * Detach a device from a mirror. The operation will be refused if <device>
4897 * is the last device in the mirror, or if the DTLs indicate that this device
4898 * has the only valid copy of some data.
4902 zpool_do_detach(int argc, char **argv)
4905 char *poolname, *path;
4906 zpool_handle_t *zhp;
4910 while ((c = getopt(argc, argv, "f")) != -1) {
4914 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4923 /* get pool name and check number of arguments */
4925 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4930 (void) fprintf(stderr,
4931 gettext("missing <device> specification\n"));
4938 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4941 ret = zpool_vdev_detach(zhp, path);
4949 * zpool split [-gLnP] [-o prop=val] ...
4951 * [-R altroot] <pool> <newpool> [<device> ...]
4953 * -g Display guid for individual vdev name.
4954 * -L Follow links when resolving vdev path name.
4955 * -n Do not split the pool, but display the resulting layout if
4956 * it were to be split.
4957 * -o Set property=value, or set mount options.
4958 * -P Display full path for vdev name.
4959 * -R Mount the split-off pool under an alternate root.
4961 * Splits the named pool and gives it the new pool name. Devices to be split
4962 * off may be listed, provided that no more than one device is specified
4963 * per top-level vdev mirror. The newly split pool is left in an exported
4964 * state unless -R is specified.
4966 * Restrictions: the top-level of the pool pool must only be made up of
4967 * mirrors; all devices in the pool must be healthy; no device may be
4968 * undergoing a resilvering operation.
4971 zpool_do_split(int argc, char **argv)
4973 char *srcpool, *newpool, *propval;
4974 char *mntopts = NULL;
4977 zpool_handle_t *zhp;
4978 nvlist_t *config, *props = NULL;
4980 flags.dryrun = B_FALSE;
4981 flags.import = B_FALSE;
4982 flags.name_flags = 0;
4985 while ((c = getopt(argc, argv, ":gLR:no:P")) != -1) {
4988 flags.name_flags |= VDEV_NAME_GUID;
4991 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
4994 flags.import = B_TRUE;
4996 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
4997 &props, B_TRUE) != 0) {
5003 flags.dryrun = B_TRUE;
5006 if ((propval = strchr(optarg, '=')) != NULL) {
5009 if (add_prop_list(optarg, propval,
5010 &props, B_TRUE) != 0) {
5019 flags.name_flags |= VDEV_NAME_PATH;
5022 (void) fprintf(stderr, gettext("missing argument for "
5023 "'%c' option\n"), optopt);
5027 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5034 if (!flags.import && mntopts != NULL) {
5035 (void) fprintf(stderr, gettext("setting mntopts is only "
5036 "valid when importing the pool\n"));
5044 (void) fprintf(stderr, gettext("Missing pool name\n"));
5048 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5058 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5063 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5064 if (config == NULL) {
5068 (void) printf(gettext("would create '%s' with the "
5069 "following layout:\n\n"), newpool);
5070 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5077 if (ret != 0 || flags.dryrun || !flags.import) {
5078 nvlist_free(config);
5084 * The split was successful. Now we need to open the new
5085 * pool and import it.
5087 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5088 nvlist_free(config);
5092 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5093 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5095 (void) fprintf(stderr, gettext("Split was successful, but "
5096 "the datasets could not all be mounted\n"));
5097 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5098 "different altroot\n"), "zpool import");
5101 nvlist_free(config);
5110 * zpool online <pool> <device> ...
5113 zpool_do_online(int argc, char **argv)
5117 zpool_handle_t *zhp;
5119 vdev_state_t newstate;
5123 while ((c = getopt(argc, argv, "et")) != -1) {
5126 flags |= ZFS_ONLINE_EXPAND;
5130 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5139 /* get pool name and check number of arguments */
5141 (void) fprintf(stderr, gettext("missing pool name\n"));
5145 (void) fprintf(stderr, gettext("missing device name\n"));
5151 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5154 for (i = 1; i < argc; i++) {
5155 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5156 if (newstate != VDEV_STATE_HEALTHY) {
5157 (void) printf(gettext("warning: device '%s' "
5158 "onlined, but remains in faulted state\n"),
5160 if (newstate == VDEV_STATE_FAULTED)
5161 (void) printf(gettext("use 'zpool "
5162 "clear' to restore a faulted "
5165 (void) printf(gettext("use 'zpool "
5166 "replace' to replace devices "
5167 "that are no longer present\n"));
5180 * zpool offline [-ft] <pool> <device> ...
5182 * -f Force the device into the offline state, even if doing
5183 * so would appear to compromise pool availability.
5184 * (not supported yet)
5186 * -t Only take the device off-line temporarily. The offline
5187 * state will not be persistent across reboots.
5191 zpool_do_offline(int argc, char **argv)
5195 zpool_handle_t *zhp;
5197 boolean_t istmp = B_FALSE;
5200 while ((c = getopt(argc, argv, "ft")) != -1) {
5207 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5216 /* get pool name and check number of arguments */
5218 (void) fprintf(stderr, gettext("missing pool name\n"));
5222 (void) fprintf(stderr, gettext("missing device name\n"));
5228 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5231 for (i = 1; i < argc; i++) {
5232 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5242 * zpool clear <pool> [device]
5244 * Clear all errors associated with a pool or a particular device.
5247 zpool_do_clear(int argc, char **argv)
5251 boolean_t dryrun = B_FALSE;
5252 boolean_t do_rewind = B_FALSE;
5253 boolean_t xtreme_rewind = B_FALSE;
5254 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5255 nvlist_t *policy = NULL;
5256 zpool_handle_t *zhp;
5257 char *pool, *device;
5260 while ((c = getopt(argc, argv, "FnX")) != -1) {
5269 xtreme_rewind = B_TRUE;
5272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5282 (void) fprintf(stderr, gettext("missing pool name\n"));
5287 (void) fprintf(stderr, gettext("too many arguments\n"));
5291 if ((dryrun || xtreme_rewind) && !do_rewind) {
5292 (void) fprintf(stderr,
5293 gettext("-n or -X only meaningful with -F\n"));
5297 rewind_policy = ZPOOL_TRY_REWIND;
5299 rewind_policy = ZPOOL_DO_REWIND;
5301 rewind_policy |= ZPOOL_EXTREME_REWIND;
5303 /* In future, further rewind policy choices can be passed along here */
5304 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5305 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5309 device = argc == 2 ? argv[1] : NULL;
5311 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5312 nvlist_free(policy);
5316 if (zpool_clear(zhp, device, policy) != 0)
5321 nvlist_free(policy);
5327 * zpool reguid <pool>
5330 zpool_do_reguid(int argc, char **argv)
5334 zpool_handle_t *zhp;
5338 while ((c = getopt(argc, argv, "")) != -1) {
5341 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5350 /* get pool name and check number of arguments */
5352 (void) fprintf(stderr, gettext("missing pool name\n"));
5357 (void) fprintf(stderr, gettext("too many arguments\n"));
5362 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5365 ret = zpool_reguid(zhp);
5373 * zpool reopen <pool>
5375 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5378 zpool_do_reopen(int argc, char **argv)
5382 zpool_handle_t *zhp;
5386 while ((c = getopt(argc, argv, "")) != -1) {
5389 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5399 (void) fprintf(stderr, gettext("missing pool name\n"));
5404 (void) fprintf(stderr, gettext("too many arguments\n"));
5409 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
5412 ret = zpool_reopen(zhp);
5417 typedef struct scrub_cbdata {
5424 scrub_callback(zpool_handle_t *zhp, void *data)
5426 scrub_cbdata_t *cb = data;
5430 * Ignore faulted pools.
5432 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5433 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5434 "currently unavailable\n"), zpool_get_name(zhp));
5438 err = zpool_scan(zhp, cb->cb_type);
5444 * zpool scrub [-s] <pool> ...
5446 * -s Stop. Stops any in-progress scrub.
5449 zpool_do_scrub(int argc, char **argv)
5454 cb.cb_type = POOL_SCAN_SCRUB;
5457 while ((c = getopt(argc, argv, "s")) != -1) {
5460 cb.cb_type = POOL_SCAN_NONE;
5463 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5475 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5479 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5483 * Print out detailed scrub status.
5486 print_scan_status(pool_scan_stat_t *ps)
5489 uint64_t elapsed, mins_left, hours_left;
5490 uint64_t pass_exam, examined, total;
5492 double fraction_done;
5493 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
5495 (void) printf(gettext(" scan: "));
5497 /* If there's never been a scan, there's not much to say. */
5498 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5499 ps->pss_func >= POOL_SCAN_FUNCS) {
5500 (void) printf(gettext("none requested\n"));
5504 start = ps->pss_start_time;
5505 end = ps->pss_end_time;
5506 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
5508 assert(ps->pss_func == POOL_SCAN_SCRUB ||
5509 ps->pss_func == POOL_SCAN_RESILVER);
5511 * Scan is finished or canceled.
5513 if (ps->pss_state == DSS_FINISHED) {
5514 uint64_t minutes_taken = (end - start) / 60;
5517 if (ps->pss_func == POOL_SCAN_SCRUB) {
5518 fmt = gettext("scrub repaired %s in %lluh%um with "
5519 "%llu errors on %s");
5520 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5521 fmt = gettext("resilvered %s in %lluh%um with "
5522 "%llu errors on %s");
5525 (void) printf(fmt, processed_buf,
5526 (u_longlong_t)(minutes_taken / 60),
5527 (uint_t)(minutes_taken % 60),
5528 (u_longlong_t)ps->pss_errors,
5529 ctime((time_t *)&end));
5531 } else if (ps->pss_state == DSS_CANCELED) {
5532 if (ps->pss_func == POOL_SCAN_SCRUB) {
5533 (void) printf(gettext("scrub canceled on %s"),
5535 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5536 (void) printf(gettext("resilver canceled on %s"),
5542 assert(ps->pss_state == DSS_SCANNING);
5545 * Scan is in progress.
5547 if (ps->pss_func == POOL_SCAN_SCRUB) {
5548 (void) printf(gettext("scrub in progress since %s"),
5550 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5551 (void) printf(gettext("resilver in progress since %s"),
5555 examined = ps->pss_examined ? ps->pss_examined : 1;
5556 total = ps->pss_to_examine;
5557 fraction_done = (double)examined / total;
5559 /* elapsed time for this pass */
5560 elapsed = time(NULL) - ps->pss_pass_start;
5561 elapsed = elapsed ? elapsed : 1;
5562 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
5563 rate = pass_exam / elapsed;
5564 rate = rate ? rate : 1;
5565 mins_left = ((total - examined) / rate) / 60;
5566 hours_left = mins_left / 60;
5568 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
5569 zfs_nicenum(total, total_buf, sizeof (total_buf));
5570 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
5573 * do not print estimated time if hours_left is more than 30 days
5575 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5576 examined_buf, total_buf, rate_buf);
5577 if (hours_left < (30 * 24)) {
5578 (void) printf(gettext(", %lluh%um to go\n"),
5579 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
5581 (void) printf(gettext(
5582 ", (scan is slow, no estimated time)\n"));
5585 if (ps->pss_func == POOL_SCAN_RESILVER) {
5586 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5587 processed_buf, 100 * fraction_done);
5588 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
5589 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5590 processed_buf, 100 * fraction_done);
5595 print_error_log(zpool_handle_t *zhp)
5597 nvlist_t *nverrlist = NULL;
5600 size_t len = MAXPATHLEN * 2;
5602 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
5603 (void) printf("errors: List of errors unavailable "
5604 "(insufficient privileges)\n");
5608 (void) printf("errors: Permanent errors have been "
5609 "detected in the following files:\n\n");
5611 pathname = safe_malloc(len);
5613 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
5615 uint64_t dsobj, obj;
5617 verify(nvpair_value_nvlist(elem, &nv) == 0);
5618 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
5620 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
5622 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
5623 (void) printf("%7s %s\n", "", pathname);
5626 nvlist_free(nverrlist);
5630 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
5639 (void) printf(gettext("\tspares\n"));
5641 for (i = 0; i < nspares; i++) {
5642 name = zpool_vdev_name(g_zfs, zhp, spares[i],
5644 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
5650 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
5659 (void) printf(gettext("\tcache\n"));
5661 for (i = 0; i < nl2cache; i++) {
5662 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
5664 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
5670 print_dedup_stats(nvlist_t *config)
5672 ddt_histogram_t *ddh;
5678 * If the pool was faulted then we may not have been able to
5679 * obtain the config. Otherwise, if we have anything in the dedup
5680 * table continue processing the stats.
5682 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
5683 (uint64_t **)&ddo, &c) != 0)
5686 (void) printf("\n");
5687 (void) printf(gettext(" dedup: "));
5688 if (ddo->ddo_count == 0) {
5689 (void) printf(gettext("no DDT entries\n"));
5693 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5694 (u_longlong_t)ddo->ddo_count,
5695 (u_longlong_t)ddo->ddo_dspace,
5696 (u_longlong_t)ddo->ddo_mspace);
5698 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
5699 (uint64_t **)&dds, &c) == 0);
5700 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
5701 (uint64_t **)&ddh, &c) == 0);
5702 zpool_dump_ddt(dds, ddh);
5706 * Display a summary of pool status. Displays a summary such as:
5710 * reason: One or more devices ...
5711 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5717 * When given the '-v' option, we print out the complete config. If the '-e'
5718 * option is specified, then we print out error rate information as well.
5721 status_callback(zpool_handle_t *zhp, void *data)
5723 status_cbdata_t *cbp = data;
5724 nvlist_t *config, *nvroot;
5726 zpool_status_t reason;
5727 zpool_errata_t errata;
5732 config = zpool_get_config(zhp, NULL);
5733 reason = zpool_get_status(zhp, &msgid, &errata);
5738 * If we were given 'zpool status -x', only report those pools with
5741 if (cbp->cb_explain &&
5742 (reason == ZPOOL_STATUS_OK ||
5743 reason == ZPOOL_STATUS_VERSION_OLDER ||
5744 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
5745 if (!cbp->cb_allpools) {
5746 (void) printf(gettext("pool '%s' is healthy\n"),
5747 zpool_get_name(zhp));
5749 cbp->cb_first = B_FALSE;
5755 cbp->cb_first = B_FALSE;
5757 (void) printf("\n");
5759 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5761 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
5762 (uint64_t **)&vs, &c) == 0);
5763 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
5765 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
5766 (void) printf(gettext(" state: %s\n"), health);
5769 case ZPOOL_STATUS_MISSING_DEV_R:
5770 (void) printf(gettext("status: One or more devices could not "
5771 "be opened. Sufficient replicas exist for\n\tthe pool to "
5772 "continue functioning in a degraded state.\n"));
5773 (void) printf(gettext("action: Attach the missing device and "
5774 "online it using 'zpool online'.\n"));
5777 case ZPOOL_STATUS_MISSING_DEV_NR:
5778 (void) printf(gettext("status: One or more devices could not "
5779 "be opened. There are insufficient\n\treplicas for the "
5780 "pool to continue functioning.\n"));
5781 (void) printf(gettext("action: Attach the missing device and "
5782 "online it using 'zpool online'.\n"));
5785 case ZPOOL_STATUS_CORRUPT_LABEL_R:
5786 (void) printf(gettext("status: One or more devices could not "
5787 "be used because the label is missing or\n\tinvalid. "
5788 "Sufficient replicas exist for the pool to continue\n\t"
5789 "functioning in a degraded state.\n"));
5790 (void) printf(gettext("action: Replace the device using "
5791 "'zpool replace'.\n"));
5794 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
5795 (void) printf(gettext("status: One or more devices could not "
5796 "be used because the label is missing \n\tor invalid. "
5797 "There are insufficient replicas for the pool to "
5798 "continue\n\tfunctioning.\n"));
5799 zpool_explain_recover(zpool_get_handle(zhp),
5800 zpool_get_name(zhp), reason, config);
5803 case ZPOOL_STATUS_FAILING_DEV:
5804 (void) printf(gettext("status: One or more devices has "
5805 "experienced an unrecoverable error. An\n\tattempt was "
5806 "made to correct the error. Applications are "
5808 (void) printf(gettext("action: Determine if the device needs "
5809 "to be replaced, and clear the errors\n\tusing "
5810 "'zpool clear' or replace the device with 'zpool "
5814 case ZPOOL_STATUS_OFFLINE_DEV:
5815 (void) printf(gettext("status: One or more devices has "
5816 "been taken offline by the administrator.\n\tSufficient "
5817 "replicas exist for the pool to continue functioning in "
5818 "a\n\tdegraded state.\n"));
5819 (void) printf(gettext("action: Online the device using "
5820 "'zpool online' or replace the device with\n\t'zpool "
5824 case ZPOOL_STATUS_REMOVED_DEV:
5825 (void) printf(gettext("status: One or more devices has "
5826 "been removed by the administrator.\n\tSufficient "
5827 "replicas exist for the pool to continue functioning in "
5828 "a\n\tdegraded state.\n"));
5829 (void) printf(gettext("action: Online the device using "
5830 "'zpool online' or replace the device with\n\t'zpool "
5834 case ZPOOL_STATUS_RESILVERING:
5835 (void) printf(gettext("status: One or more devices is "
5836 "currently being resilvered. The pool will\n\tcontinue "
5837 "to function, possibly in a degraded state.\n"));
5838 (void) printf(gettext("action: Wait for the resilver to "
5842 case ZPOOL_STATUS_CORRUPT_DATA:
5843 (void) printf(gettext("status: One or more devices has "
5844 "experienced an error resulting in data\n\tcorruption. "
5845 "Applications may be affected.\n"));
5846 (void) printf(gettext("action: Restore the file in question "
5847 "if possible. Otherwise restore the\n\tentire pool from "
5851 case ZPOOL_STATUS_CORRUPT_POOL:
5852 (void) printf(gettext("status: The pool metadata is corrupted "
5853 "and the pool cannot be opened.\n"));
5854 zpool_explain_recover(zpool_get_handle(zhp),
5855 zpool_get_name(zhp), reason, config);
5858 case ZPOOL_STATUS_VERSION_OLDER:
5859 (void) printf(gettext("status: The pool is formatted using a "
5860 "legacy on-disk format. The pool can\n\tstill be used, "
5861 "but some features are unavailable.\n"));
5862 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5863 "upgrade'. Once this is done, the\n\tpool will no longer "
5864 "be accessible on software that does not support\n\t"
5865 "feature flags.\n"));
5868 case ZPOOL_STATUS_VERSION_NEWER:
5869 (void) printf(gettext("status: The pool has been upgraded to a "
5870 "newer, incompatible on-disk version.\n\tThe pool cannot "
5871 "be accessed on this system.\n"));
5872 (void) printf(gettext("action: Access the pool from a system "
5873 "running more recent software, or\n\trestore the pool from "
5877 case ZPOOL_STATUS_FEAT_DISABLED:
5878 (void) printf(gettext("status: Some supported features are not "
5879 "enabled on the pool. The pool can\n\tstill be used, but "
5880 "some features are unavailable.\n"));
5881 (void) printf(gettext("action: Enable all features using "
5882 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5883 "longer be accessible by software that does not support\n\t"
5884 "the features. See zpool-features(5) for details.\n"));
5887 case ZPOOL_STATUS_UNSUP_FEAT_READ:
5888 (void) printf(gettext("status: The pool cannot be accessed on "
5889 "this system because it uses the\n\tfollowing feature(s) "
5890 "not supported on this system:\n"));
5891 zpool_print_unsup_feat(config);
5892 (void) printf("\n");
5893 (void) printf(gettext("action: Access the pool from a system "
5894 "that supports the required feature(s),\n\tor restore the "
5895 "pool from backup.\n"));
5898 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5899 (void) printf(gettext("status: The pool can only be accessed "
5900 "in read-only mode on this system. It\n\tcannot be "
5901 "accessed in read-write mode because it uses the "
5902 "following\n\tfeature(s) not supported on this system:\n"));
5903 zpool_print_unsup_feat(config);
5904 (void) printf("\n");
5905 (void) printf(gettext("action: The pool cannot be accessed in "
5906 "read-write mode. Import the pool with\n"
5907 "\t\"-o readonly=on\", access the pool from a system that "
5908 "supports the\n\trequired feature(s), or restore the "
5909 "pool from backup.\n"));
5912 case ZPOOL_STATUS_FAULTED_DEV_R:
5913 (void) printf(gettext("status: One or more devices are "
5914 "faulted in response to persistent errors.\n\tSufficient "
5915 "replicas exist for the pool to continue functioning "
5916 "in a\n\tdegraded state.\n"));
5917 (void) printf(gettext("action: Replace the faulted device, "
5918 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5921 case ZPOOL_STATUS_FAULTED_DEV_NR:
5922 (void) printf(gettext("status: One or more devices are "
5923 "faulted in response to persistent errors. There are "
5924 "insufficient replicas for the pool to\n\tcontinue "
5926 (void) printf(gettext("action: Destroy and re-create the pool "
5927 "from a backup source. Manually marking the device\n"
5928 "\trepaired using 'zpool clear' may allow some data "
5929 "to be recovered.\n"));
5932 case ZPOOL_STATUS_IO_FAILURE_WAIT:
5933 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
5934 (void) printf(gettext("status: One or more devices are "
5935 "faulted in response to IO failures.\n"));
5936 (void) printf(gettext("action: Make sure the affected devices "
5937 "are connected, then run 'zpool clear'.\n"));
5940 case ZPOOL_STATUS_BAD_LOG:
5941 (void) printf(gettext("status: An intent log record "
5942 "could not be read.\n"
5943 "\tWaiting for administrator intervention to fix the "
5944 "faulted pool.\n"));
5945 (void) printf(gettext("action: Either restore the affected "
5946 "device(s) and run 'zpool online',\n"
5947 "\tor ignore the intent log records by running "
5948 "'zpool clear'.\n"));
5951 case ZPOOL_STATUS_HOSTID_MISMATCH:
5952 (void) printf(gettext("status: Mismatch between pool hostid "
5953 "and system hostid on imported pool.\n\tThis pool was "
5954 "previously imported into a system with a different "
5955 "hostid,\n\tand then was verbatim imported into this "
5957 (void) printf(gettext("action: Export this pool on all systems "
5958 "on which it is imported.\n"
5959 "\tThen import it to correct the mismatch.\n"));
5962 case ZPOOL_STATUS_ERRATA:
5963 (void) printf(gettext("status: Errata #%d detected.\n"),
5967 case ZPOOL_ERRATA_NONE:
5970 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
5971 (void) printf(gettext("action: To correct the issue "
5972 "run 'zpool scrub'.\n"));
5977 * All errata which allow the pool to be imported
5978 * must contain an action message.
5986 * The remaining errors can't actually be generated, yet.
5988 assert(reason == ZPOOL_STATUS_OK);
5992 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
5995 if (config != NULL) {
5997 nvlist_t **spares, **l2cache;
5998 uint_t nspares, nl2cache;
5999 pool_scan_stat_t *ps = NULL;
6001 (void) nvlist_lookup_uint64_array(nvroot,
6002 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6003 print_scan_status(ps);
6005 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6006 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6007 if (cbp->cb_namewidth < 10)
6008 cbp->cb_namewidth = 10;
6010 (void) printf(gettext("config:\n\n"));
6011 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
6012 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6014 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6017 if (num_logs(nvroot) > 0)
6018 print_logs(zhp, cbp, nvroot);
6019 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6020 &l2cache, &nl2cache) == 0)
6021 print_l2cache(zhp, cbp, l2cache, nl2cache);
6023 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6024 &spares, &nspares) == 0)
6025 print_spares(zhp, cbp, spares, nspares);
6027 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6029 nvlist_t *nverrlist = NULL;
6032 * If the approximate error count is small, get a
6033 * precise count by fetching the entire log and
6034 * uniquifying the results.
6036 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6037 zpool_get_errlog(zhp, &nverrlist) == 0) {
6042 while ((elem = nvlist_next_nvpair(nverrlist,
6047 nvlist_free(nverrlist);
6049 (void) printf("\n");
6052 (void) printf(gettext("errors: No known data "
6054 else if (!cbp->cb_verbose)
6055 (void) printf(gettext("errors: %llu data "
6056 "errors, use '-v' for a list\n"),
6057 (u_longlong_t)nerr);
6059 print_error_log(zhp);
6062 if (cbp->cb_dedup_stats)
6063 print_dedup_stats(config);
6065 (void) printf(gettext("config: The configuration cannot be "
6073 * zpool status [-c CMD] [-gLPvx] [-T d|u] [pool] ... [interval [count]]
6075 * -c CMD For each vdev, run command CMD
6076 * -g Display guid for individual vdev name.
6077 * -L Follow links when resolving vdev path name.
6078 * -P Display full path for vdev name.
6079 * -v Display complete error logs
6080 * -x Display only pools with potential problems
6081 * -D Display dedup status (undocumented)
6082 * -T Display a timestamp in date(1) or Unix format
6084 * Describes the health status of all pools or some subset.
6087 zpool_do_status(int argc, char **argv)
6092 unsigned long count = 0;
6093 status_cbdata_t cb = { 0 };
6097 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6103 cb.cb_name_flags |= VDEV_NAME_GUID;
6106 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6109 cb.cb_name_flags |= VDEV_NAME_PATH;
6112 cb.cb_verbose = B_TRUE;
6115 cb.cb_explain = B_TRUE;
6118 cb.cb_dedup_stats = B_TRUE;
6121 get_timestamp_arg(*optarg);
6124 if (optopt == 'c') {
6126 gettext("Missing CMD for -c\n"));
6129 gettext("invalid option '%c'\n"), optopt);
6138 get_interval_count(&argc, argv, &interval, &count);
6141 cb.cb_allpools = B_TRUE;
6143 cb.cb_first = B_TRUE;
6144 cb.cb_print_status = B_TRUE;
6147 if (timestamp_fmt != NODATE)
6148 print_timestamp(timestamp_fmt);
6151 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6154 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6155 status_callback, &cb);
6157 if (cb.vcdl != NULL)
6158 free_vdev_cmd_data_list(cb.vcdl);
6160 if (argc == 0 && cb.cb_count == 0)
6161 (void) fprintf(stderr, gettext("no pools available\n"));
6162 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6163 (void) printf(gettext("all pools are healthy\n"));
6171 if (count != 0 && --count == 0)
6174 (void) fsleep(interval);
6180 typedef struct upgrade_cbdata {
6183 uint64_t cb_version;
6188 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6190 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6191 int *count = (int *)unsupp_fs;
6193 if (zfs_version > ZPL_VERSION) {
6194 (void) printf(gettext("%s (v%d) is not supported by this "
6195 "implementation of ZFS.\n"),
6196 zfs_get_name(zhp), zfs_version);
6200 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6208 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6212 uint64_t oldversion;
6215 config = zpool_get_config(zhp, NULL);
6216 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6219 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6220 assert(oldversion < version);
6222 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6227 (void) fprintf(stderr, gettext("Upgrade not performed due "
6228 "to %d unsupported filesystems (max v%d).\n"),
6229 unsupp_fs, (int)ZPL_VERSION);
6233 ret = zpool_upgrade(zhp, version);
6237 if (version >= SPA_VERSION_FEATURES) {
6238 (void) printf(gettext("Successfully upgraded "
6239 "'%s' from version %llu to feature flags.\n"),
6240 zpool_get_name(zhp), (u_longlong_t)oldversion);
6242 (void) printf(gettext("Successfully upgraded "
6243 "'%s' from version %llu to version %llu.\n"),
6244 zpool_get_name(zhp), (u_longlong_t)oldversion,
6245 (u_longlong_t)version);
6252 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6255 boolean_t firstff = B_TRUE;
6256 nvlist_t *enabled = zpool_get_features(zhp);
6259 for (i = 0; i < SPA_FEATURES; i++) {
6260 const char *fname = spa_feature_table[i].fi_uname;
6261 const char *fguid = spa_feature_table[i].fi_guid;
6262 if (!nvlist_exists(enabled, fguid)) {
6264 verify(-1 != asprintf(&propname, "feature@%s", fname));
6265 ret = zpool_set_prop(zhp, propname,
6266 ZFS_FEATURE_ENABLED);
6274 (void) printf(gettext("Enabled the "
6275 "following features on '%s':\n"),
6276 zpool_get_name(zhp));
6279 (void) printf(gettext(" %s\n"), fname);
6290 upgrade_cb(zpool_handle_t *zhp, void *arg)
6292 upgrade_cbdata_t *cbp = arg;
6295 boolean_t printnl = B_FALSE;
6298 config = zpool_get_config(zhp, NULL);
6299 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6302 assert(SPA_VERSION_IS_SUPPORTED(version));
6304 if (version < cbp->cb_version) {
6305 cbp->cb_first = B_FALSE;
6306 ret = upgrade_version(zhp, cbp->cb_version);
6312 * If they did "zpool upgrade -a", then we could
6313 * be doing ioctls to different pools. We need
6314 * to log this history once to each pool, and bypass
6315 * the normal history logging that happens in main().
6317 (void) zpool_log_history(g_zfs, history_str);
6318 log_history = B_FALSE;
6321 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6323 ret = upgrade_enable_all(zhp, &count);
6328 cbp->cb_first = B_FALSE;
6334 (void) printf(gettext("\n"));
6341 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6343 upgrade_cbdata_t *cbp = arg;
6347 config = zpool_get_config(zhp, NULL);
6348 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6351 assert(SPA_VERSION_IS_SUPPORTED(version));
6353 if (version < SPA_VERSION_FEATURES) {
6354 if (cbp->cb_first) {
6355 (void) printf(gettext("The following pools are "
6356 "formatted with legacy version numbers and can\n"
6357 "be upgraded to use feature flags. After "
6358 "being upgraded, these pools\nwill no "
6359 "longer be accessible by software that does not "
6360 "support feature\nflags.\n\n"));
6361 (void) printf(gettext("VER POOL\n"));
6362 (void) printf(gettext("--- ------------\n"));
6363 cbp->cb_first = B_FALSE;
6366 (void) printf("%2llu %s\n", (u_longlong_t)version,
6367 zpool_get_name(zhp));
6374 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6376 upgrade_cbdata_t *cbp = arg;
6380 config = zpool_get_config(zhp, NULL);
6381 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6384 if (version >= SPA_VERSION_FEATURES) {
6386 boolean_t poolfirst = B_TRUE;
6387 nvlist_t *enabled = zpool_get_features(zhp);
6389 for (i = 0; i < SPA_FEATURES; i++) {
6390 const char *fguid = spa_feature_table[i].fi_guid;
6391 const char *fname = spa_feature_table[i].fi_uname;
6392 if (!nvlist_exists(enabled, fguid)) {
6393 if (cbp->cb_first) {
6394 (void) printf(gettext("\nSome "
6395 "supported features are not "
6396 "enabled on the following pools. "
6397 "Once a\nfeature is enabled the "
6398 "pool may become incompatible with "
6399 "software\nthat does not support "
6401 "zpool-features(5) for "
6403 (void) printf(gettext("POOL "
6405 (void) printf(gettext("------"
6407 cbp->cb_first = B_FALSE;
6411 (void) printf(gettext("%s\n"),
6412 zpool_get_name(zhp));
6413 poolfirst = B_FALSE;
6416 (void) printf(gettext(" %s\n"), fname);
6419 * If they did "zpool upgrade -a", then we could
6420 * be doing ioctls to different pools. We need
6421 * to log this history once to each pool, and bypass
6422 * the normal history logging that happens in main().
6424 (void) zpool_log_history(g_zfs, history_str);
6425 log_history = B_FALSE;
6434 upgrade_one(zpool_handle_t *zhp, void *data)
6436 boolean_t printnl = B_FALSE;
6437 upgrade_cbdata_t *cbp = data;
6438 uint64_t cur_version;
6441 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6442 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6443 "Pool 'log' must be renamed using export and import"
6448 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6449 if (cur_version > cbp->cb_version) {
6450 (void) printf(gettext("Pool '%s' is already formatted "
6451 "using more current version '%llu'.\n\n"),
6452 zpool_get_name(zhp), (u_longlong_t)cur_version);
6456 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
6457 (void) printf(gettext("Pool '%s' is already formatted "
6458 "using version %llu.\n\n"), zpool_get_name(zhp),
6459 (u_longlong_t)cbp->cb_version);
6463 if (cur_version != cbp->cb_version) {
6465 ret = upgrade_version(zhp, cbp->cb_version);
6470 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6472 ret = upgrade_enable_all(zhp, &count);
6478 } else if (cur_version == SPA_VERSION) {
6479 (void) printf(gettext("Pool '%s' already has all "
6480 "supported features enabled.\n"),
6481 zpool_get_name(zhp));
6486 (void) printf(gettext("\n"));
6495 * zpool upgrade [-V version] <-a | pool ...>
6497 * With no arguments, display downrev'd ZFS pool available for upgrade.
6498 * Individual pools can be upgraded by specifying the pool, and '-a' will
6499 * upgrade all pools.
6502 zpool_do_upgrade(int argc, char **argv)
6505 upgrade_cbdata_t cb = { 0 };
6507 boolean_t showversions = B_FALSE;
6508 boolean_t upgradeall = B_FALSE;
6513 while ((c = getopt(argc, argv, ":avV:")) != -1) {
6516 upgradeall = B_TRUE;
6519 showversions = B_TRUE;
6522 cb.cb_version = strtoll(optarg, &end, 10);
6524 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
6525 (void) fprintf(stderr,
6526 gettext("invalid version '%s'\n"), optarg);
6531 (void) fprintf(stderr, gettext("missing argument for "
6532 "'%c' option\n"), optopt);
6536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6547 if (cb.cb_version == 0) {
6548 cb.cb_version = SPA_VERSION;
6549 } else if (!upgradeall && argc == 0) {
6550 (void) fprintf(stderr, gettext("-V option is "
6551 "incompatible with other arguments\n"));
6556 if (upgradeall || argc != 0) {
6557 (void) fprintf(stderr, gettext("-v option is "
6558 "incompatible with other arguments\n"));
6561 } else if (upgradeall) {
6563 (void) fprintf(stderr, gettext("-a option should not "
6564 "be used along with a pool name\n"));
6569 (void) printf(gettext("This system supports ZFS pool feature "
6574 (void) printf(gettext("The following features are "
6576 (void) printf(gettext("FEAT DESCRIPTION\n"));
6577 (void) printf("----------------------------------------------"
6578 "---------------\n");
6579 for (i = 0; i < SPA_FEATURES; i++) {
6580 zfeature_info_t *fi = &spa_feature_table[i];
6582 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
6583 " (read-only compatible)" : "";
6585 (void) printf("%-37s%s\n", fi->fi_uname, ro);
6586 (void) printf(" %s\n", fi->fi_desc);
6588 (void) printf("\n");
6590 (void) printf(gettext("The following legacy versions are also "
6592 (void) printf(gettext("VER DESCRIPTION\n"));
6593 (void) printf("--- -----------------------------------------"
6594 "---------------\n");
6595 (void) printf(gettext(" 1 Initial ZFS version\n"));
6596 (void) printf(gettext(" 2 Ditto blocks "
6597 "(replicated metadata)\n"));
6598 (void) printf(gettext(" 3 Hot spares and double parity "
6600 (void) printf(gettext(" 4 zpool history\n"));
6601 (void) printf(gettext(" 5 Compression using the gzip "
6603 (void) printf(gettext(" 6 bootfs pool property\n"));
6604 (void) printf(gettext(" 7 Separate intent log devices\n"));
6605 (void) printf(gettext(" 8 Delegated administration\n"));
6606 (void) printf(gettext(" 9 refquota and refreservation "
6608 (void) printf(gettext(" 10 Cache devices\n"));
6609 (void) printf(gettext(" 11 Improved scrub performance\n"));
6610 (void) printf(gettext(" 12 Snapshot properties\n"));
6611 (void) printf(gettext(" 13 snapused property\n"));
6612 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6613 (void) printf(gettext(" 15 user/group space accounting\n"));
6614 (void) printf(gettext(" 16 stmf property support\n"));
6615 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6616 (void) printf(gettext(" 18 Snapshot user holds\n"));
6617 (void) printf(gettext(" 19 Log device removal\n"));
6618 (void) printf(gettext(" 20 Compression using zle "
6619 "(zero-length encoding)\n"));
6620 (void) printf(gettext(" 21 Deduplication\n"));
6621 (void) printf(gettext(" 22 Received properties\n"));
6622 (void) printf(gettext(" 23 Slim ZIL\n"));
6623 (void) printf(gettext(" 24 System attributes\n"));
6624 (void) printf(gettext(" 25 Improved scrub stats\n"));
6625 (void) printf(gettext(" 26 Improved snapshot deletion "
6627 (void) printf(gettext(" 27 Improved snapshot creation "
6629 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6630 (void) printf(gettext("\nFor more information on a particular "
6631 "version, including supported releases,\n"));
6632 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6633 } else if (argc == 0 && upgradeall) {
6634 cb.cb_first = B_TRUE;
6635 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
6636 if (ret == 0 && cb.cb_first) {
6637 if (cb.cb_version == SPA_VERSION) {
6638 (void) printf(gettext("All pools are already "
6639 "formatted using feature flags.\n\n"));
6640 (void) printf(gettext("Every feature flags "
6641 "pool already has all supported features "
6644 (void) printf(gettext("All pools are already "
6645 "formatted with version %llu or higher.\n"),
6646 (u_longlong_t)cb.cb_version);
6649 } else if (argc == 0) {
6650 cb.cb_first = B_TRUE;
6651 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
6655 (void) printf(gettext("All pools are formatted "
6656 "using feature flags.\n\n"));
6658 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6659 "for a list of available legacy versions.\n"));
6662 cb.cb_first = B_TRUE;
6663 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
6667 (void) printf(gettext("Every feature flags pool has "
6668 "all supported features enabled.\n"));
6670 (void) printf(gettext("\n"));
6673 ret = for_each_pool(argc, argv, B_FALSE, NULL,
6680 typedef struct hist_cbdata {
6687 * Print out the command history for a specific pool.
6690 get_history_one(zpool_handle_t *zhp, void *data)
6696 hist_cbdata_t *cb = (hist_cbdata_t *)data;
6698 cb->first = B_FALSE;
6700 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
6702 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
6705 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
6706 &records, &numrecords) == 0);
6707 for (i = 0; i < numrecords; i++) {
6708 nvlist_t *rec = records[i];
6711 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
6715 tsec = fnvlist_lookup_uint64(records[i],
6717 (void) localtime_r(&tsec, &t);
6718 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
6721 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
6722 (void) printf("%s %s", tbuf,
6723 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
6724 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
6726 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
6729 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
6730 (void) printf("%s unrecognized record:\n",
6732 dump_nvlist(rec, 4);
6735 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
6736 zfs_history_event_names[ievent],
6737 (longlong_t)fnvlist_lookup_uint64(
6738 rec, ZPOOL_HIST_TXG),
6739 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
6740 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
6743 (void) printf("%s [txg:%lld] %s", tbuf,
6744 (longlong_t)fnvlist_lookup_uint64(
6745 rec, ZPOOL_HIST_TXG),
6746 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
6747 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
6748 (void) printf(" %s (%llu)",
6749 fnvlist_lookup_string(rec,
6751 (u_longlong_t)fnvlist_lookup_uint64(rec,
6754 (void) printf(" %s", fnvlist_lookup_string(rec,
6755 ZPOOL_HIST_INT_STR));
6756 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
6759 (void) printf("%s ioctl %s\n", tbuf,
6760 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
6761 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
6762 (void) printf(" input:\n");
6763 dump_nvlist(fnvlist_lookup_nvlist(rec,
6764 ZPOOL_HIST_INPUT_NVL), 8);
6766 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
6767 (void) printf(" output:\n");
6768 dump_nvlist(fnvlist_lookup_nvlist(rec,
6769 ZPOOL_HIST_OUTPUT_NVL), 8);
6774 (void) printf("%s unrecognized record:\n", tbuf);
6775 dump_nvlist(rec, 4);
6779 (void) printf("\n");
6782 (void) printf(" [");
6783 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
6784 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
6785 struct passwd *pwd = getpwuid(who);
6786 (void) printf("user %d ", (int)who);
6788 (void) printf("(%s) ", pwd->pw_name);
6790 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
6791 (void) printf("on %s",
6792 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
6794 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
6795 (void) printf(":%s",
6796 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
6800 (void) printf("\n");
6802 (void) printf("\n");
6809 * zpool history <pool>
6811 * Displays the history of commands that modified pools.
6814 zpool_do_history(int argc, char **argv)
6816 hist_cbdata_t cbdata = { 0 };
6820 cbdata.first = B_TRUE;
6822 while ((c = getopt(argc, argv, "li")) != -1) {
6825 cbdata.longfmt = B_TRUE;
6828 cbdata.internal = B_TRUE;
6831 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6839 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
6842 if (argc == 0 && cbdata.first == B_TRUE) {
6843 (void) fprintf(stderr, gettext("no pools available\n"));
6850 typedef struct ev_opts {
6858 zpool_do_events_short(nvlist_t *nvl)
6860 char ctime_str[26], str[32], *ptr;
6864 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
6865 memset(str, ' ', 32);
6866 (void) ctime_r((const time_t *)&tv[0], ctime_str);
6867 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
6868 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
6869 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
6870 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
6871 (void) printf(gettext("%s "), str);
6873 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
6874 (void) printf(gettext("%s\n"), ptr);
6878 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
6882 for (nvp = nvlist_next_nvpair(nvl, NULL);
6883 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
6885 data_type_t type = nvpair_type(nvp);
6886 const char *name = nvpair_name(nvp);
6896 printf(gettext("%*s%s = "), depth, "", name);
6899 case DATA_TYPE_BOOLEAN:
6900 printf(gettext("%s"), "1");
6903 case DATA_TYPE_BOOLEAN_VALUE:
6904 (void) nvpair_value_boolean_value(nvp, &b);
6905 printf(gettext("%s"), b ? "1" : "0");
6908 case DATA_TYPE_BYTE:
6909 (void) nvpair_value_byte(nvp, &i8);
6910 printf(gettext("0x%x"), i8);
6913 case DATA_TYPE_INT8:
6914 (void) nvpair_value_int8(nvp, (void *)&i8);
6915 printf(gettext("0x%x"), i8);
6918 case DATA_TYPE_UINT8:
6919 (void) nvpair_value_uint8(nvp, &i8);
6920 printf(gettext("0x%x"), i8);
6923 case DATA_TYPE_INT16:
6924 (void) nvpair_value_int16(nvp, (void *)&i16);
6925 printf(gettext("0x%x"), i16);
6928 case DATA_TYPE_UINT16:
6929 (void) nvpair_value_uint16(nvp, &i16);
6930 printf(gettext("0x%x"), i16);
6933 case DATA_TYPE_INT32:
6934 (void) nvpair_value_int32(nvp, (void *)&i32);
6935 printf(gettext("0x%x"), i32);
6938 case DATA_TYPE_UINT32:
6939 (void) nvpair_value_uint32(nvp, &i32);
6940 printf(gettext("0x%x"), i32);
6943 case DATA_TYPE_INT64:
6944 (void) nvpair_value_int64(nvp, (void *)&i64);
6945 printf(gettext("0x%llx"), (u_longlong_t)i64);
6948 case DATA_TYPE_UINT64:
6949 (void) nvpair_value_uint64(nvp, &i64);
6951 * translate vdev state values to readable
6952 * strings to aide zpool events consumers
6955 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
6957 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
6958 printf(gettext("\"%s\" (0x%llx)"),
6959 zpool_state_to_name(i64, VDEV_AUX_NONE),
6962 printf(gettext("0x%llx"), (u_longlong_t)i64);
6966 case DATA_TYPE_HRTIME:
6967 (void) nvpair_value_hrtime(nvp, (void *)&i64);
6968 printf(gettext("0x%llx"), (u_longlong_t)i64);
6971 case DATA_TYPE_STRING:
6972 (void) nvpair_value_string(nvp, &str);
6973 printf(gettext("\"%s\""), str ? str : "<NULL>");
6976 case DATA_TYPE_NVLIST:
6977 printf(gettext("(embedded nvlist)\n"));
6978 (void) nvpair_value_nvlist(nvp, &cnv);
6979 zpool_do_events_nvprint(cnv, depth + 8);
6980 printf(gettext("%*s(end %s)"), depth, "", name);
6983 case DATA_TYPE_NVLIST_ARRAY: {
6987 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
6988 printf(gettext("(%d embedded nvlists)\n"), nelem);
6989 for (i = 0; i < nelem; i++) {
6990 printf(gettext("%*s%s[%d] = %s\n"),
6991 depth, "", name, i, "(embedded nvlist)");
6992 zpool_do_events_nvprint(val[i], depth + 8);
6993 printf(gettext("%*s(end %s[%i])\n"),
6994 depth, "", name, i);
6996 printf(gettext("%*s(end %s)\n"), depth, "", name);
7000 case DATA_TYPE_INT8_ARRAY: {
7004 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7005 for (i = 0; i < nelem; i++)
7006 printf(gettext("0x%x "), val[i]);
7011 case DATA_TYPE_UINT8_ARRAY: {
7015 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7016 for (i = 0; i < nelem; i++)
7017 printf(gettext("0x%x "), val[i]);
7022 case DATA_TYPE_INT16_ARRAY: {
7026 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7027 for (i = 0; i < nelem; i++)
7028 printf(gettext("0x%x "), val[i]);
7033 case DATA_TYPE_UINT16_ARRAY: {
7037 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7038 for (i = 0; i < nelem; i++)
7039 printf(gettext("0x%x "), val[i]);
7044 case DATA_TYPE_INT32_ARRAY: {
7048 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7049 for (i = 0; i < nelem; i++)
7050 printf(gettext("0x%x "), val[i]);
7055 case DATA_TYPE_UINT32_ARRAY: {
7059 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7060 for (i = 0; i < nelem; i++)
7061 printf(gettext("0x%x "), val[i]);
7066 case DATA_TYPE_INT64_ARRAY: {
7070 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7071 for (i = 0; i < nelem; i++)
7072 printf(gettext("0x%llx "),
7073 (u_longlong_t)val[i]);
7078 case DATA_TYPE_UINT64_ARRAY: {
7082 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7083 for (i = 0; i < nelem; i++)
7084 printf(gettext("0x%llx "),
7085 (u_longlong_t)val[i]);
7090 case DATA_TYPE_STRING_ARRAY: {
7094 (void) nvpair_value_string_array(nvp, &str, &nelem);
7095 for (i = 0; i < nelem; i++)
7096 printf(gettext("\"%s\" "),
7097 str[i] ? str[i] : "<NULL>");
7102 case DATA_TYPE_BOOLEAN_ARRAY:
7103 case DATA_TYPE_BYTE_ARRAY:
7104 case DATA_TYPE_DOUBLE:
7105 case DATA_TYPE_UNKNOWN:
7106 printf(gettext("<unknown>"));
7110 printf(gettext("\n"));
7115 zpool_do_events_next(ev_opts_t *opts)
7118 int zevent_fd, ret, dropped;
7120 zevent_fd = open(ZFS_DEV, O_RDWR);
7121 VERIFY(zevent_fd >= 0);
7123 if (!opts->scripted)
7124 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7127 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7128 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7129 if (ret || nvl == NULL)
7133 (void) printf(gettext("dropped %d events\n"), dropped);
7135 zpool_do_events_short(nvl);
7137 if (opts->verbose) {
7138 zpool_do_events_nvprint(nvl, 8);
7139 printf(gettext("\n"));
7141 (void) fflush(stdout);
7146 VERIFY(0 == close(zevent_fd));
7152 zpool_do_events_clear(ev_opts_t *opts)
7156 ret = zpool_events_clear(g_zfs, &count);
7158 (void) printf(gettext("cleared %d events\n"), count);
7164 * zpool events [-vfc]
7166 * Displays events logs by ZFS.
7169 zpool_do_events(int argc, char **argv)
7171 ev_opts_t opts = { 0 };
7176 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7191 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7200 ret = zpool_do_events_clear(&opts);
7202 ret = zpool_do_events_next(&opts);
7208 get_callback(zpool_handle_t *zhp, void *data)
7210 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7211 char value[MAXNAMELEN];
7212 zprop_source_t srctype;
7215 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7218 * Skip the special fake placeholder. This will also skip
7219 * over the name property when 'all' is specified.
7221 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7222 pl == cbp->cb_proplist)
7225 if (pl->pl_prop == ZPROP_INVAL &&
7226 (zpool_prop_feature(pl->pl_user_prop) ||
7227 zpool_prop_unsupported(pl->pl_user_prop))) {
7228 srctype = ZPROP_SRC_LOCAL;
7230 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7231 value, sizeof (value)) == 0) {
7232 zprop_print_one_property(zpool_get_name(zhp),
7233 cbp, pl->pl_user_prop, value, srctype,
7237 if (zpool_get_prop(zhp, pl->pl_prop, value,
7238 sizeof (value), &srctype, cbp->cb_literal) != 0)
7241 zprop_print_one_property(zpool_get_name(zhp), cbp,
7242 zpool_prop_to_name(pl->pl_prop), value, srctype,
7250 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7252 * -H Scripted mode. Don't display headers, and separate properties
7254 * -o List of columns to display. Defaults to
7255 * "name,property,value,source".
7256 * -p Display values in parsable (exact) format.
7258 * Get properties of pools in the system. Output space statistics
7259 * for each one as well as other attributes.
7262 zpool_do_get(int argc, char **argv)
7264 zprop_get_cbdata_t cb = { 0 };
7265 zprop_list_t fake_name = { 0 };
7270 cb.cb_first = B_TRUE;
7273 * Set up default columns and sources.
7275 cb.cb_sources = ZPROP_SRC_ALL;
7276 cb.cb_columns[0] = GET_COL_NAME;
7277 cb.cb_columns[1] = GET_COL_PROPERTY;
7278 cb.cb_columns[2] = GET_COL_VALUE;
7279 cb.cb_columns[3] = GET_COL_SOURCE;
7280 cb.cb_type = ZFS_TYPE_POOL;
7283 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7286 cb.cb_literal = B_TRUE;
7289 cb.cb_scripted = B_TRUE;
7292 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7294 while (*optarg != '\0') {
7295 static char *col_subopts[] =
7296 { "name", "property", "value", "source",
7299 if (i == ZFS_GET_NCOLS) {
7300 (void) fprintf(stderr, gettext("too "
7301 "many fields given to -o "
7306 switch (getsubopt(&optarg, col_subopts,
7309 cb.cb_columns[i++] = GET_COL_NAME;
7312 cb.cb_columns[i++] = GET_COL_PROPERTY;
7315 cb.cb_columns[i++] = GET_COL_VALUE;
7318 cb.cb_columns[i++] = GET_COL_SOURCE;
7322 (void) fprintf(stderr,
7323 gettext("\"all\" conflicts "
7324 "with specific fields "
7325 "given to -o option\n"));
7328 cb.cb_columns[0] = GET_COL_NAME;
7329 cb.cb_columns[1] = GET_COL_PROPERTY;
7330 cb.cb_columns[2] = GET_COL_VALUE;
7331 cb.cb_columns[3] = GET_COL_SOURCE;
7335 (void) fprintf(stderr,
7336 gettext("invalid column name "
7343 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7353 (void) fprintf(stderr, gettext("missing property "
7358 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7359 ZFS_TYPE_POOL) != 0)
7365 if (cb.cb_proplist != NULL) {
7366 fake_name.pl_prop = ZPOOL_PROP_NAME;
7367 fake_name.pl_width = strlen(gettext("NAME"));
7368 fake_name.pl_next = cb.cb_proplist;
7369 cb.cb_proplist = &fake_name;
7372 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7375 if (cb.cb_proplist == &fake_name)
7376 zprop_free_list(fake_name.pl_next);
7378 zprop_free_list(cb.cb_proplist);
7383 typedef struct set_cbdata {
7386 boolean_t cb_any_successful;
7390 set_callback(zpool_handle_t *zhp, void *data)
7393 set_cbdata_t *cb = (set_cbdata_t *)data;
7395 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7398 cb->cb_any_successful = B_TRUE;
7404 zpool_do_set(int argc, char **argv)
7406 set_cbdata_t cb = { 0 };
7409 if (argc > 1 && argv[1][0] == '-') {
7410 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7416 (void) fprintf(stderr, gettext("missing property=value "
7422 (void) fprintf(stderr, gettext("missing pool name\n"));
7427 (void) fprintf(stderr, gettext("too many pool names\n"));
7431 cb.cb_propname = argv[1];
7432 cb.cb_value = strchr(cb.cb_propname, '=');
7433 if (cb.cb_value == NULL) {
7434 (void) fprintf(stderr, gettext("missing value in "
7435 "property=value argument\n"));
7439 *(cb.cb_value) = '\0';
7442 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
7449 find_command_idx(char *command, int *idx)
7453 for (i = 0; i < NCOMMAND; i++) {
7454 if (command_table[i].name == NULL)
7457 if (strcmp(command, command_table[i].name) == 0) {
7466 main(int argc, char **argv)
7472 (void) setlocale(LC_ALL, "");
7473 (void) textdomain(TEXT_DOMAIN);
7476 dprintf_setup(&argc, argv);
7481 * Make sure the user has specified some command.
7484 (void) fprintf(stderr, gettext("missing command\n"));
7493 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
7496 if ((g_zfs = libzfs_init()) == NULL) {
7497 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7501 libzfs_print_on_error(g_zfs, B_TRUE);
7503 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7506 * Run the appropriate command.
7508 if (find_command_idx(cmdname, &i) == 0) {
7509 current_command = &command_table[i];
7510 ret = command_table[i].func(argc - 1, argv + 1);
7511 } else if (strchr(cmdname, '=')) {
7512 verify(find_command_idx("set", &i) == 0);
7513 current_command = &command_table[i];
7514 ret = command_table[i].func(argc, argv);
7515 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
7517 * 'freeze' is a vile debugging abomination, so we treat
7521 int fd = open(ZFS_DEV, O_RDWR);
7522 (void) strlcpy((void *)buf, argv[2], sizeof (buf));
7523 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
7525 (void) fprintf(stderr, gettext("unrecognized "
7526 "command '%s'\n"), cmdname);
7531 if (ret == 0 && log_history)
7532 (void) zpool_log_history(g_zfs, history_str);
7537 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7538 * for the purposes of running ::findleaks.
7540 if (getenv("ZFS_ABORT") != NULL) {
7541 (void) printf("dumping core by request\n");