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.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/fs/zfs.h>
51 #include <sys/fm/util.h>
52 #include <sys/fm/protocol.h>
53 #include <sys/zfs_ioctl.h>
58 #include "zpool_util.h"
59 #include "zfs_comutil.h"
60 #include "zfeature_common.h"
62 #include "statcommon.h"
64 static int zpool_do_create(int, char **);
65 static int zpool_do_destroy(int, char **);
67 static int zpool_do_add(int, char **);
68 static int zpool_do_remove(int, char **);
69 static int zpool_do_labelclear(int, char **);
71 static int zpool_do_list(int, char **);
72 static int zpool_do_iostat(int, char **);
73 static int zpool_do_status(int, char **);
75 static int zpool_do_online(int, char **);
76 static int zpool_do_offline(int, char **);
77 static int zpool_do_clear(int, char **);
78 static int zpool_do_reopen(int, char **);
80 static int zpool_do_reguid(int, char **);
82 static int zpool_do_attach(int, char **);
83 static int zpool_do_detach(int, char **);
84 static int zpool_do_replace(int, char **);
85 static int zpool_do_split(int, char **);
87 static int zpool_do_scrub(int, char **);
89 static int zpool_do_import(int, char **);
90 static int zpool_do_export(int, char **);
92 static int zpool_do_upgrade(int, char **);
94 static int zpool_do_history(int, char **);
95 static int zpool_do_events(int, char **);
97 static int zpool_do_get(int, char **);
98 static int zpool_do_set(int, char **);
101 * These libumem hooks provide a reasonable set of defaults for the allocator's
102 * debugging facilities.
107 _umem_debug_init(void)
109 return ("default,verbose"); /* $UMEM_DEBUG setting */
113 _umem_logging_init(void)
115 return ("fail,contents"); /* $UMEM_LOGGING setting */
149 * Flags for stats to display with "zpool iostats"
157 IOS_COUNT, /* always last element */
160 /* iostat_type entries as bitmasks */
161 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
162 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
163 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
164 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
165 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
167 /* Mask of all the histo bits */
168 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
171 * Lookup table for iostat flags to nvlist names. Basically a list
172 * of all the nvlists a flag requires. Also specifies the order in
173 * which data gets printed in zpool iostat.
175 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
177 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
178 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
179 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
180 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
181 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
182 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
183 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
184 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
185 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
188 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
194 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
195 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
196 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
197 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
198 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
201 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
202 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
203 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
204 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
205 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
206 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
207 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
208 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
209 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
210 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
216 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
217 * Right now, only one histo bit is ever set at one time, so we can
218 * just do a highbit64(a)
220 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
222 typedef struct zpool_command {
224 int (*func)(int, char **);
229 * Master command table. Each ZFS command has a name, associated function, and
230 * usage message. The usage messages need to be internationalized, so we have
231 * to have a function to return the usage message based on a command index.
233 * These commands are organized according to how they are displayed in the usage
234 * message. An empty command (one with a NULL name) indicates an empty line in
235 * the generic usage message.
237 static zpool_command_t command_table[] = {
238 { "create", zpool_do_create, HELP_CREATE },
239 { "destroy", zpool_do_destroy, HELP_DESTROY },
241 { "add", zpool_do_add, HELP_ADD },
242 { "remove", zpool_do_remove, HELP_REMOVE },
244 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
246 { "list", zpool_do_list, HELP_LIST },
247 { "iostat", zpool_do_iostat, HELP_IOSTAT },
248 { "status", zpool_do_status, HELP_STATUS },
250 { "online", zpool_do_online, HELP_ONLINE },
251 { "offline", zpool_do_offline, HELP_OFFLINE },
252 { "clear", zpool_do_clear, HELP_CLEAR },
253 { "reopen", zpool_do_reopen, HELP_REOPEN },
255 { "attach", zpool_do_attach, HELP_ATTACH },
256 { "detach", zpool_do_detach, HELP_DETACH },
257 { "replace", zpool_do_replace, HELP_REPLACE },
258 { "split", zpool_do_split, HELP_SPLIT },
260 { "scrub", zpool_do_scrub, HELP_SCRUB },
262 { "import", zpool_do_import, HELP_IMPORT },
263 { "export", zpool_do_export, HELP_EXPORT },
264 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
265 { "reguid", zpool_do_reguid, HELP_REGUID },
267 { "history", zpool_do_history, HELP_HISTORY },
268 { "events", zpool_do_events, HELP_EVENTS },
270 { "get", zpool_do_get, HELP_GET },
271 { "set", zpool_do_set, HELP_SET },
274 #define NCOMMAND (ARRAY_SIZE(command_table))
276 static zpool_command_t *current_command;
277 static char history_str[HIS_MAX_RECORD_LEN];
278 static boolean_t log_history = B_TRUE;
279 static uint_t timestamp_fmt = NODATE;
282 get_usage(zpool_help_t idx)
286 return (gettext("\tadd [-fgLnP] [-o property=value] "
287 "<pool> <vdev> ...\n"));
289 return (gettext("\tattach [-f] [-o property=value] "
290 "<pool> <device> <new-device>\n"));
292 return (gettext("\tclear [-nF] <pool> [device]\n"));
294 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
295 "\t [-O file-system-property=value] ... \n"
296 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
298 return (gettext("\tdestroy [-f] <pool>\n"));
300 return (gettext("\tdetach <pool> <device>\n"));
302 return (gettext("\texport [-af] <pool> ...\n"));
304 return (gettext("\thistory [-il] [<pool>] ...\n"));
306 return (gettext("\timport [-d dir] [-D]\n"
307 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
308 "\timport [-o mntopts] [-o property=value] ... \n"
309 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
310 "[-R root] [-F [-n]] -a\n"
311 "\timport [-o mntopts] [-o property=value] ... \n"
312 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
313 "[-R root] [-F [-n]]\n"
314 "\t <pool | id> [newpool]\n"));
316 return (gettext("\tiostat [-c CMD] [-T d | u] [-ghHLpPvy] "
318 "\t [[pool ...]|[pool vdev ...]|[vdev ...]] "
319 "[interval [count]]\n"));
320 case HELP_LABELCLEAR:
321 return (gettext("\tlabelclear [-f] <vdev>\n"));
323 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
324 "[-T d|u] [pool] ... [interval [count]]\n"));
326 return (gettext("\toffline [-t] <pool> <device> ...\n"));
328 return (gettext("\tonline <pool> <device> ...\n"));
330 return (gettext("\treplace [-f] [-o property=value] "
331 "<pool> <device> [new-device]\n"));
333 return (gettext("\tremove <pool> <device> ...\n"));
335 return (gettext("\treopen <pool>\n"));
337 return (gettext("\tscrub [-s] <pool> ...\n"));
339 return (gettext("\tstatus [-c CMD] [-gLPvxD] [-T d|u] [pool]"
340 " ... [interval [count]]\n"));
342 return (gettext("\tupgrade\n"
344 "\tupgrade [-V version] <-a | pool ...>\n"));
346 return (gettext("\tevents [-vHfc]\n"));
348 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
349 "<\"all\" | property[,...]> <pool> ...\n"));
351 return (gettext("\tset <property=value> <pool> \n"));
353 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
354 "\t [-o property=value] <pool> <newpool> "
355 "[<device> ...]\n"));
357 return (gettext("\treguid <pool>\n"));
366 * Callback routine that will print out a pool property value.
369 print_prop_cb(int prop, void *cb)
373 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
375 if (zpool_prop_readonly(prop))
376 (void) fprintf(fp, " NO ");
378 (void) fprintf(fp, " YES ");
380 if (zpool_prop_values(prop) == NULL)
381 (void) fprintf(fp, "-\n");
383 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
389 * Display usage message. If we're inside a command, display only the usage for
390 * that command. Otherwise, iterate over the entire command table and display
391 * a complete usage message.
394 usage(boolean_t requested)
396 FILE *fp = requested ? stdout : stderr;
398 if (current_command == NULL) {
401 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
403 gettext("where 'command' is one of the following:\n\n"));
405 for (i = 0; i < NCOMMAND; i++) {
406 if (command_table[i].name == NULL)
407 (void) fprintf(fp, "\n");
409 (void) fprintf(fp, "%s",
410 get_usage(command_table[i].usage));
413 (void) fprintf(fp, gettext("usage:\n"));
414 (void) fprintf(fp, "%s", get_usage(current_command->usage));
417 if (current_command != NULL &&
418 ((strcmp(current_command->name, "set") == 0) ||
419 (strcmp(current_command->name, "get") == 0) ||
420 (strcmp(current_command->name, "list") == 0))) {
423 gettext("\nthe following properties are supported:\n"));
425 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
426 "PROPERTY", "EDIT", "VALUES");
428 /* Iterate over all properties */
429 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
432 (void) fprintf(fp, "\t%-15s ", "feature@...");
433 (void) fprintf(fp, "YES disabled | enabled | active\n");
435 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
436 "appended with a feature name.\nSee zpool-features(5).\n"));
440 * See comments at end of main().
442 if (getenv("ZFS_ABORT") != NULL) {
443 (void) printf("dumping core by request\n");
447 exit(requested ? 0 : 2);
451 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
452 boolean_t print_logs, int name_flags)
459 (void) printf("\t%*s%s\n", indent, "", name);
461 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
462 &child, &children) != 0)
465 for (c = 0; c < children; c++) {
466 uint64_t is_log = B_FALSE;
468 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
470 if ((is_log && !print_logs) || (!is_log && print_logs))
473 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
474 print_vdev_tree(zhp, vname, child[c], indent + 2,
475 B_FALSE, name_flags);
481 prop_list_contains_feature(nvlist_t *proplist)
484 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
485 nvp = nvlist_next_nvpair(proplist, nvp)) {
486 if (zpool_prop_feature(nvpair_name(nvp)))
493 * Add a property pair (name, string-value) into a property nvlist.
496 add_prop_list(const char *propname, char *propval, nvlist_t **props,
499 zpool_prop_t prop = ZPROP_INVAL;
505 if (*props == NULL &&
506 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
507 (void) fprintf(stderr,
508 gettext("internal error: out of memory\n"));
515 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
517 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
518 !zpool_prop_feature(propname)) {
519 (void) fprintf(stderr, gettext("property '%s' is "
520 "not a valid pool property\n"), propname);
525 * feature@ properties and version should not be specified
528 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
529 nvlist_exists(proplist, vname)) ||
530 (prop == ZPOOL_PROP_VERSION &&
531 prop_list_contains_feature(proplist))) {
532 (void) fprintf(stderr, gettext("'feature@' and "
533 "'version' properties cannot be specified "
539 if (zpool_prop_feature(propname))
542 normnm = zpool_prop_to_name(prop);
544 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
545 normnm = zfs_prop_to_name(fprop);
551 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
552 prop != ZPOOL_PROP_CACHEFILE) {
553 (void) fprintf(stderr, gettext("property '%s' "
554 "specified multiple times\n"), propname);
558 if (nvlist_add_string(proplist, normnm, propval) != 0) {
559 (void) fprintf(stderr, gettext("internal "
560 "error: out of memory\n"));
568 * Set a default property pair (name, string-value) in a property nvlist
571 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
576 if (nvlist_lookup_string(*props, propname, &pval) == 0)
579 return (add_prop_list(propname, propval, props, B_TRUE));
583 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
585 * -f Force addition of devices, even if they appear in use
586 * -g Display guid for individual vdev name.
587 * -L Follow links when resolving vdev path name.
588 * -n Do not add the devices, but display the resulting layout if
589 * they were to be added.
590 * -o Set property=value.
591 * -P Display full path for vdev name.
593 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
594 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
598 zpool_do_add(int argc, char **argv)
600 boolean_t force = B_FALSE;
601 boolean_t dryrun = B_FALSE;
609 nvlist_t *props = NULL;
613 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
619 name_flags |= VDEV_NAME_GUID;
622 name_flags |= VDEV_NAME_FOLLOW_LINKS;
628 if ((propval = strchr(optarg, '=')) == NULL) {
629 (void) fprintf(stderr, gettext("missing "
630 "'=' for -o option\n"));
636 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
637 (add_prop_list(optarg, propval, &props, B_TRUE)))
641 name_flags |= VDEV_NAME_PATH;
644 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
653 /* get pool name and check number of arguments */
655 (void) fprintf(stderr, gettext("missing pool name argument\n"));
659 (void) fprintf(stderr, gettext("missing vdev specification\n"));
668 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
671 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
672 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
678 /* pass off to get_vdev_spec for processing */
679 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
681 if (nvroot == NULL) {
687 nvlist_t *poolnvroot;
689 uint_t l2children, c;
691 boolean_t hadcache = B_FALSE;
693 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
696 (void) printf(gettext("would update '%s' to the following "
697 "configuration:\n"), zpool_get_name(zhp));
699 /* print original main pool and new tree */
700 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
702 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
704 /* Do the same for the logs */
705 if (num_logs(poolnvroot) > 0) {
706 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
708 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
710 } else if (num_logs(nvroot) > 0) {
711 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
715 /* Do the same for the caches */
716 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
717 &l2child, &l2children) == 0 && l2children) {
719 (void) printf(gettext("\tcache\n"));
720 for (c = 0; c < l2children; c++) {
721 vname = zpool_vdev_name(g_zfs, NULL,
722 l2child[c], name_flags);
723 (void) printf("\t %s\n", vname);
727 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
728 &l2child, &l2children) == 0 && l2children) {
730 (void) printf(gettext("\tcache\n"));
731 for (c = 0; c < l2children; c++) {
732 vname = zpool_vdev_name(g_zfs, NULL,
733 l2child[c], name_flags);
734 (void) printf("\t %s\n", vname);
741 ret = (zpool_add(zhp, nvroot) != 0);
752 * zpool remove <pool> <vdev> ...
754 * Removes the given vdev from the pool. Currently, this supports removing
755 * spares, cache, and log devices from the pool.
758 zpool_do_remove(int argc, char **argv)
762 zpool_handle_t *zhp = NULL;
767 /* get pool name and check number of arguments */
769 (void) fprintf(stderr, gettext("missing pool name argument\n"));
773 (void) fprintf(stderr, gettext("missing device\n"));
779 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
782 for (i = 1; i < argc; i++) {
783 if (zpool_vdev_remove(zhp, argv[i]) != 0)
792 * zpool labelclear <vdev>
794 * Verifies that the vdev is not active and zeros out the label information
798 zpool_do_labelclear(int argc, char **argv)
801 int c, fd = -1, ret = 0;
803 boolean_t inuse = B_FALSE;
804 boolean_t force = B_FALSE;
807 while ((c = getopt(argc, argv, "f")) != -1) {
813 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
824 (void) fprintf(stderr, gettext("missing vdev device name\n"));
829 if ((fd = open(vdev, O_RDWR)) < 0) {
830 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
835 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
839 (void) fprintf(stderr,
840 gettext("Unable to determine pool state for %s\n"
841 "Use -f to force the clearing any label data\n"), vdev);
849 case POOL_STATE_ACTIVE:
850 case POOL_STATE_SPARE:
851 case POOL_STATE_L2CACHE:
852 (void) fprintf(stderr,
853 gettext("labelclear operation failed.\n"
854 "\tVdev %s is a member (%s), of pool \"%s\".\n"
855 "\tTo remove label information from this device, "
856 "export or destroy\n\tthe pool, or remove %s from "
857 "the configuration of this pool\n\tand retry the "
858 "labelclear operation.\n"),
859 vdev, zpool_pool_state_to_name(state), name, vdev);
863 case POOL_STATE_EXPORTED:
867 (void) fprintf(stderr,
868 gettext("labelclear operation failed.\n\tVdev "
869 "%s is a member of the exported pool \"%s\".\n"
870 "\tUse \"zpool labelclear -f %s\" to force the "
871 "removal of label\n\tinformation.\n"),
876 case POOL_STATE_POTENTIALLY_ACTIVE:
880 (void) fprintf(stderr,
881 gettext("labelclear operation failed.\n"
882 "\tVdev %s is a member of the pool \"%s\".\n"
883 "\tThis pool is unknown to this system, but may "
884 "be active on\n\tanother system. Use "
885 "\'zpool labelclear -f %s\' to force the\n"
886 "\tremoval of label information.\n"),
891 case POOL_STATE_DESTROYED:
892 /* inuse should never be set for a destroyed pool... */
898 if (zpool_clear_label(fd) != 0) {
899 (void) fprintf(stderr,
900 gettext("Label clear failed on vdev %s\n"), vdev);
913 * zpool create [-fnd] [-o property=value] ...
914 * [-O file-system-property=value] ...
915 * [-R root] [-m mountpoint] <pool> <dev> ...
917 * -f Force creation, even if devices appear in use
918 * -n Do not create the pool, but display the resulting layout if it
919 * were to be created.
920 * -R Create a pool under an alternate root
921 * -m Set default mountpoint for the root dataset. By default it's
923 * -o Set property=value.
924 * -o Set feature@feature=enabled|disabled.
925 * -d Don't automatically enable all supported pool features
926 * (individual features can be enabled with -o).
927 * -O Set fsproperty=value in the pool's root file system
929 * Creates the named pool according to the given vdev specification. The
930 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
931 * we get the nvlist back from get_vdev_spec(), we either print out the contents
932 * (if '-n' was specified), or pass it to libzfs to do the creation.
935 zpool_do_create(int argc, char **argv)
937 boolean_t force = B_FALSE;
938 boolean_t dryrun = B_FALSE;
939 boolean_t enable_all_pool_feat = B_TRUE;
941 nvlist_t *nvroot = NULL;
945 char *altroot = NULL;
946 char *mountpoint = NULL;
947 nvlist_t *fsprops = NULL;
948 nvlist_t *props = NULL;
952 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
961 enable_all_pool_feat = B_FALSE;
965 if (add_prop_list(zpool_prop_to_name(
966 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
968 if (add_prop_list_default(zpool_prop_to_name(
969 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
973 /* Equivalent to -O mountpoint=optarg */
977 if ((propval = strchr(optarg, '=')) == NULL) {
978 (void) fprintf(stderr, gettext("missing "
979 "'=' for -o option\n"));
985 if (add_prop_list(optarg, propval, &props, B_TRUE))
989 * If the user is creating a pool that doesn't support
990 * feature flags, don't enable any features.
992 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
996 ver = strtoull(propval, &end, 10);
998 ver < SPA_VERSION_FEATURES) {
999 enable_all_pool_feat = B_FALSE;
1002 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1006 if ((propval = strchr(optarg, '=')) == NULL) {
1007 (void) fprintf(stderr, gettext("missing "
1008 "'=' for -O option\n"));
1015 * Mountpoints are checked and then added later.
1016 * Uniquely among properties, they can be specified
1017 * more than once, to avoid conflict with -m.
1019 if (0 == strcmp(optarg,
1020 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1021 mountpoint = propval;
1022 } else if (add_prop_list(optarg, propval, &fsprops,
1029 * Sanity check temporary pool name.
1031 if (strchr(optarg, '/') != NULL) {
1032 (void) fprintf(stderr, gettext("cannot create "
1033 "'%s': invalid character '/' in temporary "
1035 (void) fprintf(stderr, gettext("use 'zfs "
1036 "create' to create a dataset\n"));
1040 if (add_prop_list(zpool_prop_to_name(
1041 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1043 if (add_prop_list_default(zpool_prop_to_name(
1044 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1049 (void) fprintf(stderr, gettext("missing argument for "
1050 "'%c' option\n"), optopt);
1053 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1062 /* get pool name and check number of arguments */
1064 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1068 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1075 * As a special case, check for use of '/' in the name, and direct the
1076 * user to use 'zfs create' instead.
1078 if (strchr(poolname, '/') != NULL) {
1079 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1080 "character '/' in pool name\n"), poolname);
1081 (void) fprintf(stderr, gettext("use 'zfs create' to "
1082 "create a dataset\n"));
1086 /* pass off to get_vdev_spec for bulk processing */
1087 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1088 argc - 1, argv + 1);
1092 /* make_root_vdev() allows 0 toplevel children if there are spares */
1093 if (!zfs_allocatable_devs(nvroot)) {
1094 (void) fprintf(stderr, gettext("invalid vdev "
1095 "specification: at least one toplevel vdev must be "
1100 if (altroot != NULL && altroot[0] != '/') {
1101 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1102 "must be an absolute path\n"), altroot);
1107 * Check the validity of the mountpoint and direct the user to use the
1108 * '-m' mountpoint option if it looks like its in use.
1110 if (mountpoint == NULL ||
1111 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1112 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1113 char buf[MAXPATHLEN];
1116 if (mountpoint && mountpoint[0] != '/') {
1117 (void) fprintf(stderr, gettext("invalid mountpoint "
1118 "'%s': must be an absolute path, 'legacy', or "
1119 "'none'\n"), mountpoint);
1123 if (mountpoint == NULL) {
1124 if (altroot != NULL)
1125 (void) snprintf(buf, sizeof (buf), "%s/%s",
1128 (void) snprintf(buf, sizeof (buf), "/%s",
1131 if (altroot != NULL)
1132 (void) snprintf(buf, sizeof (buf), "%s%s",
1133 altroot, mountpoint);
1135 (void) snprintf(buf, sizeof (buf), "%s",
1139 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1140 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1141 "%s\n"), buf, strerror(errno));
1142 (void) fprintf(stderr, gettext("use '-m' "
1143 "option to provide a different default\n"));
1148 while (count < 3 && readdir(dirp) != NULL)
1150 (void) closedir(dirp);
1153 (void) fprintf(stderr, gettext("mountpoint "
1154 "'%s' exists and is not empty\n"), buf);
1155 (void) fprintf(stderr, gettext("use '-m' "
1156 "option to provide a "
1157 "different default\n"));
1164 * Now that the mountpoint's validity has been checked, ensure that
1165 * the property is set appropriately prior to creating the pool.
1167 if (mountpoint != NULL) {
1168 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1169 mountpoint, &fsprops, B_FALSE);
1177 * For a dry run invocation, print out a basic message and run
1178 * through all the vdevs in the list and print out in an
1179 * appropriate hierarchy.
1181 (void) printf(gettext("would create '%s' with the "
1182 "following layout:\n\n"), poolname);
1184 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1185 if (num_logs(nvroot) > 0)
1186 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1191 * Hand off to libzfs.
1194 for (i = 0; i < SPA_FEATURES; i++) {
1195 char propname[MAXPATHLEN];
1197 zfeature_info_t *feat = &spa_feature_table[i];
1199 (void) snprintf(propname, sizeof (propname),
1200 "feature@%s", feat->fi_uname);
1203 * Only features contained in props will be enabled:
1204 * remove from the nvlist every ZFS_FEATURE_DISABLED
1205 * value and add every missing ZFS_FEATURE_ENABLED if
1206 * enable_all_pool_feat is set.
1208 if (!nvlist_lookup_string(props, propname, &propval)) {
1209 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1210 (void) nvlist_remove_all(props,
1212 } else if (enable_all_pool_feat) {
1213 ret = add_prop_list(propname,
1214 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1221 if (zpool_create(g_zfs, poolname,
1222 nvroot, props, fsprops) == 0) {
1223 zfs_handle_t *pool = zfs_open(g_zfs,
1224 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1226 if (zfs_mount(pool, NULL, 0) == 0)
1227 ret = zfs_shareall(pool);
1230 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1231 (void) fprintf(stderr, gettext("pool name may have "
1237 nvlist_free(nvroot);
1238 nvlist_free(fsprops);
1242 nvlist_free(fsprops);
1249 * zpool destroy <pool>
1251 * -f Forcefully unmount any datasets
1253 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1256 zpool_do_destroy(int argc, char **argv)
1258 boolean_t force = B_FALSE;
1261 zpool_handle_t *zhp;
1265 while ((c = getopt(argc, argv, "f")) != -1) {
1271 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1280 /* check arguments */
1282 (void) fprintf(stderr, gettext("missing pool argument\n"));
1286 (void) fprintf(stderr, gettext("too many arguments\n"));
1292 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1294 * As a special case, check for use of '/' in the name, and
1295 * direct the user to use 'zfs destroy' instead.
1297 if (strchr(pool, '/') != NULL)
1298 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1299 "destroy a dataset\n"));
1303 if (zpool_disable_datasets(zhp, force) != 0) {
1304 (void) fprintf(stderr, gettext("could not destroy '%s': "
1305 "could not unmount datasets\n"), zpool_get_name(zhp));
1310 /* The history must be logged as part of the export */
1311 log_history = B_FALSE;
1313 ret = (zpool_destroy(zhp, history_str) != 0);
1320 typedef struct export_cbdata {
1322 boolean_t hardforce;
1329 zpool_export_one(zpool_handle_t *zhp, void *data)
1331 export_cbdata_t *cb = data;
1333 if (zpool_disable_datasets(zhp, cb->force) != 0)
1336 /* The history must be logged as part of the export */
1337 log_history = B_FALSE;
1339 if (cb->hardforce) {
1340 if (zpool_export_force(zhp, history_str) != 0)
1342 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1350 * zpool export [-f] <pool> ...
1352 * -a Export all pools
1353 * -f Forcefully unmount datasets
1355 * Export the given pools. By default, the command will attempt to cleanly
1356 * unmount any active datasets within the pool. If the '-f' flag is specified,
1357 * then the datasets will be forcefully unmounted.
1360 zpool_do_export(int argc, char **argv)
1363 boolean_t do_all = B_FALSE;
1364 boolean_t force = B_FALSE;
1365 boolean_t hardforce = B_FALSE;
1369 while ((c = getopt(argc, argv, "afF")) != -1) {
1381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1388 cb.hardforce = hardforce;
1394 (void) fprintf(stderr, gettext("too many arguments\n"));
1398 return (for_each_pool(argc, argv, B_TRUE, NULL,
1399 zpool_export_one, &cb));
1402 /* check arguments */
1404 (void) fprintf(stderr, gettext("missing pool argument\n"));
1408 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1414 * Given a vdev configuration, determine the maximum width needed for the device
1418 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1426 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1427 if (strlen(name) + depth > max)
1428 max = strlen(name) + depth;
1432 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1433 &child, &children) == 0) {
1434 for (c = 0; c < children; c++)
1435 if ((ret = max_width(zhp, child[c], depth + 2,
1436 max, name_flags)) > max)
1440 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1441 &child, &children) == 0) {
1442 for (c = 0; c < children; c++)
1443 if ((ret = max_width(zhp, child[c], depth + 2,
1444 max, name_flags)) > max)
1448 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1449 &child, &children) == 0) {
1450 for (c = 0; c < children; c++)
1451 if ((ret = max_width(zhp, child[c], depth + 2,
1452 max, name_flags)) > max)
1459 typedef struct spare_cbdata {
1461 zpool_handle_t *cb_zhp;
1465 find_vdev(nvlist_t *nv, uint64_t search)
1471 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1475 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1476 &child, &children) == 0) {
1477 for (c = 0; c < children; c++)
1478 if (find_vdev(child[c], search))
1486 find_spare(zpool_handle_t *zhp, void *data)
1488 spare_cbdata_t *cbp = data;
1489 nvlist_t *config, *nvroot;
1491 config = zpool_get_config(zhp, NULL);
1492 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1495 if (find_vdev(nvroot, cbp->cb_guid)) {
1504 typedef struct status_cbdata {
1508 boolean_t cb_allpools;
1509 boolean_t cb_verbose;
1510 boolean_t cb_explain;
1512 boolean_t cb_dedup_stats;
1513 boolean_t cb_print_status;
1514 vdev_cmd_data_list_t *vcdl;
1517 /* Print output line for specific vdev in a specific pool */
1519 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1522 for (i = 0; i < vcdl->count; i++) {
1523 if ((strcmp(vcdl->data[i].path, path) == 0) &&
1524 (strcmp(vcdl->data[i].pool, pool) == 0)) {
1525 printf("%s", vcdl->data[i].line);
1532 * Print out configuration state as requested by status_callback.
1535 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1536 nvlist_t *nv, int depth, boolean_t isspare)
1540 pool_scan_stat_t *ps = NULL;
1542 char rbuf[6], wbuf[6], cbuf[6];
1544 uint64_t notpresent;
1545 spare_cbdata_t spare_cb;
1549 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1550 &child, &children) != 0)
1553 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1554 (uint64_t **)&vs, &c) == 0);
1556 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1559 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1562 if (vs->vs_aux == VDEV_AUX_SPARED)
1564 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1568 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1572 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1573 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1574 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1575 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1578 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1579 ¬present) == 0) {
1580 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1581 (void) printf(" was %s", path);
1582 } else if (vs->vs_aux != 0) {
1585 switch (vs->vs_aux) {
1586 case VDEV_AUX_OPEN_FAILED:
1587 (void) printf(gettext("cannot open"));
1590 case VDEV_AUX_BAD_GUID_SUM:
1591 (void) printf(gettext("missing device"));
1594 case VDEV_AUX_NO_REPLICAS:
1595 (void) printf(gettext("insufficient replicas"));
1598 case VDEV_AUX_VERSION_NEWER:
1599 (void) printf(gettext("newer version"));
1602 case VDEV_AUX_UNSUP_FEAT:
1603 (void) printf(gettext("unsupported feature(s)"));
1606 case VDEV_AUX_SPARED:
1607 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1608 &spare_cb.cb_guid) == 0);
1609 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1610 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1611 zpool_get_name(zhp)) == 0)
1612 (void) printf(gettext("currently in "
1615 (void) printf(gettext("in use by "
1617 zpool_get_name(spare_cb.cb_zhp));
1618 zpool_close(spare_cb.cb_zhp);
1620 (void) printf(gettext("currently in use"));
1624 case VDEV_AUX_ERR_EXCEEDED:
1625 (void) printf(gettext("too many errors"));
1628 case VDEV_AUX_IO_FAILURE:
1629 (void) printf(gettext("experienced I/O failures"));
1632 case VDEV_AUX_BAD_LOG:
1633 (void) printf(gettext("bad intent log"));
1636 case VDEV_AUX_EXTERNAL:
1637 (void) printf(gettext("external device fault"));
1640 case VDEV_AUX_SPLIT_POOL:
1641 (void) printf(gettext("split into new pool"));
1645 (void) printf(gettext("corrupted data"));
1650 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1651 (uint64_t **)&ps, &c);
1653 if (ps && ps->pss_state == DSS_SCANNING &&
1654 vs->vs_scan_processed != 0 && children == 0) {
1655 (void) printf(gettext(" (%s)"),
1656 (ps->pss_func == POOL_SCAN_RESILVER) ?
1657 "resilvering" : "repairing");
1660 if (cb->vcdl != NULL) {
1661 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1663 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1667 (void) printf("\n");
1669 for (c = 0; c < children; c++) {
1670 uint64_t islog = B_FALSE, ishole = B_FALSE;
1672 /* Don't print logs or holes here */
1673 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1675 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1677 if (islog || ishole)
1679 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1680 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1681 print_status_config(zhp, cb, vname, child[c], depth + 2,
1688 * Print the configuration of an exported pool. Iterate over all vdevs in the
1689 * pool, printing out the name and status for each one.
1692 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1700 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1701 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1702 strcmp(type, VDEV_TYPE_HOLE) == 0)
1705 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1706 (uint64_t **)&vs, &c) == 0);
1708 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1709 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1711 if (vs->vs_aux != 0) {
1714 switch (vs->vs_aux) {
1715 case VDEV_AUX_OPEN_FAILED:
1716 (void) printf(gettext("cannot open"));
1719 case VDEV_AUX_BAD_GUID_SUM:
1720 (void) printf(gettext("missing device"));
1723 case VDEV_AUX_NO_REPLICAS:
1724 (void) printf(gettext("insufficient replicas"));
1727 case VDEV_AUX_VERSION_NEWER:
1728 (void) printf(gettext("newer version"));
1731 case VDEV_AUX_UNSUP_FEAT:
1732 (void) printf(gettext("unsupported feature(s)"));
1735 case VDEV_AUX_ERR_EXCEEDED:
1736 (void) printf(gettext("too many errors"));
1740 (void) printf(gettext("corrupted data"));
1744 (void) printf("\n");
1746 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1747 &child, &children) != 0)
1750 for (c = 0; c < children; c++) {
1751 uint64_t is_log = B_FALSE;
1753 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1758 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1759 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1760 print_import_config(cb, vname, child[c], depth + 2);
1764 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1765 &child, &children) == 0) {
1766 (void) printf(gettext("\tcache\n"));
1767 for (c = 0; c < children; c++) {
1768 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1770 (void) printf("\t %s\n", vname);
1775 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1776 &child, &children) == 0) {
1777 (void) printf(gettext("\tspares\n"));
1778 for (c = 0; c < children; c++) {
1779 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1781 (void) printf("\t %s\n", vname);
1789 * Logs are recorded as top level vdevs in the main pool child array
1790 * but with "is_log" set to 1. We use either print_status_config() or
1791 * print_import_config() to print the top level logs then any log
1792 * children (eg mirrored slogs) are printed recursively - which
1793 * works because only the top level vdev is marked "is_log"
1796 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1801 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1805 (void) printf(gettext("\tlogs\n"));
1807 for (c = 0; c < children; c++) {
1808 uint64_t is_log = B_FALSE;
1811 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1815 name = zpool_vdev_name(g_zfs, zhp, child[c],
1816 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1817 if (cb->cb_print_status)
1818 print_status_config(zhp, cb, name, child[c], 2,
1821 print_import_config(cb, name, child[c], 2);
1827 * Display the status for the given pool.
1830 show_import(nvlist_t *config)
1832 uint64_t pool_state;
1838 zpool_status_t reason;
1839 zpool_errata_t errata;
1843 status_cbdata_t cb = { 0 };
1845 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1847 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1849 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1851 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1854 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1855 (uint64_t **)&vs, &vsc) == 0);
1856 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1858 reason = zpool_import_status(config, &msgid, &errata);
1860 (void) printf(gettext(" pool: %s\n"), name);
1861 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1862 (void) printf(gettext(" state: %s"), health);
1863 if (pool_state == POOL_STATE_DESTROYED)
1864 (void) printf(gettext(" (DESTROYED)"));
1865 (void) printf("\n");
1868 case ZPOOL_STATUS_MISSING_DEV_R:
1869 case ZPOOL_STATUS_MISSING_DEV_NR:
1870 case ZPOOL_STATUS_BAD_GUID_SUM:
1871 (void) printf(gettext(" status: One or more devices are "
1872 "missing from the system.\n"));
1875 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1876 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1877 (void) printf(gettext(" status: One or more devices contains "
1878 "corrupted data.\n"));
1881 case ZPOOL_STATUS_CORRUPT_DATA:
1883 gettext(" status: The pool data is corrupted.\n"));
1886 case ZPOOL_STATUS_OFFLINE_DEV:
1887 (void) printf(gettext(" status: One or more devices "
1888 "are offlined.\n"));
1891 case ZPOOL_STATUS_CORRUPT_POOL:
1892 (void) printf(gettext(" status: The pool metadata is "
1896 case ZPOOL_STATUS_VERSION_OLDER:
1897 (void) printf(gettext(" status: The pool is formatted using a "
1898 "legacy on-disk version.\n"));
1901 case ZPOOL_STATUS_VERSION_NEWER:
1902 (void) printf(gettext(" status: The pool is formatted using an "
1903 "incompatible version.\n"));
1906 case ZPOOL_STATUS_FEAT_DISABLED:
1907 (void) printf(gettext(" status: Some supported features are "
1908 "not enabled on the pool.\n"));
1911 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1912 (void) printf(gettext("status: The pool uses the following "
1913 "feature(s) not supported on this system:\n"));
1914 zpool_print_unsup_feat(config);
1917 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1918 (void) printf(gettext("status: The pool can only be accessed "
1919 "in read-only mode on this system. It\n\tcannot be "
1920 "accessed in read-write mode because it uses the "
1921 "following\n\tfeature(s) not supported on this system:\n"));
1922 zpool_print_unsup_feat(config);
1925 case ZPOOL_STATUS_HOSTID_MISMATCH:
1926 (void) printf(gettext(" status: The pool was last accessed by "
1927 "another system.\n"));
1930 case ZPOOL_STATUS_FAULTED_DEV_R:
1931 case ZPOOL_STATUS_FAULTED_DEV_NR:
1932 (void) printf(gettext(" status: One or more devices are "
1936 case ZPOOL_STATUS_BAD_LOG:
1937 (void) printf(gettext(" status: An intent log record cannot be "
1941 case ZPOOL_STATUS_RESILVERING:
1942 (void) printf(gettext(" status: One or more devices were being "
1946 case ZPOOL_STATUS_ERRATA:
1947 (void) printf(gettext(" status: Errata #%d detected.\n"),
1953 * No other status can be seen when importing pools.
1955 assert(reason == ZPOOL_STATUS_OK);
1959 * Print out an action according to the overall state of the pool.
1961 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1962 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1963 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1964 (void) printf(gettext(" action: The pool can be "
1965 "imported using its name or numeric identifier, "
1966 "though\n\tsome features will not be available "
1967 "without an explicit 'zpool upgrade'.\n"));
1968 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1969 (void) printf(gettext(" action: The pool can be "
1970 "imported using its name or numeric "
1971 "identifier and\n\tthe '-f' flag.\n"));
1972 } else if (reason == ZPOOL_STATUS_ERRATA) {
1974 case ZPOOL_ERRATA_NONE:
1977 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1978 (void) printf(gettext(" action: The pool can "
1979 "be imported using its name or numeric "
1980 "identifier,\n\thowever there is a compat"
1981 "ibility issue which should be corrected"
1982 "\n\tby running 'zpool scrub'\n"));
1985 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1986 (void) printf(gettext(" action: The pool can"
1987 "not be imported with this version of ZFS "
1988 "due to\n\tan active asynchronous destroy. "
1989 "Revert to an earlier version\n\tand "
1990 "allow the destroy to complete before "
1996 * All errata must contain an action message.
2001 (void) printf(gettext(" action: The pool can be "
2002 "imported using its name or numeric "
2005 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2006 (void) printf(gettext(" action: The pool can be imported "
2007 "despite missing or damaged devices. The\n\tfault "
2008 "tolerance of the pool may be compromised if imported.\n"));
2011 case ZPOOL_STATUS_VERSION_NEWER:
2012 (void) printf(gettext(" action: The pool cannot be "
2013 "imported. Access the pool on a system running "
2014 "newer\n\tsoftware, or recreate the pool from "
2017 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2018 (void) printf(gettext("action: The pool cannot be "
2019 "imported. Access the pool on a system that "
2020 "supports\n\tthe required feature(s), or recreate "
2021 "the pool from backup.\n"));
2023 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2024 (void) printf(gettext("action: The pool cannot be "
2025 "imported in read-write mode. Import the pool "
2027 "\t\"-o readonly=on\", access the pool on a system "
2028 "that supports the\n\trequired feature(s), or "
2029 "recreate the pool from backup.\n"));
2031 case ZPOOL_STATUS_MISSING_DEV_R:
2032 case ZPOOL_STATUS_MISSING_DEV_NR:
2033 case ZPOOL_STATUS_BAD_GUID_SUM:
2034 (void) printf(gettext(" action: The pool cannot be "
2035 "imported. Attach the missing\n\tdevices and try "
2039 (void) printf(gettext(" action: The pool cannot be "
2040 "imported due to damaged devices or data.\n"));
2044 /* Print the comment attached to the pool. */
2045 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2046 (void) printf(gettext("comment: %s\n"), comment);
2049 * If the state is "closed" or "can't open", and the aux state
2050 * is "corrupt data":
2052 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2053 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2054 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2055 if (pool_state == POOL_STATE_DESTROYED)
2056 (void) printf(gettext("\tThe pool was destroyed, "
2057 "but can be imported using the '-Df' flags.\n"));
2058 else if (pool_state != POOL_STATE_EXPORTED)
2059 (void) printf(gettext("\tThe pool may be active on "
2060 "another system, but can be imported using\n\t"
2061 "the '-f' flag.\n"));
2065 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2068 (void) printf(gettext(" config:\n\n"));
2070 cb.cb_namewidth = max_width(NULL, nvroot, 0, 0, VDEV_NAME_TYPE_ID);
2071 if (cb.cb_namewidth < 10)
2072 cb.cb_namewidth = 10;
2074 print_import_config(&cb, name, nvroot, 0);
2075 if (num_logs(nvroot) > 0)
2076 print_logs(NULL, &cb, nvroot);
2078 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2079 (void) printf(gettext("\n\tAdditional devices are known to "
2080 "be part of this pool, though their\n\texact "
2081 "configuration cannot be determined.\n"));
2086 * Perform the import for the given configuration. This passes the heavy
2087 * lifting off to zpool_import_props(), and then mounts the datasets contained
2091 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2092 nvlist_t *props, int flags)
2094 zpool_handle_t *zhp;
2099 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2102 verify(nvlist_lookup_uint64(config,
2103 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2104 verify(nvlist_lookup_uint64(config,
2105 ZPOOL_CONFIG_VERSION, &version) == 0);
2106 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2107 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2108 "is formatted using an unsupported ZFS version\n"), name);
2110 } else if (state != POOL_STATE_EXPORTED &&
2111 !(flags & ZFS_IMPORT_ANY_HOST)) {
2112 uint64_t hostid = 0;
2113 unsigned long system_hostid = get_system_hostid();
2115 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
2118 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
2123 verify(nvlist_lookup_string(config,
2124 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2125 verify(nvlist_lookup_uint64(config,
2126 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
2128 (void) fprintf(stderr, gettext("cannot import "
2129 "'%s': pool may be in use from other "
2130 "system, it was last accessed by %s "
2131 "(hostid: 0x%lx) on %s"), name, hostname,
2132 (unsigned long)hostid,
2133 asctime(localtime(&t)));
2134 (void) fprintf(stderr, gettext("use '-f' to "
2135 "import anyway\n"));
2140 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2143 if (newname != NULL)
2144 name = (char *)newname;
2146 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2149 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2150 !(flags & ZFS_IMPORT_ONLY) &&
2151 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2161 * zpool import [-d dir] [-D]
2162 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2163 * [-d dir | -c cachefile] [-f] -a
2164 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2165 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2167 * -c Read pool information from a cachefile instead of searching
2170 * -d Scan in a specific directory, other than /dev/. More than
2171 * one directory can be specified using multiple '-d' options.
2173 * -D Scan for previously destroyed pools or import all or only
2174 * specified destroyed pools.
2176 * -R Temporarily import the pool, with all mountpoints relative to
2177 * the given root. The pool will remain exported when the machine
2180 * -V Import even in the presence of faulted vdevs. This is an
2181 * intentionally undocumented option for testing purposes, and
2182 * treats the pool configuration as complete, leaving any bad
2183 * vdevs in the FAULTED state. In other words, it does verbatim
2186 * -f Force import, even if it appears that the pool is active.
2188 * -F Attempt rewind if necessary.
2190 * -n See if rewind would work, but don't actually rewind.
2192 * -N Import the pool but don't mount datasets.
2194 * -T Specify a starting txg to use for import. This option is
2195 * intentionally undocumented option for testing purposes.
2197 * -a Import all pools found.
2199 * -o Set property=value and/or temporary mount options (without '=').
2201 * -s Scan using the default search path, the libblkid cache will
2204 * The import command scans for pools to import, and import pools based on pool
2205 * name and GUID. The pool can also be renamed as part of the import process.
2208 zpool_do_import(int argc, char **argv)
2210 char **searchdirs = NULL;
2211 char *env, *envdup = NULL;
2215 nvlist_t *pools = NULL;
2216 boolean_t do_all = B_FALSE;
2217 boolean_t do_destroyed = B_FALSE;
2218 char *mntopts = NULL;
2221 uint64_t searchguid = 0;
2222 char *searchname = NULL;
2224 nvlist_t *found_config;
2225 nvlist_t *policy = NULL;
2226 nvlist_t *props = NULL;
2228 int flags = ZFS_IMPORT_NORMAL;
2229 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2230 boolean_t dryrun = B_FALSE;
2231 boolean_t do_rewind = B_FALSE;
2232 boolean_t xtreme_rewind = B_FALSE;
2233 boolean_t do_scan = B_FALSE;
2234 uint64_t pool_state, txg = -1ULL;
2235 char *cachefile = NULL;
2236 importargs_t idata = { 0 };
2240 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2249 if (searchdirs == NULL) {
2250 searchdirs = safe_malloc(sizeof (char *));
2252 char **tmp = safe_malloc((nsearch + 1) *
2254 bcopy(searchdirs, tmp, nsearch *
2259 searchdirs[nsearch++] = optarg;
2262 do_destroyed = B_TRUE;
2265 flags |= ZFS_IMPORT_ANY_HOST;
2271 flags |= ZFS_IMPORT_MISSING_LOG;
2277 flags |= ZFS_IMPORT_ONLY;
2280 if ((propval = strchr(optarg, '=')) != NULL) {
2283 if (add_prop_list(optarg, propval,
2291 if (add_prop_list(zpool_prop_to_name(
2292 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2294 if (add_prop_list_default(zpool_prop_to_name(
2295 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2302 flags |= ZFS_IMPORT_TEMP_NAME;
2303 if (add_prop_list_default(zpool_prop_to_name(
2304 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2310 txg = strtoull(optarg, &endptr, 0);
2311 if (errno != 0 || *endptr != '\0') {
2312 (void) fprintf(stderr,
2313 gettext("invalid txg value\n"));
2316 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2319 flags |= ZFS_IMPORT_VERBATIM;
2322 xtreme_rewind = B_TRUE;
2325 (void) fprintf(stderr, gettext("missing argument for "
2326 "'%c' option\n"), optopt);
2330 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2339 if (cachefile && nsearch != 0) {
2340 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2344 if ((dryrun || xtreme_rewind) && !do_rewind) {
2345 (void) fprintf(stderr,
2346 gettext("-n or -X only meaningful with -F\n"));
2350 rewind_policy = ZPOOL_TRY_REWIND;
2352 rewind_policy = ZPOOL_DO_REWIND;
2354 rewind_policy |= ZPOOL_EXTREME_REWIND;
2356 /* In the future, we can capture further policy and include it here */
2357 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2358 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2359 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2362 /* check argument count */
2365 (void) fprintf(stderr, gettext("too many arguments\n"));
2370 (void) fprintf(stderr, gettext("too many arguments\n"));
2376 * Check for the effective uid. We do this explicitly here because
2377 * otherwise any attempt to discover pools will silently fail.
2379 if (argc == 0 && geteuid() != 0) {
2380 (void) fprintf(stderr, gettext("cannot "
2381 "discover pools: permission denied\n"));
2382 if (searchdirs != NULL)
2386 nvlist_free(policy);
2391 * Depending on the arguments given, we do one of the following:
2393 * <none> Iterate through all pools and display information about
2396 * -a Iterate through all pools and try to import each one.
2398 * <id> Find the pool that corresponds to the given GUID/pool
2399 * name and import that one.
2401 * -D Above options applies only to destroyed pools.
2407 searchguid = strtoull(argv[0], &endptr, 10);
2408 if (errno != 0 || *endptr != '\0') {
2409 searchname = argv[0];
2412 found_config = NULL;
2415 * User specified a name or guid. Ensure it's unique.
2417 idata.unique = B_TRUE;
2421 * Check the environment for the preferred search path.
2423 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2426 envdup = strdup(env);
2428 dir = strtok(envdup, ":");
2429 while (dir != NULL) {
2430 if (searchdirs == NULL) {
2431 searchdirs = safe_malloc(sizeof (char *));
2433 char **tmp = safe_malloc((nsearch + 1) *
2435 bcopy(searchdirs, tmp, nsearch *
2440 searchdirs[nsearch++] = dir;
2441 dir = strtok(NULL, ":");
2445 idata.path = searchdirs;
2446 idata.paths = nsearch;
2447 idata.poolname = searchname;
2448 idata.guid = searchguid;
2449 idata.cachefile = cachefile;
2450 idata.scan = do_scan;
2453 * Under Linux the zpool_find_import_impl() function leverages the
2454 * taskq implementation to parallelize device scanning. It is
2455 * therefore necessary to initialize this functionality for the
2456 * duration of the zpool_search_import() function.
2459 pools = zpool_search_import(g_zfs, &idata);
2462 if (pools != NULL && idata.exists &&
2463 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2464 (void) fprintf(stderr, gettext("cannot import '%s': "
2465 "a pool with that name already exists\n"),
2467 (void) fprintf(stderr, gettext("use the form '%s "
2468 "<pool | id> <newpool>' to give it a new name\n"),
2471 } else if (pools == NULL && idata.exists) {
2472 (void) fprintf(stderr, gettext("cannot import '%s': "
2473 "a pool with that name is already created/imported,\n"),
2475 (void) fprintf(stderr, gettext("and no additional pools "
2476 "with that name were found\n"));
2478 } else if (pools == NULL) {
2480 (void) fprintf(stderr, gettext("cannot import '%s': "
2481 "no such pool available\n"), argv[0]);
2487 if (searchdirs != NULL)
2491 nvlist_free(policy);
2498 * At this point we have a list of import candidate configs. Even if
2499 * we were searching by pool name or guid, we still need to
2500 * post-process the list to deal with pool state and possible
2506 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2508 verify(nvpair_value_nvlist(elem, &config) == 0);
2510 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2512 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2514 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2517 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2524 (void) printf("\n");
2527 err |= do_import(config, NULL, mntopts,
2530 show_import(config);
2532 } else if (searchname != NULL) {
2536 * We are searching for a pool based on name.
2538 verify(nvlist_lookup_string(config,
2539 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2541 if (strcmp(name, searchname) == 0) {
2542 if (found_config != NULL) {
2543 (void) fprintf(stderr, gettext(
2544 "cannot import '%s': more than "
2545 "one matching pool\n"), searchname);
2546 (void) fprintf(stderr, gettext(
2547 "import by numeric ID instead\n"));
2550 found_config = config;
2556 * Search for a pool by guid.
2558 verify(nvlist_lookup_uint64(config,
2559 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2561 if (guid == searchguid)
2562 found_config = config;
2567 * If we were searching for a specific pool, verify that we found a
2568 * pool, and then do the import.
2570 if (argc != 0 && err == 0) {
2571 if (found_config == NULL) {
2572 (void) fprintf(stderr, gettext("cannot import '%s': "
2573 "no such pool available\n"), argv[0]);
2576 err |= do_import(found_config, argc == 1 ? NULL :
2577 argv[1], mntopts, props, flags);
2582 * If we were just looking for pools, report an error if none were
2585 if (argc == 0 && first)
2586 (void) fprintf(stderr,
2587 gettext("no pools available to import\n"));
2592 nvlist_free(policy);
2593 if (searchdirs != NULL)
2598 return (err ? 1 : 0);
2601 typedef struct iostat_cbdata {
2606 char **cb_vdev_names; /* Only show these vdevs */
2607 unsigned int cb_vdev_names_count;
2608 boolean_t cb_verbose;
2609 boolean_t cb_literal;
2610 boolean_t cb_scripted;
2611 zpool_list_t *cb_list;
2612 vdev_cmd_data_list_t *vcdl;
2616 typedef struct name_and_columns {
2617 const char *name; /* Column name */
2618 unsigned int columns; /* Center name to this number of columns */
2619 } name_and_columns_t;
2621 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2623 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2625 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2627 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2628 {"asyncq_wait", 2}, {"scrub"}},
2629 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2630 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2632 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2633 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2634 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2635 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2639 /* Shorthand - if "columns" field not set, default to 1 column */
2640 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2642 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2644 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2645 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2646 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2647 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2648 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2649 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2650 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2651 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2654 static const char *histo_to_title[] = {
2655 [IOS_L_HISTO] = "latency",
2656 [IOS_RQ_HISTO] = "req_size",
2660 * Return the number of labels in a null-terminated name_and_columns_t
2665 label_array_len(const name_and_columns_t *labels)
2669 while (labels[i].name)
2676 * Return the number of strings in a null-terminated string array.
2679 * const char foo[] = {"bar", "baz", NULL}
2684 str_array_len(const char *array[])
2695 * Return a default column width for default/latency/queue columns. This does
2696 * not include histograms, which have their columns autosized.
2699 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2701 unsigned long column_width = 5; /* Normal niceprint */
2702 static unsigned long widths[] = {
2704 * Choose some sane default column sizes for printing the
2707 [IOS_DEFAULT] = 15, /* 1PB capacity */
2708 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2709 [IOS_QUEUES] = 6, /* 1M queue entries */
2713 column_width = widths[type];
2715 return (column_width);
2719 * Print the column labels, i.e:
2721 * capacity operations bandwidth
2722 * alloc free read write read write ...
2724 * If force_column_width is set, use it for the column width. If not set, use
2725 * the default column width.
2728 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2729 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2732 unsigned int text_start, rw_column_width, spaces_to_end;
2733 uint64_t flags = cb->cb_flags;
2735 unsigned int column_width = force_column_width;
2737 /* For each bit set in flags */
2738 for (f = flags; f; f &= ~(1ULL << idx)) {
2739 idx = lowbit64(f) - 1;
2740 if (!force_column_width)
2741 column_width = default_column_width(cb, idx);
2742 /* Print our top labels centered over "read write" label. */
2743 for (i = 0; i < label_array_len(labels[idx]); i++) {
2744 const char *name = labels[idx][i].name;
2746 * We treat labels[][].columns == 0 as shorthand
2747 * for one column. It makes writing out the label
2748 * tables more concise.
2750 unsigned int columns = MAX(1, labels[idx][i].columns);
2751 unsigned int slen = strlen(name);
2753 rw_column_width = (column_width * columns) +
2754 (2 * (columns - 1));
2756 text_start = (int)((rw_column_width)/columns -
2759 printf(" "); /* Two spaces between columns */
2761 /* Space from beginning of column to label */
2762 for (s = 0; s < text_start; s++)
2767 /* Print space after label to end of column */
2768 spaces_to_end = rw_column_width - text_start - slen;
2769 for (s = 0; s < spaces_to_end; s++)
2778 * Utility function to print out a line of dashes like:
2780 * -------------------------------- ----- ----- ----- ----- -----
2782 * ...or a dashed named-row line like:
2788 * @force_column_width If non-zero, use the value as the column width.
2789 * Otherwise use the default column widths.
2791 * @name: Print a dashed named-row line starting
2792 * with @name. Otherwise, print a regular
2796 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
2800 unsigned int namewidth;
2801 uint64_t flags = cb->cb_flags;
2804 const name_and_columns_t *labels;
2808 if (cb->cb_flags & IOS_ANYHISTO_M) {
2809 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2810 } else if (cb->cb_vdev_names_count) {
2816 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2817 name ? strlen(name) : 0);
2821 printf("%-*s", namewidth, name);
2823 for (i = 0; i < namewidth; i++)
2827 /* For each bit in flags */
2828 for (f = flags; f; f &= ~(1ULL << idx)) {
2829 unsigned int column_width;
2830 idx = lowbit64(f) - 1;
2831 if (force_column_width)
2832 column_width = force_column_width;
2834 column_width = default_column_width(cb, idx);
2836 labels = iostat_bottom_labels[idx];
2837 for (i = 0; i < label_array_len(labels); i++) {
2839 printf(" %*s-", column_width - 1, " ");
2841 printf(" %.*s", column_width,
2842 "--------------------");
2850 print_iostat_separator_impl(iostat_cbdata_t *cb,
2851 unsigned int force_column_width)
2853 print_iostat_dashes(cb, force_column_width, NULL);
2857 print_iostat_separator(iostat_cbdata_t *cb)
2859 print_iostat_separator_impl(cb, 0);
2863 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
2864 const char *histo_vdev_name)
2866 unsigned int namewidth;
2869 if (cb->cb_flags & IOS_ANYHISTO_M) {
2870 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2871 } else if (cb->cb_vdev_names_count) {
2877 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2878 histo_vdev_name ? strlen(histo_vdev_name) : 0);
2880 if (histo_vdev_name)
2881 printf("%-*s", namewidth, histo_vdev_name);
2883 printf("%*s", namewidth, "");
2886 print_iostat_labels(cb, force_column_width, iostat_top_labels);
2888 printf("%-*s", namewidth, title);
2890 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
2892 print_iostat_separator_impl(cb, force_column_width);
2896 print_iostat_header(iostat_cbdata_t *cb)
2898 print_iostat_header_impl(cb, 0, NULL);
2903 * Display a single statistic.
2906 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
2907 unsigned int column_size, boolean_t scripted)
2911 zfs_nicenum_format(value, buf, sizeof (buf), format);
2914 printf("\t%s", buf);
2916 printf(" %*s", column_size, buf);
2920 * Calculate the default vdev stats
2922 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
2923 * stats into calcvs.
2926 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
2927 vdev_stat_t *calcvs)
2931 memcpy(calcvs, newvs, sizeof (*calcvs));
2932 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
2933 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
2935 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
2936 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
2940 * Internal representation of the extended iostats data.
2942 * The extended iostat stats are exported in nvlists as either uint64_t arrays
2943 * or single uint64_t's. We make both look like arrays to make them easier
2944 * to process. In order to make single uint64_t's look like arrays, we set
2945 * __data to the stat data, and then set *data = &__data with count = 1. Then,
2946 * we can just use *data and count.
2950 uint_t count; /* Number of entries in data[] */
2951 uint64_t __data; /* Only used when data is a single uint64_t */
2955 stat_histo_max(struct stat_array *nva, unsigned int len) {
2958 for (i = 0; i < len; i++)
2959 max = MAX(max, array64_max(nva[i].data, nva[i].count));
2965 * Helper function to lookup a uint64_t array or uint64_t value and store its
2966 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
2967 * it look like a one element array to make it easier to process.
2970 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
2971 struct stat_array *nva) {
2975 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
2976 switch (nvpair_type(tmp)) {
2977 case DATA_TYPE_UINT64_ARRAY:
2978 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
2980 case DATA_TYPE_UINT64:
2981 ret = nvpair_value_uint64(tmp, &nva->__data);
2982 nva->data = &nva->__data;
2986 /* Not a uint64_t */
2995 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
2996 * subtract them, and return the results in a newly allocated stat_array.
2997 * You must free the returned array after you are done with it with
2998 * free_calc_stats().
3000 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3003 static struct stat_array *
3004 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3007 nvlist_t *oldnvx = NULL, *newnvx;
3008 struct stat_array *oldnva, *newnva, *calcnva;
3010 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3012 /* Extract our extended stats nvlist from the main list */
3013 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3016 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3020 newnva = safe_malloc(alloc_size);
3021 oldnva = safe_malloc(alloc_size);
3022 calcnva = safe_malloc(alloc_size);
3024 for (j = 0; j < len; j++) {
3025 verify(nvpair64_to_stat_array(newnvx, names[j],
3027 calcnva[j].count = newnva[j].count;
3028 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3029 calcnva[j].data = safe_malloc(alloc_size);
3030 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3033 verify(nvpair64_to_stat_array(oldnvx, names[j],
3035 for (i = 0; i < oldnva[j].count; i++)
3036 calcnva[j].data[i] -= oldnva[j].data[i];
3045 free_calc_stats(struct stat_array *nva, unsigned int len)
3048 for (i = 0; i < len; i++)
3055 print_iostat_histo(struct stat_array *nva, unsigned int len,
3056 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3062 enum zfs_nicenum_format format;
3063 unsigned int buckets;
3064 unsigned int start_bucket;
3067 format = ZFS_NICENUM_RAW;
3069 format = ZFS_NICENUM_1024;
3071 /* All these histos are the same size, so just use nva[0].count */
3072 buckets = nva[0].count;
3074 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3075 /* Start at 512 - req size should never be lower than this */
3081 for (j = start_bucket; j < buckets; j++) {
3082 /* Print histogram bucket label */
3083 if (cb->cb_flags & IOS_L_HISTO_M) {
3084 /* Ending range of this bucket */
3085 val = (1UL << (j + 1)) - 1;
3086 zfs_nicetime(val, buf, sizeof (buf));
3088 /* Request size (starting range of bucket) */
3090 zfs_nicenum(val, buf, sizeof (buf));
3093 if (cb->cb_scripted)
3094 printf("%llu", (u_longlong_t)val);
3096 printf("%-*s", namewidth, buf);
3098 /* Print the values on the line */
3099 for (i = 0; i < len; i++) {
3100 print_one_stat(nva[i].data[j] * scale, format,
3101 column_width, cb->cb_scripted);
3108 print_solid_separator(unsigned int length)
3116 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3117 nvlist_t *newnv, double scale, const char *name)
3119 unsigned int column_width;
3120 unsigned int namewidth;
3121 unsigned int entire_width;
3122 enum iostat_type type;
3123 struct stat_array *nva;
3125 unsigned int names_len;
3127 /* What type of histo are we? */
3128 type = IOS_HISTO_IDX(cb->cb_flags);
3130 /* Get NULL-terminated array of nvlist names for our histo */
3131 names = vsx_type_to_nvlist[type];
3132 names_len = str_array_len(names); /* num of names */
3134 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3136 if (cb->cb_literal) {
3137 column_width = MAX(5,
3138 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3143 namewidth = MAX(cb->cb_namewidth,
3144 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3147 * Calculate the entire line width of what we're printing. The
3148 * +2 is for the two spaces between columns:
3152 /* |___| <---------- column_width */
3154 /* |__________| <--- entire_width */
3156 entire_width = namewidth + (column_width + 2) *
3157 label_array_len(iostat_bottom_labels[type]);
3159 if (cb->cb_scripted)
3160 printf("%s\n", name);
3162 print_iostat_header_impl(cb, column_width, name);
3164 print_iostat_histo(nva, names_len, cb, column_width,
3167 free_calc_stats(nva, names_len);
3168 if (!cb->cb_scripted)
3169 print_solid_separator(entire_width);
3173 * Calculate the average latency of a power-of-two latency histogram
3176 single_histo_average(uint64_t *histo, unsigned int buckets)
3179 uint64_t count = 0, total = 0;
3181 for (i = 0; i < buckets; i++) {
3183 * Our buckets are power-of-two latency ranges. Use the
3184 * midpoint latency of each bucket to calculate the average.
3192 if (histo[i] != 0) {
3193 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3198 /* Prevent divide by zero */
3199 return (count == 0 ? 0 : total / count);
3203 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3204 nvlist_t *newnv, double scale)
3208 const char *names[] = {
3209 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3210 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3211 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3212 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3213 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3214 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3215 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3216 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3217 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3218 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3221 struct stat_array *nva;
3223 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3224 enum zfs_nicenum_format format;
3226 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3229 format = ZFS_NICENUM_RAW;
3231 format = ZFS_NICENUM_1024;
3233 for (i = 0; i < ARRAY_SIZE(names); i++) {
3234 val = nva[i].data[0] * scale;
3235 print_one_stat(val, format, column_width, cb->cb_scripted);
3238 free_calc_stats(nva, ARRAY_SIZE(names));
3242 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3243 nvlist_t *newnv, double scale)
3247 const char *names[] = {
3248 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3249 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3250 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3251 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3252 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3253 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3254 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3255 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3256 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3258 struct stat_array *nva;
3260 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3261 enum zfs_nicenum_format format;
3263 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3266 format = ZFS_NICENUM_RAW;
3268 format = ZFS_NICENUM_TIME;
3270 /* Print our avg latencies on the line */
3271 for (i = 0; i < ARRAY_SIZE(names); i++) {
3272 /* Compute average latency for a latency histo */
3273 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3274 print_one_stat(val, format, column_width, cb->cb_scripted);
3276 free_calc_stats(nva, ARRAY_SIZE(names));
3280 * Print default statistics (capacity/operations/bandwidth)
3283 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3285 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3286 enum zfs_nicenum_format format;
3287 char na; /* char to print for "not applicable" values */
3289 if (cb->cb_literal) {
3290 format = ZFS_NICENUM_RAW;
3293 format = ZFS_NICENUM_1024;
3297 /* only toplevel vdevs have capacity stats */
3298 if (vs->vs_space == 0) {
3299 if (cb->cb_scripted)
3300 printf("\t%c\t%c", na, na);
3302 printf(" %*c %*c", column_width, na, column_width,
3305 print_one_stat(vs->vs_alloc, format, column_width,
3307 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3308 column_width, cb->cb_scripted);
3311 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3312 format, column_width, cb->cb_scripted);
3313 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3314 format, column_width, cb->cb_scripted);
3315 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3316 format, column_width, cb->cb_scripted);
3317 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3318 format, column_width, cb->cb_scripted);
3322 * Print out all the statistics for the given vdev. This can either be the
3323 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3324 * is a verbose output, and we don't want to display the toplevel pool stats.
3326 * Returns the number of stat lines printed.
3329 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3330 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3332 nvlist_t **oldchild, **newchild;
3334 vdev_stat_t *oldvs, *newvs, *calcvs;
3335 vdev_stat_t zerovs = { 0 };
3342 calcvs = safe_malloc(sizeof (*calcvs));
3344 if (oldnv != NULL) {
3345 verify(nvlist_lookup_uint64_array(oldnv,
3346 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3351 /* Do we only want to see a specific vdev? */
3352 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3353 /* Yes we do. Is this the vdev? */
3354 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3356 * This is our vdev. Since it is the only vdev we
3357 * will be displaying, make depth = 0 so that it
3358 * doesn't get indented.
3365 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3366 /* Couldn't match the name */
3371 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3372 (uint64_t **)&newvs, &c) == 0);
3375 * Print the vdev name unless it's is a histogram. Histograms
3376 * display the vdev name in the header itself.
3378 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3379 if (cb->cb_scripted) {
3382 if (strlen(name) + depth > cb->cb_namewidth)
3383 (void) printf("%*s%s", depth, "", name);
3385 (void) printf("%*s%s%*s", depth, "", name,
3386 (int)(cb->cb_namewidth - strlen(name) -
3391 /* Calculate our scaling factor */
3392 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3393 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3395 * If we specify printing histograms with no time interval, then
3396 * print the histogram numbers over the entire lifetime of the
3404 scale = (double)NANOSEC / tdelta;
3407 if (cb->cb_flags & IOS_DEFAULT_M) {
3408 calc_default_iostats(oldvs, newvs, calcvs);
3409 print_iostat_default(calcvs, cb, scale);
3411 if (cb->cb_flags & IOS_LATENCY_M)
3412 print_iostat_latency(cb, oldnv, newnv, scale);
3413 if (cb->cb_flags & IOS_QUEUES_M)
3414 print_iostat_queues(cb, oldnv, newnv, scale);
3415 if (cb->cb_flags & IOS_ANYHISTO_M) {
3417 print_iostat_histos(cb, oldnv, newnv, scale, name);
3420 if (cb->vcdl != NULL) {
3422 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3424 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3426 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3427 if (cb->cb_flags & IOS_ANYHISTO_M)
3432 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3441 if (!cb->cb_verbose)
3444 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3445 &newchild, &children) != 0)
3448 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3449 &oldchild, &c) != 0)
3452 for (c = 0; c < children; c++) {
3453 uint64_t ishole = B_FALSE, islog = B_FALSE;
3455 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3458 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3461 if (ishole || islog)
3464 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3466 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3467 newchild[c], cb, depth + 2);
3472 * Log device section
3475 if (num_logs(newnv) > 0) {
3476 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3477 !cb->cb_vdev_names) {
3478 print_iostat_dashes(cb, 0, "logs");
3481 for (c = 0; c < children; c++) {
3482 uint64_t islog = B_FALSE;
3483 (void) nvlist_lookup_uint64(newchild[c],
3484 ZPOOL_CONFIG_IS_LOG, &islog);
3487 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3489 ret += print_vdev_stats(zhp, vname, oldnv ?
3490 oldchild[c] : NULL, newchild[c],
3499 * Include level 2 ARC devices in iostat output
3501 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3502 &newchild, &children) != 0)
3505 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3506 &oldchild, &c) != 0)
3510 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3511 !cb->cb_vdev_names) {
3512 print_iostat_dashes(cb, 0, "cache");
3515 for (c = 0; c < children; c++) {
3516 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3518 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3519 : NULL, newchild[c], cb, depth + 2);
3528 refresh_iostat(zpool_handle_t *zhp, void *data)
3530 iostat_cbdata_t *cb = data;
3534 * If the pool has disappeared, remove it from the list and continue.
3536 if (zpool_refresh_stats(zhp, &missing) != 0)
3540 pool_list_remove(cb->cb_list, zhp);
3546 * Callback to print out the iostats for the given pool.
3549 print_iostat(zpool_handle_t *zhp, void *data)
3551 iostat_cbdata_t *cb = data;
3552 nvlist_t *oldconfig, *newconfig;
3553 nvlist_t *oldnvroot, *newnvroot;
3556 newconfig = zpool_get_config(zhp, &oldconfig);
3558 if (cb->cb_iteration == 1)
3561 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3564 if (oldconfig == NULL)
3567 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3570 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3572 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3573 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3574 print_iostat_separator(cb);
3587 if (isatty(STDOUT_FILENO)) {
3588 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3590 columns = ws.ws_col;
3599 get_namewidth(zpool_handle_t *zhp, void *data)
3601 iostat_cbdata_t *cb = data;
3602 nvlist_t *config, *nvroot;
3605 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3606 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3608 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3609 if (!cb->cb_verbose)
3610 cb->cb_namewidth = poolname_len;
3612 cb->cb_namewidth = MAX(poolname_len,
3613 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3614 cb->cb_name_flags));
3617 * The width must be at least 10, but may be as large as the
3618 * column width - 42 so that we can still fit in one line.
3620 columns = get_columns();
3622 if (cb->cb_namewidth < 10)
3623 cb->cb_namewidth = 10;
3624 if (cb->cb_namewidth > columns - 42)
3625 cb->cb_namewidth = columns - 42;
3631 * Parse the input string, get the 'interval' and 'count' value if there is one.
3634 get_interval_count(int *argcp, char **argv, float *iv,
3638 unsigned long count = 0;
3642 * Determine if the last argument is an integer or a pool name
3644 if (argc > 0 && isnumber(argv[argc - 1])) {
3648 interval = strtof(argv[argc - 1], &end);
3650 if (*end == '\0' && errno == 0) {
3651 if (interval == 0) {
3652 (void) fprintf(stderr, gettext("interval "
3653 "cannot be zero\n"));
3657 * Ignore the last parameter
3662 * If this is not a valid number, just plow on. The
3663 * user will get a more informative error message later
3671 * If the last argument is also an integer, then we have both a count
3674 if (argc > 0 && isnumber(argv[argc - 1])) {
3679 interval = strtof(argv[argc - 1], &end);
3681 if (*end == '\0' && errno == 0) {
3682 if (interval == 0) {
3683 (void) fprintf(stderr, gettext("interval "
3684 "cannot be zero\n"));
3689 * Ignore the last parameter
3703 get_timestamp_arg(char c)
3706 timestamp_fmt = UDATE;
3708 timestamp_fmt = DDATE;
3714 * Return stat flags that are supported by all pools by both the module and
3715 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3716 * It will get ANDed down until only the flags that are supported on all pools
3720 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
3722 uint64_t *mask = data;
3723 nvlist_t *config, *nvroot, *nvx;
3727 config = zpool_get_config(zhp, NULL);
3728 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3731 /* Default stats are always supported, but for completeness.. */
3732 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
3733 flags |= IOS_DEFAULT_M;
3735 /* Get our extended stats nvlist from the main list */
3736 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
3739 * No extended stats; they're probably running an older
3740 * module. No big deal, we support that too.
3745 /* For each extended stat, make sure all its nvpairs are supported */
3746 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
3747 if (!vsx_type_to_nvlist[j][0])
3750 /* Start off by assuming the flag is supported, then check */
3751 flags |= (1ULL << j);
3752 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
3753 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
3754 /* flag isn't supported */
3755 flags = flags & ~(1ULL << j);
3761 *mask = *mask & flags;
3766 * Return a bitmask of stats that are supported on all pools by both the module
3770 get_stat_flags(zpool_list_t *list)
3775 * get_stat_flags_cb() will lop off bits from "mask" until only the
3776 * flags that are supported on all pools remain.
3778 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
3783 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3786 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
3788 iostat_cbdata_t *cb = cb_data;
3792 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
3794 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
3795 ret = 1; /* match */
3802 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3805 is_vdev(zpool_handle_t *zhp, void *cb_data)
3807 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
3811 * Check if vdevs are in a pool
3813 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3814 * return 0. If pool_name is NULL, then search all pools.
3817 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
3818 iostat_cbdata_t *cb)
3825 if ((argc == 0) || !*argv)
3831 /* Temporarily hijack cb_vdev_names for a second... */
3832 tmp_name = cb->cb_vdev_names;
3834 /* Go though our list of prospective vdev names */
3835 for (i = 0; i < argc; i++) {
3836 cb->cb_vdev_names = argv + i;
3838 /* Is this name a vdev in our pools? */
3839 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
3847 cb->cb_vdev_names = tmp_name;
3853 is_pool_cb(zpool_handle_t *zhp, void *data)
3856 if (strcmp(name, zpool_get_name(zhp)) == 0)
3863 * Do we have a pool named *name? If so, return 1, otherwise 0.
3868 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
3871 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
3873 are_all_pools(int argc, char **argv) {
3874 if ((argc == 0) || !*argv)
3878 if (!is_pool(argv[argc]))
3885 * Helper function to print out vdev/pool names we can't resolve. Used for an
3889 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
3890 iostat_cbdata_t *cb)
3895 for (i = 0; i < argc; i++) {
3899 str = gettext("pool");
3900 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
3901 str = gettext("vdev in this pool");
3902 else if (are_vdevs_in_pool(1, &name, NULL, cb))
3903 str = gettext("vdev in another pool");
3905 str = gettext("unknown");
3907 fprintf(stderr, "\t%s (%s)\n", name, str);
3912 * Same as get_interval_count(), but with additional checks to not misinterpret
3913 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
3917 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
3918 unsigned long *count, iostat_cbdata_t *cb)
3920 char **tmpargv = argv;
3921 int argc_for_interval = 0;
3923 /* Is the last arg an interval value? Or a guid? */
3924 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
3926 * The last arg is not a guid, so it's probably an
3929 argc_for_interval++;
3932 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
3934 * The 2nd to last arg is not a guid, so it's probably
3935 * an interval value.
3937 argc_for_interval++;
3941 /* Point to our list of possible intervals */
3942 tmpargv = &argv[*argc - argc_for_interval];
3944 *argc = *argc - argc_for_interval;
3945 get_interval_count(&argc_for_interval, tmpargv,
3950 * Floating point sleep(). Allows you to pass in a floating point value for
3955 struct timespec req;
3956 req.tv_sec = floor(sec);
3957 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
3958 nanosleep(&req, NULL);
3963 * zpool iostat [-c CMD] [-ghHLpPvy] [[-lq]|[-r|-w]] [-n name] [-T d|u]
3964 * [[ pool ...]|[pool vdev ...]|[vdev ...]]
3965 * [interval [count]]
3967 * -c CMD For each vdev, run command CMD
3968 * -g Display guid for individual vdev name.
3969 * -L Follow links when resolving vdev path name.
3970 * -P Display full path for vdev name.
3971 * -v Display statistics for individual vdevs
3973 * -p Display values in parsable (exact) format.
3974 * -H Scripted mode. Don't display headers, and separate properties
3976 * -l Display average latency
3977 * -q Display queue depths
3978 * -w Display latency histograms
3979 * -r Display request size histogram
3980 * -T Display a timestamp in date(1) or Unix format
3982 * This command can be tricky because we want to be able to deal with pool
3983 * creation/destruction as well as vdev configuration changes. The bulk of this
3984 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3985 * on pool_list_update() to detect the addition of new pools. Configuration
3986 * changes are all handled within libzfs.
3989 zpool_do_iostat(int argc, char **argv)
3995 unsigned long count = 0;
3997 boolean_t verbose = B_FALSE;
3998 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
3999 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4000 boolean_t omit_since_boot = B_FALSE;
4001 boolean_t guid = B_FALSE;
4002 boolean_t follow_links = B_FALSE;
4003 boolean_t full_name = B_FALSE;
4004 iostat_cbdata_t cb = { 0 };
4007 /* Used for printing error message */
4008 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4009 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4011 uint64_t unsupported_flags;
4014 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4023 follow_links = B_TRUE;
4029 get_timestamp_arg(*optarg);
4053 omit_since_boot = B_TRUE;
4059 if (optopt == 'c') {
4061 gettext("Missing CMD for -c\n"));
4064 gettext("invalid option '%c'\n"), optopt);
4073 cb.cb_literal = parsable;
4074 cb.cb_scripted = scripted;
4077 cb.cb_name_flags |= VDEV_NAME_GUID;
4079 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4081 cb.cb_name_flags |= VDEV_NAME_PATH;
4082 cb.cb_iteration = 0;
4083 cb.cb_namewidth = 0;
4084 cb.cb_verbose = verbose;
4086 /* Get our interval and count values (if any) */
4088 get_interval_count_filter_guids(&argc, argv, &interval,
4091 get_interval_count(&argc, argv, &interval, &count);
4095 /* No args, so just print the defaults. */
4096 } else if (are_all_pools(argc, argv)) {
4097 /* All the args are pool names */
4098 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4099 /* All the args are vdevs */
4100 cb.cb_vdev_names = argv;
4101 cb.cb_vdev_names_count = argc;
4102 argc = 0; /* No pools to process */
4103 } else if (are_all_pools(1, argv)) {
4104 /* The first arg is a pool name */
4105 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4106 /* ...and the rest are vdev names */
4107 cb.cb_vdev_names = argv + 1;
4108 cb.cb_vdev_names_count = argc - 1;
4109 argc = 1; /* One pool to process */
4111 fprintf(stderr, gettext("Expected either a list of "));
4112 fprintf(stderr, gettext("pools, or list of vdevs in"));
4113 fprintf(stderr, " \"%s\", ", argv[0]);
4114 fprintf(stderr, gettext("but got:\n"));
4115 error_list_unresolved_vdevs(argc - 1, argv + 1,
4117 fprintf(stderr, "\n");
4123 * The args don't make sense. The first arg isn't a pool name,
4124 * nor are all the args vdevs.
4126 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4127 fprintf(stderr, "\n");
4131 if (cb.cb_vdev_names_count != 0) {
4133 * If user specified vdevs, it implies verbose.
4135 cb.cb_verbose = B_TRUE;
4139 * Construct the list of all interesting pools.
4142 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4145 if (pool_list_count(list) == 0 && argc != 0) {
4146 pool_list_free(list);
4150 if (pool_list_count(list) == 0 && interval == 0) {
4151 pool_list_free(list);
4152 (void) fprintf(stderr, gettext("no pools available\n"));
4156 if ((l_histo || rq_histo) && (queues || latency)) {
4157 pool_list_free(list);
4158 (void) fprintf(stderr,
4159 gettext("[-r|-w] isn't allowed with [-q|-l]\n"));
4164 if (l_histo && rq_histo) {
4165 pool_list_free(list);
4166 (void) fprintf(stderr,
4167 gettext("Only one of [-r|-w] can be passed at a time\n"));
4173 * Enter the main iostat loop.
4179 * Histograms tables look out of place when you try to display
4180 * them with the other stats, so make a rule that you can only
4181 * print histograms by themselves.
4183 cb.cb_flags = IOS_L_HISTO_M;
4184 } else if (rq_histo) {
4185 cb.cb_flags = IOS_RQ_HISTO_M;
4187 cb.cb_flags = IOS_DEFAULT_M;
4189 cb.cb_flags |= IOS_LATENCY_M;
4191 cb.cb_flags |= IOS_QUEUES_M;
4195 * See if the module supports all the stats we want to display.
4197 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4198 if (unsupported_flags) {
4202 gettext("The loaded zfs module doesn't support:"));
4204 /* for each bit set in unsupported_flags */
4205 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4206 idx = lowbit64(f) - 1;
4207 fprintf(stderr, " -%c", flag_to_arg[idx]);
4210 fprintf(stderr, ". Try running a newer module.\n");
4211 pool_list_free(list);
4217 if ((npools = pool_list_count(list)) == 0)
4218 (void) fprintf(stderr, gettext("no pools available\n"));
4221 * If this is the first iteration and -y was supplied
4222 * we skip any printing.
4224 boolean_t skip = (omit_since_boot &&
4225 cb.cb_iteration == 0);
4228 * Refresh all statistics. This is done as an
4229 * explicit step before calculating the maximum name
4230 * width, so that any * configuration changes are
4231 * properly accounted for.
4233 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4237 * Iterate over all pools to determine the maximum width
4238 * for the pool / device name column across all pools.
4240 cb.cb_namewidth = 0;
4241 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4244 if (timestamp_fmt != NODATE)
4245 print_timestamp(timestamp_fmt);
4248 * If it's the first time and we're not skipping it,
4249 * or either skip or verbose mode, print the header.
4251 * The histogram code explicitly prints its header on
4252 * every vdev, so skip this for histograms.
4254 if (((++cb.cb_iteration == 1 && !skip) ||
4255 (skip != verbose)) &&
4256 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4258 print_iostat_header(&cb);
4261 (void) fsleep(interval);
4265 if (cmd != NULL && cb.cb_verbose)
4266 cb.vcdl = all_pools_for_each_vdev_run(argc,
4267 argv, cmd, g_zfs, cb.cb_vdev_names,
4268 cb.cb_vdev_names_count, cb.cb_name_flags);
4270 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4272 if (cb.vcdl != NULL)
4273 free_vdev_cmd_data_list(cb.vcdl);
4276 * If there's more than one pool, and we're not in
4277 * verbose mode (which prints a separator for us),
4278 * then print a separator.
4280 * In addition, if we're printing specific vdevs then
4281 * we also want an ending separator.
4283 if (((npools > 1 && !verbose &&
4284 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4285 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4286 cb.cb_vdev_names_count)) &&
4288 print_iostat_separator(&cb);
4293 * Flush the output so that redirection to a file isn't buffered
4296 (void) fflush(stdout);
4301 if (count != 0 && --count == 0)
4304 (void) fsleep(interval);
4307 pool_list_free(list);
4312 typedef struct list_cbdata {
4313 boolean_t cb_verbose;
4316 boolean_t cb_scripted;
4317 zprop_list_t *cb_proplist;
4318 boolean_t cb_literal;
4322 * Given a list of columns to display, output appropriate headers for each one.
4325 print_header(list_cbdata_t *cb)
4327 zprop_list_t *pl = cb->cb_proplist;
4328 char headerbuf[ZPOOL_MAXPROPLEN];
4330 boolean_t first = B_TRUE;
4331 boolean_t right_justify;
4334 for (; pl != NULL; pl = pl->pl_next) {
4335 width = pl->pl_width;
4336 if (first && cb->cb_verbose) {
4338 * Reset the width to accommodate the verbose listing
4341 width = cb->cb_namewidth;
4349 right_justify = B_FALSE;
4350 if (pl->pl_prop != ZPROP_INVAL) {
4351 header = zpool_prop_column_name(pl->pl_prop);
4352 right_justify = zpool_prop_align_right(pl->pl_prop);
4356 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4357 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4358 headerbuf[i] = '\0';
4362 if (pl->pl_next == NULL && !right_justify)
4363 (void) printf("%s", header);
4364 else if (right_justify)
4365 (void) printf("%*s", (int)width, header);
4367 (void) printf("%-*s", (int)width, header);
4370 (void) printf("\n");
4374 * Given a pool and a list of properties, print out all the properties according
4375 * to the described layout.
4378 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4380 zprop_list_t *pl = cb->cb_proplist;
4381 boolean_t first = B_TRUE;
4382 char property[ZPOOL_MAXPROPLEN];
4384 boolean_t right_justify;
4387 for (; pl != NULL; pl = pl->pl_next) {
4389 width = pl->pl_width;
4390 if (first && cb->cb_verbose) {
4392 * Reset the width to accommodate the verbose listing
4395 width = cb->cb_namewidth;
4399 if (cb->cb_scripted)
4400 (void) printf("\t");
4407 right_justify = B_FALSE;
4408 if (pl->pl_prop != ZPROP_INVAL) {
4409 if (zpool_get_prop(zhp, pl->pl_prop, property,
4410 sizeof (property), NULL, cb->cb_literal) != 0)
4415 right_justify = zpool_prop_align_right(pl->pl_prop);
4416 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4417 zpool_prop_unsupported(pl->pl_user_prop)) &&
4418 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4419 sizeof (property)) == 0) {
4427 * If this is being called in scripted mode, or if this is the
4428 * last column and it is left-justified, don't include a width
4431 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4432 (void) printf("%s", propstr);
4433 else if (right_justify)
4434 (void) printf("%*s", (int)width, propstr);
4436 (void) printf("%-*s", (int)width, propstr);
4439 (void) printf("\n");
4443 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4444 boolean_t valid, enum zfs_nicenum_format format)
4448 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4451 case ZPOOL_PROP_EXPANDSZ:
4453 (void) strlcpy(propval, "-", sizeof (propval));
4455 zfs_nicenum_format(value, propval, sizeof (propval),
4458 case ZPOOL_PROP_FRAGMENTATION:
4459 if (value == ZFS_FRAG_INVALID) {
4460 (void) strlcpy(propval, "-", sizeof (propval));
4461 } else if (format == ZFS_NICENUM_RAW) {
4462 (void) snprintf(propval, sizeof (propval), "%llu",
4463 (unsigned long long)value);
4465 (void) snprintf(propval, sizeof (propval), "%llu%%",
4466 (unsigned long long)value);
4469 case ZPOOL_PROP_CAPACITY:
4470 if (format == ZFS_NICENUM_RAW)
4471 (void) snprintf(propval, sizeof (propval), "%llu",
4472 (unsigned long long)value);
4474 (void) snprintf(propval, sizeof (propval), "%llu%%",
4475 (unsigned long long)value);
4478 zfs_nicenum_format(value, propval, sizeof (propval), format);
4482 (void) strlcpy(propval, "-", sizeof (propval));
4485 (void) printf("\t%s", propval);
4487 (void) printf(" %*s", (int)width, propval);
4491 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4492 list_cbdata_t *cb, int depth)
4498 boolean_t scripted = cb->cb_scripted;
4499 uint64_t islog = B_FALSE;
4500 boolean_t haslog = B_FALSE;
4501 char *dashes = "%-*s - - - - - -\n";
4503 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4504 (uint64_t **)&vs, &c) == 0);
4507 boolean_t toplevel = (vs->vs_space != 0);
4509 enum zfs_nicenum_format format;
4512 format = ZFS_NICENUM_RAW;
4514 format = ZFS_NICENUM_1024;
4517 (void) printf("\t%s", name);
4518 else if (strlen(name) + depth > cb->cb_namewidth)
4519 (void) printf("%*s%s", depth, "", name);
4521 (void) printf("%*s%s%*s", depth, "", name,
4522 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4525 * Print the properties for the individual vdevs. Some
4526 * properties are only applicable to toplevel vdevs. The
4527 * 'toplevel' boolean value is passed to the print_one_column()
4528 * to indicate that the value is valid.
4530 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4532 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4534 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4535 scripted, toplevel, format);
4536 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4538 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4539 vs->vs_fragmentation, scripted,
4540 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4542 cap = (vs->vs_space == 0) ? 0 :
4543 (vs->vs_alloc * 100 / vs->vs_space);
4544 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4546 (void) printf("\n");
4549 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4550 &child, &children) != 0)
4553 for (c = 0; c < children; c++) {
4554 uint64_t ishole = B_FALSE;
4556 if (nvlist_lookup_uint64(child[c],
4557 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4560 if (nvlist_lookup_uint64(child[c],
4561 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
4566 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4568 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4572 if (haslog == B_TRUE) {
4573 /* LINTED E_SEC_PRINTF_VAR_FMT */
4574 (void) printf(dashes, cb->cb_namewidth, "log");
4575 for (c = 0; c < children; c++) {
4576 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4577 &islog) != 0 || !islog)
4579 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4581 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4586 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
4587 &child, &children) == 0 && children > 0) {
4588 /* LINTED E_SEC_PRINTF_VAR_FMT */
4589 (void) printf(dashes, cb->cb_namewidth, "cache");
4590 for (c = 0; c < children; c++) {
4591 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4593 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4598 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
4599 &children) == 0 && children > 0) {
4600 /* LINTED E_SEC_PRINTF_VAR_FMT */
4601 (void) printf(dashes, cb->cb_namewidth, "spare");
4602 for (c = 0; c < children; c++) {
4603 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4605 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4613 * Generic callback function to list a pool.
4616 list_callback(zpool_handle_t *zhp, void *data)
4618 list_cbdata_t *cbp = data;
4622 config = zpool_get_config(zhp, NULL);
4624 print_pool(zhp, cbp);
4625 if (!cbp->cb_verbose)
4628 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4630 print_list_stats(zhp, NULL, nvroot, cbp, 0);
4636 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4638 * -g Display guid for individual vdev name.
4639 * -H Scripted mode. Don't display headers, and separate properties
4641 * -L Follow links when resolving vdev path name.
4642 * -o List of properties to display. Defaults to
4643 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4644 * "dedupratio,health,altroot"
4645 * -p Display values in parsable (exact) format.
4646 * -P Display full path for vdev name.
4647 * -T Display a timestamp in date(1) or Unix format
4649 * List all pools in the system, whether or not they're healthy. Output space
4650 * statistics for each one, as well as health status summary.
4653 zpool_do_list(int argc, char **argv)
4657 list_cbdata_t cb = { 0 };
4658 static char default_props[] =
4659 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4660 "dedupratio,health,altroot";
4661 char *props = default_props;
4663 unsigned long count = 0;
4665 boolean_t first = B_TRUE;
4668 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
4671 cb.cb_name_flags |= VDEV_NAME_GUID;
4674 cb.cb_scripted = B_TRUE;
4677 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4683 cb.cb_name_flags |= VDEV_NAME_PATH;
4686 cb.cb_literal = B_TRUE;
4689 get_timestamp_arg(*optarg);
4692 cb.cb_verbose = B_TRUE;
4695 (void) fprintf(stderr, gettext("missing argument for "
4696 "'%c' option\n"), optopt);
4700 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4709 get_interval_count(&argc, argv, &interval, &count);
4711 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
4715 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
4719 if (pool_list_count(list) == 0)
4722 if (timestamp_fmt != NODATE)
4723 print_timestamp(timestamp_fmt);
4725 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
4729 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
4734 if (count != 0 && --count == 0)
4737 pool_list_free(list);
4738 (void) fsleep(interval);
4741 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
4742 (void) printf(gettext("no pools available\n"));
4746 pool_list_free(list);
4747 zprop_free_list(cb.cb_proplist);
4752 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
4754 boolean_t force = B_FALSE;
4757 char *poolname, *old_disk, *new_disk;
4758 zpool_handle_t *zhp;
4759 nvlist_t *props = NULL;
4764 while ((c = getopt(argc, argv, "fo:")) != -1) {
4770 if ((propval = strchr(optarg, '=')) == NULL) {
4771 (void) fprintf(stderr, gettext("missing "
4772 "'=' for -o option\n"));
4778 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
4779 (add_prop_list(optarg, propval, &props, B_TRUE)))
4783 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4792 /* get pool name and check number of arguments */
4794 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4801 (void) fprintf(stderr,
4802 gettext("missing <device> specification\n"));
4810 (void) fprintf(stderr,
4811 gettext("missing <new_device> specification\n"));
4814 new_disk = old_disk;
4824 (void) fprintf(stderr, gettext("too many arguments\n"));
4828 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
4833 if (zpool_get_config(zhp, NULL) == NULL) {
4834 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
4841 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
4843 if (nvroot == NULL) {
4849 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
4852 nvlist_free(nvroot);
4859 * zpool replace [-f] <pool> <device> <new_device>
4861 * -f Force attach, even if <new_device> appears to be in use.
4863 * Replace <device> with <new_device>.
4867 zpool_do_replace(int argc, char **argv)
4869 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
4873 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
4875 * -f Force attach, even if <new_device> appears to be in use.
4876 * -o Set property=value.
4878 * Attach <new_device> to the mirror containing <device>. If <device> is not
4879 * part of a mirror, then <device> will be transformed into a mirror of
4880 * <device> and <new_device>. In either case, <new_device> will begin life
4881 * with a DTL of [0, now], and will immediately begin to resilver itself.
4884 zpool_do_attach(int argc, char **argv)
4886 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
4890 * zpool detach [-f] <pool> <device>
4892 * -f Force detach of <device>, even if DTLs argue against it
4893 * (not supported yet)
4895 * Detach a device from a mirror. The operation will be refused if <device>
4896 * is the last device in the mirror, or if the DTLs indicate that this device
4897 * has the only valid copy of some data.
4901 zpool_do_detach(int argc, char **argv)
4904 char *poolname, *path;
4905 zpool_handle_t *zhp;
4909 while ((c = getopt(argc, argv, "f")) != -1) {
4913 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4922 /* get pool name and check number of arguments */
4924 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4929 (void) fprintf(stderr,
4930 gettext("missing <device> specification\n"));
4937 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4940 ret = zpool_vdev_detach(zhp, path);
4948 * zpool split [-gLnP] [-o prop=val] ...
4950 * [-R altroot] <pool> <newpool> [<device> ...]
4952 * -g Display guid for individual vdev name.
4953 * -L Follow links when resolving vdev path name.
4954 * -n Do not split the pool, but display the resulting layout if
4955 * it were to be split.
4956 * -o Set property=value, or set mount options.
4957 * -P Display full path for vdev name.
4958 * -R Mount the split-off pool under an alternate root.
4960 * Splits the named pool and gives it the new pool name. Devices to be split
4961 * off may be listed, provided that no more than one device is specified
4962 * per top-level vdev mirror. The newly split pool is left in an exported
4963 * state unless -R is specified.
4965 * Restrictions: the top-level of the pool pool must only be made up of
4966 * mirrors; all devices in the pool must be healthy; no device may be
4967 * undergoing a resilvering operation.
4970 zpool_do_split(int argc, char **argv)
4972 char *srcpool, *newpool, *propval;
4973 char *mntopts = NULL;
4976 zpool_handle_t *zhp;
4977 nvlist_t *config, *props = NULL;
4979 flags.dryrun = B_FALSE;
4980 flags.import = B_FALSE;
4981 flags.name_flags = 0;
4984 while ((c = getopt(argc, argv, ":gLR:no:P")) != -1) {
4987 flags.name_flags |= VDEV_NAME_GUID;
4990 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
4993 flags.import = B_TRUE;
4995 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
4996 &props, B_TRUE) != 0) {
5002 flags.dryrun = B_TRUE;
5005 if ((propval = strchr(optarg, '=')) != NULL) {
5008 if (add_prop_list(optarg, propval,
5009 &props, B_TRUE) != 0) {
5018 flags.name_flags |= VDEV_NAME_PATH;
5021 (void) fprintf(stderr, gettext("missing argument for "
5022 "'%c' option\n"), optopt);
5026 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5033 if (!flags.import && mntopts != NULL) {
5034 (void) fprintf(stderr, gettext("setting mntopts is only "
5035 "valid when importing the pool\n"));
5043 (void) fprintf(stderr, gettext("Missing pool name\n"));
5047 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5057 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5062 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5063 if (config == NULL) {
5067 (void) printf(gettext("would create '%s' with the "
5068 "following layout:\n\n"), newpool);
5069 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5076 if (ret != 0 || flags.dryrun || !flags.import) {
5077 nvlist_free(config);
5083 * The split was successful. Now we need to open the new
5084 * pool and import it.
5086 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5087 nvlist_free(config);
5091 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5092 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5094 (void) fprintf(stderr, gettext("Split was successful, but "
5095 "the datasets could not all be mounted\n"));
5096 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5097 "different altroot\n"), "zpool import");
5100 nvlist_free(config);
5109 * zpool online <pool> <device> ...
5112 zpool_do_online(int argc, char **argv)
5116 zpool_handle_t *zhp;
5118 vdev_state_t newstate;
5122 while ((c = getopt(argc, argv, "et")) != -1) {
5125 flags |= ZFS_ONLINE_EXPAND;
5129 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5138 /* get pool name and check number of arguments */
5140 (void) fprintf(stderr, gettext("missing pool name\n"));
5144 (void) fprintf(stderr, gettext("missing device name\n"));
5150 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5153 for (i = 1; i < argc; i++) {
5154 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5155 if (newstate != VDEV_STATE_HEALTHY) {
5156 (void) printf(gettext("warning: device '%s' "
5157 "onlined, but remains in faulted state\n"),
5159 if (newstate == VDEV_STATE_FAULTED)
5160 (void) printf(gettext("use 'zpool "
5161 "clear' to restore a faulted "
5164 (void) printf(gettext("use 'zpool "
5165 "replace' to replace devices "
5166 "that are no longer present\n"));
5179 * zpool offline [-ft] <pool> <device> ...
5181 * -f Force the device into the offline state, even if doing
5182 * so would appear to compromise pool availability.
5183 * (not supported yet)
5185 * -t Only take the device off-line temporarily. The offline
5186 * state will not be persistent across reboots.
5190 zpool_do_offline(int argc, char **argv)
5194 zpool_handle_t *zhp;
5196 boolean_t istmp = B_FALSE;
5199 while ((c = getopt(argc, argv, "ft")) != -1) {
5206 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5215 /* get pool name and check number of arguments */
5217 (void) fprintf(stderr, gettext("missing pool name\n"));
5221 (void) fprintf(stderr, gettext("missing device name\n"));
5227 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5230 for (i = 1; i < argc; i++) {
5231 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5241 * zpool clear <pool> [device]
5243 * Clear all errors associated with a pool or a particular device.
5246 zpool_do_clear(int argc, char **argv)
5250 boolean_t dryrun = B_FALSE;
5251 boolean_t do_rewind = B_FALSE;
5252 boolean_t xtreme_rewind = B_FALSE;
5253 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5254 nvlist_t *policy = NULL;
5255 zpool_handle_t *zhp;
5256 char *pool, *device;
5259 while ((c = getopt(argc, argv, "FnX")) != -1) {
5268 xtreme_rewind = B_TRUE;
5271 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5281 (void) fprintf(stderr, gettext("missing pool name\n"));
5286 (void) fprintf(stderr, gettext("too many arguments\n"));
5290 if ((dryrun || xtreme_rewind) && !do_rewind) {
5291 (void) fprintf(stderr,
5292 gettext("-n or -X only meaningful with -F\n"));
5296 rewind_policy = ZPOOL_TRY_REWIND;
5298 rewind_policy = ZPOOL_DO_REWIND;
5300 rewind_policy |= ZPOOL_EXTREME_REWIND;
5302 /* In future, further rewind policy choices can be passed along here */
5303 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5304 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5308 device = argc == 2 ? argv[1] : NULL;
5310 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5311 nvlist_free(policy);
5315 if (zpool_clear(zhp, device, policy) != 0)
5320 nvlist_free(policy);
5326 * zpool reguid <pool>
5329 zpool_do_reguid(int argc, char **argv)
5333 zpool_handle_t *zhp;
5337 while ((c = getopt(argc, argv, "")) != -1) {
5340 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5349 /* get pool name and check number of arguments */
5351 (void) fprintf(stderr, gettext("missing pool name\n"));
5356 (void) fprintf(stderr, gettext("too many arguments\n"));
5361 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5364 ret = zpool_reguid(zhp);
5372 * zpool reopen <pool>
5374 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5377 zpool_do_reopen(int argc, char **argv)
5381 zpool_handle_t *zhp;
5385 while ((c = getopt(argc, argv, "")) != -1) {
5388 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5398 (void) fprintf(stderr, gettext("missing pool name\n"));
5403 (void) fprintf(stderr, gettext("too many arguments\n"));
5408 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
5411 ret = zpool_reopen(zhp);
5416 typedef struct scrub_cbdata {
5423 scrub_callback(zpool_handle_t *zhp, void *data)
5425 scrub_cbdata_t *cb = data;
5429 * Ignore faulted pools.
5431 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5432 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5433 "currently unavailable\n"), zpool_get_name(zhp));
5437 err = zpool_scan(zhp, cb->cb_type);
5443 * zpool scrub [-s] <pool> ...
5445 * -s Stop. Stops any in-progress scrub.
5448 zpool_do_scrub(int argc, char **argv)
5453 cb.cb_type = POOL_SCAN_SCRUB;
5456 while ((c = getopt(argc, argv, "s")) != -1) {
5459 cb.cb_type = POOL_SCAN_NONE;
5462 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5474 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5478 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5482 * Print out detailed scrub status.
5485 print_scan_status(pool_scan_stat_t *ps)
5488 uint64_t elapsed, mins_left, hours_left;
5489 uint64_t pass_exam, examined, total;
5491 double fraction_done;
5492 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
5494 (void) printf(gettext(" scan: "));
5496 /* If there's never been a scan, there's not much to say. */
5497 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5498 ps->pss_func >= POOL_SCAN_FUNCS) {
5499 (void) printf(gettext("none requested\n"));
5503 start = ps->pss_start_time;
5504 end = ps->pss_end_time;
5505 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
5507 assert(ps->pss_func == POOL_SCAN_SCRUB ||
5508 ps->pss_func == POOL_SCAN_RESILVER);
5510 * Scan is finished or canceled.
5512 if (ps->pss_state == DSS_FINISHED) {
5513 uint64_t minutes_taken = (end - start) / 60;
5516 if (ps->pss_func == POOL_SCAN_SCRUB) {
5517 fmt = gettext("scrub repaired %s in %lluh%um with "
5518 "%llu errors on %s");
5519 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5520 fmt = gettext("resilvered %s in %lluh%um with "
5521 "%llu errors on %s");
5524 (void) printf(fmt, processed_buf,
5525 (u_longlong_t)(minutes_taken / 60),
5526 (uint_t)(minutes_taken % 60),
5527 (u_longlong_t)ps->pss_errors,
5528 ctime((time_t *)&end));
5530 } else if (ps->pss_state == DSS_CANCELED) {
5531 if (ps->pss_func == POOL_SCAN_SCRUB) {
5532 (void) printf(gettext("scrub canceled on %s"),
5534 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5535 (void) printf(gettext("resilver canceled on %s"),
5541 assert(ps->pss_state == DSS_SCANNING);
5544 * Scan is in progress.
5546 if (ps->pss_func == POOL_SCAN_SCRUB) {
5547 (void) printf(gettext("scrub in progress since %s"),
5549 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5550 (void) printf(gettext("resilver in progress since %s"),
5554 examined = ps->pss_examined ? ps->pss_examined : 1;
5555 total = ps->pss_to_examine;
5556 fraction_done = (double)examined / total;
5558 /* elapsed time for this pass */
5559 elapsed = time(NULL) - ps->pss_pass_start;
5560 elapsed = elapsed ? elapsed : 1;
5561 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
5562 rate = pass_exam / elapsed;
5563 rate = rate ? rate : 1;
5564 mins_left = ((total - examined) / rate) / 60;
5565 hours_left = mins_left / 60;
5567 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
5568 zfs_nicenum(total, total_buf, sizeof (total_buf));
5569 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
5572 * do not print estimated time if hours_left is more than 30 days
5574 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5575 examined_buf, total_buf, rate_buf);
5576 if (hours_left < (30 * 24)) {
5577 (void) printf(gettext(", %lluh%um to go\n"),
5578 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
5580 (void) printf(gettext(
5581 ", (scan is slow, no estimated time)\n"));
5584 if (ps->pss_func == POOL_SCAN_RESILVER) {
5585 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5586 processed_buf, 100 * fraction_done);
5587 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
5588 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5589 processed_buf, 100 * fraction_done);
5594 print_error_log(zpool_handle_t *zhp)
5596 nvlist_t *nverrlist = NULL;
5599 size_t len = MAXPATHLEN * 2;
5601 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
5602 (void) printf("errors: List of errors unavailable "
5603 "(insufficient privileges)\n");
5607 (void) printf("errors: Permanent errors have been "
5608 "detected in the following files:\n\n");
5610 pathname = safe_malloc(len);
5612 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
5614 uint64_t dsobj, obj;
5616 verify(nvpair_value_nvlist(elem, &nv) == 0);
5617 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
5619 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
5621 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
5622 (void) printf("%7s %s\n", "", pathname);
5625 nvlist_free(nverrlist);
5629 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
5638 (void) printf(gettext("\tspares\n"));
5640 for (i = 0; i < nspares; i++) {
5641 name = zpool_vdev_name(g_zfs, zhp, spares[i],
5643 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
5649 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
5658 (void) printf(gettext("\tcache\n"));
5660 for (i = 0; i < nl2cache; i++) {
5661 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
5663 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
5669 print_dedup_stats(nvlist_t *config)
5671 ddt_histogram_t *ddh;
5677 * If the pool was faulted then we may not have been able to
5678 * obtain the config. Otherwise, if we have anything in the dedup
5679 * table continue processing the stats.
5681 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
5682 (uint64_t **)&ddo, &c) != 0)
5685 (void) printf("\n");
5686 (void) printf(gettext(" dedup: "));
5687 if (ddo->ddo_count == 0) {
5688 (void) printf(gettext("no DDT entries\n"));
5692 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5693 (u_longlong_t)ddo->ddo_count,
5694 (u_longlong_t)ddo->ddo_dspace,
5695 (u_longlong_t)ddo->ddo_mspace);
5697 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
5698 (uint64_t **)&dds, &c) == 0);
5699 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
5700 (uint64_t **)&ddh, &c) == 0);
5701 zpool_dump_ddt(dds, ddh);
5705 * Display a summary of pool status. Displays a summary such as:
5709 * reason: One or more devices ...
5710 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5716 * When given the '-v' option, we print out the complete config. If the '-e'
5717 * option is specified, then we print out error rate information as well.
5720 status_callback(zpool_handle_t *zhp, void *data)
5722 status_cbdata_t *cbp = data;
5723 nvlist_t *config, *nvroot;
5725 zpool_status_t reason;
5726 zpool_errata_t errata;
5731 config = zpool_get_config(zhp, NULL);
5732 reason = zpool_get_status(zhp, &msgid, &errata);
5737 * If we were given 'zpool status -x', only report those pools with
5740 if (cbp->cb_explain &&
5741 (reason == ZPOOL_STATUS_OK ||
5742 reason == ZPOOL_STATUS_VERSION_OLDER ||
5743 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
5744 if (!cbp->cb_allpools) {
5745 (void) printf(gettext("pool '%s' is healthy\n"),
5746 zpool_get_name(zhp));
5748 cbp->cb_first = B_FALSE;
5754 cbp->cb_first = B_FALSE;
5756 (void) printf("\n");
5758 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5760 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
5761 (uint64_t **)&vs, &c) == 0);
5762 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
5764 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
5765 (void) printf(gettext(" state: %s\n"), health);
5768 case ZPOOL_STATUS_MISSING_DEV_R:
5769 (void) printf(gettext("status: One or more devices could not "
5770 "be opened. Sufficient replicas exist for\n\tthe pool to "
5771 "continue functioning in a degraded state.\n"));
5772 (void) printf(gettext("action: Attach the missing device and "
5773 "online it using 'zpool online'.\n"));
5776 case ZPOOL_STATUS_MISSING_DEV_NR:
5777 (void) printf(gettext("status: One or more devices could not "
5778 "be opened. There are insufficient\n\treplicas for the "
5779 "pool to continue functioning.\n"));
5780 (void) printf(gettext("action: Attach the missing device and "
5781 "online it using 'zpool online'.\n"));
5784 case ZPOOL_STATUS_CORRUPT_LABEL_R:
5785 (void) printf(gettext("status: One or more devices could not "
5786 "be used because the label is missing or\n\tinvalid. "
5787 "Sufficient replicas exist for the pool to continue\n\t"
5788 "functioning in a degraded state.\n"));
5789 (void) printf(gettext("action: Replace the device using "
5790 "'zpool replace'.\n"));
5793 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
5794 (void) printf(gettext("status: One or more devices could not "
5795 "be used because the label is missing \n\tor invalid. "
5796 "There are insufficient replicas for the pool to "
5797 "continue\n\tfunctioning.\n"));
5798 zpool_explain_recover(zpool_get_handle(zhp),
5799 zpool_get_name(zhp), reason, config);
5802 case ZPOOL_STATUS_FAILING_DEV:
5803 (void) printf(gettext("status: One or more devices has "
5804 "experienced an unrecoverable error. An\n\tattempt was "
5805 "made to correct the error. Applications are "
5807 (void) printf(gettext("action: Determine if the device needs "
5808 "to be replaced, and clear the errors\n\tusing "
5809 "'zpool clear' or replace the device with 'zpool "
5813 case ZPOOL_STATUS_OFFLINE_DEV:
5814 (void) printf(gettext("status: One or more devices has "
5815 "been taken offline by the administrator.\n\tSufficient "
5816 "replicas exist for the pool to continue functioning in "
5817 "a\n\tdegraded state.\n"));
5818 (void) printf(gettext("action: Online the device using "
5819 "'zpool online' or replace the device with\n\t'zpool "
5823 case ZPOOL_STATUS_REMOVED_DEV:
5824 (void) printf(gettext("status: One or more devices has "
5825 "been removed by the administrator.\n\tSufficient "
5826 "replicas exist for the pool to continue functioning in "
5827 "a\n\tdegraded state.\n"));
5828 (void) printf(gettext("action: Online the device using "
5829 "'zpool online' or replace the device with\n\t'zpool "
5833 case ZPOOL_STATUS_RESILVERING:
5834 (void) printf(gettext("status: One or more devices is "
5835 "currently being resilvered. The pool will\n\tcontinue "
5836 "to function, possibly in a degraded state.\n"));
5837 (void) printf(gettext("action: Wait for the resilver to "
5841 case ZPOOL_STATUS_CORRUPT_DATA:
5842 (void) printf(gettext("status: One or more devices has "
5843 "experienced an error resulting in data\n\tcorruption. "
5844 "Applications may be affected.\n"));
5845 (void) printf(gettext("action: Restore the file in question "
5846 "if possible. Otherwise restore the\n\tentire pool from "
5850 case ZPOOL_STATUS_CORRUPT_POOL:
5851 (void) printf(gettext("status: The pool metadata is corrupted "
5852 "and the pool cannot be opened.\n"));
5853 zpool_explain_recover(zpool_get_handle(zhp),
5854 zpool_get_name(zhp), reason, config);
5857 case ZPOOL_STATUS_VERSION_OLDER:
5858 (void) printf(gettext("status: The pool is formatted using a "
5859 "legacy on-disk format. The pool can\n\tstill be used, "
5860 "but some features are unavailable.\n"));
5861 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5862 "upgrade'. Once this is done, the\n\tpool will no longer "
5863 "be accessible on software that does not support\n\t"
5864 "feature flags.\n"));
5867 case ZPOOL_STATUS_VERSION_NEWER:
5868 (void) printf(gettext("status: The pool has been upgraded to a "
5869 "newer, incompatible on-disk version.\n\tThe pool cannot "
5870 "be accessed on this system.\n"));
5871 (void) printf(gettext("action: Access the pool from a system "
5872 "running more recent software, or\n\trestore the pool from "
5876 case ZPOOL_STATUS_FEAT_DISABLED:
5877 (void) printf(gettext("status: Some supported features are not "
5878 "enabled on the pool. The pool can\n\tstill be used, but "
5879 "some features are unavailable.\n"));
5880 (void) printf(gettext("action: Enable all features using "
5881 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5882 "longer be accessible by software that does not support\n\t"
5883 "the features. See zpool-features(5) for details.\n"));
5886 case ZPOOL_STATUS_UNSUP_FEAT_READ:
5887 (void) printf(gettext("status: The pool cannot be accessed on "
5888 "this system because it uses the\n\tfollowing feature(s) "
5889 "not supported on this system:\n"));
5890 zpool_print_unsup_feat(config);
5891 (void) printf("\n");
5892 (void) printf(gettext("action: Access the pool from a system "
5893 "that supports the required feature(s),\n\tor restore the "
5894 "pool from backup.\n"));
5897 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5898 (void) printf(gettext("status: The pool can only be accessed "
5899 "in read-only mode on this system. It\n\tcannot be "
5900 "accessed in read-write mode because it uses the "
5901 "following\n\tfeature(s) not supported on this system:\n"));
5902 zpool_print_unsup_feat(config);
5903 (void) printf("\n");
5904 (void) printf(gettext("action: The pool cannot be accessed in "
5905 "read-write mode. Import the pool with\n"
5906 "\t\"-o readonly=on\", access the pool from a system that "
5907 "supports the\n\trequired feature(s), or restore the "
5908 "pool from backup.\n"));
5911 case ZPOOL_STATUS_FAULTED_DEV_R:
5912 (void) printf(gettext("status: One or more devices are "
5913 "faulted in response to persistent errors.\n\tSufficient "
5914 "replicas exist for the pool to continue functioning "
5915 "in a\n\tdegraded state.\n"));
5916 (void) printf(gettext("action: Replace the faulted device, "
5917 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5920 case ZPOOL_STATUS_FAULTED_DEV_NR:
5921 (void) printf(gettext("status: One or more devices are "
5922 "faulted in response to persistent errors. There are "
5923 "insufficient replicas for the pool to\n\tcontinue "
5925 (void) printf(gettext("action: Destroy and re-create the pool "
5926 "from a backup source. Manually marking the device\n"
5927 "\trepaired using 'zpool clear' may allow some data "
5928 "to be recovered.\n"));
5931 case ZPOOL_STATUS_IO_FAILURE_WAIT:
5932 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
5933 (void) printf(gettext("status: One or more devices are "
5934 "faulted in response to IO failures.\n"));
5935 (void) printf(gettext("action: Make sure the affected devices "
5936 "are connected, then run 'zpool clear'.\n"));
5939 case ZPOOL_STATUS_BAD_LOG:
5940 (void) printf(gettext("status: An intent log record "
5941 "could not be read.\n"
5942 "\tWaiting for administrator intervention to fix the "
5943 "faulted pool.\n"));
5944 (void) printf(gettext("action: Either restore the affected "
5945 "device(s) and run 'zpool online',\n"
5946 "\tor ignore the intent log records by running "
5947 "'zpool clear'.\n"));
5950 case ZPOOL_STATUS_HOSTID_MISMATCH:
5951 (void) printf(gettext("status: Mismatch between pool hostid "
5952 "and system hostid on imported pool.\n\tThis pool was "
5953 "previously imported into a system with a different "
5954 "hostid,\n\tand then was verbatim imported into this "
5956 (void) printf(gettext("action: Export this pool on all systems "
5957 "on which it is imported.\n"
5958 "\tThen import it to correct the mismatch.\n"));
5961 case ZPOOL_STATUS_ERRATA:
5962 (void) printf(gettext("status: Errata #%d detected.\n"),
5966 case ZPOOL_ERRATA_NONE:
5969 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
5970 (void) printf(gettext("action: To correct the issue "
5971 "run 'zpool scrub'.\n"));
5976 * All errata which allow the pool to be imported
5977 * must contain an action message.
5985 * The remaining errors can't actually be generated, yet.
5987 assert(reason == ZPOOL_STATUS_OK);
5991 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
5994 if (config != NULL) {
5996 nvlist_t **spares, **l2cache;
5997 uint_t nspares, nl2cache;
5998 pool_scan_stat_t *ps = NULL;
6000 (void) nvlist_lookup_uint64_array(nvroot,
6001 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6002 print_scan_status(ps);
6004 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6005 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6006 if (cbp->cb_namewidth < 10)
6007 cbp->cb_namewidth = 10;
6009 (void) printf(gettext("config:\n\n"));
6010 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
6011 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6013 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6016 if (num_logs(nvroot) > 0)
6017 print_logs(zhp, cbp, nvroot);
6018 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6019 &l2cache, &nl2cache) == 0)
6020 print_l2cache(zhp, cbp, l2cache, nl2cache);
6022 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6023 &spares, &nspares) == 0)
6024 print_spares(zhp, cbp, spares, nspares);
6026 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6028 nvlist_t *nverrlist = NULL;
6031 * If the approximate error count is small, get a
6032 * precise count by fetching the entire log and
6033 * uniquifying the results.
6035 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6036 zpool_get_errlog(zhp, &nverrlist) == 0) {
6041 while ((elem = nvlist_next_nvpair(nverrlist,
6046 nvlist_free(nverrlist);
6048 (void) printf("\n");
6051 (void) printf(gettext("errors: No known data "
6053 else if (!cbp->cb_verbose)
6054 (void) printf(gettext("errors: %llu data "
6055 "errors, use '-v' for a list\n"),
6056 (u_longlong_t)nerr);
6058 print_error_log(zhp);
6061 if (cbp->cb_dedup_stats)
6062 print_dedup_stats(config);
6064 (void) printf(gettext("config: The configuration cannot be "
6072 * zpool status [-c CMD] [-gLPvx] [-T d|u] [pool] ... [interval [count]]
6074 * -c CMD For each vdev, run command CMD
6075 * -g Display guid for individual vdev name.
6076 * -L Follow links when resolving vdev path name.
6077 * -P Display full path for vdev name.
6078 * -v Display complete error logs
6079 * -x Display only pools with potential problems
6080 * -D Display dedup status (undocumented)
6081 * -T Display a timestamp in date(1) or Unix format
6083 * Describes the health status of all pools or some subset.
6086 zpool_do_status(int argc, char **argv)
6091 unsigned long count = 0;
6092 status_cbdata_t cb = { 0 };
6096 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6102 cb.cb_name_flags |= VDEV_NAME_GUID;
6105 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6108 cb.cb_name_flags |= VDEV_NAME_PATH;
6111 cb.cb_verbose = B_TRUE;
6114 cb.cb_explain = B_TRUE;
6117 cb.cb_dedup_stats = B_TRUE;
6120 get_timestamp_arg(*optarg);
6123 if (optopt == 'c') {
6125 gettext("Missing CMD for -c\n"));
6128 gettext("invalid option '%c'\n"), optopt);
6137 get_interval_count(&argc, argv, &interval, &count);
6140 cb.cb_allpools = B_TRUE;
6142 cb.cb_first = B_TRUE;
6143 cb.cb_print_status = B_TRUE;
6146 if (timestamp_fmt != NODATE)
6147 print_timestamp(timestamp_fmt);
6150 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6153 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6154 status_callback, &cb);
6156 if (cb.vcdl != NULL)
6157 free_vdev_cmd_data_list(cb.vcdl);
6159 if (argc == 0 && cb.cb_count == 0)
6160 (void) fprintf(stderr, gettext("no pools available\n"));
6161 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6162 (void) printf(gettext("all pools are healthy\n"));
6170 if (count != 0 && --count == 0)
6173 (void) fsleep(interval);
6179 typedef struct upgrade_cbdata {
6182 uint64_t cb_version;
6187 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6189 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6190 int *count = (int *)unsupp_fs;
6192 if (zfs_version > ZPL_VERSION) {
6193 (void) printf(gettext("%s (v%d) is not supported by this "
6194 "implementation of ZFS.\n"),
6195 zfs_get_name(zhp), zfs_version);
6199 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6207 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6211 uint64_t oldversion;
6214 config = zpool_get_config(zhp, NULL);
6215 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6218 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6219 assert(oldversion < version);
6221 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6226 (void) fprintf(stderr, gettext("Upgrade not performed due "
6227 "to %d unsupported filesystems (max v%d).\n"),
6228 unsupp_fs, (int)ZPL_VERSION);
6232 ret = zpool_upgrade(zhp, version);
6236 if (version >= SPA_VERSION_FEATURES) {
6237 (void) printf(gettext("Successfully upgraded "
6238 "'%s' from version %llu to feature flags.\n"),
6239 zpool_get_name(zhp), (u_longlong_t)oldversion);
6241 (void) printf(gettext("Successfully upgraded "
6242 "'%s' from version %llu to version %llu.\n"),
6243 zpool_get_name(zhp), (u_longlong_t)oldversion,
6244 (u_longlong_t)version);
6251 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6254 boolean_t firstff = B_TRUE;
6255 nvlist_t *enabled = zpool_get_features(zhp);
6258 for (i = 0; i < SPA_FEATURES; i++) {
6259 const char *fname = spa_feature_table[i].fi_uname;
6260 const char *fguid = spa_feature_table[i].fi_guid;
6261 if (!nvlist_exists(enabled, fguid)) {
6263 verify(-1 != asprintf(&propname, "feature@%s", fname));
6264 ret = zpool_set_prop(zhp, propname,
6265 ZFS_FEATURE_ENABLED);
6273 (void) printf(gettext("Enabled the "
6274 "following features on '%s':\n"),
6275 zpool_get_name(zhp));
6278 (void) printf(gettext(" %s\n"), fname);
6289 upgrade_cb(zpool_handle_t *zhp, void *arg)
6291 upgrade_cbdata_t *cbp = arg;
6294 boolean_t printnl = B_FALSE;
6297 config = zpool_get_config(zhp, NULL);
6298 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6301 assert(SPA_VERSION_IS_SUPPORTED(version));
6303 if (version < cbp->cb_version) {
6304 cbp->cb_first = B_FALSE;
6305 ret = upgrade_version(zhp, cbp->cb_version);
6311 * If they did "zpool upgrade -a", then we could
6312 * be doing ioctls to different pools. We need
6313 * to log this history once to each pool, and bypass
6314 * the normal history logging that happens in main().
6316 (void) zpool_log_history(g_zfs, history_str);
6317 log_history = B_FALSE;
6320 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6322 ret = upgrade_enable_all(zhp, &count);
6327 cbp->cb_first = B_FALSE;
6333 (void) printf(gettext("\n"));
6340 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6342 upgrade_cbdata_t *cbp = arg;
6346 config = zpool_get_config(zhp, NULL);
6347 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6350 assert(SPA_VERSION_IS_SUPPORTED(version));
6352 if (version < SPA_VERSION_FEATURES) {
6353 if (cbp->cb_first) {
6354 (void) printf(gettext("The following pools are "
6355 "formatted with legacy version numbers and can\n"
6356 "be upgraded to use feature flags. After "
6357 "being upgraded, these pools\nwill no "
6358 "longer be accessible by software that does not "
6359 "support feature\nflags.\n\n"));
6360 (void) printf(gettext("VER POOL\n"));
6361 (void) printf(gettext("--- ------------\n"));
6362 cbp->cb_first = B_FALSE;
6365 (void) printf("%2llu %s\n", (u_longlong_t)version,
6366 zpool_get_name(zhp));
6373 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6375 upgrade_cbdata_t *cbp = arg;
6379 config = zpool_get_config(zhp, NULL);
6380 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6383 if (version >= SPA_VERSION_FEATURES) {
6385 boolean_t poolfirst = B_TRUE;
6386 nvlist_t *enabled = zpool_get_features(zhp);
6388 for (i = 0; i < SPA_FEATURES; i++) {
6389 const char *fguid = spa_feature_table[i].fi_guid;
6390 const char *fname = spa_feature_table[i].fi_uname;
6391 if (!nvlist_exists(enabled, fguid)) {
6392 if (cbp->cb_first) {
6393 (void) printf(gettext("\nSome "
6394 "supported features are not "
6395 "enabled on the following pools. "
6396 "Once a\nfeature is enabled the "
6397 "pool may become incompatible with "
6398 "software\nthat does not support "
6400 "zpool-features(5) for "
6402 (void) printf(gettext("POOL "
6404 (void) printf(gettext("------"
6406 cbp->cb_first = B_FALSE;
6410 (void) printf(gettext("%s\n"),
6411 zpool_get_name(zhp));
6412 poolfirst = B_FALSE;
6415 (void) printf(gettext(" %s\n"), fname);
6418 * If they did "zpool upgrade -a", then we could
6419 * be doing ioctls to different pools. We need
6420 * to log this history once to each pool, and bypass
6421 * the normal history logging that happens in main().
6423 (void) zpool_log_history(g_zfs, history_str);
6424 log_history = B_FALSE;
6433 upgrade_one(zpool_handle_t *zhp, void *data)
6435 boolean_t printnl = B_FALSE;
6436 upgrade_cbdata_t *cbp = data;
6437 uint64_t cur_version;
6440 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6441 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6442 "Pool 'log' must be renamed using export and import"
6447 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6448 if (cur_version > cbp->cb_version) {
6449 (void) printf(gettext("Pool '%s' is already formatted "
6450 "using more current version '%llu'.\n\n"),
6451 zpool_get_name(zhp), (u_longlong_t)cur_version);
6455 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
6456 (void) printf(gettext("Pool '%s' is already formatted "
6457 "using version %llu.\n\n"), zpool_get_name(zhp),
6458 (u_longlong_t)cbp->cb_version);
6462 if (cur_version != cbp->cb_version) {
6464 ret = upgrade_version(zhp, cbp->cb_version);
6469 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6471 ret = upgrade_enable_all(zhp, &count);
6477 } else if (cur_version == SPA_VERSION) {
6478 (void) printf(gettext("Pool '%s' already has all "
6479 "supported features enabled.\n"),
6480 zpool_get_name(zhp));
6485 (void) printf(gettext("\n"));
6494 * zpool upgrade [-V version] <-a | pool ...>
6496 * With no arguments, display downrev'd ZFS pool available for upgrade.
6497 * Individual pools can be upgraded by specifying the pool, and '-a' will
6498 * upgrade all pools.
6501 zpool_do_upgrade(int argc, char **argv)
6504 upgrade_cbdata_t cb = { 0 };
6506 boolean_t showversions = B_FALSE;
6507 boolean_t upgradeall = B_FALSE;
6512 while ((c = getopt(argc, argv, ":avV:")) != -1) {
6515 upgradeall = B_TRUE;
6518 showversions = B_TRUE;
6521 cb.cb_version = strtoll(optarg, &end, 10);
6523 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
6524 (void) fprintf(stderr,
6525 gettext("invalid version '%s'\n"), optarg);
6530 (void) fprintf(stderr, gettext("missing argument for "
6531 "'%c' option\n"), optopt);
6535 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6546 if (cb.cb_version == 0) {
6547 cb.cb_version = SPA_VERSION;
6548 } else if (!upgradeall && argc == 0) {
6549 (void) fprintf(stderr, gettext("-V option is "
6550 "incompatible with other arguments\n"));
6555 if (upgradeall || argc != 0) {
6556 (void) fprintf(stderr, gettext("-v option is "
6557 "incompatible with other arguments\n"));
6560 } else if (upgradeall) {
6562 (void) fprintf(stderr, gettext("-a option should not "
6563 "be used along with a pool name\n"));
6568 (void) printf(gettext("This system supports ZFS pool feature "
6573 (void) printf(gettext("The following features are "
6575 (void) printf(gettext("FEAT DESCRIPTION\n"));
6576 (void) printf("----------------------------------------------"
6577 "---------------\n");
6578 for (i = 0; i < SPA_FEATURES; i++) {
6579 zfeature_info_t *fi = &spa_feature_table[i];
6581 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
6582 " (read-only compatible)" : "";
6584 (void) printf("%-37s%s\n", fi->fi_uname, ro);
6585 (void) printf(" %s\n", fi->fi_desc);
6587 (void) printf("\n");
6589 (void) printf(gettext("The following legacy versions are also "
6591 (void) printf(gettext("VER DESCRIPTION\n"));
6592 (void) printf("--- -----------------------------------------"
6593 "---------------\n");
6594 (void) printf(gettext(" 1 Initial ZFS version\n"));
6595 (void) printf(gettext(" 2 Ditto blocks "
6596 "(replicated metadata)\n"));
6597 (void) printf(gettext(" 3 Hot spares and double parity "
6599 (void) printf(gettext(" 4 zpool history\n"));
6600 (void) printf(gettext(" 5 Compression using the gzip "
6602 (void) printf(gettext(" 6 bootfs pool property\n"));
6603 (void) printf(gettext(" 7 Separate intent log devices\n"));
6604 (void) printf(gettext(" 8 Delegated administration\n"));
6605 (void) printf(gettext(" 9 refquota and refreservation "
6607 (void) printf(gettext(" 10 Cache devices\n"));
6608 (void) printf(gettext(" 11 Improved scrub performance\n"));
6609 (void) printf(gettext(" 12 Snapshot properties\n"));
6610 (void) printf(gettext(" 13 snapused property\n"));
6611 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6612 (void) printf(gettext(" 15 user/group space accounting\n"));
6613 (void) printf(gettext(" 16 stmf property support\n"));
6614 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6615 (void) printf(gettext(" 18 Snapshot user holds\n"));
6616 (void) printf(gettext(" 19 Log device removal\n"));
6617 (void) printf(gettext(" 20 Compression using zle "
6618 "(zero-length encoding)\n"));
6619 (void) printf(gettext(" 21 Deduplication\n"));
6620 (void) printf(gettext(" 22 Received properties\n"));
6621 (void) printf(gettext(" 23 Slim ZIL\n"));
6622 (void) printf(gettext(" 24 System attributes\n"));
6623 (void) printf(gettext(" 25 Improved scrub stats\n"));
6624 (void) printf(gettext(" 26 Improved snapshot deletion "
6626 (void) printf(gettext(" 27 Improved snapshot creation "
6628 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6629 (void) printf(gettext("\nFor more information on a particular "
6630 "version, including supported releases,\n"));
6631 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6632 } else if (argc == 0 && upgradeall) {
6633 cb.cb_first = B_TRUE;
6634 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
6635 if (ret == 0 && cb.cb_first) {
6636 if (cb.cb_version == SPA_VERSION) {
6637 (void) printf(gettext("All pools are already "
6638 "formatted using feature flags.\n\n"));
6639 (void) printf(gettext("Every feature flags "
6640 "pool already has all supported features "
6643 (void) printf(gettext("All pools are already "
6644 "formatted with version %llu or higher.\n"),
6645 (u_longlong_t)cb.cb_version);
6648 } else if (argc == 0) {
6649 cb.cb_first = B_TRUE;
6650 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
6654 (void) printf(gettext("All pools are formatted "
6655 "using feature flags.\n\n"));
6657 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6658 "for a list of available legacy versions.\n"));
6661 cb.cb_first = B_TRUE;
6662 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
6666 (void) printf(gettext("Every feature flags pool has "
6667 "all supported features enabled.\n"));
6669 (void) printf(gettext("\n"));
6672 ret = for_each_pool(argc, argv, B_FALSE, NULL,
6679 typedef struct hist_cbdata {
6686 * Print out the command history for a specific pool.
6689 get_history_one(zpool_handle_t *zhp, void *data)
6695 hist_cbdata_t *cb = (hist_cbdata_t *)data;
6697 cb->first = B_FALSE;
6699 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
6701 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
6704 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
6705 &records, &numrecords) == 0);
6706 for (i = 0; i < numrecords; i++) {
6707 nvlist_t *rec = records[i];
6710 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
6714 tsec = fnvlist_lookup_uint64(records[i],
6716 (void) localtime_r(&tsec, &t);
6717 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
6720 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
6721 (void) printf("%s %s", tbuf,
6722 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
6723 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
6725 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
6728 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
6729 (void) printf("%s unrecognized record:\n",
6731 dump_nvlist(rec, 4);
6734 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
6735 zfs_history_event_names[ievent],
6736 (longlong_t)fnvlist_lookup_uint64(
6737 rec, ZPOOL_HIST_TXG),
6738 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
6739 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
6742 (void) printf("%s [txg:%lld] %s", tbuf,
6743 (longlong_t)fnvlist_lookup_uint64(
6744 rec, ZPOOL_HIST_TXG),
6745 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
6746 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
6747 (void) printf(" %s (%llu)",
6748 fnvlist_lookup_string(rec,
6750 (u_longlong_t)fnvlist_lookup_uint64(rec,
6753 (void) printf(" %s", fnvlist_lookup_string(rec,
6754 ZPOOL_HIST_INT_STR));
6755 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
6758 (void) printf("%s ioctl %s\n", tbuf,
6759 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
6760 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
6761 (void) printf(" input:\n");
6762 dump_nvlist(fnvlist_lookup_nvlist(rec,
6763 ZPOOL_HIST_INPUT_NVL), 8);
6765 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
6766 (void) printf(" output:\n");
6767 dump_nvlist(fnvlist_lookup_nvlist(rec,
6768 ZPOOL_HIST_OUTPUT_NVL), 8);
6773 (void) printf("%s unrecognized record:\n", tbuf);
6774 dump_nvlist(rec, 4);
6778 (void) printf("\n");
6781 (void) printf(" [");
6782 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
6783 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
6784 struct passwd *pwd = getpwuid(who);
6785 (void) printf("user %d ", (int)who);
6787 (void) printf("(%s) ", pwd->pw_name);
6789 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
6790 (void) printf("on %s",
6791 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
6793 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
6794 (void) printf(":%s",
6795 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
6799 (void) printf("\n");
6801 (void) printf("\n");
6808 * zpool history <pool>
6810 * Displays the history of commands that modified pools.
6813 zpool_do_history(int argc, char **argv)
6815 hist_cbdata_t cbdata = { 0 };
6819 cbdata.first = B_TRUE;
6821 while ((c = getopt(argc, argv, "li")) != -1) {
6824 cbdata.longfmt = B_TRUE;
6827 cbdata.internal = B_TRUE;
6830 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6838 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
6841 if (argc == 0 && cbdata.first == B_TRUE) {
6842 (void) fprintf(stderr, gettext("no pools available\n"));
6849 typedef struct ev_opts {
6857 zpool_do_events_short(nvlist_t *nvl)
6859 char ctime_str[26], str[32], *ptr;
6863 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
6864 memset(str, ' ', 32);
6865 (void) ctime_r((const time_t *)&tv[0], ctime_str);
6866 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
6867 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
6868 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
6869 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
6870 (void) printf(gettext("%s "), str);
6872 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
6873 (void) printf(gettext("%s\n"), ptr);
6877 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
6881 for (nvp = nvlist_next_nvpair(nvl, NULL);
6882 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
6884 data_type_t type = nvpair_type(nvp);
6885 const char *name = nvpair_name(nvp);
6895 printf(gettext("%*s%s = "), depth, "", name);
6898 case DATA_TYPE_BOOLEAN:
6899 printf(gettext("%s"), "1");
6902 case DATA_TYPE_BOOLEAN_VALUE:
6903 (void) nvpair_value_boolean_value(nvp, &b);
6904 printf(gettext("%s"), b ? "1" : "0");
6907 case DATA_TYPE_BYTE:
6908 (void) nvpair_value_byte(nvp, &i8);
6909 printf(gettext("0x%x"), i8);
6912 case DATA_TYPE_INT8:
6913 (void) nvpair_value_int8(nvp, (void *)&i8);
6914 printf(gettext("0x%x"), i8);
6917 case DATA_TYPE_UINT8:
6918 (void) nvpair_value_uint8(nvp, &i8);
6919 printf(gettext("0x%x"), i8);
6922 case DATA_TYPE_INT16:
6923 (void) nvpair_value_int16(nvp, (void *)&i16);
6924 printf(gettext("0x%x"), i16);
6927 case DATA_TYPE_UINT16:
6928 (void) nvpair_value_uint16(nvp, &i16);
6929 printf(gettext("0x%x"), i16);
6932 case DATA_TYPE_INT32:
6933 (void) nvpair_value_int32(nvp, (void *)&i32);
6934 printf(gettext("0x%x"), i32);
6937 case DATA_TYPE_UINT32:
6938 (void) nvpair_value_uint32(nvp, &i32);
6939 printf(gettext("0x%x"), i32);
6942 case DATA_TYPE_INT64:
6943 (void) nvpair_value_int64(nvp, (void *)&i64);
6944 printf(gettext("0x%llx"), (u_longlong_t)i64);
6947 case DATA_TYPE_UINT64:
6948 (void) nvpair_value_uint64(nvp, &i64);
6950 * translate vdev state values to readable
6951 * strings to aide zpool events consumers
6954 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
6956 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
6957 printf(gettext("\"%s\" (0x%llx)"),
6958 zpool_state_to_name(i64, VDEV_AUX_NONE),
6961 printf(gettext("0x%llx"), (u_longlong_t)i64);
6965 case DATA_TYPE_HRTIME:
6966 (void) nvpair_value_hrtime(nvp, (void *)&i64);
6967 printf(gettext("0x%llx"), (u_longlong_t)i64);
6970 case DATA_TYPE_STRING:
6971 (void) nvpair_value_string(nvp, &str);
6972 printf(gettext("\"%s\""), str ? str : "<NULL>");
6975 case DATA_TYPE_NVLIST:
6976 printf(gettext("(embedded nvlist)\n"));
6977 (void) nvpair_value_nvlist(nvp, &cnv);
6978 zpool_do_events_nvprint(cnv, depth + 8);
6979 printf(gettext("%*s(end %s)"), depth, "", name);
6982 case DATA_TYPE_NVLIST_ARRAY: {
6986 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
6987 printf(gettext("(%d embedded nvlists)\n"), nelem);
6988 for (i = 0; i < nelem; i++) {
6989 printf(gettext("%*s%s[%d] = %s\n"),
6990 depth, "", name, i, "(embedded nvlist)");
6991 zpool_do_events_nvprint(val[i], depth + 8);
6992 printf(gettext("%*s(end %s[%i])\n"),
6993 depth, "", name, i);
6995 printf(gettext("%*s(end %s)\n"), depth, "", name);
6999 case DATA_TYPE_INT8_ARRAY: {
7003 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7004 for (i = 0; i < nelem; i++)
7005 printf(gettext("0x%x "), val[i]);
7010 case DATA_TYPE_UINT8_ARRAY: {
7014 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7015 for (i = 0; i < nelem; i++)
7016 printf(gettext("0x%x "), val[i]);
7021 case DATA_TYPE_INT16_ARRAY: {
7025 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7026 for (i = 0; i < nelem; i++)
7027 printf(gettext("0x%x "), val[i]);
7032 case DATA_TYPE_UINT16_ARRAY: {
7036 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7037 for (i = 0; i < nelem; i++)
7038 printf(gettext("0x%x "), val[i]);
7043 case DATA_TYPE_INT32_ARRAY: {
7047 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7048 for (i = 0; i < nelem; i++)
7049 printf(gettext("0x%x "), val[i]);
7054 case DATA_TYPE_UINT32_ARRAY: {
7058 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7059 for (i = 0; i < nelem; i++)
7060 printf(gettext("0x%x "), val[i]);
7065 case DATA_TYPE_INT64_ARRAY: {
7069 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7070 for (i = 0; i < nelem; i++)
7071 printf(gettext("0x%llx "),
7072 (u_longlong_t)val[i]);
7077 case DATA_TYPE_UINT64_ARRAY: {
7081 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7082 for (i = 0; i < nelem; i++)
7083 printf(gettext("0x%llx "),
7084 (u_longlong_t)val[i]);
7089 case DATA_TYPE_STRING_ARRAY: {
7093 (void) nvpair_value_string_array(nvp, &str, &nelem);
7094 for (i = 0; i < nelem; i++)
7095 printf(gettext("\"%s\" "),
7096 str[i] ? str[i] : "<NULL>");
7101 case DATA_TYPE_BOOLEAN_ARRAY:
7102 case DATA_TYPE_BYTE_ARRAY:
7103 case DATA_TYPE_DOUBLE:
7104 case DATA_TYPE_UNKNOWN:
7105 printf(gettext("<unknown>"));
7109 printf(gettext("\n"));
7114 zpool_do_events_next(ev_opts_t *opts)
7117 int zevent_fd, ret, dropped;
7119 zevent_fd = open(ZFS_DEV, O_RDWR);
7120 VERIFY(zevent_fd >= 0);
7122 if (!opts->scripted)
7123 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7126 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7127 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7128 if (ret || nvl == NULL)
7132 (void) printf(gettext("dropped %d events\n"), dropped);
7134 zpool_do_events_short(nvl);
7136 if (opts->verbose) {
7137 zpool_do_events_nvprint(nvl, 8);
7138 printf(gettext("\n"));
7140 (void) fflush(stdout);
7145 VERIFY(0 == close(zevent_fd));
7151 zpool_do_events_clear(ev_opts_t *opts)
7155 ret = zpool_events_clear(g_zfs, &count);
7157 (void) printf(gettext("cleared %d events\n"), count);
7163 * zpool events [-vfc]
7165 * Displays events logs by ZFS.
7168 zpool_do_events(int argc, char **argv)
7170 ev_opts_t opts = { 0 };
7175 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7190 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7199 ret = zpool_do_events_clear(&opts);
7201 ret = zpool_do_events_next(&opts);
7207 get_callback(zpool_handle_t *zhp, void *data)
7209 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7210 char value[MAXNAMELEN];
7211 zprop_source_t srctype;
7214 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7217 * Skip the special fake placeholder. This will also skip
7218 * over the name property when 'all' is specified.
7220 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7221 pl == cbp->cb_proplist)
7224 if (pl->pl_prop == ZPROP_INVAL &&
7225 (zpool_prop_feature(pl->pl_user_prop) ||
7226 zpool_prop_unsupported(pl->pl_user_prop))) {
7227 srctype = ZPROP_SRC_LOCAL;
7229 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7230 value, sizeof (value)) == 0) {
7231 zprop_print_one_property(zpool_get_name(zhp),
7232 cbp, pl->pl_user_prop, value, srctype,
7236 if (zpool_get_prop(zhp, pl->pl_prop, value,
7237 sizeof (value), &srctype, cbp->cb_literal) != 0)
7240 zprop_print_one_property(zpool_get_name(zhp), cbp,
7241 zpool_prop_to_name(pl->pl_prop), value, srctype,
7249 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7251 * -H Scripted mode. Don't display headers, and separate properties
7253 * -o List of columns to display. Defaults to
7254 * "name,property,value,source".
7255 * -p Display values in parsable (exact) format.
7257 * Get properties of pools in the system. Output space statistics
7258 * for each one as well as other attributes.
7261 zpool_do_get(int argc, char **argv)
7263 zprop_get_cbdata_t cb = { 0 };
7264 zprop_list_t fake_name = { 0 };
7269 cb.cb_first = B_TRUE;
7272 * Set up default columns and sources.
7274 cb.cb_sources = ZPROP_SRC_ALL;
7275 cb.cb_columns[0] = GET_COL_NAME;
7276 cb.cb_columns[1] = GET_COL_PROPERTY;
7277 cb.cb_columns[2] = GET_COL_VALUE;
7278 cb.cb_columns[3] = GET_COL_SOURCE;
7279 cb.cb_type = ZFS_TYPE_POOL;
7282 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7285 cb.cb_literal = B_TRUE;
7288 cb.cb_scripted = B_TRUE;
7291 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7293 while (*optarg != '\0') {
7294 static char *col_subopts[] =
7295 { "name", "property", "value", "source",
7298 if (i == ZFS_GET_NCOLS) {
7299 (void) fprintf(stderr, gettext("too "
7300 "many fields given to -o "
7305 switch (getsubopt(&optarg, col_subopts,
7308 cb.cb_columns[i++] = GET_COL_NAME;
7311 cb.cb_columns[i++] = GET_COL_PROPERTY;
7314 cb.cb_columns[i++] = GET_COL_VALUE;
7317 cb.cb_columns[i++] = GET_COL_SOURCE;
7321 (void) fprintf(stderr,
7322 gettext("\"all\" conflicts "
7323 "with specific fields "
7324 "given to -o option\n"));
7327 cb.cb_columns[0] = GET_COL_NAME;
7328 cb.cb_columns[1] = GET_COL_PROPERTY;
7329 cb.cb_columns[2] = GET_COL_VALUE;
7330 cb.cb_columns[3] = GET_COL_SOURCE;
7334 (void) fprintf(stderr,
7335 gettext("invalid column name "
7342 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7352 (void) fprintf(stderr, gettext("missing property "
7357 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7358 ZFS_TYPE_POOL) != 0)
7364 if (cb.cb_proplist != NULL) {
7365 fake_name.pl_prop = ZPOOL_PROP_NAME;
7366 fake_name.pl_width = strlen(gettext("NAME"));
7367 fake_name.pl_next = cb.cb_proplist;
7368 cb.cb_proplist = &fake_name;
7371 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7374 if (cb.cb_proplist == &fake_name)
7375 zprop_free_list(fake_name.pl_next);
7377 zprop_free_list(cb.cb_proplist);
7382 typedef struct set_cbdata {
7385 boolean_t cb_any_successful;
7389 set_callback(zpool_handle_t *zhp, void *data)
7392 set_cbdata_t *cb = (set_cbdata_t *)data;
7394 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7397 cb->cb_any_successful = B_TRUE;
7403 zpool_do_set(int argc, char **argv)
7405 set_cbdata_t cb = { 0 };
7408 if (argc > 1 && argv[1][0] == '-') {
7409 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7415 (void) fprintf(stderr, gettext("missing property=value "
7421 (void) fprintf(stderr, gettext("missing pool name\n"));
7426 (void) fprintf(stderr, gettext("too many pool names\n"));
7430 cb.cb_propname = argv[1];
7431 cb.cb_value = strchr(cb.cb_propname, '=');
7432 if (cb.cb_value == NULL) {
7433 (void) fprintf(stderr, gettext("missing value in "
7434 "property=value argument\n"));
7438 *(cb.cb_value) = '\0';
7441 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
7448 find_command_idx(char *command, int *idx)
7452 for (i = 0; i < NCOMMAND; i++) {
7453 if (command_table[i].name == NULL)
7456 if (strcmp(command, command_table[i].name) == 0) {
7465 main(int argc, char **argv)
7471 (void) setlocale(LC_ALL, "");
7472 (void) textdomain(TEXT_DOMAIN);
7475 dprintf_setup(&argc, argv);
7480 * Make sure the user has specified some command.
7483 (void) fprintf(stderr, gettext("missing command\n"));
7492 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
7495 if ((g_zfs = libzfs_init()) == NULL) {
7496 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7500 libzfs_print_on_error(g_zfs, B_TRUE);
7502 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7505 * Run the appropriate command.
7507 if (find_command_idx(cmdname, &i) == 0) {
7508 current_command = &command_table[i];
7509 ret = command_table[i].func(argc - 1, argv + 1);
7510 } else if (strchr(cmdname, '=')) {
7511 verify(find_command_idx("set", &i) == 0);
7512 current_command = &command_table[i];
7513 ret = command_table[i].func(argc, argv);
7514 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
7516 * 'freeze' is a vile debugging abomination, so we treat
7520 int fd = open(ZFS_DEV, O_RDWR);
7521 (void) strlcpy((void *)buf, argv[2], sizeof (buf));
7522 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
7524 (void) fprintf(stderr, gettext("unrecognized "
7525 "command '%s'\n"), cmdname);
7530 if (ret == 0 && log_history)
7531 (void) zpool_log_history(g_zfs, history_str);
7536 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7537 * for the purposes of running ::findleaks.
7539 if (getenv("ZFS_ABORT") != NULL) {
7540 (void) printf("dumping core by request\n");