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, 2018 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.
32 * Copyright (c) 2017, Intel Corporation.
54 #include <sys/fs/zfs.h>
56 #include <sys/systeminfo.h>
57 #include <sys/fm/fs/zfs.h>
58 #include <sys/fm/util.h>
59 #include <sys/fm/protocol.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/mount.h>
62 #include <sys/sysmacros.h>
69 #include "zpool_util.h"
70 #include "zfs_comutil.h"
71 #include "zfeature_common.h"
73 #include "statcommon.h"
75 static int zpool_do_create(int, char **);
76 static int zpool_do_destroy(int, char **);
78 static int zpool_do_add(int, char **);
79 static int zpool_do_remove(int, char **);
80 static int zpool_do_labelclear(int, char **);
82 static int zpool_do_checkpoint(int, char **);
84 static int zpool_do_list(int, char **);
85 static int zpool_do_iostat(int, char **);
86 static int zpool_do_status(int, char **);
88 static int zpool_do_online(int, char **);
89 static int zpool_do_offline(int, char **);
90 static int zpool_do_clear(int, char **);
91 static int zpool_do_reopen(int, char **);
93 static int zpool_do_reguid(int, char **);
95 static int zpool_do_attach(int, char **);
96 static int zpool_do_detach(int, char **);
97 static int zpool_do_replace(int, char **);
98 static int zpool_do_split(int, char **);
100 static int zpool_do_initialize(int, char **);
101 static int zpool_do_scrub(int, char **);
102 static int zpool_do_resilver(int, char **);
103 static int zpool_do_trim(int, char **);
105 static int zpool_do_import(int, char **);
106 static int zpool_do_export(int, char **);
108 static int zpool_do_upgrade(int, char **);
110 static int zpool_do_history(int, char **);
111 static int zpool_do_events(int, char **);
113 static int zpool_do_get(int, char **);
114 static int zpool_do_set(int, char **);
116 static int zpool_do_sync(int, char **);
118 static int zpool_do_version(int, char **);
121 * These libumem hooks provide a reasonable set of defaults for the allocator's
122 * debugging facilities.
127 _umem_debug_init(void)
129 return ("default,verbose"); /* $UMEM_DEBUG setting */
133 _umem_logging_init(void)
135 return ("fail,contents"); /* $UMEM_LOGGING setting */
175 * Flags for stats to display with "zpool iostats"
183 IOS_COUNT, /* always last element */
186 /* iostat_type entries as bitmasks */
187 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
188 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
189 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
190 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
191 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
193 /* Mask of all the histo bits */
194 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
197 * Lookup table for iostat flags to nvlist names. Basically a list
198 * of all the nvlists a flag requires. Also specifies the order in
199 * which data gets printed in zpool iostat.
201 static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
203 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
204 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
205 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
206 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
207 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
208 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
209 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
210 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
211 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
212 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
215 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
216 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
217 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
218 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
219 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
222 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
223 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
224 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
225 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
226 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
227 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
230 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
231 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
232 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
233 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
234 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
235 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
236 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
237 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
238 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
239 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
240 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
241 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
247 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
248 * Right now, only one histo bit is ever set at one time, so we can
249 * just do a highbit64(a)
251 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
253 typedef struct zpool_command {
255 int (*func)(int, char **);
260 * Master command table. Each ZFS command has a name, associated function, and
261 * usage message. The usage messages need to be internationalized, so we have
262 * to have a function to return the usage message based on a command index.
264 * These commands are organized according to how they are displayed in the usage
265 * message. An empty command (one with a NULL name) indicates an empty line in
266 * the generic usage message.
268 static zpool_command_t command_table[] = {
269 { "version", zpool_do_version, HELP_VERSION },
271 { "create", zpool_do_create, HELP_CREATE },
272 { "destroy", zpool_do_destroy, HELP_DESTROY },
274 { "add", zpool_do_add, HELP_ADD },
275 { "remove", zpool_do_remove, HELP_REMOVE },
277 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
279 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
281 { "list", zpool_do_list, HELP_LIST },
282 { "iostat", zpool_do_iostat, HELP_IOSTAT },
283 { "status", zpool_do_status, HELP_STATUS },
285 { "online", zpool_do_online, HELP_ONLINE },
286 { "offline", zpool_do_offline, HELP_OFFLINE },
287 { "clear", zpool_do_clear, HELP_CLEAR },
288 { "reopen", zpool_do_reopen, HELP_REOPEN },
290 { "attach", zpool_do_attach, HELP_ATTACH },
291 { "detach", zpool_do_detach, HELP_DETACH },
292 { "replace", zpool_do_replace, HELP_REPLACE },
293 { "split", zpool_do_split, HELP_SPLIT },
295 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
296 { "resilver", zpool_do_resilver, HELP_RESILVER },
297 { "scrub", zpool_do_scrub, HELP_SCRUB },
298 { "trim", zpool_do_trim, HELP_TRIM },
300 { "import", zpool_do_import, HELP_IMPORT },
301 { "export", zpool_do_export, HELP_EXPORT },
302 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
303 { "reguid", zpool_do_reguid, HELP_REGUID },
305 { "history", zpool_do_history, HELP_HISTORY },
306 { "events", zpool_do_events, HELP_EVENTS },
308 { "get", zpool_do_get, HELP_GET },
309 { "set", zpool_do_set, HELP_SET },
310 { "sync", zpool_do_sync, HELP_SYNC },
313 #define NCOMMAND (ARRAY_SIZE(command_table))
315 #define VDEV_ALLOC_CLASS_LOGS "logs"
317 static zpool_command_t *current_command;
318 static char history_str[HIS_MAX_RECORD_LEN];
319 static boolean_t log_history = B_TRUE;
320 static uint_t timestamp_fmt = NODATE;
323 get_usage(zpool_help_t idx)
327 return (gettext("\tadd [-fgLnP] [-o property=value] "
328 "<pool> <vdev> ...\n"));
330 return (gettext("\tattach [-f] [-o property=value] "
331 "<pool> <device> <new-device>\n"));
333 return (gettext("\tclear [-nF] <pool> [device]\n"));
335 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
336 "\t [-O file-system-property=value] ... \n"
337 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
338 case HELP_CHECKPOINT:
339 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
341 return (gettext("\tdestroy [-f] <pool>\n"));
343 return (gettext("\tdetach <pool> <device>\n"));
345 return (gettext("\texport [-af] <pool> ...\n"));
347 return (gettext("\thistory [-il] [<pool>] ...\n"));
349 return (gettext("\timport [-d dir] [-D]\n"
350 "\timport [-o mntopts] [-o property=value] ... \n"
351 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
352 "[-R root] [-F [-n]] -a\n"
353 "\timport [-o mntopts] [-o property=value] ... \n"
354 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
355 "[-R root] [-F [-n]]\n"
356 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
358 return (gettext("\tiostat [[[-c [script1,script2,...]"
359 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
360 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
361 " [[-n] interval [count]]\n"));
362 case HELP_LABELCLEAR:
363 return (gettext("\tlabelclear [-f] <vdev>\n"));
365 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
366 "[-T d|u] [pool] ... \n"
367 "\t [interval [count]]\n"));
369 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
371 return (gettext("\tonline [-e] <pool> <device> ...\n"));
373 return (gettext("\treplace [-f] [-o property=value] "
374 "<pool> <device> [new-device]\n"));
376 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
378 return (gettext("\treopen [-n] <pool>\n"));
379 case HELP_INITIALIZE:
380 return (gettext("\tinitialize [-c | -s] <pool> "
381 "[<device> ...]\n"));
383 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
385 return (gettext("\tresilver <pool> ...\n"));
387 return (gettext("\ttrim [-dp] [-r <rate>] [-c | -s] <pool> "
388 "[<device> ...]\n"));
390 return (gettext("\tstatus [-c [script1,script2,...]] "
391 "[-igLpPsvxD] [-T d|u] [pool] ... \n"
392 "\t [interval [count]]\n"));
394 return (gettext("\tupgrade\n"
396 "\tupgrade [-V version] <-a | pool ...>\n"));
398 return (gettext("\tevents [-vHf [pool] | -c]\n"));
400 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
401 "<\"all\" | property[,...]> <pool> ...\n"));
403 return (gettext("\tset <property=value> <pool> \n"));
405 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
406 "\t [-o property=value] <pool> <newpool> "
407 "[<device> ...]\n"));
409 return (gettext("\treguid <pool>\n"));
411 return (gettext("\tsync [pool] ...\n"));
413 return (gettext("\tversion\n"));
421 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
427 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
431 char *path = zpool_vdev_name(g_zfs, zhp, nvroot,
434 if (strcmp(path, VDEV_TYPE_INDIRECT) != 0)
435 fnvlist_add_boolean(res, path);
441 for (i = 0; i < children; i++) {
442 zpool_collect_leaves(zhp, child[i], res);
447 * Callback routine that will print out a pool property value.
450 print_prop_cb(int prop, void *cb)
454 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
456 if (zpool_prop_readonly(prop))
457 (void) fprintf(fp, " NO ");
459 (void) fprintf(fp, " YES ");
461 if (zpool_prop_values(prop) == NULL)
462 (void) fprintf(fp, "-\n");
464 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
470 * Display usage message. If we're inside a command, display only the usage for
471 * that command. Otherwise, iterate over the entire command table and display
472 * a complete usage message.
475 usage(boolean_t requested)
477 FILE *fp = requested ? stdout : stderr;
479 if (current_command == NULL) {
482 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
484 gettext("where 'command' is one of the following:\n\n"));
486 for (i = 0; i < NCOMMAND; i++) {
487 if (command_table[i].name == NULL)
488 (void) fprintf(fp, "\n");
490 (void) fprintf(fp, "%s",
491 get_usage(command_table[i].usage));
494 (void) fprintf(fp, gettext("usage:\n"));
495 (void) fprintf(fp, "%s", get_usage(current_command->usage));
498 if (current_command != NULL &&
499 ((strcmp(current_command->name, "set") == 0) ||
500 (strcmp(current_command->name, "get") == 0) ||
501 (strcmp(current_command->name, "list") == 0))) {
504 gettext("\nthe following properties are supported:\n"));
506 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
507 "PROPERTY", "EDIT", "VALUES");
509 /* Iterate over all properties */
510 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
513 (void) fprintf(fp, "\t%-19s ", "feature@...");
514 (void) fprintf(fp, "YES disabled | enabled | active\n");
516 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
517 "appended with a feature name.\nSee zpool-features(5).\n"));
521 * See comments at end of main().
523 if (getenv("ZFS_ABORT") != NULL) {
524 (void) printf("dumping core by request\n");
528 exit(requested ? 0 : 2);
532 * zpool initialize [-c | -s] <pool> [<vdev> ...]
533 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
536 * -c Cancel. Ends active initializing.
537 * -s Suspend. Initializing can then be restarted with no flags.
540 zpool_do_initialize(int argc, char **argv)
548 struct option long_options[] = {
549 {"cancel", no_argument, NULL, 'c'},
550 {"suspend", no_argument, NULL, 's'},
554 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
555 while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
558 if (cmd_type != POOL_INITIALIZE_START &&
559 cmd_type != POOL_INITIALIZE_CANCEL) {
560 (void) fprintf(stderr, gettext("-c cannot be "
561 "combined with other options\n"));
564 cmd_type = POOL_INITIALIZE_CANCEL;
567 if (cmd_type != POOL_INITIALIZE_START &&
568 cmd_type != POOL_INITIALIZE_SUSPEND) {
569 (void) fprintf(stderr, gettext("-s cannot be "
570 "combined with other options\n"));
573 cmd_type = POOL_INITIALIZE_SUSPEND;
577 (void) fprintf(stderr,
578 gettext("invalid option '%c'\n"), optopt);
580 (void) fprintf(stderr,
581 gettext("invalid option '%s'\n"),
592 (void) fprintf(stderr, gettext("missing pool name argument\n"));
598 zhp = zpool_open(g_zfs, poolname);
602 vdevs = fnvlist_alloc();
604 /* no individual leaf vdevs specified, so add them all */
605 nvlist_t *config = zpool_get_config(zhp, NULL);
606 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
607 ZPOOL_CONFIG_VDEV_TREE);
608 zpool_collect_leaves(zhp, nvroot, vdevs);
610 for (int i = 1; i < argc; i++) {
611 fnvlist_add_boolean(vdevs, argv[i]);
615 err = zpool_initialize(zhp, cmd_type, vdevs);
624 * print a pool vdev config for dry runs
627 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
628 const char *match, int name_flags)
633 boolean_t printed = B_FALSE;
635 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
636 &child, &children) != 0) {
638 (void) printf("\t%*s%s\n", indent, "", name);
642 for (c = 0; c < children; c++) {
643 uint64_t is_log = B_FALSE;
646 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
649 class = VDEV_ALLOC_BIAS_LOG;
650 (void) nvlist_lookup_string(child[c],
651 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
652 if (strcmp(match, class) != 0)
655 if (!printed && name != NULL) {
656 (void) printf("\t%*s%s\n", indent, "", name);
659 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
660 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
667 prop_list_contains_feature(nvlist_t *proplist)
670 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
671 nvp = nvlist_next_nvpair(proplist, nvp)) {
672 if (zpool_prop_feature(nvpair_name(nvp)))
679 * Add a property pair (name, string-value) into a property nvlist.
682 add_prop_list(const char *propname, char *propval, nvlist_t **props,
685 zpool_prop_t prop = ZPOOL_PROP_INVAL;
690 if (*props == NULL &&
691 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
692 (void) fprintf(stderr,
693 gettext("internal error: out of memory\n"));
700 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
702 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
703 !zpool_prop_feature(propname)) {
704 (void) fprintf(stderr, gettext("property '%s' is "
705 "not a valid pool property\n"), propname);
710 * feature@ properties and version should not be specified
713 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
714 nvlist_exists(proplist, vname)) ||
715 (prop == ZPOOL_PROP_VERSION &&
716 prop_list_contains_feature(proplist))) {
717 (void) fprintf(stderr, gettext("'feature@' and "
718 "'version' properties cannot be specified "
724 if (zpool_prop_feature(propname))
727 normnm = zpool_prop_to_name(prop);
729 zfs_prop_t fsprop = zfs_name_to_prop(propname);
731 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
733 normnm = zfs_prop_to_name(fsprop);
734 } else if (zfs_prop_user(propname) ||
735 zfs_prop_userquota(propname)) {
738 (void) fprintf(stderr, gettext("property '%s' is "
739 "not a valid filesystem property\n"), propname);
744 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
745 prop != ZPOOL_PROP_CACHEFILE) {
746 (void) fprintf(stderr, gettext("property '%s' "
747 "specified multiple times\n"), propname);
751 if (nvlist_add_string(proplist, normnm, propval) != 0) {
752 (void) fprintf(stderr, gettext("internal "
753 "error: out of memory\n"));
761 * Set a default property pair (name, string-value) in a property nvlist
764 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
769 if (nvlist_lookup_string(*props, propname, &pval) == 0)
772 return (add_prop_list(propname, propval, props, B_TRUE));
776 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
778 * -f Force addition of devices, even if they appear in use
779 * -g Display guid for individual vdev name.
780 * -L Follow links when resolving vdev path name.
781 * -n Do not add the devices, but display the resulting layout if
782 * they were to be added.
783 * -o Set property=value.
784 * -P Display full path for vdev name.
786 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
787 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
791 zpool_do_add(int argc, char **argv)
793 boolean_t force = B_FALSE;
794 boolean_t dryrun = B_FALSE;
802 nvlist_t *props = NULL;
806 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
812 name_flags |= VDEV_NAME_GUID;
815 name_flags |= VDEV_NAME_FOLLOW_LINKS;
821 if ((propval = strchr(optarg, '=')) == NULL) {
822 (void) fprintf(stderr, gettext("missing "
823 "'=' for -o option\n"));
829 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
830 (add_prop_list(optarg, propval, &props, B_TRUE)))
834 name_flags |= VDEV_NAME_PATH;
837 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
846 /* get pool name and check number of arguments */
848 (void) fprintf(stderr, gettext("missing pool name argument\n"));
852 (void) fprintf(stderr, gettext("missing vdev specification\n"));
861 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
864 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
865 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
871 /* unless manually specified use "ashift" pool property (if set) */
872 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
875 char strval[ZPOOL_MAXPROPLEN];
877 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
878 if (src != ZPROP_SRC_DEFAULT) {
879 (void) sprintf(strval, "%" PRId32, intval);
880 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
881 &props, B_TRUE) == 0);
885 /* pass off to get_vdev_spec for processing */
886 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
888 if (nvroot == NULL) {
894 nvlist_t *poolnvroot;
896 uint_t l2children, c;
898 boolean_t hadcache = B_FALSE;
900 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
903 (void) printf(gettext("would update '%s' to the following "
904 "configuration:\n"), zpool_get_name(zhp));
906 /* print original main pool and new tree */
907 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
908 name_flags | VDEV_NAME_TYPE_ID);
909 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
911 /* print other classes: 'dedup', 'special', and 'log' */
912 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
913 VDEV_ALLOC_BIAS_DEDUP, name_flags);
914 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP,
917 print_vdev_tree(zhp, "special", poolnvroot, 0,
918 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
919 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL,
922 print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG,
924 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG,
927 /* Do the same for the caches */
928 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
929 &l2child, &l2children) == 0 && l2children) {
931 (void) printf(gettext("\tcache\n"));
932 for (c = 0; c < l2children; c++) {
933 vname = zpool_vdev_name(g_zfs, NULL,
934 l2child[c], name_flags);
935 (void) printf("\t %s\n", vname);
939 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
940 &l2child, &l2children) == 0 && l2children) {
942 (void) printf(gettext("\tcache\n"));
943 for (c = 0; c < l2children; c++) {
944 vname = zpool_vdev_name(g_zfs, NULL,
945 l2child[c], name_flags);
946 (void) printf("\t %s\n", vname);
953 ret = (zpool_add(zhp, nvroot) != 0);
964 * zpool remove <pool> <vdev> ...
966 * Removes the given vdev from the pool.
969 zpool_do_remove(int argc, char **argv)
973 zpool_handle_t *zhp = NULL;
974 boolean_t stop = B_FALSE;
976 boolean_t noop = B_FALSE;
977 boolean_t parsable = B_FALSE;
980 while ((c = getopt(argc, argv, "nps")) != -1) {
992 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1001 /* get pool name and check number of arguments */
1003 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1009 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1013 (void) fprintf(stderr, gettext("stop request ignored\n"));
1019 (void) fprintf(stderr, gettext("too many arguments\n"));
1022 if (zpool_vdev_remove_cancel(zhp) != 0)
1026 (void) fprintf(stderr, gettext("missing device\n"));
1030 for (i = 1; i < argc; i++) {
1034 if (zpool_vdev_indirect_size(zhp, argv[i],
1040 (void) printf("%s %llu\n",
1041 argv[i], (unsigned long long)size);
1044 zfs_nicenum(size, valstr,
1046 (void) printf("Memory that will be "
1047 "used after removing %s: %s\n",
1051 if (zpool_vdev_remove(zhp, argv[i]) != 0)
1062 * zpool labelclear [-f] <vdev>
1064 * -f Force clearing the label for the vdevs which are members of
1065 * the exported or foreign pools.
1067 * Verifies that the vdev is not active and zeros out the label information
1071 zpool_do_labelclear(int argc, char **argv)
1073 char vdev[MAXPATHLEN];
1076 int c, fd = -1, ret = 0;
1079 boolean_t inuse = B_FALSE;
1080 boolean_t force = B_FALSE;
1083 while ((c = getopt(argc, argv, "f")) != -1) {
1089 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1100 (void) fprintf(stderr, gettext("missing vdev name\n"));
1104 (void) fprintf(stderr, gettext("too many arguments\n"));
1109 * Check if we were given absolute path and use it as is.
1110 * Otherwise if the provided vdev name doesn't point to a file,
1111 * try prepending expected disk paths and partition numbers.
1113 (void) strlcpy(vdev, argv[0], sizeof (vdev));
1114 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
1117 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
1118 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
1119 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
1123 if (error || (stat(vdev, &st) != 0)) {
1124 (void) fprintf(stderr, gettext(
1125 "failed to find device %s, try specifying absolute "
1126 "path instead\n"), argv[0]);
1131 if ((fd = open(vdev, O_RDWR)) < 0) {
1132 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
1133 vdev, strerror(errno));
1138 * Flush all dirty pages for the block device. This should not be
1139 * fatal when the device does not support BLKFLSBUF as would be the
1140 * case for a file vdev.
1142 if ((ioctl(fd, BLKFLSBUF) != 0) && (errno != ENOTTY))
1143 (void) fprintf(stderr, gettext("failed to invalidate "
1144 "cache for %s: %s\n"), vdev, strerror(errno));
1146 if (zpool_read_label(fd, &config, NULL) != 0) {
1147 (void) fprintf(stderr,
1148 gettext("failed to read label from %s\n"), vdev);
1152 nvlist_free(config);
1154 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1156 (void) fprintf(stderr,
1157 gettext("failed to check state for %s\n"), vdev);
1167 case POOL_STATE_ACTIVE:
1168 case POOL_STATE_SPARE:
1169 case POOL_STATE_L2CACHE:
1170 (void) fprintf(stderr, gettext(
1171 "%s is a member (%s) of pool \"%s\"\n"),
1172 vdev, zpool_pool_state_to_name(state), name);
1176 case POOL_STATE_EXPORTED:
1179 (void) fprintf(stderr, gettext(
1180 "use '-f' to override the following error:\n"
1181 "%s is a member of exported pool \"%s\"\n"),
1186 case POOL_STATE_POTENTIALLY_ACTIVE:
1189 (void) fprintf(stderr, gettext(
1190 "use '-f' to override the following error:\n"
1191 "%s is a member of potentially active pool \"%s\"\n"),
1196 case POOL_STATE_DESTROYED:
1197 /* inuse should never be set for a destroyed pool */
1203 ret = zpool_clear_label(fd);
1205 (void) fprintf(stderr,
1206 gettext("failed to clear label for %s\n"), vdev);
1217 * zpool create [-fnd] [-o property=value] ...
1218 * [-O file-system-property=value] ...
1219 * [-R root] [-m mountpoint] <pool> <dev> ...
1221 * -f Force creation, even if devices appear in use
1222 * -n Do not create the pool, but display the resulting layout if it
1223 * were to be created.
1224 * -R Create a pool under an alternate root
1225 * -m Set default mountpoint for the root dataset. By default it's
1227 * -o Set property=value.
1228 * -o Set feature@feature=enabled|disabled.
1229 * -d Don't automatically enable all supported pool features
1230 * (individual features can be enabled with -o).
1231 * -O Set fsproperty=value in the pool's root file system
1233 * Creates the named pool according to the given vdev specification. The
1234 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1235 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1236 * (if '-n' was specified), or pass it to libzfs to do the creation.
1239 zpool_do_create(int argc, char **argv)
1241 boolean_t force = B_FALSE;
1242 boolean_t dryrun = B_FALSE;
1243 boolean_t enable_all_pool_feat = B_TRUE;
1245 nvlist_t *nvroot = NULL;
1249 char *altroot = NULL;
1250 char *mountpoint = NULL;
1251 nvlist_t *fsprops = NULL;
1252 nvlist_t *props = NULL;
1256 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1265 enable_all_pool_feat = B_FALSE;
1269 if (add_prop_list(zpool_prop_to_name(
1270 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1272 if (add_prop_list_default(zpool_prop_to_name(
1273 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1277 /* Equivalent to -O mountpoint=optarg */
1278 mountpoint = optarg;
1281 if ((propval = strchr(optarg, '=')) == NULL) {
1282 (void) fprintf(stderr, gettext("missing "
1283 "'=' for -o option\n"));
1289 if (add_prop_list(optarg, propval, &props, B_TRUE))
1293 * If the user is creating a pool that doesn't support
1294 * feature flags, don't enable any features.
1296 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1300 ver = strtoull(propval, &end, 10);
1302 ver < SPA_VERSION_FEATURES) {
1303 enable_all_pool_feat = B_FALSE;
1306 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1310 if ((propval = strchr(optarg, '=')) == NULL) {
1311 (void) fprintf(stderr, gettext("missing "
1312 "'=' for -O option\n"));
1319 * Mountpoints are checked and then added later.
1320 * Uniquely among properties, they can be specified
1321 * more than once, to avoid conflict with -m.
1323 if (0 == strcmp(optarg,
1324 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1325 mountpoint = propval;
1326 } else if (add_prop_list(optarg, propval, &fsprops,
1333 * Sanity check temporary pool name.
1335 if (strchr(optarg, '/') != NULL) {
1336 (void) fprintf(stderr, gettext("cannot create "
1337 "'%s': invalid character '/' in temporary "
1339 (void) fprintf(stderr, gettext("use 'zfs "
1340 "create' to create a dataset\n"));
1344 if (add_prop_list(zpool_prop_to_name(
1345 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1347 if (add_prop_list_default(zpool_prop_to_name(
1348 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1353 (void) fprintf(stderr, gettext("missing argument for "
1354 "'%c' option\n"), optopt);
1357 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1366 /* get pool name and check number of arguments */
1368 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1372 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1379 * As a special case, check for use of '/' in the name, and direct the
1380 * user to use 'zfs create' instead.
1382 if (strchr(poolname, '/') != NULL) {
1383 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1384 "character '/' in pool name\n"), poolname);
1385 (void) fprintf(stderr, gettext("use 'zfs create' to "
1386 "create a dataset\n"));
1390 /* pass off to get_vdev_spec for bulk processing */
1391 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1392 argc - 1, argv + 1);
1396 /* make_root_vdev() allows 0 toplevel children if there are spares */
1397 if (!zfs_allocatable_devs(nvroot)) {
1398 (void) fprintf(stderr, gettext("invalid vdev "
1399 "specification: at least one toplevel vdev must be "
1404 if (altroot != NULL && altroot[0] != '/') {
1405 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1406 "must be an absolute path\n"), altroot);
1411 * Check the validity of the mountpoint and direct the user to use the
1412 * '-m' mountpoint option if it looks like its in use.
1414 if (mountpoint == NULL ||
1415 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1416 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1417 char buf[MAXPATHLEN];
1420 if (mountpoint && mountpoint[0] != '/') {
1421 (void) fprintf(stderr, gettext("invalid mountpoint "
1422 "'%s': must be an absolute path, 'legacy', or "
1423 "'none'\n"), mountpoint);
1427 if (mountpoint == NULL) {
1428 if (altroot != NULL)
1429 (void) snprintf(buf, sizeof (buf), "%s/%s",
1432 (void) snprintf(buf, sizeof (buf), "/%s",
1435 if (altroot != NULL)
1436 (void) snprintf(buf, sizeof (buf), "%s%s",
1437 altroot, mountpoint);
1439 (void) snprintf(buf, sizeof (buf), "%s",
1443 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1444 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1445 "%s\n"), buf, strerror(errno));
1446 (void) fprintf(stderr, gettext("use '-m' "
1447 "option to provide a different default\n"));
1452 while (count < 3 && readdir(dirp) != NULL)
1454 (void) closedir(dirp);
1457 (void) fprintf(stderr, gettext("mountpoint "
1458 "'%s' exists and is not empty\n"), buf);
1459 (void) fprintf(stderr, gettext("use '-m' "
1460 "option to provide a "
1461 "different default\n"));
1468 * Now that the mountpoint's validity has been checked, ensure that
1469 * the property is set appropriately prior to creating the pool.
1471 if (mountpoint != NULL) {
1472 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1473 mountpoint, &fsprops, B_FALSE);
1481 * For a dry run invocation, print out a basic message and run
1482 * through all the vdevs in the list and print out in an
1483 * appropriate hierarchy.
1485 (void) printf(gettext("would create '%s' with the "
1486 "following layout:\n\n"), poolname);
1488 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1489 print_vdev_tree(NULL, "dedup", nvroot, 0,
1490 VDEV_ALLOC_BIAS_DEDUP, 0);
1491 print_vdev_tree(NULL, "special", nvroot, 0,
1492 VDEV_ALLOC_BIAS_SPECIAL, 0);
1493 print_vdev_tree(NULL, "logs", nvroot, 0,
1494 VDEV_ALLOC_BIAS_LOG, 0);
1499 * Hand off to libzfs.
1502 for (i = 0; i < SPA_FEATURES; i++) {
1503 char propname[MAXPATHLEN];
1505 zfeature_info_t *feat = &spa_feature_table[i];
1507 (void) snprintf(propname, sizeof (propname),
1508 "feature@%s", feat->fi_uname);
1511 * Only features contained in props will be enabled:
1512 * remove from the nvlist every ZFS_FEATURE_DISABLED
1513 * value and add every missing ZFS_FEATURE_ENABLED if
1514 * enable_all_pool_feat is set.
1516 if (!nvlist_lookup_string(props, propname, &propval)) {
1517 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1518 (void) nvlist_remove_all(props,
1520 } else if (enable_all_pool_feat) {
1521 ret = add_prop_list(propname,
1522 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1529 if (zpool_create(g_zfs, poolname,
1530 nvroot, props, fsprops) == 0) {
1531 zfs_handle_t *pool = zfs_open(g_zfs,
1532 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1534 if (zfs_mount(pool, NULL, 0) == 0)
1535 ret = zfs_shareall(pool);
1538 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1539 (void) fprintf(stderr, gettext("pool name may have "
1545 nvlist_free(nvroot);
1546 nvlist_free(fsprops);
1550 nvlist_free(fsprops);
1557 * zpool destroy <pool>
1559 * -f Forcefully unmount any datasets
1561 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1564 zpool_do_destroy(int argc, char **argv)
1566 boolean_t force = B_FALSE;
1569 zpool_handle_t *zhp;
1573 while ((c = getopt(argc, argv, "f")) != -1) {
1579 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1588 /* check arguments */
1590 (void) fprintf(stderr, gettext("missing pool argument\n"));
1594 (void) fprintf(stderr, gettext("too many arguments\n"));
1600 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1602 * As a special case, check for use of '/' in the name, and
1603 * direct the user to use 'zfs destroy' instead.
1605 if (strchr(pool, '/') != NULL)
1606 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1607 "destroy a dataset\n"));
1611 if (zpool_disable_datasets(zhp, force) != 0) {
1612 (void) fprintf(stderr, gettext("could not destroy '%s': "
1613 "could not unmount datasets\n"), zpool_get_name(zhp));
1618 /* The history must be logged as part of the export */
1619 log_history = B_FALSE;
1621 ret = (zpool_destroy(zhp, history_str) != 0);
1628 typedef struct export_cbdata {
1630 boolean_t hardforce;
1637 zpool_export_one(zpool_handle_t *zhp, void *data)
1639 export_cbdata_t *cb = data;
1641 if (zpool_disable_datasets(zhp, cb->force) != 0)
1644 /* The history must be logged as part of the export */
1645 log_history = B_FALSE;
1647 if (cb->hardforce) {
1648 if (zpool_export_force(zhp, history_str) != 0)
1650 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1658 * zpool export [-f] <pool> ...
1660 * -a Export all pools
1661 * -f Forcefully unmount datasets
1663 * Export the given pools. By default, the command will attempt to cleanly
1664 * unmount any active datasets within the pool. If the '-f' flag is specified,
1665 * then the datasets will be forcefully unmounted.
1668 zpool_do_export(int argc, char **argv)
1671 boolean_t do_all = B_FALSE;
1672 boolean_t force = B_FALSE;
1673 boolean_t hardforce = B_FALSE;
1677 while ((c = getopt(argc, argv, "afF")) != -1) {
1689 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1696 cb.hardforce = hardforce;
1702 (void) fprintf(stderr, gettext("too many arguments\n"));
1706 return (for_each_pool(argc, argv, B_TRUE, NULL,
1707 zpool_export_one, &cb));
1710 /* check arguments */
1712 (void) fprintf(stderr, gettext("missing pool argument\n"));
1716 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1722 * Given a vdev configuration, determine the maximum width needed for the device
1726 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1734 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1735 if (strlen(name) + depth > max)
1736 max = strlen(name) + depth;
1740 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1741 &child, &children) == 0) {
1742 for (c = 0; c < children; c++)
1743 if ((ret = max_width(zhp, child[c], depth + 2,
1744 max, name_flags)) > max)
1748 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1749 &child, &children) == 0) {
1750 for (c = 0; c < children; c++)
1751 if ((ret = max_width(zhp, child[c], depth + 2,
1752 max, name_flags)) > max)
1756 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1757 &child, &children) == 0) {
1758 for (c = 0; c < children; c++)
1759 if ((ret = max_width(zhp, child[c], depth + 2,
1760 max, name_flags)) > max)
1767 typedef struct spare_cbdata {
1769 zpool_handle_t *cb_zhp;
1773 find_vdev(nvlist_t *nv, uint64_t search)
1779 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1783 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1784 &child, &children) == 0) {
1785 for (c = 0; c < children; c++)
1786 if (find_vdev(child[c], search))
1794 find_spare(zpool_handle_t *zhp, void *data)
1796 spare_cbdata_t *cbp = data;
1797 nvlist_t *config, *nvroot;
1799 config = zpool_get_config(zhp, NULL);
1800 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1803 if (find_vdev(nvroot, cbp->cb_guid)) {
1812 typedef struct status_cbdata {
1816 boolean_t cb_allpools;
1817 boolean_t cb_verbose;
1818 boolean_t cb_literal;
1819 boolean_t cb_explain;
1821 boolean_t cb_dedup_stats;
1822 boolean_t cb_print_status;
1823 boolean_t cb_print_slow_ios;
1824 boolean_t cb_print_vdev_init;
1825 boolean_t cb_print_vdev_trim;
1826 vdev_cmd_data_list_t *vcdl;
1829 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1831 is_blank_str(char *str)
1833 while (str != NULL && *str != '\0') {
1841 /* Print command output lines for specific vdev in a specific pool */
1843 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1845 vdev_cmd_data_t *data;
1849 for (i = 0; i < vcdl->count; i++) {
1850 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1851 (strcmp(vcdl->data[i].pool, pool) != 0)) {
1852 /* Not the vdev we're looking for */
1856 data = &vcdl->data[i];
1857 /* Print out all the output values for this vdev */
1858 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1860 /* Does this vdev have values for this column? */
1861 for (int k = 0; k < data->cols_cnt; k++) {
1862 if (strcmp(data->cols[k],
1863 vcdl->uniq_cols[j]) == 0) {
1864 /* yes it does, record the value */
1865 val = data->lines[k];
1870 * Mark empty values with dashes to make output
1873 if (is_blank_str(val))
1876 printf("%*s", vcdl->uniq_cols_width[j], val);
1877 if (j < vcdl->uniq_cols_cnt - 1)
1881 /* Print out any values that aren't in a column at the end */
1882 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1883 /* Did we have any columns? If so print a spacer. */
1884 if (vcdl->uniq_cols_cnt > 0)
1887 val = data->lines[j];
1888 printf("%s", val ? val : "");
1895 * Print vdev initialization status for leaves
1898 print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
1901 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
1902 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
1903 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
1904 !vs->vs_scan_removing) {
1907 struct tm zaction_ts;
1909 time_t t = vs->vs_initialize_action_time;
1910 int initialize_pct = 100;
1911 if (vs->vs_initialize_state !=
1912 VDEV_INITIALIZE_COMPLETE) {
1913 initialize_pct = (vs->vs_initialize_bytes_done *
1914 100 / (vs->vs_initialize_bytes_est + 1));
1917 (void) localtime_r(&t, &zaction_ts);
1918 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1920 switch (vs->vs_initialize_state) {
1921 case VDEV_INITIALIZE_SUSPENDED:
1922 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1923 gettext("suspended, started at"), tbuf);
1925 case VDEV_INITIALIZE_ACTIVE:
1926 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1927 gettext("started at"), tbuf);
1929 case VDEV_INITIALIZE_COMPLETE:
1930 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1931 gettext("completed at"), tbuf);
1935 (void) printf(gettext(" (%d%% initialized%s)"),
1936 initialize_pct, zbuf);
1938 (void) printf(gettext(" (uninitialized)"));
1940 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
1941 (void) printf(gettext(" (initializing)"));
1946 * Print vdev TRIM status for leaves
1949 print_status_trim(vdev_stat_t *vs, boolean_t verbose)
1952 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
1953 vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
1954 vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
1955 !vs->vs_scan_removing) {
1958 struct tm zaction_ts;
1960 time_t t = vs->vs_trim_action_time;
1962 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
1963 trim_pct = (vs->vs_trim_bytes_done *
1964 100 / (vs->vs_trim_bytes_est + 1));
1967 (void) localtime_r(&t, &zaction_ts);
1968 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1970 switch (vs->vs_trim_state) {
1971 case VDEV_TRIM_SUSPENDED:
1972 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1973 gettext("suspended, started at"), tbuf);
1975 case VDEV_TRIM_ACTIVE:
1976 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1977 gettext("started at"), tbuf);
1979 case VDEV_TRIM_COMPLETE:
1980 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
1981 gettext("completed at"), tbuf);
1985 (void) printf(gettext(" (%d%% trimmed%s)"),
1987 } else if (vs->vs_trim_notsup) {
1988 (void) printf(gettext(" (trim unsupported)"));
1990 (void) printf(gettext(" (untrimmed)"));
1992 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
1993 (void) printf(gettext(" (trimming)"));
1998 * Print out configuration state as requested by status_callback.
2001 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
2002 nvlist_t *nv, int depth, boolean_t isspare)
2004 nvlist_t **child, *root;
2006 pool_scan_stat_t *ps = NULL;
2008 char rbuf[6], wbuf[6], cbuf[6];
2010 uint64_t notpresent;
2011 spare_cbdata_t spare_cb;
2016 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2017 &child, &children) != 0)
2020 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2021 (uint64_t **)&vs, &c) == 0);
2023 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2025 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2028 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2031 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2034 if (vs->vs_aux == VDEV_AUX_SPARED)
2036 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2040 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
2044 if (cb->cb_literal) {
2045 printf(" %5llu %5llu %5llu",
2046 (u_longlong_t)vs->vs_read_errors,
2047 (u_longlong_t)vs->vs_write_errors,
2048 (u_longlong_t)vs->vs_checksum_errors);
2050 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2051 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2052 zfs_nicenum(vs->vs_checksum_errors, cbuf,
2054 printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2057 if (cb->cb_print_slow_ios) {
2058 if (children == 0) {
2059 /* Only leafs vdevs have slow IOs */
2060 zfs_nicenum(vs->vs_slow_ios, rbuf,
2063 snprintf(rbuf, sizeof (rbuf), "-");
2067 printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
2069 printf(" %5s", rbuf);
2074 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2075 ¬present) == 0) {
2076 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2077 (void) printf(" was %s", path);
2078 } else if (vs->vs_aux != 0) {
2081 switch (vs->vs_aux) {
2082 case VDEV_AUX_OPEN_FAILED:
2083 (void) printf(gettext("cannot open"));
2086 case VDEV_AUX_BAD_GUID_SUM:
2087 (void) printf(gettext("missing device"));
2090 case VDEV_AUX_NO_REPLICAS:
2091 (void) printf(gettext("insufficient replicas"));
2094 case VDEV_AUX_VERSION_NEWER:
2095 (void) printf(gettext("newer version"));
2098 case VDEV_AUX_UNSUP_FEAT:
2099 (void) printf(gettext("unsupported feature(s)"));
2102 case VDEV_AUX_SPARED:
2103 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2104 &spare_cb.cb_guid) == 0);
2105 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
2106 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
2107 zpool_get_name(zhp)) == 0)
2108 (void) printf(gettext("currently in "
2111 (void) printf(gettext("in use by "
2113 zpool_get_name(spare_cb.cb_zhp));
2114 zpool_close(spare_cb.cb_zhp);
2116 (void) printf(gettext("currently in use"));
2120 case VDEV_AUX_ERR_EXCEEDED:
2121 (void) printf(gettext("too many errors"));
2124 case VDEV_AUX_IO_FAILURE:
2125 (void) printf(gettext("experienced I/O failures"));
2128 case VDEV_AUX_BAD_LOG:
2129 (void) printf(gettext("bad intent log"));
2132 case VDEV_AUX_EXTERNAL:
2133 (void) printf(gettext("external device fault"));
2136 case VDEV_AUX_SPLIT_POOL:
2137 (void) printf(gettext("split into new pool"));
2140 case VDEV_AUX_ACTIVE:
2141 (void) printf(gettext("currently in use"));
2144 case VDEV_AUX_CHILDREN_OFFLINE:
2145 (void) printf(gettext("all children offline"));
2149 (void) printf(gettext("corrupted data"));
2154 /* The root vdev has the scrub/resilver stats */
2155 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2156 ZPOOL_CONFIG_VDEV_TREE);
2157 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
2158 (uint64_t **)&ps, &c);
2160 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) {
2161 if (vs->vs_scan_processed != 0) {
2162 (void) printf(gettext(" (%s)"),
2163 (ps->pss_func == POOL_SCAN_RESILVER) ?
2164 "resilvering" : "repairing");
2165 } else if (vs->vs_resilver_deferred) {
2166 (void) printf(gettext(" (awaiting resilver)"));
2170 if (cb->vcdl != NULL) {
2171 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2173 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
2177 /* Display vdev initialization and trim status for leaves */
2178 if (children == 0) {
2179 print_status_initialize(vs, cb->cb_print_vdev_init);
2180 print_status_trim(vs, cb->cb_print_vdev_trim);
2183 (void) printf("\n");
2185 for (c = 0; c < children; c++) {
2186 uint64_t islog = B_FALSE, ishole = B_FALSE;
2188 /* Don't print logs or holes here */
2189 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2191 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2193 if (islog || ishole)
2195 /* Only print normal classes here */
2196 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2199 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2200 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2202 print_status_config(zhp, cb, vname, child[c], depth + 2,
2209 * Print the configuration of an exported pool. Iterate over all vdevs in the
2210 * pool, printing out the name and status for each one.
2213 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
2221 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2222 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
2223 strcmp(type, VDEV_TYPE_HOLE) == 0)
2226 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2227 (uint64_t **)&vs, &c) == 0);
2229 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
2230 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
2232 if (vs->vs_aux != 0) {
2235 switch (vs->vs_aux) {
2236 case VDEV_AUX_OPEN_FAILED:
2237 (void) printf(gettext("cannot open"));
2240 case VDEV_AUX_BAD_GUID_SUM:
2241 (void) printf(gettext("missing device"));
2244 case VDEV_AUX_NO_REPLICAS:
2245 (void) printf(gettext("insufficient replicas"));
2248 case VDEV_AUX_VERSION_NEWER:
2249 (void) printf(gettext("newer version"));
2252 case VDEV_AUX_UNSUP_FEAT:
2253 (void) printf(gettext("unsupported feature(s)"));
2256 case VDEV_AUX_ERR_EXCEEDED:
2257 (void) printf(gettext("too many errors"));
2260 case VDEV_AUX_ACTIVE:
2261 (void) printf(gettext("currently in use"));
2264 case VDEV_AUX_CHILDREN_OFFLINE:
2265 (void) printf(gettext("all children offline"));
2269 (void) printf(gettext("corrupted data"));
2273 (void) printf("\n");
2275 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2276 &child, &children) != 0)
2279 for (c = 0; c < children; c++) {
2280 uint64_t is_log = B_FALSE;
2282 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2286 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2289 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2290 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2291 print_import_config(cb, vname, child[c], depth + 2);
2295 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2296 &child, &children) == 0) {
2297 (void) printf(gettext("\tcache\n"));
2298 for (c = 0; c < children; c++) {
2299 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2301 (void) printf("\t %s\n", vname);
2306 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2307 &child, &children) == 0) {
2308 (void) printf(gettext("\tspares\n"));
2309 for (c = 0; c < children; c++) {
2310 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2312 (void) printf("\t %s\n", vname);
2319 * Print specialized class vdevs.
2321 * These are recorded as top level vdevs in the main pool child array
2322 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2323 * print_status_config() or print_import_config() to print the top level
2324 * class vdevs then any of their children (eg mirrored slogs) are printed
2325 * recursively - which works because only the top level vdev is marked.
2328 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2333 boolean_t printed = B_FALSE;
2335 assert(zhp != NULL || !cb->cb_verbose);
2337 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2341 for (c = 0; c < children; c++) {
2342 uint64_t is_log = B_FALSE;
2346 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2350 bias = VDEV_ALLOC_CLASS_LOGS;
2352 (void) nvlist_lookup_string(child[c],
2353 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2354 (void) nvlist_lookup_string(child[c],
2355 ZPOOL_CONFIG_TYPE, &type);
2358 if (bias == NULL || strcmp(bias, class) != 0)
2360 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2364 (void) printf("\t%s\t\n", gettext(class));
2368 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
2369 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2370 if (cb->cb_print_status)
2371 print_status_config(zhp, cb, name, child[c], 2,
2374 print_import_config(cb, name, child[c], 2);
2380 * Display the status for the given pool.
2383 show_import(nvlist_t *config)
2385 uint64_t pool_state;
2389 uint64_t hostid = 0;
2391 char *hostname = "unknown";
2392 nvlist_t *nvroot, *nvinfo;
2393 zpool_status_t reason;
2394 zpool_errata_t errata;
2398 status_cbdata_t cb = { 0 };
2400 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2402 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2404 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2406 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2409 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2410 (uint64_t **)&vs, &vsc) == 0);
2411 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2413 reason = zpool_import_status(config, &msgid, &errata);
2415 (void) printf(gettext(" pool: %s\n"), name);
2416 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2417 (void) printf(gettext(" state: %s"), health);
2418 if (pool_state == POOL_STATE_DESTROYED)
2419 (void) printf(gettext(" (DESTROYED)"));
2420 (void) printf("\n");
2423 case ZPOOL_STATUS_MISSING_DEV_R:
2424 case ZPOOL_STATUS_MISSING_DEV_NR:
2425 case ZPOOL_STATUS_BAD_GUID_SUM:
2426 (void) printf(gettext(" status: One or more devices are "
2427 "missing from the system.\n"));
2430 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2431 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2432 (void) printf(gettext(" status: One or more devices contains "
2433 "corrupted data.\n"));
2436 case ZPOOL_STATUS_CORRUPT_DATA:
2438 gettext(" status: The pool data is corrupted.\n"));
2441 case ZPOOL_STATUS_OFFLINE_DEV:
2442 (void) printf(gettext(" status: One or more devices "
2443 "are offlined.\n"));
2446 case ZPOOL_STATUS_CORRUPT_POOL:
2447 (void) printf(gettext(" status: The pool metadata is "
2451 case ZPOOL_STATUS_VERSION_OLDER:
2452 (void) printf(gettext(" status: The pool is formatted using a "
2453 "legacy on-disk version.\n"));
2456 case ZPOOL_STATUS_VERSION_NEWER:
2457 (void) printf(gettext(" status: The pool is formatted using an "
2458 "incompatible version.\n"));
2461 case ZPOOL_STATUS_FEAT_DISABLED:
2462 (void) printf(gettext(" status: Some supported features are "
2463 "not enabled on the pool.\n"));
2466 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2467 (void) printf(gettext("status: The pool uses the following "
2468 "feature(s) not supported on this system:\n"));
2469 zpool_print_unsup_feat(config);
2472 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2473 (void) printf(gettext("status: The pool can only be accessed "
2474 "in read-only mode on this system. It\n\tcannot be "
2475 "accessed in read-write mode because it uses the "
2476 "following\n\tfeature(s) not supported on this system:\n"));
2477 zpool_print_unsup_feat(config);
2480 case ZPOOL_STATUS_HOSTID_ACTIVE:
2481 (void) printf(gettext(" status: The pool is currently "
2482 "imported by another system.\n"));
2485 case ZPOOL_STATUS_HOSTID_REQUIRED:
2486 (void) printf(gettext(" status: The pool has the "
2487 "multihost property on. It cannot\n\tbe safely imported "
2488 "when the system hostid is not set.\n"));
2491 case ZPOOL_STATUS_HOSTID_MISMATCH:
2492 (void) printf(gettext(" status: The pool was last accessed by "
2493 "another system.\n"));
2496 case ZPOOL_STATUS_FAULTED_DEV_R:
2497 case ZPOOL_STATUS_FAULTED_DEV_NR:
2498 (void) printf(gettext(" status: One or more devices are "
2502 case ZPOOL_STATUS_BAD_LOG:
2503 (void) printf(gettext(" status: An intent log record cannot be "
2507 case ZPOOL_STATUS_RESILVERING:
2508 (void) printf(gettext(" status: One or more devices were being "
2512 case ZPOOL_STATUS_ERRATA:
2513 (void) printf(gettext(" status: Errata #%d detected.\n"),
2519 * No other status can be seen when importing pools.
2521 assert(reason == ZPOOL_STATUS_OK);
2525 * Print out an action according to the overall state of the pool.
2527 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2528 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2529 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2530 (void) printf(gettext(" action: The pool can be "
2531 "imported using its name or numeric identifier, "
2532 "though\n\tsome features will not be available "
2533 "without an explicit 'zpool upgrade'.\n"));
2534 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2535 (void) printf(gettext(" action: The pool can be "
2536 "imported using its name or numeric "
2537 "identifier and\n\tthe '-f' flag.\n"));
2538 } else if (reason == ZPOOL_STATUS_ERRATA) {
2540 case ZPOOL_ERRATA_NONE:
2543 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2544 (void) printf(gettext(" action: The pool can "
2545 "be imported using its name or numeric "
2546 "identifier,\n\thowever there is a compat"
2547 "ibility issue which should be corrected"
2548 "\n\tby running 'zpool scrub'\n"));
2551 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2552 (void) printf(gettext(" action: The pool can"
2553 "not be imported with this version of ZFS "
2554 "due to\n\tan active asynchronous destroy. "
2555 "Revert to an earlier version\n\tand "
2556 "allow the destroy to complete before "
2560 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2561 (void) printf(gettext(" action: Existing "
2562 "encrypted datasets contain an on-disk "
2563 "incompatibility, which\n\tneeds to be "
2564 "corrected. Backup these datasets to new "
2565 "encrypted datasets\n\tand destroy the "
2569 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
2570 (void) printf(gettext(" action: Existing "
2571 "encrypted snapshots and bookmarks contain "
2572 "an on-disk\n\tincompatibility. This may "
2573 "cause on-disk corruption if they are used"
2574 "\n\twith 'zfs recv'. To correct the "
2575 "issue, enable the bookmark_v2 feature.\n\t"
2576 "No additional action is needed if there "
2577 "are no encrypted snapshots or\n\t"
2578 "bookmarks. If preserving the encrypted "
2579 "snapshots and bookmarks is\n\trequired, "
2580 "use a non-raw send to backup and restore "
2581 "them. Alternately,\n\tthey may be removed"
2582 " to resolve the incompatibility.\n"));
2586 * All errata must contain an action message.
2591 (void) printf(gettext(" action: The pool can be "
2592 "imported using its name or numeric "
2595 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2596 (void) printf(gettext(" action: The pool can be imported "
2597 "despite missing or damaged devices. The\n\tfault "
2598 "tolerance of the pool may be compromised if imported.\n"));
2601 case ZPOOL_STATUS_VERSION_NEWER:
2602 (void) printf(gettext(" action: The pool cannot be "
2603 "imported. Access the pool on a system running "
2604 "newer\n\tsoftware, or recreate the pool from "
2607 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2608 (void) printf(gettext("action: The pool cannot be "
2609 "imported. Access the pool on a system that "
2610 "supports\n\tthe required feature(s), or recreate "
2611 "the pool from backup.\n"));
2613 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2614 (void) printf(gettext("action: The pool cannot be "
2615 "imported in read-write mode. Import the pool "
2617 "\t\"-o readonly=on\", access the pool on a system "
2618 "that supports the\n\trequired feature(s), or "
2619 "recreate the pool from backup.\n"));
2621 case ZPOOL_STATUS_MISSING_DEV_R:
2622 case ZPOOL_STATUS_MISSING_DEV_NR:
2623 case ZPOOL_STATUS_BAD_GUID_SUM:
2624 (void) printf(gettext(" action: The pool cannot be "
2625 "imported. Attach the missing\n\tdevices and try "
2628 case ZPOOL_STATUS_HOSTID_ACTIVE:
2629 VERIFY0(nvlist_lookup_nvlist(config,
2630 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2632 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2633 hostname = fnvlist_lookup_string(nvinfo,
2634 ZPOOL_CONFIG_MMP_HOSTNAME);
2636 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2637 hostid = fnvlist_lookup_uint64(nvinfo,
2638 ZPOOL_CONFIG_MMP_HOSTID);
2640 (void) printf(gettext(" action: The pool must be "
2641 "exported from %s (hostid=%lx)\n\tbefore it "
2642 "can be safely imported.\n"), hostname,
2643 (unsigned long) hostid);
2645 case ZPOOL_STATUS_HOSTID_REQUIRED:
2646 (void) printf(gettext(" action: Set a unique system "
2647 "hostid with the zgenhostid(8) command.\n"));
2650 (void) printf(gettext(" action: The pool cannot be "
2651 "imported due to damaged devices or data.\n"));
2655 /* Print the comment attached to the pool. */
2656 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2657 (void) printf(gettext("comment: %s\n"), comment);
2660 * If the state is "closed" or "can't open", and the aux state
2661 * is "corrupt data":
2663 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2664 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2665 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2666 if (pool_state == POOL_STATE_DESTROYED)
2667 (void) printf(gettext("\tThe pool was destroyed, "
2668 "but can be imported using the '-Df' flags.\n"));
2669 else if (pool_state != POOL_STATE_EXPORTED)
2670 (void) printf(gettext("\tThe pool may be active on "
2671 "another system, but can be imported using\n\t"
2672 "the '-f' flag.\n"));
2676 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2679 (void) printf(gettext(" config:\n\n"));
2681 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2683 if (cb.cb_namewidth < 10)
2684 cb.cb_namewidth = 10;
2686 print_import_config(&cb, name, nvroot, 0);
2688 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
2689 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
2690 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
2692 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2693 (void) printf(gettext("\n\tAdditional devices are known to "
2694 "be part of this pool, though their\n\texact "
2695 "configuration cannot be determined.\n"));
2700 zfs_force_import_required(nvlist_t *config)
2703 uint64_t hostid = 0;
2706 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2707 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2709 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2712 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2713 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2714 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2715 ZPOOL_CONFIG_MMP_STATE);
2717 if (mmp_state != MMP_STATE_INACTIVE)
2725 * Perform the import for the given configuration. This passes the heavy
2726 * lifting off to zpool_import_props(), and then mounts the datasets contained
2730 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2731 nvlist_t *props, int flags)
2734 zpool_handle_t *zhp;
2738 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2739 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2741 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2742 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2743 "is formatted using an unsupported ZFS version\n"), name);
2745 } else if (zfs_force_import_required(config) &&
2746 !(flags & ZFS_IMPORT_ANY_HOST)) {
2747 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2750 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2751 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2752 mmp_state = fnvlist_lookup_uint64(nvinfo,
2753 ZPOOL_CONFIG_MMP_STATE);
2755 if (mmp_state == MMP_STATE_ACTIVE) {
2756 char *hostname = "<unknown>";
2757 uint64_t hostid = 0;
2759 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2760 hostname = fnvlist_lookup_string(nvinfo,
2761 ZPOOL_CONFIG_MMP_HOSTNAME);
2763 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2764 hostid = fnvlist_lookup_uint64(nvinfo,
2765 ZPOOL_CONFIG_MMP_HOSTID);
2767 (void) fprintf(stderr, gettext("cannot import '%s': "
2768 "pool is imported on %s (hostid: "
2769 "0x%lx)\nExport the pool on the other system, "
2770 "then run 'zpool import'.\n"),
2771 name, hostname, (unsigned long) hostid);
2772 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2773 (void) fprintf(stderr, gettext("Cannot import '%s': "
2774 "pool has the multihost property on and the\n"
2775 "system's hostid is not set. Set a unique hostid "
2776 "with the zgenhostid(8) command.\n"), name);
2778 char *hostname = "<unknown>";
2779 uint64_t timestamp = 0;
2780 uint64_t hostid = 0;
2782 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2783 hostname = fnvlist_lookup_string(config,
2784 ZPOOL_CONFIG_HOSTNAME);
2786 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2787 timestamp = fnvlist_lookup_uint64(config,
2788 ZPOOL_CONFIG_TIMESTAMP);
2790 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2791 hostid = fnvlist_lookup_uint64(config,
2792 ZPOOL_CONFIG_HOSTID);
2794 (void) fprintf(stderr, gettext("cannot import '%s': "
2795 "pool was previously in use from another system.\n"
2796 "Last accessed by %s (hostid=%lx) at %s"
2797 "The pool can be imported, use 'zpool import -f' "
2798 "to import the pool.\n"), name, hostname,
2799 (unsigned long)hostid, ctime((time_t *)×tamp));
2805 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2808 if (newname != NULL)
2809 name = (char *)newname;
2811 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2815 * Loading keys is best effort. We don't want to return immediately
2816 * if it fails but we do want to give the error to the caller.
2818 if (flags & ZFS_IMPORT_LOAD_KEYS) {
2819 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2824 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2825 !(flags & ZFS_IMPORT_ONLY) &&
2826 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2835 typedef struct target_exists_args {
2836 const char *poolname;
2838 } target_exists_args_t;
2841 name_or_guid_exists(zpool_handle_t *zhp, void *data)
2843 target_exists_args_t *args = data;
2844 nvlist_t *config = zpool_get_config(zhp, NULL);
2850 if (args->poolname != NULL) {
2853 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2855 if (strcmp(pool_name, args->poolname) == 0)
2860 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2862 if (pool_guid == args->poolguid)
2870 * zpool checkpoint <pool>
2871 * checkpoint --discard <pool>
2873 * -d Discard the checkpoint from a checkpointed
2876 * Checkpoints the specified pool, by taking a "snapshot" of its
2877 * current state. A pool can only have one checkpoint at a time.
2880 zpool_do_checkpoint(int argc, char **argv)
2884 zpool_handle_t *zhp;
2887 struct option long_options[] = {
2888 {"discard", no_argument, NULL, 'd'},
2893 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2899 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2909 (void) fprintf(stderr, gettext("missing pool argument\n"));
2914 (void) fprintf(stderr, gettext("too many arguments\n"));
2920 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2921 /* As a special case, check for use of '/' in the name */
2922 if (strchr(pool, '/') != NULL)
2923 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2924 "doesn't work on datasets. To save the state "
2925 "of a dataset from a specific point in time "
2926 "please use 'zfs snapshot'\n"));
2931 err = (zpool_discard_checkpoint(zhp) != 0);
2933 err = (zpool_checkpoint(zhp) != 0);
2940 #define CHECKPOINT_OPT 1024
2943 * zpool import [-d dir] [-D]
2944 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2945 * [-d dir | -c cachefile] [-f] -a
2946 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2947 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2949 * -c Read pool information from a cachefile instead of searching
2952 * -d Scan in a specific directory, other than /dev/. More than
2953 * one directory can be specified using multiple '-d' options.
2955 * -D Scan for previously destroyed pools or import all or only
2956 * specified destroyed pools.
2958 * -R Temporarily import the pool, with all mountpoints relative to
2959 * the given root. The pool will remain exported when the machine
2962 * -V Import even in the presence of faulted vdevs. This is an
2963 * intentionally undocumented option for testing purposes, and
2964 * treats the pool configuration as complete, leaving any bad
2965 * vdevs in the FAULTED state. In other words, it does verbatim
2968 * -f Force import, even if it appears that the pool is active.
2970 * -F Attempt rewind if necessary.
2972 * -n See if rewind would work, but don't actually rewind.
2974 * -N Import the pool but don't mount datasets.
2976 * -T Specify a starting txg to use for import. This option is
2977 * intentionally undocumented option for testing purposes.
2979 * -a Import all pools found.
2981 * -l Load encryption keys while importing.
2983 * -o Set property=value and/or temporary mount options (without '=').
2985 * -s Scan using the default search path, the libblkid cache will
2988 * --rewind-to-checkpoint
2989 * Import the pool and revert back to the checkpoint.
2991 * The import command scans for pools to import, and import pools based on pool
2992 * name and GUID. The pool can also be renamed as part of the import process.
2995 zpool_do_import(int argc, char **argv)
2997 char **searchdirs = NULL;
2998 char *env, *envdup = NULL;
3002 nvlist_t *pools = NULL;
3003 boolean_t do_all = B_FALSE;
3004 boolean_t do_destroyed = B_FALSE;
3005 char *mntopts = NULL;
3008 uint64_t searchguid = 0;
3009 char *searchname = NULL;
3011 nvlist_t *found_config;
3012 nvlist_t *policy = NULL;
3013 nvlist_t *props = NULL;
3015 int flags = ZFS_IMPORT_NORMAL;
3016 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3017 boolean_t dryrun = B_FALSE;
3018 boolean_t do_rewind = B_FALSE;
3019 boolean_t xtreme_rewind = B_FALSE;
3020 boolean_t do_scan = B_FALSE;
3021 boolean_t pool_exists = B_FALSE;
3022 uint64_t pool_state, txg = -1ULL;
3023 char *cachefile = NULL;
3024 importargs_t idata = { 0 };
3027 struct option long_options[] = {
3028 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
3033 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
3034 long_options, NULL)) != -1) {
3043 if (searchdirs == NULL) {
3044 searchdirs = safe_malloc(sizeof (char *));
3046 char **tmp = safe_malloc((nsearch + 1) *
3048 bcopy(searchdirs, tmp, nsearch *
3053 searchdirs[nsearch++] = optarg;
3056 do_destroyed = B_TRUE;
3059 flags |= ZFS_IMPORT_ANY_HOST;
3065 flags |= ZFS_IMPORT_LOAD_KEYS;
3068 flags |= ZFS_IMPORT_MISSING_LOG;
3074 flags |= ZFS_IMPORT_ONLY;
3077 if ((propval = strchr(optarg, '=')) != NULL) {
3080 if (add_prop_list(optarg, propval,
3088 if (add_prop_list(zpool_prop_to_name(
3089 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
3091 if (add_prop_list_default(zpool_prop_to_name(
3092 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
3099 flags |= ZFS_IMPORT_TEMP_NAME;
3100 if (add_prop_list_default(zpool_prop_to_name(
3101 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
3107 txg = strtoull(optarg, &endptr, 0);
3108 if (errno != 0 || *endptr != '\0') {
3109 (void) fprintf(stderr,
3110 gettext("invalid txg value\n"));
3113 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
3116 flags |= ZFS_IMPORT_VERBATIM;
3119 xtreme_rewind = B_TRUE;
3121 case CHECKPOINT_OPT:
3122 flags |= ZFS_IMPORT_CHECKPOINT;
3125 (void) fprintf(stderr, gettext("missing argument for "
3126 "'%c' option\n"), optopt);
3130 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3139 if (cachefile && nsearch != 0) {
3140 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
3144 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
3145 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
3149 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
3150 (void) fprintf(stderr, gettext("-l is only meaningful during "
3155 if ((dryrun || xtreme_rewind) && !do_rewind) {
3156 (void) fprintf(stderr,
3157 gettext("-n or -X only meaningful with -F\n"));
3161 rewind_policy = ZPOOL_TRY_REWIND;
3163 rewind_policy = ZPOOL_DO_REWIND;
3165 rewind_policy |= ZPOOL_EXTREME_REWIND;
3167 /* In the future, we can capture further policy and include it here */
3168 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3169 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
3170 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
3171 rewind_policy) != 0)
3174 /* check argument count */
3177 (void) fprintf(stderr, gettext("too many arguments\n"));
3182 (void) fprintf(stderr, gettext("too many arguments\n"));
3188 * Check for the effective uid. We do this explicitly here because
3189 * otherwise any attempt to discover pools will silently fail.
3191 if (argc == 0 && geteuid() != 0) {
3192 (void) fprintf(stderr, gettext("cannot "
3193 "discover pools: permission denied\n"));
3194 if (searchdirs != NULL)
3198 nvlist_free(policy);
3203 * Depending on the arguments given, we do one of the following:
3205 * <none> Iterate through all pools and display information about
3208 * -a Iterate through all pools and try to import each one.
3210 * <id> Find the pool that corresponds to the given GUID/pool
3211 * name and import that one.
3213 * -D Above options applies only to destroyed pools.
3219 searchguid = strtoull(argv[0], &endptr, 10);
3220 if (errno != 0 || *endptr != '\0') {
3221 searchname = argv[0];
3224 found_config = NULL;
3227 * User specified a name or guid. Ensure it's unique.
3229 target_exists_args_t search = {searchname, searchguid};
3230 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
3234 * Check the environment for the preferred search path.
3236 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
3239 envdup = strdup(env);
3241 dir = strtok(envdup, ":");
3242 while (dir != NULL) {
3243 if (searchdirs == NULL) {
3244 searchdirs = safe_malloc(sizeof (char *));
3246 char **tmp = safe_malloc((nsearch + 1) *
3248 bcopy(searchdirs, tmp, nsearch *
3253 searchdirs[nsearch++] = dir;
3254 dir = strtok(NULL, ":");
3258 idata.path = searchdirs;
3259 idata.paths = nsearch;
3260 idata.poolname = searchname;
3261 idata.guid = searchguid;
3262 idata.cachefile = cachefile;
3263 idata.scan = do_scan;
3264 idata.policy = policy;
3266 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
3268 if (pools != NULL && pool_exists &&
3269 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
3270 (void) fprintf(stderr, gettext("cannot import '%s': "
3271 "a pool with that name already exists\n"),
3273 (void) fprintf(stderr, gettext("use the form '%s "
3274 "<pool | id> <newpool>' to give it a new name\n"),
3277 } else if (pools == NULL && pool_exists) {
3278 (void) fprintf(stderr, gettext("cannot import '%s': "
3279 "a pool with that name is already created/imported,\n"),
3281 (void) fprintf(stderr, gettext("and no additional pools "
3282 "with that name were found\n"));
3284 } else if (pools == NULL) {
3286 (void) fprintf(stderr, gettext("cannot import '%s': "
3287 "no such pool available\n"), argv[0]);
3293 if (searchdirs != NULL)
3297 nvlist_free(policy);
3304 * At this point we have a list of import candidate configs. Even if
3305 * we were searching by pool name or guid, we still need to
3306 * post-process the list to deal with pool state and possible
3312 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3314 verify(nvpair_value_nvlist(elem, &config) == 0);
3316 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3318 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
3320 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
3323 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
3330 (void) printf("\n");
3333 err |= do_import(config, NULL, mntopts,
3336 show_import(config);
3338 } else if (searchname != NULL) {
3342 * We are searching for a pool based on name.
3344 verify(nvlist_lookup_string(config,
3345 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3347 if (strcmp(name, searchname) == 0) {
3348 if (found_config != NULL) {
3349 (void) fprintf(stderr, gettext(
3350 "cannot import '%s': more than "
3351 "one matching pool\n"), searchname);
3352 (void) fprintf(stderr, gettext(
3353 "import by numeric ID instead\n"));
3356 found_config = config;
3362 * Search for a pool by guid.
3364 verify(nvlist_lookup_uint64(config,
3365 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3367 if (guid == searchguid)
3368 found_config = config;
3373 * If we were searching for a specific pool, verify that we found a
3374 * pool, and then do the import.
3376 if (argc != 0 && err == 0) {
3377 if (found_config == NULL) {
3378 (void) fprintf(stderr, gettext("cannot import '%s': "
3379 "no such pool available\n"), argv[0]);
3382 err |= do_import(found_config, argc == 1 ? NULL :
3383 argv[1], mntopts, props, flags);
3388 * If we were just looking for pools, report an error if none were
3391 if (argc == 0 && first)
3392 (void) fprintf(stderr,
3393 gettext("no pools available to import\n"));
3398 nvlist_free(policy);
3399 if (searchdirs != NULL)
3404 return (err ? 1 : 0);
3408 * zpool sync [-f] [pool] ...
3410 * -f (undocumented) force uberblock (and config including zpool cache file)
3413 * Sync the specified pool(s).
3414 * Without arguments "zpool sync" will sync all pools.
3415 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3419 zpool_do_sync(int argc, char **argv)
3422 boolean_t force = B_FALSE;
3425 while ((ret = getopt(argc, argv, "f")) != -1) {
3431 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3440 /* if argc == 0 we will execute zpool_sync_one on all pools */
3441 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
3446 typedef struct iostat_cbdata {
3451 char **cb_vdev_names; /* Only show these vdevs */
3452 unsigned int cb_vdev_names_count;
3453 boolean_t cb_verbose;
3454 boolean_t cb_literal;
3455 boolean_t cb_scripted;
3456 zpool_list_t *cb_list;
3457 vdev_cmd_data_list_t *vcdl;
3461 typedef struct name_and_columns {
3462 const char *name; /* Column name */
3463 unsigned int columns; /* Center name to this number of columns */
3464 } name_and_columns_t;
3466 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3468 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3470 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3472 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3473 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL}},
3474 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3475 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3476 {"trimq_write", 2}, {NULL}},
3477 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3478 {"asyncq_wait", 2}, {NULL}},
3479 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
3480 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3481 {"trim", 2}, {NULL}},
3484 /* Shorthand - if "columns" field not set, default to 1 column */
3485 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3487 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3489 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3490 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL}},
3491 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3492 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3493 {"pend"}, {"activ"}, {NULL}},
3494 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3495 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL}},
3496 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3497 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
3500 static const char *histo_to_title[] = {
3501 [IOS_L_HISTO] = "latency",
3502 [IOS_RQ_HISTO] = "req_size",
3506 * Return the number of labels in a null-terminated name_and_columns_t
3511 label_array_len(const name_and_columns_t *labels)
3515 while (labels[i].name)
3522 * Return the number of strings in a null-terminated string array.
3525 * const char foo[] = {"bar", "baz", NULL}
3530 str_array_len(const char *array[])
3541 * Return a default column width for default/latency/queue columns. This does
3542 * not include histograms, which have their columns autosized.
3545 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
3547 unsigned long column_width = 5; /* Normal niceprint */
3548 static unsigned long widths[] = {
3550 * Choose some sane default column sizes for printing the
3553 [IOS_DEFAULT] = 15, /* 1PB capacity */
3554 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
3555 [IOS_QUEUES] = 6, /* 1M queue entries */
3556 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */
3557 [IOS_RQ_HISTO] = 6, /* 1M queue entries */
3561 column_width = widths[type];
3563 return (column_width);
3567 * Print the column labels, i.e:
3569 * capacity operations bandwidth
3570 * alloc free read write read write ...
3572 * If force_column_width is set, use it for the column width. If not set, use
3573 * the default column width.
3576 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
3577 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
3580 int text_start, rw_column_width, spaces_to_end;
3581 uint64_t flags = cb->cb_flags;
3583 unsigned int column_width = force_column_width;
3585 /* For each bit set in flags */
3586 for (f = flags; f; f &= ~(1ULL << idx)) {
3587 idx = lowbit64(f) - 1;
3588 if (!force_column_width)
3589 column_width = default_column_width(cb, idx);
3590 /* Print our top labels centered over "read write" label. */
3591 for (i = 0; i < label_array_len(labels[idx]); i++) {
3592 const char *name = labels[idx][i].name;
3594 * We treat labels[][].columns == 0 as shorthand
3595 * for one column. It makes writing out the label
3596 * tables more concise.
3598 unsigned int columns = MAX(1, labels[idx][i].columns);
3599 unsigned int slen = strlen(name);
3601 rw_column_width = (column_width * columns) +
3602 (2 * (columns - 1));
3604 text_start = (int)((rw_column_width) / columns -
3609 printf(" "); /* Two spaces between columns */
3611 /* Space from beginning of column to label */
3612 for (s = 0; s < text_start; s++)
3617 /* Print space after label to end of column */
3618 spaces_to_end = rw_column_width - text_start - slen;
3619 if (spaces_to_end < 0)
3622 for (s = 0; s < spaces_to_end; s++)
3630 * print_cmd_columns - Print custom column titles from -c
3632 * If the user specified the "zpool status|iostat -c" then print their custom
3633 * column titles in the header. For example, print_cmd_columns() would print
3634 * the " col1 col2" part of this:
3636 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3638 * capacity operations bandwidth
3639 * pool alloc free read write read write col1 col2
3640 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3641 * mypool 269K 1008M 0 0 107 946
3642 * mirror 269K 1008M 0 0 107 946
3643 * sdb - - 0 0 102 473 val1 val2
3644 * sdc - - 0 0 5 473 val1 val2
3645 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3648 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3651 vdev_cmd_data_t *data = &vcdl->data[0];
3653 if (vcdl->count == 0 || data == NULL)
3657 * Each vdev cmd should have the same column names unless the user did
3658 * something weird with their cmd. Just take the column names from the
3659 * first vdev and assume it works for all of them.
3661 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3664 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3667 printf("%*s", vcdl->uniq_cols_width[i],
3668 vcdl->uniq_cols[i]);
3675 * Utility function to print out a line of dashes like:
3677 * -------------------------------- ----- ----- ----- ----- -----
3679 * ...or a dashed named-row line like:
3685 * @force_column_width If non-zero, use the value as the column width.
3686 * Otherwise use the default column widths.
3688 * @name: Print a dashed named-row line starting
3689 * with @name. Otherwise, print a regular
3693 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3697 unsigned int namewidth;
3698 uint64_t flags = cb->cb_flags;
3701 const name_and_columns_t *labels;
3705 if (cb->cb_flags & IOS_ANYHISTO_M) {
3706 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3707 } else if (cb->cb_vdev_names_count) {
3713 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3714 name ? strlen(name) : 0);
3718 printf("%-*s", namewidth, name);
3720 for (i = 0; i < namewidth; i++)
3724 /* For each bit in flags */
3725 for (f = flags; f; f &= ~(1ULL << idx)) {
3726 unsigned int column_width;
3727 idx = lowbit64(f) - 1;
3728 if (force_column_width)
3729 column_width = force_column_width;
3731 column_width = default_column_width(cb, idx);
3733 labels = iostat_bottom_labels[idx];
3734 for (i = 0; i < label_array_len(labels); i++) {
3736 printf(" %*s-", column_width - 1, " ");
3738 printf(" %.*s", column_width,
3739 "--------------------");
3746 print_iostat_separator_impl(iostat_cbdata_t *cb,
3747 unsigned int force_column_width)
3749 print_iostat_dashes(cb, force_column_width, NULL);
3753 print_iostat_separator(iostat_cbdata_t *cb)
3755 print_iostat_separator_impl(cb, 0);
3759 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3760 const char *histo_vdev_name)
3762 unsigned int namewidth;
3765 if (cb->cb_flags & IOS_ANYHISTO_M) {
3766 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3767 } else if (cb->cb_vdev_names_count) {
3773 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3774 histo_vdev_name ? strlen(histo_vdev_name) : 0);
3776 if (histo_vdev_name)
3777 printf("%-*s", namewidth, histo_vdev_name);
3779 printf("%*s", namewidth, "");
3782 print_iostat_labels(cb, force_column_width, iostat_top_labels);
3785 printf("%-*s", namewidth, title);
3787 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3788 if (cb->vcdl != NULL)
3789 print_cmd_columns(cb->vcdl, 0);
3793 print_iostat_separator_impl(cb, force_column_width);
3795 if (cb->vcdl != NULL)
3796 print_cmd_columns(cb->vcdl, 1);
3802 print_iostat_header(iostat_cbdata_t *cb)
3804 print_iostat_header_impl(cb, 0, NULL);
3809 * Display a single statistic.
3812 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3813 unsigned int column_size, boolean_t scripted)
3817 zfs_nicenum_format(value, buf, sizeof (buf), format);
3820 printf("\t%s", buf);
3822 printf(" %*s", column_size, buf);
3826 * Calculate the default vdev stats
3828 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3829 * stats into calcvs.
3832 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3833 vdev_stat_t *calcvs)
3837 memcpy(calcvs, newvs, sizeof (*calcvs));
3838 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3839 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3841 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3842 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3846 * Internal representation of the extended iostats data.
3848 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3849 * or single uint64_t's. We make both look like arrays to make them easier
3850 * to process. In order to make single uint64_t's look like arrays, we set
3851 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3852 * we can just use *data and count.
3856 uint_t count; /* Number of entries in data[] */
3857 uint64_t __data; /* Only used when data is a single uint64_t */
3861 stat_histo_max(struct stat_array *nva, unsigned int len)
3865 for (i = 0; i < len; i++)
3866 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3872 * Helper function to lookup a uint64_t array or uint64_t value and store its
3873 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3874 * it look like a one element array to make it easier to process.
3877 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3878 struct stat_array *nva)
3883 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3884 switch (nvpair_type(tmp)) {
3885 case DATA_TYPE_UINT64_ARRAY:
3886 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3888 case DATA_TYPE_UINT64:
3889 ret = nvpair_value_uint64(tmp, &nva->__data);
3890 nva->data = &nva->__data;
3894 /* Not a uint64_t */
3903 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3904 * subtract them, and return the results in a newly allocated stat_array.
3905 * You must free the returned array after you are done with it with
3906 * free_calc_stats().
3908 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3911 static struct stat_array *
3912 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3915 nvlist_t *oldnvx = NULL, *newnvx;
3916 struct stat_array *oldnva, *newnva, *calcnva;
3918 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3920 /* Extract our extended stats nvlist from the main list */
3921 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3924 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3928 newnva = safe_malloc(alloc_size);
3929 oldnva = safe_malloc(alloc_size);
3930 calcnva = safe_malloc(alloc_size);
3932 for (j = 0; j < len; j++) {
3933 verify(nvpair64_to_stat_array(newnvx, names[j],
3935 calcnva[j].count = newnva[j].count;
3936 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3937 calcnva[j].data = safe_malloc(alloc_size);
3938 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3941 verify(nvpair64_to_stat_array(oldnvx, names[j],
3943 for (i = 0; i < oldnva[j].count; i++)
3944 calcnva[j].data[i] -= oldnva[j].data[i];
3953 free_calc_stats(struct stat_array *nva, unsigned int len)
3956 for (i = 0; i < len; i++)
3963 print_iostat_histo(struct stat_array *nva, unsigned int len,
3964 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3970 enum zfs_nicenum_format format;
3971 unsigned int buckets;
3972 unsigned int start_bucket;
3975 format = ZFS_NICENUM_RAW;
3977 format = ZFS_NICENUM_1024;
3979 /* All these histos are the same size, so just use nva[0].count */
3980 buckets = nva[0].count;
3982 if (cb->cb_flags & IOS_RQ_HISTO_M) {
3983 /* Start at 512 - req size should never be lower than this */
3989 for (j = start_bucket; j < buckets; j++) {
3990 /* Print histogram bucket label */
3991 if (cb->cb_flags & IOS_L_HISTO_M) {
3992 /* Ending range of this bucket */
3993 val = (1UL << (j + 1)) - 1;
3994 zfs_nicetime(val, buf, sizeof (buf));
3996 /* Request size (starting range of bucket) */
3998 zfs_nicenum(val, buf, sizeof (buf));
4001 if (cb->cb_scripted)
4002 printf("%llu", (u_longlong_t)val);
4004 printf("%-*s", namewidth, buf);
4006 /* Print the values on the line */
4007 for (i = 0; i < len; i++) {
4008 print_one_stat(nva[i].data[j] * scale, format,
4009 column_width, cb->cb_scripted);
4016 print_solid_separator(unsigned int length)
4024 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
4025 nvlist_t *newnv, double scale, const char *name)
4027 unsigned int column_width;
4028 unsigned int namewidth;
4029 unsigned int entire_width;
4030 enum iostat_type type;
4031 struct stat_array *nva;
4033 unsigned int names_len;
4035 /* What type of histo are we? */
4036 type = IOS_HISTO_IDX(cb->cb_flags);
4038 /* Get NULL-terminated array of nvlist names for our histo */
4039 names = vsx_type_to_nvlist[type];
4040 names_len = str_array_len(names); /* num of names */
4042 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
4044 if (cb->cb_literal) {
4045 column_width = MAX(5,
4046 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
4051 namewidth = MAX(cb->cb_namewidth,
4052 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
4055 * Calculate the entire line width of what we're printing. The
4056 * +2 is for the two spaces between columns:
4060 /* |___| <---------- column_width */
4062 /* |__________| <--- entire_width */
4064 entire_width = namewidth + (column_width + 2) *
4065 label_array_len(iostat_bottom_labels[type]);
4067 if (cb->cb_scripted)
4068 printf("%s\n", name);
4070 print_iostat_header_impl(cb, column_width, name);
4072 print_iostat_histo(nva, names_len, cb, column_width,
4075 free_calc_stats(nva, names_len);
4076 if (!cb->cb_scripted)
4077 print_solid_separator(entire_width);
4081 * Calculate the average latency of a power-of-two latency histogram
4084 single_histo_average(uint64_t *histo, unsigned int buckets)
4087 uint64_t count = 0, total = 0;
4089 for (i = 0; i < buckets; i++) {
4091 * Our buckets are power-of-two latency ranges. Use the
4092 * midpoint latency of each bucket to calculate the average.
4100 if (histo[i] != 0) {
4101 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
4106 /* Prevent divide by zero */
4107 return (count == 0 ? 0 : total / count);
4111 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
4116 const char *names[] = {
4117 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
4118 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
4119 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
4120 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
4121 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
4122 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
4123 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
4124 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
4125 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
4126 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
4127 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
4128 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
4131 struct stat_array *nva;
4133 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
4134 enum zfs_nicenum_format format;
4136 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
4139 format = ZFS_NICENUM_RAW;
4141 format = ZFS_NICENUM_1024;
4143 for (i = 0; i < ARRAY_SIZE(names); i++) {
4144 val = nva[i].data[0];
4145 print_one_stat(val, format, column_width, cb->cb_scripted);
4148 free_calc_stats(nva, ARRAY_SIZE(names));
4152 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
4157 const char *names[] = {
4158 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
4159 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
4160 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
4161 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
4162 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
4163 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
4164 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
4165 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
4166 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
4167 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
4169 struct stat_array *nva;
4171 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
4172 enum zfs_nicenum_format format;
4174 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
4177 format = ZFS_NICENUM_RAWTIME;
4179 format = ZFS_NICENUM_TIME;
4181 /* Print our avg latencies on the line */
4182 for (i = 0; i < ARRAY_SIZE(names); i++) {
4183 /* Compute average latency for a latency histo */
4184 val = single_histo_average(nva[i].data, nva[i].count);
4185 print_one_stat(val, format, column_width, cb->cb_scripted);
4187 free_calc_stats(nva, ARRAY_SIZE(names));
4191 * Print default statistics (capacity/operations/bandwidth)
4194 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
4196 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
4197 enum zfs_nicenum_format format;
4198 char na; /* char to print for "not applicable" values */
4200 if (cb->cb_literal) {
4201 format = ZFS_NICENUM_RAW;
4204 format = ZFS_NICENUM_1024;
4208 /* only toplevel vdevs have capacity stats */
4209 if (vs->vs_space == 0) {
4210 if (cb->cb_scripted)
4211 printf("\t%c\t%c", na, na);
4213 printf(" %*c %*c", column_width, na, column_width,
4216 print_one_stat(vs->vs_alloc, format, column_width,
4218 print_one_stat(vs->vs_space - vs->vs_alloc, format,
4219 column_width, cb->cb_scripted);
4222 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
4223 format, column_width, cb->cb_scripted);
4224 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
4225 format, column_width, cb->cb_scripted);
4226 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
4227 format, column_width, cb->cb_scripted);
4228 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
4229 format, column_width, cb->cb_scripted);
4232 static const char *class_name[] = {
4233 VDEV_ALLOC_BIAS_DEDUP,
4234 VDEV_ALLOC_BIAS_SPECIAL,
4235 VDEV_ALLOC_CLASS_LOGS
4239 * Print out all the statistics for the given vdev. This can either be the
4240 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4241 * is a verbose output, and we don't want to display the toplevel pool stats.
4243 * Returns the number of stat lines printed.
4246 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
4247 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
4249 nvlist_t **oldchild, **newchild;
4250 uint_t c, children, oldchildren;
4251 vdev_stat_t *oldvs, *newvs, *calcvs;
4252 vdev_stat_t zerovs = { 0 };
4259 calcvs = safe_malloc(sizeof (*calcvs));
4261 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
4264 if (oldnv != NULL) {
4265 verify(nvlist_lookup_uint64_array(oldnv,
4266 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
4271 /* Do we only want to see a specific vdev? */
4272 for (i = 0; i < cb->cb_vdev_names_count; i++) {
4273 /* Yes we do. Is this the vdev? */
4274 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
4276 * This is our vdev. Since it is the only vdev we
4277 * will be displaying, make depth = 0 so that it
4278 * doesn't get indented.
4285 if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
4286 /* Couldn't match the name */
4291 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
4292 (uint64_t **)&newvs, &c) == 0);
4295 * Print the vdev name unless it's is a histogram. Histograms
4296 * display the vdev name in the header itself.
4298 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
4299 if (cb->cb_scripted) {
4302 if (strlen(name) + depth > cb->cb_namewidth)
4303 (void) printf("%*s%s", depth, "", name);
4305 (void) printf("%*s%s%*s", depth, "", name,
4306 (int)(cb->cb_namewidth - strlen(name) -
4311 /* Calculate our scaling factor */
4312 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
4313 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
4315 * If we specify printing histograms with no time interval, then
4316 * print the histogram numbers over the entire lifetime of the
4324 scale = (double)NANOSEC / tdelta;
4327 if (cb->cb_flags & IOS_DEFAULT_M) {
4328 calc_default_iostats(oldvs, newvs, calcvs);
4329 print_iostat_default(calcvs, cb, scale);
4331 if (cb->cb_flags & IOS_LATENCY_M)
4332 print_iostat_latency(cb, oldnv, newnv);
4333 if (cb->cb_flags & IOS_QUEUES_M)
4334 print_iostat_queues(cb, oldnv, newnv);
4335 if (cb->cb_flags & IOS_ANYHISTO_M) {
4337 print_iostat_histos(cb, oldnv, newnv, scale, name);
4340 if (cb->vcdl != NULL) {
4342 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
4345 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
4349 if (!(cb->cb_flags & IOS_ANYHISTO_M))
4358 if (!cb->cb_verbose)
4361 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4362 &newchild, &children) != 0)
4366 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4367 &oldchild, &oldchildren) != 0)
4370 children = MIN(oldchildren, children);
4374 * print normal top-level devices
4376 for (c = 0; c < children; c++) {
4377 uint64_t ishole = B_FALSE, islog = B_FALSE;
4379 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4382 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4385 if (ishole || islog)
4388 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4391 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4393 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
4394 newchild[c], cb, depth + 2);
4399 * print all other top-level devices
4401 for (uint_t n = 0; n < 3; n++) {
4402 boolean_t printed = B_FALSE;
4404 for (c = 0; c < children; c++) {
4405 uint64_t islog = B_FALSE;
4409 (void) nvlist_lookup_uint64(newchild[c],
4410 ZPOOL_CONFIG_IS_LOG, &islog);
4412 bias = VDEV_ALLOC_CLASS_LOGS;
4414 (void) nvlist_lookup_string(newchild[c],
4415 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4416 (void) nvlist_lookup_string(newchild[c],
4417 ZPOOL_CONFIG_TYPE, &type);
4419 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4421 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4425 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
4426 !cb->cb_scripted && !cb->cb_vdev_names) {
4427 print_iostat_dashes(cb, 0,
4434 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4436 ret += print_vdev_stats(zhp, vname, oldnv ?
4437 oldchild[c] : NULL, newchild[c], cb, depth + 2);
4443 * Include level 2 ARC devices in iostat output
4445 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4446 &newchild, &children) != 0)
4450 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4451 &oldchild, &oldchildren) != 0)
4454 children = MIN(oldchildren, children);
4458 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
4459 !cb->cb_vdev_names) {
4460 print_iostat_dashes(cb, 0, "cache");
4464 for (c = 0; c < children; c++) {
4465 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4467 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4468 : NULL, newchild[c], cb, depth + 2);
4477 refresh_iostat(zpool_handle_t *zhp, void *data)
4479 iostat_cbdata_t *cb = data;
4483 * If the pool has disappeared, remove it from the list and continue.
4485 if (zpool_refresh_stats(zhp, &missing) != 0)
4489 pool_list_remove(cb->cb_list, zhp);
4495 * Callback to print out the iostats for the given pool.
4498 print_iostat(zpool_handle_t *zhp, void *data)
4500 iostat_cbdata_t *cb = data;
4501 nvlist_t *oldconfig, *newconfig;
4502 nvlist_t *oldnvroot, *newnvroot;
4505 newconfig = zpool_get_config(zhp, &oldconfig);
4507 if (cb->cb_iteration == 1)
4510 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
4513 if (oldconfig == NULL)
4516 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
4519 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
4521 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
4522 !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
4523 print_iostat_separator(cb);
4524 if (cb->vcdl != NULL) {
4525 print_cmd_columns(cb->vcdl, 1);
4540 if (isatty(STDOUT_FILENO)) {
4541 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
4543 columns = ws.ws_col;
4552 * Return the required length of the pool/vdev name column. The minimum
4553 * allowed width and output formatting flags must be provided.
4556 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
4558 nvlist_t *config, *nvroot;
4559 int width = min_width;
4561 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
4562 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4564 unsigned int poolname_len = strlen(zpool_get_name(zhp));
4565 if (verbose == B_FALSE) {
4566 width = MAX(poolname_len, min_width);
4568 width = MAX(poolname_len,
4569 max_width(zhp, nvroot, 0, min_width, flags));
4577 * Parse the input string, get the 'interval' and 'count' value if there is one.
4580 get_interval_count(int *argcp, char **argv, float *iv,
4584 unsigned long count = 0;
4588 * Determine if the last argument is an integer or a pool name
4590 if (argc > 0 && isnumber(argv[argc - 1])) {
4594 interval = strtof(argv[argc - 1], &end);
4596 if (*end == '\0' && errno == 0) {
4597 if (interval == 0) {
4598 (void) fprintf(stderr, gettext("interval "
4599 "cannot be zero\n"));
4603 * Ignore the last parameter
4608 * If this is not a valid number, just plow on. The
4609 * user will get a more informative error message later
4617 * If the last argument is also an integer, then we have both a count
4620 if (argc > 0 && isnumber(argv[argc - 1])) {
4625 interval = strtof(argv[argc - 1], &end);
4627 if (*end == '\0' && errno == 0) {
4628 if (interval == 0) {
4629 (void) fprintf(stderr, gettext("interval "
4630 "cannot be zero\n"));
4635 * Ignore the last parameter
4649 get_timestamp_arg(char c)
4652 timestamp_fmt = UDATE;
4654 timestamp_fmt = DDATE;
4660 * Return stat flags that are supported by all pools by both the module and
4661 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4662 * It will get ANDed down until only the flags that are supported on all pools
4666 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4668 uint64_t *mask = data;
4669 nvlist_t *config, *nvroot, *nvx;
4673 config = zpool_get_config(zhp, NULL);
4674 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4677 /* Default stats are always supported, but for completeness.. */
4678 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4679 flags |= IOS_DEFAULT_M;
4681 /* Get our extended stats nvlist from the main list */
4682 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4685 * No extended stats; they're probably running an older
4686 * module. No big deal, we support that too.
4691 /* For each extended stat, make sure all its nvpairs are supported */
4692 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4693 if (!vsx_type_to_nvlist[j][0])
4696 /* Start off by assuming the flag is supported, then check */
4697 flags |= (1ULL << j);
4698 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4699 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4700 /* flag isn't supported */
4701 flags = flags & ~(1ULL << j);
4707 *mask = *mask & flags;
4712 * Return a bitmask of stats that are supported on all pools by both the module
4716 get_stat_flags(zpool_list_t *list)
4721 * get_stat_flags_cb() will lop off bits from "mask" until only the
4722 * flags that are supported on all pools remain.
4724 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4729 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4732 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4734 iostat_cbdata_t *cb = cb_data;
4738 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4740 if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4741 ret = 1; /* match */
4748 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4751 is_vdev(zpool_handle_t *zhp, void *cb_data)
4753 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4757 * Check if vdevs are in a pool
4759 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4760 * return 0. If pool_name is NULL, then search all pools.
4763 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4764 iostat_cbdata_t *cb)
4771 if ((argc == 0) || !*argv)
4777 /* Temporarily hijack cb_vdev_names for a second... */
4778 tmp_name = cb->cb_vdev_names;
4780 /* Go though our list of prospective vdev names */
4781 for (i = 0; i < argc; i++) {
4782 cb->cb_vdev_names = argv + i;
4784 /* Is this name a vdev in our pools? */
4785 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4793 cb->cb_vdev_names = tmp_name;
4799 is_pool_cb(zpool_handle_t *zhp, void *data)
4802 if (strcmp(name, zpool_get_name(zhp)) == 0)
4809 * Do we have a pool named *name? If so, return 1, otherwise 0.
4814 return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name));
4817 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4819 are_all_pools(int argc, char **argv)
4821 if ((argc == 0) || !*argv)
4825 if (!is_pool(argv[argc]))
4832 * Helper function to print out vdev/pool names we can't resolve. Used for an
4836 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4837 iostat_cbdata_t *cb)
4842 for (i = 0; i < argc; i++) {
4846 str = gettext("pool");
4847 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4848 str = gettext("vdev in this pool");
4849 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4850 str = gettext("vdev in another pool");
4852 str = gettext("unknown");
4854 fprintf(stderr, "\t%s (%s)\n", name, str);
4859 * Same as get_interval_count(), but with additional checks to not misinterpret
4860 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4864 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4865 unsigned long *count, iostat_cbdata_t *cb)
4867 char **tmpargv = argv;
4868 int argc_for_interval = 0;
4870 /* Is the last arg an interval value? Or a guid? */
4871 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4873 * The last arg is not a guid, so it's probably an
4876 argc_for_interval++;
4879 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4881 * The 2nd to last arg is not a guid, so it's probably
4882 * an interval value.
4884 argc_for_interval++;
4888 /* Point to our list of possible intervals */
4889 tmpargv = &argv[*argc - argc_for_interval];
4891 *argc = *argc - argc_for_interval;
4892 get_interval_count(&argc_for_interval, tmpargv,
4897 * Floating point sleep(). Allows you to pass in a floating point value for
4903 struct timespec req;
4904 req.tv_sec = floor(sec);
4905 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4906 nanosleep(&req, NULL);
4910 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4913 * name: Short name of the script ('iostat').
4914 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4917 print_zpool_script_help(char *name, char *path)
4919 char *argv[] = {path, "-h", NULL};
4920 char **lines = NULL;
4924 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4926 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4928 libzfs_free_str_array(lines, lines_cnt);
4932 for (int i = 0; i < lines_cnt; i++)
4933 if (!is_blank_str(lines[i]))
4934 printf(" %-14s %s\n", name, lines[i]);
4936 libzfs_free_str_array(lines, lines_cnt);
4940 * Go though the zpool status/iostat -c scripts in the user's path, run their
4941 * help option (-h), and print out the results.
4944 print_zpool_dir_scripts(char *dirpath)
4948 char fullpath[MAXPATHLEN];
4949 struct stat dir_stat;
4951 if ((dir = opendir(dirpath)) != NULL) {
4952 /* print all the files and directories within directory */
4953 while ((ent = readdir(dir)) != NULL) {
4954 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4956 /* Print the scripts */
4957 if (stat(fullpath, &dir_stat) == 0)
4958 if (dir_stat.st_mode & S_IXUSR &&
4959 S_ISREG(dir_stat.st_mode))
4960 print_zpool_script_help(ent->d_name,
4968 * Print out help text for all zpool status/iostat -c scripts.
4971 print_zpool_script_list(char *subcommand)
4975 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4977 sp = zpool_get_cmd_search_path();
4981 dir = strtok(sp, ":");
4982 while (dir != NULL) {
4983 print_zpool_dir_scripts(dir);
4984 dir = strtok(NULL, ":");
4991 * Set the minimum pool/vdev name column width. The width must be at least 10,
4992 * but may be as large as the column width - 42 so it still fits on one line.
4995 get_namewidth_iostat(zpool_handle_t *zhp, void *data)
4997 iostat_cbdata_t *cb = data;
5000 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
5002 columns = get_columns();
5006 if (width > columns - 42)
5007 width = columns - 42;
5009 cb->cb_namewidth = width;
5015 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5016 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5017 * [interval [count]]
5019 * -c CMD For each vdev, run command CMD
5020 * -g Display guid for individual vdev name.
5021 * -L Follow links when resolving vdev path name.
5022 * -P Display full path for vdev name.
5023 * -v Display statistics for individual vdevs
5025 * -p Display values in parsable (exact) format.
5026 * -H Scripted mode. Don't display headers, and separate properties
5028 * -l Display average latency
5029 * -q Display queue depths
5030 * -w Display latency histograms
5031 * -r Display request size histogram
5032 * -T Display a timestamp in date(1) or Unix format
5033 * -n Only print headers once
5035 * This command can be tricky because we want to be able to deal with pool
5036 * creation/destruction as well as vdev configuration changes. The bulk of this
5037 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5038 * on pool_list_update() to detect the addition of new pools. Configuration
5039 * changes are all handled within libzfs.
5042 zpool_do_iostat(int argc, char **argv)
5048 unsigned long count = 0;
5052 boolean_t verbose = B_FALSE;
5053 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
5054 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
5055 boolean_t omit_since_boot = B_FALSE;
5056 boolean_t guid = B_FALSE;
5057 boolean_t follow_links = B_FALSE;
5058 boolean_t full_name = B_FALSE;
5059 boolean_t headers_once = B_FALSE;
5060 iostat_cbdata_t cb = { 0 };
5063 /* Used for printing error message */
5064 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
5065 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
5067 uint64_t unsupported_flags;
5070 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
5075 gettext("Can't set -c flag twice\n"));
5079 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
5080 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5081 fprintf(stderr, gettext(
5082 "Can't run -c, disabled by "
5083 "ZPOOL_SCRIPTS_ENABLED.\n"));
5087 if ((getuid() <= 0 || geteuid() <= 0) &&
5088 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5089 fprintf(stderr, gettext(
5090 "Can't run -c with root privileges "
5091 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5101 follow_links = B_TRUE;
5107 get_timestamp_arg(*optarg);
5131 omit_since_boot = B_TRUE;
5134 headers_once = B_TRUE;
5140 if (optopt == 'c') {
5141 print_zpool_script_list("iostat");
5145 gettext("invalid option '%c'\n"), optopt);
5154 cb.cb_literal = parsable;
5155 cb.cb_scripted = scripted;
5158 cb.cb_name_flags |= VDEV_NAME_GUID;
5160 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5162 cb.cb_name_flags |= VDEV_NAME_PATH;
5163 cb.cb_iteration = 0;
5164 cb.cb_namewidth = 0;
5165 cb.cb_verbose = verbose;
5167 /* Get our interval and count values (if any) */
5169 get_interval_count_filter_guids(&argc, argv, &interval,
5172 get_interval_count(&argc, argv, &interval, &count);
5176 /* No args, so just print the defaults. */
5177 } else if (are_all_pools(argc, argv)) {
5178 /* All the args are pool names */
5179 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
5180 /* All the args are vdevs */
5181 cb.cb_vdev_names = argv;
5182 cb.cb_vdev_names_count = argc;
5183 argc = 0; /* No pools to process */
5184 } else if (are_all_pools(1, argv)) {
5185 /* The first arg is a pool name */
5186 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
5187 /* ...and the rest are vdev names */
5188 cb.cb_vdev_names = argv + 1;
5189 cb.cb_vdev_names_count = argc - 1;
5190 argc = 1; /* One pool to process */
5192 fprintf(stderr, gettext("Expected either a list of "));
5193 fprintf(stderr, gettext("pools, or list of vdevs in"));
5194 fprintf(stderr, " \"%s\", ", argv[0]);
5195 fprintf(stderr, gettext("but got:\n"));
5196 error_list_unresolved_vdevs(argc - 1, argv + 1,
5198 fprintf(stderr, "\n");
5204 * The args don't make sense. The first arg isn't a pool name,
5205 * nor are all the args vdevs.
5207 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
5208 fprintf(stderr, "\n");
5212 if (cb.cb_vdev_names_count != 0) {
5214 * If user specified vdevs, it implies verbose.
5216 cb.cb_verbose = B_TRUE;
5220 * Construct the list of all interesting pools.
5223 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
5226 if (pool_list_count(list) == 0 && argc != 0) {
5227 pool_list_free(list);
5231 if (pool_list_count(list) == 0 && interval == 0) {
5232 pool_list_free(list);
5233 (void) fprintf(stderr, gettext("no pools available\n"));
5237 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
5238 pool_list_free(list);
5239 (void) fprintf(stderr,
5240 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5245 if (l_histo && rq_histo) {
5246 pool_list_free(list);
5247 (void) fprintf(stderr,
5248 gettext("Only one of [-r|-w] can be passed at a time\n"));
5254 * Enter the main iostat loop.
5260 * Histograms tables look out of place when you try to display
5261 * them with the other stats, so make a rule that you can only
5262 * print histograms by themselves.
5264 cb.cb_flags = IOS_L_HISTO_M;
5265 } else if (rq_histo) {
5266 cb.cb_flags = IOS_RQ_HISTO_M;
5268 cb.cb_flags = IOS_DEFAULT_M;
5270 cb.cb_flags |= IOS_LATENCY_M;
5272 cb.cb_flags |= IOS_QUEUES_M;
5276 * See if the module supports all the stats we want to display.
5278 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
5279 if (unsupported_flags) {
5283 gettext("The loaded zfs module doesn't support:"));
5285 /* for each bit set in unsupported_flags */
5286 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
5287 idx = lowbit64(f) - 1;
5288 fprintf(stderr, " -%c", flag_to_arg[idx]);
5291 fprintf(stderr, ". Try running a newer module.\n");
5292 pool_list_free(list);
5298 if ((npools = pool_list_count(list)) == 0)
5299 (void) fprintf(stderr, gettext("no pools available\n"));
5302 * If this is the first iteration and -y was supplied
5303 * we skip any printing.
5305 boolean_t skip = (omit_since_boot &&
5306 cb.cb_iteration == 0);
5309 * Refresh all statistics. This is done as an
5310 * explicit step before calculating the maximum name
5311 * width, so that any * configuration changes are
5312 * properly accounted for.
5314 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
5318 * Iterate over all pools to determine the maximum width
5319 * for the pool / device name column across all pools.
5321 cb.cb_namewidth = 0;
5322 (void) pool_list_iter(list, B_FALSE,
5323 get_namewidth_iostat, &cb);
5325 if (timestamp_fmt != NODATE)
5326 print_timestamp(timestamp_fmt);
5328 if (cmd != NULL && cb.cb_verbose &&
5329 !(cb.cb_flags & IOS_ANYHISTO_M)) {
5330 cb.vcdl = all_pools_for_each_vdev_run(argc,
5331 argv, cmd, g_zfs, cb.cb_vdev_names,
5332 cb.cb_vdev_names_count, cb.cb_name_flags);
5338 * Are we connected to TTY? If not, headers_once
5339 * should be true, to avoid breaking scripts.
5341 if (isatty(fileno(stdout)) == 0)
5342 headers_once = B_TRUE;
5345 * Check terminal size so we can print headers
5346 * even when terminal window has its height
5349 if (headers_once == B_FALSE) {
5350 if (ioctl(1, TIOCGWINSZ, &win) != -1 &&
5352 winheight = win.ws_row;
5354 headers_once = B_TRUE;
5358 * If it's the first time and we're not skipping it,
5359 * or either skip or verbose mode, print the header.
5361 * The histogram code explicitly prints its header on
5362 * every vdev, so skip this for histograms.
5364 if (((++cb.cb_iteration == 1 && !skip) ||
5365 (skip != verbose) ||
5367 (cb.cb_iteration % winheight) == 0)) &&
5368 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
5370 print_iostat_header(&cb);
5373 (void) fsleep(interval);
5377 pool_list_iter(list, B_FALSE, print_iostat, &cb);
5380 * If there's more than one pool, and we're not in
5381 * verbose mode (which prints a separator for us),
5382 * then print a separator.
5384 * In addition, if we're printing specific vdevs then
5385 * we also want an ending separator.
5387 if (((npools > 1 && !verbose &&
5388 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
5389 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
5390 cb.cb_vdev_names_count)) &&
5392 print_iostat_separator(&cb);
5393 if (cb.vcdl != NULL)
5394 print_cmd_columns(cb.vcdl, 1);
5398 if (cb.vcdl != NULL)
5399 free_vdev_cmd_data_list(cb.vcdl);
5404 * Flush the output so that redirection to a file isn't buffered
5407 (void) fflush(stdout);
5412 if (count != 0 && --count == 0)
5415 (void) fsleep(interval);
5418 pool_list_free(list);
5423 typedef struct list_cbdata {
5424 boolean_t cb_verbose;
5427 boolean_t cb_scripted;
5428 zprop_list_t *cb_proplist;
5429 boolean_t cb_literal;
5434 * Given a list of columns to display, output appropriate headers for each one.
5437 print_header(list_cbdata_t *cb)
5439 zprop_list_t *pl = cb->cb_proplist;
5440 char headerbuf[ZPOOL_MAXPROPLEN];
5442 boolean_t first = B_TRUE;
5443 boolean_t right_justify;
5446 for (; pl != NULL; pl = pl->pl_next) {
5447 width = pl->pl_width;
5448 if (first && cb->cb_verbose) {
5450 * Reset the width to accommodate the verbose listing
5453 width = cb->cb_namewidth;
5461 right_justify = B_FALSE;
5462 if (pl->pl_prop != ZPROP_INVAL) {
5463 header = zpool_prop_column_name(pl->pl_prop);
5464 right_justify = zpool_prop_align_right(pl->pl_prop);
5468 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5469 headerbuf[i] = toupper(pl->pl_user_prop[i]);
5470 headerbuf[i] = '\0';
5474 if (pl->pl_next == NULL && !right_justify)
5475 (void) printf("%s", header);
5476 else if (right_justify)
5477 (void) printf("%*s", (int)width, header);
5479 (void) printf("%-*s", (int)width, header);
5482 (void) printf("\n");
5486 * Given a pool and a list of properties, print out all the properties according
5487 * to the described layout. Used by zpool_do_list().
5490 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
5492 zprop_list_t *pl = cb->cb_proplist;
5493 boolean_t first = B_TRUE;
5494 char property[ZPOOL_MAXPROPLEN];
5496 boolean_t right_justify;
5499 for (; pl != NULL; pl = pl->pl_next) {
5501 width = pl->pl_width;
5502 if (first && cb->cb_verbose) {
5504 * Reset the width to accommodate the verbose listing
5507 width = cb->cb_namewidth;
5511 if (cb->cb_scripted)
5512 (void) printf("\t");
5519 right_justify = B_FALSE;
5520 if (pl->pl_prop != ZPROP_INVAL) {
5521 if (zpool_get_prop(zhp, pl->pl_prop, property,
5522 sizeof (property), NULL, cb->cb_literal) != 0)
5527 right_justify = zpool_prop_align_right(pl->pl_prop);
5528 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
5529 zpool_prop_unsupported(pl->pl_user_prop)) &&
5530 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
5531 sizeof (property)) == 0) {
5539 * If this is being called in scripted mode, or if this is the
5540 * last column and it is left-justified, don't include a width
5543 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
5544 (void) printf("%s", propstr);
5545 else if (right_justify)
5546 (void) printf("%*s", (int)width, propstr);
5548 (void) printf("%-*s", (int)width, propstr);
5551 (void) printf("\n");
5555 print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
5556 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format)
5560 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
5563 case ZPOOL_PROP_EXPANDSZ:
5564 case ZPOOL_PROP_CHECKPOINT:
5565 case ZPOOL_PROP_DEDUPRATIO:
5567 (void) strlcpy(propval, "-", sizeof (propval));
5569 zfs_nicenum_format(value, propval, sizeof (propval),
5572 case ZPOOL_PROP_FRAGMENTATION:
5573 if (value == ZFS_FRAG_INVALID) {
5574 (void) strlcpy(propval, "-", sizeof (propval));
5575 } else if (format == ZFS_NICENUM_RAW) {
5576 (void) snprintf(propval, sizeof (propval), "%llu",
5577 (unsigned long long)value);
5579 (void) snprintf(propval, sizeof (propval), "%llu%%",
5580 (unsigned long long)value);
5583 case ZPOOL_PROP_CAPACITY:
5584 /* capacity value is in parts-per-10,000 (aka permyriad) */
5585 if (format == ZFS_NICENUM_RAW)
5586 (void) snprintf(propval, sizeof (propval), "%llu",
5587 (unsigned long long)value / 100);
5589 (void) snprintf(propval, sizeof (propval),
5590 value < 1000 ? "%1.2f%%" : value < 10000 ?
5591 "%2.1f%%" : "%3.0f%%", value / 100.0);
5593 case ZPOOL_PROP_HEALTH:
5595 snprintf(propval, sizeof (propval), "%-*s", (int)width, str);
5598 zfs_nicenum_format(value, propval, sizeof (propval), format);
5602 (void) strlcpy(propval, "-", sizeof (propval));
5605 (void) printf("\t%s", propval);
5607 (void) printf(" %*s", (int)width, propval);
5611 * print static default line per vdev
5612 * not compatible with '-o' <proplist> option
5615 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
5616 list_cbdata_t *cb, int depth, boolean_t isspare)
5622 boolean_t scripted = cb->cb_scripted;
5623 uint64_t islog = B_FALSE;
5624 char *dashes = "%-*s - - - - "
5627 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
5628 (uint64_t **)&vs, &c) == 0);
5631 boolean_t toplevel = (vs->vs_space != 0);
5633 enum zfs_nicenum_format format;
5637 format = ZFS_NICENUM_RAW;
5639 format = ZFS_NICENUM_1024;
5641 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
5645 (void) printf("\t%s", name);
5646 else if (strlen(name) + depth > cb->cb_namewidth)
5647 (void) printf("%*s%s", depth, "", name);
5649 (void) printf("%*s%s%*s", depth, "", name,
5650 (int)(cb->cb_namewidth - strlen(name) - depth), "");
5653 * Print the properties for the individual vdevs. Some
5654 * properties are only applicable to toplevel vdevs. The
5655 * 'toplevel' boolean value is passed to the print_one_column()
5656 * to indicate that the value is valid.
5658 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted,
5660 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL,
5661 scripted, toplevel, format);
5662 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
5663 NULL, scripted, toplevel, format);
5664 print_one_column(ZPOOL_PROP_CHECKPOINT,
5665 vs->vs_checkpoint_space, NULL, scripted, toplevel, format);
5666 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL,
5667 scripted, B_TRUE, format);
5668 print_one_column(ZPOOL_PROP_FRAGMENTATION,
5669 vs->vs_fragmentation, NULL, scripted,
5670 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
5672 cap = (vs->vs_space == 0) ? 0 :
5673 (vs->vs_alloc * 10000 / vs->vs_space);
5674 print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL,
5675 scripted, toplevel, format);
5676 print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL,
5677 scripted, toplevel, format);
5678 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
5680 if (vs->vs_aux == VDEV_AUX_SPARED)
5682 else if (vs->vs_state == VDEV_STATE_HEALTHY)
5685 print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
5687 (void) printf("\n");
5690 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
5691 &child, &children) != 0)
5694 /* list the normal vdevs first */
5695 for (c = 0; c < children; c++) {
5696 uint64_t ishole = B_FALSE;
5698 if (nvlist_lookup_uint64(child[c],
5699 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
5702 if (nvlist_lookup_uint64(child[c],
5703 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
5706 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
5709 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5711 print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
5715 /* list the classes: 'logs', 'dedup', and 'special' */
5716 for (uint_t n = 0; n < 3; n++) {
5717 boolean_t printed = B_FALSE;
5719 for (c = 0; c < children; c++) {
5723 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5724 &islog) == 0 && islog) {
5725 bias = VDEV_ALLOC_CLASS_LOGS;
5727 (void) nvlist_lookup_string(child[c],
5728 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
5729 (void) nvlist_lookup_string(child[c],
5730 ZPOOL_CONFIG_TYPE, &type);
5732 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
5734 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
5738 /* LINTED E_SEC_PRINTF_VAR_FMT */
5739 (void) printf(dashes, cb->cb_namewidth,
5743 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5745 print_list_stats(zhp, vname, child[c], cb, depth + 2,
5751 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5752 &child, &children) == 0 && children > 0) {
5753 /* LINTED E_SEC_PRINTF_VAR_FMT */
5754 (void) printf(dashes, cb->cb_namewidth, "cache");
5755 for (c = 0; c < children; c++) {
5756 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5758 print_list_stats(zhp, vname, child[c], cb, depth + 2,
5764 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5765 &children) == 0 && children > 0) {
5766 /* LINTED E_SEC_PRINTF_VAR_FMT */
5767 (void) printf(dashes, cb->cb_namewidth, "spare");
5768 for (c = 0; c < children; c++) {
5769 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5771 print_list_stats(zhp, vname, child[c], cb, depth + 2,
5779 * Generic callback function to list a pool.
5782 list_callback(zpool_handle_t *zhp, void *data)
5784 list_cbdata_t *cbp = data;
5786 print_pool(zhp, cbp);
5788 if (cbp->cb_verbose) {
5789 nvlist_t *config, *nvroot;
5791 config = zpool_get_config(zhp, NULL);
5792 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5794 print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE);
5801 * Set the minimum pool/vdev name column width. The width must be at least 9,
5802 * but may be as large as needed.
5805 get_namewidth_list(zpool_handle_t *zhp, void *data)
5807 list_cbdata_t *cb = data;
5810 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
5816 cb->cb_namewidth = width;
5822 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5824 * -g Display guid for individual vdev name.
5825 * -H Scripted mode. Don't display headers, and separate properties
5827 * -L Follow links when resolving vdev path name.
5828 * -o List of properties to display. Defaults to
5829 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5830 * "dedupratio,health,altroot"
5831 * -p Display values in parsable (exact) format.
5832 * -P Display full path for vdev name.
5833 * -T Display a timestamp in date(1) or Unix format
5835 * List all pools in the system, whether or not they're healthy. Output space
5836 * statistics for each one, as well as health status summary.
5839 zpool_do_list(int argc, char **argv)
5843 list_cbdata_t cb = { 0 };
5844 static char default_props[] =
5845 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
5846 "capacity,dedupratio,health,altroot";
5847 char *props = default_props;
5849 unsigned long count = 0;
5851 boolean_t first = B_TRUE;
5854 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5857 cb.cb_name_flags |= VDEV_NAME_GUID;
5860 cb.cb_scripted = B_TRUE;
5863 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5869 cb.cb_name_flags |= VDEV_NAME_PATH;
5872 cb.cb_literal = B_TRUE;
5875 get_timestamp_arg(*optarg);
5878 cb.cb_verbose = B_TRUE;
5879 cb.cb_namewidth = 8; /* 8 until precalc is avail */
5882 (void) fprintf(stderr, gettext("missing argument for "
5883 "'%c' option\n"), optopt);
5887 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5896 get_interval_count(&argc, argv, &interval, &count);
5898 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5902 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5906 if (pool_list_count(list) == 0)
5909 cb.cb_namewidth = 0;
5910 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
5912 if (timestamp_fmt != NODATE)
5913 print_timestamp(timestamp_fmt);
5915 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5919 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5924 if (count != 0 && --count == 0)
5927 pool_list_free(list);
5928 (void) fsleep(interval);
5931 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5932 (void) printf(gettext("no pools available\n"));
5936 pool_list_free(list);
5937 zprop_free_list(cb.cb_proplist);
5942 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5944 boolean_t force = B_FALSE;
5947 char *poolname, *old_disk, *new_disk;
5948 zpool_handle_t *zhp;
5949 nvlist_t *props = NULL;
5954 while ((c = getopt(argc, argv, "fo:")) != -1) {
5960 if ((propval = strchr(optarg, '=')) == NULL) {
5961 (void) fprintf(stderr, gettext("missing "
5962 "'=' for -o option\n"));
5968 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5969 (add_prop_list(optarg, propval, &props, B_TRUE)))
5973 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5982 /* get pool name and check number of arguments */
5984 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5991 (void) fprintf(stderr,
5992 gettext("missing <device> specification\n"));
6000 (void) fprintf(stderr,
6001 gettext("missing <new_device> specification\n"));
6004 new_disk = old_disk;
6014 (void) fprintf(stderr, gettext("too many arguments\n"));
6018 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
6023 if (zpool_get_config(zhp, NULL) == NULL) {
6024 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
6031 /* unless manually specified use "ashift" pool property (if set) */
6032 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
6035 char strval[ZPOOL_MAXPROPLEN];
6037 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
6038 if (src != ZPROP_SRC_DEFAULT) {
6039 (void) sprintf(strval, "%" PRId32, intval);
6040 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
6041 &props, B_TRUE) == 0);
6045 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
6047 if (nvroot == NULL) {
6053 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
6056 nvlist_free(nvroot);
6063 * zpool replace [-f] <pool> <device> <new_device>
6065 * -f Force attach, even if <new_device> appears to be in use.
6067 * Replace <device> with <new_device>.
6071 zpool_do_replace(int argc, char **argv)
6073 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
6077 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
6079 * -f Force attach, even if <new_device> appears to be in use.
6080 * -o Set property=value.
6082 * Attach <new_device> to the mirror containing <device>. If <device> is not
6083 * part of a mirror, then <device> will be transformed into a mirror of
6084 * <device> and <new_device>. In either case, <new_device> will begin life
6085 * with a DTL of [0, now], and will immediately begin to resilver itself.
6088 zpool_do_attach(int argc, char **argv)
6090 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
6094 * zpool detach [-f] <pool> <device>
6096 * -f Force detach of <device>, even if DTLs argue against it
6097 * (not supported yet)
6099 * Detach a device from a mirror. The operation will be refused if <device>
6100 * is the last device in the mirror, or if the DTLs indicate that this device
6101 * has the only valid copy of some data.
6105 zpool_do_detach(int argc, char **argv)
6108 char *poolname, *path;
6109 zpool_handle_t *zhp;
6113 while ((c = getopt(argc, argv, "f")) != -1) {
6117 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6126 /* get pool name and check number of arguments */
6128 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6133 (void) fprintf(stderr,
6134 gettext("missing <device> specification\n"));
6141 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6144 ret = zpool_vdev_detach(zhp, path);
6152 * zpool split [-gLnP] [-o prop=val] ...
6154 * [-R altroot] <pool> <newpool> [<device> ...]
6156 * -g Display guid for individual vdev name.
6157 * -L Follow links when resolving vdev path name.
6158 * -n Do not split the pool, but display the resulting layout if
6159 * it were to be split.
6160 * -o Set property=value, or set mount options.
6161 * -P Display full path for vdev name.
6162 * -R Mount the split-off pool under an alternate root.
6163 * -l Load encryption keys while importing.
6165 * Splits the named pool and gives it the new pool name. Devices to be split
6166 * off may be listed, provided that no more than one device is specified
6167 * per top-level vdev mirror. The newly split pool is left in an exported
6168 * state unless -R is specified.
6170 * Restrictions: the top-level of the pool pool must only be made up of
6171 * mirrors; all devices in the pool must be healthy; no device may be
6172 * undergoing a resilvering operation.
6175 zpool_do_split(int argc, char **argv)
6177 char *srcpool, *newpool, *propval;
6178 char *mntopts = NULL;
6181 boolean_t loadkeys = B_FALSE;
6182 zpool_handle_t *zhp;
6183 nvlist_t *config, *props = NULL;
6185 flags.dryrun = B_FALSE;
6186 flags.import = B_FALSE;
6187 flags.name_flags = 0;
6190 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
6193 flags.name_flags |= VDEV_NAME_GUID;
6196 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
6199 flags.import = B_TRUE;
6201 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
6202 &props, B_TRUE) != 0) {
6211 flags.dryrun = B_TRUE;
6214 if ((propval = strchr(optarg, '=')) != NULL) {
6217 if (add_prop_list(optarg, propval,
6218 &props, B_TRUE) != 0) {
6227 flags.name_flags |= VDEV_NAME_PATH;
6230 (void) fprintf(stderr, gettext("missing argument for "
6231 "'%c' option\n"), optopt);
6235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6242 if (!flags.import && mntopts != NULL) {
6243 (void) fprintf(stderr, gettext("setting mntopts is only "
6244 "valid when importing the pool\n"));
6248 if (!flags.import && loadkeys) {
6249 (void) fprintf(stderr, gettext("loading keys is only "
6250 "valid when importing the pool\n"));
6258 (void) fprintf(stderr, gettext("Missing pool name\n"));
6262 (void) fprintf(stderr, gettext("Missing new pool name\n"));
6272 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
6277 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
6278 if (config == NULL) {
6282 (void) printf(gettext("would create '%s' with the "
6283 "following layout:\n\n"), newpool);
6284 print_vdev_tree(NULL, newpool, config, 0, "",
6291 if (ret != 0 || flags.dryrun || !flags.import) {
6292 nvlist_free(config);
6298 * The split was successful. Now we need to open the new
6299 * pool and import it.
6301 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
6302 nvlist_free(config);
6308 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
6313 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
6314 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
6316 (void) fprintf(stderr, gettext("Split was successful, but "
6317 "the datasets could not all be mounted\n"));
6318 (void) fprintf(stderr, gettext("Try doing '%s' with a "
6319 "different altroot\n"), "zpool import");
6322 nvlist_free(config);
6331 * zpool online <pool> <device> ...
6334 zpool_do_online(int argc, char **argv)
6338 zpool_handle_t *zhp;
6340 vdev_state_t newstate;
6344 while ((c = getopt(argc, argv, "et")) != -1) {
6347 flags |= ZFS_ONLINE_EXPAND;
6351 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6360 /* get pool name and check number of arguments */
6362 (void) fprintf(stderr, gettext("missing pool name\n"));
6366 (void) fprintf(stderr, gettext("missing device name\n"));
6372 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6375 for (i = 1; i < argc; i++) {
6376 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
6377 if (newstate != VDEV_STATE_HEALTHY) {
6378 (void) printf(gettext("warning: device '%s' "
6379 "onlined, but remains in faulted state\n"),
6381 if (newstate == VDEV_STATE_FAULTED)
6382 (void) printf(gettext("use 'zpool "
6383 "clear' to restore a faulted "
6386 (void) printf(gettext("use 'zpool "
6387 "replace' to replace devices "
6388 "that are no longer present\n"));
6401 * zpool offline [-ft] <pool> <device> ...
6403 * -f Force the device into a faulted state.
6405 * -t Only take the device off-line temporarily. The offline/faulted
6406 * state will not be persistent across reboots.
6410 zpool_do_offline(int argc, char **argv)
6414 zpool_handle_t *zhp;
6416 boolean_t istmp = B_FALSE;
6417 boolean_t fault = B_FALSE;
6420 while ((c = getopt(argc, argv, "ft")) != -1) {
6429 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6438 /* get pool name and check number of arguments */
6440 (void) fprintf(stderr, gettext("missing pool name\n"));
6444 (void) fprintf(stderr, gettext("missing device name\n"));
6450 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6453 for (i = 1; i < argc; i++) {
6455 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
6457 if (istmp == B_FALSE) {
6458 /* Force the fault to persist across imports */
6459 aux = VDEV_AUX_EXTERNAL_PERSIST;
6461 aux = VDEV_AUX_EXTERNAL;
6464 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
6467 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
6478 * zpool clear <pool> [device]
6480 * Clear all errors associated with a pool or a particular device.
6483 zpool_do_clear(int argc, char **argv)
6487 boolean_t dryrun = B_FALSE;
6488 boolean_t do_rewind = B_FALSE;
6489 boolean_t xtreme_rewind = B_FALSE;
6490 uint32_t rewind_policy = ZPOOL_NO_REWIND;
6491 nvlist_t *policy = NULL;
6492 zpool_handle_t *zhp;
6493 char *pool, *device;
6496 while ((c = getopt(argc, argv, "FnX")) != -1) {
6505 xtreme_rewind = B_TRUE;
6508 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6518 (void) fprintf(stderr, gettext("missing pool name\n"));
6523 (void) fprintf(stderr, gettext("too many arguments\n"));
6527 if ((dryrun || xtreme_rewind) && !do_rewind) {
6528 (void) fprintf(stderr,
6529 gettext("-n or -X only meaningful with -F\n"));
6533 rewind_policy = ZPOOL_TRY_REWIND;
6535 rewind_policy = ZPOOL_DO_REWIND;
6537 rewind_policy |= ZPOOL_EXTREME_REWIND;
6539 /* In future, further rewind policy choices can be passed along here */
6540 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
6541 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
6542 rewind_policy) != 0) {
6547 device = argc == 2 ? argv[1] : NULL;
6549 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
6550 nvlist_free(policy);
6554 if (zpool_clear(zhp, device, policy) != 0)
6559 nvlist_free(policy);
6565 * zpool reguid <pool>
6568 zpool_do_reguid(int argc, char **argv)
6572 zpool_handle_t *zhp;
6576 while ((c = getopt(argc, argv, "")) != -1) {
6579 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6588 /* get pool name and check number of arguments */
6590 (void) fprintf(stderr, gettext("missing pool name\n"));
6595 (void) fprintf(stderr, gettext("too many arguments\n"));
6600 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6603 ret = zpool_reguid(zhp);
6611 * zpool reopen <pool>
6613 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6616 zpool_do_reopen(int argc, char **argv)
6620 boolean_t scrub_restart = B_TRUE;
6623 while ((c = getopt(argc, argv, "n")) != -1) {
6626 scrub_restart = B_FALSE;
6629 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6638 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6639 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
6645 typedef struct scrub_cbdata {
6649 pool_scrub_cmd_t cb_scrub_cmd;
6653 zpool_has_checkpoint(zpool_handle_t *zhp)
6655 nvlist_t *config, *nvroot;
6657 config = zpool_get_config(zhp, NULL);
6659 if (config != NULL) {
6660 pool_checkpoint_stat_t *pcs = NULL;
6663 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6664 (void) nvlist_lookup_uint64_array(nvroot,
6665 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
6667 if (pcs == NULL || pcs->pcs_state == CS_NONE)
6670 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
6671 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
6679 scrub_callback(zpool_handle_t *zhp, void *data)
6681 scrub_cbdata_t *cb = data;
6685 * Ignore faulted pools.
6687 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
6688 (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
6689 "currently unavailable\n"), zpool_get_name(zhp));
6693 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
6695 if (err == 0 && zpool_has_checkpoint(zhp) &&
6696 cb->cb_type == POOL_SCAN_SCRUB) {
6697 (void) printf(gettext("warning: will not scrub state that "
6698 "belongs to the checkpoint of pool '%s'\n"),
6699 zpool_get_name(zhp));
6706 * zpool scrub [-s | -p] <pool> ...
6708 * -s Stop. Stops any in-progress scrub.
6709 * -p Pause. Pause in-progress scrub.
6712 zpool_do_scrub(int argc, char **argv)
6717 cb.cb_type = POOL_SCAN_SCRUB;
6718 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6721 while ((c = getopt(argc, argv, "sp")) != -1) {
6724 cb.cb_type = POOL_SCAN_NONE;
6727 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
6730 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6736 if (cb.cb_type == POOL_SCAN_NONE &&
6737 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
6738 (void) fprintf(stderr, gettext("invalid option combination: "
6739 "-s and -p are mutually exclusive\n"));
6749 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6753 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6757 * zpool resilver <pool> ...
6759 * Restarts any in-progress resilver
6762 zpool_do_resilver(int argc, char **argv)
6767 cb.cb_type = POOL_SCAN_RESILVER;
6768 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
6773 while ((c = getopt(argc, argv, "")) != -1) {
6776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6786 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6790 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
6794 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
6796 * -c Cancel. Ends any in-progress trim.
6797 * -d Secure trim. Requires kernel and device support.
6798 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
6799 * adding a multiplier suffix such as 'k' or 'm'.
6800 * -s Suspend. TRIM can then be restarted with no flags.
6803 zpool_do_trim(int argc, char **argv)
6805 struct option long_options[] = {
6806 {"cancel", no_argument, NULL, 'c'},
6807 {"secure", no_argument, NULL, 'd'},
6808 {"rate", required_argument, NULL, 'r'},
6809 {"suspend", no_argument, NULL, 's'},
6813 pool_trim_func_t cmd_type = POOL_TRIM_START;
6815 boolean_t secure = B_FALSE;
6818 while ((c = getopt_long(argc, argv, "cdr:s", long_options, NULL))
6822 if (cmd_type != POOL_TRIM_START &&
6823 cmd_type != POOL_TRIM_CANCEL) {
6824 (void) fprintf(stderr, gettext("-c cannot be "
6825 "combined with other options\n"));
6828 cmd_type = POOL_TRIM_CANCEL;
6831 if (cmd_type != POOL_TRIM_START) {
6832 (void) fprintf(stderr, gettext("-d cannot be "
6833 "combined with the -c or -s options\n"));
6839 if (cmd_type != POOL_TRIM_START) {
6840 (void) fprintf(stderr, gettext("-r cannot be "
6841 "combined with the -c or -s options\n"));
6844 if (zfs_nicestrtonum(NULL, optarg, &rate) == -1) {
6845 (void) fprintf(stderr,
6846 gettext("invalid value for rate\n"));
6851 if (cmd_type != POOL_TRIM_START &&
6852 cmd_type != POOL_TRIM_SUSPEND) {
6853 (void) fprintf(stderr, gettext("-s cannot be "
6854 "combined with other options\n"));
6857 cmd_type = POOL_TRIM_SUSPEND;
6861 (void) fprintf(stderr,
6862 gettext("invalid option '%c'\n"), optopt);
6864 (void) fprintf(stderr,
6865 gettext("invalid option '%s'\n"),
6876 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6881 char *poolname = argv[0];
6882 zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
6886 trimflags_t trim_flags = {
6891 nvlist_t *vdevs = fnvlist_alloc();
6893 /* no individual leaf vdevs specified, so add them all */
6894 nvlist_t *config = zpool_get_config(zhp, NULL);
6895 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
6896 ZPOOL_CONFIG_VDEV_TREE);
6897 zpool_collect_leaves(zhp, nvroot, vdevs);
6898 trim_flags.fullpool = B_TRUE;
6900 trim_flags.fullpool = B_FALSE;
6901 for (int i = 1; i < argc; i++) {
6902 fnvlist_add_boolean(vdevs, argv[i]);
6906 int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags);
6908 fnvlist_free(vdevs);
6915 * Print out detailed scrub status.
6918 print_scan_status(pool_scan_stat_t *ps)
6920 time_t start, end, pause;
6921 uint64_t total_secs_left;
6922 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
6923 uint64_t pass_scanned, scanned, pass_issued, issued, total;
6924 uint64_t scan_rate, issue_rate;
6925 double fraction_done;
6926 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
6927 char srate_buf[7], irate_buf[7];
6929 (void) printf(gettext(" scan: "));
6931 /* If there's never been a scan, there's not much to say. */
6932 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
6933 ps->pss_func >= POOL_SCAN_FUNCS) {
6934 (void) printf(gettext("none requested\n"));
6938 start = ps->pss_start_time;
6939 end = ps->pss_end_time;
6940 pause = ps->pss_pass_scrub_pause;
6942 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6944 assert(ps->pss_func == POOL_SCAN_SCRUB ||
6945 ps->pss_func == POOL_SCAN_RESILVER);
6947 /* Scan is finished or canceled. */
6948 if (ps->pss_state == DSS_FINISHED) {
6949 total_secs_left = end - start;
6950 days_left = total_secs_left / 60 / 60 / 24;
6951 hours_left = (total_secs_left / 60 / 60) % 24;
6952 mins_left = (total_secs_left / 60) % 60;
6953 secs_left = (total_secs_left % 60);
6955 if (ps->pss_func == POOL_SCAN_SCRUB) {
6956 (void) printf(gettext("scrub repaired %s "
6957 "in %llu days %02llu:%02llu:%02llu "
6958 "with %llu errors on %s"), processed_buf,
6959 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6960 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6961 (u_longlong_t)ps->pss_errors, ctime(&end));
6962 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6963 (void) printf(gettext("resilvered %s "
6964 "in %llu days %02llu:%02llu:%02llu "
6965 "with %llu errors on %s"), processed_buf,
6966 (u_longlong_t)days_left, (u_longlong_t)hours_left,
6967 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6968 (u_longlong_t)ps->pss_errors, ctime(&end));
6971 } else if (ps->pss_state == DSS_CANCELED) {
6972 if (ps->pss_func == POOL_SCAN_SCRUB) {
6973 (void) printf(gettext("scrub canceled on %s"),
6975 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6976 (void) printf(gettext("resilver canceled on %s"),
6982 assert(ps->pss_state == DSS_SCANNING);
6984 /* Scan is in progress. Resilvers can't be paused. */
6985 if (ps->pss_func == POOL_SCAN_SCRUB) {
6987 (void) printf(gettext("scrub in progress since %s"),
6990 (void) printf(gettext("scrub paused since %s"),
6992 (void) printf(gettext("\tscrub started on %s"),
6995 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6996 (void) printf(gettext("resilver in progress since %s"),
7000 scanned = ps->pss_examined;
7001 pass_scanned = ps->pss_pass_exam;
7002 issued = ps->pss_issued;
7003 pass_issued = ps->pss_pass_issued;
7004 total = ps->pss_to_examine;
7006 /* we are only done with a block once we have issued the IO for it */
7007 fraction_done = (double)issued / total;
7009 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7010 elapsed = time(NULL) - ps->pss_pass_start;
7011 elapsed -= ps->pss_pass_scrub_spent_paused;
7012 elapsed = (elapsed != 0) ? elapsed : 1;
7014 scan_rate = pass_scanned / elapsed;
7015 issue_rate = pass_issued / elapsed;
7016 total_secs_left = (issue_rate != 0 && total >= issued) ?
7017 ((total - issued) / issue_rate) : UINT64_MAX;
7019 days_left = total_secs_left / 60 / 60 / 24;
7020 hours_left = (total_secs_left / 60 / 60) % 24;
7021 mins_left = (total_secs_left / 60) % 60;
7022 secs_left = (total_secs_left % 60);
7024 /* format all of the numbers we will be reporting */
7025 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
7026 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
7027 zfs_nicebytes(total, total_buf, sizeof (total_buf));
7028 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
7029 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
7031 /* do not print estimated time if we have a paused scrub */
7033 (void) printf(gettext("\t%s scanned at %s/s, "
7034 "%s issued at %s/s, %s total\n"),
7035 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
7037 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7038 scanned_buf, issued_buf, total_buf);
7041 if (ps->pss_func == POOL_SCAN_RESILVER) {
7042 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7043 processed_buf, 100 * fraction_done);
7044 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
7045 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7046 processed_buf, 100 * fraction_done);
7050 if (total_secs_left != UINT64_MAX &&
7051 issue_rate >= 10 * 1024 * 1024) {
7052 (void) printf(gettext(", %llu days "
7053 "%02llu:%02llu:%02llu to go\n"),
7054 (u_longlong_t)days_left, (u_longlong_t)hours_left,
7055 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
7057 (void) printf(gettext(", no estimated "
7058 "completion time\n"));
7061 (void) printf(gettext("\n"));
7066 * As we don't scrub checkpointed blocks, we want to warn the
7067 * user that we skipped scanning some blocks if a checkpoint exists
7068 * or existed at any time during the scan.
7071 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
7073 if (ps == NULL || pcs == NULL)
7076 if (pcs->pcs_state == CS_NONE ||
7077 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
7080 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
7082 if (ps->pss_state == DSS_NONE)
7085 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
7086 ps->pss_end_time < pcs->pcs_start_time)
7089 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
7090 (void) printf(gettext(" scan warning: skipped blocks "
7091 "that are only referenced by the checkpoint.\n"));
7093 assert(ps->pss_state == DSS_SCANNING);
7094 (void) printf(gettext(" scan warning: skipping blocks "
7095 "that are only referenced by the checkpoint.\n"));
7100 * Print out detailed removal status.
7103 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
7105 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
7107 nvlist_t *config, *nvroot;
7112 if (prs == NULL || prs->prs_state == DSS_NONE)
7116 * Determine name of vdev.
7118 config = zpool_get_config(zhp, NULL);
7119 nvroot = fnvlist_lookup_nvlist(config,
7120 ZPOOL_CONFIG_VDEV_TREE);
7121 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7122 &child, &children) == 0);
7123 assert(prs->prs_removing_vdev < children);
7124 vdev_name = zpool_vdev_name(g_zfs, zhp,
7125 child[prs->prs_removing_vdev], B_TRUE);
7127 (void) printf(gettext("remove: "));
7129 start = prs->prs_start_time;
7130 end = prs->prs_end_time;
7131 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
7134 * Removal is finished or canceled.
7136 if (prs->prs_state == DSS_FINISHED) {
7137 uint64_t minutes_taken = (end - start) / 60;
7139 (void) printf(gettext("Removal of vdev %llu copied %s "
7140 "in %lluh%um, completed on %s"),
7141 (longlong_t)prs->prs_removing_vdev,
7143 (u_longlong_t)(minutes_taken / 60),
7144 (uint_t)(minutes_taken % 60),
7145 ctime((time_t *)&end));
7146 } else if (prs->prs_state == DSS_CANCELED) {
7147 (void) printf(gettext("Removal of %s canceled on %s"),
7148 vdev_name, ctime(&end));
7150 uint64_t copied, total, elapsed, mins_left, hours_left;
7151 double fraction_done;
7154 assert(prs->prs_state == DSS_SCANNING);
7157 * Removal is in progress.
7159 (void) printf(gettext(
7160 "Evacuation of %s in progress since %s"),
7161 vdev_name, ctime(&start));
7163 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
7164 total = prs->prs_to_copy;
7165 fraction_done = (double)copied / total;
7167 /* elapsed time for this pass */
7168 elapsed = time(NULL) - prs->prs_start_time;
7169 elapsed = elapsed > 0 ? elapsed : 1;
7170 rate = copied / elapsed;
7171 rate = rate > 0 ? rate : 1;
7172 mins_left = ((total - copied) / rate) / 60;
7173 hours_left = mins_left / 60;
7175 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
7176 zfs_nicenum(total, total_buf, sizeof (total_buf));
7177 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
7180 * do not print estimated time if hours_left is more than
7183 (void) printf(gettext(" %s copied out of %s at %s/s, "
7185 examined_buf, total_buf, rate_buf, 100 * fraction_done);
7186 if (hours_left < (30 * 24)) {
7187 (void) printf(gettext(", %lluh%um to go\n"),
7188 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
7190 (void) printf(gettext(
7191 ", (copy is slow, no estimated time)\n"));
7195 if (prs->prs_mapping_memory > 0) {
7197 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
7198 (void) printf(gettext(" %s memory used for "
7199 "removed device mappings\n"),
7205 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
7210 if (pcs == NULL || pcs->pcs_state == CS_NONE)
7213 (void) printf(gettext("checkpoint: "));
7215 start = pcs->pcs_start_time;
7216 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
7218 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
7219 char *date = ctime(&start);
7222 * ctime() adds a newline at the end of the generated
7223 * string, thus the weird format specifier and the
7224 * strlen() call used to chop it off from the output.
7226 (void) printf(gettext("created %.*s, consumes %s\n"),
7227 (int)(strlen(date) - 1), date, space_buf);
7231 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
7233 (void) printf(gettext("discarding, %s remaining.\n"),
7238 print_error_log(zpool_handle_t *zhp)
7240 nvlist_t *nverrlist = NULL;
7243 size_t len = MAXPATHLEN * 2;
7245 if (zpool_get_errlog(zhp, &nverrlist) != 0)
7248 (void) printf("errors: Permanent errors have been "
7249 "detected in the following files:\n\n");
7251 pathname = safe_malloc(len);
7253 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
7255 uint64_t dsobj, obj;
7257 verify(nvpair_value_nvlist(elem, &nv) == 0);
7258 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
7260 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
7262 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
7263 (void) printf("%7s %s\n", "", pathname);
7266 nvlist_free(nverrlist);
7270 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
7279 (void) printf(gettext("\tspares\n"));
7281 for (i = 0; i < nspares; i++) {
7282 name = zpool_vdev_name(g_zfs, zhp, spares[i],
7284 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
7290 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
7299 (void) printf(gettext("\tcache\n"));
7301 for (i = 0; i < nl2cache; i++) {
7302 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
7304 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
7310 print_dedup_stats(nvlist_t *config)
7312 ddt_histogram_t *ddh;
7316 char dspace[6], mspace[6];
7319 * If the pool was faulted then we may not have been able to
7320 * obtain the config. Otherwise, if we have anything in the dedup
7321 * table continue processing the stats.
7323 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
7324 (uint64_t **)&ddo, &c) != 0)
7327 (void) printf("\n");
7328 (void) printf(gettext(" dedup: "));
7329 if (ddo->ddo_count == 0) {
7330 (void) printf(gettext("no DDT entries\n"));
7334 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
7335 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
7336 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
7337 (u_longlong_t)ddo->ddo_count,
7341 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
7342 (uint64_t **)&dds, &c) == 0);
7343 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
7344 (uint64_t **)&ddh, &c) == 0);
7345 zpool_dump_ddt(dds, ddh);
7349 * Display a summary of pool status. Displays a summary such as:
7353 * reason: One or more devices ...
7354 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
7360 * When given the '-v' option, we print out the complete config. If the '-e'
7361 * option is specified, then we print out error rate information as well.
7364 status_callback(zpool_handle_t *zhp, void *data)
7366 status_cbdata_t *cbp = data;
7367 nvlist_t *config, *nvroot;
7369 zpool_status_t reason;
7370 zpool_errata_t errata;
7375 config = zpool_get_config(zhp, NULL);
7376 reason = zpool_get_status(zhp, &msgid, &errata);
7381 * If we were given 'zpool status -x', only report those pools with
7384 if (cbp->cb_explain &&
7385 (reason == ZPOOL_STATUS_OK ||
7386 reason == ZPOOL_STATUS_VERSION_OLDER ||
7387 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
7388 if (!cbp->cb_allpools) {
7389 (void) printf(gettext("pool '%s' is healthy\n"),
7390 zpool_get_name(zhp));
7392 cbp->cb_first = B_FALSE;
7398 cbp->cb_first = B_FALSE;
7400 (void) printf("\n");
7402 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
7403 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
7404 (uint64_t **)&vs, &c) == 0);
7406 health = zpool_get_state_str(zhp);
7408 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
7409 (void) printf(gettext(" state: %s\n"), health);
7412 case ZPOOL_STATUS_MISSING_DEV_R:
7413 (void) printf(gettext("status: One or more devices could not "
7414 "be opened. Sufficient replicas exist for\n\tthe pool to "
7415 "continue functioning in a degraded state.\n"));
7416 (void) printf(gettext("action: Attach the missing device and "
7417 "online it using 'zpool online'.\n"));
7420 case ZPOOL_STATUS_MISSING_DEV_NR:
7421 (void) printf(gettext("status: One or more devices could not "
7422 "be opened. There are insufficient\n\treplicas for the "
7423 "pool to continue functioning.\n"));
7424 (void) printf(gettext("action: Attach the missing device and "
7425 "online it using 'zpool online'.\n"));
7428 case ZPOOL_STATUS_CORRUPT_LABEL_R:
7429 (void) printf(gettext("status: One or more devices could not "
7430 "be used because the label is missing or\n\tinvalid. "
7431 "Sufficient replicas exist for the pool to continue\n\t"
7432 "functioning in a degraded state.\n"));
7433 (void) printf(gettext("action: Replace the device using "
7434 "'zpool replace'.\n"));
7437 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
7438 (void) printf(gettext("status: One or more devices could not "
7439 "be used because the label is missing \n\tor invalid. "
7440 "There are insufficient replicas for the pool to "
7441 "continue\n\tfunctioning.\n"));
7442 zpool_explain_recover(zpool_get_handle(zhp),
7443 zpool_get_name(zhp), reason, config);
7446 case ZPOOL_STATUS_FAILING_DEV:
7447 (void) printf(gettext("status: One or more devices has "
7448 "experienced an unrecoverable error. An\n\tattempt was "
7449 "made to correct the error. Applications are "
7451 (void) printf(gettext("action: Determine if the device needs "
7452 "to be replaced, and clear the errors\n\tusing "
7453 "'zpool clear' or replace the device with 'zpool "
7457 case ZPOOL_STATUS_OFFLINE_DEV:
7458 (void) printf(gettext("status: One or more devices has "
7459 "been taken offline by the administrator.\n\tSufficient "
7460 "replicas exist for the pool to continue functioning in "
7461 "a\n\tdegraded state.\n"));
7462 (void) printf(gettext("action: Online the device using "
7463 "'zpool online' or replace the device with\n\t'zpool "
7467 case ZPOOL_STATUS_REMOVED_DEV:
7468 (void) printf(gettext("status: One or more devices has "
7469 "been removed by the administrator.\n\tSufficient "
7470 "replicas exist for the pool to continue functioning in "
7471 "a\n\tdegraded state.\n"));
7472 (void) printf(gettext("action: Online the device using "
7473 "'zpool online' or replace the device with\n\t'zpool "
7477 case ZPOOL_STATUS_RESILVERING:
7478 (void) printf(gettext("status: One or more devices is "
7479 "currently being resilvered. The pool will\n\tcontinue "
7480 "to function, possibly in a degraded state.\n"));
7481 (void) printf(gettext("action: Wait for the resilver to "
7485 case ZPOOL_STATUS_CORRUPT_DATA:
7486 (void) printf(gettext("status: One or more devices has "
7487 "experienced an error resulting in data\n\tcorruption. "
7488 "Applications may be affected.\n"));
7489 (void) printf(gettext("action: Restore the file in question "
7490 "if possible. Otherwise restore the\n\tentire pool from "
7494 case ZPOOL_STATUS_CORRUPT_POOL:
7495 (void) printf(gettext("status: The pool metadata is corrupted "
7496 "and the pool cannot be opened.\n"));
7497 zpool_explain_recover(zpool_get_handle(zhp),
7498 zpool_get_name(zhp), reason, config);
7501 case ZPOOL_STATUS_VERSION_OLDER:
7502 (void) printf(gettext("status: The pool is formatted using a "
7503 "legacy on-disk format. The pool can\n\tstill be used, "
7504 "but some features are unavailable.\n"));
7505 (void) printf(gettext("action: Upgrade the pool using 'zpool "
7506 "upgrade'. Once this is done, the\n\tpool will no longer "
7507 "be accessible on software that does not support\n\t"
7508 "feature flags.\n"));
7511 case ZPOOL_STATUS_VERSION_NEWER:
7512 (void) printf(gettext("status: The pool has been upgraded to a "
7513 "newer, incompatible on-disk version.\n\tThe pool cannot "
7514 "be accessed on this system.\n"));
7515 (void) printf(gettext("action: Access the pool from a system "
7516 "running more recent software, or\n\trestore the pool from "
7520 case ZPOOL_STATUS_FEAT_DISABLED:
7521 (void) printf(gettext("status: Some supported features are not "
7522 "enabled on the pool. The pool can\n\tstill be used, but "
7523 "some features are unavailable.\n"));
7524 (void) printf(gettext("action: Enable all features using "
7525 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
7526 "longer be accessible by software that does not support\n\t"
7527 "the features. See zpool-features(5) for details.\n"));
7530 case ZPOOL_STATUS_UNSUP_FEAT_READ:
7531 (void) printf(gettext("status: The pool cannot be accessed on "
7532 "this system because it uses the\n\tfollowing feature(s) "
7533 "not supported on this system:\n"));
7534 zpool_print_unsup_feat(config);
7535 (void) printf("\n");
7536 (void) printf(gettext("action: Access the pool from a system "
7537 "that supports the required feature(s),\n\tor restore the "
7538 "pool from backup.\n"));
7541 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
7542 (void) printf(gettext("status: The pool can only be accessed "
7543 "in read-only mode on this system. It\n\tcannot be "
7544 "accessed in read-write mode because it uses the "
7545 "following\n\tfeature(s) not supported on this system:\n"));
7546 zpool_print_unsup_feat(config);
7547 (void) printf("\n");
7548 (void) printf(gettext("action: The pool cannot be accessed in "
7549 "read-write mode. Import the pool with\n"
7550 "\t\"-o readonly=on\", access the pool from a system that "
7551 "supports the\n\trequired feature(s), or restore the "
7552 "pool from backup.\n"));
7555 case ZPOOL_STATUS_FAULTED_DEV_R:
7556 (void) printf(gettext("status: One or more devices are "
7557 "faulted in response to persistent errors.\n\tSufficient "
7558 "replicas exist for the pool to continue functioning "
7559 "in a\n\tdegraded state.\n"));
7560 (void) printf(gettext("action: Replace the faulted device, "
7561 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
7564 case ZPOOL_STATUS_FAULTED_DEV_NR:
7565 (void) printf(gettext("status: One or more devices are "
7566 "faulted in response to persistent errors. There are "
7567 "insufficient replicas for the pool to\n\tcontinue "
7569 (void) printf(gettext("action: Destroy and re-create the pool "
7570 "from a backup source. Manually marking the device\n"
7571 "\trepaired using 'zpool clear' may allow some data "
7572 "to be recovered.\n"));
7575 case ZPOOL_STATUS_IO_FAILURE_MMP:
7576 (void) printf(gettext("status: The pool is suspended because "
7577 "multihost writes failed or were delayed;\n\tanother "
7578 "system could import the pool undetected.\n"));
7579 (void) printf(gettext("action: Make sure the pool's devices "
7580 "are connected, then reboot your system and\n\timport the "
7584 case ZPOOL_STATUS_IO_FAILURE_WAIT:
7585 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
7586 (void) printf(gettext("status: One or more devices are "
7587 "faulted in response to IO failures.\n"));
7588 (void) printf(gettext("action: Make sure the affected devices "
7589 "are connected, then run 'zpool clear'.\n"));
7592 case ZPOOL_STATUS_BAD_LOG:
7593 (void) printf(gettext("status: An intent log record "
7594 "could not be read.\n"
7595 "\tWaiting for administrator intervention to fix the "
7596 "faulted pool.\n"));
7597 (void) printf(gettext("action: Either restore the affected "
7598 "device(s) and run 'zpool online',\n"
7599 "\tor ignore the intent log records by running "
7600 "'zpool clear'.\n"));
7603 case ZPOOL_STATUS_HOSTID_MISMATCH:
7604 (void) printf(gettext("status: Mismatch between pool hostid "
7605 "and system hostid on imported pool.\n\tThis pool was "
7606 "previously imported into a system with a different "
7607 "hostid,\n\tand then was verbatim imported into this "
7609 (void) printf(gettext("action: Export this pool on all systems "
7610 "on which it is imported.\n"
7611 "\tThen import it to correct the mismatch.\n"));
7614 case ZPOOL_STATUS_ERRATA:
7615 (void) printf(gettext("status: Errata #%d detected.\n"),
7619 case ZPOOL_ERRATA_NONE:
7622 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
7623 (void) printf(gettext("action: To correct the issue "
7624 "run 'zpool scrub'.\n"));
7627 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
7628 (void) printf(gettext("\tExisting encrypted datasets "
7629 "contain an on-disk incompatibility\n\twhich "
7630 "needs to be corrected.\n"));
7631 (void) printf(gettext("action: To correct the issue "
7632 "backup existing encrypted datasets to new\n\t"
7633 "encrypted datasets and destroy the old ones. "
7634 "'zfs mount -o ro' can\n\tbe used to temporarily "
7635 "mount existing encrypted datasets readonly.\n"));
7638 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
7639 (void) printf(gettext("\tExisting encrypted snapshots "
7640 "and bookmarks contain an on-disk\n\tincompat"
7641 "ibility. This may cause on-disk corruption if "
7642 "they are used\n\twith 'zfs recv'.\n"));
7643 (void) printf(gettext("action: To correct the issue, "
7644 "enable the bookmark_v2 feature. No additional\n\t"
7645 "action is needed if there are no encrypted "
7646 "snapshots or bookmarks.\n\tIf preserving the "
7647 "encrypted snapshots and bookmarks is required, "
7648 "use\n\ta non-raw send to backup and restore them. "
7649 "Alternately, they may be\n\tremoved to resolve "
7650 "the incompatibility.\n"));
7655 * All errata which allow the pool to be imported
7656 * must contain an action message.
7664 * The remaining errors can't actually be generated, yet.
7666 assert(reason == ZPOOL_STATUS_OK);
7670 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
7673 if (config != NULL) {
7675 nvlist_t **spares, **l2cache;
7676 uint_t nspares, nl2cache;
7677 pool_checkpoint_stat_t *pcs = NULL;
7678 pool_scan_stat_t *ps = NULL;
7679 pool_removal_stat_t *prs = NULL;
7681 (void) nvlist_lookup_uint64_array(nvroot,
7682 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7683 (void) nvlist_lookup_uint64_array(nvroot,
7684 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
7685 (void) nvlist_lookup_uint64_array(nvroot,
7686 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
7688 print_scan_status(ps);
7689 print_checkpoint_scan_warning(ps, pcs);
7690 print_removal_status(zhp, prs);
7691 print_checkpoint_status(pcs);
7693 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
7694 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
7695 if (cbp->cb_namewidth < 10)
7696 cbp->cb_namewidth = 10;
7698 (void) printf(gettext("config:\n\n"));
7699 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
7700 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
7703 if (cbp->cb_print_slow_ios)
7704 (void) printf(" %5s", gettext("SLOW"));
7706 if (cbp->vcdl != NULL)
7707 print_cmd_columns(cbp->vcdl, 0);
7711 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
7714 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
7715 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
7716 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
7718 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
7719 &l2cache, &nl2cache) == 0)
7720 print_l2cache(zhp, cbp, l2cache, nl2cache);
7722 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
7723 &spares, &nspares) == 0)
7724 print_spares(zhp, cbp, spares, nspares);
7726 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
7728 nvlist_t *nverrlist = NULL;
7731 * If the approximate error count is small, get a
7732 * precise count by fetching the entire log and
7733 * uniquifying the results.
7735 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
7736 zpool_get_errlog(zhp, &nverrlist) == 0) {
7741 while ((elem = nvlist_next_nvpair(nverrlist,
7746 nvlist_free(nverrlist);
7748 (void) printf("\n");
7751 (void) printf(gettext("errors: No known data "
7753 else if (!cbp->cb_verbose)
7754 (void) printf(gettext("errors: %llu data "
7755 "errors, use '-v' for a list\n"),
7756 (u_longlong_t)nerr);
7758 print_error_log(zhp);
7761 if (cbp->cb_dedup_stats)
7762 print_dedup_stats(config);
7764 (void) printf(gettext("config: The configuration cannot be "
7772 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
7773 * [interval [count]]
7775 * -c CMD For each vdev, run command CMD
7776 * -i Display vdev initialization status.
7777 * -g Display guid for individual vdev name.
7778 * -L Follow links when resolving vdev path name.
7779 * -p Display values in parsable (exact) format.
7780 * -P Display full path for vdev name.
7781 * -s Display slow IOs column.
7782 * -v Display complete error logs
7783 * -x Display only pools with potential problems
7784 * -D Display dedup status (undocumented)
7785 * -t Display vdev TRIM status.
7786 * -T Display a timestamp in date(1) or Unix format
7788 * Describes the health status of all pools or some subset.
7791 zpool_do_status(int argc, char **argv)
7796 unsigned long count = 0;
7797 status_cbdata_t cb = { 0 };
7801 while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
7806 gettext("Can't set -c flag twice\n"));
7810 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
7811 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
7812 fprintf(stderr, gettext(
7813 "Can't run -c, disabled by "
7814 "ZPOOL_SCRIPTS_ENABLED.\n"));
7818 if ((getuid() <= 0 || geteuid() <= 0) &&
7819 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
7820 fprintf(stderr, gettext(
7821 "Can't run -c with root privileges "
7822 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
7828 cb.cb_print_vdev_init = B_TRUE;
7831 cb.cb_name_flags |= VDEV_NAME_GUID;
7834 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
7837 cb.cb_literal = B_TRUE;
7840 cb.cb_name_flags |= VDEV_NAME_PATH;
7843 cb.cb_print_slow_ios = B_TRUE;
7846 cb.cb_verbose = B_TRUE;
7849 cb.cb_explain = B_TRUE;
7852 cb.cb_dedup_stats = B_TRUE;
7855 cb.cb_print_vdev_trim = B_TRUE;
7858 get_timestamp_arg(*optarg);
7861 if (optopt == 'c') {
7862 print_zpool_script_list("status");
7866 gettext("invalid option '%c'\n"), optopt);
7875 get_interval_count(&argc, argv, &interval, &count);
7878 cb.cb_allpools = B_TRUE;
7880 cb.cb_first = B_TRUE;
7881 cb.cb_print_status = B_TRUE;
7884 if (timestamp_fmt != NODATE)
7885 print_timestamp(timestamp_fmt);
7888 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
7891 ret = for_each_pool(argc, argv, B_TRUE, NULL,
7892 status_callback, &cb);
7894 if (cb.vcdl != NULL)
7895 free_vdev_cmd_data_list(cb.vcdl);
7897 if (argc == 0 && cb.cb_count == 0)
7898 (void) fprintf(stderr, gettext("no pools available\n"));
7899 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
7900 (void) printf(gettext("all pools are healthy\n"));
7908 if (count != 0 && --count == 0)
7911 (void) fsleep(interval);
7917 typedef struct upgrade_cbdata {
7920 uint64_t cb_version;
7925 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
7927 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
7928 int *count = (int *)unsupp_fs;
7930 if (zfs_version > ZPL_VERSION) {
7931 (void) printf(gettext("%s (v%d) is not supported by this "
7932 "implementation of ZFS.\n"),
7933 zfs_get_name(zhp), zfs_version);
7937 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
7945 upgrade_version(zpool_handle_t *zhp, uint64_t version)
7949 uint64_t oldversion;
7952 config = zpool_get_config(zhp, NULL);
7953 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
7956 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
7957 assert(oldversion < version);
7959 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
7964 (void) fprintf(stderr, gettext("Upgrade not performed due "
7965 "to %d unsupported filesystems (max v%d).\n"),
7966 unsupp_fs, (int)ZPL_VERSION);
7970 ret = zpool_upgrade(zhp, version);
7974 if (version >= SPA_VERSION_FEATURES) {
7975 (void) printf(gettext("Successfully upgraded "
7976 "'%s' from version %llu to feature flags.\n"),
7977 zpool_get_name(zhp), (u_longlong_t)oldversion);
7979 (void) printf(gettext("Successfully upgraded "
7980 "'%s' from version %llu to version %llu.\n"),
7981 zpool_get_name(zhp), (u_longlong_t)oldversion,
7982 (u_longlong_t)version);
7989 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
7992 boolean_t firstff = B_TRUE;
7993 nvlist_t *enabled = zpool_get_features(zhp);
7996 for (i = 0; i < SPA_FEATURES; i++) {
7997 const char *fname = spa_feature_table[i].fi_uname;
7998 const char *fguid = spa_feature_table[i].fi_guid;
7999 if (!nvlist_exists(enabled, fguid)) {
8001 verify(-1 != asprintf(&propname, "feature@%s", fname));
8002 ret = zpool_set_prop(zhp, propname,
8003 ZFS_FEATURE_ENABLED);
8011 (void) printf(gettext("Enabled the "
8012 "following features on '%s':\n"),
8013 zpool_get_name(zhp));
8016 (void) printf(gettext(" %s\n"), fname);
8027 upgrade_cb(zpool_handle_t *zhp, void *arg)
8029 upgrade_cbdata_t *cbp = arg;
8032 boolean_t printnl = B_FALSE;
8035 config = zpool_get_config(zhp, NULL);
8036 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8039 assert(SPA_VERSION_IS_SUPPORTED(version));
8041 if (version < cbp->cb_version) {
8042 cbp->cb_first = B_FALSE;
8043 ret = upgrade_version(zhp, cbp->cb_version);
8049 * If they did "zpool upgrade -a", then we could
8050 * be doing ioctls to different pools. We need
8051 * to log this history once to each pool, and bypass
8052 * the normal history logging that happens in main().
8054 (void) zpool_log_history(g_zfs, history_str);
8055 log_history = B_FALSE;
8058 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
8060 ret = upgrade_enable_all(zhp, &count);
8065 cbp->cb_first = B_FALSE;
8071 (void) printf(gettext("\n"));
8078 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
8080 upgrade_cbdata_t *cbp = arg;
8084 config = zpool_get_config(zhp, NULL);
8085 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8088 assert(SPA_VERSION_IS_SUPPORTED(version));
8090 if (version < SPA_VERSION_FEATURES) {
8091 if (cbp->cb_first) {
8092 (void) printf(gettext("The following pools are "
8093 "formatted with legacy version numbers and can\n"
8094 "be upgraded to use feature flags. After "
8095 "being upgraded, these pools\nwill no "
8096 "longer be accessible by software that does not "
8097 "support feature\nflags.\n\n"));
8098 (void) printf(gettext("VER POOL\n"));
8099 (void) printf(gettext("--- ------------\n"));
8100 cbp->cb_first = B_FALSE;
8103 (void) printf("%2llu %s\n", (u_longlong_t)version,
8104 zpool_get_name(zhp));
8111 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
8113 upgrade_cbdata_t *cbp = arg;
8117 config = zpool_get_config(zhp, NULL);
8118 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8121 if (version >= SPA_VERSION_FEATURES) {
8123 boolean_t poolfirst = B_TRUE;
8124 nvlist_t *enabled = zpool_get_features(zhp);
8126 for (i = 0; i < SPA_FEATURES; i++) {
8127 const char *fguid = spa_feature_table[i].fi_guid;
8128 const char *fname = spa_feature_table[i].fi_uname;
8129 if (!nvlist_exists(enabled, fguid)) {
8130 if (cbp->cb_first) {
8131 (void) printf(gettext("\nSome "
8132 "supported features are not "
8133 "enabled on the following pools. "
8134 "Once a\nfeature is enabled the "
8135 "pool may become incompatible with "
8136 "software\nthat does not support "
8138 "zpool-features(5) for "
8140 (void) printf(gettext("POOL "
8142 (void) printf(gettext("------"
8144 cbp->cb_first = B_FALSE;
8148 (void) printf(gettext("%s\n"),
8149 zpool_get_name(zhp));
8150 poolfirst = B_FALSE;
8153 (void) printf(gettext(" %s\n"), fname);
8156 * If they did "zpool upgrade -a", then we could
8157 * be doing ioctls to different pools. We need
8158 * to log this history once to each pool, and bypass
8159 * the normal history logging that happens in main().
8161 (void) zpool_log_history(g_zfs, history_str);
8162 log_history = B_FALSE;
8171 upgrade_one(zpool_handle_t *zhp, void *data)
8173 boolean_t printnl = B_FALSE;
8174 upgrade_cbdata_t *cbp = data;
8175 uint64_t cur_version;
8178 if (strcmp("log", zpool_get_name(zhp)) == 0) {
8179 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
8180 "Pool 'log' must be renamed using export and import"
8185 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
8186 if (cur_version > cbp->cb_version) {
8187 (void) printf(gettext("Pool '%s' is already formatted "
8188 "using more current version '%llu'.\n\n"),
8189 zpool_get_name(zhp), (u_longlong_t)cur_version);
8193 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
8194 (void) printf(gettext("Pool '%s' is already formatted "
8195 "using version %llu.\n\n"), zpool_get_name(zhp),
8196 (u_longlong_t)cbp->cb_version);
8200 if (cur_version != cbp->cb_version) {
8202 ret = upgrade_version(zhp, cbp->cb_version);
8207 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
8209 ret = upgrade_enable_all(zhp, &count);
8215 } else if (cur_version == SPA_VERSION) {
8216 (void) printf(gettext("Pool '%s' already has all "
8217 "supported features enabled.\n"),
8218 zpool_get_name(zhp));
8223 (void) printf(gettext("\n"));
8232 * zpool upgrade [-V version] <-a | pool ...>
8234 * With no arguments, display downrev'd ZFS pool available for upgrade.
8235 * Individual pools can be upgraded by specifying the pool, and '-a' will
8236 * upgrade all pools.
8239 zpool_do_upgrade(int argc, char **argv)
8242 upgrade_cbdata_t cb = { 0 };
8244 boolean_t showversions = B_FALSE;
8245 boolean_t upgradeall = B_FALSE;
8250 while ((c = getopt(argc, argv, ":avV:")) != -1) {
8253 upgradeall = B_TRUE;
8256 showversions = B_TRUE;
8259 cb.cb_version = strtoll(optarg, &end, 10);
8261 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
8262 (void) fprintf(stderr,
8263 gettext("invalid version '%s'\n"), optarg);
8268 (void) fprintf(stderr, gettext("missing argument for "
8269 "'%c' option\n"), optopt);
8273 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8284 if (cb.cb_version == 0) {
8285 cb.cb_version = SPA_VERSION;
8286 } else if (!upgradeall && argc == 0) {
8287 (void) fprintf(stderr, gettext("-V option is "
8288 "incompatible with other arguments\n"));
8293 if (upgradeall || argc != 0) {
8294 (void) fprintf(stderr, gettext("-v option is "
8295 "incompatible with other arguments\n"));
8298 } else if (upgradeall) {
8300 (void) fprintf(stderr, gettext("-a option should not "
8301 "be used along with a pool name\n"));
8306 (void) printf(gettext("This system supports ZFS pool feature "
8311 (void) printf(gettext("The following features are "
8313 (void) printf(gettext("FEAT DESCRIPTION\n"));
8314 (void) printf("----------------------------------------------"
8315 "---------------\n");
8316 for (i = 0; i < SPA_FEATURES; i++) {
8317 zfeature_info_t *fi = &spa_feature_table[i];
8319 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
8320 " (read-only compatible)" : "";
8322 (void) printf("%-37s%s\n", fi->fi_uname, ro);
8323 (void) printf(" %s\n", fi->fi_desc);
8325 (void) printf("\n");
8327 (void) printf(gettext("The following legacy versions are also "
8329 (void) printf(gettext("VER DESCRIPTION\n"));
8330 (void) printf("--- -----------------------------------------"
8331 "---------------\n");
8332 (void) printf(gettext(" 1 Initial ZFS version\n"));
8333 (void) printf(gettext(" 2 Ditto blocks "
8334 "(replicated metadata)\n"));
8335 (void) printf(gettext(" 3 Hot spares and double parity "
8337 (void) printf(gettext(" 4 zpool history\n"));
8338 (void) printf(gettext(" 5 Compression using the gzip "
8340 (void) printf(gettext(" 6 bootfs pool property\n"));
8341 (void) printf(gettext(" 7 Separate intent log devices\n"));
8342 (void) printf(gettext(" 8 Delegated administration\n"));
8343 (void) printf(gettext(" 9 refquota and refreservation "
8345 (void) printf(gettext(" 10 Cache devices\n"));
8346 (void) printf(gettext(" 11 Improved scrub performance\n"));
8347 (void) printf(gettext(" 12 Snapshot properties\n"));
8348 (void) printf(gettext(" 13 snapused property\n"));
8349 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
8350 (void) printf(gettext(" 15 user/group space accounting\n"));
8351 (void) printf(gettext(" 16 stmf property support\n"));
8352 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
8353 (void) printf(gettext(" 18 Snapshot user holds\n"));
8354 (void) printf(gettext(" 19 Log device removal\n"));
8355 (void) printf(gettext(" 20 Compression using zle "
8356 "(zero-length encoding)\n"));
8357 (void) printf(gettext(" 21 Deduplication\n"));
8358 (void) printf(gettext(" 22 Received properties\n"));
8359 (void) printf(gettext(" 23 Slim ZIL\n"));
8360 (void) printf(gettext(" 24 System attributes\n"));
8361 (void) printf(gettext(" 25 Improved scrub stats\n"));
8362 (void) printf(gettext(" 26 Improved snapshot deletion "
8364 (void) printf(gettext(" 27 Improved snapshot creation "
8366 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
8367 (void) printf(gettext("\nFor more information on a particular "
8368 "version, including supported releases,\n"));
8369 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
8370 } else if (argc == 0 && upgradeall) {
8371 cb.cb_first = B_TRUE;
8372 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
8373 if (ret == 0 && cb.cb_first) {
8374 if (cb.cb_version == SPA_VERSION) {
8375 (void) printf(gettext("All pools are already "
8376 "formatted using feature flags.\n\n"));
8377 (void) printf(gettext("Every feature flags "
8378 "pool already has all supported features "
8381 (void) printf(gettext("All pools are already "
8382 "formatted with version %llu or higher.\n"),
8383 (u_longlong_t)cb.cb_version);
8386 } else if (argc == 0) {
8387 cb.cb_first = B_TRUE;
8388 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
8392 (void) printf(gettext("All pools are formatted "
8393 "using feature flags.\n\n"));
8395 (void) printf(gettext("\nUse 'zpool upgrade -v' "
8396 "for a list of available legacy versions.\n"));
8399 cb.cb_first = B_TRUE;
8400 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
8404 (void) printf(gettext("Every feature flags pool has "
8405 "all supported features enabled.\n"));
8407 (void) printf(gettext("\n"));
8410 ret = for_each_pool(argc, argv, B_FALSE, NULL,
8417 typedef struct hist_cbdata {
8424 * Print out the command history for a specific pool.
8427 get_history_one(zpool_handle_t *zhp, void *data)
8433 hist_cbdata_t *cb = (hist_cbdata_t *)data;
8435 cb->first = B_FALSE;
8437 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
8439 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
8442 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
8443 &records, &numrecords) == 0);
8444 for (i = 0; i < numrecords; i++) {
8445 nvlist_t *rec = records[i];
8448 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
8452 tsec = fnvlist_lookup_uint64(records[i],
8454 (void) localtime_r(&tsec, &t);
8455 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
8458 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
8459 (void) printf("%s %s", tbuf,
8460 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
8461 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
8463 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
8466 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
8467 (void) printf("%s unrecognized record:\n",
8469 dump_nvlist(rec, 4);
8472 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
8473 zfs_history_event_names[ievent],
8474 (longlong_t)fnvlist_lookup_uint64(
8475 rec, ZPOOL_HIST_TXG),
8476 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
8477 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
8480 (void) printf("%s [txg:%lld] %s", tbuf,
8481 (longlong_t)fnvlist_lookup_uint64(
8482 rec, ZPOOL_HIST_TXG),
8483 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
8484 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
8485 (void) printf(" %s (%llu)",
8486 fnvlist_lookup_string(rec,
8488 (u_longlong_t)fnvlist_lookup_uint64(rec,
8491 (void) printf(" %s", fnvlist_lookup_string(rec,
8492 ZPOOL_HIST_INT_STR));
8493 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
8496 (void) printf("%s ioctl %s\n", tbuf,
8497 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
8498 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
8499 (void) printf(" input:\n");
8500 dump_nvlist(fnvlist_lookup_nvlist(rec,
8501 ZPOOL_HIST_INPUT_NVL), 8);
8503 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
8504 (void) printf(" output:\n");
8505 dump_nvlist(fnvlist_lookup_nvlist(rec,
8506 ZPOOL_HIST_OUTPUT_NVL), 8);
8508 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
8509 (void) printf(" errno: %lld\n",
8510 (longlong_t)fnvlist_lookup_int64(rec,
8516 (void) printf("%s unrecognized record:\n", tbuf);
8517 dump_nvlist(rec, 4);
8521 (void) printf("\n");
8524 (void) printf(" [");
8525 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
8526 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
8527 struct passwd *pwd = getpwuid(who);
8528 (void) printf("user %d ", (int)who);
8530 (void) printf("(%s) ", pwd->pw_name);
8532 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
8533 (void) printf("on %s",
8534 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
8536 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
8537 (void) printf(":%s",
8538 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
8542 (void) printf("\n");
8544 (void) printf("\n");
8551 * zpool history <pool>
8553 * Displays the history of commands that modified pools.
8556 zpool_do_history(int argc, char **argv)
8558 hist_cbdata_t cbdata = { 0 };
8562 cbdata.first = B_TRUE;
8564 while ((c = getopt(argc, argv, "li")) != -1) {
8567 cbdata.longfmt = B_TRUE;
8570 cbdata.internal = B_TRUE;
8573 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8581 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
8584 if (argc == 0 && cbdata.first == B_TRUE) {
8585 (void) fprintf(stderr, gettext("no pools available\n"));
8592 typedef struct ev_opts {
8597 char poolname[ZFS_MAX_DATASET_NAME_LEN];
8601 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
8603 char ctime_str[26], str[32], *ptr;
8607 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
8608 memset(str, ' ', 32);
8609 (void) ctime_r((const time_t *)&tv[0], ctime_str);
8610 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
8611 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
8612 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
8613 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
8615 (void) printf(gettext("%s\t"), str);
8617 (void) printf(gettext("%s "), str);
8619 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
8620 (void) printf(gettext("%s\n"), ptr);
8624 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
8628 for (nvp = nvlist_next_nvpair(nvl, NULL);
8629 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
8631 data_type_t type = nvpair_type(nvp);
8632 const char *name = nvpair_name(nvp);
8642 printf(gettext("%*s%s = "), depth, "", name);
8645 case DATA_TYPE_BOOLEAN:
8646 printf(gettext("%s"), "1");
8649 case DATA_TYPE_BOOLEAN_VALUE:
8650 (void) nvpair_value_boolean_value(nvp, &b);
8651 printf(gettext("%s"), b ? "1" : "0");
8654 case DATA_TYPE_BYTE:
8655 (void) nvpair_value_byte(nvp, &i8);
8656 printf(gettext("0x%x"), i8);
8659 case DATA_TYPE_INT8:
8660 (void) nvpair_value_int8(nvp, (void *)&i8);
8661 printf(gettext("0x%x"), i8);
8664 case DATA_TYPE_UINT8:
8665 (void) nvpair_value_uint8(nvp, &i8);
8666 printf(gettext("0x%x"), i8);
8669 case DATA_TYPE_INT16:
8670 (void) nvpair_value_int16(nvp, (void *)&i16);
8671 printf(gettext("0x%x"), i16);
8674 case DATA_TYPE_UINT16:
8675 (void) nvpair_value_uint16(nvp, &i16);
8676 printf(gettext("0x%x"), i16);
8679 case DATA_TYPE_INT32:
8680 (void) nvpair_value_int32(nvp, (void *)&i32);
8681 printf(gettext("0x%x"), i32);
8684 case DATA_TYPE_UINT32:
8685 (void) nvpair_value_uint32(nvp, &i32);
8686 printf(gettext("0x%x"), i32);
8689 case DATA_TYPE_INT64:
8690 (void) nvpair_value_int64(nvp, (void *)&i64);
8691 printf(gettext("0x%llx"), (u_longlong_t)i64);
8694 case DATA_TYPE_UINT64:
8695 (void) nvpair_value_uint64(nvp, &i64);
8697 * translate vdev state values to readable
8698 * strings to aide zpool events consumers
8701 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
8703 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
8704 printf(gettext("\"%s\" (0x%llx)"),
8705 zpool_state_to_name(i64, VDEV_AUX_NONE),
8708 printf(gettext("0x%llx"), (u_longlong_t)i64);
8712 case DATA_TYPE_HRTIME:
8713 (void) nvpair_value_hrtime(nvp, (void *)&i64);
8714 printf(gettext("0x%llx"), (u_longlong_t)i64);
8717 case DATA_TYPE_STRING:
8718 (void) nvpair_value_string(nvp, &str);
8719 printf(gettext("\"%s\""), str ? str : "<NULL>");
8722 case DATA_TYPE_NVLIST:
8723 printf(gettext("(embedded nvlist)\n"));
8724 (void) nvpair_value_nvlist(nvp, &cnv);
8725 zpool_do_events_nvprint(cnv, depth + 8);
8726 printf(gettext("%*s(end %s)"), depth, "", name);
8729 case DATA_TYPE_NVLIST_ARRAY: {
8733 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
8734 printf(gettext("(%d embedded nvlists)\n"), nelem);
8735 for (i = 0; i < nelem; i++) {
8736 printf(gettext("%*s%s[%d] = %s\n"),
8737 depth, "", name, i, "(embedded nvlist)");
8738 zpool_do_events_nvprint(val[i], depth + 8);
8739 printf(gettext("%*s(end %s[%i])\n"),
8740 depth, "", name, i);
8742 printf(gettext("%*s(end %s)\n"), depth, "", name);
8746 case DATA_TYPE_INT8_ARRAY: {
8750 (void) nvpair_value_int8_array(nvp, &val, &nelem);
8751 for (i = 0; i < nelem; i++)
8752 printf(gettext("0x%x "), val[i]);
8757 case DATA_TYPE_UINT8_ARRAY: {
8761 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
8762 for (i = 0; i < nelem; i++)
8763 printf(gettext("0x%x "), val[i]);
8768 case DATA_TYPE_INT16_ARRAY: {
8772 (void) nvpair_value_int16_array(nvp, &val, &nelem);
8773 for (i = 0; i < nelem; i++)
8774 printf(gettext("0x%x "), val[i]);
8779 case DATA_TYPE_UINT16_ARRAY: {
8783 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
8784 for (i = 0; i < nelem; i++)
8785 printf(gettext("0x%x "), val[i]);
8790 case DATA_TYPE_INT32_ARRAY: {
8794 (void) nvpair_value_int32_array(nvp, &val, &nelem);
8795 for (i = 0; i < nelem; i++)
8796 printf(gettext("0x%x "), val[i]);
8801 case DATA_TYPE_UINT32_ARRAY: {
8805 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
8806 for (i = 0; i < nelem; i++)
8807 printf(gettext("0x%x "), val[i]);
8812 case DATA_TYPE_INT64_ARRAY: {
8816 (void) nvpair_value_int64_array(nvp, &val, &nelem);
8817 for (i = 0; i < nelem; i++)
8818 printf(gettext("0x%llx "),
8819 (u_longlong_t)val[i]);
8824 case DATA_TYPE_UINT64_ARRAY: {
8828 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
8829 for (i = 0; i < nelem; i++)
8830 printf(gettext("0x%llx "),
8831 (u_longlong_t)val[i]);
8836 case DATA_TYPE_STRING_ARRAY: {
8840 (void) nvpair_value_string_array(nvp, &str, &nelem);
8841 for (i = 0; i < nelem; i++)
8842 printf(gettext("\"%s\" "),
8843 str[i] ? str[i] : "<NULL>");
8848 case DATA_TYPE_BOOLEAN_ARRAY:
8849 case DATA_TYPE_BYTE_ARRAY:
8850 case DATA_TYPE_DOUBLE:
8851 case DATA_TYPE_DONTCARE:
8852 case DATA_TYPE_UNKNOWN:
8853 printf(gettext("<unknown>"));
8857 printf(gettext("\n"));
8862 zpool_do_events_next(ev_opts_t *opts)
8865 int zevent_fd, ret, dropped;
8868 zevent_fd = open(ZFS_DEV, O_RDWR);
8869 VERIFY(zevent_fd >= 0);
8871 if (!opts->scripted)
8872 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
8875 ret = zpool_events_next(g_zfs, &nvl, &dropped,
8876 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
8877 if (ret || nvl == NULL)
8881 (void) printf(gettext("dropped %d events\n"), dropped);
8883 if (strlen(opts->poolname) > 0 &&
8884 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
8885 strcmp(opts->poolname, pool) != 0)
8888 zpool_do_events_short(nvl, opts);
8890 if (opts->verbose) {
8891 zpool_do_events_nvprint(nvl, 8);
8892 printf(gettext("\n"));
8894 (void) fflush(stdout);
8899 VERIFY(0 == close(zevent_fd));
8905 zpool_do_events_clear(ev_opts_t *opts)
8909 ret = zpool_events_clear(g_zfs, &count);
8911 (void) printf(gettext("cleared %d events\n"), count);
8917 * zpool events [-vHf [pool] | -c]
8919 * Displays events logs by ZFS.
8922 zpool_do_events(int argc, char **argv)
8924 ev_opts_t opts = { 0 };
8929 while ((c = getopt(argc, argv, "vHfc")) != -1) {
8944 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8953 (void) fprintf(stderr, gettext("too many arguments\n"));
8955 } else if (argc == 1) {
8956 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
8957 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
8958 (void) fprintf(stderr,
8959 gettext("invalid pool name '%s'\n"), opts.poolname);
8964 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
8966 (void) fprintf(stderr,
8967 gettext("invalid options combined with -c\n"));
8972 ret = zpool_do_events_clear(&opts);
8974 ret = zpool_do_events_next(&opts);
8980 get_callback(zpool_handle_t *zhp, void *data)
8982 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
8983 char value[MAXNAMELEN];
8984 zprop_source_t srctype;
8987 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8990 * Skip the special fake placeholder. This will also skip
8991 * over the name property when 'all' is specified.
8993 if (pl->pl_prop == ZPOOL_PROP_NAME &&
8994 pl == cbp->cb_proplist)
8997 if (pl->pl_prop == ZPROP_INVAL &&
8998 (zpool_prop_feature(pl->pl_user_prop) ||
8999 zpool_prop_unsupported(pl->pl_user_prop))) {
9000 srctype = ZPROP_SRC_LOCAL;
9002 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
9003 value, sizeof (value)) == 0) {
9004 zprop_print_one_property(zpool_get_name(zhp),
9005 cbp, pl->pl_user_prop, value, srctype,
9009 if (zpool_get_prop(zhp, pl->pl_prop, value,
9010 sizeof (value), &srctype, cbp->cb_literal) != 0)
9013 zprop_print_one_property(zpool_get_name(zhp), cbp,
9014 zpool_prop_to_name(pl->pl_prop), value, srctype,
9022 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9024 * -H Scripted mode. Don't display headers, and separate properties
9026 * -o List of columns to display. Defaults to
9027 * "name,property,value,source".
9028 * -p Display values in parsable (exact) format.
9030 * Get properties of pools in the system. Output space statistics
9031 * for each one as well as other attributes.
9034 zpool_do_get(int argc, char **argv)
9036 zprop_get_cbdata_t cb = { 0 };
9037 zprop_list_t fake_name = { 0 };
9042 cb.cb_first = B_TRUE;
9045 * Set up default columns and sources.
9047 cb.cb_sources = ZPROP_SRC_ALL;
9048 cb.cb_columns[0] = GET_COL_NAME;
9049 cb.cb_columns[1] = GET_COL_PROPERTY;
9050 cb.cb_columns[2] = GET_COL_VALUE;
9051 cb.cb_columns[3] = GET_COL_SOURCE;
9052 cb.cb_type = ZFS_TYPE_POOL;
9055 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
9058 cb.cb_literal = B_TRUE;
9061 cb.cb_scripted = B_TRUE;
9064 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
9066 while (*optarg != '\0') {
9067 static char *col_subopts[] =
9068 { "name", "property", "value", "source",
9071 if (i == ZFS_GET_NCOLS) {
9072 (void) fprintf(stderr, gettext("too "
9073 "many fields given to -o "
9078 switch (getsubopt(&optarg, col_subopts,
9081 cb.cb_columns[i++] = GET_COL_NAME;
9084 cb.cb_columns[i++] = GET_COL_PROPERTY;
9087 cb.cb_columns[i++] = GET_COL_VALUE;
9090 cb.cb_columns[i++] = GET_COL_SOURCE;
9094 (void) fprintf(stderr,
9095 gettext("\"all\" conflicts "
9096 "with specific fields "
9097 "given to -o option\n"));
9100 cb.cb_columns[0] = GET_COL_NAME;
9101 cb.cb_columns[1] = GET_COL_PROPERTY;
9102 cb.cb_columns[2] = GET_COL_VALUE;
9103 cb.cb_columns[3] = GET_COL_SOURCE;
9107 (void) fprintf(stderr,
9108 gettext("invalid column name "
9115 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9125 (void) fprintf(stderr, gettext("missing property "
9130 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
9131 ZFS_TYPE_POOL) != 0)
9137 if (cb.cb_proplist != NULL) {
9138 fake_name.pl_prop = ZPOOL_PROP_NAME;
9139 fake_name.pl_width = strlen(gettext("NAME"));
9140 fake_name.pl_next = cb.cb_proplist;
9141 cb.cb_proplist = &fake_name;
9144 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
9147 if (cb.cb_proplist == &fake_name)
9148 zprop_free_list(fake_name.pl_next);
9150 zprop_free_list(cb.cb_proplist);
9155 typedef struct set_cbdata {
9158 boolean_t cb_any_successful;
9162 set_callback(zpool_handle_t *zhp, void *data)
9165 set_cbdata_t *cb = (set_cbdata_t *)data;
9167 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
9170 cb->cb_any_successful = B_TRUE;
9176 zpool_do_set(int argc, char **argv)
9178 set_cbdata_t cb = { 0 };
9181 if (argc > 1 && argv[1][0] == '-') {
9182 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9188 (void) fprintf(stderr, gettext("missing property=value "
9194 (void) fprintf(stderr, gettext("missing pool name\n"));
9199 (void) fprintf(stderr, gettext("too many pool names\n"));
9203 cb.cb_propname = argv[1];
9204 cb.cb_value = strchr(cb.cb_propname, '=');
9205 if (cb.cb_value == NULL) {
9206 (void) fprintf(stderr, gettext("missing value in "
9207 "property=value argument\n"));
9211 *(cb.cb_value) = '\0';
9214 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
9221 find_command_idx(char *command, int *idx)
9225 for (i = 0; i < NCOMMAND; i++) {
9226 if (command_table[i].name == NULL)
9229 if (strcmp(command, command_table[i].name) == 0) {
9238 * Display version message
9241 zpool_do_version(int argc, char **argv)
9243 if (zfs_version_print() == -1)
9250 main(int argc, char **argv)
9257 (void) setlocale(LC_ALL, "");
9258 (void) textdomain(TEXT_DOMAIN);
9264 * Make sure the user has specified some command.
9267 (void) fprintf(stderr, gettext("missing command\n"));
9276 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
9280 * Special case '-V|--version'
9282 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
9283 return (zpool_do_version(argc, argv));
9285 if ((g_zfs = libzfs_init()) == NULL) {
9286 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
9290 libzfs_print_on_error(g_zfs, B_TRUE);
9292 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
9295 * Many commands modify input strings for string parsing reasons.
9296 * We create a copy to protect the original argv.
9298 newargv = malloc((argc + 1) * sizeof (newargv[0]));
9299 for (i = 0; i < argc; i++)
9300 newargv[i] = strdup(argv[i]);
9301 newargv[argc] = NULL;
9304 * Run the appropriate command.
9306 if (find_command_idx(cmdname, &i) == 0) {
9307 current_command = &command_table[i];
9308 ret = command_table[i].func(argc - 1, newargv + 1);
9309 } else if (strchr(cmdname, '=')) {
9310 verify(find_command_idx("set", &i) == 0);
9311 current_command = &command_table[i];
9312 ret = command_table[i].func(argc, newargv);
9313 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
9315 * 'freeze' is a vile debugging abomination, so we treat
9318 zfs_cmd_t zc = {"\0"};
9320 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
9321 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
9323 (void) fprintf(stderr,
9324 gettext("failed to freeze pool: %d\n"), errno);
9330 (void) fprintf(stderr, gettext("unrecognized "
9331 "command '%s'\n"), cmdname);
9336 for (i = 0; i < argc; i++)
9340 if (ret == 0 && log_history)
9341 (void) zpool_log_history(g_zfs, history_str);
9346 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
9347 * for the purposes of running ::findleaks.
9349 if (getenv("ZFS_ABORT") != NULL) {
9350 (void) printf("dumping core by request\n");