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_scrub(int, char **);
101 static int zpool_do_resilver(int, char **);
103 static int zpool_do_import(int, char **);
104 static int zpool_do_export(int, char **);
106 static int zpool_do_upgrade(int, char **);
108 static int zpool_do_history(int, char **);
109 static int zpool_do_events(int, char **);
111 static int zpool_do_get(int, char **);
112 static int zpool_do_set(int, char **);
114 static int zpool_do_sync(int, char **);
117 * These libumem hooks provide a reasonable set of defaults for the allocator's
118 * debugging facilities.
123 _umem_debug_init(void)
125 return ("default,verbose"); /* $UMEM_DEBUG setting */
129 _umem_logging_init(void)
131 return ("fail,contents"); /* $UMEM_LOGGING setting */
168 * Flags for stats to display with "zpool iostats"
176 IOS_COUNT
, /* always last element */
179 /* iostat_type entries as bitmasks */
180 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
181 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
182 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
183 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
184 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
186 /* Mask of all the histo bits */
187 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
190 * Lookup table for iostat flags to nvlist names. Basically a list
191 * of all the nvlists a flag requires. Also specifies the order in
192 * which data gets printed in zpool iostat.
194 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
196 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
197 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
198 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
199 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
200 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
201 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
202 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
203 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
204 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
207 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
208 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
209 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
210 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
213 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
214 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
215 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
216 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
217 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
220 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
221 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
222 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
223 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
224 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
225 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
226 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
227 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
228 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
229 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
235 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
236 * Right now, only one histo bit is ever set at one time, so we can
237 * just do a highbit64(a)
239 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
241 typedef struct zpool_command
{
243 int (*func
)(int, char **);
248 * Master command table. Each ZFS command has a name, associated function, and
249 * usage message. The usage messages need to be internationalized, so we have
250 * to have a function to return the usage message based on a command index.
252 * These commands are organized according to how they are displayed in the usage
253 * message. An empty command (one with a NULL name) indicates an empty line in
254 * the generic usage message.
256 static zpool_command_t command_table
[] = {
257 { "create", zpool_do_create
, HELP_CREATE
},
258 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
260 { "add", zpool_do_add
, HELP_ADD
},
261 { "remove", zpool_do_remove
, HELP_REMOVE
},
263 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
265 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
267 { "list", zpool_do_list
, HELP_LIST
},
268 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
269 { "status", zpool_do_status
, HELP_STATUS
},
271 { "online", zpool_do_online
, HELP_ONLINE
},
272 { "offline", zpool_do_offline
, HELP_OFFLINE
},
273 { "clear", zpool_do_clear
, HELP_CLEAR
},
274 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
276 { "attach", zpool_do_attach
, HELP_ATTACH
},
277 { "detach", zpool_do_detach
, HELP_DETACH
},
278 { "replace", zpool_do_replace
, HELP_REPLACE
},
279 { "split", zpool_do_split
, HELP_SPLIT
},
281 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
282 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
284 { "import", zpool_do_import
, HELP_IMPORT
},
285 { "export", zpool_do_export
, HELP_EXPORT
},
286 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
287 { "reguid", zpool_do_reguid
, HELP_REGUID
},
289 { "history", zpool_do_history
, HELP_HISTORY
},
290 { "events", zpool_do_events
, HELP_EVENTS
},
292 { "get", zpool_do_get
, HELP_GET
},
293 { "set", zpool_do_set
, HELP_SET
},
294 { "sync", zpool_do_sync
, HELP_SYNC
},
297 #define NCOMMAND (ARRAY_SIZE(command_table))
299 #define VDEV_ALLOC_CLASS_LOGS "logs"
301 static zpool_command_t
*current_command
;
302 static char history_str
[HIS_MAX_RECORD_LEN
];
303 static boolean_t log_history
= B_TRUE
;
304 static uint_t timestamp_fmt
= NODATE
;
307 get_usage(zpool_help_t idx
)
311 return (gettext("\tadd [-fgLnP] [-o property=value] "
312 "<pool> <vdev> ...\n"));
314 return (gettext("\tattach [-f] [-o property=value] "
315 "<pool> <device> <new-device>\n"));
317 return (gettext("\tclear [-nF] <pool> [device]\n"));
319 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
320 "\t [-O file-system-property=value] ... \n"
321 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
322 case HELP_CHECKPOINT
:
323 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
325 return (gettext("\tdestroy [-f] <pool>\n"));
327 return (gettext("\tdetach <pool> <device>\n"));
329 return (gettext("\texport [-af] <pool> ...\n"));
331 return (gettext("\thistory [-il] [<pool>] ...\n"));
333 return (gettext("\timport [-d dir] [-D]\n"
334 "\timport [-o mntopts] [-o property=value] ... \n"
335 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
336 "[-R root] [-F [-n]] -a\n"
337 "\timport [-o mntopts] [-o property=value] ... \n"
338 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
339 "[-R root] [-F [-n]]\n"
340 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
342 return (gettext("\tiostat [[[-c [script1,script2,...]"
343 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
344 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
345 " [interval [count]]\n"));
346 case HELP_LABELCLEAR
:
347 return (gettext("\tlabelclear [-f] <vdev>\n"));
349 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
350 "[-T d|u] [pool] ... \n"
351 "\t [interval [count]]\n"));
353 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
355 return (gettext("\tonline [-e] <pool> <device> ...\n"));
357 return (gettext("\treplace [-f] [-o property=value] "
358 "<pool> <device> [new-device]\n"));
360 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
362 return (gettext("\treopen [-n] <pool>\n"));
364 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
366 return (gettext("\tresilver <pool> ...\n"));
368 return (gettext("\tstatus [-c [script1,script2,...]] "
369 "[-gLpPsvxD] [-T d|u] [pool] ... \n"
370 "\t [interval [count]]\n"));
372 return (gettext("\tupgrade\n"
374 "\tupgrade [-V version] <-a | pool ...>\n"));
376 return (gettext("\tevents [-vHf [pool] | -c]\n"));
378 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
379 "<\"all\" | property[,...]> <pool> ...\n"));
381 return (gettext("\tset <property=value> <pool> \n"));
383 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
384 "\t [-o property=value] <pool> <newpool> "
385 "[<device> ...]\n"));
387 return (gettext("\treguid <pool>\n"));
389 return (gettext("\tsync [pool] ...\n"));
398 * Callback routine that will print out a pool property value.
401 print_prop_cb(int prop
, void *cb
)
405 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
407 if (zpool_prop_readonly(prop
))
408 (void) fprintf(fp
, " NO ");
410 (void) fprintf(fp
, " YES ");
412 if (zpool_prop_values(prop
) == NULL
)
413 (void) fprintf(fp
, "-\n");
415 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
421 * Display usage message. If we're inside a command, display only the usage for
422 * that command. Otherwise, iterate over the entire command table and display
423 * a complete usage message.
426 usage(boolean_t requested
)
428 FILE *fp
= requested
? stdout
: stderr
;
430 if (current_command
== NULL
) {
433 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
435 gettext("where 'command' is one of the following:\n\n"));
437 for (i
= 0; i
< NCOMMAND
; i
++) {
438 if (command_table
[i
].name
== NULL
)
439 (void) fprintf(fp
, "\n");
441 (void) fprintf(fp
, "%s",
442 get_usage(command_table
[i
].usage
));
445 (void) fprintf(fp
, gettext("usage:\n"));
446 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
449 if (current_command
!= NULL
&&
450 ((strcmp(current_command
->name
, "set") == 0) ||
451 (strcmp(current_command
->name
, "get") == 0) ||
452 (strcmp(current_command
->name
, "list") == 0))) {
455 gettext("\nthe following properties are supported:\n"));
457 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
458 "PROPERTY", "EDIT", "VALUES");
460 /* Iterate over all properties */
461 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
464 (void) fprintf(fp
, "\t%-19s ", "feature@...");
465 (void) fprintf(fp
, "YES disabled | enabled | active\n");
467 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
468 "appended with a feature name.\nSee zpool-features(5).\n"));
472 * See comments at end of main().
474 if (getenv("ZFS_ABORT") != NULL
) {
475 (void) printf("dumping core by request\n");
479 exit(requested
? 0 : 2);
483 * print a pool vdev config for dry runs
486 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
487 const char *match
, int name_flags
)
492 boolean_t printed
= B_FALSE
;
494 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
495 &child
, &children
) != 0) {
497 (void) printf("\t%*s%s\n", indent
, "", name
);
501 for (c
= 0; c
< children
; c
++) {
502 uint64_t is_log
= B_FALSE
;
505 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
508 class = VDEV_ALLOC_BIAS_LOG
;
509 (void) nvlist_lookup_string(child
[c
],
510 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
511 if (strcmp(match
, class) != 0)
514 if (!printed
&& name
!= NULL
) {
515 (void) printf("\t%*s%s\n", indent
, "", name
);
518 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
519 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
526 prop_list_contains_feature(nvlist_t
*proplist
)
529 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
530 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
531 if (zpool_prop_feature(nvpair_name(nvp
)))
538 * Add a property pair (name, string-value) into a property nvlist.
541 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
544 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
549 if (*props
== NULL
&&
550 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
551 (void) fprintf(stderr
,
552 gettext("internal error: out of memory\n"));
559 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
561 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
562 !zpool_prop_feature(propname
)) {
563 (void) fprintf(stderr
, gettext("property '%s' is "
564 "not a valid pool property\n"), propname
);
569 * feature@ properties and version should not be specified
572 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
573 nvlist_exists(proplist
, vname
)) ||
574 (prop
== ZPOOL_PROP_VERSION
&&
575 prop_list_contains_feature(proplist
))) {
576 (void) fprintf(stderr
, gettext("'feature@' and "
577 "'version' properties cannot be specified "
583 if (zpool_prop_feature(propname
))
586 normnm
= zpool_prop_to_name(prop
);
588 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
590 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
592 normnm
= zfs_prop_to_name(fsprop
);
593 } else if (zfs_prop_user(propname
) ||
594 zfs_prop_userquota(propname
)) {
597 (void) fprintf(stderr
, gettext("property '%s' is "
598 "not a valid filesystem property\n"), propname
);
603 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
604 prop
!= ZPOOL_PROP_CACHEFILE
) {
605 (void) fprintf(stderr
, gettext("property '%s' "
606 "specified multiple times\n"), propname
);
610 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
611 (void) fprintf(stderr
, gettext("internal "
612 "error: out of memory\n"));
620 * Set a default property pair (name, string-value) in a property nvlist
623 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
628 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
631 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
635 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
637 * -f Force addition of devices, even if they appear in use
638 * -g Display guid for individual vdev name.
639 * -L Follow links when resolving vdev path name.
640 * -n Do not add the devices, but display the resulting layout if
641 * they were to be added.
642 * -o Set property=value.
643 * -P Display full path for vdev name.
645 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
646 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
650 zpool_do_add(int argc
, char **argv
)
652 boolean_t force
= B_FALSE
;
653 boolean_t dryrun
= B_FALSE
;
661 nvlist_t
*props
= NULL
;
665 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
671 name_flags
|= VDEV_NAME_GUID
;
674 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
680 if ((propval
= strchr(optarg
, '=')) == NULL
) {
681 (void) fprintf(stderr
, gettext("missing "
682 "'=' for -o option\n"));
688 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
689 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
693 name_flags
|= VDEV_NAME_PATH
;
696 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
705 /* get pool name and check number of arguments */
707 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
711 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
720 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
723 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
724 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
730 /* unless manually specified use "ashift" pool property (if set) */
731 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
734 char strval
[ZPOOL_MAXPROPLEN
];
736 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
737 if (src
!= ZPROP_SRC_DEFAULT
) {
738 (void) sprintf(strval
, "%" PRId32
, intval
);
739 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
740 &props
, B_TRUE
) == 0);
744 /* pass off to get_vdev_spec for processing */
745 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
747 if (nvroot
== NULL
) {
753 nvlist_t
*poolnvroot
;
755 uint_t l2children
, c
;
757 boolean_t hadcache
= B_FALSE
;
759 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
762 (void) printf(gettext("would update '%s' to the following "
763 "configuration:\n"), zpool_get_name(zhp
));
765 /* print original main pool and new tree */
766 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
767 name_flags
| VDEV_NAME_TYPE_ID
);
768 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
770 /* print other classes: 'dedup', 'special', and 'log' */
771 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
772 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
773 print_vdev_tree(zhp
, NULL
, nvroot
, 0, VDEV_ALLOC_BIAS_DEDUP
,
776 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
777 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
778 print_vdev_tree(zhp
, NULL
, nvroot
, 0, VDEV_ALLOC_BIAS_SPECIAL
,
781 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, VDEV_ALLOC_BIAS_LOG
,
783 print_vdev_tree(zhp
, NULL
, nvroot
, 0, VDEV_ALLOC_BIAS_LOG
,
786 /* Do the same for the caches */
787 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
788 &l2child
, &l2children
) == 0 && l2children
) {
790 (void) printf(gettext("\tcache\n"));
791 for (c
= 0; c
< l2children
; c
++) {
792 vname
= zpool_vdev_name(g_zfs
, NULL
,
793 l2child
[c
], name_flags
);
794 (void) printf("\t %s\n", vname
);
798 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
799 &l2child
, &l2children
) == 0 && l2children
) {
801 (void) printf(gettext("\tcache\n"));
802 for (c
= 0; c
< l2children
; c
++) {
803 vname
= zpool_vdev_name(g_zfs
, NULL
,
804 l2child
[c
], name_flags
);
805 (void) printf("\t %s\n", vname
);
812 ret
= (zpool_add(zhp
, nvroot
) != 0);
823 * zpool remove <pool> <vdev> ...
825 * Removes the given vdev from the pool.
828 zpool_do_remove(int argc
, char **argv
)
832 zpool_handle_t
*zhp
= NULL
;
833 boolean_t stop
= B_FALSE
;
835 boolean_t noop
= B_FALSE
;
836 boolean_t parsable
= B_FALSE
;
839 while ((c
= getopt(argc
, argv
, "nps")) != -1) {
851 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
860 /* get pool name and check number of arguments */
862 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
868 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
872 (void) fprintf(stderr
, gettext("stop request ignored\n"));
878 (void) fprintf(stderr
, gettext("too many arguments\n"));
881 if (zpool_vdev_remove_cancel(zhp
) != 0)
885 (void) fprintf(stderr
, gettext("missing device\n"));
889 for (i
= 1; i
< argc
; i
++) {
893 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
899 (void) printf("%s %llu\n",
900 argv
[i
], (unsigned long long)size
);
903 zfs_nicenum(size
, valstr
,
905 (void) printf("Memory that will be "
906 "used after removing %s: %s\n",
910 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
921 * zpool labelclear [-f] <vdev>
923 * -f Force clearing the label for the vdevs which are members of
924 * the exported or foreign pools.
926 * Verifies that the vdev is not active and zeros out the label information
930 zpool_do_labelclear(int argc
, char **argv
)
932 char vdev
[MAXPATHLEN
];
935 int c
, fd
= -1, ret
= 0;
938 boolean_t inuse
= B_FALSE
;
939 boolean_t force
= B_FALSE
;
942 while ((c
= getopt(argc
, argv
, "f")) != -1) {
948 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
959 (void) fprintf(stderr
, gettext("missing vdev name\n"));
963 (void) fprintf(stderr
, gettext("too many arguments\n"));
968 * Check if we were given absolute path and use it as is.
969 * Otherwise if the provided vdev name doesn't point to a file,
970 * try prepending expected disk paths and partition numbers.
972 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
973 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
976 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
977 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
978 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
982 if (error
|| (stat(vdev
, &st
) != 0)) {
983 (void) fprintf(stderr
, gettext(
984 "failed to find device %s, try specifying absolute "
985 "path instead\n"), argv
[0]);
990 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
991 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
992 vdev
, strerror(errno
));
996 if (ioctl(fd
, BLKFLSBUF
) != 0)
997 (void) fprintf(stderr
, gettext("failed to invalidate "
998 "cache for %s: %s\n"), vdev
, strerror(errno
));
1000 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
1001 (void) fprintf(stderr
,
1002 gettext("failed to check state for %s\n"), vdev
);
1006 nvlist_free(config
);
1008 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1010 (void) fprintf(stderr
,
1011 gettext("failed to check state for %s\n"), vdev
);
1021 case POOL_STATE_ACTIVE
:
1022 case POOL_STATE_SPARE
:
1023 case POOL_STATE_L2CACHE
:
1024 (void) fprintf(stderr
, gettext(
1025 "%s is a member (%s) of pool \"%s\"\n"),
1026 vdev
, zpool_pool_state_to_name(state
), name
);
1030 case POOL_STATE_EXPORTED
:
1033 (void) fprintf(stderr
, gettext(
1034 "use '-f' to override the following error:\n"
1035 "%s is a member of exported pool \"%s\"\n"),
1040 case POOL_STATE_POTENTIALLY_ACTIVE
:
1043 (void) fprintf(stderr
, gettext(
1044 "use '-f' to override the following error:\n"
1045 "%s is a member of potentially active pool \"%s\"\n"),
1050 case POOL_STATE_DESTROYED
:
1051 /* inuse should never be set for a destroyed pool */
1057 ret
= zpool_clear_label(fd
);
1059 (void) fprintf(stderr
,
1060 gettext("failed to clear label for %s\n"), vdev
);
1071 * zpool create [-fnd] [-o property=value] ...
1072 * [-O file-system-property=value] ...
1073 * [-R root] [-m mountpoint] <pool> <dev> ...
1075 * -f Force creation, even if devices appear in use
1076 * -n Do not create the pool, but display the resulting layout if it
1077 * were to be created.
1078 * -R Create a pool under an alternate root
1079 * -m Set default mountpoint for the root dataset. By default it's
1081 * -o Set property=value.
1082 * -o Set feature@feature=enabled|disabled.
1083 * -d Don't automatically enable all supported pool features
1084 * (individual features can be enabled with -o).
1085 * -O Set fsproperty=value in the pool's root file system
1087 * Creates the named pool according to the given vdev specification. The
1088 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1089 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1090 * (if '-n' was specified), or pass it to libzfs to do the creation.
1093 zpool_do_create(int argc
, char **argv
)
1095 boolean_t force
= B_FALSE
;
1096 boolean_t dryrun
= B_FALSE
;
1097 boolean_t enable_all_pool_feat
= B_TRUE
;
1099 nvlist_t
*nvroot
= NULL
;
1103 char *altroot
= NULL
;
1104 char *mountpoint
= NULL
;
1105 nvlist_t
*fsprops
= NULL
;
1106 nvlist_t
*props
= NULL
;
1110 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1119 enable_all_pool_feat
= B_FALSE
;
1123 if (add_prop_list(zpool_prop_to_name(
1124 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1126 if (add_prop_list_default(zpool_prop_to_name(
1127 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1131 /* Equivalent to -O mountpoint=optarg */
1132 mountpoint
= optarg
;
1135 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1136 (void) fprintf(stderr
, gettext("missing "
1137 "'=' for -o option\n"));
1143 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1147 * If the user is creating a pool that doesn't support
1148 * feature flags, don't enable any features.
1150 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1154 ver
= strtoull(propval
, &end
, 10);
1156 ver
< SPA_VERSION_FEATURES
) {
1157 enable_all_pool_feat
= B_FALSE
;
1160 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1164 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1165 (void) fprintf(stderr
, gettext("missing "
1166 "'=' for -O option\n"));
1173 * Mountpoints are checked and then added later.
1174 * Uniquely among properties, they can be specified
1175 * more than once, to avoid conflict with -m.
1177 if (0 == strcmp(optarg
,
1178 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1179 mountpoint
= propval
;
1180 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1187 * Sanity check temporary pool name.
1189 if (strchr(optarg
, '/') != NULL
) {
1190 (void) fprintf(stderr
, gettext("cannot create "
1191 "'%s': invalid character '/' in temporary "
1193 (void) fprintf(stderr
, gettext("use 'zfs "
1194 "create' to create a dataset\n"));
1198 if (add_prop_list(zpool_prop_to_name(
1199 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1201 if (add_prop_list_default(zpool_prop_to_name(
1202 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1207 (void) fprintf(stderr
, gettext("missing argument for "
1208 "'%c' option\n"), optopt
);
1211 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1220 /* get pool name and check number of arguments */
1222 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1226 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1233 * As a special case, check for use of '/' in the name, and direct the
1234 * user to use 'zfs create' instead.
1236 if (strchr(poolname
, '/') != NULL
) {
1237 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1238 "character '/' in pool name\n"), poolname
);
1239 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1240 "create a dataset\n"));
1244 /* pass off to get_vdev_spec for bulk processing */
1245 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1246 argc
- 1, argv
+ 1);
1250 /* make_root_vdev() allows 0 toplevel children if there are spares */
1251 if (!zfs_allocatable_devs(nvroot
)) {
1252 (void) fprintf(stderr
, gettext("invalid vdev "
1253 "specification: at least one toplevel vdev must be "
1258 if (altroot
!= NULL
&& altroot
[0] != '/') {
1259 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1260 "must be an absolute path\n"), altroot
);
1265 * Check the validity of the mountpoint and direct the user to use the
1266 * '-m' mountpoint option if it looks like its in use.
1268 if (mountpoint
== NULL
||
1269 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1270 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1271 char buf
[MAXPATHLEN
];
1274 if (mountpoint
&& mountpoint
[0] != '/') {
1275 (void) fprintf(stderr
, gettext("invalid mountpoint "
1276 "'%s': must be an absolute path, 'legacy', or "
1277 "'none'\n"), mountpoint
);
1281 if (mountpoint
== NULL
) {
1282 if (altroot
!= NULL
)
1283 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1286 (void) snprintf(buf
, sizeof (buf
), "/%s",
1289 if (altroot
!= NULL
)
1290 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1291 altroot
, mountpoint
);
1293 (void) snprintf(buf
, sizeof (buf
), "%s",
1297 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1298 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1299 "%s\n"), buf
, strerror(errno
));
1300 (void) fprintf(stderr
, gettext("use '-m' "
1301 "option to provide a different default\n"));
1306 while (count
< 3 && readdir(dirp
) != NULL
)
1308 (void) closedir(dirp
);
1311 (void) fprintf(stderr
, gettext("mountpoint "
1312 "'%s' exists and is not empty\n"), buf
);
1313 (void) fprintf(stderr
, gettext("use '-m' "
1314 "option to provide a "
1315 "different default\n"));
1322 * Now that the mountpoint's validity has been checked, ensure that
1323 * the property is set appropriately prior to creating the pool.
1325 if (mountpoint
!= NULL
) {
1326 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1327 mountpoint
, &fsprops
, B_FALSE
);
1335 * For a dry run invocation, print out a basic message and run
1336 * through all the vdevs in the list and print out in an
1337 * appropriate hierarchy.
1339 (void) printf(gettext("would create '%s' with the "
1340 "following layout:\n\n"), poolname
);
1342 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1343 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1344 VDEV_ALLOC_BIAS_DEDUP
, 0);
1345 print_vdev_tree(NULL
, "special", nvroot
, 0,
1346 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1347 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1348 VDEV_ALLOC_BIAS_LOG
, 0);
1353 * Hand off to libzfs.
1356 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1357 char propname
[MAXPATHLEN
];
1359 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1361 (void) snprintf(propname
, sizeof (propname
),
1362 "feature@%s", feat
->fi_uname
);
1365 * Only features contained in props will be enabled:
1366 * remove from the nvlist every ZFS_FEATURE_DISABLED
1367 * value and add every missing ZFS_FEATURE_ENABLED if
1368 * enable_all_pool_feat is set.
1370 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1371 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1372 (void) nvlist_remove_all(props
,
1374 } else if (enable_all_pool_feat
) {
1375 ret
= add_prop_list(propname
,
1376 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1383 if (zpool_create(g_zfs
, poolname
,
1384 nvroot
, props
, fsprops
) == 0) {
1385 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1386 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1388 if (zfs_mount(pool
, NULL
, 0) == 0)
1389 ret
= zfs_shareall(pool
);
1392 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1393 (void) fprintf(stderr
, gettext("pool name may have "
1399 nvlist_free(nvroot
);
1400 nvlist_free(fsprops
);
1404 nvlist_free(fsprops
);
1411 * zpool destroy <pool>
1413 * -f Forcefully unmount any datasets
1415 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1418 zpool_do_destroy(int argc
, char **argv
)
1420 boolean_t force
= B_FALSE
;
1423 zpool_handle_t
*zhp
;
1427 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1433 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1442 /* check arguments */
1444 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1448 (void) fprintf(stderr
, gettext("too many arguments\n"));
1454 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1456 * As a special case, check for use of '/' in the name, and
1457 * direct the user to use 'zfs destroy' instead.
1459 if (strchr(pool
, '/') != NULL
)
1460 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1461 "destroy a dataset\n"));
1465 if (zpool_disable_datasets(zhp
, force
) != 0) {
1466 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1467 "could not unmount datasets\n"), zpool_get_name(zhp
));
1472 /* The history must be logged as part of the export */
1473 log_history
= B_FALSE
;
1475 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1482 typedef struct export_cbdata
{
1484 boolean_t hardforce
;
1491 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1493 export_cbdata_t
*cb
= data
;
1495 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1498 /* The history must be logged as part of the export */
1499 log_history
= B_FALSE
;
1501 if (cb
->hardforce
) {
1502 if (zpool_export_force(zhp
, history_str
) != 0)
1504 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1512 * zpool export [-f] <pool> ...
1514 * -a Export all pools
1515 * -f Forcefully unmount datasets
1517 * Export the given pools. By default, the command will attempt to cleanly
1518 * unmount any active datasets within the pool. If the '-f' flag is specified,
1519 * then the datasets will be forcefully unmounted.
1522 zpool_do_export(int argc
, char **argv
)
1525 boolean_t do_all
= B_FALSE
;
1526 boolean_t force
= B_FALSE
;
1527 boolean_t hardforce
= B_FALSE
;
1531 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1543 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1550 cb
.hardforce
= hardforce
;
1556 (void) fprintf(stderr
, gettext("too many arguments\n"));
1560 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1561 zpool_export_one
, &cb
));
1564 /* check arguments */
1566 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1570 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1576 * Given a vdev configuration, determine the maximum width needed for the device
1580 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1588 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1589 if (strlen(name
) + depth
> max
)
1590 max
= strlen(name
) + depth
;
1594 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1595 &child
, &children
) == 0) {
1596 for (c
= 0; c
< children
; c
++)
1597 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1598 max
, name_flags
)) > max
)
1602 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1603 &child
, &children
) == 0) {
1604 for (c
= 0; c
< children
; c
++)
1605 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1606 max
, name_flags
)) > max
)
1610 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1611 &child
, &children
) == 0) {
1612 for (c
= 0; c
< children
; c
++)
1613 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1614 max
, name_flags
)) > max
)
1621 typedef struct spare_cbdata
{
1623 zpool_handle_t
*cb_zhp
;
1627 find_vdev(nvlist_t
*nv
, uint64_t search
)
1633 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1637 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1638 &child
, &children
) == 0) {
1639 for (c
= 0; c
< children
; c
++)
1640 if (find_vdev(child
[c
], search
))
1648 find_spare(zpool_handle_t
*zhp
, void *data
)
1650 spare_cbdata_t
*cbp
= data
;
1651 nvlist_t
*config
, *nvroot
;
1653 config
= zpool_get_config(zhp
, NULL
);
1654 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1657 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1666 typedef struct status_cbdata
{
1670 boolean_t cb_allpools
;
1671 boolean_t cb_verbose
;
1672 boolean_t cb_literal
;
1673 boolean_t cb_explain
;
1675 boolean_t cb_dedup_stats
;
1676 boolean_t cb_print_status
;
1677 boolean_t cb_print_slow_ios
;
1678 vdev_cmd_data_list_t
*vcdl
;
1681 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1683 is_blank_str(char *str
)
1685 while (str
!= NULL
&& *str
!= '\0') {
1693 /* Print command output lines for specific vdev in a specific pool */
1695 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1697 vdev_cmd_data_t
*data
;
1701 for (i
= 0; i
< vcdl
->count
; i
++) {
1702 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1703 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1704 /* Not the vdev we're looking for */
1708 data
= &vcdl
->data
[i
];
1709 /* Print out all the output values for this vdev */
1710 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1712 /* Does this vdev have values for this column? */
1713 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1714 if (strcmp(data
->cols
[k
],
1715 vcdl
->uniq_cols
[j
]) == 0) {
1716 /* yes it does, record the value */
1717 val
= data
->lines
[k
];
1722 * Mark empty values with dashes to make output
1725 if (is_blank_str(val
))
1728 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1729 if (j
< vcdl
->uniq_cols_cnt
- 1)
1733 /* Print out any values that aren't in a column at the end */
1734 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1735 /* Did we have any columns? If so print a spacer. */
1736 if (vcdl
->uniq_cols_cnt
> 0)
1739 val
= data
->lines
[j
];
1740 printf("%s", val
? val
: "");
1747 * Print out configuration state as requested by status_callback.
1750 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1751 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1753 nvlist_t
**child
, *root
;
1755 pool_scan_stat_t
*ps
= NULL
;
1757 char rbuf
[6], wbuf
[6], cbuf
[6];
1759 uint64_t notpresent
;
1760 spare_cbdata_t spare_cb
;
1765 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1766 &child
, &children
) != 0)
1769 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1770 (uint64_t **)&vs
, &c
) == 0);
1772 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1774 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
1777 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1780 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1783 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1785 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1789 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1793 if (cb
->cb_literal
) {
1794 printf(" %5llu %5llu %5llu",
1795 (u_longlong_t
)vs
->vs_read_errors
,
1796 (u_longlong_t
)vs
->vs_write_errors
,
1797 (u_longlong_t
)vs
->vs_checksum_errors
);
1799 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1800 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1801 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
1803 printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1806 if (cb
->cb_print_slow_ios
) {
1807 if (children
== 0) {
1808 /* Only leafs vdevs have slow IOs */
1809 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
1812 snprintf(rbuf
, sizeof (rbuf
), "-");
1816 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
1818 printf(" %5s", rbuf
);
1823 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1824 ¬present
) == 0) {
1825 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1826 (void) printf(" was %s", path
);
1827 } else if (vs
->vs_aux
!= 0) {
1830 switch (vs
->vs_aux
) {
1831 case VDEV_AUX_OPEN_FAILED
:
1832 (void) printf(gettext("cannot open"));
1835 case VDEV_AUX_BAD_GUID_SUM
:
1836 (void) printf(gettext("missing device"));
1839 case VDEV_AUX_NO_REPLICAS
:
1840 (void) printf(gettext("insufficient replicas"));
1843 case VDEV_AUX_VERSION_NEWER
:
1844 (void) printf(gettext("newer version"));
1847 case VDEV_AUX_UNSUP_FEAT
:
1848 (void) printf(gettext("unsupported feature(s)"));
1851 case VDEV_AUX_SPARED
:
1852 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1853 &spare_cb
.cb_guid
) == 0);
1854 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1855 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1856 zpool_get_name(zhp
)) == 0)
1857 (void) printf(gettext("currently in "
1860 (void) printf(gettext("in use by "
1862 zpool_get_name(spare_cb
.cb_zhp
));
1863 zpool_close(spare_cb
.cb_zhp
);
1865 (void) printf(gettext("currently in use"));
1869 case VDEV_AUX_ERR_EXCEEDED
:
1870 (void) printf(gettext("too many errors"));
1873 case VDEV_AUX_IO_FAILURE
:
1874 (void) printf(gettext("experienced I/O failures"));
1877 case VDEV_AUX_BAD_LOG
:
1878 (void) printf(gettext("bad intent log"));
1881 case VDEV_AUX_EXTERNAL
:
1882 (void) printf(gettext("external device fault"));
1885 case VDEV_AUX_SPLIT_POOL
:
1886 (void) printf(gettext("split into new pool"));
1889 case VDEV_AUX_ACTIVE
:
1890 (void) printf(gettext("currently in use"));
1893 case VDEV_AUX_CHILDREN_OFFLINE
:
1894 (void) printf(gettext("all children offline"));
1898 (void) printf(gettext("corrupted data"));
1903 /* The root vdev has the scrub/resilver stats */
1904 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
1905 ZPOOL_CONFIG_VDEV_TREE
);
1906 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
1907 (uint64_t **)&ps
, &c
);
1909 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
1910 if (vs
->vs_scan_processed
!= 0) {
1911 (void) printf(gettext(" (%s)"),
1912 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1913 "resilvering" : "repairing");
1914 } else if (vs
->vs_resilver_deferred
) {
1915 (void) printf(gettext(" (awaiting resilver)"));
1919 if (cb
->vcdl
!= NULL
) {
1920 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1922 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1926 (void) printf("\n");
1928 for (c
= 0; c
< children
; c
++) {
1929 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1931 /* Don't print logs or holes here */
1932 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1934 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1936 if (islog
|| ishole
)
1938 /* Only print normal classes here */
1939 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
1942 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1943 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1945 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1952 * Print the configuration of an exported pool. Iterate over all vdevs in the
1953 * pool, printing out the name and status for each one.
1956 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1964 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1965 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1966 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1969 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1970 (uint64_t **)&vs
, &c
) == 0);
1972 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1973 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1975 if (vs
->vs_aux
!= 0) {
1978 switch (vs
->vs_aux
) {
1979 case VDEV_AUX_OPEN_FAILED
:
1980 (void) printf(gettext("cannot open"));
1983 case VDEV_AUX_BAD_GUID_SUM
:
1984 (void) printf(gettext("missing device"));
1987 case VDEV_AUX_NO_REPLICAS
:
1988 (void) printf(gettext("insufficient replicas"));
1991 case VDEV_AUX_VERSION_NEWER
:
1992 (void) printf(gettext("newer version"));
1995 case VDEV_AUX_UNSUP_FEAT
:
1996 (void) printf(gettext("unsupported feature(s)"));
1999 case VDEV_AUX_ERR_EXCEEDED
:
2000 (void) printf(gettext("too many errors"));
2003 case VDEV_AUX_ACTIVE
:
2004 (void) printf(gettext("currently in use"));
2007 case VDEV_AUX_CHILDREN_OFFLINE
:
2008 (void) printf(gettext("all children offline"));
2012 (void) printf(gettext("corrupted data"));
2016 (void) printf("\n");
2018 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2019 &child
, &children
) != 0)
2022 for (c
= 0; c
< children
; c
++) {
2023 uint64_t is_log
= B_FALSE
;
2025 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2029 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2032 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2033 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2034 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2038 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2039 &child
, &children
) == 0) {
2040 (void) printf(gettext("\tcache\n"));
2041 for (c
= 0; c
< children
; c
++) {
2042 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2044 (void) printf("\t %s\n", vname
);
2049 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2050 &child
, &children
) == 0) {
2051 (void) printf(gettext("\tspares\n"));
2052 for (c
= 0; c
< children
; c
++) {
2053 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2055 (void) printf("\t %s\n", vname
);
2062 * Print specialized class vdevs.
2064 * These are recorded as top level vdevs in the main pool child array
2065 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2066 * print_status_config() or print_import_config() to print the top level
2067 * class vdevs then any of their children (eg mirrored slogs) are printed
2068 * recursively - which works because only the top level vdev is marked.
2071 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2076 boolean_t printed
= B_FALSE
;
2078 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2080 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2084 for (c
= 0; c
< children
; c
++) {
2085 uint64_t is_log
= B_FALSE
;
2089 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2093 bias
= VDEV_ALLOC_CLASS_LOGS
;
2095 (void) nvlist_lookup_string(child
[c
],
2096 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2097 (void) nvlist_lookup_string(child
[c
],
2098 ZPOOL_CONFIG_TYPE
, &type
);
2101 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2103 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2107 (void) printf("\t%s\t\n", gettext(class));
2111 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2112 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2113 if (cb
->cb_print_status
)
2114 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2117 print_import_config(cb
, name
, child
[c
], 2);
2123 * Display the status for the given pool.
2126 show_import(nvlist_t
*config
)
2128 uint64_t pool_state
;
2132 uint64_t hostid
= 0;
2134 char *hostname
= "unknown";
2135 nvlist_t
*nvroot
, *nvinfo
;
2136 zpool_status_t reason
;
2137 zpool_errata_t errata
;
2141 status_cbdata_t cb
= { 0 };
2143 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2145 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2147 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2149 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2152 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2153 (uint64_t **)&vs
, &vsc
) == 0);
2154 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2156 reason
= zpool_import_status(config
, &msgid
, &errata
);
2158 (void) printf(gettext(" pool: %s\n"), name
);
2159 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2160 (void) printf(gettext(" state: %s"), health
);
2161 if (pool_state
== POOL_STATE_DESTROYED
)
2162 (void) printf(gettext(" (DESTROYED)"));
2163 (void) printf("\n");
2166 case ZPOOL_STATUS_MISSING_DEV_R
:
2167 case ZPOOL_STATUS_MISSING_DEV_NR
:
2168 case ZPOOL_STATUS_BAD_GUID_SUM
:
2169 (void) printf(gettext(" status: One or more devices are "
2170 "missing from the system.\n"));
2173 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2174 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2175 (void) printf(gettext(" status: One or more devices contains "
2176 "corrupted data.\n"));
2179 case ZPOOL_STATUS_CORRUPT_DATA
:
2181 gettext(" status: The pool data is corrupted.\n"));
2184 case ZPOOL_STATUS_OFFLINE_DEV
:
2185 (void) printf(gettext(" status: One or more devices "
2186 "are offlined.\n"));
2189 case ZPOOL_STATUS_CORRUPT_POOL
:
2190 (void) printf(gettext(" status: The pool metadata is "
2194 case ZPOOL_STATUS_VERSION_OLDER
:
2195 (void) printf(gettext(" status: The pool is formatted using a "
2196 "legacy on-disk version.\n"));
2199 case ZPOOL_STATUS_VERSION_NEWER
:
2200 (void) printf(gettext(" status: The pool is formatted using an "
2201 "incompatible version.\n"));
2204 case ZPOOL_STATUS_FEAT_DISABLED
:
2205 (void) printf(gettext(" status: Some supported features are "
2206 "not enabled on the pool.\n"));
2209 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2210 (void) printf(gettext("status: The pool uses the following "
2211 "feature(s) not supported on this system:\n"));
2212 zpool_print_unsup_feat(config
);
2215 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2216 (void) printf(gettext("status: The pool can only be accessed "
2217 "in read-only mode on this system. It\n\tcannot be "
2218 "accessed in read-write mode because it uses the "
2219 "following\n\tfeature(s) not supported on this system:\n"));
2220 zpool_print_unsup_feat(config
);
2223 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2224 (void) printf(gettext(" status: The pool is currently "
2225 "imported by another system.\n"));
2228 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2229 (void) printf(gettext(" status: The pool has the "
2230 "multihost property on. It cannot\n\tbe safely imported "
2231 "when the system hostid is not set.\n"));
2234 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2235 (void) printf(gettext(" status: The pool was last accessed by "
2236 "another system.\n"));
2239 case ZPOOL_STATUS_FAULTED_DEV_R
:
2240 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2241 (void) printf(gettext(" status: One or more devices are "
2245 case ZPOOL_STATUS_BAD_LOG
:
2246 (void) printf(gettext(" status: An intent log record cannot be "
2250 case ZPOOL_STATUS_RESILVERING
:
2251 (void) printf(gettext(" status: One or more devices were being "
2255 case ZPOOL_STATUS_ERRATA
:
2256 (void) printf(gettext(" status: Errata #%d detected.\n"),
2262 * No other status can be seen when importing pools.
2264 assert(reason
== ZPOOL_STATUS_OK
);
2268 * Print out an action according to the overall state of the pool.
2270 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2271 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2272 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2273 (void) printf(gettext(" action: The pool can be "
2274 "imported using its name or numeric identifier, "
2275 "though\n\tsome features will not be available "
2276 "without an explicit 'zpool upgrade'.\n"));
2277 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2278 (void) printf(gettext(" action: The pool can be "
2279 "imported using its name or numeric "
2280 "identifier and\n\tthe '-f' flag.\n"));
2281 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2283 case ZPOOL_ERRATA_NONE
:
2286 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2287 (void) printf(gettext(" action: The pool can "
2288 "be imported using its name or numeric "
2289 "identifier,\n\thowever there is a compat"
2290 "ibility issue which should be corrected"
2291 "\n\tby running 'zpool scrub'\n"));
2294 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2295 (void) printf(gettext(" action: The pool can"
2296 "not be imported with this version of ZFS "
2297 "due to\n\tan active asynchronous destroy. "
2298 "Revert to an earlier version\n\tand "
2299 "allow the destroy to complete before "
2303 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2304 (void) printf(gettext(" action: Existing "
2305 "encrypted datasets contain an on-disk "
2306 "incompatibility, which\n\tneeds to be "
2307 "corrected. Backup these datasets to new "
2308 "encrypted datasets\n\tand destroy the "
2314 * All errata must contain an action message.
2319 (void) printf(gettext(" action: The pool can be "
2320 "imported using its name or numeric "
2323 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2324 (void) printf(gettext(" action: The pool can be imported "
2325 "despite missing or damaged devices. The\n\tfault "
2326 "tolerance of the pool may be compromised if imported.\n"));
2329 case ZPOOL_STATUS_VERSION_NEWER
:
2330 (void) printf(gettext(" action: The pool cannot be "
2331 "imported. Access the pool on a system running "
2332 "newer\n\tsoftware, or recreate the pool from "
2335 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2336 (void) printf(gettext("action: The pool cannot be "
2337 "imported. Access the pool on a system that "
2338 "supports\n\tthe required feature(s), or recreate "
2339 "the pool from backup.\n"));
2341 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2342 (void) printf(gettext("action: The pool cannot be "
2343 "imported in read-write mode. Import the pool "
2345 "\t\"-o readonly=on\", access the pool on a system "
2346 "that supports the\n\trequired feature(s), or "
2347 "recreate the pool from backup.\n"));
2349 case ZPOOL_STATUS_MISSING_DEV_R
:
2350 case ZPOOL_STATUS_MISSING_DEV_NR
:
2351 case ZPOOL_STATUS_BAD_GUID_SUM
:
2352 (void) printf(gettext(" action: The pool cannot be "
2353 "imported. Attach the missing\n\tdevices and try "
2356 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2357 VERIFY0(nvlist_lookup_nvlist(config
,
2358 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2360 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2361 hostname
= fnvlist_lookup_string(nvinfo
,
2362 ZPOOL_CONFIG_MMP_HOSTNAME
);
2364 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2365 hostid
= fnvlist_lookup_uint64(nvinfo
,
2366 ZPOOL_CONFIG_MMP_HOSTID
);
2368 (void) printf(gettext(" action: The pool must be "
2369 "exported from %s (hostid=%lx)\n\tbefore it "
2370 "can be safely imported.\n"), hostname
,
2371 (unsigned long) hostid
);
2373 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2374 (void) printf(gettext(" action: Set a unique system "
2375 "hostid with the zgenhostid(8) command.\n"));
2378 (void) printf(gettext(" action: The pool cannot be "
2379 "imported due to damaged devices or data.\n"));
2383 /* Print the comment attached to the pool. */
2384 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2385 (void) printf(gettext("comment: %s\n"), comment
);
2388 * If the state is "closed" or "can't open", and the aux state
2389 * is "corrupt data":
2391 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2392 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2393 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2394 if (pool_state
== POOL_STATE_DESTROYED
)
2395 (void) printf(gettext("\tThe pool was destroyed, "
2396 "but can be imported using the '-Df' flags.\n"));
2397 else if (pool_state
!= POOL_STATE_EXPORTED
)
2398 (void) printf(gettext("\tThe pool may be active on "
2399 "another system, but can be imported using\n\t"
2400 "the '-f' flag.\n"));
2404 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2407 (void) printf(gettext(" config:\n\n"));
2409 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2411 if (cb
.cb_namewidth
< 10)
2412 cb
.cb_namewidth
= 10;
2414 print_import_config(&cb
, name
, nvroot
, 0);
2416 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
2417 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
2418 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
2420 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2421 (void) printf(gettext("\n\tAdditional devices are known to "
2422 "be part of this pool, though their\n\texact "
2423 "configuration cannot be determined.\n"));
2428 zfs_force_import_required(nvlist_t
*config
)
2431 uint64_t hostid
= 0;
2434 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2435 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2437 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2440 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2441 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2442 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2443 ZPOOL_CONFIG_MMP_STATE
);
2445 if (mmp_state
!= MMP_STATE_INACTIVE
)
2453 * Perform the import for the given configuration. This passes the heavy
2454 * lifting off to zpool_import_props(), and then mounts the datasets contained
2458 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2459 nvlist_t
*props
, int flags
)
2462 zpool_handle_t
*zhp
;
2466 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2467 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2469 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2470 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2471 "is formatted using an unsupported ZFS version\n"), name
);
2473 } else if (zfs_force_import_required(config
) &&
2474 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2475 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2478 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2479 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2480 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2481 ZPOOL_CONFIG_MMP_STATE
);
2483 if (mmp_state
== MMP_STATE_ACTIVE
) {
2484 char *hostname
= "<unknown>";
2485 uint64_t hostid
= 0;
2487 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2488 hostname
= fnvlist_lookup_string(nvinfo
,
2489 ZPOOL_CONFIG_MMP_HOSTNAME
);
2491 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2492 hostid
= fnvlist_lookup_uint64(nvinfo
,
2493 ZPOOL_CONFIG_MMP_HOSTID
);
2495 (void) fprintf(stderr
, gettext("cannot import '%s': "
2496 "pool is imported on %s (hostid: "
2497 "0x%lx)\nExport the pool on the other system, "
2498 "then run 'zpool import'.\n"),
2499 name
, hostname
, (unsigned long) hostid
);
2500 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2501 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2502 "pool has the multihost property on and the\n"
2503 "system's hostid is not set. Set a unique hostid "
2504 "with the zgenhostid(8) command.\n"), name
);
2506 char *hostname
= "<unknown>";
2507 uint64_t timestamp
= 0;
2508 uint64_t hostid
= 0;
2510 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2511 hostname
= fnvlist_lookup_string(config
,
2512 ZPOOL_CONFIG_HOSTNAME
);
2514 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2515 timestamp
= fnvlist_lookup_uint64(config
,
2516 ZPOOL_CONFIG_TIMESTAMP
);
2518 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2519 hostid
= fnvlist_lookup_uint64(config
,
2520 ZPOOL_CONFIG_HOSTID
);
2522 (void) fprintf(stderr
, gettext("cannot import '%s': "
2523 "pool was previously in use from another system.\n"
2524 "Last accessed by %s (hostid=%lx) at %s"
2525 "The pool can be imported, use 'zpool import -f' "
2526 "to import the pool.\n"), name
, hostname
,
2527 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2533 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2536 if (newname
!= NULL
)
2537 name
= (char *)newname
;
2539 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2543 * Loading keys is best effort. We don't want to return immediately
2544 * if it fails but we do want to give the error to the caller.
2546 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2547 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2552 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2553 !(flags
& ZFS_IMPORT_ONLY
) &&
2554 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2563 typedef struct target_exists_args
{
2564 const char *poolname
;
2566 } target_exists_args_t
;
2569 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
2571 target_exists_args_t
*args
= data
;
2572 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
2578 if (args
->poolname
!= NULL
) {
2581 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2583 if (strcmp(pool_name
, args
->poolname
) == 0)
2588 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2590 if (pool_guid
== args
->poolguid
)
2598 * zpool checkpoint <pool>
2599 * checkpoint --discard <pool>
2601 * -d Discard the checkpoint from a checkpointed
2604 * Checkpoints the specified pool, by taking a "snapshot" of its
2605 * current state. A pool can only have one checkpoint at a time.
2608 zpool_do_checkpoint(int argc
, char **argv
)
2612 zpool_handle_t
*zhp
;
2615 struct option long_options
[] = {
2616 {"discard", no_argument
, NULL
, 'd'},
2621 while ((c
= getopt_long(argc
, argv
, ":d", long_options
, NULL
)) != -1) {
2627 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2637 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2642 (void) fprintf(stderr
, gettext("too many arguments\n"));
2648 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
2649 /* As a special case, check for use of '/' in the name */
2650 if (strchr(pool
, '/') != NULL
)
2651 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
2652 "doesn't work on datasets. To save the state "
2653 "of a dataset from a specific point in time "
2654 "please use 'zfs snapshot'\n"));
2659 err
= (zpool_discard_checkpoint(zhp
) != 0);
2661 err
= (zpool_checkpoint(zhp
) != 0);
2668 #define CHECKPOINT_OPT 1024
2671 * zpool import [-d dir] [-D]
2672 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2673 * [-d dir | -c cachefile] [-f] -a
2674 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2675 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2677 * -c Read pool information from a cachefile instead of searching
2680 * -d Scan in a specific directory, other than /dev/. More than
2681 * one directory can be specified using multiple '-d' options.
2683 * -D Scan for previously destroyed pools or import all or only
2684 * specified destroyed pools.
2686 * -R Temporarily import the pool, with all mountpoints relative to
2687 * the given root. The pool will remain exported when the machine
2690 * -V Import even in the presence of faulted vdevs. This is an
2691 * intentionally undocumented option for testing purposes, and
2692 * treats the pool configuration as complete, leaving any bad
2693 * vdevs in the FAULTED state. In other words, it does verbatim
2696 * -f Force import, even if it appears that the pool is active.
2698 * -F Attempt rewind if necessary.
2700 * -n See if rewind would work, but don't actually rewind.
2702 * -N Import the pool but don't mount datasets.
2704 * -T Specify a starting txg to use for import. This option is
2705 * intentionally undocumented option for testing purposes.
2707 * -a Import all pools found.
2709 * -l Load encryption keys while importing.
2711 * -o Set property=value and/or temporary mount options (without '=').
2713 * -s Scan using the default search path, the libblkid cache will
2716 * --rewind-to-checkpoint
2717 * Import the pool and revert back to the checkpoint.
2719 * The import command scans for pools to import, and import pools based on pool
2720 * name and GUID. The pool can also be renamed as part of the import process.
2723 zpool_do_import(int argc
, char **argv
)
2725 char **searchdirs
= NULL
;
2726 char *env
, *envdup
= NULL
;
2730 nvlist_t
*pools
= NULL
;
2731 boolean_t do_all
= B_FALSE
;
2732 boolean_t do_destroyed
= B_FALSE
;
2733 char *mntopts
= NULL
;
2736 uint64_t searchguid
= 0;
2737 char *searchname
= NULL
;
2739 nvlist_t
*found_config
;
2740 nvlist_t
*policy
= NULL
;
2741 nvlist_t
*props
= NULL
;
2743 int flags
= ZFS_IMPORT_NORMAL
;
2744 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2745 boolean_t dryrun
= B_FALSE
;
2746 boolean_t do_rewind
= B_FALSE
;
2747 boolean_t xtreme_rewind
= B_FALSE
;
2748 boolean_t do_scan
= B_FALSE
;
2749 boolean_t pool_exists
= B_FALSE
;
2750 uint64_t pool_state
, txg
= -1ULL;
2751 char *cachefile
= NULL
;
2752 importargs_t idata
= { 0 };
2755 struct option long_options
[] = {
2756 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
2761 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
2762 long_options
, NULL
)) != -1) {
2771 if (searchdirs
== NULL
) {
2772 searchdirs
= safe_malloc(sizeof (char *));
2774 char **tmp
= safe_malloc((nsearch
+ 1) *
2776 bcopy(searchdirs
, tmp
, nsearch
*
2781 searchdirs
[nsearch
++] = optarg
;
2784 do_destroyed
= B_TRUE
;
2787 flags
|= ZFS_IMPORT_ANY_HOST
;
2793 flags
|= ZFS_IMPORT_LOAD_KEYS
;
2796 flags
|= ZFS_IMPORT_MISSING_LOG
;
2802 flags
|= ZFS_IMPORT_ONLY
;
2805 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2808 if (add_prop_list(optarg
, propval
,
2816 if (add_prop_list(zpool_prop_to_name(
2817 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2819 if (add_prop_list_default(zpool_prop_to_name(
2820 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2827 flags
|= ZFS_IMPORT_TEMP_NAME
;
2828 if (add_prop_list_default(zpool_prop_to_name(
2829 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2835 txg
= strtoull(optarg
, &endptr
, 0);
2836 if (errno
!= 0 || *endptr
!= '\0') {
2837 (void) fprintf(stderr
,
2838 gettext("invalid txg value\n"));
2841 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2844 flags
|= ZFS_IMPORT_VERBATIM
;
2847 xtreme_rewind
= B_TRUE
;
2849 case CHECKPOINT_OPT
:
2850 flags
|= ZFS_IMPORT_CHECKPOINT
;
2853 (void) fprintf(stderr
, gettext("missing argument for "
2854 "'%c' option\n"), optopt
);
2858 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2867 if (cachefile
&& nsearch
!= 0) {
2868 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2872 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
2873 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
2877 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
2878 (void) fprintf(stderr
, gettext("-l is only meaningful during "
2883 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2884 (void) fprintf(stderr
,
2885 gettext("-n or -X only meaningful with -F\n"));
2889 rewind_policy
= ZPOOL_TRY_REWIND
;
2891 rewind_policy
= ZPOOL_DO_REWIND
;
2893 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2895 /* In the future, we can capture further policy and include it here */
2896 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2897 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
2898 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
2899 rewind_policy
) != 0)
2902 /* check argument count */
2905 (void) fprintf(stderr
, gettext("too many arguments\n"));
2910 (void) fprintf(stderr
, gettext("too many arguments\n"));
2916 * Check for the effective uid. We do this explicitly here because
2917 * otherwise any attempt to discover pools will silently fail.
2919 if (argc
== 0 && geteuid() != 0) {
2920 (void) fprintf(stderr
, gettext("cannot "
2921 "discover pools: permission denied\n"));
2922 if (searchdirs
!= NULL
)
2926 nvlist_free(policy
);
2931 * Depending on the arguments given, we do one of the following:
2933 * <none> Iterate through all pools and display information about
2936 * -a Iterate through all pools and try to import each one.
2938 * <id> Find the pool that corresponds to the given GUID/pool
2939 * name and import that one.
2941 * -D Above options applies only to destroyed pools.
2947 searchguid
= strtoull(argv
[0], &endptr
, 10);
2948 if (errno
!= 0 || *endptr
!= '\0') {
2949 searchname
= argv
[0];
2952 found_config
= NULL
;
2955 * User specified a name or guid. Ensure it's unique.
2957 target_exists_args_t search
= {searchname
, searchguid
};
2958 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
2962 * Check the environment for the preferred search path.
2964 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2967 envdup
= strdup(env
);
2969 dir
= strtok(envdup
, ":");
2970 while (dir
!= NULL
) {
2971 if (searchdirs
== NULL
) {
2972 searchdirs
= safe_malloc(sizeof (char *));
2974 char **tmp
= safe_malloc((nsearch
+ 1) *
2976 bcopy(searchdirs
, tmp
, nsearch
*
2981 searchdirs
[nsearch
++] = dir
;
2982 dir
= strtok(NULL
, ":");
2986 idata
.path
= searchdirs
;
2987 idata
.paths
= nsearch
;
2988 idata
.poolname
= searchname
;
2989 idata
.guid
= searchguid
;
2990 idata
.cachefile
= cachefile
;
2991 idata
.scan
= do_scan
;
2992 idata
.policy
= policy
;
2994 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
2996 if (pools
!= NULL
&& pool_exists
&&
2997 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2998 (void) fprintf(stderr
, gettext("cannot import '%s': "
2999 "a pool with that name already exists\n"),
3001 (void) fprintf(stderr
, gettext("use the form '%s "
3002 "<pool | id> <newpool>' to give it a new name\n"),
3005 } else if (pools
== NULL
&& pool_exists
) {
3006 (void) fprintf(stderr
, gettext("cannot import '%s': "
3007 "a pool with that name is already created/imported,\n"),
3009 (void) fprintf(stderr
, gettext("and no additional pools "
3010 "with that name were found\n"));
3012 } else if (pools
== NULL
) {
3014 (void) fprintf(stderr
, gettext("cannot import '%s': "
3015 "no such pool available\n"), argv
[0]);
3021 if (searchdirs
!= NULL
)
3025 nvlist_free(policy
);
3032 * At this point we have a list of import candidate configs. Even if
3033 * we were searching by pool name or guid, we still need to
3034 * post-process the list to deal with pool state and possible
3040 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3042 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3044 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3046 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3048 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3051 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3058 (void) printf("\n");
3061 err
|= do_import(config
, NULL
, mntopts
,
3064 show_import(config
);
3066 } else if (searchname
!= NULL
) {
3070 * We are searching for a pool based on name.
3072 verify(nvlist_lookup_string(config
,
3073 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3075 if (strcmp(name
, searchname
) == 0) {
3076 if (found_config
!= NULL
) {
3077 (void) fprintf(stderr
, gettext(
3078 "cannot import '%s': more than "
3079 "one matching pool\n"), searchname
);
3080 (void) fprintf(stderr
, gettext(
3081 "import by numeric ID instead\n"));
3084 found_config
= config
;
3090 * Search for a pool by guid.
3092 verify(nvlist_lookup_uint64(config
,
3093 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3095 if (guid
== searchguid
)
3096 found_config
= config
;
3101 * If we were searching for a specific pool, verify that we found a
3102 * pool, and then do the import.
3104 if (argc
!= 0 && err
== 0) {
3105 if (found_config
== NULL
) {
3106 (void) fprintf(stderr
, gettext("cannot import '%s': "
3107 "no such pool available\n"), argv
[0]);
3110 err
|= do_import(found_config
, argc
== 1 ? NULL
:
3111 argv
[1], mntopts
, props
, flags
);
3116 * If we were just looking for pools, report an error if none were
3119 if (argc
== 0 && first
)
3120 (void) fprintf(stderr
,
3121 gettext("no pools available to import\n"));
3126 nvlist_free(policy
);
3127 if (searchdirs
!= NULL
)
3132 return (err
? 1 : 0);
3136 * zpool sync [-f] [pool] ...
3138 * -f (undocumented) force uberblock (and config including zpool cache file)
3141 * Sync the specified pool(s).
3142 * Without arguments "zpool sync" will sync all pools.
3143 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3147 zpool_do_sync(int argc
, char **argv
)
3150 boolean_t force
= B_FALSE
;
3153 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3159 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3168 /* if argc == 0 we will execute zpool_sync_one on all pools */
3169 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
3174 typedef struct iostat_cbdata
{
3179 char **cb_vdev_names
; /* Only show these vdevs */
3180 unsigned int cb_vdev_names_count
;
3181 boolean_t cb_verbose
;
3182 boolean_t cb_literal
;
3183 boolean_t cb_scripted
;
3184 zpool_list_t
*cb_list
;
3185 vdev_cmd_data_list_t
*vcdl
;
3189 typedef struct name_and_columns
{
3190 const char *name
; /* Column name */
3191 unsigned int columns
; /* Center name to this number of columns */
3192 } name_and_columns_t
;
3194 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
3196 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3198 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3200 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3201 {"asyncq_wait", 2}, {"scrub"}},
3202 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3203 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3205 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
3206 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
3207 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3208 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
3212 /* Shorthand - if "columns" field not set, default to 1 column */
3213 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3215 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3217 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3218 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
3219 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3220 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3221 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3222 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
3223 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3224 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3227 static const char *histo_to_title
[] = {
3228 [IOS_L_HISTO
] = "latency",
3229 [IOS_RQ_HISTO
] = "req_size",
3233 * Return the number of labels in a null-terminated name_and_columns_t
3238 label_array_len(const name_and_columns_t
*labels
)
3242 while (labels
[i
].name
)
3249 * Return the number of strings in a null-terminated string array.
3252 * const char foo[] = {"bar", "baz", NULL}
3257 str_array_len(const char *array
[])
3268 * Return a default column width for default/latency/queue columns. This does
3269 * not include histograms, which have their columns autosized.
3272 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3274 unsigned long column_width
= 5; /* Normal niceprint */
3275 static unsigned long widths
[] = {
3277 * Choose some sane default column sizes for printing the
3280 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3281 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3282 [IOS_QUEUES
] = 6, /* 1M queue entries */
3286 column_width
= widths
[type
];
3288 return (column_width
);
3292 * Print the column labels, i.e:
3294 * capacity operations bandwidth
3295 * alloc free read write read write ...
3297 * If force_column_width is set, use it for the column width. If not set, use
3298 * the default column width.
3301 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3302 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3305 unsigned int text_start
, rw_column_width
, spaces_to_end
;
3306 uint64_t flags
= cb
->cb_flags
;
3308 unsigned int column_width
= force_column_width
;
3310 /* For each bit set in flags */
3311 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3312 idx
= lowbit64(f
) - 1;
3313 if (!force_column_width
)
3314 column_width
= default_column_width(cb
, idx
);
3315 /* Print our top labels centered over "read write" label. */
3316 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3317 const char *name
= labels
[idx
][i
].name
;
3319 * We treat labels[][].columns == 0 as shorthand
3320 * for one column. It makes writing out the label
3321 * tables more concise.
3323 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3324 unsigned int slen
= strlen(name
);
3326 rw_column_width
= (column_width
* columns
) +
3327 (2 * (columns
- 1));
3329 text_start
= (int)((rw_column_width
)/columns
-
3332 printf(" "); /* Two spaces between columns */
3334 /* Space from beginning of column to label */
3335 for (s
= 0; s
< text_start
; s
++)
3340 /* Print space after label to end of column */
3341 spaces_to_end
= rw_column_width
- text_start
- slen
;
3342 for (s
= 0; s
< spaces_to_end
; s
++)
3351 * print_cmd_columns - Print custom column titles from -c
3353 * If the user specified the "zpool status|iostat -c" then print their custom
3354 * column titles in the header. For example, print_cmd_columns() would print
3355 * the " col1 col2" part of this:
3357 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3359 * capacity operations bandwidth
3360 * pool alloc free read write read write col1 col2
3361 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3362 * mypool 269K 1008M 0 0 107 946
3363 * mirror 269K 1008M 0 0 107 946
3364 * sdb - - 0 0 102 473 val1 val2
3365 * sdc - - 0 0 5 473 val1 val2
3366 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3369 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3372 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3374 if (vcdl
->count
== 0 || data
== NULL
)
3378 * Each vdev cmd should have the same column names unless the user did
3379 * something weird with their cmd. Just take the column names from the
3380 * first vdev and assume it works for all of them.
3382 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3385 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3388 printf("%*s", vcdl
->uniq_cols_width
[i
],
3389 vcdl
->uniq_cols
[i
]);
3396 * Utility function to print out a line of dashes like:
3398 * -------------------------------- ----- ----- ----- ----- -----
3400 * ...or a dashed named-row line like:
3406 * @force_column_width If non-zero, use the value as the column width.
3407 * Otherwise use the default column widths.
3409 * @name: Print a dashed named-row line starting
3410 * with @name. Otherwise, print a regular
3414 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3418 unsigned int namewidth
;
3419 uint64_t flags
= cb
->cb_flags
;
3422 const name_and_columns_t
*labels
;
3426 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3427 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3428 } else if (cb
->cb_vdev_names_count
) {
3434 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3435 name
? strlen(name
) : 0);
3439 printf("%-*s", namewidth
, name
);
3441 for (i
= 0; i
< namewidth
; i
++)
3445 /* For each bit in flags */
3446 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3447 unsigned int column_width
;
3448 idx
= lowbit64(f
) - 1;
3449 if (force_column_width
)
3450 column_width
= force_column_width
;
3452 column_width
= default_column_width(cb
, idx
);
3454 labels
= iostat_bottom_labels
[idx
];
3455 for (i
= 0; i
< label_array_len(labels
); i
++) {
3457 printf(" %*s-", column_width
- 1, " ");
3459 printf(" %.*s", column_width
,
3460 "--------------------");
3467 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3468 unsigned int force_column_width
)
3470 print_iostat_dashes(cb
, force_column_width
, NULL
);
3474 print_iostat_separator(iostat_cbdata_t
*cb
)
3476 print_iostat_separator_impl(cb
, 0);
3480 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3481 const char *histo_vdev_name
)
3483 unsigned int namewidth
;
3486 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3487 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3488 } else if (cb
->cb_vdev_names_count
) {
3494 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3495 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3497 if (histo_vdev_name
)
3498 printf("%-*s", namewidth
, histo_vdev_name
);
3500 printf("%*s", namewidth
, "");
3503 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3506 printf("%-*s", namewidth
, title
);
3508 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3509 if (cb
->vcdl
!= NULL
)
3510 print_cmd_columns(cb
->vcdl
, 0);
3514 print_iostat_separator_impl(cb
, force_column_width
);
3516 if (cb
->vcdl
!= NULL
)
3517 print_cmd_columns(cb
->vcdl
, 1);
3523 print_iostat_header(iostat_cbdata_t
*cb
)
3525 print_iostat_header_impl(cb
, 0, NULL
);
3530 * Display a single statistic.
3533 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3534 unsigned int column_size
, boolean_t scripted
)
3538 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3541 printf("\t%s", buf
);
3543 printf(" %*s", column_size
, buf
);
3547 * Calculate the default vdev stats
3549 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3550 * stats into calcvs.
3553 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3554 vdev_stat_t
*calcvs
)
3558 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3559 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3560 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3562 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3563 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3567 * Internal representation of the extended iostats data.
3569 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3570 * or single uint64_t's. We make both look like arrays to make them easier
3571 * to process. In order to make single uint64_t's look like arrays, we set
3572 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3573 * we can just use *data and count.
3577 uint_t count
; /* Number of entries in data[] */
3578 uint64_t __data
; /* Only used when data is a single uint64_t */
3582 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3586 for (i
= 0; i
< len
; i
++)
3587 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3593 * Helper function to lookup a uint64_t array or uint64_t value and store its
3594 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3595 * it look like a one element array to make it easier to process.
3598 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3599 struct stat_array
*nva
)
3604 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3605 switch (nvpair_type(tmp
)) {
3606 case DATA_TYPE_UINT64_ARRAY
:
3607 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3609 case DATA_TYPE_UINT64
:
3610 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3611 nva
->data
= &nva
->__data
;
3615 /* Not a uint64_t */
3624 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3625 * subtract them, and return the results in a newly allocated stat_array.
3626 * You must free the returned array after you are done with it with
3627 * free_calc_stats().
3629 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3632 static struct stat_array
*
3633 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3636 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3637 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3639 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3641 /* Extract our extended stats nvlist from the main list */
3642 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3645 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3649 newnva
= safe_malloc(alloc_size
);
3650 oldnva
= safe_malloc(alloc_size
);
3651 calcnva
= safe_malloc(alloc_size
);
3653 for (j
= 0; j
< len
; j
++) {
3654 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3656 calcnva
[j
].count
= newnva
[j
].count
;
3657 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3658 calcnva
[j
].data
= safe_malloc(alloc_size
);
3659 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3662 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3664 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3665 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3674 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3677 for (i
= 0; i
< len
; i
++)
3684 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3685 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3691 enum zfs_nicenum_format format
;
3692 unsigned int buckets
;
3693 unsigned int start_bucket
;
3696 format
= ZFS_NICENUM_RAW
;
3698 format
= ZFS_NICENUM_1024
;
3700 /* All these histos are the same size, so just use nva[0].count */
3701 buckets
= nva
[0].count
;
3703 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3704 /* Start at 512 - req size should never be lower than this */
3710 for (j
= start_bucket
; j
< buckets
; j
++) {
3711 /* Print histogram bucket label */
3712 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3713 /* Ending range of this bucket */
3714 val
= (1UL << (j
+ 1)) - 1;
3715 zfs_nicetime(val
, buf
, sizeof (buf
));
3717 /* Request size (starting range of bucket) */
3719 zfs_nicenum(val
, buf
, sizeof (buf
));
3722 if (cb
->cb_scripted
)
3723 printf("%llu", (u_longlong_t
)val
);
3725 printf("%-*s", namewidth
, buf
);
3727 /* Print the values on the line */
3728 for (i
= 0; i
< len
; i
++) {
3729 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3730 column_width
, cb
->cb_scripted
);
3737 print_solid_separator(unsigned int length
)
3745 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3746 nvlist_t
*newnv
, double scale
, const char *name
)
3748 unsigned int column_width
;
3749 unsigned int namewidth
;
3750 unsigned int entire_width
;
3751 enum iostat_type type
;
3752 struct stat_array
*nva
;
3754 unsigned int names_len
;
3756 /* What type of histo are we? */
3757 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3759 /* Get NULL-terminated array of nvlist names for our histo */
3760 names
= vsx_type_to_nvlist
[type
];
3761 names_len
= str_array_len(names
); /* num of names */
3763 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3765 if (cb
->cb_literal
) {
3766 column_width
= MAX(5,
3767 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3772 namewidth
= MAX(cb
->cb_namewidth
,
3773 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3776 * Calculate the entire line width of what we're printing. The
3777 * +2 is for the two spaces between columns:
3781 /* |___| <---------- column_width */
3783 /* |__________| <--- entire_width */
3785 entire_width
= namewidth
+ (column_width
+ 2) *
3786 label_array_len(iostat_bottom_labels
[type
]);
3788 if (cb
->cb_scripted
)
3789 printf("%s\n", name
);
3791 print_iostat_header_impl(cb
, column_width
, name
);
3793 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3796 free_calc_stats(nva
, names_len
);
3797 if (!cb
->cb_scripted
)
3798 print_solid_separator(entire_width
);
3802 * Calculate the average latency of a power-of-two latency histogram
3805 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3808 uint64_t count
= 0, total
= 0;
3810 for (i
= 0; i
< buckets
; i
++) {
3812 * Our buckets are power-of-two latency ranges. Use the
3813 * midpoint latency of each bucket to calculate the average.
3821 if (histo
[i
] != 0) {
3822 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3827 /* Prevent divide by zero */
3828 return (count
== 0 ? 0 : total
/ count
);
3832 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3837 const char *names
[] = {
3838 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3839 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3840 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3841 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3842 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3843 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3844 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3845 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3846 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3847 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3850 struct stat_array
*nva
;
3852 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3853 enum zfs_nicenum_format format
;
3855 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3858 format
= ZFS_NICENUM_RAW
;
3860 format
= ZFS_NICENUM_1024
;
3862 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3863 val
= nva
[i
].data
[0];
3864 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3867 free_calc_stats(nva
, ARRAY_SIZE(names
));
3871 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3876 const char *names
[] = {
3877 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3878 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3879 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3880 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3881 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3882 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3883 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3884 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3885 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3887 struct stat_array
*nva
;
3889 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3890 enum zfs_nicenum_format format
;
3892 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3895 format
= ZFS_NICENUM_RAWTIME
;
3897 format
= ZFS_NICENUM_TIME
;
3899 /* Print our avg latencies on the line */
3900 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3901 /* Compute average latency for a latency histo */
3902 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
3903 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3905 free_calc_stats(nva
, ARRAY_SIZE(names
));
3909 * Print default statistics (capacity/operations/bandwidth)
3912 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3914 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3915 enum zfs_nicenum_format format
;
3916 char na
; /* char to print for "not applicable" values */
3918 if (cb
->cb_literal
) {
3919 format
= ZFS_NICENUM_RAW
;
3922 format
= ZFS_NICENUM_1024
;
3926 /* only toplevel vdevs have capacity stats */
3927 if (vs
->vs_space
== 0) {
3928 if (cb
->cb_scripted
)
3929 printf("\t%c\t%c", na
, na
);
3931 printf(" %*c %*c", column_width
, na
, column_width
,
3934 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3936 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3937 column_width
, cb
->cb_scripted
);
3940 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3941 format
, column_width
, cb
->cb_scripted
);
3942 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3943 format
, column_width
, cb
->cb_scripted
);
3944 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3945 format
, column_width
, cb
->cb_scripted
);
3946 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3947 format
, column_width
, cb
->cb_scripted
);
3950 static const char *class_name
[] = {
3951 VDEV_ALLOC_BIAS_DEDUP
,
3952 VDEV_ALLOC_BIAS_SPECIAL
,
3953 VDEV_ALLOC_CLASS_LOGS
3957 * Print out all the statistics for the given vdev. This can either be the
3958 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3959 * is a verbose output, and we don't want to display the toplevel pool stats.
3961 * Returns the number of stat lines printed.
3964 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3965 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3967 nvlist_t
**oldchild
, **newchild
;
3968 uint_t c
, children
, oldchildren
;
3969 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3970 vdev_stat_t zerovs
= { 0 };
3977 calcvs
= safe_malloc(sizeof (*calcvs
));
3979 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
3982 if (oldnv
!= NULL
) {
3983 verify(nvlist_lookup_uint64_array(oldnv
,
3984 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3989 /* Do we only want to see a specific vdev? */
3990 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3991 /* Yes we do. Is this the vdev? */
3992 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3994 * This is our vdev. Since it is the only vdev we
3995 * will be displaying, make depth = 0 so that it
3996 * doesn't get indented.
4003 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4004 /* Couldn't match the name */
4009 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4010 (uint64_t **)&newvs
, &c
) == 0);
4013 * Print the vdev name unless it's is a histogram. Histograms
4014 * display the vdev name in the header itself.
4016 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4017 if (cb
->cb_scripted
) {
4020 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4021 (void) printf("%*s%s", depth
, "", name
);
4023 (void) printf("%*s%s%*s", depth
, "", name
,
4024 (int)(cb
->cb_namewidth
- strlen(name
) -
4029 /* Calculate our scaling factor */
4030 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4031 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4033 * If we specify printing histograms with no time interval, then
4034 * print the histogram numbers over the entire lifetime of the
4042 scale
= (double)NANOSEC
/ tdelta
;
4045 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4046 calc_default_iostats(oldvs
, newvs
, calcvs
);
4047 print_iostat_default(calcvs
, cb
, scale
);
4049 if (cb
->cb_flags
& IOS_LATENCY_M
)
4050 print_iostat_latency(cb
, oldnv
, newnv
);
4051 if (cb
->cb_flags
& IOS_QUEUES_M
)
4052 print_iostat_queues(cb
, oldnv
, newnv
);
4053 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4055 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4058 if (cb
->vcdl
!= NULL
) {
4060 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4063 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4067 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4076 if (!cb
->cb_verbose
)
4079 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4080 &newchild
, &children
) != 0)
4084 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4085 &oldchild
, &oldchildren
) != 0)
4088 children
= MIN(oldchildren
, children
);
4092 * print normal top-level devices
4094 for (c
= 0; c
< children
; c
++) {
4095 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4097 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4100 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4103 if (ishole
|| islog
)
4106 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4109 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4111 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4112 newchild
[c
], cb
, depth
+ 2);
4117 * print all other top-level devices
4119 for (uint_t n
= 0; n
< 3; n
++) {
4120 boolean_t printed
= B_FALSE
;
4122 for (c
= 0; c
< children
; c
++) {
4123 uint64_t islog
= B_FALSE
;
4127 (void) nvlist_lookup_uint64(newchild
[c
],
4128 ZPOOL_CONFIG_IS_LOG
, &islog
);
4130 bias
= VDEV_ALLOC_CLASS_LOGS
;
4132 (void) nvlist_lookup_string(newchild
[c
],
4133 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4134 (void) nvlist_lookup_string(newchild
[c
],
4135 ZPOOL_CONFIG_TYPE
, &type
);
4137 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4139 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4143 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4144 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4145 print_iostat_dashes(cb
, 0,
4152 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4154 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4155 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4161 * Include level 2 ARC devices in iostat output
4163 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4164 &newchild
, &children
) != 0)
4168 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4169 &oldchild
, &oldchildren
) != 0)
4172 children
= MIN(oldchildren
, children
);
4176 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4177 !cb
->cb_vdev_names
) {
4178 print_iostat_dashes(cb
, 0, "cache");
4182 for (c
= 0; c
< children
; c
++) {
4183 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4185 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4186 : NULL
, newchild
[c
], cb
, depth
+ 2);
4195 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4197 iostat_cbdata_t
*cb
= data
;
4201 * If the pool has disappeared, remove it from the list and continue.
4203 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4207 pool_list_remove(cb
->cb_list
, zhp
);
4213 * Callback to print out the iostats for the given pool.
4216 print_iostat(zpool_handle_t
*zhp
, void *data
)
4218 iostat_cbdata_t
*cb
= data
;
4219 nvlist_t
*oldconfig
, *newconfig
;
4220 nvlist_t
*oldnvroot
, *newnvroot
;
4223 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4225 if (cb
->cb_iteration
== 1)
4228 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4231 if (oldconfig
== NULL
)
4234 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4237 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4239 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4240 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4241 print_iostat_separator(cb
);
4242 if (cb
->vcdl
!= NULL
) {
4243 print_cmd_columns(cb
->vcdl
, 1);
4258 if (isatty(STDOUT_FILENO
)) {
4259 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4261 columns
= ws
.ws_col
;
4270 * Return the required length of the pool/vdev name column. The minimum
4271 * allowed width and output formatting flags must be provided.
4274 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4276 nvlist_t
*config
, *nvroot
;
4277 int width
= min_width
;
4279 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4280 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4282 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4283 if (verbose
== B_FALSE
) {
4284 width
= MAX(poolname_len
, min_width
);
4286 width
= MAX(poolname_len
,
4287 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4295 * Parse the input string, get the 'interval' and 'count' value if there is one.
4298 get_interval_count(int *argcp
, char **argv
, float *iv
,
4302 unsigned long count
= 0;
4306 * Determine if the last argument is an integer or a pool name
4308 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4312 interval
= strtof(argv
[argc
- 1], &end
);
4314 if (*end
== '\0' && errno
== 0) {
4315 if (interval
== 0) {
4316 (void) fprintf(stderr
, gettext("interval "
4317 "cannot be zero\n"));
4321 * Ignore the last parameter
4326 * If this is not a valid number, just plow on. The
4327 * user will get a more informative error message later
4335 * If the last argument is also an integer, then we have both a count
4338 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4343 interval
= strtof(argv
[argc
- 1], &end
);
4345 if (*end
== '\0' && errno
== 0) {
4346 if (interval
== 0) {
4347 (void) fprintf(stderr
, gettext("interval "
4348 "cannot be zero\n"));
4353 * Ignore the last parameter
4367 get_timestamp_arg(char c
)
4370 timestamp_fmt
= UDATE
;
4372 timestamp_fmt
= DDATE
;
4378 * Return stat flags that are supported by all pools by both the module and
4379 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4380 * It will get ANDed down until only the flags that are supported on all pools
4384 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4386 uint64_t *mask
= data
;
4387 nvlist_t
*config
, *nvroot
, *nvx
;
4391 config
= zpool_get_config(zhp
, NULL
);
4392 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4395 /* Default stats are always supported, but for completeness.. */
4396 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4397 flags
|= IOS_DEFAULT_M
;
4399 /* Get our extended stats nvlist from the main list */
4400 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4403 * No extended stats; they're probably running an older
4404 * module. No big deal, we support that too.
4409 /* For each extended stat, make sure all its nvpairs are supported */
4410 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4411 if (!vsx_type_to_nvlist
[j
][0])
4414 /* Start off by assuming the flag is supported, then check */
4415 flags
|= (1ULL << j
);
4416 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4417 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4418 /* flag isn't supported */
4419 flags
= flags
& ~(1ULL << j
);
4425 *mask
= *mask
& flags
;
4430 * Return a bitmask of stats that are supported on all pools by both the module
4434 get_stat_flags(zpool_list_t
*list
)
4439 * get_stat_flags_cb() will lop off bits from "mask" until only the
4440 * flags that are supported on all pools remain.
4442 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4447 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4450 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4452 iostat_cbdata_t
*cb
= cb_data
;
4456 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4458 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4459 ret
= 1; /* match */
4466 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4469 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4471 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4475 * Check if vdevs are in a pool
4477 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4478 * return 0. If pool_name is NULL, then search all pools.
4481 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4482 iostat_cbdata_t
*cb
)
4489 if ((argc
== 0) || !*argv
)
4495 /* Temporarily hijack cb_vdev_names for a second... */
4496 tmp_name
= cb
->cb_vdev_names
;
4498 /* Go though our list of prospective vdev names */
4499 for (i
= 0; i
< argc
; i
++) {
4500 cb
->cb_vdev_names
= argv
+ i
;
4502 /* Is this name a vdev in our pools? */
4503 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4511 cb
->cb_vdev_names
= tmp_name
;
4517 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4520 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4527 * Do we have a pool named *name? If so, return 1, otherwise 0.
4532 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4535 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4537 are_all_pools(int argc
, char **argv
)
4539 if ((argc
== 0) || !*argv
)
4543 if (!is_pool(argv
[argc
]))
4550 * Helper function to print out vdev/pool names we can't resolve. Used for an
4554 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4555 iostat_cbdata_t
*cb
)
4560 for (i
= 0; i
< argc
; i
++) {
4564 str
= gettext("pool");
4565 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4566 str
= gettext("vdev in this pool");
4567 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4568 str
= gettext("vdev in another pool");
4570 str
= gettext("unknown");
4572 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4577 * Same as get_interval_count(), but with additional checks to not misinterpret
4578 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4582 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4583 unsigned long *count
, iostat_cbdata_t
*cb
)
4585 char **tmpargv
= argv
;
4586 int argc_for_interval
= 0;
4588 /* Is the last arg an interval value? Or a guid? */
4589 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4591 * The last arg is not a guid, so it's probably an
4594 argc_for_interval
++;
4597 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4599 * The 2nd to last arg is not a guid, so it's probably
4600 * an interval value.
4602 argc_for_interval
++;
4606 /* Point to our list of possible intervals */
4607 tmpargv
= &argv
[*argc
- argc_for_interval
];
4609 *argc
= *argc
- argc_for_interval
;
4610 get_interval_count(&argc_for_interval
, tmpargv
,
4615 * Floating point sleep(). Allows you to pass in a floating point value for
4621 struct timespec req
;
4622 req
.tv_sec
= floor(sec
);
4623 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4624 nanosleep(&req
, NULL
);
4628 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4631 * name: Short name of the script ('iostat').
4632 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4635 print_zpool_script_help(char *name
, char *path
)
4637 char *argv
[] = {path
, "-h", NULL
};
4638 char **lines
= NULL
;
4642 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4644 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
4646 libzfs_free_str_array(lines
, lines_cnt
);
4650 for (int i
= 0; i
< lines_cnt
; i
++)
4651 if (!is_blank_str(lines
[i
]))
4652 printf(" %-14s %s\n", name
, lines
[i
]);
4654 libzfs_free_str_array(lines
, lines_cnt
);
4658 * Go though the zpool status/iostat -c scripts in the user's path, run their
4659 * help option (-h), and print out the results.
4662 print_zpool_dir_scripts(char *dirpath
)
4666 char fullpath
[MAXPATHLEN
];
4667 struct stat dir_stat
;
4669 if ((dir
= opendir(dirpath
)) != NULL
) {
4670 /* print all the files and directories within directory */
4671 while ((ent
= readdir(dir
)) != NULL
) {
4672 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
4674 /* Print the scripts */
4675 if (stat(fullpath
, &dir_stat
) == 0)
4676 if (dir_stat
.st_mode
& S_IXUSR
&&
4677 S_ISREG(dir_stat
.st_mode
))
4678 print_zpool_script_help(ent
->d_name
,
4686 * Print out help text for all zpool status/iostat -c scripts.
4689 print_zpool_script_list(char *subcommand
)
4693 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
4695 sp
= zpool_get_cmd_search_path();
4699 dir
= strtok(sp
, ":");
4700 while (dir
!= NULL
) {
4701 print_zpool_dir_scripts(dir
);
4702 dir
= strtok(NULL
, ":");
4709 * Set the minimum pool/vdev name column width. The width must be at least 10,
4710 * but may be as large as the column width - 42 so it still fits on one line.
4713 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
4715 iostat_cbdata_t
*cb
= data
;
4718 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
4720 columns
= get_columns();
4724 if (width
> columns
- 42)
4725 width
= columns
- 42;
4727 cb
->cb_namewidth
= width
;
4733 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4734 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4735 * [interval [count]]
4737 * -c CMD For each vdev, run command CMD
4738 * -g Display guid for individual vdev name.
4739 * -L Follow links when resolving vdev path name.
4740 * -P Display full path for vdev name.
4741 * -v Display statistics for individual vdevs
4743 * -p Display values in parsable (exact) format.
4744 * -H Scripted mode. Don't display headers, and separate properties
4746 * -l Display average latency
4747 * -q Display queue depths
4748 * -w Display latency histograms
4749 * -r Display request size histogram
4750 * -T Display a timestamp in date(1) or Unix format
4752 * This command can be tricky because we want to be able to deal with pool
4753 * creation/destruction as well as vdev configuration changes. The bulk of this
4754 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4755 * on pool_list_update() to detect the addition of new pools. Configuration
4756 * changes are all handled within libzfs.
4759 zpool_do_iostat(int argc
, char **argv
)
4765 unsigned long count
= 0;
4767 boolean_t verbose
= B_FALSE
;
4768 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4769 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4770 boolean_t omit_since_boot
= B_FALSE
;
4771 boolean_t guid
= B_FALSE
;
4772 boolean_t follow_links
= B_FALSE
;
4773 boolean_t full_name
= B_FALSE
;
4774 iostat_cbdata_t cb
= { 0 };
4777 /* Used for printing error message */
4778 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4779 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4781 uint64_t unsupported_flags
;
4784 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4789 gettext("Can't set -c flag twice\n"));
4793 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
4794 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4795 fprintf(stderr
, gettext(
4796 "Can't run -c, disabled by "
4797 "ZPOOL_SCRIPTS_ENABLED.\n"));
4801 if ((getuid() <= 0 || geteuid() <= 0) &&
4802 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4803 fprintf(stderr
, gettext(
4804 "Can't run -c with root privileges "
4805 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4815 follow_links
= B_TRUE
;
4821 get_timestamp_arg(*optarg
);
4845 omit_since_boot
= B_TRUE
;
4851 if (optopt
== 'c') {
4852 print_zpool_script_list("iostat");
4856 gettext("invalid option '%c'\n"), optopt
);
4865 cb
.cb_literal
= parsable
;
4866 cb
.cb_scripted
= scripted
;
4869 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4871 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4873 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4874 cb
.cb_iteration
= 0;
4875 cb
.cb_namewidth
= 0;
4876 cb
.cb_verbose
= verbose
;
4878 /* Get our interval and count values (if any) */
4880 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4883 get_interval_count(&argc
, argv
, &interval
, &count
);
4887 /* No args, so just print the defaults. */
4888 } else if (are_all_pools(argc
, argv
)) {
4889 /* All the args are pool names */
4890 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4891 /* All the args are vdevs */
4892 cb
.cb_vdev_names
= argv
;
4893 cb
.cb_vdev_names_count
= argc
;
4894 argc
= 0; /* No pools to process */
4895 } else if (are_all_pools(1, argv
)) {
4896 /* The first arg is a pool name */
4897 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4898 /* ...and the rest are vdev names */
4899 cb
.cb_vdev_names
= argv
+ 1;
4900 cb
.cb_vdev_names_count
= argc
- 1;
4901 argc
= 1; /* One pool to process */
4903 fprintf(stderr
, gettext("Expected either a list of "));
4904 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4905 fprintf(stderr
, " \"%s\", ", argv
[0]);
4906 fprintf(stderr
, gettext("but got:\n"));
4907 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4909 fprintf(stderr
, "\n");
4915 * The args don't make sense. The first arg isn't a pool name,
4916 * nor are all the args vdevs.
4918 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4919 fprintf(stderr
, "\n");
4923 if (cb
.cb_vdev_names_count
!= 0) {
4925 * If user specified vdevs, it implies verbose.
4927 cb
.cb_verbose
= B_TRUE
;
4931 * Construct the list of all interesting pools.
4934 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4937 if (pool_list_count(list
) == 0 && argc
!= 0) {
4938 pool_list_free(list
);
4942 if (pool_list_count(list
) == 0 && interval
== 0) {
4943 pool_list_free(list
);
4944 (void) fprintf(stderr
, gettext("no pools available\n"));
4948 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4949 pool_list_free(list
);
4950 (void) fprintf(stderr
,
4951 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4956 if (l_histo
&& rq_histo
) {
4957 pool_list_free(list
);
4958 (void) fprintf(stderr
,
4959 gettext("Only one of [-r|-w] can be passed at a time\n"));
4965 * Enter the main iostat loop.
4971 * Histograms tables look out of place when you try to display
4972 * them with the other stats, so make a rule that you can only
4973 * print histograms by themselves.
4975 cb
.cb_flags
= IOS_L_HISTO_M
;
4976 } else if (rq_histo
) {
4977 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4979 cb
.cb_flags
= IOS_DEFAULT_M
;
4981 cb
.cb_flags
|= IOS_LATENCY_M
;
4983 cb
.cb_flags
|= IOS_QUEUES_M
;
4987 * See if the module supports all the stats we want to display.
4989 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4990 if (unsupported_flags
) {
4994 gettext("The loaded zfs module doesn't support:"));
4996 /* for each bit set in unsupported_flags */
4997 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4998 idx
= lowbit64(f
) - 1;
4999 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5002 fprintf(stderr
, ". Try running a newer module.\n");
5003 pool_list_free(list
);
5009 if ((npools
= pool_list_count(list
)) == 0)
5010 (void) fprintf(stderr
, gettext("no pools available\n"));
5013 * If this is the first iteration and -y was supplied
5014 * we skip any printing.
5016 boolean_t skip
= (omit_since_boot
&&
5017 cb
.cb_iteration
== 0);
5020 * Refresh all statistics. This is done as an
5021 * explicit step before calculating the maximum name
5022 * width, so that any * configuration changes are
5023 * properly accounted for.
5025 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5029 * Iterate over all pools to determine the maximum width
5030 * for the pool / device name column across all pools.
5032 cb
.cb_namewidth
= 0;
5033 (void) pool_list_iter(list
, B_FALSE
,
5034 get_namewidth_iostat
, &cb
);
5036 if (timestamp_fmt
!= NODATE
)
5037 print_timestamp(timestamp_fmt
);
5039 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5040 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5041 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5042 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5043 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5049 * If it's the first time and we're not skipping it,
5050 * or either skip or verbose mode, print the header.
5052 * The histogram code explicitly prints its header on
5053 * every vdev, so skip this for histograms.
5055 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5056 (skip
!= verbose
)) &&
5057 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5059 print_iostat_header(&cb
);
5062 (void) fsleep(interval
);
5067 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5070 * If there's more than one pool, and we're not in
5071 * verbose mode (which prints a separator for us),
5072 * then print a separator.
5074 * In addition, if we're printing specific vdevs then
5075 * we also want an ending separator.
5077 if (((npools
> 1 && !verbose
&&
5078 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5079 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5080 cb
.cb_vdev_names_count
)) &&
5082 print_iostat_separator(&cb
);
5083 if (cb
.vcdl
!= NULL
)
5084 print_cmd_columns(cb
.vcdl
, 1);
5088 if (cb
.vcdl
!= NULL
)
5089 free_vdev_cmd_data_list(cb
.vcdl
);
5094 * Flush the output so that redirection to a file isn't buffered
5097 (void) fflush(stdout
);
5102 if (count
!= 0 && --count
== 0)
5105 (void) fsleep(interval
);
5108 pool_list_free(list
);
5113 typedef struct list_cbdata
{
5114 boolean_t cb_verbose
;
5117 boolean_t cb_scripted
;
5118 zprop_list_t
*cb_proplist
;
5119 boolean_t cb_literal
;
5124 * Given a list of columns to display, output appropriate headers for each one.
5127 print_header(list_cbdata_t
*cb
)
5129 zprop_list_t
*pl
= cb
->cb_proplist
;
5130 char headerbuf
[ZPOOL_MAXPROPLEN
];
5132 boolean_t first
= B_TRUE
;
5133 boolean_t right_justify
;
5136 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5137 width
= pl
->pl_width
;
5138 if (first
&& cb
->cb_verbose
) {
5140 * Reset the width to accommodate the verbose listing
5143 width
= cb
->cb_namewidth
;
5151 right_justify
= B_FALSE
;
5152 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5153 header
= zpool_prop_column_name(pl
->pl_prop
);
5154 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5158 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5159 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5160 headerbuf
[i
] = '\0';
5164 if (pl
->pl_next
== NULL
&& !right_justify
)
5165 (void) printf("%s", header
);
5166 else if (right_justify
)
5167 (void) printf("%*s", (int)width
, header
);
5169 (void) printf("%-*s", (int)width
, header
);
5172 (void) printf("\n");
5176 * Given a pool and a list of properties, print out all the properties according
5177 * to the described layout. Used by zpool_do_list().
5180 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5182 zprop_list_t
*pl
= cb
->cb_proplist
;
5183 boolean_t first
= B_TRUE
;
5184 char property
[ZPOOL_MAXPROPLEN
];
5186 boolean_t right_justify
;
5189 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5191 width
= pl
->pl_width
;
5192 if (first
&& cb
->cb_verbose
) {
5194 * Reset the width to accommodate the verbose listing
5197 width
= cb
->cb_namewidth
;
5201 if (cb
->cb_scripted
)
5202 (void) printf("\t");
5209 right_justify
= B_FALSE
;
5210 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5211 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5212 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5217 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5218 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5219 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5220 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5221 sizeof (property
)) == 0) {
5229 * If this is being called in scripted mode, or if this is the
5230 * last column and it is left-justified, don't include a width
5233 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5234 (void) printf("%s", propstr
);
5235 else if (right_justify
)
5236 (void) printf("%*s", (int)width
, propstr
);
5238 (void) printf("%-*s", (int)width
, propstr
);
5241 (void) printf("\n");
5245 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5246 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5250 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5253 case ZPOOL_PROP_EXPANDSZ
:
5254 case ZPOOL_PROP_CHECKPOINT
:
5255 case ZPOOL_PROP_DEDUPRATIO
:
5257 (void) strlcpy(propval
, "-", sizeof (propval
));
5259 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5262 case ZPOOL_PROP_FRAGMENTATION
:
5263 if (value
== ZFS_FRAG_INVALID
) {
5264 (void) strlcpy(propval
, "-", sizeof (propval
));
5265 } else if (format
== ZFS_NICENUM_RAW
) {
5266 (void) snprintf(propval
, sizeof (propval
), "%llu",
5267 (unsigned long long)value
);
5269 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5270 (unsigned long long)value
);
5273 case ZPOOL_PROP_CAPACITY
:
5274 /* capacity value is in parts-per-10,000 (aka permyriad) */
5275 if (format
== ZFS_NICENUM_RAW
)
5276 (void) snprintf(propval
, sizeof (propval
), "%llu",
5277 (unsigned long long)value
/ 100);
5279 (void) snprintf(propval
, sizeof (propval
),
5280 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
5281 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
5283 case ZPOOL_PROP_HEALTH
:
5285 snprintf(propval
, sizeof (propval
), "%-*s", (int)width
, str
);
5288 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5292 (void) strlcpy(propval
, "-", sizeof (propval
));
5295 (void) printf("\t%s", propval
);
5297 (void) printf(" %*s", (int)width
, propval
);
5301 * print static default line per vdev
5302 * not compatible with '-o' <proplist> option
5305 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5306 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
5312 boolean_t scripted
= cb
->cb_scripted
;
5313 uint64_t islog
= B_FALSE
;
5314 char *dashes
= "%-*s - - - - "
5317 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5318 (uint64_t **)&vs
, &c
) == 0);
5321 boolean_t toplevel
= (vs
->vs_space
!= 0);
5323 enum zfs_nicenum_format format
;
5327 format
= ZFS_NICENUM_RAW
;
5329 format
= ZFS_NICENUM_1024
;
5331 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5335 (void) printf("\t%s", name
);
5336 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5337 (void) printf("%*s%s", depth
, "", name
);
5339 (void) printf("%*s%s%*s", depth
, "", name
,
5340 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5343 * Print the properties for the individual vdevs. Some
5344 * properties are only applicable to toplevel vdevs. The
5345 * 'toplevel' boolean value is passed to the print_one_column()
5346 * to indicate that the value is valid.
5348 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
5350 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
5351 scripted
, toplevel
, format
);
5352 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5353 NULL
, scripted
, toplevel
, format
);
5354 print_one_column(ZPOOL_PROP_CHECKPOINT
,
5355 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
5356 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
5357 scripted
, B_TRUE
, format
);
5358 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5359 vs
->vs_fragmentation
, NULL
, scripted
,
5360 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5362 cap
= (vs
->vs_space
== 0) ? 0 :
5363 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
5364 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
5365 scripted
, toplevel
, format
);
5366 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
5367 scripted
, toplevel
, format
);
5368 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5370 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
5372 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
5375 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
5377 (void) printf("\n");
5380 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5381 &child
, &children
) != 0)
5384 /* list the normal vdevs first */
5385 for (c
= 0; c
< children
; c
++) {
5386 uint64_t ishole
= B_FALSE
;
5388 if (nvlist_lookup_uint64(child
[c
],
5389 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5392 if (nvlist_lookup_uint64(child
[c
],
5393 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
5396 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
5399 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5401 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
5405 /* list the classes: 'logs', 'dedup', and 'special' */
5406 for (uint_t n
= 0; n
< 3; n
++) {
5407 boolean_t printed
= B_FALSE
;
5409 for (c
= 0; c
< children
; c
++) {
5413 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5414 &islog
) == 0 && islog
) {
5415 bias
= VDEV_ALLOC_CLASS_LOGS
;
5417 (void) nvlist_lookup_string(child
[c
],
5418 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
5419 (void) nvlist_lookup_string(child
[c
],
5420 ZPOOL_CONFIG_TYPE
, &type
);
5422 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
5424 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
5428 /* LINTED E_SEC_PRINTF_VAR_FMT */
5429 (void) printf(dashes
, cb
->cb_namewidth
,
5433 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5435 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5441 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5442 &child
, &children
) == 0 && children
> 0) {
5443 /* LINTED E_SEC_PRINTF_VAR_FMT */
5444 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5445 for (c
= 0; c
< children
; c
++) {
5446 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5448 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5454 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5455 &children
) == 0 && children
> 0) {
5456 /* LINTED E_SEC_PRINTF_VAR_FMT */
5457 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5458 for (c
= 0; c
< children
; c
++) {
5459 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5461 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
5469 * Generic callback function to list a pool.
5472 list_callback(zpool_handle_t
*zhp
, void *data
)
5474 list_cbdata_t
*cbp
= data
;
5476 print_pool(zhp
, cbp
);
5478 if (cbp
->cb_verbose
) {
5479 nvlist_t
*config
, *nvroot
;
5481 config
= zpool_get_config(zhp
, NULL
);
5482 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5484 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
5491 * Set the minimum pool/vdev name column width. The width must be at least 9,
5492 * but may be as large as needed.
5495 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
5497 list_cbdata_t
*cb
= data
;
5500 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5506 cb
->cb_namewidth
= width
;
5512 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5514 * -g Display guid for individual vdev name.
5515 * -H Scripted mode. Don't display headers, and separate properties
5517 * -L Follow links when resolving vdev path name.
5518 * -o List of properties to display. Defaults to
5519 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5520 * "dedupratio,health,altroot"
5521 * -p Display values in parsable (exact) format.
5522 * -P Display full path for vdev name.
5523 * -T Display a timestamp in date(1) or Unix format
5525 * List all pools in the system, whether or not they're healthy. Output space
5526 * statistics for each one, as well as health status summary.
5529 zpool_do_list(int argc
, char **argv
)
5533 list_cbdata_t cb
= { 0 };
5534 static char default_props
[] =
5535 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
5536 "capacity,dedupratio,health,altroot";
5537 char *props
= default_props
;
5539 unsigned long count
= 0;
5541 boolean_t first
= B_TRUE
;
5544 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
5547 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5550 cb
.cb_scripted
= B_TRUE
;
5553 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5559 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5562 cb
.cb_literal
= B_TRUE
;
5565 get_timestamp_arg(*optarg
);
5568 cb
.cb_verbose
= B_TRUE
;
5569 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
5572 (void) fprintf(stderr
, gettext("missing argument for "
5573 "'%c' option\n"), optopt
);
5577 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5586 get_interval_count(&argc
, argv
, &interval
, &count
);
5588 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5592 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
5596 if (pool_list_count(list
) == 0)
5599 cb
.cb_namewidth
= 0;
5600 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
5602 if (timestamp_fmt
!= NODATE
)
5603 print_timestamp(timestamp_fmt
);
5605 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
5609 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
5614 if (count
!= 0 && --count
== 0)
5617 pool_list_free(list
);
5618 (void) fsleep(interval
);
5621 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
5622 (void) printf(gettext("no pools available\n"));
5626 pool_list_free(list
);
5627 zprop_free_list(cb
.cb_proplist
);
5632 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
5634 boolean_t force
= B_FALSE
;
5637 char *poolname
, *old_disk
, *new_disk
;
5638 zpool_handle_t
*zhp
;
5639 nvlist_t
*props
= NULL
;
5644 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5650 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5651 (void) fprintf(stderr
, gettext("missing "
5652 "'=' for -o option\n"));
5658 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5659 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5663 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5672 /* get pool name and check number of arguments */
5674 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5681 (void) fprintf(stderr
,
5682 gettext("missing <device> specification\n"));
5690 (void) fprintf(stderr
,
5691 gettext("missing <new_device> specification\n"));
5694 new_disk
= old_disk
;
5704 (void) fprintf(stderr
, gettext("too many arguments\n"));
5708 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5713 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5714 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5721 /* unless manually specified use "ashift" pool property (if set) */
5722 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5725 char strval
[ZPOOL_MAXPROPLEN
];
5727 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5728 if (src
!= ZPROP_SRC_DEFAULT
) {
5729 (void) sprintf(strval
, "%" PRId32
, intval
);
5730 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5731 &props
, B_TRUE
) == 0);
5735 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5737 if (nvroot
== NULL
) {
5743 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5746 nvlist_free(nvroot
);
5753 * zpool replace [-f] <pool> <device> <new_device>
5755 * -f Force attach, even if <new_device> appears to be in use.
5757 * Replace <device> with <new_device>.
5761 zpool_do_replace(int argc
, char **argv
)
5763 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5767 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5769 * -f Force attach, even if <new_device> appears to be in use.
5770 * -o Set property=value.
5772 * Attach <new_device> to the mirror containing <device>. If <device> is not
5773 * part of a mirror, then <device> will be transformed into a mirror of
5774 * <device> and <new_device>. In either case, <new_device> will begin life
5775 * with a DTL of [0, now], and will immediately begin to resilver itself.
5778 zpool_do_attach(int argc
, char **argv
)
5780 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5784 * zpool detach [-f] <pool> <device>
5786 * -f Force detach of <device>, even if DTLs argue against it
5787 * (not supported yet)
5789 * Detach a device from a mirror. The operation will be refused if <device>
5790 * is the last device in the mirror, or if the DTLs indicate that this device
5791 * has the only valid copy of some data.
5795 zpool_do_detach(int argc
, char **argv
)
5798 char *poolname
, *path
;
5799 zpool_handle_t
*zhp
;
5803 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5807 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5816 /* get pool name and check number of arguments */
5818 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5823 (void) fprintf(stderr
,
5824 gettext("missing <device> specification\n"));
5831 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5834 ret
= zpool_vdev_detach(zhp
, path
);
5842 * zpool split [-gLnP] [-o prop=val] ...
5844 * [-R altroot] <pool> <newpool> [<device> ...]
5846 * -g Display guid for individual vdev name.
5847 * -L Follow links when resolving vdev path name.
5848 * -n Do not split the pool, but display the resulting layout if
5849 * it were to be split.
5850 * -o Set property=value, or set mount options.
5851 * -P Display full path for vdev name.
5852 * -R Mount the split-off pool under an alternate root.
5853 * -l Load encryption keys while importing.
5855 * Splits the named pool and gives it the new pool name. Devices to be split
5856 * off may be listed, provided that no more than one device is specified
5857 * per top-level vdev mirror. The newly split pool is left in an exported
5858 * state unless -R is specified.
5860 * Restrictions: the top-level of the pool pool must only be made up of
5861 * mirrors; all devices in the pool must be healthy; no device may be
5862 * undergoing a resilvering operation.
5865 zpool_do_split(int argc
, char **argv
)
5867 char *srcpool
, *newpool
, *propval
;
5868 char *mntopts
= NULL
;
5871 boolean_t loadkeys
= B_FALSE
;
5872 zpool_handle_t
*zhp
;
5873 nvlist_t
*config
, *props
= NULL
;
5875 flags
.dryrun
= B_FALSE
;
5876 flags
.import
= B_FALSE
;
5877 flags
.name_flags
= 0;
5880 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
5883 flags
.name_flags
|= VDEV_NAME_GUID
;
5886 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5889 flags
.import
= B_TRUE
;
5891 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5892 &props
, B_TRUE
) != 0) {
5901 flags
.dryrun
= B_TRUE
;
5904 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5907 if (add_prop_list(optarg
, propval
,
5908 &props
, B_TRUE
) != 0) {
5917 flags
.name_flags
|= VDEV_NAME_PATH
;
5920 (void) fprintf(stderr
, gettext("missing argument for "
5921 "'%c' option\n"), optopt
);
5925 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5932 if (!flags
.import
&& mntopts
!= NULL
) {
5933 (void) fprintf(stderr
, gettext("setting mntopts is only "
5934 "valid when importing the pool\n"));
5938 if (!flags
.import
&& loadkeys
) {
5939 (void) fprintf(stderr
, gettext("loading keys is only "
5940 "valid when importing the pool\n"));
5948 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5952 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5962 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5967 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5968 if (config
== NULL
) {
5972 (void) printf(gettext("would create '%s' with the "
5973 "following layout:\n\n"), newpool
);
5974 print_vdev_tree(NULL
, newpool
, config
, 0, "",
5981 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5982 nvlist_free(config
);
5988 * The split was successful. Now we need to open the new
5989 * pool and import it.
5991 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5992 nvlist_free(config
);
5998 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6003 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6004 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6006 (void) fprintf(stderr
, gettext("Split was successful, but "
6007 "the datasets could not all be mounted\n"));
6008 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6009 "different altroot\n"), "zpool import");
6012 nvlist_free(config
);
6021 * zpool online <pool> <device> ...
6024 zpool_do_online(int argc
, char **argv
)
6028 zpool_handle_t
*zhp
;
6030 vdev_state_t newstate
;
6034 while ((c
= getopt(argc
, argv
, "et")) != -1) {
6037 flags
|= ZFS_ONLINE_EXPAND
;
6041 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6050 /* get pool name and check number of arguments */
6052 (void) fprintf(stderr
, gettext("missing pool name\n"));
6056 (void) fprintf(stderr
, gettext("missing device name\n"));
6062 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6065 for (i
= 1; i
< argc
; i
++) {
6066 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6067 if (newstate
!= VDEV_STATE_HEALTHY
) {
6068 (void) printf(gettext("warning: device '%s' "
6069 "onlined, but remains in faulted state\n"),
6071 if (newstate
== VDEV_STATE_FAULTED
)
6072 (void) printf(gettext("use 'zpool "
6073 "clear' to restore a faulted "
6076 (void) printf(gettext("use 'zpool "
6077 "replace' to replace devices "
6078 "that are no longer present\n"));
6091 * zpool offline [-ft] <pool> <device> ...
6093 * -f Force the device into a faulted state.
6095 * -t Only take the device off-line temporarily. The offline/faulted
6096 * state will not be persistent across reboots.
6100 zpool_do_offline(int argc
, char **argv
)
6104 zpool_handle_t
*zhp
;
6106 boolean_t istmp
= B_FALSE
;
6107 boolean_t fault
= B_FALSE
;
6110 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6119 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6128 /* get pool name and check number of arguments */
6130 (void) fprintf(stderr
, gettext("missing pool name\n"));
6134 (void) fprintf(stderr
, gettext("missing device name\n"));
6140 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6143 for (i
= 1; i
< argc
; i
++) {
6145 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6147 if (istmp
== B_FALSE
) {
6148 /* Force the fault to persist across imports */
6149 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6151 aux
= VDEV_AUX_EXTERNAL
;
6154 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6157 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6168 * zpool clear <pool> [device]
6170 * Clear all errors associated with a pool or a particular device.
6173 zpool_do_clear(int argc
, char **argv
)
6177 boolean_t dryrun
= B_FALSE
;
6178 boolean_t do_rewind
= B_FALSE
;
6179 boolean_t xtreme_rewind
= B_FALSE
;
6180 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6181 nvlist_t
*policy
= NULL
;
6182 zpool_handle_t
*zhp
;
6183 char *pool
, *device
;
6186 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6195 xtreme_rewind
= B_TRUE
;
6198 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6208 (void) fprintf(stderr
, gettext("missing pool name\n"));
6213 (void) fprintf(stderr
, gettext("too many arguments\n"));
6217 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6218 (void) fprintf(stderr
,
6219 gettext("-n or -X only meaningful with -F\n"));
6223 rewind_policy
= ZPOOL_TRY_REWIND
;
6225 rewind_policy
= ZPOOL_DO_REWIND
;
6227 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6229 /* In future, further rewind policy choices can be passed along here */
6230 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6231 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
6232 rewind_policy
) != 0) {
6237 device
= argc
== 2 ? argv
[1] : NULL
;
6239 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
6240 nvlist_free(policy
);
6244 if (zpool_clear(zhp
, device
, policy
) != 0)
6249 nvlist_free(policy
);
6255 * zpool reguid <pool>
6258 zpool_do_reguid(int argc
, char **argv
)
6262 zpool_handle_t
*zhp
;
6266 while ((c
= getopt(argc
, argv
, "")) != -1) {
6269 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6278 /* get pool name and check number of arguments */
6280 (void) fprintf(stderr
, gettext("missing pool name\n"));
6285 (void) fprintf(stderr
, gettext("too many arguments\n"));
6290 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6293 ret
= zpool_reguid(zhp
);
6301 * zpool reopen <pool>
6303 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6306 zpool_do_reopen(int argc
, char **argv
)
6310 boolean_t scrub_restart
= B_TRUE
;
6313 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6316 scrub_restart
= B_FALSE
;
6319 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6328 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6329 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
6335 typedef struct scrub_cbdata
{
6339 pool_scrub_cmd_t cb_scrub_cmd
;
6343 zpool_has_checkpoint(zpool_handle_t
*zhp
)
6345 nvlist_t
*config
, *nvroot
;
6347 config
= zpool_get_config(zhp
, NULL
);
6349 if (config
!= NULL
) {
6350 pool_checkpoint_stat_t
*pcs
= NULL
;
6353 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6354 (void) nvlist_lookup_uint64_array(nvroot
,
6355 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6357 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6360 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
6361 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6369 scrub_callback(zpool_handle_t
*zhp
, void *data
)
6371 scrub_cbdata_t
*cb
= data
;
6375 * Ignore faulted pools.
6377 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
6378 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
6379 "currently unavailable\n"), zpool_get_name(zhp
));
6383 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
6385 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
6386 cb
->cb_type
== POOL_SCAN_SCRUB
) {
6387 (void) printf(gettext("warning: will not scrub state that "
6388 "belongs to the checkpoint of pool '%s'\n"),
6389 zpool_get_name(zhp
));
6396 * zpool scrub [-s | -p] <pool> ...
6398 * -s Stop. Stops any in-progress scrub.
6399 * -p Pause. Pause in-progress scrub.
6402 zpool_do_scrub(int argc
, char **argv
)
6407 cb
.cb_type
= POOL_SCAN_SCRUB
;
6408 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6411 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
6414 cb
.cb_type
= POOL_SCAN_NONE
;
6417 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
6420 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6426 if (cb
.cb_type
== POOL_SCAN_NONE
&&
6427 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
6428 (void) fprintf(stderr
, gettext("invalid option combination: "
6429 "-s and -p are mutually exclusive\n"));
6439 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6443 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
6447 * zpool resilver <pool> ...
6449 * Restarts any in-progress resilver
6452 zpool_do_resilver(int argc
, char **argv
)
6457 cb
.cb_type
= POOL_SCAN_RESILVER
;
6458 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6463 while ((c
= getopt(argc
, argv
, "")) != -1) {
6466 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6476 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6480 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
6485 * Print out detailed scrub status.
6488 print_scan_status(pool_scan_stat_t
*ps
)
6490 time_t start
, end
, pause
;
6491 uint64_t total_secs_left
;
6492 uint64_t elapsed
, secs_left
, mins_left
, hours_left
, days_left
;
6493 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
6494 uint64_t scan_rate
, issue_rate
;
6495 double fraction_done
;
6496 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
6497 char srate_buf
[7], irate_buf
[7];
6499 (void) printf(gettext(" scan: "));
6501 /* If there's never been a scan, there's not much to say. */
6502 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
6503 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
6504 (void) printf(gettext("none requested\n"));
6508 start
= ps
->pss_start_time
;
6509 end
= ps
->pss_end_time
;
6510 pause
= ps
->pss_pass_scrub_pause
;
6512 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
6514 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
6515 ps
->pss_func
== POOL_SCAN_RESILVER
);
6517 /* Scan is finished or canceled. */
6518 if (ps
->pss_state
== DSS_FINISHED
) {
6519 total_secs_left
= end
- start
;
6520 days_left
= total_secs_left
/ 60 / 60 / 24;
6521 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6522 mins_left
= (total_secs_left
/ 60) % 60;
6523 secs_left
= (total_secs_left
% 60);
6525 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6526 (void) printf(gettext("scrub repaired %s "
6527 "in %llu days %02llu:%02llu:%02llu "
6528 "with %llu errors on %s"), processed_buf
,
6529 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6530 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6531 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6532 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6533 (void) printf(gettext("resilvered %s "
6534 "in %llu days %02llu:%02llu:%02llu "
6535 "with %llu errors on %s"), processed_buf
,
6536 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6537 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6538 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6541 } else if (ps
->pss_state
== DSS_CANCELED
) {
6542 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6543 (void) printf(gettext("scrub canceled on %s"),
6545 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6546 (void) printf(gettext("resilver canceled on %s"),
6552 assert(ps
->pss_state
== DSS_SCANNING
);
6554 /* Scan is in progress. Resilvers can't be paused. */
6555 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6557 (void) printf(gettext("scrub in progress since %s"),
6560 (void) printf(gettext("scrub paused since %s"),
6562 (void) printf(gettext("\tscrub started on %s"),
6565 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6566 (void) printf(gettext("resilver in progress since %s"),
6570 scanned
= ps
->pss_examined
;
6571 pass_scanned
= ps
->pss_pass_exam
;
6572 issued
= ps
->pss_issued
;
6573 pass_issued
= ps
->pss_pass_issued
;
6574 total
= ps
->pss_to_examine
;
6576 /* we are only done with a block once we have issued the IO for it */
6577 fraction_done
= (double)issued
/ total
;
6579 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6580 elapsed
= time(NULL
) - ps
->pss_pass_start
;
6581 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
6582 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
6584 scan_rate
= pass_scanned
/ elapsed
;
6585 issue_rate
= pass_issued
/ elapsed
;
6586 total_secs_left
= (issue_rate
!= 0) ?
6587 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
6589 days_left
= total_secs_left
/ 60 / 60 / 24;
6590 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6591 mins_left
= (total_secs_left
/ 60) % 60;
6592 secs_left
= (total_secs_left
% 60);
6594 /* format all of the numbers we will be reporting */
6595 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
6596 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
6597 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
6598 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
6599 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
6601 /* do not print estimated time if we have a paused scrub */
6603 (void) printf(gettext("\t%s scanned at %s/s, "
6604 "%s issued at %s/s, %s total\n"),
6605 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
6607 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6608 scanned_buf
, issued_buf
, total_buf
);
6611 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6612 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6613 processed_buf
, 100 * fraction_done
);
6614 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6615 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6616 processed_buf
, 100 * fraction_done
);
6620 if (issue_rate
>= 10 * 1024 * 1024) {
6621 (void) printf(gettext(", %llu days "
6622 "%02llu:%02llu:%02llu to go\n"),
6623 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6624 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
);
6626 (void) printf(gettext(", no estimated "
6627 "completion time\n"));
6630 (void) printf(gettext("\n"));
6635 * As we don't scrub checkpointed blocks, we want to warn the
6636 * user that we skipped scanning some blocks if a checkpoint exists
6637 * or existed at any time during the scan.
6640 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
6642 if (ps
== NULL
|| pcs
== NULL
)
6645 if (pcs
->pcs_state
== CS_NONE
||
6646 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
6649 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
6651 if (ps
->pss_state
== DSS_NONE
)
6654 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
6655 ps
->pss_end_time
< pcs
->pcs_start_time
)
6658 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
6659 (void) printf(gettext(" scan warning: skipped blocks "
6660 "that are only referenced by the checkpoint.\n"));
6662 assert(ps
->pss_state
== DSS_SCANNING
);
6663 (void) printf(gettext(" scan warning: skipping blocks "
6664 "that are only referenced by the checkpoint.\n"));
6669 * Print out detailed removal status.
6672 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
6674 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
6676 nvlist_t
*config
, *nvroot
;
6681 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
6685 * Determine name of vdev.
6687 config
= zpool_get_config(zhp
, NULL
);
6688 nvroot
= fnvlist_lookup_nvlist(config
,
6689 ZPOOL_CONFIG_VDEV_TREE
);
6690 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
6691 &child
, &children
) == 0);
6692 assert(prs
->prs_removing_vdev
< children
);
6693 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
6694 child
[prs
->prs_removing_vdev
], B_TRUE
);
6696 (void) printf(gettext("remove: "));
6698 start
= prs
->prs_start_time
;
6699 end
= prs
->prs_end_time
;
6700 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
6703 * Removal is finished or canceled.
6705 if (prs
->prs_state
== DSS_FINISHED
) {
6706 uint64_t minutes_taken
= (end
- start
) / 60;
6708 (void) printf(gettext("Removal of vdev %llu copied %s "
6709 "in %lluh%um, completed on %s"),
6710 (longlong_t
)prs
->prs_removing_vdev
,
6712 (u_longlong_t
)(minutes_taken
/ 60),
6713 (uint_t
)(minutes_taken
% 60),
6714 ctime((time_t *)&end
));
6715 } else if (prs
->prs_state
== DSS_CANCELED
) {
6716 (void) printf(gettext("Removal of %s canceled on %s"),
6717 vdev_name
, ctime(&end
));
6719 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
6720 double fraction_done
;
6723 assert(prs
->prs_state
== DSS_SCANNING
);
6726 * Removal is in progress.
6728 (void) printf(gettext(
6729 "Evacuation of %s in progress since %s"),
6730 vdev_name
, ctime(&start
));
6732 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
6733 total
= prs
->prs_to_copy
;
6734 fraction_done
= (double)copied
/ total
;
6736 /* elapsed time for this pass */
6737 elapsed
= time(NULL
) - prs
->prs_start_time
;
6738 elapsed
= elapsed
> 0 ? elapsed
: 1;
6739 rate
= copied
/ elapsed
;
6740 rate
= rate
> 0 ? rate
: 1;
6741 mins_left
= ((total
- copied
) / rate
) / 60;
6742 hours_left
= mins_left
/ 60;
6744 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
6745 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
6746 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
6749 * do not print estimated time if hours_left is more than
6752 (void) printf(gettext(" %s copied out of %s at %s/s, "
6754 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
6755 if (hours_left
< (30 * 24)) {
6756 (void) printf(gettext(", %lluh%um to go\n"),
6757 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
6759 (void) printf(gettext(
6760 ", (copy is slow, no estimated time)\n"));
6764 if (prs
->prs_mapping_memory
> 0) {
6766 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
6767 (void) printf(gettext(" %s memory used for "
6768 "removed device mappings\n"),
6774 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
6779 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6782 (void) printf(gettext("checkpoint: "));
6784 start
= pcs
->pcs_start_time
;
6785 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
6787 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
6788 char *date
= ctime(&start
);
6791 * ctime() adds a newline at the end of the generated
6792 * string, thus the weird format specifier and the
6793 * strlen() call used to chop it off from the output.
6795 (void) printf(gettext("created %.*s, consumes %s\n"),
6796 (int)(strlen(date
) - 1), date
, space_buf
);
6800 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6802 (void) printf(gettext("discarding, %s remaining.\n"),
6807 print_error_log(zpool_handle_t
*zhp
)
6809 nvlist_t
*nverrlist
= NULL
;
6812 size_t len
= MAXPATHLEN
* 2;
6814 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
6817 (void) printf("errors: Permanent errors have been "
6818 "detected in the following files:\n\n");
6820 pathname
= safe_malloc(len
);
6822 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
6824 uint64_t dsobj
, obj
;
6826 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
6827 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
6829 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
6831 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
6832 (void) printf("%7s %s\n", "", pathname
);
6835 nvlist_free(nverrlist
);
6839 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
6848 (void) printf(gettext("\tspares\n"));
6850 for (i
= 0; i
< nspares
; i
++) {
6851 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
6853 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
6859 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
6868 (void) printf(gettext("\tcache\n"));
6870 for (i
= 0; i
< nl2cache
; i
++) {
6871 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
6873 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
6879 print_dedup_stats(nvlist_t
*config
)
6881 ddt_histogram_t
*ddh
;
6885 char dspace
[6], mspace
[6];
6888 * If the pool was faulted then we may not have been able to
6889 * obtain the config. Otherwise, if we have anything in the dedup
6890 * table continue processing the stats.
6892 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
6893 (uint64_t **)&ddo
, &c
) != 0)
6896 (void) printf("\n");
6897 (void) printf(gettext(" dedup: "));
6898 if (ddo
->ddo_count
== 0) {
6899 (void) printf(gettext("no DDT entries\n"));
6903 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
6904 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
6905 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6906 (u_longlong_t
)ddo
->ddo_count
,
6910 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
6911 (uint64_t **)&dds
, &c
) == 0);
6912 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
6913 (uint64_t **)&ddh
, &c
) == 0);
6914 zpool_dump_ddt(dds
, ddh
);
6918 * Display a summary of pool status. Displays a summary such as:
6922 * reason: One or more devices ...
6923 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6929 * When given the '-v' option, we print out the complete config. If the '-e'
6930 * option is specified, then we print out error rate information as well.
6933 status_callback(zpool_handle_t
*zhp
, void *data
)
6935 status_cbdata_t
*cbp
= data
;
6936 nvlist_t
*config
, *nvroot
;
6938 zpool_status_t reason
;
6939 zpool_errata_t errata
;
6944 config
= zpool_get_config(zhp
, NULL
);
6945 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
6950 * If we were given 'zpool status -x', only report those pools with
6953 if (cbp
->cb_explain
&&
6954 (reason
== ZPOOL_STATUS_OK
||
6955 reason
== ZPOOL_STATUS_VERSION_OLDER
||
6956 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6957 if (!cbp
->cb_allpools
) {
6958 (void) printf(gettext("pool '%s' is healthy\n"),
6959 zpool_get_name(zhp
));
6961 cbp
->cb_first
= B_FALSE
;
6967 cbp
->cb_first
= B_FALSE
;
6969 (void) printf("\n");
6971 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6972 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6973 (uint64_t **)&vs
, &c
) == 0);
6975 health
= zpool_get_state_str(zhp
);
6977 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6978 (void) printf(gettext(" state: %s\n"), health
);
6981 case ZPOOL_STATUS_MISSING_DEV_R
:
6982 (void) printf(gettext("status: One or more devices could not "
6983 "be opened. Sufficient replicas exist for\n\tthe pool to "
6984 "continue functioning in a degraded state.\n"));
6985 (void) printf(gettext("action: Attach the missing device and "
6986 "online it using 'zpool online'.\n"));
6989 case ZPOOL_STATUS_MISSING_DEV_NR
:
6990 (void) printf(gettext("status: One or more devices could not "
6991 "be opened. There are insufficient\n\treplicas for the "
6992 "pool to continue functioning.\n"));
6993 (void) printf(gettext("action: Attach the missing device and "
6994 "online it using 'zpool online'.\n"));
6997 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6998 (void) printf(gettext("status: One or more devices could not "
6999 "be used because the label is missing or\n\tinvalid. "
7000 "Sufficient replicas exist for the pool to continue\n\t"
7001 "functioning in a degraded state.\n"));
7002 (void) printf(gettext("action: Replace the device using "
7003 "'zpool replace'.\n"));
7006 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
7007 (void) printf(gettext("status: One or more devices could not "
7008 "be used because the label is missing \n\tor invalid. "
7009 "There are insufficient replicas for the pool to "
7010 "continue\n\tfunctioning.\n"));
7011 zpool_explain_recover(zpool_get_handle(zhp
),
7012 zpool_get_name(zhp
), reason
, config
);
7015 case ZPOOL_STATUS_FAILING_DEV
:
7016 (void) printf(gettext("status: One or more devices has "
7017 "experienced an unrecoverable error. An\n\tattempt was "
7018 "made to correct the error. Applications are "
7020 (void) printf(gettext("action: Determine if the device needs "
7021 "to be replaced, and clear the errors\n\tusing "
7022 "'zpool clear' or replace the device with 'zpool "
7026 case ZPOOL_STATUS_OFFLINE_DEV
:
7027 (void) printf(gettext("status: One or more devices has "
7028 "been taken offline by the administrator.\n\tSufficient "
7029 "replicas exist for the pool to continue functioning in "
7030 "a\n\tdegraded state.\n"));
7031 (void) printf(gettext("action: Online the device using "
7032 "'zpool online' or replace the device with\n\t'zpool "
7036 case ZPOOL_STATUS_REMOVED_DEV
:
7037 (void) printf(gettext("status: One or more devices has "
7038 "been removed by the administrator.\n\tSufficient "
7039 "replicas exist for the pool to continue functioning in "
7040 "a\n\tdegraded state.\n"));
7041 (void) printf(gettext("action: Online the device using "
7042 "'zpool online' or replace the device with\n\t'zpool "
7046 case ZPOOL_STATUS_RESILVERING
:
7047 (void) printf(gettext("status: One or more devices is "
7048 "currently being resilvered. The pool will\n\tcontinue "
7049 "to function, possibly in a degraded state.\n"));
7050 (void) printf(gettext("action: Wait for the resilver to "
7054 case ZPOOL_STATUS_CORRUPT_DATA
:
7055 (void) printf(gettext("status: One or more devices has "
7056 "experienced an error resulting in data\n\tcorruption. "
7057 "Applications may be affected.\n"));
7058 (void) printf(gettext("action: Restore the file in question "
7059 "if possible. Otherwise restore the\n\tentire pool from "
7063 case ZPOOL_STATUS_CORRUPT_POOL
:
7064 (void) printf(gettext("status: The pool metadata is corrupted "
7065 "and the pool cannot be opened.\n"));
7066 zpool_explain_recover(zpool_get_handle(zhp
),
7067 zpool_get_name(zhp
), reason
, config
);
7070 case ZPOOL_STATUS_VERSION_OLDER
:
7071 (void) printf(gettext("status: The pool is formatted using a "
7072 "legacy on-disk format. The pool can\n\tstill be used, "
7073 "but some features are unavailable.\n"));
7074 (void) printf(gettext("action: Upgrade the pool using 'zpool "
7075 "upgrade'. Once this is done, the\n\tpool will no longer "
7076 "be accessible on software that does not support\n\t"
7077 "feature flags.\n"));
7080 case ZPOOL_STATUS_VERSION_NEWER
:
7081 (void) printf(gettext("status: The pool has been upgraded to a "
7082 "newer, incompatible on-disk version.\n\tThe pool cannot "
7083 "be accessed on this system.\n"));
7084 (void) printf(gettext("action: Access the pool from a system "
7085 "running more recent software, or\n\trestore the pool from "
7089 case ZPOOL_STATUS_FEAT_DISABLED
:
7090 (void) printf(gettext("status: Some supported features are not "
7091 "enabled on the pool. The pool can\n\tstill be used, but "
7092 "some features are unavailable.\n"));
7093 (void) printf(gettext("action: Enable all features using "
7094 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
7095 "longer be accessible by software that does not support\n\t"
7096 "the features. See zpool-features(5) for details.\n"));
7099 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
7100 (void) printf(gettext("status: The pool cannot be accessed on "
7101 "this system because it uses the\n\tfollowing feature(s) "
7102 "not supported on this system:\n"));
7103 zpool_print_unsup_feat(config
);
7104 (void) printf("\n");
7105 (void) printf(gettext("action: Access the pool from a system "
7106 "that supports the required feature(s),\n\tor restore the "
7107 "pool from backup.\n"));
7110 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
7111 (void) printf(gettext("status: The pool can only be accessed "
7112 "in read-only mode on this system. It\n\tcannot be "
7113 "accessed in read-write mode because it uses the "
7114 "following\n\tfeature(s) not supported on this system:\n"));
7115 zpool_print_unsup_feat(config
);
7116 (void) printf("\n");
7117 (void) printf(gettext("action: The pool cannot be accessed in "
7118 "read-write mode. Import the pool with\n"
7119 "\t\"-o readonly=on\", access the pool from a system that "
7120 "supports the\n\trequired feature(s), or restore the "
7121 "pool from backup.\n"));
7124 case ZPOOL_STATUS_FAULTED_DEV_R
:
7125 (void) printf(gettext("status: One or more devices are "
7126 "faulted in response to persistent errors.\n\tSufficient "
7127 "replicas exist for the pool to continue functioning "
7128 "in a\n\tdegraded state.\n"));
7129 (void) printf(gettext("action: Replace the faulted device, "
7130 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
7133 case ZPOOL_STATUS_FAULTED_DEV_NR
:
7134 (void) printf(gettext("status: One or more devices are "
7135 "faulted in response to persistent errors. There are "
7136 "insufficient replicas for the pool to\n\tcontinue "
7138 (void) printf(gettext("action: Destroy and re-create the pool "
7139 "from a backup source. Manually marking the device\n"
7140 "\trepaired using 'zpool clear' may allow some data "
7141 "to be recovered.\n"));
7144 case ZPOOL_STATUS_IO_FAILURE_MMP
:
7145 (void) printf(gettext("status: The pool is suspended because "
7146 "multihost writes failed or were delayed;\n\tanother "
7147 "system could import the pool undetected.\n"));
7148 (void) printf(gettext("action: Make sure the pool's devices "
7149 "are connected, then reboot your system and\n\timport the "
7153 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
7154 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
7155 (void) printf(gettext("status: One or more devices are "
7156 "faulted in response to IO failures.\n"));
7157 (void) printf(gettext("action: Make sure the affected devices "
7158 "are connected, then run 'zpool clear'.\n"));
7161 case ZPOOL_STATUS_BAD_LOG
:
7162 (void) printf(gettext("status: An intent log record "
7163 "could not be read.\n"
7164 "\tWaiting for administrator intervention to fix the "
7165 "faulted pool.\n"));
7166 (void) printf(gettext("action: Either restore the affected "
7167 "device(s) and run 'zpool online',\n"
7168 "\tor ignore the intent log records by running "
7169 "'zpool clear'.\n"));
7172 case ZPOOL_STATUS_HOSTID_MISMATCH
:
7173 (void) printf(gettext("status: Mismatch between pool hostid "
7174 "and system hostid on imported pool.\n\tThis pool was "
7175 "previously imported into a system with a different "
7176 "hostid,\n\tand then was verbatim imported into this "
7178 (void) printf(gettext("action: Export this pool on all systems "
7179 "on which it is imported.\n"
7180 "\tThen import it to correct the mismatch.\n"));
7183 case ZPOOL_STATUS_ERRATA
:
7184 (void) printf(gettext("status: Errata #%d detected.\n"),
7188 case ZPOOL_ERRATA_NONE
:
7191 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
7192 (void) printf(gettext("action: To correct the issue "
7193 "run 'zpool scrub'.\n"));
7196 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
7197 (void) printf(gettext("\tExisting encrypted datasets "
7198 "contain an on-disk incompatibility\n\twhich "
7199 "needs to be corrected.\n"));
7200 (void) printf(gettext("action: To correct the issue "
7201 "backup existing encrypted datasets to new\n\t"
7202 "encrypted datasets and destroy the old ones. "
7203 "'zfs mount -o ro' can\n\tbe used to temporarily "
7204 "mount existing encrypted datasets readonly.\n"));
7209 * All errata which allow the pool to be imported
7210 * must contain an action message.
7218 * The remaining errors can't actually be generated, yet.
7220 assert(reason
== ZPOOL_STATUS_OK
);
7224 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
7227 if (config
!= NULL
) {
7229 nvlist_t
**spares
, **l2cache
;
7230 uint_t nspares
, nl2cache
;
7231 pool_checkpoint_stat_t
*pcs
= NULL
;
7232 pool_scan_stat_t
*ps
= NULL
;
7233 pool_removal_stat_t
*prs
= NULL
;
7235 (void) nvlist_lookup_uint64_array(nvroot
,
7236 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7237 (void) nvlist_lookup_uint64_array(nvroot
,
7238 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
7239 (void) nvlist_lookup_uint64_array(nvroot
,
7240 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
7242 print_scan_status(ps
);
7243 print_checkpoint_scan_warning(ps
, pcs
);
7244 print_removal_status(zhp
, prs
);
7245 print_checkpoint_status(pcs
);
7247 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
7248 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
7249 if (cbp
->cb_namewidth
< 10)
7250 cbp
->cb_namewidth
= 10;
7252 (void) printf(gettext("config:\n\n"));
7253 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
7254 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
7257 if (cbp
->cb_print_slow_ios
)
7258 (void) printf(" %5s", gettext("SLOW"));
7260 if (cbp
->vcdl
!= NULL
)
7261 print_cmd_columns(cbp
->vcdl
, 0);
7265 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
7268 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
7269 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
7270 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
7272 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
7273 &l2cache
, &nl2cache
) == 0)
7274 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
7276 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
7277 &spares
, &nspares
) == 0)
7278 print_spares(zhp
, cbp
, spares
, nspares
);
7280 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
7282 nvlist_t
*nverrlist
= NULL
;
7285 * If the approximate error count is small, get a
7286 * precise count by fetching the entire log and
7287 * uniquifying the results.
7289 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
7290 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
7295 while ((elem
= nvlist_next_nvpair(nverrlist
,
7300 nvlist_free(nverrlist
);
7302 (void) printf("\n");
7305 (void) printf(gettext("errors: No known data "
7307 else if (!cbp
->cb_verbose
)
7308 (void) printf(gettext("errors: %llu data "
7309 "errors, use '-v' for a list\n"),
7310 (u_longlong_t
)nerr
);
7312 print_error_log(zhp
);
7315 if (cbp
->cb_dedup_stats
)
7316 print_dedup_stats(config
);
7318 (void) printf(gettext("config: The configuration cannot be "
7326 * zpool status [-c [script1,script2,...]] [-gLpPsvx] [-T d|u] [pool] ...
7327 * [interval [count]]
7329 * -c CMD For each vdev, run command CMD
7330 * -g Display guid for individual vdev name.
7331 * -L Follow links when resolving vdev path name.
7332 * -p Display values in parsable (exact) format.
7333 * -P Display full path for vdev name.
7334 * -s Display slow IOs column.
7335 * -v Display complete error logs
7336 * -x Display only pools with potential problems
7337 * -D Display dedup status (undocumented)
7338 * -T Display a timestamp in date(1) or Unix format
7340 * Describes the health status of all pools or some subset.
7343 zpool_do_status(int argc
, char **argv
)
7348 unsigned long count
= 0;
7349 status_cbdata_t cb
= { 0 };
7353 while ((c
= getopt(argc
, argv
, "c:gLpPsvxDT:")) != -1) {
7358 gettext("Can't set -c flag twice\n"));
7362 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
7363 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
7364 fprintf(stderr
, gettext(
7365 "Can't run -c, disabled by "
7366 "ZPOOL_SCRIPTS_ENABLED.\n"));
7370 if ((getuid() <= 0 || geteuid() <= 0) &&
7371 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
7372 fprintf(stderr
, gettext(
7373 "Can't run -c with root privileges "
7374 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
7380 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
7383 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
7386 cb
.cb_literal
= B_TRUE
;
7389 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
7392 cb
.cb_print_slow_ios
= B_TRUE
;
7395 cb
.cb_verbose
= B_TRUE
;
7398 cb
.cb_explain
= B_TRUE
;
7401 cb
.cb_dedup_stats
= B_TRUE
;
7404 get_timestamp_arg(*optarg
);
7407 if (optopt
== 'c') {
7408 print_zpool_script_list("status");
7412 gettext("invalid option '%c'\n"), optopt
);
7421 get_interval_count(&argc
, argv
, &interval
, &count
);
7424 cb
.cb_allpools
= B_TRUE
;
7426 cb
.cb_first
= B_TRUE
;
7427 cb
.cb_print_status
= B_TRUE
;
7430 if (timestamp_fmt
!= NODATE
)
7431 print_timestamp(timestamp_fmt
);
7434 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
7437 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
7438 status_callback
, &cb
);
7440 if (cb
.vcdl
!= NULL
)
7441 free_vdev_cmd_data_list(cb
.vcdl
);
7443 if (argc
== 0 && cb
.cb_count
== 0)
7444 (void) fprintf(stderr
, gettext("no pools available\n"));
7445 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
7446 (void) printf(gettext("all pools are healthy\n"));
7454 if (count
!= 0 && --count
== 0)
7457 (void) fsleep(interval
);
7463 typedef struct upgrade_cbdata
{
7466 uint64_t cb_version
;
7471 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
7473 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
7474 int *count
= (int *)unsupp_fs
;
7476 if (zfs_version
> ZPL_VERSION
) {
7477 (void) printf(gettext("%s (v%d) is not supported by this "
7478 "implementation of ZFS.\n"),
7479 zfs_get_name(zhp
), zfs_version
);
7483 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
7491 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
7495 uint64_t oldversion
;
7498 config
= zpool_get_config(zhp
, NULL
);
7499 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7502 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
7503 assert(oldversion
< version
);
7505 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
7510 (void) fprintf(stderr
, gettext("Upgrade not performed due "
7511 "to %d unsupported filesystems (max v%d).\n"),
7512 unsupp_fs
, (int)ZPL_VERSION
);
7516 ret
= zpool_upgrade(zhp
, version
);
7520 if (version
>= SPA_VERSION_FEATURES
) {
7521 (void) printf(gettext("Successfully upgraded "
7522 "'%s' from version %llu to feature flags.\n"),
7523 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
7525 (void) printf(gettext("Successfully upgraded "
7526 "'%s' from version %llu to version %llu.\n"),
7527 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
7528 (u_longlong_t
)version
);
7535 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
7538 boolean_t firstff
= B_TRUE
;
7539 nvlist_t
*enabled
= zpool_get_features(zhp
);
7542 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7543 const char *fname
= spa_feature_table
[i
].fi_uname
;
7544 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7545 if (!nvlist_exists(enabled
, fguid
)) {
7547 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
7548 ret
= zpool_set_prop(zhp
, propname
,
7549 ZFS_FEATURE_ENABLED
);
7557 (void) printf(gettext("Enabled the "
7558 "following features on '%s':\n"),
7559 zpool_get_name(zhp
));
7562 (void) printf(gettext(" %s\n"), fname
);
7573 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
7575 upgrade_cbdata_t
*cbp
= arg
;
7578 boolean_t printnl
= B_FALSE
;
7581 config
= zpool_get_config(zhp
, NULL
);
7582 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7585 assert(SPA_VERSION_IS_SUPPORTED(version
));
7587 if (version
< cbp
->cb_version
) {
7588 cbp
->cb_first
= B_FALSE
;
7589 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7595 * If they did "zpool upgrade -a", then we could
7596 * be doing ioctls to different pools. We need
7597 * to log this history once to each pool, and bypass
7598 * the normal history logging that happens in main().
7600 (void) zpool_log_history(g_zfs
, history_str
);
7601 log_history
= B_FALSE
;
7604 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7606 ret
= upgrade_enable_all(zhp
, &count
);
7611 cbp
->cb_first
= B_FALSE
;
7617 (void) printf(gettext("\n"));
7624 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
7626 upgrade_cbdata_t
*cbp
= arg
;
7630 config
= zpool_get_config(zhp
, NULL
);
7631 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7634 assert(SPA_VERSION_IS_SUPPORTED(version
));
7636 if (version
< SPA_VERSION_FEATURES
) {
7637 if (cbp
->cb_first
) {
7638 (void) printf(gettext("The following pools are "
7639 "formatted with legacy version numbers and can\n"
7640 "be upgraded to use feature flags. After "
7641 "being upgraded, these pools\nwill no "
7642 "longer be accessible by software that does not "
7643 "support feature\nflags.\n\n"));
7644 (void) printf(gettext("VER POOL\n"));
7645 (void) printf(gettext("--- ------------\n"));
7646 cbp
->cb_first
= B_FALSE
;
7649 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
7650 zpool_get_name(zhp
));
7657 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
7659 upgrade_cbdata_t
*cbp
= arg
;
7663 config
= zpool_get_config(zhp
, NULL
);
7664 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7667 if (version
>= SPA_VERSION_FEATURES
) {
7669 boolean_t poolfirst
= B_TRUE
;
7670 nvlist_t
*enabled
= zpool_get_features(zhp
);
7672 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7673 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7674 const char *fname
= spa_feature_table
[i
].fi_uname
;
7675 if (!nvlist_exists(enabled
, fguid
)) {
7676 if (cbp
->cb_first
) {
7677 (void) printf(gettext("\nSome "
7678 "supported features are not "
7679 "enabled on the following pools. "
7680 "Once a\nfeature is enabled the "
7681 "pool may become incompatible with "
7682 "software\nthat does not support "
7684 "zpool-features(5) for "
7686 (void) printf(gettext("POOL "
7688 (void) printf(gettext("------"
7690 cbp
->cb_first
= B_FALSE
;
7694 (void) printf(gettext("%s\n"),
7695 zpool_get_name(zhp
));
7696 poolfirst
= B_FALSE
;
7699 (void) printf(gettext(" %s\n"), fname
);
7702 * If they did "zpool upgrade -a", then we could
7703 * be doing ioctls to different pools. We need
7704 * to log this history once to each pool, and bypass
7705 * the normal history logging that happens in main().
7707 (void) zpool_log_history(g_zfs
, history_str
);
7708 log_history
= B_FALSE
;
7717 upgrade_one(zpool_handle_t
*zhp
, void *data
)
7719 boolean_t printnl
= B_FALSE
;
7720 upgrade_cbdata_t
*cbp
= data
;
7721 uint64_t cur_version
;
7724 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
7725 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
7726 "Pool 'log' must be renamed using export and import"
7731 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
7732 if (cur_version
> cbp
->cb_version
) {
7733 (void) printf(gettext("Pool '%s' is already formatted "
7734 "using more current version '%llu'.\n\n"),
7735 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
7739 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
7740 (void) printf(gettext("Pool '%s' is already formatted "
7741 "using version %llu.\n\n"), zpool_get_name(zhp
),
7742 (u_longlong_t
)cbp
->cb_version
);
7746 if (cur_version
!= cbp
->cb_version
) {
7748 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7753 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7755 ret
= upgrade_enable_all(zhp
, &count
);
7761 } else if (cur_version
== SPA_VERSION
) {
7762 (void) printf(gettext("Pool '%s' already has all "
7763 "supported features enabled.\n"),
7764 zpool_get_name(zhp
));
7769 (void) printf(gettext("\n"));
7778 * zpool upgrade [-V version] <-a | pool ...>
7780 * With no arguments, display downrev'd ZFS pool available for upgrade.
7781 * Individual pools can be upgraded by specifying the pool, and '-a' will
7782 * upgrade all pools.
7785 zpool_do_upgrade(int argc
, char **argv
)
7788 upgrade_cbdata_t cb
= { 0 };
7790 boolean_t showversions
= B_FALSE
;
7791 boolean_t upgradeall
= B_FALSE
;
7796 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
7799 upgradeall
= B_TRUE
;
7802 showversions
= B_TRUE
;
7805 cb
.cb_version
= strtoll(optarg
, &end
, 10);
7807 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
7808 (void) fprintf(stderr
,
7809 gettext("invalid version '%s'\n"), optarg
);
7814 (void) fprintf(stderr
, gettext("missing argument for "
7815 "'%c' option\n"), optopt
);
7819 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7830 if (cb
.cb_version
== 0) {
7831 cb
.cb_version
= SPA_VERSION
;
7832 } else if (!upgradeall
&& argc
== 0) {
7833 (void) fprintf(stderr
, gettext("-V option is "
7834 "incompatible with other arguments\n"));
7839 if (upgradeall
|| argc
!= 0) {
7840 (void) fprintf(stderr
, gettext("-v option is "
7841 "incompatible with other arguments\n"));
7844 } else if (upgradeall
) {
7846 (void) fprintf(stderr
, gettext("-a option should not "
7847 "be used along with a pool name\n"));
7852 (void) printf(gettext("This system supports ZFS pool feature "
7857 (void) printf(gettext("The following features are "
7859 (void) printf(gettext("FEAT DESCRIPTION\n"));
7860 (void) printf("----------------------------------------------"
7861 "---------------\n");
7862 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7863 zfeature_info_t
*fi
= &spa_feature_table
[i
];
7865 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
7866 " (read-only compatible)" : "";
7868 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
7869 (void) printf(" %s\n", fi
->fi_desc
);
7871 (void) printf("\n");
7873 (void) printf(gettext("The following legacy versions are also "
7875 (void) printf(gettext("VER DESCRIPTION\n"));
7876 (void) printf("--- -----------------------------------------"
7877 "---------------\n");
7878 (void) printf(gettext(" 1 Initial ZFS version\n"));
7879 (void) printf(gettext(" 2 Ditto blocks "
7880 "(replicated metadata)\n"));
7881 (void) printf(gettext(" 3 Hot spares and double parity "
7883 (void) printf(gettext(" 4 zpool history\n"));
7884 (void) printf(gettext(" 5 Compression using the gzip "
7886 (void) printf(gettext(" 6 bootfs pool property\n"));
7887 (void) printf(gettext(" 7 Separate intent log devices\n"));
7888 (void) printf(gettext(" 8 Delegated administration\n"));
7889 (void) printf(gettext(" 9 refquota and refreservation "
7891 (void) printf(gettext(" 10 Cache devices\n"));
7892 (void) printf(gettext(" 11 Improved scrub performance\n"));
7893 (void) printf(gettext(" 12 Snapshot properties\n"));
7894 (void) printf(gettext(" 13 snapused property\n"));
7895 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7896 (void) printf(gettext(" 15 user/group space accounting\n"));
7897 (void) printf(gettext(" 16 stmf property support\n"));
7898 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7899 (void) printf(gettext(" 18 Snapshot user holds\n"));
7900 (void) printf(gettext(" 19 Log device removal\n"));
7901 (void) printf(gettext(" 20 Compression using zle "
7902 "(zero-length encoding)\n"));
7903 (void) printf(gettext(" 21 Deduplication\n"));
7904 (void) printf(gettext(" 22 Received properties\n"));
7905 (void) printf(gettext(" 23 Slim ZIL\n"));
7906 (void) printf(gettext(" 24 System attributes\n"));
7907 (void) printf(gettext(" 25 Improved scrub stats\n"));
7908 (void) printf(gettext(" 26 Improved snapshot deletion "
7910 (void) printf(gettext(" 27 Improved snapshot creation "
7912 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7913 (void) printf(gettext("\nFor more information on a particular "
7914 "version, including supported releases,\n"));
7915 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7916 } else if (argc
== 0 && upgradeall
) {
7917 cb
.cb_first
= B_TRUE
;
7918 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
7919 if (ret
== 0 && cb
.cb_first
) {
7920 if (cb
.cb_version
== SPA_VERSION
) {
7921 (void) printf(gettext("All pools are already "
7922 "formatted using feature flags.\n\n"));
7923 (void) printf(gettext("Every feature flags "
7924 "pool already has all supported features "
7927 (void) printf(gettext("All pools are already "
7928 "formatted with version %llu or higher.\n"),
7929 (u_longlong_t
)cb
.cb_version
);
7932 } else if (argc
== 0) {
7933 cb
.cb_first
= B_TRUE
;
7934 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
7938 (void) printf(gettext("All pools are formatted "
7939 "using feature flags.\n\n"));
7941 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7942 "for a list of available legacy versions.\n"));
7945 cb
.cb_first
= B_TRUE
;
7946 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
7950 (void) printf(gettext("Every feature flags pool has "
7951 "all supported features enabled.\n"));
7953 (void) printf(gettext("\n"));
7956 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
7963 typedef struct hist_cbdata
{
7970 * Print out the command history for a specific pool.
7973 get_history_one(zpool_handle_t
*zhp
, void *data
)
7979 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
7981 cb
->first
= B_FALSE
;
7983 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
7985 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
7988 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
7989 &records
, &numrecords
) == 0);
7990 for (i
= 0; i
< numrecords
; i
++) {
7991 nvlist_t
*rec
= records
[i
];
7994 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
7998 tsec
= fnvlist_lookup_uint64(records
[i
],
8000 (void) localtime_r(&tsec
, &t
);
8001 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
8004 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
8005 (void) printf("%s %s", tbuf
,
8006 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
8007 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
8009 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
8012 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
8013 (void) printf("%s unrecognized record:\n",
8015 dump_nvlist(rec
, 4);
8018 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
8019 zfs_history_event_names
[ievent
],
8020 (longlong_t
)fnvlist_lookup_uint64(
8021 rec
, ZPOOL_HIST_TXG
),
8022 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
8023 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
8026 (void) printf("%s [txg:%lld] %s", tbuf
,
8027 (longlong_t
)fnvlist_lookup_uint64(
8028 rec
, ZPOOL_HIST_TXG
),
8029 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
8030 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
8031 (void) printf(" %s (%llu)",
8032 fnvlist_lookup_string(rec
,
8034 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
8037 (void) printf(" %s", fnvlist_lookup_string(rec
,
8038 ZPOOL_HIST_INT_STR
));
8039 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
8042 (void) printf("%s ioctl %s\n", tbuf
,
8043 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
8044 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
8045 (void) printf(" input:\n");
8046 dump_nvlist(fnvlist_lookup_nvlist(rec
,
8047 ZPOOL_HIST_INPUT_NVL
), 8);
8049 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
8050 (void) printf(" output:\n");
8051 dump_nvlist(fnvlist_lookup_nvlist(rec
,
8052 ZPOOL_HIST_OUTPUT_NVL
), 8);
8054 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
8055 (void) printf(" errno: %lld\n",
8056 (longlong_t
)fnvlist_lookup_int64(rec
,
8062 (void) printf("%s unrecognized record:\n", tbuf
);
8063 dump_nvlist(rec
, 4);
8067 (void) printf("\n");
8070 (void) printf(" [");
8071 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
8072 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
8073 struct passwd
*pwd
= getpwuid(who
);
8074 (void) printf("user %d ", (int)who
);
8076 (void) printf("(%s) ", pwd
->pw_name
);
8078 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
8079 (void) printf("on %s",
8080 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
8082 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
8083 (void) printf(":%s",
8084 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
8088 (void) printf("\n");
8090 (void) printf("\n");
8097 * zpool history <pool>
8099 * Displays the history of commands that modified pools.
8102 zpool_do_history(int argc
, char **argv
)
8104 hist_cbdata_t cbdata
= { 0 };
8108 cbdata
.first
= B_TRUE
;
8110 while ((c
= getopt(argc
, argv
, "li")) != -1) {
8113 cbdata
.longfmt
= B_TRUE
;
8116 cbdata
.internal
= B_TRUE
;
8119 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8127 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
8130 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
8131 (void) fprintf(stderr
, gettext("no pools available\n"));
8138 typedef struct ev_opts
{
8143 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
8147 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
8149 char ctime_str
[26], str
[32], *ptr
;
8153 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
8154 memset(str
, ' ', 32);
8155 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
8156 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
8157 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
8158 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
8159 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
8161 (void) printf(gettext("%s\t"), str
);
8163 (void) printf(gettext("%s "), str
);
8165 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
8166 (void) printf(gettext("%s\n"), ptr
);
8170 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
8174 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
8175 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
8177 data_type_t type
= nvpair_type(nvp
);
8178 const char *name
= nvpair_name(nvp
);
8188 printf(gettext("%*s%s = "), depth
, "", name
);
8191 case DATA_TYPE_BOOLEAN
:
8192 printf(gettext("%s"), "1");
8195 case DATA_TYPE_BOOLEAN_VALUE
:
8196 (void) nvpair_value_boolean_value(nvp
, &b
);
8197 printf(gettext("%s"), b
? "1" : "0");
8200 case DATA_TYPE_BYTE
:
8201 (void) nvpair_value_byte(nvp
, &i8
);
8202 printf(gettext("0x%x"), i8
);
8205 case DATA_TYPE_INT8
:
8206 (void) nvpair_value_int8(nvp
, (void *)&i8
);
8207 printf(gettext("0x%x"), i8
);
8210 case DATA_TYPE_UINT8
:
8211 (void) nvpair_value_uint8(nvp
, &i8
);
8212 printf(gettext("0x%x"), i8
);
8215 case DATA_TYPE_INT16
:
8216 (void) nvpair_value_int16(nvp
, (void *)&i16
);
8217 printf(gettext("0x%x"), i16
);
8220 case DATA_TYPE_UINT16
:
8221 (void) nvpair_value_uint16(nvp
, &i16
);
8222 printf(gettext("0x%x"), i16
);
8225 case DATA_TYPE_INT32
:
8226 (void) nvpair_value_int32(nvp
, (void *)&i32
);
8227 printf(gettext("0x%x"), i32
);
8230 case DATA_TYPE_UINT32
:
8231 (void) nvpair_value_uint32(nvp
, &i32
);
8232 printf(gettext("0x%x"), i32
);
8235 case DATA_TYPE_INT64
:
8236 (void) nvpair_value_int64(nvp
, (void *)&i64
);
8237 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
8240 case DATA_TYPE_UINT64
:
8241 (void) nvpair_value_uint64(nvp
, &i64
);
8243 * translate vdev state values to readable
8244 * strings to aide zpool events consumers
8247 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
8249 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
8250 printf(gettext("\"%s\" (0x%llx)"),
8251 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
8254 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
8258 case DATA_TYPE_HRTIME
:
8259 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
8260 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
8263 case DATA_TYPE_STRING
:
8264 (void) nvpair_value_string(nvp
, &str
);
8265 printf(gettext("\"%s\""), str
? str
: "<NULL>");
8268 case DATA_TYPE_NVLIST
:
8269 printf(gettext("(embedded nvlist)\n"));
8270 (void) nvpair_value_nvlist(nvp
, &cnv
);
8271 zpool_do_events_nvprint(cnv
, depth
+ 8);
8272 printf(gettext("%*s(end %s)"), depth
, "", name
);
8275 case DATA_TYPE_NVLIST_ARRAY
: {
8279 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
8280 printf(gettext("(%d embedded nvlists)\n"), nelem
);
8281 for (i
= 0; i
< nelem
; i
++) {
8282 printf(gettext("%*s%s[%d] = %s\n"),
8283 depth
, "", name
, i
, "(embedded nvlist)");
8284 zpool_do_events_nvprint(val
[i
], depth
+ 8);
8285 printf(gettext("%*s(end %s[%i])\n"),
8286 depth
, "", name
, i
);
8288 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
8292 case DATA_TYPE_INT8_ARRAY
: {
8296 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
8297 for (i
= 0; i
< nelem
; i
++)
8298 printf(gettext("0x%x "), val
[i
]);
8303 case DATA_TYPE_UINT8_ARRAY
: {
8307 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
8308 for (i
= 0; i
< nelem
; i
++)
8309 printf(gettext("0x%x "), val
[i
]);
8314 case DATA_TYPE_INT16_ARRAY
: {
8318 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
8319 for (i
= 0; i
< nelem
; i
++)
8320 printf(gettext("0x%x "), val
[i
]);
8325 case DATA_TYPE_UINT16_ARRAY
: {
8329 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
8330 for (i
= 0; i
< nelem
; i
++)
8331 printf(gettext("0x%x "), val
[i
]);
8336 case DATA_TYPE_INT32_ARRAY
: {
8340 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
8341 for (i
= 0; i
< nelem
; i
++)
8342 printf(gettext("0x%x "), val
[i
]);
8347 case DATA_TYPE_UINT32_ARRAY
: {
8351 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
8352 for (i
= 0; i
< nelem
; i
++)
8353 printf(gettext("0x%x "), val
[i
]);
8358 case DATA_TYPE_INT64_ARRAY
: {
8362 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
8363 for (i
= 0; i
< nelem
; i
++)
8364 printf(gettext("0x%llx "),
8365 (u_longlong_t
)val
[i
]);
8370 case DATA_TYPE_UINT64_ARRAY
: {
8374 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
8375 for (i
= 0; i
< nelem
; i
++)
8376 printf(gettext("0x%llx "),
8377 (u_longlong_t
)val
[i
]);
8382 case DATA_TYPE_STRING_ARRAY
: {
8386 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
8387 for (i
= 0; i
< nelem
; i
++)
8388 printf(gettext("\"%s\" "),
8389 str
[i
] ? str
[i
] : "<NULL>");
8394 case DATA_TYPE_BOOLEAN_ARRAY
:
8395 case DATA_TYPE_BYTE_ARRAY
:
8396 case DATA_TYPE_DOUBLE
:
8397 case DATA_TYPE_DONTCARE
:
8398 case DATA_TYPE_UNKNOWN
:
8399 printf(gettext("<unknown>"));
8403 printf(gettext("\n"));
8408 zpool_do_events_next(ev_opts_t
*opts
)
8411 int zevent_fd
, ret
, dropped
;
8414 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
8415 VERIFY(zevent_fd
>= 0);
8417 if (!opts
->scripted
)
8418 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
8421 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
8422 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
8423 if (ret
|| nvl
== NULL
)
8427 (void) printf(gettext("dropped %d events\n"), dropped
);
8429 if (strlen(opts
->poolname
) > 0 &&
8430 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
8431 strcmp(opts
->poolname
, pool
) != 0)
8434 zpool_do_events_short(nvl
, opts
);
8436 if (opts
->verbose
) {
8437 zpool_do_events_nvprint(nvl
, 8);
8438 printf(gettext("\n"));
8440 (void) fflush(stdout
);
8445 VERIFY(0 == close(zevent_fd
));
8451 zpool_do_events_clear(ev_opts_t
*opts
)
8455 ret
= zpool_events_clear(g_zfs
, &count
);
8457 (void) printf(gettext("cleared %d events\n"), count
);
8463 * zpool events [-vHf [pool] | -c]
8465 * Displays events logs by ZFS.
8468 zpool_do_events(int argc
, char **argv
)
8470 ev_opts_t opts
= { 0 };
8475 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
8490 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8499 (void) fprintf(stderr
, gettext("too many arguments\n"));
8501 } else if (argc
== 1) {
8502 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
8503 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
8504 (void) fprintf(stderr
,
8505 gettext("invalid pool name '%s'\n"), opts
.poolname
);
8510 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
8512 (void) fprintf(stderr
,
8513 gettext("invalid options combined with -c\n"));
8518 ret
= zpool_do_events_clear(&opts
);
8520 ret
= zpool_do_events_next(&opts
);
8526 get_callback(zpool_handle_t
*zhp
, void *data
)
8528 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
8529 char value
[MAXNAMELEN
];
8530 zprop_source_t srctype
;
8533 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
8536 * Skip the special fake placeholder. This will also skip
8537 * over the name property when 'all' is specified.
8539 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
8540 pl
== cbp
->cb_proplist
)
8543 if (pl
->pl_prop
== ZPROP_INVAL
&&
8544 (zpool_prop_feature(pl
->pl_user_prop
) ||
8545 zpool_prop_unsupported(pl
->pl_user_prop
))) {
8546 srctype
= ZPROP_SRC_LOCAL
;
8548 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
8549 value
, sizeof (value
)) == 0) {
8550 zprop_print_one_property(zpool_get_name(zhp
),
8551 cbp
, pl
->pl_user_prop
, value
, srctype
,
8555 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
8556 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
8559 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
8560 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
8568 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8570 * -H Scripted mode. Don't display headers, and separate properties
8572 * -o List of columns to display. Defaults to
8573 * "name,property,value,source".
8574 * -p Display values in parsable (exact) format.
8576 * Get properties of pools in the system. Output space statistics
8577 * for each one as well as other attributes.
8580 zpool_do_get(int argc
, char **argv
)
8582 zprop_get_cbdata_t cb
= { 0 };
8583 zprop_list_t fake_name
= { 0 };
8588 cb
.cb_first
= B_TRUE
;
8591 * Set up default columns and sources.
8593 cb
.cb_sources
= ZPROP_SRC_ALL
;
8594 cb
.cb_columns
[0] = GET_COL_NAME
;
8595 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8596 cb
.cb_columns
[2] = GET_COL_VALUE
;
8597 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8598 cb
.cb_type
= ZFS_TYPE_POOL
;
8601 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
8604 cb
.cb_literal
= B_TRUE
;
8607 cb
.cb_scripted
= B_TRUE
;
8610 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
8612 while (*optarg
!= '\0') {
8613 static char *col_subopts
[] =
8614 { "name", "property", "value", "source",
8617 if (i
== ZFS_GET_NCOLS
) {
8618 (void) fprintf(stderr
, gettext("too "
8619 "many fields given to -o "
8624 switch (getsubopt(&optarg
, col_subopts
,
8627 cb
.cb_columns
[i
++] = GET_COL_NAME
;
8630 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
8633 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
8636 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
8640 (void) fprintf(stderr
,
8641 gettext("\"all\" conflicts "
8642 "with specific fields "
8643 "given to -o option\n"));
8646 cb
.cb_columns
[0] = GET_COL_NAME
;
8647 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8648 cb
.cb_columns
[2] = GET_COL_VALUE
;
8649 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8653 (void) fprintf(stderr
,
8654 gettext("invalid column name "
8661 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8671 (void) fprintf(stderr
, gettext("missing property "
8676 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
8677 ZFS_TYPE_POOL
) != 0)
8683 if (cb
.cb_proplist
!= NULL
) {
8684 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
8685 fake_name
.pl_width
= strlen(gettext("NAME"));
8686 fake_name
.pl_next
= cb
.cb_proplist
;
8687 cb
.cb_proplist
= &fake_name
;
8690 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
8693 if (cb
.cb_proplist
== &fake_name
)
8694 zprop_free_list(fake_name
.pl_next
);
8696 zprop_free_list(cb
.cb_proplist
);
8701 typedef struct set_cbdata
{
8704 boolean_t cb_any_successful
;
8708 set_callback(zpool_handle_t
*zhp
, void *data
)
8711 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
8713 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
8716 cb
->cb_any_successful
= B_TRUE
;
8722 zpool_do_set(int argc
, char **argv
)
8724 set_cbdata_t cb
= { 0 };
8727 if (argc
> 1 && argv
[1][0] == '-') {
8728 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8734 (void) fprintf(stderr
, gettext("missing property=value "
8740 (void) fprintf(stderr
, gettext("missing pool name\n"));
8745 (void) fprintf(stderr
, gettext("too many pool names\n"));
8749 cb
.cb_propname
= argv
[1];
8750 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
8751 if (cb
.cb_value
== NULL
) {
8752 (void) fprintf(stderr
, gettext("missing value in "
8753 "property=value argument\n"));
8757 *(cb
.cb_value
) = '\0';
8760 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
8767 find_command_idx(char *command
, int *idx
)
8771 for (i
= 0; i
< NCOMMAND
; i
++) {
8772 if (command_table
[i
].name
== NULL
)
8775 if (strcmp(command
, command_table
[i
].name
) == 0) {
8784 main(int argc
, char **argv
)
8791 (void) setlocale(LC_ALL
, "");
8792 (void) textdomain(TEXT_DOMAIN
);
8798 * Make sure the user has specified some command.
8801 (void) fprintf(stderr
, gettext("missing command\n"));
8810 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
8813 if ((g_zfs
= libzfs_init()) == NULL
) {
8814 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
8818 libzfs_print_on_error(g_zfs
, B_TRUE
);
8820 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8823 * Many commands modify input strings for string parsing reasons.
8824 * We create a copy to protect the original argv.
8826 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
8827 for (i
= 0; i
< argc
; i
++)
8828 newargv
[i
] = strdup(argv
[i
]);
8829 newargv
[argc
] = NULL
;
8832 * Run the appropriate command.
8834 if (find_command_idx(cmdname
, &i
) == 0) {
8835 current_command
= &command_table
[i
];
8836 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
8837 } else if (strchr(cmdname
, '=')) {
8838 verify(find_command_idx("set", &i
) == 0);
8839 current_command
= &command_table
[i
];
8840 ret
= command_table
[i
].func(argc
, newargv
);
8841 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
8843 * 'freeze' is a vile debugging abomination, so we treat
8846 zfs_cmd_t zc
= {"\0"};
8848 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
8849 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
8851 (void) fprintf(stderr
,
8852 gettext("failed to freeze pool: %d\n"), errno
);
8858 (void) fprintf(stderr
, gettext("unrecognized "
8859 "command '%s'\n"), cmdname
);
8864 for (i
= 0; i
< argc
; i
++)
8868 if (ret
== 0 && log_history
)
8869 (void) zpool_log_history(g_zfs
, history_str
);
8874 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8875 * for the purposes of running ::findleaks.
8877 if (getenv("ZFS_ABORT") != NULL
) {
8878 (void) printf("dumping core by request\n");