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>
60 #include <sys/sysmacros.h>
66 #include "zpool_util.h"
67 #include "zfs_comutil.h"
68 #include "zfeature_common.h"
70 #include "statcommon.h"
72 static int zpool_do_create(int, char **);
73 static int zpool_do_destroy(int, char **);
75 static int zpool_do_add(int, char **);
76 static int zpool_do_remove(int, char **);
77 static int zpool_do_labelclear(int, char **);
79 static int zpool_do_list(int, char **);
80 static int zpool_do_iostat(int, char **);
81 static int zpool_do_status(int, char **);
83 static int zpool_do_online(int, char **);
84 static int zpool_do_offline(int, char **);
85 static int zpool_do_clear(int, char **);
86 static int zpool_do_reopen(int, char **);
88 static int zpool_do_reguid(int, char **);
90 static int zpool_do_attach(int, char **);
91 static int zpool_do_detach(int, char **);
92 static int zpool_do_replace(int, char **);
93 static int zpool_do_split(int, char **);
95 static int zpool_do_scrub(int, char **);
97 static int zpool_do_import(int, char **);
98 static int zpool_do_export(int, char **);
100 static int zpool_do_upgrade(int, char **);
102 static int zpool_do_history(int, char **);
103 static int zpool_do_events(int, char **);
105 static int zpool_do_get(int, char **);
106 static int zpool_do_set(int, char **);
108 static int zpool_do_sync(int, char **);
111 * These libumem hooks provide a reasonable set of defaults for the allocator's
112 * debugging facilities.
117 _umem_debug_init(void)
119 return ("default,verbose"); /* $UMEM_DEBUG setting */
123 _umem_logging_init(void)
125 return ("fail,contents"); /* $UMEM_LOGGING setting */
160 * Flags for stats to display with "zpool iostats"
168 IOS_COUNT, /* always last element */
171 /* iostat_type entries as bitmasks */
172 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
173 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
174 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
175 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
176 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
178 /* Mask of all the histo bits */
179 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
182 * Lookup table for iostat flags to nvlist names. Basically a list
183 * of all the nvlists a flag requires. Also specifies the order in
184 * which data gets printed in zpool iostat.
186 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
188 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
193 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
194 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
195 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
196 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
200 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
201 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
202 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
205 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
206 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
207 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
208 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
209 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
212 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
213 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
214 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
215 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
216 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
217 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
218 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
219 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
220 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
221 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
227 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
228 * Right now, only one histo bit is ever set at one time, so we can
229 * just do a highbit64(a)
231 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
233 typedef struct zpool_command {
235 int (*func)(int, char **);
240 * Master command table. Each ZFS command has a name, associated function, and
241 * usage message. The usage messages need to be internationalized, so we have
242 * to have a function to return the usage message based on a command index.
244 * These commands are organized according to how they are displayed in the usage
245 * message. An empty command (one with a NULL name) indicates an empty line in
246 * the generic usage message.
248 static zpool_command_t command_table[] = {
249 { "create", zpool_do_create, HELP_CREATE },
250 { "destroy", zpool_do_destroy, HELP_DESTROY },
252 { "add", zpool_do_add, HELP_ADD },
253 { "remove", zpool_do_remove, HELP_REMOVE },
255 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
257 { "list", zpool_do_list, HELP_LIST },
258 { "iostat", zpool_do_iostat, HELP_IOSTAT },
259 { "status", zpool_do_status, HELP_STATUS },
261 { "online", zpool_do_online, HELP_ONLINE },
262 { "offline", zpool_do_offline, HELP_OFFLINE },
263 { "clear", zpool_do_clear, HELP_CLEAR },
264 { "reopen", zpool_do_reopen, HELP_REOPEN },
266 { "attach", zpool_do_attach, HELP_ATTACH },
267 { "detach", zpool_do_detach, HELP_DETACH },
268 { "replace", zpool_do_replace, HELP_REPLACE },
269 { "split", zpool_do_split, HELP_SPLIT },
271 { "scrub", zpool_do_scrub, HELP_SCRUB },
273 { "import", zpool_do_import, HELP_IMPORT },
274 { "export", zpool_do_export, HELP_EXPORT },
275 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
276 { "reguid", zpool_do_reguid, HELP_REGUID },
278 { "history", zpool_do_history, HELP_HISTORY },
279 { "events", zpool_do_events, HELP_EVENTS },
281 { "get", zpool_do_get, HELP_GET },
282 { "set", zpool_do_set, HELP_SET },
283 { "sync", zpool_do_sync, HELP_SYNC },
286 #define NCOMMAND (ARRAY_SIZE(command_table))
288 static zpool_command_t *current_command;
289 static char history_str[HIS_MAX_RECORD_LEN];
290 static boolean_t log_history = B_TRUE;
291 static uint_t timestamp_fmt = NODATE;
294 get_usage(zpool_help_t idx)
298 return (gettext("\tadd [-fgLnP] [-o property=value] "
299 "<pool> <vdev> ...\n"));
301 return (gettext("\tattach [-f] [-o property=value] "
302 "<pool> <device> <new-device>\n"));
304 return (gettext("\tclear [-nF] <pool> [device]\n"));
306 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
307 "\t [-O file-system-property=value] ... \n"
308 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
310 return (gettext("\tdestroy [-f] <pool>\n"));
312 return (gettext("\tdetach <pool> <device>\n"));
314 return (gettext("\texport [-af] <pool> ...\n"));
316 return (gettext("\thistory [-il] [<pool>] ...\n"));
318 return (gettext("\timport [-d dir] [-D]\n"
319 "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
321 "\timport [-o mntopts] [-o property=value] ... \n"
322 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
323 "[-R root] [-F [-n]] -a\n"
324 "\timport [-o mntopts] [-o property=value] ... \n"
325 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
326 "[-R root] [-F [-n]]\n"
327 "\t <pool | id> [newpool]\n"));
329 return (gettext("\tiostat [[[-c [script1,script2,...]"
330 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
331 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
332 " [interval [count]]\n"));
333 case HELP_LABELCLEAR:
334 return (gettext("\tlabelclear [-f] <vdev>\n"));
336 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
337 "[-T d|u] [pool] ... [interval [count]]\n"));
339 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
341 return (gettext("\tonline <pool> <device> ...\n"));
343 return (gettext("\treplace [-f] [-o property=value] "
344 "<pool> <device> [new-device]\n"));
346 return (gettext("\tremove <pool> <device> ...\n"));
348 return (gettext("\treopen [-n] <pool>\n"));
350 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
352 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
353 "[-T d|u] [pool] ... [interval [count]]\n"));
355 return (gettext("\tupgrade\n"
357 "\tupgrade [-V version] <-a | pool ...>\n"));
359 return (gettext("\tevents [-vHf [pool] | -c]\n"));
361 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
362 "<\"all\" | property[,...]> <pool> ...\n"));
364 return (gettext("\tset <property=value> <pool> \n"));
366 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
367 "\t [-o property=value] <pool> <newpool> "
368 "[<device> ...]\n"));
370 return (gettext("\treguid <pool>\n"));
372 return (gettext("\tsync [pool] ...\n"));
381 * Callback routine that will print out a pool property value.
384 print_prop_cb(int prop, void *cb)
388 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
390 if (zpool_prop_readonly(prop))
391 (void) fprintf(fp, " NO ");
393 (void) fprintf(fp, " YES ");
395 if (zpool_prop_values(prop) == NULL)
396 (void) fprintf(fp, "-\n");
398 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
404 * Display usage message. If we're inside a command, display only the usage for
405 * that command. Otherwise, iterate over the entire command table and display
406 * a complete usage message.
409 usage(boolean_t requested)
411 FILE *fp = requested ? stdout : stderr;
413 if (current_command == NULL) {
416 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
418 gettext("where 'command' is one of the following:\n\n"));
420 for (i = 0; i < NCOMMAND; i++) {
421 if (command_table[i].name == NULL)
422 (void) fprintf(fp, "\n");
424 (void) fprintf(fp, "%s",
425 get_usage(command_table[i].usage));
428 (void) fprintf(fp, gettext("usage:\n"));
429 (void) fprintf(fp, "%s", get_usage(current_command->usage));
432 if (current_command != NULL &&
433 ((strcmp(current_command->name, "set") == 0) ||
434 (strcmp(current_command->name, "get") == 0) ||
435 (strcmp(current_command->name, "list") == 0))) {
438 gettext("\nthe following properties are supported:\n"));
440 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
441 "PROPERTY", "EDIT", "VALUES");
443 /* Iterate over all properties */
444 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
447 (void) fprintf(fp, "\t%-15s ", "feature@...");
448 (void) fprintf(fp, "YES disabled | enabled | active\n");
450 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
451 "appended with a feature name.\nSee zpool-features(5).\n"));
455 * See comments at end of main().
457 if (getenv("ZFS_ABORT") != NULL) {
458 (void) printf("dumping core by request\n");
462 exit(requested ? 0 : 2);
466 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
467 boolean_t print_logs, int name_flags)
474 (void) printf("\t%*s%s\n", indent, "", name);
476 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
477 &child, &children) != 0)
480 for (c = 0; c < children; c++) {
481 uint64_t is_log = B_FALSE;
483 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
485 if ((is_log && !print_logs) || (!is_log && print_logs))
488 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
489 print_vdev_tree(zhp, vname, child[c], indent + 2,
490 B_FALSE, name_flags);
496 prop_list_contains_feature(nvlist_t *proplist)
499 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
500 nvp = nvlist_next_nvpair(proplist, nvp)) {
501 if (zpool_prop_feature(nvpair_name(nvp)))
508 * Add a property pair (name, string-value) into a property nvlist.
511 add_prop_list(const char *propname, char *propval, nvlist_t **props,
514 zpool_prop_t prop = ZPROP_INVAL;
520 if (*props == NULL &&
521 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
522 (void) fprintf(stderr,
523 gettext("internal error: out of memory\n"));
530 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
532 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
533 !zpool_prop_feature(propname)) {
534 (void) fprintf(stderr, gettext("property '%s' is "
535 "not a valid pool property\n"), propname);
540 * feature@ properties and version should not be specified
543 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
544 nvlist_exists(proplist, vname)) ||
545 (prop == ZPOOL_PROP_VERSION &&
546 prop_list_contains_feature(proplist))) {
547 (void) fprintf(stderr, gettext("'feature@' and "
548 "'version' properties cannot be specified "
554 if (zpool_prop_feature(propname))
557 normnm = zpool_prop_to_name(prop);
559 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
560 normnm = zfs_prop_to_name(fprop);
566 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
567 prop != ZPOOL_PROP_CACHEFILE) {
568 (void) fprintf(stderr, gettext("property '%s' "
569 "specified multiple times\n"), propname);
573 if (nvlist_add_string(proplist, normnm, propval) != 0) {
574 (void) fprintf(stderr, gettext("internal "
575 "error: out of memory\n"));
583 * Set a default property pair (name, string-value) in a property nvlist
586 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
591 if (nvlist_lookup_string(*props, propname, &pval) == 0)
594 return (add_prop_list(propname, propval, props, B_TRUE));
598 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
600 * -f Force addition of devices, even if they appear in use
601 * -g Display guid for individual vdev name.
602 * -L Follow links when resolving vdev path name.
603 * -n Do not add the devices, but display the resulting layout if
604 * they were to be added.
605 * -o Set property=value.
606 * -P Display full path for vdev name.
608 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
609 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
613 zpool_do_add(int argc, char **argv)
615 boolean_t force = B_FALSE;
616 boolean_t dryrun = B_FALSE;
624 nvlist_t *props = NULL;
628 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
634 name_flags |= VDEV_NAME_GUID;
637 name_flags |= VDEV_NAME_FOLLOW_LINKS;
643 if ((propval = strchr(optarg, '=')) == NULL) {
644 (void) fprintf(stderr, gettext("missing "
645 "'=' for -o option\n"));
651 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
652 (add_prop_list(optarg, propval, &props, B_TRUE)))
656 name_flags |= VDEV_NAME_PATH;
659 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
668 /* get pool name and check number of arguments */
670 (void) fprintf(stderr, gettext("missing pool name argument\n"));
674 (void) fprintf(stderr, gettext("missing vdev specification\n"));
683 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
686 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
687 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
693 /* unless manually specified use "ashift" pool property (if set) */
694 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
697 char strval[ZPOOL_MAXPROPLEN];
699 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
700 if (src != ZPROP_SRC_DEFAULT) {
701 (void) sprintf(strval, "%" PRId32, intval);
702 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
703 &props, B_TRUE) == 0);
707 /* pass off to get_vdev_spec for processing */
708 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
710 if (nvroot == NULL) {
716 nvlist_t *poolnvroot;
718 uint_t l2children, c;
720 boolean_t hadcache = B_FALSE;
722 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
725 (void) printf(gettext("would update '%s' to the following "
726 "configuration:\n"), zpool_get_name(zhp));
728 /* print original main pool and new tree */
729 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
731 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
733 /* Do the same for the logs */
734 if (num_logs(poolnvroot) > 0) {
735 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
737 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
739 } else if (num_logs(nvroot) > 0) {
740 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
744 /* Do the same for the caches */
745 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
746 &l2child, &l2children) == 0 && l2children) {
748 (void) printf(gettext("\tcache\n"));
749 for (c = 0; c < l2children; c++) {
750 vname = zpool_vdev_name(g_zfs, NULL,
751 l2child[c], name_flags);
752 (void) printf("\t %s\n", vname);
756 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
757 &l2child, &l2children) == 0 && l2children) {
759 (void) printf(gettext("\tcache\n"));
760 for (c = 0; c < l2children; c++) {
761 vname = zpool_vdev_name(g_zfs, NULL,
762 l2child[c], name_flags);
763 (void) printf("\t %s\n", vname);
770 ret = (zpool_add(zhp, nvroot) != 0);
781 * zpool remove <pool> <vdev> ...
783 * Removes the given vdev from the pool. Currently, this supports removing
784 * spares, cache, and log devices from the pool.
787 zpool_do_remove(int argc, char **argv)
791 zpool_handle_t *zhp = NULL;
796 /* get pool name and check number of arguments */
798 (void) fprintf(stderr, gettext("missing pool name argument\n"));
802 (void) fprintf(stderr, gettext("missing device\n"));
808 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
811 for (i = 1; i < argc; i++) {
812 if (zpool_vdev_remove(zhp, argv[i]) != 0)
821 * zpool labelclear [-f] <vdev>
823 * -f Force clearing the label for the vdevs which are members of
824 * the exported or foreign pools.
826 * Verifies that the vdev is not active and zeros out the label information
830 zpool_do_labelclear(int argc, char **argv)
832 char vdev[MAXPATHLEN];
835 int c, fd = -1, ret = 0;
838 boolean_t inuse = B_FALSE;
839 boolean_t force = B_FALSE;
842 while ((c = getopt(argc, argv, "f")) != -1) {
848 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
859 (void) fprintf(stderr, gettext("missing vdev name\n"));
863 (void) fprintf(stderr, gettext("too many arguments\n"));
868 * Check if we were given absolute path and use it as is.
869 * Otherwise if the provided vdev name doesn't point to a file,
870 * try prepending expected disk paths and partition numbers.
872 (void) strlcpy(vdev, argv[0], sizeof (vdev));
873 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
876 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
877 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
878 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
882 if (error || (stat(vdev, &st) != 0)) {
883 (void) fprintf(stderr, gettext(
884 "failed to find device %s, try specifying absolute "
885 "path instead\n"), argv[0]);
890 if ((fd = open(vdev, O_RDWR)) < 0) {
891 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
892 vdev, strerror(errno));
896 if (ioctl(fd, BLKFLSBUF) != 0)
897 (void) fprintf(stderr, gettext("failed to invalidate "
898 "cache for %s: %s\n"), vdev, strerror(errno));
900 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
901 (void) fprintf(stderr,
902 gettext("failed to check state for %s\n"), vdev);
908 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
910 (void) fprintf(stderr,
911 gettext("failed to check state for %s\n"), vdev);
921 case POOL_STATE_ACTIVE:
922 case POOL_STATE_SPARE:
923 case POOL_STATE_L2CACHE:
924 (void) fprintf(stderr, gettext(
925 "%s is a member (%s) of pool \"%s\"\n"),
926 vdev, zpool_pool_state_to_name(state), name);
930 case POOL_STATE_EXPORTED:
933 (void) fprintf(stderr, gettext(
934 "use '-f' to override the following error:\n"
935 "%s is a member of exported pool \"%s\"\n"),
940 case POOL_STATE_POTENTIALLY_ACTIVE:
943 (void) fprintf(stderr, gettext(
944 "use '-f' to override the following error:\n"
945 "%s is a member of potentially active pool \"%s\"\n"),
950 case POOL_STATE_DESTROYED:
951 /* inuse should never be set for a destroyed pool */
957 ret = zpool_clear_label(fd);
959 (void) fprintf(stderr,
960 gettext("failed to clear label for %s\n"), vdev);
971 * zpool create [-fnd] [-o property=value] ...
972 * [-O file-system-property=value] ...
973 * [-R root] [-m mountpoint] <pool> <dev> ...
975 * -f Force creation, even if devices appear in use
976 * -n Do not create the pool, but display the resulting layout if it
977 * were to be created.
978 * -R Create a pool under an alternate root
979 * -m Set default mountpoint for the root dataset. By default it's
981 * -o Set property=value.
982 * -o Set feature@feature=enabled|disabled.
983 * -d Don't automatically enable all supported pool features
984 * (individual features can be enabled with -o).
985 * -O Set fsproperty=value in the pool's root file system
987 * Creates the named pool according to the given vdev specification. The
988 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
989 * we get the nvlist back from get_vdev_spec(), we either print out the contents
990 * (if '-n' was specified), or pass it to libzfs to do the creation.
993 zpool_do_create(int argc, char **argv)
995 boolean_t force = B_FALSE;
996 boolean_t dryrun = B_FALSE;
997 boolean_t enable_all_pool_feat = B_TRUE;
999 nvlist_t *nvroot = NULL;
1003 char *altroot = NULL;
1004 char *mountpoint = NULL;
1005 nvlist_t *fsprops = NULL;
1006 nvlist_t *props = NULL;
1010 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1019 enable_all_pool_feat = B_FALSE;
1023 if (add_prop_list(zpool_prop_to_name(
1024 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1026 if (add_prop_list_default(zpool_prop_to_name(
1027 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1031 /* Equivalent to -O mountpoint=optarg */
1032 mountpoint = optarg;
1035 if ((propval = strchr(optarg, '=')) == NULL) {
1036 (void) fprintf(stderr, gettext("missing "
1037 "'=' for -o option\n"));
1043 if (add_prop_list(optarg, propval, &props, B_TRUE))
1047 * If the user is creating a pool that doesn't support
1048 * feature flags, don't enable any features.
1050 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1054 ver = strtoull(propval, &end, 10);
1056 ver < SPA_VERSION_FEATURES) {
1057 enable_all_pool_feat = B_FALSE;
1060 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1064 if ((propval = strchr(optarg, '=')) == NULL) {
1065 (void) fprintf(stderr, gettext("missing "
1066 "'=' for -O option\n"));
1073 * Mountpoints are checked and then added later.
1074 * Uniquely among properties, they can be specified
1075 * more than once, to avoid conflict with -m.
1077 if (0 == strcmp(optarg,
1078 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1079 mountpoint = propval;
1080 } else if (add_prop_list(optarg, propval, &fsprops,
1087 * Sanity check temporary pool name.
1089 if (strchr(optarg, '/') != NULL) {
1090 (void) fprintf(stderr, gettext("cannot create "
1091 "'%s': invalid character '/' in temporary "
1093 (void) fprintf(stderr, gettext("use 'zfs "
1094 "create' to create a dataset\n"));
1098 if (add_prop_list(zpool_prop_to_name(
1099 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1101 if (add_prop_list_default(zpool_prop_to_name(
1102 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1107 (void) fprintf(stderr, gettext("missing argument for "
1108 "'%c' option\n"), optopt);
1111 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1120 /* get pool name and check number of arguments */
1122 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1126 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1133 * As a special case, check for use of '/' in the name, and direct the
1134 * user to use 'zfs create' instead.
1136 if (strchr(poolname, '/') != NULL) {
1137 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1138 "character '/' in pool name\n"), poolname);
1139 (void) fprintf(stderr, gettext("use 'zfs create' to "
1140 "create a dataset\n"));
1144 /* pass off to get_vdev_spec for bulk processing */
1145 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1146 argc - 1, argv + 1);
1150 /* make_root_vdev() allows 0 toplevel children if there are spares */
1151 if (!zfs_allocatable_devs(nvroot)) {
1152 (void) fprintf(stderr, gettext("invalid vdev "
1153 "specification: at least one toplevel vdev must be "
1158 if (altroot != NULL && altroot[0] != '/') {
1159 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1160 "must be an absolute path\n"), altroot);
1165 * Check the validity of the mountpoint and direct the user to use the
1166 * '-m' mountpoint option if it looks like its in use.
1168 if (mountpoint == NULL ||
1169 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1170 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1171 char buf[MAXPATHLEN];
1174 if (mountpoint && mountpoint[0] != '/') {
1175 (void) fprintf(stderr, gettext("invalid mountpoint "
1176 "'%s': must be an absolute path, 'legacy', or "
1177 "'none'\n"), mountpoint);
1181 if (mountpoint == NULL) {
1182 if (altroot != NULL)
1183 (void) snprintf(buf, sizeof (buf), "%s/%s",
1186 (void) snprintf(buf, sizeof (buf), "/%s",
1189 if (altroot != NULL)
1190 (void) snprintf(buf, sizeof (buf), "%s%s",
1191 altroot, mountpoint);
1193 (void) snprintf(buf, sizeof (buf), "%s",
1197 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1198 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1199 "%s\n"), buf, strerror(errno));
1200 (void) fprintf(stderr, gettext("use '-m' "
1201 "option to provide a different default\n"));
1206 while (count < 3 && readdir(dirp) != NULL)
1208 (void) closedir(dirp);
1211 (void) fprintf(stderr, gettext("mountpoint "
1212 "'%s' exists and is not empty\n"), buf);
1213 (void) fprintf(stderr, gettext("use '-m' "
1214 "option to provide a "
1215 "different default\n"));
1222 * Now that the mountpoint's validity has been checked, ensure that
1223 * the property is set appropriately prior to creating the pool.
1225 if (mountpoint != NULL) {
1226 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1227 mountpoint, &fsprops, B_FALSE);
1235 * For a dry run invocation, print out a basic message and run
1236 * through all the vdevs in the list and print out in an
1237 * appropriate hierarchy.
1239 (void) printf(gettext("would create '%s' with the "
1240 "following layout:\n\n"), poolname);
1242 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1243 if (num_logs(nvroot) > 0)
1244 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1249 * Hand off to libzfs.
1252 for (i = 0; i < SPA_FEATURES; i++) {
1253 char propname[MAXPATHLEN];
1255 zfeature_info_t *feat = &spa_feature_table[i];
1257 (void) snprintf(propname, sizeof (propname),
1258 "feature@%s", feat->fi_uname);
1261 * Only features contained in props will be enabled:
1262 * remove from the nvlist every ZFS_FEATURE_DISABLED
1263 * value and add every missing ZFS_FEATURE_ENABLED if
1264 * enable_all_pool_feat is set.
1266 if (!nvlist_lookup_string(props, propname, &propval)) {
1267 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1268 (void) nvlist_remove_all(props,
1270 } else if (enable_all_pool_feat) {
1271 ret = add_prop_list(propname,
1272 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1279 if (zpool_create(g_zfs, poolname,
1280 nvroot, props, fsprops) == 0) {
1281 zfs_handle_t *pool = zfs_open(g_zfs,
1282 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1284 if (zfs_mount(pool, NULL, 0) == 0)
1285 ret = zfs_shareall(pool);
1288 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1289 (void) fprintf(stderr, gettext("pool name may have "
1295 nvlist_free(nvroot);
1296 nvlist_free(fsprops);
1300 nvlist_free(fsprops);
1307 * zpool destroy <pool>
1309 * -f Forcefully unmount any datasets
1311 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1314 zpool_do_destroy(int argc, char **argv)
1316 boolean_t force = B_FALSE;
1319 zpool_handle_t *zhp;
1323 while ((c = getopt(argc, argv, "f")) != -1) {
1329 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1338 /* check arguments */
1340 (void) fprintf(stderr, gettext("missing pool argument\n"));
1344 (void) fprintf(stderr, gettext("too many arguments\n"));
1350 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1352 * As a special case, check for use of '/' in the name, and
1353 * direct the user to use 'zfs destroy' instead.
1355 if (strchr(pool, '/') != NULL)
1356 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1357 "destroy a dataset\n"));
1361 if (zpool_disable_datasets(zhp, force) != 0) {
1362 (void) fprintf(stderr, gettext("could not destroy '%s': "
1363 "could not unmount datasets\n"), zpool_get_name(zhp));
1368 /* The history must be logged as part of the export */
1369 log_history = B_FALSE;
1371 ret = (zpool_destroy(zhp, history_str) != 0);
1378 typedef struct export_cbdata {
1380 boolean_t hardforce;
1387 zpool_export_one(zpool_handle_t *zhp, void *data)
1389 export_cbdata_t *cb = data;
1391 if (zpool_disable_datasets(zhp, cb->force) != 0)
1394 /* The history must be logged as part of the export */
1395 log_history = B_FALSE;
1397 if (cb->hardforce) {
1398 if (zpool_export_force(zhp, history_str) != 0)
1400 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1408 * zpool export [-f] <pool> ...
1410 * -a Export all pools
1411 * -f Forcefully unmount datasets
1413 * Export the given pools. By default, the command will attempt to cleanly
1414 * unmount any active datasets within the pool. If the '-f' flag is specified,
1415 * then the datasets will be forcefully unmounted.
1418 zpool_do_export(int argc, char **argv)
1421 boolean_t do_all = B_FALSE;
1422 boolean_t force = B_FALSE;
1423 boolean_t hardforce = B_FALSE;
1427 while ((c = getopt(argc, argv, "afF")) != -1) {
1439 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1446 cb.hardforce = hardforce;
1452 (void) fprintf(stderr, gettext("too many arguments\n"));
1456 return (for_each_pool(argc, argv, B_TRUE, NULL,
1457 zpool_export_one, &cb));
1460 /* check arguments */
1462 (void) fprintf(stderr, gettext("missing pool argument\n"));
1466 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1472 * Given a vdev configuration, determine the maximum width needed for the device
1476 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1484 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1485 if (strlen(name) + depth > max)
1486 max = strlen(name) + depth;
1490 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1491 &child, &children) == 0) {
1492 for (c = 0; c < children; c++)
1493 if ((ret = max_width(zhp, child[c], depth + 2,
1494 max, name_flags)) > max)
1498 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1499 &child, &children) == 0) {
1500 for (c = 0; c < children; c++)
1501 if ((ret = max_width(zhp, child[c], depth + 2,
1502 max, name_flags)) > max)
1506 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1507 &child, &children) == 0) {
1508 for (c = 0; c < children; c++)
1509 if ((ret = max_width(zhp, child[c], depth + 2,
1510 max, name_flags)) > max)
1517 typedef struct spare_cbdata {
1519 zpool_handle_t *cb_zhp;
1523 find_vdev(nvlist_t *nv, uint64_t search)
1529 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1533 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1534 &child, &children) == 0) {
1535 for (c = 0; c < children; c++)
1536 if (find_vdev(child[c], search))
1544 find_spare(zpool_handle_t *zhp, void *data)
1546 spare_cbdata_t *cbp = data;
1547 nvlist_t *config, *nvroot;
1549 config = zpool_get_config(zhp, NULL);
1550 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1553 if (find_vdev(nvroot, cbp->cb_guid)) {
1562 typedef struct status_cbdata {
1566 boolean_t cb_allpools;
1567 boolean_t cb_verbose;
1568 boolean_t cb_explain;
1570 boolean_t cb_dedup_stats;
1571 boolean_t cb_print_status;
1572 vdev_cmd_data_list_t *vcdl;
1575 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1577 is_blank_str(char *str)
1579 while (str != NULL && *str != '\0') {
1587 /* Print command output lines for specific vdev in a specific pool */
1589 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1591 vdev_cmd_data_t *data;
1595 for (i = 0; i < vcdl->count; i++) {
1596 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1597 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1598 /* Not the vdev we're looking for */
1602 data = &vcdl->data[i];
1603 /* Print out all the output values for this vdev */
1604 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1606 /* Does this vdev have values for this column? */
1607 for (int k = 0; k < data->cols_cnt; k++) {
1608 if (strcmp(data->cols[k],
1609 vcdl->uniq_cols[j]) == 0) {
1610 /* yes it does, record the value */
1611 val = data->lines[k];
1616 * Mark empty values with dashes to make output
1619 if (is_blank_str(val))
1622 printf("%*s", vcdl->uniq_cols_width[j], val);
1623 if (j < vcdl->uniq_cols_cnt - 1)
1627 /* Print out any values that aren't in a column at the end */
1628 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1629 /* Did we have any columns? If so print a spacer. */
1630 if (vcdl->uniq_cols_cnt > 0)
1633 val = data->lines[j];
1634 printf("%s", val ? val : "");
1641 * Print out configuration state as requested by status_callback.
1644 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1645 nvlist_t *nv, int depth, boolean_t isspare)
1649 pool_scan_stat_t *ps = NULL;
1651 char rbuf[6], wbuf[6], cbuf[6];
1653 uint64_t notpresent;
1654 spare_cbdata_t spare_cb;
1658 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1659 &child, &children) != 0)
1662 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1663 (uint64_t **)&vs, &c) == 0);
1665 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1668 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1671 if (vs->vs_aux == VDEV_AUX_SPARED)
1673 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1677 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1681 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1682 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1683 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1684 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1687 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1688 ¬present) == 0) {
1689 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1690 (void) printf(" was %s", path);
1691 } else if (vs->vs_aux != 0) {
1694 switch (vs->vs_aux) {
1695 case VDEV_AUX_OPEN_FAILED:
1696 (void) printf(gettext("cannot open"));
1699 case VDEV_AUX_BAD_GUID_SUM:
1700 (void) printf(gettext("missing device"));
1703 case VDEV_AUX_NO_REPLICAS:
1704 (void) printf(gettext("insufficient replicas"));
1707 case VDEV_AUX_VERSION_NEWER:
1708 (void) printf(gettext("newer version"));
1711 case VDEV_AUX_UNSUP_FEAT:
1712 (void) printf(gettext("unsupported feature(s)"));
1715 case VDEV_AUX_SPARED:
1716 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1717 &spare_cb.cb_guid) == 0);
1718 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1719 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1720 zpool_get_name(zhp)) == 0)
1721 (void) printf(gettext("currently in "
1724 (void) printf(gettext("in use by "
1726 zpool_get_name(spare_cb.cb_zhp));
1727 zpool_close(spare_cb.cb_zhp);
1729 (void) printf(gettext("currently in use"));
1733 case VDEV_AUX_ERR_EXCEEDED:
1734 (void) printf(gettext("too many errors"));
1737 case VDEV_AUX_IO_FAILURE:
1738 (void) printf(gettext("experienced I/O failures"));
1741 case VDEV_AUX_BAD_LOG:
1742 (void) printf(gettext("bad intent log"));
1745 case VDEV_AUX_EXTERNAL:
1746 (void) printf(gettext("external device fault"));
1749 case VDEV_AUX_SPLIT_POOL:
1750 (void) printf(gettext("split into new pool"));
1753 case VDEV_AUX_ACTIVE:
1754 (void) printf(gettext("currently in use"));
1758 (void) printf(gettext("corrupted data"));
1763 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1764 (uint64_t **)&ps, &c);
1766 if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1767 vs->vs_scan_processed != 0 && children == 0) {
1768 (void) printf(gettext(" (%s)"),
1769 (ps->pss_func == POOL_SCAN_RESILVER) ?
1770 "resilvering" : "repairing");
1773 if (cb->vcdl != NULL) {
1774 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1776 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1780 (void) printf("\n");
1782 for (c = 0; c < children; c++) {
1783 uint64_t islog = B_FALSE, ishole = B_FALSE;
1785 /* Don't print logs or holes here */
1786 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1788 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1790 if (islog || ishole)
1792 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1793 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1794 print_status_config(zhp, cb, vname, child[c], depth + 2,
1801 * Print the configuration of an exported pool. Iterate over all vdevs in the
1802 * pool, printing out the name and status for each one.
1805 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1813 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1814 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1815 strcmp(type, VDEV_TYPE_HOLE) == 0)
1818 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1819 (uint64_t **)&vs, &c) == 0);
1821 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1822 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1824 if (vs->vs_aux != 0) {
1827 switch (vs->vs_aux) {
1828 case VDEV_AUX_OPEN_FAILED:
1829 (void) printf(gettext("cannot open"));
1832 case VDEV_AUX_BAD_GUID_SUM:
1833 (void) printf(gettext("missing device"));
1836 case VDEV_AUX_NO_REPLICAS:
1837 (void) printf(gettext("insufficient replicas"));
1840 case VDEV_AUX_VERSION_NEWER:
1841 (void) printf(gettext("newer version"));
1844 case VDEV_AUX_UNSUP_FEAT:
1845 (void) printf(gettext("unsupported feature(s)"));
1848 case VDEV_AUX_ERR_EXCEEDED:
1849 (void) printf(gettext("too many errors"));
1852 case VDEV_AUX_ACTIVE:
1853 (void) printf(gettext("currently in use"));
1857 (void) printf(gettext("corrupted data"));
1861 (void) printf("\n");
1863 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1864 &child, &children) != 0)
1867 for (c = 0; c < children; c++) {
1868 uint64_t is_log = B_FALSE;
1870 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1875 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1876 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1877 print_import_config(cb, vname, child[c], depth + 2);
1881 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1882 &child, &children) == 0) {
1883 (void) printf(gettext("\tcache\n"));
1884 for (c = 0; c < children; c++) {
1885 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1887 (void) printf("\t %s\n", vname);
1892 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1893 &child, &children) == 0) {
1894 (void) printf(gettext("\tspares\n"));
1895 for (c = 0; c < children; c++) {
1896 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1898 (void) printf("\t %s\n", vname);
1906 * Logs are recorded as top level vdevs in the main pool child array
1907 * but with "is_log" set to 1. We use either print_status_config() or
1908 * print_import_config() to print the top level logs then any log
1909 * children (eg mirrored slogs) are printed recursively - which
1910 * works because only the top level vdev is marked "is_log"
1913 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1918 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1922 (void) printf(gettext("\tlogs\n"));
1924 for (c = 0; c < children; c++) {
1925 uint64_t is_log = B_FALSE;
1928 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1932 name = zpool_vdev_name(g_zfs, zhp, child[c],
1933 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1934 if (cb->cb_print_status)
1935 print_status_config(zhp, cb, name, child[c], 2,
1938 print_import_config(cb, name, child[c], 2);
1944 * Display the status for the given pool.
1947 show_import(nvlist_t *config)
1949 uint64_t pool_state;
1953 uint64_t hostid = 0;
1955 char *hostname = "unknown";
1956 nvlist_t *nvroot, *nvinfo;
1957 zpool_status_t reason;
1958 zpool_errata_t errata;
1962 status_cbdata_t cb = { 0 };
1964 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1966 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1968 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1970 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1973 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1974 (uint64_t **)&vs, &vsc) == 0);
1975 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1977 reason = zpool_import_status(config, &msgid, &errata);
1979 (void) printf(gettext(" pool: %s\n"), name);
1980 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1981 (void) printf(gettext(" state: %s"), health);
1982 if (pool_state == POOL_STATE_DESTROYED)
1983 (void) printf(gettext(" (DESTROYED)"));
1984 (void) printf("\n");
1987 case ZPOOL_STATUS_MISSING_DEV_R:
1988 case ZPOOL_STATUS_MISSING_DEV_NR:
1989 case ZPOOL_STATUS_BAD_GUID_SUM:
1990 (void) printf(gettext(" status: One or more devices are "
1991 "missing from the system.\n"));
1994 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1995 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1996 (void) printf(gettext(" status: One or more devices contains "
1997 "corrupted data.\n"));
2000 case ZPOOL_STATUS_CORRUPT_DATA:
2002 gettext(" status: The pool data is corrupted.\n"));
2005 case ZPOOL_STATUS_OFFLINE_DEV:
2006 (void) printf(gettext(" status: One or more devices "
2007 "are offlined.\n"));
2010 case ZPOOL_STATUS_CORRUPT_POOL:
2011 (void) printf(gettext(" status: The pool metadata is "
2015 case ZPOOL_STATUS_VERSION_OLDER:
2016 (void) printf(gettext(" status: The pool is formatted using a "
2017 "legacy on-disk version.\n"));
2020 case ZPOOL_STATUS_VERSION_NEWER:
2021 (void) printf(gettext(" status: The pool is formatted using an "
2022 "incompatible version.\n"));
2025 case ZPOOL_STATUS_FEAT_DISABLED:
2026 (void) printf(gettext(" status: Some supported features are "
2027 "not enabled on the pool.\n"));
2030 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2031 (void) printf(gettext("status: The pool uses the following "
2032 "feature(s) not supported on this system:\n"));
2033 zpool_print_unsup_feat(config);
2036 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2037 (void) printf(gettext("status: The pool can only be accessed "
2038 "in read-only mode on this system. It\n\tcannot be "
2039 "accessed in read-write mode because it uses the "
2040 "following\n\tfeature(s) not supported on this system:\n"));
2041 zpool_print_unsup_feat(config);
2044 case ZPOOL_STATUS_HOSTID_ACTIVE:
2045 (void) printf(gettext(" status: The pool is currently "
2046 "imported by another system.\n"));
2049 case ZPOOL_STATUS_HOSTID_REQUIRED:
2050 (void) printf(gettext(" status: The pool has the "
2051 "multihost property on. It cannot\n\tbe safely imported "
2052 "when the system hostid is not set.\n"));
2055 case ZPOOL_STATUS_HOSTID_MISMATCH:
2056 (void) printf(gettext(" status: The pool was last accessed by "
2057 "another system.\n"));
2060 case ZPOOL_STATUS_FAULTED_DEV_R:
2061 case ZPOOL_STATUS_FAULTED_DEV_NR:
2062 (void) printf(gettext(" status: One or more devices are "
2066 case ZPOOL_STATUS_BAD_LOG:
2067 (void) printf(gettext(" status: An intent log record cannot be "
2071 case ZPOOL_STATUS_RESILVERING:
2072 (void) printf(gettext(" status: One or more devices were being "
2076 case ZPOOL_STATUS_ERRATA:
2077 (void) printf(gettext(" status: Errata #%d detected.\n"),
2083 * No other status can be seen when importing pools.
2085 assert(reason == ZPOOL_STATUS_OK);
2089 * Print out an action according to the overall state of the pool.
2091 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2092 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2093 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2094 (void) printf(gettext(" action: The pool can be "
2095 "imported using its name or numeric identifier, "
2096 "though\n\tsome features will not be available "
2097 "without an explicit 'zpool upgrade'.\n"));
2098 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2099 (void) printf(gettext(" action: The pool can be "
2100 "imported using its name or numeric "
2101 "identifier and\n\tthe '-f' flag.\n"));
2102 } else if (reason == ZPOOL_STATUS_ERRATA) {
2104 case ZPOOL_ERRATA_NONE:
2107 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2108 (void) printf(gettext(" action: The pool can "
2109 "be imported using its name or numeric "
2110 "identifier,\n\thowever there is a compat"
2111 "ibility issue which should be corrected"
2112 "\n\tby running 'zpool scrub'\n"));
2115 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2116 (void) printf(gettext(" action: The pool can"
2117 "not be imported with this version of ZFS "
2118 "due to\n\tan active asynchronous destroy. "
2119 "Revert to an earlier version\n\tand "
2120 "allow the destroy to complete before "
2126 * All errata must contain an action message.
2131 (void) printf(gettext(" action: The pool can be "
2132 "imported using its name or numeric "
2135 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2136 (void) printf(gettext(" action: The pool can be imported "
2137 "despite missing or damaged devices. The\n\tfault "
2138 "tolerance of the pool may be compromised if imported.\n"));
2141 case ZPOOL_STATUS_VERSION_NEWER:
2142 (void) printf(gettext(" action: The pool cannot be "
2143 "imported. Access the pool on a system running "
2144 "newer\n\tsoftware, or recreate the pool from "
2147 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2148 (void) printf(gettext("action: The pool cannot be "
2149 "imported. Access the pool on a system that "
2150 "supports\n\tthe required feature(s), or recreate "
2151 "the pool from backup.\n"));
2153 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2154 (void) printf(gettext("action: The pool cannot be "
2155 "imported in read-write mode. Import the pool "
2157 "\t\"-o readonly=on\", access the pool on a system "
2158 "that supports the\n\trequired feature(s), or "
2159 "recreate the pool from backup.\n"));
2161 case ZPOOL_STATUS_MISSING_DEV_R:
2162 case ZPOOL_STATUS_MISSING_DEV_NR:
2163 case ZPOOL_STATUS_BAD_GUID_SUM:
2164 (void) printf(gettext(" action: The pool cannot be "
2165 "imported. Attach the missing\n\tdevices and try "
2168 case ZPOOL_STATUS_HOSTID_ACTIVE:
2169 VERIFY0(nvlist_lookup_nvlist(config,
2170 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2172 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2173 hostname = fnvlist_lookup_string(nvinfo,
2174 ZPOOL_CONFIG_MMP_HOSTNAME);
2176 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2177 hostid = fnvlist_lookup_uint64(nvinfo,
2178 ZPOOL_CONFIG_MMP_HOSTID);
2180 (void) printf(gettext(" action: The pool must be "
2181 "exported from %s (hostid=%lx)\n\tbefore it "
2182 "can be safely imported.\n"), hostname,
2183 (unsigned long) hostid);
2185 case ZPOOL_STATUS_HOSTID_REQUIRED:
2186 (void) printf(gettext(" action: Set a unique system "
2187 "hostid with the zgenhostid(8) command.\n"));
2190 (void) printf(gettext(" action: The pool cannot be "
2191 "imported due to damaged devices or data.\n"));
2195 /* Print the comment attached to the pool. */
2196 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2197 (void) printf(gettext("comment: %s\n"), comment);
2200 * If the state is "closed" or "can't open", and the aux state
2201 * is "corrupt data":
2203 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2204 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2205 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2206 if (pool_state == POOL_STATE_DESTROYED)
2207 (void) printf(gettext("\tThe pool was destroyed, "
2208 "but can be imported using the '-Df' flags.\n"));
2209 else if (pool_state != POOL_STATE_EXPORTED)
2210 (void) printf(gettext("\tThe pool may be active on "
2211 "another system, but can be imported using\n\t"
2212 "the '-f' flag.\n"));
2216 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2219 (void) printf(gettext(" config:\n\n"));
2221 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2223 if (cb.cb_namewidth < 10)
2224 cb.cb_namewidth = 10;
2226 print_import_config(&cb, name, nvroot, 0);
2227 if (num_logs(nvroot) > 0)
2228 print_logs(NULL, &cb, nvroot);
2230 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2231 (void) printf(gettext("\n\tAdditional devices are known to "
2232 "be part of this pool, though their\n\texact "
2233 "configuration cannot be determined.\n"));
2238 zfs_force_import_required(nvlist_t *config)
2241 uint64_t hostid = 0;
2244 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2245 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2247 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2250 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2251 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2252 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2253 ZPOOL_CONFIG_MMP_STATE);
2255 if (mmp_state != MMP_STATE_INACTIVE)
2263 * Perform the import for the given configuration. This passes the heavy
2264 * lifting off to zpool_import_props(), and then mounts the datasets contained
2268 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2269 nvlist_t *props, int flags)
2272 zpool_handle_t *zhp;
2276 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
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;
3649 uint_t c, children, oldchildren;
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)
3762 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3763 &oldchild, &oldchildren) != 0)
3766 children = MIN(oldchildren, children);
3769 for (c = 0; c < children; c++) {
3770 uint64_t ishole = B_FALSE, islog = B_FALSE;
3772 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3775 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3778 if (ishole || islog)
3781 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3783 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3784 newchild[c], cb, depth + 2);
3789 * Log device section
3792 if (num_logs(newnv) > 0) {
3793 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3794 !cb->cb_vdev_names) {
3795 print_iostat_dashes(cb, 0, "logs");
3799 for (c = 0; c < children; c++) {
3800 uint64_t islog = B_FALSE;
3801 (void) nvlist_lookup_uint64(newchild[c],
3802 ZPOOL_CONFIG_IS_LOG, &islog);
3805 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3807 ret += print_vdev_stats(zhp, vname, oldnv ?
3808 oldchild[c] : NULL, newchild[c],
3817 * Include level 2 ARC devices in iostat output
3819 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3820 &newchild, &children) != 0)
3824 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3825 &oldchild, &oldchildren) != 0)
3828 children = MIN(oldchildren, children);
3832 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3833 !cb->cb_vdev_names) {
3834 print_iostat_dashes(cb, 0, "cache");
3838 for (c = 0; c < children; c++) {
3839 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3841 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3842 : NULL, newchild[c], cb, depth + 2);
3851 refresh_iostat(zpool_handle_t *zhp, void *data)
3853 iostat_cbdata_t *cb = data;
3857 * If the pool has disappeared, remove it from the list and continue.
3859 if (zpool_refresh_stats(zhp, &missing) != 0)
3863 pool_list_remove(cb->cb_list, zhp);
3869 * Callback to print out the iostats for the given pool.
3872 print_iostat(zpool_handle_t *zhp, void *data)
3874 iostat_cbdata_t *cb = data;
3875 nvlist_t *oldconfig, *newconfig;
3876 nvlist_t *oldnvroot, *newnvroot;
3879 newconfig = zpool_get_config(zhp, &oldconfig);
3881 if (cb->cb_iteration == 1)
3884 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3887 if (oldconfig == NULL)
3890 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3893 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3895 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3896 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3897 print_iostat_separator(cb);
3898 if (cb->vcdl != NULL) {
3899 print_cmd_columns(cb->vcdl, 1);
3914 if (isatty(STDOUT_FILENO)) {
3915 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3917 columns = ws.ws_col;
3926 get_namewidth(zpool_handle_t *zhp, void *data)
3928 iostat_cbdata_t *cb = data;
3929 nvlist_t *config, *nvroot;
3932 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3933 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3935 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3936 if (!cb->cb_verbose)
3937 cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
3939 cb->cb_namewidth = MAX(poolname_len,
3940 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3941 cb->cb_name_flags));
3944 * The width must be at least 10, but may be as large as the
3945 * column width - 42 so that we can still fit in one line.
3947 columns = get_columns();
3949 if (cb->cb_namewidth < 10)
3950 cb->cb_namewidth = 10;
3951 if (cb->cb_namewidth > columns - 42)
3952 cb->cb_namewidth = columns - 42;
3958 * Parse the input string, get the 'interval' and 'count' value if there is one.
3961 get_interval_count(int *argcp, char **argv, float *iv,
3965 unsigned long count = 0;
3969 * Determine if the last argument is an integer or a pool name
3971 if (argc > 0 && isnumber(argv[argc - 1])) {
3975 interval = strtof(argv[argc - 1], &end);
3977 if (*end == '\0' && errno == 0) {
3978 if (interval == 0) {
3979 (void) fprintf(stderr, gettext("interval "
3980 "cannot be zero\n"));
3984 * Ignore the last parameter
3989 * If this is not a valid number, just plow on. The
3990 * user will get a more informative error message later
3998 * If the last argument is also an integer, then we have both a count
4001 if (argc > 0 && isnumber(argv[argc - 1])) {
4006 interval = strtof(argv[argc - 1], &end);
4008 if (*end == '\0' && errno == 0) {
4009 if (interval == 0) {
4010 (void) fprintf(stderr, gettext("interval "
4011 "cannot be zero\n"));
4016 * Ignore the last parameter
4030 get_timestamp_arg(char c)
4033 timestamp_fmt = UDATE;
4035 timestamp_fmt = DDATE;
4041 * Return stat flags that are supported by all pools by both the module and
4042 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4043 * It will get ANDed down until only the flags that are supported on all pools
4047 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4049 uint64_t *mask = data;
4050 nvlist_t *config, *nvroot, *nvx;
4054 config = zpool_get_config(zhp, NULL);
4055 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4058 /* Default stats are always supported, but for completeness.. */
4059 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4060 flags |= IOS_DEFAULT_M;
4062 /* Get our extended stats nvlist from the main list */
4063 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4066 * No extended stats; they're probably running an older
4067 * module. No big deal, we support that too.
4072 /* For each extended stat, make sure all its nvpairs are supported */
4073 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4074 if (!vsx_type_to_nvlist[j][0])
4077 /* Start off by assuming the flag is supported, then check */
4078 flags |= (1ULL << j);
4079 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4080 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4081 /* flag isn't supported */
4082 flags = flags & ~(1ULL << j);
4088 *mask = *mask & flags;
4093 * Return a bitmask of stats that are supported on all pools by both the module
4097 get_stat_flags(zpool_list_t *list)
4102 * get_stat_flags_cb() will lop off bits from "mask" until only the
4103 * flags that are supported on all pools remain.
4105 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4110 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4113 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4115 iostat_cbdata_t *cb = cb_data;
4119 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4121 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4122 ret = 1; /* match */
4129 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4132 is_vdev(zpool_handle_t *zhp, void *cb_data)
4134 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4138 * Check if vdevs are in a pool
4140 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4141 * return 0. If pool_name is NULL, then search all pools.
4144 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4145 iostat_cbdata_t *cb)
4152 if ((argc == 0) || !*argv)
4158 /* Temporarily hijack cb_vdev_names for a second... */
4159 tmp_name = cb->cb_vdev_names;
4161 /* Go though our list of prospective vdev names */
4162 for (i = 0; i < argc; i++) {
4163 cb->cb_vdev_names = argv + i;
4165 /* Is this name a vdev in our pools? */
4166 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4174 cb->cb_vdev_names = tmp_name;
4180 is_pool_cb(zpool_handle_t *zhp, void *data)
4183 if (strcmp(name, zpool_get_name(zhp)) == 0)
4190 * Do we have a pool named *name? If so, return 1, otherwise 0.
4195 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4198 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4200 are_all_pools(int argc, char **argv)
4202 if ((argc == 0) || !*argv)
4206 if (!is_pool(argv[argc]))
4213 * Helper function to print out vdev/pool names we can't resolve. Used for an
4217 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4218 iostat_cbdata_t *cb)
4223 for (i = 0; i < argc; i++) {
4227 str = gettext("pool");
4228 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4229 str = gettext("vdev in this pool");
4230 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4231 str = gettext("vdev in another pool");
4233 str = gettext("unknown");
4235 fprintf(stderr, "\t%s (%s)\n", name, str);
4240 * Same as get_interval_count(), but with additional checks to not misinterpret
4241 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4245 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4246 unsigned long *count, iostat_cbdata_t *cb)
4248 char **tmpargv = argv;
4249 int argc_for_interval = 0;
4251 /* Is the last arg an interval value? Or a guid? */
4252 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4254 * The last arg is not a guid, so it's probably an
4257 argc_for_interval++;
4260 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4262 * The 2nd to last arg is not a guid, so it's probably
4263 * an interval value.
4265 argc_for_interval++;
4269 /* Point to our list of possible intervals */
4270 tmpargv = &argv[*argc - argc_for_interval];
4272 *argc = *argc - argc_for_interval;
4273 get_interval_count(&argc_for_interval, tmpargv,
4278 * Floating point sleep(). Allows you to pass in a floating point value for
4284 struct timespec req;
4285 req.tv_sec = floor(sec);
4286 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4287 nanosleep(&req, NULL);
4291 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4294 * name: Short name of the script ('iostat').
4295 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4298 print_zpool_script_help(char *name, char *path)
4300 char *argv[] = {path, "-h", NULL};
4301 char **lines = NULL;
4305 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4307 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4309 libzfs_free_str_array(lines, lines_cnt);
4313 for (int i = 0; i < lines_cnt; i++)
4314 if (!is_blank_str(lines[i]))
4315 printf(" %-14s %s\n", name, lines[i]);
4317 libzfs_free_str_array(lines, lines_cnt);
4321 * Go though the zpool status/iostat -c scripts in the user's path, run their
4322 * help option (-h), and print out the results.
4325 print_zpool_dir_scripts(char *dirpath)
4329 char fullpath[MAXPATHLEN];
4330 struct stat dir_stat;
4332 if ((dir = opendir(dirpath)) != NULL) {
4333 /* print all the files and directories within directory */
4334 while ((ent = readdir(dir)) != NULL) {
4335 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4337 /* Print the scripts */
4338 if (stat(fullpath, &dir_stat) == 0)
4339 if (dir_stat.st_mode & S_IXUSR &&
4340 S_ISREG(dir_stat.st_mode))
4341 print_zpool_script_help(ent->d_name,
4349 * Print out help text for all zpool status/iostat -c scripts.
4352 print_zpool_script_list(char *subcommand)
4356 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4358 sp = zpool_get_cmd_search_path();
4362 dir = strtok(sp, ":");
4363 while (dir != NULL) {
4364 print_zpool_dir_scripts(dir);
4365 dir = strtok(NULL, ":");
4372 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4373 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4374 * [interval [count]]
4376 * -c CMD For each vdev, run command CMD
4377 * -g Display guid for individual vdev name.
4378 * -L Follow links when resolving vdev path name.
4379 * -P Display full path for vdev name.
4380 * -v Display statistics for individual vdevs
4382 * -p Display values in parsable (exact) format.
4383 * -H Scripted mode. Don't display headers, and separate properties
4385 * -l Display average latency
4386 * -q Display queue depths
4387 * -w Display latency histograms
4388 * -r Display request size histogram
4389 * -T Display a timestamp in date(1) or Unix format
4391 * This command can be tricky because we want to be able to deal with pool
4392 * creation/destruction as well as vdev configuration changes. The bulk of this
4393 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4394 * on pool_list_update() to detect the addition of new pools. Configuration
4395 * changes are all handled within libzfs.
4398 zpool_do_iostat(int argc, char **argv)
4404 unsigned long count = 0;
4406 boolean_t verbose = B_FALSE;
4407 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4408 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4409 boolean_t omit_since_boot = B_FALSE;
4410 boolean_t guid = B_FALSE;
4411 boolean_t follow_links = B_FALSE;
4412 boolean_t full_name = B_FALSE;
4413 iostat_cbdata_t cb = { 0 };
4416 /* Used for printing error message */
4417 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4418 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4420 uint64_t unsupported_flags;
4423 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4428 gettext("Can't set -c flag twice\n"));
4432 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4433 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4434 fprintf(stderr, gettext(
4435 "Can't run -c, disabled by "
4436 "ZPOOL_SCRIPTS_ENABLED.\n"));
4440 if ((getuid() <= 0 || geteuid() <= 0) &&
4441 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4442 fprintf(stderr, gettext(
4443 "Can't run -c with root privileges "
4444 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4454 follow_links = B_TRUE;
4460 get_timestamp_arg(*optarg);
4484 omit_since_boot = B_TRUE;
4490 if (optopt == 'c') {
4491 print_zpool_script_list("iostat");
4495 gettext("invalid option '%c'\n"), optopt);
4504 cb.cb_literal = parsable;
4505 cb.cb_scripted = scripted;
4508 cb.cb_name_flags |= VDEV_NAME_GUID;
4510 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4512 cb.cb_name_flags |= VDEV_NAME_PATH;
4513 cb.cb_iteration = 0;
4514 cb.cb_namewidth = 0;
4515 cb.cb_verbose = verbose;
4517 /* Get our interval and count values (if any) */
4519 get_interval_count_filter_guids(&argc, argv, &interval,
4522 get_interval_count(&argc, argv, &interval, &count);
4526 /* No args, so just print the defaults. */
4527 } else if (are_all_pools(argc, argv)) {
4528 /* All the args are pool names */
4529 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4530 /* All the args are vdevs */
4531 cb.cb_vdev_names = argv;
4532 cb.cb_vdev_names_count = argc;
4533 argc = 0; /* No pools to process */
4534 } else if (are_all_pools(1, argv)) {
4535 /* The first arg is a pool name */
4536 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4537 /* ...and the rest are vdev names */
4538 cb.cb_vdev_names = argv + 1;
4539 cb.cb_vdev_names_count = argc - 1;
4540 argc = 1; /* One pool to process */
4542 fprintf(stderr, gettext("Expected either a list of "));
4543 fprintf(stderr, gettext("pools, or list of vdevs in"));
4544 fprintf(stderr, " \"%s\", ", argv[0]);
4545 fprintf(stderr, gettext("but got:\n"));
4546 error_list_unresolved_vdevs(argc - 1, argv + 1,
4548 fprintf(stderr, "\n");
4554 * The args don't make sense. The first arg isn't a pool name,
4555 * nor are all the args vdevs.
4557 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4558 fprintf(stderr, "\n");
4562 if (cb.cb_vdev_names_count != 0) {
4564 * If user specified vdevs, it implies verbose.
4566 cb.cb_verbose = B_TRUE;
4570 * Construct the list of all interesting pools.
4573 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4576 if (pool_list_count(list) == 0 && argc != 0) {
4577 pool_list_free(list);
4581 if (pool_list_count(list) == 0 && interval == 0) {
4582 pool_list_free(list);
4583 (void) fprintf(stderr, gettext("no pools available\n"));
4587 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4588 pool_list_free(list);
4589 (void) fprintf(stderr,
4590 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4595 if (l_histo && rq_histo) {
4596 pool_list_free(list);
4597 (void) fprintf(stderr,
4598 gettext("Only one of [-r|-w] can be passed at a time\n"));
4604 * Enter the main iostat loop.
4610 * Histograms tables look out of place when you try to display
4611 * them with the other stats, so make a rule that you can only
4612 * print histograms by themselves.
4614 cb.cb_flags = IOS_L_HISTO_M;
4615 } else if (rq_histo) {
4616 cb.cb_flags = IOS_RQ_HISTO_M;
4618 cb.cb_flags = IOS_DEFAULT_M;
4620 cb.cb_flags |= IOS_LATENCY_M;
4622 cb.cb_flags |= IOS_QUEUES_M;
4626 * See if the module supports all the stats we want to display.
4628 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4629 if (unsupported_flags) {
4633 gettext("The loaded zfs module doesn't support:"));
4635 /* for each bit set in unsupported_flags */
4636 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4637 idx = lowbit64(f) - 1;
4638 fprintf(stderr, " -%c", flag_to_arg[idx]);
4641 fprintf(stderr, ". Try running a newer module.\n");
4642 pool_list_free(list);
4648 if ((npools = pool_list_count(list)) == 0)
4649 (void) fprintf(stderr, gettext("no pools available\n"));
4652 * If this is the first iteration and -y was supplied
4653 * we skip any printing.
4655 boolean_t skip = (omit_since_boot &&
4656 cb.cb_iteration == 0);
4659 * Refresh all statistics. This is done as an
4660 * explicit step before calculating the maximum name
4661 * width, so that any * configuration changes are
4662 * properly accounted for.
4664 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4668 * Iterate over all pools to determine the maximum width
4669 * for the pool / device name column across all pools.
4671 cb.cb_namewidth = 0;
4672 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4675 if (timestamp_fmt != NODATE)
4676 print_timestamp(timestamp_fmt);
4678 if (cmd != NULL && cb.cb_verbose &&
4679 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4680 cb.vcdl = all_pools_for_each_vdev_run(argc,
4681 argv, cmd, g_zfs, cb.cb_vdev_names,
4682 cb.cb_vdev_names_count, cb.cb_name_flags);
4688 * If it's the first time and we're not skipping it,
4689 * or either skip or verbose mode, print the header.
4691 * The histogram code explicitly prints its header on
4692 * every vdev, so skip this for histograms.
4694 if (((++cb.cb_iteration == 1 && !skip) ||
4695 (skip != verbose)) &&
4696 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4698 print_iostat_header(&cb);
4701 (void) fsleep(interval);
4706 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4709 * If there's more than one pool, and we're not in
4710 * verbose mode (which prints a separator for us),
4711 * then print a separator.
4713 * In addition, if we're printing specific vdevs then
4714 * we also want an ending separator.
4716 if (((npools > 1 && !verbose &&
4717 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4718 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4719 cb.cb_vdev_names_count)) &&
4721 print_iostat_separator(&cb);
4722 if (cb.vcdl != NULL)
4723 print_cmd_columns(cb.vcdl, 1);
4727 if (cb.vcdl != NULL)
4728 free_vdev_cmd_data_list(cb.vcdl);
4733 * Flush the output so that redirection to a file isn't buffered
4736 (void) fflush(stdout);
4741 if (count != 0 && --count == 0)
4744 (void) fsleep(interval);
4747 pool_list_free(list);
4752 typedef struct list_cbdata {
4753 boolean_t cb_verbose;
4756 boolean_t cb_scripted;
4757 zprop_list_t *cb_proplist;
4758 boolean_t cb_literal;
4762 * Given a list of columns to display, output appropriate headers for each one.
4765 print_header(list_cbdata_t *cb)
4767 zprop_list_t *pl = cb->cb_proplist;
4768 char headerbuf[ZPOOL_MAXPROPLEN];
4770 boolean_t first = B_TRUE;
4771 boolean_t right_justify;
4774 for (; pl != NULL; pl = pl->pl_next) {
4775 width = pl->pl_width;
4776 if (first && cb->cb_verbose) {
4778 * Reset the width to accommodate the verbose listing
4781 width = cb->cb_namewidth;
4789 right_justify = B_FALSE;
4790 if (pl->pl_prop != ZPROP_INVAL) {
4791 header = zpool_prop_column_name(pl->pl_prop);
4792 right_justify = zpool_prop_align_right(pl->pl_prop);
4796 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4797 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4798 headerbuf[i] = '\0';
4802 if (pl->pl_next == NULL && !right_justify)
4803 (void) printf("%s", header);
4804 else if (right_justify)
4805 (void) printf("%*s", (int)width, header);
4807 (void) printf("%-*s", (int)width, header);
4810 (void) printf("\n");
4814 * Given a pool and a list of properties, print out all the properties according
4815 * to the described layout.
4818 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4820 zprop_list_t *pl = cb->cb_proplist;
4821 boolean_t first = B_TRUE;
4822 char property[ZPOOL_MAXPROPLEN];
4824 boolean_t right_justify;
4827 for (; pl != NULL; pl = pl->pl_next) {
4829 width = pl->pl_width;
4830 if (first && cb->cb_verbose) {
4832 * Reset the width to accommodate the verbose listing
4835 width = cb->cb_namewidth;
4839 if (cb->cb_scripted)
4840 (void) printf("\t");
4847 right_justify = B_FALSE;
4848 if (pl->pl_prop != ZPROP_INVAL) {
4849 if (zpool_get_prop(zhp, pl->pl_prop, property,
4850 sizeof (property), NULL, cb->cb_literal) != 0)
4855 right_justify = zpool_prop_align_right(pl->pl_prop);
4856 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4857 zpool_prop_unsupported(pl->pl_user_prop)) &&
4858 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4859 sizeof (property)) == 0) {
4867 * If this is being called in scripted mode, or if this is the
4868 * last column and it is left-justified, don't include a width
4871 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4872 (void) printf("%s", propstr);
4873 else if (right_justify)
4874 (void) printf("%*s", (int)width, propstr);
4876 (void) printf("%-*s", (int)width, propstr);
4879 (void) printf("\n");
4883 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4884 boolean_t valid, enum zfs_nicenum_format format)
4888 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4891 case ZPOOL_PROP_EXPANDSZ:
4893 (void) strlcpy(propval, "-", sizeof (propval));
4895 zfs_nicenum_format(value, propval, sizeof (propval),
4898 case ZPOOL_PROP_FRAGMENTATION:
4899 if (value == ZFS_FRAG_INVALID) {
4900 (void) strlcpy(propval, "-", sizeof (propval));
4901 } else if (format == ZFS_NICENUM_RAW) {
4902 (void) snprintf(propval, sizeof (propval), "%llu",
4903 (unsigned long long)value);
4905 (void) snprintf(propval, sizeof (propval), "%llu%%",
4906 (unsigned long long)value);
4909 case ZPOOL_PROP_CAPACITY:
4910 if (format == ZFS_NICENUM_RAW)
4911 (void) snprintf(propval, sizeof (propval), "%llu",
4912 (unsigned long long)value);
4914 (void) snprintf(propval, sizeof (propval), "%llu%%",
4915 (unsigned long long)value);
4918 zfs_nicenum_format(value, propval, sizeof (propval), format);
4922 (void) strlcpy(propval, "-", sizeof (propval));
4925 (void) printf("\t%s", propval);
4927 (void) printf(" %*s", (int)width, propval);
4931 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4932 list_cbdata_t *cb, int depth)
4938 boolean_t scripted = cb->cb_scripted;
4939 uint64_t islog = B_FALSE;
4940 boolean_t haslog = B_FALSE;
4941 char *dashes = "%-*s - - - - - -\n";
4943 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4944 (uint64_t **)&vs, &c) == 0);
4947 boolean_t toplevel = (vs->vs_space != 0);
4949 enum zfs_nicenum_format format;
4952 format = ZFS_NICENUM_RAW;
4954 format = ZFS_NICENUM_1024;
4957 (void) printf("\t%s", name);
4958 else if (strlen(name) + depth > cb->cb_namewidth)
4959 (void) printf("%*s%s", depth, "", name);
4961 (void) printf("%*s%s%*s", depth, "", name,
4962 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4965 * Print the properties for the individual vdevs. Some
4966 * properties are only applicable to toplevel vdevs. The
4967 * 'toplevel' boolean value is passed to the print_one_column()
4968 * to indicate that the value is valid.
4970 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4972 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4974 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4975 scripted, toplevel, format);
4976 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4978 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4979 vs->vs_fragmentation, scripted,
4980 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4982 cap = (vs->vs_space == 0) ? 0 :
4983 (vs->vs_alloc * 100 / vs->vs_space);
4984 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4986 (void) printf("\n");
4989 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4990 &child, &children) != 0)
4993 for (c = 0; c < children; c++) {
4994 uint64_t ishole = B_FALSE;
4996 if (nvlist_lookup_uint64(child[c],
4997 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
5000 if (nvlist_lookup_uint64(child[c],
5001 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
5006 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5008 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5012 if (haslog == B_TRUE) {
5013 /* LINTED E_SEC_PRINTF_VAR_FMT */
5014 (void) printf(dashes, cb->cb_namewidth, "log");
5015 for (c = 0; c < children; c++) {
5016 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5017 &islog) != 0 || !islog)
5019 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5021 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5026 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5027 &child, &children) == 0 && children > 0) {
5028 /* LINTED E_SEC_PRINTF_VAR_FMT */
5029 (void) printf(dashes, cb->cb_namewidth, "cache");
5030 for (c = 0; c < children; c++) {
5031 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5033 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5038 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5039 &children) == 0 && children > 0) {
5040 /* LINTED E_SEC_PRINTF_VAR_FMT */
5041 (void) printf(dashes, cb->cb_namewidth, "spare");
5042 for (c = 0; c < children; c++) {
5043 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5045 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5053 * Generic callback function to list a pool.
5056 list_callback(zpool_handle_t *zhp, void *data)
5058 list_cbdata_t *cbp = data;
5062 config = zpool_get_config(zhp, NULL);
5064 print_pool(zhp, cbp);
5065 if (!cbp->cb_verbose)
5068 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5070 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5076 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5078 * -g Display guid for individual vdev name.
5079 * -H Scripted mode. Don't display headers, and separate properties
5081 * -L Follow links when resolving vdev path name.
5082 * -o List of properties to display. Defaults to
5083 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5084 * "dedupratio,health,altroot"
5085 * -p Display values in parsable (exact) format.
5086 * -P Display full path for vdev name.
5087 * -T Display a timestamp in date(1) or Unix format
5089 * List all pools in the system, whether or not they're healthy. Output space
5090 * statistics for each one, as well as health status summary.
5093 zpool_do_list(int argc, char **argv)
5097 list_cbdata_t cb = { 0 };
5098 static char default_props[] =
5099 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5100 "dedupratio,health,altroot";
5101 char *props = default_props;
5103 unsigned long count = 0;
5105 boolean_t first = B_TRUE;
5108 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5111 cb.cb_name_flags |= VDEV_NAME_GUID;
5114 cb.cb_scripted = B_TRUE;
5117 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5123 cb.cb_name_flags |= VDEV_NAME_PATH;
5126 cb.cb_literal = B_TRUE;
5129 get_timestamp_arg(*optarg);
5132 cb.cb_verbose = B_TRUE;
5135 (void) fprintf(stderr, gettext("missing argument for "
5136 "'%c' option\n"), optopt);
5140 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5149 get_interval_count(&argc, argv, &interval, &count);
5151 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5155 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5159 if (pool_list_count(list) == 0)
5162 if (timestamp_fmt != NODATE)
5163 print_timestamp(timestamp_fmt);
5165 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5169 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5174 if (count != 0 && --count == 0)
5177 pool_list_free(list);
5178 (void) fsleep(interval);
5181 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5182 (void) printf(gettext("no pools available\n"));
5186 pool_list_free(list);
5187 zprop_free_list(cb.cb_proplist);
5192 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5194 boolean_t force = B_FALSE;
5197 char *poolname, *old_disk, *new_disk;
5198 zpool_handle_t *zhp;
5199 nvlist_t *props = NULL;
5204 while ((c = getopt(argc, argv, "fo:")) != -1) {
5210 if ((propval = strchr(optarg, '=')) == NULL) {
5211 (void) fprintf(stderr, gettext("missing "
5212 "'=' for -o option\n"));
5218 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5219 (add_prop_list(optarg, propval, &props, B_TRUE)))
5223 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5232 /* get pool name and check number of arguments */
5234 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5241 (void) fprintf(stderr,
5242 gettext("missing <device> specification\n"));
5250 (void) fprintf(stderr,
5251 gettext("missing <new_device> specification\n"));
5254 new_disk = old_disk;
5264 (void) fprintf(stderr, gettext("too many arguments\n"));
5268 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5273 if (zpool_get_config(zhp, NULL) == NULL) {
5274 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5281 /* unless manually specified use "ashift" pool property (if set) */
5282 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5285 char strval[ZPOOL_MAXPROPLEN];
5287 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5288 if (src != ZPROP_SRC_DEFAULT) {
5289 (void) sprintf(strval, "%" PRId32, intval);
5290 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5291 &props, B_TRUE) == 0);
5295 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5297 if (nvroot == NULL) {
5303 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5306 nvlist_free(nvroot);
5313 * zpool replace [-f] <pool> <device> <new_device>
5315 * -f Force attach, even if <new_device> appears to be in use.
5317 * Replace <device> with <new_device>.
5321 zpool_do_replace(int argc, char **argv)
5323 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5327 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5329 * -f Force attach, even if <new_device> appears to be in use.
5330 * -o Set property=value.
5332 * Attach <new_device> to the mirror containing <device>. If <device> is not
5333 * part of a mirror, then <device> will be transformed into a mirror of
5334 * <device> and <new_device>. In either case, <new_device> will begin life
5335 * with a DTL of [0, now], and will immediately begin to resilver itself.
5338 zpool_do_attach(int argc, char **argv)
5340 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5344 * zpool detach [-f] <pool> <device>
5346 * -f Force detach of <device>, even if DTLs argue against it
5347 * (not supported yet)
5349 * Detach a device from a mirror. The operation will be refused if <device>
5350 * is the last device in the mirror, or if the DTLs indicate that this device
5351 * has the only valid copy of some data.
5355 zpool_do_detach(int argc, char **argv)
5358 char *poolname, *path;
5359 zpool_handle_t *zhp;
5363 while ((c = getopt(argc, argv, "f")) != -1) {
5367 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5376 /* get pool name and check number of arguments */
5378 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5383 (void) fprintf(stderr,
5384 gettext("missing <device> specification\n"));
5391 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5394 ret = zpool_vdev_detach(zhp, path);
5402 * zpool split [-gLnP] [-o prop=val] ...
5404 * [-R altroot] <pool> <newpool> [<device> ...]
5406 * -g Display guid for individual vdev name.
5407 * -L Follow links when resolving vdev path name.
5408 * -n Do not split the pool, but display the resulting layout if
5409 * it were to be split.
5410 * -o Set property=value, or set mount options.
5411 * -P Display full path for vdev name.
5412 * -R Mount the split-off pool under an alternate root.
5413 * -l Load encryption keys while importing.
5415 * Splits the named pool and gives it the new pool name. Devices to be split
5416 * off may be listed, provided that no more than one device is specified
5417 * per top-level vdev mirror. The newly split pool is left in an exported
5418 * state unless -R is specified.
5420 * Restrictions: the top-level of the pool pool must only be made up of
5421 * mirrors; all devices in the pool must be healthy; no device may be
5422 * undergoing a resilvering operation.
5425 zpool_do_split(int argc, char **argv)
5427 char *srcpool, *newpool, *propval;
5428 char *mntopts = NULL;
5431 boolean_t loadkeys = B_FALSE;
5432 zpool_handle_t *zhp;
5433 nvlist_t *config, *props = NULL;
5435 flags.dryrun = B_FALSE;
5436 flags.import = B_FALSE;
5437 flags.name_flags = 0;
5440 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5443 flags.name_flags |= VDEV_NAME_GUID;
5446 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5449 flags.import = B_TRUE;
5451 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5452 &props, B_TRUE) != 0) {
5461 flags.dryrun = B_TRUE;
5464 if ((propval = strchr(optarg, '=')) != NULL) {
5467 if (add_prop_list(optarg, propval,
5468 &props, B_TRUE) != 0) {
5477 flags.name_flags |= VDEV_NAME_PATH;
5480 (void) fprintf(stderr, gettext("missing argument for "
5481 "'%c' option\n"), optopt);
5485 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5492 if (!flags.import && mntopts != NULL) {
5493 (void) fprintf(stderr, gettext("setting mntopts is only "
5494 "valid when importing the pool\n"));
5498 if (!flags.import && loadkeys) {
5499 (void) fprintf(stderr, gettext("loading keys is only "
5500 "valid when importing the pool\n"));
5508 (void) fprintf(stderr, gettext("Missing pool name\n"));
5512 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5522 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5527 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5528 if (config == NULL) {
5532 (void) printf(gettext("would create '%s' with the "
5533 "following layout:\n\n"), newpool);
5534 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5541 if (ret != 0 || flags.dryrun || !flags.import) {
5542 nvlist_free(config);
5548 * The split was successful. Now we need to open the new
5549 * pool and import it.
5551 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5552 nvlist_free(config);
5558 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5563 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5564 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5566 (void) fprintf(stderr, gettext("Split was successful, but "
5567 "the datasets could not all be mounted\n"));
5568 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5569 "different altroot\n"), "zpool import");
5572 nvlist_free(config);
5581 * zpool online <pool> <device> ...
5584 zpool_do_online(int argc, char **argv)
5588 zpool_handle_t *zhp;
5590 vdev_state_t newstate;
5594 while ((c = getopt(argc, argv, "et")) != -1) {
5597 flags |= ZFS_ONLINE_EXPAND;
5601 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5610 /* get pool name and check number of arguments */
5612 (void) fprintf(stderr, gettext("missing pool name\n"));
5616 (void) fprintf(stderr, gettext("missing device name\n"));
5622 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5625 for (i = 1; i < argc; i++) {
5626 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5627 if (newstate != VDEV_STATE_HEALTHY) {
5628 (void) printf(gettext("warning: device '%s' "
5629 "onlined, but remains in faulted state\n"),
5631 if (newstate == VDEV_STATE_FAULTED)
5632 (void) printf(gettext("use 'zpool "
5633 "clear' to restore a faulted "
5636 (void) printf(gettext("use 'zpool "
5637 "replace' to replace devices "
5638 "that are no longer present\n"));
5651 * zpool offline [-ft] <pool> <device> ...
5653 * -f Force the device into a faulted state.
5655 * -t Only take the device off-line temporarily. The offline/faulted
5656 * state will not be persistent across reboots.
5660 zpool_do_offline(int argc, char **argv)
5664 zpool_handle_t *zhp;
5666 boolean_t istmp = B_FALSE;
5667 boolean_t fault = B_FALSE;
5670 while ((c = getopt(argc, argv, "ft")) != -1) {
5679 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5688 /* get pool name and check number of arguments */
5690 (void) fprintf(stderr, gettext("missing pool name\n"));
5694 (void) fprintf(stderr, gettext("missing device name\n"));
5700 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5703 for (i = 1; i < argc; i++) {
5705 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
5707 if (istmp == B_FALSE) {
5708 /* Force the fault to persist across imports */
5709 aux = VDEV_AUX_EXTERNAL_PERSIST;
5711 aux = VDEV_AUX_EXTERNAL;
5714 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
5717 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5728 * zpool clear <pool> [device]
5730 * Clear all errors associated with a pool or a particular device.
5733 zpool_do_clear(int argc, char **argv)
5737 boolean_t dryrun = B_FALSE;
5738 boolean_t do_rewind = B_FALSE;
5739 boolean_t xtreme_rewind = B_FALSE;
5740 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5741 nvlist_t *policy = NULL;
5742 zpool_handle_t *zhp;
5743 char *pool, *device;
5746 while ((c = getopt(argc, argv, "FnX")) != -1) {
5755 xtreme_rewind = B_TRUE;
5758 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5768 (void) fprintf(stderr, gettext("missing pool name\n"));
5773 (void) fprintf(stderr, gettext("too many arguments\n"));
5777 if ((dryrun || xtreme_rewind) && !do_rewind) {
5778 (void) fprintf(stderr,
5779 gettext("-n or -X only meaningful with -F\n"));
5783 rewind_policy = ZPOOL_TRY_REWIND;
5785 rewind_policy = ZPOOL_DO_REWIND;
5787 rewind_policy |= ZPOOL_EXTREME_REWIND;
5789 /* In future, further rewind policy choices can be passed along here */
5790 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5791 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5795 device = argc == 2 ? argv[1] : NULL;
5797 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5798 nvlist_free(policy);
5802 if (zpool_clear(zhp, device, policy) != 0)
5807 nvlist_free(policy);
5813 * zpool reguid <pool>
5816 zpool_do_reguid(int argc, char **argv)
5820 zpool_handle_t *zhp;
5824 while ((c = getopt(argc, argv, "")) != -1) {
5827 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5836 /* get pool name and check number of arguments */
5838 (void) fprintf(stderr, gettext("missing pool name\n"));
5843 (void) fprintf(stderr, gettext("too many arguments\n"));
5848 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5851 ret = zpool_reguid(zhp);
5859 * zpool reopen <pool>
5861 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5864 zpool_do_reopen(int argc, char **argv)
5868 boolean_t scrub_restart = B_TRUE;
5871 while ((c = getopt(argc, argv, "n")) != -1) {
5874 scrub_restart = B_FALSE;
5877 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5886 /* if argc == 0 we will execute zpool_reopen_one on all pools */
5887 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
5893 typedef struct scrub_cbdata {
5897 pool_scrub_cmd_t cb_scrub_cmd;
5901 scrub_callback(zpool_handle_t *zhp, void *data)
5903 scrub_cbdata_t *cb = data;
5907 * Ignore faulted pools.
5909 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5910 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5911 "currently unavailable\n"), zpool_get_name(zhp));
5915 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
5921 * zpool scrub [-s | -p] <pool> ...
5923 * -s Stop. Stops any in-progress scrub.
5924 * -p Pause. Pause in-progress scrub.
5927 zpool_do_scrub(int argc, char **argv)
5932 cb.cb_type = POOL_SCAN_SCRUB;
5933 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
5936 while ((c = getopt(argc, argv, "sp")) != -1) {
5939 cb.cb_type = POOL_SCAN_NONE;
5942 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
5945 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5951 if (cb.cb_type == POOL_SCAN_NONE &&
5952 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
5953 (void) fprintf(stderr, gettext("invalid option combination: "
5954 "-s and -p are mutually exclusive\n"));
5964 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5968 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5972 * Print out detailed scrub status.
5975 print_scan_status(pool_scan_stat_t *ps)
5977 time_t start, end, pause;
5978 uint64_t total_secs_left;
5979 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
5980 uint64_t pass_scanned, scanned, pass_issued, issued, total;
5981 uint_t scan_rate, issue_rate;
5982 double fraction_done;
5983 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
5984 char srate_buf[7], irate_buf[7];
5986 (void) printf(gettext(" scan: "));
5988 /* If there's never been a scan, there's not much to say. */
5989 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5990 ps->pss_func >= POOL_SCAN_FUNCS) {
5991 (void) printf(gettext("none requested\n"));
5995 start = ps->pss_start_time;
5996 end = ps->pss_end_time;
5997 pause = ps->pss_pass_scrub_pause;
5999 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6001 assert(ps->pss_func == POOL_SCAN_SCRUB ||
6002 ps->pss_func == POOL_SCAN_RESILVER);
6004 /* Scan is finished or canceled. */
6005 if (ps->pss_state == DSS_FINISHED) {
6006 total_secs_left = end - start;
6007 days_left = total_secs_left / 60 / 60 / 24;
6008 hours_left = (total_secs_left / 60 / 60) % 24;
6009 mins_left = (total_secs_left / 60) % 60;
6010 secs_left = (total_secs_left % 60);
6012 if (ps->pss_func == POOL_SCAN_SCRUB) {
6013 (void) printf(gettext("scrub repaired %s "
6014 "in %llu days %02llu:%02llu:%02llu "
6015 "with %llu errors on %s"), processed_buf,
6016 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6017 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6018 (u_longlong_t)ps->pss_errors, ctime(&end));
6019 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6020 (void) printf(gettext("resilvered %s "
6021 "in %llu days %02llu:%02llu:%02llu "
6022 "with %llu errors on %s"), processed_buf,
6023 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6024 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6025 (u_longlong_t)ps->pss_errors, ctime(&end));
6028 } else if (ps->pss_state == DSS_CANCELED) {
6029 if (ps->pss_func == POOL_SCAN_SCRUB) {
6030 (void) printf(gettext("scrub canceled on %s"),
6032 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6033 (void) printf(gettext("resilver canceled on %s"),
6039 assert(ps->pss_state == DSS_SCANNING);
6041 /* Scan is in progress. Resilvers can't be paused. */
6042 if (ps->pss_func == POOL_SCAN_SCRUB) {
6044 (void) printf(gettext("scrub in progress since %s"),
6047 (void) printf(gettext("scrub paused since %s"),
6049 (void) printf(gettext("\tscrub started on %s"),
6052 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6053 (void) printf(gettext("resilver in progress since %s"),
6057 scanned = ps->pss_examined;
6058 pass_scanned = ps->pss_pass_exam;
6059 issued = ps->pss_issued;
6060 pass_issued = ps->pss_pass_issued;
6061 total = ps->pss_to_examine;
6063 /* we are only done with a block once we have issued the IO for it */
6064 fraction_done = (double)issued / total;
6066 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6067 elapsed = time(NULL) - ps->pss_pass_start;
6068 elapsed -= ps->pss_pass_scrub_spent_paused;
6069 elapsed = (elapsed != 0) ? elapsed : 1;
6071 scan_rate = pass_scanned / elapsed;
6072 issue_rate = pass_issued / elapsed;
6073 total_secs_left = (issue_rate != 0) ?
6074 ((total - issued) / issue_rate) : UINT64_MAX;
6076 days_left = total_secs_left / 60 / 60 / 24;
6077 hours_left = (total_secs_left / 60 / 60) % 24;
6078 mins_left = (total_secs_left / 60) % 60;
6079 secs_left = (total_secs_left % 60);
6081 /* format all of the numbers we will be reporting */
6082 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
6083 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
6084 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6085 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
6086 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
6088 /* do not print estimated time if we have a paused scrub */
6090 (void) printf(gettext("\t%s scanned at %s/s, "
6091 "%s issued at %s/s, %s total\n"),
6092 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
6094 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6095 scanned_buf, issued_buf, total_buf);
6098 if (ps->pss_func == POOL_SCAN_RESILVER) {
6099 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6100 processed_buf, 100 * fraction_done);
6101 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6102 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6103 processed_buf, 100 * fraction_done);
6107 if (issue_rate >= 10 * 1024 * 1024) {
6108 (void) printf(gettext(", %llu days "
6109 "%02llu:%02llu:%02llu to go\n"),
6110 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6111 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
6113 (void) printf(gettext(", no estimated "
6114 "completion time\n"));
6117 (void) printf(gettext("\n"));
6122 print_error_log(zpool_handle_t *zhp)
6124 nvlist_t *nverrlist = NULL;
6127 size_t len = MAXPATHLEN * 2;
6129 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6132 (void) printf("errors: Permanent errors have been "
6133 "detected in the following files:\n\n");
6135 pathname = safe_malloc(len);
6137 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6139 uint64_t dsobj, obj;
6141 verify(nvpair_value_nvlist(elem, &nv) == 0);
6142 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6144 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6146 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6147 (void) printf("%7s %s\n", "", pathname);
6150 nvlist_free(nverrlist);
6154 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6163 (void) printf(gettext("\tspares\n"));
6165 for (i = 0; i < nspares; i++) {
6166 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6168 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6174 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6183 (void) printf(gettext("\tcache\n"));
6185 for (i = 0; i < nl2cache; i++) {
6186 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6188 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6194 print_dedup_stats(nvlist_t *config)
6196 ddt_histogram_t *ddh;
6200 char dspace[6], mspace[6];
6203 * If the pool was faulted then we may not have been able to
6204 * obtain the config. Otherwise, if we have anything in the dedup
6205 * table continue processing the stats.
6207 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6208 (uint64_t **)&ddo, &c) != 0)
6211 (void) printf("\n");
6212 (void) printf(gettext(" dedup: "));
6213 if (ddo->ddo_count == 0) {
6214 (void) printf(gettext("no DDT entries\n"));
6218 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6219 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6220 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6221 (u_longlong_t)ddo->ddo_count,
6225 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6226 (uint64_t **)&dds, &c) == 0);
6227 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6228 (uint64_t **)&ddh, &c) == 0);
6229 zpool_dump_ddt(dds, ddh);
6233 * Display a summary of pool status. Displays a summary such as:
6237 * reason: One or more devices ...
6238 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6244 * When given the '-v' option, we print out the complete config. If the '-e'
6245 * option is specified, then we print out error rate information as well.
6248 status_callback(zpool_handle_t *zhp, void *data)
6250 status_cbdata_t *cbp = data;
6251 nvlist_t *config, *nvroot;
6253 zpool_status_t reason;
6254 zpool_errata_t errata;
6259 config = zpool_get_config(zhp, NULL);
6260 reason = zpool_get_status(zhp, &msgid, &errata);
6265 * If we were given 'zpool status -x', only report those pools with
6268 if (cbp->cb_explain &&
6269 (reason == ZPOOL_STATUS_OK ||
6270 reason == ZPOOL_STATUS_VERSION_OLDER ||
6271 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6272 if (!cbp->cb_allpools) {
6273 (void) printf(gettext("pool '%s' is healthy\n"),
6274 zpool_get_name(zhp));
6276 cbp->cb_first = B_FALSE;
6282 cbp->cb_first = B_FALSE;
6284 (void) printf("\n");
6286 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6288 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6289 (uint64_t **)&vs, &c) == 0);
6290 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6292 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6293 (void) printf(gettext(" state: %s\n"), health);
6296 case ZPOOL_STATUS_MISSING_DEV_R:
6297 (void) printf(gettext("status: One or more devices could not "
6298 "be opened. Sufficient replicas exist for\n\tthe pool to "
6299 "continue functioning in a degraded state.\n"));
6300 (void) printf(gettext("action: Attach the missing device and "
6301 "online it using 'zpool online'.\n"));
6304 case ZPOOL_STATUS_MISSING_DEV_NR:
6305 (void) printf(gettext("status: One or more devices could not "
6306 "be opened. There are insufficient\n\treplicas for the "
6307 "pool to continue functioning.\n"));
6308 (void) printf(gettext("action: Attach the missing device and "
6309 "online it using 'zpool online'.\n"));
6312 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6313 (void) printf(gettext("status: One or more devices could not "
6314 "be used because the label is missing or\n\tinvalid. "
6315 "Sufficient replicas exist for the pool to continue\n\t"
6316 "functioning in a degraded state.\n"));
6317 (void) printf(gettext("action: Replace the device using "
6318 "'zpool replace'.\n"));
6321 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6322 (void) printf(gettext("status: One or more devices could not "
6323 "be used because the label is missing \n\tor invalid. "
6324 "There are insufficient replicas for the pool to "
6325 "continue\n\tfunctioning.\n"));
6326 zpool_explain_recover(zpool_get_handle(zhp),
6327 zpool_get_name(zhp), reason, config);
6330 case ZPOOL_STATUS_FAILING_DEV:
6331 (void) printf(gettext("status: One or more devices has "
6332 "experienced an unrecoverable error. An\n\tattempt was "
6333 "made to correct the error. Applications are "
6335 (void) printf(gettext("action: Determine if the device needs "
6336 "to be replaced, and clear the errors\n\tusing "
6337 "'zpool clear' or replace the device with 'zpool "
6341 case ZPOOL_STATUS_OFFLINE_DEV:
6342 (void) printf(gettext("status: One or more devices has "
6343 "been taken offline by the administrator.\n\tSufficient "
6344 "replicas exist for the pool to continue functioning in "
6345 "a\n\tdegraded state.\n"));
6346 (void) printf(gettext("action: Online the device using "
6347 "'zpool online' or replace the device with\n\t'zpool "
6351 case ZPOOL_STATUS_REMOVED_DEV:
6352 (void) printf(gettext("status: One or more devices has "
6353 "been removed by the administrator.\n\tSufficient "
6354 "replicas exist for the pool to continue functioning in "
6355 "a\n\tdegraded state.\n"));
6356 (void) printf(gettext("action: Online the device using "
6357 "'zpool online' or replace the device with\n\t'zpool "
6361 case ZPOOL_STATUS_RESILVERING:
6362 (void) printf(gettext("status: One or more devices is "
6363 "currently being resilvered. The pool will\n\tcontinue "
6364 "to function, possibly in a degraded state.\n"));
6365 (void) printf(gettext("action: Wait for the resilver to "
6369 case ZPOOL_STATUS_CORRUPT_DATA:
6370 (void) printf(gettext("status: One or more devices has "
6371 "experienced an error resulting in data\n\tcorruption. "
6372 "Applications may be affected.\n"));
6373 (void) printf(gettext("action: Restore the file in question "
6374 "if possible. Otherwise restore the\n\tentire pool from "
6378 case ZPOOL_STATUS_CORRUPT_POOL:
6379 (void) printf(gettext("status: The pool metadata is corrupted "
6380 "and the pool cannot be opened.\n"));
6381 zpool_explain_recover(zpool_get_handle(zhp),
6382 zpool_get_name(zhp), reason, config);
6385 case ZPOOL_STATUS_VERSION_OLDER:
6386 (void) printf(gettext("status: The pool is formatted using a "
6387 "legacy on-disk format. The pool can\n\tstill be used, "
6388 "but some features are unavailable.\n"));
6389 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6390 "upgrade'. Once this is done, the\n\tpool will no longer "
6391 "be accessible on software that does not support\n\t"
6392 "feature flags.\n"));
6395 case ZPOOL_STATUS_VERSION_NEWER:
6396 (void) printf(gettext("status: The pool has been upgraded to a "
6397 "newer, incompatible on-disk version.\n\tThe pool cannot "
6398 "be accessed on this system.\n"));
6399 (void) printf(gettext("action: Access the pool from a system "
6400 "running more recent software, or\n\trestore the pool from "
6404 case ZPOOL_STATUS_FEAT_DISABLED:
6405 (void) printf(gettext("status: Some supported features are not "
6406 "enabled on the pool. The pool can\n\tstill be used, but "
6407 "some features are unavailable.\n"));
6408 (void) printf(gettext("action: Enable all features using "
6409 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6410 "longer be accessible by software that does not support\n\t"
6411 "the features. See zpool-features(5) for details.\n"));
6414 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6415 (void) printf(gettext("status: The pool cannot be accessed on "
6416 "this system because it uses the\n\tfollowing feature(s) "
6417 "not supported on this system:\n"));
6418 zpool_print_unsup_feat(config);
6419 (void) printf("\n");
6420 (void) printf(gettext("action: Access the pool from a system "
6421 "that supports the required feature(s),\n\tor restore the "
6422 "pool from backup.\n"));
6425 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6426 (void) printf(gettext("status: The pool can only be accessed "
6427 "in read-only mode on this system. It\n\tcannot be "
6428 "accessed in read-write mode because it uses the "
6429 "following\n\tfeature(s) not supported on this system:\n"));
6430 zpool_print_unsup_feat(config);
6431 (void) printf("\n");
6432 (void) printf(gettext("action: The pool cannot be accessed in "
6433 "read-write mode. Import the pool with\n"
6434 "\t\"-o readonly=on\", access the pool from a system that "
6435 "supports the\n\trequired feature(s), or restore the "
6436 "pool from backup.\n"));
6439 case ZPOOL_STATUS_FAULTED_DEV_R:
6440 (void) printf(gettext("status: One or more devices are "
6441 "faulted in response to persistent errors.\n\tSufficient "
6442 "replicas exist for the pool to continue functioning "
6443 "in a\n\tdegraded state.\n"));
6444 (void) printf(gettext("action: Replace the faulted device, "
6445 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6448 case ZPOOL_STATUS_FAULTED_DEV_NR:
6449 (void) printf(gettext("status: One or more devices are "
6450 "faulted in response to persistent errors. There are "
6451 "insufficient replicas for the pool to\n\tcontinue "
6453 (void) printf(gettext("action: Destroy and re-create the pool "
6454 "from a backup source. Manually marking the device\n"
6455 "\trepaired using 'zpool clear' may allow some data "
6456 "to be recovered.\n"));
6459 case ZPOOL_STATUS_IO_FAILURE_WAIT:
6460 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6461 (void) printf(gettext("status: One or more devices are "
6462 "faulted in response to IO failures.\n"));
6463 (void) printf(gettext("action: Make sure the affected devices "
6464 "are connected, then run 'zpool clear'.\n"));
6467 case ZPOOL_STATUS_BAD_LOG:
6468 (void) printf(gettext("status: An intent log record "
6469 "could not be read.\n"
6470 "\tWaiting for administrator intervention to fix the "
6471 "faulted pool.\n"));
6472 (void) printf(gettext("action: Either restore the affected "
6473 "device(s) and run 'zpool online',\n"
6474 "\tor ignore the intent log records by running "
6475 "'zpool clear'.\n"));
6478 case ZPOOL_STATUS_HOSTID_MISMATCH:
6479 (void) printf(gettext("status: Mismatch between pool hostid "
6480 "and system hostid on imported pool.\n\tThis pool was "
6481 "previously imported into a system with a different "
6482 "hostid,\n\tand then was verbatim imported into this "
6484 (void) printf(gettext("action: Export this pool on all systems "
6485 "on which it is imported.\n"
6486 "\tThen import it to correct the mismatch.\n"));
6489 case ZPOOL_STATUS_ERRATA:
6490 (void) printf(gettext("status: Errata #%d detected.\n"),
6494 case ZPOOL_ERRATA_NONE:
6497 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
6498 (void) printf(gettext("action: To correct the issue "
6499 "run 'zpool scrub'.\n"));
6504 * All errata which allow the pool to be imported
6505 * must contain an action message.
6513 * The remaining errors can't actually be generated, yet.
6515 assert(reason == ZPOOL_STATUS_OK);
6519 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6522 if (config != NULL) {
6524 nvlist_t **spares, **l2cache;
6525 uint_t nspares, nl2cache;
6526 pool_scan_stat_t *ps = NULL;
6528 (void) nvlist_lookup_uint64_array(nvroot,
6529 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6530 print_scan_status(ps);
6532 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6533 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6534 if (cbp->cb_namewidth < 10)
6535 cbp->cb_namewidth = 10;
6537 (void) printf(gettext("config:\n\n"));
6538 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6539 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6542 if (cbp->vcdl != NULL)
6543 print_cmd_columns(cbp->vcdl, 0);
6546 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6549 if (num_logs(nvroot) > 0)
6550 print_logs(zhp, cbp, nvroot);
6551 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6552 &l2cache, &nl2cache) == 0)
6553 print_l2cache(zhp, cbp, l2cache, nl2cache);
6555 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6556 &spares, &nspares) == 0)
6557 print_spares(zhp, cbp, spares, nspares);
6559 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6561 nvlist_t *nverrlist = NULL;
6564 * If the approximate error count is small, get a
6565 * precise count by fetching the entire log and
6566 * uniquifying the results.
6568 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6569 zpool_get_errlog(zhp, &nverrlist) == 0) {
6574 while ((elem = nvlist_next_nvpair(nverrlist,
6579 nvlist_free(nverrlist);
6581 (void) printf("\n");
6584 (void) printf(gettext("errors: No known data "
6586 else if (!cbp->cb_verbose)
6587 (void) printf(gettext("errors: %llu data "
6588 "errors, use '-v' for a list\n"),
6589 (u_longlong_t)nerr);
6591 print_error_log(zhp);
6594 if (cbp->cb_dedup_stats)
6595 print_dedup_stats(config);
6597 (void) printf(gettext("config: The configuration cannot be "
6605 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6606 * [interval [count]]
6608 * -c CMD For each vdev, run command CMD
6609 * -g Display guid for individual vdev name.
6610 * -L Follow links when resolving vdev path name.
6611 * -P Display full path for vdev name.
6612 * -v Display complete error logs
6613 * -x Display only pools with potential problems
6614 * -D Display dedup status (undocumented)
6615 * -T Display a timestamp in date(1) or Unix format
6617 * Describes the health status of all pools or some subset.
6620 zpool_do_status(int argc, char **argv)
6625 unsigned long count = 0;
6626 status_cbdata_t cb = { 0 };
6630 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6635 gettext("Can't set -c flag twice\n"));
6639 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6640 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6641 fprintf(stderr, gettext(
6642 "Can't run -c, disabled by "
6643 "ZPOOL_SCRIPTS_ENABLED.\n"));
6647 if ((getuid() <= 0 || geteuid() <= 0) &&
6648 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6649 fprintf(stderr, gettext(
6650 "Can't run -c with root privileges "
6651 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6657 cb.cb_name_flags |= VDEV_NAME_GUID;
6660 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6663 cb.cb_name_flags |= VDEV_NAME_PATH;
6666 cb.cb_verbose = B_TRUE;
6669 cb.cb_explain = B_TRUE;
6672 cb.cb_dedup_stats = B_TRUE;
6675 get_timestamp_arg(*optarg);
6678 if (optopt == 'c') {
6679 print_zpool_script_list("status");
6683 gettext("invalid option '%c'\n"), optopt);
6692 get_interval_count(&argc, argv, &interval, &count);
6695 cb.cb_allpools = B_TRUE;
6697 cb.cb_first = B_TRUE;
6698 cb.cb_print_status = B_TRUE;
6701 if (timestamp_fmt != NODATE)
6702 print_timestamp(timestamp_fmt);
6705 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6708 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6709 status_callback, &cb);
6711 if (cb.vcdl != NULL)
6712 free_vdev_cmd_data_list(cb.vcdl);
6714 if (argc == 0 && cb.cb_count == 0)
6715 (void) fprintf(stderr, gettext("no pools available\n"));
6716 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6717 (void) printf(gettext("all pools are healthy\n"));
6725 if (count != 0 && --count == 0)
6728 (void) fsleep(interval);
6734 typedef struct upgrade_cbdata {
6737 uint64_t cb_version;
6742 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6744 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6745 int *count = (int *)unsupp_fs;
6747 if (zfs_version > ZPL_VERSION) {
6748 (void) printf(gettext("%s (v%d) is not supported by this "
6749 "implementation of ZFS.\n"),
6750 zfs_get_name(zhp), zfs_version);
6754 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6762 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6766 uint64_t oldversion;
6769 config = zpool_get_config(zhp, NULL);
6770 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6773 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6774 assert(oldversion < version);
6776 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6781 (void) fprintf(stderr, gettext("Upgrade not performed due "
6782 "to %d unsupported filesystems (max v%d).\n"),
6783 unsupp_fs, (int)ZPL_VERSION);
6787 ret = zpool_upgrade(zhp, version);
6791 if (version >= SPA_VERSION_FEATURES) {
6792 (void) printf(gettext("Successfully upgraded "
6793 "'%s' from version %llu to feature flags.\n"),
6794 zpool_get_name(zhp), (u_longlong_t)oldversion);
6796 (void) printf(gettext("Successfully upgraded "
6797 "'%s' from version %llu to version %llu.\n"),
6798 zpool_get_name(zhp), (u_longlong_t)oldversion,
6799 (u_longlong_t)version);
6806 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6809 boolean_t firstff = B_TRUE;
6810 nvlist_t *enabled = zpool_get_features(zhp);
6813 for (i = 0; i < SPA_FEATURES; i++) {
6814 const char *fname = spa_feature_table[i].fi_uname;
6815 const char *fguid = spa_feature_table[i].fi_guid;
6816 if (!nvlist_exists(enabled, fguid)) {
6818 verify(-1 != asprintf(&propname, "feature@%s", fname));
6819 ret = zpool_set_prop(zhp, propname,
6820 ZFS_FEATURE_ENABLED);
6828 (void) printf(gettext("Enabled the "
6829 "following features on '%s':\n"),
6830 zpool_get_name(zhp));
6833 (void) printf(gettext(" %s\n"), fname);
6844 upgrade_cb(zpool_handle_t *zhp, void *arg)
6846 upgrade_cbdata_t *cbp = arg;
6849 boolean_t printnl = B_FALSE;
6852 config = zpool_get_config(zhp, NULL);
6853 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6856 assert(SPA_VERSION_IS_SUPPORTED(version));
6858 if (version < cbp->cb_version) {
6859 cbp->cb_first = B_FALSE;
6860 ret = upgrade_version(zhp, cbp->cb_version);
6866 * If they did "zpool upgrade -a", then we could
6867 * be doing ioctls to different pools. We need
6868 * to log this history once to each pool, and bypass
6869 * the normal history logging that happens in main().
6871 (void) zpool_log_history(g_zfs, history_str);
6872 log_history = B_FALSE;
6875 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6877 ret = upgrade_enable_all(zhp, &count);
6882 cbp->cb_first = B_FALSE;
6888 (void) printf(gettext("\n"));
6895 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6897 upgrade_cbdata_t *cbp = arg;
6901 config = zpool_get_config(zhp, NULL);
6902 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6905 assert(SPA_VERSION_IS_SUPPORTED(version));
6907 if (version < SPA_VERSION_FEATURES) {
6908 if (cbp->cb_first) {
6909 (void) printf(gettext("The following pools are "
6910 "formatted with legacy version numbers and can\n"
6911 "be upgraded to use feature flags. After "
6912 "being upgraded, these pools\nwill no "
6913 "longer be accessible by software that does not "
6914 "support feature\nflags.\n\n"));
6915 (void) printf(gettext("VER POOL\n"));
6916 (void) printf(gettext("--- ------------\n"));
6917 cbp->cb_first = B_FALSE;
6920 (void) printf("%2llu %s\n", (u_longlong_t)version,
6921 zpool_get_name(zhp));
6928 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6930 upgrade_cbdata_t *cbp = arg;
6934 config = zpool_get_config(zhp, NULL);
6935 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6938 if (version >= SPA_VERSION_FEATURES) {
6940 boolean_t poolfirst = B_TRUE;
6941 nvlist_t *enabled = zpool_get_features(zhp);
6943 for (i = 0; i < SPA_FEATURES; i++) {
6944 const char *fguid = spa_feature_table[i].fi_guid;
6945 const char *fname = spa_feature_table[i].fi_uname;
6946 if (!nvlist_exists(enabled, fguid)) {
6947 if (cbp->cb_first) {
6948 (void) printf(gettext("\nSome "
6949 "supported features are not "
6950 "enabled on the following pools. "
6951 "Once a\nfeature is enabled the "
6952 "pool may become incompatible with "
6953 "software\nthat does not support "
6955 "zpool-features(5) for "
6957 (void) printf(gettext("POOL "
6959 (void) printf(gettext("------"
6961 cbp->cb_first = B_FALSE;
6965 (void) printf(gettext("%s\n"),
6966 zpool_get_name(zhp));
6967 poolfirst = B_FALSE;
6970 (void) printf(gettext(" %s\n"), fname);
6973 * If they did "zpool upgrade -a", then we could
6974 * be doing ioctls to different pools. We need
6975 * to log this history once to each pool, and bypass
6976 * the normal history logging that happens in main().
6978 (void) zpool_log_history(g_zfs, history_str);
6979 log_history = B_FALSE;
6988 upgrade_one(zpool_handle_t *zhp, void *data)
6990 boolean_t printnl = B_FALSE;
6991 upgrade_cbdata_t *cbp = data;
6992 uint64_t cur_version;
6995 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6996 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6997 "Pool 'log' must be renamed using export and import"
7002 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7003 if (cur_version > cbp->cb_version) {
7004 (void) printf(gettext("Pool '%s' is already formatted "
7005 "using more current version '%llu'.\n\n"),
7006 zpool_get_name(zhp), (u_longlong_t)cur_version);
7010 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
7011 (void) printf(gettext("Pool '%s' is already formatted "
7012 "using version %llu.\n\n"), zpool_get_name(zhp),
7013 (u_longlong_t)cbp->cb_version);
7017 if (cur_version != cbp->cb_version) {
7019 ret = upgrade_version(zhp, cbp->cb_version);
7024 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7026 ret = upgrade_enable_all(zhp, &count);
7032 } else if (cur_version == SPA_VERSION) {
7033 (void) printf(gettext("Pool '%s' already has all "
7034 "supported features enabled.\n"),
7035 zpool_get_name(zhp));
7040 (void) printf(gettext("\n"));
7049 * zpool upgrade [-V version] <-a | pool ...>
7051 * With no arguments, display downrev'd ZFS pool available for upgrade.
7052 * Individual pools can be upgraded by specifying the pool, and '-a' will
7053 * upgrade all pools.
7056 zpool_do_upgrade(int argc, char **argv)
7059 upgrade_cbdata_t cb = { 0 };
7061 boolean_t showversions = B_FALSE;
7062 boolean_t upgradeall = B_FALSE;
7067 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7070 upgradeall = B_TRUE;
7073 showversions = B_TRUE;
7076 cb.cb_version = strtoll(optarg, &end, 10);
7078 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7079 (void) fprintf(stderr,
7080 gettext("invalid version '%s'\n"), optarg);
7085 (void) fprintf(stderr, gettext("missing argument for "
7086 "'%c' option\n"), optopt);
7090 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7101 if (cb.cb_version == 0) {
7102 cb.cb_version = SPA_VERSION;
7103 } else if (!upgradeall && argc == 0) {
7104 (void) fprintf(stderr, gettext("-V option is "
7105 "incompatible with other arguments\n"));
7110 if (upgradeall || argc != 0) {
7111 (void) fprintf(stderr, gettext("-v option is "
7112 "incompatible with other arguments\n"));
7115 } else if (upgradeall) {
7117 (void) fprintf(stderr, gettext("-a option should not "
7118 "be used along with a pool name\n"));
7123 (void) printf(gettext("This system supports ZFS pool feature "
7128 (void) printf(gettext("The following features are "
7130 (void) printf(gettext("FEAT DESCRIPTION\n"));
7131 (void) printf("----------------------------------------------"
7132 "---------------\n");
7133 for (i = 0; i < SPA_FEATURES; i++) {
7134 zfeature_info_t *fi = &spa_feature_table[i];
7136 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7137 " (read-only compatible)" : "";
7139 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7140 (void) printf(" %s\n", fi->fi_desc);
7142 (void) printf("\n");
7144 (void) printf(gettext("The following legacy versions are also "
7146 (void) printf(gettext("VER DESCRIPTION\n"));
7147 (void) printf("--- -----------------------------------------"
7148 "---------------\n");
7149 (void) printf(gettext(" 1 Initial ZFS version\n"));
7150 (void) printf(gettext(" 2 Ditto blocks "
7151 "(replicated metadata)\n"));
7152 (void) printf(gettext(" 3 Hot spares and double parity "
7154 (void) printf(gettext(" 4 zpool history\n"));
7155 (void) printf(gettext(" 5 Compression using the gzip "
7157 (void) printf(gettext(" 6 bootfs pool property\n"));
7158 (void) printf(gettext(" 7 Separate intent log devices\n"));
7159 (void) printf(gettext(" 8 Delegated administration\n"));
7160 (void) printf(gettext(" 9 refquota and refreservation "
7162 (void) printf(gettext(" 10 Cache devices\n"));
7163 (void) printf(gettext(" 11 Improved scrub performance\n"));
7164 (void) printf(gettext(" 12 Snapshot properties\n"));
7165 (void) printf(gettext(" 13 snapused property\n"));
7166 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7167 (void) printf(gettext(" 15 user/group space accounting\n"));
7168 (void) printf(gettext(" 16 stmf property support\n"));
7169 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7170 (void) printf(gettext(" 18 Snapshot user holds\n"));
7171 (void) printf(gettext(" 19 Log device removal\n"));
7172 (void) printf(gettext(" 20 Compression using zle "
7173 "(zero-length encoding)\n"));
7174 (void) printf(gettext(" 21 Deduplication\n"));
7175 (void) printf(gettext(" 22 Received properties\n"));
7176 (void) printf(gettext(" 23 Slim ZIL\n"));
7177 (void) printf(gettext(" 24 System attributes\n"));
7178 (void) printf(gettext(" 25 Improved scrub stats\n"));
7179 (void) printf(gettext(" 26 Improved snapshot deletion "
7181 (void) printf(gettext(" 27 Improved snapshot creation "
7183 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7184 (void) printf(gettext("\nFor more information on a particular "
7185 "version, including supported releases,\n"));
7186 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7187 } else if (argc == 0 && upgradeall) {
7188 cb.cb_first = B_TRUE;
7189 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7190 if (ret == 0 && cb.cb_first) {
7191 if (cb.cb_version == SPA_VERSION) {
7192 (void) printf(gettext("All pools are already "
7193 "formatted using feature flags.\n\n"));
7194 (void) printf(gettext("Every feature flags "
7195 "pool already has all supported features "
7198 (void) printf(gettext("All pools are already "
7199 "formatted with version %llu or higher.\n"),
7200 (u_longlong_t)cb.cb_version);
7203 } else if (argc == 0) {
7204 cb.cb_first = B_TRUE;
7205 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7209 (void) printf(gettext("All pools are formatted "
7210 "using feature flags.\n\n"));
7212 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7213 "for a list of available legacy versions.\n"));
7216 cb.cb_first = B_TRUE;
7217 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7221 (void) printf(gettext("Every feature flags pool has "
7222 "all supported features enabled.\n"));
7224 (void) printf(gettext("\n"));
7227 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7234 typedef struct hist_cbdata {
7241 * Print out the command history for a specific pool.
7244 get_history_one(zpool_handle_t *zhp, void *data)
7250 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7252 cb->first = B_FALSE;
7254 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7256 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7259 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7260 &records, &numrecords) == 0);
7261 for (i = 0; i < numrecords; i++) {
7262 nvlist_t *rec = records[i];
7265 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7269 tsec = fnvlist_lookup_uint64(records[i],
7271 (void) localtime_r(&tsec, &t);
7272 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7275 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7276 (void) printf("%s %s", tbuf,
7277 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7278 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7280 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7283 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7284 (void) printf("%s unrecognized record:\n",
7286 dump_nvlist(rec, 4);
7289 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7290 zfs_history_event_names[ievent],
7291 (longlong_t)fnvlist_lookup_uint64(
7292 rec, ZPOOL_HIST_TXG),
7293 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7294 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7297 (void) printf("%s [txg:%lld] %s", tbuf,
7298 (longlong_t)fnvlist_lookup_uint64(
7299 rec, ZPOOL_HIST_TXG),
7300 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7301 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7302 (void) printf(" %s (%llu)",
7303 fnvlist_lookup_string(rec,
7305 (u_longlong_t)fnvlist_lookup_uint64(rec,
7308 (void) printf(" %s", fnvlist_lookup_string(rec,
7309 ZPOOL_HIST_INT_STR));
7310 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7313 (void) printf("%s ioctl %s\n", tbuf,
7314 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7315 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7316 (void) printf(" input:\n");
7317 dump_nvlist(fnvlist_lookup_nvlist(rec,
7318 ZPOOL_HIST_INPUT_NVL), 8);
7320 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7321 (void) printf(" output:\n");
7322 dump_nvlist(fnvlist_lookup_nvlist(rec,
7323 ZPOOL_HIST_OUTPUT_NVL), 8);
7328 (void) printf("%s unrecognized record:\n", tbuf);
7329 dump_nvlist(rec, 4);
7333 (void) printf("\n");
7336 (void) printf(" [");
7337 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7338 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7339 struct passwd *pwd = getpwuid(who);
7340 (void) printf("user %d ", (int)who);
7342 (void) printf("(%s) ", pwd->pw_name);
7344 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7345 (void) printf("on %s",
7346 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7348 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7349 (void) printf(":%s",
7350 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7354 (void) printf("\n");
7356 (void) printf("\n");
7363 * zpool history <pool>
7365 * Displays the history of commands that modified pools.
7368 zpool_do_history(int argc, char **argv)
7370 hist_cbdata_t cbdata = { 0 };
7374 cbdata.first = B_TRUE;
7376 while ((c = getopt(argc, argv, "li")) != -1) {
7379 cbdata.longfmt = B_TRUE;
7382 cbdata.internal = B_TRUE;
7385 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7393 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
7396 if (argc == 0 && cbdata.first == B_TRUE) {
7397 (void) fprintf(stderr, gettext("no pools available\n"));
7404 typedef struct ev_opts {
7409 char poolname[ZFS_MAX_DATASET_NAME_LEN];
7413 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7415 char ctime_str[26], str[32], *ptr;
7419 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7420 memset(str, ' ', 32);
7421 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7422 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
7423 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
7424 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
7425 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7427 (void) printf(gettext("%s\t"), str);
7429 (void) printf(gettext("%s "), str);
7431 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7432 (void) printf(gettext("%s\n"), ptr);
7436 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7440 for (nvp = nvlist_next_nvpair(nvl, NULL);
7441 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
7443 data_type_t type = nvpair_type(nvp);
7444 const char *name = nvpair_name(nvp);
7454 printf(gettext("%*s%s = "), depth, "", name);
7457 case DATA_TYPE_BOOLEAN:
7458 printf(gettext("%s"), "1");
7461 case DATA_TYPE_BOOLEAN_VALUE:
7462 (void) nvpair_value_boolean_value(nvp, &b);
7463 printf(gettext("%s"), b ? "1" : "0");
7466 case DATA_TYPE_BYTE:
7467 (void) nvpair_value_byte(nvp, &i8);
7468 printf(gettext("0x%x"), i8);
7471 case DATA_TYPE_INT8:
7472 (void) nvpair_value_int8(nvp, (void *)&i8);
7473 printf(gettext("0x%x"), i8);
7476 case DATA_TYPE_UINT8:
7477 (void) nvpair_value_uint8(nvp, &i8);
7478 printf(gettext("0x%x"), i8);
7481 case DATA_TYPE_INT16:
7482 (void) nvpair_value_int16(nvp, (void *)&i16);
7483 printf(gettext("0x%x"), i16);
7486 case DATA_TYPE_UINT16:
7487 (void) nvpair_value_uint16(nvp, &i16);
7488 printf(gettext("0x%x"), i16);
7491 case DATA_TYPE_INT32:
7492 (void) nvpair_value_int32(nvp, (void *)&i32);
7493 printf(gettext("0x%x"), i32);
7496 case DATA_TYPE_UINT32:
7497 (void) nvpair_value_uint32(nvp, &i32);
7498 printf(gettext("0x%x"), i32);
7501 case DATA_TYPE_INT64:
7502 (void) nvpair_value_int64(nvp, (void *)&i64);
7503 printf(gettext("0x%llx"), (u_longlong_t)i64);
7506 case DATA_TYPE_UINT64:
7507 (void) nvpair_value_uint64(nvp, &i64);
7509 * translate vdev state values to readable
7510 * strings to aide zpool events consumers
7513 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
7515 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
7516 printf(gettext("\"%s\" (0x%llx)"),
7517 zpool_state_to_name(i64, VDEV_AUX_NONE),
7520 printf(gettext("0x%llx"), (u_longlong_t)i64);
7524 case DATA_TYPE_HRTIME:
7525 (void) nvpair_value_hrtime(nvp, (void *)&i64);
7526 printf(gettext("0x%llx"), (u_longlong_t)i64);
7529 case DATA_TYPE_STRING:
7530 (void) nvpair_value_string(nvp, &str);
7531 printf(gettext("\"%s\""), str ? str : "<NULL>");
7534 case DATA_TYPE_NVLIST:
7535 printf(gettext("(embedded nvlist)\n"));
7536 (void) nvpair_value_nvlist(nvp, &cnv);
7537 zpool_do_events_nvprint(cnv, depth + 8);
7538 printf(gettext("%*s(end %s)"), depth, "", name);
7541 case DATA_TYPE_NVLIST_ARRAY: {
7545 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7546 printf(gettext("(%d embedded nvlists)\n"), nelem);
7547 for (i = 0; i < nelem; i++) {
7548 printf(gettext("%*s%s[%d] = %s\n"),
7549 depth, "", name, i, "(embedded nvlist)");
7550 zpool_do_events_nvprint(val[i], depth + 8);
7551 printf(gettext("%*s(end %s[%i])\n"),
7552 depth, "", name, i);
7554 printf(gettext("%*s(end %s)\n"), depth, "", name);
7558 case DATA_TYPE_INT8_ARRAY: {
7562 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7563 for (i = 0; i < nelem; i++)
7564 printf(gettext("0x%x "), val[i]);
7569 case DATA_TYPE_UINT8_ARRAY: {
7573 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7574 for (i = 0; i < nelem; i++)
7575 printf(gettext("0x%x "), val[i]);
7580 case DATA_TYPE_INT16_ARRAY: {
7584 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7585 for (i = 0; i < nelem; i++)
7586 printf(gettext("0x%x "), val[i]);
7591 case DATA_TYPE_UINT16_ARRAY: {
7595 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7596 for (i = 0; i < nelem; i++)
7597 printf(gettext("0x%x "), val[i]);
7602 case DATA_TYPE_INT32_ARRAY: {
7606 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7607 for (i = 0; i < nelem; i++)
7608 printf(gettext("0x%x "), val[i]);
7613 case DATA_TYPE_UINT32_ARRAY: {
7617 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7618 for (i = 0; i < nelem; i++)
7619 printf(gettext("0x%x "), val[i]);
7624 case DATA_TYPE_INT64_ARRAY: {
7628 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7629 for (i = 0; i < nelem; i++)
7630 printf(gettext("0x%llx "),
7631 (u_longlong_t)val[i]);
7636 case DATA_TYPE_UINT64_ARRAY: {
7640 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7641 for (i = 0; i < nelem; i++)
7642 printf(gettext("0x%llx "),
7643 (u_longlong_t)val[i]);
7648 case DATA_TYPE_STRING_ARRAY: {
7652 (void) nvpair_value_string_array(nvp, &str, &nelem);
7653 for (i = 0; i < nelem; i++)
7654 printf(gettext("\"%s\" "),
7655 str[i] ? str[i] : "<NULL>");
7660 case DATA_TYPE_BOOLEAN_ARRAY:
7661 case DATA_TYPE_BYTE_ARRAY:
7662 case DATA_TYPE_DOUBLE:
7663 case DATA_TYPE_UNKNOWN:
7664 printf(gettext("<unknown>"));
7668 printf(gettext("\n"));
7673 zpool_do_events_next(ev_opts_t *opts)
7676 int zevent_fd, ret, dropped;
7679 zevent_fd = open(ZFS_DEV, O_RDWR);
7680 VERIFY(zevent_fd >= 0);
7682 if (!opts->scripted)
7683 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7686 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7687 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7688 if (ret || nvl == NULL)
7692 (void) printf(gettext("dropped %d events\n"), dropped);
7694 if (strlen(opts->poolname) > 0 &&
7695 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
7696 strcmp(opts->poolname, pool) != 0)
7699 zpool_do_events_short(nvl, opts);
7701 if (opts->verbose) {
7702 zpool_do_events_nvprint(nvl, 8);
7703 printf(gettext("\n"));
7705 (void) fflush(stdout);
7710 VERIFY(0 == close(zevent_fd));
7716 zpool_do_events_clear(ev_opts_t *opts)
7720 ret = zpool_events_clear(g_zfs, &count);
7722 (void) printf(gettext("cleared %d events\n"), count);
7728 * zpool events [-vHf [pool] | -c]
7730 * Displays events logs by ZFS.
7733 zpool_do_events(int argc, char **argv)
7735 ev_opts_t opts = { 0 };
7740 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7755 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7764 (void) fprintf(stderr, gettext("too many arguments\n"));
7766 } else if (argc == 1) {
7767 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
7768 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
7769 (void) fprintf(stderr,
7770 gettext("invalid pool name '%s'\n"), opts.poolname);
7775 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
7777 (void) fprintf(stderr,
7778 gettext("invalid options combined with -c\n"));
7783 ret = zpool_do_events_clear(&opts);
7785 ret = zpool_do_events_next(&opts);
7791 get_callback(zpool_handle_t *zhp, void *data)
7793 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7794 char value[MAXNAMELEN];
7795 zprop_source_t srctype;
7798 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7801 * Skip the special fake placeholder. This will also skip
7802 * over the name property when 'all' is specified.
7804 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7805 pl == cbp->cb_proplist)
7808 if (pl->pl_prop == ZPROP_INVAL &&
7809 (zpool_prop_feature(pl->pl_user_prop) ||
7810 zpool_prop_unsupported(pl->pl_user_prop))) {
7811 srctype = ZPROP_SRC_LOCAL;
7813 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7814 value, sizeof (value)) == 0) {
7815 zprop_print_one_property(zpool_get_name(zhp),
7816 cbp, pl->pl_user_prop, value, srctype,
7820 if (zpool_get_prop(zhp, pl->pl_prop, value,
7821 sizeof (value), &srctype, cbp->cb_literal) != 0)
7824 zprop_print_one_property(zpool_get_name(zhp), cbp,
7825 zpool_prop_to_name(pl->pl_prop), value, srctype,
7833 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7835 * -H Scripted mode. Don't display headers, and separate properties
7837 * -o List of columns to display. Defaults to
7838 * "name,property,value,source".
7839 * -p Display values in parsable (exact) format.
7841 * Get properties of pools in the system. Output space statistics
7842 * for each one as well as other attributes.
7845 zpool_do_get(int argc, char **argv)
7847 zprop_get_cbdata_t cb = { 0 };
7848 zprop_list_t fake_name = { 0 };
7853 cb.cb_first = B_TRUE;
7856 * Set up default columns and sources.
7858 cb.cb_sources = ZPROP_SRC_ALL;
7859 cb.cb_columns[0] = GET_COL_NAME;
7860 cb.cb_columns[1] = GET_COL_PROPERTY;
7861 cb.cb_columns[2] = GET_COL_VALUE;
7862 cb.cb_columns[3] = GET_COL_SOURCE;
7863 cb.cb_type = ZFS_TYPE_POOL;
7866 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7869 cb.cb_literal = B_TRUE;
7872 cb.cb_scripted = B_TRUE;
7875 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7877 while (*optarg != '\0') {
7878 static char *col_subopts[] =
7879 { "name", "property", "value", "source",
7882 if (i == ZFS_GET_NCOLS) {
7883 (void) fprintf(stderr, gettext("too "
7884 "many fields given to -o "
7889 switch (getsubopt(&optarg, col_subopts,
7892 cb.cb_columns[i++] = GET_COL_NAME;
7895 cb.cb_columns[i++] = GET_COL_PROPERTY;
7898 cb.cb_columns[i++] = GET_COL_VALUE;
7901 cb.cb_columns[i++] = GET_COL_SOURCE;
7905 (void) fprintf(stderr,
7906 gettext("\"all\" conflicts "
7907 "with specific fields "
7908 "given to -o option\n"));
7911 cb.cb_columns[0] = GET_COL_NAME;
7912 cb.cb_columns[1] = GET_COL_PROPERTY;
7913 cb.cb_columns[2] = GET_COL_VALUE;
7914 cb.cb_columns[3] = GET_COL_SOURCE;
7918 (void) fprintf(stderr,
7919 gettext("invalid column name "
7926 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7936 (void) fprintf(stderr, gettext("missing property "
7941 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7942 ZFS_TYPE_POOL) != 0)
7948 if (cb.cb_proplist != NULL) {
7949 fake_name.pl_prop = ZPOOL_PROP_NAME;
7950 fake_name.pl_width = strlen(gettext("NAME"));
7951 fake_name.pl_next = cb.cb_proplist;
7952 cb.cb_proplist = &fake_name;
7955 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7958 if (cb.cb_proplist == &fake_name)
7959 zprop_free_list(fake_name.pl_next);
7961 zprop_free_list(cb.cb_proplist);
7966 typedef struct set_cbdata {
7969 boolean_t cb_any_successful;
7973 set_callback(zpool_handle_t *zhp, void *data)
7976 set_cbdata_t *cb = (set_cbdata_t *)data;
7978 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7981 cb->cb_any_successful = B_TRUE;
7987 zpool_do_set(int argc, char **argv)
7989 set_cbdata_t cb = { 0 };
7992 if (argc > 1 && argv[1][0] == '-') {
7993 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7999 (void) fprintf(stderr, gettext("missing property=value "
8005 (void) fprintf(stderr, gettext("missing pool name\n"));
8010 (void) fprintf(stderr, gettext("too many pool names\n"));
8014 cb.cb_propname = argv[1];
8015 cb.cb_value = strchr(cb.cb_propname, '=');
8016 if (cb.cb_value == NULL) {
8017 (void) fprintf(stderr, gettext("missing value in "
8018 "property=value argument\n"));
8022 *(cb.cb_value) = '\0';
8025 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8032 find_command_idx(char *command, int *idx)
8036 for (i = 0; i < NCOMMAND; i++) {
8037 if (command_table[i].name == NULL)
8040 if (strcmp(command, command_table[i].name) == 0) {
8049 main(int argc, char **argv)
8055 (void) setlocale(LC_ALL, "");
8056 (void) textdomain(TEXT_DOMAIN);
8062 * Make sure the user has specified some command.
8065 (void) fprintf(stderr, gettext("missing command\n"));
8074 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8077 if ((g_zfs = libzfs_init()) == NULL) {
8078 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8082 libzfs_print_on_error(g_zfs, B_TRUE);
8084 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8087 * Run the appropriate command.
8089 if (find_command_idx(cmdname, &i) == 0) {
8090 current_command = &command_table[i];
8091 ret = command_table[i].func(argc - 1, argv + 1);
8092 } else if (strchr(cmdname, '=')) {
8093 verify(find_command_idx("set", &i) == 0);
8094 current_command = &command_table[i];
8095 ret = command_table[i].func(argc, argv);
8096 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8098 * 'freeze' is a vile debugging abomination, so we treat
8101 zfs_cmd_t zc = {"\0"};
8103 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8104 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8106 (void) fprintf(stderr,
8107 gettext("failed to freeze pool: %d\n"), errno);
8113 (void) fprintf(stderr, gettext("unrecognized "
8114 "command '%s'\n"), cmdname);
8119 if (ret == 0 && log_history)
8120 (void) zpool_log_history(g_zfs, history_str);
8125 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8126 * for the purposes of running ::findleaks.
8128 if (getenv("ZFS_ABORT") != NULL) {
8129 (void) printf("dumping core by request\n");