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, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
52 #include <sys/fs/zfs.h>
54 #include <sys/systeminfo.h>
55 #include <sys/fm/fs/zfs.h>
56 #include <sys/fm/util.h>
57 #include <sys/fm/protocol.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/mount.h>
60 #include <sys/sysmacros.h>
66 #include "zpool_util.h"
67 #include "zfs_comutil.h"
68 #include "zfeature_common.h"
70 #include "statcommon.h"
72 static int zpool_do_create(int, char **);
73 static int zpool_do_destroy(int, char **);
75 static int zpool_do_add(int, char **);
76 static int zpool_do_remove(int, char **);
77 static int zpool_do_labelclear(int, char **);
79 static int zpool_do_list(int, char **);
80 static int zpool_do_iostat(int, char **);
81 static int zpool_do_status(int, char **);
83 static int zpool_do_online(int, char **);
84 static int zpool_do_offline(int, char **);
85 static int zpool_do_clear(int, char **);
86 static int zpool_do_reopen(int, char **);
88 static int zpool_do_reguid(int, char **);
90 static int zpool_do_attach(int, char **);
91 static int zpool_do_detach(int, char **);
92 static int zpool_do_replace(int, char **);
93 static int zpool_do_split(int, char **);
95 static int zpool_do_scrub(int, char **);
97 static int zpool_do_import(int, char **);
98 static int zpool_do_export(int, char **);
100 static int zpool_do_upgrade(int, char **);
102 static int zpool_do_history(int, char **);
103 static int zpool_do_events(int, char **);
105 static int zpool_do_get(int, char **);
106 static int zpool_do_set(int, char **);
108 static int zpool_do_sync(int, char **);
111 * These libumem hooks provide a reasonable set of defaults for the allocator's
112 * debugging facilities.
117 _umem_debug_init(void)
119 return ("default,verbose"); /* $UMEM_DEBUG setting */
123 _umem_logging_init(void)
125 return ("fail,contents"); /* $UMEM_LOGGING setting */
160 * Flags for stats to display with "zpool iostats"
168 IOS_COUNT, /* always last element */
171 /* iostat_type entries as bitmasks */
172 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
173 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
174 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
175 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
176 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
178 /* Mask of all the histo bits */
179 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
182 * Lookup table for iostat flags to nvlist names. Basically a list
183 * of all the nvlists a flag requires. Also specifies the order in
184 * which data gets printed in zpool iostat.
186 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
188 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
189 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
190 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
191 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
192 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
193 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
194 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
195 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
196 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
199 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
200 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
201 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
202 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
205 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
206 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
207 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
208 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
209 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
212 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
213 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
214 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
215 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
216 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
217 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
218 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
219 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
220 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
221 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
227 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
228 * Right now, only one histo bit is ever set at one time, so we can
229 * just do a highbit64(a)
231 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
233 typedef struct zpool_command {
235 int (*func)(int, char **);
240 * Master command table. Each ZFS command has a name, associated function, and
241 * usage message. The usage messages need to be internationalized, so we have
242 * to have a function to return the usage message based on a command index.
244 * These commands are organized according to how they are displayed in the usage
245 * message. An empty command (one with a NULL name) indicates an empty line in
246 * the generic usage message.
248 static zpool_command_t command_table[] = {
249 { "create", zpool_do_create, HELP_CREATE },
250 { "destroy", zpool_do_destroy, HELP_DESTROY },
252 { "add", zpool_do_add, HELP_ADD },
253 { "remove", zpool_do_remove, HELP_REMOVE },
255 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
257 { "list", zpool_do_list, HELP_LIST },
258 { "iostat", zpool_do_iostat, HELP_IOSTAT },
259 { "status", zpool_do_status, HELP_STATUS },
261 { "online", zpool_do_online, HELP_ONLINE },
262 { "offline", zpool_do_offline, HELP_OFFLINE },
263 { "clear", zpool_do_clear, HELP_CLEAR },
264 { "reopen", zpool_do_reopen, HELP_REOPEN },
266 { "attach", zpool_do_attach, HELP_ATTACH },
267 { "detach", zpool_do_detach, HELP_DETACH },
268 { "replace", zpool_do_replace, HELP_REPLACE },
269 { "split", zpool_do_split, HELP_SPLIT },
271 { "scrub", zpool_do_scrub, HELP_SCRUB },
273 { "import", zpool_do_import, HELP_IMPORT },
274 { "export", zpool_do_export, HELP_EXPORT },
275 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
276 { "reguid", zpool_do_reguid, HELP_REGUID },
278 { "history", zpool_do_history, HELP_HISTORY },
279 { "events", zpool_do_events, HELP_EVENTS },
281 { "get", zpool_do_get, HELP_GET },
282 { "set", zpool_do_set, HELP_SET },
283 { "sync", zpool_do_sync, HELP_SYNC },
286 #define NCOMMAND (ARRAY_SIZE(command_table))
288 static zpool_command_t *current_command;
289 static char history_str[HIS_MAX_RECORD_LEN];
290 static boolean_t log_history = B_TRUE;
291 static uint_t timestamp_fmt = NODATE;
294 get_usage(zpool_help_t idx)
298 return (gettext("\tadd [-fgLnP] [-o property=value] "
299 "<pool> <vdev> ...\n"));
301 return (gettext("\tattach [-f] [-o property=value] "
302 "<pool> <device> <new-device>\n"));
304 return (gettext("\tclear [-nF] <pool> [device]\n"));
306 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
307 "\t [-O file-system-property=value] ... \n"
308 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
310 return (gettext("\tdestroy [-f] <pool>\n"));
312 return (gettext("\tdetach <pool> <device>\n"));
314 return (gettext("\texport [-af] <pool> ...\n"));
316 return (gettext("\thistory [-il] [<pool>] ...\n"));
318 return (gettext("\timport [-d dir] [-D]\n"
319 "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
321 "\timport [-o mntopts] [-o property=value] ... \n"
322 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
323 "[-R root] [-F [-n]] -a\n"
324 "\timport [-o mntopts] [-o property=value] ... \n"
325 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
326 "[-R root] [-F [-n]]\n"
327 "\t <pool | id> [newpool]\n"));
329 return (gettext("\tiostat [[[-c [script1,script2,...]"
330 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
331 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
332 " [interval [count]]\n"));
333 case HELP_LABELCLEAR:
334 return (gettext("\tlabelclear [-f] <vdev>\n"));
336 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
337 "[-T d|u] [pool] ... \n"
338 "\t [interval [count]]\n"));
340 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
342 return (gettext("\tonline <pool> <device> ...\n"));
344 return (gettext("\treplace [-f] [-o property=value] "
345 "<pool> <device> [new-device]\n"));
347 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
349 return (gettext("\treopen [-n] <pool>\n"));
351 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
353 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
354 "[-T d|u] [pool] ... \n"
355 "\t [interval [count]]\n"));
357 return (gettext("\tupgrade\n"
359 "\tupgrade [-V version] <-a | pool ...>\n"));
361 return (gettext("\tevents [-vHf [pool] | -c]\n"));
363 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
364 "<\"all\" | property[,...]> <pool> ...\n"));
366 return (gettext("\tset <property=value> <pool> \n"));
368 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
369 "\t [-o property=value] <pool> <newpool> "
370 "[<device> ...]\n"));
372 return (gettext("\treguid <pool>\n"));
374 return (gettext("\tsync [pool] ...\n"));
383 * Callback routine that will print out a pool property value.
386 print_prop_cb(int prop, void *cb)
390 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
392 if (zpool_prop_readonly(prop))
393 (void) fprintf(fp, " NO ");
395 (void) fprintf(fp, " YES ");
397 if (zpool_prop_values(prop) == NULL)
398 (void) fprintf(fp, "-\n");
400 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
406 * Display usage message. If we're inside a command, display only the usage for
407 * that command. Otherwise, iterate over the entire command table and display
408 * a complete usage message.
411 usage(boolean_t requested)
413 FILE *fp = requested ? stdout : stderr;
415 if (current_command == NULL) {
418 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
420 gettext("where 'command' is one of the following:\n\n"));
422 for (i = 0; i < NCOMMAND; i++) {
423 if (command_table[i].name == NULL)
424 (void) fprintf(fp, "\n");
426 (void) fprintf(fp, "%s",
427 get_usage(command_table[i].usage));
430 (void) fprintf(fp, gettext("usage:\n"));
431 (void) fprintf(fp, "%s", get_usage(current_command->usage));
434 if (current_command != NULL &&
435 ((strcmp(current_command->name, "set") == 0) ||
436 (strcmp(current_command->name, "get") == 0) ||
437 (strcmp(current_command->name, "list") == 0))) {
440 gettext("\nthe following properties are supported:\n"));
442 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
443 "PROPERTY", "EDIT", "VALUES");
445 /* Iterate over all properties */
446 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
449 (void) fprintf(fp, "\t%-15s ", "feature@...");
450 (void) fprintf(fp, "YES disabled | enabled | active\n");
452 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
453 "appended with a feature name.\nSee zpool-features(5).\n"));
457 * See comments at end of main().
459 if (getenv("ZFS_ABORT") != NULL) {
460 (void) printf("dumping core by request\n");
464 exit(requested ? 0 : 2);
468 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
469 boolean_t print_logs, int name_flags)
476 (void) printf("\t%*s%s\n", indent, "", name);
478 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
479 &child, &children) != 0)
482 for (c = 0; c < children; c++) {
483 uint64_t is_log = B_FALSE;
485 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
487 if ((is_log && !print_logs) || (!is_log && print_logs))
490 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
491 print_vdev_tree(zhp, vname, child[c], indent + 2,
492 B_FALSE, name_flags);
498 prop_list_contains_feature(nvlist_t *proplist)
501 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
502 nvp = nvlist_next_nvpair(proplist, nvp)) {
503 if (zpool_prop_feature(nvpair_name(nvp)))
510 * Add a property pair (name, string-value) into a property nvlist.
513 add_prop_list(const char *propname, char *propval, nvlist_t **props,
516 zpool_prop_t prop = ZPOOL_PROP_INVAL;
522 if (*props == NULL &&
523 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
524 (void) fprintf(stderr,
525 gettext("internal error: out of memory\n"));
532 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
534 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
535 !zpool_prop_feature(propname)) {
536 (void) fprintf(stderr, gettext("property '%s' is "
537 "not a valid pool property\n"), propname);
542 * feature@ properties and version should not be specified
545 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
546 nvlist_exists(proplist, vname)) ||
547 (prop == ZPOOL_PROP_VERSION &&
548 prop_list_contains_feature(proplist))) {
549 (void) fprintf(stderr, gettext("'feature@' and "
550 "'version' properties cannot be specified "
556 if (zpool_prop_feature(propname))
559 normnm = zpool_prop_to_name(prop);
561 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
562 normnm = zfs_prop_to_name(fprop);
568 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
569 prop != ZPOOL_PROP_CACHEFILE) {
570 (void) fprintf(stderr, gettext("property '%s' "
571 "specified multiple times\n"), propname);
575 if (nvlist_add_string(proplist, normnm, propval) != 0) {
576 (void) fprintf(stderr, gettext("internal "
577 "error: out of memory\n"));
585 * Set a default property pair (name, string-value) in a property nvlist
588 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
593 if (nvlist_lookup_string(*props, propname, &pval) == 0)
596 return (add_prop_list(propname, propval, props, B_TRUE));
600 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
602 * -f Force addition of devices, even if they appear in use
603 * -g Display guid for individual vdev name.
604 * -L Follow links when resolving vdev path name.
605 * -n Do not add the devices, but display the resulting layout if
606 * they were to be added.
607 * -o Set property=value.
608 * -P Display full path for vdev name.
610 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
611 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
615 zpool_do_add(int argc, char **argv)
617 boolean_t force = B_FALSE;
618 boolean_t dryrun = B_FALSE;
626 nvlist_t *props = NULL;
630 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
636 name_flags |= VDEV_NAME_GUID;
639 name_flags |= VDEV_NAME_FOLLOW_LINKS;
645 if ((propval = strchr(optarg, '=')) == NULL) {
646 (void) fprintf(stderr, gettext("missing "
647 "'=' for -o option\n"));
653 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
654 (add_prop_list(optarg, propval, &props, B_TRUE)))
658 name_flags |= VDEV_NAME_PATH;
661 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
670 /* get pool name and check number of arguments */
672 (void) fprintf(stderr, gettext("missing pool name argument\n"));
676 (void) fprintf(stderr, gettext("missing vdev specification\n"));
685 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
688 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
689 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
695 /* unless manually specified use "ashift" pool property (if set) */
696 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
699 char strval[ZPOOL_MAXPROPLEN];
701 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
702 if (src != ZPROP_SRC_DEFAULT) {
703 (void) sprintf(strval, "%" PRId32, intval);
704 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
705 &props, B_TRUE) == 0);
709 /* pass off to get_vdev_spec for processing */
710 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
712 if (nvroot == NULL) {
718 nvlist_t *poolnvroot;
720 uint_t l2children, c;
722 boolean_t hadcache = B_FALSE;
724 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
727 (void) printf(gettext("would update '%s' to the following "
728 "configuration:\n"), zpool_get_name(zhp));
730 /* print original main pool and new tree */
731 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
733 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
735 /* Do the same for the logs */
736 if (num_logs(poolnvroot) > 0) {
737 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
739 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
741 } else if (num_logs(nvroot) > 0) {
742 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
746 /* Do the same for the caches */
747 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
748 &l2child, &l2children) == 0 && l2children) {
750 (void) printf(gettext("\tcache\n"));
751 for (c = 0; c < l2children; c++) {
752 vname = zpool_vdev_name(g_zfs, NULL,
753 l2child[c], name_flags);
754 (void) printf("\t %s\n", vname);
758 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
759 &l2child, &l2children) == 0 && l2children) {
761 (void) printf(gettext("\tcache\n"));
762 for (c = 0; c < l2children; c++) {
763 vname = zpool_vdev_name(g_zfs, NULL,
764 l2child[c], name_flags);
765 (void) printf("\t %s\n", vname);
772 ret = (zpool_add(zhp, nvroot) != 0);
783 * zpool remove <pool> <vdev> ...
785 * Removes the given vdev from the pool.
788 zpool_do_remove(int argc, char **argv)
792 zpool_handle_t *zhp = NULL;
793 boolean_t stop = B_FALSE;
795 boolean_t noop = B_FALSE;
796 boolean_t parsable = B_FALSE;
799 while ((c = getopt(argc, argv, "nps")) != -1) {
811 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
820 /* get pool name and check number of arguments */
822 (void) fprintf(stderr, gettext("missing pool name argument\n"));
828 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
832 (void) fprintf(stderr, gettext("stop request ignored\n"));
838 (void) fprintf(stderr, gettext("too many arguments\n"));
841 if (zpool_vdev_remove_cancel(zhp) != 0)
845 (void) fprintf(stderr, gettext("missing device\n"));
849 for (i = 1; i < argc; i++) {
853 if (zpool_vdev_indirect_size(zhp, argv[i],
859 (void) printf("%s %llu\n",
860 argv[i], (unsigned long long)size);
863 zfs_nicenum(size, valstr,
865 (void) printf("Memory that will be "
866 "used after removing %s: %s\n",
870 if (zpool_vdev_remove(zhp, argv[i]) != 0)
881 * zpool labelclear [-f] <vdev>
883 * -f Force clearing the label for the vdevs which are members of
884 * the exported or foreign pools.
886 * Verifies that the vdev is not active and zeros out the label information
890 zpool_do_labelclear(int argc, char **argv)
892 char vdev[MAXPATHLEN];
895 int c, fd = -1, ret = 0;
898 boolean_t inuse = B_FALSE;
899 boolean_t force = B_FALSE;
902 while ((c = getopt(argc, argv, "f")) != -1) {
908 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
919 (void) fprintf(stderr, gettext("missing vdev name\n"));
923 (void) fprintf(stderr, gettext("too many arguments\n"));
928 * Check if we were given absolute path and use it as is.
929 * Otherwise if the provided vdev name doesn't point to a file,
930 * try prepending expected disk paths and partition numbers.
932 (void) strlcpy(vdev, argv[0], sizeof (vdev));
933 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
936 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
937 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
938 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
942 if (error || (stat(vdev, &st) != 0)) {
943 (void) fprintf(stderr, gettext(
944 "failed to find device %s, try specifying absolute "
945 "path instead\n"), argv[0]);
950 if ((fd = open(vdev, O_RDWR)) < 0) {
951 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
952 vdev, strerror(errno));
956 if (ioctl(fd, BLKFLSBUF) != 0)
957 (void) fprintf(stderr, gettext("failed to invalidate "
958 "cache for %s: %s\n"), vdev, strerror(errno));
960 if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
961 (void) fprintf(stderr,
962 gettext("failed to check state for %s\n"), vdev);
968 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
970 (void) fprintf(stderr,
971 gettext("failed to check state for %s\n"), vdev);
981 case POOL_STATE_ACTIVE:
982 case POOL_STATE_SPARE:
983 case POOL_STATE_L2CACHE:
984 (void) fprintf(stderr, gettext(
985 "%s is a member (%s) of pool \"%s\"\n"),
986 vdev, zpool_pool_state_to_name(state), name);
990 case POOL_STATE_EXPORTED:
993 (void) fprintf(stderr, gettext(
994 "use '-f' to override the following error:\n"
995 "%s is a member of exported pool \"%s\"\n"),
1000 case POOL_STATE_POTENTIALLY_ACTIVE:
1003 (void) fprintf(stderr, gettext(
1004 "use '-f' to override the following error:\n"
1005 "%s is a member of potentially active pool \"%s\"\n"),
1010 case POOL_STATE_DESTROYED:
1011 /* inuse should never be set for a destroyed pool */
1017 ret = zpool_clear_label(fd);
1019 (void) fprintf(stderr,
1020 gettext("failed to clear label for %s\n"), vdev);
1031 * zpool create [-fnd] [-o property=value] ...
1032 * [-O file-system-property=value] ...
1033 * [-R root] [-m mountpoint] <pool> <dev> ...
1035 * -f Force creation, even if devices appear in use
1036 * -n Do not create the pool, but display the resulting layout if it
1037 * were to be created.
1038 * -R Create a pool under an alternate root
1039 * -m Set default mountpoint for the root dataset. By default it's
1041 * -o Set property=value.
1042 * -o Set feature@feature=enabled|disabled.
1043 * -d Don't automatically enable all supported pool features
1044 * (individual features can be enabled with -o).
1045 * -O Set fsproperty=value in the pool's root file system
1047 * Creates the named pool according to the given vdev specification. The
1048 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1049 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1050 * (if '-n' was specified), or pass it to libzfs to do the creation.
1053 zpool_do_create(int argc, char **argv)
1055 boolean_t force = B_FALSE;
1056 boolean_t dryrun = B_FALSE;
1057 boolean_t enable_all_pool_feat = B_TRUE;
1059 nvlist_t *nvroot = NULL;
1063 char *altroot = NULL;
1064 char *mountpoint = NULL;
1065 nvlist_t *fsprops = NULL;
1066 nvlist_t *props = NULL;
1070 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1079 enable_all_pool_feat = B_FALSE;
1083 if (add_prop_list(zpool_prop_to_name(
1084 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1086 if (add_prop_list_default(zpool_prop_to_name(
1087 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1091 /* Equivalent to -O mountpoint=optarg */
1092 mountpoint = optarg;
1095 if ((propval = strchr(optarg, '=')) == NULL) {
1096 (void) fprintf(stderr, gettext("missing "
1097 "'=' for -o option\n"));
1103 if (add_prop_list(optarg, propval, &props, B_TRUE))
1107 * If the user is creating a pool that doesn't support
1108 * feature flags, don't enable any features.
1110 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1114 ver = strtoull(propval, &end, 10);
1116 ver < SPA_VERSION_FEATURES) {
1117 enable_all_pool_feat = B_FALSE;
1120 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1124 if ((propval = strchr(optarg, '=')) == NULL) {
1125 (void) fprintf(stderr, gettext("missing "
1126 "'=' for -O option\n"));
1133 * Mountpoints are checked and then added later.
1134 * Uniquely among properties, they can be specified
1135 * more than once, to avoid conflict with -m.
1137 if (0 == strcmp(optarg,
1138 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1139 mountpoint = propval;
1140 } else if (add_prop_list(optarg, propval, &fsprops,
1147 * Sanity check temporary pool name.
1149 if (strchr(optarg, '/') != NULL) {
1150 (void) fprintf(stderr, gettext("cannot create "
1151 "'%s': invalid character '/' in temporary "
1153 (void) fprintf(stderr, gettext("use 'zfs "
1154 "create' to create a dataset\n"));
1158 if (add_prop_list(zpool_prop_to_name(
1159 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1161 if (add_prop_list_default(zpool_prop_to_name(
1162 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1167 (void) fprintf(stderr, gettext("missing argument for "
1168 "'%c' option\n"), optopt);
1171 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1180 /* get pool name and check number of arguments */
1182 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1186 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1193 * As a special case, check for use of '/' in the name, and direct the
1194 * user to use 'zfs create' instead.
1196 if (strchr(poolname, '/') != NULL) {
1197 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1198 "character '/' in pool name\n"), poolname);
1199 (void) fprintf(stderr, gettext("use 'zfs create' to "
1200 "create a dataset\n"));
1204 /* pass off to get_vdev_spec for bulk processing */
1205 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1206 argc - 1, argv + 1);
1210 /* make_root_vdev() allows 0 toplevel children if there are spares */
1211 if (!zfs_allocatable_devs(nvroot)) {
1212 (void) fprintf(stderr, gettext("invalid vdev "
1213 "specification: at least one toplevel vdev must be "
1218 if (altroot != NULL && altroot[0] != '/') {
1219 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1220 "must be an absolute path\n"), altroot);
1225 * Check the validity of the mountpoint and direct the user to use the
1226 * '-m' mountpoint option if it looks like its in use.
1228 if (mountpoint == NULL ||
1229 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1230 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1231 char buf[MAXPATHLEN];
1234 if (mountpoint && mountpoint[0] != '/') {
1235 (void) fprintf(stderr, gettext("invalid mountpoint "
1236 "'%s': must be an absolute path, 'legacy', or "
1237 "'none'\n"), mountpoint);
1241 if (mountpoint == NULL) {
1242 if (altroot != NULL)
1243 (void) snprintf(buf, sizeof (buf), "%s/%s",
1246 (void) snprintf(buf, sizeof (buf), "/%s",
1249 if (altroot != NULL)
1250 (void) snprintf(buf, sizeof (buf), "%s%s",
1251 altroot, mountpoint);
1253 (void) snprintf(buf, sizeof (buf), "%s",
1257 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1258 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1259 "%s\n"), buf, strerror(errno));
1260 (void) fprintf(stderr, gettext("use '-m' "
1261 "option to provide a different default\n"));
1266 while (count < 3 && readdir(dirp) != NULL)
1268 (void) closedir(dirp);
1271 (void) fprintf(stderr, gettext("mountpoint "
1272 "'%s' exists and is not empty\n"), buf);
1273 (void) fprintf(stderr, gettext("use '-m' "
1274 "option to provide a "
1275 "different default\n"));
1282 * Now that the mountpoint's validity has been checked, ensure that
1283 * the property is set appropriately prior to creating the pool.
1285 if (mountpoint != NULL) {
1286 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1287 mountpoint, &fsprops, B_FALSE);
1295 * For a dry run invocation, print out a basic message and run
1296 * through all the vdevs in the list and print out in an
1297 * appropriate hierarchy.
1299 (void) printf(gettext("would create '%s' with the "
1300 "following layout:\n\n"), poolname);
1302 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1303 if (num_logs(nvroot) > 0)
1304 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1309 * Hand off to libzfs.
1312 for (i = 0; i < SPA_FEATURES; i++) {
1313 char propname[MAXPATHLEN];
1315 zfeature_info_t *feat = &spa_feature_table[i];
1317 (void) snprintf(propname, sizeof (propname),
1318 "feature@%s", feat->fi_uname);
1321 * Only features contained in props will be enabled:
1322 * remove from the nvlist every ZFS_FEATURE_DISABLED
1323 * value and add every missing ZFS_FEATURE_ENABLED if
1324 * enable_all_pool_feat is set.
1326 if (!nvlist_lookup_string(props, propname, &propval)) {
1327 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1328 (void) nvlist_remove_all(props,
1330 } else if (enable_all_pool_feat) {
1331 ret = add_prop_list(propname,
1332 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1339 if (zpool_create(g_zfs, poolname,
1340 nvroot, props, fsprops) == 0) {
1341 zfs_handle_t *pool = zfs_open(g_zfs,
1342 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1344 if (zfs_mount(pool, NULL, 0) == 0)
1345 ret = zfs_shareall(pool);
1348 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1349 (void) fprintf(stderr, gettext("pool name may have "
1355 nvlist_free(nvroot);
1356 nvlist_free(fsprops);
1360 nvlist_free(fsprops);
1367 * zpool destroy <pool>
1369 * -f Forcefully unmount any datasets
1371 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1374 zpool_do_destroy(int argc, char **argv)
1376 boolean_t force = B_FALSE;
1379 zpool_handle_t *zhp;
1383 while ((c = getopt(argc, argv, "f")) != -1) {
1389 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1398 /* check arguments */
1400 (void) fprintf(stderr, gettext("missing pool argument\n"));
1404 (void) fprintf(stderr, gettext("too many arguments\n"));
1410 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1412 * As a special case, check for use of '/' in the name, and
1413 * direct the user to use 'zfs destroy' instead.
1415 if (strchr(pool, '/') != NULL)
1416 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1417 "destroy a dataset\n"));
1421 if (zpool_disable_datasets(zhp, force) != 0) {
1422 (void) fprintf(stderr, gettext("could not destroy '%s': "
1423 "could not unmount datasets\n"), zpool_get_name(zhp));
1428 /* The history must be logged as part of the export */
1429 log_history = B_FALSE;
1431 ret = (zpool_destroy(zhp, history_str) != 0);
1438 typedef struct export_cbdata {
1440 boolean_t hardforce;
1447 zpool_export_one(zpool_handle_t *zhp, void *data)
1449 export_cbdata_t *cb = data;
1451 if (zpool_disable_datasets(zhp, cb->force) != 0)
1454 /* The history must be logged as part of the export */
1455 log_history = B_FALSE;
1457 if (cb->hardforce) {
1458 if (zpool_export_force(zhp, history_str) != 0)
1460 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1468 * zpool export [-f] <pool> ...
1470 * -a Export all pools
1471 * -f Forcefully unmount datasets
1473 * Export the given pools. By default, the command will attempt to cleanly
1474 * unmount any active datasets within the pool. If the '-f' flag is specified,
1475 * then the datasets will be forcefully unmounted.
1478 zpool_do_export(int argc, char **argv)
1481 boolean_t do_all = B_FALSE;
1482 boolean_t force = B_FALSE;
1483 boolean_t hardforce = B_FALSE;
1487 while ((c = getopt(argc, argv, "afF")) != -1) {
1499 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1506 cb.hardforce = hardforce;
1512 (void) fprintf(stderr, gettext("too many arguments\n"));
1516 return (for_each_pool(argc, argv, B_TRUE, NULL,
1517 zpool_export_one, &cb));
1520 /* check arguments */
1522 (void) fprintf(stderr, gettext("missing pool argument\n"));
1526 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1532 * Given a vdev configuration, determine the maximum width needed for the device
1536 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1544 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1545 if (strlen(name) + depth > max)
1546 max = strlen(name) + depth;
1550 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1551 &child, &children) == 0) {
1552 for (c = 0; c < children; c++)
1553 if ((ret = max_width(zhp, child[c], depth + 2,
1554 max, name_flags)) > max)
1558 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1559 &child, &children) == 0) {
1560 for (c = 0; c < children; c++)
1561 if ((ret = max_width(zhp, child[c], depth + 2,
1562 max, name_flags)) > max)
1566 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1567 &child, &children) == 0) {
1568 for (c = 0; c < children; c++)
1569 if ((ret = max_width(zhp, child[c], depth + 2,
1570 max, name_flags)) > max)
1577 typedef struct spare_cbdata {
1579 zpool_handle_t *cb_zhp;
1583 find_vdev(nvlist_t *nv, uint64_t search)
1589 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1593 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1594 &child, &children) == 0) {
1595 for (c = 0; c < children; c++)
1596 if (find_vdev(child[c], search))
1604 find_spare(zpool_handle_t *zhp, void *data)
1606 spare_cbdata_t *cbp = data;
1607 nvlist_t *config, *nvroot;
1609 config = zpool_get_config(zhp, NULL);
1610 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1613 if (find_vdev(nvroot, cbp->cb_guid)) {
1622 typedef struct status_cbdata {
1626 boolean_t cb_allpools;
1627 boolean_t cb_verbose;
1628 boolean_t cb_explain;
1630 boolean_t cb_dedup_stats;
1631 boolean_t cb_print_status;
1632 vdev_cmd_data_list_t *vcdl;
1635 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1637 is_blank_str(char *str)
1639 while (str != NULL && *str != '\0') {
1647 /* Print command output lines for specific vdev in a specific pool */
1649 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1651 vdev_cmd_data_t *data;
1655 for (i = 0; i < vcdl->count; i++) {
1656 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1657 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1658 /* Not the vdev we're looking for */
1662 data = &vcdl->data[i];
1663 /* Print out all the output values for this vdev */
1664 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1666 /* Does this vdev have values for this column? */
1667 for (int k = 0; k < data->cols_cnt; k++) {
1668 if (strcmp(data->cols[k],
1669 vcdl->uniq_cols[j]) == 0) {
1670 /* yes it does, record the value */
1671 val = data->lines[k];
1676 * Mark empty values with dashes to make output
1679 if (is_blank_str(val))
1682 printf("%*s", vcdl->uniq_cols_width[j], val);
1683 if (j < vcdl->uniq_cols_cnt - 1)
1687 /* Print out any values that aren't in a column at the end */
1688 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1689 /* Did we have any columns? If so print a spacer. */
1690 if (vcdl->uniq_cols_cnt > 0)
1693 val = data->lines[j];
1694 printf("%s", val ? val : "");
1701 * Print out configuration state as requested by status_callback.
1704 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1705 nvlist_t *nv, int depth, boolean_t isspare)
1709 pool_scan_stat_t *ps = NULL;
1711 char rbuf[6], wbuf[6], cbuf[6];
1713 uint64_t notpresent;
1714 spare_cbdata_t spare_cb;
1719 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1720 &child, &children) != 0)
1723 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1724 (uint64_t **)&vs, &c) == 0);
1726 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1728 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1731 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1734 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1737 if (vs->vs_aux == VDEV_AUX_SPARED)
1739 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1743 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1747 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1748 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1749 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1750 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1753 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1754 ¬present) == 0) {
1755 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1756 (void) printf(" was %s", path);
1757 } else if (vs->vs_aux != 0) {
1760 switch (vs->vs_aux) {
1761 case VDEV_AUX_OPEN_FAILED:
1762 (void) printf(gettext("cannot open"));
1765 case VDEV_AUX_BAD_GUID_SUM:
1766 (void) printf(gettext("missing device"));
1769 case VDEV_AUX_NO_REPLICAS:
1770 (void) printf(gettext("insufficient replicas"));
1773 case VDEV_AUX_VERSION_NEWER:
1774 (void) printf(gettext("newer version"));
1777 case VDEV_AUX_UNSUP_FEAT:
1778 (void) printf(gettext("unsupported feature(s)"));
1781 case VDEV_AUX_SPARED:
1782 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1783 &spare_cb.cb_guid) == 0);
1784 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1785 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1786 zpool_get_name(zhp)) == 0)
1787 (void) printf(gettext("currently in "
1790 (void) printf(gettext("in use by "
1792 zpool_get_name(spare_cb.cb_zhp));
1793 zpool_close(spare_cb.cb_zhp);
1795 (void) printf(gettext("currently in use"));
1799 case VDEV_AUX_ERR_EXCEEDED:
1800 (void) printf(gettext("too many errors"));
1803 case VDEV_AUX_IO_FAILURE:
1804 (void) printf(gettext("experienced I/O failures"));
1807 case VDEV_AUX_BAD_LOG:
1808 (void) printf(gettext("bad intent log"));
1811 case VDEV_AUX_EXTERNAL:
1812 (void) printf(gettext("external device fault"));
1815 case VDEV_AUX_SPLIT_POOL:
1816 (void) printf(gettext("split into new pool"));
1819 case VDEV_AUX_ACTIVE:
1820 (void) printf(gettext("currently in use"));
1823 case VDEV_AUX_CHILDREN_OFFLINE:
1824 (void) printf(gettext("all children offline"));
1828 (void) printf(gettext("corrupted data"));
1833 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1834 (uint64_t **)&ps, &c);
1836 if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1837 vs->vs_scan_processed != 0 && children == 0) {
1838 (void) printf(gettext(" (%s)"),
1839 (ps->pss_func == POOL_SCAN_RESILVER) ?
1840 "resilvering" : "repairing");
1843 if (cb->vcdl != NULL) {
1844 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1846 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1850 (void) printf("\n");
1852 for (c = 0; c < children; c++) {
1853 uint64_t islog = B_FALSE, ishole = B_FALSE;
1855 /* Don't print logs or holes here */
1856 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1858 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1860 if (islog || ishole)
1862 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1863 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1864 print_status_config(zhp, cb, vname, child[c], depth + 2,
1871 * Print the configuration of an exported pool. Iterate over all vdevs in the
1872 * pool, printing out the name and status for each one.
1875 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1883 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1884 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1885 strcmp(type, VDEV_TYPE_HOLE) == 0)
1888 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1889 (uint64_t **)&vs, &c) == 0);
1891 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1892 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1894 if (vs->vs_aux != 0) {
1897 switch (vs->vs_aux) {
1898 case VDEV_AUX_OPEN_FAILED:
1899 (void) printf(gettext("cannot open"));
1902 case VDEV_AUX_BAD_GUID_SUM:
1903 (void) printf(gettext("missing device"));
1906 case VDEV_AUX_NO_REPLICAS:
1907 (void) printf(gettext("insufficient replicas"));
1910 case VDEV_AUX_VERSION_NEWER:
1911 (void) printf(gettext("newer version"));
1914 case VDEV_AUX_UNSUP_FEAT:
1915 (void) printf(gettext("unsupported feature(s)"));
1918 case VDEV_AUX_ERR_EXCEEDED:
1919 (void) printf(gettext("too many errors"));
1922 case VDEV_AUX_ACTIVE:
1923 (void) printf(gettext("currently in use"));
1926 case VDEV_AUX_CHILDREN_OFFLINE:
1927 (void) printf(gettext("all children offline"));
1931 (void) printf(gettext("corrupted data"));
1935 (void) printf("\n");
1937 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1938 &child, &children) != 0)
1941 for (c = 0; c < children; c++) {
1942 uint64_t is_log = B_FALSE;
1944 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1949 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1950 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1951 print_import_config(cb, vname, child[c], depth + 2);
1955 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1956 &child, &children) == 0) {
1957 (void) printf(gettext("\tcache\n"));
1958 for (c = 0; c < children; c++) {
1959 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1961 (void) printf("\t %s\n", vname);
1966 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1967 &child, &children) == 0) {
1968 (void) printf(gettext("\tspares\n"));
1969 for (c = 0; c < children; c++) {
1970 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1972 (void) printf("\t %s\n", vname);
1980 * Logs are recorded as top level vdevs in the main pool child array
1981 * but with "is_log" set to 1. We use either print_status_config() or
1982 * print_import_config() to print the top level logs then any log
1983 * children (eg mirrored slogs) are printed recursively - which
1984 * works because only the top level vdev is marked "is_log"
1987 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1992 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1996 (void) printf(gettext("\tlogs\n"));
1998 for (c = 0; c < children; c++) {
1999 uint64_t is_log = B_FALSE;
2002 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2006 name = zpool_vdev_name(g_zfs, zhp, child[c],
2007 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2008 if (cb->cb_print_status)
2009 print_status_config(zhp, cb, name, child[c], 2,
2012 print_import_config(cb, name, child[c], 2);
2018 * Display the status for the given pool.
2021 show_import(nvlist_t *config)
2023 uint64_t pool_state;
2027 uint64_t hostid = 0;
2029 char *hostname = "unknown";
2030 nvlist_t *nvroot, *nvinfo;
2031 zpool_status_t reason;
2032 zpool_errata_t errata;
2036 status_cbdata_t cb = { 0 };
2038 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2040 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2042 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2044 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2047 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2048 (uint64_t **)&vs, &vsc) == 0);
2049 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2051 reason = zpool_import_status(config, &msgid, &errata);
2053 (void) printf(gettext(" pool: %s\n"), name);
2054 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2055 (void) printf(gettext(" state: %s"), health);
2056 if (pool_state == POOL_STATE_DESTROYED)
2057 (void) printf(gettext(" (DESTROYED)"));
2058 (void) printf("\n");
2061 case ZPOOL_STATUS_MISSING_DEV_R:
2062 case ZPOOL_STATUS_MISSING_DEV_NR:
2063 case ZPOOL_STATUS_BAD_GUID_SUM:
2064 (void) printf(gettext(" status: One or more devices are "
2065 "missing from the system.\n"));
2068 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2069 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2070 (void) printf(gettext(" status: One or more devices contains "
2071 "corrupted data.\n"));
2074 case ZPOOL_STATUS_CORRUPT_DATA:
2076 gettext(" status: The pool data is corrupted.\n"));
2079 case ZPOOL_STATUS_OFFLINE_DEV:
2080 (void) printf(gettext(" status: One or more devices "
2081 "are offlined.\n"));
2084 case ZPOOL_STATUS_CORRUPT_POOL:
2085 (void) printf(gettext(" status: The pool metadata is "
2089 case ZPOOL_STATUS_VERSION_OLDER:
2090 (void) printf(gettext(" status: The pool is formatted using a "
2091 "legacy on-disk version.\n"));
2094 case ZPOOL_STATUS_VERSION_NEWER:
2095 (void) printf(gettext(" status: The pool is formatted using an "
2096 "incompatible version.\n"));
2099 case ZPOOL_STATUS_FEAT_DISABLED:
2100 (void) printf(gettext(" status: Some supported features are "
2101 "not enabled on the pool.\n"));
2104 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2105 (void) printf(gettext("status: The pool uses the following "
2106 "feature(s) not supported on this system:\n"));
2107 zpool_print_unsup_feat(config);
2110 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2111 (void) printf(gettext("status: The pool can only be accessed "
2112 "in read-only mode on this system. It\n\tcannot be "
2113 "accessed in read-write mode because it uses the "
2114 "following\n\tfeature(s) not supported on this system:\n"));
2115 zpool_print_unsup_feat(config);
2118 case ZPOOL_STATUS_HOSTID_ACTIVE:
2119 (void) printf(gettext(" status: The pool is currently "
2120 "imported by another system.\n"));
2123 case ZPOOL_STATUS_HOSTID_REQUIRED:
2124 (void) printf(gettext(" status: The pool has the "
2125 "multihost property on. It cannot\n\tbe safely imported "
2126 "when the system hostid is not set.\n"));
2129 case ZPOOL_STATUS_HOSTID_MISMATCH:
2130 (void) printf(gettext(" status: The pool was last accessed by "
2131 "another system.\n"));
2134 case ZPOOL_STATUS_FAULTED_DEV_R:
2135 case ZPOOL_STATUS_FAULTED_DEV_NR:
2136 (void) printf(gettext(" status: One or more devices are "
2140 case ZPOOL_STATUS_BAD_LOG:
2141 (void) printf(gettext(" status: An intent log record cannot be "
2145 case ZPOOL_STATUS_RESILVERING:
2146 (void) printf(gettext(" status: One or more devices were being "
2150 case ZPOOL_STATUS_ERRATA:
2151 (void) printf(gettext(" status: Errata #%d detected.\n"),
2157 * No other status can be seen when importing pools.
2159 assert(reason == ZPOOL_STATUS_OK);
2163 * Print out an action according to the overall state of the pool.
2165 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2166 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2167 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2168 (void) printf(gettext(" action: The pool can be "
2169 "imported using its name or numeric identifier, "
2170 "though\n\tsome features will not be available "
2171 "without an explicit 'zpool upgrade'.\n"));
2172 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2173 (void) printf(gettext(" action: The pool can be "
2174 "imported using its name or numeric "
2175 "identifier and\n\tthe '-f' flag.\n"));
2176 } else if (reason == ZPOOL_STATUS_ERRATA) {
2178 case ZPOOL_ERRATA_NONE:
2181 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2182 (void) printf(gettext(" action: The pool can "
2183 "be imported using its name or numeric "
2184 "identifier,\n\thowever there is a compat"
2185 "ibility issue which should be corrected"
2186 "\n\tby running 'zpool scrub'\n"));
2189 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2190 (void) printf(gettext(" action: The pool can"
2191 "not be imported with this version of ZFS "
2192 "due to\n\tan active asynchronous destroy. "
2193 "Revert to an earlier version\n\tand "
2194 "allow the destroy to complete before "
2198 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2199 (void) printf(gettext(" action: Existing "
2200 "encrypted datasets contain an on-disk "
2201 "incompatibility, which\n\tneeds to be "
2202 "corrected. Backup these datasets to new "
2203 "encrypted datasets\n\tand destroy the "
2209 * All errata must contain an action message.
2214 (void) printf(gettext(" action: The pool can be "
2215 "imported using its name or numeric "
2218 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2219 (void) printf(gettext(" action: The pool can be imported "
2220 "despite missing or damaged devices. The\n\tfault "
2221 "tolerance of the pool may be compromised if imported.\n"));
2224 case ZPOOL_STATUS_VERSION_NEWER:
2225 (void) printf(gettext(" action: The pool cannot be "
2226 "imported. Access the pool on a system running "
2227 "newer\n\tsoftware, or recreate the pool from "
2230 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2231 (void) printf(gettext("action: The pool cannot be "
2232 "imported. Access the pool on a system that "
2233 "supports\n\tthe required feature(s), or recreate "
2234 "the pool from backup.\n"));
2236 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2237 (void) printf(gettext("action: The pool cannot be "
2238 "imported in read-write mode. Import the pool "
2240 "\t\"-o readonly=on\", access the pool on a system "
2241 "that supports the\n\trequired feature(s), or "
2242 "recreate the pool from backup.\n"));
2244 case ZPOOL_STATUS_MISSING_DEV_R:
2245 case ZPOOL_STATUS_MISSING_DEV_NR:
2246 case ZPOOL_STATUS_BAD_GUID_SUM:
2247 (void) printf(gettext(" action: The pool cannot be "
2248 "imported. Attach the missing\n\tdevices and try "
2251 case ZPOOL_STATUS_HOSTID_ACTIVE:
2252 VERIFY0(nvlist_lookup_nvlist(config,
2253 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2255 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2256 hostname = fnvlist_lookup_string(nvinfo,
2257 ZPOOL_CONFIG_MMP_HOSTNAME);
2259 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2260 hostid = fnvlist_lookup_uint64(nvinfo,
2261 ZPOOL_CONFIG_MMP_HOSTID);
2263 (void) printf(gettext(" action: The pool must be "
2264 "exported from %s (hostid=%lx)\n\tbefore it "
2265 "can be safely imported.\n"), hostname,
2266 (unsigned long) hostid);
2268 case ZPOOL_STATUS_HOSTID_REQUIRED:
2269 (void) printf(gettext(" action: Set a unique system "
2270 "hostid with the zgenhostid(8) command.\n"));
2273 (void) printf(gettext(" action: The pool cannot be "
2274 "imported due to damaged devices or data.\n"));
2278 /* Print the comment attached to the pool. */
2279 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2280 (void) printf(gettext("comment: %s\n"), comment);
2283 * If the state is "closed" or "can't open", and the aux state
2284 * is "corrupt data":
2286 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2287 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2288 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2289 if (pool_state == POOL_STATE_DESTROYED)
2290 (void) printf(gettext("\tThe pool was destroyed, "
2291 "but can be imported using the '-Df' flags.\n"));
2292 else if (pool_state != POOL_STATE_EXPORTED)
2293 (void) printf(gettext("\tThe pool may be active on "
2294 "another system, but can be imported using\n\t"
2295 "the '-f' flag.\n"));
2299 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2302 (void) printf(gettext(" config:\n\n"));
2304 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2306 if (cb.cb_namewidth < 10)
2307 cb.cb_namewidth = 10;
2309 print_import_config(&cb, name, nvroot, 0);
2310 if (num_logs(nvroot) > 0)
2311 print_logs(NULL, &cb, nvroot);
2313 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2314 (void) printf(gettext("\n\tAdditional devices are known to "
2315 "be part of this pool, though their\n\texact "
2316 "configuration cannot be determined.\n"));
2321 zfs_force_import_required(nvlist_t *config)
2324 uint64_t hostid = 0;
2327 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2328 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2330 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2333 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2334 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2335 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2336 ZPOOL_CONFIG_MMP_STATE);
2338 if (mmp_state != MMP_STATE_INACTIVE)
2346 * Perform the import for the given configuration. This passes the heavy
2347 * lifting off to zpool_import_props(), and then mounts the datasets contained
2351 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2352 nvlist_t *props, int flags)
2355 zpool_handle_t *zhp;
2359 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2360 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2362 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2363 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2364 "is formatted using an unsupported ZFS version\n"), name);
2366 } else if (zfs_force_import_required(config) &&
2367 !(flags & ZFS_IMPORT_ANY_HOST)) {
2368 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2371 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2372 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2373 mmp_state = fnvlist_lookup_uint64(nvinfo,
2374 ZPOOL_CONFIG_MMP_STATE);
2376 if (mmp_state == MMP_STATE_ACTIVE) {
2377 char *hostname = "<unknown>";
2378 uint64_t hostid = 0;
2380 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2381 hostname = fnvlist_lookup_string(nvinfo,
2382 ZPOOL_CONFIG_MMP_HOSTNAME);
2384 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2385 hostid = fnvlist_lookup_uint64(nvinfo,
2386 ZPOOL_CONFIG_MMP_HOSTID);
2388 (void) fprintf(stderr, gettext("cannot import '%s': "
2389 "pool is imported on %s (hostid: "
2390 "0x%lx)\nExport the pool on the other system, "
2391 "then run 'zpool import'.\n"),
2392 name, hostname, (unsigned long) hostid);
2393 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2394 (void) fprintf(stderr, gettext("Cannot import '%s': "
2395 "pool has the multihost property on and the\n"
2396 "system's hostid is not set. Set a unique hostid "
2397 "with the zgenhostid(8) command.\n"), name);
2399 char *hostname = "<unknown>";
2400 uint64_t timestamp = 0;
2401 uint64_t hostid = 0;
2403 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2404 hostname = fnvlist_lookup_string(config,
2405 ZPOOL_CONFIG_HOSTNAME);
2407 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2408 timestamp = fnvlist_lookup_uint64(config,
2409 ZPOOL_CONFIG_TIMESTAMP);
2411 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2412 hostid = fnvlist_lookup_uint64(config,
2413 ZPOOL_CONFIG_HOSTID);
2415 (void) fprintf(stderr, gettext("cannot import '%s': "
2416 "pool was previously in use from another system.\n"
2417 "Last accessed by %s (hostid=%lx) at %s"
2418 "The pool can be imported, use 'zpool import -f' "
2419 "to import the pool.\n"), name, hostname,
2420 (unsigned long)hostid, ctime((time_t *)×tamp));
2426 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2429 if (newname != NULL)
2430 name = (char *)newname;
2432 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2436 * Loading keys is best effort. We don't want to return immediately
2437 * if it fails but we do want to give the error to the caller.
2439 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2440 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2445 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2446 !(flags & ZFS_IMPORT_ONLY) &&
2447 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2457 * zpool import [-d dir] [-D]
2458 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2459 * [-d dir | -c cachefile] [-f] -a
2460 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2461 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2463 * -c Read pool information from a cachefile instead of searching
2466 * -d Scan in a specific directory, other than /dev/. More than
2467 * one directory can be specified using multiple '-d' options.
2469 * -D Scan for previously destroyed pools or import all or only
2470 * specified destroyed pools.
2472 * -R Temporarily import the pool, with all mountpoints relative to
2473 * the given root. The pool will remain exported when the machine
2476 * -V Import even in the presence of faulted vdevs. This is an
2477 * intentionally undocumented option for testing purposes, and
2478 * treats the pool configuration as complete, leaving any bad
2479 * vdevs in the FAULTED state. In other words, it does verbatim
2482 * -f Force import, even if it appears that the pool is active.
2484 * -F Attempt rewind if necessary.
2486 * -n See if rewind would work, but don't actually rewind.
2488 * -N Import the pool but don't mount datasets.
2490 * -T Specify a starting txg to use for import. This option is
2491 * intentionally undocumented option for testing purposes.
2493 * -a Import all pools found.
2495 * -l Load encryption keys while importing.
2497 * -o Set property=value and/or temporary mount options (without '=').
2499 * -s Scan using the default search path, the libblkid cache will
2502 * The import command scans for pools to import, and import pools based on pool
2503 * name and GUID. The pool can also be renamed as part of the import process.
2506 zpool_do_import(int argc, char **argv)
2508 char **searchdirs = NULL;
2509 char *env, *envdup = NULL;
2513 nvlist_t *pools = NULL;
2514 boolean_t do_all = B_FALSE;
2515 boolean_t do_destroyed = B_FALSE;
2516 char *mntopts = NULL;
2519 uint64_t searchguid = 0;
2520 char *searchname = NULL;
2522 nvlist_t *found_config;
2523 nvlist_t *policy = NULL;
2524 nvlist_t *props = NULL;
2526 int flags = ZFS_IMPORT_NORMAL;
2527 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2528 boolean_t dryrun = B_FALSE;
2529 boolean_t do_rewind = B_FALSE;
2530 boolean_t xtreme_rewind = B_FALSE;
2531 boolean_t do_scan = B_FALSE;
2532 uint64_t pool_state, txg = -1ULL;
2533 char *cachefile = NULL;
2534 importargs_t idata = { 0 };
2538 while ((c = getopt(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2547 if (searchdirs == NULL) {
2548 searchdirs = safe_malloc(sizeof (char *));
2550 char **tmp = safe_malloc((nsearch + 1) *
2552 bcopy(searchdirs, tmp, nsearch *
2557 searchdirs[nsearch++] = optarg;
2560 do_destroyed = B_TRUE;
2563 flags |= ZFS_IMPORT_ANY_HOST;
2569 flags |= ZFS_IMPORT_LOAD_KEYS;
2572 flags |= ZFS_IMPORT_MISSING_LOG;
2578 flags |= ZFS_IMPORT_ONLY;
2581 if ((propval = strchr(optarg, '=')) != NULL) {
2584 if (add_prop_list(optarg, propval,
2592 if (add_prop_list(zpool_prop_to_name(
2593 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2595 if (add_prop_list_default(zpool_prop_to_name(
2596 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2603 flags |= ZFS_IMPORT_TEMP_NAME;
2604 if (add_prop_list_default(zpool_prop_to_name(
2605 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2611 txg = strtoull(optarg, &endptr, 0);
2612 if (errno != 0 || *endptr != '\0') {
2613 (void) fprintf(stderr,
2614 gettext("invalid txg value\n"));
2617 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2620 flags |= ZFS_IMPORT_VERBATIM;
2623 xtreme_rewind = B_TRUE;
2626 (void) fprintf(stderr, gettext("missing argument for "
2627 "'%c' option\n"), optopt);
2631 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2640 if (cachefile && nsearch != 0) {
2641 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2645 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2646 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2650 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2651 (void) fprintf(stderr, gettext("-l is only meaningful during "
2656 if ((dryrun || xtreme_rewind) && !do_rewind) {
2657 (void) fprintf(stderr,
2658 gettext("-n or -X only meaningful with -F\n"));
2662 rewind_policy = ZPOOL_TRY_REWIND;
2664 rewind_policy = ZPOOL_DO_REWIND;
2666 rewind_policy |= ZPOOL_EXTREME_REWIND;
2668 /* In the future, we can capture further policy and include it here */
2669 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2670 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2671 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2674 /* check argument count */
2677 (void) fprintf(stderr, gettext("too many arguments\n"));
2682 (void) fprintf(stderr, gettext("too many arguments\n"));
2688 * Check for the effective uid. We do this explicitly here because
2689 * otherwise any attempt to discover pools will silently fail.
2691 if (argc == 0 && geteuid() != 0) {
2692 (void) fprintf(stderr, gettext("cannot "
2693 "discover pools: permission denied\n"));
2694 if (searchdirs != NULL)
2698 nvlist_free(policy);
2703 * Depending on the arguments given, we do one of the following:
2705 * <none> Iterate through all pools and display information about
2708 * -a Iterate through all pools and try to import each one.
2710 * <id> Find the pool that corresponds to the given GUID/pool
2711 * name and import that one.
2713 * -D Above options applies only to destroyed pools.
2719 searchguid = strtoull(argv[0], &endptr, 10);
2720 if (errno != 0 || *endptr != '\0') {
2721 searchname = argv[0];
2724 found_config = NULL;
2727 * User specified a name or guid. Ensure it's unique.
2729 idata.unique = B_TRUE;
2733 * Check the environment for the preferred search path.
2735 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2738 envdup = strdup(env);
2740 dir = strtok(envdup, ":");
2741 while (dir != NULL) {
2742 if (searchdirs == NULL) {
2743 searchdirs = safe_malloc(sizeof (char *));
2745 char **tmp = safe_malloc((nsearch + 1) *
2747 bcopy(searchdirs, tmp, nsearch *
2752 searchdirs[nsearch++] = dir;
2753 dir = strtok(NULL, ":");
2757 idata.path = searchdirs;
2758 idata.paths = nsearch;
2759 idata.poolname = searchname;
2760 idata.guid = searchguid;
2761 idata.cachefile = cachefile;
2762 idata.scan = do_scan;
2763 idata.policy = policy;
2765 pools = zpool_search_import(g_zfs, &idata);
2767 if (pools != NULL && idata.exists &&
2768 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2769 (void) fprintf(stderr, gettext("cannot import '%s': "
2770 "a pool with that name already exists\n"),
2772 (void) fprintf(stderr, gettext("use the form '%s "
2773 "<pool | id> <newpool>' to give it a new name\n"),
2776 } else if (pools == NULL && idata.exists) {
2777 (void) fprintf(stderr, gettext("cannot import '%s': "
2778 "a pool with that name is already created/imported,\n"),
2780 (void) fprintf(stderr, gettext("and no additional pools "
2781 "with that name were found\n"));
2783 } else if (pools == NULL) {
2785 (void) fprintf(stderr, gettext("cannot import '%s': "
2786 "no such pool available\n"), argv[0]);
2792 if (searchdirs != NULL)
2796 nvlist_free(policy);
2803 * At this point we have a list of import candidate configs. Even if
2804 * we were searching by pool name or guid, we still need to
2805 * post-process the list to deal with pool state and possible
2811 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2813 verify(nvpair_value_nvlist(elem, &config) == 0);
2815 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2817 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2819 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2822 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2829 (void) printf("\n");
2832 err |= do_import(config, NULL, mntopts,
2835 show_import(config);
2837 } else if (searchname != NULL) {
2841 * We are searching for a pool based on name.
2843 verify(nvlist_lookup_string(config,
2844 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2846 if (strcmp(name, searchname) == 0) {
2847 if (found_config != NULL) {
2848 (void) fprintf(stderr, gettext(
2849 "cannot import '%s': more than "
2850 "one matching pool\n"), searchname);
2851 (void) fprintf(stderr, gettext(
2852 "import by numeric ID instead\n"));
2855 found_config = config;
2861 * Search for a pool by guid.
2863 verify(nvlist_lookup_uint64(config,
2864 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2866 if (guid == searchguid)
2867 found_config = config;
2872 * If we were searching for a specific pool, verify that we found a
2873 * pool, and then do the import.
2875 if (argc != 0 && err == 0) {
2876 if (found_config == NULL) {
2877 (void) fprintf(stderr, gettext("cannot import '%s': "
2878 "no such pool available\n"), argv[0]);
2881 err |= do_import(found_config, argc == 1 ? NULL :
2882 argv[1], mntopts, props, flags);
2887 * If we were just looking for pools, report an error if none were
2890 if (argc == 0 && first)
2891 (void) fprintf(stderr,
2892 gettext("no pools available to import\n"));
2897 nvlist_free(policy);
2898 if (searchdirs != NULL)
2903 return (err ? 1 : 0);
2907 * zpool sync [-f] [pool] ...
2909 * -f (undocumented) force uberblock (and config including zpool cache file)
2912 * Sync the specified pool(s).
2913 * Without arguments "zpool sync" will sync all pools.
2914 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2918 zpool_do_sync(int argc, char **argv)
2921 boolean_t force = B_FALSE;
2924 while ((ret = getopt(argc, argv, "f")) != -1) {
2930 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2939 /* if argc == 0 we will execute zpool_sync_one on all pools */
2940 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2945 typedef struct iostat_cbdata {
2950 char **cb_vdev_names; /* Only show these vdevs */
2951 unsigned int cb_vdev_names_count;
2952 boolean_t cb_verbose;
2953 boolean_t cb_literal;
2954 boolean_t cb_scripted;
2955 zpool_list_t *cb_list;
2956 vdev_cmd_data_list_t *vcdl;
2960 typedef struct name_and_columns {
2961 const char *name; /* Column name */
2962 unsigned int columns; /* Center name to this number of columns */
2963 } name_and_columns_t;
2965 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2967 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2969 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2971 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2972 {"asyncq_wait", 2}, {"scrub"}},
2973 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2974 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2976 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2977 {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2978 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2979 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2983 /* Shorthand - if "columns" field not set, default to 1 column */
2984 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2986 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2988 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2989 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2990 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2991 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2992 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2993 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2994 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2995 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2998 static const char *histo_to_title[] = {
2999 [IOS_L_HISTO] = "latency",
3000 [IOS_RQ_HISTO] = "req_size",
3004 * Return the number of labels in a null-terminated name_and_columns_t
3009 label_array_len(const name_and_columns_t *labels)
3013 while (labels[i].name)
3020 * Return the number of strings in a null-terminated string array.
3023 * const char foo[] = {"bar", "baz", NULL}
3028 str_array_len(const char *array[])
3039 * Return a default column width for default/latency/queue columns. This does
3040 * not include histograms, which have their columns autosized.
3043 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
3045 unsigned long column_width = 5; /* Normal niceprint */
3046 static unsigned long widths[] = {
3048 * Choose some sane default column sizes for printing the
3051 [IOS_DEFAULT] = 15, /* 1PB capacity */
3052 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
3053 [IOS_QUEUES] = 6, /* 1M queue entries */
3057 column_width = widths[type];
3059 return (column_width);
3063 * Print the column labels, i.e:
3065 * capacity operations bandwidth
3066 * alloc free read write read write ...
3068 * If force_column_width is set, use it for the column width. If not set, use
3069 * the default column width.
3072 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
3073 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
3076 unsigned int text_start, rw_column_width, spaces_to_end;
3077 uint64_t flags = cb->cb_flags;
3079 unsigned int column_width = force_column_width;
3081 /* For each bit set in flags */
3082 for (f = flags; f; f &= ~(1ULL << idx)) {
3083 idx = lowbit64(f) - 1;
3084 if (!force_column_width)
3085 column_width = default_column_width(cb, idx);
3086 /* Print our top labels centered over "read write" label. */
3087 for (i = 0; i < label_array_len(labels[idx]); i++) {
3088 const char *name = labels[idx][i].name;
3090 * We treat labels[][].columns == 0 as shorthand
3091 * for one column. It makes writing out the label
3092 * tables more concise.
3094 unsigned int columns = MAX(1, labels[idx][i].columns);
3095 unsigned int slen = strlen(name);
3097 rw_column_width = (column_width * columns) +
3098 (2 * (columns - 1));
3100 text_start = (int)((rw_column_width)/columns -
3103 printf(" "); /* Two spaces between columns */
3105 /* Space from beginning of column to label */
3106 for (s = 0; s < text_start; s++)
3111 /* Print space after label to end of column */
3112 spaces_to_end = rw_column_width - text_start - slen;
3113 for (s = 0; s < spaces_to_end; s++)
3122 * print_cmd_columns - Print custom column titles from -c
3124 * If the user specified the "zpool status|iostat -c" then print their custom
3125 * column titles in the header. For example, print_cmd_columns() would print
3126 * the " col1 col2" part of this:
3128 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3130 * capacity operations bandwidth
3131 * pool alloc free read write read write col1 col2
3132 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3133 * mypool 269K 1008M 0 0 107 946
3134 * mirror 269K 1008M 0 0 107 946
3135 * sdb - - 0 0 102 473 val1 val2
3136 * sdc - - 0 0 5 473 val1 val2
3137 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3140 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3143 vdev_cmd_data_t *data = &vcdl->data[0];
3145 if (vcdl->count == 0 || data == NULL)
3149 * Each vdev cmd should have the same column names unless the user did
3150 * something weird with their cmd. Just take the column names from the
3151 * first vdev and assume it works for all of them.
3153 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3156 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3159 printf("%*s", vcdl->uniq_cols_width[i],
3160 vcdl->uniq_cols[i]);
3167 * Utility function to print out a line of dashes like:
3169 * -------------------------------- ----- ----- ----- ----- -----
3171 * ...or a dashed named-row line like:
3177 * @force_column_width If non-zero, use the value as the column width.
3178 * Otherwise use the default column widths.
3180 * @name: Print a dashed named-row line starting
3181 * with @name. Otherwise, print a regular
3185 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3189 unsigned int namewidth;
3190 uint64_t flags = cb->cb_flags;
3193 const name_and_columns_t *labels;
3197 if (cb->cb_flags & IOS_ANYHISTO_M) {
3198 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3199 } else if (cb->cb_vdev_names_count) {
3205 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3206 name ? strlen(name) : 0);
3210 printf("%-*s", namewidth, name);
3212 for (i = 0; i < namewidth; i++)
3216 /* For each bit in flags */
3217 for (f = flags; f; f &= ~(1ULL << idx)) {
3218 unsigned int column_width;
3219 idx = lowbit64(f) - 1;
3220 if (force_column_width)
3221 column_width = force_column_width;
3223 column_width = default_column_width(cb, idx);
3225 labels = iostat_bottom_labels[idx];
3226 for (i = 0; i < label_array_len(labels); i++) {
3228 printf(" %*s-", column_width - 1, " ");
3230 printf(" %.*s", column_width,
3231 "--------------------");
3238 print_iostat_separator_impl(iostat_cbdata_t *cb,
3239 unsigned int force_column_width)
3241 print_iostat_dashes(cb, force_column_width, NULL);
3245 print_iostat_separator(iostat_cbdata_t *cb)
3247 print_iostat_separator_impl(cb, 0);
3251 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3252 const char *histo_vdev_name)
3254 unsigned int namewidth;
3257 if (cb->cb_flags & IOS_ANYHISTO_M) {
3258 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3259 } else if (cb->cb_vdev_names_count) {
3265 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3266 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3268 if (histo_vdev_name)
3269 printf("%-*s", namewidth, histo_vdev_name);
3271 printf("%*s", namewidth, "");
3274 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3277 printf("%-*s", namewidth, title);
3279 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3280 if (cb->vcdl != NULL)
3281 print_cmd_columns(cb->vcdl, 0);
3285 print_iostat_separator_impl(cb, force_column_width);
3287 if (cb->vcdl != NULL)
3288 print_cmd_columns(cb->vcdl, 1);
3294 print_iostat_header(iostat_cbdata_t *cb)
3296 print_iostat_header_impl(cb, 0, NULL);
3301 * Display a single statistic.
3304 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3305 unsigned int column_size, boolean_t scripted)
3309 zfs_nicenum_format(value, buf, sizeof (buf), format);
3312 printf("\t%s", buf);
3314 printf(" %*s", column_size, buf);
3318 * Calculate the default vdev stats
3320 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3321 * stats into calcvs.
3324 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3325 vdev_stat_t *calcvs)
3329 memcpy(calcvs, newvs, sizeof (*calcvs));
3330 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3331 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3333 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3334 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3338 * Internal representation of the extended iostats data.
3340 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3341 * or single uint64_t's. We make both look like arrays to make them easier
3342 * to process. In order to make single uint64_t's look like arrays, we set
3343 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3344 * we can just use *data and count.
3348 uint_t count; /* Number of entries in data[] */
3349 uint64_t __data; /* Only used when data is a single uint64_t */
3353 stat_histo_max(struct stat_array *nva, unsigned int len)
3357 for (i = 0; i < len; i++)
3358 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3364 * Helper function to lookup a uint64_t array or uint64_t value and store its
3365 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3366 * it look like a one element array to make it easier to process.
3369 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3370 struct stat_array *nva)
3375 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3376 switch (nvpair_type(tmp)) {
3377 case DATA_TYPE_UINT64_ARRAY:
3378 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3380 case DATA_TYPE_UINT64:
3381 ret = nvpair_value_uint64(tmp, &nva->__data);
3382 nva->data = &nva->__data;
3386 /* Not a uint64_t */
3395 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3396 * subtract them, and return the results in a newly allocated stat_array.
3397 * You must free the returned array after you are done with it with
3398 * free_calc_stats().
3400 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3403 static struct stat_array *
3404 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3407 nvlist_t *oldnvx = NULL, *newnvx;
3408 struct stat_array *oldnva, *newnva, *calcnva;
3410 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3412 /* Extract our extended stats nvlist from the main list */
3413 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3416 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3420 newnva = safe_malloc(alloc_size);
3421 oldnva = safe_malloc(alloc_size);
3422 calcnva = safe_malloc(alloc_size);
3424 for (j = 0; j < len; j++) {
3425 verify(nvpair64_to_stat_array(newnvx, names[j],
3427 calcnva[j].count = newnva[j].count;
3428 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3429 calcnva[j].data = safe_malloc(alloc_size);
3430 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3433 verify(nvpair64_to_stat_array(oldnvx, names[j],
3435 for (i = 0; i < oldnva[j].count; i++)
3436 calcnva[j].data[i] -= oldnva[j].data[i];
3445 free_calc_stats(struct stat_array *nva, unsigned int len)
3448 for (i = 0; i < len; i++)
3455 print_iostat_histo(struct stat_array *nva, unsigned int len,
3456 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3462 enum zfs_nicenum_format format;
3463 unsigned int buckets;
3464 unsigned int start_bucket;
3467 format = ZFS_NICENUM_RAW;
3469 format = ZFS_NICENUM_1024;
3471 /* All these histos are the same size, so just use nva[0].count */
3472 buckets = nva[0].count;
3474 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3475 /* Start at 512 - req size should never be lower than this */
3481 for (j = start_bucket; j < buckets; j++) {
3482 /* Print histogram bucket label */
3483 if (cb->cb_flags & IOS_L_HISTO_M) {
3484 /* Ending range of this bucket */
3485 val = (1UL << (j + 1)) - 1;
3486 zfs_nicetime(val, buf, sizeof (buf));
3488 /* Request size (starting range of bucket) */
3490 zfs_nicenum(val, buf, sizeof (buf));
3493 if (cb->cb_scripted)
3494 printf("%llu", (u_longlong_t)val);
3496 printf("%-*s", namewidth, buf);
3498 /* Print the values on the line */
3499 for (i = 0; i < len; i++) {
3500 print_one_stat(nva[i].data[j] * scale, format,
3501 column_width, cb->cb_scripted);
3508 print_solid_separator(unsigned int length)
3516 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3517 nvlist_t *newnv, double scale, const char *name)
3519 unsigned int column_width;
3520 unsigned int namewidth;
3521 unsigned int entire_width;
3522 enum iostat_type type;
3523 struct stat_array *nva;
3525 unsigned int names_len;
3527 /* What type of histo are we? */
3528 type = IOS_HISTO_IDX(cb->cb_flags);
3530 /* Get NULL-terminated array of nvlist names for our histo */
3531 names = vsx_type_to_nvlist[type];
3532 names_len = str_array_len(names); /* num of names */
3534 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3536 if (cb->cb_literal) {
3537 column_width = MAX(5,
3538 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3543 namewidth = MAX(cb->cb_namewidth,
3544 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3547 * Calculate the entire line width of what we're printing. The
3548 * +2 is for the two spaces between columns:
3552 /* |___| <---------- column_width */
3554 /* |__________| <--- entire_width */
3556 entire_width = namewidth + (column_width + 2) *
3557 label_array_len(iostat_bottom_labels[type]);
3559 if (cb->cb_scripted)
3560 printf("%s\n", name);
3562 print_iostat_header_impl(cb, column_width, name);
3564 print_iostat_histo(nva, names_len, cb, column_width,
3567 free_calc_stats(nva, names_len);
3568 if (!cb->cb_scripted)
3569 print_solid_separator(entire_width);
3573 * Calculate the average latency of a power-of-two latency histogram
3576 single_histo_average(uint64_t *histo, unsigned int buckets)
3579 uint64_t count = 0, total = 0;
3581 for (i = 0; i < buckets; i++) {
3583 * Our buckets are power-of-two latency ranges. Use the
3584 * midpoint latency of each bucket to calculate the average.
3592 if (histo[i] != 0) {
3593 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3598 /* Prevent divide by zero */
3599 return (count == 0 ? 0 : total / count);
3603 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3604 nvlist_t *newnv, double scale)
3608 const char *names[] = {
3609 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3610 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3611 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3612 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3613 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3614 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3615 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3616 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3617 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3618 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3621 struct stat_array *nva;
3623 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3624 enum zfs_nicenum_format format;
3626 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3629 format = ZFS_NICENUM_RAW;
3631 format = ZFS_NICENUM_1024;
3633 for (i = 0; i < ARRAY_SIZE(names); i++) {
3634 val = nva[i].data[0] * scale;
3635 print_one_stat(val, format, column_width, cb->cb_scripted);
3638 free_calc_stats(nva, ARRAY_SIZE(names));
3642 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3643 nvlist_t *newnv, double scale)
3647 const char *names[] = {
3648 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3649 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3650 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3651 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3652 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3653 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3654 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3655 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3656 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3658 struct stat_array *nva;
3660 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3661 enum zfs_nicenum_format format;
3663 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3666 format = ZFS_NICENUM_RAWTIME;
3668 format = ZFS_NICENUM_TIME;
3670 /* Print our avg latencies on the line */
3671 for (i = 0; i < ARRAY_SIZE(names); i++) {
3672 /* Compute average latency for a latency histo */
3673 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3674 print_one_stat(val, format, column_width, cb->cb_scripted);
3676 free_calc_stats(nva, ARRAY_SIZE(names));
3680 * Print default statistics (capacity/operations/bandwidth)
3683 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3685 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3686 enum zfs_nicenum_format format;
3687 char na; /* char to print for "not applicable" values */
3689 if (cb->cb_literal) {
3690 format = ZFS_NICENUM_RAW;
3693 format = ZFS_NICENUM_1024;
3697 /* only toplevel vdevs have capacity stats */
3698 if (vs->vs_space == 0) {
3699 if (cb->cb_scripted)
3700 printf("\t%c\t%c", na, na);
3702 printf(" %*c %*c", column_width, na, column_width,
3705 print_one_stat(vs->vs_alloc, format, column_width,
3707 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3708 column_width, cb->cb_scripted);
3711 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3712 format, column_width, cb->cb_scripted);
3713 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3714 format, column_width, cb->cb_scripted);
3715 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3716 format, column_width, cb->cb_scripted);
3717 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3718 format, column_width, cb->cb_scripted);
3722 * Print out all the statistics for the given vdev. This can either be the
3723 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3724 * is a verbose output, and we don't want to display the toplevel pool stats.
3726 * Returns the number of stat lines printed.
3729 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3730 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3732 nvlist_t **oldchild, **newchild;
3733 uint_t c, children, oldchildren;
3734 vdev_stat_t *oldvs, *newvs, *calcvs;
3735 vdev_stat_t zerovs = { 0 };
3742 calcvs = safe_malloc(sizeof (*calcvs));
3744 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3747 if (oldnv != NULL) {
3748 verify(nvlist_lookup_uint64_array(oldnv,
3749 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3754 /* Do we only want to see a specific vdev? */
3755 for (i = 0; i < cb->cb_vdev_names_count; i++) {
3756 /* Yes we do. Is this the vdev? */
3757 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3759 * This is our vdev. Since it is the only vdev we
3760 * will be displaying, make depth = 0 so that it
3761 * doesn't get indented.
3768 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3769 /* Couldn't match the name */
3774 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3775 (uint64_t **)&newvs, &c) == 0);
3778 * Print the vdev name unless it's is a histogram. Histograms
3779 * display the vdev name in the header itself.
3781 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3782 if (cb->cb_scripted) {
3785 if (strlen(name) + depth > cb->cb_namewidth)
3786 (void) printf("%*s%s", depth, "", name);
3788 (void) printf("%*s%s%*s", depth, "", name,
3789 (int)(cb->cb_namewidth - strlen(name) -
3794 /* Calculate our scaling factor */
3795 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3796 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3798 * If we specify printing histograms with no time interval, then
3799 * print the histogram numbers over the entire lifetime of the
3807 scale = (double)NANOSEC / tdelta;
3810 if (cb->cb_flags & IOS_DEFAULT_M) {
3811 calc_default_iostats(oldvs, newvs, calcvs);
3812 print_iostat_default(calcvs, cb, scale);
3814 if (cb->cb_flags & IOS_LATENCY_M)
3815 print_iostat_latency(cb, oldnv, newnv, scale);
3816 if (cb->cb_flags & IOS_QUEUES_M)
3817 print_iostat_queues(cb, oldnv, newnv, scale);
3818 if (cb->cb_flags & IOS_ANYHISTO_M) {
3820 print_iostat_histos(cb, oldnv, newnv, scale, name);
3823 if (cb->vcdl != NULL) {
3825 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3828 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3832 if (!(cb->cb_flags & IOS_ANYHISTO_M))
3841 if (!cb->cb_verbose)
3844 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3845 &newchild, &children) != 0)
3849 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3850 &oldchild, &oldchildren) != 0)
3853 children = MIN(oldchildren, children);
3856 for (c = 0; c < children; c++) {
3857 uint64_t ishole = B_FALSE, islog = B_FALSE;
3859 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3862 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3865 if (ishole || islog)
3868 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3870 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3871 newchild[c], cb, depth + 2);
3876 * Log device section
3879 if (num_logs(newnv) > 0) {
3880 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3881 !cb->cb_vdev_names) {
3882 print_iostat_dashes(cb, 0, "logs");
3886 for (c = 0; c < children; c++) {
3887 uint64_t islog = B_FALSE;
3888 (void) nvlist_lookup_uint64(newchild[c],
3889 ZPOOL_CONFIG_IS_LOG, &islog);
3892 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3894 ret += print_vdev_stats(zhp, vname, oldnv ?
3895 oldchild[c] : NULL, newchild[c],
3904 * Include level 2 ARC devices in iostat output
3906 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3907 &newchild, &children) != 0)
3911 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3912 &oldchild, &oldchildren) != 0)
3915 children = MIN(oldchildren, children);
3919 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3920 !cb->cb_vdev_names) {
3921 print_iostat_dashes(cb, 0, "cache");
3925 for (c = 0; c < children; c++) {
3926 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3928 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3929 : NULL, newchild[c], cb, depth + 2);
3938 refresh_iostat(zpool_handle_t *zhp, void *data)
3940 iostat_cbdata_t *cb = data;
3944 * If the pool has disappeared, remove it from the list and continue.
3946 if (zpool_refresh_stats(zhp, &missing) != 0)
3950 pool_list_remove(cb->cb_list, zhp);
3956 * Callback to print out the iostats for the given pool.
3959 print_iostat(zpool_handle_t *zhp, void *data)
3961 iostat_cbdata_t *cb = data;
3962 nvlist_t *oldconfig, *newconfig;
3963 nvlist_t *oldnvroot, *newnvroot;
3966 newconfig = zpool_get_config(zhp, &oldconfig);
3968 if (cb->cb_iteration == 1)
3971 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3974 if (oldconfig == NULL)
3977 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3980 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3982 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3983 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3984 print_iostat_separator(cb);
3985 if (cb->vcdl != NULL) {
3986 print_cmd_columns(cb->vcdl, 1);
4001 if (isatty(STDOUT_FILENO)) {
4002 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
4004 columns = ws.ws_col;
4013 get_namewidth(zpool_handle_t *zhp, void *data)
4015 iostat_cbdata_t *cb = data;
4016 nvlist_t *config, *nvroot;
4019 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
4020 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4022 unsigned int poolname_len = strlen(zpool_get_name(zhp));
4023 if (!cb->cb_verbose)
4024 cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
4026 cb->cb_namewidth = MAX(poolname_len,
4027 max_width(zhp, nvroot, 0, cb->cb_namewidth,
4028 cb->cb_name_flags));
4031 * The width must be at least 10, but may be as large as the
4032 * column width - 42 so that we can still fit in one line.
4034 columns = get_columns();
4036 if (cb->cb_namewidth < 10)
4037 cb->cb_namewidth = 10;
4038 if (cb->cb_namewidth > columns - 42)
4039 cb->cb_namewidth = columns - 42;
4045 * Parse the input string, get the 'interval' and 'count' value if there is one.
4048 get_interval_count(int *argcp, char **argv, float *iv,
4052 unsigned long count = 0;
4056 * Determine if the last argument is an integer or a pool name
4058 if (argc > 0 && isnumber(argv[argc - 1])) {
4062 interval = strtof(argv[argc - 1], &end);
4064 if (*end == '\0' && errno == 0) {
4065 if (interval == 0) {
4066 (void) fprintf(stderr, gettext("interval "
4067 "cannot be zero\n"));
4071 * Ignore the last parameter
4076 * If this is not a valid number, just plow on. The
4077 * user will get a more informative error message later
4085 * If the last argument is also an integer, then we have both a count
4088 if (argc > 0 && isnumber(argv[argc - 1])) {
4093 interval = strtof(argv[argc - 1], &end);
4095 if (*end == '\0' && errno == 0) {
4096 if (interval == 0) {
4097 (void) fprintf(stderr, gettext("interval "
4098 "cannot be zero\n"));
4103 * Ignore the last parameter
4117 get_timestamp_arg(char c)
4120 timestamp_fmt = UDATE;
4122 timestamp_fmt = DDATE;
4128 * Return stat flags that are supported by all pools by both the module and
4129 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4130 * It will get ANDed down until only the flags that are supported on all pools
4134 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4136 uint64_t *mask = data;
4137 nvlist_t *config, *nvroot, *nvx;
4141 config = zpool_get_config(zhp, NULL);
4142 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4145 /* Default stats are always supported, but for completeness.. */
4146 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4147 flags |= IOS_DEFAULT_M;
4149 /* Get our extended stats nvlist from the main list */
4150 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4153 * No extended stats; they're probably running an older
4154 * module. No big deal, we support that too.
4159 /* For each extended stat, make sure all its nvpairs are supported */
4160 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4161 if (!vsx_type_to_nvlist[j][0])
4164 /* Start off by assuming the flag is supported, then check */
4165 flags |= (1ULL << j);
4166 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4167 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4168 /* flag isn't supported */
4169 flags = flags & ~(1ULL << j);
4175 *mask = *mask & flags;
4180 * Return a bitmask of stats that are supported on all pools by both the module
4184 get_stat_flags(zpool_list_t *list)
4189 * get_stat_flags_cb() will lop off bits from "mask" until only the
4190 * flags that are supported on all pools remain.
4192 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4197 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4200 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4202 iostat_cbdata_t *cb = cb_data;
4206 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4208 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4209 ret = 1; /* match */
4216 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4219 is_vdev(zpool_handle_t *zhp, void *cb_data)
4221 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4225 * Check if vdevs are in a pool
4227 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4228 * return 0. If pool_name is NULL, then search all pools.
4231 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4232 iostat_cbdata_t *cb)
4239 if ((argc == 0) || !*argv)
4245 /* Temporarily hijack cb_vdev_names for a second... */
4246 tmp_name = cb->cb_vdev_names;
4248 /* Go though our list of prospective vdev names */
4249 for (i = 0; i < argc; i++) {
4250 cb->cb_vdev_names = argv + i;
4252 /* Is this name a vdev in our pools? */
4253 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4261 cb->cb_vdev_names = tmp_name;
4267 is_pool_cb(zpool_handle_t *zhp, void *data)
4270 if (strcmp(name, zpool_get_name(zhp)) == 0)
4277 * Do we have a pool named *name? If so, return 1, otherwise 0.
4282 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4285 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4287 are_all_pools(int argc, char **argv)
4289 if ((argc == 0) || !*argv)
4293 if (!is_pool(argv[argc]))
4300 * Helper function to print out vdev/pool names we can't resolve. Used for an
4304 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4305 iostat_cbdata_t *cb)
4310 for (i = 0; i < argc; i++) {
4314 str = gettext("pool");
4315 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4316 str = gettext("vdev in this pool");
4317 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4318 str = gettext("vdev in another pool");
4320 str = gettext("unknown");
4322 fprintf(stderr, "\t%s (%s)\n", name, str);
4327 * Same as get_interval_count(), but with additional checks to not misinterpret
4328 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4332 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4333 unsigned long *count, iostat_cbdata_t *cb)
4335 char **tmpargv = argv;
4336 int argc_for_interval = 0;
4338 /* Is the last arg an interval value? Or a guid? */
4339 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4341 * The last arg is not a guid, so it's probably an
4344 argc_for_interval++;
4347 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4349 * The 2nd to last arg is not a guid, so it's probably
4350 * an interval value.
4352 argc_for_interval++;
4356 /* Point to our list of possible intervals */
4357 tmpargv = &argv[*argc - argc_for_interval];
4359 *argc = *argc - argc_for_interval;
4360 get_interval_count(&argc_for_interval, tmpargv,
4365 * Floating point sleep(). Allows you to pass in a floating point value for
4371 struct timespec req;
4372 req.tv_sec = floor(sec);
4373 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4374 nanosleep(&req, NULL);
4378 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4381 * name: Short name of the script ('iostat').
4382 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4385 print_zpool_script_help(char *name, char *path)
4387 char *argv[] = {path, "-h", NULL};
4388 char **lines = NULL;
4392 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4394 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4396 libzfs_free_str_array(lines, lines_cnt);
4400 for (int i = 0; i < lines_cnt; i++)
4401 if (!is_blank_str(lines[i]))
4402 printf(" %-14s %s\n", name, lines[i]);
4404 libzfs_free_str_array(lines, lines_cnt);
4408 * Go though the zpool status/iostat -c scripts in the user's path, run their
4409 * help option (-h), and print out the results.
4412 print_zpool_dir_scripts(char *dirpath)
4416 char fullpath[MAXPATHLEN];
4417 struct stat dir_stat;
4419 if ((dir = opendir(dirpath)) != NULL) {
4420 /* print all the files and directories within directory */
4421 while ((ent = readdir(dir)) != NULL) {
4422 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4424 /* Print the scripts */
4425 if (stat(fullpath, &dir_stat) == 0)
4426 if (dir_stat.st_mode & S_IXUSR &&
4427 S_ISREG(dir_stat.st_mode))
4428 print_zpool_script_help(ent->d_name,
4436 * Print out help text for all zpool status/iostat -c scripts.
4439 print_zpool_script_list(char *subcommand)
4443 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4445 sp = zpool_get_cmd_search_path();
4449 dir = strtok(sp, ":");
4450 while (dir != NULL) {
4451 print_zpool_dir_scripts(dir);
4452 dir = strtok(NULL, ":");
4459 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4460 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4461 * [interval [count]]
4463 * -c CMD For each vdev, run command CMD
4464 * -g Display guid for individual vdev name.
4465 * -L Follow links when resolving vdev path name.
4466 * -P Display full path for vdev name.
4467 * -v Display statistics for individual vdevs
4469 * -p Display values in parsable (exact) format.
4470 * -H Scripted mode. Don't display headers, and separate properties
4472 * -l Display average latency
4473 * -q Display queue depths
4474 * -w Display latency histograms
4475 * -r Display request size histogram
4476 * -T Display a timestamp in date(1) or Unix format
4478 * This command can be tricky because we want to be able to deal with pool
4479 * creation/destruction as well as vdev configuration changes. The bulk of this
4480 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4481 * on pool_list_update() to detect the addition of new pools. Configuration
4482 * changes are all handled within libzfs.
4485 zpool_do_iostat(int argc, char **argv)
4491 unsigned long count = 0;
4493 boolean_t verbose = B_FALSE;
4494 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4495 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4496 boolean_t omit_since_boot = B_FALSE;
4497 boolean_t guid = B_FALSE;
4498 boolean_t follow_links = B_FALSE;
4499 boolean_t full_name = B_FALSE;
4500 iostat_cbdata_t cb = { 0 };
4503 /* Used for printing error message */
4504 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4505 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4507 uint64_t unsupported_flags;
4510 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4515 gettext("Can't set -c flag twice\n"));
4519 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4520 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4521 fprintf(stderr, gettext(
4522 "Can't run -c, disabled by "
4523 "ZPOOL_SCRIPTS_ENABLED.\n"));
4527 if ((getuid() <= 0 || geteuid() <= 0) &&
4528 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4529 fprintf(stderr, gettext(
4530 "Can't run -c with root privileges "
4531 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4541 follow_links = B_TRUE;
4547 get_timestamp_arg(*optarg);
4571 omit_since_boot = B_TRUE;
4577 if (optopt == 'c') {
4578 print_zpool_script_list("iostat");
4582 gettext("invalid option '%c'\n"), optopt);
4591 cb.cb_literal = parsable;
4592 cb.cb_scripted = scripted;
4595 cb.cb_name_flags |= VDEV_NAME_GUID;
4597 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4599 cb.cb_name_flags |= VDEV_NAME_PATH;
4600 cb.cb_iteration = 0;
4601 cb.cb_namewidth = 0;
4602 cb.cb_verbose = verbose;
4604 /* Get our interval and count values (if any) */
4606 get_interval_count_filter_guids(&argc, argv, &interval,
4609 get_interval_count(&argc, argv, &interval, &count);
4613 /* No args, so just print the defaults. */
4614 } else if (are_all_pools(argc, argv)) {
4615 /* All the args are pool names */
4616 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4617 /* All the args are vdevs */
4618 cb.cb_vdev_names = argv;
4619 cb.cb_vdev_names_count = argc;
4620 argc = 0; /* No pools to process */
4621 } else if (are_all_pools(1, argv)) {
4622 /* The first arg is a pool name */
4623 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4624 /* ...and the rest are vdev names */
4625 cb.cb_vdev_names = argv + 1;
4626 cb.cb_vdev_names_count = argc - 1;
4627 argc = 1; /* One pool to process */
4629 fprintf(stderr, gettext("Expected either a list of "));
4630 fprintf(stderr, gettext("pools, or list of vdevs in"));
4631 fprintf(stderr, " \"%s\", ", argv[0]);
4632 fprintf(stderr, gettext("but got:\n"));
4633 error_list_unresolved_vdevs(argc - 1, argv + 1,
4635 fprintf(stderr, "\n");
4641 * The args don't make sense. The first arg isn't a pool name,
4642 * nor are all the args vdevs.
4644 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4645 fprintf(stderr, "\n");
4649 if (cb.cb_vdev_names_count != 0) {
4651 * If user specified vdevs, it implies verbose.
4653 cb.cb_verbose = B_TRUE;
4657 * Construct the list of all interesting pools.
4660 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4663 if (pool_list_count(list) == 0 && argc != 0) {
4664 pool_list_free(list);
4668 if (pool_list_count(list) == 0 && interval == 0) {
4669 pool_list_free(list);
4670 (void) fprintf(stderr, gettext("no pools available\n"));
4674 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4675 pool_list_free(list);
4676 (void) fprintf(stderr,
4677 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4682 if (l_histo && rq_histo) {
4683 pool_list_free(list);
4684 (void) fprintf(stderr,
4685 gettext("Only one of [-r|-w] can be passed at a time\n"));
4691 * Enter the main iostat loop.
4697 * Histograms tables look out of place when you try to display
4698 * them with the other stats, so make a rule that you can only
4699 * print histograms by themselves.
4701 cb.cb_flags = IOS_L_HISTO_M;
4702 } else if (rq_histo) {
4703 cb.cb_flags = IOS_RQ_HISTO_M;
4705 cb.cb_flags = IOS_DEFAULT_M;
4707 cb.cb_flags |= IOS_LATENCY_M;
4709 cb.cb_flags |= IOS_QUEUES_M;
4713 * See if the module supports all the stats we want to display.
4715 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4716 if (unsupported_flags) {
4720 gettext("The loaded zfs module doesn't support:"));
4722 /* for each bit set in unsupported_flags */
4723 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4724 idx = lowbit64(f) - 1;
4725 fprintf(stderr, " -%c", flag_to_arg[idx]);
4728 fprintf(stderr, ". Try running a newer module.\n");
4729 pool_list_free(list);
4735 if ((npools = pool_list_count(list)) == 0)
4736 (void) fprintf(stderr, gettext("no pools available\n"));
4739 * If this is the first iteration and -y was supplied
4740 * we skip any printing.
4742 boolean_t skip = (omit_since_boot &&
4743 cb.cb_iteration == 0);
4746 * Refresh all statistics. This is done as an
4747 * explicit step before calculating the maximum name
4748 * width, so that any * configuration changes are
4749 * properly accounted for.
4751 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4755 * Iterate over all pools to determine the maximum width
4756 * for the pool / device name column across all pools.
4758 cb.cb_namewidth = 0;
4759 (void) pool_list_iter(list, B_FALSE, get_namewidth,
4762 if (timestamp_fmt != NODATE)
4763 print_timestamp(timestamp_fmt);
4765 if (cmd != NULL && cb.cb_verbose &&
4766 !(cb.cb_flags & IOS_ANYHISTO_M)) {
4767 cb.vcdl = all_pools_for_each_vdev_run(argc,
4768 argv, cmd, g_zfs, cb.cb_vdev_names,
4769 cb.cb_vdev_names_count, cb.cb_name_flags);
4775 * If it's the first time and we're not skipping it,
4776 * or either skip or verbose mode, print the header.
4778 * The histogram code explicitly prints its header on
4779 * every vdev, so skip this for histograms.
4781 if (((++cb.cb_iteration == 1 && !skip) ||
4782 (skip != verbose)) &&
4783 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4785 print_iostat_header(&cb);
4788 (void) fsleep(interval);
4793 pool_list_iter(list, B_FALSE, print_iostat, &cb);
4796 * If there's more than one pool, and we're not in
4797 * verbose mode (which prints a separator for us),
4798 * then print a separator.
4800 * In addition, if we're printing specific vdevs then
4801 * we also want an ending separator.
4803 if (((npools > 1 && !verbose &&
4804 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4805 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4806 cb.cb_vdev_names_count)) &&
4808 print_iostat_separator(&cb);
4809 if (cb.vcdl != NULL)
4810 print_cmd_columns(cb.vcdl, 1);
4814 if (cb.vcdl != NULL)
4815 free_vdev_cmd_data_list(cb.vcdl);
4820 * Flush the output so that redirection to a file isn't buffered
4823 (void) fflush(stdout);
4828 if (count != 0 && --count == 0)
4831 (void) fsleep(interval);
4834 pool_list_free(list);
4839 typedef struct list_cbdata {
4840 boolean_t cb_verbose;
4843 boolean_t cb_scripted;
4844 zprop_list_t *cb_proplist;
4845 boolean_t cb_literal;
4849 * Given a list of columns to display, output appropriate headers for each one.
4852 print_header(list_cbdata_t *cb)
4854 zprop_list_t *pl = cb->cb_proplist;
4855 char headerbuf[ZPOOL_MAXPROPLEN];
4857 boolean_t first = B_TRUE;
4858 boolean_t right_justify;
4861 for (; pl != NULL; pl = pl->pl_next) {
4862 width = pl->pl_width;
4863 if (first && cb->cb_verbose) {
4865 * Reset the width to accommodate the verbose listing
4868 width = cb->cb_namewidth;
4876 right_justify = B_FALSE;
4877 if (pl->pl_prop != ZPROP_INVAL) {
4878 header = zpool_prop_column_name(pl->pl_prop);
4879 right_justify = zpool_prop_align_right(pl->pl_prop);
4883 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4884 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4885 headerbuf[i] = '\0';
4889 if (pl->pl_next == NULL && !right_justify)
4890 (void) printf("%s", header);
4891 else if (right_justify)
4892 (void) printf("%*s", (int)width, header);
4894 (void) printf("%-*s", (int)width, header);
4897 (void) printf("\n");
4901 * Given a pool and a list of properties, print out all the properties according
4902 * to the described layout.
4905 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4907 zprop_list_t *pl = cb->cb_proplist;
4908 boolean_t first = B_TRUE;
4909 char property[ZPOOL_MAXPROPLEN];
4911 boolean_t right_justify;
4914 for (; pl != NULL; pl = pl->pl_next) {
4916 width = pl->pl_width;
4917 if (first && cb->cb_verbose) {
4919 * Reset the width to accommodate the verbose listing
4922 width = cb->cb_namewidth;
4926 if (cb->cb_scripted)
4927 (void) printf("\t");
4934 right_justify = B_FALSE;
4935 if (pl->pl_prop != ZPROP_INVAL) {
4936 if (zpool_get_prop(zhp, pl->pl_prop, property,
4937 sizeof (property), NULL, cb->cb_literal) != 0)
4942 right_justify = zpool_prop_align_right(pl->pl_prop);
4943 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4944 zpool_prop_unsupported(pl->pl_user_prop)) &&
4945 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4946 sizeof (property)) == 0) {
4954 * If this is being called in scripted mode, or if this is the
4955 * last column and it is left-justified, don't include a width
4958 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4959 (void) printf("%s", propstr);
4960 else if (right_justify)
4961 (void) printf("%*s", (int)width, propstr);
4963 (void) printf("%-*s", (int)width, propstr);
4966 (void) printf("\n");
4970 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4971 boolean_t valid, enum zfs_nicenum_format format)
4975 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4978 case ZPOOL_PROP_EXPANDSZ:
4980 (void) strlcpy(propval, "-", sizeof (propval));
4982 zfs_nicenum_format(value, propval, sizeof (propval),
4985 case ZPOOL_PROP_FRAGMENTATION:
4986 if (value == ZFS_FRAG_INVALID) {
4987 (void) strlcpy(propval, "-", sizeof (propval));
4988 } else if (format == ZFS_NICENUM_RAW) {
4989 (void) snprintf(propval, sizeof (propval), "%llu",
4990 (unsigned long long)value);
4992 (void) snprintf(propval, sizeof (propval), "%llu%%",
4993 (unsigned long long)value);
4996 case ZPOOL_PROP_CAPACITY:
4997 if (format == ZFS_NICENUM_RAW)
4998 (void) snprintf(propval, sizeof (propval), "%llu",
4999 (unsigned long long)value);
5001 (void) snprintf(propval, sizeof (propval), "%llu%%",
5002 (unsigned long long)value);
5005 zfs_nicenum_format(value, propval, sizeof (propval), format);
5009 (void) strlcpy(propval, "-", sizeof (propval));
5012 (void) printf("\t%s", propval);
5014 (void) printf(" %*s", (int)width, propval);
5018 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
5019 list_cbdata_t *cb, int depth)
5025 boolean_t scripted = cb->cb_scripted;
5026 uint64_t islog = B_FALSE;
5027 boolean_t haslog = B_FALSE;
5028 char *dashes = "%-*s - - - - - -\n";
5030 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
5031 (uint64_t **)&vs, &c) == 0);
5034 boolean_t toplevel = (vs->vs_space != 0);
5036 enum zfs_nicenum_format format;
5039 format = ZFS_NICENUM_RAW;
5041 format = ZFS_NICENUM_1024;
5043 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
5047 (void) printf("\t%s", name);
5048 else if (strlen(name) + depth > cb->cb_namewidth)
5049 (void) printf("%*s%s", depth, "", name);
5051 (void) printf("%*s%s%*s", depth, "", name,
5052 (int)(cb->cb_namewidth - strlen(name) - depth), "");
5055 * Print the properties for the individual vdevs. Some
5056 * properties are only applicable to toplevel vdevs. The
5057 * 'toplevel' boolean value is passed to the print_one_column()
5058 * to indicate that the value is valid.
5060 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
5062 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
5064 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
5065 scripted, toplevel, format);
5066 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
5068 print_one_column(ZPOOL_PROP_FRAGMENTATION,
5069 vs->vs_fragmentation, scripted,
5070 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
5072 cap = (vs->vs_space == 0) ? 0 :
5073 (vs->vs_alloc * 100 / vs->vs_space);
5074 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
5076 (void) printf("\n");
5079 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
5080 &child, &children) != 0)
5083 for (c = 0; c < children; c++) {
5084 uint64_t ishole = B_FALSE;
5086 if (nvlist_lookup_uint64(child[c],
5087 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
5090 if (nvlist_lookup_uint64(child[c],
5091 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
5096 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5098 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5102 if (haslog == B_TRUE) {
5103 /* LINTED E_SEC_PRINTF_VAR_FMT */
5104 (void) printf(dashes, cb->cb_namewidth, "log");
5105 for (c = 0; c < children; c++) {
5106 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5107 &islog) != 0 || !islog)
5109 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5111 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5116 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5117 &child, &children) == 0 && children > 0) {
5118 /* LINTED E_SEC_PRINTF_VAR_FMT */
5119 (void) printf(dashes, cb->cb_namewidth, "cache");
5120 for (c = 0; c < children; c++) {
5121 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5123 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5128 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5129 &children) == 0 && children > 0) {
5130 /* LINTED E_SEC_PRINTF_VAR_FMT */
5131 (void) printf(dashes, cb->cb_namewidth, "spare");
5132 for (c = 0; c < children; c++) {
5133 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5135 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5143 * Generic callback function to list a pool.
5146 list_callback(zpool_handle_t *zhp, void *data)
5148 list_cbdata_t *cbp = data;
5152 config = zpool_get_config(zhp, NULL);
5154 print_pool(zhp, cbp);
5155 if (!cbp->cb_verbose)
5158 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5160 print_list_stats(zhp, NULL, nvroot, cbp, 0);
5166 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5168 * -g Display guid for individual vdev name.
5169 * -H Scripted mode. Don't display headers, and separate properties
5171 * -L Follow links when resolving vdev path name.
5172 * -o List of properties to display. Defaults to
5173 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5174 * "dedupratio,health,altroot"
5175 * -p Display values in parsable (exact) format.
5176 * -P Display full path for vdev name.
5177 * -T Display a timestamp in date(1) or Unix format
5179 * List all pools in the system, whether or not they're healthy. Output space
5180 * statistics for each one, as well as health status summary.
5183 zpool_do_list(int argc, char **argv)
5187 list_cbdata_t cb = { 0 };
5188 static char default_props[] =
5189 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5190 "dedupratio,health,altroot";
5191 char *props = default_props;
5193 unsigned long count = 0;
5195 boolean_t first = B_TRUE;
5198 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5201 cb.cb_name_flags |= VDEV_NAME_GUID;
5204 cb.cb_scripted = B_TRUE;
5207 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5213 cb.cb_name_flags |= VDEV_NAME_PATH;
5216 cb.cb_literal = B_TRUE;
5219 get_timestamp_arg(*optarg);
5222 cb.cb_verbose = B_TRUE;
5225 (void) fprintf(stderr, gettext("missing argument for "
5226 "'%c' option\n"), optopt);
5230 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5239 get_interval_count(&argc, argv, &interval, &count);
5241 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5245 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5249 if (pool_list_count(list) == 0)
5252 if (timestamp_fmt != NODATE)
5253 print_timestamp(timestamp_fmt);
5255 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5259 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5264 if (count != 0 && --count == 0)
5267 pool_list_free(list);
5268 (void) fsleep(interval);
5271 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5272 (void) printf(gettext("no pools available\n"));
5276 pool_list_free(list);
5277 zprop_free_list(cb.cb_proplist);
5282 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5284 boolean_t force = B_FALSE;
5287 char *poolname, *old_disk, *new_disk;
5288 zpool_handle_t *zhp;
5289 nvlist_t *props = NULL;
5294 while ((c = getopt(argc, argv, "fo:")) != -1) {
5300 if ((propval = strchr(optarg, '=')) == NULL) {
5301 (void) fprintf(stderr, gettext("missing "
5302 "'=' for -o option\n"));
5308 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5309 (add_prop_list(optarg, propval, &props, B_TRUE)))
5313 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5322 /* get pool name and check number of arguments */
5324 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5331 (void) fprintf(stderr,
5332 gettext("missing <device> specification\n"));
5340 (void) fprintf(stderr,
5341 gettext("missing <new_device> specification\n"));
5344 new_disk = old_disk;
5354 (void) fprintf(stderr, gettext("too many arguments\n"));
5358 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5363 if (zpool_get_config(zhp, NULL) == NULL) {
5364 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5371 /* unless manually specified use "ashift" pool property (if set) */
5372 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5375 char strval[ZPOOL_MAXPROPLEN];
5377 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5378 if (src != ZPROP_SRC_DEFAULT) {
5379 (void) sprintf(strval, "%" PRId32, intval);
5380 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5381 &props, B_TRUE) == 0);
5385 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5387 if (nvroot == NULL) {
5393 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5396 nvlist_free(nvroot);
5403 * zpool replace [-f] <pool> <device> <new_device>
5405 * -f Force attach, even if <new_device> appears to be in use.
5407 * Replace <device> with <new_device>.
5411 zpool_do_replace(int argc, char **argv)
5413 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5417 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5419 * -f Force attach, even if <new_device> appears to be in use.
5420 * -o Set property=value.
5422 * Attach <new_device> to the mirror containing <device>. If <device> is not
5423 * part of a mirror, then <device> will be transformed into a mirror of
5424 * <device> and <new_device>. In either case, <new_device> will begin life
5425 * with a DTL of [0, now], and will immediately begin to resilver itself.
5428 zpool_do_attach(int argc, char **argv)
5430 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5434 * zpool detach [-f] <pool> <device>
5436 * -f Force detach of <device>, even if DTLs argue against it
5437 * (not supported yet)
5439 * Detach a device from a mirror. The operation will be refused if <device>
5440 * is the last device in the mirror, or if the DTLs indicate that this device
5441 * has the only valid copy of some data.
5445 zpool_do_detach(int argc, char **argv)
5448 char *poolname, *path;
5449 zpool_handle_t *zhp;
5453 while ((c = getopt(argc, argv, "f")) != -1) {
5457 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5466 /* get pool name and check number of arguments */
5468 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5473 (void) fprintf(stderr,
5474 gettext("missing <device> specification\n"));
5481 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5484 ret = zpool_vdev_detach(zhp, path);
5492 * zpool split [-gLnP] [-o prop=val] ...
5494 * [-R altroot] <pool> <newpool> [<device> ...]
5496 * -g Display guid for individual vdev name.
5497 * -L Follow links when resolving vdev path name.
5498 * -n Do not split the pool, but display the resulting layout if
5499 * it were to be split.
5500 * -o Set property=value, or set mount options.
5501 * -P Display full path for vdev name.
5502 * -R Mount the split-off pool under an alternate root.
5503 * -l Load encryption keys while importing.
5505 * Splits the named pool and gives it the new pool name. Devices to be split
5506 * off may be listed, provided that no more than one device is specified
5507 * per top-level vdev mirror. The newly split pool is left in an exported
5508 * state unless -R is specified.
5510 * Restrictions: the top-level of the pool pool must only be made up of
5511 * mirrors; all devices in the pool must be healthy; no device may be
5512 * undergoing a resilvering operation.
5515 zpool_do_split(int argc, char **argv)
5517 char *srcpool, *newpool, *propval;
5518 char *mntopts = NULL;
5521 boolean_t loadkeys = B_FALSE;
5522 zpool_handle_t *zhp;
5523 nvlist_t *config, *props = NULL;
5525 flags.dryrun = B_FALSE;
5526 flags.import = B_FALSE;
5527 flags.name_flags = 0;
5530 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5533 flags.name_flags |= VDEV_NAME_GUID;
5536 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5539 flags.import = B_TRUE;
5541 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5542 &props, B_TRUE) != 0) {
5551 flags.dryrun = B_TRUE;
5554 if ((propval = strchr(optarg, '=')) != NULL) {
5557 if (add_prop_list(optarg, propval,
5558 &props, B_TRUE) != 0) {
5567 flags.name_flags |= VDEV_NAME_PATH;
5570 (void) fprintf(stderr, gettext("missing argument for "
5571 "'%c' option\n"), optopt);
5575 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5582 if (!flags.import && mntopts != NULL) {
5583 (void) fprintf(stderr, gettext("setting mntopts is only "
5584 "valid when importing the pool\n"));
5588 if (!flags.import && loadkeys) {
5589 (void) fprintf(stderr, gettext("loading keys is only "
5590 "valid when importing the pool\n"));
5598 (void) fprintf(stderr, gettext("Missing pool name\n"));
5602 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5612 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5617 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5618 if (config == NULL) {
5622 (void) printf(gettext("would create '%s' with the "
5623 "following layout:\n\n"), newpool);
5624 print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5631 if (ret != 0 || flags.dryrun || !flags.import) {
5632 nvlist_free(config);
5638 * The split was successful. Now we need to open the new
5639 * pool and import it.
5641 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5642 nvlist_free(config);
5648 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5653 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5654 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5656 (void) fprintf(stderr, gettext("Split was successful, but "
5657 "the datasets could not all be mounted\n"));
5658 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5659 "different altroot\n"), "zpool import");
5662 nvlist_free(config);
5671 * zpool online <pool> <device> ...
5674 zpool_do_online(int argc, char **argv)
5678 zpool_handle_t *zhp;
5680 vdev_state_t newstate;
5684 while ((c = getopt(argc, argv, "et")) != -1) {
5687 flags |= ZFS_ONLINE_EXPAND;
5691 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5700 /* get pool name and check number of arguments */
5702 (void) fprintf(stderr, gettext("missing pool name\n"));
5706 (void) fprintf(stderr, gettext("missing device name\n"));
5712 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5715 for (i = 1; i < argc; i++) {
5716 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5717 if (newstate != VDEV_STATE_HEALTHY) {
5718 (void) printf(gettext("warning: device '%s' "
5719 "onlined, but remains in faulted state\n"),
5721 if (newstate == VDEV_STATE_FAULTED)
5722 (void) printf(gettext("use 'zpool "
5723 "clear' to restore a faulted "
5726 (void) printf(gettext("use 'zpool "
5727 "replace' to replace devices "
5728 "that are no longer present\n"));
5741 * zpool offline [-ft] <pool> <device> ...
5743 * -f Force the device into a faulted state.
5745 * -t Only take the device off-line temporarily. The offline/faulted
5746 * state will not be persistent across reboots.
5750 zpool_do_offline(int argc, char **argv)
5754 zpool_handle_t *zhp;
5756 boolean_t istmp = B_FALSE;
5757 boolean_t fault = B_FALSE;
5760 while ((c = getopt(argc, argv, "ft")) != -1) {
5769 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5778 /* get pool name and check number of arguments */
5780 (void) fprintf(stderr, gettext("missing pool name\n"));
5784 (void) fprintf(stderr, gettext("missing device name\n"));
5790 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5793 for (i = 1; i < argc; i++) {
5795 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
5797 if (istmp == B_FALSE) {
5798 /* Force the fault to persist across imports */
5799 aux = VDEV_AUX_EXTERNAL_PERSIST;
5801 aux = VDEV_AUX_EXTERNAL;
5804 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
5807 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5818 * zpool clear <pool> [device]
5820 * Clear all errors associated with a pool or a particular device.
5823 zpool_do_clear(int argc, char **argv)
5827 boolean_t dryrun = B_FALSE;
5828 boolean_t do_rewind = B_FALSE;
5829 boolean_t xtreme_rewind = B_FALSE;
5830 uint32_t rewind_policy = ZPOOL_NO_REWIND;
5831 nvlist_t *policy = NULL;
5832 zpool_handle_t *zhp;
5833 char *pool, *device;
5836 while ((c = getopt(argc, argv, "FnX")) != -1) {
5845 xtreme_rewind = B_TRUE;
5848 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5858 (void) fprintf(stderr, gettext("missing pool name\n"));
5863 (void) fprintf(stderr, gettext("too many arguments\n"));
5867 if ((dryrun || xtreme_rewind) && !do_rewind) {
5868 (void) fprintf(stderr,
5869 gettext("-n or -X only meaningful with -F\n"));
5873 rewind_policy = ZPOOL_TRY_REWIND;
5875 rewind_policy = ZPOOL_DO_REWIND;
5877 rewind_policy |= ZPOOL_EXTREME_REWIND;
5879 /* In future, further rewind policy choices can be passed along here */
5880 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5881 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5885 device = argc == 2 ? argv[1] : NULL;
5887 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5888 nvlist_free(policy);
5892 if (zpool_clear(zhp, device, policy) != 0)
5897 nvlist_free(policy);
5903 * zpool reguid <pool>
5906 zpool_do_reguid(int argc, char **argv)
5910 zpool_handle_t *zhp;
5914 while ((c = getopt(argc, argv, "")) != -1) {
5917 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5926 /* get pool name and check number of arguments */
5928 (void) fprintf(stderr, gettext("missing pool name\n"));
5933 (void) fprintf(stderr, gettext("too many arguments\n"));
5938 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5941 ret = zpool_reguid(zhp);
5949 * zpool reopen <pool>
5951 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5954 zpool_do_reopen(int argc, char **argv)
5958 boolean_t scrub_restart = B_TRUE;
5961 while ((c = getopt(argc, argv, "n")) != -1) {
5964 scrub_restart = B_FALSE;
5967 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5976 /* if argc == 0 we will execute zpool_reopen_one on all pools */
5977 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
5983 typedef struct scrub_cbdata {
5987 pool_scrub_cmd_t cb_scrub_cmd;
5991 scrub_callback(zpool_handle_t *zhp, void *data)
5993 scrub_cbdata_t *cb = data;
5997 * Ignore faulted pools.
5999 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
6000 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
6001 "currently unavailable\n"), zpool_get_name(zhp));
6005 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
6011 * zpool scrub [-s | -p] <pool> ...
6013 * -s Stop. Stops any in-progress scrub.
6014 * -p Pause. Pause in-progress scrub.
6017 zpool_do_scrub(int argc, char **argv)
6022 cb.cb_type = POOL_SCAN_SCRUB;
6023 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6026 while ((c = getopt(argc, argv, "sp")) != -1) {
6029 cb.cb_type = POOL_SCAN_NONE;
6032 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
6035 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6041 if (cb.cb_type == POOL_SCAN_NONE &&
6042 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
6043 (void) fprintf(stderr, gettext("invalid option combination: "
6044 "-s and -p are mutually exclusive\n"));
6054 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6058 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6062 * Print out detailed scrub status.
6065 print_scan_status(pool_scan_stat_t *ps)
6067 time_t start, end, pause;
6068 uint64_t total_secs_left;
6069 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
6070 uint64_t pass_scanned, scanned, pass_issued, issued, total;
6071 uint64_t scan_rate, issue_rate;
6072 double fraction_done;
6073 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
6074 char srate_buf[7], irate_buf[7];
6076 (void) printf(gettext(" scan: "));
6078 /* If there's never been a scan, there's not much to say. */
6079 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
6080 ps->pss_func >= POOL_SCAN_FUNCS) {
6081 (void) printf(gettext("none requested\n"));
6085 start = ps->pss_start_time;
6086 end = ps->pss_end_time;
6087 pause = ps->pss_pass_scrub_pause;
6089 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6091 assert(ps->pss_func == POOL_SCAN_SCRUB ||
6092 ps->pss_func == POOL_SCAN_RESILVER);
6094 /* Scan is finished or canceled. */
6095 if (ps->pss_state == DSS_FINISHED) {
6096 total_secs_left = end - start;
6097 days_left = total_secs_left / 60 / 60 / 24;
6098 hours_left = (total_secs_left / 60 / 60) % 24;
6099 mins_left = (total_secs_left / 60) % 60;
6100 secs_left = (total_secs_left % 60);
6102 if (ps->pss_func == POOL_SCAN_SCRUB) {
6103 (void) printf(gettext("scrub repaired %s "
6104 "in %llu days %02llu:%02llu:%02llu "
6105 "with %llu errors on %s"), processed_buf,
6106 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6107 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6108 (u_longlong_t)ps->pss_errors, ctime(&end));
6109 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6110 (void) printf(gettext("resilvered %s "
6111 "in %llu days %02llu:%02llu:%02llu "
6112 "with %llu errors on %s"), processed_buf,
6113 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6114 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6115 (u_longlong_t)ps->pss_errors, ctime(&end));
6118 } else if (ps->pss_state == DSS_CANCELED) {
6119 if (ps->pss_func == POOL_SCAN_SCRUB) {
6120 (void) printf(gettext("scrub canceled on %s"),
6122 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6123 (void) printf(gettext("resilver canceled on %s"),
6129 assert(ps->pss_state == DSS_SCANNING);
6131 /* Scan is in progress. Resilvers can't be paused. */
6132 if (ps->pss_func == POOL_SCAN_SCRUB) {
6134 (void) printf(gettext("scrub in progress since %s"),
6137 (void) printf(gettext("scrub paused since %s"),
6139 (void) printf(gettext("\tscrub started on %s"),
6142 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6143 (void) printf(gettext("resilver in progress since %s"),
6147 scanned = ps->pss_examined;
6148 pass_scanned = ps->pss_pass_exam;
6149 issued = ps->pss_issued;
6150 pass_issued = ps->pss_pass_issued;
6151 total = ps->pss_to_examine;
6153 /* we are only done with a block once we have issued the IO for it */
6154 fraction_done = (double)issued / total;
6156 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6157 elapsed = time(NULL) - ps->pss_pass_start;
6158 elapsed -= ps->pss_pass_scrub_spent_paused;
6159 elapsed = (elapsed != 0) ? elapsed : 1;
6161 scan_rate = pass_scanned / elapsed;
6162 issue_rate = pass_issued / elapsed;
6163 total_secs_left = (issue_rate != 0) ?
6164 ((total - issued) / issue_rate) : UINT64_MAX;
6166 days_left = total_secs_left / 60 / 60 / 24;
6167 hours_left = (total_secs_left / 60 / 60) % 24;
6168 mins_left = (total_secs_left / 60) % 60;
6169 secs_left = (total_secs_left % 60);
6171 /* format all of the numbers we will be reporting */
6172 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
6173 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
6174 zfs_nicebytes(total, total_buf, sizeof (total_buf));
6175 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
6176 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
6178 /* do not print estimated time if we have a paused scrub */
6180 (void) printf(gettext("\t%s scanned at %s/s, "
6181 "%s issued at %s/s, %s total\n"),
6182 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
6184 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6185 scanned_buf, issued_buf, total_buf);
6188 if (ps->pss_func == POOL_SCAN_RESILVER) {
6189 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6190 processed_buf, 100 * fraction_done);
6191 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6192 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6193 processed_buf, 100 * fraction_done);
6197 if (issue_rate >= 10 * 1024 * 1024) {
6198 (void) printf(gettext(", %llu days "
6199 "%02llu:%02llu:%02llu to go\n"),
6200 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6201 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
6203 (void) printf(gettext(", no estimated "
6204 "completion time\n"));
6207 (void) printf(gettext("\n"));
6212 * Print out detailed removal status.
6215 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
6217 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
6219 nvlist_t *config, *nvroot;
6224 if (prs == NULL || prs->prs_state == DSS_NONE)
6228 * Determine name of vdev.
6230 config = zpool_get_config(zhp, NULL);
6231 nvroot = fnvlist_lookup_nvlist(config,
6232 ZPOOL_CONFIG_VDEV_TREE);
6233 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
6234 &child, &children) == 0);
6235 assert(prs->prs_removing_vdev < children);
6236 vdev_name = zpool_vdev_name(g_zfs, zhp,
6237 child[prs->prs_removing_vdev], B_TRUE);
6239 (void) printf(gettext("remove: "));
6241 start = prs->prs_start_time;
6242 end = prs->prs_end_time;
6243 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
6246 * Removal is finished or canceled.
6248 if (prs->prs_state == DSS_FINISHED) {
6249 uint64_t minutes_taken = (end - start) / 60;
6251 (void) printf(gettext("Removal of vdev %llu copied %s "
6252 "in %lluh%um, completed on %s"),
6253 (longlong_t)prs->prs_removing_vdev,
6255 (u_longlong_t)(minutes_taken / 60),
6256 (uint_t)(minutes_taken % 60),
6257 ctime((time_t *)&end));
6258 } else if (prs->prs_state == DSS_CANCELED) {
6259 (void) printf(gettext("Removal of %s canceled on %s"),
6260 vdev_name, ctime(&end));
6262 uint64_t copied, total, elapsed, mins_left, hours_left;
6263 double fraction_done;
6266 assert(prs->prs_state == DSS_SCANNING);
6269 * Removal is in progress.
6271 (void) printf(gettext(
6272 "Evacuation of %s in progress since %s"),
6273 vdev_name, ctime(&start));
6275 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
6276 total = prs->prs_to_copy;
6277 fraction_done = (double)copied / total;
6279 /* elapsed time for this pass */
6280 elapsed = time(NULL) - prs->prs_start_time;
6281 elapsed = elapsed > 0 ? elapsed : 1;
6282 rate = copied / elapsed;
6283 rate = rate > 0 ? rate : 1;
6284 mins_left = ((total - copied) / rate) / 60;
6285 hours_left = mins_left / 60;
6287 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
6288 zfs_nicenum(total, total_buf, sizeof (total_buf));
6289 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
6292 * do not print estimated time if hours_left is more than
6295 (void) printf(gettext(" %s copied out of %s at %s/s, "
6297 examined_buf, total_buf, rate_buf, 100 * fraction_done);
6298 if (hours_left < (30 * 24)) {
6299 (void) printf(gettext(", %lluh%um to go\n"),
6300 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
6302 (void) printf(gettext(
6303 ", (copy is slow, no estimated time)\n"));
6307 if (prs->prs_mapping_memory > 0) {
6309 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
6310 (void) printf(gettext(" %s memory used for "
6311 "removed device mappings\n"),
6317 print_error_log(zpool_handle_t *zhp)
6319 nvlist_t *nverrlist = NULL;
6322 size_t len = MAXPATHLEN * 2;
6324 if (zpool_get_errlog(zhp, &nverrlist) != 0)
6327 (void) printf("errors: Permanent errors have been "
6328 "detected in the following files:\n\n");
6330 pathname = safe_malloc(len);
6332 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6334 uint64_t dsobj, obj;
6336 verify(nvpair_value_nvlist(elem, &nv) == 0);
6337 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6339 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6341 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6342 (void) printf("%7s %s\n", "", pathname);
6345 nvlist_free(nverrlist);
6349 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6358 (void) printf(gettext("\tspares\n"));
6360 for (i = 0; i < nspares; i++) {
6361 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6363 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6369 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6378 (void) printf(gettext("\tcache\n"));
6380 for (i = 0; i < nl2cache; i++) {
6381 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6383 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6389 print_dedup_stats(nvlist_t *config)
6391 ddt_histogram_t *ddh;
6395 char dspace[6], mspace[6];
6398 * If the pool was faulted then we may not have been able to
6399 * obtain the config. Otherwise, if we have anything in the dedup
6400 * table continue processing the stats.
6402 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6403 (uint64_t **)&ddo, &c) != 0)
6406 (void) printf("\n");
6407 (void) printf(gettext(" dedup: "));
6408 if (ddo->ddo_count == 0) {
6409 (void) printf(gettext("no DDT entries\n"));
6413 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6414 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6415 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6416 (u_longlong_t)ddo->ddo_count,
6420 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6421 (uint64_t **)&dds, &c) == 0);
6422 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6423 (uint64_t **)&ddh, &c) == 0);
6424 zpool_dump_ddt(dds, ddh);
6428 * Display a summary of pool status. Displays a summary such as:
6432 * reason: One or more devices ...
6433 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6439 * When given the '-v' option, we print out the complete config. If the '-e'
6440 * option is specified, then we print out error rate information as well.
6443 status_callback(zpool_handle_t *zhp, void *data)
6445 status_cbdata_t *cbp = data;
6446 nvlist_t *config, *nvroot;
6448 zpool_status_t reason;
6449 zpool_errata_t errata;
6454 config = zpool_get_config(zhp, NULL);
6455 reason = zpool_get_status(zhp, &msgid, &errata);
6460 * If we were given 'zpool status -x', only report those pools with
6463 if (cbp->cb_explain &&
6464 (reason == ZPOOL_STATUS_OK ||
6465 reason == ZPOOL_STATUS_VERSION_OLDER ||
6466 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6467 if (!cbp->cb_allpools) {
6468 (void) printf(gettext("pool '%s' is healthy\n"),
6469 zpool_get_name(zhp));
6471 cbp->cb_first = B_FALSE;
6477 cbp->cb_first = B_FALSE;
6479 (void) printf("\n");
6481 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6482 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6483 (uint64_t **)&vs, &c) == 0);
6484 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6486 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
6487 (void) printf(gettext(" state: %s\n"), health);
6490 case ZPOOL_STATUS_MISSING_DEV_R:
6491 (void) printf(gettext("status: One or more devices could not "
6492 "be opened. Sufficient replicas exist for\n\tthe pool to "
6493 "continue functioning in a degraded state.\n"));
6494 (void) printf(gettext("action: Attach the missing device and "
6495 "online it using 'zpool online'.\n"));
6498 case ZPOOL_STATUS_MISSING_DEV_NR:
6499 (void) printf(gettext("status: One or more devices could not "
6500 "be opened. There are insufficient\n\treplicas for the "
6501 "pool to continue functioning.\n"));
6502 (void) printf(gettext("action: Attach the missing device and "
6503 "online it using 'zpool online'.\n"));
6506 case ZPOOL_STATUS_CORRUPT_LABEL_R:
6507 (void) printf(gettext("status: One or more devices could not "
6508 "be used because the label is missing or\n\tinvalid. "
6509 "Sufficient replicas exist for the pool to continue\n\t"
6510 "functioning in a degraded state.\n"));
6511 (void) printf(gettext("action: Replace the device using "
6512 "'zpool replace'.\n"));
6515 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6516 (void) printf(gettext("status: One or more devices could not "
6517 "be used because the label is missing \n\tor invalid. "
6518 "There are insufficient replicas for the pool to "
6519 "continue\n\tfunctioning.\n"));
6520 zpool_explain_recover(zpool_get_handle(zhp),
6521 zpool_get_name(zhp), reason, config);
6524 case ZPOOL_STATUS_FAILING_DEV:
6525 (void) printf(gettext("status: One or more devices has "
6526 "experienced an unrecoverable error. An\n\tattempt was "
6527 "made to correct the error. Applications are "
6529 (void) printf(gettext("action: Determine if the device needs "
6530 "to be replaced, and clear the errors\n\tusing "
6531 "'zpool clear' or replace the device with 'zpool "
6535 case ZPOOL_STATUS_OFFLINE_DEV:
6536 (void) printf(gettext("status: One or more devices has "
6537 "been taken offline by the administrator.\n\tSufficient "
6538 "replicas exist for the pool to continue functioning in "
6539 "a\n\tdegraded state.\n"));
6540 (void) printf(gettext("action: Online the device using "
6541 "'zpool online' or replace the device with\n\t'zpool "
6545 case ZPOOL_STATUS_REMOVED_DEV:
6546 (void) printf(gettext("status: One or more devices has "
6547 "been removed by the administrator.\n\tSufficient "
6548 "replicas exist for the pool to continue functioning in "
6549 "a\n\tdegraded state.\n"));
6550 (void) printf(gettext("action: Online the device using "
6551 "'zpool online' or replace the device with\n\t'zpool "
6555 case ZPOOL_STATUS_RESILVERING:
6556 (void) printf(gettext("status: One or more devices is "
6557 "currently being resilvered. The pool will\n\tcontinue "
6558 "to function, possibly in a degraded state.\n"));
6559 (void) printf(gettext("action: Wait for the resilver to "
6563 case ZPOOL_STATUS_CORRUPT_DATA:
6564 (void) printf(gettext("status: One or more devices has "
6565 "experienced an error resulting in data\n\tcorruption. "
6566 "Applications may be affected.\n"));
6567 (void) printf(gettext("action: Restore the file in question "
6568 "if possible. Otherwise restore the\n\tentire pool from "
6572 case ZPOOL_STATUS_CORRUPT_POOL:
6573 (void) printf(gettext("status: The pool metadata is corrupted "
6574 "and the pool cannot be opened.\n"));
6575 zpool_explain_recover(zpool_get_handle(zhp),
6576 zpool_get_name(zhp), reason, config);
6579 case ZPOOL_STATUS_VERSION_OLDER:
6580 (void) printf(gettext("status: The pool is formatted using a "
6581 "legacy on-disk format. The pool can\n\tstill be used, "
6582 "but some features are unavailable.\n"));
6583 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6584 "upgrade'. Once this is done, the\n\tpool will no longer "
6585 "be accessible on software that does not support\n\t"
6586 "feature flags.\n"));
6589 case ZPOOL_STATUS_VERSION_NEWER:
6590 (void) printf(gettext("status: The pool has been upgraded to a "
6591 "newer, incompatible on-disk version.\n\tThe pool cannot "
6592 "be accessed on this system.\n"));
6593 (void) printf(gettext("action: Access the pool from a system "
6594 "running more recent software, or\n\trestore the pool from "
6598 case ZPOOL_STATUS_FEAT_DISABLED:
6599 (void) printf(gettext("status: Some supported features are not "
6600 "enabled on the pool. The pool can\n\tstill be used, but "
6601 "some features are unavailable.\n"));
6602 (void) printf(gettext("action: Enable all features using "
6603 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6604 "longer be accessible by software that does not support\n\t"
6605 "the features. See zpool-features(5) for details.\n"));
6608 case ZPOOL_STATUS_UNSUP_FEAT_READ:
6609 (void) printf(gettext("status: The pool cannot be accessed on "
6610 "this system because it uses the\n\tfollowing feature(s) "
6611 "not supported on this system:\n"));
6612 zpool_print_unsup_feat(config);
6613 (void) printf("\n");
6614 (void) printf(gettext("action: Access the pool from a system "
6615 "that supports the required feature(s),\n\tor restore the "
6616 "pool from backup.\n"));
6619 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6620 (void) printf(gettext("status: The pool can only be accessed "
6621 "in read-only mode on this system. It\n\tcannot be "
6622 "accessed in read-write mode because it uses the "
6623 "following\n\tfeature(s) not supported on this system:\n"));
6624 zpool_print_unsup_feat(config);
6625 (void) printf("\n");
6626 (void) printf(gettext("action: The pool cannot be accessed in "
6627 "read-write mode. Import the pool with\n"
6628 "\t\"-o readonly=on\", access the pool from a system that "
6629 "supports the\n\trequired feature(s), or restore the "
6630 "pool from backup.\n"));
6633 case ZPOOL_STATUS_FAULTED_DEV_R:
6634 (void) printf(gettext("status: One or more devices are "
6635 "faulted in response to persistent errors.\n\tSufficient "
6636 "replicas exist for the pool to continue functioning "
6637 "in a\n\tdegraded state.\n"));
6638 (void) printf(gettext("action: Replace the faulted device, "
6639 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6642 case ZPOOL_STATUS_FAULTED_DEV_NR:
6643 (void) printf(gettext("status: One or more devices are "
6644 "faulted in response to persistent errors. There are "
6645 "insufficient replicas for the pool to\n\tcontinue "
6647 (void) printf(gettext("action: Destroy and re-create the pool "
6648 "from a backup source. Manually marking the device\n"
6649 "\trepaired using 'zpool clear' may allow some data "
6650 "to be recovered.\n"));
6653 case ZPOOL_STATUS_IO_FAILURE_MMP:
6654 (void) printf(gettext("status: The pool is suspended because "
6655 "multihost writes failed or were delayed;\n\tanother "
6656 "system could import the pool undetected.\n"));
6657 (void) printf(gettext("action: Make sure the pool's devices "
6658 "are connected, then reboot your system and\n\timport the "
6662 case ZPOOL_STATUS_IO_FAILURE_WAIT:
6663 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6664 (void) printf(gettext("status: One or more devices are "
6665 "faulted in response to IO failures.\n"));
6666 (void) printf(gettext("action: Make sure the affected devices "
6667 "are connected, then run 'zpool clear'.\n"));
6670 case ZPOOL_STATUS_BAD_LOG:
6671 (void) printf(gettext("status: An intent log record "
6672 "could not be read.\n"
6673 "\tWaiting for administrator intervention to fix the "
6674 "faulted pool.\n"));
6675 (void) printf(gettext("action: Either restore the affected "
6676 "device(s) and run 'zpool online',\n"
6677 "\tor ignore the intent log records by running "
6678 "'zpool clear'.\n"));
6681 case ZPOOL_STATUS_HOSTID_MISMATCH:
6682 (void) printf(gettext("status: Mismatch between pool hostid "
6683 "and system hostid on imported pool.\n\tThis pool was "
6684 "previously imported into a system with a different "
6685 "hostid,\n\tand then was verbatim imported into this "
6687 (void) printf(gettext("action: Export this pool on all systems "
6688 "on which it is imported.\n"
6689 "\tThen import it to correct the mismatch.\n"));
6692 case ZPOOL_STATUS_ERRATA:
6693 (void) printf(gettext("status: Errata #%d detected.\n"),
6697 case ZPOOL_ERRATA_NONE:
6700 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
6701 (void) printf(gettext("action: To correct the issue "
6702 "run 'zpool scrub'.\n"));
6705 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
6706 (void) printf(gettext("\tExisting encrypted datasets "
6707 "contain an on-disk incompatibility\n\twhich "
6708 "needs to be corrected.\n"));
6709 (void) printf(gettext("action: To correct the issue "
6710 "backup existing encrypted datasets to new\n\t"
6711 "encrypted datasets and destroy the old ones. "
6712 "'zfs mount -o ro' can\n\tbe used to temporarily "
6713 "mount existing encrypted datasets readonly.\n"));
6718 * All errata which allow the pool to be imported
6719 * must contain an action message.
6727 * The remaining errors can't actually be generated, yet.
6729 assert(reason == ZPOOL_STATUS_OK);
6733 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6736 if (config != NULL) {
6738 nvlist_t **spares, **l2cache;
6739 uint_t nspares, nl2cache;
6740 pool_scan_stat_t *ps = NULL;
6741 pool_removal_stat_t *prs = NULL;
6743 (void) nvlist_lookup_uint64_array(nvroot,
6744 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6745 print_scan_status(ps);
6747 (void) nvlist_lookup_uint64_array(nvroot,
6748 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
6749 print_removal_status(zhp, prs);
6751 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6752 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6753 if (cbp->cb_namewidth < 10)
6754 cbp->cb_namewidth = 10;
6756 (void) printf(gettext("config:\n\n"));
6757 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6758 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6761 if (cbp->vcdl != NULL)
6762 print_cmd_columns(cbp->vcdl, 0);
6765 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6768 if (num_logs(nvroot) > 0)
6769 print_logs(zhp, cbp, nvroot);
6770 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6771 &l2cache, &nl2cache) == 0)
6772 print_l2cache(zhp, cbp, l2cache, nl2cache);
6774 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6775 &spares, &nspares) == 0)
6776 print_spares(zhp, cbp, spares, nspares);
6778 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6780 nvlist_t *nverrlist = NULL;
6783 * If the approximate error count is small, get a
6784 * precise count by fetching the entire log and
6785 * uniquifying the results.
6787 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6788 zpool_get_errlog(zhp, &nverrlist) == 0) {
6793 while ((elem = nvlist_next_nvpair(nverrlist,
6798 nvlist_free(nverrlist);
6800 (void) printf("\n");
6803 (void) printf(gettext("errors: No known data "
6805 else if (!cbp->cb_verbose)
6806 (void) printf(gettext("errors: %llu data "
6807 "errors, use '-v' for a list\n"),
6808 (u_longlong_t)nerr);
6810 print_error_log(zhp);
6813 if (cbp->cb_dedup_stats)
6814 print_dedup_stats(config);
6816 (void) printf(gettext("config: The configuration cannot be "
6824 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6825 * [interval [count]]
6827 * -c CMD For each vdev, run command CMD
6828 * -g Display guid for individual vdev name.
6829 * -L Follow links when resolving vdev path name.
6830 * -P Display full path for vdev name.
6831 * -v Display complete error logs
6832 * -x Display only pools with potential problems
6833 * -D Display dedup status (undocumented)
6834 * -T Display a timestamp in date(1) or Unix format
6836 * Describes the health status of all pools or some subset.
6839 zpool_do_status(int argc, char **argv)
6844 unsigned long count = 0;
6845 status_cbdata_t cb = { 0 };
6849 while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6854 gettext("Can't set -c flag twice\n"));
6858 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6859 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6860 fprintf(stderr, gettext(
6861 "Can't run -c, disabled by "
6862 "ZPOOL_SCRIPTS_ENABLED.\n"));
6866 if ((getuid() <= 0 || geteuid() <= 0) &&
6867 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6868 fprintf(stderr, gettext(
6869 "Can't run -c with root privileges "
6870 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6876 cb.cb_name_flags |= VDEV_NAME_GUID;
6879 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6882 cb.cb_name_flags |= VDEV_NAME_PATH;
6885 cb.cb_verbose = B_TRUE;
6888 cb.cb_explain = B_TRUE;
6891 cb.cb_dedup_stats = B_TRUE;
6894 get_timestamp_arg(*optarg);
6897 if (optopt == 'c') {
6898 print_zpool_script_list("status");
6902 gettext("invalid option '%c'\n"), optopt);
6911 get_interval_count(&argc, argv, &interval, &count);
6914 cb.cb_allpools = B_TRUE;
6916 cb.cb_first = B_TRUE;
6917 cb.cb_print_status = B_TRUE;
6920 if (timestamp_fmt != NODATE)
6921 print_timestamp(timestamp_fmt);
6924 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6927 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6928 status_callback, &cb);
6930 if (cb.vcdl != NULL)
6931 free_vdev_cmd_data_list(cb.vcdl);
6933 if (argc == 0 && cb.cb_count == 0)
6934 (void) fprintf(stderr, gettext("no pools available\n"));
6935 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6936 (void) printf(gettext("all pools are healthy\n"));
6944 if (count != 0 && --count == 0)
6947 (void) fsleep(interval);
6953 typedef struct upgrade_cbdata {
6956 uint64_t cb_version;
6961 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6963 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6964 int *count = (int *)unsupp_fs;
6966 if (zfs_version > ZPL_VERSION) {
6967 (void) printf(gettext("%s (v%d) is not supported by this "
6968 "implementation of ZFS.\n"),
6969 zfs_get_name(zhp), zfs_version);
6973 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6981 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6985 uint64_t oldversion;
6988 config = zpool_get_config(zhp, NULL);
6989 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6992 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6993 assert(oldversion < version);
6995 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
7000 (void) fprintf(stderr, gettext("Upgrade not performed due "
7001 "to %d unsupported filesystems (max v%d).\n"),
7002 unsupp_fs, (int)ZPL_VERSION);
7006 ret = zpool_upgrade(zhp, version);
7010 if (version >= SPA_VERSION_FEATURES) {
7011 (void) printf(gettext("Successfully upgraded "
7012 "'%s' from version %llu to feature flags.\n"),
7013 zpool_get_name(zhp), (u_longlong_t)oldversion);
7015 (void) printf(gettext("Successfully upgraded "
7016 "'%s' from version %llu to version %llu.\n"),
7017 zpool_get_name(zhp), (u_longlong_t)oldversion,
7018 (u_longlong_t)version);
7025 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
7028 boolean_t firstff = B_TRUE;
7029 nvlist_t *enabled = zpool_get_features(zhp);
7032 for (i = 0; i < SPA_FEATURES; i++) {
7033 const char *fname = spa_feature_table[i].fi_uname;
7034 const char *fguid = spa_feature_table[i].fi_guid;
7035 if (!nvlist_exists(enabled, fguid)) {
7037 verify(-1 != asprintf(&propname, "feature@%s", fname));
7038 ret = zpool_set_prop(zhp, propname,
7039 ZFS_FEATURE_ENABLED);
7047 (void) printf(gettext("Enabled the "
7048 "following features on '%s':\n"),
7049 zpool_get_name(zhp));
7052 (void) printf(gettext(" %s\n"), fname);
7063 upgrade_cb(zpool_handle_t *zhp, void *arg)
7065 upgrade_cbdata_t *cbp = arg;
7068 boolean_t printnl = B_FALSE;
7071 config = zpool_get_config(zhp, NULL);
7072 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7075 assert(SPA_VERSION_IS_SUPPORTED(version));
7077 if (version < cbp->cb_version) {
7078 cbp->cb_first = B_FALSE;
7079 ret = upgrade_version(zhp, cbp->cb_version);
7085 * If they did "zpool upgrade -a", then we could
7086 * be doing ioctls to different pools. We need
7087 * to log this history once to each pool, and bypass
7088 * the normal history logging that happens in main().
7090 (void) zpool_log_history(g_zfs, history_str);
7091 log_history = B_FALSE;
7094 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7096 ret = upgrade_enable_all(zhp, &count);
7101 cbp->cb_first = B_FALSE;
7107 (void) printf(gettext("\n"));
7114 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
7116 upgrade_cbdata_t *cbp = arg;
7120 config = zpool_get_config(zhp, NULL);
7121 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7124 assert(SPA_VERSION_IS_SUPPORTED(version));
7126 if (version < SPA_VERSION_FEATURES) {
7127 if (cbp->cb_first) {
7128 (void) printf(gettext("The following pools are "
7129 "formatted with legacy version numbers and can\n"
7130 "be upgraded to use feature flags. After "
7131 "being upgraded, these pools\nwill no "
7132 "longer be accessible by software that does not "
7133 "support feature\nflags.\n\n"));
7134 (void) printf(gettext("VER POOL\n"));
7135 (void) printf(gettext("--- ------------\n"));
7136 cbp->cb_first = B_FALSE;
7139 (void) printf("%2llu %s\n", (u_longlong_t)version,
7140 zpool_get_name(zhp));
7147 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
7149 upgrade_cbdata_t *cbp = arg;
7153 config = zpool_get_config(zhp, NULL);
7154 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7157 if (version >= SPA_VERSION_FEATURES) {
7159 boolean_t poolfirst = B_TRUE;
7160 nvlist_t *enabled = zpool_get_features(zhp);
7162 for (i = 0; i < SPA_FEATURES; i++) {
7163 const char *fguid = spa_feature_table[i].fi_guid;
7164 const char *fname = spa_feature_table[i].fi_uname;
7165 if (!nvlist_exists(enabled, fguid)) {
7166 if (cbp->cb_first) {
7167 (void) printf(gettext("\nSome "
7168 "supported features are not "
7169 "enabled on the following pools. "
7170 "Once a\nfeature is enabled the "
7171 "pool may become incompatible with "
7172 "software\nthat does not support "
7174 "zpool-features(5) for "
7176 (void) printf(gettext("POOL "
7178 (void) printf(gettext("------"
7180 cbp->cb_first = B_FALSE;
7184 (void) printf(gettext("%s\n"),
7185 zpool_get_name(zhp));
7186 poolfirst = B_FALSE;
7189 (void) printf(gettext(" %s\n"), fname);
7192 * If they did "zpool upgrade -a", then we could
7193 * be doing ioctls to different pools. We need
7194 * to log this history once to each pool, and bypass
7195 * the normal history logging that happens in main().
7197 (void) zpool_log_history(g_zfs, history_str);
7198 log_history = B_FALSE;
7207 upgrade_one(zpool_handle_t *zhp, void *data)
7209 boolean_t printnl = B_FALSE;
7210 upgrade_cbdata_t *cbp = data;
7211 uint64_t cur_version;
7214 if (strcmp("log", zpool_get_name(zhp)) == 0) {
7215 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
7216 "Pool 'log' must be renamed using export and import"
7221 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7222 if (cur_version > cbp->cb_version) {
7223 (void) printf(gettext("Pool '%s' is already formatted "
7224 "using more current version '%llu'.\n\n"),
7225 zpool_get_name(zhp), (u_longlong_t)cur_version);
7229 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
7230 (void) printf(gettext("Pool '%s' is already formatted "
7231 "using version %llu.\n\n"), zpool_get_name(zhp),
7232 (u_longlong_t)cbp->cb_version);
7236 if (cur_version != cbp->cb_version) {
7238 ret = upgrade_version(zhp, cbp->cb_version);
7243 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7245 ret = upgrade_enable_all(zhp, &count);
7251 } else if (cur_version == SPA_VERSION) {
7252 (void) printf(gettext("Pool '%s' already has all "
7253 "supported features enabled.\n"),
7254 zpool_get_name(zhp));
7259 (void) printf(gettext("\n"));
7268 * zpool upgrade [-V version] <-a | pool ...>
7270 * With no arguments, display downrev'd ZFS pool available for upgrade.
7271 * Individual pools can be upgraded by specifying the pool, and '-a' will
7272 * upgrade all pools.
7275 zpool_do_upgrade(int argc, char **argv)
7278 upgrade_cbdata_t cb = { 0 };
7280 boolean_t showversions = B_FALSE;
7281 boolean_t upgradeall = B_FALSE;
7286 while ((c = getopt(argc, argv, ":avV:")) != -1) {
7289 upgradeall = B_TRUE;
7292 showversions = B_TRUE;
7295 cb.cb_version = strtoll(optarg, &end, 10);
7297 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7298 (void) fprintf(stderr,
7299 gettext("invalid version '%s'\n"), optarg);
7304 (void) fprintf(stderr, gettext("missing argument for "
7305 "'%c' option\n"), optopt);
7309 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7320 if (cb.cb_version == 0) {
7321 cb.cb_version = SPA_VERSION;
7322 } else if (!upgradeall && argc == 0) {
7323 (void) fprintf(stderr, gettext("-V option is "
7324 "incompatible with other arguments\n"));
7329 if (upgradeall || argc != 0) {
7330 (void) fprintf(stderr, gettext("-v option is "
7331 "incompatible with other arguments\n"));
7334 } else if (upgradeall) {
7336 (void) fprintf(stderr, gettext("-a option should not "
7337 "be used along with a pool name\n"));
7342 (void) printf(gettext("This system supports ZFS pool feature "
7347 (void) printf(gettext("The following features are "
7349 (void) printf(gettext("FEAT DESCRIPTION\n"));
7350 (void) printf("----------------------------------------------"
7351 "---------------\n");
7352 for (i = 0; i < SPA_FEATURES; i++) {
7353 zfeature_info_t *fi = &spa_feature_table[i];
7355 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7356 " (read-only compatible)" : "";
7358 (void) printf("%-37s%s\n", fi->fi_uname, ro);
7359 (void) printf(" %s\n", fi->fi_desc);
7361 (void) printf("\n");
7363 (void) printf(gettext("The following legacy versions are also "
7365 (void) printf(gettext("VER DESCRIPTION\n"));
7366 (void) printf("--- -----------------------------------------"
7367 "---------------\n");
7368 (void) printf(gettext(" 1 Initial ZFS version\n"));
7369 (void) printf(gettext(" 2 Ditto blocks "
7370 "(replicated metadata)\n"));
7371 (void) printf(gettext(" 3 Hot spares and double parity "
7373 (void) printf(gettext(" 4 zpool history\n"));
7374 (void) printf(gettext(" 5 Compression using the gzip "
7376 (void) printf(gettext(" 6 bootfs pool property\n"));
7377 (void) printf(gettext(" 7 Separate intent log devices\n"));
7378 (void) printf(gettext(" 8 Delegated administration\n"));
7379 (void) printf(gettext(" 9 refquota and refreservation "
7381 (void) printf(gettext(" 10 Cache devices\n"));
7382 (void) printf(gettext(" 11 Improved scrub performance\n"));
7383 (void) printf(gettext(" 12 Snapshot properties\n"));
7384 (void) printf(gettext(" 13 snapused property\n"));
7385 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7386 (void) printf(gettext(" 15 user/group space accounting\n"));
7387 (void) printf(gettext(" 16 stmf property support\n"));
7388 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7389 (void) printf(gettext(" 18 Snapshot user holds\n"));
7390 (void) printf(gettext(" 19 Log device removal\n"));
7391 (void) printf(gettext(" 20 Compression using zle "
7392 "(zero-length encoding)\n"));
7393 (void) printf(gettext(" 21 Deduplication\n"));
7394 (void) printf(gettext(" 22 Received properties\n"));
7395 (void) printf(gettext(" 23 Slim ZIL\n"));
7396 (void) printf(gettext(" 24 System attributes\n"));
7397 (void) printf(gettext(" 25 Improved scrub stats\n"));
7398 (void) printf(gettext(" 26 Improved snapshot deletion "
7400 (void) printf(gettext(" 27 Improved snapshot creation "
7402 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7403 (void) printf(gettext("\nFor more information on a particular "
7404 "version, including supported releases,\n"));
7405 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7406 } else if (argc == 0 && upgradeall) {
7407 cb.cb_first = B_TRUE;
7408 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7409 if (ret == 0 && cb.cb_first) {
7410 if (cb.cb_version == SPA_VERSION) {
7411 (void) printf(gettext("All pools are already "
7412 "formatted using feature flags.\n\n"));
7413 (void) printf(gettext("Every feature flags "
7414 "pool already has all supported features "
7417 (void) printf(gettext("All pools are already "
7418 "formatted with version %llu or higher.\n"),
7419 (u_longlong_t)cb.cb_version);
7422 } else if (argc == 0) {
7423 cb.cb_first = B_TRUE;
7424 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7428 (void) printf(gettext("All pools are formatted "
7429 "using feature flags.\n\n"));
7431 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7432 "for a list of available legacy versions.\n"));
7435 cb.cb_first = B_TRUE;
7436 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7440 (void) printf(gettext("Every feature flags pool has "
7441 "all supported features enabled.\n"));
7443 (void) printf(gettext("\n"));
7446 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7453 typedef struct hist_cbdata {
7460 * Print out the command history for a specific pool.
7463 get_history_one(zpool_handle_t *zhp, void *data)
7469 hist_cbdata_t *cb = (hist_cbdata_t *)data;
7471 cb->first = B_FALSE;
7473 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7475 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7478 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7479 &records, &numrecords) == 0);
7480 for (i = 0; i < numrecords; i++) {
7481 nvlist_t *rec = records[i];
7484 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7488 tsec = fnvlist_lookup_uint64(records[i],
7490 (void) localtime_r(&tsec, &t);
7491 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7494 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7495 (void) printf("%s %s", tbuf,
7496 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7497 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7499 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7502 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7503 (void) printf("%s unrecognized record:\n",
7505 dump_nvlist(rec, 4);
7508 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7509 zfs_history_event_names[ievent],
7510 (longlong_t)fnvlist_lookup_uint64(
7511 rec, ZPOOL_HIST_TXG),
7512 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7513 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7516 (void) printf("%s [txg:%lld] %s", tbuf,
7517 (longlong_t)fnvlist_lookup_uint64(
7518 rec, ZPOOL_HIST_TXG),
7519 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7520 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7521 (void) printf(" %s (%llu)",
7522 fnvlist_lookup_string(rec,
7524 (u_longlong_t)fnvlist_lookup_uint64(rec,
7527 (void) printf(" %s", fnvlist_lookup_string(rec,
7528 ZPOOL_HIST_INT_STR));
7529 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7532 (void) printf("%s ioctl %s\n", tbuf,
7533 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7534 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7535 (void) printf(" input:\n");
7536 dump_nvlist(fnvlist_lookup_nvlist(rec,
7537 ZPOOL_HIST_INPUT_NVL), 8);
7539 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7540 (void) printf(" output:\n");
7541 dump_nvlist(fnvlist_lookup_nvlist(rec,
7542 ZPOOL_HIST_OUTPUT_NVL), 8);
7544 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
7545 (void) printf(" errno: %lld\n",
7546 (longlong_t)fnvlist_lookup_int64(rec,
7552 (void) printf("%s unrecognized record:\n", tbuf);
7553 dump_nvlist(rec, 4);
7557 (void) printf("\n");
7560 (void) printf(" [");
7561 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7562 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7563 struct passwd *pwd = getpwuid(who);
7564 (void) printf("user %d ", (int)who);
7566 (void) printf("(%s) ", pwd->pw_name);
7568 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7569 (void) printf("on %s",
7570 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7572 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7573 (void) printf(":%s",
7574 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7578 (void) printf("\n");
7580 (void) printf("\n");
7587 * zpool history <pool>
7589 * Displays the history of commands that modified pools.
7592 zpool_do_history(int argc, char **argv)
7594 hist_cbdata_t cbdata = { 0 };
7598 cbdata.first = B_TRUE;
7600 while ((c = getopt(argc, argv, "li")) != -1) {
7603 cbdata.longfmt = B_TRUE;
7606 cbdata.internal = B_TRUE;
7609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7617 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
7620 if (argc == 0 && cbdata.first == B_TRUE) {
7621 (void) fprintf(stderr, gettext("no pools available\n"));
7628 typedef struct ev_opts {
7633 char poolname[ZFS_MAX_DATASET_NAME_LEN];
7637 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7639 char ctime_str[26], str[32], *ptr;
7643 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7644 memset(str, ' ', 32);
7645 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7646 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
7647 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
7648 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
7649 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7651 (void) printf(gettext("%s\t"), str);
7653 (void) printf(gettext("%s "), str);
7655 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7656 (void) printf(gettext("%s\n"), ptr);
7660 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7664 for (nvp = nvlist_next_nvpair(nvl, NULL);
7665 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
7667 data_type_t type = nvpair_type(nvp);
7668 const char *name = nvpair_name(nvp);
7678 printf(gettext("%*s%s = "), depth, "", name);
7681 case DATA_TYPE_BOOLEAN:
7682 printf(gettext("%s"), "1");
7685 case DATA_TYPE_BOOLEAN_VALUE:
7686 (void) nvpair_value_boolean_value(nvp, &b);
7687 printf(gettext("%s"), b ? "1" : "0");
7690 case DATA_TYPE_BYTE:
7691 (void) nvpair_value_byte(nvp, &i8);
7692 printf(gettext("0x%x"), i8);
7695 case DATA_TYPE_INT8:
7696 (void) nvpair_value_int8(nvp, (void *)&i8);
7697 printf(gettext("0x%x"), i8);
7700 case DATA_TYPE_UINT8:
7701 (void) nvpair_value_uint8(nvp, &i8);
7702 printf(gettext("0x%x"), i8);
7705 case DATA_TYPE_INT16:
7706 (void) nvpair_value_int16(nvp, (void *)&i16);
7707 printf(gettext("0x%x"), i16);
7710 case DATA_TYPE_UINT16:
7711 (void) nvpair_value_uint16(nvp, &i16);
7712 printf(gettext("0x%x"), i16);
7715 case DATA_TYPE_INT32:
7716 (void) nvpair_value_int32(nvp, (void *)&i32);
7717 printf(gettext("0x%x"), i32);
7720 case DATA_TYPE_UINT32:
7721 (void) nvpair_value_uint32(nvp, &i32);
7722 printf(gettext("0x%x"), i32);
7725 case DATA_TYPE_INT64:
7726 (void) nvpair_value_int64(nvp, (void *)&i64);
7727 printf(gettext("0x%llx"), (u_longlong_t)i64);
7730 case DATA_TYPE_UINT64:
7731 (void) nvpair_value_uint64(nvp, &i64);
7733 * translate vdev state values to readable
7734 * strings to aide zpool events consumers
7737 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
7739 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
7740 printf(gettext("\"%s\" (0x%llx)"),
7741 zpool_state_to_name(i64, VDEV_AUX_NONE),
7744 printf(gettext("0x%llx"), (u_longlong_t)i64);
7748 case DATA_TYPE_HRTIME:
7749 (void) nvpair_value_hrtime(nvp, (void *)&i64);
7750 printf(gettext("0x%llx"), (u_longlong_t)i64);
7753 case DATA_TYPE_STRING:
7754 (void) nvpair_value_string(nvp, &str);
7755 printf(gettext("\"%s\""), str ? str : "<NULL>");
7758 case DATA_TYPE_NVLIST:
7759 printf(gettext("(embedded nvlist)\n"));
7760 (void) nvpair_value_nvlist(nvp, &cnv);
7761 zpool_do_events_nvprint(cnv, depth + 8);
7762 printf(gettext("%*s(end %s)"), depth, "", name);
7765 case DATA_TYPE_NVLIST_ARRAY: {
7769 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7770 printf(gettext("(%d embedded nvlists)\n"), nelem);
7771 for (i = 0; i < nelem; i++) {
7772 printf(gettext("%*s%s[%d] = %s\n"),
7773 depth, "", name, i, "(embedded nvlist)");
7774 zpool_do_events_nvprint(val[i], depth + 8);
7775 printf(gettext("%*s(end %s[%i])\n"),
7776 depth, "", name, i);
7778 printf(gettext("%*s(end %s)\n"), depth, "", name);
7782 case DATA_TYPE_INT8_ARRAY: {
7786 (void) nvpair_value_int8_array(nvp, &val, &nelem);
7787 for (i = 0; i < nelem; i++)
7788 printf(gettext("0x%x "), val[i]);
7793 case DATA_TYPE_UINT8_ARRAY: {
7797 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7798 for (i = 0; i < nelem; i++)
7799 printf(gettext("0x%x "), val[i]);
7804 case DATA_TYPE_INT16_ARRAY: {
7808 (void) nvpair_value_int16_array(nvp, &val, &nelem);
7809 for (i = 0; i < nelem; i++)
7810 printf(gettext("0x%x "), val[i]);
7815 case DATA_TYPE_UINT16_ARRAY: {
7819 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7820 for (i = 0; i < nelem; i++)
7821 printf(gettext("0x%x "), val[i]);
7826 case DATA_TYPE_INT32_ARRAY: {
7830 (void) nvpair_value_int32_array(nvp, &val, &nelem);
7831 for (i = 0; i < nelem; i++)
7832 printf(gettext("0x%x "), val[i]);
7837 case DATA_TYPE_UINT32_ARRAY: {
7841 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7842 for (i = 0; i < nelem; i++)
7843 printf(gettext("0x%x "), val[i]);
7848 case DATA_TYPE_INT64_ARRAY: {
7852 (void) nvpair_value_int64_array(nvp, &val, &nelem);
7853 for (i = 0; i < nelem; i++)
7854 printf(gettext("0x%llx "),
7855 (u_longlong_t)val[i]);
7860 case DATA_TYPE_UINT64_ARRAY: {
7864 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7865 for (i = 0; i < nelem; i++)
7866 printf(gettext("0x%llx "),
7867 (u_longlong_t)val[i]);
7872 case DATA_TYPE_STRING_ARRAY: {
7876 (void) nvpair_value_string_array(nvp, &str, &nelem);
7877 for (i = 0; i < nelem; i++)
7878 printf(gettext("\"%s\" "),
7879 str[i] ? str[i] : "<NULL>");
7884 case DATA_TYPE_BOOLEAN_ARRAY:
7885 case DATA_TYPE_BYTE_ARRAY:
7886 case DATA_TYPE_DOUBLE:
7887 case DATA_TYPE_UNKNOWN:
7888 printf(gettext("<unknown>"));
7892 printf(gettext("\n"));
7897 zpool_do_events_next(ev_opts_t *opts)
7900 int zevent_fd, ret, dropped;
7903 zevent_fd = open(ZFS_DEV, O_RDWR);
7904 VERIFY(zevent_fd >= 0);
7906 if (!opts->scripted)
7907 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7910 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7911 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7912 if (ret || nvl == NULL)
7916 (void) printf(gettext("dropped %d events\n"), dropped);
7918 if (strlen(opts->poolname) > 0 &&
7919 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
7920 strcmp(opts->poolname, pool) != 0)
7923 zpool_do_events_short(nvl, opts);
7925 if (opts->verbose) {
7926 zpool_do_events_nvprint(nvl, 8);
7927 printf(gettext("\n"));
7929 (void) fflush(stdout);
7934 VERIFY(0 == close(zevent_fd));
7940 zpool_do_events_clear(ev_opts_t *opts)
7944 ret = zpool_events_clear(g_zfs, &count);
7946 (void) printf(gettext("cleared %d events\n"), count);
7952 * zpool events [-vHf [pool] | -c]
7954 * Displays events logs by ZFS.
7957 zpool_do_events(int argc, char **argv)
7959 ev_opts_t opts = { 0 };
7964 while ((c = getopt(argc, argv, "vHfc")) != -1) {
7979 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7988 (void) fprintf(stderr, gettext("too many arguments\n"));
7990 } else if (argc == 1) {
7991 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
7992 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
7993 (void) fprintf(stderr,
7994 gettext("invalid pool name '%s'\n"), opts.poolname);
7999 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
8001 (void) fprintf(stderr,
8002 gettext("invalid options combined with -c\n"));
8007 ret = zpool_do_events_clear(&opts);
8009 ret = zpool_do_events_next(&opts);
8015 get_callback(zpool_handle_t *zhp, void *data)
8017 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
8018 char value[MAXNAMELEN];
8019 zprop_source_t srctype;
8022 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8025 * Skip the special fake placeholder. This will also skip
8026 * over the name property when 'all' is specified.
8028 if (pl->pl_prop == ZPOOL_PROP_NAME &&
8029 pl == cbp->cb_proplist)
8032 if (pl->pl_prop == ZPROP_INVAL &&
8033 (zpool_prop_feature(pl->pl_user_prop) ||
8034 zpool_prop_unsupported(pl->pl_user_prop))) {
8035 srctype = ZPROP_SRC_LOCAL;
8037 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
8038 value, sizeof (value)) == 0) {
8039 zprop_print_one_property(zpool_get_name(zhp),
8040 cbp, pl->pl_user_prop, value, srctype,
8044 if (zpool_get_prop(zhp, pl->pl_prop, value,
8045 sizeof (value), &srctype, cbp->cb_literal) != 0)
8048 zprop_print_one_property(zpool_get_name(zhp), cbp,
8049 zpool_prop_to_name(pl->pl_prop), value, srctype,
8057 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8059 * -H Scripted mode. Don't display headers, and separate properties
8061 * -o List of columns to display. Defaults to
8062 * "name,property,value,source".
8063 * -p Display values in parsable (exact) format.
8065 * Get properties of pools in the system. Output space statistics
8066 * for each one as well as other attributes.
8069 zpool_do_get(int argc, char **argv)
8071 zprop_get_cbdata_t cb = { 0 };
8072 zprop_list_t fake_name = { 0 };
8077 cb.cb_first = B_TRUE;
8080 * Set up default columns and sources.
8082 cb.cb_sources = ZPROP_SRC_ALL;
8083 cb.cb_columns[0] = GET_COL_NAME;
8084 cb.cb_columns[1] = GET_COL_PROPERTY;
8085 cb.cb_columns[2] = GET_COL_VALUE;
8086 cb.cb_columns[3] = GET_COL_SOURCE;
8087 cb.cb_type = ZFS_TYPE_POOL;
8090 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
8093 cb.cb_literal = B_TRUE;
8096 cb.cb_scripted = B_TRUE;
8099 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
8101 while (*optarg != '\0') {
8102 static char *col_subopts[] =
8103 { "name", "property", "value", "source",
8106 if (i == ZFS_GET_NCOLS) {
8107 (void) fprintf(stderr, gettext("too "
8108 "many fields given to -o "
8113 switch (getsubopt(&optarg, col_subopts,
8116 cb.cb_columns[i++] = GET_COL_NAME;
8119 cb.cb_columns[i++] = GET_COL_PROPERTY;
8122 cb.cb_columns[i++] = GET_COL_VALUE;
8125 cb.cb_columns[i++] = GET_COL_SOURCE;
8129 (void) fprintf(stderr,
8130 gettext("\"all\" conflicts "
8131 "with specific fields "
8132 "given to -o option\n"));
8135 cb.cb_columns[0] = GET_COL_NAME;
8136 cb.cb_columns[1] = GET_COL_PROPERTY;
8137 cb.cb_columns[2] = GET_COL_VALUE;
8138 cb.cb_columns[3] = GET_COL_SOURCE;
8142 (void) fprintf(stderr,
8143 gettext("invalid column name "
8150 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8160 (void) fprintf(stderr, gettext("missing property "
8165 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
8166 ZFS_TYPE_POOL) != 0)
8172 if (cb.cb_proplist != NULL) {
8173 fake_name.pl_prop = ZPOOL_PROP_NAME;
8174 fake_name.pl_width = strlen(gettext("NAME"));
8175 fake_name.pl_next = cb.cb_proplist;
8176 cb.cb_proplist = &fake_name;
8179 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
8182 if (cb.cb_proplist == &fake_name)
8183 zprop_free_list(fake_name.pl_next);
8185 zprop_free_list(cb.cb_proplist);
8190 typedef struct set_cbdata {
8193 boolean_t cb_any_successful;
8197 set_callback(zpool_handle_t *zhp, void *data)
8200 set_cbdata_t *cb = (set_cbdata_t *)data;
8202 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
8205 cb->cb_any_successful = B_TRUE;
8211 zpool_do_set(int argc, char **argv)
8213 set_cbdata_t cb = { 0 };
8216 if (argc > 1 && argv[1][0] == '-') {
8217 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8223 (void) fprintf(stderr, gettext("missing property=value "
8229 (void) fprintf(stderr, gettext("missing pool name\n"));
8234 (void) fprintf(stderr, gettext("too many pool names\n"));
8238 cb.cb_propname = argv[1];
8239 cb.cb_value = strchr(cb.cb_propname, '=');
8240 if (cb.cb_value == NULL) {
8241 (void) fprintf(stderr, gettext("missing value in "
8242 "property=value argument\n"));
8246 *(cb.cb_value) = '\0';
8249 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8256 find_command_idx(char *command, int *idx)
8260 for (i = 0; i < NCOMMAND; i++) {
8261 if (command_table[i].name == NULL)
8264 if (strcmp(command, command_table[i].name) == 0) {
8273 main(int argc, char **argv)
8279 (void) setlocale(LC_ALL, "");
8280 (void) textdomain(TEXT_DOMAIN);
8286 * Make sure the user has specified some command.
8289 (void) fprintf(stderr, gettext("missing command\n"));
8298 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8301 if ((g_zfs = libzfs_init()) == NULL) {
8302 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8306 libzfs_print_on_error(g_zfs, B_TRUE);
8308 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8311 * Run the appropriate command.
8313 if (find_command_idx(cmdname, &i) == 0) {
8314 current_command = &command_table[i];
8315 ret = command_table[i].func(argc - 1, argv + 1);
8316 } else if (strchr(cmdname, '=')) {
8317 verify(find_command_idx("set", &i) == 0);
8318 current_command = &command_table[i];
8319 ret = command_table[i].func(argc, argv);
8320 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8322 * 'freeze' is a vile debugging abomination, so we treat
8325 zfs_cmd_t zc = {"\0"};
8327 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8328 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8330 (void) fprintf(stderr,
8331 gettext("failed to freeze pool: %d\n"), errno);
8337 (void) fprintf(stderr, gettext("unrecognized "
8338 "command '%s'\n"), cmdname);
8343 if (ret == 0 && log_history)
8344 (void) zpool_log_history(g_zfs, history_str);
8349 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8350 * for the purposes of running ::findleaks.
8352 if (getenv("ZFS_ABORT") != NULL) {
8353 (void) printf("dumping core by request\n");