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.
51 #include <sys/fs/zfs.h>
53 #include <sys/systeminfo.h>
54 #include <sys/fm/fs/zfs.h>
55 #include <sys/fm/util.h>
56 #include <sys/fm/protocol.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/mount.h>
63 #include "zpool_util.h"
64 #include "zfs_comutil.h"
65 #include "zfeature_common.h"
67 #include "statcommon.h"
69 static int zpool_do_create(int, char **);
70 static int zpool_do_destroy(int, char **);
72 static int zpool_do_add(int, char **);
73 static int zpool_do_remove(int, char **);
74 static int zpool_do_labelclear(int, char **);
76 static int zpool_do_list(int, char **);
77 static int zpool_do_iostat(int, char **);
78 static int zpool_do_status(int, char **);
80 static int zpool_do_online(int, char **);
81 static int zpool_do_offline(int, char **);
82 static int zpool_do_clear(int, char **);
83 static int zpool_do_reopen(int, char **);
85 static int zpool_do_reguid(int, char **);
87 static int zpool_do_attach(int, char **);
88 static int zpool_do_detach(int, char **);
89 static int zpool_do_replace(int, char **);
90 static int zpool_do_split(int, char **);
92 static int zpool_do_scrub(int, char **);
94 static int zpool_do_import(int, char **);
95 static int zpool_do_export(int, char **);
97 static int zpool_do_upgrade(int, char **);
99 static int zpool_do_history(int, char **);
100 static int zpool_do_events(int, char **);
102 static int zpool_do_get(int, char **);
103 static int zpool_do_set(int, char **);
105 static int zpool_do_sync(int, char **);
108 * These libumem hooks provide a reasonable set of defaults for the allocator's
109 * debugging facilities.
114 _umem_debug_init(void)
116 return ("default,verbose"); /* $UMEM_DEBUG setting */
120 _umem_logging_init(void)
122 return ("fail,contents"); /* $UMEM_LOGGING setting */
157 * Flags for stats to display with "zpool iostats"
165 IOS_COUNT, /* always last element */
168 /* iostat_type entries as bitmasks */
169 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
170 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
171 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
172 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
173 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
175 /* Mask of all the histo bits */
176 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
179 * Lookup table for iostat flags to nvlist names. Basically a list
180 * of all the nvlists a flag requires. Also specifies the order in
181 * which data gets printed in zpool iostat.
183 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
185 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
186 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
187 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
188 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
193 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
196 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
197 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
198 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
202 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
203 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
204 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
205 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
206 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
209 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
210 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
211 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
212 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
213 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
214 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
215 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
216 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
217 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
218 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
224 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
225 * Right now, only one histo bit is ever set at one time, so we can
226 * just do a highbit64(a)
228 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
230 typedef struct zpool_command {
232 int (*func)(int, char **);
237 * Master command table. Each ZFS command has a name, associated function, and
238 * usage message. The usage messages need to be internationalized, so we have
239 * to have a function to return the usage message based on a command index.
241 * These commands are organized according to how they are displayed in the usage
242 * message. An empty command (one with a NULL name) indicates an empty line in
243 * the generic usage message.
245 static zpool_command_t command_table[] = {
246 { "create", zpool_do_create, HELP_CREATE },
247 { "destroy", zpool_do_destroy, HELP_DESTROY },
249 { "add", zpool_do_add, HELP_ADD },
250 { "remove", zpool_do_remove, HELP_REMOVE },
252 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
254 { "list", zpool_do_list, HELP_LIST },
255 { "iostat", zpool_do_iostat, HELP_IOSTAT },
256 { "status", zpool_do_status, HELP_STATUS },
258 { "online", zpool_do_online, HELP_ONLINE },
259 { "offline", zpool_do_offline, HELP_OFFLINE },
260 { "clear", zpool_do_clear, HELP_CLEAR },
261 { "reopen", zpool_do_reopen, HELP_REOPEN },
263 { "attach", zpool_do_attach, HELP_ATTACH },
264 { "detach", zpool_do_detach, HELP_DETACH },
265 { "replace", zpool_do_replace, HELP_REPLACE },
266 { "split", zpool_do_split, HELP_SPLIT },
268 { "scrub", zpool_do_scrub, HELP_SCRUB },
270 { "import", zpool_do_import, HELP_IMPORT },
271 { "export", zpool_do_export, HELP_EXPORT },
272 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
273 { "reguid", zpool_do_reguid, HELP_REGUID },
275 { "history", zpool_do_history, HELP_HISTORY },
276 { "events", zpool_do_events, HELP_EVENTS },
278 { "get", zpool_do_get, HELP_GET },
279 { "set", zpool_do_set, HELP_SET },
280 { "sync", zpool_do_sync, HELP_SYNC },
283 #define NCOMMAND (ARRAY_SIZE(command_table))
285 static zpool_command_t *current_command;
286 static char history_str[HIS_MAX_RECORD_LEN];
287 static boolean_t log_history = B_TRUE;
288 static uint_t timestamp_fmt = NODATE;
291 get_usage(zpool_help_t idx)
295 return (gettext("\tadd [-fgLnP] [-o property=value] "
296 "<pool> <vdev> ...\n"));
298 return (gettext("\tattach [-f] [-o property=value] "
299 "<pool> <device> <new-device>\n"));
301 return (gettext("\tclear [-nF] <pool> [device]\n"));
303 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
304 "\t [-O file-system-property=value] ... \n"
305 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
307 return (gettext("\tdestroy [-f] <pool>\n"));
309 return (gettext("\tdetach <pool> <device>\n"));
311 return (gettext("\texport [-af] <pool> ...\n"));
313 return (gettext("\thistory [-il] [<pool>] ...\n"));
315 return (gettext("\timport [-d dir] [-D]\n"
316 "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
318 "\timport [-o mntopts] [-o property=value] ... \n"
319 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
320 "[-R root] [-F [-n]] -a\n"
321 "\timport [-o mntopts] [-o property=value] ... \n"
322 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
323 "[-R root] [-F [-n]]\n"
324 "\t <pool | id> [newpool]\n"));
326 return (gettext("\tiostat [[[-c [script1,script2,...]"
327 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
328 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
329 " [interval [count]]\n"));
330 case HELP_LABELCLEAR:
331 return (gettext("\tlabelclear [-f] <vdev>\n"));
333 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
334 "[-T d|u] [pool] ... [interval [count]]\n"));
336 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
338 return (gettext("\tonline <pool> <device> ...\n"));
340 return (gettext("\treplace [-f] [-o property=value] "
341 "<pool> <device> [new-device]\n"));
343 return (gettext("\tremove <pool> <device> ...\n"));
345 return (gettext("\treopen <pool>\n"));
347 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
349 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
350 "[-T d|u] [pool] ... [interval [count]]\n"));
352 return (gettext("\tupgrade\n"
354 "\tupgrade [-V version] <-a | pool ...>\n"));
356 return (gettext("\tevents [-vHfc]\n"));
358 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
359 "<\"all\" | property[,...]> <pool> ...\n"));
361 return (gettext("\tset <property=value> <pool> \n"));
363 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
364 "\t [-o property=value] <pool> <newpool> "
365 "[<device> ...]\n"));
367 return (gettext("\treguid <pool>\n"));
369 return (gettext("\tsync [pool] ...\n"));
378 * Callback routine that will print out a pool property value.
381 print_prop_cb(int prop, void *cb)
385 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
387 if (zpool_prop_readonly(prop))
388 (void) fprintf(fp, " NO ");
390 (void) fprintf(fp, " YES ");
392 if (zpool_prop_values(prop) == NULL)
393 (void) fprintf(fp, "-\n");
395 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
401 * Display usage message. If we're inside a command, display only the usage for
402 * that command. Otherwise, iterate over the entire command table and display
403 * a complete usage message.
406 usage(boolean_t requested)
408 FILE *fp = requested ? stdout : stderr;
410 if (current_command == NULL) {
413 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
415 gettext("where 'command' is one of the following:\n\n"));
417 for (i = 0; i < NCOMMAND; i++) {
418 if (command_table[i].name == NULL)
419 (void) fprintf(fp, "\n");
421 (void) fprintf(fp, "%s",
422 get_usage(command_table[i].usage));
425 (void) fprintf(fp, gettext("usage:\n"));
426 (void) fprintf(fp, "%s", get_usage(current_command->usage));
429 if (current_command != NULL &&
430 ((strcmp(current_command->name, "set") == 0) ||
431 (strcmp(current_command->name, "get") == 0) ||
432 (strcmp(current_command->name, "list") == 0))) {
435 gettext("\nthe following properties are supported:\n"));
437 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
438 "PROPERTY", "EDIT", "VALUES");
440 /* Iterate over all properties */
441 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
444 (void) fprintf(fp, "\t%-15s ", "feature@...");
445 (void) fprintf(fp, "YES disabled | enabled | active\n");
447 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
448 "appended with a feature name.\nSee zpool-features(5).\n"));
452 * See comments at end of main().
454 if (getenv("ZFS_ABORT") != NULL) {
455 (void) printf("dumping core by request\n");
459 exit(requested ? 0 : 2);
463 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
464 boolean_t print_logs, int name_flags)
471 (void) printf("\t%*s%s\n", indent, "", name);
473 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
474 &child, &children) != 0)
477 for (c = 0; c < children; c++) {
478 uint64_t is_log = B_FALSE;
480 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
482 if ((is_log && !print_logs) || (!is_log && print_logs))
485 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
486 print_vdev_tree(zhp, vname, child[c], indent + 2,
487 B_FALSE, name_flags);
493 prop_list_contains_feature(nvlist_t *proplist)
496 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
497 nvp = nvlist_next_nvpair(proplist, nvp)) {
498 if (zpool_prop_feature(nvpair_name(nvp)))
505 * Add a property pair (name, string-value) into a property nvlist.
508 add_prop_list(const char *propname, char *propval, nvlist_t **props,
511 zpool_prop_t prop = ZPROP_INVAL;
517 if (*props == NULL &&
518 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
519 (void) fprintf(stderr,
520 gettext("internal error: out of memory\n"));
527 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
529 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
530 !zpool_prop_feature(propname)) {
531 (void) fprintf(stderr, gettext("property '%s' is "
532 "not a valid pool property\n"), propname);
537 * feature@ properties and version should not be specified
540 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
541 nvlist_exists(proplist, vname)) ||
542 (prop == ZPOOL_PROP_VERSION &&
543 prop_list_contains_feature(proplist))) {
544 (void) fprintf(stderr, gettext("'feature@' and "
545 "'version' properties cannot be specified "
551 if (zpool_prop_feature(propname))
554 normnm = zpool_prop_to_name(prop);
556 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
557 normnm = zfs_prop_to_name(fprop);
563 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
564 prop != ZPOOL_PROP_CACHEFILE) {
565 (void) fprintf(stderr, gettext("property '%s' "
566 "specified multiple times\n"), propname);
570 if (nvlist_add_string(proplist, normnm, propval) != 0) {
571 (void) fprintf(stderr, gettext("internal "
572 "error: out of memory\n"));
580 * Set a default property pair (name, string-value) in a property nvlist
583 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
588 if (nvlist_lookup_string(*props, propname, &pval) == 0)
591 return (add_prop_list(propname, propval, props, B_TRUE));
595 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
597 * -f Force addition of devices, even if they appear in use
598 * -g Display guid for individual vdev name.
599 * -L Follow links when resolving vdev path name.
600 * -n Do not add the devices, but display the resulting layout if
601 * they were to be added.
602 * -o Set property=value.
603 * -P Display full path for vdev name.
605 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
606 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
610 zpool_do_add(int argc, char **argv)
612 boolean_t force = B_FALSE;
613 boolean_t dryrun = B_FALSE;
621 nvlist_t *props = NULL;
625 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
631 name_flags |= VDEV_NAME_GUID;
634 name_flags |= VDEV_NAME_FOLLOW_LINKS;
640 if ((propval = strchr(optarg, '=')) == NULL) {
641 (void) fprintf(stderr, gettext("missing "
642 "'=' for -o option\n"));
648 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
649 (add_prop_list(optarg, propval, &props, B_TRUE)))
653 name_flags |= VDEV_NAME_PATH;
656 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
665 /* get pool name and check number of arguments */
667 (void) fprintf(stderr, gettext("missing pool name argument\n"));
671 (void) fprintf(stderr, gettext("missing vdev specification\n"));
680 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
683 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
684 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
690 /* unless manually specified use "ashift" pool property (if set) */
691 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
694 char strval[ZPOOL_MAXPROPLEN];
696 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
697 if (src != ZPROP_SRC_DEFAULT) {
698 (void) sprintf(strval, "%" PRId32, intval);
699 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
700 &props, B_TRUE) == 0);
704 /* pass off to get_vdev_spec for processing */
705 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
707 if (nvroot == NULL) {
713 nvlist_t *poolnvroot;
715 uint_t l2children, c;
717 boolean_t hadcache = B_FALSE;
719 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
722 (void) printf(gettext("would update '%s' to the following "
723 "configuration:\n"), zpool_get_name(zhp));
725 /* print original main pool and new tree */
726 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
728 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
730 /* Do the same for the logs */
731 if (num_logs(poolnvroot) > 0) {
732 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
734 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
736 } else if (num_logs(nvroot) > 0) {
737 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
741 /* Do the same for the caches */
742 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
743 &l2child, &l2children) == 0 && l2children) {
745 (void) printf(gettext("\tcache\n"));
746 for (c = 0; c < l2children; c++) {
747 vname = zpool_vdev_name(g_zfs, NULL,
748 l2child[c], name_flags);
749 (void) printf("\t %s\n", vname);
753 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
754 &l2child, &l2children) == 0 && l2children) {
756 (void) printf(gettext("\tcache\n"));
757 for (c = 0; c < l2children; c++) {
758 vname = zpool_vdev_name(g_zfs, NULL,
759 l2child[c], name_flags);
760 (void) printf("\t %s\n", vname);
767 ret = (zpool_add(zhp, nvroot) != 0);
778 * zpool remove <pool> <vdev> ...
780 * Removes the given vdev from the pool. Currently, this supports removing
781 * spares, cache, and log devices from the pool.
784 zpool_do_remove(int argc, char **argv)
788 zpool_handle_t *zhp = NULL;
793 /* get pool name and check number of arguments */
795 (void) fprintf(stderr, gettext("missing pool name argument\n"));
799 (void) fprintf(stderr, gettext("missing device\n"));
805 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
808 for (i = 1; i < argc; i++) {
809 if (zpool_vdev_remove(zhp, argv[i]) != 0)
818 * zpool labelclear [-f] <vdev>
820 * -f Force clearing the label for the vdevs which are members of
821 * the exported or foreign pools.
823 * Verifies that the vdev is not active and zeros out the label information
827 zpool_do_labelclear(int argc, char **argv)
829 char vdev[MAXPATHLEN];
832 int c, fd = -1, ret = 0;
835 boolean_t inuse = B_FALSE;
836 boolean_t force = B_FALSE;
839 while ((c = getopt(argc, argv, "f")) != -1) {
845 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
856 (void) fprintf(stderr, gettext("missing vdev name\n"));
860 (void) fprintf(stderr, gettext("too many arguments\n"));
865 * Check if we were given absolute path and use it as is.
866 * Otherwise if the provided vdev name doesn't point to a file,
867 * try prepending expected disk paths and partition numbers.
869 (void) strlcpy(vdev, argv[0], sizeof (vdev));
870 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
873 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
874 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
875 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
879 if (error || (stat(vdev, &st) != 0)) {
880 (void) fprintf(stderr, gettext(
881 "failed to find device %s, try specifying absolute "
882 "path instead\n"), argv[0]);
887 if ((fd = open(vdev, O_RDWR)) < 0) {
888 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
889 vdev, strerror(errno));
893 if (ioctl(fd, BLKFLSBUF) != 0)
894 (void) fprintf(stderr, gettext("failed to invalidate "
895 "cache for %s: %s\n"), vdev, strerror(errno));
897 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
898 (void) fprintf(stderr,
899 gettext("failed to check state for %s\n"), vdev);
904 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
906 (void) fprintf(stderr,
907 gettext("failed to check state for %s\n"), vdev);
916 case POOL_STATE_ACTIVE:
917 case POOL_STATE_SPARE:
918 case POOL_STATE_L2CACHE:
919 (void) fprintf(stderr, gettext(
920 "%s is a member (%s) of pool \"%s\"\n"),
921 vdev, zpool_pool_state_to_name(state), name);
925 case POOL_STATE_EXPORTED:
928 (void) fprintf(stderr, gettext(
929 "use '-f' to override the following error:\n"
930 "%s is a member of exported pool \"%s\"\n"),
935 case POOL_STATE_POTENTIALLY_ACTIVE:
938 (void) fprintf(stderr, gettext(
939 "use '-f' to override the following error:\n"
940 "%s is a member of potentially active pool \"%s\"\n"),
945 case POOL_STATE_DESTROYED:
946 /* inuse should never be set for a destroyed pool */
952 ret = zpool_clear_label(fd);
954 (void) fprintf(stderr,
955 gettext("failed to clear label for %s\n"), vdev);
966 * zpool create [-fnd] [-o property=value] ...
967 * [-O file-system-property=value] ...
968 * [-R root] [-m mountpoint] <pool> <dev> ...
970 * -f Force creation, even if devices appear in use
971 * -n Do not create the pool, but display the resulting layout if it
972 * were to be created.
973 * -R Create a pool under an alternate root
974 * -m Set default mountpoint for the root dataset. By default it's
976 * -o Set property=value.
977 * -o Set feature@feature=enabled|disabled.
978 * -d Don't automatically enable all supported pool features
979 * (individual features can be enabled with -o).
980 * -O Set fsproperty=value in the pool's root file system
982 * Creates the named pool according to the given vdev specification. The
983 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
984 * we get the nvlist back from get_vdev_spec(), we either print out the contents
985 * (if '-n' was specified), or pass it to libzfs to do the creation.
988 zpool_do_create(int argc, char **argv)
990 boolean_t force = B_FALSE;
991 boolean_t dryrun = B_FALSE;
992 boolean_t enable_all_pool_feat = B_TRUE;
994 nvlist_t *nvroot = NULL;
998 char *altroot = NULL;
999 char *mountpoint = NULL;
1000 nvlist_t *fsprops = NULL;
1001 nvlist_t *props = NULL;
1005 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1014 enable_all_pool_feat = B_FALSE;
1018 if (add_prop_list(zpool_prop_to_name(
1019 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1021 if (add_prop_list_default(zpool_prop_to_name(
1022 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1026 /* Equivalent to -O mountpoint=optarg */
1027 mountpoint = optarg;
1030 if ((propval = strchr(optarg, '=')) == NULL) {
1031 (void) fprintf(stderr, gettext("missing "
1032 "'=' for -o option\n"));
1038 if (add_prop_list(optarg, propval, &props, B_TRUE))
1042 * If the user is creating a pool that doesn't support
1043 * feature flags, don't enable any features.
1045 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1049 ver = strtoull(propval, &end, 10);
1051 ver < SPA_VERSION_FEATURES) {
1052 enable_all_pool_feat = B_FALSE;
1055 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1059 if ((propval = strchr(optarg, '=')) == NULL) {
1060 (void) fprintf(stderr, gettext("missing "
1061 "'=' for -O option\n"));
1068 * Mountpoints are checked and then added later.
1069 * Uniquely among properties, they can be specified
1070 * more than once, to avoid conflict with -m.
1072 if (0 == strcmp(optarg,
1073 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1074 mountpoint = propval;
1075 } else if (add_prop_list(optarg, propval, &fsprops,
1082 * Sanity check temporary pool name.
1084 if (strchr(optarg, '/') != NULL) {
1085 (void) fprintf(stderr, gettext("cannot create "
1086 "'%s': invalid character '/' in temporary "
1088 (void) fprintf(stderr, gettext("use 'zfs "
1089 "create' to create a dataset\n"));
1093 if (add_prop_list(zpool_prop_to_name(
1094 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1096 if (add_prop_list_default(zpool_prop_to_name(
1097 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1102 (void) fprintf(stderr, gettext("missing argument for "
1103 "'%c' option\n"), optopt);
1106 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1115 /* get pool name and check number of arguments */
1117 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1121 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1128 * As a special case, check for use of '/' in the name, and direct the
1129 * user to use 'zfs create' instead.
1131 if (strchr(poolname, '/') != NULL) {
1132 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1133 "character '/' in pool name\n"), poolname);
1134 (void) fprintf(stderr, gettext("use 'zfs create' to "
1135 "create a dataset\n"));
1139 /* pass off to get_vdev_spec for bulk processing */
1140 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1141 argc - 1, argv + 1);
1145 /* make_root_vdev() allows 0 toplevel children if there are spares */
1146 if (!zfs_allocatable_devs(nvroot)) {
1147 (void) fprintf(stderr, gettext("invalid vdev "
1148 "specification: at least one toplevel vdev must be "
1153 if (altroot != NULL && altroot[0] != '/') {
1154 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1155 "must be an absolute path\n"), altroot);
1160 * Check the validity of the mountpoint and direct the user to use the
1161 * '-m' mountpoint option if it looks like its in use.
1163 if (mountpoint == NULL ||
1164 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1165 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1166 char buf[MAXPATHLEN];
1169 if (mountpoint && mountpoint[0] != '/') {
1170 (void) fprintf(stderr, gettext("invalid mountpoint "
1171 "'%s': must be an absolute path, 'legacy', or "
1172 "'none'\n"), mountpoint);
1176 if (mountpoint == NULL) {
1177 if (altroot != NULL)
1178 (void) snprintf(buf, sizeof (buf), "%s/%s",
1181 (void) snprintf(buf, sizeof (buf), "/%s",
1184 if (altroot != NULL)
1185 (void) snprintf(buf, sizeof (buf), "%s%s",
1186 altroot, mountpoint);
1188 (void) snprintf(buf, sizeof (buf), "%s",
1192 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1193 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1194 "%s\n"), buf, strerror(errno));
1195 (void) fprintf(stderr, gettext("use '-m' "
1196 "option to provide a different default\n"));
1201 while (count < 3 && readdir(dirp) != NULL)
1203 (void) closedir(dirp);
1206 (void) fprintf(stderr, gettext("mountpoint "
1207 "'%s' exists and is not empty\n"), buf);
1208 (void) fprintf(stderr, gettext("use '-m' "
1209 "option to provide a "
1210 "different default\n"));
1217 * Now that the mountpoint's validity has been checked, ensure that
1218 * the property is set appropriately prior to creating the pool.
1220 if (mountpoint != NULL) {
1221 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1222 mountpoint, &fsprops, B_FALSE);
1230 * For a dry run invocation, print out a basic message and run
1231 * through all the vdevs in the list and print out in an
1232 * appropriate hierarchy.
1234 (void) printf(gettext("would create '%s' with the "
1235 "following layout:\n\n"), poolname);
1237 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1238 if (num_logs(nvroot) > 0)
1239 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1244 * Hand off to libzfs.
1247 for (i = 0; i < SPA_FEATURES; i++) {
1248 char propname[MAXPATHLEN];
1250 zfeature_info_t *feat = &spa_feature_table[i];
1252 (void) snprintf(propname, sizeof (propname),
1253 "feature@%s", feat->fi_uname);
1256 * Only features contained in props will be enabled:
1257 * remove from the nvlist every ZFS_FEATURE_DISABLED
1258 * value and add every missing ZFS_FEATURE_ENABLED if
1259 * enable_all_pool_feat is set.
1261 if (!nvlist_lookup_string(props, propname, &propval)) {
1262 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1263 (void) nvlist_remove_all(props,
1265 } else if (enable_all_pool_feat) {
1266 ret = add_prop_list(propname,
1267 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1274 if (zpool_create(g_zfs, poolname,
1275 nvroot, props, fsprops) == 0) {
1276 zfs_handle_t *pool = zfs_open(g_zfs,
1277 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1279 if (zfs_mount(pool, NULL, 0) == 0)
1280 ret = zfs_shareall(pool);
1283 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1284 (void) fprintf(stderr, gettext("pool name may have "
1290 nvlist_free(nvroot);
1291 nvlist_free(fsprops);
1295 nvlist_free(fsprops);
1302 * zpool destroy <pool>
1304 * -f Forcefully unmount any datasets
1306 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1309 zpool_do_destroy(int argc, char **argv)
1311 boolean_t force = B_FALSE;
1314 zpool_handle_t *zhp;
1318 while ((c = getopt(argc, argv, "f")) != -1) {
1324 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1333 /* check arguments */
1335 (void) fprintf(stderr, gettext("missing pool argument\n"));
1339 (void) fprintf(stderr, gettext("too many arguments\n"));
1345 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1347 * As a special case, check for use of '/' in the name, and
1348 * direct the user to use 'zfs destroy' instead.
1350 if (strchr(pool, '/') != NULL)
1351 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1352 "destroy a dataset\n"));
1356 if (zpool_disable_datasets(zhp, force) != 0) {
1357 (void) fprintf(stderr, gettext("could not destroy '%s': "
1358 "could not unmount datasets\n"), zpool_get_name(zhp));
1363 /* The history must be logged as part of the export */
1364 log_history = B_FALSE;
1366 ret = (zpool_destroy(zhp, history_str) != 0);
1373 typedef struct export_cbdata {
1375 boolean_t hardforce;
1382 zpool_export_one(zpool_handle_t *zhp, void *data)
1384 export_cbdata_t *cb = data;
1386 if (zpool_disable_datasets(zhp, cb->force) != 0)
1389 /* The history must be logged as part of the export */
1390 log_history = B_FALSE;
1392 if (cb->hardforce) {
1393 if (zpool_export_force(zhp, history_str) != 0)
1395 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1403 * zpool export [-f] <pool> ...
1405 * -a Export all pools
1406 * -f Forcefully unmount datasets
1408 * Export the given pools. By default, the command will attempt to cleanly
1409 * unmount any active datasets within the pool. If the '-f' flag is specified,
1410 * then the datasets will be forcefully unmounted.
1413 zpool_do_export(int argc, char **argv)
1416 boolean_t do_all = B_FALSE;
1417 boolean_t force = B_FALSE;
1418 boolean_t hardforce = B_FALSE;
1422 while ((c = getopt(argc, argv, "afF")) != -1) {
1434 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1441 cb.hardforce = hardforce;
1447 (void) fprintf(stderr, gettext("too many arguments\n"));
1451 return (for_each_pool(argc, argv, B_TRUE, NULL,
1452 zpool_export_one, &cb));
1455 /* check arguments */
1457 (void) fprintf(stderr, gettext("missing pool argument\n"));
1461 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1467 * Given a vdev configuration, determine the maximum width needed for the device
1471 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1479 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1480 if (strlen(name) + depth > max)
1481 max = strlen(name) + depth;
1485 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1486 &child, &children) == 0) {
1487 for (c = 0; c < children; c++)
1488 if ((ret = max_width(zhp, child[c], depth + 2,
1489 max, name_flags)) > max)
1493 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1494 &child, &children) == 0) {
1495 for (c = 0; c < children; c++)
1496 if ((ret = max_width(zhp, child[c], depth + 2,
1497 max, name_flags)) > max)
1501 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1502 &child, &children) == 0) {
1503 for (c = 0; c < children; c++)
1504 if ((ret = max_width(zhp, child[c], depth + 2,
1505 max, name_flags)) > max)
1512 typedef struct spare_cbdata {
1514 zpool_handle_t *cb_zhp;
1518 find_vdev(nvlist_t *nv, uint64_t search)
1524 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1528 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1529 &child, &children) == 0) {
1530 for (c = 0; c < children; c++)
1531 if (find_vdev(child[c], search))
1539 find_spare(zpool_handle_t *zhp, void *data)
1541 spare_cbdata_t *cbp = data;
1542 nvlist_t *config, *nvroot;
1544 config = zpool_get_config(zhp, NULL);
1545 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1548 if (find_vdev(nvroot, cbp->cb_guid)) {
1557 typedef struct status_cbdata {
1561 boolean_t cb_allpools;
1562 boolean_t cb_verbose;
1563 boolean_t cb_explain;
1565 boolean_t cb_dedup_stats;
1566 boolean_t cb_print_status;
1567 vdev_cmd_data_list_t *vcdl;
1570 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1572 is_blank_str(char *str)
1574 while (str != NULL && *str != '\0') {
1582 /* Print command output lines for specific vdev in a specific pool */
1584 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1586 vdev_cmd_data_t *data;
1590 for (i = 0; i < vcdl->count; i++) {
1591 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1592 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1593 /* Not the vdev we're looking for */
1597 data = &vcdl->data[i];
1598 /* Print out all the output values for this vdev */
1599 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1601 /* Does this vdev have values for this column? */
1602 for (int k = 0; k < data->cols_cnt; k++) {
1603 if (strcmp(data->cols[k],
1604 vcdl->uniq_cols[j]) == 0) {
1605 /* yes it does, record the value */
1606 val = data->lines[k];
1611 * Mark empty values with dashes to make output
1614 if (is_blank_str(val))
1617 printf("%*s", vcdl->uniq_cols_width[j], val);
1618 if (j < vcdl->uniq_cols_cnt - 1)
1622 /* Print out any values that aren't in a column at the end */
1623 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1624 /* Did we have any columns? If so print a spacer. */
1625 if (vcdl->uniq_cols_cnt > 0)
1628 val = data->lines[j];
1629 printf("%s", val ? val : "");
1636 * Print out configuration state as requested by status_callback.
1639 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1640 nvlist_t *nv, int depth, boolean_t isspare)
1644 pool_scan_stat_t *ps = NULL;
1646 char rbuf[6], wbuf[6], cbuf[6];
1648 uint64_t notpresent;
1649 spare_cbdata_t spare_cb;
1653 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1654 &child, &children) != 0)
1657 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1658 (uint64_t **)&vs, &c) == 0);
1660 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1663 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1666 if (vs->vs_aux == VDEV_AUX_SPARED)
1668 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1672 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1676 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1677 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1678 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1679 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1682 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1683 ¬present) == 0) {
1684 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1685 (void) printf(" was %s", path);
1686 } else if (vs->vs_aux != 0) {
1689 switch (vs->vs_aux) {
1690 case VDEV_AUX_OPEN_FAILED:
1691 (void) printf(gettext("cannot open"));
1694 case VDEV_AUX_BAD_GUID_SUM:
1695 (void) printf(gettext("missing device"));
1698 case VDEV_AUX_NO_REPLICAS:
1699 (void) printf(gettext("insufficient replicas"));
1702 case VDEV_AUX_VERSION_NEWER:
1703 (void) printf(gettext("newer version"));
1706 case VDEV_AUX_UNSUP_FEAT:
1707 (void) printf(gettext("unsupported feature(s)"));
1710 case VDEV_AUX_SPARED:
1711 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1712 &spare_cb.cb_guid) == 0);
1713 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1714 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1715 zpool_get_name(zhp)) == 0)
1716 (void) printf(gettext("currently in "
1719 (void) printf(gettext("in use by "
1721 zpool_get_name(spare_cb.cb_zhp));
1722 zpool_close(spare_cb.cb_zhp);
1724 (void) printf(gettext("currently in use"));
1728 case VDEV_AUX_ERR_EXCEEDED:
1729 (void) printf(gettext("too many errors"));
1732 case VDEV_AUX_IO_FAILURE:
1733 (void) printf(gettext("experienced I/O failures"));
1736 case VDEV_AUX_BAD_LOG:
1737 (void) printf(gettext("bad intent log"));
1740 case VDEV_AUX_EXTERNAL:
1741 (void) printf(gettext("external device fault"));
1744 case VDEV_AUX_SPLIT_POOL:
1745 (void) printf(gettext("split into new pool"));
1748 case VDEV_AUX_ACTIVE:
1749 (void) printf(gettext("currently in use"));
1753 (void) printf(gettext("corrupted data"));
1758 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1759 (uint64_t **)&ps, &c);
1761 if (ps && ps->pss_state == DSS_SCANNING &&
1762 vs->vs_scan_processed != 0 && children == 0) {
1763 (void) printf(gettext(" (%s)"),
1764 (ps->pss_func == POOL_SCAN_RESILVER) ?
1765 "resilvering" : "repairing");
1768 if (cb->vcdl != NULL) {
1769 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1771 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1775 (void) printf("\n");
1777 for (c = 0; c < children; c++) {
1778 uint64_t islog = B_FALSE, ishole = B_FALSE;
1780 /* Don't print logs or holes here */
1781 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1783 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1785 if (islog || ishole)
1787 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1788 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1789 print_status_config(zhp, cb, vname, child[c], depth + 2,
1796 * Print the configuration of an exported pool. Iterate over all vdevs in the
1797 * pool, printing out the name and status for each one.
1800 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1808 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1809 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1810 strcmp(type, VDEV_TYPE_HOLE) == 0)
1813 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1814 (uint64_t **)&vs, &c) == 0);
1816 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1817 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1819 if (vs->vs_aux != 0) {
1822 switch (vs->vs_aux) {
1823 case VDEV_AUX_OPEN_FAILED:
1824 (void) printf(gettext("cannot open"));
1827 case VDEV_AUX_BAD_GUID_SUM:
1828 (void) printf(gettext("missing device"));
1831 case VDEV_AUX_NO_REPLICAS:
1832 (void) printf(gettext("insufficient replicas"));
1835 case VDEV_AUX_VERSION_NEWER:
1836 (void) printf(gettext("newer version"));
1839 case VDEV_AUX_UNSUP_FEAT:
1840 (void) printf(gettext("unsupported feature(s)"));
1843 case VDEV_AUX_ERR_EXCEEDED:
1844 (void) printf(gettext("too many errors"));
1847 case VDEV_AUX_ACTIVE:
1848 (void) printf(gettext("currently in use"));
1852 (void) printf(gettext("corrupted data"));
1856 (void) printf("\n");
1858 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1859 &child, &children) != 0)
1862 for (c = 0; c < children; c++) {
1863 uint64_t is_log = B_FALSE;
1865 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1870 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1871 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1872 print_import_config(cb, vname, child[c], depth + 2);
1876 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1877 &child, &children) == 0) {
1878 (void) printf(gettext("\tcache\n"));
1879 for (c = 0; c < children; c++) {
1880 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1882 (void) printf("\t %s\n", vname);
1887 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1888 &child, &children) == 0) {
1889 (void) printf(gettext("\tspares\n"));
1890 for (c = 0; c < children; c++) {
1891 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1893 (void) printf("\t %s\n", vname);
1901 * Logs are recorded as top level vdevs in the main pool child array
1902 * but with "is_log" set to 1. We use either print_status_config() or
1903 * print_import_config() to print the top level logs then any log
1904 * children (eg mirrored slogs) are printed recursively - which
1905 * works because only the top level vdev is marked "is_log"
1908 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1913 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1917 (void) printf(gettext("\tlogs\n"));
1919 for (c = 0; c < children; c++) {
1920 uint64_t is_log = B_FALSE;
1923 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1927 name = zpool_vdev_name(g_zfs, zhp, child[c],
1928 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1929 if (cb->cb_print_status)
1930 print_status_config(zhp, cb, name, child[c], 2,
1933 print_import_config(cb, name, child[c], 2);
1939 * Display the status for the given pool.
1942 show_import(nvlist_t *config)
1944 uint64_t pool_state;
1948 uint64_t hostid = 0;
1950 char *hostname = "unknown";
1951 nvlist_t *nvroot, *nvinfo;
1952 zpool_status_t reason;
1953 zpool_errata_t errata;
1957 status_cbdata_t cb = { 0 };
1959 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1961 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1963 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1965 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1968 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1969 (uint64_t **)&vs, &vsc) == 0);
1970 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1972 reason = zpool_import_status(config, &msgid, &errata);
1974 (void) printf(gettext(" pool: %s\n"), name);
1975 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1976 (void) printf(gettext(" state: %s"), health);
1977 if (pool_state == POOL_STATE_DESTROYED)
1978 (void) printf(gettext(" (DESTROYED)"));
1979 (void) printf("\n");
1982 case ZPOOL_STATUS_MISSING_DEV_R:
1983 case ZPOOL_STATUS_MISSING_DEV_NR:
1984 case ZPOOL_STATUS_BAD_GUID_SUM:
1985 (void) printf(gettext(" status: One or more devices are "
1986 "missing from the system.\n"));
1989 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1990 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1991 (void) printf(gettext(" status: One or more devices contains "
1992 "corrupted data.\n"));
1995 case ZPOOL_STATUS_CORRUPT_DATA:
1997 gettext(" status: The pool data is corrupted.\n"));
2000 case ZPOOL_STATUS_OFFLINE_DEV:
2001 (void) printf(gettext(" status: One or more devices "
2002 "are offlined.\n"));
2005 case ZPOOL_STATUS_CORRUPT_POOL:
2006 (void) printf(gettext(" status: The pool metadata is "
2010 case ZPOOL_STATUS_VERSION_OLDER:
2011 (void) printf(gettext(" status: The pool is formatted using a "
2012 "legacy on-disk version.\n"));
2015 case ZPOOL_STATUS_VERSION_NEWER:
2016 (void) printf(gettext(" status: The pool is formatted using an "
2017 "incompatible version.\n"));
2020 case ZPOOL_STATUS_FEAT_DISABLED:
2021 (void) printf(gettext(" status: Some supported features are "
2022 "not enabled on the pool.\n"));
2025 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2026 (void) printf(gettext("status: The pool uses the following "
2027 "feature(s) not supported on this system:\n"));
2028 zpool_print_unsup_feat(config);
2031 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2032 (void) printf(gettext("status: The pool can only be accessed "
2033 "in read-only mode on this system. It\n\tcannot be "
2034 "accessed in read-write mode because it uses the "
2035 "following\n\tfeature(s) not supported on this system:\n"));
2036 zpool_print_unsup_feat(config);
2039 case ZPOOL_STATUS_HOSTID_ACTIVE:
2040 (void) printf(gettext(" status: The pool is currently "
2041 "imported by another system.\n"));
2044 case ZPOOL_STATUS_HOSTID_REQUIRED:
2045 (void) printf(gettext(" status: The pool has the "
2046 "multihost property on. It cannot\n\tbe safely imported "
2047 "when the system hostid is not set.\n"));
2050 case ZPOOL_STATUS_HOSTID_MISMATCH:
2051 (void) printf(gettext(" status: The pool was last accessed by "
2052 "another system.\n"));
2055 case ZPOOL_STATUS_FAULTED_DEV_R:
2056 case ZPOOL_STATUS_FAULTED_DEV_NR:
2057 (void) printf(gettext(" status: One or more devices are "
2061 case ZPOOL_STATUS_BAD_LOG:
2062 (void) printf(gettext(" status: An intent log record cannot be "
2066 case ZPOOL_STATUS_RESILVERING:
2067 (void) printf(gettext(" status: One or more devices were being "
2071 case ZPOOL_STATUS_ERRATA:
2072 (void) printf(gettext(" status: Errata #%d detected.\n"),
2078 * No other status can be seen when importing pools.
2080 assert(reason == ZPOOL_STATUS_OK);
2084 * Print out an action according to the overall state of the pool.
2086 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2087 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2088 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2089 (void) printf(gettext(" action: The pool can be "
2090 "imported using its name or numeric identifier, "
2091 "though\n\tsome features will not be available "
2092 "without an explicit 'zpool upgrade'.\n"));
2093 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2094 (void) printf(gettext(" action: The pool can be "
2095 "imported using its name or numeric "
2096 "identifier and\n\tthe '-f' flag.\n"));
2097 } else if (reason == ZPOOL_STATUS_ERRATA) {
2099 case ZPOOL_ERRATA_NONE:
2102 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2103 (void) printf(gettext(" action: The pool can "
2104 "be imported using its name or numeric "
2105 "identifier,\n\thowever there is a compat"
2106 "ibility issue which should be corrected"
2107 "\n\tby running 'zpool scrub'\n"));
2110 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2111 (void) printf(gettext(" action: The pool can"
2112 "not be imported with this version of ZFS "
2113 "due to\n\tan active asynchronous destroy. "
2114 "Revert to an earlier version\n\tand "
2115 "allow the destroy to complete before "
2121 * All errata must contain an action message.
2126 (void) printf(gettext(" action: The pool can be "
2127 "imported using its name or numeric "
2130 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2131 (void) printf(gettext(" action: The pool can be imported "
2132 "despite missing or damaged devices. The\n\tfault "
2133 "tolerance of the pool may be compromised if imported.\n"));
2136 case ZPOOL_STATUS_VERSION_NEWER:
2137 (void) printf(gettext(" action: The pool cannot be "
2138 "imported. Access the pool on a system running "
2139 "newer\n\tsoftware, or recreate the pool from "
2142 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2143 (void) printf(gettext("action: The pool cannot be "
2144 "imported. Access the pool on a system that "
2145 "supports\n\tthe required feature(s), or recreate "
2146 "the pool from backup.\n"));
2148 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2149 (void) printf(gettext("action: The pool cannot be "
2150 "imported in read-write mode. Import the pool "
2152 "\t\"-o readonly=on\", access the pool on a system "
2153 "that supports the\n\trequired feature(s), or "
2154 "recreate the pool from backup.\n"));
2156 case ZPOOL_STATUS_MISSING_DEV_R:
2157 case ZPOOL_STATUS_MISSING_DEV_NR:
2158 case ZPOOL_STATUS_BAD_GUID_SUM:
2159 (void) printf(gettext(" action: The pool cannot be "
2160 "imported. Attach the missing\n\tdevices and try "
2163 case ZPOOL_STATUS_HOSTID_ACTIVE:
2164 VERIFY0(nvlist_lookup_nvlist(config,
2165 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2167 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2168 hostname = fnvlist_lookup_string(nvinfo,
2169 ZPOOL_CONFIG_MMP_HOSTNAME);
2171 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2172 hostid = fnvlist_lookup_uint64(nvinfo,
2173 ZPOOL_CONFIG_MMP_HOSTID);
2175 (void) printf(gettext(" action: The pool must be "
2176 "exported from %s (hostid=%lx)\n\tbefore it "
2177 "can be safely imported.\n"), hostname,
2178 (unsigned long) hostid);
2180 case ZPOOL_STATUS_HOSTID_REQUIRED:
2181 (void) printf(gettext(" action: Set a unique system "
2182 "hostid with the zgenhostid(8) command.\n"));
2185 (void) printf(gettext(" action: The pool cannot be "
2186 "imported due to damaged devices or data.\n"));
2190 /* Print the comment attached to the pool. */
2191 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2192 (void) printf(gettext("comment: %s\n"), comment);
2195 * If the state is "closed" or "can't open", and the aux state
2196 * is "corrupt data":
2198 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2199 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2200 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2201 if (pool_state == POOL_STATE_DESTROYED)
2202 (void) printf(gettext("\tThe pool was destroyed, "
2203 "but can be imported using the '-Df' flags.\n"));
2204 else if (pool_state != POOL_STATE_EXPORTED)
2205 (void) printf(gettext("\tThe pool may be active on "
2206 "another system, but can be imported using\n\t"
2207 "the '-f' flag.\n"));
2211 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2214 (void) printf(gettext(" config:\n\n"));
2216 cb.cb_namewidth = max_width(NULL, nvroot, 0, 0, VDEV_NAME_TYPE_ID);
2217 if (cb.cb_namewidth < 10)
2218 cb.cb_namewidth = 10;
2220 print_import_config(&cb, name, nvroot, 0);
2221 if (num_logs(nvroot) > 0)
2222 print_logs(NULL, &cb, nvroot);
2224 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2225 (void) printf(gettext("\n\tAdditional devices are known to "
2226 "be part of this pool, though their\n\texact "
2227 "configuration cannot be determined.\n"));
2232 zfs_force_import_required(nvlist_t *config)
2235 uint64_t hostid = 0;
2238 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2239 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2241 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2244 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2245 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2246 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2247 ZPOOL_CONFIG_MMP_STATE);
2249 if (mmp_state != MMP_STATE_INACTIVE)
2257 * Perform the import for the given configuration. This passes the heavy
2258 * lifting off to zpool_import_props(), and then mounts the datasets contained
2262 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2263 nvlist_t *props, int flags)
2266 zpool_handle_t *zhp;
2271 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2272 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2273 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2275 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2276 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2277 "is formatted using an unsupported ZFS version\n"), name);
2279 } else if (zfs_force_import_required(config) &&
2280 !(flags & ZFS_IMPORT_ANY_HOST)) {
2281 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2284 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2285 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2286 mmp_state = fnvlist_lookup_uint64(nvinfo,
2287 ZPOOL_CONFIG_MMP_STATE);
2289 if (mmp_state == MMP_STATE_ACTIVE) {
2290 char *hostname = "<unknown>";
2291 uint64_t hostid = 0;
2293 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2294 hostname = fnvlist_lookup_string(nvinfo,
2295 ZPOOL_CONFIG_MMP_HOSTNAME);
2297 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2298 hostid = fnvlist_lookup_uint64(nvinfo,
2299 ZPOOL_CONFIG_MMP_HOSTID);
2301 (void) fprintf(stderr, gettext("cannot import '%s': "
2302 "pool is imported on %s (hostid: "
2303 "0x%lx)\nExport the pool on the other system, "
2304 "then run 'zpool import'.\n"),
2305 name, hostname, (unsigned long) hostid);
2306 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2307 (void) fprintf(stderr, gettext("Cannot import '%s': "
2308 "pool has the multihost property on and the\n"
2309 "system's hostid is not set. Set a unique hostid "
2310 "with the zgenhostid(8) command.\n"), name);
2312 char *hostname = "<unknown>";
2313 uint64_t timestamp = 0;
2314 uint64_t hostid = 0;
2316 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2317 hostname = fnvlist_lookup_string(config,
2318 ZPOOL_CONFIG_HOSTNAME);
2320 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2321 timestamp = fnvlist_lookup_uint64(config,
2322 ZPOOL_CONFIG_TIMESTAMP);
2324 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2325 hostid = fnvlist_lookup_uint64(config,
2326 ZPOOL_CONFIG_HOSTID);
2328 (void) fprintf(stderr, gettext("cannot import '%s': "
2329 "pool was previously in use from another system.\n"
2330 "Last accessed by %s (hostid=%lx) at %s"
2331 "The pool can be imported, use 'zpool import -f' "
2332 "to import the pool.\n"), name, hostname,
2333 (unsigned long)hostid, ctime((time_t *)×tamp));
2339 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2342 if (newname != NULL)
2343 name = (char *)newname;
2345 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2349 * Loading keys is best effort. We don't want to return immediately
2350 * if it fails but we do want to give the error to the caller.
2352 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2353 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2358 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2359 !(flags & ZFS_IMPORT_ONLY) &&
2360 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2370 * zpool import [-d dir] [-D]
2371 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2372 * [-d dir | -c cachefile] [-f] -a
2373 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2374 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2376 * -c Read pool information from a cachefile instead of searching
2379 * -d Scan in a specific directory, other than /dev/. More than
2380 * one directory can be specified using multiple '-d' options.
2382 * -D Scan for previously destroyed pools or import all or only
2383 * specified destroyed pools.
2385 * -R Temporarily import the pool, with all mountpoints relative to
2386 * the given root. The pool will remain exported when the machine
2389 * -V Import even in the presence of faulted vdevs. This is an
2390 * intentionally undocumented option for testing purposes, and
2391 * treats the pool configuration as complete, leaving any bad
2392 * vdevs in the FAULTED state. In other words, it does verbatim
2395 * -f Force import, even if it appears that the pool is active.
2397 * -F Attempt rewind if necessary.
2399 * -n See if rewind would work, but don't actually rewind.
2401 * -N Import the pool but don't mount datasets.
2403 * -T Specify a starting txg to use for import. This option is
2404 * intentionally undocumented option for testing purposes.
2406 * -a Import all pools found.
2408 * -l Load encryption keys while importing.
2410 * -o Set property=value and/or temporary mount options (without '=').
2412 * -s Scan using the default search path, the libblkid cache will
2415 * The import command scans for pools to import, and import pools based on pool
2416 * name and GUID. The pool can also be renamed as part of the import process.
2419 zpool_do_import(int argc, char **argv)
2421 char **searchdirs = NULL;
2422 char *env, *envdup = NULL;
2426 nvlist_t *pools = NULL;
2427 boolean_t do_all = B_FALSE;
2428 boolean_t do_destroyed = B_FALSE;
2429 char *mntopts = NULL;
2432 uint64_t searchguid = 0;
2433 char *searchname = NULL;
2435 nvlist_t *found_config;
2436 nvlist_t *policy = NULL;
2437 nvlist_t *props = NULL;
2439 int flags = ZFS_IMPORT_NORMAL;
2440 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2441 boolean_t dryrun = B_FALSE;
2442 boolean_t do_rewind = B_FALSE;
2443 boolean_t xtreme_rewind = B_FALSE;
2444 boolean_t do_scan = B_FALSE;
2445 uint64_t pool_state, txg = -1ULL;
2446 char *cachefile = NULL;
2447 importargs_t idata = { 0 };
2451 while ((c = getopt(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2460 if (searchdirs == NULL) {
2461 searchdirs = safe_malloc(sizeof (char *));
2463 char **tmp = safe_malloc((nsearch + 1) *
2465 bcopy(searchdirs, tmp, nsearch *
2470 searchdirs[nsearch++] = optarg;
2473 do_destroyed = B_TRUE;
2476 flags |= ZFS_IMPORT_ANY_HOST;
2482 flags |= ZFS_IMPORT_LOAD_KEYS;
2485 flags |= ZFS_IMPORT_MISSING_LOG;
2491 flags |= ZFS_IMPORT_ONLY;
2494 if ((propval = strchr(optarg, '=')) != NULL) {
2497 if (add_prop_list(optarg, propval,
2505 if (add_prop_list(zpool_prop_to_name(
2506 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2508 if (add_prop_list_default(zpool_prop_to_name(
2509 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2516 flags |= ZFS_IMPORT_TEMP_NAME;
2517 if (add_prop_list_default(zpool_prop_to_name(
2518 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2524 txg = strtoull(optarg, &endptr, 0);
2525 if (errno != 0 || *endptr != '\0') {
2526 (void) fprintf(stderr,
2527 gettext("invalid txg value\n"));
2530 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2533 flags |= ZFS_IMPORT_VERBATIM;
2536 xtreme_rewind = B_TRUE;
2539 (void) fprintf(stderr, gettext("missing argument for "
2540 "'%c' option\n"), optopt);
2544 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2553 if (cachefile && nsearch != 0) {
2554 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2558 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2559 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2563 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2564 (void) fprintf(stderr, gettext("-l is only meaningful during "
2569 if ((dryrun || xtreme_rewind) && !do_rewind) {
2570 (void) fprintf(stderr,
2571 gettext("-n or -X only meaningful with -F\n"));
2575 rewind_policy = ZPOOL_TRY_REWIND;
2577 rewind_policy = ZPOOL_DO_REWIND;
2579 rewind_policy |= ZPOOL_EXTREME_REWIND;
2581 /* In the future, we can capture further policy and include it here */
2582 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2583 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2584 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2587 /* check argument count */
2590 (void) fprintf(stderr, gettext("too many arguments\n"));
2595 (void) fprintf(stderr, gettext("too many arguments\n"));
2601 * Check for the effective uid. We do this explicitly here because
2602 * otherwise any attempt to discover pools will silently fail.
2604 if (argc == 0 && geteuid() != 0) {
2605 (void) fprintf(stderr, gettext("cannot "
2606 "discover pools: permission denied\n"));
2607 if (searchdirs != NULL)
2611 nvlist_free(policy);
2616 * Depending on the arguments given, we do one of the following:
2618 * <none> Iterate through all pools and display information about
2621 * -a Iterate through all pools and try to import each one.
2623 * <id> Find the pool that corresponds to the given GUID/pool
2624 * name and import that one.
2626 * -D Above options applies only to destroyed pools.
2632 searchguid = strtoull(argv[0], &endptr, 10);
2633 if (errno != 0 || *endptr != '\0') {
2634 searchname = argv[0];
2637 found_config = NULL;
2640 * User specified a name or guid. Ensure it's unique.
2642 idata.unique = B_TRUE;
2646 * Check the environment for the preferred search path.
2648 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2651 envdup = strdup(env);
2653 dir = strtok(envdup, ":");
2654 while (dir != NULL) {
2655 if (searchdirs == NULL) {
2656 searchdirs = safe_malloc(sizeof (char *));
2658 char **tmp = safe_malloc((nsearch + 1) *
2660 bcopy(searchdirs, tmp, nsearch *
2665 searchdirs[nsearch++] = dir;
2666 dir = strtok(NULL, ":");
2670 idata.path = searchdirs;
2671 idata.paths = nsearch;
2672 idata.poolname = searchname;
2673 idata.guid = searchguid;
2674 idata.cachefile = cachefile;
2675 idata.scan = do_scan;
2677 pools = zpool_search_import(g_zfs, &idata);
2679 if (pools != NULL && idata.exists &&
2680 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2681 (void) fprintf(stderr, gettext("cannot import '%s': "
2682 "a pool with that name already exists\n"),
2684 (void) fprintf(stderr, gettext("use the form '%s "
2685 "<pool | id> <newpool>' to give it a new name\n"),
2688 } else if (pools == NULL && idata.exists) {
2689 (void) fprintf(stderr, gettext("cannot import '%s': "
2690 "a pool with that name is already created/imported,\n"),
2692 (void) fprintf(stderr, gettext("and no additional pools "
2693 "with that name were found\n"));
2695 } else if (pools == NULL) {
2697 (void) fprintf(stderr, gettext("cannot import '%s': "
2698 "no such pool available\n"), argv[0]);
2704 if (searchdirs != NULL)
2708 nvlist_free(policy);
2715 * At this point we have a list of import candidate configs. Even if
2716 * we were searching by pool name or guid, we still need to
2717 * post-process the list to deal with pool state and possible
2723 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2725 verify(nvpair_value_nvlist(elem, &config) == 0);
2727 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2729 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2731 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2734 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2741 (void) printf("\n");
2744 err |= do_import(config, NULL, mntopts,
2747 show_import(config);
2749 } else if (searchname != NULL) {
2753 * We are searching for a pool based on name.
2755 verify(nvlist_lookup_string(config,
2756 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2758 if (strcmp(name, searchname) == 0) {
2759 if (found_config != NULL) {
2760 (void) fprintf(stderr, gettext(
2761 "cannot import '%s': more than "
2762 "one matching pool\n"), searchname);
2763 (void) fprintf(stderr, gettext(
2764 "import by numeric ID instead\n"));
2767 found_config = config;
2773 * Search for a pool by guid.
2775 verify(nvlist_lookup_uint64(config,
2776 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2778 if (guid == searchguid)
2779 found_config = config;
2784 * If we were searching for a specific pool, verify that we found a
2785 * pool, and then do the import.
2787 if (argc != 0 && err == 0) {
2788 if (found_config == NULL) {
2789 (void) fprintf(stderr, gettext("cannot import '%s': "
2790 "no such pool available\n"), argv[0]);
2793 err |= do_import(found_config, argc == 1 ? NULL :
2794 argv[1], mntopts, props, flags);
2799 * If we were just looking for pools, report an error if none were
2802 if (argc == 0 && first)
2803 (void) fprintf(stderr,
2804 gettext("no pools available to import\n"));
2809 nvlist_free(policy);
2810 if (searchdirs != NULL)
2815 return (err ? 1 : 0);
2819 * zpool sync [-f] [pool] ...
2821 * -f (undocumented) force uberblock (and config including zpool cache file)
2824 * Sync the specified pool(s).
2825 * Without arguments "zpool sync" will sync all pools.
2826 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2830 zpool_do_sync(int argc, char **argv)
2833 boolean_t force = B_FALSE;
2836 while ((ret = getopt(argc, argv, "f")) != -1) {
2842 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2851 /* if argc == 0 we will execute zpool_sync_one on all pools */
2852 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2857 typedef struct iostat_cbdata {
2862 char **cb_vdev_names; /* Only show these vdevs */
2863 unsigned int cb_vdev_names_count;
2864 boolean_t cb_verbose;
2865 boolean_t cb_literal;
2866 boolean_t cb_scripted;
2867 zpool_list_t *cb_list;
2868 vdev_cmd_data_list_t *vcdl;
2872 typedef struct name_and_columns {
2873 const char *name; /* Column name */
2874 unsigned int columns; /* Center name to this number of columns */
2875 } name_and_columns_t;
2877 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2879 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2881 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2883 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2884 {"asyncq_wait", 2}, {"scrub"}},
2885 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2886 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2888 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2889 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2890 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2891 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2895 /* Shorthand - if "columns" field not set, default to 1 column */
2896 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2898 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2900 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2901 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2902 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2903 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2904 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2905 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2906 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2907 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2910 static const char *histo_to_title[] = {
2911 [IOS_L_HISTO] = "latency",
2912 [IOS_RQ_HISTO] = "req_size",
2916 * Return the number of labels in a null-terminated name_and_columns_t
2921 label_array_len(const name_and_columns_t *labels)
2925 while (labels[i].name)
2932 * Return the number of strings in a null-terminated string array.
2935 * const char foo[] = {"bar", "baz", NULL}
2940 str_array_len(const char *array[])
2951 * Return a default column width for default/latency/queue columns. This does
2952 * not include histograms, which have their columns autosized.
2955 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2957 unsigned long column_width = 5; /* Normal niceprint */
2958 static unsigned long widths[] = {
2960 * Choose some sane default column sizes for printing the
2963 [IOS_DEFAULT] = 15, /* 1PB capacity */
2964 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2965 [IOS_QUEUES] = 6, /* 1M queue entries */
2969 column_width = widths[type];
2971 return (column_width);
2975 * Print the column labels, i.e:
2977 * capacity operations bandwidth
2978 * alloc free read write read write ...
2980 * If force_column_width is set, use it for the column width. If not set, use
2981 * the default column width.
2984 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2985 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2988 unsigned int text_start, rw_column_width, spaces_to_end;
2989 uint64_t flags = cb->cb_flags;
2991 unsigned int column_width = force_column_width;
2993 /* For each bit set in flags */
2994 for (f = flags; f; f &= ~(1ULL << idx)) {
2995 idx = lowbit64(f) - 1;
2996 if (!force_column_width)
2997 column_width = default_column_width(cb, idx);
2998 /* Print our top labels centered over "read write" label. */
2999 for (i = 0; i < label_array_len(labels[idx]); i++) {
3000 const char *name = labels[idx][i].name;
3002 * We treat labels[][].columns == 0 as shorthand
3003 * for one column. It makes writing out the label
3004 * tables more concise.
3006 unsigned int columns = MAX(1, labels[idx][i].columns);
3007 unsigned int slen = strlen(name);
3009 rw_column_width = (column_width * columns) +
3010 (2 * (columns - 1));
3012 text_start = (int)((rw_column_width)/columns -
3015 printf(" "); /* Two spaces between columns */
3017 /* Space from beginning of column to label */
3018 for (s = 0; s < text_start; s++)
3023 /* Print space after label to end of column */
3024 spaces_to_end = rw_column_width - text_start - slen;
3025 for (s = 0; s < spaces_to_end; s++)
3034 * print_cmd_columns - Print custom column titles from -c
3036 * If the user specified the "zpool status|iostat -c" then print their custom
3037 * column titles in the header. For example, print_cmd_columns() would print
3038 * the " col1 col2" part of this:
3040 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3042 * capacity operations bandwidth
3043 * pool alloc free read write read write col1 col2
3044 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3045 * mypool 269K 1008M 0 0 107 946
3046 * mirror 269K 1008M 0 0 107 946
3047 * sdb - - 0 0 102 473 val1 val2
3048 * sdc - - 0 0 5 473 val1 val2
3049 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3052 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3055 vdev_cmd_data_t *data = &vcdl->data[0];
3057 if (vcdl->count == 0 || data == NULL)
3061 * Each vdev cmd should have the same column names unless the user did
3062 * something weird with their cmd. Just take the column names from the
3063 * first vdev and assume it works for all of them.
3065 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3068 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3071 printf("%*s", vcdl->uniq_cols_width[i],
3072 vcdl->uniq_cols[i]);
3079 * Utility function to print out a line of dashes like:
3081 * -------------------------------- ----- ----- ----- ----- -----
3083 * ...or a dashed named-row line like:
3089 * @force_column_width If non-zero, use the value as the column width.
3090 * Otherwise use the default column widths.
3092 * @name: Print a dashed named-row line starting
3093 * with @name. Otherwise, print a regular
3097 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3101 unsigned int namewidth;
3102 uint64_t flags = cb->cb_flags;
3105 const name_and_columns_t *labels;
3109 if (cb->cb_flags & IOS_ANYHISTO_M) {
3110 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3111 } else if (cb->cb_vdev_names_count) {
3117 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3118 name ? strlen(name) : 0);
3122 printf("%-*s", namewidth, name);
3124 for (i = 0; i < namewidth; i++)
3128 /* For each bit in flags */
3129 for (f = flags; f; f &= ~(1ULL << idx)) {
3130 unsigned int column_width;
3131 idx = lowbit64(f) - 1;
3132 if (force_column_width)
3133 column_width = force_column_width;
3135 column_width = default_column_width(cb, idx);
3137 labels = iostat_bottom_labels[idx];
3138 for (i = 0; i < label_array_len(labels); i++) {
3140 printf(" %*s-", column_width - 1, " ");
3142 printf(" %.*s", column_width,
3143 "--------------------");
3150 print_iostat_separator_impl(iostat_cbdata_t *cb,
3151 unsigned int force_column_width)
3153 print_iostat_dashes(cb, force_column_width, NULL);
3157 print_iostat_separator(iostat_cbdata_t *cb)
3159 print_iostat_separator_impl(cb, 0);
3163 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3164 const char *histo_vdev_name)
3166 unsigned int namewidth;
3169 if (cb->cb_flags & IOS_ANYHISTO_M) {
3170 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3171 } else if (cb->cb_vdev_names_count) {
3177 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3178 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3180 if (histo_vdev_name)
3181 printf("%-*s", namewidth, histo_vdev_name);
3183 printf("%*s", namewidth, "");
3186 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3189 printf("%-*s", namewidth, title);
3191 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3192 if (cb->vcdl != NULL)
3193 print_cmd_columns(cb->vcdl, 0);
3197 print_iostat_separator_impl(cb, force_column_width);
3199 if (cb->vcdl != NULL)
3200 print_cmd_columns(cb->vcdl, 1);
3206 print_iostat_header(iostat_cbdata_t *cb)
3208 print_iostat_header_impl(cb, 0, NULL);
3213 * Display a single statistic.
3216 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3217 unsigned int column_size, boolean_t scripted)
3221 zfs_nicenum_format(value, buf, sizeof (buf), format);
3224 printf("\t%s", buf);
3226 printf(" %*s", column_size, buf);
3230 * Calculate the default vdev stats
3232 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3233 * stats into calcvs.
3236 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3237 vdev_stat_t *calcvs)
3241 memcpy(calcvs, newvs, sizeof (*calcvs));
3242 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3243 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3245 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3246 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3250 * Internal representation of the extended iostats data.
3252 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3253 * or single uint64_t's. We make both look like arrays to make them easier
3254 * to process. In order to make single uint64_t's look like arrays, we set
3255 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3256 * we can just use *data and count.
3260 uint_t count; /* Number of entries in data[] */
3261 uint64_t __data; /* Only used when data is a single uint64_t */
3265 stat_histo_max(struct stat_array *nva, unsigned int len)
3269 for (i = 0; i < len; i++)
3270 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3276 * Helper function to lookup a uint64_t array or uint64_t value and store its
3277 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3278 * it look like a one element array to make it easier to process.
3281 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3282 struct stat_array *nva)
3287 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3288 switch (nvpair_type(tmp)) {
3289 case DATA_TYPE_UINT64_ARRAY:
3290 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3292 case DATA_TYPE_UINT64:
3293 ret = nvpair_value_uint64(tmp, &nva->__data);
3294 nva->data = &nva->__data;
3298 /* Not a uint64_t */
3307 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3308 * subtract them, and return the results in a newly allocated stat_array.
3309 * You must free the returned array after you are done with it with
3310 * free_calc_stats().
3312 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3315 static struct stat_array *
3316 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3319 nvlist_t *oldnvx = NULL, *newnvx;
3320 struct stat_array *oldnva, *newnva, *calcnva;
3322 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3324 /* Extract our extended stats nvlist from the main list */
3325 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3328 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3332 newnva = safe_malloc(alloc_size);
3333 oldnva = safe_malloc(alloc_size);
3334 calcnva = safe_malloc(alloc_size);
3336 for (j = 0; j < len; j++) {
3337 verify(nvpair64_to_stat_array(newnvx, names[j],
3339 calcnva[j].count = newnva[j].count;
3340 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3341 calcnva[j].data = safe_malloc(alloc_size);
3342 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3345 verify(nvpair64_to_stat_array(oldnvx, names[j],
3347 for (i = 0; i < oldnva[j].count; i++)
3348 calcnva[j].data[i] -= oldnva[j].data[i];
3357 free_calc_stats(struct stat_array *nva, unsigned int len)
3360 for (i = 0; i < len; i++)
3367 print_iostat_histo(struct stat_array *nva, unsigned int len,
3368 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3374 enum zfs_nicenum_format format;
3375 unsigned int buckets;
3376 unsigned int start_bucket;
3379 format = ZFS_NICENUM_RAW;
3381 format = ZFS_NICENUM_1024;
3383 /* All these histos are the same size, so just use nva[0].count */
3384 buckets = nva[0].count;
3386 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3387 /* Start at 512 - req size should never be lower than this */
3393 for (j = start_bucket; j < buckets; j++) {
3394 /* Print histogram bucket label */
3395 if (cb->cb_flags & IOS_L_HISTO_M) {
3396 /* Ending range of this bucket */
3397 val = (1UL << (j + 1)) - 1;
3398 zfs_nicetime(val, buf, sizeof (buf));
3400 /* Request size (starting range of bucket) */
3402 zfs_nicenum(val, buf, sizeof (buf));
3405 if (cb->cb_scripted)
3406 printf("%llu", (u_longlong_t)val);
3408 printf("%-*s", namewidth, buf);
3410 /* Print the values on the line */
3411 for (i = 0; i < len; i++) {
3412 print_one_stat(nva[i].data[j] * scale, format,
3413 column_width, cb->cb_scripted);
3420 print_solid_separator(unsigned int length)
3428 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3429 nvlist_t *newnv, double scale, const char *name)
3431 unsigned int column_width;
3432 unsigned int namewidth;
3433 unsigned int entire_width;
3434 enum iostat_type type;
3435 struct stat_array *nva;
3437 unsigned int names_len;
3439 /* What type of histo are we? */
3440 type = IOS_HISTO_IDX(cb->cb_flags);
3442 /* Get NULL-terminated array of nvlist names for our histo */
3443 names = vsx_type_to_nvlist[type];
3444 names_len = str_array_len(names); /* num of names */
3446 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3448 if (cb->cb_literal) {
3449 column_width = MAX(5,
3450 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3455 namewidth = MAX(cb->cb_namewidth,
3456 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3459 * Calculate the entire line width of what we're printing. The
3460 * +2 is for the two spaces between columns:
3464 /* |___| <---------- column_width */
3466 /* |__________| <--- entire_width */
3468 entire_width = namewidth + (column_width + 2) *
3469 label_array_len(iostat_bottom_labels[type]);
3471 if (cb->cb_scripted)
3472 printf("%s\n", name);
3474 print_iostat_header_impl(cb, column_width, name);
3476 print_iostat_histo(nva, names_len, cb, column_width,
3479 free_calc_stats(nva, names_len);
3480 if (!cb->cb_scripted)
3481 print_solid_separator(entire_width);
3485 * Calculate the average latency of a power-of-two latency histogram
3488 single_histo_average(uint64_t *histo, unsigned int buckets)
3491 uint64_t count = 0, total = 0;
3493 for (i = 0; i < buckets; i++) {
3495 * Our buckets are power-of-two latency ranges. Use the
3496 * midpoint latency of each bucket to calculate the average.
3504 if (histo[i] != 0) {
3505 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3510 /* Prevent divide by zero */
3511 return (count == 0 ? 0 : total / count);
3515 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3516 nvlist_t *newnv, double scale)
3520 const char *names[] = {
3521 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3522 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3523 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3524 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3525 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3526 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3527 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3528 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3529 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3530 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3533 struct stat_array *nva;
3535 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3536 enum zfs_nicenum_format format;
3538 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3541 format = ZFS_NICENUM_RAW;
3543 format = ZFS_NICENUM_1024;
3545 for (i = 0; i < ARRAY_SIZE(names); i++) {
3546 val = nva[i].data[0] * scale;
3547 print_one_stat(val, format, column_width, cb->cb_scripted);
3550 free_calc_stats(nva, ARRAY_SIZE(names));
3554 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3555 nvlist_t *newnv, double scale)
3559 const char *names[] = {
3560 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3561 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3562 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3563 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3564 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3565 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3566 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3567 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3568 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3570 struct stat_array *nva;
3572 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3573 enum zfs_nicenum_format format;
3575 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3578 format = ZFS_NICENUM_RAWTIME;
3580 format = ZFS_NICENUM_TIME;
3582 /* Print our avg latencies on the line */
3583 for (i = 0; i < ARRAY_SIZE(names); i++) {
3584 /* Compute average latency for a latency histo */
3585 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3586 print_one_stat(val, format, column_width, cb->cb_scripted);
3588 free_calc_stats(nva, ARRAY_SIZE(names));
3592 * Print default statistics (capacity/operations/bandwidth)
3595 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3597 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3598 enum zfs_nicenum_format format;
3599 char na; /* char to print for "not applicable" values */
3601 if (cb->cb_literal) {
3602 format = ZFS_NICENUM_RAW;
3605 format = ZFS_NICENUM_1024;
3609 /* only toplevel vdevs have capacity stats */
3610 if (vs->vs_space == 0) {
3611 if (cb->cb_scripted)
3612 printf("\t%c\t%c", na, na);
3614 printf(" %*c %*c", column_width, na, column_width,
3617 print_one_stat(vs->vs_alloc, format, column_width,
3619 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3620 column_width, cb->cb_scripted);
3623 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3624 format, column_width, cb->cb_scripted);
3625 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3626 format, column_width, cb->cb_scripted);
3627 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3628 format, column_width, cb->cb_scripted);
3629 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3630 format, column_width, cb->cb_scripted);
3634 * Print out all the statistics for the given vdev. This can either be the
3635 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3636 * is a verbose output, and we don't want to display the toplevel pool stats.
3638 * Returns the number of stat lines printed.
3641 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3642 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3644 nvlist_t **oldchild, **newchild;
3646 vdev_stat_t *oldvs, *newvs, *calcvs;
3647 vdev_stat_t zerovs = { 0 };
3654 calcvs = safe_malloc(sizeof (*calcvs));
3656 if (oldnv != NULL) {
3657 verify(nvlist_lookup_uint64_array(oldnv,
3658 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3663 /* Do we only want to see a specific vdev? */
3664 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3665 /* Yes we do. Is this the vdev? */
3666 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3668 * This is our vdev. Since it is the only vdev we
3669 * will be displaying, make depth = 0 so that it
3670 * doesn't get indented.
3677 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3678 /* Couldn't match the name */
3683 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3684 (uint64_t **)&newvs, &c) == 0);
3687 * Print the vdev name unless it's is a histogram. Histograms
3688 * display the vdev name in the header itself.
3690 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3691 if (cb->cb_scripted) {
3694 if (strlen(name) + depth > cb->cb_namewidth)
3695 (void) printf("%*s%s", depth, "", name);
3697 (void) printf("%*s%s%*s", depth, "", name,
3698 (int)(cb->cb_namewidth - strlen(name) -
3703 /* Calculate our scaling factor */
3704 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3705 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3707 * If we specify printing histograms with no time interval, then
3708 * print the histogram numbers over the entire lifetime of the
3716 scale = (double)NANOSEC / tdelta;
3719 if (cb->cb_flags & IOS_DEFAULT_M) {
3720 calc_default_iostats(oldvs, newvs, calcvs);
3721 print_iostat_default(calcvs, cb, scale);
3723 if (cb->cb_flags & IOS_LATENCY_M)
3724 print_iostat_latency(cb, oldnv, newnv, scale);
3725 if (cb->cb_flags & IOS_QUEUES_M)
3726 print_iostat_queues(cb, oldnv, newnv, scale);
3727 if (cb->cb_flags & IOS_ANYHISTO_M) {
3729 print_iostat_histos(cb, oldnv, newnv, scale, name);
3732 if (cb->vcdl != NULL) {
3734 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3737 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3741 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3750 if (!cb->cb_verbose)
3753 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3754 &newchild, &children) != 0)
3757 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3758 &oldchild, &c) != 0)
3761 for (c = 0; c < children; c++) {
3762 uint64_t ishole = B_FALSE, islog = B_FALSE;
3764 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3767 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3770 if (ishole || islog)
3773 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3775 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3776 newchild[c], cb, depth + 2);
3781 * Log device section
3784 if (num_logs(newnv) > 0) {
3785 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3786 !cb->cb_vdev_names) {
3787 print_iostat_dashes(cb, 0, "logs");
3791 for (c = 0; c < children; c++) {
3792 uint64_t islog = B_FALSE;
3793 (void) nvlist_lookup_uint64(newchild[c],
3794 ZPOOL_CONFIG_IS_LOG, &islog);
3797 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3799 ret += print_vdev_stats(zhp, vname, oldnv ?
3800 oldchild[c] : NULL, newchild[c],
3809 * Include level 2 ARC devices in iostat output
3811 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3812 &newchild, &children) != 0)
3815 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3816 &oldchild, &c) != 0)
3820 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3821 !cb->cb_vdev_names) {
3822 print_iostat_dashes(cb, 0, "cache");
3826 for (c = 0; c < children; c++) {
3827 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3829 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3830 : NULL, newchild[c], cb, depth + 2);
3839 refresh_iostat(zpool_handle_t *zhp, void *data)
3841 iostat_cbdata_t *cb = data;
3845 * If the pool has disappeared, remove it from the list and continue.
3847 if (zpool_refresh_stats(zhp, &missing) != 0)
3851 pool_list_remove(cb->cb_list, zhp);
3857 * Callback to print out the iostats for the given pool.
3860 print_iostat(zpool_handle_t *zhp, void *data)
3862 iostat_cbdata_t *cb = data;
3863 nvlist_t *oldconfig, *newconfig;
3864 nvlist_t *oldnvroot, *newnvroot;
3867 newconfig = zpool_get_config(zhp, &oldconfig);
3869 if (cb->cb_iteration == 1)
3872 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3875 if (oldconfig == NULL)
3878 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3881 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3883 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3884 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3885 print_iostat_separator(cb);
3886 if (cb->vcdl != NULL) {
3887 print_cmd_columns(cb->vcdl, 1);
3902 if (isatty(STDOUT_FILENO)) {
3903 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3905 columns = ws.ws_col;
3914 get_namewidth(zpool_handle_t *zhp, void *data)
3916 iostat_cbdata_t *cb = data;
3917 nvlist_t *config, *nvroot;
3920 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3921 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3923 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3924 if (!cb->cb_verbose)
3925 cb->cb_namewidth = poolname_len;
3927 cb->cb_namewidth = MAX(poolname_len,
3928 max_width(zhp, nvroot, 0, cb->cb_namewidth,
3929 cb->cb_name_flags));
3932 * The width must be at least 10, but may be as large as the
3933 * column width - 42 so that we can still fit in one line.
3935 columns = get_columns();
3937 if (cb->cb_namewidth < 10)
3938 cb->cb_namewidth = 10;
3939 if (cb->cb_namewidth > columns - 42)
3940 cb->cb_namewidth = columns - 42;
3946 * Parse the input string, get the 'interval' and 'count' value if there is one.
3949 get_interval_count(int *argcp, char **argv, float *iv,
3953 unsigned long count = 0;
3957 * Determine if the last argument is an integer or a pool name
3959 if (argc > 0 && isnumber(argv[argc - 1])) {
3963 interval = strtof(argv[argc - 1], &end);
3965 if (*end == '\0' && errno == 0) {
3966 if (interval == 0) {
3967 (void) fprintf(stderr, gettext("interval "
3968 "cannot be zero\n"));
3972 * Ignore the last parameter
3977 * If this is not a valid number, just plow on. The
3978 * user will get a more informative error message later
3986 * If the last argument is also an integer, then we have both a count
3989 if (argc > 0 && isnumber(argv[argc - 1])) {
3994 interval = strtof(argv[argc - 1], &end);
3996 if (*end == '\0' && errno == 0) {
3997 if (interval == 0) {
3998 (void) fprintf(stderr, gettext("interval "
3999 "cannot be zero\n"));
4004 * Ignore the last parameter
4018 get_timestamp_arg(char c)
4021 timestamp_fmt = UDATE;
4023 timestamp_fmt = DDATE;
4029 * Return stat flags that are supported by all pools by both the module and
4030 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4031 * It will get ANDed down until only the flags that are supported on all pools
4035 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4037 uint64_t *mask = data;
4038 nvlist_t *config, *nvroot, *nvx;
4042 config = zpool_get_config(zhp, NULL);
4043 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4046 /* Default stats are always supported, but for completeness.. */
4047 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4048 flags |= IOS_DEFAULT_M;
4050 /* Get our extended stats nvlist from the main list */
4051 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4054 * No extended stats; they're probably running an older
4055 * module. No big deal, we support that too.
4060 /* For each extended stat, make sure all its nvpairs are supported */
4061 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4062 if (!vsx_type_to_nvlist[j][0])
4065 /* Start off by assuming the flag is supported, then check */
4066 flags |= (1ULL << j);
4067 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4068 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4069 /* flag isn't supported */
4070 flags = flags & ~(1ULL << j);
4076 *mask = *mask & flags;
4081 * Return a bitmask of stats that are supported on all pools by both the module
4085 get_stat_flags(zpool_list_t *list)
4090 * get_stat_flags_cb() will lop off bits from "mask" until only the
4091 * flags that are supported on all pools remain.
4093 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4098 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4101 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4103 iostat_cbdata_t *cb = cb_data;
4107 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4109 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4110 ret = 1; /* match */
4117 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4120 is_vdev(zpool_handle_t *zhp, void *cb_data)
4122 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4126 * Check if vdevs are in a pool
4128 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4129 * return 0. If pool_name is NULL, then search all pools.
4132 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4133 iostat_cbdata_t *cb)
4140 if ((argc == 0) || !*argv)
4146 /* Temporarily hijack cb_vdev_names for a second... */
4147 tmp_name = cb->cb_vdev_names;
4149 /* Go though our list of prospective vdev names */
4150 for (i = 0; i < argc; i++) {
4151 cb->cb_vdev_names = argv + i;
4153 /* Is this name a vdev in our pools? */
4154 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4162 cb->cb_vdev_names = tmp_name;
4168 is_pool_cb(zpool_handle_t *zhp, void *data)
4171 if (strcmp(name, zpool_get_name(zhp)) == 0)
4178 * Do we have a pool named *name? If so, return 1, otherwise 0.
4183 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4186 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4188 are_all_pools(int argc, char **argv)
4190 if ((argc == 0) || !*argv)
4194 if (!is_pool(argv[argc]))
4201 * Helper function to print out vdev/pool names we can't resolve. Used for an
4205 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4206 iostat_cbdata_t *cb)
4211 for (i = 0; i < argc; i++) {
4215 str = gettext("pool");
4216 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4217 str = gettext("vdev in this pool");
4218 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4219 str = gettext("vdev in another pool");
4221 str = gettext("unknown");
4223 fprintf(stderr, "\t%s (%s)\n", name, str);
4228 * Same as get_interval_count(), but with additional checks to not misinterpret
4229 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4233 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4234 unsigned long *count, iostat_cbdata_t *cb)
4236 char **tmpargv = argv;
4237 int argc_for_interval = 0;
4239 /* Is the last arg an interval value? Or a guid? */
4240 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4242 * The last arg is not a guid, so it's probably an
4245 argc_for_interval++;
4248 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4250 * The 2nd to last arg is not a guid, so it's probably
4251 * an interval value.
4253 argc_for_interval++;
4257 /* Point to our list of possible intervals */
4258 tmpargv = &argv[*argc - argc_for_interval];
4260 *argc = *argc - argc_for_interval;
4261 get_interval_count(&argc_for_interval, tmpargv,
4266 * Floating point sleep(). Allows you to pass in a floating point value for
4272 struct timespec req;
4273 req.tv_sec = floor(sec);
4274 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4275 nanosleep(&req, NULL);
4279 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4282 * name: Short name of the script ('iostat').
4283 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4286 print_zpool_script_help(char *name, char *path)
4288 char *argv[] = {path, "-h", NULL};
4289 char **lines = NULL;
4293 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4295 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4297 libzfs_free_str_array(lines, lines_cnt);
4301 for (int i = 0; i < lines_cnt; i++)
4302 if (!is_blank_str(lines[i]))
4303 printf(" %-14s %s\n", name, lines[i]);
4305 libzfs_free_str_array(lines, lines_cnt);
4309 * Go though the zpool status/iostat -c scripts in the user's path, run their
4310 * help option (-h), and print out the results.
4313 print_zpool_dir_scripts(char *dirpath)
4317 char fullpath[MAXPATHLEN];
4318 struct stat dir_stat;
4320 if ((dir = opendir(dirpath)) != NULL) {
4321 /* print all the files and directories within directory */
4322 while ((ent = readdir(dir)) != NULL) {
4323 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4325 /* Print the scripts */
4326 if (stat(fullpath, &dir_stat) == 0)
4327 if (dir_stat.st_mode & S_IXUSR &&
4328 S_ISREG(dir_stat.st_mode))
4329 print_zpool_script_help(ent->d_name,
4337 * Print out help text for all zpool status/iostat -c scripts.
4340 print_zpool_script_list(char *subcommand)
4344 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4346 sp = zpool_get_cmd_search_path();
4350 dir = strtok(sp, ":");
4351 while (dir != NULL) {
4352 print_zpool_dir_scripts(dir);
4353 dir = strtok(NULL, ":");
4360 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4361 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4362 * [interval [count]]
4364 * -c CMD For each vdev, run command CMD
4365 * -g Display guid for individual vdev name.
4366 * -L Follow links when resolving vdev path name.
4367 * -P Display full path for vdev name.
4368 * -v Display statistics for individual vdevs
4370 * -p Display values in parsable (exact) format.
4371 * -H Scripted mode. Don't display headers, and separate properties
4373 * -l Display average latency
4374 * -q Display queue depths
4375 * -w Display latency histograms
4376 * -r Display request size histogram
4377 * -T Display a timestamp in date(1) or Unix format
4379 * This command can be tricky because we want to be able to deal with pool
4380 * creation/destruction as well as vdev configuration changes. The bulk of this
4381 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4382 * on pool_list_update() to detect the addition of new pools. Configuration
4383 * changes are all handled within libzfs.
4386 zpool_do_iostat(int argc, char **argv)
4392 unsigned long count = 0;
4394 boolean_t verbose = B_FALSE;
4395 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4396 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4397 boolean_t omit_since_boot = B_FALSE;
4398 boolean_t guid = B_FALSE;
4399 boolean_t follow_links = B_FALSE;
4400 boolean_t full_name = B_FALSE;
4401 iostat_cbdata_t cb = { 0 };
4404 /* Used for printing error message */
4405 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4406 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4408 uint64_t unsupported_flags;
4411 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4416 gettext("Can't set -c flag twice\n"));
4420 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4421 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4422 fprintf(stderr, gettext(
4423 "Can't run -c, disabled by "
4424 "ZPOOL_SCRIPTS_ENABLED.\n"));
4428 if ((getuid() <= 0 || geteuid() <= 0) &&
4429 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4430 fprintf(stderr, gettext(
4431 "Can't run -c with root privileges "
4432 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4442 follow_links = B_TRUE;
4448 get_timestamp_arg(*optarg);
4472 omit_since_boot = B_TRUE;
4478 if (optopt == 'c') {
4479 print_zpool_script_list("iostat");
4483 gettext("invalid option '%c'\n"), optopt);
4492 cb.cb_literal = parsable;
4493 cb.cb_scripted = scripted;
4496 cb.cb_name_flags |= VDEV_NAME_GUID;
4498 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4500 cb.cb_name_flags |= VDEV_NAME_PATH;
4501 cb.cb_iteration = 0;
4502 cb.cb_namewidth = 0;
4503 cb.cb_verbose = verbose;
4505 /* Get our interval and count values (if any) */
4507 get_interval_count_filter_guids(&argc, argv, &interval,
4510 get_interval_count(&argc, argv, &interval, &count);
4514 /* No args, so just print the defaults. */
4515 } else if (are_all_pools(argc, argv)) {
4516 /* All the args are pool names */
4517 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4518 /* All the args are vdevs */
4519 cb.cb_vdev_names = argv;
4520 cb.cb_vdev_names_count = argc;
4521 argc = 0; /* No pools to process */
4522 } else if (are_all_pools(1, argv)) {
4523 /* The first arg is a pool name */
4524 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4525 /* ...and the rest are vdev names */
4526 cb.cb_vdev_names = argv + 1;
4527 cb.cb_vdev_names_count = argc - 1;
4528 argc = 1; /* One pool to process */
4530 fprintf(stderr, gettext("Expected either a list of "));
4531 fprintf(stderr, gettext("pools, or list of vdevs in"));
4532 fprintf(stderr, " \"%s\", ", argv[0]);
4533 fprintf(stderr, gettext("but got:\n"));
4534 error_list_unresolved_vdevs(argc - 1, argv + 1,
4536 fprintf(stderr, "\n");
4542 * The args don't make sense. The first arg isn't a pool name,
4543 * nor are all the args vdevs.
4545 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4546 fprintf(stderr, "\n");
4550 if (cb.cb_vdev_names_count != 0) {
4552 * If user specified vdevs, it implies verbose.
4554 cb.cb_verbose = B_TRUE;
4558 * Construct the list of all interesting pools.
4561 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4564 if (pool_list_count(list) == 0 && argc != 0) {
4565 pool_list_free(list);
4569 if (pool_list_count(list) == 0 && interval == 0) {
4570 pool_list_free(list);
4571 (void) fprintf(stderr, gettext("no pools available\n"));
4575 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4576 pool_list_free(list);
4577 (void) fprintf(stderr,
4578 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4583 if (l_histo && rq_histo) {
4584 pool_list_free(list);
4585 (void) fprintf(stderr,
4586 gettext("Only one of [-r|-w] can be passed at a time\n"));
4592 * Enter the main iostat loop.
4598 * Histograms tables look out of place when you try to display
4599 * them with the other stats, so make a rule that you can only
4600 * print histograms by themselves.
4602 cb.cb_flags = IOS_L_HISTO_M;
4603 } else if (rq_histo) {
4604 cb.cb_flags = IOS_RQ_HISTO_M;
4606 cb.cb_flags = IOS_DEFAULT_M;
4608 cb.cb_flags |= IOS_LATENCY_M;
4610 cb.cb_flags |= IOS_QUEUES_M;
4614 * See if the module supports all the stats we want to display.
4616 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4617 if (unsupported_flags) {
4621 gettext("The loaded zfs module doesn't support:"));
4623 /* for each bit set in unsupported_flags */
4624 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4625 idx = lowbit64(f) - 1;
4626 fprintf(stderr, " -%c", flag_to_arg[idx]);
4629 fprintf(stderr, ". Try running a newer module.\n");
4630 pool_list_free(list);
4636 if ((npools = pool_list_count(list)) == 0)
4637 (void) fprintf(stderr, gettext("no pools available\n"));
4640 * If this is the first iteration and -y was supplied
4641 * we skip any printing.
4643 boolean_t skip = (omit_since_boot &&
4644 cb.cb_iteration == 0);
4647 * Refresh all statistics. This is done as an
4648 * explicit step before calculating the maximum name
4649 * width, so that any * configuration changes are
4650 * properly accounted for.
4652 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4656 * Iterate over all pools to determine the maximum width
4657 * for the pool / device name column across all pools.
4659 cb.cb_namewidth = 0;
4660 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4663 if (timestamp_fmt != NODATE)
4664 print_timestamp(timestamp_fmt);
4666 if (cmd != NULL && cb.cb_verbose &&
4667 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4668 cb.vcdl = all_pools_for_each_vdev_run(argc,
4669 argv, cmd, g_zfs, cb.cb_vdev_names,
4670 cb.cb_vdev_names_count, cb.cb_name_flags);
4676 * If it's the first time and we're not skipping it,
4677 * or either skip or verbose mode, print the header.
4679 * The histogram code explicitly prints its header on
4680 * every vdev, so skip this for histograms.
4682 if (((++cb.cb_iteration == 1 && !skip) ||
4683 (skip != verbose)) &&
4684 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4686 print_iostat_header(&cb);
4689 (void) fsleep(interval);
4694 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4697 * If there's more than one pool, and we're not in
4698 * verbose mode (which prints a separator for us),
4699 * then print a separator.
4701 * In addition, if we're printing specific vdevs then
4702 * we also want an ending separator.
4704 if (((npools > 1 && !verbose &&
4705 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4706 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4707 cb.cb_vdev_names_count)) &&
4709 print_iostat_separator(&cb);
4710 if (cb.vcdl != NULL)
4711 print_cmd_columns(cb.vcdl, 1);
4715 if (cb.vcdl != NULL)
4716 free_vdev_cmd_data_list(cb.vcdl);
4721 * Flush the output so that redirection to a file isn't buffered
4724 (void) fflush(stdout);
4729 if (count != 0 && --count == 0)
4732 (void) fsleep(interval);
4735 pool_list_free(list);
4740 typedef struct list_cbdata {
4741 boolean_t cb_verbose;
4744 boolean_t cb_scripted;
4745 zprop_list_t *cb_proplist;
4746 boolean_t cb_literal;
4750 * Given a list of columns to display, output appropriate headers for each one.
4753 print_header(list_cbdata_t *cb)
4755 zprop_list_t *pl = cb->cb_proplist;
4756 char headerbuf[ZPOOL_MAXPROPLEN];
4758 boolean_t first = B_TRUE;
4759 boolean_t right_justify;
4762 for (; pl != NULL; pl = pl->pl_next) {
4763 width = pl->pl_width;
4764 if (first && cb->cb_verbose) {
4766 * Reset the width to accommodate the verbose listing
4769 width = cb->cb_namewidth;
4777 right_justify = B_FALSE;
4778 if (pl->pl_prop != ZPROP_INVAL) {
4779 header = zpool_prop_column_name(pl->pl_prop);
4780 right_justify = zpool_prop_align_right(pl->pl_prop);
4784 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4785 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4786 headerbuf[i] = '\0';
4790 if (pl->pl_next == NULL && !right_justify)
4791 (void) printf("%s", header);
4792 else if (right_justify)
4793 (void) printf("%*s", (int)width, header);
4795 (void) printf("%-*s", (int)width, header);
4798 (void) printf("\n");
4802 * Given a pool and a list of properties, print out all the properties according
4803 * to the described layout.
4806 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4808 zprop_list_t *pl = cb->cb_proplist;
4809 boolean_t first = B_TRUE;
4810 char property[ZPOOL_MAXPROPLEN];
4812 boolean_t right_justify;
4815 for (; pl != NULL; pl = pl->pl_next) {
4817 width = pl->pl_width;
4818 if (first && cb->cb_verbose) {
4820 * Reset the width to accommodate the verbose listing
4823 width = cb->cb_namewidth;
4827 if (cb->cb_scripted)
4828 (void) printf("\t");
4835 right_justify = B_FALSE;
4836 if (pl->pl_prop != ZPROP_INVAL) {
4837 if (zpool_get_prop(zhp, pl->pl_prop, property,
4838 sizeof (property), NULL, cb->cb_literal) != 0)
4843 right_justify = zpool_prop_align_right(pl->pl_prop);
4844 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4845 zpool_prop_unsupported(pl->pl_user_prop)) &&
4846 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4847 sizeof (property)) == 0) {
4855 * If this is being called in scripted mode, or if this is the
4856 * last column and it is left-justified, don't include a width
4859 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4860 (void) printf("%s", propstr);
4861 else if (right_justify)
4862 (void) printf("%*s", (int)width, propstr);
4864 (void) printf("%-*s", (int)width, propstr);
4867 (void) printf("\n");
4871 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4872 boolean_t valid, enum zfs_nicenum_format format)
4876 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4879 case ZPOOL_PROP_EXPANDSZ:
4881 (void) strlcpy(propval, "-", sizeof (propval));
4883 zfs_nicenum_format(value, propval, sizeof (propval),
4886 case ZPOOL_PROP_FRAGMENTATION:
4887 if (value == ZFS_FRAG_INVALID) {
4888 (void) strlcpy(propval, "-", sizeof (propval));
4889 } else if (format == ZFS_NICENUM_RAW) {
4890 (void) snprintf(propval, sizeof (propval), "%llu",
4891 (unsigned long long)value);
4893 (void) snprintf(propval, sizeof (propval), "%llu%%",
4894 (unsigned long long)value);
4897 case ZPOOL_PROP_CAPACITY:
4898 if (format == ZFS_NICENUM_RAW)
4899 (void) snprintf(propval, sizeof (propval), "%llu",
4900 (unsigned long long)value);
4902 (void) snprintf(propval, sizeof (propval), "%llu%%",
4903 (unsigned long long)value);
4906 zfs_nicenum_format(value, propval, sizeof (propval), format);
4910 (void) strlcpy(propval, "-", sizeof (propval));
4913 (void) printf("\t%s", propval);
4915 (void) printf(" %*s", (int)width, propval);
4919 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4920 list_cbdata_t *cb, int depth)
4926 boolean_t scripted = cb->cb_scripted;
4927 uint64_t islog = B_FALSE;
4928 boolean_t haslog = B_FALSE;
4929 char *dashes = "%-*s - - - - - -\n";
4931 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4932 (uint64_t **)&vs, &c) == 0);
4935 boolean_t toplevel = (vs->vs_space != 0);
4937 enum zfs_nicenum_format format;
4940 format = ZFS_NICENUM_RAW;
4942 format = ZFS_NICENUM_1024;
4945 (void) printf("\t%s", name);
4946 else if (strlen(name) + depth > cb->cb_namewidth)
4947 (void) printf("%*s%s", depth, "", name);
4949 (void) printf("%*s%s%*s", depth, "", name,
4950 (int)(cb->cb_namewidth - strlen(name) - depth), "");
4953 * Print the properties for the individual vdevs. Some
4954 * properties are only applicable to toplevel vdevs. The
4955 * 'toplevel' boolean value is passed to the print_one_column()
4956 * to indicate that the value is valid.
4958 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4960 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4962 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4963 scripted, toplevel, format);
4964 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4966 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4967 vs->vs_fragmentation, scripted,
4968 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4970 cap = (vs->vs_space == 0) ? 0 :
4971 (vs->vs_alloc * 100 / vs->vs_space);
4972 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4974 (void) printf("\n");
4977 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4978 &child, &children) != 0)
4981 for (c = 0; c < children; c++) {
4982 uint64_t ishole = B_FALSE;
4984 if (nvlist_lookup_uint64(child[c],
4985 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4988 if (nvlist_lookup_uint64(child[c],
4989 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
4994 vname = zpool_vdev_name(g_zfs, zhp, child[c],
4996 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5000 if (haslog == B_TRUE) {
5001 /* LINTED E_SEC_PRINTF_VAR_FMT */
5002 (void) printf(dashes, cb->cb_namewidth, "log");
5003 for (c = 0; c < children; c++) {
5004 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5005 &islog) != 0 || !islog)
5007 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5009 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5014 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5015 &child, &children) == 0 && children > 0) {
5016 /* LINTED E_SEC_PRINTF_VAR_FMT */
5017 (void) printf(dashes, cb->cb_namewidth, "cache");
5018 for (c = 0; c < children; c++) {
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_SPARES, &child,
5027 &children) == 0 && children > 0) {
5028 /* LINTED E_SEC_PRINTF_VAR_FMT */
5029 (void) printf(dashes, cb->cb_namewidth, "spare");
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);
5041 * Generic callback function to list a pool.
5044 list_callback(zpool_handle_t *zhp, void *data)
5046 list_cbdata_t *cbp = data;
5050 config = zpool_get_config(zhp, NULL);
5052 print_pool(zhp, cbp);
5053 if (!cbp->cb_verbose)
5056 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5058 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5064 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5066 * -g Display guid for individual vdev name.
5067 * -H Scripted mode. Don't display headers, and separate properties
5069 * -L Follow links when resolving vdev path name.
5070 * -o List of properties to display. Defaults to
5071 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5072 * "dedupratio,health,altroot"
5073 * -p Display values in parsable (exact) format.
5074 * -P Display full path for vdev name.
5075 * -T Display a timestamp in date(1) or Unix format
5077 * List all pools in the system, whether or not they're healthy. Output space
5078 * statistics for each one, as well as health status summary.
5081 zpool_do_list(int argc, char **argv)
5085 list_cbdata_t cb = { 0 };
5086 static char default_props[] =
5087 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5088 "dedupratio,health,altroot";
5089 char *props = default_props;
5091 unsigned long count = 0;
5093 boolean_t first = B_TRUE;
5096 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5099 cb.cb_name_flags |= VDEV_NAME_GUID;
5102 cb.cb_scripted = B_TRUE;
5105 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5111 cb.cb_name_flags |= VDEV_NAME_PATH;
5114 cb.cb_literal = B_TRUE;
5117 get_timestamp_arg(*optarg);
5120 cb.cb_verbose = B_TRUE;
5123 (void) fprintf(stderr, gettext("missing argument for "
5124 "'%c' option\n"), optopt);
5128 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5137 get_interval_count(&argc, argv, &interval, &count);
5139 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5143 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5147 if (pool_list_count(list) == 0)
5150 if (timestamp_fmt != NODATE)
5151 print_timestamp(timestamp_fmt);
5153 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5157 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5162 if (count != 0 && --count == 0)
5165 pool_list_free(list);
5166 (void) fsleep(interval);
5169 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5170 (void) printf(gettext("no pools available\n"));
5174 pool_list_free(list);
5175 zprop_free_list(cb.cb_proplist);
5180 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5182 boolean_t force = B_FALSE;
5185 char *poolname, *old_disk, *new_disk;
5186 zpool_handle_t *zhp;
5187 nvlist_t *props = NULL;
5192 while ((c = getopt(argc, argv, "fo:")) != -1) {
5198 if ((propval = strchr(optarg, '=')) == NULL) {
5199 (void) fprintf(stderr, gettext("missing "
5200 "'=' for -o option\n"));
5206 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5207 (add_prop_list(optarg, propval, &props, B_TRUE)))
5211 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5220 /* get pool name and check number of arguments */
5222 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5229 (void) fprintf(stderr,
5230 gettext("missing <device> specification\n"));
5238 (void) fprintf(stderr,
5239 gettext("missing <new_device> specification\n"));
5242 new_disk = old_disk;
5252 (void) fprintf(stderr, gettext("too many arguments\n"));
5256 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5261 if (zpool_get_config(zhp, NULL) == NULL) {
5262 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5269 /* unless manually specified use "ashift" pool property (if set) */
5270 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5273 char strval[ZPOOL_MAXPROPLEN];
5275 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5276 if (src != ZPROP_SRC_DEFAULT) {
5277 (void) sprintf(strval, "%" PRId32, intval);
5278 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5279 &props, B_TRUE) == 0);
5283 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5285 if (nvroot == NULL) {
5291 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5294 nvlist_free(nvroot);
5301 * zpool replace [-f] <pool> <device> <new_device>
5303 * -f Force attach, even if <new_device> appears to be in use.
5305 * Replace <device> with <new_device>.
5309 zpool_do_replace(int argc, char **argv)
5311 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5315 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5317 * -f Force attach, even if <new_device> appears to be in use.
5318 * -o Set property=value.
5320 * Attach <new_device> to the mirror containing <device>. If <device> is not
5321 * part of a mirror, then <device> will be transformed into a mirror of
5322 * <device> and <new_device>. In either case, <new_device> will begin life
5323 * with a DTL of [0, now], and will immediately begin to resilver itself.
5326 zpool_do_attach(int argc, char **argv)
5328 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5332 * zpool detach [-f] <pool> <device>
5334 * -f Force detach of <device>, even if DTLs argue against it
5335 * (not supported yet)
5337 * Detach a device from a mirror. The operation will be refused if <device>
5338 * is the last device in the mirror, or if the DTLs indicate that this device
5339 * has the only valid copy of some data.
5343 zpool_do_detach(int argc, char **argv)
5346 char *poolname, *path;
5347 zpool_handle_t *zhp;
5351 while ((c = getopt(argc, argv, "f")) != -1) {
5355 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5364 /* get pool name and check number of arguments */
5366 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5371 (void) fprintf(stderr,
5372 gettext("missing <device> specification\n"));
5379 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5382 ret = zpool_vdev_detach(zhp, path);
5390 * zpool split [-gLnP] [-o prop=val] ...
5392 * [-R altroot] <pool> <newpool> [<device> ...]
5394 * -g Display guid for individual vdev name.
5395 * -L Follow links when resolving vdev path name.
5396 * -n Do not split the pool, but display the resulting layout if
5397 * it were to be split.
5398 * -o Set property=value, or set mount options.
5399 * -P Display full path for vdev name.
5400 * -R Mount the split-off pool under an alternate root.
5401 * -l Load encryption keys while importing.
5403 * Splits the named pool and gives it the new pool name. Devices to be split
5404 * off may be listed, provided that no more than one device is specified
5405 * per top-level vdev mirror. The newly split pool is left in an exported
5406 * state unless -R is specified.
5408 * Restrictions: the top-level of the pool pool must only be made up of
5409 * mirrors; all devices in the pool must be healthy; no device may be
5410 * undergoing a resilvering operation.
5413 zpool_do_split(int argc, char **argv)
5415 char *srcpool, *newpool, *propval;
5416 char *mntopts = NULL;
5419 boolean_t loadkeys = B_FALSE;
5420 zpool_handle_t *zhp;
5421 nvlist_t *config, *props = NULL;
5423 flags.dryrun = B_FALSE;
5424 flags.import = B_FALSE;
5425 flags.name_flags = 0;
5428 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5431 flags.name_flags |= VDEV_NAME_GUID;
5434 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5437 flags.import = B_TRUE;
5439 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5440 &props, B_TRUE) != 0) {
5449 flags.dryrun = B_TRUE;
5452 if ((propval = strchr(optarg, '=')) != NULL) {
5455 if (add_prop_list(optarg, propval,
5456 &props, B_TRUE) != 0) {
5465 flags.name_flags |= VDEV_NAME_PATH;
5468 (void) fprintf(stderr, gettext("missing argument for "
5469 "'%c' option\n"), optopt);
5473 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5480 if (!flags.import && mntopts != NULL) {
5481 (void) fprintf(stderr, gettext("setting mntopts is only "
5482 "valid when importing the pool\n"));
5486 if (!flags.import && loadkeys) {
5487 (void) fprintf(stderr, gettext("loading keys is only "
5488 "valid when importing the pool\n"));
5496 (void) fprintf(stderr, gettext("Missing pool name\n"));
5500 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5510 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5515 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5516 if (config == NULL) {
5520 (void) printf(gettext("would create '%s' with the "
5521 "following layout:\n\n"), newpool);
5522 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5529 if (ret != 0 || flags.dryrun || !flags.import) {
5530 nvlist_free(config);
5536 * The split was successful. Now we need to open the new
5537 * pool and import it.
5539 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5540 nvlist_free(config);
5546 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5551 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5552 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5554 (void) fprintf(stderr, gettext("Split was successful, but "
5555 "the datasets could not all be mounted\n"));
5556 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5557 "different altroot\n"), "zpool import");
5560 nvlist_free(config);
5569 * zpool online <pool> <device> ...
5572 zpool_do_online(int argc, char **argv)
5576 zpool_handle_t *zhp;
5578 vdev_state_t newstate;
5582 while ((c = getopt(argc, argv, "et")) != -1) {
5585 flags |= ZFS_ONLINE_EXPAND;
5589 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5598 /* get pool name and check number of arguments */
5600 (void) fprintf(stderr, gettext("missing pool name\n"));
5604 (void) fprintf(stderr, gettext("missing device name\n"));
5610 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5613 for (i = 1; i < argc; i++) {
5614 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5615 if (newstate != VDEV_STATE_HEALTHY) {
5616 (void) printf(gettext("warning: device '%s' "
5617 "onlined, but remains in faulted state\n"),
5619 if (newstate == VDEV_STATE_FAULTED)
5620 (void) printf(gettext("use 'zpool "
5621 "clear' to restore a faulted "
5624 (void) printf(gettext("use 'zpool "
5625 "replace' to replace devices "
5626 "that are no longer present\n"));
5639 * zpool offline [-ft] <pool> <device> ...
5641 * -f Force the device into a faulted state.
5643 * -t Only take the device off-line temporarily. The offline/faulted
5644 * state will not be persistent across reboots.
5648 zpool_do_offline(int argc, char **argv)
5652 zpool_handle_t *zhp;
5654 boolean_t istmp = B_FALSE;
5655 boolean_t fault = B_FALSE;
5658 while ((c = getopt(argc, argv, "ft")) != -1) {
5667 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5676 /* get pool name and check number of arguments */
5678 (void) fprintf(stderr, gettext("missing pool name\n"));
5682 (void) fprintf(stderr, gettext("missing device name\n"));
5688 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5691 for (i = 1; i < argc; i++) {
5693 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
5695 if (istmp == B_FALSE) {
5696 /* Force the fault to persist across imports */
5697 aux = VDEV_AUX_EXTERNAL_PERSIST;
5699 aux = VDEV_AUX_EXTERNAL;
5702 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
5705 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5716 * zpool clear <pool> [device]
5718 * Clear all errors associated with a pool or a particular device.
5721 zpool_do_clear(int argc, char **argv)
5725 boolean_t dryrun = B_FALSE;
5726 boolean_t do_rewind = B_FALSE;
5727 boolean_t xtreme_rewind = B_FALSE;
5728 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5729 nvlist_t *policy = NULL;
5730 zpool_handle_t *zhp;
5731 char *pool, *device;
5734 while ((c = getopt(argc, argv, "FnX")) != -1) {
5743 xtreme_rewind = B_TRUE;
5746 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5756 (void) fprintf(stderr, gettext("missing pool name\n"));
5761 (void) fprintf(stderr, gettext("too many arguments\n"));
5765 if ((dryrun || xtreme_rewind) && !do_rewind) {
5766 (void) fprintf(stderr,
5767 gettext("-n or -X only meaningful with -F\n"));
5771 rewind_policy = ZPOOL_TRY_REWIND;
5773 rewind_policy = ZPOOL_DO_REWIND;
5775 rewind_policy |= ZPOOL_EXTREME_REWIND;
5777 /* In future, further rewind policy choices can be passed along here */
5778 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5779 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5783 device = argc == 2 ? argv[1] : NULL;
5785 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5786 nvlist_free(policy);
5790 if (zpool_clear(zhp, device, policy) != 0)
5795 nvlist_free(policy);
5801 * zpool reguid <pool>
5804 zpool_do_reguid(int argc, char **argv)
5808 zpool_handle_t *zhp;
5812 while ((c = getopt(argc, argv, "")) != -1) {
5815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5824 /* get pool name and check number of arguments */
5826 (void) fprintf(stderr, gettext("missing pool name\n"));
5831 (void) fprintf(stderr, gettext("too many arguments\n"));
5836 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5839 ret = zpool_reguid(zhp);
5847 * zpool reopen <pool>
5849 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5852 zpool_do_reopen(int argc, char **argv)
5856 zpool_handle_t *zhp;
5860 while ((c = getopt(argc, argv, "")) != -1) {
5863 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5873 (void) fprintf(stderr, gettext("missing pool name\n"));
5878 (void) fprintf(stderr, gettext("too many arguments\n"));
5883 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
5886 ret = zpool_reopen(zhp);
5891 typedef struct scrub_cbdata {
5895 pool_scrub_cmd_t cb_scrub_cmd;
5899 scrub_callback(zpool_handle_t *zhp, void *data)
5901 scrub_cbdata_t *cb = data;
5905 * Ignore faulted pools.
5907 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5908 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5909 "currently unavailable\n"), zpool_get_name(zhp));
5913 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
5919 * zpool scrub [-s | -p] <pool> ...
5921 * -s Stop. Stops any in-progress scrub.
5922 * -p Pause. Pause in-progress scrub.
5925 zpool_do_scrub(int argc, char **argv)
5930 cb.cb_type = POOL_SCAN_SCRUB;
5931 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
5934 while ((c = getopt(argc, argv, "sp")) != -1) {
5937 cb.cb_type = POOL_SCAN_NONE;
5940 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
5943 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5949 if (cb.cb_type == POOL_SCAN_NONE &&
5950 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
5951 (void) fprintf(stderr, gettext("invalid option combination: "
5952 "-s and -p are mutually exclusive\n"));
5962 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5966 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5970 * Print out detailed scrub status.
5973 print_scan_status(pool_scan_stat_t *ps)
5975 time_t start, end, pause;
5976 uint64_t elapsed, mins_left, hours_left;
5977 uint64_t pass_exam, examined, total;
5979 double fraction_done;
5980 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
5982 (void) printf(gettext(" scan: "));
5984 /* If there's never been a scan, there's not much to say. */
5985 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5986 ps->pss_func >= POOL_SCAN_FUNCS) {
5987 (void) printf(gettext("none requested\n"));
5991 start = ps->pss_start_time;
5992 end = ps->pss_end_time;
5993 pause = ps->pss_pass_scrub_pause;
5994 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
5996 assert(ps->pss_func == POOL_SCAN_SCRUB ||
5997 ps->pss_func == POOL_SCAN_RESILVER);
5999 * Scan is finished or canceled.
6001 if (ps->pss_state == DSS_FINISHED) {
6002 uint64_t minutes_taken = (end - start) / 60;
6005 if (ps->pss_func == POOL_SCAN_SCRUB) {
6006 fmt = gettext("scrub repaired %s in %lluh%um with "
6007 "%llu errors on %s");
6008 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6009 fmt = gettext("resilvered %s in %lluh%um with "
6010 "%llu errors on %s");
6013 (void) printf(fmt, processed_buf,
6014 (u_longlong_t)(minutes_taken / 60),
6015 (uint_t)(minutes_taken % 60),
6016 (u_longlong_t)ps->pss_errors,
6017 ctime((time_t *)&end));
6019 } else if (ps->pss_state == DSS_CANCELED) {
6020 if (ps->pss_func == POOL_SCAN_SCRUB) {
6021 (void) printf(gettext("scrub canceled on %s"),
6023 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6024 (void) printf(gettext("resilver canceled on %s"),
6030 assert(ps->pss_state == DSS_SCANNING);
6033 * Scan is in progress.
6035 if (ps->pss_func == POOL_SCAN_SCRUB) {
6037 (void) printf(gettext("scrub in progress since %s"),
6041 struct tm *p = localtime(&pause);
6042 (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
6043 (void) printf(gettext("scrub paused since %s\n"), buf);
6044 (void) printf(gettext("\tscrub started on %s"),
6047 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6048 (void) printf(gettext("resilver in progress since %s"),
6052 examined = ps->pss_examined ? ps->pss_examined : 1;
6053 total = ps->pss_to_examine;
6054 fraction_done = (double)examined / total;
6056 /* elapsed time for this pass */
6057 elapsed = time(NULL) - ps->pss_pass_start;
6058 elapsed -= ps->pss_pass_scrub_spent_paused;
6059 elapsed = elapsed ? elapsed : 1;
6060 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
6061 rate = pass_exam / elapsed;
6062 rate = rate ? rate : 1;
6063 mins_left = ((total - examined) / rate) / 60;
6064 hours_left = mins_left / 60;
6066 zfs_nicebytes(examined, examined_buf, sizeof (examined_buf));
6067 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6070 * do not print estimated time if hours_left is more than 30 days
6071 * or we have a paused scrub
6074 zfs_nicebytes(rate, rate_buf, sizeof (rate_buf));
6075 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
6076 examined_buf, total_buf, rate_buf);
6077 if (hours_left < (30 * 24)) {
6078 (void) printf(gettext(", %lluh%um to go\n"),
6079 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
6081 (void) printf(gettext(
6082 ", (scan is slow, no estimated time)\n"));
6085 (void) printf(gettext("\t%s scanned out of %s\n"),
6086 examined_buf, total_buf);
6089 if (ps->pss_func == POOL_SCAN_RESILVER) {
6090 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
6091 processed_buf, 100 * fraction_done);
6092 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6093 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
6094 processed_buf, 100 * fraction_done);
6099 print_error_log(zpool_handle_t *zhp)
6101 nvlist_t *nverrlist = NULL;
6104 size_t len = MAXPATHLEN * 2;
6106 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6109 (void) printf("errors: Permanent errors have been "
6110 "detected in the following files:\n\n");
6112 pathname = safe_malloc(len);
6114 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6116 uint64_t dsobj, obj;
6118 verify(nvpair_value_nvlist(elem, &nv) == 0);
6119 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6121 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6123 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6124 (void) printf("%7s %s\n", "", pathname);
6127 nvlist_free(nverrlist);
6131 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6140 (void) printf(gettext("\tspares\n"));
6142 for (i = 0; i < nspares; i++) {
6143 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6145 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6151 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6160 (void) printf(gettext("\tcache\n"));
6162 for (i = 0; i < nl2cache; i++) {
6163 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6165 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6171 print_dedup_stats(nvlist_t *config)
6173 ddt_histogram_t *ddh;
6177 char dspace[6], mspace[6];
6180 * If the pool was faulted then we may not have been able to
6181 * obtain the config. Otherwise, if we have anything in the dedup
6182 * table continue processing the stats.
6184 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6185 (uint64_t **)&ddo, &c) != 0)
6188 (void) printf("\n");
6189 (void) printf(gettext(" dedup: "));
6190 if (ddo->ddo_count == 0) {
6191 (void) printf(gettext("no DDT entries\n"));
6195 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6196 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6197 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6198 (u_longlong_t)ddo->ddo_count,
6202 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6203 (uint64_t **)&dds, &c) == 0);
6204 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6205 (uint64_t **)&ddh, &c) == 0);
6206 zpool_dump_ddt(dds, ddh);
6210 * Display a summary of pool status. Displays a summary such as:
6214 * reason: One or more devices ...
6215 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6221 * When given the '-v' option, we print out the complete config. If the '-e'
6222 * option is specified, then we print out error rate information as well.
6225 status_callback(zpool_handle_t *zhp, void *data)
6227 status_cbdata_t *cbp = data;
6228 nvlist_t *config, *nvroot;
6230 zpool_status_t reason;
6231 zpool_errata_t errata;
6236 config = zpool_get_config(zhp, NULL);
6237 reason = zpool_get_status(zhp, &msgid, &errata);
6242 * If we were given 'zpool status -x', only report those pools with
6245 if (cbp->cb_explain &&
6246 (reason == ZPOOL_STATUS_OK ||
6247 reason == ZPOOL_STATUS_VERSION_OLDER ||
6248 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6249 if (!cbp->cb_allpools) {
6250 (void) printf(gettext("pool '%s' is healthy\n"),
6251 zpool_get_name(zhp));
6253 cbp->cb_first = B_FALSE;
6259 cbp->cb_first = B_FALSE;
6261 (void) printf("\n");
6263 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6265 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6266 (uint64_t **)&vs, &c) == 0);
6267 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6269 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6270 (void) printf(gettext(" state: %s\n"), health);
6273 case ZPOOL_STATUS_MISSING_DEV_R:
6274 (void) printf(gettext("status: One or more devices could not "
6275 "be opened. Sufficient replicas exist for\n\tthe pool to "
6276 "continue functioning in a degraded state.\n"));
6277 (void) printf(gettext("action: Attach the missing device and "
6278 "online it using 'zpool online'.\n"));
6281 case ZPOOL_STATUS_MISSING_DEV_NR:
6282 (void) printf(gettext("status: One or more devices could not "
6283 "be opened. There are insufficient\n\treplicas for the "
6284 "pool to continue functioning.\n"));
6285 (void) printf(gettext("action: Attach the missing device and "
6286 "online it using 'zpool online'.\n"));
6289 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6290 (void) printf(gettext("status: One or more devices could not "
6291 "be used because the label is missing or\n\tinvalid. "
6292 "Sufficient replicas exist for the pool to continue\n\t"
6293 "functioning in a degraded state.\n"));
6294 (void) printf(gettext("action: Replace the device using "
6295 "'zpool replace'.\n"));
6298 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6299 (void) printf(gettext("status: One or more devices could not "
6300 "be used because the label is missing \n\tor invalid. "
6301 "There are insufficient replicas for the pool to "
6302 "continue\n\tfunctioning.\n"));
6303 zpool_explain_recover(zpool_get_handle(zhp),
6304 zpool_get_name(zhp), reason, config);
6307 case ZPOOL_STATUS_FAILING_DEV:
6308 (void) printf(gettext("status: One or more devices has "
6309 "experienced an unrecoverable error. An\n\tattempt was "
6310 "made to correct the error. Applications are "
6312 (void) printf(gettext("action: Determine if the device needs "
6313 "to be replaced, and clear the errors\n\tusing "
6314 "'zpool clear' or replace the device with 'zpool "
6318 case ZPOOL_STATUS_OFFLINE_DEV:
6319 (void) printf(gettext("status: One or more devices has "
6320 "been taken offline by the administrator.\n\tSufficient "
6321 "replicas exist for the pool to continue functioning in "
6322 "a\n\tdegraded state.\n"));
6323 (void) printf(gettext("action: Online the device using "
6324 "'zpool online' or replace the device with\n\t'zpool "
6328 case ZPOOL_STATUS_REMOVED_DEV:
6329 (void) printf(gettext("status: One or more devices has "
6330 "been removed by the administrator.\n\tSufficient "
6331 "replicas exist for the pool to continue functioning in "
6332 "a\n\tdegraded state.\n"));
6333 (void) printf(gettext("action: Online the device using "
6334 "'zpool online' or replace the device with\n\t'zpool "
6338 case ZPOOL_STATUS_RESILVERING:
6339 (void) printf(gettext("status: One or more devices is "
6340 "currently being resilvered. The pool will\n\tcontinue "
6341 "to function, possibly in a degraded state.\n"));
6342 (void) printf(gettext("action: Wait for the resilver to "
6346 case ZPOOL_STATUS_CORRUPT_DATA:
6347 (void) printf(gettext("status: One or more devices has "
6348 "experienced an error resulting in data\n\tcorruption. "
6349 "Applications may be affected.\n"));
6350 (void) printf(gettext("action: Restore the file in question "
6351 "if possible. Otherwise restore the\n\tentire pool from "
6355 case ZPOOL_STATUS_CORRUPT_POOL:
6356 (void) printf(gettext("status: The pool metadata is corrupted "
6357 "and the pool cannot be opened.\n"));
6358 zpool_explain_recover(zpool_get_handle(zhp),
6359 zpool_get_name(zhp), reason, config);
6362 case ZPOOL_STATUS_VERSION_OLDER:
6363 (void) printf(gettext("status: The pool is formatted using a "
6364 "legacy on-disk format. The pool can\n\tstill be used, "
6365 "but some features are unavailable.\n"));
6366 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6367 "upgrade'. Once this is done, the\n\tpool will no longer "
6368 "be accessible on software that does not support\n\t"
6369 "feature flags.\n"));
6372 case ZPOOL_STATUS_VERSION_NEWER:
6373 (void) printf(gettext("status: The pool has been upgraded to a "
6374 "newer, incompatible on-disk version.\n\tThe pool cannot "
6375 "be accessed on this system.\n"));
6376 (void) printf(gettext("action: Access the pool from a system "
6377 "running more recent software, or\n\trestore the pool from "
6381 case ZPOOL_STATUS_FEAT_DISABLED:
6382 (void) printf(gettext("status: Some supported features are not "
6383 "enabled on the pool. The pool can\n\tstill be used, but "
6384 "some features are unavailable.\n"));
6385 (void) printf(gettext("action: Enable all features using "
6386 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6387 "longer be accessible by software that does not support\n\t"
6388 "the features. See zpool-features(5) for details.\n"));
6391 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6392 (void) printf(gettext("status: The pool cannot be accessed on "
6393 "this system because it uses the\n\tfollowing feature(s) "
6394 "not supported on this system:\n"));
6395 zpool_print_unsup_feat(config);
6396 (void) printf("\n");
6397 (void) printf(gettext("action: Access the pool from a system "
6398 "that supports the required feature(s),\n\tor restore the "
6399 "pool from backup.\n"));
6402 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6403 (void) printf(gettext("status: The pool can only be accessed "
6404 "in read-only mode on this system. It\n\tcannot be "
6405 "accessed in read-write mode because it uses the "
6406 "following\n\tfeature(s) not supported on this system:\n"));
6407 zpool_print_unsup_feat(config);
6408 (void) printf("\n");
6409 (void) printf(gettext("action: The pool cannot be accessed in "
6410 "read-write mode. Import the pool with\n"
6411 "\t\"-o readonly=on\", access the pool from a system that "
6412 "supports the\n\trequired feature(s), or restore the "
6413 "pool from backup.\n"));
6416 case ZPOOL_STATUS_FAULTED_DEV_R:
6417 (void) printf(gettext("status: One or more devices are "
6418 "faulted in response to persistent errors.\n\tSufficient "
6419 "replicas exist for the pool to continue functioning "
6420 "in a\n\tdegraded state.\n"));
6421 (void) printf(gettext("action: Replace the faulted device, "
6422 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6425 case ZPOOL_STATUS_FAULTED_DEV_NR:
6426 (void) printf(gettext("status: One or more devices are "
6427 "faulted in response to persistent errors. There are "
6428 "insufficient replicas for the pool to\n\tcontinue "
6430 (void) printf(gettext("action: Destroy and re-create the pool "
6431 "from a backup source. Manually marking the device\n"
6432 "\trepaired using 'zpool clear' may allow some data "
6433 "to be recovered.\n"));
6436 case ZPOOL_STATUS_IO_FAILURE_WAIT:
6437 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6438 (void) printf(gettext("status: One or more devices are "
6439 "faulted in response to IO failures.\n"));
6440 (void) printf(gettext("action: Make sure the affected devices "
6441 "are connected, then run 'zpool clear'.\n"));
6444 case ZPOOL_STATUS_BAD_LOG:
6445 (void) printf(gettext("status: An intent log record "
6446 "could not be read.\n"
6447 "\tWaiting for administrator intervention to fix the "
6448 "faulted pool.\n"));
6449 (void) printf(gettext("action: Either restore the affected "
6450 "device(s) and run 'zpool online',\n"
6451 "\tor ignore the intent log records by running "
6452 "'zpool clear'.\n"));
6455 case ZPOOL_STATUS_HOSTID_MISMATCH:
6456 (void) printf(gettext("status: Mismatch between pool hostid "
6457 "and system hostid on imported pool.\n\tThis pool was "
6458 "previously imported into a system with a different "
6459 "hostid,\n\tand then was verbatim imported into this "
6461 (void) printf(gettext("action: Export this pool on all systems "
6462 "on which it is imported.\n"
6463 "\tThen import it to correct the mismatch.\n"));
6466 case ZPOOL_STATUS_ERRATA:
6467 (void) printf(gettext("status: Errata #%d detected.\n"),
6471 case ZPOOL_ERRATA_NONE:
6474 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
6475 (void) printf(gettext("action: To correct the issue "
6476 "run 'zpool scrub'.\n"));
6481 * All errata which allow the pool to be imported
6482 * must contain an action message.
6490 * The remaining errors can't actually be generated, yet.
6492 assert(reason == ZPOOL_STATUS_OK);
6496 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6499 if (config != NULL) {
6501 nvlist_t **spares, **l2cache;
6502 uint_t nspares, nl2cache;
6503 pool_scan_stat_t *ps = NULL;
6505 (void) nvlist_lookup_uint64_array(nvroot,
6506 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6507 print_scan_status(ps);
6509 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6510 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6511 if (cbp->cb_namewidth < 10)
6512 cbp->cb_namewidth = 10;
6514 (void) printf(gettext("config:\n\n"));
6515 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6516 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6519 if (cbp->vcdl != NULL)
6520 print_cmd_columns(cbp->vcdl, 0);
6523 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6526 if (num_logs(nvroot) > 0)
6527 print_logs(zhp, cbp, nvroot);
6528 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6529 &l2cache, &nl2cache) == 0)
6530 print_l2cache(zhp, cbp, l2cache, nl2cache);
6532 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6533 &spares, &nspares) == 0)
6534 print_spares(zhp, cbp, spares, nspares);
6536 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6538 nvlist_t *nverrlist = NULL;
6541 * If the approximate error count is small, get a
6542 * precise count by fetching the entire log and
6543 * uniquifying the results.
6545 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6546 zpool_get_errlog(zhp, &nverrlist) == 0) {
6551 while ((elem = nvlist_next_nvpair(nverrlist,
6556 nvlist_free(nverrlist);
6558 (void) printf("\n");
6561 (void) printf(gettext("errors: No known data "
6563 else if (!cbp->cb_verbose)
6564 (void) printf(gettext("errors: %llu data "
6565 "errors, use '-v' for a list\n"),
6566 (u_longlong_t)nerr);
6568 print_error_log(zhp);
6571 if (cbp->cb_dedup_stats)
6572 print_dedup_stats(config);
6574 (void) printf(gettext("config: The configuration cannot be "
6582 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6583 * [interval [count]]
6585 * -c CMD For each vdev, run command CMD
6586 * -g Display guid for individual vdev name.
6587 * -L Follow links when resolving vdev path name.
6588 * -P Display full path for vdev name.
6589 * -v Display complete error logs
6590 * -x Display only pools with potential problems
6591 * -D Display dedup status (undocumented)
6592 * -T Display a timestamp in date(1) or Unix format
6594 * Describes the health status of all pools or some subset.
6597 zpool_do_status(int argc, char **argv)
6602 unsigned long count = 0;
6603 status_cbdata_t cb = { 0 };
6607 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6612 gettext("Can't set -c flag twice\n"));
6616 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6617 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6618 fprintf(stderr, gettext(
6619 "Can't run -c, disabled by "
6620 "ZPOOL_SCRIPTS_ENABLED.\n"));
6624 if ((getuid() <= 0 || geteuid() <= 0) &&
6625 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6626 fprintf(stderr, gettext(
6627 "Can't run -c with root privileges "
6628 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6634 cb.cb_name_flags |= VDEV_NAME_GUID;
6637 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6640 cb.cb_name_flags |= VDEV_NAME_PATH;
6643 cb.cb_verbose = B_TRUE;
6646 cb.cb_explain = B_TRUE;
6649 cb.cb_dedup_stats = B_TRUE;
6652 get_timestamp_arg(*optarg);
6655 if (optopt == 'c') {
6656 print_zpool_script_list("status");
6660 gettext("invalid option '%c'\n"), optopt);
6669 get_interval_count(&argc, argv, &interval, &count);
6672 cb.cb_allpools = B_TRUE;
6674 cb.cb_first = B_TRUE;
6675 cb.cb_print_status = B_TRUE;
6678 if (timestamp_fmt != NODATE)
6679 print_timestamp(timestamp_fmt);
6682 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6685 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6686 status_callback, &cb);
6688 if (cb.vcdl != NULL)
6689 free_vdev_cmd_data_list(cb.vcdl);
6691 if (argc == 0 && cb.cb_count == 0)
6692 (void) fprintf(stderr, gettext("no pools available\n"));
6693 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6694 (void) printf(gettext("all pools are healthy\n"));
6702 if (count != 0 && --count == 0)
6705 (void) fsleep(interval);
6711 typedef struct upgrade_cbdata {
6714 uint64_t cb_version;
6719 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6721 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6722 int *count = (int *)unsupp_fs;
6724 if (zfs_version > ZPL_VERSION) {
6725 (void) printf(gettext("%s (v%d) is not supported by this "
6726 "implementation of ZFS.\n"),
6727 zfs_get_name(zhp), zfs_version);
6731 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6739 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6743 uint64_t oldversion;
6746 config = zpool_get_config(zhp, NULL);
6747 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6750 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6751 assert(oldversion < version);
6753 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6758 (void) fprintf(stderr, gettext("Upgrade not performed due "
6759 "to %d unsupported filesystems (max v%d).\n"),
6760 unsupp_fs, (int)ZPL_VERSION);
6764 ret = zpool_upgrade(zhp, version);
6768 if (version >= SPA_VERSION_FEATURES) {
6769 (void) printf(gettext("Successfully upgraded "
6770 "'%s' from version %llu to feature flags.\n"),
6771 zpool_get_name(zhp), (u_longlong_t)oldversion);
6773 (void) printf(gettext("Successfully upgraded "
6774 "'%s' from version %llu to version %llu.\n"),
6775 zpool_get_name(zhp), (u_longlong_t)oldversion,
6776 (u_longlong_t)version);
6783 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6786 boolean_t firstff = B_TRUE;
6787 nvlist_t *enabled = zpool_get_features(zhp);
6790 for (i = 0; i < SPA_FEATURES; i++) {
6791 const char *fname = spa_feature_table[i].fi_uname;
6792 const char *fguid = spa_feature_table[i].fi_guid;
6793 if (!nvlist_exists(enabled, fguid)) {
6795 verify(-1 != asprintf(&propname, "feature@%s", fname));
6796 ret = zpool_set_prop(zhp, propname,
6797 ZFS_FEATURE_ENABLED);
6805 (void) printf(gettext("Enabled the "
6806 "following features on '%s':\n"),
6807 zpool_get_name(zhp));
6810 (void) printf(gettext(" %s\n"), fname);
6821 upgrade_cb(zpool_handle_t *zhp, void *arg)
6823 upgrade_cbdata_t *cbp = arg;
6826 boolean_t printnl = B_FALSE;
6829 config = zpool_get_config(zhp, NULL);
6830 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6833 assert(SPA_VERSION_IS_SUPPORTED(version));
6835 if (version < cbp->cb_version) {
6836 cbp->cb_first = B_FALSE;
6837 ret = upgrade_version(zhp, cbp->cb_version);
6843 * If they did "zpool upgrade -a", then we could
6844 * be doing ioctls to different pools. We need
6845 * to log this history once to each pool, and bypass
6846 * the normal history logging that happens in main().
6848 (void) zpool_log_history(g_zfs, history_str);
6849 log_history = B_FALSE;
6852 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6854 ret = upgrade_enable_all(zhp, &count);
6859 cbp->cb_first = B_FALSE;
6865 (void) printf(gettext("\n"));
6872 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6874 upgrade_cbdata_t *cbp = arg;
6878 config = zpool_get_config(zhp, NULL);
6879 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6882 assert(SPA_VERSION_IS_SUPPORTED(version));
6884 if (version < SPA_VERSION_FEATURES) {
6885 if (cbp->cb_first) {
6886 (void) printf(gettext("The following pools are "
6887 "formatted with legacy version numbers and can\n"
6888 "be upgraded to use feature flags. After "
6889 "being upgraded, these pools\nwill no "
6890 "longer be accessible by software that does not "
6891 "support feature\nflags.\n\n"));
6892 (void) printf(gettext("VER POOL\n"));
6893 (void) printf(gettext("--- ------------\n"));
6894 cbp->cb_first = B_FALSE;
6897 (void) printf("%2llu %s\n", (u_longlong_t)version,
6898 zpool_get_name(zhp));
6905 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6907 upgrade_cbdata_t *cbp = arg;
6911 config = zpool_get_config(zhp, NULL);
6912 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6915 if (version >= SPA_VERSION_FEATURES) {
6917 boolean_t poolfirst = B_TRUE;
6918 nvlist_t *enabled = zpool_get_features(zhp);
6920 for (i = 0; i < SPA_FEATURES; i++) {
6921 const char *fguid = spa_feature_table[i].fi_guid;
6922 const char *fname = spa_feature_table[i].fi_uname;
6923 if (!nvlist_exists(enabled, fguid)) {
6924 if (cbp->cb_first) {
6925 (void) printf(gettext("\nSome "
6926 "supported features are not "
6927 "enabled on the following pools. "
6928 "Once a\nfeature is enabled the "
6929 "pool may become incompatible with "
6930 "software\nthat does not support "
6932 "zpool-features(5) for "
6934 (void) printf(gettext("POOL "
6936 (void) printf(gettext("------"
6938 cbp->cb_first = B_FALSE;
6942 (void) printf(gettext("%s\n"),
6943 zpool_get_name(zhp));
6944 poolfirst = B_FALSE;
6947 (void) printf(gettext(" %s\n"), fname);
6950 * If they did "zpool upgrade -a", then we could
6951 * be doing ioctls to different pools. We need
6952 * to log this history once to each pool, and bypass
6953 * the normal history logging that happens in main().
6955 (void) zpool_log_history(g_zfs, history_str);
6956 log_history = B_FALSE;
6965 upgrade_one(zpool_handle_t *zhp, void *data)
6967 boolean_t printnl = B_FALSE;
6968 upgrade_cbdata_t *cbp = data;
6969 uint64_t cur_version;
6972 if (strcmp("log", zpool_get_name(zhp)) == 0) {
6973 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6974 "Pool 'log' must be renamed using export and import"
6979 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6980 if (cur_version > cbp->cb_version) {
6981 (void) printf(gettext("Pool '%s' is already formatted "
6982 "using more current version '%llu'.\n\n"),
6983 zpool_get_name(zhp), (u_longlong_t)cur_version);
6987 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
6988 (void) printf(gettext("Pool '%s' is already formatted "
6989 "using version %llu.\n\n"), zpool_get_name(zhp),
6990 (u_longlong_t)cbp->cb_version);
6994 if (cur_version != cbp->cb_version) {
6996 ret = upgrade_version(zhp, cbp->cb_version);
7001 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7003 ret = upgrade_enable_all(zhp, &count);
7009 } else if (cur_version == SPA_VERSION) {
7010 (void) printf(gettext("Pool '%s' already has all "
7011 "supported features enabled.\n"),
7012 zpool_get_name(zhp));
7017 (void) printf(gettext("\n"));
7026 * zpool upgrade [-V version] <-a | pool ...>
7028 * With no arguments, display downrev'd ZFS pool available for upgrade.
7029 * Individual pools can be upgraded by specifying the pool, and '-a' will
7030 * upgrade all pools.
7033 zpool_do_upgrade(int argc, char **argv)
7036 upgrade_cbdata_t cb = { 0 };
7038 boolean_t showversions = B_FALSE;
7039 boolean_t upgradeall = B_FALSE;
7044 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7047 upgradeall = B_TRUE;
7050 showversions = B_TRUE;
7053 cb.cb_version = strtoll(optarg, &end, 10);
7055 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7056 (void) fprintf(stderr,
7057 gettext("invalid version '%s'\n"), optarg);
7062 (void) fprintf(stderr, gettext("missing argument for "
7063 "'%c' option\n"), optopt);
7067 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7078 if (cb.cb_version == 0) {
7079 cb.cb_version = SPA_VERSION;
7080 } else if (!upgradeall && argc == 0) {
7081 (void) fprintf(stderr, gettext("-V option is "
7082 "incompatible with other arguments\n"));
7087 if (upgradeall || argc != 0) {
7088 (void) fprintf(stderr, gettext("-v option is "
7089 "incompatible with other arguments\n"));
7092 } else if (upgradeall) {
7094 (void) fprintf(stderr, gettext("-a option should not "
7095 "be used along with a pool name\n"));
7100 (void) printf(gettext("This system supports ZFS pool feature "
7105 (void) printf(gettext("The following features are "
7107 (void) printf(gettext("FEAT DESCRIPTION\n"));
7108 (void) printf("----------------------------------------------"
7109 "---------------\n");
7110 for (i = 0; i < SPA_FEATURES; i++) {
7111 zfeature_info_t *fi = &spa_feature_table[i];
7113 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7114 " (read-only compatible)" : "";
7116 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7117 (void) printf(" %s\n", fi->fi_desc);
7119 (void) printf("\n");
7121 (void) printf(gettext("The following legacy versions are also "
7123 (void) printf(gettext("VER DESCRIPTION\n"));
7124 (void) printf("--- -----------------------------------------"
7125 "---------------\n");
7126 (void) printf(gettext(" 1 Initial ZFS version\n"));
7127 (void) printf(gettext(" 2 Ditto blocks "
7128 "(replicated metadata)\n"));
7129 (void) printf(gettext(" 3 Hot spares and double parity "
7131 (void) printf(gettext(" 4 zpool history\n"));
7132 (void) printf(gettext(" 5 Compression using the gzip "
7134 (void) printf(gettext(" 6 bootfs pool property\n"));
7135 (void) printf(gettext(" 7 Separate intent log devices\n"));
7136 (void) printf(gettext(" 8 Delegated administration\n"));
7137 (void) printf(gettext(" 9 refquota and refreservation "
7139 (void) printf(gettext(" 10 Cache devices\n"));
7140 (void) printf(gettext(" 11 Improved scrub performance\n"));
7141 (void) printf(gettext(" 12 Snapshot properties\n"));
7142 (void) printf(gettext(" 13 snapused property\n"));
7143 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7144 (void) printf(gettext(" 15 user/group space accounting\n"));
7145 (void) printf(gettext(" 16 stmf property support\n"));
7146 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7147 (void) printf(gettext(" 18 Snapshot user holds\n"));
7148 (void) printf(gettext(" 19 Log device removal\n"));
7149 (void) printf(gettext(" 20 Compression using zle "
7150 "(zero-length encoding)\n"));
7151 (void) printf(gettext(" 21 Deduplication\n"));
7152 (void) printf(gettext(" 22 Received properties\n"));
7153 (void) printf(gettext(" 23 Slim ZIL\n"));
7154 (void) printf(gettext(" 24 System attributes\n"));
7155 (void) printf(gettext(" 25 Improved scrub stats\n"));
7156 (void) printf(gettext(" 26 Improved snapshot deletion "
7158 (void) printf(gettext(" 27 Improved snapshot creation "
7160 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7161 (void) printf(gettext("\nFor more information on a particular "
7162 "version, including supported releases,\n"));
7163 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7164 } else if (argc == 0 && upgradeall) {
7165 cb.cb_first = B_TRUE;
7166 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7167 if (ret == 0 && cb.cb_first) {
7168 if (cb.cb_version == SPA_VERSION) {
7169 (void) printf(gettext("All pools are already "
7170 "formatted using feature flags.\n\n"));
7171 (void) printf(gettext("Every feature flags "
7172 "pool already has all supported features "
7175 (void) printf(gettext("All pools are already "
7176 "formatted with version %llu or higher.\n"),
7177 (u_longlong_t)cb.cb_version);
7180 } else if (argc == 0) {
7181 cb.cb_first = B_TRUE;
7182 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7186 (void) printf(gettext("All pools are formatted "
7187 "using feature flags.\n\n"));
7189 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7190 "for a list of available legacy versions.\n"));
7193 cb.cb_first = B_TRUE;
7194 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7198 (void) printf(gettext("Every feature flags pool has "
7199 "all supported features enabled.\n"));
7201 (void) printf(gettext("\n"));
7204 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7211 typedef struct hist_cbdata {
7218 * Print out the command history for a specific pool.
7221 get_history_one(zpool_handle_t *zhp, void *data)
7227 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7229 cb->first = B_FALSE;
7231 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7233 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7236 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7237 &records, &numrecords) == 0);
7238 for (i = 0; i < numrecords; i++) {
7239 nvlist_t *rec = records[i];
7242 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7246 tsec = fnvlist_lookup_uint64(records[i],
7248 (void) localtime_r(&tsec, &t);
7249 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7252 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7253 (void) printf("%s %s", tbuf,
7254 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7255 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7257 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7260 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7261 (void) printf("%s unrecognized record:\n",
7263 dump_nvlist(rec, 4);
7266 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7267 zfs_history_event_names[ievent],
7268 (longlong_t)fnvlist_lookup_uint64(
7269 rec, ZPOOL_HIST_TXG),
7270 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7271 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7274 (void) printf("%s [txg:%lld] %s", tbuf,
7275 (longlong_t)fnvlist_lookup_uint64(
7276 rec, ZPOOL_HIST_TXG),
7277 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7278 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7279 (void) printf(" %s (%llu)",
7280 fnvlist_lookup_string(rec,
7282 (u_longlong_t)fnvlist_lookup_uint64(rec,
7285 (void) printf(" %s", fnvlist_lookup_string(rec,
7286 ZPOOL_HIST_INT_STR));
7287 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7290 (void) printf("%s ioctl %s\n", tbuf,
7291 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7292 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7293 (void) printf(" input:\n");
7294 dump_nvlist(fnvlist_lookup_nvlist(rec,
7295 ZPOOL_HIST_INPUT_NVL), 8);
7297 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7298 (void) printf(" output:\n");
7299 dump_nvlist(fnvlist_lookup_nvlist(rec,
7300 ZPOOL_HIST_OUTPUT_NVL), 8);
7305 (void) printf("%s unrecognized record:\n", tbuf);
7306 dump_nvlist(rec, 4);
7310 (void) printf("\n");
7313 (void) printf(" [");
7314 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7315 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7316 struct passwd *pwd = getpwuid(who);
7317 (void) printf("user %d ", (int)who);
7319 (void) printf("(%s) ", pwd->pw_name);
7321 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7322 (void) printf("on %s",
7323 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7325 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7326 (void) printf(":%s",
7327 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7331 (void) printf("\n");
7333 (void) printf("\n");
7340 * zpool history <pool>
7342 * Displays the history of commands that modified pools.
7345 zpool_do_history(int argc, char **argv)
7347 hist_cbdata_t cbdata = { 0 };
7351 cbdata.first = B_TRUE;
7353 while ((c = getopt(argc, argv, "li")) != -1) {
7356 cbdata.longfmt = B_TRUE;
7359 cbdata.internal = B_TRUE;
7362 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7370 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
7373 if (argc == 0 && cbdata.first == B_TRUE) {
7374 (void) fprintf(stderr, gettext("no pools available\n"));
7381 typedef struct ev_opts {
7389 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7391 char ctime_str[26], str[32], *ptr;
7395 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7396 memset(str, ' ', 32);
7397 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7398 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
7399 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
7400 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
7401 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7403 (void) printf(gettext("%s\t"), str);
7405 (void) printf(gettext("%s "), str);
7407 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7408 (void) printf(gettext("%s\n"), ptr);
7412 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7416 for (nvp = nvlist_next_nvpair(nvl, NULL);
7417 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
7419 data_type_t type = nvpair_type(nvp);
7420 const char *name = nvpair_name(nvp);
7430 printf(gettext("%*s%s = "), depth, "", name);
7433 case DATA_TYPE_BOOLEAN:
7434 printf(gettext("%s"), "1");
7437 case DATA_TYPE_BOOLEAN_VALUE:
7438 (void) nvpair_value_boolean_value(nvp, &b);
7439 printf(gettext("%s"), b ? "1" : "0");
7442 case DATA_TYPE_BYTE:
7443 (void) nvpair_value_byte(nvp, &i8);
7444 printf(gettext("0x%x"), i8);
7447 case DATA_TYPE_INT8:
7448 (void) nvpair_value_int8(nvp, (void *)&i8);
7449 printf(gettext("0x%x"), i8);
7452 case DATA_TYPE_UINT8:
7453 (void) nvpair_value_uint8(nvp, &i8);
7454 printf(gettext("0x%x"), i8);
7457 case DATA_TYPE_INT16:
7458 (void) nvpair_value_int16(nvp, (void *)&i16);
7459 printf(gettext("0x%x"), i16);
7462 case DATA_TYPE_UINT16:
7463 (void) nvpair_value_uint16(nvp, &i16);
7464 printf(gettext("0x%x"), i16);
7467 case DATA_TYPE_INT32:
7468 (void) nvpair_value_int32(nvp, (void *)&i32);
7469 printf(gettext("0x%x"), i32);
7472 case DATA_TYPE_UINT32:
7473 (void) nvpair_value_uint32(nvp, &i32);
7474 printf(gettext("0x%x"), i32);
7477 case DATA_TYPE_INT64:
7478 (void) nvpair_value_int64(nvp, (void *)&i64);
7479 printf(gettext("0x%llx"), (u_longlong_t)i64);
7482 case DATA_TYPE_UINT64:
7483 (void) nvpair_value_uint64(nvp, &i64);
7485 * translate vdev state values to readable
7486 * strings to aide zpool events consumers
7489 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
7491 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
7492 printf(gettext("\"%s\" (0x%llx)"),
7493 zpool_state_to_name(i64, VDEV_AUX_NONE),
7496 printf(gettext("0x%llx"), (u_longlong_t)i64);
7500 case DATA_TYPE_HRTIME:
7501 (void) nvpair_value_hrtime(nvp, (void *)&i64);
7502 printf(gettext("0x%llx"), (u_longlong_t)i64);
7505 case DATA_TYPE_STRING:
7506 (void) nvpair_value_string(nvp, &str);
7507 printf(gettext("\"%s\""), str ? str : "<NULL>");
7510 case DATA_TYPE_NVLIST:
7511 printf(gettext("(embedded nvlist)\n"));
7512 (void) nvpair_value_nvlist(nvp, &cnv);
7513 zpool_do_events_nvprint(cnv, depth + 8);
7514 printf(gettext("%*s(end %s)"), depth, "", name);
7517 case DATA_TYPE_NVLIST_ARRAY: {
7521 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7522 printf(gettext("(%d embedded nvlists)\n"), nelem);
7523 for (i = 0; i < nelem; i++) {
7524 printf(gettext("%*s%s[%d] = %s\n"),
7525 depth, "", name, i, "(embedded nvlist)");
7526 zpool_do_events_nvprint(val[i], depth + 8);
7527 printf(gettext("%*s(end %s[%i])\n"),
7528 depth, "", name, i);
7530 printf(gettext("%*s(end %s)\n"), depth, "", name);
7534 case DATA_TYPE_INT8_ARRAY: {
7538 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7539 for (i = 0; i < nelem; i++)
7540 printf(gettext("0x%x "), val[i]);
7545 case DATA_TYPE_UINT8_ARRAY: {
7549 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7550 for (i = 0; i < nelem; i++)
7551 printf(gettext("0x%x "), val[i]);
7556 case DATA_TYPE_INT16_ARRAY: {
7560 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7561 for (i = 0; i < nelem; i++)
7562 printf(gettext("0x%x "), val[i]);
7567 case DATA_TYPE_UINT16_ARRAY: {
7571 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7572 for (i = 0; i < nelem; i++)
7573 printf(gettext("0x%x "), val[i]);
7578 case DATA_TYPE_INT32_ARRAY: {
7582 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7583 for (i = 0; i < nelem; i++)
7584 printf(gettext("0x%x "), val[i]);
7589 case DATA_TYPE_UINT32_ARRAY: {
7593 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7594 for (i = 0; i < nelem; i++)
7595 printf(gettext("0x%x "), val[i]);
7600 case DATA_TYPE_INT64_ARRAY: {
7604 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7605 for (i = 0; i < nelem; i++)
7606 printf(gettext("0x%llx "),
7607 (u_longlong_t)val[i]);
7612 case DATA_TYPE_UINT64_ARRAY: {
7616 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7617 for (i = 0; i < nelem; i++)
7618 printf(gettext("0x%llx "),
7619 (u_longlong_t)val[i]);
7624 case DATA_TYPE_STRING_ARRAY: {
7628 (void) nvpair_value_string_array(nvp, &str, &nelem);
7629 for (i = 0; i < nelem; i++)
7630 printf(gettext("\"%s\" "),
7631 str[i] ? str[i] : "<NULL>");
7636 case DATA_TYPE_BOOLEAN_ARRAY:
7637 case DATA_TYPE_BYTE_ARRAY:
7638 case DATA_TYPE_DOUBLE:
7639 case DATA_TYPE_UNKNOWN:
7640 printf(gettext("<unknown>"));
7644 printf(gettext("\n"));
7649 zpool_do_events_next(ev_opts_t *opts)
7652 int zevent_fd, ret, dropped;
7654 zevent_fd = open(ZFS_DEV, O_RDWR);
7655 VERIFY(zevent_fd >= 0);
7657 if (!opts->scripted)
7658 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7661 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7662 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7663 if (ret || nvl == NULL)
7667 (void) printf(gettext("dropped %d events\n"), dropped);
7669 zpool_do_events_short(nvl, opts);
7671 if (opts->verbose) {
7672 zpool_do_events_nvprint(nvl, 8);
7673 printf(gettext("\n"));
7675 (void) fflush(stdout);
7680 VERIFY(0 == close(zevent_fd));
7686 zpool_do_events_clear(ev_opts_t *opts)
7690 ret = zpool_events_clear(g_zfs, &count);
7692 (void) printf(gettext("cleared %d events\n"), count);
7698 * zpool events [-vfc]
7700 * Displays events logs by ZFS.
7703 zpool_do_events(int argc, char **argv)
7705 ev_opts_t opts = { 0 };
7710 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7725 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7734 ret = zpool_do_events_clear(&opts);
7736 ret = zpool_do_events_next(&opts);
7742 get_callback(zpool_handle_t *zhp, void *data)
7744 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7745 char value[MAXNAMELEN];
7746 zprop_source_t srctype;
7749 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7752 * Skip the special fake placeholder. This will also skip
7753 * over the name property when 'all' is specified.
7755 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7756 pl == cbp->cb_proplist)
7759 if (pl->pl_prop == ZPROP_INVAL &&
7760 (zpool_prop_feature(pl->pl_user_prop) ||
7761 zpool_prop_unsupported(pl->pl_user_prop))) {
7762 srctype = ZPROP_SRC_LOCAL;
7764 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7765 value, sizeof (value)) == 0) {
7766 zprop_print_one_property(zpool_get_name(zhp),
7767 cbp, pl->pl_user_prop, value, srctype,
7771 if (zpool_get_prop(zhp, pl->pl_prop, value,
7772 sizeof (value), &srctype, cbp->cb_literal) != 0)
7775 zprop_print_one_property(zpool_get_name(zhp), cbp,
7776 zpool_prop_to_name(pl->pl_prop), value, srctype,
7784 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7786 * -H Scripted mode. Don't display headers, and separate properties
7788 * -o List of columns to display. Defaults to
7789 * "name,property,value,source".
7790 * -p Display values in parsable (exact) format.
7792 * Get properties of pools in the system. Output space statistics
7793 * for each one as well as other attributes.
7796 zpool_do_get(int argc, char **argv)
7798 zprop_get_cbdata_t cb = { 0 };
7799 zprop_list_t fake_name = { 0 };
7804 cb.cb_first = B_TRUE;
7807 * Set up default columns and sources.
7809 cb.cb_sources = ZPROP_SRC_ALL;
7810 cb.cb_columns[0] = GET_COL_NAME;
7811 cb.cb_columns[1] = GET_COL_PROPERTY;
7812 cb.cb_columns[2] = GET_COL_VALUE;
7813 cb.cb_columns[3] = GET_COL_SOURCE;
7814 cb.cb_type = ZFS_TYPE_POOL;
7817 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7820 cb.cb_literal = B_TRUE;
7823 cb.cb_scripted = B_TRUE;
7826 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7828 while (*optarg != '\0') {
7829 static char *col_subopts[] =
7830 { "name", "property", "value", "source",
7833 if (i == ZFS_GET_NCOLS) {
7834 (void) fprintf(stderr, gettext("too "
7835 "many fields given to -o "
7840 switch (getsubopt(&optarg, col_subopts,
7843 cb.cb_columns[i++] = GET_COL_NAME;
7846 cb.cb_columns[i++] = GET_COL_PROPERTY;
7849 cb.cb_columns[i++] = GET_COL_VALUE;
7852 cb.cb_columns[i++] = GET_COL_SOURCE;
7856 (void) fprintf(stderr,
7857 gettext("\"all\" conflicts "
7858 "with specific fields "
7859 "given to -o option\n"));
7862 cb.cb_columns[0] = GET_COL_NAME;
7863 cb.cb_columns[1] = GET_COL_PROPERTY;
7864 cb.cb_columns[2] = GET_COL_VALUE;
7865 cb.cb_columns[3] = GET_COL_SOURCE;
7869 (void) fprintf(stderr,
7870 gettext("invalid column name "
7877 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7887 (void) fprintf(stderr, gettext("missing property "
7892 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7893 ZFS_TYPE_POOL) != 0)
7899 if (cb.cb_proplist != NULL) {
7900 fake_name.pl_prop = ZPOOL_PROP_NAME;
7901 fake_name.pl_width = strlen(gettext("NAME"));
7902 fake_name.pl_next = cb.cb_proplist;
7903 cb.cb_proplist = &fake_name;
7906 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7909 if (cb.cb_proplist == &fake_name)
7910 zprop_free_list(fake_name.pl_next);
7912 zprop_free_list(cb.cb_proplist);
7917 typedef struct set_cbdata {
7920 boolean_t cb_any_successful;
7924 set_callback(zpool_handle_t *zhp, void *data)
7927 set_cbdata_t *cb = (set_cbdata_t *)data;
7929 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7932 cb->cb_any_successful = B_TRUE;
7938 zpool_do_set(int argc, char **argv)
7940 set_cbdata_t cb = { 0 };
7943 if (argc > 1 && argv[1][0] == '-') {
7944 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7950 (void) fprintf(stderr, gettext("missing property=value "
7956 (void) fprintf(stderr, gettext("missing pool name\n"));
7961 (void) fprintf(stderr, gettext("too many pool names\n"));
7965 cb.cb_propname = argv[1];
7966 cb.cb_value = strchr(cb.cb_propname, '=');
7967 if (cb.cb_value == NULL) {
7968 (void) fprintf(stderr, gettext("missing value in "
7969 "property=value argument\n"));
7973 *(cb.cb_value) = '\0';
7976 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
7983 find_command_idx(char *command, int *idx)
7987 for (i = 0; i < NCOMMAND; i++) {
7988 if (command_table[i].name == NULL)
7991 if (strcmp(command, command_table[i].name) == 0) {
8000 main(int argc, char **argv)
8006 (void) setlocale(LC_ALL, "");
8007 (void) textdomain(TEXT_DOMAIN);
8013 * Make sure the user has specified some command.
8016 (void) fprintf(stderr, gettext("missing command\n"));
8025 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8028 if ((g_zfs = libzfs_init()) == NULL) {
8029 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8033 libzfs_print_on_error(g_zfs, B_TRUE);
8035 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8038 * Run the appropriate command.
8040 if (find_command_idx(cmdname, &i) == 0) {
8041 current_command = &command_table[i];
8042 ret = command_table[i].func(argc - 1, argv + 1);
8043 } else if (strchr(cmdname, '=')) {
8044 verify(find_command_idx("set", &i) == 0);
8045 current_command = &command_table[i];
8046 ret = command_table[i].func(argc, argv);
8047 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8049 * 'freeze' is a vile debugging abomination, so we treat
8052 zfs_cmd_t zc = {"\0"};
8054 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8055 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8057 (void) fprintf(stderr,
8058 gettext("failed to freeze pool: %d\n"), errno);
8064 (void) fprintf(stderr, gettext("unrecognized "
8065 "command '%s'\n"), cmdname);
8070 if (ret == 0 && log_history)
8071 (void) zpool_log_history(g_zfs, history_str);
8076 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8077 * for the purposes of running ::findleaks.
8079 if (getenv("ZFS_ABORT") != NULL) {
8080 (void) printf("dumping core by request\n");