4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
49 #include <sys/fs/zfs.h>
51 #include <sys/fm/fs/zfs.h>
52 #include <sys/fm/util.h>
53 #include <sys/fm/protocol.h>
54 #include <sys/zfs_ioctl.h>
59 #include "zpool_util.h"
60 #include "zfs_comutil.h"
61 #include "zfeature_common.h"
63 #include "statcommon.h"
65 static int zpool_do_create(int, char **);
66 static int zpool_do_destroy(int, char **);
68 static int zpool_do_add(int, char **);
69 static int zpool_do_remove(int, char **);
70 static int zpool_do_labelclear(int, char **);
72 static int zpool_do_list(int, char **);
73 static int zpool_do_iostat(int, char **);
74 static int zpool_do_status(int, char **);
76 static int zpool_do_online(int, char **);
77 static int zpool_do_offline(int, char **);
78 static int zpool_do_clear(int, char **);
79 static int zpool_do_reopen(int, char **);
81 static int zpool_do_reguid(int, char **);
83 static int zpool_do_attach(int, char **);
84 static int zpool_do_detach(int, char **);
85 static int zpool_do_replace(int, char **);
86 static int zpool_do_split(int, char **);
88 static int zpool_do_scrub(int, char **);
90 static int zpool_do_import(int, char **);
91 static int zpool_do_export(int, char **);
93 static int zpool_do_upgrade(int, char **);
95 static int zpool_do_history(int, char **);
96 static int zpool_do_events(int, char **);
98 static int zpool_do_get(int, char **);
99 static int zpool_do_set(int, char **);
102 * These libumem hooks provide a reasonable set of defaults for the allocator's
103 * debugging facilities.
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
150 * Flags for stats to display with "zpool iostats"
158 IOS_COUNT, /* always last element */
161 /* iostat_type entries as bitmasks */
162 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
163 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
164 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
165 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
166 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
168 /* Mask of all the histo bits */
169 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
172 * Lookup table for iostat flags to nvlist names. Basically a list
173 * of all the nvlists a flag requires. Also specifies the order in
174 * which data gets printed in zpool iostat.
176 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
178 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
179 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
180 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
181 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
182 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
183 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
184 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
185 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
186 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
195 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
196 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
197 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
198 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
199 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
202 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
203 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
204 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
205 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
206 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
207 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
208 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
209 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
210 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
211 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
217 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
218 * Right now, only one histo bit is ever set at one time, so we can
219 * just do a highbit64(a)
221 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
223 typedef struct zpool_command {
225 int (*func)(int, char **);
230 * Master command table. Each ZFS command has a name, associated function, and
231 * usage message. The usage messages need to be internationalized, so we have
232 * to have a function to return the usage message based on a command index.
234 * These commands are organized according to how they are displayed in the usage
235 * message. An empty command (one with a NULL name) indicates an empty line in
236 * the generic usage message.
238 static zpool_command_t command_table[] = {
239 { "create", zpool_do_create, HELP_CREATE },
240 { "destroy", zpool_do_destroy, HELP_DESTROY },
242 { "add", zpool_do_add, HELP_ADD },
243 { "remove", zpool_do_remove, HELP_REMOVE },
245 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
247 { "list", zpool_do_list, HELP_LIST },
248 { "iostat", zpool_do_iostat, HELP_IOSTAT },
249 { "status", zpool_do_status, HELP_STATUS },
251 { "online", zpool_do_online, HELP_ONLINE },
252 { "offline", zpool_do_offline, HELP_OFFLINE },
253 { "clear", zpool_do_clear, HELP_CLEAR },
254 { "reopen", zpool_do_reopen, HELP_REOPEN },
256 { "attach", zpool_do_attach, HELP_ATTACH },
257 { "detach", zpool_do_detach, HELP_DETACH },
258 { "replace", zpool_do_replace, HELP_REPLACE },
259 { "split", zpool_do_split, HELP_SPLIT },
261 { "scrub", zpool_do_scrub, HELP_SCRUB },
263 { "import", zpool_do_import, HELP_IMPORT },
264 { "export", zpool_do_export, HELP_EXPORT },
265 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
266 { "reguid", zpool_do_reguid, HELP_REGUID },
268 { "history", zpool_do_history, HELP_HISTORY },
269 { "events", zpool_do_events, HELP_EVENTS },
271 { "get", zpool_do_get, HELP_GET },
272 { "set", zpool_do_set, HELP_SET },
275 #define NCOMMAND (ARRAY_SIZE(command_table))
277 static zpool_command_t *current_command;
278 static char history_str[HIS_MAX_RECORD_LEN];
279 static boolean_t log_history = B_TRUE;
280 static uint_t timestamp_fmt = NODATE;
283 get_usage(zpool_help_t idx)
287 return (gettext("\tadd [-fgLnP] [-o property=value] "
288 "<pool> <vdev> ...\n"));
290 return (gettext("\tattach [-f] [-o property=value] "
291 "<pool> <device> <new-device>\n"));
293 return (gettext("\tclear [-nF] <pool> [device]\n"));
295 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
296 "\t [-O file-system-property=value] ... \n"
297 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
299 return (gettext("\tdestroy [-f] <pool>\n"));
301 return (gettext("\tdetach <pool> <device>\n"));
303 return (gettext("\texport [-af] <pool> ...\n"));
305 return (gettext("\thistory [-il] [<pool>] ...\n"));
307 return (gettext("\timport [-d dir] [-D]\n"
308 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
309 "\timport [-o mntopts] [-o property=value] ... \n"
310 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
311 "[-R root] [-F [-n]] -a\n"
312 "\timport [-o mntopts] [-o property=value] ... \n"
313 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
314 "[-R root] [-F [-n]]\n"
315 "\t <pool | id> [newpool]\n"));
317 return (gettext("\tiostat [-c CMD] [-T d | u] [-ghHLpPvy] "
319 "\t [[pool ...]|[pool vdev ...]|[vdev ...]] "
320 "[interval [count]]\n"));
321 case HELP_LABELCLEAR:
322 return (gettext("\tlabelclear [-f] <vdev>\n"));
324 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
325 "[-T d|u] [pool] ... [interval [count]]\n"));
327 return (gettext("\toffline [-t] <pool> <device> ...\n"));
329 return (gettext("\tonline <pool> <device> ...\n"));
331 return (gettext("\treplace [-f] [-o property=value] "
332 "<pool> <device> [new-device]\n"));
334 return (gettext("\tremove <pool> <device> ...\n"));
336 return (gettext("\treopen <pool>\n"));
338 return (gettext("\tscrub [-s] <pool> ...\n"));
340 return (gettext("\tstatus [-c CMD] [-gLPvxD] [-T d|u] [pool]"
341 " ... [interval [count]]\n"));
343 return (gettext("\tupgrade\n"
345 "\tupgrade [-V version] <-a | pool ...>\n"));
347 return (gettext("\tevents [-vHfc]\n"));
349 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
350 "<\"all\" | property[,...]> <pool> ...\n"));
352 return (gettext("\tset <property=value> <pool> \n"));
354 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
355 "\t [-o property=value] <pool> <newpool> "
356 "[<device> ...]\n"));
358 return (gettext("\treguid <pool>\n"));
367 * Callback routine that will print out a pool property value.
370 print_prop_cb(int prop, void *cb)
374 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
376 if (zpool_prop_readonly(prop))
377 (void) fprintf(fp, " NO ");
379 (void) fprintf(fp, " YES ");
381 if (zpool_prop_values(prop) == NULL)
382 (void) fprintf(fp, "-\n");
384 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
390 * Display usage message. If we're inside a command, display only the usage for
391 * that command. Otherwise, iterate over the entire command table and display
392 * a complete usage message.
395 usage(boolean_t requested)
397 FILE *fp = requested ? stdout : stderr;
399 if (current_command == NULL) {
402 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
404 gettext("where 'command' is one of the following:\n\n"));
406 for (i = 0; i < NCOMMAND; i++) {
407 if (command_table[i].name == NULL)
408 (void) fprintf(fp, "\n");
410 (void) fprintf(fp, "%s",
411 get_usage(command_table[i].usage));
414 (void) fprintf(fp, gettext("usage:\n"));
415 (void) fprintf(fp, "%s", get_usage(current_command->usage));
418 if (current_command != NULL &&
419 ((strcmp(current_command->name, "set") == 0) ||
420 (strcmp(current_command->name, "get") == 0) ||
421 (strcmp(current_command->name, "list") == 0))) {
424 gettext("\nthe following properties are supported:\n"));
426 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
427 "PROPERTY", "EDIT", "VALUES");
429 /* Iterate over all properties */
430 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
433 (void) fprintf(fp, "\t%-15s ", "feature@...");
434 (void) fprintf(fp, "YES disabled | enabled | active\n");
436 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
437 "appended with a feature name.\nSee zpool-features(5).\n"));
441 * See comments at end of main().
443 if (getenv("ZFS_ABORT") != NULL) {
444 (void) printf("dumping core by request\n");
448 exit(requested ? 0 : 2);
452 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
453 boolean_t print_logs, int name_flags)
460 (void) printf("\t%*s%s\n", indent, "", name);
462 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
463 &child, &children) != 0)
466 for (c = 0; c < children; c++) {
467 uint64_t is_log = B_FALSE;
469 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
471 if ((is_log && !print_logs) || (!is_log && print_logs))
474 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
475 print_vdev_tree(zhp, vname, child[c], indent + 2,
476 B_FALSE, name_flags);
482 prop_list_contains_feature(nvlist_t *proplist)
485 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
486 nvp = nvlist_next_nvpair(proplist, nvp)) {
487 if (zpool_prop_feature(nvpair_name(nvp)))
494 * Add a property pair (name, string-value) into a property nvlist.
497 add_prop_list(const char *propname, char *propval, nvlist_t **props,
500 zpool_prop_t prop = ZPROP_INVAL;
506 if (*props == NULL &&
507 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
508 (void) fprintf(stderr,
509 gettext("internal error: out of memory\n"));
516 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
518 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
519 !zpool_prop_feature(propname)) {
520 (void) fprintf(stderr, gettext("property '%s' is "
521 "not a valid pool property\n"), propname);
526 * feature@ properties and version should not be specified
529 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
530 nvlist_exists(proplist, vname)) ||
531 (prop == ZPOOL_PROP_VERSION &&
532 prop_list_contains_feature(proplist))) {
533 (void) fprintf(stderr, gettext("'feature@' and "
534 "'version' properties cannot be specified "
540 if (zpool_prop_feature(propname))
543 normnm = zpool_prop_to_name(prop);
545 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
546 normnm = zfs_prop_to_name(fprop);
552 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
553 prop != ZPOOL_PROP_CACHEFILE) {
554 (void) fprintf(stderr, gettext("property '%s' "
555 "specified multiple times\n"), propname);
559 if (nvlist_add_string(proplist, normnm, propval) != 0) {
560 (void) fprintf(stderr, gettext("internal "
561 "error: out of memory\n"));
569 * Set a default property pair (name, string-value) in a property nvlist
572 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
577 if (nvlist_lookup_string(*props, propname, &pval) == 0)
580 return (add_prop_list(propname, propval, props, B_TRUE));
584 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
586 * -f Force addition of devices, even if they appear in use
587 * -g Display guid for individual vdev name.
588 * -L Follow links when resolving vdev path name.
589 * -n Do not add the devices, but display the resulting layout if
590 * they were to be added.
591 * -o Set property=value.
592 * -P Display full path for vdev name.
594 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
595 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
599 zpool_do_add(int argc, char **argv)
601 boolean_t force = B_FALSE;
602 boolean_t dryrun = B_FALSE;
610 nvlist_t *props = NULL;
614 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
620 name_flags |= VDEV_NAME_GUID;
623 name_flags |= VDEV_NAME_FOLLOW_LINKS;
629 if ((propval = strchr(optarg, '=')) == NULL) {
630 (void) fprintf(stderr, gettext("missing "
631 "'=' for -o option\n"));
637 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
638 (add_prop_list(optarg, propval, &props, B_TRUE)))
642 name_flags |= VDEV_NAME_PATH;
645 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
654 /* get pool name and check number of arguments */
656 (void) fprintf(stderr, gettext("missing pool name argument\n"));
660 (void) fprintf(stderr, gettext("missing vdev specification\n"));
669 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
672 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
673 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
679 /* pass off to get_vdev_spec for processing */
680 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
682 if (nvroot == NULL) {
688 nvlist_t *poolnvroot;
690 uint_t l2children, c;
692 boolean_t hadcache = B_FALSE;
694 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
697 (void) printf(gettext("would update '%s' to the following "
698 "configuration:\n"), zpool_get_name(zhp));
700 /* print original main pool and new tree */
701 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
703 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
705 /* Do the same for the logs */
706 if (num_logs(poolnvroot) > 0) {
707 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
709 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
711 } else if (num_logs(nvroot) > 0) {
712 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
716 /* Do the same for the caches */
717 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
718 &l2child, &l2children) == 0 && l2children) {
720 (void) printf(gettext("\tcache\n"));
721 for (c = 0; c < l2children; c++) {
722 vname = zpool_vdev_name(g_zfs, NULL,
723 l2child[c], name_flags);
724 (void) printf("\t %s\n", vname);
728 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
729 &l2child, &l2children) == 0 && l2children) {
731 (void) printf(gettext("\tcache\n"));
732 for (c = 0; c < l2children; c++) {
733 vname = zpool_vdev_name(g_zfs, NULL,
734 l2child[c], name_flags);
735 (void) printf("\t %s\n", vname);
742 ret = (zpool_add(zhp, nvroot) != 0);
753 * zpool remove <pool> <vdev> ...
755 * Removes the given vdev from the pool. Currently, this supports removing
756 * spares, cache, and log devices from the pool.
759 zpool_do_remove(int argc, char **argv)
763 zpool_handle_t *zhp = NULL;
768 /* get pool name and check number of arguments */
770 (void) fprintf(stderr, gettext("missing pool name argument\n"));
774 (void) fprintf(stderr, gettext("missing device\n"));
780 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
783 for (i = 1; i < argc; i++) {
784 if (zpool_vdev_remove(zhp, argv[i]) != 0)
793 * zpool labelclear [-f] <vdev>
795 * -f Force clearing the label for the vdevs which are members of
796 * the exported or foreign pools.
798 * Verifies that the vdev is not active and zeros out the label information
802 zpool_do_labelclear(int argc, char **argv)
804 char vdev[MAXPATHLEN];
807 int c, fd = -1, ret = 0;
810 boolean_t inuse = B_FALSE;
811 boolean_t force = B_FALSE;
814 while ((c = getopt(argc, argv, "f")) != -1) {
820 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
831 (void) fprintf(stderr, gettext("missing vdev name\n"));
835 (void) fprintf(stderr, gettext("too many arguments\n"));
840 * Check if we were given absolute path and use it as is.
841 * Otherwise if the provided vdev name doesn't point to a file,
842 * try prepending expected disk paths and partition numbers.
844 (void) strlcpy(vdev, argv[0], sizeof (vdev));
845 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
848 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
849 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
850 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
854 if (error || (stat(vdev, &st) != 0)) {
855 (void) fprintf(stderr, gettext(
856 "failed to find device %s, try specifying absolute "
857 "path instead\n"), argv[0]);
862 if ((fd = open(vdev, O_RDWR)) < 0) {
863 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
864 vdev, strerror(errno));
868 if (ioctl(fd, BLKFLSBUF) != 0)
869 (void) fprintf(stderr, gettext("failed to invalidate "
870 "cache for %s: %s\n"), vdev, strerror(errno));
872 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
873 (void) fprintf(stderr,
874 gettext("failed to check state for %s\n"), vdev);
879 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
881 (void) fprintf(stderr,
882 gettext("failed to check state for %s\n"), vdev);
891 case POOL_STATE_ACTIVE:
892 case POOL_STATE_SPARE:
893 case POOL_STATE_L2CACHE:
894 (void) fprintf(stderr, gettext(
895 "%s is a member (%s) of pool \"%s\"\n"),
896 vdev, zpool_pool_state_to_name(state), name);
900 case POOL_STATE_EXPORTED:
903 (void) fprintf(stderr, gettext(
904 "use '-f' to override the following error:\n"
905 "%s is a member of exported pool \"%s\"\n"),
910 case POOL_STATE_POTENTIALLY_ACTIVE:
913 (void) fprintf(stderr, gettext(
914 "use '-f' to override the following error:\n"
915 "%s is a member of potentially active pool \"%s\"\n"),
920 case POOL_STATE_DESTROYED:
921 /* inuse should never be set for a destroyed pool */
927 ret = zpool_clear_label(fd);
929 (void) fprintf(stderr,
930 gettext("failed to clear label for %s\n"), vdev);
941 * zpool create [-fnd] [-o property=value] ...
942 * [-O file-system-property=value] ...
943 * [-R root] [-m mountpoint] <pool> <dev> ...
945 * -f Force creation, even if devices appear in use
946 * -n Do not create the pool, but display the resulting layout if it
947 * were to be created.
948 * -R Create a pool under an alternate root
949 * -m Set default mountpoint for the root dataset. By default it's
951 * -o Set property=value.
952 * -o Set feature@feature=enabled|disabled.
953 * -d Don't automatically enable all supported pool features
954 * (individual features can be enabled with -o).
955 * -O Set fsproperty=value in the pool's root file system
957 * Creates the named pool according to the given vdev specification. The
958 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
959 * we get the nvlist back from get_vdev_spec(), we either print out the contents
960 * (if '-n' was specified), or pass it to libzfs to do the creation.
963 zpool_do_create(int argc, char **argv)
965 boolean_t force = B_FALSE;
966 boolean_t dryrun = B_FALSE;
967 boolean_t enable_all_pool_feat = B_TRUE;
969 nvlist_t *nvroot = NULL;
973 char *altroot = NULL;
974 char *mountpoint = NULL;
975 nvlist_t *fsprops = NULL;
976 nvlist_t *props = NULL;
980 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
989 enable_all_pool_feat = B_FALSE;
993 if (add_prop_list(zpool_prop_to_name(
994 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
996 if (add_prop_list_default(zpool_prop_to_name(
997 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1001 /* Equivalent to -O mountpoint=optarg */
1002 mountpoint = optarg;
1005 if ((propval = strchr(optarg, '=')) == NULL) {
1006 (void) fprintf(stderr, gettext("missing "
1007 "'=' for -o option\n"));
1013 if (add_prop_list(optarg, propval, &props, B_TRUE))
1017 * If the user is creating a pool that doesn't support
1018 * feature flags, don't enable any features.
1020 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1024 ver = strtoull(propval, &end, 10);
1026 ver < SPA_VERSION_FEATURES) {
1027 enable_all_pool_feat = B_FALSE;
1030 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1034 if ((propval = strchr(optarg, '=')) == NULL) {
1035 (void) fprintf(stderr, gettext("missing "
1036 "'=' for -O option\n"));
1043 * Mountpoints are checked and then added later.
1044 * Uniquely among properties, they can be specified
1045 * more than once, to avoid conflict with -m.
1047 if (0 == strcmp(optarg,
1048 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1049 mountpoint = propval;
1050 } else if (add_prop_list(optarg, propval, &fsprops,
1057 * Sanity check temporary pool name.
1059 if (strchr(optarg, '/') != NULL) {
1060 (void) fprintf(stderr, gettext("cannot create "
1061 "'%s': invalid character '/' in temporary "
1063 (void) fprintf(stderr, gettext("use 'zfs "
1064 "create' to create a dataset\n"));
1068 if (add_prop_list(zpool_prop_to_name(
1069 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1071 if (add_prop_list_default(zpool_prop_to_name(
1072 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1077 (void) fprintf(stderr, gettext("missing argument for "
1078 "'%c' option\n"), optopt);
1081 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1090 /* get pool name and check number of arguments */
1092 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1096 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1103 * As a special case, check for use of '/' in the name, and direct the
1104 * user to use 'zfs create' instead.
1106 if (strchr(poolname, '/') != NULL) {
1107 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1108 "character '/' in pool name\n"), poolname);
1109 (void) fprintf(stderr, gettext("use 'zfs create' to "
1110 "create a dataset\n"));
1114 /* pass off to get_vdev_spec for bulk processing */
1115 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1116 argc - 1, argv + 1);
1120 /* make_root_vdev() allows 0 toplevel children if there are spares */
1121 if (!zfs_allocatable_devs(nvroot)) {
1122 (void) fprintf(stderr, gettext("invalid vdev "
1123 "specification: at least one toplevel vdev must be "
1128 if (altroot != NULL && altroot[0] != '/') {
1129 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1130 "must be an absolute path\n"), altroot);
1135 * Check the validity of the mountpoint and direct the user to use the
1136 * '-m' mountpoint option if it looks like its in use.
1138 if (mountpoint == NULL ||
1139 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1140 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1141 char buf[MAXPATHLEN];
1144 if (mountpoint && mountpoint[0] != '/') {
1145 (void) fprintf(stderr, gettext("invalid mountpoint "
1146 "'%s': must be an absolute path, 'legacy', or "
1147 "'none'\n"), mountpoint);
1151 if (mountpoint == NULL) {
1152 if (altroot != NULL)
1153 (void) snprintf(buf, sizeof (buf), "%s/%s",
1156 (void) snprintf(buf, sizeof (buf), "/%s",
1159 if (altroot != NULL)
1160 (void) snprintf(buf, sizeof (buf), "%s%s",
1161 altroot, mountpoint);
1163 (void) snprintf(buf, sizeof (buf), "%s",
1167 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1168 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1169 "%s\n"), buf, strerror(errno));
1170 (void) fprintf(stderr, gettext("use '-m' "
1171 "option to provide a different default\n"));
1176 while (count < 3 && readdir(dirp) != NULL)
1178 (void) closedir(dirp);
1181 (void) fprintf(stderr, gettext("mountpoint "
1182 "'%s' exists and is not empty\n"), buf);
1183 (void) fprintf(stderr, gettext("use '-m' "
1184 "option to provide a "
1185 "different default\n"));
1192 * Now that the mountpoint's validity has been checked, ensure that
1193 * the property is set appropriately prior to creating the pool.
1195 if (mountpoint != NULL) {
1196 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1197 mountpoint, &fsprops, B_FALSE);
1205 * For a dry run invocation, print out a basic message and run
1206 * through all the vdevs in the list and print out in an
1207 * appropriate hierarchy.
1209 (void) printf(gettext("would create '%s' with the "
1210 "following layout:\n\n"), poolname);
1212 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1213 if (num_logs(nvroot) > 0)
1214 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1219 * Hand off to libzfs.
1222 for (i = 0; i < SPA_FEATURES; i++) {
1223 char propname[MAXPATHLEN];
1225 zfeature_info_t *feat = &spa_feature_table[i];
1227 (void) snprintf(propname, sizeof (propname),
1228 "feature@%s", feat->fi_uname);
1231 * Only features contained in props will be enabled:
1232 * remove from the nvlist every ZFS_FEATURE_DISABLED
1233 * value and add every missing ZFS_FEATURE_ENABLED if
1234 * enable_all_pool_feat is set.
1236 if (!nvlist_lookup_string(props, propname, &propval)) {
1237 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1238 (void) nvlist_remove_all(props,
1240 } else if (enable_all_pool_feat) {
1241 ret = add_prop_list(propname,
1242 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1249 if (zpool_create(g_zfs, poolname,
1250 nvroot, props, fsprops) == 0) {
1251 zfs_handle_t *pool = zfs_open(g_zfs,
1252 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1254 if (zfs_mount(pool, NULL, 0) == 0)
1255 ret = zfs_shareall(pool);
1258 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1259 (void) fprintf(stderr, gettext("pool name may have "
1265 nvlist_free(nvroot);
1266 nvlist_free(fsprops);
1270 nvlist_free(fsprops);
1277 * zpool destroy <pool>
1279 * -f Forcefully unmount any datasets
1281 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1284 zpool_do_destroy(int argc, char **argv)
1286 boolean_t force = B_FALSE;
1289 zpool_handle_t *zhp;
1293 while ((c = getopt(argc, argv, "f")) != -1) {
1299 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1308 /* check arguments */
1310 (void) fprintf(stderr, gettext("missing pool argument\n"));
1314 (void) fprintf(stderr, gettext("too many arguments\n"));
1320 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1322 * As a special case, check for use of '/' in the name, and
1323 * direct the user to use 'zfs destroy' instead.
1325 if (strchr(pool, '/') != NULL)
1326 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1327 "destroy a dataset\n"));
1331 if (zpool_disable_datasets(zhp, force) != 0) {
1332 (void) fprintf(stderr, gettext("could not destroy '%s': "
1333 "could not unmount datasets\n"), zpool_get_name(zhp));
1338 /* The history must be logged as part of the export */
1339 log_history = B_FALSE;
1341 ret = (zpool_destroy(zhp, history_str) != 0);
1348 typedef struct export_cbdata {
1350 boolean_t hardforce;
1357 zpool_export_one(zpool_handle_t *zhp, void *data)
1359 export_cbdata_t *cb = data;
1361 if (zpool_disable_datasets(zhp, cb->force) != 0)
1364 /* The history must be logged as part of the export */
1365 log_history = B_FALSE;
1367 if (cb->hardforce) {
1368 if (zpool_export_force(zhp, history_str) != 0)
1370 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1378 * zpool export [-f] <pool> ...
1380 * -a Export all pools
1381 * -f Forcefully unmount datasets
1383 * Export the given pools. By default, the command will attempt to cleanly
1384 * unmount any active datasets within the pool. If the '-f' flag is specified,
1385 * then the datasets will be forcefully unmounted.
1388 zpool_do_export(int argc, char **argv)
1391 boolean_t do_all = B_FALSE;
1392 boolean_t force = B_FALSE;
1393 boolean_t hardforce = B_FALSE;
1397 while ((c = getopt(argc, argv, "afF")) != -1) {
1409 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1416 cb.hardforce = hardforce;
1422 (void) fprintf(stderr, gettext("too many arguments\n"));
1426 return (for_each_pool(argc, argv, B_TRUE, NULL,
1427 zpool_export_one, &cb));
1430 /* check arguments */
1432 (void) fprintf(stderr, gettext("missing pool argument\n"));
1436 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1442 * Given a vdev configuration, determine the maximum width needed for the device
1446 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1454 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1455 if (strlen(name) + depth > max)
1456 max = strlen(name) + depth;
1460 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1461 &child, &children) == 0) {
1462 for (c = 0; c < children; c++)
1463 if ((ret = max_width(zhp, child[c], depth + 2,
1464 max, name_flags)) > max)
1468 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1469 &child, &children) == 0) {
1470 for (c = 0; c < children; c++)
1471 if ((ret = max_width(zhp, child[c], depth + 2,
1472 max, name_flags)) > max)
1476 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1477 &child, &children) == 0) {
1478 for (c = 0; c < children; c++)
1479 if ((ret = max_width(zhp, child[c], depth + 2,
1480 max, name_flags)) > max)
1487 typedef struct spare_cbdata {
1489 zpool_handle_t *cb_zhp;
1493 find_vdev(nvlist_t *nv, uint64_t search)
1499 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1503 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1504 &child, &children) == 0) {
1505 for (c = 0; c < children; c++)
1506 if (find_vdev(child[c], search))
1514 find_spare(zpool_handle_t *zhp, void *data)
1516 spare_cbdata_t *cbp = data;
1517 nvlist_t *config, *nvroot;
1519 config = zpool_get_config(zhp, NULL);
1520 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1523 if (find_vdev(nvroot, cbp->cb_guid)) {
1532 typedef struct status_cbdata {
1536 boolean_t cb_allpools;
1537 boolean_t cb_verbose;
1538 boolean_t cb_explain;
1540 boolean_t cb_dedup_stats;
1541 boolean_t cb_print_status;
1542 vdev_cmd_data_list_t *vcdl;
1545 /* Print output line for specific vdev in a specific pool */
1547 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1550 for (i = 0; i < vcdl->count; i++) {
1551 if ((strcmp(vcdl->data[i].path, path) == 0) &&
1552 (strcmp(vcdl->data[i].pool, pool) == 0)) {
1553 printf("%s", vcdl->data[i].line);
1560 * Print out configuration state as requested by status_callback.
1563 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1564 nvlist_t *nv, int depth, boolean_t isspare)
1568 pool_scan_stat_t *ps = NULL;
1570 char rbuf[6], wbuf[6], cbuf[6];
1572 uint64_t notpresent;
1573 spare_cbdata_t spare_cb;
1577 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1578 &child, &children) != 0)
1581 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1582 (uint64_t **)&vs, &c) == 0);
1584 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1587 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1590 if (vs->vs_aux == VDEV_AUX_SPARED)
1592 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1596 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1600 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1601 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1602 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1603 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1606 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1607 ¬present) == 0) {
1608 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1609 (void) printf(" was %s", path);
1610 } else if (vs->vs_aux != 0) {
1613 switch (vs->vs_aux) {
1614 case VDEV_AUX_OPEN_FAILED:
1615 (void) printf(gettext("cannot open"));
1618 case VDEV_AUX_BAD_GUID_SUM:
1619 (void) printf(gettext("missing device"));
1622 case VDEV_AUX_NO_REPLICAS:
1623 (void) printf(gettext("insufficient replicas"));
1626 case VDEV_AUX_VERSION_NEWER:
1627 (void) printf(gettext("newer version"));
1630 case VDEV_AUX_UNSUP_FEAT:
1631 (void) printf(gettext("unsupported feature(s)"));
1634 case VDEV_AUX_SPARED:
1635 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1636 &spare_cb.cb_guid) == 0);
1637 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1638 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1639 zpool_get_name(zhp)) == 0)
1640 (void) printf(gettext("currently in "
1643 (void) printf(gettext("in use by "
1645 zpool_get_name(spare_cb.cb_zhp));
1646 zpool_close(spare_cb.cb_zhp);
1648 (void) printf(gettext("currently in use"));
1652 case VDEV_AUX_ERR_EXCEEDED:
1653 (void) printf(gettext("too many errors"));
1656 case VDEV_AUX_IO_FAILURE:
1657 (void) printf(gettext("experienced I/O failures"));
1660 case VDEV_AUX_BAD_LOG:
1661 (void) printf(gettext("bad intent log"));
1664 case VDEV_AUX_EXTERNAL:
1665 (void) printf(gettext("external device fault"));
1668 case VDEV_AUX_SPLIT_POOL:
1669 (void) printf(gettext("split into new pool"));
1673 (void) printf(gettext("corrupted data"));
1678 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1679 (uint64_t **)&ps, &c);
1681 if (ps && ps->pss_state == DSS_SCANNING &&
1682 vs->vs_scan_processed != 0 && children == 0) {
1683 (void) printf(gettext(" (%s)"),
1684 (ps->pss_func == POOL_SCAN_RESILVER) ?
1685 "resilvering" : "repairing");
1688 if (cb->vcdl != NULL) {
1689 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1691 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1695 (void) printf("\n");
1697 for (c = 0; c < children; c++) {
1698 uint64_t islog = B_FALSE, ishole = B_FALSE;
1700 /* Don't print logs or holes here */
1701 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1703 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1705 if (islog || ishole)
1707 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1708 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1709 print_status_config(zhp, cb, vname, child[c], depth + 2,
1716 * Print the configuration of an exported pool. Iterate over all vdevs in the
1717 * pool, printing out the name and status for each one.
1720 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1728 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1729 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1730 strcmp(type, VDEV_TYPE_HOLE) == 0)
1733 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1734 (uint64_t **)&vs, &c) == 0);
1736 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1737 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1739 if (vs->vs_aux != 0) {
1742 switch (vs->vs_aux) {
1743 case VDEV_AUX_OPEN_FAILED:
1744 (void) printf(gettext("cannot open"));
1747 case VDEV_AUX_BAD_GUID_SUM:
1748 (void) printf(gettext("missing device"));
1751 case VDEV_AUX_NO_REPLICAS:
1752 (void) printf(gettext("insufficient replicas"));
1755 case VDEV_AUX_VERSION_NEWER:
1756 (void) printf(gettext("newer version"));
1759 case VDEV_AUX_UNSUP_FEAT:
1760 (void) printf(gettext("unsupported feature(s)"));
1763 case VDEV_AUX_ERR_EXCEEDED:
1764 (void) printf(gettext("too many errors"));
1768 (void) printf(gettext("corrupted data"));
1772 (void) printf("\n");
1774 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1775 &child, &children) != 0)
1778 for (c = 0; c < children; c++) {
1779 uint64_t is_log = B_FALSE;
1781 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1786 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1787 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1788 print_import_config(cb, vname, child[c], depth + 2);
1792 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1793 &child, &children) == 0) {
1794 (void) printf(gettext("\tcache\n"));
1795 for (c = 0; c < children; c++) {
1796 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1798 (void) printf("\t %s\n", vname);
1803 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1804 &child, &children) == 0) {
1805 (void) printf(gettext("\tspares\n"));
1806 for (c = 0; c < children; c++) {
1807 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1809 (void) printf("\t %s\n", vname);
1817 * Logs are recorded as top level vdevs in the main pool child array
1818 * but with "is_log" set to 1. We use either print_status_config() or
1819 * print_import_config() to print the top level logs then any log
1820 * children (eg mirrored slogs) are printed recursively - which
1821 * works because only the top level vdev is marked "is_log"
1824 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1829 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1833 (void) printf(gettext("\tlogs\n"));
1835 for (c = 0; c < children; c++) {
1836 uint64_t is_log = B_FALSE;
1839 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1843 name = zpool_vdev_name(g_zfs, zhp, child[c],
1844 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1845 if (cb->cb_print_status)
1846 print_status_config(zhp, cb, name, child[c], 2,
1849 print_import_config(cb, name, child[c], 2);
1855 * Display the status for the given pool.
1858 show_import(nvlist_t *config)
1860 uint64_t pool_state;
1866 zpool_status_t reason;
1867 zpool_errata_t errata;
1871 status_cbdata_t cb = { 0 };
1873 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1875 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1877 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1879 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1882 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1883 (uint64_t **)&vs, &vsc) == 0);
1884 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1886 reason = zpool_import_status(config, &msgid, &errata);
1888 (void) printf(gettext(" pool: %s\n"), name);
1889 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1890 (void) printf(gettext(" state: %s"), health);
1891 if (pool_state == POOL_STATE_DESTROYED)
1892 (void) printf(gettext(" (DESTROYED)"));
1893 (void) printf("\n");
1896 case ZPOOL_STATUS_MISSING_DEV_R:
1897 case ZPOOL_STATUS_MISSING_DEV_NR:
1898 case ZPOOL_STATUS_BAD_GUID_SUM:
1899 (void) printf(gettext(" status: One or more devices are "
1900 "missing from the system.\n"));
1903 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1904 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1905 (void) printf(gettext(" status: One or more devices contains "
1906 "corrupted data.\n"));
1909 case ZPOOL_STATUS_CORRUPT_DATA:
1911 gettext(" status: The pool data is corrupted.\n"));
1914 case ZPOOL_STATUS_OFFLINE_DEV:
1915 (void) printf(gettext(" status: One or more devices "
1916 "are offlined.\n"));
1919 case ZPOOL_STATUS_CORRUPT_POOL:
1920 (void) printf(gettext(" status: The pool metadata is "
1924 case ZPOOL_STATUS_VERSION_OLDER:
1925 (void) printf(gettext(" status: The pool is formatted using a "
1926 "legacy on-disk version.\n"));
1929 case ZPOOL_STATUS_VERSION_NEWER:
1930 (void) printf(gettext(" status: The pool is formatted using an "
1931 "incompatible version.\n"));
1934 case ZPOOL_STATUS_FEAT_DISABLED:
1935 (void) printf(gettext(" status: Some supported features are "
1936 "not enabled on the pool.\n"));
1939 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1940 (void) printf(gettext("status: The pool uses the following "
1941 "feature(s) not supported on this system:\n"));
1942 zpool_print_unsup_feat(config);
1945 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1946 (void) printf(gettext("status: The pool can only be accessed "
1947 "in read-only mode on this system. It\n\tcannot be "
1948 "accessed in read-write mode because it uses the "
1949 "following\n\tfeature(s) not supported on this system:\n"));
1950 zpool_print_unsup_feat(config);
1953 case ZPOOL_STATUS_HOSTID_MISMATCH:
1954 (void) printf(gettext(" status: The pool was last accessed by "
1955 "another system.\n"));
1958 case ZPOOL_STATUS_FAULTED_DEV_R:
1959 case ZPOOL_STATUS_FAULTED_DEV_NR:
1960 (void) printf(gettext(" status: One or more devices are "
1964 case ZPOOL_STATUS_BAD_LOG:
1965 (void) printf(gettext(" status: An intent log record cannot be "
1969 case ZPOOL_STATUS_RESILVERING:
1970 (void) printf(gettext(" status: One or more devices were being "
1974 case ZPOOL_STATUS_ERRATA:
1975 (void) printf(gettext(" status: Errata #%d detected.\n"),
1981 * No other status can be seen when importing pools.
1983 assert(reason == ZPOOL_STATUS_OK);
1987 * Print out an action according to the overall state of the pool.
1989 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1990 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1991 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1992 (void) printf(gettext(" action: The pool can be "
1993 "imported using its name or numeric identifier, "
1994 "though\n\tsome features will not be available "
1995 "without an explicit 'zpool upgrade'.\n"));
1996 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1997 (void) printf(gettext(" action: The pool can be "
1998 "imported using its name or numeric "
1999 "identifier and\n\tthe '-f' flag.\n"));
2000 } else if (reason == ZPOOL_STATUS_ERRATA) {
2002 case ZPOOL_ERRATA_NONE:
2005 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2006 (void) printf(gettext(" action: The pool can "
2007 "be imported using its name or numeric "
2008 "identifier,\n\thowever there is a compat"
2009 "ibility issue which should be corrected"
2010 "\n\tby running 'zpool scrub'\n"));
2013 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2014 (void) printf(gettext(" action: The pool can"
2015 "not be imported with this version of ZFS "
2016 "due to\n\tan active asynchronous destroy. "
2017 "Revert to an earlier version\n\tand "
2018 "allow the destroy to complete before "
2024 * All errata must contain an action message.
2029 (void) printf(gettext(" action: The pool can be "
2030 "imported using its name or numeric "
2033 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2034 (void) printf(gettext(" action: The pool can be imported "
2035 "despite missing or damaged devices. The\n\tfault "
2036 "tolerance of the pool may be compromised if imported.\n"));
2039 case ZPOOL_STATUS_VERSION_NEWER:
2040 (void) printf(gettext(" action: The pool cannot be "
2041 "imported. Access the pool on a system running "
2042 "newer\n\tsoftware, or recreate the pool from "
2045 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2046 (void) printf(gettext("action: The pool cannot be "
2047 "imported. Access the pool on a system that "
2048 "supports\n\tthe required feature(s), or recreate "
2049 "the pool from backup.\n"));
2051 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2052 (void) printf(gettext("action: The pool cannot be "
2053 "imported in read-write mode. Import the pool "
2055 "\t\"-o readonly=on\", access the pool on a system "
2056 "that supports the\n\trequired feature(s), or "
2057 "recreate the pool from backup.\n"));
2059 case ZPOOL_STATUS_MISSING_DEV_R:
2060 case ZPOOL_STATUS_MISSING_DEV_NR:
2061 case ZPOOL_STATUS_BAD_GUID_SUM:
2062 (void) printf(gettext(" action: The pool cannot be "
2063 "imported. Attach the missing\n\tdevices and try "
2067 (void) printf(gettext(" action: The pool cannot be "
2068 "imported due to damaged devices or data.\n"));
2072 /* Print the comment attached to the pool. */
2073 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2074 (void) printf(gettext("comment: %s\n"), comment);
2077 * If the state is "closed" or "can't open", and the aux state
2078 * is "corrupt data":
2080 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2081 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2082 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2083 if (pool_state == POOL_STATE_DESTROYED)
2084 (void) printf(gettext("\tThe pool was destroyed, "
2085 "but can be imported using the '-Df' flags.\n"));
2086 else if (pool_state != POOL_STATE_EXPORTED)
2087 (void) printf(gettext("\tThe pool may be active on "
2088 "another system, but can be imported using\n\t"
2089 "the '-f' flag.\n"));
2093 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2096 (void) printf(gettext(" config:\n\n"));
2098 cb.cb_namewidth = max_width(NULL, nvroot, 0, 0, VDEV_NAME_TYPE_ID);
2099 if (cb.cb_namewidth < 10)
2100 cb.cb_namewidth = 10;
2102 print_import_config(&cb, name, nvroot, 0);
2103 if (num_logs(nvroot) > 0)
2104 print_logs(NULL, &cb, nvroot);
2106 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2107 (void) printf(gettext("\n\tAdditional devices are known to "
2108 "be part of this pool, though their\n\texact "
2109 "configuration cannot be determined.\n"));
2114 * Perform the import for the given configuration. This passes the heavy
2115 * lifting off to zpool_import_props(), and then mounts the datasets contained
2119 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2120 nvlist_t *props, int flags)
2122 zpool_handle_t *zhp;
2127 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2130 verify(nvlist_lookup_uint64(config,
2131 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2132 verify(nvlist_lookup_uint64(config,
2133 ZPOOL_CONFIG_VERSION, &version) == 0);
2134 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2135 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2136 "is formatted using an unsupported ZFS version\n"), name);
2138 } else if (state != POOL_STATE_EXPORTED &&
2139 !(flags & ZFS_IMPORT_ANY_HOST)) {
2140 uint64_t hostid = 0;
2141 unsigned long system_hostid = get_system_hostid();
2143 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
2146 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
2151 verify(nvlist_lookup_string(config,
2152 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2153 verify(nvlist_lookup_uint64(config,
2154 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
2156 (void) fprintf(stderr, gettext("cannot import "
2157 "'%s': pool may be in use from other "
2158 "system, it was last accessed by %s "
2159 "(hostid: 0x%lx) on %s"), name, hostname,
2160 (unsigned long)hostid,
2161 asctime(localtime(&t)));
2162 (void) fprintf(stderr, gettext("use '-f' to "
2163 "import anyway\n"));
2168 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2171 if (newname != NULL)
2172 name = (char *)newname;
2174 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2177 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2178 !(flags & ZFS_IMPORT_ONLY) &&
2179 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2189 * zpool import [-d dir] [-D]
2190 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2191 * [-d dir | -c cachefile] [-f] -a
2192 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2193 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2195 * -c Read pool information from a cachefile instead of searching
2198 * -d Scan in a specific directory, other than /dev/. More than
2199 * one directory can be specified using multiple '-d' options.
2201 * -D Scan for previously destroyed pools or import all or only
2202 * specified destroyed pools.
2204 * -R Temporarily import the pool, with all mountpoints relative to
2205 * the given root. The pool will remain exported when the machine
2208 * -V Import even in the presence of faulted vdevs. This is an
2209 * intentionally undocumented option for testing purposes, and
2210 * treats the pool configuration as complete, leaving any bad
2211 * vdevs in the FAULTED state. In other words, it does verbatim
2214 * -f Force import, even if it appears that the pool is active.
2216 * -F Attempt rewind if necessary.
2218 * -n See if rewind would work, but don't actually rewind.
2220 * -N Import the pool but don't mount datasets.
2222 * -T Specify a starting txg to use for import. This option is
2223 * intentionally undocumented option for testing purposes.
2225 * -a Import all pools found.
2227 * -o Set property=value and/or temporary mount options (without '=').
2229 * -s Scan using the default search path, the libblkid cache will
2232 * The import command scans for pools to import, and import pools based on pool
2233 * name and GUID. The pool can also be renamed as part of the import process.
2236 zpool_do_import(int argc, char **argv)
2238 char **searchdirs = NULL;
2239 char *env, *envdup = NULL;
2243 nvlist_t *pools = NULL;
2244 boolean_t do_all = B_FALSE;
2245 boolean_t do_destroyed = B_FALSE;
2246 char *mntopts = NULL;
2249 uint64_t searchguid = 0;
2250 char *searchname = NULL;
2252 nvlist_t *found_config;
2253 nvlist_t *policy = NULL;
2254 nvlist_t *props = NULL;
2256 int flags = ZFS_IMPORT_NORMAL;
2257 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2258 boolean_t dryrun = B_FALSE;
2259 boolean_t do_rewind = B_FALSE;
2260 boolean_t xtreme_rewind = B_FALSE;
2261 boolean_t do_scan = B_FALSE;
2262 uint64_t pool_state, txg = -1ULL;
2263 char *cachefile = NULL;
2264 importargs_t idata = { 0 };
2268 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2277 if (searchdirs == NULL) {
2278 searchdirs = safe_malloc(sizeof (char *));
2280 char **tmp = safe_malloc((nsearch + 1) *
2282 bcopy(searchdirs, tmp, nsearch *
2287 searchdirs[nsearch++] = optarg;
2290 do_destroyed = B_TRUE;
2293 flags |= ZFS_IMPORT_ANY_HOST;
2299 flags |= ZFS_IMPORT_MISSING_LOG;
2305 flags |= ZFS_IMPORT_ONLY;
2308 if ((propval = strchr(optarg, '=')) != NULL) {
2311 if (add_prop_list(optarg, propval,
2319 if (add_prop_list(zpool_prop_to_name(
2320 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2322 if (add_prop_list_default(zpool_prop_to_name(
2323 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2330 flags |= ZFS_IMPORT_TEMP_NAME;
2331 if (add_prop_list_default(zpool_prop_to_name(
2332 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2338 txg = strtoull(optarg, &endptr, 0);
2339 if (errno != 0 || *endptr != '\0') {
2340 (void) fprintf(stderr,
2341 gettext("invalid txg value\n"));
2344 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2347 flags |= ZFS_IMPORT_VERBATIM;
2350 xtreme_rewind = B_TRUE;
2353 (void) fprintf(stderr, gettext("missing argument for "
2354 "'%c' option\n"), optopt);
2358 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2367 if (cachefile && nsearch != 0) {
2368 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2372 if ((dryrun || xtreme_rewind) && !do_rewind) {
2373 (void) fprintf(stderr,
2374 gettext("-n or -X only meaningful with -F\n"));
2378 rewind_policy = ZPOOL_TRY_REWIND;
2380 rewind_policy = ZPOOL_DO_REWIND;
2382 rewind_policy |= ZPOOL_EXTREME_REWIND;
2384 /* In the future, we can capture further policy and include it here */
2385 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2386 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2387 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2390 /* check argument count */
2393 (void) fprintf(stderr, gettext("too many arguments\n"));
2398 (void) fprintf(stderr, gettext("too many arguments\n"));
2404 * Check for the effective uid. We do this explicitly here because
2405 * otherwise any attempt to discover pools will silently fail.
2407 if (argc == 0 && geteuid() != 0) {
2408 (void) fprintf(stderr, gettext("cannot "
2409 "discover pools: permission denied\n"));
2410 if (searchdirs != NULL)
2414 nvlist_free(policy);
2419 * Depending on the arguments given, we do one of the following:
2421 * <none> Iterate through all pools and display information about
2424 * -a Iterate through all pools and try to import each one.
2426 * <id> Find the pool that corresponds to the given GUID/pool
2427 * name and import that one.
2429 * -D Above options applies only to destroyed pools.
2435 searchguid = strtoull(argv[0], &endptr, 10);
2436 if (errno != 0 || *endptr != '\0') {
2437 searchname = argv[0];
2440 found_config = NULL;
2443 * User specified a name or guid. Ensure it's unique.
2445 idata.unique = B_TRUE;
2449 * Check the environment for the preferred search path.
2451 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2454 envdup = strdup(env);
2456 dir = strtok(envdup, ":");
2457 while (dir != NULL) {
2458 if (searchdirs == NULL) {
2459 searchdirs = safe_malloc(sizeof (char *));
2461 char **tmp = safe_malloc((nsearch + 1) *
2463 bcopy(searchdirs, tmp, nsearch *
2468 searchdirs[nsearch++] = dir;
2469 dir = strtok(NULL, ":");
2473 idata.path = searchdirs;
2474 idata.paths = nsearch;
2475 idata.poolname = searchname;
2476 idata.guid = searchguid;
2477 idata.cachefile = cachefile;
2478 idata.scan = do_scan;
2481 * Under Linux the zpool_find_import_impl() function leverages the
2482 * taskq implementation to parallelize device scanning. It is
2483 * therefore necessary to initialize this functionality for the
2484 * duration of the zpool_search_import() function.
2487 pools = zpool_search_import(g_zfs, &idata);
2490 if (pools != NULL && idata.exists &&
2491 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2492 (void) fprintf(stderr, gettext("cannot import '%s': "
2493 "a pool with that name already exists\n"),
2495 (void) fprintf(stderr, gettext("use the form '%s "
2496 "<pool | id> <newpool>' to give it a new name\n"),
2499 } else if (pools == NULL && idata.exists) {
2500 (void) fprintf(stderr, gettext("cannot import '%s': "
2501 "a pool with that name is already created/imported,\n"),
2503 (void) fprintf(stderr, gettext("and no additional pools "
2504 "with that name were found\n"));
2506 } else if (pools == NULL) {
2508 (void) fprintf(stderr, gettext("cannot import '%s': "
2509 "no such pool available\n"), argv[0]);
2515 if (searchdirs != NULL)
2519 nvlist_free(policy);
2526 * At this point we have a list of import candidate configs. Even if
2527 * we were searching by pool name or guid, we still need to
2528 * post-process the list to deal with pool state and possible
2534 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2536 verify(nvpair_value_nvlist(elem, &config) == 0);
2538 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2540 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2542 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2545 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2552 (void) printf("\n");
2555 err |= do_import(config, NULL, mntopts,
2558 show_import(config);
2560 } else if (searchname != NULL) {
2564 * We are searching for a pool based on name.
2566 verify(nvlist_lookup_string(config,
2567 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2569 if (strcmp(name, searchname) == 0) {
2570 if (found_config != NULL) {
2571 (void) fprintf(stderr, gettext(
2572 "cannot import '%s': more than "
2573 "one matching pool\n"), searchname);
2574 (void) fprintf(stderr, gettext(
2575 "import by numeric ID instead\n"));
2578 found_config = config;
2584 * Search for a pool by guid.
2586 verify(nvlist_lookup_uint64(config,
2587 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2589 if (guid == searchguid)
2590 found_config = config;
2595 * If we were searching for a specific pool, verify that we found a
2596 * pool, and then do the import.
2598 if (argc != 0 && err == 0) {
2599 if (found_config == NULL) {
2600 (void) fprintf(stderr, gettext("cannot import '%s': "
2601 "no such pool available\n"), argv[0]);
2604 err |= do_import(found_config, argc == 1 ? NULL :
2605 argv[1], mntopts, props, flags);
2610 * If we were just looking for pools, report an error if none were
2613 if (argc == 0 && first)
2614 (void) fprintf(stderr,
2615 gettext("no pools available to import\n"));
2620 nvlist_free(policy);
2621 if (searchdirs != NULL)
2626 return (err ? 1 : 0);
2629 typedef struct iostat_cbdata {
2634 char **cb_vdev_names; /* Only show these vdevs */
2635 unsigned int cb_vdev_names_count;
2636 boolean_t cb_verbose;
2637 boolean_t cb_literal;
2638 boolean_t cb_scripted;
2639 zpool_list_t *cb_list;
2640 vdev_cmd_data_list_t *vcdl;
2644 typedef struct name_and_columns {
2645 const char *name; /* Column name */
2646 unsigned int columns; /* Center name to this number of columns */
2647 } name_and_columns_t;
2649 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2651 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2653 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2655 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2656 {"asyncq_wait", 2}, {"scrub"}},
2657 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2658 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2660 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2661 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2662 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2663 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2667 /* Shorthand - if "columns" field not set, default to 1 column */
2668 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2670 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2672 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2673 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2674 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2675 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2676 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2677 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2678 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2679 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2682 static const char *histo_to_title[] = {
2683 [IOS_L_HISTO] = "latency",
2684 [IOS_RQ_HISTO] = "req_size",
2688 * Return the number of labels in a null-terminated name_and_columns_t
2693 label_array_len(const name_and_columns_t *labels)
2697 while (labels[i].name)
2704 * Return the number of strings in a null-terminated string array.
2707 * const char foo[] = {"bar", "baz", NULL}
2712 str_array_len(const char *array[])
2723 * Return a default column width for default/latency/queue columns. This does
2724 * not include histograms, which have their columns autosized.
2727 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2729 unsigned long column_width = 5; /* Normal niceprint */
2730 static unsigned long widths[] = {
2732 * Choose some sane default column sizes for printing the
2735 [IOS_DEFAULT] = 15, /* 1PB capacity */
2736 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2737 [IOS_QUEUES] = 6, /* 1M queue entries */
2741 column_width = widths[type];
2743 return (column_width);
2747 * Print the column labels, i.e:
2749 * capacity operations bandwidth
2750 * alloc free read write read write ...
2752 * If force_column_width is set, use it for the column width. If not set, use
2753 * the default column width.
2756 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2757 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2760 unsigned int text_start, rw_column_width, spaces_to_end;
2761 uint64_t flags = cb->cb_flags;
2763 unsigned int column_width = force_column_width;
2765 /* For each bit set in flags */
2766 for (f = flags; f; f &= ~(1ULL << idx)) {
2767 idx = lowbit64(f) - 1;
2768 if (!force_column_width)
2769 column_width = default_column_width(cb, idx);
2770 /* Print our top labels centered over "read write" label. */
2771 for (i = 0; i < label_array_len(labels[idx]); i++) {
2772 const char *name = labels[idx][i].name;
2774 * We treat labels[][].columns == 0 as shorthand
2775 * for one column. It makes writing out the label
2776 * tables more concise.
2778 unsigned int columns = MAX(1, labels[idx][i].columns);
2779 unsigned int slen = strlen(name);
2781 rw_column_width = (column_width * columns) +
2782 (2 * (columns - 1));
2784 text_start = (int)((rw_column_width)/columns -
2787 printf(" "); /* Two spaces between columns */
2789 /* Space from beginning of column to label */
2790 for (s = 0; s < text_start; s++)
2795 /* Print space after label to end of column */
2796 spaces_to_end = rw_column_width - text_start - slen;
2797 for (s = 0; s < spaces_to_end; s++)
2806 * Utility function to print out a line of dashes like:
2808 * -------------------------------- ----- ----- ----- ----- -----
2810 * ...or a dashed named-row line like:
2816 * @force_column_width If non-zero, use the value as the column width.
2817 * Otherwise use the default column widths.
2819 * @name: Print a dashed named-row line starting
2820 * with @name. Otherwise, print a regular
2824 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
2828 unsigned int namewidth;
2829 uint64_t flags = cb->cb_flags;
2832 const name_and_columns_t *labels;
2836 if (cb->cb_flags & IOS_ANYHISTO_M) {
2837 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2838 } else if (cb->cb_vdev_names_count) {
2844 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2845 name ? strlen(name) : 0);
2849 printf("%-*s", namewidth, name);
2851 for (i = 0; i < namewidth; i++)
2855 /* For each bit in flags */
2856 for (f = flags; f; f &= ~(1ULL << idx)) {
2857 unsigned int column_width;
2858 idx = lowbit64(f) - 1;
2859 if (force_column_width)
2860 column_width = force_column_width;
2862 column_width = default_column_width(cb, idx);
2864 labels = iostat_bottom_labels[idx];
2865 for (i = 0; i < label_array_len(labels); i++) {
2867 printf(" %*s-", column_width - 1, " ");
2869 printf(" %.*s", column_width,
2870 "--------------------");
2878 print_iostat_separator_impl(iostat_cbdata_t *cb,
2879 unsigned int force_column_width)
2881 print_iostat_dashes(cb, force_column_width, NULL);
2885 print_iostat_separator(iostat_cbdata_t *cb)
2887 print_iostat_separator_impl(cb, 0);
2891 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
2892 const char *histo_vdev_name)
2894 unsigned int namewidth;
2897 if (cb->cb_flags & IOS_ANYHISTO_M) {
2898 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2899 } else if (cb->cb_vdev_names_count) {
2905 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
2906 histo_vdev_name ? strlen(histo_vdev_name) : 0);
2908 if (histo_vdev_name)
2909 printf("%-*s", namewidth, histo_vdev_name);
2911 printf("%*s", namewidth, "");
2914 print_iostat_labels(cb, force_column_width, iostat_top_labels);
2916 printf("%-*s", namewidth, title);
2918 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
2920 print_iostat_separator_impl(cb, force_column_width);
2924 print_iostat_header(iostat_cbdata_t *cb)
2926 print_iostat_header_impl(cb, 0, NULL);
2931 * Display a single statistic.
2934 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
2935 unsigned int column_size, boolean_t scripted)
2939 zfs_nicenum_format(value, buf, sizeof (buf), format);
2942 printf("\t%s", buf);
2944 printf(" %*s", column_size, buf);
2948 * Calculate the default vdev stats
2950 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
2951 * stats into calcvs.
2954 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
2955 vdev_stat_t *calcvs)
2959 memcpy(calcvs, newvs, sizeof (*calcvs));
2960 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
2961 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
2963 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
2964 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
2968 * Internal representation of the extended iostats data.
2970 * The extended iostat stats are exported in nvlists as either uint64_t arrays
2971 * or single uint64_t's. We make both look like arrays to make them easier
2972 * to process. In order to make single uint64_t's look like arrays, we set
2973 * __data to the stat data, and then set *data = &__data with count = 1. Then,
2974 * we can just use *data and count.
2978 uint_t count; /* Number of entries in data[] */
2979 uint64_t __data; /* Only used when data is a single uint64_t */
2983 stat_histo_max(struct stat_array *nva, unsigned int len)
2987 for (i = 0; i < len; i++)
2988 max = MAX(max, array64_max(nva[i].data, nva[i].count));
2994 * Helper function to lookup a uint64_t array or uint64_t value and store its
2995 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
2996 * it look like a one element array to make it easier to process.
2999 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3000 struct stat_array *nva)
3005 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3006 switch (nvpair_type(tmp)) {
3007 case DATA_TYPE_UINT64_ARRAY:
3008 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3010 case DATA_TYPE_UINT64:
3011 ret = nvpair_value_uint64(tmp, &nva->__data);
3012 nva->data = &nva->__data;
3016 /* Not a uint64_t */
3025 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3026 * subtract them, and return the results in a newly allocated stat_array.
3027 * You must free the returned array after you are done with it with
3028 * free_calc_stats().
3030 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3033 static struct stat_array *
3034 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3037 nvlist_t *oldnvx = NULL, *newnvx;
3038 struct stat_array *oldnva, *newnva, *calcnva;
3040 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3042 /* Extract our extended stats nvlist from the main list */
3043 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3046 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3050 newnva = safe_malloc(alloc_size);
3051 oldnva = safe_malloc(alloc_size);
3052 calcnva = safe_malloc(alloc_size);
3054 for (j = 0; j < len; j++) {
3055 verify(nvpair64_to_stat_array(newnvx, names[j],
3057 calcnva[j].count = newnva[j].count;
3058 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3059 calcnva[j].data = safe_malloc(alloc_size);
3060 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3063 verify(nvpair64_to_stat_array(oldnvx, names[j],
3065 for (i = 0; i < oldnva[j].count; i++)
3066 calcnva[j].data[i] -= oldnva[j].data[i];
3075 free_calc_stats(struct stat_array *nva, unsigned int len)
3078 for (i = 0; i < len; i++)
3085 print_iostat_histo(struct stat_array *nva, unsigned int len,
3086 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3092 enum zfs_nicenum_format format;
3093 unsigned int buckets;
3094 unsigned int start_bucket;
3097 format = ZFS_NICENUM_RAW;
3099 format = ZFS_NICENUM_1024;
3101 /* All these histos are the same size, so just use nva[0].count */
3102 buckets = nva[0].count;
3104 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3105 /* Start at 512 - req size should never be lower than this */
3111 for (j = start_bucket; j < buckets; j++) {
3112 /* Print histogram bucket label */
3113 if (cb->cb_flags & IOS_L_HISTO_M) {
3114 /* Ending range of this bucket */
3115 val = (1UL << (j + 1)) - 1;
3116 zfs_nicetime(val, buf, sizeof (buf));
3118 /* Request size (starting range of bucket) */
3120 zfs_nicenum(val, buf, sizeof (buf));
3123 if (cb->cb_scripted)
3124 printf("%llu", (u_longlong_t)val);
3126 printf("%-*s", namewidth, buf);
3128 /* Print the values on the line */
3129 for (i = 0; i < len; i++) {
3130 print_one_stat(nva[i].data[j] * scale, format,
3131 column_width, cb->cb_scripted);
3138 print_solid_separator(unsigned int length)
3146 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3147 nvlist_t *newnv, double scale, const char *name)
3149 unsigned int column_width;
3150 unsigned int namewidth;
3151 unsigned int entire_width;
3152 enum iostat_type type;
3153 struct stat_array *nva;
3155 unsigned int names_len;
3157 /* What type of histo are we? */
3158 type = IOS_HISTO_IDX(cb->cb_flags);
3160 /* Get NULL-terminated array of nvlist names for our histo */
3161 names = vsx_type_to_nvlist[type];
3162 names_len = str_array_len(names); /* num of names */
3164 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3166 if (cb->cb_literal) {
3167 column_width = MAX(5,
3168 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3173 namewidth = MAX(cb->cb_namewidth,
3174 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3177 * Calculate the entire line width of what we're printing. The
3178 * +2 is for the two spaces between columns:
3182 /* |___| <---------- column_width */
3184 /* |__________| <--- entire_width */
3186 entire_width = namewidth + (column_width + 2) *
3187 label_array_len(iostat_bottom_labels[type]);
3189 if (cb->cb_scripted)
3190 printf("%s\n", name);
3192 print_iostat_header_impl(cb, column_width, name);
3194 print_iostat_histo(nva, names_len, cb, column_width,
3197 free_calc_stats(nva, names_len);
3198 if (!cb->cb_scripted)
3199 print_solid_separator(entire_width);
3203 * Calculate the average latency of a power-of-two latency histogram
3206 single_histo_average(uint64_t *histo, unsigned int buckets)
3209 uint64_t count = 0, total = 0;
3211 for (i = 0; i < buckets; i++) {
3213 * Our buckets are power-of-two latency ranges. Use the
3214 * midpoint latency of each bucket to calculate the average.
3222 if (histo[i] != 0) {
3223 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3228 /* Prevent divide by zero */
3229 return (count == 0 ? 0 : total / count);
3233 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3234 nvlist_t *newnv, double scale)
3238 const char *names[] = {
3239 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3240 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3241 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3242 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3243 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3244 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3245 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3246 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3247 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3248 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3251 struct stat_array *nva;
3253 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3254 enum zfs_nicenum_format format;
3256 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3259 format = ZFS_NICENUM_RAW;
3261 format = ZFS_NICENUM_1024;
3263 for (i = 0; i < ARRAY_SIZE(names); i++) {
3264 val = nva[i].data[0] * scale;
3265 print_one_stat(val, format, column_width, cb->cb_scripted);
3268 free_calc_stats(nva, ARRAY_SIZE(names));
3272 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3273 nvlist_t *newnv, double scale)
3277 const char *names[] = {
3278 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3279 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3280 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3281 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3282 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3283 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3284 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3285 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3286 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3288 struct stat_array *nva;
3290 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3291 enum zfs_nicenum_format format;
3293 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3296 format = ZFS_NICENUM_RAW;
3298 format = ZFS_NICENUM_TIME;
3300 /* Print our avg latencies on the line */
3301 for (i = 0; i < ARRAY_SIZE(names); i++) {
3302 /* Compute average latency for a latency histo */
3303 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3304 print_one_stat(val, format, column_width, cb->cb_scripted);
3306 free_calc_stats(nva, ARRAY_SIZE(names));
3310 * Print default statistics (capacity/operations/bandwidth)
3313 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3315 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3316 enum zfs_nicenum_format format;
3317 char na; /* char to print for "not applicable" values */
3319 if (cb->cb_literal) {
3320 format = ZFS_NICENUM_RAW;
3323 format = ZFS_NICENUM_1024;
3327 /* only toplevel vdevs have capacity stats */
3328 if (vs->vs_space == 0) {
3329 if (cb->cb_scripted)
3330 printf("\t%c\t%c", na, na);
3332 printf(" %*c %*c", column_width, na, column_width,
3335 print_one_stat(vs->vs_alloc, format, column_width,
3337 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3338 column_width, cb->cb_scripted);
3341 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3342 format, column_width, cb->cb_scripted);
3343 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3344 format, column_width, cb->cb_scripted);
3345 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3346 format, column_width, cb->cb_scripted);
3347 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3348 format, column_width, cb->cb_scripted);
3352 * Print out all the statistics for the given vdev. This can either be the
3353 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3354 * is a verbose output, and we don't want to display the toplevel pool stats.
3356 * Returns the number of stat lines printed.
3359 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3360 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3362 nvlist_t **oldchild, **newchild;
3364 vdev_stat_t *oldvs, *newvs, *calcvs;
3365 vdev_stat_t zerovs = { 0 };
3372 calcvs = safe_malloc(sizeof (*calcvs));
3374 if (oldnv != NULL) {
3375 verify(nvlist_lookup_uint64_array(oldnv,
3376 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3381 /* Do we only want to see a specific vdev? */
3382 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3383 /* Yes we do. Is this the vdev? */
3384 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3386 * This is our vdev. Since it is the only vdev we
3387 * will be displaying, make depth = 0 so that it
3388 * doesn't get indented.
3395 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3396 /* Couldn't match the name */
3401 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3402 (uint64_t **)&newvs, &c) == 0);
3405 * Print the vdev name unless it's is a histogram. Histograms
3406 * display the vdev name in the header itself.
3408 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3409 if (cb->cb_scripted) {
3412 if (strlen(name) + depth > cb->cb_namewidth)
3413 (void) printf("%*s%s", depth, "", name);
3415 (void) printf("%*s%s%*s", depth, "", name,
3416 (int)(cb->cb_namewidth - strlen(name) -
3421 /* Calculate our scaling factor */
3422 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3423 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3425 * If we specify printing histograms with no time interval, then
3426 * print the histogram numbers over the entire lifetime of the
3434 scale = (double)NANOSEC / tdelta;
3437 if (cb->cb_flags & IOS_DEFAULT_M) {
3438 calc_default_iostats(oldvs, newvs, calcvs);
3439 print_iostat_default(calcvs, cb, scale);
3441 if (cb->cb_flags & IOS_LATENCY_M)
3442 print_iostat_latency(cb, oldnv, newnv, scale);
3443 if (cb->cb_flags & IOS_QUEUES_M)
3444 print_iostat_queues(cb, oldnv, newnv, scale);
3445 if (cb->cb_flags & IOS_ANYHISTO_M) {
3447 print_iostat_histos(cb, oldnv, newnv, scale, name);
3450 if (cb->vcdl != NULL) {
3452 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3454 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3456 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3457 if (cb->cb_flags & IOS_ANYHISTO_M)
3462 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3471 if (!cb->cb_verbose)
3474 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3475 &newchild, &children) != 0)
3478 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3479 &oldchild, &c) != 0)
3482 for (c = 0; c < children; c++) {
3483 uint64_t ishole = B_FALSE, islog = B_FALSE;
3485 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3488 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3491 if (ishole || islog)
3494 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3496 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3497 newchild[c], cb, depth + 2);
3502 * Log device section
3505 if (num_logs(newnv) > 0) {
3506 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3507 !cb->cb_vdev_names) {
3508 print_iostat_dashes(cb, 0, "logs");
3511 for (c = 0; c < children; c++) {
3512 uint64_t islog = B_FALSE;
3513 (void) nvlist_lookup_uint64(newchild[c],
3514 ZPOOL_CONFIG_IS_LOG, &islog);
3517 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3519 ret += print_vdev_stats(zhp, vname, oldnv ?
3520 oldchild[c] : NULL, newchild[c],
3529 * Include level 2 ARC devices in iostat output
3531 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3532 &newchild, &children) != 0)
3535 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3536 &oldchild, &c) != 0)
3540 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3541 !cb->cb_vdev_names) {
3542 print_iostat_dashes(cb, 0, "cache");
3545 for (c = 0; c < children; c++) {
3546 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3548 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3549 : NULL, newchild[c], cb, depth + 2);
3558 refresh_iostat(zpool_handle_t *zhp, void *data)
3560 iostat_cbdata_t *cb = data;
3564 * If the pool has disappeared, remove it from the list and continue.
3566 if (zpool_refresh_stats(zhp, &missing) != 0)
3570 pool_list_remove(cb->cb_list, zhp);
3576 * Callback to print out the iostats for the given pool.
3579 print_iostat(zpool_handle_t *zhp, void *data)
3581 iostat_cbdata_t *cb = data;
3582 nvlist_t *oldconfig, *newconfig;
3583 nvlist_t *oldnvroot, *newnvroot;
3586 newconfig = zpool_get_config(zhp, &oldconfig);
3588 if (cb->cb_iteration == 1)
3591 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3594 if (oldconfig == NULL)
3597 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3600 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3602 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3603 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3604 print_iostat_separator(cb);
3617 if (isatty(STDOUT_FILENO)) {
3618 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3620 columns = ws.ws_col;
3629 get_namewidth(zpool_handle_t *zhp, void *data)
3631 iostat_cbdata_t *cb = data;
3632 nvlist_t *config, *nvroot;
3635 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3636 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3638 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3639 if (!cb->cb_verbose)
3640 cb->cb_namewidth = poolname_len;
3642 cb->cb_namewidth = MAX(poolname_len,
3643 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3644 cb->cb_name_flags));
3647 * The width must be at least 10, but may be as large as the
3648 * column width - 42 so that we can still fit in one line.
3650 columns = get_columns();
3652 if (cb->cb_namewidth < 10)
3653 cb->cb_namewidth = 10;
3654 if (cb->cb_namewidth > columns - 42)
3655 cb->cb_namewidth = columns - 42;
3661 * Parse the input string, get the 'interval' and 'count' value if there is one.
3664 get_interval_count(int *argcp, char **argv, float *iv,
3668 unsigned long count = 0;
3672 * Determine if the last argument is an integer or a pool name
3674 if (argc > 0 && isnumber(argv[argc - 1])) {
3678 interval = strtof(argv[argc - 1], &end);
3680 if (*end == '\0' && errno == 0) {
3681 if (interval == 0) {
3682 (void) fprintf(stderr, gettext("interval "
3683 "cannot be zero\n"));
3687 * Ignore the last parameter
3692 * If this is not a valid number, just plow on. The
3693 * user will get a more informative error message later
3701 * If the last argument is also an integer, then we have both a count
3704 if (argc > 0 && isnumber(argv[argc - 1])) {
3709 interval = strtof(argv[argc - 1], &end);
3711 if (*end == '\0' && errno == 0) {
3712 if (interval == 0) {
3713 (void) fprintf(stderr, gettext("interval "
3714 "cannot be zero\n"));
3719 * Ignore the last parameter
3733 get_timestamp_arg(char c)
3736 timestamp_fmt = UDATE;
3738 timestamp_fmt = DDATE;
3744 * Return stat flags that are supported by all pools by both the module and
3745 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3746 * It will get ANDed down until only the flags that are supported on all pools
3750 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
3752 uint64_t *mask = data;
3753 nvlist_t *config, *nvroot, *nvx;
3757 config = zpool_get_config(zhp, NULL);
3758 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3761 /* Default stats are always supported, but for completeness.. */
3762 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
3763 flags |= IOS_DEFAULT_M;
3765 /* Get our extended stats nvlist from the main list */
3766 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
3769 * No extended stats; they're probably running an older
3770 * module. No big deal, we support that too.
3775 /* For each extended stat, make sure all its nvpairs are supported */
3776 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
3777 if (!vsx_type_to_nvlist[j][0])
3780 /* Start off by assuming the flag is supported, then check */
3781 flags |= (1ULL << j);
3782 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
3783 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
3784 /* flag isn't supported */
3785 flags = flags & ~(1ULL << j);
3791 *mask = *mask & flags;
3796 * Return a bitmask of stats that are supported on all pools by both the module
3800 get_stat_flags(zpool_list_t *list)
3805 * get_stat_flags_cb() will lop off bits from "mask" until only the
3806 * flags that are supported on all pools remain.
3808 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
3813 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3816 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
3818 iostat_cbdata_t *cb = cb_data;
3822 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
3824 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
3825 ret = 1; /* match */
3832 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3835 is_vdev(zpool_handle_t *zhp, void *cb_data)
3837 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
3841 * Check if vdevs are in a pool
3843 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3844 * return 0. If pool_name is NULL, then search all pools.
3847 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
3848 iostat_cbdata_t *cb)
3855 if ((argc == 0) || !*argv)
3861 /* Temporarily hijack cb_vdev_names for a second... */
3862 tmp_name = cb->cb_vdev_names;
3864 /* Go though our list of prospective vdev names */
3865 for (i = 0; i < argc; i++) {
3866 cb->cb_vdev_names = argv + i;
3868 /* Is this name a vdev in our pools? */
3869 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
3877 cb->cb_vdev_names = tmp_name;
3883 is_pool_cb(zpool_handle_t *zhp, void *data)
3886 if (strcmp(name, zpool_get_name(zhp)) == 0)
3893 * Do we have a pool named *name? If so, return 1, otherwise 0.
3898 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
3901 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
3903 are_all_pools(int argc, char **argv)
3905 if ((argc == 0) || !*argv)
3909 if (!is_pool(argv[argc]))
3916 * Helper function to print out vdev/pool names we can't resolve. Used for an
3920 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
3921 iostat_cbdata_t *cb)
3926 for (i = 0; i < argc; i++) {
3930 str = gettext("pool");
3931 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
3932 str = gettext("vdev in this pool");
3933 else if (are_vdevs_in_pool(1, &name, NULL, cb))
3934 str = gettext("vdev in another pool");
3936 str = gettext("unknown");
3938 fprintf(stderr, "\t%s (%s)\n", name, str);
3943 * Same as get_interval_count(), but with additional checks to not misinterpret
3944 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
3948 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
3949 unsigned long *count, iostat_cbdata_t *cb)
3951 char **tmpargv = argv;
3952 int argc_for_interval = 0;
3954 /* Is the last arg an interval value? Or a guid? */
3955 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
3957 * The last arg is not a guid, so it's probably an
3960 argc_for_interval++;
3963 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
3965 * The 2nd to last arg is not a guid, so it's probably
3966 * an interval value.
3968 argc_for_interval++;
3972 /* Point to our list of possible intervals */
3973 tmpargv = &argv[*argc - argc_for_interval];
3975 *argc = *argc - argc_for_interval;
3976 get_interval_count(&argc_for_interval, tmpargv,
3981 * Floating point sleep(). Allows you to pass in a floating point value for
3987 struct timespec req;
3988 req.tv_sec = floor(sec);
3989 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
3990 nanosleep(&req, NULL);
3995 * zpool iostat [-c CMD] [-ghHLpPvy] [[-lq]|[-r|-w]] [-n name] [-T d|u]
3996 * [[ pool ...]|[pool vdev ...]|[vdev ...]]
3997 * [interval [count]]
3999 * -c CMD For each vdev, run command CMD
4000 * -g Display guid for individual vdev name.
4001 * -L Follow links when resolving vdev path name.
4002 * -P Display full path for vdev name.
4003 * -v Display statistics for individual vdevs
4005 * -p Display values in parsable (exact) format.
4006 * -H Scripted mode. Don't display headers, and separate properties
4008 * -l Display average latency
4009 * -q Display queue depths
4010 * -w Display latency histograms
4011 * -r Display request size histogram
4012 * -T Display a timestamp in date(1) or Unix format
4014 * This command can be tricky because we want to be able to deal with pool
4015 * creation/destruction as well as vdev configuration changes. The bulk of this
4016 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4017 * on pool_list_update() to detect the addition of new pools. Configuration
4018 * changes are all handled within libzfs.
4021 zpool_do_iostat(int argc, char **argv)
4027 unsigned long count = 0;
4029 boolean_t verbose = B_FALSE;
4030 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4031 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4032 boolean_t omit_since_boot = B_FALSE;
4033 boolean_t guid = B_FALSE;
4034 boolean_t follow_links = B_FALSE;
4035 boolean_t full_name = B_FALSE;
4036 iostat_cbdata_t cb = { 0 };
4039 /* Used for printing error message */
4040 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4041 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4043 uint64_t unsupported_flags;
4046 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4055 follow_links = B_TRUE;
4061 get_timestamp_arg(*optarg);
4085 omit_since_boot = B_TRUE;
4091 if (optopt == 'c') {
4093 gettext("Missing CMD for -c\n"));
4096 gettext("invalid option '%c'\n"), optopt);
4105 cb.cb_literal = parsable;
4106 cb.cb_scripted = scripted;
4109 cb.cb_name_flags |= VDEV_NAME_GUID;
4111 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4113 cb.cb_name_flags |= VDEV_NAME_PATH;
4114 cb.cb_iteration = 0;
4115 cb.cb_namewidth = 0;
4116 cb.cb_verbose = verbose;
4118 /* Get our interval and count values (if any) */
4120 get_interval_count_filter_guids(&argc, argv, &interval,
4123 get_interval_count(&argc, argv, &interval, &count);
4127 /* No args, so just print the defaults. */
4128 } else if (are_all_pools(argc, argv)) {
4129 /* All the args are pool names */
4130 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4131 /* All the args are vdevs */
4132 cb.cb_vdev_names = argv;
4133 cb.cb_vdev_names_count = argc;
4134 argc = 0; /* No pools to process */
4135 } else if (are_all_pools(1, argv)) {
4136 /* The first arg is a pool name */
4137 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4138 /* ...and the rest are vdev names */
4139 cb.cb_vdev_names = argv + 1;
4140 cb.cb_vdev_names_count = argc - 1;
4141 argc = 1; /* One pool to process */
4143 fprintf(stderr, gettext("Expected either a list of "));
4144 fprintf(stderr, gettext("pools, or list of vdevs in"));
4145 fprintf(stderr, " \"%s\", ", argv[0]);
4146 fprintf(stderr, gettext("but got:\n"));
4147 error_list_unresolved_vdevs(argc - 1, argv + 1,
4149 fprintf(stderr, "\n");
4155 * The args don't make sense. The first arg isn't a pool name,
4156 * nor are all the args vdevs.
4158 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4159 fprintf(stderr, "\n");
4163 if (cb.cb_vdev_names_count != 0) {
4165 * If user specified vdevs, it implies verbose.
4167 cb.cb_verbose = B_TRUE;
4171 * Construct the list of all interesting pools.
4174 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4177 if (pool_list_count(list) == 0 && argc != 0) {
4178 pool_list_free(list);
4182 if (pool_list_count(list) == 0 && interval == 0) {
4183 pool_list_free(list);
4184 (void) fprintf(stderr, gettext("no pools available\n"));
4188 if ((l_histo || rq_histo) && (queues || latency)) {
4189 pool_list_free(list);
4190 (void) fprintf(stderr,
4191 gettext("[-r|-w] isn't allowed with [-q|-l]\n"));
4196 if (l_histo && rq_histo) {
4197 pool_list_free(list);
4198 (void) fprintf(stderr,
4199 gettext("Only one of [-r|-w] can be passed at a time\n"));
4205 * Enter the main iostat loop.
4211 * Histograms tables look out of place when you try to display
4212 * them with the other stats, so make a rule that you can only
4213 * print histograms by themselves.
4215 cb.cb_flags = IOS_L_HISTO_M;
4216 } else if (rq_histo) {
4217 cb.cb_flags = IOS_RQ_HISTO_M;
4219 cb.cb_flags = IOS_DEFAULT_M;
4221 cb.cb_flags |= IOS_LATENCY_M;
4223 cb.cb_flags |= IOS_QUEUES_M;
4227 * See if the module supports all the stats we want to display.
4229 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4230 if (unsupported_flags) {
4234 gettext("The loaded zfs module doesn't support:"));
4236 /* for each bit set in unsupported_flags */
4237 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4238 idx = lowbit64(f) - 1;
4239 fprintf(stderr, " -%c", flag_to_arg[idx]);
4242 fprintf(stderr, ". Try running a newer module.\n");
4243 pool_list_free(list);
4249 if ((npools = pool_list_count(list)) == 0)
4250 (void) fprintf(stderr, gettext("no pools available\n"));
4253 * If this is the first iteration and -y was supplied
4254 * we skip any printing.
4256 boolean_t skip = (omit_since_boot &&
4257 cb.cb_iteration == 0);
4260 * Refresh all statistics. This is done as an
4261 * explicit step before calculating the maximum name
4262 * width, so that any * configuration changes are
4263 * properly accounted for.
4265 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4269 * Iterate over all pools to determine the maximum width
4270 * for the pool / device name column across all pools.
4272 cb.cb_namewidth = 0;
4273 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4276 if (timestamp_fmt != NODATE)
4277 print_timestamp(timestamp_fmt);
4280 * If it's the first time and we're not skipping it,
4281 * or either skip or verbose mode, print the header.
4283 * The histogram code explicitly prints its header on
4284 * every vdev, so skip this for histograms.
4286 if (((++cb.cb_iteration == 1 && !skip) ||
4287 (skip != verbose)) &&
4288 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4290 print_iostat_header(&cb);
4293 (void) fsleep(interval);
4297 if (cmd != NULL && cb.cb_verbose)
4298 cb.vcdl = all_pools_for_each_vdev_run(argc,
4299 argv, cmd, g_zfs, cb.cb_vdev_names,
4300 cb.cb_vdev_names_count, cb.cb_name_flags);
4302 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4304 if (cb.vcdl != NULL)
4305 free_vdev_cmd_data_list(cb.vcdl);
4308 * If there's more than one pool, and we're not in
4309 * verbose mode (which prints a separator for us),
4310 * then print a separator.
4312 * In addition, if we're printing specific vdevs then
4313 * we also want an ending separator.
4315 if (((npools > 1 && !verbose &&
4316 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4317 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4318 cb.cb_vdev_names_count)) &&
4320 print_iostat_separator(&cb);
4325 * Flush the output so that redirection to a file isn't buffered
4328 (void) fflush(stdout);
4333 if (count != 0 && --count == 0)
4336 (void) fsleep(interval);
4339 pool_list_free(list);
4344 typedef struct list_cbdata {
4345 boolean_t cb_verbose;
4348 boolean_t cb_scripted;
4349 zprop_list_t *cb_proplist;
4350 boolean_t cb_literal;
4354 * Given a list of columns to display, output appropriate headers for each one.
4357 print_header(list_cbdata_t *cb)
4359 zprop_list_t *pl = cb->cb_proplist;
4360 char headerbuf[ZPOOL_MAXPROPLEN];
4362 boolean_t first = B_TRUE;
4363 boolean_t right_justify;
4366 for (; pl != NULL; pl = pl->pl_next) {
4367 width = pl->pl_width;
4368 if (first && cb->cb_verbose) {
4370 * Reset the width to accommodate the verbose listing
4373 width = cb->cb_namewidth;
4381 right_justify = B_FALSE;
4382 if (pl->pl_prop != ZPROP_INVAL) {
4383 header = zpool_prop_column_name(pl->pl_prop);
4384 right_justify = zpool_prop_align_right(pl->pl_prop);
4388 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4389 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4390 headerbuf[i] = '\0';
4394 if (pl->pl_next == NULL && !right_justify)
4395 (void) printf("%s", header);
4396 else if (right_justify)
4397 (void) printf("%*s", (int)width, header);
4399 (void) printf("%-*s", (int)width, header);
4402 (void) printf("\n");
4406 * Given a pool and a list of properties, print out all the properties according
4407 * to the described layout.
4410 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4412 zprop_list_t *pl = cb->cb_proplist;
4413 boolean_t first = B_TRUE;
4414 char property[ZPOOL_MAXPROPLEN];
4416 boolean_t right_justify;
4419 for (; pl != NULL; pl = pl->pl_next) {
4421 width = pl->pl_width;
4422 if (first && cb->cb_verbose) {
4424 * Reset the width to accommodate the verbose listing
4427 width = cb->cb_namewidth;
4431 if (cb->cb_scripted)
4432 (void) printf("\t");
4439 right_justify = B_FALSE;
4440 if (pl->pl_prop != ZPROP_INVAL) {
4441 if (zpool_get_prop(zhp, pl->pl_prop, property,
4442 sizeof (property), NULL, cb->cb_literal) != 0)
4447 right_justify = zpool_prop_align_right(pl->pl_prop);
4448 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4449 zpool_prop_unsupported(pl->pl_user_prop)) &&
4450 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4451 sizeof (property)) == 0) {
4459 * If this is being called in scripted mode, or if this is the
4460 * last column and it is left-justified, don't include a width
4463 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4464 (void) printf("%s", propstr);
4465 else if (right_justify)
4466 (void) printf("%*s", (int)width, propstr);
4468 (void) printf("%-*s", (int)width, propstr);
4471 (void) printf("\n");
4475 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4476 boolean_t valid, enum zfs_nicenum_format format)
4480 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4483 case ZPOOL_PROP_EXPANDSZ:
4485 (void) strlcpy(propval, "-", sizeof (propval));
4487 zfs_nicenum_format(value, propval, sizeof (propval),
4490 case ZPOOL_PROP_FRAGMENTATION:
4491 if (value == ZFS_FRAG_INVALID) {
4492 (void) strlcpy(propval, "-", sizeof (propval));
4493 } else if (format == ZFS_NICENUM_RAW) {
4494 (void) snprintf(propval, sizeof (propval), "%llu",
4495 (unsigned long long)value);
4497 (void) snprintf(propval, sizeof (propval), "%llu%%",
4498 (unsigned long long)value);
4501 case ZPOOL_PROP_CAPACITY:
4502 if (format == ZFS_NICENUM_RAW)
4503 (void) snprintf(propval, sizeof (propval), "%llu",
4504 (unsigned long long)value);
4506 (void) snprintf(propval, sizeof (propval), "%llu%%",
4507 (unsigned long long)value);
4510 zfs_nicenum_format(value, propval, sizeof (propval), format);
4514 (void) strlcpy(propval, "-", sizeof (propval));
4517 (void) printf("\t%s", propval);
4519 (void) printf(" %*s", (int)width, propval);
4523 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4524 list_cbdata_t *cb, int depth)
4530 boolean_t scripted = cb->cb_scripted;
4531 uint64_t islog = B_FALSE;
4532 boolean_t haslog = B_FALSE;
4533 char *dashes = "%-*s - - - - - -\n";
4535 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4536 (uint64_t **)&vs, &c) == 0);
4539 boolean_t toplevel = (vs->vs_space != 0);
4541 enum zfs_nicenum_format format;
4544 format = ZFS_NICENUM_RAW;
4546 format = ZFS_NICENUM_1024;
4549 (void) printf("\t%s", name);
4550 else if (strlen(name) + depth > cb->cb_namewidth)
4551 (void) printf("%*s%s", depth, "", name);
4553 (void) printf("%*s%s%*s", depth, "", name,
4554 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4557 * Print the properties for the individual vdevs. Some
4558 * properties are only applicable to toplevel vdevs. The
4559 * 'toplevel' boolean value is passed to the print_one_column()
4560 * to indicate that the value is valid.
4562 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4564 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4566 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4567 scripted, toplevel, format);
4568 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4570 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4571 vs->vs_fragmentation, scripted,
4572 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4574 cap = (vs->vs_space == 0) ? 0 :
4575 (vs->vs_alloc * 100 / vs->vs_space);
4576 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4578 (void) printf("\n");
4581 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4582 &child, &children) != 0)
4585 for (c = 0; c < children; c++) {
4586 uint64_t ishole = B_FALSE;
4588 if (nvlist_lookup_uint64(child[c],
4589 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4592 if (nvlist_lookup_uint64(child[c],
4593 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
4598 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4600 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4604 if (haslog == B_TRUE) {
4605 /* LINTED E_SEC_PRINTF_VAR_FMT */
4606 (void) printf(dashes, cb->cb_namewidth, "log");
4607 for (c = 0; c < children; c++) {
4608 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4609 &islog) != 0 || !islog)
4611 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4613 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4618 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
4619 &child, &children) == 0 && children > 0) {
4620 /* LINTED E_SEC_PRINTF_VAR_FMT */
4621 (void) printf(dashes, cb->cb_namewidth, "cache");
4622 for (c = 0; c < children; c++) {
4623 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4625 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4630 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
4631 &children) == 0 && children > 0) {
4632 /* LINTED E_SEC_PRINTF_VAR_FMT */
4633 (void) printf(dashes, cb->cb_namewidth, "spare");
4634 for (c = 0; c < children; c++) {
4635 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4637 print_list_stats(zhp, vname, child[c], cb, depth + 2);
4645 * Generic callback function to list a pool.
4648 list_callback(zpool_handle_t *zhp, void *data)
4650 list_cbdata_t *cbp = data;
4654 config = zpool_get_config(zhp, NULL);
4656 print_pool(zhp, cbp);
4657 if (!cbp->cb_verbose)
4660 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4662 print_list_stats(zhp, NULL, nvroot, cbp, 0);
4668 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4670 * -g Display guid for individual vdev name.
4671 * -H Scripted mode. Don't display headers, and separate properties
4673 * -L Follow links when resolving vdev path name.
4674 * -o List of properties to display. Defaults to
4675 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4676 * "dedupratio,health,altroot"
4677 * -p Display values in parsable (exact) format.
4678 * -P Display full path for vdev name.
4679 * -T Display a timestamp in date(1) or Unix format
4681 * List all pools in the system, whether or not they're healthy. Output space
4682 * statistics for each one, as well as health status summary.
4685 zpool_do_list(int argc, char **argv)
4689 list_cbdata_t cb = { 0 };
4690 static char default_props[] =
4691 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4692 "dedupratio,health,altroot";
4693 char *props = default_props;
4695 unsigned long count = 0;
4697 boolean_t first = B_TRUE;
4700 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
4703 cb.cb_name_flags |= VDEV_NAME_GUID;
4706 cb.cb_scripted = B_TRUE;
4709 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4715 cb.cb_name_flags |= VDEV_NAME_PATH;
4718 cb.cb_literal = B_TRUE;
4721 get_timestamp_arg(*optarg);
4724 cb.cb_verbose = B_TRUE;
4727 (void) fprintf(stderr, gettext("missing argument for "
4728 "'%c' option\n"), optopt);
4732 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4741 get_interval_count(&argc, argv, &interval, &count);
4743 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
4747 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
4751 if (pool_list_count(list) == 0)
4754 if (timestamp_fmt != NODATE)
4755 print_timestamp(timestamp_fmt);
4757 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
4761 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
4766 if (count != 0 && --count == 0)
4769 pool_list_free(list);
4770 (void) fsleep(interval);
4773 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
4774 (void) printf(gettext("no pools available\n"));
4778 pool_list_free(list);
4779 zprop_free_list(cb.cb_proplist);
4784 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
4786 boolean_t force = B_FALSE;
4789 char *poolname, *old_disk, *new_disk;
4790 zpool_handle_t *zhp;
4791 nvlist_t *props = NULL;
4796 while ((c = getopt(argc, argv, "fo:")) != -1) {
4802 if ((propval = strchr(optarg, '=')) == NULL) {
4803 (void) fprintf(stderr, gettext("missing "
4804 "'=' for -o option\n"));
4810 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
4811 (add_prop_list(optarg, propval, &props, B_TRUE)))
4815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4824 /* get pool name and check number of arguments */
4826 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4833 (void) fprintf(stderr,
4834 gettext("missing <device> specification\n"));
4842 (void) fprintf(stderr,
4843 gettext("missing <new_device> specification\n"));
4846 new_disk = old_disk;
4856 (void) fprintf(stderr, gettext("too many arguments\n"));
4860 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
4865 if (zpool_get_config(zhp, NULL) == NULL) {
4866 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
4873 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
4875 if (nvroot == NULL) {
4881 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
4884 nvlist_free(nvroot);
4891 * zpool replace [-f] <pool> <device> <new_device>
4893 * -f Force attach, even if <new_device> appears to be in use.
4895 * Replace <device> with <new_device>.
4899 zpool_do_replace(int argc, char **argv)
4901 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
4905 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
4907 * -f Force attach, even if <new_device> appears to be in use.
4908 * -o Set property=value.
4910 * Attach <new_device> to the mirror containing <device>. If <device> is not
4911 * part of a mirror, then <device> will be transformed into a mirror of
4912 * <device> and <new_device>. In either case, <new_device> will begin life
4913 * with a DTL of [0, now], and will immediately begin to resilver itself.
4916 zpool_do_attach(int argc, char **argv)
4918 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
4922 * zpool detach [-f] <pool> <device>
4924 * -f Force detach of <device>, even if DTLs argue against it
4925 * (not supported yet)
4927 * Detach a device from a mirror. The operation will be refused if <device>
4928 * is the last device in the mirror, or if the DTLs indicate that this device
4929 * has the only valid copy of some data.
4933 zpool_do_detach(int argc, char **argv)
4936 char *poolname, *path;
4937 zpool_handle_t *zhp;
4941 while ((c = getopt(argc, argv, "f")) != -1) {
4945 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4954 /* get pool name and check number of arguments */
4956 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4961 (void) fprintf(stderr,
4962 gettext("missing <device> specification\n"));
4969 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4972 ret = zpool_vdev_detach(zhp, path);
4980 * zpool split [-gLnP] [-o prop=val] ...
4982 * [-R altroot] <pool> <newpool> [<device> ...]
4984 * -g Display guid for individual vdev name.
4985 * -L Follow links when resolving vdev path name.
4986 * -n Do not split the pool, but display the resulting layout if
4987 * it were to be split.
4988 * -o Set property=value, or set mount options.
4989 * -P Display full path for vdev name.
4990 * -R Mount the split-off pool under an alternate root.
4992 * Splits the named pool and gives it the new pool name. Devices to be split
4993 * off may be listed, provided that no more than one device is specified
4994 * per top-level vdev mirror. The newly split pool is left in an exported
4995 * state unless -R is specified.
4997 * Restrictions: the top-level of the pool pool must only be made up of
4998 * mirrors; all devices in the pool must be healthy; no device may be
4999 * undergoing a resilvering operation.
5002 zpool_do_split(int argc, char **argv)
5004 char *srcpool, *newpool, *propval;
5005 char *mntopts = NULL;
5008 zpool_handle_t *zhp;
5009 nvlist_t *config, *props = NULL;
5011 flags.dryrun = B_FALSE;
5012 flags.import = B_FALSE;
5013 flags.name_flags = 0;
5016 while ((c = getopt(argc, argv, ":gLR:no:P")) != -1) {
5019 flags.name_flags |= VDEV_NAME_GUID;
5022 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5025 flags.import = B_TRUE;
5027 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5028 &props, B_TRUE) != 0) {
5034 flags.dryrun = B_TRUE;
5037 if ((propval = strchr(optarg, '=')) != NULL) {
5040 if (add_prop_list(optarg, propval,
5041 &props, B_TRUE) != 0) {
5050 flags.name_flags |= VDEV_NAME_PATH;
5053 (void) fprintf(stderr, gettext("missing argument for "
5054 "'%c' option\n"), optopt);
5058 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5065 if (!flags.import && mntopts != NULL) {
5066 (void) fprintf(stderr, gettext("setting mntopts is only "
5067 "valid when importing the pool\n"));
5075 (void) fprintf(stderr, gettext("Missing pool name\n"));
5079 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5089 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5094 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5095 if (config == NULL) {
5099 (void) printf(gettext("would create '%s' with the "
5100 "following layout:\n\n"), newpool);
5101 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5108 if (ret != 0 || flags.dryrun || !flags.import) {
5109 nvlist_free(config);
5115 * The split was successful. Now we need to open the new
5116 * pool and import it.
5118 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5119 nvlist_free(config);
5123 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5124 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5126 (void) fprintf(stderr, gettext("Split was successful, but "
5127 "the datasets could not all be mounted\n"));
5128 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5129 "different altroot\n"), "zpool import");
5132 nvlist_free(config);
5141 * zpool online <pool> <device> ...
5144 zpool_do_online(int argc, char **argv)
5148 zpool_handle_t *zhp;
5150 vdev_state_t newstate;
5154 while ((c = getopt(argc, argv, "et")) != -1) {
5157 flags |= ZFS_ONLINE_EXPAND;
5161 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5170 /* get pool name and check number of arguments */
5172 (void) fprintf(stderr, gettext("missing pool name\n"));
5176 (void) fprintf(stderr, gettext("missing device name\n"));
5182 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5185 for (i = 1; i < argc; i++) {
5186 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5187 if (newstate != VDEV_STATE_HEALTHY) {
5188 (void) printf(gettext("warning: device '%s' "
5189 "onlined, but remains in faulted state\n"),
5191 if (newstate == VDEV_STATE_FAULTED)
5192 (void) printf(gettext("use 'zpool "
5193 "clear' to restore a faulted "
5196 (void) printf(gettext("use 'zpool "
5197 "replace' to replace devices "
5198 "that are no longer present\n"));
5211 * zpool offline [-ft] <pool> <device> ...
5213 * -f Force the device into the offline state, even if doing
5214 * so would appear to compromise pool availability.
5215 * (not supported yet)
5217 * -t Only take the device off-line temporarily. The offline
5218 * state will not be persistent across reboots.
5222 zpool_do_offline(int argc, char **argv)
5226 zpool_handle_t *zhp;
5228 boolean_t istmp = B_FALSE;
5231 while ((c = getopt(argc, argv, "ft")) != -1) {
5238 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5247 /* get pool name and check number of arguments */
5249 (void) fprintf(stderr, gettext("missing pool name\n"));
5253 (void) fprintf(stderr, gettext("missing device name\n"));
5259 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5262 for (i = 1; i < argc; i++) {
5263 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5273 * zpool clear <pool> [device]
5275 * Clear all errors associated with a pool or a particular device.
5278 zpool_do_clear(int argc, char **argv)
5282 boolean_t dryrun = B_FALSE;
5283 boolean_t do_rewind = B_FALSE;
5284 boolean_t xtreme_rewind = B_FALSE;
5285 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5286 nvlist_t *policy = NULL;
5287 zpool_handle_t *zhp;
5288 char *pool, *device;
5291 while ((c = getopt(argc, argv, "FnX")) != -1) {
5300 xtreme_rewind = B_TRUE;
5303 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5313 (void) fprintf(stderr, gettext("missing pool name\n"));
5318 (void) fprintf(stderr, gettext("too many arguments\n"));
5322 if ((dryrun || xtreme_rewind) && !do_rewind) {
5323 (void) fprintf(stderr,
5324 gettext("-n or -X only meaningful with -F\n"));
5328 rewind_policy = ZPOOL_TRY_REWIND;
5330 rewind_policy = ZPOOL_DO_REWIND;
5332 rewind_policy |= ZPOOL_EXTREME_REWIND;
5334 /* In future, further rewind policy choices can be passed along here */
5335 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5336 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5340 device = argc == 2 ? argv[1] : NULL;
5342 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5343 nvlist_free(policy);
5347 if (zpool_clear(zhp, device, policy) != 0)
5352 nvlist_free(policy);
5358 * zpool reguid <pool>
5361 zpool_do_reguid(int argc, char **argv)
5365 zpool_handle_t *zhp;
5369 while ((c = getopt(argc, argv, "")) != -1) {
5372 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5381 /* get pool name and check number of arguments */
5383 (void) fprintf(stderr, gettext("missing pool name\n"));
5388 (void) fprintf(stderr, gettext("too many arguments\n"));
5393 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5396 ret = zpool_reguid(zhp);
5404 * zpool reopen <pool>
5406 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5409 zpool_do_reopen(int argc, char **argv)
5413 zpool_handle_t *zhp;
5417 while ((c = getopt(argc, argv, "")) != -1) {
5420 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5430 (void) fprintf(stderr, gettext("missing pool name\n"));
5435 (void) fprintf(stderr, gettext("too many arguments\n"));
5440 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
5443 ret = zpool_reopen(zhp);
5448 typedef struct scrub_cbdata {
5455 scrub_callback(zpool_handle_t *zhp, void *data)
5457 scrub_cbdata_t *cb = data;
5461 * Ignore faulted pools.
5463 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5464 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5465 "currently unavailable\n"), zpool_get_name(zhp));
5469 err = zpool_scan(zhp, cb->cb_type);
5475 * zpool scrub [-s] <pool> ...
5477 * -s Stop. Stops any in-progress scrub.
5480 zpool_do_scrub(int argc, char **argv)
5485 cb.cb_type = POOL_SCAN_SCRUB;
5488 while ((c = getopt(argc, argv, "s")) != -1) {
5491 cb.cb_type = POOL_SCAN_NONE;
5494 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5506 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5510 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5514 * Print out detailed scrub status.
5517 print_scan_status(pool_scan_stat_t *ps)
5520 uint64_t elapsed, mins_left, hours_left;
5521 uint64_t pass_exam, examined, total;
5523 double fraction_done;
5524 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
5526 (void) printf(gettext(" scan: "));
5528 /* If there's never been a scan, there's not much to say. */
5529 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5530 ps->pss_func >= POOL_SCAN_FUNCS) {
5531 (void) printf(gettext("none requested\n"));
5535 start = ps->pss_start_time;
5536 end = ps->pss_end_time;
5537 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
5539 assert(ps->pss_func == POOL_SCAN_SCRUB ||
5540 ps->pss_func == POOL_SCAN_RESILVER);
5542 * Scan is finished or canceled.
5544 if (ps->pss_state == DSS_FINISHED) {
5545 uint64_t minutes_taken = (end - start) / 60;
5548 if (ps->pss_func == POOL_SCAN_SCRUB) {
5549 fmt = gettext("scrub repaired %s in %lluh%um with "
5550 "%llu errors on %s");
5551 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5552 fmt = gettext("resilvered %s in %lluh%um with "
5553 "%llu errors on %s");
5556 (void) printf(fmt, processed_buf,
5557 (u_longlong_t)(minutes_taken / 60),
5558 (uint_t)(minutes_taken % 60),
5559 (u_longlong_t)ps->pss_errors,
5560 ctime((time_t *)&end));
5562 } else if (ps->pss_state == DSS_CANCELED) {
5563 if (ps->pss_func == POOL_SCAN_SCRUB) {
5564 (void) printf(gettext("scrub canceled on %s"),
5566 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5567 (void) printf(gettext("resilver canceled on %s"),
5573 assert(ps->pss_state == DSS_SCANNING);
5576 * Scan is in progress.
5578 if (ps->pss_func == POOL_SCAN_SCRUB) {
5579 (void) printf(gettext("scrub in progress since %s"),
5581 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
5582 (void) printf(gettext("resilver in progress since %s"),
5586 examined = ps->pss_examined ? ps->pss_examined : 1;
5587 total = ps->pss_to_examine;
5588 fraction_done = (double)examined / total;
5590 /* elapsed time for this pass */
5591 elapsed = time(NULL) - ps->pss_pass_start;
5592 elapsed = elapsed ? elapsed : 1;
5593 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
5594 rate = pass_exam / elapsed;
5595 rate = rate ? rate : 1;
5596 mins_left = ((total - examined) / rate) / 60;
5597 hours_left = mins_left / 60;
5599 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
5600 zfs_nicenum(total, total_buf, sizeof (total_buf));
5601 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
5604 * do not print estimated time if hours_left is more than 30 days
5606 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5607 examined_buf, total_buf, rate_buf);
5608 if (hours_left < (30 * 24)) {
5609 (void) printf(gettext(", %lluh%um to go\n"),
5610 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
5612 (void) printf(gettext(
5613 ", (scan is slow, no estimated time)\n"));
5616 if (ps->pss_func == POOL_SCAN_RESILVER) {
5617 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5618 processed_buf, 100 * fraction_done);
5619 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
5620 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5621 processed_buf, 100 * fraction_done);
5626 print_error_log(zpool_handle_t *zhp)
5628 nvlist_t *nverrlist = NULL;
5631 size_t len = MAXPATHLEN * 2;
5633 if (zpool_get_errlog(zhp, &nverrlist) != 0)
5636 (void) printf("errors: Permanent errors have been "
5637 "detected in the following files:\n\n");
5639 pathname = safe_malloc(len);
5641 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
5643 uint64_t dsobj, obj;
5645 verify(nvpair_value_nvlist(elem, &nv) == 0);
5646 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
5648 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
5650 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
5651 (void) printf("%7s %s\n", "", pathname);
5654 nvlist_free(nverrlist);
5658 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
5667 (void) printf(gettext("\tspares\n"));
5669 for (i = 0; i < nspares; i++) {
5670 name = zpool_vdev_name(g_zfs, zhp, spares[i],
5672 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
5678 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
5687 (void) printf(gettext("\tcache\n"));
5689 for (i = 0; i < nl2cache; i++) {
5690 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
5692 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
5698 print_dedup_stats(nvlist_t *config)
5700 ddt_histogram_t *ddh;
5706 * If the pool was faulted then we may not have been able to
5707 * obtain the config. Otherwise, if we have anything in the dedup
5708 * table continue processing the stats.
5710 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
5711 (uint64_t **)&ddo, &c) != 0)
5714 (void) printf("\n");
5715 (void) printf(gettext(" dedup: "));
5716 if (ddo->ddo_count == 0) {
5717 (void) printf(gettext("no DDT entries\n"));
5721 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5722 (u_longlong_t)ddo->ddo_count,
5723 (u_longlong_t)ddo->ddo_dspace,
5724 (u_longlong_t)ddo->ddo_mspace);
5726 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
5727 (uint64_t **)&dds, &c) == 0);
5728 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
5729 (uint64_t **)&ddh, &c) == 0);
5730 zpool_dump_ddt(dds, ddh);
5734 * Display a summary of pool status. Displays a summary such as:
5738 * reason: One or more devices ...
5739 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5745 * When given the '-v' option, we print out the complete config. If the '-e'
5746 * option is specified, then we print out error rate information as well.
5749 status_callback(zpool_handle_t *zhp, void *data)
5751 status_cbdata_t *cbp = data;
5752 nvlist_t *config, *nvroot;
5754 zpool_status_t reason;
5755 zpool_errata_t errata;
5760 config = zpool_get_config(zhp, NULL);
5761 reason = zpool_get_status(zhp, &msgid, &errata);
5766 * If we were given 'zpool status -x', only report those pools with
5769 if (cbp->cb_explain &&
5770 (reason == ZPOOL_STATUS_OK ||
5771 reason == ZPOOL_STATUS_VERSION_OLDER ||
5772 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
5773 if (!cbp->cb_allpools) {
5774 (void) printf(gettext("pool '%s' is healthy\n"),
5775 zpool_get_name(zhp));
5777 cbp->cb_first = B_FALSE;
5783 cbp->cb_first = B_FALSE;
5785 (void) printf("\n");
5787 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5789 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
5790 (uint64_t **)&vs, &c) == 0);
5791 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
5793 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
5794 (void) printf(gettext(" state: %s\n"), health);
5797 case ZPOOL_STATUS_MISSING_DEV_R:
5798 (void) printf(gettext("status: One or more devices could not "
5799 "be opened. Sufficient replicas exist for\n\tthe pool to "
5800 "continue functioning in a degraded state.\n"));
5801 (void) printf(gettext("action: Attach the missing device and "
5802 "online it using 'zpool online'.\n"));
5805 case ZPOOL_STATUS_MISSING_DEV_NR:
5806 (void) printf(gettext("status: One or more devices could not "
5807 "be opened. There are insufficient\n\treplicas for the "
5808 "pool to continue functioning.\n"));
5809 (void) printf(gettext("action: Attach the missing device and "
5810 "online it using 'zpool online'.\n"));
5813 case ZPOOL_STATUS_CORRUPT_LABEL_R:
5814 (void) printf(gettext("status: One or more devices could not "
5815 "be used because the label is missing or\n\tinvalid. "
5816 "Sufficient replicas exist for the pool to continue\n\t"
5817 "functioning in a degraded state.\n"));
5818 (void) printf(gettext("action: Replace the device using "
5819 "'zpool replace'.\n"));
5822 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
5823 (void) printf(gettext("status: One or more devices could not "
5824 "be used because the label is missing \n\tor invalid. "
5825 "There are insufficient replicas for the pool to "
5826 "continue\n\tfunctioning.\n"));
5827 zpool_explain_recover(zpool_get_handle(zhp),
5828 zpool_get_name(zhp), reason, config);
5831 case ZPOOL_STATUS_FAILING_DEV:
5832 (void) printf(gettext("status: One or more devices has "
5833 "experienced an unrecoverable error. An\n\tattempt was "
5834 "made to correct the error. Applications are "
5836 (void) printf(gettext("action: Determine if the device needs "
5837 "to be replaced, and clear the errors\n\tusing "
5838 "'zpool clear' or replace the device with 'zpool "
5842 case ZPOOL_STATUS_OFFLINE_DEV:
5843 (void) printf(gettext("status: One or more devices has "
5844 "been taken offline by the administrator.\n\tSufficient "
5845 "replicas exist for the pool to continue functioning in "
5846 "a\n\tdegraded state.\n"));
5847 (void) printf(gettext("action: Online the device using "
5848 "'zpool online' or replace the device with\n\t'zpool "
5852 case ZPOOL_STATUS_REMOVED_DEV:
5853 (void) printf(gettext("status: One or more devices has "
5854 "been removed by the administrator.\n\tSufficient "
5855 "replicas exist for the pool to continue functioning in "
5856 "a\n\tdegraded state.\n"));
5857 (void) printf(gettext("action: Online the device using "
5858 "'zpool online' or replace the device with\n\t'zpool "
5862 case ZPOOL_STATUS_RESILVERING:
5863 (void) printf(gettext("status: One or more devices is "
5864 "currently being resilvered. The pool will\n\tcontinue "
5865 "to function, possibly in a degraded state.\n"));
5866 (void) printf(gettext("action: Wait for the resilver to "
5870 case ZPOOL_STATUS_CORRUPT_DATA:
5871 (void) printf(gettext("status: One or more devices has "
5872 "experienced an error resulting in data\n\tcorruption. "
5873 "Applications may be affected.\n"));
5874 (void) printf(gettext("action: Restore the file in question "
5875 "if possible. Otherwise restore the\n\tentire pool from "
5879 case ZPOOL_STATUS_CORRUPT_POOL:
5880 (void) printf(gettext("status: The pool metadata is corrupted "
5881 "and the pool cannot be opened.\n"));
5882 zpool_explain_recover(zpool_get_handle(zhp),
5883 zpool_get_name(zhp), reason, config);
5886 case ZPOOL_STATUS_VERSION_OLDER:
5887 (void) printf(gettext("status: The pool is formatted using a "
5888 "legacy on-disk format. The pool can\n\tstill be used, "
5889 "but some features are unavailable.\n"));
5890 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5891 "upgrade'. Once this is done, the\n\tpool will no longer "
5892 "be accessible on software that does not support\n\t"
5893 "feature flags.\n"));
5896 case ZPOOL_STATUS_VERSION_NEWER:
5897 (void) printf(gettext("status: The pool has been upgraded to a "
5898 "newer, incompatible on-disk version.\n\tThe pool cannot "
5899 "be accessed on this system.\n"));
5900 (void) printf(gettext("action: Access the pool from a system "
5901 "running more recent software, or\n\trestore the pool from "
5905 case ZPOOL_STATUS_FEAT_DISABLED:
5906 (void) printf(gettext("status: Some supported features are not "
5907 "enabled on the pool. The pool can\n\tstill be used, but "
5908 "some features are unavailable.\n"));
5909 (void) printf(gettext("action: Enable all features using "
5910 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5911 "longer be accessible by software that does not support\n\t"
5912 "the features. See zpool-features(5) for details.\n"));
5915 case ZPOOL_STATUS_UNSUP_FEAT_READ:
5916 (void) printf(gettext("status: The pool cannot be accessed on "
5917 "this system because it uses the\n\tfollowing feature(s) "
5918 "not supported on this system:\n"));
5919 zpool_print_unsup_feat(config);
5920 (void) printf("\n");
5921 (void) printf(gettext("action: Access the pool from a system "
5922 "that supports the required feature(s),\n\tor restore the "
5923 "pool from backup.\n"));
5926 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5927 (void) printf(gettext("status: The pool can only be accessed "
5928 "in read-only mode on this system. It\n\tcannot be "
5929 "accessed in read-write mode because it uses the "
5930 "following\n\tfeature(s) not supported on this system:\n"));
5931 zpool_print_unsup_feat(config);
5932 (void) printf("\n");
5933 (void) printf(gettext("action: The pool cannot be accessed in "
5934 "read-write mode. Import the pool with\n"
5935 "\t\"-o readonly=on\", access the pool from a system that "
5936 "supports the\n\trequired feature(s), or restore the "
5937 "pool from backup.\n"));
5940 case ZPOOL_STATUS_FAULTED_DEV_R:
5941 (void) printf(gettext("status: One or more devices are "
5942 "faulted in response to persistent errors.\n\tSufficient "
5943 "replicas exist for the pool to continue functioning "
5944 "in a\n\tdegraded state.\n"));
5945 (void) printf(gettext("action: Replace the faulted device, "
5946 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5949 case ZPOOL_STATUS_FAULTED_DEV_NR:
5950 (void) printf(gettext("status: One or more devices are "
5951 "faulted in response to persistent errors. There are "
5952 "insufficient replicas for the pool to\n\tcontinue "
5954 (void) printf(gettext("action: Destroy and re-create the pool "
5955 "from a backup source. Manually marking the device\n"
5956 "\trepaired using 'zpool clear' may allow some data "
5957 "to be recovered.\n"));
5960 case ZPOOL_STATUS_IO_FAILURE_WAIT:
5961 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
5962 (void) printf(gettext("status: One or more devices are "
5963 "faulted in response to IO failures.\n"));
5964 (void) printf(gettext("action: Make sure the affected devices "
5965 "are connected, then run 'zpool clear'.\n"));
5968 case ZPOOL_STATUS_BAD_LOG:
5969 (void) printf(gettext("status: An intent log record "
5970 "could not be read.\n"
5971 "\tWaiting for administrator intervention to fix the "
5972 "faulted pool.\n"));
5973 (void) printf(gettext("action: Either restore the affected "
5974 "device(s) and run 'zpool online',\n"
5975 "\tor ignore the intent log records by running "
5976 "'zpool clear'.\n"));
5979 case ZPOOL_STATUS_HOSTID_MISMATCH:
5980 (void) printf(gettext("status: Mismatch between pool hostid "
5981 "and system hostid on imported pool.\n\tThis pool was "
5982 "previously imported into a system with a different "
5983 "hostid,\n\tand then was verbatim imported into this "
5985 (void) printf(gettext("action: Export this pool on all systems "
5986 "on which it is imported.\n"
5987 "\tThen import it to correct the mismatch.\n"));
5990 case ZPOOL_STATUS_ERRATA:
5991 (void) printf(gettext("status: Errata #%d detected.\n"),
5995 case ZPOOL_ERRATA_NONE:
5998 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
5999 (void) printf(gettext("action: To correct the issue "
6000 "run 'zpool scrub'.\n"));
6005 * All errata which allow the pool to be imported
6006 * must contain an action message.
6014 * The remaining errors can't actually be generated, yet.
6016 assert(reason == ZPOOL_STATUS_OK);
6020 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6023 if (config != NULL) {
6025 nvlist_t **spares, **l2cache;
6026 uint_t nspares, nl2cache;
6027 pool_scan_stat_t *ps = NULL;
6029 (void) nvlist_lookup_uint64_array(nvroot,
6030 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6031 print_scan_status(ps);
6033 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6034 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6035 if (cbp->cb_namewidth < 10)
6036 cbp->cb_namewidth = 10;
6038 (void) printf(gettext("config:\n\n"));
6039 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
6040 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6042 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6045 if (num_logs(nvroot) > 0)
6046 print_logs(zhp, cbp, nvroot);
6047 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6048 &l2cache, &nl2cache) == 0)
6049 print_l2cache(zhp, cbp, l2cache, nl2cache);
6051 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6052 &spares, &nspares) == 0)
6053 print_spares(zhp, cbp, spares, nspares);
6055 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6057 nvlist_t *nverrlist = NULL;
6060 * If the approximate error count is small, get a
6061 * precise count by fetching the entire log and
6062 * uniquifying the results.
6064 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6065 zpool_get_errlog(zhp, &nverrlist) == 0) {
6070 while ((elem = nvlist_next_nvpair(nverrlist,
6075 nvlist_free(nverrlist);
6077 (void) printf("\n");
6080 (void) printf(gettext("errors: No known data "
6082 else if (!cbp->cb_verbose)
6083 (void) printf(gettext("errors: %llu data "
6084 "errors, use '-v' for a list\n"),
6085 (u_longlong_t)nerr);
6087 print_error_log(zhp);
6090 if (cbp->cb_dedup_stats)
6091 print_dedup_stats(config);
6093 (void) printf(gettext("config: The configuration cannot be "
6101 * zpool status [-c CMD] [-gLPvx] [-T d|u] [pool] ... [interval [count]]
6103 * -c CMD For each vdev, run command CMD
6104 * -g Display guid for individual vdev name.
6105 * -L Follow links when resolving vdev path name.
6106 * -P Display full path for vdev name.
6107 * -v Display complete error logs
6108 * -x Display only pools with potential problems
6109 * -D Display dedup status (undocumented)
6110 * -T Display a timestamp in date(1) or Unix format
6112 * Describes the health status of all pools or some subset.
6115 zpool_do_status(int argc, char **argv)
6120 unsigned long count = 0;
6121 status_cbdata_t cb = { 0 };
6125 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6131 cb.cb_name_flags |= VDEV_NAME_GUID;
6134 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6137 cb.cb_name_flags |= VDEV_NAME_PATH;
6140 cb.cb_verbose = B_TRUE;
6143 cb.cb_explain = B_TRUE;
6146 cb.cb_dedup_stats = B_TRUE;
6149 get_timestamp_arg(*optarg);
6152 if (optopt == 'c') {
6154 gettext("Missing CMD for -c\n"));
6157 gettext("invalid option '%c'\n"), optopt);
6166 get_interval_count(&argc, argv, &interval, &count);
6169 cb.cb_allpools = B_TRUE;
6171 cb.cb_first = B_TRUE;
6172 cb.cb_print_status = B_TRUE;
6175 if (timestamp_fmt != NODATE)
6176 print_timestamp(timestamp_fmt);
6179 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6182 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6183 status_callback, &cb);
6185 if (cb.vcdl != NULL)
6186 free_vdev_cmd_data_list(cb.vcdl);
6188 if (argc == 0 && cb.cb_count == 0)
6189 (void) fprintf(stderr, gettext("no pools available\n"));
6190 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6191 (void) printf(gettext("all pools are healthy\n"));
6199 if (count != 0 && --count == 0)
6202 (void) fsleep(interval);
6208 typedef struct upgrade_cbdata {
6211 uint64_t cb_version;
6216 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6218 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6219 int *count = (int *)unsupp_fs;
6221 if (zfs_version > ZPL_VERSION) {
6222 (void) printf(gettext("%s (v%d) is not supported by this "
6223 "implementation of ZFS.\n"),
6224 zfs_get_name(zhp), zfs_version);
6228 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6236 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6240 uint64_t oldversion;
6243 config = zpool_get_config(zhp, NULL);
6244 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6247 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6248 assert(oldversion < version);
6250 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6255 (void) fprintf(stderr, gettext("Upgrade not performed due "
6256 "to %d unsupported filesystems (max v%d).\n"),
6257 unsupp_fs, (int)ZPL_VERSION);
6261 ret = zpool_upgrade(zhp, version);
6265 if (version >= SPA_VERSION_FEATURES) {
6266 (void) printf(gettext("Successfully upgraded "
6267 "'%s' from version %llu to feature flags.\n"),
6268 zpool_get_name(zhp), (u_longlong_t)oldversion);
6270 (void) printf(gettext("Successfully upgraded "
6271 "'%s' from version %llu to version %llu.\n"),
6272 zpool_get_name(zhp), (u_longlong_t)oldversion,
6273 (u_longlong_t)version);
6280 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6283 boolean_t firstff = B_TRUE;
6284 nvlist_t *enabled = zpool_get_features(zhp);
6287 for (i = 0; i < SPA_FEATURES; i++) {
6288 const char *fname = spa_feature_table[i].fi_uname;
6289 const char *fguid = spa_feature_table[i].fi_guid;
6290 if (!nvlist_exists(enabled, fguid)) {
6292 verify(-1 != asprintf(&propname, "feature@%s", fname));
6293 ret = zpool_set_prop(zhp, propname,
6294 ZFS_FEATURE_ENABLED);
6302 (void) printf(gettext("Enabled the "
6303 "following features on '%s':\n"),
6304 zpool_get_name(zhp));
6307 (void) printf(gettext(" %s\n"), fname);
6318 upgrade_cb(zpool_handle_t *zhp, void *arg)
6320 upgrade_cbdata_t *cbp = arg;
6323 boolean_t printnl = B_FALSE;
6326 config = zpool_get_config(zhp, NULL);
6327 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6330 assert(SPA_VERSION_IS_SUPPORTED(version));
6332 if (version < cbp->cb_version) {
6333 cbp->cb_first = B_FALSE;
6334 ret = upgrade_version(zhp, cbp->cb_version);
6340 * If they did "zpool upgrade -a", then we could
6341 * be doing ioctls to different pools. We need
6342 * to log this history once to each pool, and bypass
6343 * the normal history logging that happens in main().
6345 (void) zpool_log_history(g_zfs, history_str);
6346 log_history = B_FALSE;
6349 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6351 ret = upgrade_enable_all(zhp, &count);
6356 cbp->cb_first = B_FALSE;
6362 (void) printf(gettext("\n"));
6369 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6371 upgrade_cbdata_t *cbp = arg;
6375 config = zpool_get_config(zhp, NULL);
6376 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6379 assert(SPA_VERSION_IS_SUPPORTED(version));
6381 if (version < SPA_VERSION_FEATURES) {
6382 if (cbp->cb_first) {
6383 (void) printf(gettext("The following pools are "
6384 "formatted with legacy version numbers and can\n"
6385 "be upgraded to use feature flags. After "
6386 "being upgraded, these pools\nwill no "
6387 "longer be accessible by software that does not "
6388 "support feature\nflags.\n\n"));
6389 (void) printf(gettext("VER POOL\n"));
6390 (void) printf(gettext("--- ------------\n"));
6391 cbp->cb_first = B_FALSE;
6394 (void) printf("%2llu %s\n", (u_longlong_t)version,
6395 zpool_get_name(zhp));
6402 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6404 upgrade_cbdata_t *cbp = arg;
6408 config = zpool_get_config(zhp, NULL);
6409 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6412 if (version >= SPA_VERSION_FEATURES) {
6414 boolean_t poolfirst = B_TRUE;
6415 nvlist_t *enabled = zpool_get_features(zhp);
6417 for (i = 0; i < SPA_FEATURES; i++) {
6418 const char *fguid = spa_feature_table[i].fi_guid;
6419 const char *fname = spa_feature_table[i].fi_uname;
6420 if (!nvlist_exists(enabled, fguid)) {
6421 if (cbp->cb_first) {
6422 (void) printf(gettext("\nSome "
6423 "supported features are not "
6424 "enabled on the following pools. "
6425 "Once a\nfeature is enabled the "
6426 "pool may become incompatible with "
6427 "software\nthat does not support "
6429 "zpool-features(5) for "
6431 (void) printf(gettext("POOL "
6433 (void) printf(gettext("------"
6435 cbp->cb_first = B_FALSE;
6439 (void) printf(gettext("%s\n"),
6440 zpool_get_name(zhp));
6441 poolfirst = B_FALSE;
6444 (void) printf(gettext(" %s\n"), fname);
6447 * If they did "zpool upgrade -a", then we could
6448 * be doing ioctls to different pools. We need
6449 * to log this history once to each pool, and bypass
6450 * the normal history logging that happens in main().
6452 (void) zpool_log_history(g_zfs, history_str);
6453 log_history = B_FALSE;
6462 upgrade_one(zpool_handle_t *zhp, void *data)
6464 boolean_t printnl = B_FALSE;
6465 upgrade_cbdata_t *cbp = data;
6466 uint64_t cur_version;
6469 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6470 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6471 "Pool 'log' must be renamed using export and import"
6476 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6477 if (cur_version > cbp->cb_version) {
6478 (void) printf(gettext("Pool '%s' is already formatted "
6479 "using more current version '%llu'.\n\n"),
6480 zpool_get_name(zhp), (u_longlong_t)cur_version);
6484 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
6485 (void) printf(gettext("Pool '%s' is already formatted "
6486 "using version %llu.\n\n"), zpool_get_name(zhp),
6487 (u_longlong_t)cbp->cb_version);
6491 if (cur_version != cbp->cb_version) {
6493 ret = upgrade_version(zhp, cbp->cb_version);
6498 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6500 ret = upgrade_enable_all(zhp, &count);
6506 } else if (cur_version == SPA_VERSION) {
6507 (void) printf(gettext("Pool '%s' already has all "
6508 "supported features enabled.\n"),
6509 zpool_get_name(zhp));
6514 (void) printf(gettext("\n"));
6523 * zpool upgrade [-V version] <-a | pool ...>
6525 * With no arguments, display downrev'd ZFS pool available for upgrade.
6526 * Individual pools can be upgraded by specifying the pool, and '-a' will
6527 * upgrade all pools.
6530 zpool_do_upgrade(int argc, char **argv)
6533 upgrade_cbdata_t cb = { 0 };
6535 boolean_t showversions = B_FALSE;
6536 boolean_t upgradeall = B_FALSE;
6541 while ((c = getopt(argc, argv, ":avV:")) != -1) {
6544 upgradeall = B_TRUE;
6547 showversions = B_TRUE;
6550 cb.cb_version = strtoll(optarg, &end, 10);
6552 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
6553 (void) fprintf(stderr,
6554 gettext("invalid version '%s'\n"), optarg);
6559 (void) fprintf(stderr, gettext("missing argument for "
6560 "'%c' option\n"), optopt);
6564 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6575 if (cb.cb_version == 0) {
6576 cb.cb_version = SPA_VERSION;
6577 } else if (!upgradeall && argc == 0) {
6578 (void) fprintf(stderr, gettext("-V option is "
6579 "incompatible with other arguments\n"));
6584 if (upgradeall || argc != 0) {
6585 (void) fprintf(stderr, gettext("-v option is "
6586 "incompatible with other arguments\n"));
6589 } else if (upgradeall) {
6591 (void) fprintf(stderr, gettext("-a option should not "
6592 "be used along with a pool name\n"));
6597 (void) printf(gettext("This system supports ZFS pool feature "
6602 (void) printf(gettext("The following features are "
6604 (void) printf(gettext("FEAT DESCRIPTION\n"));
6605 (void) printf("----------------------------------------------"
6606 "---------------\n");
6607 for (i = 0; i < SPA_FEATURES; i++) {
6608 zfeature_info_t *fi = &spa_feature_table[i];
6610 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
6611 " (read-only compatible)" : "";
6613 (void) printf("%-37s%s\n", fi->fi_uname, ro);
6614 (void) printf(" %s\n", fi->fi_desc);
6616 (void) printf("\n");
6618 (void) printf(gettext("The following legacy versions are also "
6620 (void) printf(gettext("VER DESCRIPTION\n"));
6621 (void) printf("--- -----------------------------------------"
6622 "---------------\n");
6623 (void) printf(gettext(" 1 Initial ZFS version\n"));
6624 (void) printf(gettext(" 2 Ditto blocks "
6625 "(replicated metadata)\n"));
6626 (void) printf(gettext(" 3 Hot spares and double parity "
6628 (void) printf(gettext(" 4 zpool history\n"));
6629 (void) printf(gettext(" 5 Compression using the gzip "
6631 (void) printf(gettext(" 6 bootfs pool property\n"));
6632 (void) printf(gettext(" 7 Separate intent log devices\n"));
6633 (void) printf(gettext(" 8 Delegated administration\n"));
6634 (void) printf(gettext(" 9 refquota and refreservation "
6636 (void) printf(gettext(" 10 Cache devices\n"));
6637 (void) printf(gettext(" 11 Improved scrub performance\n"));
6638 (void) printf(gettext(" 12 Snapshot properties\n"));
6639 (void) printf(gettext(" 13 snapused property\n"));
6640 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6641 (void) printf(gettext(" 15 user/group space accounting\n"));
6642 (void) printf(gettext(" 16 stmf property support\n"));
6643 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6644 (void) printf(gettext(" 18 Snapshot user holds\n"));
6645 (void) printf(gettext(" 19 Log device removal\n"));
6646 (void) printf(gettext(" 20 Compression using zle "
6647 "(zero-length encoding)\n"));
6648 (void) printf(gettext(" 21 Deduplication\n"));
6649 (void) printf(gettext(" 22 Received properties\n"));
6650 (void) printf(gettext(" 23 Slim ZIL\n"));
6651 (void) printf(gettext(" 24 System attributes\n"));
6652 (void) printf(gettext(" 25 Improved scrub stats\n"));
6653 (void) printf(gettext(" 26 Improved snapshot deletion "
6655 (void) printf(gettext(" 27 Improved snapshot creation "
6657 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6658 (void) printf(gettext("\nFor more information on a particular "
6659 "version, including supported releases,\n"));
6660 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6661 } else if (argc == 0 && upgradeall) {
6662 cb.cb_first = B_TRUE;
6663 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
6664 if (ret == 0 && cb.cb_first) {
6665 if (cb.cb_version == SPA_VERSION) {
6666 (void) printf(gettext("All pools are already "
6667 "formatted using feature flags.\n\n"));
6668 (void) printf(gettext("Every feature flags "
6669 "pool already has all supported features "
6672 (void) printf(gettext("All pools are already "
6673 "formatted with version %llu or higher.\n"),
6674 (u_longlong_t)cb.cb_version);
6677 } else if (argc == 0) {
6678 cb.cb_first = B_TRUE;
6679 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
6683 (void) printf(gettext("All pools are formatted "
6684 "using feature flags.\n\n"));
6686 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6687 "for a list of available legacy versions.\n"));
6690 cb.cb_first = B_TRUE;
6691 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
6695 (void) printf(gettext("Every feature flags pool has "
6696 "all supported features enabled.\n"));
6698 (void) printf(gettext("\n"));
6701 ret = for_each_pool(argc, argv, B_FALSE, NULL,
6708 typedef struct hist_cbdata {
6715 * Print out the command history for a specific pool.
6718 get_history_one(zpool_handle_t *zhp, void *data)
6724 hist_cbdata_t *cb = (hist_cbdata_t *)data;
6726 cb->first = B_FALSE;
6728 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
6730 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
6733 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
6734 &records, &numrecords) == 0);
6735 for (i = 0; i < numrecords; i++) {
6736 nvlist_t *rec = records[i];
6739 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
6743 tsec = fnvlist_lookup_uint64(records[i],
6745 (void) localtime_r(&tsec, &t);
6746 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
6749 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
6750 (void) printf("%s %s", tbuf,
6751 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
6752 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
6754 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
6757 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
6758 (void) printf("%s unrecognized record:\n",
6760 dump_nvlist(rec, 4);
6763 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
6764 zfs_history_event_names[ievent],
6765 (longlong_t)fnvlist_lookup_uint64(
6766 rec, ZPOOL_HIST_TXG),
6767 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
6768 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
6771 (void) printf("%s [txg:%lld] %s", tbuf,
6772 (longlong_t)fnvlist_lookup_uint64(
6773 rec, ZPOOL_HIST_TXG),
6774 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
6775 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
6776 (void) printf(" %s (%llu)",
6777 fnvlist_lookup_string(rec,
6779 (u_longlong_t)fnvlist_lookup_uint64(rec,
6782 (void) printf(" %s", fnvlist_lookup_string(rec,
6783 ZPOOL_HIST_INT_STR));
6784 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
6787 (void) printf("%s ioctl %s\n", tbuf,
6788 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
6789 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
6790 (void) printf(" input:\n");
6791 dump_nvlist(fnvlist_lookup_nvlist(rec,
6792 ZPOOL_HIST_INPUT_NVL), 8);
6794 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
6795 (void) printf(" output:\n");
6796 dump_nvlist(fnvlist_lookup_nvlist(rec,
6797 ZPOOL_HIST_OUTPUT_NVL), 8);
6802 (void) printf("%s unrecognized record:\n", tbuf);
6803 dump_nvlist(rec, 4);
6807 (void) printf("\n");
6810 (void) printf(" [");
6811 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
6812 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
6813 struct passwd *pwd = getpwuid(who);
6814 (void) printf("user %d ", (int)who);
6816 (void) printf("(%s) ", pwd->pw_name);
6818 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
6819 (void) printf("on %s",
6820 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
6822 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
6823 (void) printf(":%s",
6824 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
6828 (void) printf("\n");
6830 (void) printf("\n");
6837 * zpool history <pool>
6839 * Displays the history of commands that modified pools.
6842 zpool_do_history(int argc, char **argv)
6844 hist_cbdata_t cbdata = { 0 };
6848 cbdata.first = B_TRUE;
6850 while ((c = getopt(argc, argv, "li")) != -1) {
6853 cbdata.longfmt = B_TRUE;
6856 cbdata.internal = B_TRUE;
6859 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6867 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
6870 if (argc == 0 && cbdata.first == B_TRUE) {
6871 (void) fprintf(stderr, gettext("no pools available\n"));
6878 typedef struct ev_opts {
6886 zpool_do_events_short(nvlist_t *nvl)
6888 char ctime_str[26], str[32], *ptr;
6892 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
6893 memset(str, ' ', 32);
6894 (void) ctime_r((const time_t *)&tv[0], ctime_str);
6895 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
6896 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
6897 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
6898 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
6899 (void) printf(gettext("%s "), str);
6901 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
6902 (void) printf(gettext("%s\n"), ptr);
6906 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
6910 for (nvp = nvlist_next_nvpair(nvl, NULL);
6911 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
6913 data_type_t type = nvpair_type(nvp);
6914 const char *name = nvpair_name(nvp);
6924 printf(gettext("%*s%s = "), depth, "", name);
6927 case DATA_TYPE_BOOLEAN:
6928 printf(gettext("%s"), "1");
6931 case DATA_TYPE_BOOLEAN_VALUE:
6932 (void) nvpair_value_boolean_value(nvp, &b);
6933 printf(gettext("%s"), b ? "1" : "0");
6936 case DATA_TYPE_BYTE:
6937 (void) nvpair_value_byte(nvp, &i8);
6938 printf(gettext("0x%x"), i8);
6941 case DATA_TYPE_INT8:
6942 (void) nvpair_value_int8(nvp, (void *)&i8);
6943 printf(gettext("0x%x"), i8);
6946 case DATA_TYPE_UINT8:
6947 (void) nvpair_value_uint8(nvp, &i8);
6948 printf(gettext("0x%x"), i8);
6951 case DATA_TYPE_INT16:
6952 (void) nvpair_value_int16(nvp, (void *)&i16);
6953 printf(gettext("0x%x"), i16);
6956 case DATA_TYPE_UINT16:
6957 (void) nvpair_value_uint16(nvp, &i16);
6958 printf(gettext("0x%x"), i16);
6961 case DATA_TYPE_INT32:
6962 (void) nvpair_value_int32(nvp, (void *)&i32);
6963 printf(gettext("0x%x"), i32);
6966 case DATA_TYPE_UINT32:
6967 (void) nvpair_value_uint32(nvp, &i32);
6968 printf(gettext("0x%x"), i32);
6971 case DATA_TYPE_INT64:
6972 (void) nvpair_value_int64(nvp, (void *)&i64);
6973 printf(gettext("0x%llx"), (u_longlong_t)i64);
6976 case DATA_TYPE_UINT64:
6977 (void) nvpair_value_uint64(nvp, &i64);
6979 * translate vdev state values to readable
6980 * strings to aide zpool events consumers
6983 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
6985 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
6986 printf(gettext("\"%s\" (0x%llx)"),
6987 zpool_state_to_name(i64, VDEV_AUX_NONE),
6990 printf(gettext("0x%llx"), (u_longlong_t)i64);
6994 case DATA_TYPE_HRTIME:
6995 (void) nvpair_value_hrtime(nvp, (void *)&i64);
6996 printf(gettext("0x%llx"), (u_longlong_t)i64);
6999 case DATA_TYPE_STRING:
7000 (void) nvpair_value_string(nvp, &str);
7001 printf(gettext("\"%s\""), str ? str : "<NULL>");
7004 case DATA_TYPE_NVLIST:
7005 printf(gettext("(embedded nvlist)\n"));
7006 (void) nvpair_value_nvlist(nvp, &cnv);
7007 zpool_do_events_nvprint(cnv, depth + 8);
7008 printf(gettext("%*s(end %s)"), depth, "", name);
7011 case DATA_TYPE_NVLIST_ARRAY: {
7015 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7016 printf(gettext("(%d embedded nvlists)\n"), nelem);
7017 for (i = 0; i < nelem; i++) {
7018 printf(gettext("%*s%s[%d] = %s\n"),
7019 depth, "", name, i, "(embedded nvlist)");
7020 zpool_do_events_nvprint(val[i], depth + 8);
7021 printf(gettext("%*s(end %s[%i])\n"),
7022 depth, "", name, i);
7024 printf(gettext("%*s(end %s)\n"), depth, "", name);
7028 case DATA_TYPE_INT8_ARRAY: {
7032 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7033 for (i = 0; i < nelem; i++)
7034 printf(gettext("0x%x "), val[i]);
7039 case DATA_TYPE_UINT8_ARRAY: {
7043 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7044 for (i = 0; i < nelem; i++)
7045 printf(gettext("0x%x "), val[i]);
7050 case DATA_TYPE_INT16_ARRAY: {
7054 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7055 for (i = 0; i < nelem; i++)
7056 printf(gettext("0x%x "), val[i]);
7061 case DATA_TYPE_UINT16_ARRAY: {
7065 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7066 for (i = 0; i < nelem; i++)
7067 printf(gettext("0x%x "), val[i]);
7072 case DATA_TYPE_INT32_ARRAY: {
7076 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7077 for (i = 0; i < nelem; i++)
7078 printf(gettext("0x%x "), val[i]);
7083 case DATA_TYPE_UINT32_ARRAY: {
7087 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7088 for (i = 0; i < nelem; i++)
7089 printf(gettext("0x%x "), val[i]);
7094 case DATA_TYPE_INT64_ARRAY: {
7098 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7099 for (i = 0; i < nelem; i++)
7100 printf(gettext("0x%llx "),
7101 (u_longlong_t)val[i]);
7106 case DATA_TYPE_UINT64_ARRAY: {
7110 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7111 for (i = 0; i < nelem; i++)
7112 printf(gettext("0x%llx "),
7113 (u_longlong_t)val[i]);
7118 case DATA_TYPE_STRING_ARRAY: {
7122 (void) nvpair_value_string_array(nvp, &str, &nelem);
7123 for (i = 0; i < nelem; i++)
7124 printf(gettext("\"%s\" "),
7125 str[i] ? str[i] : "<NULL>");
7130 case DATA_TYPE_BOOLEAN_ARRAY:
7131 case DATA_TYPE_BYTE_ARRAY:
7132 case DATA_TYPE_DOUBLE:
7133 case DATA_TYPE_UNKNOWN:
7134 printf(gettext("<unknown>"));
7138 printf(gettext("\n"));
7143 zpool_do_events_next(ev_opts_t *opts)
7146 int zevent_fd, ret, dropped;
7148 zevent_fd = open(ZFS_DEV, O_RDWR);
7149 VERIFY(zevent_fd >= 0);
7151 if (!opts->scripted)
7152 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7155 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7156 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7157 if (ret || nvl == NULL)
7161 (void) printf(gettext("dropped %d events\n"), dropped);
7163 zpool_do_events_short(nvl);
7165 if (opts->verbose) {
7166 zpool_do_events_nvprint(nvl, 8);
7167 printf(gettext("\n"));
7169 (void) fflush(stdout);
7174 VERIFY(0 == close(zevent_fd));
7180 zpool_do_events_clear(ev_opts_t *opts)
7184 ret = zpool_events_clear(g_zfs, &count);
7186 (void) printf(gettext("cleared %d events\n"), count);
7192 * zpool events [-vfc]
7194 * Displays events logs by ZFS.
7197 zpool_do_events(int argc, char **argv)
7199 ev_opts_t opts = { 0 };
7204 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7219 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7228 ret = zpool_do_events_clear(&opts);
7230 ret = zpool_do_events_next(&opts);
7236 get_callback(zpool_handle_t *zhp, void *data)
7238 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7239 char value[MAXNAMELEN];
7240 zprop_source_t srctype;
7243 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7246 * Skip the special fake placeholder. This will also skip
7247 * over the name property when 'all' is specified.
7249 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7250 pl == cbp->cb_proplist)
7253 if (pl->pl_prop == ZPROP_INVAL &&
7254 (zpool_prop_feature(pl->pl_user_prop) ||
7255 zpool_prop_unsupported(pl->pl_user_prop))) {
7256 srctype = ZPROP_SRC_LOCAL;
7258 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7259 value, sizeof (value)) == 0) {
7260 zprop_print_one_property(zpool_get_name(zhp),
7261 cbp, pl->pl_user_prop, value, srctype,
7265 if (zpool_get_prop(zhp, pl->pl_prop, value,
7266 sizeof (value), &srctype, cbp->cb_literal) != 0)
7269 zprop_print_one_property(zpool_get_name(zhp), cbp,
7270 zpool_prop_to_name(pl->pl_prop), value, srctype,
7278 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7280 * -H Scripted mode. Don't display headers, and separate properties
7282 * -o List of columns to display. Defaults to
7283 * "name,property,value,source".
7284 * -p Display values in parsable (exact) format.
7286 * Get properties of pools in the system. Output space statistics
7287 * for each one as well as other attributes.
7290 zpool_do_get(int argc, char **argv)
7292 zprop_get_cbdata_t cb = { 0 };
7293 zprop_list_t fake_name = { 0 };
7298 cb.cb_first = B_TRUE;
7301 * Set up default columns and sources.
7303 cb.cb_sources = ZPROP_SRC_ALL;
7304 cb.cb_columns[0] = GET_COL_NAME;
7305 cb.cb_columns[1] = GET_COL_PROPERTY;
7306 cb.cb_columns[2] = GET_COL_VALUE;
7307 cb.cb_columns[3] = GET_COL_SOURCE;
7308 cb.cb_type = ZFS_TYPE_POOL;
7311 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7314 cb.cb_literal = B_TRUE;
7317 cb.cb_scripted = B_TRUE;
7320 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7322 while (*optarg != '\0') {
7323 static char *col_subopts[] =
7324 { "name", "property", "value", "source",
7327 if (i == ZFS_GET_NCOLS) {
7328 (void) fprintf(stderr, gettext("too "
7329 "many fields given to -o "
7334 switch (getsubopt(&optarg, col_subopts,
7337 cb.cb_columns[i++] = GET_COL_NAME;
7340 cb.cb_columns[i++] = GET_COL_PROPERTY;
7343 cb.cb_columns[i++] = GET_COL_VALUE;
7346 cb.cb_columns[i++] = GET_COL_SOURCE;
7350 (void) fprintf(stderr,
7351 gettext("\"all\" conflicts "
7352 "with specific fields "
7353 "given to -o option\n"));
7356 cb.cb_columns[0] = GET_COL_NAME;
7357 cb.cb_columns[1] = GET_COL_PROPERTY;
7358 cb.cb_columns[2] = GET_COL_VALUE;
7359 cb.cb_columns[3] = GET_COL_SOURCE;
7363 (void) fprintf(stderr,
7364 gettext("invalid column name "
7371 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7381 (void) fprintf(stderr, gettext("missing property "
7386 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7387 ZFS_TYPE_POOL) != 0)
7393 if (cb.cb_proplist != NULL) {
7394 fake_name.pl_prop = ZPOOL_PROP_NAME;
7395 fake_name.pl_width = strlen(gettext("NAME"));
7396 fake_name.pl_next = cb.cb_proplist;
7397 cb.cb_proplist = &fake_name;
7400 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7403 if (cb.cb_proplist == &fake_name)
7404 zprop_free_list(fake_name.pl_next);
7406 zprop_free_list(cb.cb_proplist);
7411 typedef struct set_cbdata {
7414 boolean_t cb_any_successful;
7418 set_callback(zpool_handle_t *zhp, void *data)
7421 set_cbdata_t *cb = (set_cbdata_t *)data;
7423 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7426 cb->cb_any_successful = B_TRUE;
7432 zpool_do_set(int argc, char **argv)
7434 set_cbdata_t cb = { 0 };
7437 if (argc > 1 && argv[1][0] == '-') {
7438 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7444 (void) fprintf(stderr, gettext("missing property=value "
7450 (void) fprintf(stderr, gettext("missing pool name\n"));
7455 (void) fprintf(stderr, gettext("too many pool names\n"));
7459 cb.cb_propname = argv[1];
7460 cb.cb_value = strchr(cb.cb_propname, '=');
7461 if (cb.cb_value == NULL) {
7462 (void) fprintf(stderr, gettext("missing value in "
7463 "property=value argument\n"));
7467 *(cb.cb_value) = '\0';
7470 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
7477 find_command_idx(char *command, int *idx)
7481 for (i = 0; i < NCOMMAND; i++) {
7482 if (command_table[i].name == NULL)
7485 if (strcmp(command, command_table[i].name) == 0) {
7494 main(int argc, char **argv)
7500 (void) setlocale(LC_ALL, "");
7501 (void) textdomain(TEXT_DOMAIN);
7504 dprintf_setup(&argc, argv);
7509 * Make sure the user has specified some command.
7512 (void) fprintf(stderr, gettext("missing command\n"));
7521 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
7524 if ((g_zfs = libzfs_init()) == NULL) {
7525 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7529 libzfs_print_on_error(g_zfs, B_TRUE);
7531 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7534 * Run the appropriate command.
7536 if (find_command_idx(cmdname, &i) == 0) {
7537 current_command = &command_table[i];
7538 ret = command_table[i].func(argc - 1, argv + 1);
7539 } else if (strchr(cmdname, '=')) {
7540 verify(find_command_idx("set", &i) == 0);
7541 current_command = &command_table[i];
7542 ret = command_table[i].func(argc, argv);
7543 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
7545 * 'freeze' is a vile debugging abomination, so we treat
7549 int fd = open(ZFS_DEV, O_RDWR);
7550 (void) strlcpy((void *)buf, argv[2], sizeof (buf));
7551 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
7553 (void) fprintf(stderr, gettext("unrecognized "
7554 "command '%s'\n"), cmdname);
7559 if (ret == 0 && log_history)
7560 (void) zpool_log_history(g_zfs, history_str);
7565 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7566 * for the purposes of running ::findleaks.
7568 if (getenv("ZFS_ABORT") != NULL) {
7569 (void) printf("dumping core by request\n");