4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
49 #include <sys/fs/zfs.h>
51 #include <sys/fm/fs/zfs.h>
52 #include <sys/fm/util.h>
53 #include <sys/fm/protocol.h>
54 #include <sys/zfs_ioctl.h>
59 #include "zpool_util.h"
60 #include "zfs_comutil.h"
61 #include "zfeature_common.h"
63 #include "statcommon.h"
65 static int zpool_do_create(int, char **);
66 static int zpool_do_destroy(int, char **);
68 static int zpool_do_add(int, char **);
69 static int zpool_do_remove(int, char **);
70 static int zpool_do_labelclear(int, char **);
72 static int zpool_do_list(int, char **);
73 static int zpool_do_iostat(int, char **);
74 static int zpool_do_status(int, char **);
76 static int zpool_do_online(int, char **);
77 static int zpool_do_offline(int, char **);
78 static int zpool_do_clear(int, char **);
79 static int zpool_do_reopen(int, char **);
81 static int zpool_do_reguid(int, char **);
83 static int zpool_do_attach(int, char **);
84 static int zpool_do_detach(int, char **);
85 static int zpool_do_replace(int, char **);
86 static int zpool_do_split(int, char **);
88 static int zpool_do_scrub(int, char **);
90 static int zpool_do_import(int, char **);
91 static int zpool_do_export(int, char **);
93 static int zpool_do_upgrade(int, char **);
95 static int zpool_do_history(int, char **);
96 static int zpool_do_events(int, char **);
98 static int zpool_do_get(int, char **);
99 static int zpool_do_set(int, char **);
102 * These libumem hooks provide a reasonable set of defaults for the allocator's
103 * debugging facilities.
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
150 * Flags for stats to display with "zpool iostats"
158 IOS_COUNT
, /* always last element */
161 /* iostat_type entries as bitmasks */
162 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
163 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
164 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
165 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
166 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
168 /* Mask of all the histo bits */
169 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
172 * Lookup table for iostat flags to nvlist names. Basically a list
173 * of all the nvlists a flag requires. Also specifies the order in
174 * which data gets printed in zpool iostat.
176 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
178 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
179 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
180 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
181 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
182 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
183 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
184 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
185 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
186 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
189 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
190 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
191 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
192 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
195 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
196 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
197 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
198 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
199 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
202 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
203 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
204 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
205 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
206 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
207 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
208 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
209 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
210 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
211 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
217 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
218 * Right now, only one histo bit is ever set at one time, so we can
219 * just do a highbit64(a)
221 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
223 typedef struct zpool_command
{
225 int (*func
)(int, char **);
230 * Master command table. Each ZFS command has a name, associated function, and
231 * usage message. The usage messages need to be internationalized, so we have
232 * to have a function to return the usage message based on a command index.
234 * These commands are organized according to how they are displayed in the usage
235 * message. An empty command (one with a NULL name) indicates an empty line in
236 * the generic usage message.
238 static zpool_command_t command_table
[] = {
239 { "create", zpool_do_create
, HELP_CREATE
},
240 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
242 { "add", zpool_do_add
, HELP_ADD
},
243 { "remove", zpool_do_remove
, HELP_REMOVE
},
245 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
247 { "list", zpool_do_list
, HELP_LIST
},
248 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
249 { "status", zpool_do_status
, HELP_STATUS
},
251 { "online", zpool_do_online
, HELP_ONLINE
},
252 { "offline", zpool_do_offline
, HELP_OFFLINE
},
253 { "clear", zpool_do_clear
, HELP_CLEAR
},
254 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
256 { "attach", zpool_do_attach
, HELP_ATTACH
},
257 { "detach", zpool_do_detach
, HELP_DETACH
},
258 { "replace", zpool_do_replace
, HELP_REPLACE
},
259 { "split", zpool_do_split
, HELP_SPLIT
},
261 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
263 { "import", zpool_do_import
, HELP_IMPORT
},
264 { "export", zpool_do_export
, HELP_EXPORT
},
265 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
266 { "reguid", zpool_do_reguid
, HELP_REGUID
},
268 { "history", zpool_do_history
, HELP_HISTORY
},
269 { "events", zpool_do_events
, HELP_EVENTS
},
271 { "get", zpool_do_get
, HELP_GET
},
272 { "set", zpool_do_set
, HELP_SET
},
275 #define NCOMMAND (ARRAY_SIZE(command_table))
277 static zpool_command_t
*current_command
;
278 static char history_str
[HIS_MAX_RECORD_LEN
];
279 static boolean_t log_history
= B_TRUE
;
280 static uint_t timestamp_fmt
= NODATE
;
283 get_usage(zpool_help_t idx
)
287 return (gettext("\tadd [-fgLnP] [-o property=value] "
288 "<pool> <vdev> ...\n"));
290 return (gettext("\tattach [-f] [-o property=value] "
291 "<pool> <device> <new-device>\n"));
293 return (gettext("\tclear [-nF] <pool> [device]\n"));
295 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
296 "\t [-O file-system-property=value] ... \n"
297 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
299 return (gettext("\tdestroy [-f] <pool>\n"));
301 return (gettext("\tdetach <pool> <device>\n"));
303 return (gettext("\texport [-af] <pool> ...\n"));
305 return (gettext("\thistory [-il] [<pool>] ...\n"));
307 return (gettext("\timport [-d dir] [-D]\n"
308 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
309 "\timport [-o mntopts] [-o property=value] ... \n"
310 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
311 "[-R root] [-F [-n]] -a\n"
312 "\timport [-o mntopts] [-o property=value] ... \n"
313 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
314 "[-R root] [-F [-n]]\n"
315 "\t <pool | id> [newpool]\n"));
317 return (gettext("\tiostat [-c CMD] [-T d | u] [-ghHLpPvy] "
319 "\t [[pool ...]|[pool vdev ...]|[vdev ...]] "
320 "[interval [count]]\n"));
321 case HELP_LABELCLEAR
:
322 return (gettext("\tlabelclear [-f] <vdev>\n"));
324 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
325 "[-T d|u] [pool] ... [interval [count]]\n"));
327 return (gettext("\toffline [-t] <pool> <device> ...\n"));
329 return (gettext("\tonline <pool> <device> ...\n"));
331 return (gettext("\treplace [-f] [-o property=value] "
332 "<pool> <device> [new-device]\n"));
334 return (gettext("\tremove <pool> <device> ...\n"));
336 return (gettext("\treopen <pool>\n"));
338 return (gettext("\tscrub [-s] <pool> ...\n"));
340 return (gettext("\tstatus [-c CMD] [-gLPvxD] [-T d|u] [pool]"
341 " ... [interval [count]]\n"));
343 return (gettext("\tupgrade\n"
345 "\tupgrade [-V version] <-a | pool ...>\n"));
347 return (gettext("\tevents [-vHfc]\n"));
349 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
350 "<\"all\" | property[,...]> <pool> ...\n"));
352 return (gettext("\tset <property=value> <pool> \n"));
354 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
355 "\t [-o property=value] <pool> <newpool> "
356 "[<device> ...]\n"));
358 return (gettext("\treguid <pool>\n"));
367 * Callback routine that will print out a pool property value.
370 print_prop_cb(int prop
, void *cb
)
374 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
376 if (zpool_prop_readonly(prop
))
377 (void) fprintf(fp
, " NO ");
379 (void) fprintf(fp
, " YES ");
381 if (zpool_prop_values(prop
) == NULL
)
382 (void) fprintf(fp
, "-\n");
384 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
390 * Display usage message. If we're inside a command, display only the usage for
391 * that command. Otherwise, iterate over the entire command table and display
392 * a complete usage message.
395 usage(boolean_t requested
)
397 FILE *fp
= requested
? stdout
: stderr
;
399 if (current_command
== NULL
) {
402 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
404 gettext("where 'command' is one of the following:\n\n"));
406 for (i
= 0; i
< NCOMMAND
; i
++) {
407 if (command_table
[i
].name
== NULL
)
408 (void) fprintf(fp
, "\n");
410 (void) fprintf(fp
, "%s",
411 get_usage(command_table
[i
].usage
));
414 (void) fprintf(fp
, gettext("usage:\n"));
415 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
418 if (current_command
!= NULL
&&
419 ((strcmp(current_command
->name
, "set") == 0) ||
420 (strcmp(current_command
->name
, "get") == 0) ||
421 (strcmp(current_command
->name
, "list") == 0))) {
424 gettext("\nthe following properties are supported:\n"));
426 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
427 "PROPERTY", "EDIT", "VALUES");
429 /* Iterate over all properties */
430 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
433 (void) fprintf(fp
, "\t%-15s ", "feature@...");
434 (void) fprintf(fp
, "YES disabled | enabled | active\n");
436 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
437 "appended with a feature name.\nSee zpool-features(5).\n"));
441 * See comments at end of main().
443 if (getenv("ZFS_ABORT") != NULL
) {
444 (void) printf("dumping core by request\n");
448 exit(requested
? 0 : 2);
452 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
453 boolean_t print_logs
, int name_flags
)
460 (void) printf("\t%*s%s\n", indent
, "", name
);
462 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
463 &child
, &children
) != 0)
466 for (c
= 0; c
< children
; c
++) {
467 uint64_t is_log
= B_FALSE
;
469 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
471 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
474 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
475 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
476 B_FALSE
, name_flags
);
482 prop_list_contains_feature(nvlist_t
*proplist
)
485 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
486 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
487 if (zpool_prop_feature(nvpair_name(nvp
)))
494 * Add a property pair (name, string-value) into a property nvlist.
497 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
500 zpool_prop_t prop
= ZPROP_INVAL
;
506 if (*props
== NULL
&&
507 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
508 (void) fprintf(stderr
,
509 gettext("internal error: out of memory\n"));
516 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
518 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
519 !zpool_prop_feature(propname
)) {
520 (void) fprintf(stderr
, gettext("property '%s' is "
521 "not a valid pool property\n"), propname
);
526 * feature@ properties and version should not be specified
529 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
530 nvlist_exists(proplist
, vname
)) ||
531 (prop
== ZPOOL_PROP_VERSION
&&
532 prop_list_contains_feature(proplist
))) {
533 (void) fprintf(stderr
, gettext("'feature@' and "
534 "'version' properties cannot be specified "
540 if (zpool_prop_feature(propname
))
543 normnm
= zpool_prop_to_name(prop
);
545 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
546 normnm
= zfs_prop_to_name(fprop
);
552 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
553 prop
!= ZPOOL_PROP_CACHEFILE
) {
554 (void) fprintf(stderr
, gettext("property '%s' "
555 "specified multiple times\n"), propname
);
559 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
560 (void) fprintf(stderr
, gettext("internal "
561 "error: out of memory\n"));
569 * Set a default property pair (name, string-value) in a property nvlist
572 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
577 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
580 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
584 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
586 * -f Force addition of devices, even if they appear in use
587 * -g Display guid for individual vdev name.
588 * -L Follow links when resolving vdev path name.
589 * -n Do not add the devices, but display the resulting layout if
590 * they were to be added.
591 * -o Set property=value.
592 * -P Display full path for vdev name.
594 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
595 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
599 zpool_do_add(int argc
, char **argv
)
601 boolean_t force
= B_FALSE
;
602 boolean_t dryrun
= B_FALSE
;
610 nvlist_t
*props
= NULL
;
614 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
620 name_flags
|= VDEV_NAME_GUID
;
623 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
629 if ((propval
= strchr(optarg
, '=')) == NULL
) {
630 (void) fprintf(stderr
, gettext("missing "
631 "'=' for -o option\n"));
637 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
638 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
642 name_flags
|= VDEV_NAME_PATH
;
645 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
654 /* get pool name and check number of arguments */
656 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
660 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
669 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
672 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
673 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
679 /* pass off to get_vdev_spec for processing */
680 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
682 if (nvroot
== NULL
) {
688 nvlist_t
*poolnvroot
;
690 uint_t l2children
, c
;
692 boolean_t hadcache
= B_FALSE
;
694 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
697 (void) printf(gettext("would update '%s' to the following "
698 "configuration:\n"), zpool_get_name(zhp
));
700 /* print original main pool and new tree */
701 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
703 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
705 /* Do the same for the logs */
706 if (num_logs(poolnvroot
) > 0) {
707 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
709 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
711 } else if (num_logs(nvroot
) > 0) {
712 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
716 /* Do the same for the caches */
717 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
718 &l2child
, &l2children
) == 0 && l2children
) {
720 (void) printf(gettext("\tcache\n"));
721 for (c
= 0; c
< l2children
; c
++) {
722 vname
= zpool_vdev_name(g_zfs
, NULL
,
723 l2child
[c
], name_flags
);
724 (void) printf("\t %s\n", vname
);
728 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
729 &l2child
, &l2children
) == 0 && l2children
) {
731 (void) printf(gettext("\tcache\n"));
732 for (c
= 0; c
< l2children
; c
++) {
733 vname
= zpool_vdev_name(g_zfs
, NULL
,
734 l2child
[c
], name_flags
);
735 (void) printf("\t %s\n", vname
);
742 ret
= (zpool_add(zhp
, nvroot
) != 0);
753 * zpool remove <pool> <vdev> ...
755 * Removes the given vdev from the pool. Currently, this supports removing
756 * spares, cache, and log devices from the pool.
759 zpool_do_remove(int argc
, char **argv
)
763 zpool_handle_t
*zhp
= NULL
;
768 /* get pool name and check number of arguments */
770 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
774 (void) fprintf(stderr
, gettext("missing device\n"));
780 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
783 for (i
= 1; i
< argc
; i
++) {
784 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
793 * zpool labelclear <vdev>
795 * Verifies that the vdev is not active and zeros out the label information
799 zpool_do_labelclear(int argc
, char **argv
)
802 int c
, fd
= -1, ret
= 0;
804 boolean_t inuse
= B_FALSE
;
805 boolean_t force
= B_FALSE
;
808 while ((c
= getopt(argc
, argv
, "f")) != -1) {
814 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
825 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
830 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
831 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
836 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
840 (void) fprintf(stderr
,
841 gettext("Unable to determine pool state for %s\n"
842 "Use -f to force the clearing any label data\n"), vdev
);
850 case POOL_STATE_ACTIVE
:
851 case POOL_STATE_SPARE
:
852 case POOL_STATE_L2CACHE
:
853 (void) fprintf(stderr
,
854 gettext("labelclear operation failed.\n"
855 "\tVdev %s is a member (%s), of pool \"%s\".\n"
856 "\tTo remove label information from this device, "
857 "export or destroy\n\tthe pool, or remove %s from "
858 "the configuration of this pool\n\tand retry the "
859 "labelclear operation.\n"),
860 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
864 case POOL_STATE_EXPORTED
:
868 (void) fprintf(stderr
,
869 gettext("labelclear operation failed.\n\tVdev "
870 "%s is a member of the exported pool \"%s\".\n"
871 "\tUse \"zpool labelclear -f %s\" to force the "
872 "removal of label\n\tinformation.\n"),
877 case POOL_STATE_POTENTIALLY_ACTIVE
:
881 (void) fprintf(stderr
,
882 gettext("labelclear operation failed.\n"
883 "\tVdev %s is a member of the pool \"%s\".\n"
884 "\tThis pool is unknown to this system, but may "
885 "be active on\n\tanother system. Use "
886 "\'zpool labelclear -f %s\' to force the\n"
887 "\tremoval of label information.\n"),
892 case POOL_STATE_DESTROYED
:
893 /* inuse should never be set for a destroyed pool... */
899 if (zpool_clear_label(fd
) != 0) {
900 (void) fprintf(stderr
,
901 gettext("Label clear failed on vdev %s\n"), vdev
);
914 * zpool create [-fnd] [-o property=value] ...
915 * [-O file-system-property=value] ...
916 * [-R root] [-m mountpoint] <pool> <dev> ...
918 * -f Force creation, even if devices appear in use
919 * -n Do not create the pool, but display the resulting layout if it
920 * were to be created.
921 * -R Create a pool under an alternate root
922 * -m Set default mountpoint for the root dataset. By default it's
924 * -o Set property=value.
925 * -o Set feature@feature=enabled|disabled.
926 * -d Don't automatically enable all supported pool features
927 * (individual features can be enabled with -o).
928 * -O Set fsproperty=value in the pool's root file system
930 * Creates the named pool according to the given vdev specification. The
931 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
932 * we get the nvlist back from get_vdev_spec(), we either print out the contents
933 * (if '-n' was specified), or pass it to libzfs to do the creation.
936 zpool_do_create(int argc
, char **argv
)
938 boolean_t force
= B_FALSE
;
939 boolean_t dryrun
= B_FALSE
;
940 boolean_t enable_all_pool_feat
= B_TRUE
;
942 nvlist_t
*nvroot
= NULL
;
946 char *altroot
= NULL
;
947 char *mountpoint
= NULL
;
948 nvlist_t
*fsprops
= NULL
;
949 nvlist_t
*props
= NULL
;
953 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
962 enable_all_pool_feat
= B_FALSE
;
966 if (add_prop_list(zpool_prop_to_name(
967 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
969 if (add_prop_list_default(zpool_prop_to_name(
970 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
974 /* Equivalent to -O mountpoint=optarg */
978 if ((propval
= strchr(optarg
, '=')) == NULL
) {
979 (void) fprintf(stderr
, gettext("missing "
980 "'=' for -o option\n"));
986 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
990 * If the user is creating a pool that doesn't support
991 * feature flags, don't enable any features.
993 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
997 ver
= strtoull(propval
, &end
, 10);
999 ver
< SPA_VERSION_FEATURES
) {
1000 enable_all_pool_feat
= B_FALSE
;
1003 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1007 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1008 (void) fprintf(stderr
, gettext("missing "
1009 "'=' for -O option\n"));
1016 * Mountpoints are checked and then added later.
1017 * Uniquely among properties, they can be specified
1018 * more than once, to avoid conflict with -m.
1020 if (0 == strcmp(optarg
,
1021 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1022 mountpoint
= propval
;
1023 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1030 * Sanity check temporary pool name.
1032 if (strchr(optarg
, '/') != NULL
) {
1033 (void) fprintf(stderr
, gettext("cannot create "
1034 "'%s': invalid character '/' in temporary "
1036 (void) fprintf(stderr
, gettext("use 'zfs "
1037 "create' to create a dataset\n"));
1041 if (add_prop_list(zpool_prop_to_name(
1042 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1044 if (add_prop_list_default(zpool_prop_to_name(
1045 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1050 (void) fprintf(stderr
, gettext("missing argument for "
1051 "'%c' option\n"), optopt
);
1054 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1063 /* get pool name and check number of arguments */
1065 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1069 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1076 * As a special case, check for use of '/' in the name, and direct the
1077 * user to use 'zfs create' instead.
1079 if (strchr(poolname
, '/') != NULL
) {
1080 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1081 "character '/' in pool name\n"), poolname
);
1082 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1083 "create a dataset\n"));
1087 /* pass off to get_vdev_spec for bulk processing */
1088 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1089 argc
- 1, argv
+ 1);
1093 /* make_root_vdev() allows 0 toplevel children if there are spares */
1094 if (!zfs_allocatable_devs(nvroot
)) {
1095 (void) fprintf(stderr
, gettext("invalid vdev "
1096 "specification: at least one toplevel vdev must be "
1101 if (altroot
!= NULL
&& altroot
[0] != '/') {
1102 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1103 "must be an absolute path\n"), altroot
);
1108 * Check the validity of the mountpoint and direct the user to use the
1109 * '-m' mountpoint option if it looks like its in use.
1111 if (mountpoint
== NULL
||
1112 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1113 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1114 char buf
[MAXPATHLEN
];
1117 if (mountpoint
&& mountpoint
[0] != '/') {
1118 (void) fprintf(stderr
, gettext("invalid mountpoint "
1119 "'%s': must be an absolute path, 'legacy', or "
1120 "'none'\n"), mountpoint
);
1124 if (mountpoint
== NULL
) {
1125 if (altroot
!= NULL
)
1126 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1129 (void) snprintf(buf
, sizeof (buf
), "/%s",
1132 if (altroot
!= NULL
)
1133 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1134 altroot
, mountpoint
);
1136 (void) snprintf(buf
, sizeof (buf
), "%s",
1140 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1141 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1142 "%s\n"), buf
, strerror(errno
));
1143 (void) fprintf(stderr
, gettext("use '-m' "
1144 "option to provide a different default\n"));
1149 while (count
< 3 && readdir(dirp
) != NULL
)
1151 (void) closedir(dirp
);
1154 (void) fprintf(stderr
, gettext("mountpoint "
1155 "'%s' exists and is not empty\n"), buf
);
1156 (void) fprintf(stderr
, gettext("use '-m' "
1157 "option to provide a "
1158 "different default\n"));
1165 * Now that the mountpoint's validity has been checked, ensure that
1166 * the property is set appropriately prior to creating the pool.
1168 if (mountpoint
!= NULL
) {
1169 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1170 mountpoint
, &fsprops
, B_FALSE
);
1178 * For a dry run invocation, print out a basic message and run
1179 * through all the vdevs in the list and print out in an
1180 * appropriate hierarchy.
1182 (void) printf(gettext("would create '%s' with the "
1183 "following layout:\n\n"), poolname
);
1185 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1186 if (num_logs(nvroot
) > 0)
1187 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1192 * Hand off to libzfs.
1195 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1196 char propname
[MAXPATHLEN
];
1198 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1200 (void) snprintf(propname
, sizeof (propname
),
1201 "feature@%s", feat
->fi_uname
);
1204 * Only features contained in props will be enabled:
1205 * remove from the nvlist every ZFS_FEATURE_DISABLED
1206 * value and add every missing ZFS_FEATURE_ENABLED if
1207 * enable_all_pool_feat is set.
1209 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1210 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1211 (void) nvlist_remove_all(props
,
1213 } else if (enable_all_pool_feat
) {
1214 ret
= add_prop_list(propname
,
1215 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1222 if (zpool_create(g_zfs
, poolname
,
1223 nvroot
, props
, fsprops
) == 0) {
1224 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1225 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1227 if (zfs_mount(pool
, NULL
, 0) == 0)
1228 ret
= zfs_shareall(pool
);
1231 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1232 (void) fprintf(stderr
, gettext("pool name may have "
1238 nvlist_free(nvroot
);
1239 nvlist_free(fsprops
);
1243 nvlist_free(fsprops
);
1250 * zpool destroy <pool>
1252 * -f Forcefully unmount any datasets
1254 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1257 zpool_do_destroy(int argc
, char **argv
)
1259 boolean_t force
= B_FALSE
;
1262 zpool_handle_t
*zhp
;
1266 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1272 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1281 /* check arguments */
1283 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1287 (void) fprintf(stderr
, gettext("too many arguments\n"));
1293 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1295 * As a special case, check for use of '/' in the name, and
1296 * direct the user to use 'zfs destroy' instead.
1298 if (strchr(pool
, '/') != NULL
)
1299 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1300 "destroy a dataset\n"));
1304 if (zpool_disable_datasets(zhp
, force
) != 0) {
1305 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1306 "could not unmount datasets\n"), zpool_get_name(zhp
));
1311 /* The history must be logged as part of the export */
1312 log_history
= B_FALSE
;
1314 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1321 typedef struct export_cbdata
{
1323 boolean_t hardforce
;
1330 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1332 export_cbdata_t
*cb
= data
;
1334 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1337 /* The history must be logged as part of the export */
1338 log_history
= B_FALSE
;
1340 if (cb
->hardforce
) {
1341 if (zpool_export_force(zhp
, history_str
) != 0)
1343 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1351 * zpool export [-f] <pool> ...
1353 * -a Export all pools
1354 * -f Forcefully unmount datasets
1356 * Export the given pools. By default, the command will attempt to cleanly
1357 * unmount any active datasets within the pool. If the '-f' flag is specified,
1358 * then the datasets will be forcefully unmounted.
1361 zpool_do_export(int argc
, char **argv
)
1364 boolean_t do_all
= B_FALSE
;
1365 boolean_t force
= B_FALSE
;
1366 boolean_t hardforce
= B_FALSE
;
1370 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1382 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1389 cb
.hardforce
= hardforce
;
1395 (void) fprintf(stderr
, gettext("too many arguments\n"));
1399 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1400 zpool_export_one
, &cb
));
1403 /* check arguments */
1405 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1409 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1415 * Given a vdev configuration, determine the maximum width needed for the device
1419 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1427 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1428 if (strlen(name
) + depth
> max
)
1429 max
= strlen(name
) + depth
;
1433 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1434 &child
, &children
) == 0) {
1435 for (c
= 0; c
< children
; c
++)
1436 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1437 max
, name_flags
)) > max
)
1441 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1442 &child
, &children
) == 0) {
1443 for (c
= 0; c
< children
; c
++)
1444 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1445 max
, name_flags
)) > max
)
1449 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1450 &child
, &children
) == 0) {
1451 for (c
= 0; c
< children
; c
++)
1452 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1453 max
, name_flags
)) > max
)
1460 typedef struct spare_cbdata
{
1462 zpool_handle_t
*cb_zhp
;
1466 find_vdev(nvlist_t
*nv
, uint64_t search
)
1472 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1476 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1477 &child
, &children
) == 0) {
1478 for (c
= 0; c
< children
; c
++)
1479 if (find_vdev(child
[c
], search
))
1487 find_spare(zpool_handle_t
*zhp
, void *data
)
1489 spare_cbdata_t
*cbp
= data
;
1490 nvlist_t
*config
, *nvroot
;
1492 config
= zpool_get_config(zhp
, NULL
);
1493 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1496 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1505 typedef struct status_cbdata
{
1509 boolean_t cb_allpools
;
1510 boolean_t cb_verbose
;
1511 boolean_t cb_explain
;
1513 boolean_t cb_dedup_stats
;
1514 boolean_t cb_print_status
;
1515 vdev_cmd_data_list_t
*vcdl
;
1518 /* Print output line for specific vdev in a specific pool */
1520 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1523 for (i
= 0; i
< vcdl
->count
; i
++) {
1524 if ((strcmp(vcdl
->data
[i
].path
, path
) == 0) &&
1525 (strcmp(vcdl
->data
[i
].pool
, pool
) == 0)) {
1526 printf("%s", vcdl
->data
[i
].line
);
1533 * Print out configuration state as requested by status_callback.
1536 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1537 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1541 pool_scan_stat_t
*ps
= NULL
;
1543 char rbuf
[6], wbuf
[6], cbuf
[6];
1545 uint64_t notpresent
;
1546 spare_cbdata_t spare_cb
;
1550 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1551 &child
, &children
) != 0)
1554 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1555 (uint64_t **)&vs
, &c
) == 0);
1557 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1560 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1563 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1565 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1569 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1573 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1574 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1575 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1576 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1579 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1580 ¬present
) == 0) {
1581 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1582 (void) printf(" was %s", path
);
1583 } else if (vs
->vs_aux
!= 0) {
1586 switch (vs
->vs_aux
) {
1587 case VDEV_AUX_OPEN_FAILED
:
1588 (void) printf(gettext("cannot open"));
1591 case VDEV_AUX_BAD_GUID_SUM
:
1592 (void) printf(gettext("missing device"));
1595 case VDEV_AUX_NO_REPLICAS
:
1596 (void) printf(gettext("insufficient replicas"));
1599 case VDEV_AUX_VERSION_NEWER
:
1600 (void) printf(gettext("newer version"));
1603 case VDEV_AUX_UNSUP_FEAT
:
1604 (void) printf(gettext("unsupported feature(s)"));
1607 case VDEV_AUX_SPARED
:
1608 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1609 &spare_cb
.cb_guid
) == 0);
1610 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1611 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1612 zpool_get_name(zhp
)) == 0)
1613 (void) printf(gettext("currently in "
1616 (void) printf(gettext("in use by "
1618 zpool_get_name(spare_cb
.cb_zhp
));
1619 zpool_close(spare_cb
.cb_zhp
);
1621 (void) printf(gettext("currently in use"));
1625 case VDEV_AUX_ERR_EXCEEDED
:
1626 (void) printf(gettext("too many errors"));
1629 case VDEV_AUX_IO_FAILURE
:
1630 (void) printf(gettext("experienced I/O failures"));
1633 case VDEV_AUX_BAD_LOG
:
1634 (void) printf(gettext("bad intent log"));
1637 case VDEV_AUX_EXTERNAL
:
1638 (void) printf(gettext("external device fault"));
1641 case VDEV_AUX_SPLIT_POOL
:
1642 (void) printf(gettext("split into new pool"));
1646 (void) printf(gettext("corrupted data"));
1651 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1652 (uint64_t **)&ps
, &c
);
1654 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1655 vs
->vs_scan_processed
!= 0 && children
== 0) {
1656 (void) printf(gettext(" (%s)"),
1657 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1658 "resilvering" : "repairing");
1661 if (cb
->vcdl
!= NULL
) {
1662 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1664 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1668 (void) printf("\n");
1670 for (c
= 0; c
< children
; c
++) {
1671 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1673 /* Don't print logs or holes here */
1674 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1676 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1678 if (islog
|| ishole
)
1680 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1681 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1682 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1689 * Print the configuration of an exported pool. Iterate over all vdevs in the
1690 * pool, printing out the name and status for each one.
1693 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1701 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1702 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1703 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1706 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1707 (uint64_t **)&vs
, &c
) == 0);
1709 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1710 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1712 if (vs
->vs_aux
!= 0) {
1715 switch (vs
->vs_aux
) {
1716 case VDEV_AUX_OPEN_FAILED
:
1717 (void) printf(gettext("cannot open"));
1720 case VDEV_AUX_BAD_GUID_SUM
:
1721 (void) printf(gettext("missing device"));
1724 case VDEV_AUX_NO_REPLICAS
:
1725 (void) printf(gettext("insufficient replicas"));
1728 case VDEV_AUX_VERSION_NEWER
:
1729 (void) printf(gettext("newer version"));
1732 case VDEV_AUX_UNSUP_FEAT
:
1733 (void) printf(gettext("unsupported feature(s)"));
1736 case VDEV_AUX_ERR_EXCEEDED
:
1737 (void) printf(gettext("too many errors"));
1741 (void) printf(gettext("corrupted data"));
1745 (void) printf("\n");
1747 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1748 &child
, &children
) != 0)
1751 for (c
= 0; c
< children
; c
++) {
1752 uint64_t is_log
= B_FALSE
;
1754 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1759 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1760 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1761 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1765 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1766 &child
, &children
) == 0) {
1767 (void) printf(gettext("\tcache\n"));
1768 for (c
= 0; c
< children
; c
++) {
1769 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1771 (void) printf("\t %s\n", vname
);
1776 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1777 &child
, &children
) == 0) {
1778 (void) printf(gettext("\tspares\n"));
1779 for (c
= 0; c
< children
; c
++) {
1780 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1782 (void) printf("\t %s\n", vname
);
1790 * Logs are recorded as top level vdevs in the main pool child array
1791 * but with "is_log" set to 1. We use either print_status_config() or
1792 * print_import_config() to print the top level logs then any log
1793 * children (eg mirrored slogs) are printed recursively - which
1794 * works because only the top level vdev is marked "is_log"
1797 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1802 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1806 (void) printf(gettext("\tlogs\n"));
1808 for (c
= 0; c
< children
; c
++) {
1809 uint64_t is_log
= B_FALSE
;
1812 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1816 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1817 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1818 if (cb
->cb_print_status
)
1819 print_status_config(zhp
, cb
, name
, child
[c
], 2,
1822 print_import_config(cb
, name
, child
[c
], 2);
1828 * Display the status for the given pool.
1831 show_import(nvlist_t
*config
)
1833 uint64_t pool_state
;
1839 zpool_status_t reason
;
1840 zpool_errata_t errata
;
1844 status_cbdata_t cb
= { 0 };
1846 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1848 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1850 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1852 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1855 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1856 (uint64_t **)&vs
, &vsc
) == 0);
1857 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1859 reason
= zpool_import_status(config
, &msgid
, &errata
);
1861 (void) printf(gettext(" pool: %s\n"), name
);
1862 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1863 (void) printf(gettext(" state: %s"), health
);
1864 if (pool_state
== POOL_STATE_DESTROYED
)
1865 (void) printf(gettext(" (DESTROYED)"));
1866 (void) printf("\n");
1869 case ZPOOL_STATUS_MISSING_DEV_R
:
1870 case ZPOOL_STATUS_MISSING_DEV_NR
:
1871 case ZPOOL_STATUS_BAD_GUID_SUM
:
1872 (void) printf(gettext(" status: One or more devices are "
1873 "missing from the system.\n"));
1876 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1877 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1878 (void) printf(gettext(" status: One or more devices contains "
1879 "corrupted data.\n"));
1882 case ZPOOL_STATUS_CORRUPT_DATA
:
1884 gettext(" status: The pool data is corrupted.\n"));
1887 case ZPOOL_STATUS_OFFLINE_DEV
:
1888 (void) printf(gettext(" status: One or more devices "
1889 "are offlined.\n"));
1892 case ZPOOL_STATUS_CORRUPT_POOL
:
1893 (void) printf(gettext(" status: The pool metadata is "
1897 case ZPOOL_STATUS_VERSION_OLDER
:
1898 (void) printf(gettext(" status: The pool is formatted using a "
1899 "legacy on-disk version.\n"));
1902 case ZPOOL_STATUS_VERSION_NEWER
:
1903 (void) printf(gettext(" status: The pool is formatted using an "
1904 "incompatible version.\n"));
1907 case ZPOOL_STATUS_FEAT_DISABLED
:
1908 (void) printf(gettext(" status: Some supported features are "
1909 "not enabled on the pool.\n"));
1912 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1913 (void) printf(gettext("status: The pool uses the following "
1914 "feature(s) not supported on this system:\n"));
1915 zpool_print_unsup_feat(config
);
1918 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1919 (void) printf(gettext("status: The pool can only be accessed "
1920 "in read-only mode on this system. It\n\tcannot be "
1921 "accessed in read-write mode because it uses the "
1922 "following\n\tfeature(s) not supported on this system:\n"));
1923 zpool_print_unsup_feat(config
);
1926 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1927 (void) printf(gettext(" status: The pool was last accessed by "
1928 "another system.\n"));
1931 case ZPOOL_STATUS_FAULTED_DEV_R
:
1932 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1933 (void) printf(gettext(" status: One or more devices are "
1937 case ZPOOL_STATUS_BAD_LOG
:
1938 (void) printf(gettext(" status: An intent log record cannot be "
1942 case ZPOOL_STATUS_RESILVERING
:
1943 (void) printf(gettext(" status: One or more devices were being "
1947 case ZPOOL_STATUS_ERRATA
:
1948 (void) printf(gettext(" status: Errata #%d detected.\n"),
1954 * No other status can be seen when importing pools.
1956 assert(reason
== ZPOOL_STATUS_OK
);
1960 * Print out an action according to the overall state of the pool.
1962 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1963 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1964 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1965 (void) printf(gettext(" action: The pool can be "
1966 "imported using its name or numeric identifier, "
1967 "though\n\tsome features will not be available "
1968 "without an explicit 'zpool upgrade'.\n"));
1969 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1970 (void) printf(gettext(" action: The pool can be "
1971 "imported using its name or numeric "
1972 "identifier and\n\tthe '-f' flag.\n"));
1973 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1975 case ZPOOL_ERRATA_NONE
:
1978 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1979 (void) printf(gettext(" action: The pool can "
1980 "be imported using its name or numeric "
1981 "identifier,\n\thowever there is a compat"
1982 "ibility issue which should be corrected"
1983 "\n\tby running 'zpool scrub'\n"));
1986 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1987 (void) printf(gettext(" action: The pool can"
1988 "not be imported with this version of ZFS "
1989 "due to\n\tan active asynchronous destroy. "
1990 "Revert to an earlier version\n\tand "
1991 "allow the destroy to complete before "
1997 * All errata must contain an action message.
2002 (void) printf(gettext(" action: The pool can be "
2003 "imported using its name or numeric "
2006 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2007 (void) printf(gettext(" action: The pool can be imported "
2008 "despite missing or damaged devices. The\n\tfault "
2009 "tolerance of the pool may be compromised if imported.\n"));
2012 case ZPOOL_STATUS_VERSION_NEWER
:
2013 (void) printf(gettext(" action: The pool cannot be "
2014 "imported. Access the pool on a system running "
2015 "newer\n\tsoftware, or recreate the pool from "
2018 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2019 (void) printf(gettext("action: The pool cannot be "
2020 "imported. Access the pool on a system that "
2021 "supports\n\tthe required feature(s), or recreate "
2022 "the pool from backup.\n"));
2024 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2025 (void) printf(gettext("action: The pool cannot be "
2026 "imported in read-write mode. Import the pool "
2028 "\t\"-o readonly=on\", access the pool on a system "
2029 "that supports the\n\trequired feature(s), or "
2030 "recreate the pool from backup.\n"));
2032 case ZPOOL_STATUS_MISSING_DEV_R
:
2033 case ZPOOL_STATUS_MISSING_DEV_NR
:
2034 case ZPOOL_STATUS_BAD_GUID_SUM
:
2035 (void) printf(gettext(" action: The pool cannot be "
2036 "imported. Attach the missing\n\tdevices and try "
2040 (void) printf(gettext(" action: The pool cannot be "
2041 "imported due to damaged devices or data.\n"));
2045 /* Print the comment attached to the pool. */
2046 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2047 (void) printf(gettext("comment: %s\n"), comment
);
2050 * If the state is "closed" or "can't open", and the aux state
2051 * is "corrupt data":
2053 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2054 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2055 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2056 if (pool_state
== POOL_STATE_DESTROYED
)
2057 (void) printf(gettext("\tThe pool was destroyed, "
2058 "but can be imported using the '-Df' flags.\n"));
2059 else if (pool_state
!= POOL_STATE_EXPORTED
)
2060 (void) printf(gettext("\tThe pool may be active on "
2061 "another system, but can be imported using\n\t"
2062 "the '-f' flag.\n"));
2066 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2069 (void) printf(gettext(" config:\n\n"));
2071 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, 0, VDEV_NAME_TYPE_ID
);
2072 if (cb
.cb_namewidth
< 10)
2073 cb
.cb_namewidth
= 10;
2075 print_import_config(&cb
, name
, nvroot
, 0);
2076 if (num_logs(nvroot
) > 0)
2077 print_logs(NULL
, &cb
, nvroot
);
2079 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2080 (void) printf(gettext("\n\tAdditional devices are known to "
2081 "be part of this pool, though their\n\texact "
2082 "configuration cannot be determined.\n"));
2087 * Perform the import for the given configuration. This passes the heavy
2088 * lifting off to zpool_import_props(), and then mounts the datasets contained
2092 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2093 nvlist_t
*props
, int flags
)
2095 zpool_handle_t
*zhp
;
2100 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2103 verify(nvlist_lookup_uint64(config
,
2104 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
2105 verify(nvlist_lookup_uint64(config
,
2106 ZPOOL_CONFIG_VERSION
, &version
) == 0);
2107 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2108 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2109 "is formatted using an unsupported ZFS version\n"), name
);
2111 } else if (state
!= POOL_STATE_EXPORTED
&&
2112 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2113 uint64_t hostid
= 0;
2114 unsigned long system_hostid
= get_system_hostid();
2116 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
2119 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
2124 verify(nvlist_lookup_string(config
,
2125 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2126 verify(nvlist_lookup_uint64(config
,
2127 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2129 (void) fprintf(stderr
, gettext("cannot import "
2130 "'%s': pool may be in use from other "
2131 "system, it was last accessed by %s "
2132 "(hostid: 0x%lx) on %s"), name
, hostname
,
2133 (unsigned long)hostid
,
2134 asctime(localtime(&t
)));
2135 (void) fprintf(stderr
, gettext("use '-f' to "
2136 "import anyway\n"));
2141 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2144 if (newname
!= NULL
)
2145 name
= (char *)newname
;
2147 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2150 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2151 !(flags
& ZFS_IMPORT_ONLY
) &&
2152 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2162 * zpool import [-d dir] [-D]
2163 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2164 * [-d dir | -c cachefile] [-f] -a
2165 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2166 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2168 * -c Read pool information from a cachefile instead of searching
2171 * -d Scan in a specific directory, other than /dev/. More than
2172 * one directory can be specified using multiple '-d' options.
2174 * -D Scan for previously destroyed pools or import all or only
2175 * specified destroyed pools.
2177 * -R Temporarily import the pool, with all mountpoints relative to
2178 * the given root. The pool will remain exported when the machine
2181 * -V Import even in the presence of faulted vdevs. This is an
2182 * intentionally undocumented option for testing purposes, and
2183 * treats the pool configuration as complete, leaving any bad
2184 * vdevs in the FAULTED state. In other words, it does verbatim
2187 * -f Force import, even if it appears that the pool is active.
2189 * -F Attempt rewind if necessary.
2191 * -n See if rewind would work, but don't actually rewind.
2193 * -N Import the pool but don't mount datasets.
2195 * -T Specify a starting txg to use for import. This option is
2196 * intentionally undocumented option for testing purposes.
2198 * -a Import all pools found.
2200 * -o Set property=value and/or temporary mount options (without '=').
2202 * -s Scan using the default search path, the libblkid cache will
2205 * The import command scans for pools to import, and import pools based on pool
2206 * name and GUID. The pool can also be renamed as part of the import process.
2209 zpool_do_import(int argc
, char **argv
)
2211 char **searchdirs
= NULL
;
2212 char *env
, *envdup
= NULL
;
2216 nvlist_t
*pools
= NULL
;
2217 boolean_t do_all
= B_FALSE
;
2218 boolean_t do_destroyed
= B_FALSE
;
2219 char *mntopts
= NULL
;
2222 uint64_t searchguid
= 0;
2223 char *searchname
= NULL
;
2225 nvlist_t
*found_config
;
2226 nvlist_t
*policy
= NULL
;
2227 nvlist_t
*props
= NULL
;
2229 int flags
= ZFS_IMPORT_NORMAL
;
2230 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2231 boolean_t dryrun
= B_FALSE
;
2232 boolean_t do_rewind
= B_FALSE
;
2233 boolean_t xtreme_rewind
= B_FALSE
;
2234 boolean_t do_scan
= B_FALSE
;
2235 uint64_t pool_state
, txg
= -1ULL;
2236 char *cachefile
= NULL
;
2237 importargs_t idata
= { 0 };
2241 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2250 if (searchdirs
== NULL
) {
2251 searchdirs
= safe_malloc(sizeof (char *));
2253 char **tmp
= safe_malloc((nsearch
+ 1) *
2255 bcopy(searchdirs
, tmp
, nsearch
*
2260 searchdirs
[nsearch
++] = optarg
;
2263 do_destroyed
= B_TRUE
;
2266 flags
|= ZFS_IMPORT_ANY_HOST
;
2272 flags
|= ZFS_IMPORT_MISSING_LOG
;
2278 flags
|= ZFS_IMPORT_ONLY
;
2281 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2284 if (add_prop_list(optarg
, propval
,
2292 if (add_prop_list(zpool_prop_to_name(
2293 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2295 if (add_prop_list_default(zpool_prop_to_name(
2296 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2303 flags
|= ZFS_IMPORT_TEMP_NAME
;
2304 if (add_prop_list_default(zpool_prop_to_name(
2305 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2311 txg
= strtoull(optarg
, &endptr
, 0);
2312 if (errno
!= 0 || *endptr
!= '\0') {
2313 (void) fprintf(stderr
,
2314 gettext("invalid txg value\n"));
2317 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2320 flags
|= ZFS_IMPORT_VERBATIM
;
2323 xtreme_rewind
= B_TRUE
;
2326 (void) fprintf(stderr
, gettext("missing argument for "
2327 "'%c' option\n"), optopt
);
2331 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2340 if (cachefile
&& nsearch
!= 0) {
2341 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2345 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2346 (void) fprintf(stderr
,
2347 gettext("-n or -X only meaningful with -F\n"));
2351 rewind_policy
= ZPOOL_TRY_REWIND
;
2353 rewind_policy
= ZPOOL_DO_REWIND
;
2355 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2357 /* In the future, we can capture further policy and include it here */
2358 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2359 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2360 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2363 /* check argument count */
2366 (void) fprintf(stderr
, gettext("too many arguments\n"));
2371 (void) fprintf(stderr
, gettext("too many arguments\n"));
2377 * Check for the effective uid. We do this explicitly here because
2378 * otherwise any attempt to discover pools will silently fail.
2380 if (argc
== 0 && geteuid() != 0) {
2381 (void) fprintf(stderr
, gettext("cannot "
2382 "discover pools: permission denied\n"));
2383 if (searchdirs
!= NULL
)
2387 nvlist_free(policy
);
2392 * Depending on the arguments given, we do one of the following:
2394 * <none> Iterate through all pools and display information about
2397 * -a Iterate through all pools and try to import each one.
2399 * <id> Find the pool that corresponds to the given GUID/pool
2400 * name and import that one.
2402 * -D Above options applies only to destroyed pools.
2408 searchguid
= strtoull(argv
[0], &endptr
, 10);
2409 if (errno
!= 0 || *endptr
!= '\0') {
2410 searchname
= argv
[0];
2413 found_config
= NULL
;
2416 * User specified a name or guid. Ensure it's unique.
2418 idata
.unique
= B_TRUE
;
2422 * Check the environment for the preferred search path.
2424 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2427 envdup
= strdup(env
);
2429 dir
= strtok(envdup
, ":");
2430 while (dir
!= NULL
) {
2431 if (searchdirs
== NULL
) {
2432 searchdirs
= safe_malloc(sizeof (char *));
2434 char **tmp
= safe_malloc((nsearch
+ 1) *
2436 bcopy(searchdirs
, tmp
, nsearch
*
2441 searchdirs
[nsearch
++] = dir
;
2442 dir
= strtok(NULL
, ":");
2446 idata
.path
= searchdirs
;
2447 idata
.paths
= nsearch
;
2448 idata
.poolname
= searchname
;
2449 idata
.guid
= searchguid
;
2450 idata
.cachefile
= cachefile
;
2451 idata
.scan
= do_scan
;
2454 * Under Linux the zpool_find_import_impl() function leverages the
2455 * taskq implementation to parallelize device scanning. It is
2456 * therefore necessary to initialize this functionality for the
2457 * duration of the zpool_search_import() function.
2460 pools
= zpool_search_import(g_zfs
, &idata
);
2463 if (pools
!= NULL
&& idata
.exists
&&
2464 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2465 (void) fprintf(stderr
, gettext("cannot import '%s': "
2466 "a pool with that name already exists\n"),
2468 (void) fprintf(stderr
, gettext("use the form '%s "
2469 "<pool | id> <newpool>' to give it a new name\n"),
2472 } else if (pools
== NULL
&& idata
.exists
) {
2473 (void) fprintf(stderr
, gettext("cannot import '%s': "
2474 "a pool with that name is already created/imported,\n"),
2476 (void) fprintf(stderr
, gettext("and no additional pools "
2477 "with that name were found\n"));
2479 } else if (pools
== NULL
) {
2481 (void) fprintf(stderr
, gettext("cannot import '%s': "
2482 "no such pool available\n"), argv
[0]);
2488 if (searchdirs
!= NULL
)
2492 nvlist_free(policy
);
2499 * At this point we have a list of import candidate configs. Even if
2500 * we were searching by pool name or guid, we still need to
2501 * post-process the list to deal with pool state and possible
2507 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2509 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2511 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2513 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2515 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2518 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2525 (void) printf("\n");
2528 err
|= do_import(config
, NULL
, mntopts
,
2531 show_import(config
);
2533 } else if (searchname
!= NULL
) {
2537 * We are searching for a pool based on name.
2539 verify(nvlist_lookup_string(config
,
2540 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2542 if (strcmp(name
, searchname
) == 0) {
2543 if (found_config
!= NULL
) {
2544 (void) fprintf(stderr
, gettext(
2545 "cannot import '%s': more than "
2546 "one matching pool\n"), searchname
);
2547 (void) fprintf(stderr
, gettext(
2548 "import by numeric ID instead\n"));
2551 found_config
= config
;
2557 * Search for a pool by guid.
2559 verify(nvlist_lookup_uint64(config
,
2560 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2562 if (guid
== searchguid
)
2563 found_config
= config
;
2568 * If we were searching for a specific pool, verify that we found a
2569 * pool, and then do the import.
2571 if (argc
!= 0 && err
== 0) {
2572 if (found_config
== NULL
) {
2573 (void) fprintf(stderr
, gettext("cannot import '%s': "
2574 "no such pool available\n"), argv
[0]);
2577 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2578 argv
[1], mntopts
, props
, flags
);
2583 * If we were just looking for pools, report an error if none were
2586 if (argc
== 0 && first
)
2587 (void) fprintf(stderr
,
2588 gettext("no pools available to import\n"));
2593 nvlist_free(policy
);
2594 if (searchdirs
!= NULL
)
2599 return (err
? 1 : 0);
2602 typedef struct iostat_cbdata
{
2607 char **cb_vdev_names
; /* Only show these vdevs */
2608 unsigned int cb_vdev_names_count
;
2609 boolean_t cb_verbose
;
2610 boolean_t cb_literal
;
2611 boolean_t cb_scripted
;
2612 zpool_list_t
*cb_list
;
2613 vdev_cmd_data_list_t
*vcdl
;
2617 typedef struct name_and_columns
{
2618 const char *name
; /* Column name */
2619 unsigned int columns
; /* Center name to this number of columns */
2620 } name_and_columns_t
;
2622 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2624 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2626 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2628 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2629 {"asyncq_wait", 2}, {"scrub"}},
2630 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2631 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2633 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2634 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2635 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2636 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2640 /* Shorthand - if "columns" field not set, default to 1 column */
2641 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2643 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2645 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2646 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2647 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2648 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2649 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2650 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2651 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2652 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2655 static const char *histo_to_title
[] = {
2656 [IOS_L_HISTO
] = "latency",
2657 [IOS_RQ_HISTO
] = "req_size",
2661 * Return the number of labels in a null-terminated name_and_columns_t
2666 label_array_len(const name_and_columns_t
*labels
)
2670 while (labels
[i
].name
)
2677 * Return the number of strings in a null-terminated string array.
2680 * const char foo[] = {"bar", "baz", NULL}
2685 str_array_len(const char *array
[])
2696 * Return a default column width for default/latency/queue columns. This does
2697 * not include histograms, which have their columns autosized.
2700 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
2702 unsigned long column_width
= 5; /* Normal niceprint */
2703 static unsigned long widths
[] = {
2705 * Choose some sane default column sizes for printing the
2708 [IOS_DEFAULT
] = 15, /* 1PB capacity */
2709 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
2710 [IOS_QUEUES
] = 6, /* 1M queue entries */
2714 column_width
= widths
[type
];
2716 return (column_width
);
2720 * Print the column labels, i.e:
2722 * capacity operations bandwidth
2723 * alloc free read write read write ...
2725 * If force_column_width is set, use it for the column width. If not set, use
2726 * the default column width.
2729 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2730 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
2733 unsigned int text_start
, rw_column_width
, spaces_to_end
;
2734 uint64_t flags
= cb
->cb_flags
;
2736 unsigned int column_width
= force_column_width
;
2738 /* For each bit set in flags */
2739 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2740 idx
= lowbit64(f
) - 1;
2741 if (!force_column_width
)
2742 column_width
= default_column_width(cb
, idx
);
2743 /* Print our top labels centered over "read write" label. */
2744 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
2745 const char *name
= labels
[idx
][i
].name
;
2747 * We treat labels[][].columns == 0 as shorthand
2748 * for one column. It makes writing out the label
2749 * tables more concise.
2751 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
2752 unsigned int slen
= strlen(name
);
2754 rw_column_width
= (column_width
* columns
) +
2755 (2 * (columns
- 1));
2757 text_start
= (int)((rw_column_width
)/columns
-
2760 printf(" "); /* Two spaces between columns */
2762 /* Space from beginning of column to label */
2763 for (s
= 0; s
< text_start
; s
++)
2768 /* Print space after label to end of column */
2769 spaces_to_end
= rw_column_width
- text_start
- slen
;
2770 for (s
= 0; s
< spaces_to_end
; s
++)
2779 * Utility function to print out a line of dashes like:
2781 * -------------------------------- ----- ----- ----- ----- -----
2783 * ...or a dashed named-row line like:
2789 * @force_column_width If non-zero, use the value as the column width.
2790 * Otherwise use the default column widths.
2792 * @name: Print a dashed named-row line starting
2793 * with @name. Otherwise, print a regular
2797 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2801 unsigned int namewidth
;
2802 uint64_t flags
= cb
->cb_flags
;
2805 const name_and_columns_t
*labels
;
2809 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
2810 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
2811 } else if (cb
->cb_vdev_names_count
) {
2817 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
2818 name
? strlen(name
) : 0);
2822 printf("%-*s", namewidth
, name
);
2824 for (i
= 0; i
< namewidth
; i
++)
2828 /* For each bit in flags */
2829 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2830 unsigned int column_width
;
2831 idx
= lowbit64(f
) - 1;
2832 if (force_column_width
)
2833 column_width
= force_column_width
;
2835 column_width
= default_column_width(cb
, idx
);
2837 labels
= iostat_bottom_labels
[idx
];
2838 for (i
= 0; i
< label_array_len(labels
); i
++) {
2840 printf(" %*s-", column_width
- 1, " ");
2842 printf(" %.*s", column_width
,
2843 "--------------------");
2851 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
2852 unsigned int force_column_width
)
2854 print_iostat_dashes(cb
, force_column_width
, NULL
);
2858 print_iostat_separator(iostat_cbdata_t
*cb
)
2860 print_iostat_separator_impl(cb
, 0);
2864 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2865 const char *histo_vdev_name
)
2867 unsigned int namewidth
;
2870 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
2871 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
2872 } else if (cb
->cb_vdev_names_count
) {
2878 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
2879 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
2881 if (histo_vdev_name
)
2882 printf("%-*s", namewidth
, histo_vdev_name
);
2884 printf("%*s", namewidth
, "");
2887 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
2889 printf("%-*s", namewidth
, title
);
2891 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
2893 print_iostat_separator_impl(cb
, force_column_width
);
2897 print_iostat_header(iostat_cbdata_t
*cb
)
2899 print_iostat_header_impl(cb
, 0, NULL
);
2904 * Display a single statistic.
2907 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
2908 unsigned int column_size
, boolean_t scripted
)
2912 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
2915 printf("\t%s", buf
);
2917 printf(" %*s", column_size
, buf
);
2921 * Calculate the default vdev stats
2923 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
2924 * stats into calcvs.
2927 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
2928 vdev_stat_t
*calcvs
)
2932 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
2933 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
2934 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
2936 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
2937 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
2941 * Internal representation of the extended iostats data.
2943 * The extended iostat stats are exported in nvlists as either uint64_t arrays
2944 * or single uint64_t's. We make both look like arrays to make them easier
2945 * to process. In order to make single uint64_t's look like arrays, we set
2946 * __data to the stat data, and then set *data = &__data with count = 1. Then,
2947 * we can just use *data and count.
2951 uint_t count
; /* Number of entries in data[] */
2952 uint64_t __data
; /* Only used when data is a single uint64_t */
2956 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
2960 for (i
= 0; i
< len
; i
++)
2961 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
2967 * Helper function to lookup a uint64_t array or uint64_t value and store its
2968 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
2969 * it look like a one element array to make it easier to process.
2972 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
2973 struct stat_array
*nva
)
2978 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
2979 switch (nvpair_type(tmp
)) {
2980 case DATA_TYPE_UINT64_ARRAY
:
2981 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
2983 case DATA_TYPE_UINT64
:
2984 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
2985 nva
->data
= &nva
->__data
;
2989 /* Not a uint64_t */
2998 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
2999 * subtract them, and return the results in a newly allocated stat_array.
3000 * You must free the returned array after you are done with it with
3001 * free_calc_stats().
3003 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3006 static struct stat_array
*
3007 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3010 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3011 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3013 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3015 /* Extract our extended stats nvlist from the main list */
3016 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3019 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3023 newnva
= safe_malloc(alloc_size
);
3024 oldnva
= safe_malloc(alloc_size
);
3025 calcnva
= safe_malloc(alloc_size
);
3027 for (j
= 0; j
< len
; j
++) {
3028 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3030 calcnva
[j
].count
= newnva
[j
].count
;
3031 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3032 calcnva
[j
].data
= safe_malloc(alloc_size
);
3033 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3036 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3038 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3039 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3048 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3051 for (i
= 0; i
< len
; i
++)
3058 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3059 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3065 enum zfs_nicenum_format format
;
3066 unsigned int buckets
;
3067 unsigned int start_bucket
;
3070 format
= ZFS_NICENUM_RAW
;
3072 format
= ZFS_NICENUM_1024
;
3074 /* All these histos are the same size, so just use nva[0].count */
3075 buckets
= nva
[0].count
;
3077 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3078 /* Start at 512 - req size should never be lower than this */
3084 for (j
= start_bucket
; j
< buckets
; j
++) {
3085 /* Print histogram bucket label */
3086 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3087 /* Ending range of this bucket */
3088 val
= (1UL << (j
+ 1)) - 1;
3089 zfs_nicetime(val
, buf
, sizeof (buf
));
3091 /* Request size (starting range of bucket) */
3093 zfs_nicenum(val
, buf
, sizeof (buf
));
3096 if (cb
->cb_scripted
)
3097 printf("%llu", (u_longlong_t
)val
);
3099 printf("%-*s", namewidth
, buf
);
3101 /* Print the values on the line */
3102 for (i
= 0; i
< len
; i
++) {
3103 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3104 column_width
, cb
->cb_scripted
);
3111 print_solid_separator(unsigned int length
)
3119 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3120 nvlist_t
*newnv
, double scale
, const char *name
)
3122 unsigned int column_width
;
3123 unsigned int namewidth
;
3124 unsigned int entire_width
;
3125 enum iostat_type type
;
3126 struct stat_array
*nva
;
3128 unsigned int names_len
;
3130 /* What type of histo are we? */
3131 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3133 /* Get NULL-terminated array of nvlist names for our histo */
3134 names
= vsx_type_to_nvlist
[type
];
3135 names_len
= str_array_len(names
); /* num of names */
3137 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3139 if (cb
->cb_literal
) {
3140 column_width
= MAX(5,
3141 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3146 namewidth
= MAX(cb
->cb_namewidth
,
3147 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3150 * Calculate the entire line width of what we're printing. The
3151 * +2 is for the two spaces between columns:
3155 /* |___| <---------- column_width */
3157 /* |__________| <--- entire_width */
3159 entire_width
= namewidth
+ (column_width
+ 2) *
3160 label_array_len(iostat_bottom_labels
[type
]);
3162 if (cb
->cb_scripted
)
3163 printf("%s\n", name
);
3165 print_iostat_header_impl(cb
, column_width
, name
);
3167 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3170 free_calc_stats(nva
, names_len
);
3171 if (!cb
->cb_scripted
)
3172 print_solid_separator(entire_width
);
3176 * Calculate the average latency of a power-of-two latency histogram
3179 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3182 uint64_t count
= 0, total
= 0;
3184 for (i
= 0; i
< buckets
; i
++) {
3186 * Our buckets are power-of-two latency ranges. Use the
3187 * midpoint latency of each bucket to calculate the average.
3195 if (histo
[i
] != 0) {
3196 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3201 /* Prevent divide by zero */
3202 return (count
== 0 ? 0 : total
/ count
);
3206 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3207 nvlist_t
*newnv
, double scale
)
3211 const char *names
[] = {
3212 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3213 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3214 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3215 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3216 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3217 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3218 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3219 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3220 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3221 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3224 struct stat_array
*nva
;
3226 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3227 enum zfs_nicenum_format format
;
3229 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3232 format
= ZFS_NICENUM_RAW
;
3234 format
= ZFS_NICENUM_1024
;
3236 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3237 val
= nva
[i
].data
[0] * scale
;
3238 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3241 free_calc_stats(nva
, ARRAY_SIZE(names
));
3245 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3246 nvlist_t
*newnv
, double scale
)
3250 const char *names
[] = {
3251 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3252 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3253 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3254 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3255 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3256 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3257 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3258 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3259 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3261 struct stat_array
*nva
;
3263 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3264 enum zfs_nicenum_format format
;
3266 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3269 format
= ZFS_NICENUM_RAW
;
3271 format
= ZFS_NICENUM_TIME
;
3273 /* Print our avg latencies on the line */
3274 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3275 /* Compute average latency for a latency histo */
3276 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3277 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3279 free_calc_stats(nva
, ARRAY_SIZE(names
));
3283 * Print default statistics (capacity/operations/bandwidth)
3286 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3288 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3289 enum zfs_nicenum_format format
;
3290 char na
; /* char to print for "not applicable" values */
3292 if (cb
->cb_literal
) {
3293 format
= ZFS_NICENUM_RAW
;
3296 format
= ZFS_NICENUM_1024
;
3300 /* only toplevel vdevs have capacity stats */
3301 if (vs
->vs_space
== 0) {
3302 if (cb
->cb_scripted
)
3303 printf("\t%c\t%c", na
, na
);
3305 printf(" %*c %*c", column_width
, na
, column_width
,
3308 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3310 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3311 column_width
, cb
->cb_scripted
);
3314 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3315 format
, column_width
, cb
->cb_scripted
);
3316 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3317 format
, column_width
, cb
->cb_scripted
);
3318 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3319 format
, column_width
, cb
->cb_scripted
);
3320 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3321 format
, column_width
, cb
->cb_scripted
);
3325 * Print out all the statistics for the given vdev. This can either be the
3326 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3327 * is a verbose output, and we don't want to display the toplevel pool stats.
3329 * Returns the number of stat lines printed.
3332 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3333 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3335 nvlist_t
**oldchild
, **newchild
;
3337 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3338 vdev_stat_t zerovs
= { 0 };
3345 calcvs
= safe_malloc(sizeof (*calcvs
));
3347 if (oldnv
!= NULL
) {
3348 verify(nvlist_lookup_uint64_array(oldnv
,
3349 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3354 /* Do we only want to see a specific vdev? */
3355 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3356 /* Yes we do. Is this the vdev? */
3357 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3359 * This is our vdev. Since it is the only vdev we
3360 * will be displaying, make depth = 0 so that it
3361 * doesn't get indented.
3368 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3369 /* Couldn't match the name */
3374 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3375 (uint64_t **)&newvs
, &c
) == 0);
3378 * Print the vdev name unless it's is a histogram. Histograms
3379 * display the vdev name in the header itself.
3381 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3382 if (cb
->cb_scripted
) {
3385 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3386 (void) printf("%*s%s", depth
, "", name
);
3388 (void) printf("%*s%s%*s", depth
, "", name
,
3389 (int)(cb
->cb_namewidth
- strlen(name
) -
3394 /* Calculate our scaling factor */
3395 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3396 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3398 * If we specify printing histograms with no time interval, then
3399 * print the histogram numbers over the entire lifetime of the
3407 scale
= (double)NANOSEC
/ tdelta
;
3410 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3411 calc_default_iostats(oldvs
, newvs
, calcvs
);
3412 print_iostat_default(calcvs
, cb
, scale
);
3414 if (cb
->cb_flags
& IOS_LATENCY_M
)
3415 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3416 if (cb
->cb_flags
& IOS_QUEUES_M
)
3417 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3418 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3420 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3423 if (cb
->vcdl
!= NULL
) {
3425 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3427 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3429 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3430 if (cb
->cb_flags
& IOS_ANYHISTO_M
)
3435 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3444 if (!cb
->cb_verbose
)
3447 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3448 &newchild
, &children
) != 0)
3451 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3452 &oldchild
, &c
) != 0)
3455 for (c
= 0; c
< children
; c
++) {
3456 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3458 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3461 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3464 if (ishole
|| islog
)
3467 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3469 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3470 newchild
[c
], cb
, depth
+ 2);
3475 * Log device section
3478 if (num_logs(newnv
) > 0) {
3479 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3480 !cb
->cb_vdev_names
) {
3481 print_iostat_dashes(cb
, 0, "logs");
3484 for (c
= 0; c
< children
; c
++) {
3485 uint64_t islog
= B_FALSE
;
3486 (void) nvlist_lookup_uint64(newchild
[c
],
3487 ZPOOL_CONFIG_IS_LOG
, &islog
);
3490 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3492 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3493 oldchild
[c
] : NULL
, newchild
[c
],
3502 * Include level 2 ARC devices in iostat output
3504 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3505 &newchild
, &children
) != 0)
3508 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3509 &oldchild
, &c
) != 0)
3513 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3514 !cb
->cb_vdev_names
) {
3515 print_iostat_dashes(cb
, 0, "cache");
3518 for (c
= 0; c
< children
; c
++) {
3519 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3521 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3522 : NULL
, newchild
[c
], cb
, depth
+ 2);
3531 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3533 iostat_cbdata_t
*cb
= data
;
3537 * If the pool has disappeared, remove it from the list and continue.
3539 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3543 pool_list_remove(cb
->cb_list
, zhp
);
3549 * Callback to print out the iostats for the given pool.
3552 print_iostat(zpool_handle_t
*zhp
, void *data
)
3554 iostat_cbdata_t
*cb
= data
;
3555 nvlist_t
*oldconfig
, *newconfig
;
3556 nvlist_t
*oldnvroot
, *newnvroot
;
3559 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3561 if (cb
->cb_iteration
== 1)
3564 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3567 if (oldconfig
== NULL
)
3570 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3573 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3575 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3576 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3577 print_iostat_separator(cb
);
3590 if (isatty(STDOUT_FILENO
)) {
3591 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
3593 columns
= ws
.ws_col
;
3602 get_namewidth(zpool_handle_t
*zhp
, void *data
)
3604 iostat_cbdata_t
*cb
= data
;
3605 nvlist_t
*config
, *nvroot
;
3608 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
3609 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3611 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
3612 if (!cb
->cb_verbose
)
3613 cb
->cb_namewidth
= poolname_len
;
3615 cb
->cb_namewidth
= MAX(poolname_len
,
3616 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
3617 cb
->cb_name_flags
));
3620 * The width must be at least 10, but may be as large as the
3621 * column width - 42 so that we can still fit in one line.
3623 columns
= get_columns();
3625 if (cb
->cb_namewidth
< 10)
3626 cb
->cb_namewidth
= 10;
3627 if (cb
->cb_namewidth
> columns
- 42)
3628 cb
->cb_namewidth
= columns
- 42;
3634 * Parse the input string, get the 'interval' and 'count' value if there is one.
3637 get_interval_count(int *argcp
, char **argv
, float *iv
,
3641 unsigned long count
= 0;
3645 * Determine if the last argument is an integer or a pool name
3647 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3651 interval
= strtof(argv
[argc
- 1], &end
);
3653 if (*end
== '\0' && errno
== 0) {
3654 if (interval
== 0) {
3655 (void) fprintf(stderr
, gettext("interval "
3656 "cannot be zero\n"));
3660 * Ignore the last parameter
3665 * If this is not a valid number, just plow on. The
3666 * user will get a more informative error message later
3674 * If the last argument is also an integer, then we have both a count
3677 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3682 interval
= strtof(argv
[argc
- 1], &end
);
3684 if (*end
== '\0' && errno
== 0) {
3685 if (interval
== 0) {
3686 (void) fprintf(stderr
, gettext("interval "
3687 "cannot be zero\n"));
3692 * Ignore the last parameter
3706 get_timestamp_arg(char c
)
3709 timestamp_fmt
= UDATE
;
3711 timestamp_fmt
= DDATE
;
3717 * Return stat flags that are supported by all pools by both the module and
3718 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3719 * It will get ANDed down until only the flags that are supported on all pools
3723 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
3725 uint64_t *mask
= data
;
3726 nvlist_t
*config
, *nvroot
, *nvx
;
3730 config
= zpool_get_config(zhp
, NULL
);
3731 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3734 /* Default stats are always supported, but for completeness.. */
3735 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
3736 flags
|= IOS_DEFAULT_M
;
3738 /* Get our extended stats nvlist from the main list */
3739 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3742 * No extended stats; they're probably running an older
3743 * module. No big deal, we support that too.
3748 /* For each extended stat, make sure all its nvpairs are supported */
3749 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
3750 if (!vsx_type_to_nvlist
[j
][0])
3753 /* Start off by assuming the flag is supported, then check */
3754 flags
|= (1ULL << j
);
3755 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
3756 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
3757 /* flag isn't supported */
3758 flags
= flags
& ~(1ULL << j
);
3764 *mask
= *mask
& flags
;
3769 * Return a bitmask of stats that are supported on all pools by both the module
3773 get_stat_flags(zpool_list_t
*list
)
3778 * get_stat_flags_cb() will lop off bits from "mask" until only the
3779 * flags that are supported on all pools remain.
3781 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
3786 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3789 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
3791 iostat_cbdata_t
*cb
= cb_data
;
3795 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
3797 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
3798 ret
= 1; /* match */
3805 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3808 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
3810 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
3814 * Check if vdevs are in a pool
3816 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3817 * return 0. If pool_name is NULL, then search all pools.
3820 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
3821 iostat_cbdata_t
*cb
)
3828 if ((argc
== 0) || !*argv
)
3834 /* Temporarily hijack cb_vdev_names for a second... */
3835 tmp_name
= cb
->cb_vdev_names
;
3837 /* Go though our list of prospective vdev names */
3838 for (i
= 0; i
< argc
; i
++) {
3839 cb
->cb_vdev_names
= argv
+ i
;
3841 /* Is this name a vdev in our pools? */
3842 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
3850 cb
->cb_vdev_names
= tmp_name
;
3856 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
3859 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
3866 * Do we have a pool named *name? If so, return 1, otherwise 0.
3871 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
3874 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
3876 are_all_pools(int argc
, char **argv
)
3878 if ((argc
== 0) || !*argv
)
3882 if (!is_pool(argv
[argc
]))
3889 * Helper function to print out vdev/pool names we can't resolve. Used for an
3893 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
3894 iostat_cbdata_t
*cb
)
3899 for (i
= 0; i
< argc
; i
++) {
3903 str
= gettext("pool");
3904 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
3905 str
= gettext("vdev in this pool");
3906 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
3907 str
= gettext("vdev in another pool");
3909 str
= gettext("unknown");
3911 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
3916 * Same as get_interval_count(), but with additional checks to not misinterpret
3917 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
3921 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
3922 unsigned long *count
, iostat_cbdata_t
*cb
)
3924 char **tmpargv
= argv
;
3925 int argc_for_interval
= 0;
3927 /* Is the last arg an interval value? Or a guid? */
3928 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
3930 * The last arg is not a guid, so it's probably an
3933 argc_for_interval
++;
3936 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
3938 * The 2nd to last arg is not a guid, so it's probably
3939 * an interval value.
3941 argc_for_interval
++;
3945 /* Point to our list of possible intervals */
3946 tmpargv
= &argv
[*argc
- argc_for_interval
];
3948 *argc
= *argc
- argc_for_interval
;
3949 get_interval_count(&argc_for_interval
, tmpargv
,
3954 * Floating point sleep(). Allows you to pass in a floating point value for
3960 struct timespec req
;
3961 req
.tv_sec
= floor(sec
);
3962 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
3963 nanosleep(&req
, NULL
);
3968 * zpool iostat [-c CMD] [-ghHLpPvy] [[-lq]|[-r|-w]] [-n name] [-T d|u]
3969 * [[ pool ...]|[pool vdev ...]|[vdev ...]]
3970 * [interval [count]]
3972 * -c CMD For each vdev, run command CMD
3973 * -g Display guid for individual vdev name.
3974 * -L Follow links when resolving vdev path name.
3975 * -P Display full path for vdev name.
3976 * -v Display statistics for individual vdevs
3978 * -p Display values in parsable (exact) format.
3979 * -H Scripted mode. Don't display headers, and separate properties
3981 * -l Display average latency
3982 * -q Display queue depths
3983 * -w Display latency histograms
3984 * -r Display request size histogram
3985 * -T Display a timestamp in date(1) or Unix format
3987 * This command can be tricky because we want to be able to deal with pool
3988 * creation/destruction as well as vdev configuration changes. The bulk of this
3989 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3990 * on pool_list_update() to detect the addition of new pools. Configuration
3991 * changes are all handled within libzfs.
3994 zpool_do_iostat(int argc
, char **argv
)
4000 unsigned long count
= 0;
4002 boolean_t verbose
= B_FALSE
;
4003 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4004 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4005 boolean_t omit_since_boot
= B_FALSE
;
4006 boolean_t guid
= B_FALSE
;
4007 boolean_t follow_links
= B_FALSE
;
4008 boolean_t full_name
= B_FALSE
;
4009 iostat_cbdata_t cb
= { 0 };
4012 /* Used for printing error message */
4013 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4014 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4016 uint64_t unsupported_flags
;
4019 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4028 follow_links
= B_TRUE
;
4034 get_timestamp_arg(*optarg
);
4058 omit_since_boot
= B_TRUE
;
4064 if (optopt
== 'c') {
4066 gettext("Missing CMD for -c\n"));
4069 gettext("invalid option '%c'\n"), optopt
);
4078 cb
.cb_literal
= parsable
;
4079 cb
.cb_scripted
= scripted
;
4082 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4084 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4086 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4087 cb
.cb_iteration
= 0;
4088 cb
.cb_namewidth
= 0;
4089 cb
.cb_verbose
= verbose
;
4091 /* Get our interval and count values (if any) */
4093 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4096 get_interval_count(&argc
, argv
, &interval
, &count
);
4100 /* No args, so just print the defaults. */
4101 } else if (are_all_pools(argc
, argv
)) {
4102 /* All the args are pool names */
4103 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4104 /* All the args are vdevs */
4105 cb
.cb_vdev_names
= argv
;
4106 cb
.cb_vdev_names_count
= argc
;
4107 argc
= 0; /* No pools to process */
4108 } else if (are_all_pools(1, argv
)) {
4109 /* The first arg is a pool name */
4110 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4111 /* ...and the rest are vdev names */
4112 cb
.cb_vdev_names
= argv
+ 1;
4113 cb
.cb_vdev_names_count
= argc
- 1;
4114 argc
= 1; /* One pool to process */
4116 fprintf(stderr
, gettext("Expected either a list of "));
4117 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4118 fprintf(stderr
, " \"%s\", ", argv
[0]);
4119 fprintf(stderr
, gettext("but got:\n"));
4120 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4122 fprintf(stderr
, "\n");
4128 * The args don't make sense. The first arg isn't a pool name,
4129 * nor are all the args vdevs.
4131 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4132 fprintf(stderr
, "\n");
4136 if (cb
.cb_vdev_names_count
!= 0) {
4138 * If user specified vdevs, it implies verbose.
4140 cb
.cb_verbose
= B_TRUE
;
4144 * Construct the list of all interesting pools.
4147 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4150 if (pool_list_count(list
) == 0 && argc
!= 0) {
4151 pool_list_free(list
);
4155 if (pool_list_count(list
) == 0 && interval
== 0) {
4156 pool_list_free(list
);
4157 (void) fprintf(stderr
, gettext("no pools available\n"));
4161 if ((l_histo
|| rq_histo
) && (queues
|| latency
)) {
4162 pool_list_free(list
);
4163 (void) fprintf(stderr
,
4164 gettext("[-r|-w] isn't allowed with [-q|-l]\n"));
4169 if (l_histo
&& rq_histo
) {
4170 pool_list_free(list
);
4171 (void) fprintf(stderr
,
4172 gettext("Only one of [-r|-w] can be passed at a time\n"));
4178 * Enter the main iostat loop.
4184 * Histograms tables look out of place when you try to display
4185 * them with the other stats, so make a rule that you can only
4186 * print histograms by themselves.
4188 cb
.cb_flags
= IOS_L_HISTO_M
;
4189 } else if (rq_histo
) {
4190 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4192 cb
.cb_flags
= IOS_DEFAULT_M
;
4194 cb
.cb_flags
|= IOS_LATENCY_M
;
4196 cb
.cb_flags
|= IOS_QUEUES_M
;
4200 * See if the module supports all the stats we want to display.
4202 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4203 if (unsupported_flags
) {
4207 gettext("The loaded zfs module doesn't support:"));
4209 /* for each bit set in unsupported_flags */
4210 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4211 idx
= lowbit64(f
) - 1;
4212 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4215 fprintf(stderr
, ". Try running a newer module.\n");
4216 pool_list_free(list
);
4222 if ((npools
= pool_list_count(list
)) == 0)
4223 (void) fprintf(stderr
, gettext("no pools available\n"));
4226 * If this is the first iteration and -y was supplied
4227 * we skip any printing.
4229 boolean_t skip
= (omit_since_boot
&&
4230 cb
.cb_iteration
== 0);
4233 * Refresh all statistics. This is done as an
4234 * explicit step before calculating the maximum name
4235 * width, so that any * configuration changes are
4236 * properly accounted for.
4238 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4242 * Iterate over all pools to determine the maximum width
4243 * for the pool / device name column across all pools.
4245 cb
.cb_namewidth
= 0;
4246 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4249 if (timestamp_fmt
!= NODATE
)
4250 print_timestamp(timestamp_fmt
);
4253 * If it's the first time and we're not skipping it,
4254 * or either skip or verbose mode, print the header.
4256 * The histogram code explicitly prints its header on
4257 * every vdev, so skip this for histograms.
4259 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4260 (skip
!= verbose
)) &&
4261 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4263 print_iostat_header(&cb
);
4266 (void) fsleep(interval
);
4270 if (cmd
!= NULL
&& cb
.cb_verbose
)
4271 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4272 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4273 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4275 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4277 if (cb
.vcdl
!= NULL
)
4278 free_vdev_cmd_data_list(cb
.vcdl
);
4281 * If there's more than one pool, and we're not in
4282 * verbose mode (which prints a separator for us),
4283 * then print a separator.
4285 * In addition, if we're printing specific vdevs then
4286 * we also want an ending separator.
4288 if (((npools
> 1 && !verbose
&&
4289 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4290 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4291 cb
.cb_vdev_names_count
)) &&
4293 print_iostat_separator(&cb
);
4298 * Flush the output so that redirection to a file isn't buffered
4301 (void) fflush(stdout
);
4306 if (count
!= 0 && --count
== 0)
4309 (void) fsleep(interval
);
4312 pool_list_free(list
);
4317 typedef struct list_cbdata
{
4318 boolean_t cb_verbose
;
4321 boolean_t cb_scripted
;
4322 zprop_list_t
*cb_proplist
;
4323 boolean_t cb_literal
;
4327 * Given a list of columns to display, output appropriate headers for each one.
4330 print_header(list_cbdata_t
*cb
)
4332 zprop_list_t
*pl
= cb
->cb_proplist
;
4333 char headerbuf
[ZPOOL_MAXPROPLEN
];
4335 boolean_t first
= B_TRUE
;
4336 boolean_t right_justify
;
4339 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4340 width
= pl
->pl_width
;
4341 if (first
&& cb
->cb_verbose
) {
4343 * Reset the width to accommodate the verbose listing
4346 width
= cb
->cb_namewidth
;
4354 right_justify
= B_FALSE
;
4355 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4356 header
= zpool_prop_column_name(pl
->pl_prop
);
4357 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4361 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4362 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4363 headerbuf
[i
] = '\0';
4367 if (pl
->pl_next
== NULL
&& !right_justify
)
4368 (void) printf("%s", header
);
4369 else if (right_justify
)
4370 (void) printf("%*s", (int)width
, header
);
4372 (void) printf("%-*s", (int)width
, header
);
4375 (void) printf("\n");
4379 * Given a pool and a list of properties, print out all the properties according
4380 * to the described layout.
4383 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4385 zprop_list_t
*pl
= cb
->cb_proplist
;
4386 boolean_t first
= B_TRUE
;
4387 char property
[ZPOOL_MAXPROPLEN
];
4389 boolean_t right_justify
;
4392 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4394 width
= pl
->pl_width
;
4395 if (first
&& cb
->cb_verbose
) {
4397 * Reset the width to accommodate the verbose listing
4400 width
= cb
->cb_namewidth
;
4404 if (cb
->cb_scripted
)
4405 (void) printf("\t");
4412 right_justify
= B_FALSE
;
4413 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4414 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4415 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4420 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4421 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4422 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4423 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4424 sizeof (property
)) == 0) {
4432 * If this is being called in scripted mode, or if this is the
4433 * last column and it is left-justified, don't include a width
4436 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4437 (void) printf("%s", propstr
);
4438 else if (right_justify
)
4439 (void) printf("%*s", (int)width
, propstr
);
4441 (void) printf("%-*s", (int)width
, propstr
);
4444 (void) printf("\n");
4448 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4449 boolean_t valid
, enum zfs_nicenum_format format
)
4453 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4456 case ZPOOL_PROP_EXPANDSZ
:
4458 (void) strlcpy(propval
, "-", sizeof (propval
));
4460 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4463 case ZPOOL_PROP_FRAGMENTATION
:
4464 if (value
== ZFS_FRAG_INVALID
) {
4465 (void) strlcpy(propval
, "-", sizeof (propval
));
4466 } else if (format
== ZFS_NICENUM_RAW
) {
4467 (void) snprintf(propval
, sizeof (propval
), "%llu",
4468 (unsigned long long)value
);
4470 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4471 (unsigned long long)value
);
4474 case ZPOOL_PROP_CAPACITY
:
4475 if (format
== ZFS_NICENUM_RAW
)
4476 (void) snprintf(propval
, sizeof (propval
), "%llu",
4477 (unsigned long long)value
);
4479 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4480 (unsigned long long)value
);
4483 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
4487 (void) strlcpy(propval
, "-", sizeof (propval
));
4490 (void) printf("\t%s", propval
);
4492 (void) printf(" %*s", (int)width
, propval
);
4496 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
4497 list_cbdata_t
*cb
, int depth
)
4503 boolean_t scripted
= cb
->cb_scripted
;
4504 uint64_t islog
= B_FALSE
;
4505 boolean_t haslog
= B_FALSE
;
4506 char *dashes
= "%-*s - - - - - -\n";
4508 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4509 (uint64_t **)&vs
, &c
) == 0);
4512 boolean_t toplevel
= (vs
->vs_space
!= 0);
4514 enum zfs_nicenum_format format
;
4517 format
= ZFS_NICENUM_RAW
;
4519 format
= ZFS_NICENUM_1024
;
4522 (void) printf("\t%s", name
);
4523 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
4524 (void) printf("%*s%s", depth
, "", name
);
4526 (void) printf("%*s%s%*s", depth
, "", name
,
4527 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
4530 * Print the properties for the individual vdevs. Some
4531 * properties are only applicable to toplevel vdevs. The
4532 * 'toplevel' boolean value is passed to the print_one_column()
4533 * to indicate that the value is valid.
4535 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
4537 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
4539 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
4540 scripted
, toplevel
, format
);
4541 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
4543 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
4544 vs
->vs_fragmentation
, scripted
,
4545 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
4547 cap
= (vs
->vs_space
== 0) ? 0 :
4548 (vs
->vs_alloc
* 100 / vs
->vs_space
);
4549 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
4551 (void) printf("\n");
4554 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
4555 &child
, &children
) != 0)
4558 for (c
= 0; c
< children
; c
++) {
4559 uint64_t ishole
= B_FALSE
;
4561 if (nvlist_lookup_uint64(child
[c
],
4562 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
4565 if (nvlist_lookup_uint64(child
[c
],
4566 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
4571 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4573 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4577 if (haslog
== B_TRUE
) {
4578 /* LINTED E_SEC_PRINTF_VAR_FMT */
4579 (void) printf(dashes
, cb
->cb_namewidth
, "log");
4580 for (c
= 0; c
< children
; c
++) {
4581 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
4582 &islog
) != 0 || !islog
)
4584 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4586 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4591 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
4592 &child
, &children
) == 0 && children
> 0) {
4593 /* LINTED E_SEC_PRINTF_VAR_FMT */
4594 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
4595 for (c
= 0; c
< children
; c
++) {
4596 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4598 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4603 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
4604 &children
) == 0 && children
> 0) {
4605 /* LINTED E_SEC_PRINTF_VAR_FMT */
4606 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
4607 for (c
= 0; c
< children
; c
++) {
4608 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4610 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4618 * Generic callback function to list a pool.
4621 list_callback(zpool_handle_t
*zhp
, void *data
)
4623 list_cbdata_t
*cbp
= data
;
4627 config
= zpool_get_config(zhp
, NULL
);
4629 print_pool(zhp
, cbp
);
4630 if (!cbp
->cb_verbose
)
4633 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4635 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
4641 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4643 * -g Display guid for individual vdev name.
4644 * -H Scripted mode. Don't display headers, and separate properties
4646 * -L Follow links when resolving vdev path name.
4647 * -o List of properties to display. Defaults to
4648 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4649 * "dedupratio,health,altroot"
4650 * -p Display values in parsable (exact) format.
4651 * -P Display full path for vdev name.
4652 * -T Display a timestamp in date(1) or Unix format
4654 * List all pools in the system, whether or not they're healthy. Output space
4655 * statistics for each one, as well as health status summary.
4658 zpool_do_list(int argc
, char **argv
)
4662 list_cbdata_t cb
= { 0 };
4663 static char default_props
[] =
4664 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4665 "dedupratio,health,altroot";
4666 char *props
= default_props
;
4668 unsigned long count
= 0;
4670 boolean_t first
= B_TRUE
;
4673 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
4676 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4679 cb
.cb_scripted
= B_TRUE
;
4682 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4688 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4691 cb
.cb_literal
= B_TRUE
;
4694 get_timestamp_arg(*optarg
);
4697 cb
.cb_verbose
= B_TRUE
;
4700 (void) fprintf(stderr
, gettext("missing argument for "
4701 "'%c' option\n"), optopt
);
4705 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4714 get_interval_count(&argc
, argv
, &interval
, &count
);
4716 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
4720 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
4724 if (pool_list_count(list
) == 0)
4727 if (timestamp_fmt
!= NODATE
)
4728 print_timestamp(timestamp_fmt
);
4730 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
4734 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
4739 if (count
!= 0 && --count
== 0)
4742 pool_list_free(list
);
4743 (void) fsleep(interval
);
4746 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
4747 (void) printf(gettext("no pools available\n"));
4751 pool_list_free(list
);
4752 zprop_free_list(cb
.cb_proplist
);
4757 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
4759 boolean_t force
= B_FALSE
;
4762 char *poolname
, *old_disk
, *new_disk
;
4763 zpool_handle_t
*zhp
;
4764 nvlist_t
*props
= NULL
;
4769 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
4775 if ((propval
= strchr(optarg
, '=')) == NULL
) {
4776 (void) fprintf(stderr
, gettext("missing "
4777 "'=' for -o option\n"));
4783 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
4784 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
4788 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4797 /* get pool name and check number of arguments */
4799 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4806 (void) fprintf(stderr
,
4807 gettext("missing <device> specification\n"));
4815 (void) fprintf(stderr
,
4816 gettext("missing <new_device> specification\n"));
4819 new_disk
= old_disk
;
4829 (void) fprintf(stderr
, gettext("too many arguments\n"));
4833 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
4838 if (zpool_get_config(zhp
, NULL
) == NULL
) {
4839 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
4846 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
4848 if (nvroot
== NULL
) {
4854 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
4857 nvlist_free(nvroot
);
4864 * zpool replace [-f] <pool> <device> <new_device>
4866 * -f Force attach, even if <new_device> appears to be in use.
4868 * Replace <device> with <new_device>.
4872 zpool_do_replace(int argc
, char **argv
)
4874 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
4878 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
4880 * -f Force attach, even if <new_device> appears to be in use.
4881 * -o Set property=value.
4883 * Attach <new_device> to the mirror containing <device>. If <device> is not
4884 * part of a mirror, then <device> will be transformed into a mirror of
4885 * <device> and <new_device>. In either case, <new_device> will begin life
4886 * with a DTL of [0, now], and will immediately begin to resilver itself.
4889 zpool_do_attach(int argc
, char **argv
)
4891 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
4895 * zpool detach [-f] <pool> <device>
4897 * -f Force detach of <device>, even if DTLs argue against it
4898 * (not supported yet)
4900 * Detach a device from a mirror. The operation will be refused if <device>
4901 * is the last device in the mirror, or if the DTLs indicate that this device
4902 * has the only valid copy of some data.
4906 zpool_do_detach(int argc
, char **argv
)
4909 char *poolname
, *path
;
4910 zpool_handle_t
*zhp
;
4914 while ((c
= getopt(argc
, argv
, "f")) != -1) {
4918 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4927 /* get pool name and check number of arguments */
4929 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4934 (void) fprintf(stderr
,
4935 gettext("missing <device> specification\n"));
4942 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
4945 ret
= zpool_vdev_detach(zhp
, path
);
4953 * zpool split [-gLnP] [-o prop=val] ...
4955 * [-R altroot] <pool> <newpool> [<device> ...]
4957 * -g Display guid for individual vdev name.
4958 * -L Follow links when resolving vdev path name.
4959 * -n Do not split the pool, but display the resulting layout if
4960 * it were to be split.
4961 * -o Set property=value, or set mount options.
4962 * -P Display full path for vdev name.
4963 * -R Mount the split-off pool under an alternate root.
4965 * Splits the named pool and gives it the new pool name. Devices to be split
4966 * off may be listed, provided that no more than one device is specified
4967 * per top-level vdev mirror. The newly split pool is left in an exported
4968 * state unless -R is specified.
4970 * Restrictions: the top-level of the pool pool must only be made up of
4971 * mirrors; all devices in the pool must be healthy; no device may be
4972 * undergoing a resilvering operation.
4975 zpool_do_split(int argc
, char **argv
)
4977 char *srcpool
, *newpool
, *propval
;
4978 char *mntopts
= NULL
;
4981 zpool_handle_t
*zhp
;
4982 nvlist_t
*config
, *props
= NULL
;
4984 flags
.dryrun
= B_FALSE
;
4985 flags
.import
= B_FALSE
;
4986 flags
.name_flags
= 0;
4989 while ((c
= getopt(argc
, argv
, ":gLR:no:P")) != -1) {
4992 flags
.name_flags
|= VDEV_NAME_GUID
;
4995 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4998 flags
.import
= B_TRUE
;
5000 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5001 &props
, B_TRUE
) != 0) {
5007 flags
.dryrun
= B_TRUE
;
5010 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5013 if (add_prop_list(optarg
, propval
,
5014 &props
, B_TRUE
) != 0) {
5023 flags
.name_flags
|= VDEV_NAME_PATH
;
5026 (void) fprintf(stderr
, gettext("missing argument for "
5027 "'%c' option\n"), optopt
);
5031 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5038 if (!flags
.import
&& mntopts
!= NULL
) {
5039 (void) fprintf(stderr
, gettext("setting mntopts is only "
5040 "valid when importing the pool\n"));
5048 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5052 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5062 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5067 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5068 if (config
== NULL
) {
5072 (void) printf(gettext("would create '%s' with the "
5073 "following layout:\n\n"), newpool
);
5074 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5081 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5082 nvlist_free(config
);
5088 * The split was successful. Now we need to open the new
5089 * pool and import it.
5091 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5092 nvlist_free(config
);
5096 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5097 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5099 (void) fprintf(stderr
, gettext("Split was successful, but "
5100 "the datasets could not all be mounted\n"));
5101 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5102 "different altroot\n"), "zpool import");
5105 nvlist_free(config
);
5114 * zpool online <pool> <device> ...
5117 zpool_do_online(int argc
, char **argv
)
5121 zpool_handle_t
*zhp
;
5123 vdev_state_t newstate
;
5127 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5130 flags
|= ZFS_ONLINE_EXPAND
;
5134 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5143 /* get pool name and check number of arguments */
5145 (void) fprintf(stderr
, gettext("missing pool name\n"));
5149 (void) fprintf(stderr
, gettext("missing device name\n"));
5155 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5158 for (i
= 1; i
< argc
; i
++) {
5159 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5160 if (newstate
!= VDEV_STATE_HEALTHY
) {
5161 (void) printf(gettext("warning: device '%s' "
5162 "onlined, but remains in faulted state\n"),
5164 if (newstate
== VDEV_STATE_FAULTED
)
5165 (void) printf(gettext("use 'zpool "
5166 "clear' to restore a faulted "
5169 (void) printf(gettext("use 'zpool "
5170 "replace' to replace devices "
5171 "that are no longer present\n"));
5184 * zpool offline [-ft] <pool> <device> ...
5186 * -f Force the device into the offline state, even if doing
5187 * so would appear to compromise pool availability.
5188 * (not supported yet)
5190 * -t Only take the device off-line temporarily. The offline
5191 * state will not be persistent across reboots.
5195 zpool_do_offline(int argc
, char **argv
)
5199 zpool_handle_t
*zhp
;
5201 boolean_t istmp
= B_FALSE
;
5204 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5211 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5220 /* get pool name and check number of arguments */
5222 (void) fprintf(stderr
, gettext("missing pool name\n"));
5226 (void) fprintf(stderr
, gettext("missing device name\n"));
5232 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5235 for (i
= 1; i
< argc
; i
++) {
5236 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5246 * zpool clear <pool> [device]
5248 * Clear all errors associated with a pool or a particular device.
5251 zpool_do_clear(int argc
, char **argv
)
5255 boolean_t dryrun
= B_FALSE
;
5256 boolean_t do_rewind
= B_FALSE
;
5257 boolean_t xtreme_rewind
= B_FALSE
;
5258 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5259 nvlist_t
*policy
= NULL
;
5260 zpool_handle_t
*zhp
;
5261 char *pool
, *device
;
5264 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5273 xtreme_rewind
= B_TRUE
;
5276 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5286 (void) fprintf(stderr
, gettext("missing pool name\n"));
5291 (void) fprintf(stderr
, gettext("too many arguments\n"));
5295 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5296 (void) fprintf(stderr
,
5297 gettext("-n or -X only meaningful with -F\n"));
5301 rewind_policy
= ZPOOL_TRY_REWIND
;
5303 rewind_policy
= ZPOOL_DO_REWIND
;
5305 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5307 /* In future, further rewind policy choices can be passed along here */
5308 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5309 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
5313 device
= argc
== 2 ? argv
[1] : NULL
;
5315 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5316 nvlist_free(policy
);
5320 if (zpool_clear(zhp
, device
, policy
) != 0)
5325 nvlist_free(policy
);
5331 * zpool reguid <pool>
5334 zpool_do_reguid(int argc
, char **argv
)
5338 zpool_handle_t
*zhp
;
5342 while ((c
= getopt(argc
, argv
, "")) != -1) {
5345 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5354 /* get pool name and check number of arguments */
5356 (void) fprintf(stderr
, gettext("missing pool name\n"));
5361 (void) fprintf(stderr
, gettext("too many arguments\n"));
5366 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5369 ret
= zpool_reguid(zhp
);
5377 * zpool reopen <pool>
5379 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5382 zpool_do_reopen(int argc
, char **argv
)
5386 zpool_handle_t
*zhp
;
5390 while ((c
= getopt(argc
, argv
, "")) != -1) {
5393 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5403 (void) fprintf(stderr
, gettext("missing pool name\n"));
5408 (void) fprintf(stderr
, gettext("too many arguments\n"));
5413 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
5416 ret
= zpool_reopen(zhp
);
5421 typedef struct scrub_cbdata
{
5428 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5430 scrub_cbdata_t
*cb
= data
;
5434 * Ignore faulted pools.
5436 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5437 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
5438 "currently unavailable\n"), zpool_get_name(zhp
));
5442 err
= zpool_scan(zhp
, cb
->cb_type
);
5448 * zpool scrub [-s] <pool> ...
5450 * -s Stop. Stops any in-progress scrub.
5453 zpool_do_scrub(int argc
, char **argv
)
5458 cb
.cb_type
= POOL_SCAN_SCRUB
;
5461 while ((c
= getopt(argc
, argv
, "s")) != -1) {
5464 cb
.cb_type
= POOL_SCAN_NONE
;
5467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5479 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5483 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
5487 * Print out detailed scrub status.
5490 print_scan_status(pool_scan_stat_t
*ps
)
5493 uint64_t elapsed
, mins_left
, hours_left
;
5494 uint64_t pass_exam
, examined
, total
;
5496 double fraction_done
;
5497 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
5499 (void) printf(gettext(" scan: "));
5501 /* If there's never been a scan, there's not much to say. */
5502 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
5503 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
5504 (void) printf(gettext("none requested\n"));
5508 start
= ps
->pss_start_time
;
5509 end
= ps
->pss_end_time
;
5510 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
5512 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
5513 ps
->pss_func
== POOL_SCAN_RESILVER
);
5515 * Scan is finished or canceled.
5517 if (ps
->pss_state
== DSS_FINISHED
) {
5518 uint64_t minutes_taken
= (end
- start
) / 60;
5521 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5522 fmt
= gettext("scrub repaired %s in %lluh%um with "
5523 "%llu errors on %s");
5524 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5525 fmt
= gettext("resilvered %s in %lluh%um with "
5526 "%llu errors on %s");
5529 (void) printf(fmt
, processed_buf
,
5530 (u_longlong_t
)(minutes_taken
/ 60),
5531 (uint_t
)(minutes_taken
% 60),
5532 (u_longlong_t
)ps
->pss_errors
,
5533 ctime((time_t *)&end
));
5535 } else if (ps
->pss_state
== DSS_CANCELED
) {
5536 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5537 (void) printf(gettext("scrub canceled on %s"),
5539 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5540 (void) printf(gettext("resilver canceled on %s"),
5546 assert(ps
->pss_state
== DSS_SCANNING
);
5549 * Scan is in progress.
5551 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5552 (void) printf(gettext("scrub in progress since %s"),
5554 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5555 (void) printf(gettext("resilver in progress since %s"),
5559 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
5560 total
= ps
->pss_to_examine
;
5561 fraction_done
= (double)examined
/ total
;
5563 /* elapsed time for this pass */
5564 elapsed
= time(NULL
) - ps
->pss_pass_start
;
5565 elapsed
= elapsed
? elapsed
: 1;
5566 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
5567 rate
= pass_exam
/ elapsed
;
5568 rate
= rate
? rate
: 1;
5569 mins_left
= ((total
- examined
) / rate
) / 60;
5570 hours_left
= mins_left
/ 60;
5572 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
5573 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
5574 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
5577 * do not print estimated time if hours_left is more than 30 days
5579 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5580 examined_buf
, total_buf
, rate_buf
);
5581 if (hours_left
< (30 * 24)) {
5582 (void) printf(gettext(", %lluh%um to go\n"),
5583 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
5585 (void) printf(gettext(
5586 ", (scan is slow, no estimated time)\n"));
5589 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5590 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5591 processed_buf
, 100 * fraction_done
);
5592 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5593 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5594 processed_buf
, 100 * fraction_done
);
5599 print_error_log(zpool_handle_t
*zhp
)
5601 nvlist_t
*nverrlist
= NULL
;
5604 size_t len
= MAXPATHLEN
* 2;
5606 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
5607 (void) printf("errors: List of errors unavailable "
5608 "(insufficient privileges)\n");
5612 (void) printf("errors: Permanent errors have been "
5613 "detected in the following files:\n\n");
5615 pathname
= safe_malloc(len
);
5617 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
5619 uint64_t dsobj
, obj
;
5621 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
5622 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
5624 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
5626 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
5627 (void) printf("%7s %s\n", "", pathname
);
5630 nvlist_free(nverrlist
);
5634 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
5643 (void) printf(gettext("\tspares\n"));
5645 for (i
= 0; i
< nspares
; i
++) {
5646 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
5648 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
5654 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
5663 (void) printf(gettext("\tcache\n"));
5665 for (i
= 0; i
< nl2cache
; i
++) {
5666 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
5668 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
5674 print_dedup_stats(nvlist_t
*config
)
5676 ddt_histogram_t
*ddh
;
5682 * If the pool was faulted then we may not have been able to
5683 * obtain the config. Otherwise, if we have anything in the dedup
5684 * table continue processing the stats.
5686 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
5687 (uint64_t **)&ddo
, &c
) != 0)
5690 (void) printf("\n");
5691 (void) printf(gettext(" dedup: "));
5692 if (ddo
->ddo_count
== 0) {
5693 (void) printf(gettext("no DDT entries\n"));
5697 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5698 (u_longlong_t
)ddo
->ddo_count
,
5699 (u_longlong_t
)ddo
->ddo_dspace
,
5700 (u_longlong_t
)ddo
->ddo_mspace
);
5702 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
5703 (uint64_t **)&dds
, &c
) == 0);
5704 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
5705 (uint64_t **)&ddh
, &c
) == 0);
5706 zpool_dump_ddt(dds
, ddh
);
5710 * Display a summary of pool status. Displays a summary such as:
5714 * reason: One or more devices ...
5715 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5721 * When given the '-v' option, we print out the complete config. If the '-e'
5722 * option is specified, then we print out error rate information as well.
5725 status_callback(zpool_handle_t
*zhp
, void *data
)
5727 status_cbdata_t
*cbp
= data
;
5728 nvlist_t
*config
, *nvroot
;
5730 zpool_status_t reason
;
5731 zpool_errata_t errata
;
5736 config
= zpool_get_config(zhp
, NULL
);
5737 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
5742 * If we were given 'zpool status -x', only report those pools with
5745 if (cbp
->cb_explain
&&
5746 (reason
== ZPOOL_STATUS_OK
||
5747 reason
== ZPOOL_STATUS_VERSION_OLDER
||
5748 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
5749 if (!cbp
->cb_allpools
) {
5750 (void) printf(gettext("pool '%s' is healthy\n"),
5751 zpool_get_name(zhp
));
5753 cbp
->cb_first
= B_FALSE
;
5759 cbp
->cb_first
= B_FALSE
;
5761 (void) printf("\n");
5763 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5765 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
5766 (uint64_t **)&vs
, &c
) == 0);
5767 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5769 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
5770 (void) printf(gettext(" state: %s\n"), health
);
5773 case ZPOOL_STATUS_MISSING_DEV_R
:
5774 (void) printf(gettext("status: One or more devices could not "
5775 "be opened. Sufficient replicas exist for\n\tthe pool to "
5776 "continue functioning in a degraded state.\n"));
5777 (void) printf(gettext("action: Attach the missing device and "
5778 "online it using 'zpool online'.\n"));
5781 case ZPOOL_STATUS_MISSING_DEV_NR
:
5782 (void) printf(gettext("status: One or more devices could not "
5783 "be opened. There are insufficient\n\treplicas for the "
5784 "pool to continue functioning.\n"));
5785 (void) printf(gettext("action: Attach the missing device and "
5786 "online it using 'zpool online'.\n"));
5789 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
5790 (void) printf(gettext("status: One or more devices could not "
5791 "be used because the label is missing or\n\tinvalid. "
5792 "Sufficient replicas exist for the pool to continue\n\t"
5793 "functioning in a degraded state.\n"));
5794 (void) printf(gettext("action: Replace the device using "
5795 "'zpool replace'.\n"));
5798 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
5799 (void) printf(gettext("status: One or more devices could not "
5800 "be used because the label is missing \n\tor invalid. "
5801 "There are insufficient replicas for the pool to "
5802 "continue\n\tfunctioning.\n"));
5803 zpool_explain_recover(zpool_get_handle(zhp
),
5804 zpool_get_name(zhp
), reason
, config
);
5807 case ZPOOL_STATUS_FAILING_DEV
:
5808 (void) printf(gettext("status: One or more devices has "
5809 "experienced an unrecoverable error. An\n\tattempt was "
5810 "made to correct the error. Applications are "
5812 (void) printf(gettext("action: Determine if the device needs "
5813 "to be replaced, and clear the errors\n\tusing "
5814 "'zpool clear' or replace the device with 'zpool "
5818 case ZPOOL_STATUS_OFFLINE_DEV
:
5819 (void) printf(gettext("status: One or more devices has "
5820 "been taken offline by the administrator.\n\tSufficient "
5821 "replicas exist for the pool to continue functioning in "
5822 "a\n\tdegraded state.\n"));
5823 (void) printf(gettext("action: Online the device using "
5824 "'zpool online' or replace the device with\n\t'zpool "
5828 case ZPOOL_STATUS_REMOVED_DEV
:
5829 (void) printf(gettext("status: One or more devices has "
5830 "been removed by the administrator.\n\tSufficient "
5831 "replicas exist for the pool to continue functioning in "
5832 "a\n\tdegraded state.\n"));
5833 (void) printf(gettext("action: Online the device using "
5834 "'zpool online' or replace the device with\n\t'zpool "
5838 case ZPOOL_STATUS_RESILVERING
:
5839 (void) printf(gettext("status: One or more devices is "
5840 "currently being resilvered. The pool will\n\tcontinue "
5841 "to function, possibly in a degraded state.\n"));
5842 (void) printf(gettext("action: Wait for the resilver to "
5846 case ZPOOL_STATUS_CORRUPT_DATA
:
5847 (void) printf(gettext("status: One or more devices has "
5848 "experienced an error resulting in data\n\tcorruption. "
5849 "Applications may be affected.\n"));
5850 (void) printf(gettext("action: Restore the file in question "
5851 "if possible. Otherwise restore the\n\tentire pool from "
5855 case ZPOOL_STATUS_CORRUPT_POOL
:
5856 (void) printf(gettext("status: The pool metadata is corrupted "
5857 "and the pool cannot be opened.\n"));
5858 zpool_explain_recover(zpool_get_handle(zhp
),
5859 zpool_get_name(zhp
), reason
, config
);
5862 case ZPOOL_STATUS_VERSION_OLDER
:
5863 (void) printf(gettext("status: The pool is formatted using a "
5864 "legacy on-disk format. The pool can\n\tstill be used, "
5865 "but some features are unavailable.\n"));
5866 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5867 "upgrade'. Once this is done, the\n\tpool will no longer "
5868 "be accessible on software that does not support\n\t"
5869 "feature flags.\n"));
5872 case ZPOOL_STATUS_VERSION_NEWER
:
5873 (void) printf(gettext("status: The pool has been upgraded to a "
5874 "newer, incompatible on-disk version.\n\tThe pool cannot "
5875 "be accessed on this system.\n"));
5876 (void) printf(gettext("action: Access the pool from a system "
5877 "running more recent software, or\n\trestore the pool from "
5881 case ZPOOL_STATUS_FEAT_DISABLED
:
5882 (void) printf(gettext("status: Some supported features are not "
5883 "enabled on the pool. The pool can\n\tstill be used, but "
5884 "some features are unavailable.\n"));
5885 (void) printf(gettext("action: Enable all features using "
5886 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5887 "longer be accessible by software that does not support\n\t"
5888 "the features. See zpool-features(5) for details.\n"));
5891 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
5892 (void) printf(gettext("status: The pool cannot be accessed on "
5893 "this system because it uses the\n\tfollowing feature(s) "
5894 "not supported on this system:\n"));
5895 zpool_print_unsup_feat(config
);
5896 (void) printf("\n");
5897 (void) printf(gettext("action: Access the pool from a system "
5898 "that supports the required feature(s),\n\tor restore the "
5899 "pool from backup.\n"));
5902 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
5903 (void) printf(gettext("status: The pool can only be accessed "
5904 "in read-only mode on this system. It\n\tcannot be "
5905 "accessed in read-write mode because it uses the "
5906 "following\n\tfeature(s) not supported on this system:\n"));
5907 zpool_print_unsup_feat(config
);
5908 (void) printf("\n");
5909 (void) printf(gettext("action: The pool cannot be accessed in "
5910 "read-write mode. Import the pool with\n"
5911 "\t\"-o readonly=on\", access the pool from a system that "
5912 "supports the\n\trequired feature(s), or restore the "
5913 "pool from backup.\n"));
5916 case ZPOOL_STATUS_FAULTED_DEV_R
:
5917 (void) printf(gettext("status: One or more devices are "
5918 "faulted in response to persistent errors.\n\tSufficient "
5919 "replicas exist for the pool to continue functioning "
5920 "in a\n\tdegraded state.\n"));
5921 (void) printf(gettext("action: Replace the faulted device, "
5922 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5925 case ZPOOL_STATUS_FAULTED_DEV_NR
:
5926 (void) printf(gettext("status: One or more devices are "
5927 "faulted in response to persistent errors. There are "
5928 "insufficient replicas for the pool to\n\tcontinue "
5930 (void) printf(gettext("action: Destroy and re-create the pool "
5931 "from a backup source. Manually marking the device\n"
5932 "\trepaired using 'zpool clear' may allow some data "
5933 "to be recovered.\n"));
5936 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
5937 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
5938 (void) printf(gettext("status: One or more devices are "
5939 "faulted in response to IO failures.\n"));
5940 (void) printf(gettext("action: Make sure the affected devices "
5941 "are connected, then run 'zpool clear'.\n"));
5944 case ZPOOL_STATUS_BAD_LOG
:
5945 (void) printf(gettext("status: An intent log record "
5946 "could not be read.\n"
5947 "\tWaiting for administrator intervention to fix the "
5948 "faulted pool.\n"));
5949 (void) printf(gettext("action: Either restore the affected "
5950 "device(s) and run 'zpool online',\n"
5951 "\tor ignore the intent log records by running "
5952 "'zpool clear'.\n"));
5955 case ZPOOL_STATUS_HOSTID_MISMATCH
:
5956 (void) printf(gettext("status: Mismatch between pool hostid "
5957 "and system hostid on imported pool.\n\tThis pool was "
5958 "previously imported into a system with a different "
5959 "hostid,\n\tand then was verbatim imported into this "
5961 (void) printf(gettext("action: Export this pool on all systems "
5962 "on which it is imported.\n"
5963 "\tThen import it to correct the mismatch.\n"));
5966 case ZPOOL_STATUS_ERRATA
:
5967 (void) printf(gettext("status: Errata #%d detected.\n"),
5971 case ZPOOL_ERRATA_NONE
:
5974 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
5975 (void) printf(gettext("action: To correct the issue "
5976 "run 'zpool scrub'.\n"));
5981 * All errata which allow the pool to be imported
5982 * must contain an action message.
5990 * The remaining errors can't actually be generated, yet.
5992 assert(reason
== ZPOOL_STATUS_OK
);
5996 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
5999 if (config
!= NULL
) {
6001 nvlist_t
**spares
, **l2cache
;
6002 uint_t nspares
, nl2cache
;
6003 pool_scan_stat_t
*ps
= NULL
;
6005 (void) nvlist_lookup_uint64_array(nvroot
,
6006 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6007 print_scan_status(ps
);
6009 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6010 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6011 if (cbp
->cb_namewidth
< 10)
6012 cbp
->cb_namewidth
= 10;
6014 (void) printf(gettext("config:\n\n"));
6015 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
6016 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6018 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6021 if (num_logs(nvroot
) > 0)
6022 print_logs(zhp
, cbp
, nvroot
);
6023 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6024 &l2cache
, &nl2cache
) == 0)
6025 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6027 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6028 &spares
, &nspares
) == 0)
6029 print_spares(zhp
, cbp
, spares
, nspares
);
6031 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6033 nvlist_t
*nverrlist
= NULL
;
6036 * If the approximate error count is small, get a
6037 * precise count by fetching the entire log and
6038 * uniquifying the results.
6040 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6041 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6046 while ((elem
= nvlist_next_nvpair(nverrlist
,
6051 nvlist_free(nverrlist
);
6053 (void) printf("\n");
6056 (void) printf(gettext("errors: No known data "
6058 else if (!cbp
->cb_verbose
)
6059 (void) printf(gettext("errors: %llu data "
6060 "errors, use '-v' for a list\n"),
6061 (u_longlong_t
)nerr
);
6063 print_error_log(zhp
);
6066 if (cbp
->cb_dedup_stats
)
6067 print_dedup_stats(config
);
6069 (void) printf(gettext("config: The configuration cannot be "
6077 * zpool status [-c CMD] [-gLPvx] [-T d|u] [pool] ... [interval [count]]
6079 * -c CMD For each vdev, run command CMD
6080 * -g Display guid for individual vdev name.
6081 * -L Follow links when resolving vdev path name.
6082 * -P Display full path for vdev name.
6083 * -v Display complete error logs
6084 * -x Display only pools with potential problems
6085 * -D Display dedup status (undocumented)
6086 * -T Display a timestamp in date(1) or Unix format
6088 * Describes the health status of all pools or some subset.
6091 zpool_do_status(int argc
, char **argv
)
6096 unsigned long count
= 0;
6097 status_cbdata_t cb
= { 0 };
6101 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6107 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6110 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6113 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6116 cb
.cb_verbose
= B_TRUE
;
6119 cb
.cb_explain
= B_TRUE
;
6122 cb
.cb_dedup_stats
= B_TRUE
;
6125 get_timestamp_arg(*optarg
);
6128 if (optopt
== 'c') {
6130 gettext("Missing CMD for -c\n"));
6133 gettext("invalid option '%c'\n"), optopt
);
6142 get_interval_count(&argc
, argv
, &interval
, &count
);
6145 cb
.cb_allpools
= B_TRUE
;
6147 cb
.cb_first
= B_TRUE
;
6148 cb
.cb_print_status
= B_TRUE
;
6151 if (timestamp_fmt
!= NODATE
)
6152 print_timestamp(timestamp_fmt
);
6155 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
6158 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6159 status_callback
, &cb
);
6161 if (cb
.vcdl
!= NULL
)
6162 free_vdev_cmd_data_list(cb
.vcdl
);
6164 if (argc
== 0 && cb
.cb_count
== 0)
6165 (void) fprintf(stderr
, gettext("no pools available\n"));
6166 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6167 (void) printf(gettext("all pools are healthy\n"));
6175 if (count
!= 0 && --count
== 0)
6178 (void) fsleep(interval
);
6184 typedef struct upgrade_cbdata
{
6187 uint64_t cb_version
;
6192 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6194 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6195 int *count
= (int *)unsupp_fs
;
6197 if (zfs_version
> ZPL_VERSION
) {
6198 (void) printf(gettext("%s (v%d) is not supported by this "
6199 "implementation of ZFS.\n"),
6200 zfs_get_name(zhp
), zfs_version
);
6204 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6212 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6216 uint64_t oldversion
;
6219 config
= zpool_get_config(zhp
, NULL
);
6220 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6223 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6224 assert(oldversion
< version
);
6226 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
6231 (void) fprintf(stderr
, gettext("Upgrade not performed due "
6232 "to %d unsupported filesystems (max v%d).\n"),
6233 unsupp_fs
, (int)ZPL_VERSION
);
6237 ret
= zpool_upgrade(zhp
, version
);
6241 if (version
>= SPA_VERSION_FEATURES
) {
6242 (void) printf(gettext("Successfully upgraded "
6243 "'%s' from version %llu to feature flags.\n"),
6244 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
6246 (void) printf(gettext("Successfully upgraded "
6247 "'%s' from version %llu to version %llu.\n"),
6248 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
6249 (u_longlong_t
)version
);
6256 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
6259 boolean_t firstff
= B_TRUE
;
6260 nvlist_t
*enabled
= zpool_get_features(zhp
);
6263 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6264 const char *fname
= spa_feature_table
[i
].fi_uname
;
6265 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6266 if (!nvlist_exists(enabled
, fguid
)) {
6268 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
6269 ret
= zpool_set_prop(zhp
, propname
,
6270 ZFS_FEATURE_ENABLED
);
6278 (void) printf(gettext("Enabled the "
6279 "following features on '%s':\n"),
6280 zpool_get_name(zhp
));
6283 (void) printf(gettext(" %s\n"), fname
);
6294 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
6296 upgrade_cbdata_t
*cbp
= arg
;
6299 boolean_t printnl
= B_FALSE
;
6302 config
= zpool_get_config(zhp
, NULL
);
6303 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6306 assert(SPA_VERSION_IS_SUPPORTED(version
));
6308 if (version
< cbp
->cb_version
) {
6309 cbp
->cb_first
= B_FALSE
;
6310 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6316 * If they did "zpool upgrade -a", then we could
6317 * be doing ioctls to different pools. We need
6318 * to log this history once to each pool, and bypass
6319 * the normal history logging that happens in main().
6321 (void) zpool_log_history(g_zfs
, history_str
);
6322 log_history
= B_FALSE
;
6325 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6327 ret
= upgrade_enable_all(zhp
, &count
);
6332 cbp
->cb_first
= B_FALSE
;
6338 (void) printf(gettext("\n"));
6345 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
6347 upgrade_cbdata_t
*cbp
= arg
;
6351 config
= zpool_get_config(zhp
, NULL
);
6352 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6355 assert(SPA_VERSION_IS_SUPPORTED(version
));
6357 if (version
< SPA_VERSION_FEATURES
) {
6358 if (cbp
->cb_first
) {
6359 (void) printf(gettext("The following pools are "
6360 "formatted with legacy version numbers and can\n"
6361 "be upgraded to use feature flags. After "
6362 "being upgraded, these pools\nwill no "
6363 "longer be accessible by software that does not "
6364 "support feature\nflags.\n\n"));
6365 (void) printf(gettext("VER POOL\n"));
6366 (void) printf(gettext("--- ------------\n"));
6367 cbp
->cb_first
= B_FALSE
;
6370 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
6371 zpool_get_name(zhp
));
6378 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
6380 upgrade_cbdata_t
*cbp
= arg
;
6384 config
= zpool_get_config(zhp
, NULL
);
6385 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6388 if (version
>= SPA_VERSION_FEATURES
) {
6390 boolean_t poolfirst
= B_TRUE
;
6391 nvlist_t
*enabled
= zpool_get_features(zhp
);
6393 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6394 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6395 const char *fname
= spa_feature_table
[i
].fi_uname
;
6396 if (!nvlist_exists(enabled
, fguid
)) {
6397 if (cbp
->cb_first
) {
6398 (void) printf(gettext("\nSome "
6399 "supported features are not "
6400 "enabled on the following pools. "
6401 "Once a\nfeature is enabled the "
6402 "pool may become incompatible with "
6403 "software\nthat does not support "
6405 "zpool-features(5) for "
6407 (void) printf(gettext("POOL "
6409 (void) printf(gettext("------"
6411 cbp
->cb_first
= B_FALSE
;
6415 (void) printf(gettext("%s\n"),
6416 zpool_get_name(zhp
));
6417 poolfirst
= B_FALSE
;
6420 (void) printf(gettext(" %s\n"), fname
);
6423 * If they did "zpool upgrade -a", then we could
6424 * be doing ioctls to different pools. We need
6425 * to log this history once to each pool, and bypass
6426 * the normal history logging that happens in main().
6428 (void) zpool_log_history(g_zfs
, history_str
);
6429 log_history
= B_FALSE
;
6438 upgrade_one(zpool_handle_t
*zhp
, void *data
)
6440 boolean_t printnl
= B_FALSE
;
6441 upgrade_cbdata_t
*cbp
= data
;
6442 uint64_t cur_version
;
6445 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
6446 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
6447 "Pool 'log' must be renamed using export and import"
6452 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
6453 if (cur_version
> cbp
->cb_version
) {
6454 (void) printf(gettext("Pool '%s' is already formatted "
6455 "using more current version '%llu'.\n\n"),
6456 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
6460 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
6461 (void) printf(gettext("Pool '%s' is already formatted "
6462 "using version %llu.\n\n"), zpool_get_name(zhp
),
6463 (u_longlong_t
)cbp
->cb_version
);
6467 if (cur_version
!= cbp
->cb_version
) {
6469 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6474 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6476 ret
= upgrade_enable_all(zhp
, &count
);
6482 } else if (cur_version
== SPA_VERSION
) {
6483 (void) printf(gettext("Pool '%s' already has all "
6484 "supported features enabled.\n"),
6485 zpool_get_name(zhp
));
6490 (void) printf(gettext("\n"));
6499 * zpool upgrade [-V version] <-a | pool ...>
6501 * With no arguments, display downrev'd ZFS pool available for upgrade.
6502 * Individual pools can be upgraded by specifying the pool, and '-a' will
6503 * upgrade all pools.
6506 zpool_do_upgrade(int argc
, char **argv
)
6509 upgrade_cbdata_t cb
= { 0 };
6511 boolean_t showversions
= B_FALSE
;
6512 boolean_t upgradeall
= B_FALSE
;
6517 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
6520 upgradeall
= B_TRUE
;
6523 showversions
= B_TRUE
;
6526 cb
.cb_version
= strtoll(optarg
, &end
, 10);
6528 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
6529 (void) fprintf(stderr
,
6530 gettext("invalid version '%s'\n"), optarg
);
6535 (void) fprintf(stderr
, gettext("missing argument for "
6536 "'%c' option\n"), optopt
);
6540 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6551 if (cb
.cb_version
== 0) {
6552 cb
.cb_version
= SPA_VERSION
;
6553 } else if (!upgradeall
&& argc
== 0) {
6554 (void) fprintf(stderr
, gettext("-V option is "
6555 "incompatible with other arguments\n"));
6560 if (upgradeall
|| argc
!= 0) {
6561 (void) fprintf(stderr
, gettext("-v option is "
6562 "incompatible with other arguments\n"));
6565 } else if (upgradeall
) {
6567 (void) fprintf(stderr
, gettext("-a option should not "
6568 "be used along with a pool name\n"));
6573 (void) printf(gettext("This system supports ZFS pool feature "
6578 (void) printf(gettext("The following features are "
6580 (void) printf(gettext("FEAT DESCRIPTION\n"));
6581 (void) printf("----------------------------------------------"
6582 "---------------\n");
6583 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6584 zfeature_info_t
*fi
= &spa_feature_table
[i
];
6586 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
6587 " (read-only compatible)" : "";
6589 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
6590 (void) printf(" %s\n", fi
->fi_desc
);
6592 (void) printf("\n");
6594 (void) printf(gettext("The following legacy versions are also "
6596 (void) printf(gettext("VER DESCRIPTION\n"));
6597 (void) printf("--- -----------------------------------------"
6598 "---------------\n");
6599 (void) printf(gettext(" 1 Initial ZFS version\n"));
6600 (void) printf(gettext(" 2 Ditto blocks "
6601 "(replicated metadata)\n"));
6602 (void) printf(gettext(" 3 Hot spares and double parity "
6604 (void) printf(gettext(" 4 zpool history\n"));
6605 (void) printf(gettext(" 5 Compression using the gzip "
6607 (void) printf(gettext(" 6 bootfs pool property\n"));
6608 (void) printf(gettext(" 7 Separate intent log devices\n"));
6609 (void) printf(gettext(" 8 Delegated administration\n"));
6610 (void) printf(gettext(" 9 refquota and refreservation "
6612 (void) printf(gettext(" 10 Cache devices\n"));
6613 (void) printf(gettext(" 11 Improved scrub performance\n"));
6614 (void) printf(gettext(" 12 Snapshot properties\n"));
6615 (void) printf(gettext(" 13 snapused property\n"));
6616 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6617 (void) printf(gettext(" 15 user/group space accounting\n"));
6618 (void) printf(gettext(" 16 stmf property support\n"));
6619 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6620 (void) printf(gettext(" 18 Snapshot user holds\n"));
6621 (void) printf(gettext(" 19 Log device removal\n"));
6622 (void) printf(gettext(" 20 Compression using zle "
6623 "(zero-length encoding)\n"));
6624 (void) printf(gettext(" 21 Deduplication\n"));
6625 (void) printf(gettext(" 22 Received properties\n"));
6626 (void) printf(gettext(" 23 Slim ZIL\n"));
6627 (void) printf(gettext(" 24 System attributes\n"));
6628 (void) printf(gettext(" 25 Improved scrub stats\n"));
6629 (void) printf(gettext(" 26 Improved snapshot deletion "
6631 (void) printf(gettext(" 27 Improved snapshot creation "
6633 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6634 (void) printf(gettext("\nFor more information on a particular "
6635 "version, including supported releases,\n"));
6636 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6637 } else if (argc
== 0 && upgradeall
) {
6638 cb
.cb_first
= B_TRUE
;
6639 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
6640 if (ret
== 0 && cb
.cb_first
) {
6641 if (cb
.cb_version
== SPA_VERSION
) {
6642 (void) printf(gettext("All pools are already "
6643 "formatted using feature flags.\n\n"));
6644 (void) printf(gettext("Every feature flags "
6645 "pool already has all supported features "
6648 (void) printf(gettext("All pools are already "
6649 "formatted with version %llu or higher.\n"),
6650 (u_longlong_t
)cb
.cb_version
);
6653 } else if (argc
== 0) {
6654 cb
.cb_first
= B_TRUE
;
6655 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
6659 (void) printf(gettext("All pools are formatted "
6660 "using feature flags.\n\n"));
6662 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6663 "for a list of available legacy versions.\n"));
6666 cb
.cb_first
= B_TRUE
;
6667 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
6671 (void) printf(gettext("Every feature flags pool has "
6672 "all supported features enabled.\n"));
6674 (void) printf(gettext("\n"));
6677 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
6684 typedef struct hist_cbdata
{
6691 * Print out the command history for a specific pool.
6694 get_history_one(zpool_handle_t
*zhp
, void *data
)
6700 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
6702 cb
->first
= B_FALSE
;
6704 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
6706 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
6709 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
6710 &records
, &numrecords
) == 0);
6711 for (i
= 0; i
< numrecords
; i
++) {
6712 nvlist_t
*rec
= records
[i
];
6715 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
6719 tsec
= fnvlist_lookup_uint64(records
[i
],
6721 (void) localtime_r(&tsec
, &t
);
6722 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
6725 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
6726 (void) printf("%s %s", tbuf
,
6727 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
6728 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
6730 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
6733 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
6734 (void) printf("%s unrecognized record:\n",
6736 dump_nvlist(rec
, 4);
6739 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
6740 zfs_history_event_names
[ievent
],
6741 (longlong_t
)fnvlist_lookup_uint64(
6742 rec
, ZPOOL_HIST_TXG
),
6743 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
6744 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
6747 (void) printf("%s [txg:%lld] %s", tbuf
,
6748 (longlong_t
)fnvlist_lookup_uint64(
6749 rec
, ZPOOL_HIST_TXG
),
6750 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
6751 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
6752 (void) printf(" %s (%llu)",
6753 fnvlist_lookup_string(rec
,
6755 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
6758 (void) printf(" %s", fnvlist_lookup_string(rec
,
6759 ZPOOL_HIST_INT_STR
));
6760 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
6763 (void) printf("%s ioctl %s\n", tbuf
,
6764 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
6765 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
6766 (void) printf(" input:\n");
6767 dump_nvlist(fnvlist_lookup_nvlist(rec
,
6768 ZPOOL_HIST_INPUT_NVL
), 8);
6770 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
6771 (void) printf(" output:\n");
6772 dump_nvlist(fnvlist_lookup_nvlist(rec
,
6773 ZPOOL_HIST_OUTPUT_NVL
), 8);
6778 (void) printf("%s unrecognized record:\n", tbuf
);
6779 dump_nvlist(rec
, 4);
6783 (void) printf("\n");
6786 (void) printf(" [");
6787 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
6788 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
6789 struct passwd
*pwd
= getpwuid(who
);
6790 (void) printf("user %d ", (int)who
);
6792 (void) printf("(%s) ", pwd
->pw_name
);
6794 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
6795 (void) printf("on %s",
6796 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
6798 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
6799 (void) printf(":%s",
6800 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
6804 (void) printf("\n");
6806 (void) printf("\n");
6813 * zpool history <pool>
6815 * Displays the history of commands that modified pools.
6818 zpool_do_history(int argc
, char **argv
)
6820 hist_cbdata_t cbdata
= { 0 };
6824 cbdata
.first
= B_TRUE
;
6826 while ((c
= getopt(argc
, argv
, "li")) != -1) {
6829 cbdata
.longfmt
= B_TRUE
;
6832 cbdata
.internal
= B_TRUE
;
6835 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6843 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
6846 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
6847 (void) fprintf(stderr
, gettext("no pools available\n"));
6854 typedef struct ev_opts
{
6862 zpool_do_events_short(nvlist_t
*nvl
)
6864 char ctime_str
[26], str
[32], *ptr
;
6868 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
6869 memset(str
, ' ', 32);
6870 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
6871 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
6872 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
6873 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
6874 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
6875 (void) printf(gettext("%s "), str
);
6877 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
6878 (void) printf(gettext("%s\n"), ptr
);
6882 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
6886 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
6887 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
6889 data_type_t type
= nvpair_type(nvp
);
6890 const char *name
= nvpair_name(nvp
);
6900 printf(gettext("%*s%s = "), depth
, "", name
);
6903 case DATA_TYPE_BOOLEAN
:
6904 printf(gettext("%s"), "1");
6907 case DATA_TYPE_BOOLEAN_VALUE
:
6908 (void) nvpair_value_boolean_value(nvp
, &b
);
6909 printf(gettext("%s"), b
? "1" : "0");
6912 case DATA_TYPE_BYTE
:
6913 (void) nvpair_value_byte(nvp
, &i8
);
6914 printf(gettext("0x%x"), i8
);
6917 case DATA_TYPE_INT8
:
6918 (void) nvpair_value_int8(nvp
, (void *)&i8
);
6919 printf(gettext("0x%x"), i8
);
6922 case DATA_TYPE_UINT8
:
6923 (void) nvpair_value_uint8(nvp
, &i8
);
6924 printf(gettext("0x%x"), i8
);
6927 case DATA_TYPE_INT16
:
6928 (void) nvpair_value_int16(nvp
, (void *)&i16
);
6929 printf(gettext("0x%x"), i16
);
6932 case DATA_TYPE_UINT16
:
6933 (void) nvpair_value_uint16(nvp
, &i16
);
6934 printf(gettext("0x%x"), i16
);
6937 case DATA_TYPE_INT32
:
6938 (void) nvpair_value_int32(nvp
, (void *)&i32
);
6939 printf(gettext("0x%x"), i32
);
6942 case DATA_TYPE_UINT32
:
6943 (void) nvpair_value_uint32(nvp
, &i32
);
6944 printf(gettext("0x%x"), i32
);
6947 case DATA_TYPE_INT64
:
6948 (void) nvpair_value_int64(nvp
, (void *)&i64
);
6949 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6952 case DATA_TYPE_UINT64
:
6953 (void) nvpair_value_uint64(nvp
, &i64
);
6955 * translate vdev state values to readable
6956 * strings to aide zpool events consumers
6959 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
6961 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
6962 printf(gettext("\"%s\" (0x%llx)"),
6963 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
6966 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6970 case DATA_TYPE_HRTIME
:
6971 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
6972 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6975 case DATA_TYPE_STRING
:
6976 (void) nvpair_value_string(nvp
, &str
);
6977 printf(gettext("\"%s\""), str
? str
: "<NULL>");
6980 case DATA_TYPE_NVLIST
:
6981 printf(gettext("(embedded nvlist)\n"));
6982 (void) nvpair_value_nvlist(nvp
, &cnv
);
6983 zpool_do_events_nvprint(cnv
, depth
+ 8);
6984 printf(gettext("%*s(end %s)"), depth
, "", name
);
6987 case DATA_TYPE_NVLIST_ARRAY
: {
6991 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
6992 printf(gettext("(%d embedded nvlists)\n"), nelem
);
6993 for (i
= 0; i
< nelem
; i
++) {
6994 printf(gettext("%*s%s[%d] = %s\n"),
6995 depth
, "", name
, i
, "(embedded nvlist)");
6996 zpool_do_events_nvprint(val
[i
], depth
+ 8);
6997 printf(gettext("%*s(end %s[%i])\n"),
6998 depth
, "", name
, i
);
7000 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7004 case DATA_TYPE_INT8_ARRAY
: {
7008 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7009 for (i
= 0; i
< nelem
; i
++)
7010 printf(gettext("0x%x "), val
[i
]);
7015 case DATA_TYPE_UINT8_ARRAY
: {
7019 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7020 for (i
= 0; i
< nelem
; i
++)
7021 printf(gettext("0x%x "), val
[i
]);
7026 case DATA_TYPE_INT16_ARRAY
: {
7030 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7031 for (i
= 0; i
< nelem
; i
++)
7032 printf(gettext("0x%x "), val
[i
]);
7037 case DATA_TYPE_UINT16_ARRAY
: {
7041 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7042 for (i
= 0; i
< nelem
; i
++)
7043 printf(gettext("0x%x "), val
[i
]);
7048 case DATA_TYPE_INT32_ARRAY
: {
7052 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7053 for (i
= 0; i
< nelem
; i
++)
7054 printf(gettext("0x%x "), val
[i
]);
7059 case DATA_TYPE_UINT32_ARRAY
: {
7063 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7064 for (i
= 0; i
< nelem
; i
++)
7065 printf(gettext("0x%x "), val
[i
]);
7070 case DATA_TYPE_INT64_ARRAY
: {
7074 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7075 for (i
= 0; i
< nelem
; i
++)
7076 printf(gettext("0x%llx "),
7077 (u_longlong_t
)val
[i
]);
7082 case DATA_TYPE_UINT64_ARRAY
: {
7086 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7087 for (i
= 0; i
< nelem
; i
++)
7088 printf(gettext("0x%llx "),
7089 (u_longlong_t
)val
[i
]);
7094 case DATA_TYPE_STRING_ARRAY
: {
7098 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7099 for (i
= 0; i
< nelem
; i
++)
7100 printf(gettext("\"%s\" "),
7101 str
[i
] ? str
[i
] : "<NULL>");
7106 case DATA_TYPE_BOOLEAN_ARRAY
:
7107 case DATA_TYPE_BYTE_ARRAY
:
7108 case DATA_TYPE_DOUBLE
:
7109 case DATA_TYPE_UNKNOWN
:
7110 printf(gettext("<unknown>"));
7114 printf(gettext("\n"));
7119 zpool_do_events_next(ev_opts_t
*opts
)
7122 int zevent_fd
, ret
, dropped
;
7124 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7125 VERIFY(zevent_fd
>= 0);
7127 if (!opts
->scripted
)
7128 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7131 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7132 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7133 if (ret
|| nvl
== NULL
)
7137 (void) printf(gettext("dropped %d events\n"), dropped
);
7139 zpool_do_events_short(nvl
);
7141 if (opts
->verbose
) {
7142 zpool_do_events_nvprint(nvl
, 8);
7143 printf(gettext("\n"));
7145 (void) fflush(stdout
);
7150 VERIFY(0 == close(zevent_fd
));
7156 zpool_do_events_clear(ev_opts_t
*opts
)
7160 ret
= zpool_events_clear(g_zfs
, &count
);
7162 (void) printf(gettext("cleared %d events\n"), count
);
7168 * zpool events [-vfc]
7170 * Displays events logs by ZFS.
7173 zpool_do_events(int argc
, char **argv
)
7175 ev_opts_t opts
= { 0 };
7180 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7195 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7204 ret
= zpool_do_events_clear(&opts
);
7206 ret
= zpool_do_events_next(&opts
);
7212 get_callback(zpool_handle_t
*zhp
, void *data
)
7214 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
7215 char value
[MAXNAMELEN
];
7216 zprop_source_t srctype
;
7219 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
7222 * Skip the special fake placeholder. This will also skip
7223 * over the name property when 'all' is specified.
7225 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
7226 pl
== cbp
->cb_proplist
)
7229 if (pl
->pl_prop
== ZPROP_INVAL
&&
7230 (zpool_prop_feature(pl
->pl_user_prop
) ||
7231 zpool_prop_unsupported(pl
->pl_user_prop
))) {
7232 srctype
= ZPROP_SRC_LOCAL
;
7234 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
7235 value
, sizeof (value
)) == 0) {
7236 zprop_print_one_property(zpool_get_name(zhp
),
7237 cbp
, pl
->pl_user_prop
, value
, srctype
,
7241 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
7242 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
7245 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
7246 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
7254 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7256 * -H Scripted mode. Don't display headers, and separate properties
7258 * -o List of columns to display. Defaults to
7259 * "name,property,value,source".
7260 * -p Display values in parsable (exact) format.
7262 * Get properties of pools in the system. Output space statistics
7263 * for each one as well as other attributes.
7266 zpool_do_get(int argc
, char **argv
)
7268 zprop_get_cbdata_t cb
= { 0 };
7269 zprop_list_t fake_name
= { 0 };
7274 cb
.cb_first
= B_TRUE
;
7277 * Set up default columns and sources.
7279 cb
.cb_sources
= ZPROP_SRC_ALL
;
7280 cb
.cb_columns
[0] = GET_COL_NAME
;
7281 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7282 cb
.cb_columns
[2] = GET_COL_VALUE
;
7283 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7284 cb
.cb_type
= ZFS_TYPE_POOL
;
7287 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
7290 cb
.cb_literal
= B_TRUE
;
7293 cb
.cb_scripted
= B_TRUE
;
7296 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
7298 while (*optarg
!= '\0') {
7299 static char *col_subopts
[] =
7300 { "name", "property", "value", "source",
7303 if (i
== ZFS_GET_NCOLS
) {
7304 (void) fprintf(stderr
, gettext("too "
7305 "many fields given to -o "
7310 switch (getsubopt(&optarg
, col_subopts
,
7313 cb
.cb_columns
[i
++] = GET_COL_NAME
;
7316 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
7319 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
7322 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
7326 (void) fprintf(stderr
,
7327 gettext("\"all\" conflicts "
7328 "with specific fields "
7329 "given to -o option\n"));
7332 cb
.cb_columns
[0] = GET_COL_NAME
;
7333 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7334 cb
.cb_columns
[2] = GET_COL_VALUE
;
7335 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7339 (void) fprintf(stderr
,
7340 gettext("invalid column name "
7347 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7357 (void) fprintf(stderr
, gettext("missing property "
7362 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
7363 ZFS_TYPE_POOL
) != 0)
7369 if (cb
.cb_proplist
!= NULL
) {
7370 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
7371 fake_name
.pl_width
= strlen(gettext("NAME"));
7372 fake_name
.pl_next
= cb
.cb_proplist
;
7373 cb
.cb_proplist
= &fake_name
;
7376 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
7379 if (cb
.cb_proplist
== &fake_name
)
7380 zprop_free_list(fake_name
.pl_next
);
7382 zprop_free_list(cb
.cb_proplist
);
7387 typedef struct set_cbdata
{
7390 boolean_t cb_any_successful
;
7394 set_callback(zpool_handle_t
*zhp
, void *data
)
7397 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
7399 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
7402 cb
->cb_any_successful
= B_TRUE
;
7408 zpool_do_set(int argc
, char **argv
)
7410 set_cbdata_t cb
= { 0 };
7413 if (argc
> 1 && argv
[1][0] == '-') {
7414 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7420 (void) fprintf(stderr
, gettext("missing property=value "
7426 (void) fprintf(stderr
, gettext("missing pool name\n"));
7431 (void) fprintf(stderr
, gettext("too many pool names\n"));
7435 cb
.cb_propname
= argv
[1];
7436 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
7437 if (cb
.cb_value
== NULL
) {
7438 (void) fprintf(stderr
, gettext("missing value in "
7439 "property=value argument\n"));
7443 *(cb
.cb_value
) = '\0';
7446 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
7453 find_command_idx(char *command
, int *idx
)
7457 for (i
= 0; i
< NCOMMAND
; i
++) {
7458 if (command_table
[i
].name
== NULL
)
7461 if (strcmp(command
, command_table
[i
].name
) == 0) {
7470 main(int argc
, char **argv
)
7476 (void) setlocale(LC_ALL
, "");
7477 (void) textdomain(TEXT_DOMAIN
);
7480 dprintf_setup(&argc
, argv
);
7485 * Make sure the user has specified some command.
7488 (void) fprintf(stderr
, gettext("missing command\n"));
7497 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
7500 if ((g_zfs
= libzfs_init()) == NULL
) {
7501 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7505 libzfs_print_on_error(g_zfs
, B_TRUE
);
7507 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7510 * Run the appropriate command.
7512 if (find_command_idx(cmdname
, &i
) == 0) {
7513 current_command
= &command_table
[i
];
7514 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7515 } else if (strchr(cmdname
, '=')) {
7516 verify(find_command_idx("set", &i
) == 0);
7517 current_command
= &command_table
[i
];
7518 ret
= command_table
[i
].func(argc
, argv
);
7519 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
7521 * 'freeze' is a vile debugging abomination, so we treat
7525 int fd
= open(ZFS_DEV
, O_RDWR
);
7526 (void) strlcpy((void *)buf
, argv
[2], sizeof (buf
));
7527 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
7529 (void) fprintf(stderr
, gettext("unrecognized "
7530 "command '%s'\n"), cmdname
);
7535 if (ret
== 0 && log_history
)
7536 (void) zpool_log_history(g_zfs
, history_str
);
7541 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7542 * for the purposes of running ::findleaks.
7544 if (getenv("ZFS_ABORT") != NULL
) {
7545 (void) printf("dumping core by request\n");