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>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
52 #include <sys/fs/zfs.h>
54 #include <sys/systeminfo.h>
55 #include <sys/fm/fs/zfs.h>
56 #include <sys/fm/util.h>
57 #include <sys/fm/protocol.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/mount.h>
64 #include "zpool_util.h"
65 #include "zfs_comutil.h"
66 #include "zfeature_common.h"
68 #include "statcommon.h"
70 static int zpool_do_create(int, char **);
71 static int zpool_do_destroy(int, char **);
73 static int zpool_do_add(int, char **);
74 static int zpool_do_remove(int, char **);
75 static int zpool_do_labelclear(int, char **);
77 static int zpool_do_list(int, char **);
78 static int zpool_do_iostat(int, char **);
79 static int zpool_do_status(int, char **);
81 static int zpool_do_online(int, char **);
82 static int zpool_do_offline(int, char **);
83 static int zpool_do_clear(int, char **);
84 static int zpool_do_reopen(int, char **);
86 static int zpool_do_reguid(int, char **);
88 static int zpool_do_attach(int, char **);
89 static int zpool_do_detach(int, char **);
90 static int zpool_do_replace(int, char **);
91 static int zpool_do_split(int, char **);
93 static int zpool_do_scrub(int, char **);
95 static int zpool_do_import(int, char **);
96 static int zpool_do_export(int, char **);
98 static int zpool_do_upgrade(int, char **);
100 static int zpool_do_history(int, char **);
101 static int zpool_do_events(int, char **);
103 static int zpool_do_get(int, char **);
104 static int zpool_do_set(int, char **);
106 static int zpool_do_sync(int, char **);
109 * These libumem hooks provide a reasonable set of defaults for the allocator's
110 * debugging facilities.
115 _umem_debug_init(void)
117 return ("default,verbose"); /* $UMEM_DEBUG setting */
121 _umem_logging_init(void)
123 return ("fail,contents"); /* $UMEM_LOGGING setting */
158 * Flags for stats to display with "zpool iostats"
166 IOS_COUNT, /* always last element */
169 /* iostat_type entries as bitmasks */
170 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
171 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
172 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
173 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
174 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
176 /* Mask of all the histo bits */
177 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
180 * Lookup table for iostat flags to nvlist names. Basically a list
181 * of all the nvlists a flag requires. Also specifies the order in
182 * which data gets printed in zpool iostat.
184 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
186 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
187 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
188 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
193 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
194 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
197 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
198 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
200 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
203 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
204 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
205 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
206 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
207 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
210 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
211 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
212 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
213 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
214 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
215 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
216 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
217 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
218 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
219 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
225 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
226 * Right now, only one histo bit is ever set at one time, so we can
227 * just do a highbit64(a)
229 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
231 typedef struct zpool_command {
233 int (*func)(int, char **);
238 * Master command table. Each ZFS command has a name, associated function, and
239 * usage message. The usage messages need to be internationalized, so we have
240 * to have a function to return the usage message based on a command index.
242 * These commands are organized according to how they are displayed in the usage
243 * message. An empty command (one with a NULL name) indicates an empty line in
244 * the generic usage message.
246 static zpool_command_t command_table[] = {
247 { "create", zpool_do_create, HELP_CREATE },
248 { "destroy", zpool_do_destroy, HELP_DESTROY },
250 { "add", zpool_do_add, HELP_ADD },
251 { "remove", zpool_do_remove, HELP_REMOVE },
253 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
255 { "list", zpool_do_list, HELP_LIST },
256 { "iostat", zpool_do_iostat, HELP_IOSTAT },
257 { "status", zpool_do_status, HELP_STATUS },
259 { "online", zpool_do_online, HELP_ONLINE },
260 { "offline", zpool_do_offline, HELP_OFFLINE },
261 { "clear", zpool_do_clear, HELP_CLEAR },
262 { "reopen", zpool_do_reopen, HELP_REOPEN },
264 { "attach", zpool_do_attach, HELP_ATTACH },
265 { "detach", zpool_do_detach, HELP_DETACH },
266 { "replace", zpool_do_replace, HELP_REPLACE },
267 { "split", zpool_do_split, HELP_SPLIT },
269 { "scrub", zpool_do_scrub, HELP_SCRUB },
271 { "import", zpool_do_import, HELP_IMPORT },
272 { "export", zpool_do_export, HELP_EXPORT },
273 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
274 { "reguid", zpool_do_reguid, HELP_REGUID },
276 { "history", zpool_do_history, HELP_HISTORY },
277 { "events", zpool_do_events, HELP_EVENTS },
279 { "get", zpool_do_get, HELP_GET },
280 { "set", zpool_do_set, HELP_SET },
281 { "sync", zpool_do_sync, HELP_SYNC },
284 #define NCOMMAND (ARRAY_SIZE(command_table))
286 static zpool_command_t *current_command;
287 static char history_str[HIS_MAX_RECORD_LEN];
288 static boolean_t log_history = B_TRUE;
289 static uint_t timestamp_fmt = NODATE;
292 get_usage(zpool_help_t idx)
296 return (gettext("\tadd [-fgLnP] [-o property=value] "
297 "<pool> <vdev> ...\n"));
299 return (gettext("\tattach [-f] [-o property=value] "
300 "<pool> <device> <new-device>\n"));
302 return (gettext("\tclear [-nF] <pool> [device]\n"));
304 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
305 "\t [-O file-system-property=value] ... \n"
306 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
308 return (gettext("\tdestroy [-f] <pool>\n"));
310 return (gettext("\tdetach <pool> <device>\n"));
312 return (gettext("\texport [-af] <pool> ...\n"));
314 return (gettext("\thistory [-il] [<pool>] ...\n"));
316 return (gettext("\timport [-d dir] [-D]\n"
317 "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
319 "\timport [-o mntopts] [-o property=value] ... \n"
320 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
321 "[-R root] [-F [-n]] -a\n"
322 "\timport [-o mntopts] [-o property=value] ... \n"
323 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
324 "[-R root] [-F [-n]]\n"
325 "\t <pool | id> [newpool]\n"));
327 return (gettext("\tiostat [[[-c [script1,script2,...]"
328 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
329 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
330 " [interval [count]]\n"));
331 case HELP_LABELCLEAR:
332 return (gettext("\tlabelclear [-f] <vdev>\n"));
334 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
335 "[-T d|u] [pool] ... [interval [count]]\n"));
337 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
339 return (gettext("\tonline <pool> <device> ...\n"));
341 return (gettext("\treplace [-f] [-o property=value] "
342 "<pool> <device> [new-device]\n"));
344 return (gettext("\tremove <pool> <device> ...\n"));
346 return (gettext("\treopen [-n] <pool>\n"));
348 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
350 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
351 "[-T d|u] [pool] ... [interval [count]]\n"));
353 return (gettext("\tupgrade\n"
355 "\tupgrade [-V version] <-a | pool ...>\n"));
357 return (gettext("\tevents [-vHf [pool] | -c]\n"));
359 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
360 "<\"all\" | property[,...]> <pool> ...\n"));
362 return (gettext("\tset <property=value> <pool> \n"));
364 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
365 "\t [-o property=value] <pool> <newpool> "
366 "[<device> ...]\n"));
368 return (gettext("\treguid <pool>\n"));
370 return (gettext("\tsync [pool] ...\n"));
379 * Callback routine that will print out a pool property value.
382 print_prop_cb(int prop, void *cb)
386 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
388 if (zpool_prop_readonly(prop))
389 (void) fprintf(fp, " NO ");
391 (void) fprintf(fp, " YES ");
393 if (zpool_prop_values(prop) == NULL)
394 (void) fprintf(fp, "-\n");
396 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
402 * Display usage message. If we're inside a command, display only the usage for
403 * that command. Otherwise, iterate over the entire command table and display
404 * a complete usage message.
407 usage(boolean_t requested)
409 FILE *fp = requested ? stdout : stderr;
411 if (current_command == NULL) {
414 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
416 gettext("where 'command' is one of the following:\n\n"));
418 for (i = 0; i < NCOMMAND; i++) {
419 if (command_table[i].name == NULL)
420 (void) fprintf(fp, "\n");
422 (void) fprintf(fp, "%s",
423 get_usage(command_table[i].usage));
426 (void) fprintf(fp, gettext("usage:\n"));
427 (void) fprintf(fp, "%s", get_usage(current_command->usage));
430 if (current_command != NULL &&
431 ((strcmp(current_command->name, "set") == 0) ||
432 (strcmp(current_command->name, "get") == 0) ||
433 (strcmp(current_command->name, "list") == 0))) {
436 gettext("\nthe following properties are supported:\n"));
438 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
439 "PROPERTY", "EDIT", "VALUES");
441 /* Iterate over all properties */
442 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
445 (void) fprintf(fp, "\t%-15s ", "feature@...");
446 (void) fprintf(fp, "YES disabled | enabled | active\n");
448 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
449 "appended with a feature name.\nSee zpool-features(5).\n"));
453 * See comments at end of main().
455 if (getenv("ZFS_ABORT") != NULL) {
456 (void) printf("dumping core by request\n");
460 exit(requested ? 0 : 2);
464 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
465 boolean_t print_logs, int name_flags)
472 (void) printf("\t%*s%s\n", indent, "", name);
474 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
475 &child, &children) != 0)
478 for (c = 0; c < children; c++) {
479 uint64_t is_log = B_FALSE;
481 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
483 if ((is_log && !print_logs) || (!is_log && print_logs))
486 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
487 print_vdev_tree(zhp, vname, child[c], indent + 2,
488 B_FALSE, name_flags);
494 prop_list_contains_feature(nvlist_t *proplist)
497 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
498 nvp = nvlist_next_nvpair(proplist, nvp)) {
499 if (zpool_prop_feature(nvpair_name(nvp)))
506 * Add a property pair (name, string-value) into a property nvlist.
509 add_prop_list(const char *propname, char *propval, nvlist_t **props,
512 zpool_prop_t prop = ZPROP_INVAL;
518 if (*props == NULL &&
519 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
520 (void) fprintf(stderr,
521 gettext("internal error: out of memory\n"));
528 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
530 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
531 !zpool_prop_feature(propname)) {
532 (void) fprintf(stderr, gettext("property '%s' is "
533 "not a valid pool property\n"), propname);
538 * feature@ properties and version should not be specified
541 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
542 nvlist_exists(proplist, vname)) ||
543 (prop == ZPOOL_PROP_VERSION &&
544 prop_list_contains_feature(proplist))) {
545 (void) fprintf(stderr, gettext("'feature@' and "
546 "'version' properties cannot be specified "
552 if (zpool_prop_feature(propname))
555 normnm = zpool_prop_to_name(prop);
557 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
558 normnm = zfs_prop_to_name(fprop);
564 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
565 prop != ZPOOL_PROP_CACHEFILE) {
566 (void) fprintf(stderr, gettext("property '%s' "
567 "specified multiple times\n"), propname);
571 if (nvlist_add_string(proplist, normnm, propval) != 0) {
572 (void) fprintf(stderr, gettext("internal "
573 "error: out of memory\n"));
581 * Set a default property pair (name, string-value) in a property nvlist
584 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
589 if (nvlist_lookup_string(*props, propname, &pval) == 0)
592 return (add_prop_list(propname, propval, props, B_TRUE));
596 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
598 * -f Force addition of devices, even if they appear in use
599 * -g Display guid for individual vdev name.
600 * -L Follow links when resolving vdev path name.
601 * -n Do not add the devices, but display the resulting layout if
602 * they were to be added.
603 * -o Set property=value.
604 * -P Display full path for vdev name.
606 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
607 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
611 zpool_do_add(int argc, char **argv)
613 boolean_t force = B_FALSE;
614 boolean_t dryrun = B_FALSE;
622 nvlist_t *props = NULL;
626 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
632 name_flags |= VDEV_NAME_GUID;
635 name_flags |= VDEV_NAME_FOLLOW_LINKS;
641 if ((propval = strchr(optarg, '=')) == NULL) {
642 (void) fprintf(stderr, gettext("missing "
643 "'=' for -o option\n"));
649 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
650 (add_prop_list(optarg, propval, &props, B_TRUE)))
654 name_flags |= VDEV_NAME_PATH;
657 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
666 /* get pool name and check number of arguments */
668 (void) fprintf(stderr, gettext("missing pool name argument\n"));
672 (void) fprintf(stderr, gettext("missing vdev specification\n"));
681 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
684 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
685 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
691 /* unless manually specified use "ashift" pool property (if set) */
692 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
695 char strval[ZPOOL_MAXPROPLEN];
697 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
698 if (src != ZPROP_SRC_DEFAULT) {
699 (void) sprintf(strval, "%" PRId32, intval);
700 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
701 &props, B_TRUE) == 0);
705 /* pass off to get_vdev_spec for processing */
706 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
708 if (nvroot == NULL) {
714 nvlist_t *poolnvroot;
716 uint_t l2children, c;
718 boolean_t hadcache = B_FALSE;
720 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
723 (void) printf(gettext("would update '%s' to the following "
724 "configuration:\n"), zpool_get_name(zhp));
726 /* print original main pool and new tree */
727 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
729 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
731 /* Do the same for the logs */
732 if (num_logs(poolnvroot) > 0) {
733 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
735 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
737 } else if (num_logs(nvroot) > 0) {
738 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
742 /* Do the same for the caches */
743 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
744 &l2child, &l2children) == 0 && l2children) {
746 (void) printf(gettext("\tcache\n"));
747 for (c = 0; c < l2children; c++) {
748 vname = zpool_vdev_name(g_zfs, NULL,
749 l2child[c], name_flags);
750 (void) printf("\t %s\n", vname);
754 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
755 &l2child, &l2children) == 0 && l2children) {
757 (void) printf(gettext("\tcache\n"));
758 for (c = 0; c < l2children; c++) {
759 vname = zpool_vdev_name(g_zfs, NULL,
760 l2child[c], name_flags);
761 (void) printf("\t %s\n", vname);
768 ret = (zpool_add(zhp, nvroot) != 0);
779 * zpool remove <pool> <vdev> ...
781 * Removes the given vdev from the pool. Currently, this supports removing
782 * spares, cache, and log devices from the pool.
785 zpool_do_remove(int argc, char **argv)
789 zpool_handle_t *zhp = NULL;
794 /* get pool name and check number of arguments */
796 (void) fprintf(stderr, gettext("missing pool name argument\n"));
800 (void) fprintf(stderr, gettext("missing device\n"));
806 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
809 for (i = 1; i < argc; i++) {
810 if (zpool_vdev_remove(zhp, argv[i]) != 0)
819 * zpool labelclear [-f] <vdev>
821 * -f Force clearing the label for the vdevs which are members of
822 * the exported or foreign pools.
824 * Verifies that the vdev is not active and zeros out the label information
828 zpool_do_labelclear(int argc, char **argv)
830 char vdev[MAXPATHLEN];
833 int c, fd = -1, ret = 0;
836 boolean_t inuse = B_FALSE;
837 boolean_t force = B_FALSE;
840 while ((c = getopt(argc, argv, "f")) != -1) {
846 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
857 (void) fprintf(stderr, gettext("missing vdev name\n"));
861 (void) fprintf(stderr, gettext("too many arguments\n"));
866 * Check if we were given absolute path and use it as is.
867 * Otherwise if the provided vdev name doesn't point to a file,
868 * try prepending expected disk paths and partition numbers.
870 (void) strlcpy(vdev, argv[0], sizeof (vdev));
871 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
874 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
875 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
876 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
880 if (error || (stat(vdev, &st) != 0)) {
881 (void) fprintf(stderr, gettext(
882 "failed to find device %s, try specifying absolute "
883 "path instead\n"), argv[0]);
888 if ((fd = open(vdev, O_RDWR)) < 0) {
889 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
890 vdev, strerror(errno));
894 if (ioctl(fd, BLKFLSBUF) != 0)
895 (void) fprintf(stderr, gettext("failed to invalidate "
896 "cache for %s: %s\n"), vdev, strerror(errno));
898 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
899 (void) fprintf(stderr,
900 gettext("failed to check state for %s\n"), vdev);
906 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
908 (void) fprintf(stderr,
909 gettext("failed to check state for %s\n"), vdev);
919 case POOL_STATE_ACTIVE:
920 case POOL_STATE_SPARE:
921 case POOL_STATE_L2CACHE:
922 (void) fprintf(stderr, gettext(
923 "%s is a member (%s) of pool \"%s\"\n"),
924 vdev, zpool_pool_state_to_name(state), name);
928 case POOL_STATE_EXPORTED:
931 (void) fprintf(stderr, gettext(
932 "use '-f' to override the following error:\n"
933 "%s is a member of exported pool \"%s\"\n"),
938 case POOL_STATE_POTENTIALLY_ACTIVE:
941 (void) fprintf(stderr, gettext(
942 "use '-f' to override the following error:\n"
943 "%s is a member of potentially active pool \"%s\"\n"),
948 case POOL_STATE_DESTROYED:
949 /* inuse should never be set for a destroyed pool */
955 ret = zpool_clear_label(fd);
957 (void) fprintf(stderr,
958 gettext("failed to clear label for %s\n"), vdev);
969 * zpool create [-fnd] [-o property=value] ...
970 * [-O file-system-property=value] ...
971 * [-R root] [-m mountpoint] <pool> <dev> ...
973 * -f Force creation, even if devices appear in use
974 * -n Do not create the pool, but display the resulting layout if it
975 * were to be created.
976 * -R Create a pool under an alternate root
977 * -m Set default mountpoint for the root dataset. By default it's
979 * -o Set property=value.
980 * -o Set feature@feature=enabled|disabled.
981 * -d Don't automatically enable all supported pool features
982 * (individual features can be enabled with -o).
983 * -O Set fsproperty=value in the pool's root file system
985 * Creates the named pool according to the given vdev specification. The
986 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
987 * we get the nvlist back from get_vdev_spec(), we either print out the contents
988 * (if '-n' was specified), or pass it to libzfs to do the creation.
991 zpool_do_create(int argc, char **argv)
993 boolean_t force = B_FALSE;
994 boolean_t dryrun = B_FALSE;
995 boolean_t enable_all_pool_feat = B_TRUE;
997 nvlist_t *nvroot = NULL;
1001 char *altroot = NULL;
1002 char *mountpoint = NULL;
1003 nvlist_t *fsprops = NULL;
1004 nvlist_t *props = NULL;
1008 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1017 enable_all_pool_feat = B_FALSE;
1021 if (add_prop_list(zpool_prop_to_name(
1022 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1024 if (add_prop_list_default(zpool_prop_to_name(
1025 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1029 /* Equivalent to -O mountpoint=optarg */
1030 mountpoint = optarg;
1033 if ((propval = strchr(optarg, '=')) == NULL) {
1034 (void) fprintf(stderr, gettext("missing "
1035 "'=' for -o option\n"));
1041 if (add_prop_list(optarg, propval, &props, B_TRUE))
1045 * If the user is creating a pool that doesn't support
1046 * feature flags, don't enable any features.
1048 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1052 ver = strtoull(propval, &end, 10);
1054 ver < SPA_VERSION_FEATURES) {
1055 enable_all_pool_feat = B_FALSE;
1058 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1062 if ((propval = strchr(optarg, '=')) == NULL) {
1063 (void) fprintf(stderr, gettext("missing "
1064 "'=' for -O option\n"));
1071 * Mountpoints are checked and then added later.
1072 * Uniquely among properties, they can be specified
1073 * more than once, to avoid conflict with -m.
1075 if (0 == strcmp(optarg,
1076 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1077 mountpoint = propval;
1078 } else if (add_prop_list(optarg, propval, &fsprops,
1085 * Sanity check temporary pool name.
1087 if (strchr(optarg, '/') != NULL) {
1088 (void) fprintf(stderr, gettext("cannot create "
1089 "'%s': invalid character '/' in temporary "
1091 (void) fprintf(stderr, gettext("use 'zfs "
1092 "create' to create a dataset\n"));
1096 if (add_prop_list(zpool_prop_to_name(
1097 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1099 if (add_prop_list_default(zpool_prop_to_name(
1100 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1105 (void) fprintf(stderr, gettext("missing argument for "
1106 "'%c' option\n"), optopt);
1109 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1118 /* get pool name and check number of arguments */
1120 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1124 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1131 * As a special case, check for use of '/' in the name, and direct the
1132 * user to use 'zfs create' instead.
1134 if (strchr(poolname, '/') != NULL) {
1135 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1136 "character '/' in pool name\n"), poolname);
1137 (void) fprintf(stderr, gettext("use 'zfs create' to "
1138 "create a dataset\n"));
1142 /* pass off to get_vdev_spec for bulk processing */
1143 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1144 argc - 1, argv + 1);
1148 /* make_root_vdev() allows 0 toplevel children if there are spares */
1149 if (!zfs_allocatable_devs(nvroot)) {
1150 (void) fprintf(stderr, gettext("invalid vdev "
1151 "specification: at least one toplevel vdev must be "
1156 if (altroot != NULL && altroot[0] != '/') {
1157 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1158 "must be an absolute path\n"), altroot);
1163 * Check the validity of the mountpoint and direct the user to use the
1164 * '-m' mountpoint option if it looks like its in use.
1166 if (mountpoint == NULL ||
1167 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1168 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1169 char buf[MAXPATHLEN];
1172 if (mountpoint && mountpoint[0] != '/') {
1173 (void) fprintf(stderr, gettext("invalid mountpoint "
1174 "'%s': must be an absolute path, 'legacy', or "
1175 "'none'\n"), mountpoint);
1179 if (mountpoint == NULL) {
1180 if (altroot != NULL)
1181 (void) snprintf(buf, sizeof (buf), "%s/%s",
1184 (void) snprintf(buf, sizeof (buf), "/%s",
1187 if (altroot != NULL)
1188 (void) snprintf(buf, sizeof (buf), "%s%s",
1189 altroot, mountpoint);
1191 (void) snprintf(buf, sizeof (buf), "%s",
1195 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1196 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1197 "%s\n"), buf, strerror(errno));
1198 (void) fprintf(stderr, gettext("use '-m' "
1199 "option to provide a different default\n"));
1204 while (count < 3 && readdir(dirp) != NULL)
1206 (void) closedir(dirp);
1209 (void) fprintf(stderr, gettext("mountpoint "
1210 "'%s' exists and is not empty\n"), buf);
1211 (void) fprintf(stderr, gettext("use '-m' "
1212 "option to provide a "
1213 "different default\n"));
1220 * Now that the mountpoint's validity has been checked, ensure that
1221 * the property is set appropriately prior to creating the pool.
1223 if (mountpoint != NULL) {
1224 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1225 mountpoint, &fsprops, B_FALSE);
1233 * For a dry run invocation, print out a basic message and run
1234 * through all the vdevs in the list and print out in an
1235 * appropriate hierarchy.
1237 (void) printf(gettext("would create '%s' with the "
1238 "following layout:\n\n"), poolname);
1240 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1241 if (num_logs(nvroot) > 0)
1242 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1247 * Hand off to libzfs.
1250 for (i = 0; i < SPA_FEATURES; i++) {
1251 char propname[MAXPATHLEN];
1253 zfeature_info_t *feat = &spa_feature_table[i];
1255 (void) snprintf(propname, sizeof (propname),
1256 "feature@%s", feat->fi_uname);
1259 * Only features contained in props will be enabled:
1260 * remove from the nvlist every ZFS_FEATURE_DISABLED
1261 * value and add every missing ZFS_FEATURE_ENABLED if
1262 * enable_all_pool_feat is set.
1264 if (!nvlist_lookup_string(props, propname, &propval)) {
1265 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1266 (void) nvlist_remove_all(props,
1268 } else if (enable_all_pool_feat) {
1269 ret = add_prop_list(propname,
1270 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1277 if (zpool_create(g_zfs, poolname,
1278 nvroot, props, fsprops) == 0) {
1279 zfs_handle_t *pool = zfs_open(g_zfs,
1280 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1282 if (zfs_mount(pool, NULL, 0) == 0)
1283 ret = zfs_shareall(pool);
1286 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1287 (void) fprintf(stderr, gettext("pool name may have "
1293 nvlist_free(nvroot);
1294 nvlist_free(fsprops);
1298 nvlist_free(fsprops);
1305 * zpool destroy <pool>
1307 * -f Forcefully unmount any datasets
1309 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1312 zpool_do_destroy(int argc, char **argv)
1314 boolean_t force = B_FALSE;
1317 zpool_handle_t *zhp;
1321 while ((c = getopt(argc, argv, "f")) != -1) {
1327 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1336 /* check arguments */
1338 (void) fprintf(stderr, gettext("missing pool argument\n"));
1342 (void) fprintf(stderr, gettext("too many arguments\n"));
1348 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1350 * As a special case, check for use of '/' in the name, and
1351 * direct the user to use 'zfs destroy' instead.
1353 if (strchr(pool, '/') != NULL)
1354 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1355 "destroy a dataset\n"));
1359 if (zpool_disable_datasets(zhp, force) != 0) {
1360 (void) fprintf(stderr, gettext("could not destroy '%s': "
1361 "could not unmount datasets\n"), zpool_get_name(zhp));
1366 /* The history must be logged as part of the export */
1367 log_history = B_FALSE;
1369 ret = (zpool_destroy(zhp, history_str) != 0);
1376 typedef struct export_cbdata {
1378 boolean_t hardforce;
1385 zpool_export_one(zpool_handle_t *zhp, void *data)
1387 export_cbdata_t *cb = data;
1389 if (zpool_disable_datasets(zhp, cb->force) != 0)
1392 /* The history must be logged as part of the export */
1393 log_history = B_FALSE;
1395 if (cb->hardforce) {
1396 if (zpool_export_force(zhp, history_str) != 0)
1398 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1406 * zpool export [-f] <pool> ...
1408 * -a Export all pools
1409 * -f Forcefully unmount datasets
1411 * Export the given pools. By default, the command will attempt to cleanly
1412 * unmount any active datasets within the pool. If the '-f' flag is specified,
1413 * then the datasets will be forcefully unmounted.
1416 zpool_do_export(int argc, char **argv)
1419 boolean_t do_all = B_FALSE;
1420 boolean_t force = B_FALSE;
1421 boolean_t hardforce = B_FALSE;
1425 while ((c = getopt(argc, argv, "afF")) != -1) {
1437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1444 cb.hardforce = hardforce;
1450 (void) fprintf(stderr, gettext("too many arguments\n"));
1454 return (for_each_pool(argc, argv, B_TRUE, NULL,
1455 zpool_export_one, &cb));
1458 /* check arguments */
1460 (void) fprintf(stderr, gettext("missing pool argument\n"));
1464 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1470 * Given a vdev configuration, determine the maximum width needed for the device
1474 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1482 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1483 if (strlen(name) + depth > max)
1484 max = strlen(name) + depth;
1488 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1489 &child, &children) == 0) {
1490 for (c = 0; c < children; c++)
1491 if ((ret = max_width(zhp, child[c], depth + 2,
1492 max, name_flags)) > max)
1496 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1497 &child, &children) == 0) {
1498 for (c = 0; c < children; c++)
1499 if ((ret = max_width(zhp, child[c], depth + 2,
1500 max, name_flags)) > max)
1504 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1505 &child, &children) == 0) {
1506 for (c = 0; c < children; c++)
1507 if ((ret = max_width(zhp, child[c], depth + 2,
1508 max, name_flags)) > max)
1515 typedef struct spare_cbdata {
1517 zpool_handle_t *cb_zhp;
1521 find_vdev(nvlist_t *nv, uint64_t search)
1527 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1531 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1532 &child, &children) == 0) {
1533 for (c = 0; c < children; c++)
1534 if (find_vdev(child[c], search))
1542 find_spare(zpool_handle_t *zhp, void *data)
1544 spare_cbdata_t *cbp = data;
1545 nvlist_t *config, *nvroot;
1547 config = zpool_get_config(zhp, NULL);
1548 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1551 if (find_vdev(nvroot, cbp->cb_guid)) {
1560 typedef struct status_cbdata {
1564 boolean_t cb_allpools;
1565 boolean_t cb_verbose;
1566 boolean_t cb_explain;
1568 boolean_t cb_dedup_stats;
1569 boolean_t cb_print_status;
1570 vdev_cmd_data_list_t *vcdl;
1573 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1575 is_blank_str(char *str)
1577 while (str != NULL && *str != '\0') {
1585 /* Print command output lines for specific vdev in a specific pool */
1587 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1589 vdev_cmd_data_t *data;
1593 for (i = 0; i < vcdl->count; i++) {
1594 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1595 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1596 /* Not the vdev we're looking for */
1600 data = &vcdl->data[i];
1601 /* Print out all the output values for this vdev */
1602 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1604 /* Does this vdev have values for this column? */
1605 for (int k = 0; k < data->cols_cnt; k++) {
1606 if (strcmp(data->cols[k],
1607 vcdl->uniq_cols[j]) == 0) {
1608 /* yes it does, record the value */
1609 val = data->lines[k];
1614 * Mark empty values with dashes to make output
1617 if (is_blank_str(val))
1620 printf("%*s", vcdl->uniq_cols_width[j], val);
1621 if (j < vcdl->uniq_cols_cnt - 1)
1625 /* Print out any values that aren't in a column at the end */
1626 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1627 /* Did we have any columns? If so print a spacer. */
1628 if (vcdl->uniq_cols_cnt > 0)
1631 val = data->lines[j];
1632 printf("%s", val ? val : "");
1639 * Print out configuration state as requested by status_callback.
1642 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1643 nvlist_t *nv, int depth, boolean_t isspare)
1647 pool_scan_stat_t *ps = NULL;
1649 char rbuf[6], wbuf[6], cbuf[6];
1651 uint64_t notpresent;
1652 spare_cbdata_t spare_cb;
1656 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1657 &child, &children) != 0)
1660 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1661 (uint64_t **)&vs, &c) == 0);
1663 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1666 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1669 if (vs->vs_aux == VDEV_AUX_SPARED)
1671 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1675 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1679 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1680 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1681 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1682 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1685 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1686 ¬present) == 0) {
1687 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1688 (void) printf(" was %s", path);
1689 } else if (vs->vs_aux != 0) {
1692 switch (vs->vs_aux) {
1693 case VDEV_AUX_OPEN_FAILED:
1694 (void) printf(gettext("cannot open"));
1697 case VDEV_AUX_BAD_GUID_SUM:
1698 (void) printf(gettext("missing device"));
1701 case VDEV_AUX_NO_REPLICAS:
1702 (void) printf(gettext("insufficient replicas"));
1705 case VDEV_AUX_VERSION_NEWER:
1706 (void) printf(gettext("newer version"));
1709 case VDEV_AUX_UNSUP_FEAT:
1710 (void) printf(gettext("unsupported feature(s)"));
1713 case VDEV_AUX_SPARED:
1714 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1715 &spare_cb.cb_guid) == 0);
1716 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1717 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1718 zpool_get_name(zhp)) == 0)
1719 (void) printf(gettext("currently in "
1722 (void) printf(gettext("in use by "
1724 zpool_get_name(spare_cb.cb_zhp));
1725 zpool_close(spare_cb.cb_zhp);
1727 (void) printf(gettext("currently in use"));
1731 case VDEV_AUX_ERR_EXCEEDED:
1732 (void) printf(gettext("too many errors"));
1735 case VDEV_AUX_IO_FAILURE:
1736 (void) printf(gettext("experienced I/O failures"));
1739 case VDEV_AUX_BAD_LOG:
1740 (void) printf(gettext("bad intent log"));
1743 case VDEV_AUX_EXTERNAL:
1744 (void) printf(gettext("external device fault"));
1747 case VDEV_AUX_SPLIT_POOL:
1748 (void) printf(gettext("split into new pool"));
1751 case VDEV_AUX_ACTIVE:
1752 (void) printf(gettext("currently in use"));
1756 (void) printf(gettext("corrupted data"));
1761 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1762 (uint64_t **)&ps, &c);
1764 if (ps && ps->pss_state == DSS_SCANNING &&
1765 vs->vs_scan_processed != 0 && children == 0) {
1766 (void) printf(gettext(" (%s)"),
1767 (ps->pss_func == POOL_SCAN_RESILVER) ?
1768 "resilvering" : "repairing");
1771 if (cb->vcdl != NULL) {
1772 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1774 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1778 (void) printf("\n");
1780 for (c = 0; c < children; c++) {
1781 uint64_t islog = B_FALSE, ishole = B_FALSE;
1783 /* Don't print logs or holes here */
1784 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1786 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1788 if (islog || ishole)
1790 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1791 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1792 print_status_config(zhp, cb, vname, child[c], depth + 2,
1799 * Print the configuration of an exported pool. Iterate over all vdevs in the
1800 * pool, printing out the name and status for each one.
1803 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1811 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1812 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1813 strcmp(type, VDEV_TYPE_HOLE) == 0)
1816 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1817 (uint64_t **)&vs, &c) == 0);
1819 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1820 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1822 if (vs->vs_aux != 0) {
1825 switch (vs->vs_aux) {
1826 case VDEV_AUX_OPEN_FAILED:
1827 (void) printf(gettext("cannot open"));
1830 case VDEV_AUX_BAD_GUID_SUM:
1831 (void) printf(gettext("missing device"));
1834 case VDEV_AUX_NO_REPLICAS:
1835 (void) printf(gettext("insufficient replicas"));
1838 case VDEV_AUX_VERSION_NEWER:
1839 (void) printf(gettext("newer version"));
1842 case VDEV_AUX_UNSUP_FEAT:
1843 (void) printf(gettext("unsupported feature(s)"));
1846 case VDEV_AUX_ERR_EXCEEDED:
1847 (void) printf(gettext("too many errors"));
1850 case VDEV_AUX_ACTIVE:
1851 (void) printf(gettext("currently in use"));
1855 (void) printf(gettext("corrupted data"));
1859 (void) printf("\n");
1861 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1862 &child, &children) != 0)
1865 for (c = 0; c < children; c++) {
1866 uint64_t is_log = B_FALSE;
1868 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1873 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1874 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1875 print_import_config(cb, vname, child[c], depth + 2);
1879 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1880 &child, &children) == 0) {
1881 (void) printf(gettext("\tcache\n"));
1882 for (c = 0; c < children; c++) {
1883 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1885 (void) printf("\t %s\n", vname);
1890 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1891 &child, &children) == 0) {
1892 (void) printf(gettext("\tspares\n"));
1893 for (c = 0; c < children; c++) {
1894 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1896 (void) printf("\t %s\n", vname);
1904 * Logs are recorded as top level vdevs in the main pool child array
1905 * but with "is_log" set to 1. We use either print_status_config() or
1906 * print_import_config() to print the top level logs then any log
1907 * children (eg mirrored slogs) are printed recursively - which
1908 * works because only the top level vdev is marked "is_log"
1911 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1916 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1920 (void) printf(gettext("\tlogs\n"));
1922 for (c = 0; c < children; c++) {
1923 uint64_t is_log = B_FALSE;
1926 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1930 name = zpool_vdev_name(g_zfs, zhp, child[c],
1931 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1932 if (cb->cb_print_status)
1933 print_status_config(zhp, cb, name, child[c], 2,
1936 print_import_config(cb, name, child[c], 2);
1942 * Display the status for the given pool.
1945 show_import(nvlist_t *config)
1947 uint64_t pool_state;
1951 uint64_t hostid = 0;
1953 char *hostname = "unknown";
1954 nvlist_t *nvroot, *nvinfo;
1955 zpool_status_t reason;
1956 zpool_errata_t errata;
1960 status_cbdata_t cb = { 0 };
1962 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1964 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1966 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1968 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1971 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1972 (uint64_t **)&vs, &vsc) == 0);
1973 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1975 reason = zpool_import_status(config, &msgid, &errata);
1977 (void) printf(gettext(" pool: %s\n"), name);
1978 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1979 (void) printf(gettext(" state: %s"), health);
1980 if (pool_state == POOL_STATE_DESTROYED)
1981 (void) printf(gettext(" (DESTROYED)"));
1982 (void) printf("\n");
1985 case ZPOOL_STATUS_MISSING_DEV_R:
1986 case ZPOOL_STATUS_MISSING_DEV_NR:
1987 case ZPOOL_STATUS_BAD_GUID_SUM:
1988 (void) printf(gettext(" status: One or more devices are "
1989 "missing from the system.\n"));
1992 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1993 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1994 (void) printf(gettext(" status: One or more devices contains "
1995 "corrupted data.\n"));
1998 case ZPOOL_STATUS_CORRUPT_DATA:
2000 gettext(" status: The pool data is corrupted.\n"));
2003 case ZPOOL_STATUS_OFFLINE_DEV:
2004 (void) printf(gettext(" status: One or more devices "
2005 "are offlined.\n"));
2008 case ZPOOL_STATUS_CORRUPT_POOL:
2009 (void) printf(gettext(" status: The pool metadata is "
2013 case ZPOOL_STATUS_VERSION_OLDER:
2014 (void) printf(gettext(" status: The pool is formatted using a "
2015 "legacy on-disk version.\n"));
2018 case ZPOOL_STATUS_VERSION_NEWER:
2019 (void) printf(gettext(" status: The pool is formatted using an "
2020 "incompatible version.\n"));
2023 case ZPOOL_STATUS_FEAT_DISABLED:
2024 (void) printf(gettext(" status: Some supported features are "
2025 "not enabled on the pool.\n"));
2028 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2029 (void) printf(gettext("status: The pool uses the following "
2030 "feature(s) not supported on this system:\n"));
2031 zpool_print_unsup_feat(config);
2034 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2035 (void) printf(gettext("status: The pool can only be accessed "
2036 "in read-only mode on this system. It\n\tcannot be "
2037 "accessed in read-write mode because it uses the "
2038 "following\n\tfeature(s) not supported on this system:\n"));
2039 zpool_print_unsup_feat(config);
2042 case ZPOOL_STATUS_HOSTID_ACTIVE:
2043 (void) printf(gettext(" status: The pool is currently "
2044 "imported by another system.\n"));
2047 case ZPOOL_STATUS_HOSTID_REQUIRED:
2048 (void) printf(gettext(" status: The pool has the "
2049 "multihost property on. It cannot\n\tbe safely imported "
2050 "when the system hostid is not set.\n"));
2053 case ZPOOL_STATUS_HOSTID_MISMATCH:
2054 (void) printf(gettext(" status: The pool was last accessed by "
2055 "another system.\n"));
2058 case ZPOOL_STATUS_FAULTED_DEV_R:
2059 case ZPOOL_STATUS_FAULTED_DEV_NR:
2060 (void) printf(gettext(" status: One or more devices are "
2064 case ZPOOL_STATUS_BAD_LOG:
2065 (void) printf(gettext(" status: An intent log record cannot be "
2069 case ZPOOL_STATUS_RESILVERING:
2070 (void) printf(gettext(" status: One or more devices were being "
2074 case ZPOOL_STATUS_ERRATA:
2075 (void) printf(gettext(" status: Errata #%d detected.\n"),
2081 * No other status can be seen when importing pools.
2083 assert(reason == ZPOOL_STATUS_OK);
2087 * Print out an action according to the overall state of the pool.
2089 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2090 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2091 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2092 (void) printf(gettext(" action: The pool can be "
2093 "imported using its name or numeric identifier, "
2094 "though\n\tsome features will not be available "
2095 "without an explicit 'zpool upgrade'.\n"));
2096 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2097 (void) printf(gettext(" action: The pool can be "
2098 "imported using its name or numeric "
2099 "identifier and\n\tthe '-f' flag.\n"));
2100 } else if (reason == ZPOOL_STATUS_ERRATA) {
2102 case ZPOOL_ERRATA_NONE:
2105 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2106 (void) printf(gettext(" action: The pool can "
2107 "be imported using its name or numeric "
2108 "identifier,\n\thowever there is a compat"
2109 "ibility issue which should be corrected"
2110 "\n\tby running 'zpool scrub'\n"));
2113 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2114 (void) printf(gettext(" action: The pool can"
2115 "not be imported with this version of ZFS "
2116 "due to\n\tan active asynchronous destroy. "
2117 "Revert to an earlier version\n\tand "
2118 "allow the destroy to complete before "
2124 * All errata must contain an action message.
2129 (void) printf(gettext(" action: The pool can be "
2130 "imported using its name or numeric "
2133 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2134 (void) printf(gettext(" action: The pool can be imported "
2135 "despite missing or damaged devices. The\n\tfault "
2136 "tolerance of the pool may be compromised if imported.\n"));
2139 case ZPOOL_STATUS_VERSION_NEWER:
2140 (void) printf(gettext(" action: The pool cannot be "
2141 "imported. Access the pool on a system running "
2142 "newer\n\tsoftware, or recreate the pool from "
2145 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2146 (void) printf(gettext("action: The pool cannot be "
2147 "imported. Access the pool on a system that "
2148 "supports\n\tthe required feature(s), or recreate "
2149 "the pool from backup.\n"));
2151 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2152 (void) printf(gettext("action: The pool cannot be "
2153 "imported in read-write mode. Import the pool "
2155 "\t\"-o readonly=on\", access the pool on a system "
2156 "that supports the\n\trequired feature(s), or "
2157 "recreate the pool from backup.\n"));
2159 case ZPOOL_STATUS_MISSING_DEV_R:
2160 case ZPOOL_STATUS_MISSING_DEV_NR:
2161 case ZPOOL_STATUS_BAD_GUID_SUM:
2162 (void) printf(gettext(" action: The pool cannot be "
2163 "imported. Attach the missing\n\tdevices and try "
2166 case ZPOOL_STATUS_HOSTID_ACTIVE:
2167 VERIFY0(nvlist_lookup_nvlist(config,
2168 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2170 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2171 hostname = fnvlist_lookup_string(nvinfo,
2172 ZPOOL_CONFIG_MMP_HOSTNAME);
2174 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2175 hostid = fnvlist_lookup_uint64(nvinfo,
2176 ZPOOL_CONFIG_MMP_HOSTID);
2178 (void) printf(gettext(" action: The pool must be "
2179 "exported from %s (hostid=%lx)\n\tbefore it "
2180 "can be safely imported.\n"), hostname,
2181 (unsigned long) hostid);
2183 case ZPOOL_STATUS_HOSTID_REQUIRED:
2184 (void) printf(gettext(" action: Set a unique system "
2185 "hostid with the zgenhostid(8) command.\n"));
2188 (void) printf(gettext(" action: The pool cannot be "
2189 "imported due to damaged devices or data.\n"));
2193 /* Print the comment attached to the pool. */
2194 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2195 (void) printf(gettext("comment: %s\n"), comment);
2198 * If the state is "closed" or "can't open", and the aux state
2199 * is "corrupt data":
2201 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2202 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2203 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2204 if (pool_state == POOL_STATE_DESTROYED)
2205 (void) printf(gettext("\tThe pool was destroyed, "
2206 "but can be imported using the '-Df' flags.\n"));
2207 else if (pool_state != POOL_STATE_EXPORTED)
2208 (void) printf(gettext("\tThe pool may be active on "
2209 "another system, but can be imported using\n\t"
2210 "the '-f' flag.\n"));
2214 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2217 (void) printf(gettext(" config:\n\n"));
2219 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2221 if (cb.cb_namewidth < 10)
2222 cb.cb_namewidth = 10;
2224 print_import_config(&cb, name, nvroot, 0);
2225 if (num_logs(nvroot) > 0)
2226 print_logs(NULL, &cb, nvroot);
2228 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2229 (void) printf(gettext("\n\tAdditional devices are known to "
2230 "be part of this pool, though their\n\texact "
2231 "configuration cannot be determined.\n"));
2236 zfs_force_import_required(nvlist_t *config)
2239 uint64_t hostid = 0;
2242 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2243 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2245 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2248 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2249 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2250 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2251 ZPOOL_CONFIG_MMP_STATE);
2253 if (mmp_state != MMP_STATE_INACTIVE)
2261 * Perform the import for the given configuration. This passes the heavy
2262 * lifting off to zpool_import_props(), and then mounts the datasets contained
2266 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2267 nvlist_t *props, int flags)
2270 zpool_handle_t *zhp;
2275 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2276 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2277 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2279 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2280 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2281 "is formatted using an unsupported ZFS version\n"), name);
2283 } else if (zfs_force_import_required(config) &&
2284 !(flags & ZFS_IMPORT_ANY_HOST)) {
2285 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2288 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2289 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2290 mmp_state = fnvlist_lookup_uint64(nvinfo,
2291 ZPOOL_CONFIG_MMP_STATE);
2293 if (mmp_state == MMP_STATE_ACTIVE) {
2294 char *hostname = "<unknown>";
2295 uint64_t hostid = 0;
2297 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2298 hostname = fnvlist_lookup_string(nvinfo,
2299 ZPOOL_CONFIG_MMP_HOSTNAME);
2301 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2302 hostid = fnvlist_lookup_uint64(nvinfo,
2303 ZPOOL_CONFIG_MMP_HOSTID);
2305 (void) fprintf(stderr, gettext("cannot import '%s': "
2306 "pool is imported on %s (hostid: "
2307 "0x%lx)\nExport the pool on the other system, "
2308 "then run 'zpool import'.\n"),
2309 name, hostname, (unsigned long) hostid);
2310 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2311 (void) fprintf(stderr, gettext("Cannot import '%s': "
2312 "pool has the multihost property on and the\n"
2313 "system's hostid is not set. Set a unique hostid "
2314 "with the zgenhostid(8) command.\n"), name);
2316 char *hostname = "<unknown>";
2317 uint64_t timestamp = 0;
2318 uint64_t hostid = 0;
2320 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2321 hostname = fnvlist_lookup_string(config,
2322 ZPOOL_CONFIG_HOSTNAME);
2324 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2325 timestamp = fnvlist_lookup_uint64(config,
2326 ZPOOL_CONFIG_TIMESTAMP);
2328 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2329 hostid = fnvlist_lookup_uint64(config,
2330 ZPOOL_CONFIG_HOSTID);
2332 (void) fprintf(stderr, gettext("cannot import '%s': "
2333 "pool was previously in use from another system.\n"
2334 "Last accessed by %s (hostid=%lx) at %s"
2335 "The pool can be imported, use 'zpool import -f' "
2336 "to import the pool.\n"), name, hostname,
2337 (unsigned long)hostid, ctime((time_t *)×tamp));
2343 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2346 if (newname != NULL)
2347 name = (char *)newname;
2349 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2353 * Loading keys is best effort. We don't want to return immediately
2354 * if it fails but we do want to give the error to the caller.
2356 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2357 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2362 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2363 !(flags & ZFS_IMPORT_ONLY) &&
2364 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2374 * zpool import [-d dir] [-D]
2375 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2376 * [-d dir | -c cachefile] [-f] -a
2377 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2378 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2380 * -c Read pool information from a cachefile instead of searching
2383 * -d Scan in a specific directory, other than /dev/. More than
2384 * one directory can be specified using multiple '-d' options.
2386 * -D Scan for previously destroyed pools or import all or only
2387 * specified destroyed pools.
2389 * -R Temporarily import the pool, with all mountpoints relative to
2390 * the given root. The pool will remain exported when the machine
2393 * -V Import even in the presence of faulted vdevs. This is an
2394 * intentionally undocumented option for testing purposes, and
2395 * treats the pool configuration as complete, leaving any bad
2396 * vdevs in the FAULTED state. In other words, it does verbatim
2399 * -f Force import, even if it appears that the pool is active.
2401 * -F Attempt rewind if necessary.
2403 * -n See if rewind would work, but don't actually rewind.
2405 * -N Import the pool but don't mount datasets.
2407 * -T Specify a starting txg to use for import. This option is
2408 * intentionally undocumented option for testing purposes.
2410 * -a Import all pools found.
2412 * -l Load encryption keys while importing.
2414 * -o Set property=value and/or temporary mount options (without '=').
2416 * -s Scan using the default search path, the libblkid cache will
2419 * The import command scans for pools to import, and import pools based on pool
2420 * name and GUID. The pool can also be renamed as part of the import process.
2423 zpool_do_import(int argc, char **argv)
2425 char **searchdirs = NULL;
2426 char *env, *envdup = NULL;
2430 nvlist_t *pools = NULL;
2431 boolean_t do_all = B_FALSE;
2432 boolean_t do_destroyed = B_FALSE;
2433 char *mntopts = NULL;
2436 uint64_t searchguid = 0;
2437 char *searchname = NULL;
2439 nvlist_t *found_config;
2440 nvlist_t *policy = NULL;
2441 nvlist_t *props = NULL;
2443 int flags = ZFS_IMPORT_NORMAL;
2444 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2445 boolean_t dryrun = B_FALSE;
2446 boolean_t do_rewind = B_FALSE;
2447 boolean_t xtreme_rewind = B_FALSE;
2448 boolean_t do_scan = B_FALSE;
2449 uint64_t pool_state, txg = -1ULL;
2450 char *cachefile = NULL;
2451 importargs_t idata = { 0 };
2455 while ((c = getopt(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2464 if (searchdirs == NULL) {
2465 searchdirs = safe_malloc(sizeof (char *));
2467 char **tmp = safe_malloc((nsearch + 1) *
2469 bcopy(searchdirs, tmp, nsearch *
2474 searchdirs[nsearch++] = optarg;
2477 do_destroyed = B_TRUE;
2480 flags |= ZFS_IMPORT_ANY_HOST;
2486 flags |= ZFS_IMPORT_LOAD_KEYS;
2489 flags |= ZFS_IMPORT_MISSING_LOG;
2495 flags |= ZFS_IMPORT_ONLY;
2498 if ((propval = strchr(optarg, '=')) != NULL) {
2501 if (add_prop_list(optarg, propval,
2509 if (add_prop_list(zpool_prop_to_name(
2510 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2512 if (add_prop_list_default(zpool_prop_to_name(
2513 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2520 flags |= ZFS_IMPORT_TEMP_NAME;
2521 if (add_prop_list_default(zpool_prop_to_name(
2522 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2528 txg = strtoull(optarg, &endptr, 0);
2529 if (errno != 0 || *endptr != '\0') {
2530 (void) fprintf(stderr,
2531 gettext("invalid txg value\n"));
2534 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2537 flags |= ZFS_IMPORT_VERBATIM;
2540 xtreme_rewind = B_TRUE;
2543 (void) fprintf(stderr, gettext("missing argument for "
2544 "'%c' option\n"), optopt);
2548 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2557 if (cachefile && nsearch != 0) {
2558 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2562 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2563 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2567 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2568 (void) fprintf(stderr, gettext("-l is only meaningful during "
2573 if ((dryrun || xtreme_rewind) && !do_rewind) {
2574 (void) fprintf(stderr,
2575 gettext("-n or -X only meaningful with -F\n"));
2579 rewind_policy = ZPOOL_TRY_REWIND;
2581 rewind_policy = ZPOOL_DO_REWIND;
2583 rewind_policy |= ZPOOL_EXTREME_REWIND;
2585 /* In the future, we can capture further policy and include it here */
2586 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2587 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2588 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2591 /* check argument count */
2594 (void) fprintf(stderr, gettext("too many arguments\n"));
2599 (void) fprintf(stderr, gettext("too many arguments\n"));
2605 * Check for the effective uid. We do this explicitly here because
2606 * otherwise any attempt to discover pools will silently fail.
2608 if (argc == 0 && geteuid() != 0) {
2609 (void) fprintf(stderr, gettext("cannot "
2610 "discover pools: permission denied\n"));
2611 if (searchdirs != NULL)
2615 nvlist_free(policy);
2620 * Depending on the arguments given, we do one of the following:
2622 * <none> Iterate through all pools and display information about
2625 * -a Iterate through all pools and try to import each one.
2627 * <id> Find the pool that corresponds to the given GUID/pool
2628 * name and import that one.
2630 * -D Above options applies only to destroyed pools.
2636 searchguid = strtoull(argv[0], &endptr, 10);
2637 if (errno != 0 || *endptr != '\0') {
2638 searchname = argv[0];
2641 found_config = NULL;
2644 * User specified a name or guid. Ensure it's unique.
2646 idata.unique = B_TRUE;
2650 * Check the environment for the preferred search path.
2652 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2655 envdup = strdup(env);
2657 dir = strtok(envdup, ":");
2658 while (dir != NULL) {
2659 if (searchdirs == NULL) {
2660 searchdirs = safe_malloc(sizeof (char *));
2662 char **tmp = safe_malloc((nsearch + 1) *
2664 bcopy(searchdirs, tmp, nsearch *
2669 searchdirs[nsearch++] = dir;
2670 dir = strtok(NULL, ":");
2674 idata.path = searchdirs;
2675 idata.paths = nsearch;
2676 idata.poolname = searchname;
2677 idata.guid = searchguid;
2678 idata.cachefile = cachefile;
2679 idata.scan = do_scan;
2681 pools = zpool_search_import(g_zfs, &idata);
2683 if (pools != NULL && idata.exists &&
2684 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2685 (void) fprintf(stderr, gettext("cannot import '%s': "
2686 "a pool with that name already exists\n"),
2688 (void) fprintf(stderr, gettext("use the form '%s "
2689 "<pool | id> <newpool>' to give it a new name\n"),
2692 } else if (pools == NULL && idata.exists) {
2693 (void) fprintf(stderr, gettext("cannot import '%s': "
2694 "a pool with that name is already created/imported,\n"),
2696 (void) fprintf(stderr, gettext("and no additional pools "
2697 "with that name were found\n"));
2699 } else if (pools == NULL) {
2701 (void) fprintf(stderr, gettext("cannot import '%s': "
2702 "no such pool available\n"), argv[0]);
2708 if (searchdirs != NULL)
2712 nvlist_free(policy);
2719 * At this point we have a list of import candidate configs. Even if
2720 * we were searching by pool name or guid, we still need to
2721 * post-process the list to deal with pool state and possible
2727 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2729 verify(nvpair_value_nvlist(elem, &config) == 0);
2731 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2733 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2735 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2738 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2745 (void) printf("\n");
2748 err |= do_import(config, NULL, mntopts,
2751 show_import(config);
2753 } else if (searchname != NULL) {
2757 * We are searching for a pool based on name.
2759 verify(nvlist_lookup_string(config,
2760 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2762 if (strcmp(name, searchname) == 0) {
2763 if (found_config != NULL) {
2764 (void) fprintf(stderr, gettext(
2765 "cannot import '%s': more than "
2766 "one matching pool\n"), searchname);
2767 (void) fprintf(stderr, gettext(
2768 "import by numeric ID instead\n"));
2771 found_config = config;
2777 * Search for a pool by guid.
2779 verify(nvlist_lookup_uint64(config,
2780 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2782 if (guid == searchguid)
2783 found_config = config;
2788 * If we were searching for a specific pool, verify that we found a
2789 * pool, and then do the import.
2791 if (argc != 0 && err == 0) {
2792 if (found_config == NULL) {
2793 (void) fprintf(stderr, gettext("cannot import '%s': "
2794 "no such pool available\n"), argv[0]);
2797 err |= do_import(found_config, argc == 1 ? NULL :
2798 argv[1], mntopts, props, flags);
2803 * If we were just looking for pools, report an error if none were
2806 if (argc == 0 && first)
2807 (void) fprintf(stderr,
2808 gettext("no pools available to import\n"));
2813 nvlist_free(policy);
2814 if (searchdirs != NULL)
2819 return (err ? 1 : 0);
2823 * zpool sync [-f] [pool] ...
2825 * -f (undocumented) force uberblock (and config including zpool cache file)
2828 * Sync the specified pool(s).
2829 * Without arguments "zpool sync" will sync all pools.
2830 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2834 zpool_do_sync(int argc, char **argv)
2837 boolean_t force = B_FALSE;
2840 while ((ret = getopt(argc, argv, "f")) != -1) {
2846 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2855 /* if argc == 0 we will execute zpool_sync_one on all pools */
2856 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2861 typedef struct iostat_cbdata {
2866 char **cb_vdev_names; /* Only show these vdevs */
2867 unsigned int cb_vdev_names_count;
2868 boolean_t cb_verbose;
2869 boolean_t cb_literal;
2870 boolean_t cb_scripted;
2871 zpool_list_t *cb_list;
2872 vdev_cmd_data_list_t *vcdl;
2876 typedef struct name_and_columns {
2877 const char *name; /* Column name */
2878 unsigned int columns; /* Center name to this number of columns */
2879 } name_and_columns_t;
2881 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2883 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2885 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2887 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2888 {"asyncq_wait", 2}, {"scrub"}},
2889 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2890 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2892 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2893 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2894 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2895 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2899 /* Shorthand - if "columns" field not set, default to 1 column */
2900 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2902 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2904 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2905 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2906 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2907 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2908 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2909 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2910 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2911 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2914 static const char *histo_to_title[] = {
2915 [IOS_L_HISTO] = "latency",
2916 [IOS_RQ_HISTO] = "req_size",
2920 * Return the number of labels in a null-terminated name_and_columns_t
2925 label_array_len(const name_and_columns_t *labels)
2929 while (labels[i].name)
2936 * Return the number of strings in a null-terminated string array.
2939 * const char foo[] = {"bar", "baz", NULL}
2944 str_array_len(const char *array[])
2955 * Return a default column width for default/latency/queue columns. This does
2956 * not include histograms, which have their columns autosized.
2959 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2961 unsigned long column_width = 5; /* Normal niceprint */
2962 static unsigned long widths[] = {
2964 * Choose some sane default column sizes for printing the
2967 [IOS_DEFAULT] = 15, /* 1PB capacity */
2968 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2969 [IOS_QUEUES] = 6, /* 1M queue entries */
2973 column_width = widths[type];
2975 return (column_width);
2979 * Print the column labels, i.e:
2981 * capacity operations bandwidth
2982 * alloc free read write read write ...
2984 * If force_column_width is set, use it for the column width. If not set, use
2985 * the default column width.
2988 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2989 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2992 unsigned int text_start, rw_column_width, spaces_to_end;
2993 uint64_t flags = cb->cb_flags;
2995 unsigned int column_width = force_column_width;
2997 /* For each bit set in flags */
2998 for (f = flags; f; f &= ~(1ULL << idx)) {
2999 idx = lowbit64(f) - 1;
3000 if (!force_column_width)
3001 column_width = default_column_width(cb, idx);
3002 /* Print our top labels centered over "read write" label. */
3003 for (i = 0; i < label_array_len(labels[idx]); i++) {
3004 const char *name = labels[idx][i].name;
3006 * We treat labels[][].columns == 0 as shorthand
3007 * for one column. It makes writing out the label
3008 * tables more concise.
3010 unsigned int columns = MAX(1, labels[idx][i].columns);
3011 unsigned int slen = strlen(name);
3013 rw_column_width = (column_width * columns) +
3014 (2 * (columns - 1));
3016 text_start = (int)((rw_column_width)/columns -
3019 printf(" "); /* Two spaces between columns */
3021 /* Space from beginning of column to label */
3022 for (s = 0; s < text_start; s++)
3027 /* Print space after label to end of column */
3028 spaces_to_end = rw_column_width - text_start - slen;
3029 for (s = 0; s < spaces_to_end; s++)
3038 * print_cmd_columns - Print custom column titles from -c
3040 * If the user specified the "zpool status|iostat -c" then print their custom
3041 * column titles in the header. For example, print_cmd_columns() would print
3042 * the " col1 col2" part of this:
3044 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3046 * capacity operations bandwidth
3047 * pool alloc free read write read write col1 col2
3048 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3049 * mypool 269K 1008M 0 0 107 946
3050 * mirror 269K 1008M 0 0 107 946
3051 * sdb - - 0 0 102 473 val1 val2
3052 * sdc - - 0 0 5 473 val1 val2
3053 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3056 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3059 vdev_cmd_data_t *data = &vcdl->data[0];
3061 if (vcdl->count == 0 || data == NULL)
3065 * Each vdev cmd should have the same column names unless the user did
3066 * something weird with their cmd. Just take the column names from the
3067 * first vdev and assume it works for all of them.
3069 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3072 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3075 printf("%*s", vcdl->uniq_cols_width[i],
3076 vcdl->uniq_cols[i]);
3083 * Utility function to print out a line of dashes like:
3085 * -------------------------------- ----- ----- ----- ----- -----
3087 * ...or a dashed named-row line like:
3093 * @force_column_width If non-zero, use the value as the column width.
3094 * Otherwise use the default column widths.
3096 * @name: Print a dashed named-row line starting
3097 * with @name. Otherwise, print a regular
3101 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3105 unsigned int namewidth;
3106 uint64_t flags = cb->cb_flags;
3109 const name_and_columns_t *labels;
3113 if (cb->cb_flags & IOS_ANYHISTO_M) {
3114 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3115 } else if (cb->cb_vdev_names_count) {
3121 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3122 name ? strlen(name) : 0);
3126 printf("%-*s", namewidth, name);
3128 for (i = 0; i < namewidth; i++)
3132 /* For each bit in flags */
3133 for (f = flags; f; f &= ~(1ULL << idx)) {
3134 unsigned int column_width;
3135 idx = lowbit64(f) - 1;
3136 if (force_column_width)
3137 column_width = force_column_width;
3139 column_width = default_column_width(cb, idx);
3141 labels = iostat_bottom_labels[idx];
3142 for (i = 0; i < label_array_len(labels); i++) {
3144 printf(" %*s-", column_width - 1, " ");
3146 printf(" %.*s", column_width,
3147 "--------------------");
3154 print_iostat_separator_impl(iostat_cbdata_t *cb,
3155 unsigned int force_column_width)
3157 print_iostat_dashes(cb, force_column_width, NULL);
3161 print_iostat_separator(iostat_cbdata_t *cb)
3163 print_iostat_separator_impl(cb, 0);
3167 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3168 const char *histo_vdev_name)
3170 unsigned int namewidth;
3173 if (cb->cb_flags & IOS_ANYHISTO_M) {
3174 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3175 } else if (cb->cb_vdev_names_count) {
3181 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3182 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3184 if (histo_vdev_name)
3185 printf("%-*s", namewidth, histo_vdev_name);
3187 printf("%*s", namewidth, "");
3190 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3193 printf("%-*s", namewidth, title);
3195 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3196 if (cb->vcdl != NULL)
3197 print_cmd_columns(cb->vcdl, 0);
3201 print_iostat_separator_impl(cb, force_column_width);
3203 if (cb->vcdl != NULL)
3204 print_cmd_columns(cb->vcdl, 1);
3210 print_iostat_header(iostat_cbdata_t *cb)
3212 print_iostat_header_impl(cb, 0, NULL);
3217 * Display a single statistic.
3220 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3221 unsigned int column_size, boolean_t scripted)
3225 zfs_nicenum_format(value, buf, sizeof (buf), format);
3228 printf("\t%s", buf);
3230 printf(" %*s", column_size, buf);
3234 * Calculate the default vdev stats
3236 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3237 * stats into calcvs.
3240 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3241 vdev_stat_t *calcvs)
3245 memcpy(calcvs, newvs, sizeof (*calcvs));
3246 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3247 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3249 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3250 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3254 * Internal representation of the extended iostats data.
3256 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3257 * or single uint64_t's. We make both look like arrays to make them easier
3258 * to process. In order to make single uint64_t's look like arrays, we set
3259 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3260 * we can just use *data and count.
3264 uint_t count; /* Number of entries in data[] */
3265 uint64_t __data; /* Only used when data is a single uint64_t */
3269 stat_histo_max(struct stat_array *nva, unsigned int len)
3273 for (i = 0; i < len; i++)
3274 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3280 * Helper function to lookup a uint64_t array or uint64_t value and store its
3281 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3282 * it look like a one element array to make it easier to process.
3285 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3286 struct stat_array *nva)
3291 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3292 switch (nvpair_type(tmp)) {
3293 case DATA_TYPE_UINT64_ARRAY:
3294 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3296 case DATA_TYPE_UINT64:
3297 ret = nvpair_value_uint64(tmp, &nva->__data);
3298 nva->data = &nva->__data;
3302 /* Not a uint64_t */
3311 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3312 * subtract them, and return the results in a newly allocated stat_array.
3313 * You must free the returned array after you are done with it with
3314 * free_calc_stats().
3316 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3319 static struct stat_array *
3320 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3323 nvlist_t *oldnvx = NULL, *newnvx;
3324 struct stat_array *oldnva, *newnva, *calcnva;
3326 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3328 /* Extract our extended stats nvlist from the main list */
3329 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3332 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3336 newnva = safe_malloc(alloc_size);
3337 oldnva = safe_malloc(alloc_size);
3338 calcnva = safe_malloc(alloc_size);
3340 for (j = 0; j < len; j++) {
3341 verify(nvpair64_to_stat_array(newnvx, names[j],
3343 calcnva[j].count = newnva[j].count;
3344 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3345 calcnva[j].data = safe_malloc(alloc_size);
3346 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3349 verify(nvpair64_to_stat_array(oldnvx, names[j],
3351 for (i = 0; i < oldnva[j].count; i++)
3352 calcnva[j].data[i] -= oldnva[j].data[i];
3361 free_calc_stats(struct stat_array *nva, unsigned int len)
3364 for (i = 0; i < len; i++)
3371 print_iostat_histo(struct stat_array *nva, unsigned int len,
3372 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3378 enum zfs_nicenum_format format;
3379 unsigned int buckets;
3380 unsigned int start_bucket;
3383 format = ZFS_NICENUM_RAW;
3385 format = ZFS_NICENUM_1024;
3387 /* All these histos are the same size, so just use nva[0].count */
3388 buckets = nva[0].count;
3390 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3391 /* Start at 512 - req size should never be lower than this */
3397 for (j = start_bucket; j < buckets; j++) {
3398 /* Print histogram bucket label */
3399 if (cb->cb_flags & IOS_L_HISTO_M) {
3400 /* Ending range of this bucket */
3401 val = (1UL << (j + 1)) - 1;
3402 zfs_nicetime(val, buf, sizeof (buf));
3404 /* Request size (starting range of bucket) */
3406 zfs_nicenum(val, buf, sizeof (buf));
3409 if (cb->cb_scripted)
3410 printf("%llu", (u_longlong_t)val);
3412 printf("%-*s", namewidth, buf);
3414 /* Print the values on the line */
3415 for (i = 0; i < len; i++) {
3416 print_one_stat(nva[i].data[j] * scale, format,
3417 column_width, cb->cb_scripted);
3424 print_solid_separator(unsigned int length)
3432 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3433 nvlist_t *newnv, double scale, const char *name)
3435 unsigned int column_width;
3436 unsigned int namewidth;
3437 unsigned int entire_width;
3438 enum iostat_type type;
3439 struct stat_array *nva;
3441 unsigned int names_len;
3443 /* What type of histo are we? */
3444 type = IOS_HISTO_IDX(cb->cb_flags);
3446 /* Get NULL-terminated array of nvlist names for our histo */
3447 names = vsx_type_to_nvlist[type];
3448 names_len = str_array_len(names); /* num of names */
3450 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3452 if (cb->cb_literal) {
3453 column_width = MAX(5,
3454 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3459 namewidth = MAX(cb->cb_namewidth,
3460 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3463 * Calculate the entire line width of what we're printing. The
3464 * +2 is for the two spaces between columns:
3468 /* |___| <---------- column_width */
3470 /* |__________| <--- entire_width */
3472 entire_width = namewidth + (column_width + 2) *
3473 label_array_len(iostat_bottom_labels[type]);
3475 if (cb->cb_scripted)
3476 printf("%s\n", name);
3478 print_iostat_header_impl(cb, column_width, name);
3480 print_iostat_histo(nva, names_len, cb, column_width,
3483 free_calc_stats(nva, names_len);
3484 if (!cb->cb_scripted)
3485 print_solid_separator(entire_width);
3489 * Calculate the average latency of a power-of-two latency histogram
3492 single_histo_average(uint64_t *histo, unsigned int buckets)
3495 uint64_t count = 0, total = 0;
3497 for (i = 0; i < buckets; i++) {
3499 * Our buckets are power-of-two latency ranges. Use the
3500 * midpoint latency of each bucket to calculate the average.
3508 if (histo[i] != 0) {
3509 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3514 /* Prevent divide by zero */
3515 return (count == 0 ? 0 : total / count);
3519 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3520 nvlist_t *newnv, double scale)
3524 const char *names[] = {
3525 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3526 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3527 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3528 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3529 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3530 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3531 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3532 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3533 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3534 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3537 struct stat_array *nva;
3539 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3540 enum zfs_nicenum_format format;
3542 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3545 format = ZFS_NICENUM_RAW;
3547 format = ZFS_NICENUM_1024;
3549 for (i = 0; i < ARRAY_SIZE(names); i++) {
3550 val = nva[i].data[0] * scale;
3551 print_one_stat(val, format, column_width, cb->cb_scripted);
3554 free_calc_stats(nva, ARRAY_SIZE(names));
3558 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3559 nvlist_t *newnv, double scale)
3563 const char *names[] = {
3564 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3565 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3566 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3567 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3568 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3569 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3570 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3571 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3572 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3574 struct stat_array *nva;
3576 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3577 enum zfs_nicenum_format format;
3579 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3582 format = ZFS_NICENUM_RAWTIME;
3584 format = ZFS_NICENUM_TIME;
3586 /* Print our avg latencies on the line */
3587 for (i = 0; i < ARRAY_SIZE(names); i++) {
3588 /* Compute average latency for a latency histo */
3589 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3590 print_one_stat(val, format, column_width, cb->cb_scripted);
3592 free_calc_stats(nva, ARRAY_SIZE(names));
3596 * Print default statistics (capacity/operations/bandwidth)
3599 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3601 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3602 enum zfs_nicenum_format format;
3603 char na; /* char to print for "not applicable" values */
3605 if (cb->cb_literal) {
3606 format = ZFS_NICENUM_RAW;
3609 format = ZFS_NICENUM_1024;
3613 /* only toplevel vdevs have capacity stats */
3614 if (vs->vs_space == 0) {
3615 if (cb->cb_scripted)
3616 printf("\t%c\t%c", na, na);
3618 printf(" %*c %*c", column_width, na, column_width,
3621 print_one_stat(vs->vs_alloc, format, column_width,
3623 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3624 column_width, cb->cb_scripted);
3627 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3628 format, column_width, cb->cb_scripted);
3629 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3630 format, column_width, cb->cb_scripted);
3631 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3632 format, column_width, cb->cb_scripted);
3633 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3634 format, column_width, cb->cb_scripted);
3638 * Print out all the statistics for the given vdev. This can either be the
3639 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3640 * is a verbose output, and we don't want to display the toplevel pool stats.
3642 * Returns the number of stat lines printed.
3645 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3646 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3648 nvlist_t **oldchild, **newchild;
3650 vdev_stat_t *oldvs, *newvs, *calcvs;
3651 vdev_stat_t zerovs = { 0 };
3658 calcvs = safe_malloc(sizeof (*calcvs));
3660 if (oldnv != NULL) {
3661 verify(nvlist_lookup_uint64_array(oldnv,
3662 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3667 /* Do we only want to see a specific vdev? */
3668 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3669 /* Yes we do. Is this the vdev? */
3670 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3672 * This is our vdev. Since it is the only vdev we
3673 * will be displaying, make depth = 0 so that it
3674 * doesn't get indented.
3681 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3682 /* Couldn't match the name */
3687 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3688 (uint64_t **)&newvs, &c) == 0);
3691 * Print the vdev name unless it's is a histogram. Histograms
3692 * display the vdev name in the header itself.
3694 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3695 if (cb->cb_scripted) {
3698 if (strlen(name) + depth > cb->cb_namewidth)
3699 (void) printf("%*s%s", depth, "", name);
3701 (void) printf("%*s%s%*s", depth, "", name,
3702 (int)(cb->cb_namewidth - strlen(name) -
3707 /* Calculate our scaling factor */
3708 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3709 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3711 * If we specify printing histograms with no time interval, then
3712 * print the histogram numbers over the entire lifetime of the
3720 scale = (double)NANOSEC / tdelta;
3723 if (cb->cb_flags & IOS_DEFAULT_M) {
3724 calc_default_iostats(oldvs, newvs, calcvs);
3725 print_iostat_default(calcvs, cb, scale);
3727 if (cb->cb_flags & IOS_LATENCY_M)
3728 print_iostat_latency(cb, oldnv, newnv, scale);
3729 if (cb->cb_flags & IOS_QUEUES_M)
3730 print_iostat_queues(cb, oldnv, newnv, scale);
3731 if (cb->cb_flags & IOS_ANYHISTO_M) {
3733 print_iostat_histos(cb, oldnv, newnv, scale, name);
3736 if (cb->vcdl != NULL) {
3738 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3741 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3745 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3754 if (!cb->cb_verbose)
3757 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3758 &newchild, &children) != 0)
3761 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3762 &oldchild, &c) != 0)
3765 for (c = 0; c < children; c++) {
3766 uint64_t ishole = B_FALSE, islog = B_FALSE;
3768 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3771 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3774 if (ishole || islog)
3777 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3779 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3780 newchild[c], cb, depth + 2);
3785 * Log device section
3788 if (num_logs(newnv) > 0) {
3789 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3790 !cb->cb_vdev_names) {
3791 print_iostat_dashes(cb, 0, "logs");
3795 for (c = 0; c < children; c++) {
3796 uint64_t islog = B_FALSE;
3797 (void) nvlist_lookup_uint64(newchild[c],
3798 ZPOOL_CONFIG_IS_LOG, &islog);
3801 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3803 ret += print_vdev_stats(zhp, vname, oldnv ?
3804 oldchild[c] : NULL, newchild[c],
3813 * Include level 2 ARC devices in iostat output
3815 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3816 &newchild, &children) != 0)
3819 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3820 &oldchild, &c) != 0)
3824 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3825 !cb->cb_vdev_names) {
3826 print_iostat_dashes(cb, 0, "cache");
3830 for (c = 0; c < children; c++) {
3831 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3833 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3834 : NULL, newchild[c], cb, depth + 2);
3843 refresh_iostat(zpool_handle_t *zhp, void *data)
3845 iostat_cbdata_t *cb = data;
3849 * If the pool has disappeared, remove it from the list and continue.
3851 if (zpool_refresh_stats(zhp, &missing) != 0)
3855 pool_list_remove(cb->cb_list, zhp);
3861 * Callback to print out the iostats for the given pool.
3864 print_iostat(zpool_handle_t *zhp, void *data)
3866 iostat_cbdata_t *cb = data;
3867 nvlist_t *oldconfig, *newconfig;
3868 nvlist_t *oldnvroot, *newnvroot;
3871 newconfig = zpool_get_config(zhp, &oldconfig);
3873 if (cb->cb_iteration == 1)
3876 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3879 if (oldconfig == NULL)
3882 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3885 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3887 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3888 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3889 print_iostat_separator(cb);
3890 if (cb->vcdl != NULL) {
3891 print_cmd_columns(cb->vcdl, 1);
3906 if (isatty(STDOUT_FILENO)) {
3907 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3909 columns = ws.ws_col;
3918 get_namewidth(zpool_handle_t *zhp, void *data)
3920 iostat_cbdata_t *cb = data;
3921 nvlist_t *config, *nvroot;
3924 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3925 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3927 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3928 if (!cb->cb_verbose)
3929 cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
3931 cb->cb_namewidth = MAX(poolname_len,
3932 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3933 cb->cb_name_flags));
3936 * The width must be at least 10, but may be as large as the
3937 * column width - 42 so that we can still fit in one line.
3939 columns = get_columns();
3941 if (cb->cb_namewidth < 10)
3942 cb->cb_namewidth = 10;
3943 if (cb->cb_namewidth > columns - 42)
3944 cb->cb_namewidth = columns - 42;
3950 * Parse the input string, get the 'interval' and 'count' value if there is one.
3953 get_interval_count(int *argcp, char **argv, float *iv,
3957 unsigned long count = 0;
3961 * Determine if the last argument is an integer or a pool name
3963 if (argc > 0 && isnumber(argv[argc - 1])) {
3967 interval = strtof(argv[argc - 1], &end);
3969 if (*end == '\0' && errno == 0) {
3970 if (interval == 0) {
3971 (void) fprintf(stderr, gettext("interval "
3972 "cannot be zero\n"));
3976 * Ignore the last parameter
3981 * If this is not a valid number, just plow on. The
3982 * user will get a more informative error message later
3990 * If the last argument is also an integer, then we have both a count
3993 if (argc > 0 && isnumber(argv[argc - 1])) {
3998 interval = strtof(argv[argc - 1], &end);
4000 if (*end == '\0' && errno == 0) {
4001 if (interval == 0) {
4002 (void) fprintf(stderr, gettext("interval "
4003 "cannot be zero\n"));
4008 * Ignore the last parameter
4022 get_timestamp_arg(char c)
4025 timestamp_fmt = UDATE;
4027 timestamp_fmt = DDATE;
4033 * Return stat flags that are supported by all pools by both the module and
4034 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4035 * It will get ANDed down until only the flags that are supported on all pools
4039 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4041 uint64_t *mask = data;
4042 nvlist_t *config, *nvroot, *nvx;
4046 config = zpool_get_config(zhp, NULL);
4047 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4050 /* Default stats are always supported, but for completeness.. */
4051 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4052 flags |= IOS_DEFAULT_M;
4054 /* Get our extended stats nvlist from the main list */
4055 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4058 * No extended stats; they're probably running an older
4059 * module. No big deal, we support that too.
4064 /* For each extended stat, make sure all its nvpairs are supported */
4065 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4066 if (!vsx_type_to_nvlist[j][0])
4069 /* Start off by assuming the flag is supported, then check */
4070 flags |= (1ULL << j);
4071 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4072 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4073 /* flag isn't supported */
4074 flags = flags & ~(1ULL << j);
4080 *mask = *mask & flags;
4085 * Return a bitmask of stats that are supported on all pools by both the module
4089 get_stat_flags(zpool_list_t *list)
4094 * get_stat_flags_cb() will lop off bits from "mask" until only the
4095 * flags that are supported on all pools remain.
4097 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4102 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4105 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4107 iostat_cbdata_t *cb = cb_data;
4111 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4113 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4114 ret = 1; /* match */
4121 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4124 is_vdev(zpool_handle_t *zhp, void *cb_data)
4126 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4130 * Check if vdevs are in a pool
4132 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4133 * return 0. If pool_name is NULL, then search all pools.
4136 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4137 iostat_cbdata_t *cb)
4144 if ((argc == 0) || !*argv)
4150 /* Temporarily hijack cb_vdev_names for a second... */
4151 tmp_name = cb->cb_vdev_names;
4153 /* Go though our list of prospective vdev names */
4154 for (i = 0; i < argc; i++) {
4155 cb->cb_vdev_names = argv + i;
4157 /* Is this name a vdev in our pools? */
4158 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4166 cb->cb_vdev_names = tmp_name;
4172 is_pool_cb(zpool_handle_t *zhp, void *data)
4175 if (strcmp(name, zpool_get_name(zhp)) == 0)
4182 * Do we have a pool named *name? If so, return 1, otherwise 0.
4187 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4190 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4192 are_all_pools(int argc, char **argv)
4194 if ((argc == 0) || !*argv)
4198 if (!is_pool(argv[argc]))
4205 * Helper function to print out vdev/pool names we can't resolve. Used for an
4209 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4210 iostat_cbdata_t *cb)
4215 for (i = 0; i < argc; i++) {
4219 str = gettext("pool");
4220 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4221 str = gettext("vdev in this pool");
4222 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4223 str = gettext("vdev in another pool");
4225 str = gettext("unknown");
4227 fprintf(stderr, "\t%s (%s)\n", name, str);
4232 * Same as get_interval_count(), but with additional checks to not misinterpret
4233 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4237 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4238 unsigned long *count, iostat_cbdata_t *cb)
4240 char **tmpargv = argv;
4241 int argc_for_interval = 0;
4243 /* Is the last arg an interval value? Or a guid? */
4244 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4246 * The last arg is not a guid, so it's probably an
4249 argc_for_interval++;
4252 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4254 * The 2nd to last arg is not a guid, so it's probably
4255 * an interval value.
4257 argc_for_interval++;
4261 /* Point to our list of possible intervals */
4262 tmpargv = &argv[*argc - argc_for_interval];
4264 *argc = *argc - argc_for_interval;
4265 get_interval_count(&argc_for_interval, tmpargv,
4270 * Floating point sleep(). Allows you to pass in a floating point value for
4276 struct timespec req;
4277 req.tv_sec = floor(sec);
4278 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4279 nanosleep(&req, NULL);
4283 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4286 * name: Short name of the script ('iostat').
4287 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4290 print_zpool_script_help(char *name, char *path)
4292 char *argv[] = {path, "-h", NULL};
4293 char **lines = NULL;
4297 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4299 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4301 libzfs_free_str_array(lines, lines_cnt);
4305 for (int i = 0; i < lines_cnt; i++)
4306 if (!is_blank_str(lines[i]))
4307 printf(" %-14s %s\n", name, lines[i]);
4309 libzfs_free_str_array(lines, lines_cnt);
4313 * Go though the zpool status/iostat -c scripts in the user's path, run their
4314 * help option (-h), and print out the results.
4317 print_zpool_dir_scripts(char *dirpath)
4321 char fullpath[MAXPATHLEN];
4322 struct stat dir_stat;
4324 if ((dir = opendir(dirpath)) != NULL) {
4325 /* print all the files and directories within directory */
4326 while ((ent = readdir(dir)) != NULL) {
4327 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4329 /* Print the scripts */
4330 if (stat(fullpath, &dir_stat) == 0)
4331 if (dir_stat.st_mode & S_IXUSR &&
4332 S_ISREG(dir_stat.st_mode))
4333 print_zpool_script_help(ent->d_name,
4341 * Print out help text for all zpool status/iostat -c scripts.
4344 print_zpool_script_list(char *subcommand)
4348 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4350 sp = zpool_get_cmd_search_path();
4354 dir = strtok(sp, ":");
4355 while (dir != NULL) {
4356 print_zpool_dir_scripts(dir);
4357 dir = strtok(NULL, ":");
4364 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4365 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4366 * [interval [count]]
4368 * -c CMD For each vdev, run command CMD
4369 * -g Display guid for individual vdev name.
4370 * -L Follow links when resolving vdev path name.
4371 * -P Display full path for vdev name.
4372 * -v Display statistics for individual vdevs
4374 * -p Display values in parsable (exact) format.
4375 * -H Scripted mode. Don't display headers, and separate properties
4377 * -l Display average latency
4378 * -q Display queue depths
4379 * -w Display latency histograms
4380 * -r Display request size histogram
4381 * -T Display a timestamp in date(1) or Unix format
4383 * This command can be tricky because we want to be able to deal with pool
4384 * creation/destruction as well as vdev configuration changes. The bulk of this
4385 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4386 * on pool_list_update() to detect the addition of new pools. Configuration
4387 * changes are all handled within libzfs.
4390 zpool_do_iostat(int argc, char **argv)
4396 unsigned long count = 0;
4398 boolean_t verbose = B_FALSE;
4399 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4400 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4401 boolean_t omit_since_boot = B_FALSE;
4402 boolean_t guid = B_FALSE;
4403 boolean_t follow_links = B_FALSE;
4404 boolean_t full_name = B_FALSE;
4405 iostat_cbdata_t cb = { 0 };
4408 /* Used for printing error message */
4409 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4410 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4412 uint64_t unsupported_flags;
4415 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4420 gettext("Can't set -c flag twice\n"));
4424 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4425 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4426 fprintf(stderr, gettext(
4427 "Can't run -c, disabled by "
4428 "ZPOOL_SCRIPTS_ENABLED.\n"));
4432 if ((getuid() <= 0 || geteuid() <= 0) &&
4433 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4434 fprintf(stderr, gettext(
4435 "Can't run -c with root privileges "
4436 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4446 follow_links = B_TRUE;
4452 get_timestamp_arg(*optarg);
4476 omit_since_boot = B_TRUE;
4482 if (optopt == 'c') {
4483 print_zpool_script_list("iostat");
4487 gettext("invalid option '%c'\n"), optopt);
4496 cb.cb_literal = parsable;
4497 cb.cb_scripted = scripted;
4500 cb.cb_name_flags |= VDEV_NAME_GUID;
4502 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4504 cb.cb_name_flags |= VDEV_NAME_PATH;
4505 cb.cb_iteration = 0;
4506 cb.cb_namewidth = 0;
4507 cb.cb_verbose = verbose;
4509 /* Get our interval and count values (if any) */
4511 get_interval_count_filter_guids(&argc, argv, &interval,
4514 get_interval_count(&argc, argv, &interval, &count);
4518 /* No args, so just print the defaults. */
4519 } else if (are_all_pools(argc, argv)) {
4520 /* All the args are pool names */
4521 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4522 /* All the args are vdevs */
4523 cb.cb_vdev_names = argv;
4524 cb.cb_vdev_names_count = argc;
4525 argc = 0; /* No pools to process */
4526 } else if (are_all_pools(1, argv)) {
4527 /* The first arg is a pool name */
4528 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4529 /* ...and the rest are vdev names */
4530 cb.cb_vdev_names = argv + 1;
4531 cb.cb_vdev_names_count = argc - 1;
4532 argc = 1; /* One pool to process */
4534 fprintf(stderr, gettext("Expected either a list of "));
4535 fprintf(stderr, gettext("pools, or list of vdevs in"));
4536 fprintf(stderr, " \"%s\", ", argv[0]);
4537 fprintf(stderr, gettext("but got:\n"));
4538 error_list_unresolved_vdevs(argc - 1, argv + 1,
4540 fprintf(stderr, "\n");
4546 * The args don't make sense. The first arg isn't a pool name,
4547 * nor are all the args vdevs.
4549 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4550 fprintf(stderr, "\n");
4554 if (cb.cb_vdev_names_count != 0) {
4556 * If user specified vdevs, it implies verbose.
4558 cb.cb_verbose = B_TRUE;
4562 * Construct the list of all interesting pools.
4565 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4568 if (pool_list_count(list) == 0 && argc != 0) {
4569 pool_list_free(list);
4573 if (pool_list_count(list) == 0 && interval == 0) {
4574 pool_list_free(list);
4575 (void) fprintf(stderr, gettext("no pools available\n"));
4579 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4580 pool_list_free(list);
4581 (void) fprintf(stderr,
4582 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4587 if (l_histo && rq_histo) {
4588 pool_list_free(list);
4589 (void) fprintf(stderr,
4590 gettext("Only one of [-r|-w] can be passed at a time\n"));
4596 * Enter the main iostat loop.
4602 * Histograms tables look out of place when you try to display
4603 * them with the other stats, so make a rule that you can only
4604 * print histograms by themselves.
4606 cb.cb_flags = IOS_L_HISTO_M;
4607 } else if (rq_histo) {
4608 cb.cb_flags = IOS_RQ_HISTO_M;
4610 cb.cb_flags = IOS_DEFAULT_M;
4612 cb.cb_flags |= IOS_LATENCY_M;
4614 cb.cb_flags |= IOS_QUEUES_M;
4618 * See if the module supports all the stats we want to display.
4620 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4621 if (unsupported_flags) {
4625 gettext("The loaded zfs module doesn't support:"));
4627 /* for each bit set in unsupported_flags */
4628 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4629 idx = lowbit64(f) - 1;
4630 fprintf(stderr, " -%c", flag_to_arg[idx]);
4633 fprintf(stderr, ". Try running a newer module.\n");
4634 pool_list_free(list);
4640 if ((npools = pool_list_count(list)) == 0)
4641 (void) fprintf(stderr, gettext("no pools available\n"));
4644 * If this is the first iteration and -y was supplied
4645 * we skip any printing.
4647 boolean_t skip = (omit_since_boot &&
4648 cb.cb_iteration == 0);
4651 * Refresh all statistics. This is done as an
4652 * explicit step before calculating the maximum name
4653 * width, so that any * configuration changes are
4654 * properly accounted for.
4656 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4660 * Iterate over all pools to determine the maximum width
4661 * for the pool / device name column across all pools.
4663 cb.cb_namewidth = 0;
4664 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4667 if (timestamp_fmt != NODATE)
4668 print_timestamp(timestamp_fmt);
4670 if (cmd != NULL && cb.cb_verbose &&
4671 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4672 cb.vcdl = all_pools_for_each_vdev_run(argc,
4673 argv, cmd, g_zfs, cb.cb_vdev_names,
4674 cb.cb_vdev_names_count, cb.cb_name_flags);
4680 * If it's the first time and we're not skipping it,
4681 * or either skip or verbose mode, print the header.
4683 * The histogram code explicitly prints its header on
4684 * every vdev, so skip this for histograms.
4686 if (((++cb.cb_iteration == 1 && !skip) ||
4687 (skip != verbose)) &&
4688 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4690 print_iostat_header(&cb);
4693 (void) fsleep(interval);
4698 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4701 * If there's more than one pool, and we're not in
4702 * verbose mode (which prints a separator for us),
4703 * then print a separator.
4705 * In addition, if we're printing specific vdevs then
4706 * we also want an ending separator.
4708 if (((npools > 1 && !verbose &&
4709 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4710 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4711 cb.cb_vdev_names_count)) &&
4713 print_iostat_separator(&cb);
4714 if (cb.vcdl != NULL)
4715 print_cmd_columns(cb.vcdl, 1);
4719 if (cb.vcdl != NULL)
4720 free_vdev_cmd_data_list(cb.vcdl);
4725 * Flush the output so that redirection to a file isn't buffered
4728 (void) fflush(stdout);
4733 if (count != 0 && --count == 0)
4736 (void) fsleep(interval);
4739 pool_list_free(list);
4744 typedef struct list_cbdata {
4745 boolean_t cb_verbose;
4748 boolean_t cb_scripted;
4749 zprop_list_t *cb_proplist;
4750 boolean_t cb_literal;
4754 * Given a list of columns to display, output appropriate headers for each one.
4757 print_header(list_cbdata_t *cb)
4759 zprop_list_t *pl = cb->cb_proplist;
4760 char headerbuf[ZPOOL_MAXPROPLEN];
4762 boolean_t first = B_TRUE;
4763 boolean_t right_justify;
4766 for (; pl != NULL; pl = pl->pl_next) {
4767 width = pl->pl_width;
4768 if (first && cb->cb_verbose) {
4770 * Reset the width to accommodate the verbose listing
4773 width = cb->cb_namewidth;
4781 right_justify = B_FALSE;
4782 if (pl->pl_prop != ZPROP_INVAL) {
4783 header = zpool_prop_column_name(pl->pl_prop);
4784 right_justify = zpool_prop_align_right(pl->pl_prop);
4788 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4789 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4790 headerbuf[i] = '\0';
4794 if (pl->pl_next == NULL && !right_justify)
4795 (void) printf("%s", header);
4796 else if (right_justify)
4797 (void) printf("%*s", (int)width, header);
4799 (void) printf("%-*s", (int)width, header);
4802 (void) printf("\n");
4806 * Given a pool and a list of properties, print out all the properties according
4807 * to the described layout.
4810 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4812 zprop_list_t *pl = cb->cb_proplist;
4813 boolean_t first = B_TRUE;
4814 char property[ZPOOL_MAXPROPLEN];
4816 boolean_t right_justify;
4819 for (; pl != NULL; pl = pl->pl_next) {
4821 width = pl->pl_width;
4822 if (first && cb->cb_verbose) {
4824 * Reset the width to accommodate the verbose listing
4827 width = cb->cb_namewidth;
4831 if (cb->cb_scripted)
4832 (void) printf("\t");
4839 right_justify = B_FALSE;
4840 if (pl->pl_prop != ZPROP_INVAL) {
4841 if (zpool_get_prop(zhp, pl->pl_prop, property,
4842 sizeof (property), NULL, cb->cb_literal) != 0)
4847 right_justify = zpool_prop_align_right(pl->pl_prop);
4848 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4849 zpool_prop_unsupported(pl->pl_user_prop)) &&
4850 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4851 sizeof (property)) == 0) {
4859 * If this is being called in scripted mode, or if this is the
4860 * last column and it is left-justified, don't include a width
4863 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4864 (void) printf("%s", propstr);
4865 else if (right_justify)
4866 (void) printf("%*s", (int)width, propstr);
4868 (void) printf("%-*s", (int)width, propstr);
4871 (void) printf("\n");
4875 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4876 boolean_t valid, enum zfs_nicenum_format format)
4880 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4883 case ZPOOL_PROP_EXPANDSZ:
4885 (void) strlcpy(propval, "-", sizeof (propval));
4887 zfs_nicenum_format(value, propval, sizeof (propval),
4890 case ZPOOL_PROP_FRAGMENTATION:
4891 if (value == ZFS_FRAG_INVALID) {
4892 (void) strlcpy(propval, "-", sizeof (propval));
4893 } else if (format == ZFS_NICENUM_RAW) {
4894 (void) snprintf(propval, sizeof (propval), "%llu",
4895 (unsigned long long)value);
4897 (void) snprintf(propval, sizeof (propval), "%llu%%",
4898 (unsigned long long)value);
4901 case ZPOOL_PROP_CAPACITY:
4902 if (format == ZFS_NICENUM_RAW)
4903 (void) snprintf(propval, sizeof (propval), "%llu",
4904 (unsigned long long)value);
4906 (void) snprintf(propval, sizeof (propval), "%llu%%",
4907 (unsigned long long)value);
4910 zfs_nicenum_format(value, propval, sizeof (propval), format);
4914 (void) strlcpy(propval, "-", sizeof (propval));
4917 (void) printf("\t%s", propval);
4919 (void) printf(" %*s", (int)width, propval);
4923 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4924 list_cbdata_t *cb, int depth)
4930 boolean_t scripted = cb->cb_scripted;
4931 uint64_t islog = B_FALSE;
4932 boolean_t haslog = B_FALSE;
4933 char *dashes = "%-*s - - - - - -\n";
4935 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4936 (uint64_t **)&vs, &c) == 0);
4939 boolean_t toplevel = (vs->vs_space != 0);
4941 enum zfs_nicenum_format format;
4944 format = ZFS_NICENUM_RAW;
4946 format = ZFS_NICENUM_1024;
4949 (void) printf("\t%s", name);
4950 else if (strlen(name) + depth > cb->cb_namewidth)
4951 (void) printf("%*s%s", depth, "", name);
4953 (void) printf("%*s%s%*s", depth, "", name,
4954 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4957 * Print the properties for the individual vdevs. Some
4958 * properties are only applicable to toplevel vdevs. The
4959 * 'toplevel' boolean value is passed to the print_one_column()
4960 * to indicate that the value is valid.
4962 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4964 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4966 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4967 scripted, toplevel, format);
4968 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4970 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4971 vs->vs_fragmentation, scripted,
4972 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4974 cap = (vs->vs_space == 0) ? 0 :
4975 (vs->vs_alloc * 100 / vs->vs_space);
4976 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4978 (void) printf("\n");
4981 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4982 &child, &children) != 0)
4985 for (c = 0; c < children; c++) {
4986 uint64_t ishole = B_FALSE;
4988 if (nvlist_lookup_uint64(child[c],
4989 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4992 if (nvlist_lookup_uint64(child[c],
4993 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
4998 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5000 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5004 if (haslog == B_TRUE) {
5005 /* LINTED E_SEC_PRINTF_VAR_FMT */
5006 (void) printf(dashes, cb->cb_namewidth, "log");
5007 for (c = 0; c < children; c++) {
5008 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5009 &islog) != 0 || !islog)
5011 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5013 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5018 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5019 &child, &children) == 0 && children > 0) {
5020 /* LINTED E_SEC_PRINTF_VAR_FMT */
5021 (void) printf(dashes, cb->cb_namewidth, "cache");
5022 for (c = 0; c < children; c++) {
5023 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5025 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5030 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5031 &children) == 0 && children > 0) {
5032 /* LINTED E_SEC_PRINTF_VAR_FMT */
5033 (void) printf(dashes, cb->cb_namewidth, "spare");
5034 for (c = 0; c < children; c++) {
5035 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5037 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5045 * Generic callback function to list a pool.
5048 list_callback(zpool_handle_t *zhp, void *data)
5050 list_cbdata_t *cbp = data;
5054 config = zpool_get_config(zhp, NULL);
5056 print_pool(zhp, cbp);
5057 if (!cbp->cb_verbose)
5060 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5062 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5068 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5070 * -g Display guid for individual vdev name.
5071 * -H Scripted mode. Don't display headers, and separate properties
5073 * -L Follow links when resolving vdev path name.
5074 * -o List of properties to display. Defaults to
5075 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5076 * "dedupratio,health,altroot"
5077 * -p Display values in parsable (exact) format.
5078 * -P Display full path for vdev name.
5079 * -T Display a timestamp in date(1) or Unix format
5081 * List all pools in the system, whether or not they're healthy. Output space
5082 * statistics for each one, as well as health status summary.
5085 zpool_do_list(int argc, char **argv)
5089 list_cbdata_t cb = { 0 };
5090 static char default_props[] =
5091 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5092 "dedupratio,health,altroot";
5093 char *props = default_props;
5095 unsigned long count = 0;
5097 boolean_t first = B_TRUE;
5100 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5103 cb.cb_name_flags |= VDEV_NAME_GUID;
5106 cb.cb_scripted = B_TRUE;
5109 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5115 cb.cb_name_flags |= VDEV_NAME_PATH;
5118 cb.cb_literal = B_TRUE;
5121 get_timestamp_arg(*optarg);
5124 cb.cb_verbose = B_TRUE;
5127 (void) fprintf(stderr, gettext("missing argument for "
5128 "'%c' option\n"), optopt);
5132 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5141 get_interval_count(&argc, argv, &interval, &count);
5143 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5147 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5151 if (pool_list_count(list) == 0)
5154 if (timestamp_fmt != NODATE)
5155 print_timestamp(timestamp_fmt);
5157 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5161 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5166 if (count != 0 && --count == 0)
5169 pool_list_free(list);
5170 (void) fsleep(interval);
5173 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5174 (void) printf(gettext("no pools available\n"));
5178 pool_list_free(list);
5179 zprop_free_list(cb.cb_proplist);
5184 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5186 boolean_t force = B_FALSE;
5189 char *poolname, *old_disk, *new_disk;
5190 zpool_handle_t *zhp;
5191 nvlist_t *props = NULL;
5196 while ((c = getopt(argc, argv, "fo:")) != -1) {
5202 if ((propval = strchr(optarg, '=')) == NULL) {
5203 (void) fprintf(stderr, gettext("missing "
5204 "'=' for -o option\n"));
5210 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5211 (add_prop_list(optarg, propval, &props, B_TRUE)))
5215 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5224 /* get pool name and check number of arguments */
5226 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5233 (void) fprintf(stderr,
5234 gettext("missing <device> specification\n"));
5242 (void) fprintf(stderr,
5243 gettext("missing <new_device> specification\n"));
5246 new_disk = old_disk;
5256 (void) fprintf(stderr, gettext("too many arguments\n"));
5260 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5265 if (zpool_get_config(zhp, NULL) == NULL) {
5266 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5273 /* unless manually specified use "ashift" pool property (if set) */
5274 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5277 char strval[ZPOOL_MAXPROPLEN];
5279 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5280 if (src != ZPROP_SRC_DEFAULT) {
5281 (void) sprintf(strval, "%" PRId32, intval);
5282 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5283 &props, B_TRUE) == 0);
5287 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5289 if (nvroot == NULL) {
5295 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5298 nvlist_free(nvroot);
5305 * zpool replace [-f] <pool> <device> <new_device>
5307 * -f Force attach, even if <new_device> appears to be in use.
5309 * Replace <device> with <new_device>.
5313 zpool_do_replace(int argc, char **argv)
5315 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5319 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5321 * -f Force attach, even if <new_device> appears to be in use.
5322 * -o Set property=value.
5324 * Attach <new_device> to the mirror containing <device>. If <device> is not
5325 * part of a mirror, then <device> will be transformed into a mirror of
5326 * <device> and <new_device>. In either case, <new_device> will begin life
5327 * with a DTL of [0, now], and will immediately begin to resilver itself.
5330 zpool_do_attach(int argc, char **argv)
5332 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5336 * zpool detach [-f] <pool> <device>
5338 * -f Force detach of <device>, even if DTLs argue against it
5339 * (not supported yet)
5341 * Detach a device from a mirror. The operation will be refused if <device>
5342 * is the last device in the mirror, or if the DTLs indicate that this device
5343 * has the only valid copy of some data.
5347 zpool_do_detach(int argc, char **argv)
5350 char *poolname, *path;
5351 zpool_handle_t *zhp;
5355 while ((c = getopt(argc, argv, "f")) != -1) {
5359 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5368 /* get pool name and check number of arguments */
5370 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5375 (void) fprintf(stderr,
5376 gettext("missing <device> specification\n"));
5383 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5386 ret = zpool_vdev_detach(zhp, path);
5394 * zpool split [-gLnP] [-o prop=val] ...
5396 * [-R altroot] <pool> <newpool> [<device> ...]
5398 * -g Display guid for individual vdev name.
5399 * -L Follow links when resolving vdev path name.
5400 * -n Do not split the pool, but display the resulting layout if
5401 * it were to be split.
5402 * -o Set property=value, or set mount options.
5403 * -P Display full path for vdev name.
5404 * -R Mount the split-off pool under an alternate root.
5405 * -l Load encryption keys while importing.
5407 * Splits the named pool and gives it the new pool name. Devices to be split
5408 * off may be listed, provided that no more than one device is specified
5409 * per top-level vdev mirror. The newly split pool is left in an exported
5410 * state unless -R is specified.
5412 * Restrictions: the top-level of the pool pool must only be made up of
5413 * mirrors; all devices in the pool must be healthy; no device may be
5414 * undergoing a resilvering operation.
5417 zpool_do_split(int argc, char **argv)
5419 char *srcpool, *newpool, *propval;
5420 char *mntopts = NULL;
5423 boolean_t loadkeys = B_FALSE;
5424 zpool_handle_t *zhp;
5425 nvlist_t *config, *props = NULL;
5427 flags.dryrun = B_FALSE;
5428 flags.import = B_FALSE;
5429 flags.name_flags = 0;
5432 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5435 flags.name_flags |= VDEV_NAME_GUID;
5438 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5441 flags.import = B_TRUE;
5443 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5444 &props, B_TRUE) != 0) {
5453 flags.dryrun = B_TRUE;
5456 if ((propval = strchr(optarg, '=')) != NULL) {
5459 if (add_prop_list(optarg, propval,
5460 &props, B_TRUE) != 0) {
5469 flags.name_flags |= VDEV_NAME_PATH;
5472 (void) fprintf(stderr, gettext("missing argument for "
5473 "'%c' option\n"), optopt);
5477 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5484 if (!flags.import && mntopts != NULL) {
5485 (void) fprintf(stderr, gettext("setting mntopts is only "
5486 "valid when importing the pool\n"));
5490 if (!flags.import && loadkeys) {
5491 (void) fprintf(stderr, gettext("loading keys is only "
5492 "valid when importing the pool\n"));
5500 (void) fprintf(stderr, gettext("Missing pool name\n"));
5504 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5514 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5519 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5520 if (config == NULL) {
5524 (void) printf(gettext("would create '%s' with the "
5525 "following layout:\n\n"), newpool);
5526 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5533 if (ret != 0 || flags.dryrun || !flags.import) {
5534 nvlist_free(config);
5540 * The split was successful. Now we need to open the new
5541 * pool and import it.
5543 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5544 nvlist_free(config);
5550 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5555 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5556 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5558 (void) fprintf(stderr, gettext("Split was successful, but "
5559 "the datasets could not all be mounted\n"));
5560 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5561 "different altroot\n"), "zpool import");
5564 nvlist_free(config);
5573 * zpool online <pool> <device> ...
5576 zpool_do_online(int argc, char **argv)
5580 zpool_handle_t *zhp;
5582 vdev_state_t newstate;
5586 while ((c = getopt(argc, argv, "et")) != -1) {
5589 flags |= ZFS_ONLINE_EXPAND;
5593 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5602 /* get pool name and check number of arguments */
5604 (void) fprintf(stderr, gettext("missing pool name\n"));
5608 (void) fprintf(stderr, gettext("missing device name\n"));
5614 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5617 for (i = 1; i < argc; i++) {
5618 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5619 if (newstate != VDEV_STATE_HEALTHY) {
5620 (void) printf(gettext("warning: device '%s' "
5621 "onlined, but remains in faulted state\n"),
5623 if (newstate == VDEV_STATE_FAULTED)
5624 (void) printf(gettext("use 'zpool "
5625 "clear' to restore a faulted "
5628 (void) printf(gettext("use 'zpool "
5629 "replace' to replace devices "
5630 "that are no longer present\n"));
5643 * zpool offline [-ft] <pool> <device> ...
5645 * -f Force the device into a faulted state.
5647 * -t Only take the device off-line temporarily. The offline/faulted
5648 * state will not be persistent across reboots.
5652 zpool_do_offline(int argc, char **argv)
5656 zpool_handle_t *zhp;
5658 boolean_t istmp = B_FALSE;
5659 boolean_t fault = B_FALSE;
5662 while ((c = getopt(argc, argv, "ft")) != -1) {
5671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5680 /* get pool name and check number of arguments */
5682 (void) fprintf(stderr, gettext("missing pool name\n"));
5686 (void) fprintf(stderr, gettext("missing device name\n"));
5692 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5695 for (i = 1; i < argc; i++) {
5697 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
5699 if (istmp == B_FALSE) {
5700 /* Force the fault to persist across imports */
5701 aux = VDEV_AUX_EXTERNAL_PERSIST;
5703 aux = VDEV_AUX_EXTERNAL;
5706 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
5709 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5720 * zpool clear <pool> [device]
5722 * Clear all errors associated with a pool or a particular device.
5725 zpool_do_clear(int argc, char **argv)
5729 boolean_t dryrun = B_FALSE;
5730 boolean_t do_rewind = B_FALSE;
5731 boolean_t xtreme_rewind = B_FALSE;
5732 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5733 nvlist_t *policy = NULL;
5734 zpool_handle_t *zhp;
5735 char *pool, *device;
5738 while ((c = getopt(argc, argv, "FnX")) != -1) {
5747 xtreme_rewind = B_TRUE;
5750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5760 (void) fprintf(stderr, gettext("missing pool name\n"));
5765 (void) fprintf(stderr, gettext("too many arguments\n"));
5769 if ((dryrun || xtreme_rewind) && !do_rewind) {
5770 (void) fprintf(stderr,
5771 gettext("-n or -X only meaningful with -F\n"));
5775 rewind_policy = ZPOOL_TRY_REWIND;
5777 rewind_policy = ZPOOL_DO_REWIND;
5779 rewind_policy |= ZPOOL_EXTREME_REWIND;
5781 /* In future, further rewind policy choices can be passed along here */
5782 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5783 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5787 device = argc == 2 ? argv[1] : NULL;
5789 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5790 nvlist_free(policy);
5794 if (zpool_clear(zhp, device, policy) != 0)
5799 nvlist_free(policy);
5805 * zpool reguid <pool>
5808 zpool_do_reguid(int argc, char **argv)
5812 zpool_handle_t *zhp;
5816 while ((c = getopt(argc, argv, "")) != -1) {
5819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5828 /* get pool name and check number of arguments */
5830 (void) fprintf(stderr, gettext("missing pool name\n"));
5835 (void) fprintf(stderr, gettext("too many arguments\n"));
5840 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5843 ret = zpool_reguid(zhp);
5851 * zpool reopen <pool>
5853 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5856 zpool_do_reopen(int argc, char **argv)
5860 boolean_t scrub_restart = B_TRUE;
5863 while ((c = getopt(argc, argv, "n")) != -1) {
5866 scrub_restart = B_FALSE;
5869 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5878 /* if argc == 0 we will execute zpool_reopen_one on all pools */
5879 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
5885 typedef struct scrub_cbdata {
5889 pool_scrub_cmd_t cb_scrub_cmd;
5893 scrub_callback(zpool_handle_t *zhp, void *data)
5895 scrub_cbdata_t *cb = data;
5899 * Ignore faulted pools.
5901 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5902 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5903 "currently unavailable\n"), zpool_get_name(zhp));
5907 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
5913 * zpool scrub [-s | -p] <pool> ...
5915 * -s Stop. Stops any in-progress scrub.
5916 * -p Pause. Pause in-progress scrub.
5919 zpool_do_scrub(int argc, char **argv)
5924 cb.cb_type = POOL_SCAN_SCRUB;
5925 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
5928 while ((c = getopt(argc, argv, "sp")) != -1) {
5931 cb.cb_type = POOL_SCAN_NONE;
5934 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
5937 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5943 if (cb.cb_type == POOL_SCAN_NONE &&
5944 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
5945 (void) fprintf(stderr, gettext("invalid option combination: "
5946 "-s and -p are mutually exclusive\n"));
5956 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5960 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5964 * Print out detailed scrub status.
5967 print_scan_status(pool_scan_stat_t *ps)
5969 time_t start, end, pause;
5970 uint64_t elapsed, mins_left, hours_left;
5971 uint64_t pass_exam, examined, total;
5973 double fraction_done;
5974 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
5976 (void) printf(gettext(" scan: "));
5978 /* If there's never been a scan, there's not much to say. */
5979 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5980 ps->pss_func >= POOL_SCAN_FUNCS) {
5981 (void) printf(gettext("none requested\n"));
5985 start = ps->pss_start_time;
5986 end = ps->pss_end_time;
5987 pause = ps->pss_pass_scrub_pause;
5988 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
5990 assert(ps->pss_func == POOL_SCAN_SCRUB ||
5991 ps->pss_func == POOL_SCAN_RESILVER);
5993 * Scan is finished or canceled.
5995 if (ps->pss_state == DSS_FINISHED) {
5996 uint64_t minutes_taken = (end - start) / 60;
5999 if (ps->pss_func == POOL_SCAN_SCRUB) {
6000 fmt = gettext("scrub repaired %s in %lluh%um with "
6001 "%llu errors on %s");
6002 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6003 fmt = gettext("resilvered %s in %lluh%um with "
6004 "%llu errors on %s");
6007 (void) printf(fmt, processed_buf,
6008 (u_longlong_t)(minutes_taken / 60),
6009 (uint_t)(minutes_taken % 60),
6010 (u_longlong_t)ps->pss_errors,
6011 ctime((time_t *)&end));
6013 } else if (ps->pss_state == DSS_CANCELED) {
6014 if (ps->pss_func == POOL_SCAN_SCRUB) {
6015 (void) printf(gettext("scrub canceled on %s"),
6017 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6018 (void) printf(gettext("resilver canceled on %s"),
6024 assert(ps->pss_state == DSS_SCANNING);
6027 * Scan is in progress.
6029 if (ps->pss_func == POOL_SCAN_SCRUB) {
6031 (void) printf(gettext("scrub in progress since %s"),
6035 struct tm *p = localtime(&pause);
6036 (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
6037 (void) printf(gettext("scrub paused since %s\n"), buf);
6038 (void) printf(gettext("\tscrub started on %s"),
6041 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6042 (void) printf(gettext("resilver in progress since %s"),
6046 examined = ps->pss_examined ? ps->pss_examined : 1;
6047 total = ps->pss_to_examine;
6048 fraction_done = (double)examined / total;
6050 /* elapsed time for this pass */
6051 elapsed = time(NULL) - ps->pss_pass_start;
6052 elapsed -= ps->pss_pass_scrub_spent_paused;
6053 elapsed = elapsed ? elapsed : 1;
6054 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
6055 rate = pass_exam / elapsed;
6056 rate = rate ? rate : 1;
6057 mins_left = ((total - examined) / rate) / 60;
6058 hours_left = mins_left / 60;
6060 zfs_nicebytes(examined, examined_buf, sizeof (examined_buf));
6061 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6064 * do not print estimated time if hours_left is more than 30 days
6065 * or we have a paused scrub
6068 zfs_nicebytes(rate, rate_buf, sizeof (rate_buf));
6069 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
6070 examined_buf, total_buf, rate_buf);
6071 if (hours_left < (30 * 24)) {
6072 (void) printf(gettext(", %lluh%um to go\n"),
6073 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
6075 (void) printf(gettext(
6076 ", (scan is slow, no estimated time)\n"));
6079 (void) printf(gettext("\t%s scanned out of %s\n"),
6080 examined_buf, total_buf);
6083 if (ps->pss_func == POOL_SCAN_RESILVER) {
6084 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
6085 processed_buf, 100 * fraction_done);
6086 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6087 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
6088 processed_buf, 100 * fraction_done);
6093 print_error_log(zpool_handle_t *zhp)
6095 nvlist_t *nverrlist = NULL;
6098 size_t len = MAXPATHLEN * 2;
6100 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6103 (void) printf("errors: Permanent errors have been "
6104 "detected in the following files:\n\n");
6106 pathname = safe_malloc(len);
6108 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6110 uint64_t dsobj, obj;
6112 verify(nvpair_value_nvlist(elem, &nv) == 0);
6113 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6115 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6117 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6118 (void) printf("%7s %s\n", "", pathname);
6121 nvlist_free(nverrlist);
6125 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6134 (void) printf(gettext("\tspares\n"));
6136 for (i = 0; i < nspares; i++) {
6137 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6139 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6145 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6154 (void) printf(gettext("\tcache\n"));
6156 for (i = 0; i < nl2cache; i++) {
6157 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6159 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6165 print_dedup_stats(nvlist_t *config)
6167 ddt_histogram_t *ddh;
6171 char dspace[6], mspace[6];
6174 * If the pool was faulted then we may not have been able to
6175 * obtain the config. Otherwise, if we have anything in the dedup
6176 * table continue processing the stats.
6178 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6179 (uint64_t **)&ddo, &c) != 0)
6182 (void) printf("\n");
6183 (void) printf(gettext(" dedup: "));
6184 if (ddo->ddo_count == 0) {
6185 (void) printf(gettext("no DDT entries\n"));
6189 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6190 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6191 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6192 (u_longlong_t)ddo->ddo_count,
6196 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6197 (uint64_t **)&dds, &c) == 0);
6198 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6199 (uint64_t **)&ddh, &c) == 0);
6200 zpool_dump_ddt(dds, ddh);
6204 * Display a summary of pool status. Displays a summary such as:
6208 * reason: One or more devices ...
6209 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6215 * When given the '-v' option, we print out the complete config. If the '-e'
6216 * option is specified, then we print out error rate information as well.
6219 status_callback(zpool_handle_t *zhp, void *data)
6221 status_cbdata_t *cbp = data;
6222 nvlist_t *config, *nvroot;
6224 zpool_status_t reason;
6225 zpool_errata_t errata;
6230 config = zpool_get_config(zhp, NULL);
6231 reason = zpool_get_status(zhp, &msgid, &errata);
6236 * If we were given 'zpool status -x', only report those pools with
6239 if (cbp->cb_explain &&
6240 (reason == ZPOOL_STATUS_OK ||
6241 reason == ZPOOL_STATUS_VERSION_OLDER ||
6242 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6243 if (!cbp->cb_allpools) {
6244 (void) printf(gettext("pool '%s' is healthy\n"),
6245 zpool_get_name(zhp));
6247 cbp->cb_first = B_FALSE;
6253 cbp->cb_first = B_FALSE;
6255 (void) printf("\n");
6257 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6259 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6260 (uint64_t **)&vs, &c) == 0);
6261 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6263 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6264 (void) printf(gettext(" state: %s\n"), health);
6267 case ZPOOL_STATUS_MISSING_DEV_R:
6268 (void) printf(gettext("status: One or more devices could not "
6269 "be opened. Sufficient replicas exist for\n\tthe pool to "
6270 "continue functioning in a degraded state.\n"));
6271 (void) printf(gettext("action: Attach the missing device and "
6272 "online it using 'zpool online'.\n"));
6275 case ZPOOL_STATUS_MISSING_DEV_NR:
6276 (void) printf(gettext("status: One or more devices could not "
6277 "be opened. There are insufficient\n\treplicas for the "
6278 "pool to continue functioning.\n"));
6279 (void) printf(gettext("action: Attach the missing device and "
6280 "online it using 'zpool online'.\n"));
6283 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6284 (void) printf(gettext("status: One or more devices could not "
6285 "be used because the label is missing or\n\tinvalid. "
6286 "Sufficient replicas exist for the pool to continue\n\t"
6287 "functioning in a degraded state.\n"));
6288 (void) printf(gettext("action: Replace the device using "
6289 "'zpool replace'.\n"));
6292 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6293 (void) printf(gettext("status: One or more devices could not "
6294 "be used because the label is missing \n\tor invalid. "
6295 "There are insufficient replicas for the pool to "
6296 "continue\n\tfunctioning.\n"));
6297 zpool_explain_recover(zpool_get_handle(zhp),
6298 zpool_get_name(zhp), reason, config);
6301 case ZPOOL_STATUS_FAILING_DEV:
6302 (void) printf(gettext("status: One or more devices has "
6303 "experienced an unrecoverable error. An\n\tattempt was "
6304 "made to correct the error. Applications are "
6306 (void) printf(gettext("action: Determine if the device needs "
6307 "to be replaced, and clear the errors\n\tusing "
6308 "'zpool clear' or replace the device with 'zpool "
6312 case ZPOOL_STATUS_OFFLINE_DEV:
6313 (void) printf(gettext("status: One or more devices has "
6314 "been taken offline by the administrator.\n\tSufficient "
6315 "replicas exist for the pool to continue functioning in "
6316 "a\n\tdegraded state.\n"));
6317 (void) printf(gettext("action: Online the device using "
6318 "'zpool online' or replace the device with\n\t'zpool "
6322 case ZPOOL_STATUS_REMOVED_DEV:
6323 (void) printf(gettext("status: One or more devices has "
6324 "been removed by the administrator.\n\tSufficient "
6325 "replicas exist for the pool to continue functioning in "
6326 "a\n\tdegraded state.\n"));
6327 (void) printf(gettext("action: Online the device using "
6328 "'zpool online' or replace the device with\n\t'zpool "
6332 case ZPOOL_STATUS_RESILVERING:
6333 (void) printf(gettext("status: One or more devices is "
6334 "currently being resilvered. The pool will\n\tcontinue "
6335 "to function, possibly in a degraded state.\n"));
6336 (void) printf(gettext("action: Wait for the resilver to "
6340 case ZPOOL_STATUS_CORRUPT_DATA:
6341 (void) printf(gettext("status: One or more devices has "
6342 "experienced an error resulting in data\n\tcorruption. "
6343 "Applications may be affected.\n"));
6344 (void) printf(gettext("action: Restore the file in question "
6345 "if possible. Otherwise restore the\n\tentire pool from "
6349 case ZPOOL_STATUS_CORRUPT_POOL:
6350 (void) printf(gettext("status: The pool metadata is corrupted "
6351 "and the pool cannot be opened.\n"));
6352 zpool_explain_recover(zpool_get_handle(zhp),
6353 zpool_get_name(zhp), reason, config);
6356 case ZPOOL_STATUS_VERSION_OLDER:
6357 (void) printf(gettext("status: The pool is formatted using a "
6358 "legacy on-disk format. The pool can\n\tstill be used, "
6359 "but some features are unavailable.\n"));
6360 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6361 "upgrade'. Once this is done, the\n\tpool will no longer "
6362 "be accessible on software that does not support\n\t"
6363 "feature flags.\n"));
6366 case ZPOOL_STATUS_VERSION_NEWER:
6367 (void) printf(gettext("status: The pool has been upgraded to a "
6368 "newer, incompatible on-disk version.\n\tThe pool cannot "
6369 "be accessed on this system.\n"));
6370 (void) printf(gettext("action: Access the pool from a system "
6371 "running more recent software, or\n\trestore the pool from "
6375 case ZPOOL_STATUS_FEAT_DISABLED:
6376 (void) printf(gettext("status: Some supported features are not "
6377 "enabled on the pool. The pool can\n\tstill be used, but "
6378 "some features are unavailable.\n"));
6379 (void) printf(gettext("action: Enable all features using "
6380 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6381 "longer be accessible by software that does not support\n\t"
6382 "the features. See zpool-features(5) for details.\n"));
6385 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6386 (void) printf(gettext("status: The pool cannot be accessed on "
6387 "this system because it uses the\n\tfollowing feature(s) "
6388 "not supported on this system:\n"));
6389 zpool_print_unsup_feat(config);
6390 (void) printf("\n");
6391 (void) printf(gettext("action: Access the pool from a system "
6392 "that supports the required feature(s),\n\tor restore the "
6393 "pool from backup.\n"));
6396 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6397 (void) printf(gettext("status: The pool can only be accessed "
6398 "in read-only mode on this system. It\n\tcannot be "
6399 "accessed in read-write mode because it uses the "
6400 "following\n\tfeature(s) not supported on this system:\n"));
6401 zpool_print_unsup_feat(config);
6402 (void) printf("\n");
6403 (void) printf(gettext("action: The pool cannot be accessed in "
6404 "read-write mode. Import the pool with\n"
6405 "\t\"-o readonly=on\", access the pool from a system that "
6406 "supports the\n\trequired feature(s), or restore the "
6407 "pool from backup.\n"));
6410 case ZPOOL_STATUS_FAULTED_DEV_R:
6411 (void) printf(gettext("status: One or more devices are "
6412 "faulted in response to persistent errors.\n\tSufficient "
6413 "replicas exist for the pool to continue functioning "
6414 "in a\n\tdegraded state.\n"));
6415 (void) printf(gettext("action: Replace the faulted device, "
6416 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6419 case ZPOOL_STATUS_FAULTED_DEV_NR:
6420 (void) printf(gettext("status: One or more devices are "
6421 "faulted in response to persistent errors. There are "
6422 "insufficient replicas for the pool to\n\tcontinue "
6424 (void) printf(gettext("action: Destroy and re-create the pool "
6425 "from a backup source. Manually marking the device\n"
6426 "\trepaired using 'zpool clear' may allow some data "
6427 "to be recovered.\n"));
6430 case ZPOOL_STATUS_IO_FAILURE_WAIT:
6431 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6432 (void) printf(gettext("status: One or more devices are "
6433 "faulted in response to IO failures.\n"));
6434 (void) printf(gettext("action: Make sure the affected devices "
6435 "are connected, then run 'zpool clear'.\n"));
6438 case ZPOOL_STATUS_BAD_LOG:
6439 (void) printf(gettext("status: An intent log record "
6440 "could not be read.\n"
6441 "\tWaiting for administrator intervention to fix the "
6442 "faulted pool.\n"));
6443 (void) printf(gettext("action: Either restore the affected "
6444 "device(s) and run 'zpool online',\n"
6445 "\tor ignore the intent log records by running "
6446 "'zpool clear'.\n"));
6449 case ZPOOL_STATUS_HOSTID_MISMATCH:
6450 (void) printf(gettext("status: Mismatch between pool hostid "
6451 "and system hostid on imported pool.\n\tThis pool was "
6452 "previously imported into a system with a different "
6453 "hostid,\n\tand then was verbatim imported into this "
6455 (void) printf(gettext("action: Export this pool on all systems "
6456 "on which it is imported.\n"
6457 "\tThen import it to correct the mismatch.\n"));
6460 case ZPOOL_STATUS_ERRATA:
6461 (void) printf(gettext("status: Errata #%d detected.\n"),
6465 case ZPOOL_ERRATA_NONE:
6468 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
6469 (void) printf(gettext("action: To correct the issue "
6470 "run 'zpool scrub'.\n"));
6475 * All errata which allow the pool to be imported
6476 * must contain an action message.
6484 * The remaining errors can't actually be generated, yet.
6486 assert(reason == ZPOOL_STATUS_OK);
6490 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6493 if (config != NULL) {
6495 nvlist_t **spares, **l2cache;
6496 uint_t nspares, nl2cache;
6497 pool_scan_stat_t *ps = NULL;
6499 (void) nvlist_lookup_uint64_array(nvroot,
6500 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6501 print_scan_status(ps);
6503 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6504 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6505 if (cbp->cb_namewidth < 10)
6506 cbp->cb_namewidth = 10;
6508 (void) printf(gettext("config:\n\n"));
6509 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6510 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6513 if (cbp->vcdl != NULL)
6514 print_cmd_columns(cbp->vcdl, 0);
6517 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6520 if (num_logs(nvroot) > 0)
6521 print_logs(zhp, cbp, nvroot);
6522 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6523 &l2cache, &nl2cache) == 0)
6524 print_l2cache(zhp, cbp, l2cache, nl2cache);
6526 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6527 &spares, &nspares) == 0)
6528 print_spares(zhp, cbp, spares, nspares);
6530 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6532 nvlist_t *nverrlist = NULL;
6535 * If the approximate error count is small, get a
6536 * precise count by fetching the entire log and
6537 * uniquifying the results.
6539 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6540 zpool_get_errlog(zhp, &nverrlist) == 0) {
6545 while ((elem = nvlist_next_nvpair(nverrlist,
6550 nvlist_free(nverrlist);
6552 (void) printf("\n");
6555 (void) printf(gettext("errors: No known data "
6557 else if (!cbp->cb_verbose)
6558 (void) printf(gettext("errors: %llu data "
6559 "errors, use '-v' for a list\n"),
6560 (u_longlong_t)nerr);
6562 print_error_log(zhp);
6565 if (cbp->cb_dedup_stats)
6566 print_dedup_stats(config);
6568 (void) printf(gettext("config: The configuration cannot be "
6576 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6577 * [interval [count]]
6579 * -c CMD For each vdev, run command CMD
6580 * -g Display guid for individual vdev name.
6581 * -L Follow links when resolving vdev path name.
6582 * -P Display full path for vdev name.
6583 * -v Display complete error logs
6584 * -x Display only pools with potential problems
6585 * -D Display dedup status (undocumented)
6586 * -T Display a timestamp in date(1) or Unix format
6588 * Describes the health status of all pools or some subset.
6591 zpool_do_status(int argc, char **argv)
6596 unsigned long count = 0;
6597 status_cbdata_t cb = { 0 };
6601 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6606 gettext("Can't set -c flag twice\n"));
6610 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6611 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6612 fprintf(stderr, gettext(
6613 "Can't run -c, disabled by "
6614 "ZPOOL_SCRIPTS_ENABLED.\n"));
6618 if ((getuid() <= 0 || geteuid() <= 0) &&
6619 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6620 fprintf(stderr, gettext(
6621 "Can't run -c with root privileges "
6622 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6628 cb.cb_name_flags |= VDEV_NAME_GUID;
6631 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6634 cb.cb_name_flags |= VDEV_NAME_PATH;
6637 cb.cb_verbose = B_TRUE;
6640 cb.cb_explain = B_TRUE;
6643 cb.cb_dedup_stats = B_TRUE;
6646 get_timestamp_arg(*optarg);
6649 if (optopt == 'c') {
6650 print_zpool_script_list("status");
6654 gettext("invalid option '%c'\n"), optopt);
6663 get_interval_count(&argc, argv, &interval, &count);
6666 cb.cb_allpools = B_TRUE;
6668 cb.cb_first = B_TRUE;
6669 cb.cb_print_status = B_TRUE;
6672 if (timestamp_fmt != NODATE)
6673 print_timestamp(timestamp_fmt);
6676 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6679 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6680 status_callback, &cb);
6682 if (cb.vcdl != NULL)
6683 free_vdev_cmd_data_list(cb.vcdl);
6685 if (argc == 0 && cb.cb_count == 0)
6686 (void) fprintf(stderr, gettext("no pools available\n"));
6687 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6688 (void) printf(gettext("all pools are healthy\n"));
6696 if (count != 0 && --count == 0)
6699 (void) fsleep(interval);
6705 typedef struct upgrade_cbdata {
6708 uint64_t cb_version;
6713 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6715 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6716 int *count = (int *)unsupp_fs;
6718 if (zfs_version > ZPL_VERSION) {
6719 (void) printf(gettext("%s (v%d) is not supported by this "
6720 "implementation of ZFS.\n"),
6721 zfs_get_name(zhp), zfs_version);
6725 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6733 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6737 uint64_t oldversion;
6740 config = zpool_get_config(zhp, NULL);
6741 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6744 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6745 assert(oldversion < version);
6747 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6752 (void) fprintf(stderr, gettext("Upgrade not performed due "
6753 "to %d unsupported filesystems (max v%d).\n"),
6754 unsupp_fs, (int)ZPL_VERSION);
6758 ret = zpool_upgrade(zhp, version);
6762 if (version >= SPA_VERSION_FEATURES) {
6763 (void) printf(gettext("Successfully upgraded "
6764 "'%s' from version %llu to feature flags.\n"),
6765 zpool_get_name(zhp), (u_longlong_t)oldversion);
6767 (void) printf(gettext("Successfully upgraded "
6768 "'%s' from version %llu to version %llu.\n"),
6769 zpool_get_name(zhp), (u_longlong_t)oldversion,
6770 (u_longlong_t)version);
6777 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6780 boolean_t firstff = B_TRUE;
6781 nvlist_t *enabled = zpool_get_features(zhp);
6784 for (i = 0; i < SPA_FEATURES; i++) {
6785 const char *fname = spa_feature_table[i].fi_uname;
6786 const char *fguid = spa_feature_table[i].fi_guid;
6787 if (!nvlist_exists(enabled, fguid)) {
6789 verify(-1 != asprintf(&propname, "feature@%s", fname));
6790 ret = zpool_set_prop(zhp, propname,
6791 ZFS_FEATURE_ENABLED);
6799 (void) printf(gettext("Enabled the "
6800 "following features on '%s':\n"),
6801 zpool_get_name(zhp));
6804 (void) printf(gettext(" %s\n"), fname);
6815 upgrade_cb(zpool_handle_t *zhp, void *arg)
6817 upgrade_cbdata_t *cbp = arg;
6820 boolean_t printnl = B_FALSE;
6823 config = zpool_get_config(zhp, NULL);
6824 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6827 assert(SPA_VERSION_IS_SUPPORTED(version));
6829 if (version < cbp->cb_version) {
6830 cbp->cb_first = B_FALSE;
6831 ret = upgrade_version(zhp, cbp->cb_version);
6837 * If they did "zpool upgrade -a", then we could
6838 * be doing ioctls to different pools. We need
6839 * to log this history once to each pool, and bypass
6840 * the normal history logging that happens in main().
6842 (void) zpool_log_history(g_zfs, history_str);
6843 log_history = B_FALSE;
6846 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6848 ret = upgrade_enable_all(zhp, &count);
6853 cbp->cb_first = B_FALSE;
6859 (void) printf(gettext("\n"));
6866 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6868 upgrade_cbdata_t *cbp = arg;
6872 config = zpool_get_config(zhp, NULL);
6873 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6876 assert(SPA_VERSION_IS_SUPPORTED(version));
6878 if (version < SPA_VERSION_FEATURES) {
6879 if (cbp->cb_first) {
6880 (void) printf(gettext("The following pools are "
6881 "formatted with legacy version numbers and can\n"
6882 "be upgraded to use feature flags. After "
6883 "being upgraded, these pools\nwill no "
6884 "longer be accessible by software that does not "
6885 "support feature\nflags.\n\n"));
6886 (void) printf(gettext("VER POOL\n"));
6887 (void) printf(gettext("--- ------------\n"));
6888 cbp->cb_first = B_FALSE;
6891 (void) printf("%2llu %s\n", (u_longlong_t)version,
6892 zpool_get_name(zhp));
6899 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6901 upgrade_cbdata_t *cbp = arg;
6905 config = zpool_get_config(zhp, NULL);
6906 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6909 if (version >= SPA_VERSION_FEATURES) {
6911 boolean_t poolfirst = B_TRUE;
6912 nvlist_t *enabled = zpool_get_features(zhp);
6914 for (i = 0; i < SPA_FEATURES; i++) {
6915 const char *fguid = spa_feature_table[i].fi_guid;
6916 const char *fname = spa_feature_table[i].fi_uname;
6917 if (!nvlist_exists(enabled, fguid)) {
6918 if (cbp->cb_first) {
6919 (void) printf(gettext("\nSome "
6920 "supported features are not "
6921 "enabled on the following pools. "
6922 "Once a\nfeature is enabled the "
6923 "pool may become incompatible with "
6924 "software\nthat does not support "
6926 "zpool-features(5) for "
6928 (void) printf(gettext("POOL "
6930 (void) printf(gettext("------"
6932 cbp->cb_first = B_FALSE;
6936 (void) printf(gettext("%s\n"),
6937 zpool_get_name(zhp));
6938 poolfirst = B_FALSE;
6941 (void) printf(gettext(" %s\n"), fname);
6944 * If they did "zpool upgrade -a", then we could
6945 * be doing ioctls to different pools. We need
6946 * to log this history once to each pool, and bypass
6947 * the normal history logging that happens in main().
6949 (void) zpool_log_history(g_zfs, history_str);
6950 log_history = B_FALSE;
6959 upgrade_one(zpool_handle_t *zhp, void *data)
6961 boolean_t printnl = B_FALSE;
6962 upgrade_cbdata_t *cbp = data;
6963 uint64_t cur_version;
6966 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6967 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6968 "Pool 'log' must be renamed using export and import"
6973 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6974 if (cur_version > cbp->cb_version) {
6975 (void) printf(gettext("Pool '%s' is already formatted "
6976 "using more current version '%llu'.\n\n"),
6977 zpool_get_name(zhp), (u_longlong_t)cur_version);
6981 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
6982 (void) printf(gettext("Pool '%s' is already formatted "
6983 "using version %llu.\n\n"), zpool_get_name(zhp),
6984 (u_longlong_t)cbp->cb_version);
6988 if (cur_version != cbp->cb_version) {
6990 ret = upgrade_version(zhp, cbp->cb_version);
6995 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6997 ret = upgrade_enable_all(zhp, &count);
7003 } else if (cur_version == SPA_VERSION) {
7004 (void) printf(gettext("Pool '%s' already has all "
7005 "supported features enabled.\n"),
7006 zpool_get_name(zhp));
7011 (void) printf(gettext("\n"));
7020 * zpool upgrade [-V version] <-a | pool ...>
7022 * With no arguments, display downrev'd ZFS pool available for upgrade.
7023 * Individual pools can be upgraded by specifying the pool, and '-a' will
7024 * upgrade all pools.
7027 zpool_do_upgrade(int argc, char **argv)
7030 upgrade_cbdata_t cb = { 0 };
7032 boolean_t showversions = B_FALSE;
7033 boolean_t upgradeall = B_FALSE;
7038 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7041 upgradeall = B_TRUE;
7044 showversions = B_TRUE;
7047 cb.cb_version = strtoll(optarg, &end, 10);
7049 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7050 (void) fprintf(stderr,
7051 gettext("invalid version '%s'\n"), optarg);
7056 (void) fprintf(stderr, gettext("missing argument for "
7057 "'%c' option\n"), optopt);
7061 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7072 if (cb.cb_version == 0) {
7073 cb.cb_version = SPA_VERSION;
7074 } else if (!upgradeall && argc == 0) {
7075 (void) fprintf(stderr, gettext("-V option is "
7076 "incompatible with other arguments\n"));
7081 if (upgradeall || argc != 0) {
7082 (void) fprintf(stderr, gettext("-v option is "
7083 "incompatible with other arguments\n"));
7086 } else if (upgradeall) {
7088 (void) fprintf(stderr, gettext("-a option should not "
7089 "be used along with a pool name\n"));
7094 (void) printf(gettext("This system supports ZFS pool feature "
7099 (void) printf(gettext("The following features are "
7101 (void) printf(gettext("FEAT DESCRIPTION\n"));
7102 (void) printf("----------------------------------------------"
7103 "---------------\n");
7104 for (i = 0; i < SPA_FEATURES; i++) {
7105 zfeature_info_t *fi = &spa_feature_table[i];
7107 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7108 " (read-only compatible)" : "";
7110 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7111 (void) printf(" %s\n", fi->fi_desc);
7113 (void) printf("\n");
7115 (void) printf(gettext("The following legacy versions are also "
7117 (void) printf(gettext("VER DESCRIPTION\n"));
7118 (void) printf("--- -----------------------------------------"
7119 "---------------\n");
7120 (void) printf(gettext(" 1 Initial ZFS version\n"));
7121 (void) printf(gettext(" 2 Ditto blocks "
7122 "(replicated metadata)\n"));
7123 (void) printf(gettext(" 3 Hot spares and double parity "
7125 (void) printf(gettext(" 4 zpool history\n"));
7126 (void) printf(gettext(" 5 Compression using the gzip "
7128 (void) printf(gettext(" 6 bootfs pool property\n"));
7129 (void) printf(gettext(" 7 Separate intent log devices\n"));
7130 (void) printf(gettext(" 8 Delegated administration\n"));
7131 (void) printf(gettext(" 9 refquota and refreservation "
7133 (void) printf(gettext(" 10 Cache devices\n"));
7134 (void) printf(gettext(" 11 Improved scrub performance\n"));
7135 (void) printf(gettext(" 12 Snapshot properties\n"));
7136 (void) printf(gettext(" 13 snapused property\n"));
7137 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7138 (void) printf(gettext(" 15 user/group space accounting\n"));
7139 (void) printf(gettext(" 16 stmf property support\n"));
7140 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7141 (void) printf(gettext(" 18 Snapshot user holds\n"));
7142 (void) printf(gettext(" 19 Log device removal\n"));
7143 (void) printf(gettext(" 20 Compression using zle "
7144 "(zero-length encoding)\n"));
7145 (void) printf(gettext(" 21 Deduplication\n"));
7146 (void) printf(gettext(" 22 Received properties\n"));
7147 (void) printf(gettext(" 23 Slim ZIL\n"));
7148 (void) printf(gettext(" 24 System attributes\n"));
7149 (void) printf(gettext(" 25 Improved scrub stats\n"));
7150 (void) printf(gettext(" 26 Improved snapshot deletion "
7152 (void) printf(gettext(" 27 Improved snapshot creation "
7154 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7155 (void) printf(gettext("\nFor more information on a particular "
7156 "version, including supported releases,\n"));
7157 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7158 } else if (argc == 0 && upgradeall) {
7159 cb.cb_first = B_TRUE;
7160 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7161 if (ret == 0 && cb.cb_first) {
7162 if (cb.cb_version == SPA_VERSION) {
7163 (void) printf(gettext("All pools are already "
7164 "formatted using feature flags.\n\n"));
7165 (void) printf(gettext("Every feature flags "
7166 "pool already has all supported features "
7169 (void) printf(gettext("All pools are already "
7170 "formatted with version %llu or higher.\n"),
7171 (u_longlong_t)cb.cb_version);
7174 } else if (argc == 0) {
7175 cb.cb_first = B_TRUE;
7176 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7180 (void) printf(gettext("All pools are formatted "
7181 "using feature flags.\n\n"));
7183 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7184 "for a list of available legacy versions.\n"));
7187 cb.cb_first = B_TRUE;
7188 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7192 (void) printf(gettext("Every feature flags pool has "
7193 "all supported features enabled.\n"));
7195 (void) printf(gettext("\n"));
7198 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7205 typedef struct hist_cbdata {
7212 * Print out the command history for a specific pool.
7215 get_history_one(zpool_handle_t *zhp, void *data)
7221 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7223 cb->first = B_FALSE;
7225 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7227 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7230 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7231 &records, &numrecords) == 0);
7232 for (i = 0; i < numrecords; i++) {
7233 nvlist_t *rec = records[i];
7236 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7240 tsec = fnvlist_lookup_uint64(records[i],
7242 (void) localtime_r(&tsec, &t);
7243 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7246 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7247 (void) printf("%s %s", tbuf,
7248 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7249 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7251 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7254 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7255 (void) printf("%s unrecognized record:\n",
7257 dump_nvlist(rec, 4);
7260 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7261 zfs_history_event_names[ievent],
7262 (longlong_t)fnvlist_lookup_uint64(
7263 rec, ZPOOL_HIST_TXG),
7264 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7265 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7268 (void) printf("%s [txg:%lld] %s", tbuf,
7269 (longlong_t)fnvlist_lookup_uint64(
7270 rec, ZPOOL_HIST_TXG),
7271 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7272 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7273 (void) printf(" %s (%llu)",
7274 fnvlist_lookup_string(rec,
7276 (u_longlong_t)fnvlist_lookup_uint64(rec,
7279 (void) printf(" %s", fnvlist_lookup_string(rec,
7280 ZPOOL_HIST_INT_STR));
7281 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7284 (void) printf("%s ioctl %s\n", tbuf,
7285 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7286 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7287 (void) printf(" input:\n");
7288 dump_nvlist(fnvlist_lookup_nvlist(rec,
7289 ZPOOL_HIST_INPUT_NVL), 8);
7291 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7292 (void) printf(" output:\n");
7293 dump_nvlist(fnvlist_lookup_nvlist(rec,
7294 ZPOOL_HIST_OUTPUT_NVL), 8);
7299 (void) printf("%s unrecognized record:\n", tbuf);
7300 dump_nvlist(rec, 4);
7304 (void) printf("\n");
7307 (void) printf(" [");
7308 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7309 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7310 struct passwd *pwd = getpwuid(who);
7311 (void) printf("user %d ", (int)who);
7313 (void) printf("(%s) ", pwd->pw_name);
7315 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7316 (void) printf("on %s",
7317 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7319 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7320 (void) printf(":%s",
7321 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7325 (void) printf("\n");
7327 (void) printf("\n");
7334 * zpool history <pool>
7336 * Displays the history of commands that modified pools.
7339 zpool_do_history(int argc, char **argv)
7341 hist_cbdata_t cbdata = { 0 };
7345 cbdata.first = B_TRUE;
7347 while ((c = getopt(argc, argv, "li")) != -1) {
7350 cbdata.longfmt = B_TRUE;
7353 cbdata.internal = B_TRUE;
7356 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7364 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
7367 if (argc == 0 && cbdata.first == B_TRUE) {
7368 (void) fprintf(stderr, gettext("no pools available\n"));
7375 typedef struct ev_opts {
7380 char poolname[ZFS_MAX_DATASET_NAME_LEN];
7384 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7386 char ctime_str[26], str[32], *ptr;
7390 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7391 memset(str, ' ', 32);
7392 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7393 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
7394 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
7395 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
7396 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7398 (void) printf(gettext("%s\t"), str);
7400 (void) printf(gettext("%s "), str);
7402 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7403 (void) printf(gettext("%s\n"), ptr);
7407 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7411 for (nvp = nvlist_next_nvpair(nvl, NULL);
7412 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
7414 data_type_t type = nvpair_type(nvp);
7415 const char *name = nvpair_name(nvp);
7425 printf(gettext("%*s%s = "), depth, "", name);
7428 case DATA_TYPE_BOOLEAN:
7429 printf(gettext("%s"), "1");
7432 case DATA_TYPE_BOOLEAN_VALUE:
7433 (void) nvpair_value_boolean_value(nvp, &b);
7434 printf(gettext("%s"), b ? "1" : "0");
7437 case DATA_TYPE_BYTE:
7438 (void) nvpair_value_byte(nvp, &i8);
7439 printf(gettext("0x%x"), i8);
7442 case DATA_TYPE_INT8:
7443 (void) nvpair_value_int8(nvp, (void *)&i8);
7444 printf(gettext("0x%x"), i8);
7447 case DATA_TYPE_UINT8:
7448 (void) nvpair_value_uint8(nvp, &i8);
7449 printf(gettext("0x%x"), i8);
7452 case DATA_TYPE_INT16:
7453 (void) nvpair_value_int16(nvp, (void *)&i16);
7454 printf(gettext("0x%x"), i16);
7457 case DATA_TYPE_UINT16:
7458 (void) nvpair_value_uint16(nvp, &i16);
7459 printf(gettext("0x%x"), i16);
7462 case DATA_TYPE_INT32:
7463 (void) nvpair_value_int32(nvp, (void *)&i32);
7464 printf(gettext("0x%x"), i32);
7467 case DATA_TYPE_UINT32:
7468 (void) nvpair_value_uint32(nvp, &i32);
7469 printf(gettext("0x%x"), i32);
7472 case DATA_TYPE_INT64:
7473 (void) nvpair_value_int64(nvp, (void *)&i64);
7474 printf(gettext("0x%llx"), (u_longlong_t)i64);
7477 case DATA_TYPE_UINT64:
7478 (void) nvpair_value_uint64(nvp, &i64);
7480 * translate vdev state values to readable
7481 * strings to aide zpool events consumers
7484 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
7486 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
7487 printf(gettext("\"%s\" (0x%llx)"),
7488 zpool_state_to_name(i64, VDEV_AUX_NONE),
7491 printf(gettext("0x%llx"), (u_longlong_t)i64);
7495 case DATA_TYPE_HRTIME:
7496 (void) nvpair_value_hrtime(nvp, (void *)&i64);
7497 printf(gettext("0x%llx"), (u_longlong_t)i64);
7500 case DATA_TYPE_STRING:
7501 (void) nvpair_value_string(nvp, &str);
7502 printf(gettext("\"%s\""), str ? str : "<NULL>");
7505 case DATA_TYPE_NVLIST:
7506 printf(gettext("(embedded nvlist)\n"));
7507 (void) nvpair_value_nvlist(nvp, &cnv);
7508 zpool_do_events_nvprint(cnv, depth + 8);
7509 printf(gettext("%*s(end %s)"), depth, "", name);
7512 case DATA_TYPE_NVLIST_ARRAY: {
7516 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7517 printf(gettext("(%d embedded nvlists)\n"), nelem);
7518 for (i = 0; i < nelem; i++) {
7519 printf(gettext("%*s%s[%d] = %s\n"),
7520 depth, "", name, i, "(embedded nvlist)");
7521 zpool_do_events_nvprint(val[i], depth + 8);
7522 printf(gettext("%*s(end %s[%i])\n"),
7523 depth, "", name, i);
7525 printf(gettext("%*s(end %s)\n"), depth, "", name);
7529 case DATA_TYPE_INT8_ARRAY: {
7533 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7534 for (i = 0; i < nelem; i++)
7535 printf(gettext("0x%x "), val[i]);
7540 case DATA_TYPE_UINT8_ARRAY: {
7544 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7545 for (i = 0; i < nelem; i++)
7546 printf(gettext("0x%x "), val[i]);
7551 case DATA_TYPE_INT16_ARRAY: {
7555 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7556 for (i = 0; i < nelem; i++)
7557 printf(gettext("0x%x "), val[i]);
7562 case DATA_TYPE_UINT16_ARRAY: {
7566 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7567 for (i = 0; i < nelem; i++)
7568 printf(gettext("0x%x "), val[i]);
7573 case DATA_TYPE_INT32_ARRAY: {
7577 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7578 for (i = 0; i < nelem; i++)
7579 printf(gettext("0x%x "), val[i]);
7584 case DATA_TYPE_UINT32_ARRAY: {
7588 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7589 for (i = 0; i < nelem; i++)
7590 printf(gettext("0x%x "), val[i]);
7595 case DATA_TYPE_INT64_ARRAY: {
7599 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7600 for (i = 0; i < nelem; i++)
7601 printf(gettext("0x%llx "),
7602 (u_longlong_t)val[i]);
7607 case DATA_TYPE_UINT64_ARRAY: {
7611 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7612 for (i = 0; i < nelem; i++)
7613 printf(gettext("0x%llx "),
7614 (u_longlong_t)val[i]);
7619 case DATA_TYPE_STRING_ARRAY: {
7623 (void) nvpair_value_string_array(nvp, &str, &nelem);
7624 for (i = 0; i < nelem; i++)
7625 printf(gettext("\"%s\" "),
7626 str[i] ? str[i] : "<NULL>");
7631 case DATA_TYPE_BOOLEAN_ARRAY:
7632 case DATA_TYPE_BYTE_ARRAY:
7633 case DATA_TYPE_DOUBLE:
7634 case DATA_TYPE_UNKNOWN:
7635 printf(gettext("<unknown>"));
7639 printf(gettext("\n"));
7644 zpool_do_events_next(ev_opts_t *opts)
7647 int zevent_fd, ret, dropped;
7650 zevent_fd = open(ZFS_DEV, O_RDWR);
7651 VERIFY(zevent_fd >= 0);
7653 if (!opts->scripted)
7654 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7657 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7658 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7659 if (ret || nvl == NULL)
7663 (void) printf(gettext("dropped %d events\n"), dropped);
7665 if (strlen(opts->poolname) > 0 &&
7666 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
7667 strcmp(opts->poolname, pool) != 0)
7670 zpool_do_events_short(nvl, opts);
7672 if (opts->verbose) {
7673 zpool_do_events_nvprint(nvl, 8);
7674 printf(gettext("\n"));
7676 (void) fflush(stdout);
7681 VERIFY(0 == close(zevent_fd));
7687 zpool_do_events_clear(ev_opts_t *opts)
7691 ret = zpool_events_clear(g_zfs, &count);
7693 (void) printf(gettext("cleared %d events\n"), count);
7699 * zpool events [-vHf [pool] | -c]
7701 * Displays events logs by ZFS.
7704 zpool_do_events(int argc, char **argv)
7706 ev_opts_t opts = { 0 };
7711 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7726 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7735 (void) fprintf(stderr, gettext("too many arguments\n"));
7737 } else if (argc == 1) {
7738 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
7739 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
7740 (void) fprintf(stderr,
7741 gettext("invalid pool name '%s'\n"), opts.poolname);
7746 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
7748 (void) fprintf(stderr,
7749 gettext("invalid options combined with -c\n"));
7754 ret = zpool_do_events_clear(&opts);
7756 ret = zpool_do_events_next(&opts);
7762 get_callback(zpool_handle_t *zhp, void *data)
7764 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7765 char value[MAXNAMELEN];
7766 zprop_source_t srctype;
7769 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7772 * Skip the special fake placeholder. This will also skip
7773 * over the name property when 'all' is specified.
7775 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7776 pl == cbp->cb_proplist)
7779 if (pl->pl_prop == ZPROP_INVAL &&
7780 (zpool_prop_feature(pl->pl_user_prop) ||
7781 zpool_prop_unsupported(pl->pl_user_prop))) {
7782 srctype = ZPROP_SRC_LOCAL;
7784 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7785 value, sizeof (value)) == 0) {
7786 zprop_print_one_property(zpool_get_name(zhp),
7787 cbp, pl->pl_user_prop, value, srctype,
7791 if (zpool_get_prop(zhp, pl->pl_prop, value,
7792 sizeof (value), &srctype, cbp->cb_literal) != 0)
7795 zprop_print_one_property(zpool_get_name(zhp), cbp,
7796 zpool_prop_to_name(pl->pl_prop), value, srctype,
7804 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7806 * -H Scripted mode. Don't display headers, and separate properties
7808 * -o List of columns to display. Defaults to
7809 * "name,property,value,source".
7810 * -p Display values in parsable (exact) format.
7812 * Get properties of pools in the system. Output space statistics
7813 * for each one as well as other attributes.
7816 zpool_do_get(int argc, char **argv)
7818 zprop_get_cbdata_t cb = { 0 };
7819 zprop_list_t fake_name = { 0 };
7824 cb.cb_first = B_TRUE;
7827 * Set up default columns and sources.
7829 cb.cb_sources = ZPROP_SRC_ALL;
7830 cb.cb_columns[0] = GET_COL_NAME;
7831 cb.cb_columns[1] = GET_COL_PROPERTY;
7832 cb.cb_columns[2] = GET_COL_VALUE;
7833 cb.cb_columns[3] = GET_COL_SOURCE;
7834 cb.cb_type = ZFS_TYPE_POOL;
7837 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7840 cb.cb_literal = B_TRUE;
7843 cb.cb_scripted = B_TRUE;
7846 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7848 while (*optarg != '\0') {
7849 static char *col_subopts[] =
7850 { "name", "property", "value", "source",
7853 if (i == ZFS_GET_NCOLS) {
7854 (void) fprintf(stderr, gettext("too "
7855 "many fields given to -o "
7860 switch (getsubopt(&optarg, col_subopts,
7863 cb.cb_columns[i++] = GET_COL_NAME;
7866 cb.cb_columns[i++] = GET_COL_PROPERTY;
7869 cb.cb_columns[i++] = GET_COL_VALUE;
7872 cb.cb_columns[i++] = GET_COL_SOURCE;
7876 (void) fprintf(stderr,
7877 gettext("\"all\" conflicts "
7878 "with specific fields "
7879 "given to -o option\n"));
7882 cb.cb_columns[0] = GET_COL_NAME;
7883 cb.cb_columns[1] = GET_COL_PROPERTY;
7884 cb.cb_columns[2] = GET_COL_VALUE;
7885 cb.cb_columns[3] = GET_COL_SOURCE;
7889 (void) fprintf(stderr,
7890 gettext("invalid column name "
7897 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7907 (void) fprintf(stderr, gettext("missing property "
7912 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7913 ZFS_TYPE_POOL) != 0)
7919 if (cb.cb_proplist != NULL) {
7920 fake_name.pl_prop = ZPOOL_PROP_NAME;
7921 fake_name.pl_width = strlen(gettext("NAME"));
7922 fake_name.pl_next = cb.cb_proplist;
7923 cb.cb_proplist = &fake_name;
7926 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7929 if (cb.cb_proplist == &fake_name)
7930 zprop_free_list(fake_name.pl_next);
7932 zprop_free_list(cb.cb_proplist);
7937 typedef struct set_cbdata {
7940 boolean_t cb_any_successful;
7944 set_callback(zpool_handle_t *zhp, void *data)
7947 set_cbdata_t *cb = (set_cbdata_t *)data;
7949 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7952 cb->cb_any_successful = B_TRUE;
7958 zpool_do_set(int argc, char **argv)
7960 set_cbdata_t cb = { 0 };
7963 if (argc > 1 && argv[1][0] == '-') {
7964 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7970 (void) fprintf(stderr, gettext("missing property=value "
7976 (void) fprintf(stderr, gettext("missing pool name\n"));
7981 (void) fprintf(stderr, gettext("too many pool names\n"));
7985 cb.cb_propname = argv[1];
7986 cb.cb_value = strchr(cb.cb_propname, '=');
7987 if (cb.cb_value == NULL) {
7988 (void) fprintf(stderr, gettext("missing value in "
7989 "property=value argument\n"));
7993 *(cb.cb_value) = '\0';
7996 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8003 find_command_idx(char *command, int *idx)
8007 for (i = 0; i < NCOMMAND; i++) {
8008 if (command_table[i].name == NULL)
8011 if (strcmp(command, command_table[i].name) == 0) {
8020 main(int argc, char **argv)
8026 (void) setlocale(LC_ALL, "");
8027 (void) textdomain(TEXT_DOMAIN);
8033 * Make sure the user has specified some command.
8036 (void) fprintf(stderr, gettext("missing command\n"));
8045 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8048 if ((g_zfs = libzfs_init()) == NULL) {
8049 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8053 libzfs_print_on_error(g_zfs, B_TRUE);
8055 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8058 * Run the appropriate command.
8060 if (find_command_idx(cmdname, &i) == 0) {
8061 current_command = &command_table[i];
8062 ret = command_table[i].func(argc - 1, argv + 1);
8063 } else if (strchr(cmdname, '=')) {
8064 verify(find_command_idx("set", &i) == 0);
8065 current_command = &command_table[i];
8066 ret = command_table[i].func(argc, argv);
8067 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8069 * 'freeze' is a vile debugging abomination, so we treat
8072 zfs_cmd_t zc = {"\0"};
8074 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8075 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8077 (void) fprintf(stderr,
8078 gettext("failed to freeze pool: %d\n"), errno);
8084 (void) fprintf(stderr, gettext("unrecognized "
8085 "command '%s'\n"), cmdname);
8090 if (ret == 0 && log_history)
8091 (void) zpool_log_history(g_zfs, history_str);
8096 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8097 * for the purposes of running ::findleaks.
8099 if (getenv("ZFS_ABORT") != NULL) {
8100 (void) printf("dumping core by request\n");